• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SPF-OST / pytrnsys_process / 12428835679

20 Dec 2024 09:22AM UTC coverage: 97.781% (+0.1%) from 97.674%
12428835679

push

github

sebastian-swob
initial somewhat working parser
TODO:
ask damian about equations
ask if using lower on constant names is okay
potential third pass

77 of 78 new or added lines in 3 files covered. (98.72%)

749 of 766 relevant lines covered (97.78%)

1.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.41
/pytrnsys_process/deck/extractor.py
1
import lark as _lark
2✔
2

3

4
class ConstantsVisitor(_lark.Visitor):
2✔
5
    def __init__(self):
2✔
6
        self.resolved_constants: dict = {}
2✔
7

8
    def constants(self, tree: _lark.Tree):
2✔
9
        constants_transformer = ConstantsTransformer()
2✔
10
        visitor = constants_transformer.transform(tree)
2✔
11

12
        unresolved = {}
2✔
13
        for equation in visitor:
2✔
14
            if equation and "=" in equation:
2✔
15
                key, value = equation.split("=")
2✔
16
                try:
2✔
17
                    evaluated_value = eval(value)
2✔
18
                    self.resolved_constants[key] = evaluated_value
2✔
19
                except NameError:
2✔
20
                    unresolved[key] = value
2✔
21

22
        while unresolved:
2✔
23
            resolved_this_pass = False
2✔
24
            for key, value in list(unresolved.items()):
2✔
25
                try:
2✔
26
                    evaluated_value = eval(
2✔
27
                        value, {"__builtins__": {}}, self.resolved_constants
28
                    )
29
                    self.resolved_constants[key] = evaluated_value
2✔
30
                    del unresolved[key]
2✔
31
                    resolved_this_pass = True
2✔
32
                except NameError:
2✔
33
                    print(f"\nCould not be resolved after pass: {unresolved}")
2✔
34
                    continue
2✔
35

36
            if not resolved_this_pass and unresolved:
2✔
37
                print(f"\nCould not be resolved: {unresolved}")
2✔
38
                break
2✔
39

40

41
class ConstantsTransformer(_lark.Transformer):
2✔
42

43
    def equation(self, items):
2✔
44
        return f"{items[0]}={items[1]}"
2✔
45

46
    def explicit_var(self, items):
2✔
47
        return str(items[0])
2✔
48

49
    def default_visibility_var(self, items):
2✔
50
        # Using lower because the same constant is written in upper and lower in some places
51
        return str(items[0]).lower()
2✔
52

53
    def func_call(self, items):
2✔
54
        func_name = items[0]
2✔
55
        args = items[1]
2✔
56
        return f"{func_name}({','.join(args)})"
2✔
57

58
    def func_name(self, items):
2✔
59
        return items[0]
2✔
60

61
    def func_args(self, items):
2✔
62
        return items
2✔
63

64
    def divided_by(self, items):
2✔
65
        return f"{items[0]}/{items[1]}"
2✔
66

67
    def times(self, items):
2✔
68
        return f"{items[0]}*{items[1]}"
2✔
69

70
    def to_power_of(self, items):
2✔
71
        return f"{items[0]}**{items[1]}"
2✔
72

73
    def plus(self, items):
2✔
74
        return f"{items[0]}+{items[1]}"
2✔
75

76
    def minus(self, items):
2✔
77
        return f"{items[0]}-{items[1]}"
2✔
78

79
    def negate(self, items):
2✔
80
        return f"-{items[0]}"
2✔
81

82
    def number(self, items):
2✔
83
        return str(items[0])
2✔
84

85
    def start(self, items):
2✔
NEW
86
        return "\n".join(items)
×
87

88
    def constants(self, items):
2✔
89
        # Skip the "CONSTANTS" token and number_of_constants
90
        return [x for x in items[2:] if x is not None]
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc