Source

pypy / pypy / rlib / parsing / test / test_ebnfparse.py

The branch 'callback-jit' does not exist.
  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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import py
from pypy.rlib.parsing.tree import Nonterminal, Symbol, RPythonVisitor
from pypy.rlib.parsing.parsing import PackratParser, Symbol, ParseError, Rule
from pypy.rlib.parsing.ebnfparse import parse_ebnf, make_parse_function
from pypy.rlib.parsing.test.test_parse import EvaluateVisitor

from sets import Set

def test_simple():
    regexs, rules, transformer = parse_ebnf("""
IGNORE: " ";
DECIMAL: "0|[1-9][0-9]*";
additive: multitive "+" additive | multitive;
multitive: primary "*" multitive | primary; #nonsense!
# the following too!
primary: "(" additive ")" | DECIMAL;
""")
    class MyEvalVisitor(EvaluateVisitor):
        def visit_primary(self, node):
            if len(node.children) == 3:
                return node.children[1].visit(self)
            return int(node.children[0].additional_info)
    parse = make_parse_function(regexs, rules)
    tree = parse("0 + 10 + 999")
    assert tree.visit(MyEvalVisitor()) == 10 + 999
    tree = parse("22 * 12 + 44)")
    r = tree.visit(MyEvalVisitor())
    assert r == 22 * 12 + 44
    tree = parse("2*(3+5*2*(2+61))")
    assert tree.visit(MyEvalVisitor()) == 2 * (3 + 5 * 2 * (2 + 61))
    tree = parse("12 + 4 * 5)")

def test_long_inline_quotes():
    regexs, rules, transformer = parse_ebnf("""
DECIMAL: "0|[1-9][0-9]*";
IGNORE: " ";
additive: multitive "+!" additive | multitive;
multitive: primary "*!" multitive | primary; #nonsense!
primary: "(" additive ")" | DECIMAL;
""")
    class MyEvalVisitor(EvaluateVisitor):
        def visit_primary(self, node):
            if len(node.children) == 3:
                return node.children[1].visit(self)
            return int(node.children[0].additional_info)
    parse = make_parse_function(regexs, rules)
    tree = parse("0 +! 10 +! 999")
    assert tree.visit(MyEvalVisitor()) == 10 + 999

def test_toast():
    regexs, rules, ToAST = parse_ebnf("""
DECIMAL: "0|[1-9][0-9]*";
IGNORE: " ";
additive: multitive ["+!"] additive | <multitive>;
multitive: primary ["*!"] multitive | <primary>; #nonsense!
primary: "(" <additive> ")" | <DECIMAL>;
""")
    parse = make_parse_function(regexs, rules)
    tree = parse("(0 +! 10) *! (999 +! 10) +! 1")
    tree = tree.visit(ToAST())[0]
    assert len(tree.children) == 2
    assert tree.children[0].children[0].symbol == "additive"
    assert tree.children[1].symbol == "DECIMAL"

def test_eof():
    grammar = """
DECIMAL: "0|[1-9][0-9]*";
IGNORE: " ";
expr: additive0_9 EOF;
additive0_9: multitive "+!" additive0_9 | multitive;
multitive: primary "*!" multitive | primary; #nonsense!
primary: "(" additive0_9 ")" | DECIMAL;
"""
    regexs, rules, ToAST = parse_ebnf(grammar)
    class MyEvalVisitor(EvaluateVisitor):
        def visit_primary(self, node):
            if len(node.children) == 3:
                return node.children[1].visit(self)
            return int(node.children[0].additional_info)
        def visit_expr(self, node):
            return node.children[0].visit(self)
        visit_additive0_9 = EvaluateVisitor.visit_additive
    parse = make_parse_function(regexs, rules, eof=True)
    tree = parse("0 +! 10 +! 999")
    assert tree.visit(MyEvalVisitor()) == 10 + 999
    py.test.raises(ParseError, parse, "0 +! 10 +! 999 0000")
    grammar += "some garbage here"
    py.test.raises(ParseError, parse_ebnf, grammar)


