-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.py
155 lines (148 loc) · 4.56 KB
/
token.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
import re
### TOKEN TYPES
# UNKNOWN
# OPEN_BRACE {
# CLOSE_BRACE }
# OPEN_PAREN \(
# CLOSE_PAREN \)
# SEMI_COLON ;
# CHAR char
# SHORT short
# INT int
# LONG long
# RETURN return
# IDENTIFIER [a-zA-Z]\w*
# INT_LITERAL [0-9]+
# NEGATION -
# BITWISE_NEGATION ~
# LOGICAL_NEGATION !
# ADDITION +
# MULTIPLICATION *
# DIVISION /
# AND &&
# OR ||
# EQUAL ==
# NOT_EQUAL !=
# LESS_THAN <
# LESS_OR_EQUAL <=
# GREATER_THAN >
# GREATER_OR_EQUAL >=
# MODULO %
# ASSIGNMENT =
# IF if
# ELSE else
# FOR for
# WHILE while
# DO do
# BREAK break
# CONTINUE continue
# COMMA ,
# VOID void
# ADDRESS &
# OPEN_SQUARE [
# CLOSE_SQUARE ]
# DOT .
# TYPEDEF typedef
# STRUCT struct
# ACCESS ->
class Token:
def __init__(self, content):
self.content = content
self.token_type = "UNKNOWN"
def get_token_type(self):
content = self.content
### LITERAL COMPARISONS ###
if content == "{":
self.token_type = "OPEN_BRACE"
elif content == "}":
self.token_type = "CLOSE_BRACE"
elif content == "(":
self.token_type = "OPEN_PAREN"
elif content == ")":
self.token_type = "CLOSE_PAREN"
elif content == ";":
self.token_type = "SEMI_COLON"
elif content == "char":
self.token_type = "CHAR"
elif content == "short":
self.token_type = "SHORT"
elif content == "int":
self.token_type = "INT"
elif content == "long":
self.token_type = "LONG"
elif content == "return":
self.token_type = "RETURN"
elif content == "-":
self.token_type = "NEGATION"
elif content == "~":
self.token_type = "BITWISE_NEGATION"
elif content == "!":
self.token_type = "LOGICAL_NEGATION"
elif content == "+":
self.token_type = "ADDITION"
elif content == "*":
self.token_type = "MULTIPLICATION"
elif content == "/":
self.token_type = "DIVISION"
elif content == "&&":
self.token_type = "AND"
elif content == "||":
self.token_type = "OR"
elif content == "==":
self.token_type = "EQUAL"
elif content == "!=":
self.token_type = "NOT_EQUAL"
elif content == "<":
self.token_type = "LESS_THAN"
elif content == "<=":
self.token_type = "LESS_OR_EQUAL"
elif content == ">":
self.token_type = "GREATER_THAN"
elif content == ">=":
self.token_type = "GREATER_OR_EQUAL"
elif content == "%":
self.token_type = "MODULO"
elif content == "=":
self.token_type = "ASSIGNMENT"
elif content == "if":
self.token_type = "IF"
elif content == "else":
self.token_type = "ELSE"
elif content == "for":
self.token_type = "FOR"
elif content == "while":
self.token_type = "WHILE"
elif content == "do":
self.token_type = "DO"
elif content == "break":
self.token_type = "BREAK"
elif content == "continue":
self.token_type = "CONTINUE"
elif content == ",":
self.token_type = "COMMA"
elif content == "void":
self.token_type = "VOID"
elif content == "&":
self.token_type = "ADDRESS"
elif content == "[":
self.token_type = "OPEN_SQUARE"
elif content == "]":
self.token_type = "CLOSE_SQUARE"
elif content == ".":
self.token_type = "DOT"
elif content == "typedef":
self.token_type = "TYPEDEF"
elif content == "struct":
self.token_type = "STRUCT"
elif content == "->":
self.token_type = "ACCESS"
### REGEX COMPARISONS ###
else:
match = re.search('[a-zA-Z]\w*', content)
if match != None:
self.token_type = "IDENTIFIER"
return self.token_type
match = re.search('[0-9]+', content)
if match != None:
self.token_type = "INT_LITERAL"
return self.token_type