1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-
#######################################################################
# Name: test_decorator_combine
# Purpose: Test for Combine decorator. Combine decorator
# results in Terminal parse tree node. Whitespaces are
# preserved (they are not skipped) and comments are not matched.
# 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
from arpeggio import ParserPython, ZeroOrMore, OneOrMore, NonTerminal, Terminal, NoMatch, Combine
from arpeggio.peg import ParserPEG
def test_combine_python():
# This will result in NonTerminal node
def root(): return my_rule(), "."
# This will result in Terminal node
def my_rule(): return Combine(ZeroOrMore("a"), OneOrMore("b"))
parser = ParserPython(root)
input1 = "abbb."
# Whitespaces are preserved in lexical rules so the following input
# should not be recognized.
input2 = "a b bb."
ptree1 = parser.parse(input1)
with pytest.raises(NoMatch):
ptree2 = parser.parse(input2)
assert isinstance(ptree1, NonTerminal)
assert isinstance(ptree1[0], Terminal)
assert ptree1[0].value == "abbb"