Commit 3c79d326 authored by Igor Dejanovic's avatar Igor Dejanovic

Fix in the parse tree reduction. Tests.

parent f305a445
......@@ -292,7 +292,7 @@ class OrderedChoice(Sequence):
c_pos = parser.position
for e in self.nodes:
try:
result = e.parse(parser)
result = [e.parse(parser)]
match = True
except NoMatch as m:
parser.position = c_pos # Backtracking
......
......@@ -43,14 +43,3 @@ def test_parse_input():
assert str(result) == "4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | "
assert repr(result) == "[ [ [ [ number '4' [0] ] ], '+' [1], [ [ number '5' [2] ], '*' [3], [ number '7' [4] ], '/' [5], [ number '3.45' [6] ], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ [ [ number '2.56' [16] ] ], '+' [20], [ [ number '32' [21] ] ] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ [ [ number '2' [30] ] ], '-' [31], [ [ number '1.34' [32] ] ] ], ')' [36] ] ] ], EOF [37] ]"
def test_reduce_tree():
parser = ParserPython(calc, reduce_tree=True)
input = "4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
result = parser.parse(input)
assert isinstance(result, NonTerminal)
assert str(result) == "4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | "
assert repr(result) == "[ [ number '4' [0], '+' [1], [ number '5' [2], '*' [3], number '7' [4], '/' [5], number '3.45' [6], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ number '2.56' [16], '+' [20], number '32' [21] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ number '2' [30], '-' [31], number '1.34' [32] ], ')' [36] ] ] ], EOF [37] ]"
# -*- coding: utf-8 -*-
#######################################################################
# Name: test_reduce_tree
# Purpose: Test parse tree reduction
# Author: Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2014 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#######################################################################
import pytest
# Grammar
from arpeggio import ZeroOrMore, OneOrMore, ParserPython, Terminal, NonTerminal
from arpeggio.export import PTDOTExporter
from arpeggio import RegExMatch as _
def grammar(): return first, "a", second, [first, second]
def first(): return [fourth, third], ZeroOrMore(third)
def second(): return OneOrMore(third), "b"
def third(): return [third_str, fourth]
def third_str(): return "3"
def fourth(): return _(r'\d+')
def test_reduce_tree():
input = "34 a 3 3 b 3 b"
parser = ParserPython(grammar, reduce_tree=False)
result = parser.parse(input)
# PTDOTExporter().exportFile(result, 'test_reduce_tree_pt.dot')
assert result[0].rule == 'first'
assert isinstance(result[0], NonTerminal)
assert result[3].rule == 'first'
assert result[0][0].rule == 'fourth'
# Check reduction for direct OrderedChoice
assert result[2][0].rule == 'third'
parser = ParserPython(grammar, reduce_tree=True)
result = parser.parse(input)
# PTDOTExporter().exportFile(result, 'test_reduce_tree_pt.dot')
assert result[0].rule == 'fourth'
assert isinstance(result[0], Terminal)
assert result[3].rule == 'fourth'
# Check reduction for direct OrderedChoice
assert result[2][0].rule == '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