diff --git a/DHParser/compile.py b/DHParser/compile.py index 73878bdb9ec9ad468d5a5582e5e8d26096373c95..4002c4b23204adf0643160b5c32b67c76fd1f686 100644 --- a/DHParser/compile.py +++ b/DHParser/compile.py @@ -39,7 +39,7 @@ import os import re from DHParser.preprocess import strip_tokens, with_source_mapping, PreprocessorFunc -from DHParser.syntaxtree import Node, RootNode, StrictResultType +from DHParser.syntaxtree import Node, RootNode, ZOMBIE_ROOTNODE, StrictResultType from DHParser.transform import TransformationFunc from DHParser.parse import Grammar from DHParser.error import adjust_error_locations, is_error, Error @@ -97,7 +97,7 @@ class Compiler: self._reset() def _reset(self): - self.tree = None # type: Optional[RootNode] + self.tree = ZOMBIE_ROOTNODE # type: RootNode self.context = [] # type: List[Node] self._dirty_flag = False @@ -116,7 +116,7 @@ class Compiler: result = self.compile(root) return result - def set_grammar_name(self, grammar_name: str="", grammar_source: str=""): + def set_grammar_name(self, grammar_name: str = "", grammar_source: str = ""): """ Changes the grammar's name and the grammar's source. @@ -219,7 +219,7 @@ def compile_source(source: str, parser: Grammar, # str -> Node (concrete syntax tree (CST)) transformer: TransformationFunc, # Node (CST) -> Node (abstract syntax tree (AST)) compiler: Compiler, # Node (AST) -> Any - preserve_ast: bool = False) -> Tuple[Any, List[Error], Node]: + preserve_ast: bool = False) -> Tuple[Optional[Any], List[Error], Optional[Node]]: """ Compiles a source in four stages: 1. Pre-Processing (if needed) @@ -259,7 +259,7 @@ def compile_source(source: str, source_mapping = lambda i: i else: source_text, source_mapping = with_source_mapping(preprocessor(original_text)) - syntax_tree = parser(source_text) + syntax_tree = parser(source_text) # type: RootNode if is_logging(): log_ST(syntax_tree, log_file_name + '.cst') log_parsing_history(parser, log_file_name) diff --git a/DHParser/ebnf.py b/DHParser/ebnf.py index 1d4b3d2bb46be75fdd551f268e92a1629beef4fc..9a802365602c1dfdeecdce881532a33f2753a430 100644 --- a/DHParser/ebnf.py +++ b/DHParser/ebnf.py @@ -247,6 +247,7 @@ EBNF_AST_transformation_table = { def EBNFTransform() -> TransformationFunc: return partial(traverse, processing_table=EBNF_AST_transformation_table.copy()) + def get_ebnf_transformer() -> TransformationFunc: global thread_local_EBNF_transformer_singleton try: @@ -550,11 +551,11 @@ class EBNFCompiler(Compiler): # add EBNF grammar to the doc string of the parser class article = 'an ' if self.grammar_name[0:1] in "AaEeIiOoUu" else 'a ' # what about 'hour', 'universe' etc.? - declarations = ['class ' + self.grammar_name + - 'Grammar(Grammar):', - 'r"""Parser for ' + article + self.grammar_name + - ' source file' + - (', with this grammar:' if self.grammar_source else '.')] + declarations = ['class ' + self.grammar_name + + 'Grammar(Grammar):', + 'r"""Parser for ' + article + self.grammar_name + + ' source file' + + (', with this grammar:' if self.grammar_source else '.')] definitions.append(('parser_initialization__', '"upon instantiation"')) if self.grammar_source: definitions.append(('source_hash__', @@ -833,7 +834,7 @@ class EBNFCompiler(Compiler): # shift = (Node(node.parser, node.result[1].result),) # node.result[1].result = shift + node.result[2:] node.children[1].result = (Node(node.children[1].parser, node.children[1].result),) \ - + node.children[2:] + + node.children[2:] node.children[1].parser = node.parser node.result = (node.children[0], node.children[1]) diff --git a/DHParser/log.py b/DHParser/log.py index 9c36840417357ac6fd3093eddaf8743576b5a01b..0e3fe98a5a49f9c801adfe4ac508dd8d2c4afc9b 100644 --- a/DHParser/log.py +++ b/DHParser/log.py @@ -55,7 +55,7 @@ import os from DHParser.error import line_col from DHParser.stringview import StringView -from DHParser.syntaxtree import Node, WHITESPACE_PTYPE +from DHParser.syntaxtree import Node from DHParser.toolkit import is_filename, escape_control_characters, typing from typing import List, Tuple, Union @@ -210,7 +210,7 @@ class HistoryRecord: HTML_LEAD_IN = ('\n' '\n\n\n