def test_prolog():
    regexs, rules, ToAST = parse_ebnf("""
ATOM: "[a-z]([a-zA-Z0-9]|_)*";
VAR: "[A-Z]([a-zA-Z0-9]|_)*|_";
NUMBER: "0|[1-9][0-9]*";
IGNORE: "[ \\n\\t]";
file: fact file | fact;
fact: complexterm "." | complexterm ":-" compoundexpr ".";
compoundexpr: complexterm "," compoundexpr | complexterm ";" compoundexpr | complexterm;
complexterm: ATOM "(" exprlist ")" | ATOM;
exprlist: expr "," exprlist | expr;
expr: complexterm | ATOM | NUMBER | VAR;
""")
    parse = make_parse_function(regexs, rules)
    tree = parse("prefix(\n\tlonger(and_nested(term(X))), Xya, _, X0, _).")
    assert tree.children[0].children[0].children[2].children[0].getsourcepos().lineno == 1
    assert tree is not None
    tree = parse("""
foo(X, Y) :- bar(Y, X), bar(Y, X) ; foobar(X, Y, 1234, atom).""")
    assert tree is not None

def test_toast_bigger():
    regexs, rules, ToAST = parse_ebnf("""
BOOLCONST: "TRUE|FALSE";
IDENTIFIER: "[a-zA-Z_][a-zA-Z0-9_]*";
NUMBER: "0|[1-9][0-9]*";
IGNORE: " ";
# expression
expr: <intexpr> | <boolexpr>;
intexpr: multitive "+" intexpr | multitive "-" intexpr | <multitive>;
multitive: primary "*" unaryexpr | primary "/" unaryexpr |
           primary "%" unaryexpr | <unaryexpr>;
unaryexpr: "+" unaryexpr | "-" unaryexpr | <primary>;
primary: "(" <intexpr> ")" | <NUMBER> | <IDENTIFIER>;
boolexpr: <BOOLCONST>; #strange thing
""")
    parse = make_parse_function(regexs, rules)
    tree = parse("x * floor + 1")
    tree = ToAST().transform(tree)
    assert tree.children[2].symbol == "NUMBER"

def test_parser_repr_is_evalable():
    regexs, rules, ToAST = parse_ebnf("""
BOOLCONST: "TRUE|FALSE";
IDENTIFIER: "[a-zA-Z_][a-zA-Z0-9_]*";
NUMBER: "0|[1-9][0-9]*";
IGNORE: " ";
# expression
expr: <intexpr> | <boolexpr>;
intexpr: multitive "+" intexpr | multitive "-" intexpr | <multitive>;
multitive: primary "*" unaryexpr | primary "/" unaryexpr |
           primary "%" unaryexpr | <unaryexpr>;
unaryexpr: "+" unaryexpr | "-" unaryexpr | <primary>;
primary: "(" <intexpr> ")" | <NUMBER> | <IDENTIFIER>;
boolexpr: <BOOLCONST>; #strange thing

""")
    parser = PackratParser(rules, rules[0].nonterminal)
    s = repr(parser)
    print s
    newparser = eval(s)
    assert repr(newparser) == s

def test_lexer_end_string_corner_case():
    regexs, rules, ToAST = parse_ebnf("""
NUMBER: "[0-9]*(\.[0-9]+)?";
ATOM: "\.";
IGNORE: " ";
expr: NUMBER ATOM EOF;
""")
    parse = make_parse_function(regexs, rules, eof=True)
    t = parse("2.")
    assert t.children[0].additional_info == "2"
    assert t.children[1].additional_info == "."

def test_escape_quotes():
    regexs, rules, ToAST = parse_ebnf("""
QUOTE: "a\\"";
IGNORE: " ";
expr: QUOTE "\\"" EOF;""")
    parse = make_parse_function(regexs, rules, eof=True)
    t = parse('a" "')
    assert t.children[0].additional_info == 'a"'
    assert t.children[1].additional_info == '"'

def test_leftrecursion():
    regexs, rules, ToAST = parse_ebnf("""
A: "a";
B: "b";
IGNORE: " |\n";
expr1: A | expr2 A;
expr2: expr1 B;
""")
    py.test.raises(AssertionError, make_parse_function, regexs, rules, True)

