mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-03-25 06:48:33 +03:00
HACK: support for SIM factory file formats
Change-Id: I23cd37fc06b6e2d21964fd4f2694d9ee3c6012d4
This commit is contained in:
55
format_ipr.py
Normal file
55
format_ipr.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user