Commit db0a8568 authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

bugfixes

parent 0e90dfe0
......@@ -82,8 +82,9 @@ from DHParser.parsers import Grammar, Compiler, nil_scanner, \\
last_value, counterpart, accumulate, ScannerFunc
from DHParser.syntaxtree import Node, traverse, remove_enclosing_delimiters, \\
remove_children_if, reduce_single_child, replace_by_single_child, remove_whitespace, \\
no_transformation, remove_expendables, remove_tokens, flatten, is_whitespace, is_expendable, \\
collapse, map_content, WHITESPACE_PTYPE, TOKEN_PTYPE, TransformationFunc
no_transformation, remove_expendables, remove_empty, remove_tokens, flatten, is_whitespace, \\
is_empty, is_expendable, collapse, map_content, WHITESPACE_PTYPE, TOKEN_PTYPE, \\
TransformationFunc
'''
......@@ -257,7 +258,6 @@ def parser_factory(ebnf_src: str, branding="DSL") -> Grammar:
"""
grammar_src = compileDSL(ebnf_src, nil_scanner, get_ebnf_grammar(),
get_ebnf_transformer(), get_ebnf_compiler(branding))
print(grammar_src)
return compile_python_object(DHPARSER_IMPORTS + grammar_src, 'get_(?:\w+_)?grammar$')
......
......@@ -356,9 +356,10 @@ class EBNFCompiler(Compiler):
transtable = [tt_name + ' = {',
' # AST Transformations for the ' +
self.grammar_name + '-grammar']
transtable.append(' "+": remove_empty,')
for name in self.rules:
transtable.append(' "' + name + '": no_transformation,')
transtable += [' "*": no_transformation', '}', '', tf_name +
transtable += [' "*": replace_by_single_child', '}', '', tf_name +
' = partial(traverse, processing_table=%s)' % tt_name, '']
transtable += [TRANSFORMER_FACTORY.format(NAME=self.grammar_name)]
return '\n'.join(transtable)
......
......@@ -390,7 +390,9 @@ class Grammar:
particular instance of Grammar.
"""
if parser.name:
assert parser.name not in self.__dict__, \
# prevent overwriting instance variables or parsers of a different class
assert parser.name not in self.__dict__ or \
isinstance(self.__dict__[parser.name], parser.__class__), \
('Cannot add parser "%s" because a field with the same name '
'already exists in grammar object!' % parser.name)
setattr(self, parser.name, parser)
......
......@@ -58,6 +58,7 @@ __all__ = ['WHITESPACE_PTYPE',
'is_token',
'remove_children_if',
'remove_whitespace',
'remove_empty',
'remove_expendables',
'remove_tokens',
'flatten',
......@@ -755,8 +756,8 @@ def remove_children_if(node, condition):
remove_whitespace = remove_children_if(is_whitespace) # partial(remove_children_if, condition=is_whitespace)
remove_empty = remove_children_if(is_empty)
remove_expendables = remove_children_if(is_expendable) # partial(remove_children_if, condition=is_expendable)
# remove_scanner_tokens = remove_children_if(is_scanner_token) # partial(remove_children_if, condition=is_scanner_token)
@transformation_factory
......@@ -777,7 +778,8 @@ def remove_enclosing_delimiters(node):
node.result = node.result[1:-1]
def map_content(node, func: Callable[[Node], ResultType]):
@transformation_factory
def map_content(node, func: Callable): # Callable[[Node], ResultType]
"""Replaces the content of the node. ``func`` takes the node
as an argument an returns the mapped result.
"""
......
......@@ -15,4 +15,5 @@
Im allgemeinen werden die Bewohner Göttingens eingeteilt in Studenten,
Professoren, Philister und Vieh; welche vier Stände doch nichts weniger
als streng geschieden sind. Der Viehstand ist der bedeutendste.
\ No newline at end of file
als streng geschieden sind. Der Viehstand ist der bedeutendste.
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