-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.py
72 lines (62 loc) · 1.97 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Author: Tim Henderson
#Email: [email protected]
#For licensing see the LICENSE file in the top level directory.
import sys, functools
TOKENSR = (
'NUMBER', 'PLUS', 'DASH', 'STAR', 'SLASH', 'LPAREN', 'RPAREN',
)
TOKENS = dict((k, k) for i, k in enumerate(TOKENSR))
sys.modules[__name__].__dict__.update(TOKENS)
# This simple token class should be largely compatible with the one in PLY
# although I haven't tested that. I simply assume it is true...
class token(object):
def __init__(self, type, value):
self.type = type
self.value = value
def __repr__(self):
return str(self.value)
def __eq__(self, b):
if b is None: return False
if not isinstance(b, token): return False
return self.type == b.type and self.value == b.value
def Lex(inpt):
digits = list()
for x in inpt:
if x.isdigit():
digits.append(x)
elif digits:
yield token(NUMBER, int(''.join(digits)))
digits = list()
if x == ' ': continue
elif x == '\n': continue
elif x == '\t': continue
elif x == '+': yield token(PLUS, x)
elif x == '-': yield token(DASH, x)
elif x == '*': yield token(STAR, x)
elif x == '/': yield token(SLASH, x)
elif x == '(': yield token(LPAREN, x)
elif x == ')': yield token(RPAREN, x)
elif not x.isdigit():
raise Exception, 'Unknown character! "%s"' % (x)
if digits:
yield token(NUMBER, int(''.join(digits)))
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
##
## THIS HACKERY IS NOT ADVISED
##
## I wrote the following to emulate PLY lexer
## you should just use PLY's lexer instead.
##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
def __input(inpt):
Lex.c = Lex(inpt)
def __token():
if not hasattr(Lex, 'c'): return
try:
return Lex.c.next()
except StopIteration:
return
Lex.input = __input
Lex.token = __token