Commit ec729baa authored by di68kap's avatar di68kap
Browse files

error.py: refactoring moving text services to toolkit.py

parent 407ddbe6
{
"python.pythonPath": "/usr/bin/python3",
"python.pythonPath": "C:\\Users\\di68kap\\AppData\\Local\\Programs\\Python\\Python38\\python.exe",
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.testing.nosetestArgs": [
......
......@@ -5,9 +5,4 @@
import cython
@cython.locals(i=cython.int)
cpdef linebreaks(test)
@cython.locals(line=cython.int, column=cython.int)
cpdef line_col(lbreaks, pos)
......@@ -35,9 +35,7 @@ the string representations of the error objects. For example::
print("There have been warnings, but no errors.")
"""
import bisect
from typing import Iterable, Iterator, Union, Tuple, List
from typing import Iterable, Iterator, Union, List
from DHParser.preprocess import SourceMapFunc
from DHParser.stringview import StringView
......@@ -50,10 +48,10 @@ __all__ = ('ErrorCode',
'is_warning',
'has_errors',
'only_errors',
'linebreaks',
'line_col',
'adjust_error_locations')
from DHParser.toolkit import linebreaks, line_col
class ErrorCode(int):
pass
......@@ -212,33 +210,6 @@ def only_errors(messages: Iterable[Error], level: int = Error.ERROR) -> Iterator
#######################################################################
def linebreaks(text: Union[StringView, str]) -> List[int]:
"""
Returns a list of indices all line breaks in the text.
"""
lbr = [-1]
i = text.find('\n', 0)
while i >= 0:
lbr.append(i)
i = text.find('\n', i + 1)
lbr.append(len(text))
return lbr
def line_col(lbreaks: List[int], pos: int) -> Tuple[int, int]:
"""
Returns the position within a text as (line, column)-tuple based
on a list of all line breaks, including -1 and EOF.
"""
if not lbreaks and pos >= 0:
return 0, pos
if pos < 0 or pos > lbreaks[-1]: # one character behind EOF is still an allowed position!
raise ValueError('Position %i outside text of length %s !' % (pos, lbreaks[-1]))
line = bisect.bisect_left(lbreaks, pos)
column = pos - lbreaks[line - 1]
return line, column
def adjust_error_locations(errors: List[Error],
original_text: Union[StringView, str],
source_mapping: SourceMapFunc = lambda i: i):
......
......@@ -36,15 +36,14 @@ from typing import Callable, cast, List, Tuple, Set, Dict, \
DefaultDict, Union, Optional, Any
from DHParser.configuration import get_config_value
from DHParser.error import Error, linebreaks, line_col
from DHParser.error import Error
from DHParser.log import CallItem, HistoryRecord
from DHParser.preprocess import BEGIN_TOKEN, END_TOKEN, RX_TOKEN_NAME
from DHParser.stringview import StringView, EMPTY_STRING_VIEW
from DHParser.syntaxtree import ChildrenType, Node, RootNode, WHITESPACE_PTYPE, \
TOKEN_PTYPE, ZOMBIE_TAG, EMPTY_NODE, ResultType
from DHParser.toolkit import sane_parser_name, escape_control_characters, re, cython, \
abbreviate_middle, RX_NEVER_MATCH, RxPatternType
abbreviate_middle, RX_NEVER_MATCH, RxPatternType, linebreaks, line_col
__all__ = ('ParserError',
'ApplyFunc',
......
......@@ -32,9 +32,9 @@ from typing import Callable, cast, Iterator, Sequence, List, Set, Union, Tuple,
from DHParser.configuration import SERIALIZATIONS, XML_SERIALIZATION, SXPRESSION_SERIALIZATION, \
COMPACT_SERIALIZATION, JSON_SERIALIZATION, SMART_SERIALIZATION, get_config_value
from DHParser.error import Error, ErrorCode, linebreaks, line_col
from DHParser.error import Error, ErrorCode
from DHParser.stringview import StringView # , real_indices
from DHParser.toolkit import re, cython
from DHParser.toolkit import re, cython, linebreaks, line_col
__all__ = ('WHITESPACE_PTYPE',
'TOKEN_PTYPE',
......
......@@ -4,4 +4,14 @@
#cython: c_string_encoding=utf-8
# cpdef issubtype(subtype, base_type)
cpdef isgenerictype(t)
\ No newline at end of file
cpdef isgenerictype(t)
@cython.locals(i=cython.int)
cpdef linebreaks(text)
@cython.locals(line=cython.int, column=cython.int)
cpdef line_col(lbreaks, pos)
@cython.locals(line=cython.int, column=cython.int, i=cython.int)
cpdef text_pos(text, line, column, lbreaks)
......@@ -25,19 +25,22 @@ functions that are very generic.
"""
import ast
import bisect
import hashlib
import io
import multiprocessing
import os
import threading
from DHParser import StringView
try:
import regex as re
except ImportError:
import re
import sys
import typing
from typing import Any, Iterable, Sequence, Set, Union, Dict
from typing import Any, Iterable, Sequence, Set, Union, Dict, List, Tuple
try:
import cython
......@@ -405,6 +408,63 @@ def compile_python_object(python_src: str, catch_obj_regex="") -> Any:
return namespace
#######################################################################
#
# text services
#
#######################################################################
@cython.locals(i=cython.int)
def linebreaks(text: Union[StringView, str]) -> List[int]:
"""
Returns a list of indices all line breaks in the text.
"""
lbr = [-1]
i = text.find('\n', 0)
while i >= 0:
lbr.append(i)
i = text.find('\n', i + 1)
lbr.append(len(text))
return lbr
@cython.locals(line=cython.int, column=cython.int, pos=cython.int)
def line_col(lbreaks: List[int], pos: int) -> Tuple[int, int]:
"""
Returns the position within a text as (line, column)-tuple based
on a list of all line breaks, including -1 and EOF.
"""
if not lbreaks and pos >= 0:
return 0, pos
if pos < 0 or pos > lbreaks[-1]: # one character behind EOF is still an allowed position!
raise ValueError('Position %i outside text of length %s !' % (pos, lbreaks[-1]))
line = bisect.bisect_left(lbreaks, pos)
column = pos - lbreaks[line - 1]
return line, column
@cython.locals(line=cython.int, column=cython.int, i=cython.int)
def text_pos(text: Union[StringView, str],
line: int, column: int,
lbreaks: List[int] = []) -> int:
"""
Returns the text-position for a given line and column or -1 if the line
and column exceed the size of the text.
"""
if lbreaks:
try:
return lbreaks[line] + column - 1
except IndexError:
return -1
else:
i = 0
while line > 0 and i >= 0:
i = text.find('\n', i + 1)
line -= 1
return i + column - 1
#######################################################################
#
# smart lists and multi-keyword tables
......
......@@ -26,12 +26,12 @@ Grammar-object.
from typing import Tuple, Optional, List, Iterable, Union
from DHParser.error import Error, line_col
from DHParser.error import Error
from DHParser.stringview import StringView
from DHParser.syntaxtree import Node, REGEXP_PTYPE, TOKEN_PTYPE, WHITESPACE_PTYPE, ZOMBIE_TAG
from DHParser.log import freeze_callstack, HistoryRecord
from DHParser.parse import Grammar, Parser, ParserError, ParseFunc
from DHParser.toolkit import cython
from DHParser.toolkit import cython, line_col
__all__ = ('trace_history', 'all_descendants', 'set_tracer',
'resume_notices_on')
......
......@@ -31,7 +31,8 @@ try:
except ImportError:
import re
from DHParser.error import linebreaks, line_col
from DHParser.toolkit import linebreaks, line_col
class TestErrorSupport:
def mini_suite(self, s, lbreaks, offset):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment