-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.py
257 lines (238 loc) · 7.58 KB
/
scanner.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import token
import re
class PerlScanner:
def __init__(self, verbose=False):
self.verbose = verbose
def scans(self, pathFile):
try:
Perlfile = open(pathFile, "r")
tokenList = list()
line = ""
for line2 in Perlfile:
line = line + line2
while line != "":
tok, line = self.getNextToken(line)
if tok.name != "":
tokenList.append(tok)
if (self.verbose):
print tok
tokenList.append(token.token('END-SYMBOL'))
except Exception as e:
raise Exception("Le fichier ne respecte pas la syntaxe PERL", e)
tokenList = list()
Perlfile.close()
return tokenList
def getNextToken(slef, line):
line = line.lstrip()
while re.match("\n", line):
line = line[2:]
print "ligne vide"
if line == "":
return token.token("", ""), line
else:
# On cherche d'abord les operateurs "non string"
if line[0] == "-":
line = line[1:]
return token.token("MINUS", ""), line
if line[0] == "+":
line = line[1:]
return token.token("ADD", ""), line
if line[0] == ">":
if len(line) > 2 and line[1] == "=":
line = line[2:]
return token.token("GE", ""), line
else:
line = line[1:]
return token.token("GT", ""), line
if line[0] == "<":
if len(line) > 2 and line[1] == "=":
line = line[2:]
return token.token("LE", ""), line
else:
line = line[1:]
return token.token("LT", ""), line
if line[0] == "/":
line = line[1:]
return token.token("DIV", ""), line
if line[0] == "*":
line = line[1:]
return token.token("MUL", ""), line
if line[0] == "}":
line = line[1:]
return token.token("CLOSE-BRAC", ""), line
if line[0] == "{":
line = line[1:]
return token.token("OPEN-BRAC", ""), line
if line[0] == ")":
line = line[1:]
return token.token("CLOSE-PAR", ""), line
if line[0] == "(":
line = line[1:]
return token.token("OPEN-PAR", ""), line
if line[0] == ",":
line = line[1:]
return token.token("COMA", ""), line
if line[0] == ";":
line = line[1:]
return token.token("SEMICOLON", ""), line
if line[0] == ".":
line = line[1:]
return token.token("DOT", ""), line
if line[0] == "=":
if len(line) > 2 and line[1] == "=":
line = line[2:]
return token.token("EQUIV", ""), line
else:
line = line[1:]
return token.token("EQUAL", ""), line
if line[0] == "!":
if len(line) > 2 and line[1] == "=":
line = line[2:]
return token.token("DIF", ""), line
else:
line = line[1:]
return token.token("FAC", ""), line
if re.match("\|\|", line):
line = line[2:]
return token.token("OR", ""), line
if re.match("\&\&", line):
line = line[2:]
return token.token("AND", ""), line
if re.match("''", line):
line = line[2:]
return token.token("BOOL", "false"), line
# On cherche ensuite les operateurs "strings"
if line[0] == "n":
if re.match("not[^a-zA-Z0-9_-]", line):
line = line[3:]
return token.token("NOT", ""), line
elif re.match("ne[^a-zA-Z0-9__-]", line):
line = line[2:]
return token.token("NE-S", ""), line
if re.match("true[^a-zA-Z0-9_-]", line):
line = line[4:]
return token.token("BOOL", "true"), line
if re.match("false[^a-zA-Z0-9_-]", line):
line = line[5:]
return token.token("BOOL", "false"), line
if line[0] == "l":
if re.match("lt[^a-zA-Z0-9__-]", line):
line = line[2:]
return token.token("LT-S", ""), line
if re.match("le[^a-zA-Z0-9__-]", line):
line = line[2:]
return token.token("LE-S", ""), line
if re.match("length[^a-zA-Z0-9_-]", line):
line = line[6:]
return token.token("PERL-LENG", ""), line
if line[0] == "g":
if re.match("gt[^a-zA-Z0-9__-]", line):
line = line[2:]
return token.token("GT-S", ""), line
if re.match("ge[^a-zA-Z0-9__-]", line):
line = line[2:]
return token.token("GE-S", ""), line
if line[0] == "i":
if re.match("if[^a-zA-Z0-9_-]", line):
line = line[2:]
return token.token("OPEN-COND", ""), line
if re.match("int[^a-zA-Z0-9_-]", line):
line = line[3:]
return token.token("PERL-INT", ""), line
if line[0] == "e":
if re.match("eq[^a-zA-Z0-9__-]", line):
line = line[2:]
return token.token("EQ-S", ""), line
if re.match("elsif[^a-zA-Z0-9_-]", line):
line = line[5:]
return token.token("ADD-COND", ""), line
if re.match("else[^a-zA-Z0-9_-]", line):
line = line[4:]
return token.token("CLOSE-COND", ""), line
if re.match("defined[^a-zA-Z0-9_-]", line):
line = line[7:]
return token.token("PERL-DEF", ""), line
if re.match("unless[^a-zA-Z0-9_-]", line):
line = line[6:]
return token.token("NEG-COND", ""), line
if re.match("print[^a-zA-Z0-9_-]", line):
line = line[5:]
return token.token("PERL-PRIN", ""), line
if re.match("return[^a-zA-Z0-9_-]", line):
line = line[6:]
return token.token("RET", ""), line
if line[0] == "s":
if re.match("sub[^a-zA-Z0-9_-]", line):
line = line[3:]
return token.token("FUNCT-DEF", ""), line
if re.match("substr[^a-zA-Z0-9_-]", line):
line = line[6:]
return token.token("PERL-SUBS", ""), line
if re.match("scalar[^a-zA-Z0-9_-]", line):
line = line[6:]
return token.token("PERL-SCAL", ""), line
# On cherche ensuite les nombres (float et int)
if re.match("[0-9]", line):
floatNumber = re.match("([0-9])+\.([0-9])+", line)
intNumber = re.match("([0-9])+", line) # On sait deja qu il n y a pas de point apres puisqu on teste les float d abord
if floatNumber:
# On a un float
line = line[len(floatNumber.group()):]
return token.token("FLOAT", floatNumber.group()), line
if intNumber:
# on a un entier
line = line[len(intNumber.group()):]
return token.token("INT", intNumber.group()), line
# On cherche ensuite les variables, fonctions et strings (tout ce qui necessite une boucle)
if line[0] == "&":
func = re.match("&([A-Za-z])+([A-Za-z0-9_-])*", line)
if func:
# On a un appel de fonction (& suivi d'un string)
line = line[len(func.group()):]
return token.token("FUNCT-NAME", func.group()[1:]), line
if line[0] == "'":
string = re.match("'([^'])*'", line)
if string:
# On a un string (' suivi d'un string et termine par un autre ')
line = line[len(string.group()):]
return token.token("STRING", string.group()[1:-1]), line
if line[0] == "#":
com = re.match("#(.)*\n", line)
if com:
# On a un commentaire (# suivi d'un string et termine par un autre \n)
line = line[len(com.group()):]
return token.token("", ""), line
if line[0] == "$":
var = re.match("[$]([A-Za-z])+([A-Za-z0-9_-])*", line)
if var:
# On a une variable ($ suivi d'un string)
line = line[len(var.group()):]
return token.token("VARIABLE", var.group()[1:]), line
# tout ce qui reste est alors une variable
if re.match("([A-Za-z])", line):
var = re.match("([A-Za-z])+([A-Za-z0-9_-])*", line)
if var:
# On a un ID (un string)
line = line[len(var.group()):]
return token.token("ID", var.group()), line
# Si on arrive ici c est qu il y a un probleme avec la syntaxe du fichier
return None
if __name__ == '__main__':
import sys
import getopt
try:
opts, args = getopt.getopt(sys.argv[1:], "i:", ["help", "input="])
except getopt.GetoptError as err:
print(err)
sys.exit(2)
perlFile = None
for o, a in opts:
if o in ("-i", "--input"):
perlFile = a
else:
print("Option {} unknow".format(o))
sys.exit(2)
perl_scanner = PerlScanner()
inputTokens = perl_scanner.scans(perlFile)
for token in inputTokens:
print token