Commit 2b52940c authored by Eckhart Arnold's avatar Eckhart Arnold

- Adjusted Arithmetic example

parent eb54faa3
......@@ -102,7 +102,7 @@ from DHParser import logging, is_filename, load_if_file, \\
keep_children, is_one_of, not_one_of, has_content, apply_if, remove_first, remove_last, \\
remove_anonymous_empty, keep_nodes, traverse_locally, strip, lstrip, rstrip, \\
replace_content, replace_content_by, forbid, assert_content, remove_infix_operator, \\
error_on, recompile_grammar, GLOBALS
reduce_anonymous_nodes, error_on, recompile_grammar, GLOBALS
'''.format(dhparserdir=dhparserdir)
......
......@@ -562,7 +562,7 @@ class EBNFCompiler(Compiler):
tt_name = self.grammar_name + '_AST_transformation_table'
transtable = [tt_name + ' = {',
' # AST Transformations for the ' + self.grammar_name + '-grammar']
transtable.append(' "<": eliminate_anonymous_nodes,')
transtable.append(' "<": reduce_anonymous_nodes,')
for name in self.rules:
transformations = '[]'
# rule = self.definitions[name]
......
......@@ -313,9 +313,10 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
@result.setter
def result(self, result: ResultType):
# # made obsolete by static type checking with mypy
# assert ((isinstance(result, tuple) and all(isinstance(child, Node) for child in result))
# or isinstance(result, Node)
# or isinstance(result, str)), str(result)
assert ((isinstance(result, tuple) and all(isinstance(child, Node) for child in result))
or isinstance(result, Node)
or isinstance(result, str)
or isinstance(result, StringView)), "%s (%s)" % (str(result), str(type(result)))
# Possible optimization: Do not allow single nodes as argument:
# assert not isinstance(result, Node)
self._len = -1 # lazy evaluation
......
......@@ -47,7 +47,7 @@ __all__ = ('TransformationDict',
'traverse',
'is_named',
'update_attr',
'eliminate_anonymous_nodes',
'reduce_anonymous_nodes',
'replace_by_single_child',
'reduce_single_child',
'replace_or_reduce',
......@@ -559,8 +559,13 @@ def _reduce_child(node: Node, child: Node):
# _reduce_child(context[-1], child)
@transformation_factory
def eliminate_anonymous_nodes(context: List[Node]):
def reduce_anonymous_nodes(context: List[Node]):
"""
Reduces (non-recursively) all anonymous non-leaf children by adding
their result to the result of the last node in the context. If the
last node is anonymous itself, it will be replaced by a single child.
Also drops any empty anonymous nodes.
"""
node = context[-1]
if node.children:
new_result = []
......@@ -577,11 +582,13 @@ def eliminate_anonymous_nodes(context: List[Node]):
child = new_result[0]
if node.is_anonymous():
node.tag_name = child.tag_name
new_result = [child.result]
node.result = child.result
update_attr(node, child)
return
elif child.is_anonymous():
new_result = [child.result]
node.result = child.result
update_attr(node, child)
return
node.result = tuple(new_result)
......
......@@ -16,7 +16,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
"""
"""3
# TODO: This is still a stub...
......
......@@ -10,7 +10,7 @@
@ literalws = right # literals have implicit whitespace on the right hand side
@ comment = /#.*/ # comments range from a '#'-character to the end of the line
@ ignorecase = False # literals and regular expressions are case-sensitive
@ drop = whitespace # drop anonymous whitespace and tokens
@ drop = whitespace # drop anonymous whitespace
#######################################################################
#
......
......@@ -33,7 +33,7 @@ from DHParser import logging, is_filename, load_if_file, \
keep_children, is_one_of, not_one_of, has_content, apply_if, remove_first, remove_last, \
remove_anonymous_empty, keep_nodes, traverse_locally, strip, lstrip, rstrip, \
replace_content, replace_content_by, forbid, assert_content, remove_infix_operator, \
error_on, recompile_grammar, GLOBALS
error_on, recompile_grammar, reduce_anonymous_nodes, GLOBALS
#######################################################################
......@@ -59,7 +59,7 @@ class ArithmeticGrammar(Grammar):
r"""Parser for an Arithmetic source file.
"""
expression = Forward()
source_hash__ = "d03e397fb4cabd6f20f3ae7c9add4ad5"
source_hash__ = "a94242482d508901d1692b82c48ba903"
parser_initialization__ = ["upon instantiation"]
resume_rules__ = {}
COMMENT__ = r'#.*'
......@@ -95,10 +95,11 @@ def get_grammar() -> ArithmeticGrammar:
Arithmetic_AST_transformation_table = {
# AST Transformations for the Arithmetic-grammar
"<": remove_empty,
"<": reduce_anonymous_nodes,
"expression": [],
"term": [],
"factor": [replace_or_reduce],
"term": [reduce_single_child],
"factor": [reduce_single_child],
"group": [remove_tokens('(', ')'), replace_by_single_child],
"NUMBER": [],
"VARIABLE": [],
":Token": reduce_single_child,
......
Life is but a walking shadow
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