From da3181ba68e00824e36ed6a9c5b640bd038fb406 Mon Sep 17 00:00:00 2001 From: eckhart Date: Wed, 24 Jan 2018 23:34:25 +0100 Subject: [PATCH] - corrected a few static type errors --- DHParser/dsl.py | 4 ++-- DHParser/parse.py | 26 ++++++++++++++++++-------- DHParser/syntaxtree.py | 2 +- DHParser/transform.py | 2 +- examples/LaTeX/testdata/testdoc2.tex | 2 +- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/DHParser/dsl.py b/DHParser/dsl.py index 4a799a9..6ba4f3e 100644 --- a/DHParser/dsl.py +++ b/DHParser/dsl.py @@ -320,7 +320,7 @@ def load_compiler_suite(compiler_suite: str) -> \ else: # assume source is an ebnf grammar. Is there really any reasonable application case for this? with logging(False): - compiler_py, messages, _ = compile_source(source, None, get_ebnf_grammar(), + compiler_py, messages, n = compile_source(source, None, get_ebnf_grammar(), get_ebnf_transformer(), get_ebnf_compiler()) if has_errors(messages): raise GrammarError(only_errors(messages), source) @@ -352,7 +352,7 @@ def is_outdated(compiler_suite: str, grammar_source: str) -> bool: True, if ``compiler_suite`` seems to be out of date. """ try: - _, grammar, _, _ = load_compiler_suite(compiler_suite) + n1, grammar, n2, n3 = load_compiler_suite(compiler_suite) return grammar_changed(grammar(), grammar_source) except ValueError: return True diff --git a/DHParser/parse.py b/DHParser/parse.py index da9fc20..a9a69fa 100644 --- a/DHParser/parse.py +++ b/DHParser/parse.py @@ -1842,23 +1842,33 @@ class NegativeLookahead(Lookahead): class Lookbehind(FlowOperator): """ Matches, if the contained parser would match backwards. Requires - the contained parser to be a RegExp-parser. + the contained parser to be a RegExp, Re, PlainText or Token parser. """ def __init__(self, parser: Parser, name: str = '') -> None: p = parser while isinstance(p, Synonym): p = p.parser - assert isinstance(p, RegExp), str(type(p)) - self.regexp = cast(RE, p).main.regexp if isinstance(p, RE) else p.regexp + assert isinstance(p, RegExp) or isinstance(p, PlainText) or isinstance(p, RE), str(type(p)) + self.regexp = None + self.text = None + if isinstance(p, RE): + if isinstance(cast(RE, p).main, RegExp): + self.regexp = cast(RegExp, cast(RE, p).main).regexp + else: # p.main is of type PlainText + self.text = cast(PlainText, cast(RE, p).main).text + elif isinstance(p, RegExp): + self.regexp = cast(RegExp, p).regexp + else: # p is of type PlainText + self.text = cast(PlainText, p).text super().__init__(parser, name) def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]: - # backwards_text = self.grammar.document__[-len(text) - 1::-1] backwards_text = self.grammar.reversed__[len(text):] - if self.sign(backwards_text.match(self.regexp)): - return Node(self, ''), text - else: - return None, text + if self.regexp is None: # assert self.text is not None + does_match = backwards_text[:len(self.text)] == self.text + else: # assert self.regexp is not None + does_match = backwards_text.match(self.regexp) + return (Node(self, ''), text) if self.sign(does_match) else (None, text) def __repr__(self): return '-&' + self.parser.repr diff --git a/DHParser/syntaxtree.py b/DHParser/syntaxtree.py index 47c2e58..06380a8 100644 --- a/DHParser/syntaxtree.py +++ b/DHParser/syntaxtree.py @@ -352,7 +352,7 @@ class Node(collections.abc.Sized): """ if self.children: return "".join(child.content for child in self.children) - return self._result + return cast(str, self._result) @property diff --git a/DHParser/transform.py b/DHParser/transform.py index ea4a4e9..c124838 100644 --- a/DHParser/transform.py +++ b/DHParser/transform.py @@ -653,7 +653,7 @@ def rstrip(context: List[Node], condition: Callable = is_expendable): @transformation_factory(Callable) -def strip(context: List[Node], condition: Callable = is_expendable) -> str: +def strip(context: List[Node], condition: Callable = is_expendable): """Removes leading and trailing child-nodes that fulfill a given condition.""" lstrip(context, condition) rstrip(context, condition) diff --git a/examples/LaTeX/testdata/testdoc2.tex b/examples/LaTeX/testdata/testdoc2.tex index 6cc6b13..1fccbad 100644 --- a/examples/LaTeX/testdata/testdoc2.tex +++ b/examples/LaTeX/testdata/testdoc2.tex @@ -1,4 +1,4 @@ - +0 \documentclass[12pt, english, a4paper]{article} \begin{document} -- GitLab