From f7a6366b6c2e3a6963bceb11a4e0cdbd5a41b7c2 Mon Sep 17 00:00:00 2001 From: Erez Sh Date: Wed, 25 Dec 2019 11:19:10 +0200 Subject: [PATCH] Make the JSON parser fast again --- examples/json_parser.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/examples/json_parser.py b/examples/json_parser.py index ba1ff1e..7aa7d0f 100644 --- a/examples/json_parser.py +++ b/examples/json_parser.py @@ -49,11 +49,21 @@ class TreeToJson(Transformer): false = lambda self, _: False +### Create the JSON parser with Lark, using the Earley algorithm # 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()) +### 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()) parse = json_parser.parse