diff options
author | Harald Welte <laforge@osmocom.org> | 2020-03-27 00:28:53 +0100 |
---|---|---|
committer | Harald Welte <laforge@osmocom.org> | 2020-09-15 10:47:45 +0200 |
commit | 46bc6d25d62321b5bac0c6b8615e684f14ae293f (patch) | |
tree | febd0e27324ef265d65edba4831405fff63c35b6 /format_ipr.py | |
parent | 1e42420e576cf8f1626ee4d1500aa46890937135 (diff) |
HACK: support for SIM factory file formatssysmocom/factory
Change-Id: I23cd37fc06b6e2d21964fd4f2694d9ee3c6012d4
Diffstat (limited to 'format_ipr.py')
-rw-r--r-- | format_ipr.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/format_ipr.py b/format_ipr.py new file mode 100644 index 0000000..940e318 --- /dev/null +++ b/format_ipr.py @@ -0,0 +1,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 |