diff --git a/DHParser/dsl.py b/DHParser/dsl.py index 4a799a9d5cba8ddca3668f2cd8fa0a86afddb43d..6ba4f3e109fadb28516aae343fa2335f44166621 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 da9fc208514c0baef80b0ae411e8b1a1a4377ba3..a9a69faba21e5ba9f5639bd7234b01873fb499d8 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 47c2e5816d3bd10c3515e76e2abd0ef1bdc2d495..06380a826027341aa6f7161842a0b44d7ce22a14 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 ea4a4e95dc472c6e061080ce3fed50b28e3d2988..c124838883a41d0206bbf62ae351fd8e91449770 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 6cc6b131ffe260d863b55d3ea46e2c558bf708a8..1fccbad85f0e2f33f1160da4bcf8e41828e61e1f 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}