Commit 5290fee7 authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

small refactorings

parent 93a3868b
......@@ -62,7 +62,7 @@ __all__ = ('ErrorCode',
'is_warning',
'has_errors',
'only_errors',
'adjust_error_locations',
'add_source_locations',
'canonical_error_strings',
'NO_ERROR',
'NOTICE',
......@@ -377,7 +377,7 @@ def only_errors(messages: Iterable[Error], level: int = ERROR) -> Iterator[Error
#######################################################################
def adjust_error_locations(errors: List[Error], source_mapping: SourceMapFunc):
def add_source_locations(errors: List[Error], source_mapping: SourceMapFunc):
"""Adds (or adjusts) line and column numbers of error messages inplace.
Args:
......
......@@ -591,7 +591,7 @@ from typing import Callable, cast, Iterator, Sequence, List, Set, Union, \
from DHParser.configuration import get_config_value, ALLOWED_PRESET_VALUES
from DHParser.error import Error, ErrorCode, ERROR, PARSER_STOPPED_BEFORE_END, \
adjust_error_locations
add_source_locations
from DHParser.preprocess import SourceMapFunc, SourceLocation, gen_neutral_srcmap_func
from DHParser.stringview import StringView # , real_indices
from DHParser.toolkit import re, cython, linebreaks, line_col, JSONnull, \
......@@ -2794,7 +2794,7 @@ class RootNode(Node):
if id(node) in self.error_nodes:
self.error_nodes[id(self)] = self.error_nodes[id(node)]
if self.source:
adjust_error_locations(self.errors, self.source_mapping)
add_source_locations(self.errors, self.source_mapping)
return self
def add_error(self, node: Optional[Node], error: Error) -> 'RootNode':
......@@ -2832,7 +2832,7 @@ class RootNode(Node):
if node.pos == error.pos:
self.error_positions.setdefault(error.pos, set()).add(id(node))
if self.source:
adjust_error_locations([error], self.source_mapping)
add_source_locations([error], self.source_mapping)
self.errors.append(error)
self.error_flag = max(self.error_flag, error.code)
return self
......
......@@ -40,7 +40,7 @@ import time
from typing import Dict, List, Union, Deque, cast
from DHParser.configuration import get_config_value
from DHParser.error import Error, is_error, adjust_error_locations, PARSER_LOOKAHEAD_MATCH_ONLY, \
from DHParser.error import Error, is_error, add_source_locations, PARSER_LOOKAHEAD_MATCH_ONLY, \
PARSER_LOOKAHEAD_FAILURE_ONLY, MANDATORY_CONTINUATION_AT_EOF, AUTORETRIEVED_SYMBOL_NOT_CLEARED
from DHParser.log import is_logging, clear_logs, local_log_dir, log_parsing_history
from DHParser.parse import Lookahead
......@@ -440,7 +440,7 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report='REPORT'
errors = [] # type: List[Error]
if is_error(cst.error_flag) and not lookahead_artifact(cst):
errors = [e for e in cst.errors_sorted if e.code not in POSSIBLE_ARTIFACTS]
adjust_error_locations(errors, source_mapper)
add_source_locations(errors, source_mapper)
errata.append('Match test "%s" for parser "%s" failed:'
'\nExpr.: %s\n\n%s\n\n' %
(test_name, parser_name, md_codeblock(test_code),
......@@ -459,7 +459,7 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report='REPORT'
ast_errors = [e for e in ast.errors if e not in old_errors]
ast_errors.sort(key=lambda e: e.pos)
if is_error(max(e.code for e in ast_errors) if ast_errors else 0):
adjust_error_locations(ast_errors, source_mapper)
add_source_locations(ast_errors, source_mapper)
if ast_errors:
if errata: errata[-1] = errata[-1].rstrip('\n')
ast_errors.append('\n')
......@@ -541,7 +541,7 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report='REPORT'
with local_log_dir('./LOGS'):
log_parsing_history(parser, "fail_%s_%s.log" % (parser_name, test_name))
if cst.error_flag:
adjust_error_locations(cst.errors, source_mapper)
add_source_locations(cst.errors, source_mapper)
tests.setdefault('__msg__', {})[test_name] = \
"\n".join(str(e) for e in cst.errors_sorted)
if verbose:
......
......@@ -31,7 +31,7 @@ try:
except ImportError:
import re
from DHParser.error import Error, ERROR, adjust_error_locations
from DHParser.error import Error, ERROR, add_source_locations
from DHParser.preprocess import gen_neutral_srcmap_func
from DHParser.toolkit import linebreaks, line_col
......@@ -76,11 +76,11 @@ class TestErrorSupport:
def test_boundary_cases(self):
err = Error('Error-Test', 1, ERROR)
source_mapping = gen_neutral_srcmap_func(' ')
adjust_error_locations([err], source_mapping)
add_source_locations([err], source_mapping)
err = Error('Error-Test', 2, ERROR)
try:
adjust_error_locations([err], source_mapping)
add_source_locations([err], source_mapping)
assert False, "Error-location outside text. ValueError was expected but not raised"
except ValueError:
pass
......
......@@ -31,7 +31,7 @@ sys.path.append(os.path.abspath(os.path.join(scriptpath, '..')))
from DHParser.configuration import get_config_value, set_config_value
from DHParser.toolkit import compile_python_object, re
from DHParser.log import is_logging, log_ST, log_parsing_history, start_logging
from DHParser.error import Error, is_error, adjust_error_locations, MANDATORY_CONTINUATION, \
from DHParser.error import Error, is_error, add_source_locations, MANDATORY_CONTINUATION, \
MALFORMED_ERROR_STRING, MANDATORY_CONTINUATION_AT_EOF, RESUME_NOTICE, PARSER_STOPPED_BEFORE_END, \
PARSER_NEVER_TOUCHES_DOCUMENT, CAPTURE_DROPPED_CONTENT_WARNING
from DHParser.parse import ParserError, Parser, Grammar, Forward, TKN, ZeroOrMore, RE, \
......@@ -1375,7 +1375,7 @@ def next_valid_letter(text, start, end):
gr = copy.deepcopy(get_ebnf_grammar())
resume_notices_on(gr)
cst = gr(EBNF_with_Errors)
adjust_error_locations(cst.errors, gen_neutral_srcmap_func(EBNF_with_Errors))
add_source_locations(cst.errors, gen_neutral_srcmap_func(EBNF_with_Errors))
locations = []
for error in cst.errors_sorted:
locations.append((error.line, error.column))
......
Markdown is supported
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