def test_jsonparse():
    source = """
    STRING: "\\"[^\\\\"]*\\"";
    NUMBER: "\-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][\+\-]?[0-9]+)?";
    IGNORE: " |\n";
    value: <STRING> | <NUMBER> | <object> | <array> | <"null"> |
           <"true"> | <"false">;
    object: ["{"] (entry [","])* entry ["}"];
    array: ["["] (value [","])* value ["]"];
    entry: STRING [":"] value;
"""
    regexs, rules, ToAST = parse_ebnf(source)
    parse = make_parse_function(regexs, rules, eof=True)
    t = parse("""
{
    "type": "SCRIPT",
    "0": {
        "type": "SEMICOLON",
        "end": "5",
        "expression": {
            "type": "ASSIGN",
            "0": {
                "type": "IDENTIFIER",
                "assignOp": "null",
                "end": "1",
                "lineno": "1",
                "start": "0",
                "tokenizer": "[object Object]",
                "value": "x"
            } ,
            "1": {
                "type": "NUMBER",
                "end": "5",
                "lineno": "1",
                "start": "4",
                "tokenizer": "[object Object]",
                "value": "1"
            } ,
            "end": "5",
            "length": "2",
            "lineno": "1",
            "start": "0",
            "tokenizer": "[object Object]",
            "value": "="
        } ,
        "lineno": "1",
        "start": "0",
        "tokenizer": "[object Object]",
        "value": "x"
    } ,
    "funDecls": "",
    "length": "1",
    "lineno": "1",
    "tokenizer": "[object Object]",
    "varDecls": ""
}""")
    t = ToAST().transform(t)
    t = parse('{"a": "5", "b": [1, null, 3, true, {"f": "g", "h": 6}]}')
    print "".join(list(t.dot()))
    t = ToAST().transform(t)
    print "\n".join(list(t.dot()))

def test_starparse():
    regexs, rules, ToAST = parse_ebnf("""
    QUOTED_STRING: "'[^\\']*'";
    IGNORE: " |\n";
    list: ["["] (QUOTED_STRING [","])* QUOTED_STRING ["]"];
""")
    parse = make_parse_function(regexs, rules, eof=True)
    t = parse("""['a', 'b', 'c']""")
    t = ToAST().transform(t)
    assert t.symbol == "list"
    assert len(t.children) == 3
    assert [c.symbol for c in t.children] == ["QUOTED_STRING"] * 3
    t = parse("['a']")
    t = ToAST().transform(t)
    assert t.symbol == "list"
    assert len(t.children) == 1
    assert [c.symbol for c in t.children] == ["QUOTED_STRING"] * 1

def test_double_star():
    regexs, rules, ToAST = parse_ebnf("""
    IGNORE: " |\n";
    start: "a"* "b"* "c";
""")
    parse = make_parse_function(regexs, rules, eof=True)
    for s in ["a a a b b c", "a b b c", "a a c", "b b c", "c"]:
        t = parse(s)
        t = ToAST().transform(t)
        assert [c.additional_info for c in t.children] == s.split()

def test_transform_star():
    #py.test.skip("This needs to be fixed - generated transformer is buggy")
    regexs, rules, ToAST = parse_ebnf("""
    IGNORE: " ";
    ATOM: "[\+a-zA-Z_][a-zA-Z0-9_]*";

    sexpr: ATOM | list;
    list: "(" sexpr* ")";
""")
    parse = make_parse_function(regexs, rules)
    tree = parse("()")
    list_expr = tree.visit(ToAST())[0].children[0]
    assert list_expr.symbol == 'list'
    # should have two children, "(" and ")"
    assert len(list_expr.children) == 2
    assert list_expr.children[0].additional_info == '('
    assert list_expr.children[1].additional_info == ')'
    tree = parse("(a b c)")
    list_expr = ToAST().transform(tree)


def test_quoting():
    regexs, rules, ToAST = parse_ebnf("""
    ATOM: "[a-z]*";
    IGNORE: " ";
    list: ATOM "\n" ATOM;
""")
    parse = make_parse_function(regexs, rules, eof=True)
    t = parse("""abc
  abd""")
    assert len(t.children) == 3
    assert t.children[1].additional_info == "\n"

def test_check_for_missing_names():
    regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
DECIMAL: "0|[1-9][0-9]*";
additive: multitive "+" additive | multitive;
multitive: primary "*" multitive | primari; # observe the typo
# the following too!
primary: "(" additive ")" | DECIMAL;
""")
    excinfo = py.test.raises(ValueError, make_parse_function, regexs, rules)
    assert "primari" in str(excinfo.value)

def test_starred_star():
    regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
start: ("b"* "a")* EOF;
    """)
    parse = make_parse_function(regexs, rules, eof=True)
    for s in ["b b b b a b b a", "b a b a", "a a", ""]:
        t = parse(s)
        t = ToAST().transform(t)
        assert [c.additional_info for c in t.children] == (s + " EOF").split()

