aboutsummaryrefslogtreecommitdiffstats
path: root/format_ipr.py
blob: 940e318567b8e9b1f62390b3acad1856f968e5d7 (plain)
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
from lark import Lark, Transformer, Token, Tree
from script_format import ScriptFormat
from format_ldr import LdrXfrm

class IprXfrm(LdrXfrm):
    """ transform the parse tree into a more easily consumable form """
    def key(self, items):
        return ('key', ''.join(list(items)))
    def req(self, items):
        return items[:-1]
    def rsp(self, items):
        return items[:-1]
    #def NEWLINE(self, items):
        #return None


class ScriptFormatIPR(ScriptFormat):
    # parser for the IPR file format as used by the SIM card factory
    ipr_parser = Lark(r"""
        script: statement*
        ?statement: cmd | rst | rem | NEWLINE

        NONL: /[^\n]/+
        rem: "//" NONL? NEWLINE

        ALNUM: DIGIT | LETTER | "_"
        key: "[" ALNUM+ "]"

        cmd: req rsp

        req: "I:" [hexstr|key]+ NEWLINE
        hexstr: HEX_ITEM+
        HEX_ITEM: HEXDIGIT ~ 2

        rsp: "O:" swpattern? NEWLINE
        swpattern: HEX_OR_X ~ 4
        HEX_OR_X: HEXDIGIT | "X" | "x"

        rst: "RESET" NEWLINE

        %import common.ESCAPED_STRING -> STRING
        %import common.WS_INLINE
        %import common.HEXDIGIT
        %import common.DIGIT
        %import common.LETTER
        %import common.NEWLINE
        %ignore WS_INLINE

        """, start='script', parser='lalr')#, lexer='standard')

    def parse_xform(self, text):
        tree = self.ipr_parser.parse(text)
        #print(tree.pretty())
        p = IprXfrm().transform(tree)
        return p