-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenizer.py
68 lines (56 loc) · 1.74 KB
/
Tokenizer.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
from GrammerUtils import operators
# def print(*a):
# pass
def tokenize(line:str):
code = []
l = ''
in_quotes = False
line = line+' '
ignoreNext = False
for x in range(len(line)):
c: str = line[x]
c1: str = line[min(x+1, len(line) - 1)]
c_1: str = line[max(x-1, 0)]
# --------------------------
if in_quotes:
c = c.replace('\\', '').replace('`', ' ')
l += c
if c == '\'' and not c_1 == '\\':
in_quotes = not in_quotes
# This allows and or xor instead of &&, ||
vn = var_token(l)
ln = l+''
if vn != l and code[len(code) - 1]=='`':
del code[len(code) - 1]
#
# if var_token(parr) == parr:
# continue
if not in_quotes:
if not (''.join(c + c1) in operators):
if is_var(c1) != is_var(c):
if not (ignoreNext and ln=='`'):
code.append(var_token(l))
ignoreNext = False
l = ''
elif not is_var(c):
if not (ignoreNext and ln=='`'):
code.append(var_token(l))
ignoreNext = False
l = ''
ignoreNext = False
if vn != ln:
ignoreNext = True
# ignoreNext = False
code.append(var_token(l))
# print(code,'final-code===')
return code
def is_var(c1:str):
return c1.isalnum() or c1 == '_' or c1 == '@' or c1 == '\'' or c1 == '\"'
def var_token(v):
# print('{',v,'}vvv--v-vv-v')
# print(v == 'and')
if v == 'or': return '||'
if v == 'and': return '&&'
if v == 'xor': return '^'
if v == 'in': return '@='
return v