-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeg.txt
86 lines (64 loc) · 2.16 KB
/
peg.txt
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
~grammar = (ws r : rule)+ ws !. {{ result = ast_grammar(r, countr); }}
~ws = (" " / "\n" / "\t")*
~wsp = (" " / "\n" / "\t")+
~rule = "~" <name> ws "=" ws b : body
{{
result = ast_rule(c(0), *b);
}}
~name = [A-Za-z0-9_]+
~body = o : alt {{ result = *o; }}
~alt = first : seq (ws "/" ws rest : seq)*
{{
result = ast_Alt(first, rest, countrest + 1);
}}
~seq = first : operator (wsp rest : operator)*
{{
result = ast_Seq(first, rest, countrest + 1);
}}
~operator = o : prefix (ws "{{" ws <code> ws "}}")?
{{
if (ccount) { result = ast_with_code(c(0), *o); } else { result = *o; }
}}
/ o : postfix (ws "{{" ws <code> ws "}}")?
{{
if (ccount) { result = ast_with_code(c(0), *o); } else { result = *o; }
}}
/ o : group (ws "{{" ws <code> ws "}}")?
{{
if (ccount) { result = ast_with_code(c(0), *o); } else { result = *o; }
}}
~prefix = and ws o : group {{ result = ast_And(*o); }}
/ not ws o : group {{ result = ast_Not(*o); }}
~and = "&"
~not = "!"
~postfix = o : group ws star {{ result = ast_Star(*o); }}
/ o : group ws plus {{ result = ast_Plus(*o); }}
/ o : group ws option {{ result = ast_Option(*o); }}
~star = "*"
~plus = "+"
~option = "?"
~group = "(" ws o : body ws ")" {{ result = *o; }}
/ "<" ws o : body ws ">" {{ result = ast_Capture(*o); }}
/ o : final {{ result = *o; }}
~final = o : nonterminal {{ result = *o; }}
/ o : literal {{ result = *o; }}
/ o : cclass {{ result = *o; }}
/ o : dot {{ result = *o; }}
~nonterminal = <name> ws ":" ws <name>
{{
result = ast_AliasedNontm(c(1), c(0));
}}
/ <name>
{{
result = ast_Nontm(c(0));
}}
~literal = "\"" <(&"\\" . . / !"\"" .)*> "\""
{{
result = ast_Literal(c(0));
}}
~cclass = "[" <(&"\\" . . / !"]" .)*> "]"
{{
result = ast_CClass(c(0));
}}
~dot = "." {{ result = ast_dot(); }}
~code = (&"\\" . . / !"}}" .)*