-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.py
187 lines (153 loc) · 6.12 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
from . import (
Position,
Token, TokenType, KEYWORDS, DIGITS, LETTERS, SKIP_LETTERS, LETTERS_DIGITS,
COMMENT_SYMBOL, SYMBOL_TO_TOKENS, NEW_LINES,
IllegalCharError, ExpectedCharError
)
class Lexer:
def __init__(self, fn, text):
self.fn = fn
self.text = text
self.pos = Position(-1, 0, -1, fn, text)
self.current_char = None
self.save_newlines = 0
self.advance()
def advance(self):
self.pos.advance(self.current_char)
self.current_char = self.text[self.pos.idx] if self.pos.idx < len(
self.text) else None
def make_tokens(self):
tokens = []
while self.current_char != None:
if self.current_char in SKIP_LETTERS:
self.advance()
elif self.current_char == COMMENT_SYMBOL:
self.skip_comment()
elif self.current_char in DIGITS:
tokens.append(self.make_number())
elif self.current_char in LETTERS:
tokens.append(self.make_identifier())
elif self.current_char in SYMBOL_TO_TOKENS:
if (self.current_char not in NEW_LINES) or (self.save_newlines == 0):
tokens.append(Token(SYMBOL_TO_TOKENS[self.current_char], pos_start=self.pos))
if tokens[-2].type == TokenType.TT_NEWLINE:
tokens.pop(-2)
if self.current_char == "(":
self.save_newlines += 1
elif self.current_char == ")":
self.save_newlines -= 1
self.advance()
elif self.current_char == '"':
tokens.append(self.make_string())
elif self.current_char == '-':
tokens.append(self.make_minus_or_arrow())
elif self.current_char == '=':
tokens.append(self.make_equals())
elif self.current_char == '<':
tokens.append(self.make_less_than())
elif self.current_char == '>':
tokens.append(self.make_greater_than())
elif self.current_char == '!':
token, error = self.make_not_equals()
if error:
return [], error
tokens.append(token)
else:
pos_start = self.pos.copy()
char = self.current_char
self.advance()
return [], IllegalCharError(pos_start, self.pos, "'" + char + "'")
tokens.append(Token(TokenType.TT_EOF, pos_start=self.pos))
return tokens, None
def make_number(self):
num_str = ''
dot_count = 0
pos_start = self.pos.copy()
while self.current_char != None and self.current_char in DIGITS + '.':
if self.current_char == '.':
if dot_count == 1:
break
dot_count += 1
num_str += self.current_char
self.advance()
if dot_count == 0:
return Token(TokenType.TT_INT, int(num_str), pos_start, self.pos)
else:
return Token(TokenType.TT_FLOAT, float(num_str), pos_start, self.pos)
def make_string(self):
string = ''
pos_start = self.pos.copy()
escape_character = False
self.advance()
escape_characters = {
'n': '\n',
't': '\t'
}
while self.current_char != None and (self.current_char != '"' or escape_character):
if escape_character:
string += escape_characters.get(self.current_char,
self.current_char)
else:
if self.current_char == '\\':
escape_character = True
else:
string += self.current_char
self.advance()
escape_character = False
self.advance()
return Token(TokenType.TT_STRING, string, pos_start, self.pos)
def make_identifier(self):
keyword = ''
pos_start = self.pos.copy()
while self.current_char != None and self.current_char in LETTERS_DIGITS + '_':
keyword += self.current_char
self.advance()
tok_type = TokenType.TT_KEYWORD if keyword in KEYWORDS else TokenType.TT_IDENTIFIER
if tok_type == TokenType.TT_KEYWORD:
keyword = KEYWORDS[keyword]
return Token(tok_type, keyword, pos_start, self.pos)
def make_minus_or_arrow(self):
tok_type = TokenType.TT_MINUS
pos_start = self.pos.copy()
self.advance()
if self.current_char == '>':
self.advance()
tok_type = TokenType.TT_ARROW
return Token(tok_type, pos_start=pos_start, pos_end=self.pos)
def make_not_equals(self):
pos_start = self.pos.copy()
self.advance()
if self.current_char == '=':
self.advance()
return Token(TokenType.TT_NE, pos_start=pos_start, pos_end=self.pos), None
self.advance()
return None, ExpectedCharError(pos_start, self.pos, "'=' (after '!')")
def make_equals(self):
tok_type = TokenType.TT_EQ
pos_start = self.pos.copy()
self.advance()
if self.current_char == '=':
self.advance()
tok_type = TokenType.TT_EE
return Token(tok_type, pos_start=pos_start, pos_end=self.pos)
def make_less_than(self):
tok_type = TokenType.TT_LT
pos_start = self.pos.copy()
self.advance()
if self.current_char == '=':
self.advance()
tok_type = TokenType.TT_LTE
return Token(tok_type, pos_start=pos_start, pos_end=self.pos)
def make_greater_than(self):
tok_type = TokenType.TT_GT
pos_start = self.pos.copy()
self.advance()
if self.current_char == '=':
self.advance()
tok_type = TokenType.TT_GTE
return Token(tok_type, pos_start=pos_start, pos_end=self.pos)
def skip_comment(self):
self.advance()
while self.current_char is not None and self.current_char not in NEW_LINES:
self.advance()
self.advance()