-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.py
More file actions
365 lines (283 loc) · 7.46 KB
/
Copy pathtemplate.py
File metadata and controls
365 lines (283 loc) · 7.46 KB
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
import collections
import re
#imports
class Jison:#extends
symbols = {}
terminals = {}
productions = {}
table = {}
default_actions = {}
version = '0.3.12'
debug = False
action_none = 0
action_shift = 1
action_deduce = 2
action_accept = 3
unput_stack = []
def trace(self):
"""trace"""
def __init__(self):
"""Setup Parser"""
"""@@PARSER_INJECT@@"""
def parser_perform_action(self, yy, yystate, s, o):
"""@@ParserPerformAction@@"""
def parser_lex(self):
token = self.lexerLex()
#end = 1
if token is not None:
return token
return self.Symbols["end"]
def parse_error(self, _str='', _hash=None):
raise Exception(_str)
def lexer_error(self, _str='', _hash=None):
raise Exception(_str)
def parse(self, _input):
if self.table is None:
raise Exception("Empty ")
self.eof = ParserSymbol("Eof", 1)
first_action = ParserAction(0, self.table[0])
first_cached_action = ParserCachedAction(first_action)
stack = collections.deque(first_cached_action)
stack_count = 1
vstack = collections.deque(None)
vstach_count = 1
yy = None
_yy = None
recovering = 0
symbol = None
action = None
err_str = ''
pre_error_symbol = None
state = None
self.set_input(_input)
while True:
# retrieve state number from top of stack
state = stack[stack_count].action.state
# use default actions if available
if state is not None and self.default_actions[state.index]:
action = self.default_actions[state.index]
else:
if symbol is None:
symbol = self.parser_lex()
# read action for current state and first input
if state is not None:
action = state.actions[symbol.index]
else:
action = None
if action is None:
if recovering is 0:
# Report error
expected = []
actions = self.table[state.index].actions
for p in actions:
if self.terminals[p] is not None and p > 2:
expected.push(self.terminals[p].name)
if symbol.index in self.terminals:
got = self.terminals[symbol.index].name
else:
got = "NOTHING"
err_str = "Parser error on line " + self.yy.line_no + ":\n" + self.show + "\nExpecting " + (", ".join(expected)) + ". got '" + got + "'"
self.parse_error(self, err_str, ParserError())
# Jison generated lexer
eof = None
yy = None
match = ''
condition_stack = collections.deque()
rules = {}
conditions = {}
done = False
less = None
_more = False
input = None
offset = None
ranges = None
flex = False
line_expression = re.compile("(?:\r\n?|\n).*")
def set_input(self, _input):
self.input = InputReader(_input)
self._more = self.less = self.done = False
self.yy = ParserValue()#
self.condition_stack.clear()
self.condition_stack.append('INITIAL')
if self.ranges is not None:
self.yy.loc = ParserLocation()
self.yy.loc.set_range(ParserRange(0, 0))
else:
self.yy.loc = ParserLocation()
self.offset = 0
def input(self):
ch = self.input.ch()
self.yy.text += ch
self.yy.leng += 1
self.offset += 1
self.match += ch
lines = self.line_expression.match(ch)
if lines is not None:
self.yy.line_no += 1
self.yy.loc.last_line += 1
else:
self.yy.loc.last_column += 1
if self.ranges is not None:
self.yy.loc.range.y += 1
return ch
def unput(self, ch):
yy = ParserValue()#
_len = len(ch)
lines = self.line_expression.split(ch)
lines_count = len(lines)
self.input.un_ch(_len)
yy.text = self.yy.text[0: _len - 1]
self.offset -= _len
old_lines = self.line_expression.split(self.match)
old_lines_count = len(old_lines)
self.match = self.match[0:len(self.match) - 1]
if lines_count - 1 > 0:
yy.line_no = self.yy.line_no - lines_count - 1
r = self.yy.loc.range
old_lines_length = old_lines[old_lines_count - lines_count] if old_lines[old_lines_count - lines_count] is not None else 0
yy.loc = ParserLocation( self.yy.loc.first_line, self.yy.line_no, self.yy.loc.first_column, self.yy.loc.first_line, None)#TODO
if self.ranges is not None:
yy.loc.range(ParserRange(r.x, r.x + self.yy.leng - _len))
self.unput_stack.push(yy)
def more(self):
self._more = True
def past_input(self):
matched = self.input.to_string()
past = matched[0:len(matched) - len(self.match)]
result = past[-20].replace('\n', '')
if len(past) > 20:
return '...' + result
return result
def upcoming_input(self):
if self.done == false:
next = self.match
next_len = len(next)
if next_len < 20:
next += self.input.to_string()[:20 - next_len]
else:
if next_len > 20:
next = next[:-20] + '...'
return next.replace('\n', '')
def show_position(self):
pre = self.past_input()
c = '-' * len(pre)
return pre + self.upcoming_input() + '\n' + c + '^'
def next(self):
if len(self.unput_stack) > 0:
this.yy = yy
if self.done:
return self.eof
if self.input.done:
self.done = true
if self.more is false:
self.yy.text = ''
self.match = ''
rules = self.current_rules()
class ParserLocation:
first_line = 1
last_line = 0
first_column = 1
last_column = 0
range = None
def __init__(self, first_line = 1, last_line = 0, first_column = 1, last_column = 0):
self.first_line = first_line
self.last_line = last_line
self.first_column = first_column
self.last_column = last_column
def set_range(self, range):
self.range = range
class ParserValue:
leng = 0
loc = None
line_no = 0
text = None
class ParserCachedAction:
def __init__(self, action, symbol=None):
self.action = action
self.symbol = symbol
class ParserAction:
action = None
state = None
symbol = None
def __init__(self, action, state=None, symbol=None):
self.action = action
self.state = state
self.symbol = symbol
class ParserSymbol:
name = None
Index = 0
index = -1
symbols = {}
symbols_by_name = {}
def __init__(self, name, index):
self.name = name
self.index = index
def add_action(self, parser_action):
self.symbols[parser_action.index] = self.symbols_by_name[parser_action.name] = parser_action
class ParserError:
text = None
state = None
symbol = None
line_no = 0
loc = None
expected = None
def __init__(self, text, state, symbol, line_no, loc, expected):
self.text = text
self.state = state
self.symbol = symbol
self.line_no = line_no
self.loc = loc
self.expected = expected
class LexerError:
text = None
token = None
line_no = 0
def __init__(self, text, token, line_no):
self.text = text
self.token = token
self.line_no
class ParserState:
index = 0
actions = []
def __init__(self, index):
self.index = index
def set_actions(self, actions):
self.actions = actions
class ParserRange:
x = 0
y = 0
def __init__(self, x, y):
self.x = x
self.y = y
class InputReader:
input = None
length = 0
done = False
matches = []
position = 0
def __init__(self, _input):
self.input = _input
self.length = len(_input)
def add_match(self, match):
self.matches.append(match)
self.position += len(match)
self.done = (self.position >= self.length)
def ch(self):
ch = self.input[self.position]
self.add_match(ch)
return ch
def un_ch(self, ch_length):
self.position -= ch_length
self.position = max(0, self.position)
self.done = (self.position >= self.length)
def substring(self, start, end):
start = self.position if start == 0 else start + self.position
end = self.length if end == 0 else start + end
return self.input[start:end]
def match(self, rule):
matches = re.search(rule, self.position)
if matches is not None:
return matches.group()
return None
def to_string(self):
return ''.join(self.matches)