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