-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsergenerator.py
216 lines (181 loc) · 5.72 KB
/
parsergenerator.py
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
# This file was generated from the grammar file Grammars/metagrammar.peg #
from pegparsing import BaseParser, memoise, memoise_left_recursive
import argparse
from bootstrapparsergenerator import (
Grammar, Rule, Option, Repeat, Token, Optional, Join, Greedy,
generate_parser_code
)
if __name__ == '__main__':
argparser = argparse.ArgumentParser('Generate a parser from a grammar file')
argparser.add_argument('-i', '-g', '--grammar_file', required=True, help=(
'The grammar file describing the parser(.peg extension)'))
argparser.add_argument('-o', '--out_file', required=True, help=(
'The python file to generate'))
argparser.add_argument('-n', '-c', '--classname', default='Parser', help=(
'The name of the generated parser class'))
args = argparser.parse_args()
code = generate_parser_code(args.grammar_file, args.classname)
with open(args.out_file, 'w') as f:
f.write(code)
class ParserGenerator(BaseParser):
@memoise
def rule_grammar(self):
pos = self.mark()
if (True
and ((t0 := self.rule_preamble()) is not None)
and ((t1 := self.rule_rules()) is not None)
and ((t2 := self.expect('ENDMARKER')) is not None)
):
return Grammar(t1, t0)
self.reset(pos)
return None
@memoise
def rule_preamble(self):
pos = self.mark()
if (True
and ((t0 := self.expect('@')) is not None)
and ((t1 := self.expect('CODEBLOCK')) is not None)
):
return t1.string
self.reset(pos)
return None
@memoise
def rule_rules(self):
pos = self.mark()
if (True
and ((t0 := self.rule_rule()) is not None)
and ((t1 := self.rule_rules()) is not None)
):
return [t0] + t1
self.reset(pos)
if (True
and ((t0 := self.rule_rule()) is not None)
):
return [t0]
self.reset(pos)
return None
@memoise
def rule_rule(self):
pos = self.mark()
if (True
and ((t0 := self.expect('NONTERMINAL')) is not None)
and ((t1 := self.expect(':')) is not None)
and ((t2 := self.rule_options()) is not None)
and ((t3 := self.expect(';')) is not None)
):
return Rule(t0.string, t2)
self.reset(pos)
return None
@memoise
def rule_options(self):
pos = self.mark()
if (True
and ((t0 := self.rule_option()) is not None)
and ((t1 := self.expect('|')) is not None)
and ((t2 := self.rule_options()) is not None)
):
return [t0] + t2
self.reset(pos)
if (True
and ((t0 := self.rule_option()) is not None)
):
return [t0]
self.reset(pos)
return None
@memoise
def rule_option(self):
pos = self.mark()
if (True
and ((t0 := self.rule_items()) is not None)
and ((t1 := self.expect('CODEBLOCK')) is not None)
):
return Option(t0, t1.string)
self.reset(pos)
if (True
and ((t0 := self.rule_items()) is not None)
):
return Option(t0, None)
self.reset(pos)
return None
@memoise
def rule_items(self):
pos = self.mark()
if (True
and ((t0 := self.rule_item()) is not None)
and ((t1 := self.rule_items()) is not None)
):
return [t0] + t1
self.reset(pos)
if (True
and ((t0 := self.rule_item()) is not None)
):
return [t0]
self.reset(pos)
return None
@memoise_left_recursive
def rule_item(self):
pos = self.mark()
if (True
and ((t0 := self.rule_item()) is not None)
and ((t1 := self.expect('*')) is not None)
):
return Repeat(t0)
self.reset(pos)
if (True
and ((t0 := self.rule_item()) is not None)
and ((t1 := self.expect('+')) is not None)
):
return Repeat(t0, nonempty=True)
self.reset(pos)
if (True
and ((t0 := self.rule_item()) is not None)
and ((t1 := self.expect('?')) is not None)
):
return Optional(t0)
self.reset(pos)
if (True
and ((t0 := self.rule_item()) is not None)
and ((t1 := self.expect('^')) is not None)
and ((t2 := self.rule_token()) is not None)
):
return Join(t0, t2)
self.reset(pos)
if (True
and ((t0 := self.rule_item()) is not None)
and ((t1 := self.expect('$')) is not None)
and ((t2 := self.rule_item()) is not None)
):
return Greedy(t0, t2)
self.reset(pos)
if (True
and ((t0 := self.expect('(')) is not None)
and ((t1 := self.rule_options()) is not None)
and ((t2 := self.expect(')')) is not None)
):
return Rule(None, t1)
self.reset(pos)
if (True
and ((t0 := self.rule_token()) is not None)
):
return t0
self.reset(pos)
return None
@memoise
def rule_token(self):
pos = self.mark()
if (True
and ((t0 := self.expect('TERMINAL')) is not None)
):
return t0
self.reset(pos)
if (True
and ((t0 := self.expect('NONTERMINAL')) is not None)
):
return t0
self.reset(pos)
if (True
and ((t0 := self.expect('STRING')) is not None)
):
return t0
self.reset(pos)
return None