-
Notifications
You must be signed in to change notification settings - Fork 0
/
hclex.py
executable file
·208 lines (161 loc) · 2.74 KB
/
hclex.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
203
204
205
206
207
#!/usr/bin/env python3
from ply import lex, yacc
from hcexceptions import LexerError
keywords = {
"init": "INIT",
"input": "INPUT",
"output": "OUTPUT",
"if": "IF",
"else": "ELSE",
"forever": "FOREVER",
"while": "WHILE",
"true": "TRUE",
"false": "FALSE",
}
tokens = (
"NL",
"WS",
"COMMENT",
"IDENTIFIER",
"NUMBER",
"EQUALS",
"DBL_EQUALS",
"NOT_EQUALS",
"LESS_THAN",
"LESS_THAN_OR_EQUAL",
"GREATER_THAN",
"GREATER_THAN_OR_EQUAL",
"AT",
"ADD",
"DBL_ADD",
"SUBTRACT",
"DBL_SUB",
"MULTIPLY",
"OPEN_BRACKET",
"CLOSE_BRACKET",
"BANG",
"DBL_AND",
"DBL_OR",
"ADD_EQUALS",
"SUB_EQUALS",
"MUL_EQUALS",
*keywords.values(),
)
def track(tok):
if not hasattr(tok.lexer, "colno"):
tok.lexer.colno = 1
tok.colno = tok.lexer.colno
tok.lexer.colno += len(tok.value)
return tok
def t_NL(t):
r"\n"
t.colno = t.lexer.colno
t.lexer.lineno += 1
t.lexer.colno = 1
return t
def t_WS(t):
r"[\t ]+"
return track(t)
def t_COMMENT(t):
r"//[^\n]*"
return track(t)
def t_IDENTIFIER(t):
r"[a-zA-Z_][a-zA-Z_\d]*"
# Check for reserved words
if t.value in keywords:
t.type = keywords[t.value]
return track(t)
def t_NUMBER(t):
r"\d+"
track(t)
t.value = int(t.value)
return t
def t_DBL_EQUALS(t):
r"=="
return track(t)
def t_NOT_EQUALS(t):
r"!="
return track(t)
def t_EQUALS(t):
r"="
return track(t)
def t_BANG(t):
r"!"
return track(t)
def t_DBL_AND(t):
r"&&"
return track(t)
def t_DBL_OR(t):
r"\|\|"
return track(t)
def t_LESS_THAN_OR_EQUAL(t):
r"<="
return track(t)
def t_LESS_THAN(t):
r"<"
return track(t)
def t_GREATER_THAN_OR_EQUAL(t):
r">="
return track(t)
def t_GREATER_THAN(t):
r">"
return track(t)
def t_AT(t):
r"@"
return track(t)
def t_ADD_EQUALS(t):
r"\+="
return track(t)
def t_DBL_ADD(t):
r"\+\+"
return track(t)
def t_ADD(t):
r"\+"
return track(t)
def t_SUB_EQUALS(t):
r"-="
return track(t)
def t_DBL_SUB(t):
r"--"
return track(t)
def t_SUBTRACT(t):
r"-"
return track(t)
def t_MUL_EQUALS(t):
r"\*="
return track(t)
def t_MULTIPLY(t):
r"\*"
return track(t)
def t_OPEN_BRACKET(t):
r"\("
return track(t)
def t_CLOSE_BRACKET(t):
r"\)"
return track(t)
def t_error(t):
raise LexerError(f"Unexpected character at "
f"line {t.lineno}, col {t.lexer.colno}: "
+ repr(t.value.rstrip('\n')))
def main():
import sys
import argparse
parser = argparse.ArgumentParser(description="Lex .hc files")
parser.add_argument("input", default=None)
args = parser.parse_args()
f = None
close_f = False
if args.input is None:
f = sys.stdin
else:
f = open(args.input)
close_f = True
for line in f:
lex.input(line)
for token in iter(lex.token, None):
print(token.type, repr(token.value))
if close_f:
f.close()
lex.lex()
if __name__ == "__main__":
main()