-
Notifications
You must be signed in to change notification settings - Fork 2
/
lexer.py
235 lines (214 loc) · 8.14 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import re
import FA
import sys
from colorama import Fore, Style
# Style
class Format:
end = '\033[0m'
underline = '\033[4m'
tokenCollection = [
# Tuples for defining JS syntax to GRAMMAR
# IGNORE TYPES
(r'[ \t]+', None),
(r'//[^\n]*', None),
(r'/\*[^\n]+[ \t]*[//]*[\w\W]*[$\n]*\*/', None),
# DATA TYPES
(r'\"[^\"\n]*\"', "string"),
(r'\'[^\'\n]*\'', "string"),
(r'\bvar\b', "var"),
(r'\blet\b', "let"),
(r'\bconst\b', "const"),
# SYNTAX
(r'\bfunction\b', "function"),
(r'\breturn\b', "return"),
(r'\bif\b', "if"),
(r'\belse\b', "else"),
(r'\bfor\b', "for"),
(r'\bof\b', "of"),
(r'\bwhile\b', "while"),
(r'\bcontinue\b', "continue"),
(r'\bbreak\b', "break"),
(r'\btrue\b', "true"),
(r'\bfalse\b', "false"),
(r'\bnull\b', "null"),
(r'\bswitch\b', "switch"),
(r'\bcase\b', "case"),
(r'\bdefault\b', "default"),
(r'\btry\b', "try"),
(r'\bcatch\b', "catch"),
(r'\bthrow\b', "throw"),
(r'\bfinally\b', "finally"),
(r'\bthrow\b', "throw"),
(r'\bimport\b', "import"),
(r'\bfrom\b', "from"),
(r'\bexport\b', "export"),
(r'\bdelete\b', "delete"),
(r'\btypeof\b', "typeof"),
(r'\bin\b', "in"),
(r'\binstanceof\b', "instanceof"),
(r'\bInfinity\b', "infinity"),
(r'\bin\b', "in"),
(r'\bof\b', "of"),
(r'\bas\b', "as"),
# ESCAPE SEQUENCE
(r'\n', "newline"),
(r'\(', "kbki"), # kurung biasa kiri
(r'\)', "kbka"), # kurung biasa kanan
(r'\{', "kkki"), # kurung kurawal kiri
(r'\}', "kkka"), # kurung kurawal kanan
(r'\[', "kski"), # kurung siku kiri
(r'\]', "kska"), # kurung siku kanan
(r'\:', "td"), # titik dua
(r'\;', "tk"), # titik koma
(r'\,', "km"), # koma
# Operator (boolean)
(r'\===', "isstricteq"),
(r'\!==', "notstricteq"),
(r'\==', "iseq"),
(r'\!=', "isneq"),
(r'\<=', "leq"),
(r'<', "l"), # less
(r'>=', "geq"),
(r'>', "g"), # greater
(r'&&', "and"), # and boolean
(r'\|\|', "or"), # or boolean
(r'\!', "notb"), # not boolean
# ternary (dipasangkan dengan :) e.g. x = expr1 ? val1 : val2
(r'\?', "qb"),
# Operator (arithmetic)
(r'\*\*', "pow"),
(r'\+\=', "peq"), # plus equal
(r'\-\=', "meq"), # minus equal
(r'\*\=', "muleq"), # multiply equal
(r'\/\=', "diveq"), # div equal
(r'\+\+', "inc"), # increment
(r'\-\-', "dec"), # decrement
(r'\^', "xor"),
(r'&', "andb"), # and bitwise
(r'\|', "orb"), # or bitwise
(r'\=', "is"), # assigment
(r'\+', "sum"), # sum , concat str
(r'\-', "sub"),
(r'\*', "mul"),
(r'/', "div"),
(r'%', "mod"),
(r'~', "notbit"),
# Var Name, Class method, Obj Props
(r'\.', "titik"),
(r'[0-9]*[\$]*[a-zA-Z_][a-zA-Z0-9_]*', "id"),
(r'[\+\-]?[0-9]+\.[0-9]+', "number"),
(r'[\+\-]?[0-9]+[e-]?[0-9]*', "number"),
(r'[\+\-]?[0-9]+', "number"),
]
def lexer(text, tokenCollection):
currentPos = 1 # position in current line
currentLine = 1 # current line
usedTokens = []
pointerText = 0 # text pointer
endLoop = False
while(pointerText < len(text) and not endLoop):
if text[pointerText] == '\n':
currentLine += 1
currentPos = 1
match = None
for tokenExpr in tokenCollection:
pattern, tokenTag = tokenExpr
regex = re.compile(pattern) # init regex
# check till current pointer
match = regex.match(text, pointerText)
if match: # check matcher
if tokenTag: # get token
currentTokens = tokenTag
if (currentTokens == "id"):
if (FA.checkVar(match.group(0))):
usedTokens.append(currentTokens)
else:
endLoop = True
break
else:
usedTokens.append(currentTokens)
break
if not match:
endLoop = True
break
else: # set current text pointer to end of current match position
# set pointer to end of current match position
pointerText = match.end(0)
currentPos += 1
counterNewLine = 1
foundErrorLine = False
currentLineStr = ""
indexError = 0
if (endLoop):
for i in range(len(text)):
if (text[i] == '\n' and not foundErrorLine):
counterNewLine += 1
else:
if (counterNewLine == currentLine):
if (text[i] == '\n' or i == len(text)):
break
else:
if text[i] == text[pointerText]:
indexError = i
foundErrorLine = True
currentLineStr += text[i]
print()
print(Fore.RED, end="")
print("|------------------|")
print("| Syntax Error !!! |")
print("|------------------|")
print(Style.RESET_ALL, end="")
print("\n>>> Line " + str(currentLine) + " : " + "\"", end="")
for j in range(len(currentLineStr)):
if j == indexError:
print(Fore.RED, end="")
print(Format.underline + "" + currentLineStr[j] + Format.end, end="")
print(Style.RESET_ALL, end="")
else:
print(Fore.GREEN, end="")
print(currentLineStr[j], end="")
print(Style.RESET_ALL, end="")
print("\".\n")
print()
sys.exit(1)
else:
if (FA.checkExpr(usedTokens) == True):
return usedTokens
else:
currentNewline = 0
errorIndex = FA.checkExpr(usedTokens)
newlineCounter = 0
for i in range(errorIndex):
if (usedTokens[i] == 'newline'):
newlineCounter += 1
i = 0
while (currentNewline < newlineCounter):
if (text[i] == '\n'):
currentNewline += 1
i += 1
while (text[i] != '\n'):
currentLineStr += text[i]
i += 1
currentNewline += 1
print()
print(Fore.RED, end="")
print("|------------------------|")
print("| Invalid Expression !!! |")
print("|------------------------|")
print(Style.RESET_ALL, end="")
print("\n>>> Line " + str(currentNewline) + " : ", end="")
print(Fore.RED, end="")
print(Format.underline + "\"" + currentLineStr + "\"." + Format.end, end="")
print(Style.RESET_ALL)
print()
sys.exit(1)
def parseToToken(path):
file = open(path)
text = file.read()
file.close()
usedTokens = lexer(text, tokenCollection)
tokenInText = []
for token in usedTokens:
tokenInText.append(token)
resultToken = list(filter(lambda x: x != 'newline', tokenInText))
return resultToken