本文整理汇总了Python中pypy.rlib.parsing.ebnfparse.make_parse_function函数的典型用法代码示例。如果您正苦于以下问题:Python make_parse_function函数的具体用法?Python make_parse_function怎么用?Python make_parse_function使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了make_parse_function函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parse_python_args
def test_parse_python_args():
regexs, rules, ToAST = parse_ebnf("""
IGNORE: " ";
NAME: "[a-zA-Z_]*";
NUMBER: "0|[1-9][0-9]*";
parameters: ["("] >varargslist<? [")"];
varargslist: (fpdef ("=" test)? [","])* star_or_starstarargs |
fpdef ("=" test)? ([","] fpdef ("=" test)?)* [","]?;
star_or_starstarargs: "*" NAME [","] "**" NAME | "*" NAME | "**" NAME;
fpdef: <NAME> | "(" <fplist> ")";
fplist: fpdef ([","] fpdef)* [","]?;
test: NUMBER;
""")
parse = make_parse_function(regexs, rules)
t = parse("(a)").visit(ToAST())[0]
t = parse("(a,)").visit(ToAST())[0]
t = parse("(a,b,c,d)").visit(ToAST())[0]
t = parse("(a,b,c,d,)").visit(ToAST())[0]
t = parse("((a, b, c),b,c,d,)").visit(ToAST())[0]
t = parse("((a, b, (d, e, (f, g))), b, *args, **kwargs)").visit(ToAST())[0]
t = parse("((a, b, c),b,c,d,*args)").visit(ToAST())[0]
t = parse("((a, b, c),b,c,d,**kwargs)").visit(ToAST())[0]
t = parse("((a, b, c),b,c,d,*args, **args)").visit(ToAST())[0]
t = parse("()").visit(ToAST())[0]
t = parse("(*args, **args)").visit(ToAST())[0]
t = parse("(a=1)").visit(ToAST())[0]
t = parse("(a=2,)").visit(ToAST())[0]
t = parse("(a,b,c,d=3)").visit(ToAST())[0]
t = parse("(a,b,c,d=4,)").visit(ToAST())[0]
t = parse("((a, b, c),b,c,(c, d)=1,)").visit(ToAST())[0]
t = parse("((a, b, c),b,c,d=1,*args)").visit(ToAST())[0]
t = parse("((a, b, c),b,c,d=2,**kwargs)").visit(ToAST())[0]
t = parse("((a, b, c),b,c,(c, d)=4,*args, **args)").visit(ToAST())[0]
t = parse("(self, a, b, args)").visit(ToAST())[0]
示例2: parse_js
def parse_js( path ):
regexs, rules, ToAST = parse_ebnf(ebnf)
parse = make_parse_function(regexs, rules, eof=True)
doc = open(path, 'r').read()
t = parse(doc)
t = ToAST().transform(t)
return t
示例3: test_nest_star_and_questionmark
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"))
示例4: test_example1
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)
示例5: test_example2
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)
示例6: test_escape_quotes
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 == '"'
示例7: test_mix_star_and_questionmark
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
示例8: test_starred_star
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()
示例9: test_double_star
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()
示例10: test_grouping_only_parens
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)
示例11: test_clash_literal_nonterminal
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")
示例12: test_lexer_end_string_corner_case
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 == "."
示例13: test_quoting
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"
示例14: test_bug
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)
示例15: test_transform_greater_than
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"