51 lines
1.0 KiB
Python
51 lines
1.0 KiB
Python
import sys
|
|
from lark import Lark, Transformer
|
|
|
|
json_grammar = r"""
|
|
?value: dict
|
|
| list
|
|
| string
|
|
| SIGNED_NUMBER -> number
|
|
| "true" -> true
|
|
| "false" -> false
|
|
| "null" -> null
|
|
|
|
list : "[" [value ("," value)*] "]"
|
|
|
|
dict : "{" [pair ("," pair)*] "}"
|
|
pair : string ":" value
|
|
|
|
string : ESCAPED_STRING
|
|
|
|
%import common.ESCAPED_STRING
|
|
%import common.SIGNED_NUMBER
|
|
%import common.WS
|
|
%ignore WS
|
|
"""
|
|
|
|
|
|
class TreeToJson(Transformer):
|
|
def string(self, s):
|
|
(s,) = s
|
|
return s[1:-1]
|
|
|
|
def number(self, n):
|
|
(n,) = n
|
|
return float(n)
|
|
|
|
list = list
|
|
pair = tuple
|
|
dict = dict
|
|
|
|
null = lambda self, _: None
|
|
true = lambda self, _: True
|
|
false = lambda self, _: False
|
|
|
|
|
|
json_parser = Lark(json_grammar, start='value', parser='lalr', transformer=TreeToJson())
|
|
|
|
if __name__ == '__main__':
|
|
text = '{"key": ["item0", "item1", 3.14, true]}'
|
|
tree = json_parser.parse(text)
|
|
print(tree)
|