toolchain: give parse_if more input methods

This commit is contained in:
Tiziano Müller 2018-11-02 16:25:43 +01:00 committed by Tiziano Müller
parent 87acbd2ae9
commit c87f0903aa
2 changed files with 58 additions and 32 deletions

View file

@ -1084,7 +1084,7 @@ EOF
local __TMPL=$(cat $__filename)
eval "printf \"${__TMPL}\n\"" > $__filename
# pass this to parsers to replace all of the IF_XYZ statements
python ${SCRIPTDIR}/parse_if.py $__filename $__flags
"${SCRIPTDIR}/parse_if.py" -i -f "${__filename}" $__flags
echo "Wrote ${INSTALLDIR}/arch/$__filename"
}

88
tools/toolchain/scripts/parse_if.py Normal file → Executable file
View file

@ -1,21 +1,30 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import argparse
class Parser:
"Parser for files with IF_XYZ(A|B) constructs"
def __init__(self, switches):
self.mSwitches = switches
def Switches(self):
"outputs the switches used by parser"
return self.mSwitches
def SetSwitch(self, key, val):
"Set or add a (key,val) switch"
self.mSwitches[key] = val
def ParseSingleIf(self, string, switch):
"""
switch should be a tuple (key, val)
ParseSingleIf(string, switch) will replace in string the
first occurance of IF_key(A|B) with A if val = True;
B if val = False
switch should be a tuple (key, val)
ParseSingleIf(string, switch) will replace in string the
first occurance of IF_key(A|B) with A if val = True;
B if val = False
"""
init = string.find('IF_' + switch[0])
start = init
@ -44,6 +53,7 @@ B if val = False
string[mark + 1 : end] + \
string[end + 1 : len(string)]
return result
def ParseIf(self, string, switch):
"""
ParseIf(string, switch) will replace in string recursively
@ -54,6 +64,7 @@ B if val = False
while result.find('IF_' + switch[0]) > -1:
result = self.ParseSingleIf(result, switch)
return result
def ParseString(self, string):
"""
ParseString(string) will parse in string recursively
@ -64,47 +75,62 @@ in dictionary self.mSwitches
for switch in self.mSwitches.items():
result = self.ParseIf(result, switch)
return result
def ParseDocument(self, filename):
def ParseDocument(self, input_file, output_file):
"""
ParseDocument(filename) will replace in a file recursively
all of IF_key(A|B) statements for all (key, val) pairs
in dictionary self.mSwitches
ParseDocument(input_file, output_fiel) will replace recursively
all of IF_key(A|B) statements for all (key, val) pairs
in dictionary self.mSwitches in the input_file stream and output
to the output_file stream.
"""
input_file = open(filename, "r")
output=[]
for line in input_file:
output.append(self.ParseString(line))
input_file.close()
if input_file == output_file:
output_file.seek(0)
output_file.truncate()
# write to the same file
output_file = open(filename, "w")
for line in output:
output_file.write(line)
output_file.close()
# ------------------------------------------------------------------------
# main program
# ------------------------------------------------------------------------
if __name__ == '__main__':
argparser = argparse.ArgumentParser(description="Resolve IF_*() macros based on given tags")
argparser.add_argument('-f', '--file', metavar="FILENAME", type=str, help="read from given file instead of stdin")
argparser.add_argument('-i', '--inplace', action="store_true", help="do in-place replacement for the given file instead of echo to stdout")
argparser.add_argument('flags', metavar="FLAG", type=str, nargs='*', help="specified flags are set to true")
if (len(sys.argv) < 2) or \
("--help" in sys.argv) or \
("-h" in sys.argv):
sys.stderr.write("usage: parse_if <filename> MPI OMP CUDA ...\n")
sys.exit()
args = argparser.parse_args()
# default list of switches used by the parser
switches = {"MPI" : False, \
"OMP" : False, \
"CUDA" : False, \
"WARNALL" : False, \
"DEBUG" : False, \
"VALGRIND" : False, \
"COVERAGE" : False}
# default list of switches used by the parser
switches = {
"MPI": False,
"OMP": False,
"CUDA": False,
"WARNALL": False,
"DEBUG": False,
"VALGRIND": False,
"COVERAGE": False,
}
parser=Parser(switches)
parser = Parser(switches)
# set the list of switches given on the command line to True
for ii in sys.argv[2:]:
parser.SetSwitch(ii, True)
# set the list of switches given on the command line to True
for flag in args.flags:
parser.SetSwitch(flag, True)
# do parsing
parser.ParseDocument(sys.argv[1])
# do parsing
if not args.file:
parser.ParseDocument(sys.stdin, sys.stdout)
else:
if args.inplace:
with open(args.file, mode='r+') as fhandle:
parser.ParseDocument(fhandle, fhandle)
else:
with open(args.file, mode='r') as fhandle:
parser.ParseDocument(fhandle, sys.stdout)