-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.py
194 lines (160 loc) · 6.44 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
"""Lexer class that breaks down all the source char by char"""
from errors import LexerError
from token import Token, TokenType
###############################################################################
# #
# LEXER #
# #
###############################################################################
def _build_reserved_keywords():
"""Build a dictionary of reserved keywords.
The function relies on the fact that in the TokenType
enumeration the beginning of the block of reserved keywords is
marked with PROGRAM and the end of the block is marked with
the END keyword.
Result:
{'PROGRAM': <TokenType.PROGRAM: 'PROGRAM'>,
'INTEGER': <TokenType.INTEGER: 'INTEGER'>,
'REAL': <TokenType.REAL: 'REAL'>,
'DIV': <TokenType.INTEGER_DIV: 'DIV'>,
'VAR': <TokenType.VAR: 'VAR'>,
'PROCEDURE': <TokenType.PROCEDURE: 'PROCEDURE'>,
'BEGIN': <TokenType.BEGIN: 'BEGIN'>,
'END': <TokenType.END: 'END'>}
"""
# enumerations support iteration, in definition order
tt_list = list(TokenType)
start_index = tt_list.index(TokenType.PROGRAM)
end_index = tt_list.index(TokenType.END)
reserved_keywords = {
token_type.value: token_type
for token_type in tt_list[start_index:end_index + 1]
}
return reserved_keywords
RESERVED_KEYWORDS = _build_reserved_keywords()
class Lexer:
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]
# token line number and column number
self.lineno = 1
self.column = 1
def error(self):
s = "Lexer error on '{lexeme}' line: {lineno} column: {column}".format(
lexeme=self.current_char,
lineno=self.lineno,
column=self.column,
)
raise LexerError(message=s)
def advance(self):
"""Advance the `pos` pointer and set the `current_char` variable."""
if self.current_char == '\n':
self.lineno += 1
self.column = 0
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]
self.column += 1
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 skip_comment(self):
while self.current_char != '}':
self.advance()
self.advance() # the closing curly brace
def number(self):
"""Return a (multidigit) integer or float consumed from the input."""
# Create a new token with current line and column number
token = Token(type=None, value=None, lineno=self.lineno, column=self.column)
result = ''
while self.current_char is not None and self.current_char.isdigit():
result += self.current_char
self.advance()
if self.current_char == '.':
result += self.current_char
self.advance()
while self.current_char is not None and self.current_char.isdigit():
result += self.current_char
self.advance()
token.type = TokenType.REAL_CONST
token.value = float(result)
else:
token.type = TokenType.INTEGER_CONST
token.value = int(result)
return token
def _id(self):
"""Handle identifiers and reserved keywords"""
# Create a new token with current line and column number
token = Token(type=None, value=None, lineno=self.lineno, column=self.column)
value = ''
while self.current_char is not None and self.current_char.isalnum():
value += self.current_char
self.advance()
token_type = RESERVED_KEYWORDS.get(value.upper())
if token_type is None:
token.type = TokenType.ID
token.value = value
else:
# reserved keyword
token.type = token_type
token.value = value.upper()
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 == '{':
self.advance()
self.skip_comment()
continue
if self.current_char.isalpha():
return self._id()
if self.current_char.isdigit():
return self.number()
if self.current_char == ':' and self.peek() == '=':
token = Token(
type=TokenType.ASSIGN,
value=TokenType.ASSIGN.value, # ':='
lineno=self.lineno,
column=self.column,
)
self.advance()
self.advance()
return token
# single-character token
try:
# get enum member by value, e.g.
# TokenType(';') --> TokenType.SEMI
token_type = TokenType(self.current_char)
except ValueError:
# no enum member with value equal to self.current_char
self.error()
else:
# create a token with a single-character lexeme as its value
token = Token(
type=token_type,
value=token_type.value, # e.g. ';', '.', etc
lineno=self.lineno,
column=self.column,
)
self.advance()
return token
# EOF (end-of-file) token indicates that there is no more
# input left for lexical analysis
return Token(type=TokenType.EOF, value=None)