-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatcher_scanner.py
More file actions
496 lines (418 loc) · 15.6 KB
/
watcher_scanner.py
File metadata and controls
496 lines (418 loc) · 15.6 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# Standard library to take input as command line argument
import sys
# Import some helper functions
from global_helpers import error, is_alpha, is_alnum, is_digit
# Import Token class
from token_class import Token
# Import Scanner class
from scanner_class import Scanner
def is_keyword(value):
"""
Checks if string is keyword or not
Params
======
value (str)
: The string to be checked for keyword
Returns
=======
(bool)
: Whether the value passed is a keyword or not
"""
return value in [
"and",
"or",
"var",
"print",
"while",
"input",
"if",
"else",
"class",
"fun",
"for",
"do",
"not",
"true",
"false",
"elif",
]
def keyword_identifier(source_code, i, table, scanner_obj):
"""
Process keywords and identifiers in source code
Params
======
source_code (str)
: The string containing pulse source code
i (int)
: The current index in the source code
table (SymbolTable)
: Symbol table constructed holding information about identifiers and constants
scanner_obj (Scanner)
: Instance of Scanner class
Returns
=======
(Token)
: The token generated for the keyword or identifier
(int)
: Current position in source code
"""
value = ""
# Loop until we get a non-digit character
while is_alnum(source_code[i]):
value += source_code[i]
i += 1
# Check if value is keyword or not
if is_keyword(value):
return Token(value, "", scanner_obj.line_num), i
# Check if identifier is in symbol table
id = table.get_by_symbol(value)
# If identifier is not in symbol table then give a placeholder datatype var
if id == -1:
id = table.entry(value, "var", "variable")
# Returns the id, token and current index in source code
return Token("id", id, scanner_obj.line_num), i
def string_val(source_code, i, table, scanner_obj, start_char='"'):
"""
Processes string values in the source code
Params
======
source_code (str)
: The string containing simc source code
i (int)
: The current index in the source code
table (SymbolTable)
: Symbol table constructed holding information about identifiers and constants
scanner_obj (Scanner)
: Instance of Scanner class
start_char (str) (Optional)
: Character with which string starts
Returns
=======
(Token)
: The token generated for the string constant
(int)
: Current position in source code
"""
string_constant = ""
# Skip the first "/' so that the string atleast makes into the while loop
i += 1
# Loop until we get a non-digit character
while source_code[i] != start_char:
if source_code[i] == "\0":
error("Unterminated string!", scanner_obj.line_num)
string_constant += source_code[i]
i += 1
# Skip the "/' character so that it does not loop back to this function incorrectly
i += 1
# Put appropriate quote
string_constant = '"' + string_constant + '"'
# Make entry in symbol table
id = table.entry(string_constant, "string", "constant")
# Return string token and current index in source code
return Token("string", id, scanner_obj.line_num), i
def numeric_val(source_code, i, table, scanner_obj):
"""
Processes numeric values in the source code
Params
======
source_code (str)
: The string containing simc source code
i (int)
: The current index in the source code
table (SymbolTable)
: Symbol table constructed holding information about identifiers and constants
scanner_obj (Scanner)
: Instance of Scanner class
Returns
=======
(Token)
: The token generated for the numeric constant
(int)
: Current position in source code
"""
numeric_constant = ""
# Loop until we get a non-digit character
while is_digit(source_code[i]):
numeric_constant += source_code[i]
i += 1
# If a numeric constant contains more than 1 decimal point (.) then that is invalid
if numeric_constant.count(".") > 1:
error(
"Invalid numeric constant, cannot have more than one decimal point in a"
" number!",
scanner_obj.line_num,
)
# Check the length after . to distinguish between float and double
length = len(numeric_constant.split(".")[1]) if "." in numeric_constant else 0
# Determine type of numeric value
type = "int"
if length != 0:
if length <= 7:
type = "float"
elif length >= 7:
type = "double"
# Make entry in symbol table
id = table.entry(numeric_constant, type, "constant")
# Return number token and current index in source code
return Token("number", id, scanner_obj.line_num), i
def check_unindent(source_code, i, table, scanner_obj):
"""
Processes indentation in the source code
Params
======
source_code (str)
: The string containing pulse source code
i (int)
: The current index in the source code
table (SymbolTable)
: Symbol table constructed holding information about identifiers and constants
scanner_obj (Scanner)
: Instance of Scanner class
Returns
=======
(int)
: the current position in source code
"""
# Checks if the code is indented following ':'
if scanner_obj.isIndent:
# Count spaces/tabs
localTabCount = 0
localSpaceCount = 0
while source_code[i] == "\t":
localTabCount += 1
i += 1
while source_code[i] == " ":
localSpaceCount += 1
i += 1
# Convert spaces into tabs
localTabCount = localSpaceCount // 2 if localTabCount == 0 else localTabCount
# If the number of tabs before a line are less than the current level of indentation then setup unindentation
if localTabCount < scanner_obj.indentLevel:
scanner_obj.isUnindent = True
scanner_obj.unindentLevel = scanner_obj.indentLevel - localTabCount
# If scanner's indentation level is zero then set isIndent to false
if scanner_obj.indentLevel == 0:
scanner_obj.isIndent = False
return i
def gen_unindent(scanner_obj):
"""
Generates unindent token
Params
======
scanner_obj (Scanner)
: Instance of Scanner class
"""
if scanner_obj.unindentLevel > 0:
print("Here-", scanner_obj.indentLevel)
while scanner_obj.unindentLevel != 0:
token = Token("unindent", "", scanner_obj.line_num)
scanner_obj.tokens.append(token)
scanner_obj.unindentLevel -= 1
scanner_obj.indentLevel -= 1
scanner_obj.isIndent = False
def scanner(source_code, table):
"""
Generate tokens from source code
Params
======
source_code (str)
: Pulse source code
table (SymbolTable)
: Symbol table constructed holding information about identifiers and constants
Returns
========
tokens: A list of tokens of the source code
"""
# Create scanner_obj class' object
scanner_obj = Scanner()
# Loop through the source code character by character
i = 0
# To store comments string
comment_str = ""
while source_code[i] != "\0":
# If a digit appears, call numeric_val function and add the numeric token to list,
# if it was correct
if is_digit(source_code[i]):
token, i = numeric_val(source_code, i, table, scanner_obj)
scanner_obj.tokens.append(token)
# If double quote appears the value is a string token
elif source_code[i] == '"':
token, i = string_val(source_code, i, table, scanner_obj)
scanner_obj.tokens.append(token)
# If single quote appears the value is a string token
elif source_code[i] == "'":
token, i = string_val(source_code, i, table, scanner_obj, "'")
scanner_obj.tokens.append(token)
# If alphabet or number appears then it might be either a keyword or an identifier
elif is_alnum(source_code[i]):
token, i = keyword_identifier(source_code, i, table, scanner_obj)
scanner_obj.tokens.append(token)
# If character is : then generate begin block token and start indentation
elif source_code[i] == ":":
token = Token("begin_block", "", scanner_obj.line_num)
scanner_obj.tokens.append(token)
scanner_obj.isIndent = True
scanner_obj.indentLevel += 1
i += 1
# If character is \n then generate newline token and check for unindentation
elif source_code[i] == "\n":
scanner_obj.line_num += 1
token = Token("newline", "", scanner_obj.line_num)
scanner_obj.tokens.append(token)
i = check_unindent(source_code, i + 1, table, scanner_obj)
gen_unindent(scanner_obj)
# If character is ( then generate left paren token
elif source_code[i] == "(":
scanner_obj.tokens.append(Token("left_paren", "", scanner_obj.line_num))
i += 1
# If character is ) then generate right paren token
elif source_code[i] == ")":
scanner_obj.tokens.append(Token("right_paren", "", scanner_obj.line_num))
i += 1
# Identifying Left brace token
elif source_code[i] == "{":
scanner_obj.tokens.append(Token("left_brace", "", scanner_obj.line_num))
i += 1
# Identifying right brace token
elif source_code[i] == "}":
scanner_obj.tokens.append(Token("right_brace", "", scanner_obj.line_num))
i += 1
# Identifying assignment or equal token
elif source_code[i] == "=":
if source_code[i + 1] == "=":
scanner_obj.tokens.append(Token("equal", "", scanner_obj.line_num))
i += 2
else:
scanner_obj.tokens.append(Token("assignment", "", scanner_obj.line_num))
i += 1
# Identifying plus equal, increment or plus token
elif source_code[i] == "+":
if source_code[i + 1] == "=":
scanner_obj.tokens.append(Token("plus_equal", "", scanner_obj.line_num))
i += 2
elif source_code[i + 1] == "+":
scanner_obj.tokens.append(Token("increment", "", scanner_obj.line_num))
i += 2
else:
scanner_obj.tokens.append(Token("plus", "", scanner_obj.line_num))
i += 1
# Identifying minus equal, decrement or minus token
elif source_code[i] == "-":
if source_code[i + 1] == "=":
scanner_obj.tokens.append(
Token("minus_equal", "", scanner_obj.line_num)
)
i += 2
elif source_code[i + 1] == "-":
scanner_obj.tokens.append(Token("decrement", "", scanner_obj.line_num))
i += 2
else:
scanner_obj.tokens.append(Token("minus", "", scanner_obj.line_num))
i += 1
# Identifying multiply equal or multiply token
elif source_code[i] == "*":
if source_code[i + 1] == "=":
scanner_obj.tokens.append(
Token("multiply_equal", "", scanner_obj.line_num)
)
i += 2
else:
scanner_obj.tokens.append(Token("multiply", "", scanner_obj.line_num))
i += 1
# Identifying single line comment token
elif source_code[i] == "#":
i += 1
while source_code[i] != "\n":
comment_str += str(source_code[i])
i += 1
scanner_obj.tokens.append(
Token("single_line_comment", comment_str, scanner_obj.line_num)
)
comment_str = ""
# Identifying multi line comment, divide_equal, integer_divide, divide token
elif source_code[i] == "/":
if source_code[i + 1] == "*":
i += 2
while source_code[i] != "*" and source_code[i + 1] != "/":
comment_str += str(source_code[i])
i += 1
scanner_obj.tokens.append(
Token("multi_line_comment", comment_str, scanner_obj.line_num)
)
comment_str = ""
elif source_code[i + 1] == "=":
scanner_obj.tokens.append(
Token("divide_equal", "", scanner_obj.line_num)
)
i += 2
elif source_code[i + 1] == "/":
scanner_obj.tokens.append(
Token("integer_divide", "", scanner_obj.line_num)
)
i += 2
else:
scanner_obj.tokens.append(Token("divide", "", scanner_obj.line_num))
i += 1
# Identifying modulus equal or modulus token
elif source_code[i] == "%":
if source_code[i + 1] == "=":
scanner_obj.tokens.append(
Token("modulus_equal", "", scanner_obj.line_num)
)
i += 2
else:
scanner_obj.tokens.append(Token("modulus", "", scanner_obj.line_num))
i += 1
# Identifying comma token
elif source_code[i] == ",":
scanner_obj.tokens.append(Token("comma", "", scanner_obj.line_num))
i += 1
# Identifying not_equal token
elif source_code[i] == "!" and source_code[i + 1] == "=":
scanner_obj.tokens.append(Token("not_equal", "", scanner_obj.line_num))
i += 2
# Identifying greater_than or greater than equal token
elif source_code[i] == ">":
if source_code[i + 1] == "=":
scanner_obj.tokens.append(
Token("greater_than_equal", "", scanner_obj.line_num)
)
i += 2
else:
scanner_obj.tokens.append(
Token("greater_than", "", scanner_obj.line_num)
)
i += 1
# Identifying less than or less than equal to token
elif source_code[i] == "<":
if source_code[i + 1] == "=":
scanner_obj.tokens.append(
Token("less_than_equal", "", scanner_obj.line_num)
)
i += 2
else:
scanner_obj.tokens.append(Token("less_than", "", scanner_obj.line_num))
i += 1
# Identifying the token left_bracket
elif source_code[i] == "[":
scanner_obj.tokens.append(
Token("token_left_bracket", "", scanner_obj.line_num)
)
i += 1
# Identifying the token right_bracket
elif source_code[i] == "]":
scanner_obj.tokens.append(
Token("token_right_bracket", "", scanner_obj.line_num)
)
i += 1
# If nothing is matched then increment the index
else:
i += 1
# If indentLevel is not 0 then generate unindent tokens until indentLevel is zero
if scanner_obj.indentLevel > 0:
while scanner_obj.indentLevel != 0:
token = Token("unindent", "", scanner_obj.line_num)
scanner_obj.tokens.append(token)
scanner_obj.indentLevel -= 1
# Return the generated tokens
return scanner_obj.tokens