diff --git a/examples/advanced/_json_parser.py b/examples/advanced/_json_parser.py new file mode 100644 index 0000000..80d9101 --- /dev/null +++ b/examples/advanced/_json_parser.py @@ -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()) + diff --git a/examples/advanced/error_puppet.py b/examples/advanced/error_puppet.py index b749749..36c14c4 100644 --- a/examples/advanced/error_puppet.py +++ b/examples/advanced/error_puppet.py @@ -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. """ -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): if e.token.type == 'COMMA': diff --git a/examples/advanced/error_reporting_lalr.py b/examples/advanced/error_reporting_lalr.py index bf95bd6..102f7b1 100644 --- a/examples/advanced/error_reporting_lalr.py +++ b/examples/advanced/error_reporting_lalr.py @@ -7,7 +7,7 @@ A demonstration of example-driven error reporting with the LALR parser """ 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') diff --git a/examples/advanced/reconstruct_json.py b/examples/advanced/reconstruct_json.py index 4506c3a..201bc32 100644 --- a/examples/advanced/reconstruct_json.py +++ b/examples/advanced/reconstruct_json.py @@ -14,7 +14,7 @@ import json from lark import Lark from lark.reconstruct import Reconstructor -from .json_parser import json_grammar +from _json_parser import json_grammar test_json = ''' { diff --git a/examples/lark_grammar.py b/examples/lark_grammar.py index e8566fb..b424e87 100644 --- a/examples/lark_grammar.py +++ b/examples/lark_grammar.py @@ -4,18 +4,22 @@ Lark Grammar 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 = [ - '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():