Files
spark/tools/py/gen_cs_grammar.py
T
John Colvin cc2959249e build: Add lexer/parser.cs files to src ctrl
Move all lexer/parser files into a single dir, DataStreamGrammar, for
organizational purposes and to avoid dependency conflicts when
generating code. Note that we'll only need to regen cs if we modify the
parser/lexer files, which seems unlikely.
2023-09-03 13:53:07 -04:00

96 lines
2.6 KiB
Python

import argparse
import os
import subprocess
import sys
from typing import Optional
from core.file_utils import SparkPaths, find_files
from pathlib import Path
def _get_arg_parser():
parser = argparse.ArgumentParser(
description='Generates csharp lexers (lexer.cs) and parsers (parser.cs)'
)
parser.add_argument(
'--sources',
nargs='+',
help='The source files for which to generate cs',
)
parser.add_argument(
'--clean',
action='store_true',
help='Delete existing lexer.cs and parser.cs files in --out-dir',
)
parser.set_defaults(clean=False)
return parser
def _generate_code(src: Path, generator_exe: Path):
if not src.exists():
raise ValueError(f"Source file '{src}' does not exist")
if not src.is_file():
raise ValueError(f"Source file '{src}' is not a file")
generator_cmd_parts = [str(generator_exe), str(src)]
generator_cmd = ' '.join(generator_cmd_parts)
subprocess.run(generator_cmd, shell=True)
def generate_lexers(src_paths: list[Path]):
lexers = [f for sp in src_paths for f in find_files(sp, '.lexer')]
exe = SparkPaths.externals().joinpath(
'windows',
'bin',
'cstools',
'lg.exe',
)
for src_file in lexers:
_generate_code(src_file, exe)
def generate_parsers(src_paths: list[Path]):
parsers = [f for sp in src_paths for f in find_files(sp, '.parser')]
exe = SparkPaths.externals().joinpath(
'windows',
'bin',
'cstools',
'pg.exe',
)
for src_file in parsers:
if Path().absolute() != src_file.parent.absolute():
os.chdir(src_file.parent.absolute())
_generate_code(src_file, exe)
def clean_lexers(src_paths: list[Path]):
cs_lexers = [f for sp in src_paths for f in find_files(sp, '.lexer.cs')]
for cs_lexer in cs_lexers:
os.remove(cs_lexer)
def clean_parsers(src_paths: list[Path]):
cs_parsers = [f for sp in src_paths for f in find_files(sp, '.parser.cs')]
for cs_parser in cs_parsers:
os.remove(cs_parser)
def _main():
if sys.platform != 'win32':
raise ValueError('Only supported on Windows')
parser = _get_arg_parser()
args = parser.parse_args()
if args.sources is None:
print('No sources specified')
exit(1)
sources = [Path(s).absolute() for s in args.sources]
if args.clean:
clean_lexers(sources)
clean_parsers(sources)
return
generate_lexers(sources)
generate_parsers(sources)
if __name__ == '__main__':
_main()