-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_recovery_mock.py
415 lines (350 loc) · 13.4 KB
/
error_recovery_mock.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
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
# DEVICES:[
# {id: A; kind: SWITCH; qual: 0;};
# {id: B; kind: SWITCH; qual: 1;};
# ];
class DeviceParsing():
def __init__(self, mock_file):
self.mock_file = mock_file
self.index = -1 # not sure if this is necessary
self.error_count = 0
self.current_char = ""
#self.expect_qualifier = ["SWITCH", "AND"] # obvs there are more
def error(self, msg, expect_next_list):
end_of_file = False
recovered = False
print(f"ERROR at index {self.index}: " + msg +
f", received {self.current_char}")
self.error_count += 1
# while True:
# for expectedsymbol in expect_next_list:
# while self.current_char != ";":
# self.next_char()
#
# try:
# self.next_char()
# except IndexError:
# print("reached end of file!")
# end_of_file = True
# break
#
# if self.current_char == expectedsymbol:
# recovered=True
# break
#
# if end_of_file:
# break
# if recovered:
# break
while True:
while self.current_char != ";":
self.next_char()
#found a semi colon, now need to check if the expected element
# is next
try:
self.next_char()
except IndexError:
print("reached end of file!")
end_of_file = True
break
if self.current_char in expect_next_list:
# found the character we want to keep parsing, therefore we
# resume in the parsing
break
#below does the same i think
# found_character = False
# for i in range(len(expect_next_list)): # expected symbols have
# # to be in order of expectation!
# if self.current_char == expect_next_list[i]:
# found_character = True
# break
# #will this break the for loop? or
# # the while loop?
# if found_character:
# break
# this currently will not deal nicely an error at the end of the
# file / if we miss a semi-colon at the end??!!! infinite loop
# TODO: what if error recovery means that skip the whole file...
# will break out if we've found the right character, or if we've
# reached end of file.........
#TODO: what if we reach the end of the file??? :/ eek
# equivalent to if we are unable to resume parsing
# will it just work out?
def next_char(self):
self.index += 1
self.current_char = self.mock_file[self.index]
def parse_device_list(self):
while True:
self.next_char()
if self.current_char != "DEVICES":
self.error(f"no devices keyword", "DEVICES")
# will have skipped to the final semi colon
# more likely that it will just skip to the end of the file!
# self.parse_connections()
break
# self.next_char()
# if self.current_char != ":":
# self.error("expected :")
# break
self.next_char()
if self.current_char != "[":
self.error("expected [", ["CONNECTIONS", "MONITORS"]) # but
# it could also be end of file????? here/only devices
break
# if we get here we got DEVICES [
self.next_char() # set up so current char is {
parsing_devices = True
while parsing_devices:
missing_semi_colon = self.parse_device(self.error_count)
#TODO: what to do if there is a missing semi_colon????
#nothing????? what is the consequence....
if missing_semi_colon:
pass
#if missed one, we would skip until we get { or ]
#dont actually need to do anything?
#self.next_char() #issue is here....
if self.current_char == "{":
# more devices to parse
parsing_devices = True
#return keep_parsing
elif self.current_char == "]":
# reached end of device list
# if self.error_count == 0:
# print("successfully parsed the device")
# print(f"device {dev_id}-{dev_kind}-{dev_qual}")
# can move on back to rest of devices
parsing_devices = False
#return keep_parsing
else:
#TODO: what if it is neither of those???
# is this when there is an error at the end of device
# lists?
print("sort this problem out")
if self.current_char != "]":
self.error("expected ]", ["CONNECTIONS", "MONITORS"])
break
self.next_char()
if self.current_char != ";":
self.error("expected ;", ["MONITORS"])
#TODO: not sure if it's connections/monitors
break
# I mean it has to be 0 surely?
if self.error_count == 0:
print("successfully parsed a device list!")
return True
else:
print(f"found {self.error_count} error(s)")
break
# only get here if there is an error with the 'outer' device list
# wrapper
print("did not manage to parse the device list perfectly")
# wish this could be more informative.......
# maybe thats for self.error / scanner
#what is the point of this code below huhh/???
if self.error_count != 0:
return False
else:
return True
def parse_device(self, previous_errors):
missing_device_semi_colon = False
while True:
# self.next_char()
if self.current_char != "{":
self.error("expected {",["{","]"])
# go ot the next device we can parse
# TODO: what if there is only one device?
# TODO: uGH MY BRAIN CAN'T COPE something else is wrong here
# i can sense it
# the list should be searched linearly.... in case someone
# just puts ;] in there as a mistake.. deal with later rah
break
#now we can parse the id section
self.next_char() # current char should be "id"
missing_semi_colon, dev_id = self.parse_device_id()
if missing_semi_colon:
break
#helps with the looping if it is break instead of continue
missing_semi_colon, dev_kind = self.parse_device_kind()
if missing_semi_colon:
break
if self.current_char == "qual": # if it is there, we will parse it
missing_semi_colon, dev_qual = self.parse_device_qual()
if missing_semi_colon:
break
else:
dev_qual = None
if self.current_char != "}":
self.error("expected }",["{","]"])
break
self.next_char()
if self.current_char != ";":
self.error("expected ;",["{","]"])
missing_device_semi_colon = True
break
#if we get here we have done a whole device!
if self.error_count - previous_errors == 0:
print(f"successfully parsed the device: {dev_id}-{dev_kind}-{dev_qual}")
# TODO: what if there are residue errors from other devices?
# need a way of counting the additional errors which have
# occured in this call of the parse_device...
self.next_char() #setting up in case of errors....
break
else:
print(f"did not successfully parse the device sad.. would have "
f"attempted to build device "
f"{dev_id}-{dev_kind}-{dev_qual}")
self.next_char()
break
return missing_device_semi_colon
def parse_device_id(self):
missing_semi_colon = False
dev_name_string = None
while True:
if self.current_char != "id":
self.error("expected id keyword here", ["kind"])
break
self.next_char()
if self.current_char != ":":
self.error("expected : here", ["kind"])
break
self.next_char()
if not self.current_char.isalnum():
self.error("OI this is not alnum --> SYNTAX error",["kind"])
break
dev_name_string = self.current_char
self.next_char()
if self.current_char != ";":
self.error("missing semicolon", ["{"])
missing_semi_colon = True
break
# should i just call parse_device here????
self.next_char() #setting up for next function
#current_char should be "kind" here
break
return missing_semi_colon, dev_name_string
def parse_device_kind(self):
missing_semi_colon = False
dev_kind = None
while True:
if self.current_char != "kind":
self.error("expected kind keyword here", ["qual", "}"])
break
self.next_char()
if self.current_char != ":":
self.error("expected : here", ["qual", "}"])
break
self.next_char()
if not self.current_char.isalnum():
# actually this is
# different in final code (checking if its a valid name only)
self.error("OI this is not alnum --> SYNTAX error",["qual", "}"])
break
dev_kind = self.current_char
self.next_char()
if self.current_char != ";":
self.error("missing semicolon", ["{"])
missing_semi_colon = True
break
self.next_char() #either qual or }
break
return missing_semi_colon, dev_kind
def parse_device_qual(self):
missing_semi_colon = False
dev_qual = None
while True:
if self.current_char != "qual":
self.error("expected qual keyword here", ["}"])
break
self.next_char()
if self.current_char != ":":
self.error("expected : here", ["}"])
break
self.next_char()
if not self.current_char.isnumeric():
# actually this is
# different in final code (checking if its a valid name only)
self.error("OI this is not numeric --> SYNTAX error",
[ "}"])
break
dev_qual = self.current_char
self.next_char()
if self.current_char != ";":
self.error("missing semicolon", ["{"])
missing_semi_colon = True
break
self.next_char() # should be }
break
return missing_semi_colon, dev_qual
correct_file = [
"DEVICES", "[", "{", "id", ":", "A", ";", "kind", ":", "SWITCH", ";" , "qual", ":", "0", ";", "}", ";",
"{", "id", ":", "B", ";", "kind", ":", "SWITCH", ";", "qual", ":", "1", ";", "}", ";", "]", ";",
]
# dp = DeviceParsing(correct_file)
#
# dp.parse_device_list()
incorrect_file = [
"DEVICES", "[",
"{",
"id", "elephant", "**will skip this error*", ";",
"kind", ":", "SWITCH", ";" ,
"bepbop", ":", "0", ";", #this error causes the device stuff not to be
# built not that it matters....
"}", ";",
"{",
"id", "mistake", "B", ";",
"kind", ":", "noerrorherebcnotsyntax", ";",
#"qual", ":", "1", ";",
"}", ";",
"{",
"id", ":", "C", ";",
"kind", ":", "noerrorherebcnotsyntax", ";",
"qual", ":", "1", ";",
"}", ";",
"{",
"id", ":", "D", ";",
"meh", ":", "noerrorherebcnotsyntax", ";",
"qual", ":", "1", ";",
"}", ";",
"]",
";",
]
dp = DeviceParsing(incorrect_file)
dp.parse_device_list()
#
# order_incorrect_file = [
# "DEVICES", "[",
# "{",
# "id", "elephant", "**will skip this error*", ";",
# "kind", ":", "SWITCH", ";" ,
# "qual", ";", "]", ";",
# "]", ";", #gonna get confused here!
# "{",
# "id", ":", "B", ";",
# "kind", ":", "will never even parse", ";",
# #"qual", ":", "1", ";",
# "}", ";",
# "]",
# ";",
# ]
#
# dp = DeviceParsing(order_incorrect_file)
# dp.parse_device_list()
# end_incorrect_file = [
# "DEVICES", "er",
# "{",
# "id", "elephant", "**will skip this error*", ";",
# "kind", ":", "SWITCH", ";" ,
# "qual", ":", "0", ";",
# "]", ";",
# "{",
# "id", ":", "B", ";",
# "kind", ":", "88(()**errorhere", ";",
# #"qual", ":", "1", ";",
# "}", ";",
# "]",
# ";",
# "CONNECTIONS"
# ]
# if devices is wrong all together you need to do with end of file stuff
# or not bc its a scanner1!!!!!
# dp = DeviceParsing(end_incorrect_file)
# dp.parse_device_list()