Make the JSON parser fast again

This commit is contained in:
Erez Sh 2019-12-25 11:19:10 +02:00
parent dcc9d46eef
commit f7a6366b6c
1 changed files with 11 additions and 1 deletions

View File

@ -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