Commit ecf34778 authored by Igor Dejanovic's avatar Igor Dejanovic

Changed PTNode.rule->rule_name. rule is now a reference to ParsingExpression.

parent 1c76b784
This diff is collapsed.
...@@ -81,14 +81,14 @@ class PEGSemanticAction(SemanticAction): ...@@ -81,14 +81,14 @@ class PEGSemanticAction(SemanticAction):
# If resolved rule hasn't got the same name it # If resolved rule hasn't got the same name it
# should be cloned and preserved in the peg_rules cache # should be cloned and preserved in the peg_rules cache
if resolved_rule.rule != n.rule_name: if resolved_rule.rule_name != n.rule_name:
resolved_rule = copy.copy(resolved_rule) resolved_rule = copy.copy(resolved_rule)
resolved_rule.rule = n.rule_name resolved_rule.rule_name = n.rule_name
parser.peg_rules[resolved_rule.rule] = resolved_rule parser.peg_rules[resolved_rule.rule_name] = resolved_rule
if parser.debug: if parser.debug:
print("Resolving: cloned to {} = > {}"\ print("Resolving: cloned to {} = > {}"\
.format(resolved_rule.rule, resolved_rule.name)) .format(resolved_rule.rule_name, resolved_rule.name))
node.nodes[i] = resolved_rule node.nodes[i] = resolved_rule
...@@ -111,7 +111,7 @@ class SemRule(PEGSemanticAction): ...@@ -111,7 +111,7 @@ class SemRule(PEGSemanticAction):
retval = Sequence(nodes=children[1:]) retval = Sequence(nodes=children[1:])
else: else:
retval = children[1] retval = children[1]
retval.rule = rule_name retval.rule_name = rule_name
retval.root = True retval.root = True
if not hasattr(parser, "peg_rules"): if not hasattr(parser, "peg_rules"):
...@@ -230,14 +230,14 @@ class ParserPEG(Parser): ...@@ -230,14 +230,14 @@ class ParserPEG(Parser):
# visualization # visualization
if self.debug: if self.debug:
from arpeggio.export import PMDOTExporter from arpeggio.export import PMDOTExporter
root_rule = self.parser_model.rule root_rule = self.parser_model.rule_name
PMDOTExporter().exportFile(self.parser_model, PMDOTExporter().exportFile(self.parser_model,
"{}_peg_parser_model.dot".format(root_rule)) "{}_peg_parser_model.dot".format(root_rule))
# Comments should be optional and there can be more of them # Comments should be optional and there can be more of them
if self.comments_model: # and not isinstance(self.comments_model, ZeroOrMore): if self.comments_model: # and not isinstance(self.comments_model, ZeroOrMore):
self.comments_model.root = True self.comments_model.root = True
self.comments_model.rule = comment_rule_name self.comments_model.rule_name = comment_rule_name
def _parse(self): def _parse(self):
return self.parser_model.parse(self) return self.parser_model.parse(self)
......
...@@ -24,7 +24,7 @@ def test_construct_parser(): ...@@ -24,7 +24,7 @@ def test_construct_parser():
parser = ParserPEG(grammar, 'calc') parser = ParserPEG(grammar, 'calc')
assert parser.parser_model.rule == 'calc' assert parser.parser_model.rule_name == 'calc'
assert isinstance(parser.parser_model, Sequence) assert isinstance(parser.parser_model, Sequence)
assert parser.parser_model.nodes[0].name == 'OneOrMore' assert parser.parser_model.nodes[0].name == 'OneOrMore'
......
...@@ -34,9 +34,9 @@ def test_lookup_single(): ...@@ -34,9 +34,9 @@ def test_lookup_single():
assert isinstance(result, ParseTreeNode) assert isinstance(result, ParseTreeNode)
assert isinstance(result.bar, NonTerminal) assert isinstance(result.bar, NonTerminal)
# dot access # dot access
assert result.bar.rule == 'bar' assert result.bar.rule_name == 'bar'
# Index access # Index access
assert result[1].rule == 'bar' assert result[1].rule_name == 'bar'
# There are six children from result # There are six children from result
assert len(result) == 6 assert len(result) == 6
...@@ -52,7 +52,7 @@ def test_lookup_single(): ...@@ -52,7 +52,7 @@ def test_lookup_single():
# For example this returns all bum from all bar in result # For example this returns all bum from all bar in result
assert len(result.bar.bum) == 2 assert len(result.bar.bum) == 2
# Verify that proper bum are returned # Verify that proper bum are returned
assert result.bar.bum[0].rule == 'bum' assert result.bar.bum[0].rule_name == 'bum'
assert result.bar.bum[1].position == 18 assert result.bar.bum[1].position == 18
# Access to terminal # Access to terminal
......
...@@ -29,7 +29,7 @@ def test_pp_construction(): ...@@ -29,7 +29,7 @@ def test_pp_construction():
''' '''
parser = ParserPython(calc) parser = ParserPython(calc)
assert parser.parser_model.rule == 'calc' assert parser.parser_model.rule_name == 'calc'
assert isinstance(parser.parser_model, Sequence) assert isinstance(parser.parser_model, Sequence)
assert parser.parser_model.nodes[0].desc == 'OneOrMore' assert parser.parser_model.nodes[0].desc == 'OneOrMore'
......
...@@ -31,21 +31,21 @@ def test_reduce_tree(): ...@@ -31,21 +31,21 @@ def test_reduce_tree():
# PTDOTExporter().exportFile(result, 'test_reduce_tree_pt.dot') # PTDOTExporter().exportFile(result, 'test_reduce_tree_pt.dot')
assert result[0].rule == 'first' assert result[0].rule_name == 'first'
assert isinstance(result[0], NonTerminal) assert isinstance(result[0], NonTerminal)
assert result[3].rule == 'first' assert result[3].rule_name == 'first'
assert result[0][0].rule == 'fourth' assert result[0][0].rule_name == 'fourth'
# Check reduction for direct OrderedChoice # Check reduction for direct OrderedChoice
assert result[2][0].rule == 'third' assert result[2][0].rule_name == 'third'
parser = ParserPython(grammar, reduce_tree=True) parser = ParserPython(grammar, reduce_tree=True)
result = parser.parse(input) result = parser.parse(input)
# PTDOTExporter().exportFile(result, 'test_reduce_tree_pt.dot') # PTDOTExporter().exportFile(result, 'test_reduce_tree_pt.dot')
assert result[0].rule == 'fourth' assert result[0].rule_name == 'fourth'
assert isinstance(result[0], Terminal) assert isinstance(result[0], Terminal)
assert result[3].rule == 'fourth' assert result[3].rule_name == 'fourth'
# Check reduction for direct OrderedChoice # Check reduction for direct OrderedChoice
assert result[2][0].rule == 'third_str' assert result[2][0].rule_name == 'third_str'
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