Debian Codegraph @

Tools/peg_generator/pegen/metagrammar.gram

raw
1 @subheader """\
2 from ast import literal_eval
3
4 from pegen.grammar import (
5 Alt,
6 Cut,
7 Forced,
8 Gather,
9 Group,
10 Item,
11 Lookahead,
12 LookaheadOrCut,
13 MetaTuple,
14 MetaList,
15 NameLeaf,
16 NamedItem,
17 NamedItemList,
18 NegativeLookahead,
19 Opt,
20 Plain,
21 PositiveLookahead,
22 Repeat0,
23 Repeat1,
24 Rhs,
25 Rule,
26 RuleList,
27 RuleName,
28 Grammar,
29 StringLeaf,
30 )
31 """
32
33 start[Grammar]: grammar ENDMARKER { grammar }
34
35 grammar[Grammar]:
36 | metas rules { Grammar(rules, metas) }
37 | rules { Grammar(rules, []) }
38
39 metas[MetaList]:
40 | meta metas { [meta] + metas }
41 | meta { [meta] }
42
43 meta[MetaTuple]:
44 | "@" NAME NEWLINE { (name.string, None) }
45 | "@" a=NAME b=NAME NEWLINE { (a.string, b.string) }
46 | "@" NAME STRING NEWLINE { (name.string, literal_eval(string.string)) }
47
48 rules[RuleList]:
49 | rule rules { [rule] + rules }
50 | rule { [rule] }
51
52 rule[Rule]:
53 | rulename memoflag? ":" alts NEWLINE INDENT more_alts DEDENT {
54 Rule(rulename[0], rulename[1], Rhs(alts.alts + more_alts.alts), memo=opt) }
55 | rulename memoflag? ":" NEWLINE INDENT more_alts DEDENT {
56 Rule(rulename[0], rulename[1], more_alts, memo=opt) }
57 | rulename memoflag? ":" alts NEWLINE { Rule(rulename[0], rulename[1], alts, memo=opt) }
58
59 rulename[RuleName]:
60 | NAME annotation { (name.string, annotation) }
61 | NAME { (name.string, None) }
62
63 # In the future this may return something more complicated
64 memoflag[str]:
65 | '(' "memo" ')' { "memo" }
66
67 alts[Rhs]:
68 | alt "|" alts { Rhs([alt] + alts.alts)}
69 | alt { Rhs([alt]) }
70
71 more_alts[Rhs]:
72 | "|" alts NEWLINE more_alts { Rhs(alts.alts + more_alts.alts) }
73 | "|" alts NEWLINE { Rhs(alts.alts) }
74
75 alt[Alt]:
76 | items '$' action { Alt(items + [NamedItem(None, NameLeaf('ENDMARKER'))], action=action) }
77 | items '$' { Alt(items + [NamedItem(None, NameLeaf('ENDMARKER'))], action=None) }
78 | items action { Alt(items, action=action) }
79 | items { Alt(items, action=None) }
80
81 items[NamedItemList]:
82 | named_item items { [named_item] + items }
83 | named_item { [named_item] }
84
85 named_item[NamedItem]:
86 | NAME annotation '=' ~ item {NamedItem(name.string, item, annotation)}
87 | NAME '=' ~ item {NamedItem(name.string, item)}
88 | item {NamedItem(None, item)}
89 | forced=forced_atom {NamedItem(None, forced)}
90 | it=lookahead {NamedItem(None, it)}
91
92 forced_atom[Forced]:
93 | '&''&' ~ atom {Forced(atom)}
94
95 lookahead[LookaheadOrCut]:
96 | '&' ~ atom {PositiveLookahead(atom)}
97 | '!' ~ atom {NegativeLookahead(atom)}
98 | '~' {Cut()}
99
100 item[Item]:
101 | '[' ~ alts ']' {Opt(alts)}
102 | atom '?' {Opt(atom)}
103 | atom '*' {Repeat0(atom)}
104 | atom '+' {Repeat1(atom)}
105 | sep=atom '.' node=atom '+' {Gather(sep, node)}
106 | atom {atom}
107
108 atom[Plain]:
109 | '(' ~ alts ')' {Group(alts)}
110 | NAME {NameLeaf(name.string) }
111 | STRING {StringLeaf(string.string)}
112
113 # Mini-grammar for the actions and annotations
114
115 action[str]: "{" ~ target_atoms "}" { target_atoms }
116 annotation[str]: "[" ~ target_atoms "]" { target_atoms }
117
118 target_atoms[str]:
119 | target_atom target_atoms { target_atom + " " + target_atoms }
120 | target_atom { target_atom }
121
122 target_atom[str]:
123 | "{" ~ atoms=target_atoms? "}" { "{" + (atoms or "") + "}" }
124 | "[" ~ atoms=target_atoms? "]" { "[" + (atoms or "") + "]" }
125 | NAME "*" { name.string + "*" }
126 | NAME { name.string }
127 | NUMBER { number.string }
128 | STRING { string.string }
129 | FSTRING_START { fstring_start.string }
130 | FSTRING_MIDDLE { fstring_middle.string }
131 | FSTRING_END { fstring_end.string }
132 | "?" { "?" }
133 | ":" { ":" }
134 | !"}" !"]" OP { op.string }