|
1 |
| -import unittest |
2 | 1 | from ast import parse
|
3 | 2 |
|
| 3 | +import pytest |
| 4 | + |
4 | 5 | from tatsu.exceptions import FailedParse
|
5 | 6 | from tatsu.ngcodegen import codegen
|
6 | 7 | from tatsu.tool import compile
|
7 | 8 |
|
8 | 9 |
|
9 |
| -class KeywordTests(unittest.TestCase): |
10 |
| - def test_keywords_in_rule_names(self): |
11 |
| - grammar = """ |
12 |
| - start |
13 |
| - = |
14 |
| - whitespace |
15 |
| - ; |
16 |
| -
|
| 10 | +def test_keywords_in_rule_names(): |
| 11 | + grammar = """ |
| 12 | + start |
| 13 | + = |
17 | 14 | whitespace
|
18 |
| - = |
19 |
| - {'x'}+ |
20 |
| - ; |
21 |
| - """ |
22 |
| - m = compile(grammar, 'Keywords') |
23 |
| - m.parse('x') |
24 |
| - |
25 |
| - def test_python_keywords_in_rule_names(self): |
26 |
| - # This is a regression test for |
27 |
| - # https://bitbucket.org/neogeny/tatsu/issues/59 |
28 |
| - # (semantic actions not called for rules with the same name as a python |
29 |
| - # keyword). |
30 |
| - grammar = """ |
31 |
| - not = 'x' ; |
32 |
| - """ |
33 |
| - m = compile(grammar, 'Keywords') |
34 |
| - |
35 |
| - class Semantics: |
36 |
| - def __init__(self): |
37 |
| - self.called = False |
38 |
| - |
39 |
| - def not_(self, ast): |
40 |
| - self.called = True |
41 |
| - |
42 |
| - semantics = Semantics() |
43 |
| - m.parse('x', semantics=semantics) |
44 |
| - assert semantics.called |
45 |
| - |
46 |
| - def test_define_keywords(self): |
47 |
| - grammar = """ |
48 |
| - @@keyword :: B C |
49 |
| - @@keyword :: 'A' |
50 |
| -
|
51 |
| - start = ('a' 'b').{'x'}+ ; |
52 |
| - """ |
53 |
| - model = compile(grammar, 'test') |
54 |
| - c = codegen(model) |
55 |
| - parse(c) |
56 |
| - |
57 |
| - grammar2 = str(model) |
58 |
| - model2 = compile(grammar2, 'test') |
59 |
| - c2 = codegen(model2) |
60 |
| - parse(c2) |
61 |
| - |
62 |
| - self.assertEqual(grammar2, str(model2)) |
63 |
| - |
64 |
| - def test_check_keywords(self): |
65 |
| - grammar = r""" |
66 |
| - @@keyword :: A |
67 |
| -
|
68 |
| - start = {id}+ $ ; |
69 |
| -
|
70 |
| - @name |
71 |
| - id = /\w+/ ; |
72 |
| - """ |
73 |
| - model = compile(grammar, 'test') |
74 |
| - c = codegen(model) |
75 |
| - print(c) |
76 |
| - parse(c) |
77 |
| - |
78 |
| - ast = model.parse('hello world') |
79 |
| - self.assertEqual(['hello', 'world'], ast) |
| 15 | + ; |
80 | 16 |
|
81 |
| - try: |
82 |
| - ast = model.parse('hello A world') |
83 |
| - self.assertEqual(['hello', 'A', 'world'], ast) |
84 |
| - self.fail('accepted keyword as name') |
85 |
| - except FailedParse as e: |
86 |
| - self.assertTrue('"A" is a reserved word' in str(e)) |
| 17 | + whitespace |
| 18 | + = |
| 19 | + {'x'}+ |
| 20 | + ; |
| 21 | + """ |
| 22 | + m = compile(grammar, 'Keywords') |
| 23 | + m.parse('x') |
| 24 | + |
| 25 | + |
| 26 | +def test_python_keywords_in_rule_names(): |
| 27 | + # This is a regression test for |
| 28 | + # https://bitbucket.org/neogeny/tatsu/issues/59 |
| 29 | + # (semantic actions not called for rules with the same name as a python |
| 30 | + # keyword). |
| 31 | + grammar = """ |
| 32 | + not = 'x' ; |
| 33 | + """ |
| 34 | + m = compile(grammar, 'Keywords') |
| 35 | + |
| 36 | + class Semantics: |
| 37 | + def __init__(self): |
| 38 | + self.called = False |
| 39 | + |
| 40 | + def not_(self, ast): |
| 41 | + self.called = True |
| 42 | + |
| 43 | + semantics = Semantics() |
| 44 | + m.parse('x', semantics=semantics) |
| 45 | + assert semantics.called |
| 46 | + |
| 47 | + |
| 48 | +def test_define_keywords(): |
| 49 | + grammar = """ |
| 50 | + @@keyword :: B C |
| 51 | + @@keyword :: 'A' |
| 52 | +
|
| 53 | + start = ('a' 'b').{'x'}+ ; |
| 54 | + """ |
| 55 | + model = compile(grammar, 'test') |
| 56 | + c = codegen(model) |
| 57 | + parse(c) |
| 58 | + |
| 59 | + grammar2 = str(model) |
| 60 | + model2 = compile(grammar2, 'test') |
| 61 | + c2 = codegen(model2) |
| 62 | + parse(c2) |
| 63 | + |
| 64 | + assert grammar2 == str(model2) |
87 | 65 |
|
88 |
| - def test_check_unicode_name(self): |
89 |
| - grammar = r""" |
90 |
| - @@keyword :: A |
91 | 66 |
|
92 |
| - start = {id}+ $ ; |
| 67 | +def test_check_keywords(): |
| 68 | + grammar = r""" |
| 69 | + @@keyword :: A |
93 | 70 |
|
94 |
| - @name |
95 |
| - id = /\w+/ ; |
96 |
| - """ |
97 |
| - model = compile(grammar, 'test') |
98 |
| - model.parse('hello Øresund') |
| 71 | + start = {id}+ $ ; |
99 | 72 |
|
100 |
| - def test_sparse_keywords(self): |
101 |
| - grammar = r""" |
102 |
| - @@keyword :: A |
| 73 | + @name |
| 74 | + id = /\w+/ ; |
| 75 | + """ |
| 76 | + model = compile(grammar, 'test') |
| 77 | + c = codegen(model) |
| 78 | + print(c) |
| 79 | + parse(c) |
103 | 80 |
|
104 |
| - @@ignorecase :: False |
| 81 | + ast = model.parse('hello world') |
| 82 | + assert ast == ['hello', 'world'] |
105 | 83 |
|
106 |
| - start = {id}+ $ ; |
| 84 | + try: |
| 85 | + ast = model.parse('hello A world') |
| 86 | + assert ast == ['hello', 'A', 'world'] |
| 87 | + pytest.fail('accepted keyword as name') |
| 88 | + except FailedParse as e: |
| 89 | + assert '"A" is a reserved word' in str(e) |
| 90 | + |
| 91 | + |
| 92 | +def test_check_unicode_name(): |
| 93 | + grammar = r""" |
| 94 | + @@keyword :: A |
| 95 | +
|
| 96 | + start = {id}+ $ ; |
| 97 | +
|
| 98 | + @name |
| 99 | + id = /\w+/ ; |
| 100 | + """ |
| 101 | + model = compile(grammar, 'test') |
| 102 | + model.parse('hello Øresund') |
| 103 | + |
| 104 | + |
| 105 | +def test_sparse_keywords(): |
| 106 | + grammar = r""" |
| 107 | + @@keyword :: A |
| 108 | +
|
| 109 | + @@ignorecase :: False |
| 110 | +
|
| 111 | + start = {id}+ $ ; |
| 112 | +
|
| 113 | + @@keyword :: B |
| 114 | +
|
| 115 | + @name |
| 116 | + id = /\w+/ ; |
| 117 | + """ |
| 118 | + model = compile(grammar, 'test', trace=False, colorize=True) |
| 119 | + c = codegen(model) |
| 120 | + parse(c) |
| 121 | + |
| 122 | + ast = model.parse('hello world') |
| 123 | + assert ast == ['hello', 'world'] |
| 124 | + |
| 125 | + for k in ('A', 'B'): |
| 126 | + try: |
| 127 | + ast = model.parse('hello %s world' % k) |
| 128 | + assert ['hello', k, 'world'] == ast |
| 129 | + pytest.fail('accepted keyword "%s" as name' % k) |
| 130 | + except FailedParse as e: |
| 131 | + assert '"%s" is a reserved word' % k in str(e) |
107 | 132 |
|
108 |
| - @@keyword :: B |
109 | 133 |
|
110 |
| - @name |
111 |
| - id = /\w+/ ; |
112 |
| - """ |
113 |
| - model = compile(grammar, 'test', trace=False, colorize=True) |
114 |
| - c = codegen(model) |
115 |
| - parse(c) |
| 134 | +def test_ignorecase_keywords(): |
| 135 | + grammar = r""" |
| 136 | + @@ignorecase :: True |
| 137 | + @@keyword :: if |
116 | 138 |
|
117 |
| - ast = model.parse('hello world') |
118 |
| - self.assertEqual(['hello', 'world'], ast) |
| 139 | + start = rule ; |
119 | 140 |
|
120 |
| - for k in ('A', 'B'): |
121 |
| - try: |
122 |
| - ast = model.parse('hello %s world' % k) |
123 |
| - self.assertEqual(['hello', k, 'world'], ast) |
124 |
| - self.fail('accepted keyword "%s" as name' % k) |
125 |
| - except FailedParse as e: |
126 |
| - self.assertTrue('"%s" is a reserved word' % k in str(e)) |
| 141 | + @name |
| 142 | + rule = @:word if_exp $ ; |
127 | 143 |
|
128 |
| - def test_ignorecase_keywords(self): |
129 |
| - grammar = r""" |
130 |
| - @@ignorecase :: True |
131 |
| - @@keyword :: if |
| 144 | + if_exp = 'if' digit ; |
132 | 145 |
|
133 |
| - start = rule ; |
| 146 | + word = /\w+/ ; |
| 147 | + digit = /\d/ ; |
| 148 | + """ |
134 | 149 |
|
135 |
| - @name |
136 |
| - rule = @:word if_exp $ ; |
| 150 | + model = compile(grammar, 'test') |
137 | 151 |
|
138 |
| - if_exp = 'if' digit ; |
| 152 | + model.parse('nonIF if 1', trace=False) |
139 | 153 |
|
140 |
| - word = /\w+/ ; |
141 |
| - digit = /\d/ ; |
142 |
| - """ |
| 154 | + with pytest.raises(FailedParse): |
| 155 | + model.parse('i rf if 1', trace=False) |
143 | 156 |
|
144 |
| - model = compile(grammar, 'test') |
| 157 | + with pytest.raises(FailedParse): |
| 158 | + model.parse('IF if 1', trace=False) |
145 | 159 |
|
146 |
| - model.parse('nonIF if 1', trace=False) |
147 | 160 |
|
148 |
| - with self.assertRaises(FailedParse): |
149 |
| - model.parse('i rf if 1', trace=False) |
| 161 | +def test_keywords_are_str(): |
| 162 | + grammar = r""" |
| 163 | + @@keyword :: True False |
150 | 164 |
|
151 |
| - with self.assertRaises(FailedParse): |
152 |
| - model.parse('IF if 1', trace=False) |
| 165 | + start = $ ; |
| 166 | + """ |
| 167 | + model = compile(grammar, 'test') |
| 168 | + assert model.keywords == ['True', 'False'] |
| 169 | + assert all(isinstance(k, str) for k in model.keywords) |
0 commit comments