-
Notifications
You must be signed in to change notification settings - Fork 2
/
lexer.py
181 lines (144 loc) · 5.35 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
from tokens import *
class Lexer(object):
def __init__(self, text):
# client string input, e.g. "4 + 2 * 3 - 6 / 2"
self.text = text
# self.pos is an index into self.text
self.pos = 0
self.current_char = self.text[self.pos]
def error(self):
raise Exception('Invalid character')
def advance(self):
"""Advance the `pos` pointer and set the `current_char` variable."""
self.pos += 1
if self.pos > len(self.text) - 1:
self.current_char = None # Indicates end of input
else:
self.current_char = self.text[self.pos]
def peek(self):
peek_pos = self.pos + 1
if peek_pos > len(self.text) - 1:
return None
else:
return self.text[peek_pos]
def skip_whitespace(self):
while self.current_char is not None and self.current_char.isspace():
self.advance()
def _id(self):
"""Handle identifiers and reserved keywords"""
result = ''
while self.current_char is not None and self.current_char.isalnum():
result += self.current_char
self.advance()
token = KEYWORDS.get(result, Token(ID, result))
return token
def number(self):
"""Return a (multidigit) integer or float consumed from the input."""
result = ''
while self.current_char is not None and self.current_char.isdigit():
result += self.current_char
self.advance()
token = Token(INTEGER, int(result))
return token
def get_next_token(self):
"""Lexical analyzer (also known as scanner or tokenizer)
This method is responsible for breaking a sentence
apart into tokens. One token at a time.
"""
while self.current_char is not None:
if self.current_char.isspace():
self.skip_whitespace()
continue
if self.current_char.isalpha():
return self._id()
if self.current_char == '(':
self.advance()
return Token(LPAREN, '(')
if self.current_char == ')':
self.advance()
return Token(RPAREN, ')')
if self.current_char == '{':
self.advance()
return Token(OPENCURLY, "{")
if self.current_char == "}":
self.advance()
return Token(CLOSE_CURLY, "}")
if self.current_char == ';':
self.advance()
return Token(SEMICOLON, ';')
if self.current_char == ",":
self.advance()
return Token(COMA, ',')
if self.current_char.isdigit():
return self.number()
if self.current_char == '+':
self.advance()
return Token(PLUS, '+')
if self.current_char == '-':
self.advance()
return Token(MINUS, '-')
if self.current_char == '*':
self.advance()
return Token(MUL, '*')
if self.current_char == '/':
self.advance()
if self.current_char == '/':
self.advance()
return Token(INT_DIV, '//')
else:
self.error()
if self.current_char == '"':
return self.string()
if self.current_char == ":":
self.advance()
return Token(COLON, ":")
if self.current_char == '>':
next_char = self.peek()
if next_char == '=':
self.advance()
self.advance()
return Token(BIGEQ, '>=')
else:
self.advance()
return Token(BIG, '>')
if self.current_char == '<':
next_char = self.peek()
if next_char == '=':
self.advance()
self.advance()
return Token(LESSEQ, '<=')
else:
self.advance()
return Token(LESS, '>')
if self.current_char == '=':
next_char = self.peek()
if next_char == '=':
self.advance()
self.advance()
return Token(EQ, '==')
else:
self.advance()
return Token(EQUALS, '=')
if self.current_char == '!':
self.advance()
if self.current_char == '=':
self.advance()
return Token(NOTEQ, '!=')
else:
self.error()
self.error()
return Token(EOF, None)
def peek_token(self):
"""This function return the next toke without changing the lexers state (without changing the current token)"""
old_pos, old_char = self.pos, self.current_char
token = self.get_next_token()
self.pos, self.current_char = old_pos, old_char
return token
def string(self):
self.advance()
result = ""
while self.current_char != '"':
result += self.current_char
self.advance()
self.advance()
return Token(STRING, result)