Debian Codegraph @

src/vttLib/parser.py

raw
1 import string
2
3 from pyparsing import (
4 Combine,
5 Group,
6 Literal,
7 OneOrMore,
8 Optional,
9 ParseException,
10 Regex,
11 Suppress,
12 Word,
13 alphanums,
14 alphas,
15 cStyleComment,
16 nestedExpr,
17 nums,
18 oneOf,
19 pyparsing_common,
20 tokenMap,
21 )
22
23 __all__ = ["AssemblyParser", "ParseException"]
24
25
26 VTT_MNEMONIC_FLAGS = {
27 # Direction
28 "X": "1", # X axis
29 "Y": "0", # Y axis
30 # Outline
31 "O": "1", # Use original outline
32 "N": "0", # Use gridfitted outline
33 # Rounding or Line Relation
34 "R": "1", # Round distance; or perpedicular to line
35 "r": "0", # Do not round distance; or parallel to line
36 # Reference Point Autoset
37 "M": "1", # Set rp0 to point number on the stack
38 "m": "0", # Do not set rp0
39 # Reference Point Usage
40 "1": "1", # Use rp1
41 "2": "0", # Use rp2
42 # Minimum Distance flag
43 ">": "1", # Obey minimum distance
44 "<": "0", # Do not obey minimum distance
45 # Color (Distance Type)
46 "Gr": "00", # Gray
47 "Bl": "01", # Black
48 "Wh": "10", # White
49 }
50
51
52 alpha_upper = string.ascii_uppercase
53
54 mnemonic = Word(alpha_upper, bodyChars=alpha_upper + nums).setResultsName("mnemonic")
55
56 # XXX can't use pyparsing_common.signedInteger as the latest pyparsing 2.1.5
57 # has a bug which always converts them to floats. Remove this once 2.1.6 is
58 # published on PyPI.
59 signed_integer = (
60 Regex(r"[+-]?\d+").setName("signed integer").setParseAction(tokenMap(int))
61 )
62
63 variable = Word(alphas, bodyChars=alphanums)
64
65 stack_item = Suppress(",") + (signed_integer | Suppress("*") | variable)
66
67 flag = oneOf(list(VTT_MNEMONIC_FLAGS.keys()))
68 # convert flag to binary string
69 flag.setParseAction(tokenMap(lambda t: VTT_MNEMONIC_FLAGS[t]))
70 flags = Combine(OneOrMore(flag)).setResultsName("flags")
71
72 delta_point_index = pyparsing_common.integer.setResultsName("point_index")
73 delta_rel_ppem = pyparsing_common.integer.setResultsName("rel_ppem")
74 delta_step_no = signed_integer.setResultsName("step_no")
75 # the step denominator is only used in VTT's DELTA[CP]* instructions,
76 # and must always be 8 (sic!), so we can suppress it.
77 delta_spec = (
78 delta_point_index
79 + Suppress("@")
80 + delta_rel_ppem
81 + delta_step_no
82 + Optional(Literal("/8")).suppress()
83 )
84
85 delta = nestedExpr("(", ")", delta_spec, ignoreExpr=None)
86
87 deltas = Group(OneOrMore(delta)).setResultsName("deltas")
88
89 args = deltas | flags
90
91 stack_items = OneOrMore(stack_item).setResultsName("stack_items")
92
93 instruction = Group(
94 mnemonic + Suppress("[") + Optional(args) + Suppress("]") + Optional(stack_items)
95 )
96
97 label = Word("#", alphanums)
98 jump_label = Group(Combine(label + Literal(":")).setResultsName("mnemonic"))
99 assignment = Group(
100 variable.setResultsName("variable")
101 + Literal("=").suppress()
102 + label.setResultsName("label")
103 ).setResultsName("assignment")
104 jump_mnemonic = oneOf(["JMPR", "JROT", "JROF"]).setResultsName("mnemonic")
105 jump = Group(
106 jump_mnemonic
107 + Suppress("[")
108 + Suppress("]")
109 + Suppress(",")
110 + Suppress("(")
111 + assignment
112 + Suppress(")")
113 )
114
115 pragma_memonic = Word("#", bodyChars=alpha_upper).setResultsName("mnemonic")
116
117 pragma = Group(pragma_memonic + Optional(stack_items))
118
119 comment = cStyleComment.suppress()
120
121 # this is the only public class
122 AssemblyParser = OneOrMore(comment | jump_label | pragma | jump | instruction)
123
124
125 if __name__ == "__main__":
126 import sys
127
128 infile = sys.argv[1]
129 with open(infile, "r") as fp:
130 data = fp.read()
131
132 tokens = AssemblyParser.parseString(data, parseAll=True)
133
134 for i, t in enumerate(tokens):
135 print(i, repr(t))