Skip to content

Commit 80c1217

Browse files
committed
Add pgen module to the Parser folder to produce pgen output using pgen2
1 parent 8e6005c commit 80c1217

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Parser/pgen/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.
2+
# Licensed to PSF under a Contributor Agreement.
3+
4+
"""The pgen2 package."""

Parser/pgen/__main__.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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

Comments
 (0)