-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference_parser.py
749 lines (566 loc) · 22.4 KB
/
reference_parser.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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
import os.path
import re
from dataclasses import dataclass, asdict
from enum import Enum
from json import dumps
from sys import stderr
from typing import *
import lumberjack
from helper_types import *
from typing import Dict, List, Tuple, Set
def removeprefix(text, prefix):
if text.startswith(prefix):
return text[len(prefix):]
return text # or whatever
class ParseIssue(Enum):
"""
Issues in a parsed reference implementation.
Can be matched if smarter error handling is desired, or a simple error message
can be accessed using :code:`issue.value`
"""
ArrayReturnType = "Return type must be `void' or scalar"
MultiLevelPointer = "Multi-level pointers are not supported"
ScalarOutputParameter = "Output parameters must be pointers"
ScalarGivenSize = "Only array parameters can be given a size"
GivenInvalidSize = "Sizes must be a valid type"
UnsizedArrayParameter = "All unterminated arrays must be given a size"
ReferenceSignatureMismatch = "The signatures in `ref.c' and `props' differ"
InvalidIdentifierName = "All names must be valid C identifiers"
InvalidConstraint = "A forbidden constraint was given"
@staticmethod
def ignorable():
return {
ParseIssue.ScalarGivenSize,
}
@dataclass
class CType:
"""
A wrapper for a C type.
"""
contents: str
pointer_level: int
@staticmethod
def parse(type_sig: str):
"""
Build a type instance from a type signature.
Type signatures can look like: :code:`int`, :code:`int *`, :code:`char*`, :code:`void ** *`, etc.
No checking is done here to determine whether the type is valid.
:param type_sig: the type signature
:return: an instance of that type
"""
type_sig = type_sig.strip()
if '*' in type_sig:
ptr_idx = type_sig.index('*')
contents = type_sig[:ptr_idx].rstrip()
pointers = type_sig[ptr_idx:]
pointer_level = sum(1 for c in pointers if c == "*")
else:
contents = type_sig.strip()
pointer_level = 0
if pointer_level > 1:
raise UnsupportedTypeError("multi-level pointers")
return CType(contents, pointer_level)
def __str__(self):
return f"{self.contents}{'*' * self.pointer_level}"
@dataclass
class CParameter:
"""
A wrapper for a parameter.
"""
name: Name
type: CType
@staticmethod
def parse(param: str):
"""
Builds a CParameter instance.
Does not check if the type is a valid name, just separates it from the type.
:param param: the parameter definition
:return: an instance from that definition
"""
m = re.match("((?:int|char|float|double|bool|void)[* ]+)(.*)", param)
if m is None:
raise ParseError("invalid parameter")
c_type, name = m.groups()
type_info = CType.parse(c_type)
return CParameter(name, type_info)
def __str__(self):
return f"{self.type} {self.name}"
@dataclass
class FunctionSignature:
"""
A C function's full signature
"""
name: Name
type: CType
parameters: List[CParameter]
@staticmethod
def parse(sig: str):
"""
Build a FunctionSignature instance from a signature string.
This string looks like:
.. code-block:: c
[func type] [func name]([parameter], ...)
:param sig: the signature
:return: the instance built from that signature
"""
m = re.match(r"(.*)\((.*)\)", sig)
if m is None:
raise ParseError("could not parse function signature")
func_def = CParameter.parse(m[1].strip())
params = [param.strip() for param in m[2].split(",")]
return FunctionSignature(func_def.name,
func_def.type,
[CParameter.parse(param) for param in params])
def __str__(self):
return f"{self.name}({', '.join(str(param) for param in self.parameters)}) -> {self.type}"
def c_sig(self) -> str:
"""
The function signature as it would appear in C.
Note all pointer types look like :code:`type* name` (as opposed to :code:`type *name`)
:return: the signature string
"""
return f"{self.type} {self.name}({', '.join(str(param) for param in self.parameters)})"
@dataclass
class ParamSize:
"""
The base type for sizes of array parameters
"""
arr: Name
@staticmethod
def parse(size: str):
"""
Parses a size description into the correct subclass for the size
TODO: change this to something better, fails on (the comma in) inputs like "size x, { sum(x*y for x, y in zip(..)) }
:param size: the size description
:return: the actual size object built from that description
"""
size = size.lstrip()
parts = re.finditer(r"\s*,\s*", size)
part = next(parts)
arr_end, next_start = part.span()
arr = size[:part.span()[0]]
try:
part = next(parts)
val_end, expr_start = part.span()
val = int(size[next_start:val_end])
expr = size[expr_start:].rstrip()
assert expr.startswith("{") and expr.endswith("}")
return ExprSize(Name(arr), val, expr[1:-1])
except StopIteration:
try:
val = int(size[next_start:])
return ConstSize(Name(arr), val)
except ValueError:
var = size[next_start:].rstrip()
if var.startswith("{"):
assert var.endswith("}")
return SimpleExprSize(Name(arr), var[1:-1])
else:
return VarSize(Name(arr), Name(var))
def evaluate(self, values: dict, initial: bool = False) -> Optional[int]:
"""
Determine the actual size of the array
This method can be used to calculate the size of the initial native Python value being fed into a function,
as well as the size a foreign array needs to be to contain a value it will later hold.
Note this does *NOT* include the extra space needed for a '\0' in a string.
:param values: the values already determined for the parameters
:param initial: set to :code:`True` for the size of the native value,
otherwise the size is for the foreign array.
:return: the size of the array
"""
raise NotImplementedError("This is an abstract method, use the corresponding method in a subtype")
@dataclass
class VarSize(ParamSize):
"""
Denotes an association between a array parameter, and a scalar parameter containing the array's size
"""
arr: Name
var: Name
def evaluate(self, values: dict, initial: bool = False) -> Optional[int]:
var = self.var
assert var in values
return values.get(var)
@dataclass
class ConstSize(ParamSize):
"""
Denotes a constant size on an array parameter
Note this constant can also be treated as a *maximum* size.
"""
arr: Name
size: int
def evaluate(self, values: dict, initial: bool = False) -> Optional[int]:
return self.size
@dataclass
class ExprSize(ParamSize):
"""
Denotes a complex sizing scheme, typically used for output strings
Contains the size for the initial size of the array (for generation),
and an expression to calculate the maximum size the array can be (to create a foreign array large enough).
"""
arr: Name
init: int
expr: str
def evaluate(self, values: dict, initial: bool = False) -> Optional[int]:
if initial:
return self.init
else:
size = eval(self.expr, dict(values))
assert size is not None and isinstance(size, int)
return size
@dataclass
class SimpleExprSize(ParamSize):
"""
Denotes an array size calculated from an expression
This is useful to encode a more complicated relationship between parameters,
for example in matrices where the size of an m x n array M needs to be { m * n }
"""
arr: Name
expr: str
def evaluate(self, values: dict, initial: bool = False) -> Optional[int]:
size = eval(self.expr, dict(values))
assert size is not None and isinstance(size, int)
return size
class Constraint:
"""
Base class for a constraint property
"""
@staticmethod
def parse(constraint: str):
"""
Extracts a constraint from a description of a constraint,
determining the type of constraint and embedding it in an object of the corresponding subclass
:param constraint: the constraint description
:return: the constraint object
"""
constraint = constraint.lstrip()
if constraint.startswith("{"):
constraint = constraint.rstrip()
assert constraint.endswith("}")
return GlobalContstraint(constraint[1:-1])
else:
word_boundary = constraint.index(" ")
var = constraint[:word_boundary]
constraint = constraint[word_boundary:].lstrip()
word_boundary = constraint.index(" ")
op, val = constraint[:word_boundary], constraint[word_boundary:].strip()
assert op in {">", "<", ">=", "<=", "==", "!="}
return ParamConstraint(Name(var), op, val)
def satisfied(self, inputs: ParameterMapping) -> bool:
"""
Determines if a constraint is met by a particular instance of inputs
:param inputs: the parameters to check
:return: whether or not these inputs satisfy the constraint
"""
raise NotImplementedError("This is an abstract method, use the corresponding method in a subtype")
@dataclass
class ParamConstraint(Constraint):
"""
A constraint on a single parameter
"""
var: Name
op: str
val: str
def satisfied(self, inputs: ParameterMapping) -> bool:
return eval(f"{self.var} {self.op} {self.val}", dict(inputs))
@property
def value(self) -> SomeValue:
"""
Evaluates the value of the constraint
:return: the value in the constraint
"""
return eval(self.val)
@dataclass
class GlobalContstraint(Constraint):
"""
A general constraint on any number of parameters
"""
predicate: str
def satisfied(self, inputs: ParameterMapping) -> bool:
return eval(self.predicate, dict(inputs))
@dataclass
class FunctionInfo:
"""
A wrapper for additional information found in a function's *props* file.
This includes the names of any output parameters, and the given sizes of any array parameters.
"""
outputs: List[Name]
sizes: List[ParamSize]
constraints: List[Constraint]
@staticmethod
def parse(info: List[str]):
"""
Build a FunctionInfo instance from a list of description strings.
These strings may be describing either sizes or outputs.
:param info: the description strings
:return: the instance containing the information
"""
outputs = []
sizes = []
constraints = []
for line in info:
if line.startswith("output"):
outputs.append(Name(removeprefix(line, "output").strip()))
elif line.startswith("size"):
size = ParamSize.parse(removeprefix(line, "size").strip())
sizes.append(size)
elif line.startswith("constraint"):
constraint = Constraint.parse(removeprefix(line, "constraint").strip())
constraints.append(constraint)
else:
raise ParseError(f"invalid directive in props: {line.strip()}")
return FunctionInfo(outputs, sizes, constraints)
def is_output(self, param: CParameter) -> bool:
return param.name in self.outputs
def size(self, param: CParameter) -> Optional[ParamSize]:
return {size.arr: size for size in self.sizes}.get(param.name)
@dataclass
class FunctionProps:
"""
Contains all information from a function's *props* file.
This includes the signature and any additional information about the parameters.
"""
sig: FunctionSignature
arr_info: FunctionInfo
@staticmethod
def parse(props_file: str):
"""
Build a FunctionProps instance from a *props* file.
:param props_file: the path to the *props* file
:return: the instance built from that file
"""
with open(props_file, "r") as props:
sig = FunctionSignature.parse(props.readline())
rest = FunctionInfo.parse(props.readlines())
return FunctionProps(sig, rest)
@dataclass
class CReference:
"""
Contains all relevant information from a function's *ref.c* file.
This is the :code:`#includes` found in the file, as well as the C implementation of the function itself.
"""
includes: List[str]
code: str
@staticmethod
def parse(ref_file: str):
"""
Build a CReference instance from a given *ref.c* file.
:param ref_file: the path to the *ref.c* file
:return: the instance built from that file
"""
with open(ref_file, "r") as ref:
includes = []
# go through each line and:
# 1. store includes
# 2. ignore anything other than the function
# 3. store the function code
line = "" # this is just to ensure line has SOME value, to shut the warning up
for line in ref:
line = line.lstrip()
if re.match("(int|float|double|char|bool|void)", line):
break # assumes everything from here is the actual function
if line.startswith("#include"):
includes.append(line.rstrip())
func = line + ref.read()
return CReference(includes, func)
@dataclass
class FunctionReference:
"""
Wrapper for all information about a given function.
"""
signature: FunctionSignature
info: FunctionInfo
reference: CReference
@property
def type(self):
return self.signature.type
@property
def parameters(self):
return self.signature.parameters
def outputs(self):
return [parameter for parameter in self.parameters if self.info.is_output(parameter)]
@property
def name(self):
return self.signature.name
@property
def code(self):
return self.reference.code
@staticmethod
def parse(prog_name: str):
"""
Build a FunctionReference from an actual C function.
This function must have a directory containing *ref.c* and *props* files.
:param prog_name: the path to the function directory
:return: the instance built for that function
"""
path = os.path.expanduser(prog_name)
try:
props = FunctionProps.parse(os.path.join(path, "props"))
ref = CReference.parse(os.path.join(path, "ref.c"))
except ParseError as e:
raise ParseError(e.message, reference_name=os.path.split(prog_name)[1])
return FunctionReference(props.sig, props.arr_info, ref)
def issues(self, fix: bool = False) -> Set[ParseIssue]:
"""
Check this FunctionReference for any issues.
:param fix: set to :code:`True` to try and fix any issues that are encountered
:return: all issues found in the function
"""
issues = set()
if self.type.pointer_level != 0:
issues.add(ParseIssue.ArrayReturnType)
# building lookup tables
param_dict = dict()
array_params = set()
scalar_params = set()
for param in self.parameters:
name = param.name
c_type = param.type
param_dict[name] = c_type
if c_type.pointer_level == 0:
scalar_params.add(name)
elif c_type.pointer_level == 1:
array_params.add(name)
else:
issues.add(ParseIssue.MultiLevelPointer)
# this is a SUPER simplified version of checking for valid C identifiers
# doesn't take keywords etc. into consideration
m = re.match(r"^[a-zA-Z_]\w*$", name, flags=re.ASCII)
if not m or m[0] != name:
issues.add(ParseIssue.InvalidIdentifierName)
for constraint in self.info.constraints:
if not isinstance(constraint, ParamConstraint):
continue
if (constraint.var in array_params or param_dict[
constraint.var].contents == "char") and constraint.op not in {"==", "!="}:
issues.add(ParseIssue.InvalidConstraint)
for output in self.info.outputs:
if param_dict[output].pointer_level == 0:
if fix:
self.info.outputs.remove(output)
else:
issues.add(ParseIssue.ScalarOutputParameter)
sized = set()
for size in self.info.sizes:
array = size.arr
sized.add(array)
if array not in array_params:
if fix:
self.info.sizes.remove(size)
else:
issues.add(ParseIssue.ScalarGivenSize)
if isinstance(size, VarSize):
var = size.var
if param_dict[var].contents not in {"int"}:
issues.add(ParseIssue.GivenInvalidSize)
elif isinstance(size, ConstSize):
if size.size < 0:
issues.add(ParseIssue.GivenInvalidSize)
elif isinstance(size, ExprSize):
if size.init < 0:
issues.add(ParseIssue.GivenInvalidSize)
for array in array_params - sized:
if fix and param_dict[array] == CType("char", 1):
default_str_size = 100
self.info.sizes.append(ConstSize(array, default_str_size))
else:
issues.add(ParseIssue.UnsizedArrayParameter)
code = self.code
ref_signature = FunctionSignature.parse(code[:code.find("{")])
if ref_signature != self.signature:
# try and fix here if possible
issues.add(ParseIssue.ReferenceSignatureMismatch)
return issues
def validate(self, issues, ignorable: Set[issues] = None):
if ignorable is None:
ignorable = ParseIssue.ignorable()
if issues - ignorable:
raise ParseError("parse contained issues", reference_name=self.name)
def show_issues(self, issues: Set[ParseIssue], verbose: bool = False, ignore_good: bool = False) -> None:
"""
Write any issues in the function to stderr
:param issues: issues to show
:param verbose: set to :code:`True` to include a full breakdown of any issues found
:param ignore_good: set to :code:`True` to write to stderr even if no issues are found
"""
if issues:
print(f"error: {self.name} is broken!", file=stderr)
for issue in issues:
print(f" - {issue.value}", file=stderr)
if verbose:
print(dumps(asdict(self), indent=4) + "\n", file=stderr)
elif not ignore_good:
print(f"{self.name} is good", file=stderr)
def log_issues(self, issues: Set[ParseIssue]):
"""
Write any issues found to the error log
:param issues: any issues found
"""
if not issues:
return
logger = lumberjack.getLogger("error")
msg = f"{self.name} has issues: [{', '.join(issue.name for issue in issues)}]"
logger.warning(msg)
def show_all(base_path: str) -> None:
"""
Parse and show the C signature for all functions in a given directory.
Also flags errors if they occur, writing the results to stderr.
:param base_path: the path to the directory containing all of the functions
"""
base_path = os.path.expanduser(base_path)
for directory in os.listdir(base_path):
# breaking these up cos one big if was ugly
if directory.startswith("__"):
continue
if directory.startswith("."):
continue
# building out the proper path to the function
dir_path = os.path.join(base_path, directory)
if not os.path.isdir(dir_path):
continue
if "ref.c" not in os.listdir(dir_path):
continue
if "props" not in os.listdir(dir_path):
continue
parsed = FunctionReference.parse(dir_path)
parsed.show_issues(parsed.issues(), ignore_good=True)
print(parsed.signature.c_sig())
def show_single(path_to_ref: str):
"""
Parse and display the signature for a single program.
Signature is given in functional form, and the full information is given if issues are found.
:param path_to_ref: the reference directory
"""
contents = FunctionReference.parse(path_to_ref)
issues = contents.issues(fix=True)
if issues:
contents.show_issues(issues, verbose=True)
else:
print(dumps(asdict(contents), indent=4))
def load_reference(path_to_reference: str, log_issues: Callable = FunctionReference.log_issues) -> FunctionReference:
"""
Creates a reference from a reference directory
This will check the reference is valid, and will fail if the reference is not.
:param path_to_reference: the reference directory
:param log_issues: the method used to log any issues
:return: the reference built from that directory
"""
func = FunctionReference.parse(path_to_reference)
issues = func.issues(fix=True)
log_issues(func, issues)
func.validate(issues)
return func
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--all", help="display and debug all available references", action="store_true")
parser.add_argument("program", nargs="?", help="parse and output the given program")
parser.add_argument("-p", "--path", help="specify root path to example", default=".")
args = parser.parse_args()
if (args.program is None) != args.all: # this is confusing as hell, either program is set, or all is (XOR)
parser.print_usage(file=stderr)
print(f"{parser.prog}: error: exactly one argument must be set from (--all, program)", file=stderr)
exit(1)
if args.all:
show_all(args.path)
else:
show_single(os.path.join(args.path, args.program))