16.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

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

- Adjusted Arithmetic example

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