-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.py
182 lines (157 loc) · 5.61 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
import sys
import tu
from tu import logger
from tu import SUBPHASE_TAG_STR
from tu import dump_checksum
import tok
from tok import TokenType
from tok import Token
chksum_file = '/tmp/toyc.lex'
################################
# read each line and generate the tokens
def is_digit_or_letter(ch):
if ch >= '0' and ch <= '9':
return True
if ch >= 'a' and ch <= 'z':
return True
if ch >= 'A' and ch <= 'Z':
return True
return False
def read_line(line, lineno):
num = len(line)
inx = 0
col_b = 1
while inx < num:
cur_char = line[inx]
if cur_char in tok.whitespace:
logger.debug("\t%d-th char at col %d: WS" % (inx, col_b))
else:
logger.debug("\t%d-th char at col %d: %c" % (inx, col_b, cur_char))
# single character token
if cur_char in tok.single_char_tok_map.keys():
logger.debug("\t\tis single character token.")
token_type = tok.single_char_tok_map[cur_char]
token = Token(token_type, None, lineno, lineno, col_b, col_b)
tu.toks.append(token)
inx = inx + 1
col_b = col_b + 1
continue
# escape continuous whitespace
if cur_char in tok.whitespace:
while cur_char in tok.whitespace:
logger.debug("\t\tis whitespace.")
if cur_char == '\t':
col_b = col_b + tok.chars_per_tab
else:
col_b = col_b + 1
inx = inx + 1
if inx == num:
break
cur_char = line[inx]
if inx == num:
break
continue
# = and ==
if cur_char == '=':
if line[inx + 1] == '=':
logger.debug("\t\tis ==")
token = Token(TokenType.OP_EQ, None, lineno,
lineno, col_b, col_b + 1)
tu.toks.append(token)
inx = inx + 2
col_b = col_b + 2
else:
logger.debug("\t\tis =")
token = Token(TokenType.SIGN_ASSIGN, None,
lineno, lineno, col_b, col_b)
tu.toks.append(token)
inx = inx + 1
col_b = col_b + 1
continue
# !=
if cur_char == '!':
if line[inx + 1] == '=':
logger.debug("\t\tis !=")
token = Token(TokenType.OP_NOT_EQ, None,
lineno, lineno, col_b, col_b + 1)
tu.toks.append(token)
inx = inx + 2
col_b = col_b + 2
continue
else:
err_str = "tokenization failed at line:%d, column:%d. '!=' is expected." % (
lineno, col_b + 1)
if tu.FLAG_CHECK == tu.FlagCheck.LEX:
dump_checksum(chksum_file, err_str)
logger.error(err_str)
sys.exit()
# keywrod/identifier/constant
if is_digit_or_letter(cur_char):
prev_inx = inx
while is_digit_or_letter(cur_char):
inx = inx + 1
if inx == num:
break
cur_char = line[inx]
tok_str = line[prev_inx: inx]
prev_col_b = col_b
col_b = col_b + inx - prev_inx
logger.debug("\t\tis string:%s" % tok_str)
if tok_str in tok.keyword_tok_map.keys():
token_type = tok.keyword_tok_map[tok_str]
token = Token(token_type, None, lineno,
lineno, prev_col_b, col_b - 1)
elif tok_str.isdigit():
# TODO: lexer errors should be raised for '0123'
token = Token(TokenType.CONST_VAL, int(tok_str), lineno,
lineno, prev_col_b, col_b - 1)
else:
token = Token(TokenType.VAR, tok_str, lineno,
lineno, prev_col_b, col_b - 1)
tu.toks.append(token)
continue
# unrecoginized token
err_str = "unrecognized token at line:%d, column:%d" % (lineno, col_b)
if tu.FLAG_CHECK == tu.FlagCheck.LEX:
dump_checksum(chksum_file, err_str)
logger.error(err_str)
sys.exit()
################################
# dump tokens
def dump_tokens():
tokens_str = ""
logger.debug("%d tokens:" % len(tu.toks))
inx = 0
for tok in tu.toks:
assert isinstance(tok, Token), "tok must be Token!"
inx_str = "%d-th token: " % (inx)
tok_info = tok.info(True)
logger.debug(inx_str + tok_info)
tokens_str += inx_str + tok_info + '\t'
inx = inx + 1
return tokens_str
################################
# main entry
def lex():
logger.debug(SUBPHASE_TAG_STR)
logger.debug('Start to tokenize')
with open(tu.src_file_name, 'r') as src_file:
lines = src_file.readlines()
lineno = 1
for line in lines:
logger.debug("analyzing line: %d" % lineno)
read_line(line, lineno)
lineno = lineno + 1
src_file.close()
# dump all the tokens
logger.debug(SUBPHASE_TAG_STR)
logger.debug('Get all the tokens')
tokens_str = dump_tokens()
# generate the checksum and dump it
if tu.FLAG_CHECK == tu.FlagCheck.LEX:
import hashlib
tokens_checksum = hashlib.md5(tokens_str.encode('utf-8')).hexdigest()
logger.debug("token checksum:%s" % tokens_checksum)
dump_checksum(chksum_file, tokens_checksum)
# finish
logger.info('Done')