File size: 1,504 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | # SPDX-License-Identifier: LGPL-2.1-or-later
#
# Copyright (c) 2025 The FreeCAD Project
"""This script generates a standalone Python parser from the ArchSql.lark grammar."""
import sys
import os
try:
from lark import Lark
from lark.tools.standalone import gen_standalone
except ImportError:
print("Error: The 'lark' Python package is required to generate the parser.")
print("Please install it using: pip install lark")
sys.exit(1)
def main():
if len(sys.argv) != 3:
print("Usage: python ArchSqlParserGenerator.py <input_grammar.lark> <output_parser.py>")
return 1
input_file = sys.argv[1]
output_file = sys.argv[2]
if not os.path.exists(input_file):
print(f"Error: Input grammar file not found at '{input_file}'")
return 1
print(
f"Generating standalone parser from '{os.path.basename(input_file)}' to '{os.path.basename(output_file)}'..."
)
# 1. Read the grammar file content.
with open(input_file, "r", encoding="utf8") as f:
grammar_text = f.read()
# 2. Create an instance of the Lark parser.
# The 'lalr' parser is recommended for performance.
lark_instance = Lark(grammar_text, parser="lalr")
# 3. Open the output file and call the gen_standalone() API function.
with open(output_file, "w", encoding="utf8") as f:
gen_standalone(lark_instance, out=f)
print("Parser generation complete.")
return 0
if __name__ == "__main__":
sys.exit(main())
|