forked from donnemartin/gitsome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.py
526 lines (459 loc) · 16.6 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
"""
Lexer for xonsh code, written using a hybrid of ``tokenize`` and PLY
"""
import re
import sys
import tokenize
from io import BytesIO
from keyword import kwlist
from ply import lex
from ply.lex import TOKEN, LexToken
from xonsh.tools import VER_3_5, VER_MAJOR_MINOR
future_kwlist = []
token_map = {}
"""
Mapping from ``tokenize`` tokens (or token types) to PLY token types. If a
simple one-to-one mapping from ``tokenize`` to PLY exists, the lexer will look
it up here and generate a single PLY token of the given type. Otherwise, it
will fall back to handling that token using one of the handlers in
``special_handlers``.
"""
# operators
_op_map = {
# punctuation
',': 'COMMA', '.': 'PERIOD', ';': 'SEMI', ':': 'COLON',
'...': 'ELLIPSIS',
# basic operators
'+': 'PLUS', '-': 'MINUS', '*': 'TIMES', '@': 'AT', '/': 'DIVIDE',
'//': 'DOUBLEDIV', '%': 'MOD', '**': 'POW', '|': 'PIPE',
'~': 'TILDE', '^': 'XOR', '<<': 'LSHIFT', '>>': 'RSHIFT',
'<': 'LT', '<=': 'LE', '>': 'GT', '>=': 'GE', '==': 'EQ',
'!=': 'NE', '->': 'RARROW',
# assignment operators
'=': 'EQUALS', '+=': 'PLUSEQUAL', '-=': 'MINUSEQUAL',
'*=': 'TIMESEQUAL', '@=': 'ATEQUAL', '/=': 'DIVEQUAL', '%=': 'MODEQUAL',
'**=': 'POWEQUAL', '<<=': 'LSHIFTEQUAL', '>>=': 'RSHIFTEQUAL',
'&=': 'AMPERSANDEQUAL', '^=': 'XOREQUAL', '|=': 'PIPEEQUAL',
'//=': 'DOUBLEDIVEQUAL',
}
for (op, type) in _op_map.items():
token_map[(tokenize.OP, op)] = type
token_map[tokenize.STRING] = 'STRING'
token_map[tokenize.NEWLINE] = 'NEWLINE'
token_map[tokenize.INDENT] = 'INDENT'
token_map[tokenize.DEDENT] = 'DEDENT'
if VER_3_5 <= VER_MAJOR_MINOR:
token_map[tokenize.ASYNC] = 'ASYNC'
token_map[tokenize.AWAIT] = 'AWAIT'
else:
future_kwlist += ['async', 'await']
_REDIRECT_NAMES = frozenset({'out', 'err', 'all', 'o', 'e', 'a'})
def handle_name(state, token, stream):
"""
Function for handling name tokens
"""
typ = 'NAME'
state['last'] = token
if state['pymode'][-1][0]:
if token.string in kwlist:
typ = token.string.upper()
yield _new_token(typ, token.string, token.start)
else:
# subprocess mode
n = next(stream, None)
string = token.string
if (n is not None and n.string in {'<', '>', '>>'} and
n.start == token.end and token.string in _REDIRECT_NAMES):
# looks like a redirect to me!
e = n.end
string += n.string
n2 = next(stream, None)
if n2 is not None and n2.string == '&' and n2.start == n.end:
string += n2.string
e = n2.end
n2 = next(stream, None)
if n2 is not None:
if (n2.start == e and
(n2.type == tokenize.NUMBER or
(n2.type == tokenize.NAME and
n2.string in _REDIRECT_NAMES))):
string += n2.string
state['last'] = n2
yield _new_token('IOREDIRECT', string, token.start)
else:
state['last'] = n
yield _new_token('IOREDIRECT', string, token.start)
yield from handle_token(state, n2, stream)
else:
state['last'] = n
yield _new_token('IOREDIRECT', string, token.start)
else:
yield _new_token('NAME', token.string, token.start)
if n is not None:
yield from handle_token(state, n, stream)
def _make_special_handler(token_type, extra_check=lambda x: True):
def inner_handler(state, token, stream):
state['last'] = token
if state['pymode'][-1][0]:
yield _new_token(token_type, token.string, token.start)
else:
# subprocess mode
n = next(stream, None)
string = token.string
if (n is not None and
n.string in {'<', '>', '>>'} and
n.start == token.end):
e = n.end
string += n.string
n2 = next(stream, None)
if n2 is not None and n2.string == '&' and n2.start == n.end:
state['last'] = n2
string += n2.string
e = n2.end
n2 = next(stream, None)
if n2 is not None:
if (n2.start == e and
(n2.type == tokenize.NUMBER or
(n2.type == tokenize.NAME and
n2.string in _REDIRECT_NAMES))):
string += n2.string
state['last'] = n2
yield _new_token('IOREDIRECT', string, token.start)
else:
state['last'] = n
yield _new_token('IOREDIRECT', string, token.start)
yield from handle_token(state, n2, stream)
else:
state['last'] = n
yield _new_token('IOREDIRECT', string, token.start)
else:
yield _new_token(token_type, token.string, token.start)
if n is not None:
yield from handle_token(state, n, stream)
return inner_handler
handle_number = _make_special_handler('NUMBER')
"""Function for handling number tokens"""
handle_ampersand = _make_special_handler('AMPERSAND')
"""Function for handling ampersand tokens"""
def handle_dollar(state, token, stream):
"""
Function for generating PLY tokens associated with ``$``.
"""
n = next(stream, None)
if n is None:
m = "missing token after $"
yield _new_token("ERRORTOKEN", m, token.start)
elif n.start != token.end:
m = "unexpected whitespace after $"
yield _new_token("ERRORTOKEN", m, token.start)
elif n.type == tokenize.NAME:
state['last'] = n
yield _new_token('DOLLAR_NAME', '$' + n.string, token.start)
elif n.type == tokenize.OP and n.string == '(':
state['pymode'].append((False, '$(', ')', token.start))
state['last'] = n
yield _new_token('DOLLAR_LPAREN', '$(', token.start)
elif n.type == tokenize.OP and n.string == '[':
state['pymode'].append((False, '$[', ']', token.start))
state['last'] = n
yield _new_token('DOLLAR_LBRACKET', '$[', token.start)
elif n.type == tokenize.OP and n.string == '{':
state['pymode'].append((True, '${', '}', token.start))
state['last'] = n
yield _new_token('DOLLAR_LBRACE', '${', token.start)
else:
e = 'expected NAME, (, [, or {{ after $, but got {0}'
m = e.format(n)
yield _new_token("ERRORTOKEN", m, token.start)
def handle_at(state, token, stream):
"""
Function for generating PLY tokens associated with ``@``.
"""
n = next(stream, None)
if n is None:
state['last'] = token
m = "missing token after @"
yield _new_token("ERRORTOKEN", m, token.start)
elif n.type == tokenize.OP and n.string == '(' and \
n.start == token.end:
state['pymode'].append((True, '@(', ')', token.start))
state['last'] = n
yield _new_token('AT_LPAREN', '@(', token.start)
else:
state['last'] = token
yield _new_token('AT', '@', token.start)
yield from handle_token(state, n, stream)
def handle_question(state, token, stream):
"""
Function for generating PLY tokens for help and superhelp
"""
n = next(stream, None)
if n is not None and n.type == tokenize.ERRORTOKEN and \
n.string == '?' and n.start == token.end:
state['last'] = n
yield _new_token('DOUBLE_QUESTION', '??', token.start)
else:
state['last'] = token
yield _new_token('QUESTION', '?', token.start)
if n is not None:
yield from handle_token(state, n, stream)
def handle_backtick(state, token, stream):
"""
Function for generating PLY tokens representing regex globs.
"""
n = next(stream, None)
found_match = False
sofar = '`'
while n is not None:
sofar += n.string
if n.type == tokenize.ERRORTOKEN and n.string == '`':
found_match = True
break
elif n.type == tokenize.NEWLINE or n.type == tokenize.NL:
break
n = next(stream, None)
if found_match:
state['last'] = n
yield _new_token('REGEXPATH', sofar, token.start)
else:
state['last'] = token
e = "Could not find matching backtick for regex on line {0}"
m = e.format(token.start[0])
yield _new_token("ERRORTOKEN", m, token.start)
def handle_lparen(state, token, stream):
"""
Function for handling ``(``
"""
state['pymode'].append((True, '(', ')', token.start))
state['last'] = token
yield _new_token('LPAREN', '(', token.start)
def handle_lbrace(state, token, stream):
"""
Function for handling ``{``
"""
state['pymode'].append((True, '{', '}', token.start))
state['last'] = token
yield _new_token('LBRACE', '{', token.start)
def handle_lbracket(state, token, stream):
"""
Function for handling ``[``
"""
state['pymode'].append((True, '[', ']', token.start))
state['last'] = token
yield _new_token('LBRACKET', '[', token.start)
def _end_delimiter(state, token):
py = state['pymode']
s = token.string
l, c = token.start
if len(py) > 1:
mode, orig, match, pos = py.pop()
if s != match:
e = '"{}" at {} ends "{}" at {} (expected "{}")'
return e.format(s, (l, c), orig, pos, match)
else:
return 'Unmatched "{}" at line {}, column {}'.format(s, l, c)
def handle_rparen(state, token, stream):
"""
Function for handling ``)``
"""
e = _end_delimiter(state, token)
if e is None:
state['last'] = token
yield _new_token('RPAREN', ')', token.start)
else:
yield _new_token('ERRORTOKEN', e, token.start)
def handle_rbrace(state, token, stream):
"""
Function for handling ``}``
"""
e = _end_delimiter(state, token)
if e is None:
state['last'] = token
yield _new_token('RBRACE', '}', token.start)
else:
yield _new_token('ERRORTOKEN', e, token.start)
def handle_rbracket(state, token, stream):
"""
Function for handling ``]``
"""
e = _end_delimiter(state, token)
if e is None:
state['last'] = token
yield _new_token('RBRACKET', ']', token.start)
else:
yield _new_token('ERRORTOKEN', e, token.start)
def handle_error_space(state, token, stream):
"""
Function for handling special whitespace characters in subprocess mode
"""
if not state['pymode'][-1][0]:
state['last'] = token
yield _new_token('WS', token.string, token.start)
else:
yield from []
def handle_error_token(state, token, stream):
"""
Function for handling error tokens
"""
state['last'] = token
if not state['pymode'][-1][0]:
typ = 'NAME'
else:
typ = 'ERRORTOKEN'
yield _new_token(typ, token.string, token.start)
def handle_ignore(state, token, stream):
"""
Function for handling tokens that should be ignored
"""
yield from []
special_handlers = {
tokenize.NL: handle_ignore,
tokenize.COMMENT: handle_ignore,
tokenize.ENCODING: handle_ignore,
tokenize.ENDMARKER: handle_ignore,
tokenize.NAME: handle_name,
tokenize.NUMBER: handle_number,
tokenize.ERRORTOKEN: handle_error_token,
(tokenize.OP, '&'): handle_ampersand,
(tokenize.OP, '@'): handle_at,
(tokenize.OP, '('): handle_lparen,
(tokenize.OP, ')'): handle_rparen,
(tokenize.OP, '{'): handle_lbrace,
(tokenize.OP, '}'): handle_rbrace,
(tokenize.OP, '['): handle_lbracket,
(tokenize.OP, ']'): handle_rbracket,
(tokenize.ERRORTOKEN, '$'): handle_dollar,
(tokenize.ERRORTOKEN, '`'): handle_backtick,
(tokenize.ERRORTOKEN, '?'): handle_question,
(tokenize.ERRORTOKEN, ' '): handle_error_space,
}
"""
Mapping from ``tokenize`` tokens (or token types) to the proper function for
generating PLY tokens from them. In addition to yielding PLY tokens, these
functions may manipulate the Lexer's state.
"""
def handle_token(state, token, stream):
"""
General-purpose token handler. Makes use of ``token_map`` or
``special_map`` to yield one or more PLY tokens from the given input.
Parameters
----------
state :
The current state of the lexer, including information about whether
we are in Python mode or subprocess mode, which changes the lexer's
behavior
token :
The token (from ``tokenize``) currently under consideration
stream :
A generator from which more tokens can be grabbed if necessary
"""
typ = token.type
st = token.string
pymode = state['pymode'][-1][0]
if not pymode:
if state['last'] is not None and state['last'].end != token.start:
cur = token.start
old = state['last'].end
if cur[0] == old[0] and cur[1] > old[1]:
yield _new_token('WS', token.line[old[1]:cur[1]], old)
if (typ, st) in special_handlers:
yield from special_handlers[(typ, st)](state, token, stream)
elif (typ, st) in token_map:
state['last'] = token
yield _new_token(token_map[(typ, st)], st, token.start)
elif typ in special_handlers:
yield from special_handlers[typ](state, token, stream)
elif typ in token_map:
state['last'] = token
yield _new_token(token_map[typ], st, token.start)
else:
m = "Unexpected token: {0}".format(token)
yield _new_token("ERRORTOKEN", m, token.start)
def get_tokens(s):
"""
Given a string containing xonsh code, generates a stream of relevant PLY
tokens using ``handle_token``.
"""
tokstream = tokenize.tokenize(BytesIO(s.encode('utf-8')).readline)
state = {'indents': [0], 'pymode': [(True, '', '', (0, 0))], 'last': None}
while True:
try:
token = next(tokstream)
yield from handle_token(state, token, tokstream)
except StopIteration:
if len(state['pymode']) > 1:
pm, o, m, p = state['pymode'][-1]
l, c = p
e = 'Unmatched "{}" at line {}, column {}'
yield _new_token('ERRORTOKEN', e.format(o, l, c), (0, 0))
break
except tokenize.TokenError as e:
# this is recoverable in single-line mode (from the shell)
# (e.g., EOF while scanning string literal)
yield _new_token('ERRORTOKEN', e.args[0], (0, 0))
break
except IndentationError as e:
# this is never recoverable
yield _new_token('ERRORTOKEN', e, (0, 0))
break
# synthesize a new PLY token
def _new_token(type, value, pos):
o = LexToken()
o.type = type
o.value = value
o.lineno, o.lexpos = pos
return o
class Lexer(object):
"""Implements a lexer for the xonsh language."""
def __init__(self):
"""
Attributes
----------
fname : str
Filename
last : token
The last token seen.
lineno : int
The last line number seen.
"""
self.fname = ''
self.last = None
def build(self, **kwargs):
"""Part of the PLY lexer API."""
pass
def reset(self):
pass
def input(self, s):
"""Calls the lexer on the string s."""
self.token_stream = get_tokens(s)
def token(self):
"""Retrieves the next token."""
self.last = next(self.token_stream, None)
return self.last
def __iter__(self):
t = self.token()
while t is not None:
yield t
t = self.token()
#
# All the tokens recognized by the lexer
#
tokens = tuple(token_map.values()) + (
'NAME', # name tokens
'NUMBER', # numbers
'WS', # whitespace in subprocess mode
'AMPERSAND', # &
'REGEXPATH', # regex escaped with backticks
'IOREDIRECT', # subprocess io redirection token
'LPAREN', 'RPAREN', # ( )
'LBRACKET', 'RBRACKET', # [ ]
'LBRACE', 'RBRACE', # { }
'AT', # @
'QUESTION', # ?
'DOUBLE_QUESTION', # ??
'AT_LPAREN', # @(
'DOLLAR_NAME', # $NAME
'DOLLAR_LPAREN', # $(
'DOLLAR_LBRACE', # ${
'DOLLAR_LBRACKET', # $[
) + tuple(i.upper() for i in kwlist) + tuple(i.upper() for i in future_kwlist)