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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
# New parser tests.
import py
import tokenize
import token
import StringIO
from pypy.interpreter.pyparser import parser, metaparser, pygram
from pypy.interpreter.pyparser.test.test_metaparser import MyGrammar
def test_char_set():
first = {5: None, 9: None, 100: None, 255:None}
p = parser.DFA(None, None, first)
for i in range(256):
assert p.could_match_token(i) == (i in first)
class SimpleParser(parser.Parser):
def parse(self, input):
self.prepare()
rl = StringIO.StringIO(input + "\n").readline
gen = tokenize.generate_tokens(rl)
for tp, value, begin, end, line in gen:
if self.add_token(parser.Token(tp, value, begin[0], begin[1], line)):
py.test.raises(StopIteration, gen.next)
return self.root
def tree_from_string(expected, gram):
def count_indent(s):
indent = 0
for char in s:
if char != " ":
break
indent += 1
return indent
last_newline_index = 0
for i, char in enumerate(expected):
if char == "\n":
last_newline_index = i
elif char != " ":
break
if last_newline_index:
expected = expected[last_newline_index + 1:]
base_indent = count_indent(expected)
assert not divmod(base_indent, 4)[1], "not using 4 space indentation"
lines = [line[base_indent:] for line in expected.splitlines()]
last_indent = 0
node_stack = []
for line in lines:
if not line.strip():
continue
data = line.split()
if data[0].isupper():
tp = getattr(token, data[0])
if len(data) == 2:
value = data[1].strip("\"")
elif tp == token.NEWLINE:
value = "\n"
else:
value = ""
n = parser.Terminal(tp, value, 0, 0)
else:
tp = gram.symbol_ids[data[0]]
n = parser.Nonterminal(tp)
new_indent = count_indent(line)
if new_indent >= last_indent:
if new_indent == last_indent and node_stack:
node_stack.pop()
if node_stack:
node_stack[-1].append_child(n)
node_stack.append(n)
else:
diff = last_indent - new_indent
pop_nodes = diff // 4 + 1
del node_stack[-pop_nodes:]
node_stack[-1].append_child(n)
node_stack.append(n)
last_indent = new_indent
return node_stack[0]
class TestParser:
def parser_for(self, gram, add_endmarker=True):
if add_endmarker:
gram += " NEWLINE ENDMARKER\n"
pgen = metaparser.ParserGenerator(gram)
g = pgen.build_grammar(MyGrammar)
return SimpleParser(g), g
def test_multiple_rules(self):
gram = """foo: 'next_rule' bar 'end' NEWLINE ENDMARKER
bar: NAME NUMBER\n"""
p, gram = self.parser_for(gram, False)
expected = """
foo
NAME "next_rule"
bar
NAME "a_name"
NUMBER "42"
NAME "end"
NEWLINE
ENDMARKER"""
input = "next_rule a_name 42 end"
assert tree_from_string(expected, gram) == p.parse(input)
def test_recursive_rule(self):
gram = """foo: NAME bar STRING NEWLINE ENDMARKER
bar: NAME [bar] NUMBER\n"""
p, gram = self.parser_for(gram, False)
expected = """
foo
NAME "hi"
bar
NAME "hello"
bar
NAME "a_name"
NUMBER "32"
NUMBER "42"
STRING "'string'"
NEWLINE
ENDMARKER"""
input = "hi hello a_name 32 42 'string'"
assert tree_from_string(expected, gram) == p.parse(input)
def test_symbol(self):
gram = """parent: first_child second_child NEWLINE ENDMARKER
first_child: NAME age
second_child: STRING
age: NUMBER\n"""
p, gram = self.parser_for(gram, False)
expected = """
parent
first_child
NAME "harry"
age
NUMBER "13"
second_child
STRING "'fred'"
NEWLINE
ENDMARKER"""
input = "harry 13 'fred'"
assert tree_from_string(expected, gram) == p.parse(input)
def test_token(self):
p, gram = self.parser_for("foo: NAME")
expected = """
foo
NAME "hi"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("hi")
py.test.raises(parser.ParseError, p.parse, "567")
p, gram = self.parser_for("foo: NUMBER NAME STRING")
expected = """
foo
NUMBER "42"
NAME "hi"
STRING "'bar'"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("42 hi 'bar'")
def test_optional(self):
p, gram = self.parser_for("foo: [NAME] 'end'")
expected = """
foo
NAME "hi"
NAME "end"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("hi end")
expected = """
foo
NAME "end"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("end")
def test_grouping(self):
p, gram = self.parser_for(
"foo: ((NUMBER NAME | STRING) | 'second_option')")
expected = """
foo
NUMBER "42"
NAME "hi"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("42 hi")
expected = """
foo
STRING "'hi'"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("'hi'")
expected = """
foo
NAME "second_option"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("second_option")
py.test.raises(parser.ParseError, p.parse, "42 a_name 'hi'")
py.test.raises(parser.ParseError, p.parse, "42 second_option")
def test_alternative(self):
p, gram = self.parser_for("foo: (NAME | NUMBER)")
expected = """
foo
NAME "hi"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("hi")
expected = """
foo
NUMBER "42"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("42")
py.test.raises(parser.ParseError, p.parse, "hi 23")
py.test.raises(parser.ParseError, p.parse, "23 hi")
py.test.raises(parser.ParseError, p.parse, "'some string'")
def test_keyword(self):
p, gram = self.parser_for("foo: 'key'")
expected = """
foo
NAME "key"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("key")
py.test.raises(parser.ParseError, p.parse, "")
p, gram = self.parser_for("foo: NAME 'key'")
expected = """
foo
NAME "some_name"
NAME "key"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("some_name key")
py.test.raises(parser.ParseError, p.parse, "some_name")
def test_repeaters(self):
p, gram = self.parser_for("foo: NAME+ 'end'")
expected = """
foo
NAME "hi"
NAME "bye"
NAME "nothing"
NAME "end"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("hi bye nothing end")
py.test.raises(parser.ParseError, p.parse, "end")
py.test.raises(parser.ParseError, p.parse, "hi bye")
p, gram = self.parser_for("foo: NAME* 'end'")
expected = """
foo
NAME "hi"
NAME "bye"
NAME "end"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("hi bye end")
py.test.raises(parser.ParseError, p.parse, "hi bye")
expected = """
foo
NAME "end"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("end")
p, gram = self.parser_for("foo: (NAME | NUMBER)+ 'end'")
expected = """
foo
NAME "a_name"
NAME "name_two"
NAME "end"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("a_name name_two end")
expected = """
foo
NUMBER "42"
NAME "name"
NAME "end"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("42 name end")
py.test.raises(parser.ParseError, p.parse, "end")
p, gram = self.parser_for("foo: (NAME | NUMBER)* 'end'")
expected = """
foo
NAME "hi"
NUMBER 42
NAME "end"
NEWLINE
ENDMARKER"""
assert tree_from_string(expected, gram) == p.parse("hi 42 end")
def test_optimized_terminal(self):
gram = """foo: bar baz 'end' NEWLINE ENDMARKER
bar: NAME
baz: NUMBER
"""
p, gram = self.parser_for(gram, False)
expected = """
foo
bar
NAME "a_name"
baz
NUMBER "42"
NAME "end"
NEWLINE
ENDMARKER"""
input = "a_name 42 end"
tree = p.parse(input)
assert tree_from_string(expected, gram) == tree
assert isinstance(tree, parser.Nonterminal)
assert isinstance(tree.get_child(0), parser.Nonterminal1)
assert isinstance(tree.get_child(1), parser.Nonterminal1)
def test_error_string(self):
p, gram = self.parser_for(
"foo: 'if' NUMBER '+' NUMBER"
)
info = py.test.raises(parser.ParseError, p.parse, "if 42")
info.value.expected_str is None
info = py.test.raises(parser.ParseError, p.parse, "if 42 42")
info.value.expected_str == '+'
|