Skip to content

Commit

Permalink
Merge pull request #3356 from sorig/meta_fix_parser_files
Browse files Browse the repository at this point in the history
Automatically genereated parser files are now stored in the build dir…
  • Loading branch information
karlnapf committed Jul 7, 2016
2 parents 634585d + 9fea504 commit 901cec7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
3 changes: 3 additions & 0 deletions examples/meta/CMakeLists.txt
Expand Up @@ -7,6 +7,8 @@
# Generate type list
# execute_process(COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/generator/types/get_type_list.sh > ${CMAKE_CURRENT_SOURCE_DIR}/generator/types/typelist)

file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/parser_files)

# Run example generation script
add_custom_target(meta_examples
COMMAND ${PYTHON_EXECUTABLE}
Expand All @@ -16,6 +18,7 @@ add_custom_target(meta_examples
-t ${CMAKE_CURRENT_SOURCE_DIR}/generator/targets
-g ${CTAGS_FILE}
--store-vars
--parser_files_dir=${CMAKE_CURRENT_BINARY_DIR}/parser_files
COMMENT "Generating examples from meta-language"
DEPENDS ctags)

Expand Down
9 changes: 6 additions & 3 deletions examples/meta/generator/generate.py
Expand Up @@ -31,7 +31,8 @@ def parseCtags(filename):


def translateExamples(inputDir, outputDir, targetsDir, ctagsFile,
includedTargets=None, storeVars=False):
includedTargets=None, storeVars=False,
generatedFilesOutputDir=None):

tags = parseCtags(ctagsFile)
# Load all target dictionaries
Expand Down Expand Up @@ -62,7 +63,7 @@ def translateExamples(inputDir, outputDir, targetsDir, ctagsFile,
# Parse the example file
filePath = os.path.join(dirRelative, filename)
with open(os.path.join(inputDir, dirRelative, filename), 'r') as file:
ast = parse(file.read(), filePath)
ast = parse(file.read(), filePath, generatedFilesOutputDir)

# Translate ast to each target language
for target in targets:
Expand Down Expand Up @@ -117,6 +118,7 @@ def translateExamples(inputDir, outputDir, targetsDir, ctagsFile,
'lua',
]
parser.add_argument('targets', nargs='*', help="Targets to include (one or more of: %s). If not specified all targets are produced." % (' '.join(available_targets)))
parser.add_argument("--parser_files_dir", nargs='?', help='Path to directory where generated parser and lexer files should be stored.')

args = parser.parse_args()

Expand All @@ -137,4 +139,5 @@ def translateExamples(inputDir, outputDir, targetsDir, ctagsFile,

translateExamples(inputDir=inputDir, outputDir=outputDir,
targetsDir=targetsDir, ctagsFile=ctagsFile,
includedTargets=args.targets, storeVars=storeVars)
includedTargets=args.targets, storeVars=storeVars,
generatedFilesOutputDir=args.parser_files_dir)
23 changes: 17 additions & 6 deletions examples/meta/generator/parse.py
Expand Up @@ -6,13 +6,23 @@

# The FastParser parses input using PLY
class FastParser:
def __init__(self):
def __init__(self, generatedFilesOutputDir=None):
from ply import lex
from ply import yacc

generateFiles = not generatedFilesOutputDir is None

if generateFiles:
sys.path.append(generatedFilesOutputDir)

# Build the lexer and the parser
self.lexer = lex.lex(module=self, optimize=1)
self.parser = yacc.yacc(module=self)
self.lexer = lex.lex(module=self,
optimize=generateFiles,
outputdir=generatedFilesOutputDir)
self.parser = yacc.yacc(module=self,
write_tables=generateFiles,
outputdir=generatedFilesOutputDir,
debug=generateFiles)

def parse(self, programString, filePath="Unknown"):
"""
Expand Down Expand Up @@ -278,8 +288,8 @@ def p_error(self, p):
print("Reached end of file without completing parse")


def parse(programString, filePath):
parser = FastParser()
def parse(programString, filePath, generatedFilesOutputDir=None):
parser = FastParser(generatedFilesOutputDir)

# Parse input
return parser.parse(programString, filePath)
Expand All @@ -289,6 +299,7 @@ def parse(programString, filePath):
argparser = argparse.ArgumentParser()
argparser.add_argument("--pretty", action="store_true", help="If specified, output is pretty printed")
argparser.add_argument("path", nargs='?', help="Path to input file. If not specified input is read from stdin")
argparser.add_argument("--parser_files_dir", nargs='?', help='Path to directory where generated parser and lexer files should be stored.')
args = argparser.parse_args()

programString = ""
Expand All @@ -306,6 +317,6 @@ def parse(programString, filePath):
indentWidth = 2 if args.pretty > 0 else None

# Parse input and print json output
program = parse(programString, filePath)
program = parse(programString, filePath, args.parser_files_dir)

print(json.dumps(program, indent=indentWidth))

0 comments on commit 901cec7

Please sign in to comment.