-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.py
202 lines (167 loc) · 4.81 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
import sys
from enum import Enum, auto
from typing import Any
class L(Enum):
EOF = auto()
SQBL = auto()
SQBR = auto()
FLOAT = auto()
INT = auto()
STRING = auto()
OPERATOR = auto()
CONST_ASS = auto()
LET_ASS = auto()
GT = auto()
LT = auto()
EQ = auto()
ID = auto()
TERM = auto()
IF = auto()
ELSE = auto()
THEN = auto()
ELIF = auto()
BL = auto()
BR = auto()
COMMENT = auto()
FUNC_DECL = auto()
FUNC_ARROW = auto()
def isatom(kind):
return kind in (L.FLOAT, L.INT, L.STRING, L.OPERATOR)
class Lexer:
SYMBOLS = {
'[': L.SQBL,
']': L.SQBR,
'(': L.BL,
')': L.BR,
}
OPERATORS = ['+', '-', '*', '/']
def __init__(self, filename=None):
self.fdescriptor = open(filename, 'r') if filename else sys.stdin
self.char = self.fdescriptor.read(1)
self.sym = None
self.line = 1
self.tok = (None, None)
def eat(self, amount=1):
self.char = self.fdescriptor.read(amount)
def line_report(self):
pos = self.fdescriptor.tell()
self.fdescriptor.seek(0)
print('Line:', self.line)
while self.line > 1:
self.line -= 1
self.fdescriptor.readline()
line_start = self.fdescriptor.tell()
report = self.fdescriptor.readline()
report += ' ' * (pos - line_start - 2) + '^'
print(report)
def fail(self, reason):
print('-> Lexer error:', reason)
self.line_report()
def cur_tok(self): return self.tok
def read_tok(self):
ch = self.char
tok = None
if ch == '\n':
self.eat()
self.line += 1
return self.read_tok()
if ch == '.':
self.eat()
tok = (L.TERM, None)
elif len(ch) == 0:
tok = (L.EOF, None)
elif ch.isdigit(): # integer OR float
s = ''
while self.char.isdigit() or self.char == '.':
s += self.char
self.eat()
if '.' in s:
tok = (L.FLOAT, float(s))
else:
tok = (L.INT, int(s))
elif ch.isspace(): # Pass out extra whitespaces
self.eat()
# return self.read_tok()
tok = self.read_tok()
elif ch == '"': # String atom
s = ''
self.eat()
while self.char != '"':
s += self.char
self.eat()
self.eat()
tok = (L.STRING, s)
elif ch == '<': # GT operator OR assignment
self.eat()
if self.char == '-':
self.eat()
tok = (L.CONST_ASS, None)
elif self.char == '~':
self.eat()
tok = (L.LET_ASS, None)
else:
tok = (L.LT, None)
elif ch == '>':
self.eat()
tok = (L.GT, None)
# Comments processing
elif ch == '-':
self.eat()
if self.char == '{':
self.eat()
s = ''
while self.char != '}':
s += self.char
self.eat()
self.eat()
self.eat() # Eat }-
tok = (L.COMMENT, s)
elif self.char == '>':
self.eat()
tok = (L.FUNC_ARROW, None)
else:
tok = (L.OPERATOR, ch)
elif ch in self.OPERATORS:
# TODO: handle // OPERATOR
self.eat()
tok = (L.OPERATOR, ch)
elif ch in self.SYMBOLS:
self.eat()
tok = (self.SYMBOLS[ch], None)
elif ch.isalpha():
s = ''
while self.char.isalpha():
s += self.char
self.eat()
tok = (L.ID, s)
elif ch == '?': # if-else-expression
# TODO: handle infinite else expression
self.eat()
if self.char == ':':
self.eat()
tok = (L.IF, None)
else:
tok = (L.ELIF, None)
elif ch == ':':
self.eat()
if self.char == ':':
tok = (L.FUNC_DECL, None)
elif self.char == '?':
tok = (L.ELSE, None)
self.eat()
elif ch == '|':
self.eat()
tok = (L.THEN, None)
else:
self.fail(f'Unexpected character: [{self.char}]')
return
self.tok = tok
return tok
def next_token(self) -> (L, Any):
return self.read_tok()
if __name__ == '__main__':
l = Lexer(sys.argv[1] if len(sys.argv) > 1 else 'code-samples/simple.xp')
t = (None, None)
while t[0] != L.EOF:
t = l.next_token()
print(t)