|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import argparse |
| 4 | + |
| 5 | +# Make sure we pick the current version of pgen2 (and not whatever is installed) |
| 6 | +CURRENT_FOLDER_LOCATION = os.path.dirname(os.path.realpath(__file__)) |
| 7 | +LIB2TO3LOCATION = os.path.realpath(os.path.join(CURRENT_FOLDER_LOCATION, |
| 8 | + '..', '..', 'Lib', 'lib2to3')) |
| 9 | + |
| 10 | +sys.path.insert(0, LIB2TO3LOCATION) |
| 11 | +from pgen2 import pgen |
| 12 | +sys.path.pop(0) |
| 13 | + |
| 14 | +def main(grammar_file, gramminit_h_file, gramminit_c_file, verbose): |
| 15 | + grammar = pgen.generate_grammar(grammar_file, verbose=verbose) |
| 16 | + grammar.produce_graminit_h(gramminit_h_file.write) |
| 17 | + grammar.produce_graminit_c(gramminit_c_file.write) |
| 18 | + |
| 19 | + |
| 20 | +if __name__ == "__main__": |
| 21 | + parser = argparse.ArgumentParser(description="Parser generator main program.") |
| 22 | + parser.add_argument( |
| 23 | + "grammar", type=str, help="The file with the grammar definition in EBNF format" |
| 24 | + ) |
| 25 | + parser.add_argument( |
| 26 | + "gramminit_h", |
| 27 | + type=argparse.FileType('w'), |
| 28 | + help="The path to write the grammar's non-terminals as #defines", |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + "gramminit_c", |
| 32 | + type=argparse.FileType('w'), |
| 33 | + help="The path to write the grammar as initialized data", |
| 34 | + ) |
| 35 | + parser.add_argument("--verbose", "-v", action="count") |
| 36 | + args = parser.parse_args() |
| 37 | + main(args.grammar, args.gramminit_h, args.gramminit_c, args.verbose) |
0 commit comments