Commit 909142bd authored by eckhart's avatar eckhart
Browse files

- type checking errors (mypy) corrected

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