def test_transform_greater_than():
    regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
x: ["a"] >b< "c";
b: "A" "A";
    """)
    parse = make_parse_function(regexs, rules)
    t = parse("a A A c")
    t = ToAST().transform(t)
    assert len(t.children) == 3
    assert t.children[0].additional_info == "A"
    assert t.children[1].additional_info == "A"
    assert t.children[2].additional_info == "c"

def test_plus():
    regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
x: "A"+ "B";
    """)
    parse = make_parse_function(regexs, rules)
    t = parse("A A B")
    t = ToAST().transform(t)
    assert len(t.children) == 3
    assert t.children[0].additional_info == "A"
    assert t.children[1].additional_info == "A"
    assert t.children[2].additional_info == "B"
    py.test.raises(ParseError, parse, "B")

def test_questionmark():
    regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
x: ["A"] ("B" ["C"] "D")? "E";
    """)
    parse = make_parse_function(regexs, rules)
    t = parse("A B C D E")
    py.test.raises(ParseError, parse, "A B C D B C D E")
    t = ToAST().transform(t)
    assert len(t.children) == 3
    assert t.children[0].additional_info == "B"
    assert t.children[1].additional_info == "D"
    assert t.children[2].additional_info == "E"
    t = parse("A  E")
    t = ToAST().transform(t)
    assert len(t.children) == 1
    assert t.children[0].additional_info == "E"

def test_grouping_only_parens():
    regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
x: ["m"] ("a" "b") "c" | <y>;
y: ["n"] "a" "b" "c";
    """)
    parse = make_parse_function(regexs, rules)
    t0 = ToAST().transform(parse("m a b c"))
    t1 = ToAST().transform(parse("n a b c"))
    assert len(t0.children) == len(t1.children)

def test_mix_star_and_questionmark():
    regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
y: x "END";
x: "B" ("A" "B")* "A"?;
    """)
    parse = make_parse_function(regexs, rules)
    t = ToAST().transform(parse("B A B END"))
    assert len(t.children[0].children) == 3

def test_nest_star_and_questionmark():
    regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
y: x "END";
x: "B" ("A" "B"?)*;
    """)
    parse = make_parse_function(regexs, rules)
    t = ToAST().transform(parse("B A B A B END"))
    t = ToAST().transform(parse("B A A A END"))

def test_clash_literal_nonterminal():
    regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
y: x "END";
x: "y";
a: "x";
    """)
    parse = make_parse_function(regexs, rules)
    py.test.raises(ParseError, parse, "x END")
    parse("y END")

def test_example1():
    regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
n: "a" "b" "c" m;
m: "(" <n> ")" | "d";
    """)
    parse = make_parse_function(regexs, rules)
    t = parse("a b c (a b c d)")
    t = ToAST().transform(t)

def test_example2():
    regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
DECIMAL: "0|[1-9][0-9]*";
list: DECIMAL >list< | DECIMAL;
    """)
    parse = make_parse_function(regexs, rules)
    t = parse("1 2 3 4 5")
    t = ToAST().transform(t)

def test_bug():
    # this could be seen as using the transformer in the wrong way
    # but I have no clue how to detect this situation
    py.test.skip("fix me somehow")
    regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
DECIMAL: "0|[1-9][0-9]*";
list: DECIMAL >list< | <DECIMAL>;
    """)
    parse = make_parse_function(regexs, rules)
    t = parse("1 2 3 4 5")
    t = ToAST().transform(t)

def test_empty_production():
    # this could be seen as using the transformer in the wrong way
    # but I have no clue how to detect this situation
    regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
DECIMAL: "0|[1-9][0-9]*";
file: <stuff> EOF;
stuff: "a" >stuff< "a" | "y" | >empty<;
empty: ;
    """)
    parse = make_parse_function(regexs, rules, eof=True)
    t = parse(" ")
    t = ToAST().transform(t)
    assert isinstance(t, Nonterminal)
    assert len(t.children) == 0
    t = parse(" a  a  a  a a    a ")
    t = ToAST().transform(t)
    assert len(t.children) == 6
    excinfo = py.test.raises(ParseError, parse, "a")

def test_zero_repetition_production():
    grammar = """
IGNORE: " ";
foo: "A"?;
"""
    excinfo = py.test.raises(ValueError, parse_ebnf, grammar)
    assert "foo" in str(excinfo.value)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.