This repository was archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparser.py
More file actions
442 lines (386 loc) · 14.9 KB
/
parser.py
File metadata and controls
442 lines (386 loc) · 14.9 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
#!/usr/bin/env python3
import unittest
from collections import deque
BUFFER_SIZE = 1024
FUZZER_LENGTH = 8192
FUZZER_RUNS = 1024
DEFAULT_INT_MAX_LEN = 32
DEFAULT_FLOAT_MAX_LEN = 32
DEFAULT_STRING_MAX_LEN = 4096
class TokenStream:
def __init__(self,
file,
strict_spaces=False,
int_max_len=DEFAULT_INT_MAX_LEN,
float_max_len=DEFAULT_FLOAT_MAX_LEN,
str_max_len=DEFAULT_STRING_MAX_LEN,
spaces=" \t\n"):
"""
:param file: file object of the output
:param strict_spaces: whether to consider spaces as tokens, manual
skipping of them is required
:param int_max_len: maximum number of chars for a int
:param float_max_len: maximum number of chars for a float
:param str_max_len: maximum number of chars for a str
:param spaces: list of characters to consider spaces
"""
self.file = file
self.strict_spaces = strict_spaces
if str_max_len < int_max_len:
raise ValueError("str_max_len lower than int_max_len")
if str_max_len < float_max_len:
raise ValueError("str_max_len lower than float_max_len")
self.int_max_len = int_max_len
self.float_max_len = float_max_len
self.str_max_len = str_max_len
self.spaces = spaces
self.char_buffer = deque()
self.current_line_no = 1
self.eof = False
# =======================
# read a token
# =======================
def int(self, validate=lambda x: True):
"""Read an integer"""
return self._parse_number(
self.int_max_len,
int,
"+-0123456789",
validate,
advance_buffer=True)
def float(self, validate=lambda x: True):
"""Read a float"""
return self._parse_number(
self.float_max_len,
float,
"+-e0123456789.",
validate,
advance_buffer=True)
def str(self, validate=lambda x: True):
"""Read a string"""
self._skip_spaces()
buffer = list()
# read all chars that are not spaces
while not self._is_eof() and self._probe_char() not in self.spaces:
buffer += self._next_char()
if len(buffer) > self.str_max_len:
raise ValueError("string too long")
buffer = "".join(buffer)
if not validate(buffer):
raise ValueError("invalid string")
return buffer
def char(self, validate=lambda x: True):
"""Read a single char, skipping spaces"""
self._skip_spaces()
char = self._next_char()
if not validate(char):
raise ValueError("invalid char")
return char
def space(self, validate=lambda x: True):
"""Read a single space"""
if self._is_eof() or self._probe_char() not in self.spaces:
raise ValueError("expecting a space")
space = self._next_char()
if not validate(space):
raise ValueError("invalid space")
return space
def end(self):
"""Check that there are no more tokens before the next testcase
without consuming anything"""
# the end of the file is a valid end
if not self.strict_spaces:
self._skip_spaces()
if self._is_eof():
return
try:
# read at least the characters for the prefix and a digit
while len(self.char_buffer) < len("Case #") + 1:
self._read_char()
# check the buffer starts with the prefix
if self._is_prefix() >= 0:
raise ValueError("the testcase has not ended")
# if the EOF is found but not at the beginning there's a problem
except EOFError:
raise ValueError("expecting new testcase, not EOF")
def seek_next_testcase(self):
"""
skip everything until the next testcase
@:returns a pair: (testcase number, skipped bytes, line_no)
@:raises EOFError: when the file ends this error is raised with the
skipped bytes as args[1]
"""
old_spaces = self.spaces
MAX_LEN = 100
class safe_str:
def __init__(self):
self.str = list()
self.trimmed = False
def __add__(self, other):
if len(self.str) > MAX_LEN or self.trimmed:
return self
if len(self.str) + len(other) > MAX_LEN:
self.str += list(other[:MAX_LEN - len(self)]) + list("...")
self.trimmed = True
return self
self.str += list(other)
return self
def __len__(self):
return len(self.str)
def __str__(self):
return "".join(self.str)
data_read = safe_str()
skipped_from = self.current_line_no
while True:
try:
skipped = str(data_read)
skipped_from = self.current_line_no
# skip all the spaces
while self._probe_char() in self.spaces:
data_read += self._next_char()
# try to read the "Case"
case = self.str()
line = self.current_line_no
data_read += case
# if the string is not Case, read also the space and try again
if case.lower() != "case" and case.lower() != "caso":
data_read += self._probe_char()
continue
# skip one space between "Case" and "#"
data_read += self.space()
# check if the next char is #
if self._probe_char() != "#": continue
# if so read it
data_read += self.char()
# to read the testcase number use the ":" as a delimiter,
# after the int is read revert this change
self.spaces += ":"
num = self.int()
data_read += str(num)
# revert self.spaces
self.spaces = old_spaces
# if the testcase number is not valid
if num <= 0: continue
# check if the char after the number is a ":"
if self._probe_char() != ":": continue
data_read += self.char()
return num, line, str(skipped), skipped_from
except ValueError as ex:
pass
except EOFError as ex:
raise EOFError(ex.args[0], str(data_read), skipped_from)
finally:
# if the call to self.int() fails we have to be sure to have
# reverted self.spaces
self.spaces = old_spaces
def has_int(self):
"""check is the next bytes in the buffer are a valid int"""
if not self.strict_spaces:
self._skip_spaces()
try:
self._parse_number(
self.int_max_len,
int,
"+-0123456789",
lambda x: True,
advance_buffer=False)
except:
return False
else:
return True
def has_float(self):
"""check is the next bytes in the buffer are a valid float"""
if not self.strict_spaces:
self._skip_spaces()
try:
self._parse_number(
self.float_max_len,
float,
"+-e0123456789.",
lambda x: True,
advance_buffer=False)
except:
return False
else:
return True
def has_space(self, accepted=None):
"""check is the next byte in the buffer is a space (only in
strict_spaces mode)"""
if not self.strict_spaces:
raise RuntimeError(
"has_space is available only in strict_spaces mode")
if accepted is None:
accepted = self.spaces
return not self._is_eof() and self._probe_char() in accepted
# ================
# utilities
# ================
def _skip_spaces(self):
"""Try to skip the spaces, if int strict_spaces mode and there are
spaces to skip raise an error"""
if self.strict_spaces and not self._is_eof() and self._probe_char() \
in self.spaces:
raise ValueError("expecting something not a space")
spaces = ""
while not self._is_eof() and self._probe_char() in self.spaces:
spaces += self._next_char()
return spaces
def _next_char(self):
"""Read and consume a char"""
if self._probe_char() == "\n":
self.current_line_no += 1
return self.char_buffer.popleft()
def _probe_char(self, index=0):
"""Fetch the next index-th character without consuming it"""
if len(self.char_buffer) <= index:
self._read_char()
return self.char_buffer[index]
def _read_char(self):
"""Read but not consume a character"""
char = self.file.read(BUFFER_SIZE)
if char == "":
raise EOFError("End of file")
self.char_buffer.extend(char)
return char
def _parse_number(self,
max_len,
type,
allowed_chars,
validate,
advance_buffer=True):
"""
Read and parse a number
:param max_len: maximum number of characters to read
:param type: int/float
:param allowed_chars: set of allowed characters in the number
:param validate: function to call to check if the number is valid
:param advance_buffer: whether to consume the number
"""
self._skip_spaces()
# index in the char_buffer
index = 0
buffer = ""
# continue to read until the end of the file or an invalid char
while not self._is_eof(index + 1) and self._probe_char(
index) in allowed_chars:
buffer += self.char_buffer[index]
index += 1
if len(buffer) > max_len:
raise ValueError("number too long")
# if the while exited because an invalid char
if not self._is_eof(index + 1) and self._probe_char(
index) not in self.spaces:
raise ValueError(
"invalid character `%s' in number" % self._probe_char(index))
# consume the number if requested
if advance_buffer:
for _ in range(index):
self._next_char()
res = type(buffer)
if not validate(res):
raise ValueError("validation failed")
return res
def _is_prefix(self):
"""tries to match the "Case #" prefix in the buffer, returns the
index of mismatch, -1 if match"""
for i in range(len("Case #")):
if self.char_buffer[i].lower() != "case #" [i]:
return i
return -1
def _is_eof(self, at_least=1):
"""returns True if there are no more bytes to consume"""
if len(self.char_buffer) >= at_least:
return False
if self.eof:
return True
try:
self._read_char()
except EOFError:
self.eof = True
return True
else:
return False
class Parser:
def __init__(self, parse_testcase, num_inputs, file, **kwargs):
"""
:param parse_testcase: function to call to parse a testcase, will be
passed 2 parameters: (testcase number, stream). The second parameter is
an instance of TokenStream. The function may return the score [0,1] or a
tuple (score, message).
:param num_inputs: number of testcases
:param file: file with the output to parse
:param kwargs: arguments to pass to the TokenStream constructor
"""
self.stream = TokenStream(file, **kwargs)
self.parse_testcase = parse_testcase
self.num_inputs = num_inputs
def run(self):
total_score = 0.0
testcases_seen = set()
output = {
"score": 0.0,
"validation": {
"cases": [{
"status": "missing"
} for _ in range(self.num_inputs)],
"alerts": []
},
"feedback": {
"cases": [{
"correct": False
} for _ in range(self.num_inputs)],
"alerts": []
}
}
def add_warning(message):
output["validation"]["alerts"].append({
"severity": "warning",
"message": message
})
while True:
try:
num, line_no, skipped, skipped_from = \
self.stream.seek_next_testcase()
if len(skipped) > 0:
add_warning("Skipped data from line %d: %s" %
(skipped_from, skipped))
if num in testcases_seen:
add_warning("Skipped duplicate testcase %d at line %d" %
(num, line_no))
continue
if num > self.num_inputs:
add_warning("Skipped testcase %d > %d at line %d" %
(num, self.num_inputs, line_no))
continue
testcases_seen.add(num)
output["validation"]["cases"][num - 1]["status"] = "parsed"
output["validation"]["cases"][num - 1][
"message"] = "Found from line %d" % line_no
except EOFError as ex:
if len(ex.args) >= 3 and len(ex.args[1]) > 0:
add_warning("Skipped data from line %d: %s" % (ex.args[2],
ex.args[1]))
break
try:
out = self.parse_testcase(num, self.stream)
except ValueError as ex:
output["validation"]["cases"][num - 1]["status"] = "invalid"
output["validation"]["cases"][num - 1]["message"] = str(ex)
continue
if isinstance(out, tuple):
score, message = out
else:
score, message = out, ""
if len(message) > 0:
output["feedback"]["cases"][num - 1]["message"] = message
if score == 1.0:
output["feedback"]["cases"][num - 1]["correct"] = True
if score < 0.0 or score > 1.0:
score = 0.0
output["feedback"]["cases"][num - 1]["correct"] = False
output["feedback"]["cases"][num - 1][
"message"] = "buggy checker detected!"
total_score += score
output["score"] = total_score / self.num_inputs
return output
if __name__ == "__main__":
unittest.main()