-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmnlex.py
93 lines (85 loc) · 3.28 KB
/
gmnlex.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
from gmnbase import gmntoken
class gmnlex:
"""lexer class of getmenotes: Makes tokens from reading code"""
def __init__(self, filename: str):
self.filename = filename
self.file = open(self.filename, 'r')
def getline(self) -> str:
"""Read one line from file"""
line = self.file.readline()
if line:
return line
def getnexttoken(self) -> gmntoken:
"""returns a token by reading the file"""
opentag = False
closetag = False
commentfound = False
topicflag = None
skip = False
text = []
code = []
topic = []
skipper = 0
# <gm>[Lexer] check for comment
# check for start tag, check topic if not set global
# check for code lines
# </gm>
while True:
"""token making logic follows gmn grammer"""
line = self.getline()
if line is not None:
for i in range(len(line)):
# print(opentag,closetag,text,code,topic,line[i])
if skip == True and i <= skipper:
continue
else:
skip = False
if line[i] == "#" and commentfound == False:
commentfound = True
if commentfound == False and opentag == False:
continue
if commentfound == True and opentag == False and line[i:i+4] == "<gm>":
opentag = True
skip = True
skipper = i+4
continue
if opentag == True and topicflag is None:
if line[i] != ' ':
if line[i] == '[':
j = i+1
while line[j] != ']':
topic.append(line[j])
j += 1
skip = True
skipper = j
topicflag = True
continue
else:
topicflag = False
topic.append('global')
continue
if opentag == True and topicflag is not None:
if commentfound == True:
if(line[i:i+5] == "</gm>"):
closetag = True
break
if line[i] != '#':
text.append(line[i])
if commentfound == False:
code.append(line[i])
continue
if line[i:i+5] == "</gm>":
return None
commentfound = False
if closetag == True:
return gmntoken(''.join(text), ''.join(code), ''.join(topic))
else:
return None
# checks gmnlexer
if __name__ == '__main__':
GMN = gmnlex("example.py")
a = GMN.getnexttoken()
while a:
a = GMN.getnexttoken()
if a is not None:
print(''.join(a.topic))