lark/examples/json_parser.py

79 lines
1.9 KiB
Python
Raw Normal View History

#
# This example shows how to write a basic JSON parser
#
2018-01-02 07:57:22 +00:00
# The code is short and clear, and outperforms every other parser (that's written in Python).
2017-02-17 21:23:13 +00:00
# For an explanation, check out the JSON parser tutorial at /docs/json_tutorial.md
#
import sys
2017-02-11 09:12:15 +00:00
2018-06-18 08:30:32 +00:00
from lark import Lark, Transformer, v_args
json_grammar = r"""
?start: value
?value: object
| array
| string
2017-02-26 11:12:16 +00:00
| SIGNED_NUMBER -> number
| "true" -> true
| "false" -> false
| "null" -> null
array : "[" [value ("," value)*] "]"
object : "{" [pair ("," pair)*] "}"
pair : string ":" value
2017-02-23 17:08:23 +00:00
string : ESCAPED_STRING
2017-02-23 17:08:23 +00:00
%import common.ESCAPED_STRING
2017-02-23 22:45:34 +00:00
%import common.SIGNED_NUMBER
2017-02-23 17:08:23 +00:00
%import common.WS
%ignore WS
"""
class TreeToJson(Transformer):
2018-06-18 08:30:32 +00:00
@v_args(inline=True)
def string(self, s):
return s[1:-1].replace('\\"', '"')
array = list
pair = tuple
object = dict
2018-06-18 08:30:32 +00:00
number = v_args(inline=True)(float)
null = lambda self, _: None
true = lambda self, _: True
false = lambda self, _: False
2018-01-02 07:57:22 +00:00
# json_parser = Lark(json_grammar, parser='earley', lexer='standard')
# def parse(x):
# return TreeToJson().transform(json_parser.parse(x))
json_parser = Lark(json_grammar, parser='lalr', lexer='standard', transformer=TreeToJson())
2018-01-02 07:57:22 +00:00
parse = json_parser.parse
def test():
test_json = '''
{
"empty_object" : {},
"empty_array" : [],
"booleans" : { "YES" : true, "NO" : false },
"numbers" : [ 0, 1, -2, 3.3, 4.4e5, 6.6e-7 ],
"strings" : [ "This", [ "And" , "That", "And a \\"b" ] ],
"nothing" : null
}
'''
j = parse(test_json)
print(j)
import json
assert j == json.loads(test_json)
if __name__ == '__main__':
# test()
with open(sys.argv[1]) as f:
print(parse(f.read()))