-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.py
320 lines (282 loc) · 10.9 KB
/
lexer.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
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
import re
from sys import intern
from collections import deque
import tokens
# language defaults
BLOCK_START_STRING = '{%'
BLOCK_END_STRING = '%}'
COMMENT_START_STRING = '{#'
COMMENT_END_STRING = '#}'
VARIABLE_START_STRING = '{{'
VARIABLE_END_STRING = '}}'
operators = {
'+': tokens.ADD,
'-': tokens.SUB,
'/': tokens.DIV,
'*': tokens.MUL,
'//': tokens.FLOOR_DIV,
'**': tokens.POW,
'=': tokens.ASSIGN,
'==': tokens.EQ,
'!=': tokens.NE,
'>=': tokens.GTEQ,
'<=': tokens.LTEQ,
'>': tokens.GT,
'<': tokens.LT,
'(': tokens.LPAREN,
'[': tokens.LBRACKET,
'{': tokens.LBRACE,
')': tokens.RPAREN,
']': tokens.RBRACKET,
'}': tokens.RBRACE,
':': tokens.COLON,
';': tokens.SEMICOLON,
'.': tokens.DOT,
'~': tokens.TILDE,
'|': tokens.PIPE,
',': tokens.COMMA,
}
reversed_operators = dict((v, k) for k, v in operators.items())
name_re = re.compile(r'[a-zA-Z_][a-zA-Z0-9_]*')
string_re = re.compile(r"('([^'\\]*(?:\\.[^'\\]*)*)'"
r'|"([^"\\]*(?:\\.[^"\\]*)*)")', re.S)
integer_re = re.compile(r'\d+')
float_re = re.compile(r'(?<!\.)\d+\.\d+')
whitespace_re = re.compile(r'\s+', re.U)
newline_re = re.compile(r'(\r\n|\r|\n)')
operator_re = re.compile('|'.join(re.escape(o) for o in
sorted(operators, key=lambda x: -len(x))))
ignored_tokens = set([tokens.WHITESPACE, tokens.COMMENT,
tokens.COMMENT_BEGIN, tokens.COMMENT_END])
ignore_if_empty = set([tokens.DATA, tokens.COMMENT, tokens.WHITESPACE])
class Failure(Exception): pass
class TemplateSyntaxError(Exception): pass
def raise_if_failure(obj):
if isinstance(obj, Failure):
raise obj
class Token:
def __init__(self, lineno, type, value):
"""Represents a token"""
self.lineno = lineno
self.type = type
self.value = value
def __repr__(self):
return 'Token(%r, %r, %r)' % (
self.lineno,
self.type,
self.value
)
def test(self, expr):
"""Compares expressions like `name:value` or `name` with the token"""
if ':' in expr:
return expr == '{0}:{1}'.format(self.type, self.value)
return self.type == expr
class TokenStreamIterator:
def __init__(self, stream):
"""Implements an iterator for a `TokenStream`"""
self.stream = stream
def __next__(self):
rv = self.stream.current
if rv.type == tokens.EOF:
self.stream.close()
raise StopIteration()
next(self.stream)
return rv
def __iter__(self):
return self
class TokenStream:
def __init__(self, generator):
"""A token stream with `.current` as the current active token"""
self._iter = generator
self._pushed = deque()
self.current = Token(0, tokens.INITIAL, '')
self.closed = False
next(self)
def __next__(self):
"""Returns the the current token and moves on to the next one"""
if self.closed:
return self.current
rv = self.current
if self._pushed:
self.current = self._pushed.popleft()
else:
try:
self.current = next(self._iter)
except StopIteration:
self.close()
return rv
def __iter__(self):
return TokenStreamIterator(self)
def expect(self, expr):
"""Tests the current with `expr` using `token.test(expr)`"""
assert self.current.test(expr), 'expected %r got %r' % (expr, self.current.type)
return next(self)
def skip_if(self, expr):
if self.current.test(expr):
next(self)
return True
return False
def push(self, token):
self._pushed.append(token)
def look(self):
old_token = next(self)
rv = self.current
self.push(rv)
self.current = old_token
return rv
def close(self):
self.current = Token(self.current.lineno, tokens.EOF, '')
self._iter = None
self.closed = True
@property
def lineno(self):
return self.current.lineno
class Lexer:
def __init__(self, environment):
e = re.escape
c = lambda x: re.compile(x, re.M | re.S)
self.environment = environment
# tag lexing rules
self.tag_rules = [
(name_re, tokens.NAME, None),
(string_re, tokens.STRING, None),
(integer_re, tokens.INTEGER, None),
(float_re, tokens.FLOAT, None),
(operator_re, tokens.OPERATOR, None),
(whitespace_re, tokens.WHITESPACE, None),
]
# root lexing rules
self.root_tag_rules = [
('block', r'%s' % e(BLOCK_START_STRING)),
('comment', r'%s' % e(COMMENT_START_STRING)),
('variable', r'%s' % e(VARIABLE_START_STRING)),
]
# directives
# latest_match: [(regex, regex_groups, new_state)]
self.rules = {
'root': [
(c('(.*?)(?:%s)' % '|'.join(
[r'(?P<%s_begin>%s)' % (n, r)
for n, r in self.root_tag_rules]
)), (tokens.DATA, '#bygroup'), '#bygroup'),
(c('.+'), tokens.DATA, None)
],
tokens.COMMENT_BEGIN: [
(c(r'(.*?)(%s)' % e(COMMENT_END_STRING)),
(tokens.COMMENT, tokens.COMMENT_END), '#pop'),
(c(r'(.+)'), Failure('Expected %s' % tokens.COMMENT_END), None)
],
tokens.BLOCK_BEGIN: [
(c(r'(%s)' % e(BLOCK_END_STRING)), tokens.BLOCK_END, '#pop')
] + self.tag_rules,
tokens.VARIABLE_BEGIN: [
(c(r'(%s)' % e(VARIABLE_END_STRING)), tokens.VARIABLE_END, '#pop')
] + self.tag_rules,
}
def tokenize(self, source):
"""Returns a TokenStream"""
stream_generator = self.tokenizer(source)
# for t in self.wrap(stream_generator):
# print(t)
return TokenStream(self.wrap(stream_generator))
def tokenizer(self, source):
source = '\n'.join(source.splitlines())
lineno = 1
pos = 0
stack = ['root']
current_rule = self.rules[stack[-1]]
brackets_stack = []
while 1:
for regex, group_tokens, next_state in current_rule:
m = regex.match(source, pos)
if not m:
continue
# if there's a bracket mismatch, we continue scanning
# with the operator rule instead of processing the _end token
if brackets_stack and group_tokens in (tokens.BLOCK_END,
tokens.COMMENT_END, tokens.VARIABLE_END):
# print('skippin')
continue
if isinstance(group_tokens, tuple):
for idx, token in enumerate(group_tokens):
raise_if_failure(token)
# bygroups are resolved through match group names
if token == '#bygroup':
for k, v in m.groupdict().items():
if v is not None:
yield lineno, k, v
lineno += v.count('\n')
break
else:
raise RuntimeError("couldn't resolve bygroup"
"dynamically in line %d" % lineno)
else:
# yield normal data as is
data = m.group(idx + 1)
if data or token not in ignore_if_empty:
yield lineno, token, data
lineno += data.count('\n')
else:
raise_if_failure(group_tokens)
data = m.group()
closing_brackets = {
'{': '}',
'[': ']',
'(': ')'
}
# validate bracket pairs
if group_tokens == 'operator':
if data in closing_brackets:
brackets_stack.append(closing_brackets[data])
elif data in closing_brackets.values():
if not brackets_stack:
raise TemplateSyntaxError('unexpected character %s'
' in line %d' % (data, lineno))
expected_ch = brackets_stack.pop()
if data != expected_ch:
raise TemplateSyntaxError('expected %s instead of'
' %s in line %d' % (expected_ch, data, lineno))
if data or group_tokens not in ignore_if_empty:
yield lineno, group_tokens, data
lineno += data.count('\n')
# handle the next state
pos2 = m.end()
if next_state is not None:
if next_state == '#pop':
stack.pop()
elif next_state == '#bygroup':
for k, v in m.groupdict().items():
if v is not None:
stack.append(k)
break
else:
raise RuntimeError("couldn't resolve bygroup"
"dynamically in line %s" % lineno)
else:
stack.append(next_state)
current_rule = self.rules[stack[-1]]
# we have not moved forward
elif pos2 == pos:
raise RuntimeError("%r couldn't match any strings" % regex)
pos = pos2
break
# if we didn't break out, we're at the eof or in an error state
else:
if pos >= len(source):
return
raise TemplateSyntaxError('unexpected char %s in line %d' % (source[pos], lineno))
def wrap(self, stream_generator):
"""Returns a generator wrapping the tokens returned by
`tokenizer` as `Token` instances"""
for lineno, token, value in stream_generator:
if token in ignored_tokens:
continue
elif token == tokens.OPERATOR:
token = operators[value]
elif token == tokens.INTEGER:
value = int(value)
elif token == tokens.FLOAT:
value = float(value)
elif token == tokens.STRING:
value = value[1:-1]
yield Token(lineno, token, value)