| 1 |
import py |
| 2 |
|
| 3 |
from rpython.rlib.parsing.tree import Nonterminal, Symbol |
| 4 |
from rpython.rlib.parsing.lexer import Token, SourcePos |
| 5 |
|
| 6 |
class TestTreeAppLevel(object): |
| 7 |
|
| 8 |
def test_nonterminal_simple(self): |
| 9 |
pos = SourcePos(1,2,3) |
| 10 |
tree = Nonterminal(symbol="a", |
| 11 |
children=[ |
| 12 |
Symbol(symbol="b", |
| 13 |
additional_info="b", |
| 14 |
token=Token(name="B", |
| 15 |
source="b", |
| 16 |
source_pos=pos))]) |
| 17 |
assert tree.getsourcepos() == pos |
| 18 |
|
| 19 |
def test_nonterminal_nested(self): |
| 20 |
pos = SourcePos(1,2,3) |
| 21 |
tree = Nonterminal(symbol="a", |
| 22 |
children=[ |
| 23 |
Nonterminal(symbol="c", |
| 24 |
children=[ |
| 25 |
Symbol(symbol="b", |
| 26 |
additional_info="b", |
| 27 |
token=Token(name="B", |
| 28 |
source="b", |
| 29 |
source_pos=pos))])]) |
| 30 |
assert tree.getsourcepos() == pos |
| 31 |
|
| 32 |
def test_nonterminal_simple_empty(self): |
| 33 |
tree = Nonterminal(symbol="a", |
| 34 |
children=[]) |
| 35 |
assert len(tree.children) == 0 |
| 36 |
py.test.raises(IndexError, tree.getsourcepos) |
| 37 |
|
| 38 |
def test_nonterminal_nested_empty(self): |
| 39 |
tree = Nonterminal(symbol="a", |
| 40 |
children=[Nonterminal(symbol="c", |
| 41 |
children=[Nonterminal(symbol="c", |
| 42 |
children=[Nonterminal(symbol="c", |
| 43 |
children=[Nonterminal(symbol="c", |
| 44 |
children=[Nonterminal(symbol="c", |
| 45 |
children=[Nonterminal(symbol="c", |
| 46 |
children=[])])])])])])]) |
| 47 |
assert len(tree.children) != 0 |
| 48 |
py.test.raises(IndexError, tree.getsourcepos) |
| 49 |
|
| 50 |
class TestTreeTranslated(object): |
| 51 |
def compile(self, f): |
| 52 |
from rpython.translator.c.test.test_genc import compile |
| 53 |
return compile(f, []) |
| 54 |
|
| 55 |
def test_nonterminal_simple_empty(self): |
| 56 |
def foo(): |
| 57 |
tree = Nonterminal(symbol="a", |
| 58 |
children=[]) |
| 59 |
try: |
| 60 |
return tree.getsourcepos() |
| 61 |
except IndexError: |
| 62 |
return -42 |
| 63 |
f = self.compile(foo) |
| 64 |
assert f() == -42 |
| 65 |
|
| 66 |
def test_nonterminal_nested_empty(self): |
| 67 |
def foo(): |
| 68 |
tree = Nonterminal(symbol="a", |
| 69 |
children=[Nonterminal(symbol="c", |
| 70 |
children=[Nonterminal(symbol="c", |
| 71 |
children=[Nonterminal(symbol="c", |
| 72 |
children=[Nonterminal(symbol="c", |
| 73 |
children=[Nonterminal(symbol="c", |
| 74 |
children=[Nonterminal(symbol="c", |
| 75 |
children=[])])])])])])]) |
| 76 |
try: |
| 77 |
return tree.getsourcepos() |
| 78 |
except IndexError: |
| 79 |
return -42 |
| 80 |
f = self.compile(foo) |
| 81 |
assert f() == -42 |