| @@ -0,0 +1,64 @@ | |||||
| """ | |||||
| Simple JSON Parser | |||||
| ================== | |||||
| The code is short and clear, and outperforms every other parser (that's written in Python). | |||||
| For an explanation, check out the JSON parser tutorial at /docs/json_tutorial.md | |||||
| (this is here for use by the other examples) | |||||
| """ | |||||
| import sys | |||||
| from lark import Lark, Transformer, v_args | |||||
| json_grammar = r""" | |||||
| ?start: value | |||||
| ?value: object | |||||
| | array | |||||
| | string | |||||
| | SIGNED_NUMBER -> number | |||||
| | "true" -> true | |||||
| | "false" -> false | |||||
| | "null" -> null | |||||
| array : "[" [value ("," value)*] "]" | |||||
| object : "{" [pair ("," pair)*] "}" | |||||
| pair : string ":" value | |||||
| string : ESCAPED_STRING | |||||
| %import common.ESCAPED_STRING | |||||
| %import common.SIGNED_NUMBER | |||||
| %import common.WS | |||||
| %ignore WS | |||||
| """ | |||||
| class TreeToJson(Transformer): | |||||
| @v_args(inline=True) | |||||
| def string(self, s): | |||||
| return s[1:-1].replace('\\"', '"') | |||||
| array = list | |||||
| pair = tuple | |||||
| object = dict | |||||
| number = v_args(inline=True)(float) | |||||
| null = lambda self, _: None | |||||
| true = lambda self, _: True | |||||
| false = lambda self, _: False | |||||
| ### Create the JSON parser with Lark, using the LALR algorithm | |||||
| json_parser = Lark(json_grammar, parser='lalr', | |||||
| # Using the standard lexer isn't required, and isn't usually recommended. | |||||
| # But, it's good enough for JSON, and it's slightly faster. | |||||
| lexer='standard', | |||||
| # Disabling propagate_positions and placeholders slightly improves speed | |||||
| propagate_positions=False, | |||||
| maybe_placeholders=False, | |||||
| # Using an internal transformer is faster and more memory efficient | |||||
| transformer=TreeToJson()) | |||||
| @@ -10,9 +10,9 @@ to proceed step-by-step. When you've achieved the correct parse-state, | |||||
| you can resume the run by returning True. | you can resume the run by returning True. | ||||
| """ | """ | ||||
| from lark import UnexpectedToken, Token | |||||
| from lark import Token | |||||
| from .json_parser import json_parser | |||||
| from _json_parser import json_parser | |||||
| def ignore_errors(e): | def ignore_errors(e): | ||||
| if e.token.type == 'COMMA': | if e.token.type == 'COMMA': | ||||
| @@ -7,7 +7,7 @@ A demonstration of example-driven error reporting with the LALR parser | |||||
| """ | """ | ||||
| from lark import Lark, UnexpectedInput | from lark import Lark, UnexpectedInput | ||||
| from .json_parser import json_grammar # Using the grammar from the json_parser example | |||||
| from _json_parser import json_grammar # Using the grammar from the json_parser example | |||||
| json_parser = Lark(json_grammar, parser='lalr') | json_parser = Lark(json_grammar, parser='lalr') | ||||
| @@ -14,7 +14,7 @@ import json | |||||
| from lark import Lark | from lark import Lark | ||||
| from lark.reconstruct import Reconstructor | from lark.reconstruct import Reconstructor | ||||
| from .json_parser import json_grammar | |||||
| from _json_parser import json_grammar | |||||
| test_json = ''' | test_json = ''' | ||||
| { | { | ||||
| @@ -4,18 +4,22 @@ Lark Grammar | |||||
| A reference implementation of the Lark grammar (using LALR(1)) | A reference implementation of the Lark grammar (using LALR(1)) | ||||
| """ | """ | ||||
| from lark import Lark | |||||
| import lark | |||||
| from pathlib import Path | |||||
| parser = Lark(open('examples/lark.lark'), parser="lalr") | |||||
| parser = lark.Lark.open('lark.lark', rel_to=__file__, parser="lalr") | |||||
| examples_path = Path(__file__).parent | |||||
| lark_path = Path(lark.__file__).parent | |||||
| grammar_files = [ | grammar_files = [ | ||||
| 'examples/python2.lark', | |||||
| 'examples/python3.lark', | |||||
| 'examples/lark.lark', | |||||
| 'examples/relative-imports/multiples.lark', | |||||
| 'examples/relative-imports/multiple2.lark', | |||||
| 'examples/relative-imports/multiple3.lark', | |||||
| 'lark/grammars/common.lark', | |||||
| examples_path / 'lark.lark', | |||||
| examples_path / 'advanced/python2.lark', | |||||
| examples_path / 'advanced/python3.lark', | |||||
| examples_path / 'relative-imports/multiples.lark', | |||||
| examples_path / 'relative-imports/multiple2.lark', | |||||
| examples_path / 'relative-imports/multiple3.lark', | |||||
| lark_path / 'grammars/common.lark', | |||||
| ] | ] | ||||
| def test(): | def test(): | ||||