-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from erezsh/lark_cache
Use Lark with its cache feature, instead of creating a standalone parser
- Loading branch information
Showing
4 changed files
with
19 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,3 +120,4 @@ node_modules/ | |
|
||
# Don't commit the generated parser | ||
lark_parser.py | ||
.lark_cache.bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,18 @@ | ||
"""A parser for HCL2 implemented using the Lark parser""" | ||
from pathlib import Path | ||
|
||
from hcl2.transformer import DictTransformer | ||
|
||
THIS_DIR = Path(__file__).absolute().resolve().parent | ||
PARSER_FILE = THIS_DIR / "lark_parser.py" | ||
|
||
from lark import Lark | ||
|
||
def create_parser_file(parser_file: Path = PARSER_FILE) -> None: | ||
""" | ||
Parsing the Lark grammar takes about 0.5 seconds. In order to improve performance we can cache the parser | ||
file. The below code caches the entire python file which is generated by Lark's standalone parser feature | ||
See: https://github.com/lark-parser/lark/blob/master/lark/tools/standalone.py | ||
Lark also supports serializing the parser config but the deserialize function did not work for me. | ||
The lark state contains dicts with numbers as keys which is not supported by json so the serialized | ||
state can't be written to a json file. Exporting to other file types would have required | ||
adding additional dependencies or writing a lot more code. Lark's standalone parser | ||
feature works great but it expects to be run as a separate shell command | ||
The below code copies some of the standalone parser generator code in a way that we can use | ||
""" | ||
# This function is needed only if standalone parser is not yet compiled. | ||
# pylint: disable=import-outside-toplevel | ||
from lark import Lark | ||
from lark.tools.standalone import gen_standalone | ||
|
||
lark_file = THIS_DIR / "hcl2.lark" | ||
with open(parser_file, "w", encoding="utf-8") as parser_file_stream: | ||
lark_inst = Lark(lark_file.read_text(), parser="lalr", lexer="contextual") | ||
parser_file_stream.write("# mypy: ignore-errors\n") | ||
gen_standalone(lark_inst, out=parser_file_stream) | ||
from hcl2.transformer import DictTransformer | ||
|
||
|
||
if not PARSER_FILE.exists(): | ||
create_parser_file(PARSER_FILE) | ||
PARSER_FILE = Path(__file__).absolute().resolve().parent / ".lark_cache.bin" | ||
|
||
# pylint: disable=wrong-import-position | ||
# Lark_StandAlone needs to be imported after the above block of code because lark_parser.py might not exist | ||
from hcl2.lark_parser import Lark_StandAlone | ||
|
||
hcl2 = Lark_StandAlone(transformer=DictTransformer()) | ||
hcl2 = Lark.open( | ||
"hcl2.lark", | ||
parser="lalr", | ||
cache=str(PARSER_FILE), # Disable/Delete file to effect changes to the grammar | ||
rel_to=__file__, | ||
transformer=DictTransformer(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters