Commit 909142bd authored by eckhart's avatar eckhart

- type checking errors (mypy) corrected

parent eaa52b6f
......@@ -155,7 +155,7 @@ def error_str(messages: Iterable[Error]) -> str:
Returns all true errors (i.e. not just warnings) from the
`messages` as a concatenated multiline string.
"""
return '\n\n'.join(str(m) for m in messages if is_error(m.level))
return '\n\n'.join(str(m) for m in messages if is_error(m.level))
def grammar_instance(grammar_representation) -> Tuple[Grammar, str]:
......@@ -230,7 +230,7 @@ def raw_compileEBNF(ebnf_src: str, branding="DSL") -> EBNFCompiler:
CompilationError if any errors occurred during compilation
"""
grammar = get_ebnf_grammar()
compiler = get_ebnf_compiler(branding , ebnf_src)
compiler = get_ebnf_compiler(branding, ebnf_src)
transformer = get_ebnf_transformer()
compileDSL(ebnf_src, nil_preprocessor, grammar, transformer, compiler)
return compiler
......@@ -295,6 +295,7 @@ def load_compiler_suite(compiler_suite: str) -> \
global RX_SECTION_MARKER
assert isinstance(compiler_suite, str)
source = load_if_file(compiler_suite)
imports = DHPARSER_IMPORTS
if is_python_code(compiler_suite):
try:
intro, imports, preprocessor_py, parser_py, ast_py, compiler_py, outro = \
......
......@@ -767,7 +767,7 @@ class EBNFCompiler(Compiler):
'and not a %s.') % (prefix, str(arg.parser)))
return str(arg.result)
if str(arg) in self.directives['filter']:
custom_args = ['filter=%s' % self.directives['filter'][str(arg)]]
custom_args = ['rfilter=%s' % self.directives['filter'][str(arg)]]
self.variables.add(str(arg)) # cast(str, arg.result)
elif len(node.children) > 2:
......
This diff is collapsed.
......@@ -20,7 +20,7 @@ import collections.abc
import copy
import os
from functools import partial
from typing import Any, Callable, cast, Iterator, List, Union, Tuple, Hashable
from typing import Any, Callable, cast, Iterator, List, Union, Tuple, Hashable, Optional
from DHParser.error import Error, linebreaks, line_col
from DHParser.stringview import StringView
......@@ -60,6 +60,9 @@ class ParserBase:
def __str__(self):
return self.name + (' = ' if self.name else '') + repr(self)
def __call__(self, text: StringView) -> Tuple[Optional['Node'], StringView]:
return None, text
@property
def name(self):
return self._name
......@@ -72,6 +75,15 @@ class ParserBase:
def repr(self) -> str:
return self.name if self.name else repr(self)
def reset(self):
pass
def grammar(self) -> 'Grammar':
return None
def apply(self, func: Callable) -> bool:
return False
WHITESPACE_PTYPE = ':Whitespace'
TOKEN_PTYPE = ':Token'
......@@ -152,7 +164,7 @@ class Node(collections.abc.Sized):
"""
Represents a node in the concrete or abstract syntax tree.
Attributes:
Attributes and Properties:
tag_name (str): The name of the node, which is either its
parser's name or, if that is empty, the parser's class name
result (str or tuple): The result of the parser which
......@@ -319,7 +331,7 @@ class Node(collections.abc.Sized):
else:
return []
def _collect_errors(self, lbreaks: List[int] = [], clear_errors=False) -> List[Error]:
def _collect_errors(self, lbreaks: List[int]=[], clear_errors=False) -> List[Error]:
if self.error_flag:
errors = self.errors
if lbreaks:
......@@ -509,15 +521,15 @@ def mock_syntax_tree(sxpr):
if s[0] != '(': raise ValueError('"(" expected, not ' + s[:10])
# assert s[0] == '(', s
level = 1
i = 1
k = 1
while level > 0:
if s[i] == '(':
if s[k] == '(':
level += 1
elif s[i] == ')':
elif s[k] == ')':
level -= 1
i += 1
yield s[:i]
s = s[i:].strip()
k += 1
yield s[:k]
s = s[k:].strip()
sxpr = sxpr.strip()
if sxpr[0] != '(': raise ValueError('"(" expected, not ' + sxpr[:10])
......
......@@ -207,7 +207,8 @@ def traverse(root_node: Node,
else:
# normalize processing_table entries by turning single values
# into lists with a single value
table = {name: smart_list(call) for name, call in list(processing_table.items())}
table = {name: cast(Sequence[Callable], smart_list(call))
for name, call in list(processing_table.items())}
table = expand_table(table)
cache = table.setdefault('__cache__', cast(TransformationDict, dict()))
# change processing table in place, so its already expanded and cache filled next time
......@@ -249,7 +250,6 @@ def traverse(root_node: Node,
# assert processing_table['__cache__']
# ------------------------------------------------
#
# rearranging transformations:
......@@ -293,7 +293,7 @@ def replace_by_single_child(context: List[Node], condition: Callable=TRUE_CONDIT
node = context[-1]
if len(node.children) == 1:
context.append(node.children[0])
if condition(context):
if condition(context):
replace_child(node)
context.pop()
......@@ -345,8 +345,8 @@ def replace_parser(context: List[Node], name: str):
name.
Parameters:
name(str): "NAME:PTYPE" of the surogate. The ptype is optional
node(Node): The node where the parser shall be replaced
context: the context where the parser shall be replaced
name: "NAME:PTYPE" of the surogate. The ptype is optional
"""
node = context[-1]
name, ptype = (name.split(':') + [''])[:2]
......@@ -489,7 +489,7 @@ def keep_children(context: List[Node], section: slice = slice(None)):
@transformation_factory(Callable)
def remove_children_if(context: List[Node], condition: Callable, section: slice = slice(None)):
def remove_children_if(context: List[Node], condition: Callable): # , section: slice = slice(None)):
"""Removes all children for which `condition()` returns `True`."""
node = context[-1]
if node.children:
......@@ -506,7 +506,7 @@ def remove_children(context: List[Node], condition: Callable, section: slice = s
N = len(c)
rng = range(*section.indices(N))
node.result = tuple(c[i] for i in range(N)
if not i in rng or not condition(context + [c[i]]))
if i not in rng or not condition(context + [c[i]]))
# selection = []
# for i in range(N):
# context.append(c[i])
......
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