def setCanonFlags(config): setPrePostFlags(config) # configure forward/reverse mode if config.mode: if config.mode[0] == 'f': UnitCanonicalizer.setHoistConstantsFlag(False) UnitCanonicalizer.setHoistStringsFlag(False) elif config.mode[0] == 'r': UnitCanonicalizer.setHoistConstantsFlag(True) UnitCanonicalizer.setHoistStringsFlag(False) # set symtab type defaults if config.r8: Symtab.setTypeDefaults((fs.DoubleStmt, []), (fs.IntegerStmt, [])) if config.removeFunction: UnitCanonicalizer.setKeepFunctionDef(False) if config.hoistConstantsFlag: UnitCanonicalizer.setHoistConstantsFlag(config.hoistConstantsFlag) if config.hoistStringsFlag: UnitCanonicalizer.setHoistStringsFlag(config.hoistStringsFlag) if config.subroutinizeIntegerFunctions: UnitCanonicalizer.setSubroutinizeIntegerFunctions(True) if config.keepGoing: CanonError.keepGoing() if config.warn: DebugManager.warnOnlyOn(config.warn) if config.progress: DebugManager.dumpProgress() if config.includePaths: Ffile.setIncludeSearchPath(config.includePaths) if config.nonStandard: useNonStandard(config.nonStandard) if (config.overloading): UnitCanonicalizer.setOverloadingMode()
def test1(self): 'parse file f3.f' f1 = Ffile.file(fname_t('f3.f'), c_action=parse_cmnt, s_action=parse_stmts) ok_s = ( fs.Comments, fs.SubroutineStmt, fs.Comments, fs.AssignStmt, fs.Comments, fs.AssignStmt, fs.AssignStmt, fs.AssignStmt, fs.IfStmt, fs.AssignStmt, fs.AssignStmt, fs.WriteStmt, fs.OpenStmt, fs.FormatStmt, fs.Comments, fs.DoStmt, fs.AssignStmt, fs.EnddoStmt, fs.Comments, fs.EndStmt, ) for (l, c) in izip(f1.lines, ok_s): a_(isinstance(l, c))
def setSourceProcessingFlags(config): # set outputFormat explicitly if format or output file are supplied by user. # otherwise, outputFormat is set to inputFormat during parsing if config.outputFormat == None: if config.outputFile: ext = os.path.splitext(config.outputFile)[1] config.outputFormat = Ffile.get_format(ext) setOutputFormat(config.outputFormat) elif (config.outputFormat <> 'fixed') and (config.outputFormat <> 'free'): opt.error( "outputFormat option must be specified with either 'fixed' or 'free' as an argument" ) else: setOutputFormat(config.outputFormat) if config.outputLineLength: setOutputLineLength(config.outputLineLength) if config.inputLineLength: setInputLineLength(config.inputLineLength) # set symtab type defaults Symtab.setTypeDefaults((fs.RealStmt, []), (fs.IntegerStmt, [])) # parse info units if config.infoUnitFile: from PyFort.fortUnit import fortUnitIterator for aUnit in fortUnitIterator(config.infoUnitFile, config.inputFormat): # need to parse this so the type information is available, but do not modify or print units pass # set verbosity DebugManager.setVerbose(config.isVerbose) DebugManager.setQuiet(config.noWarnings) DebugManager.setCheck(config.check)
def fortUnitIterator(fileName, inputFormat=None): ''' returns an iterator over units found in the given file named fileName; the units are assembled based on install_pat and buf_iter provides the source iterator from which the units are assembled ''' return vgen(install_pat(_curr(module_handler.ourModuleHandler)), buf_iter(Ffile.file(fileName,parse_cmnt,parse_stmts,\ inputFormat).lines))
def test4(self): 'decl with ! comment' ae = self.assertEquals a_ = self.assert_ l1 = 'real :: x(:) ! here comes the comment' f1 = Ffile.here(l1, True) ll = list(f1.lines) ae(repr(pps(ll[0].line)), repr(RealStmt([], [], [_NoInit(App('x', [':']))])))
def test3(self): 'uncontinued free format string with ! comments -- KNOWN TO FAIL (internal comments are not currently being preserved, see https://trac.mcs.anl.gov/projects/openAD/ticket/187)' ae = self.assertEquals a_ = self.assert_ l1 = preclip(''' function foo(x,y) ! test it ''') f1 = Ffile.here(l1, True) ll = list(f1.lines) ae(len(ll), 1) a_(isinstance(ll[0], fline)) ae(ll[0].line, 'function foo(x,y) ') ae(ll[0].rawline, 'function foo(x,y) ')
def test5(self): 'continued statement with goto' ae = self.assertEquals a_ = self.assert_ l1 = preclip(''' 1900 IF (ABSSB.GE.APMWHD(20,IPMP).OR.ABS(WB).GE.APMWHD(19,IPMP)) GO TO&!PFLW.169 2000 !PFLW.170 ''') flow.setInputFormat('free') flow.setOutputFormat('free') f1 = Ffile.here(l1, True) ll = list(f1.lines) ae(len(ll), 1) a_(isinstance(ll[0], fline)) compLine = 'IF (ABSSB.GE.APMWHD(20,IPMP).OR.ABS(WB).GE.APMWHD(19,IPMP)) GO TO 2000 ' ae(ll[0].line, compLine)
def test1(self): 'simple free format string' ae = self.assertEquals a_ = self.assert_ l1 = preclip(''' logical subroutine(x,y, & & z) ''') flow.setInputFormat('free') flow.setOutputFormat('free') f1 = Ffile.here(l1, True) ll = list(f1.lines) ae(len(ll), 1) a_(isinstance(ll[0], fline)) ae(ll[0].line, 'logical subroutine(x,y, z)') ae(ll[0].rawline, 'logical subroutine(x,y, z)')
def main(): global ourOutFileNameList global ourOutFileHandle usage = '%prog [options] <input_file> [additional input files]' opt = OptionParser(usage=usage) addPostProcessorOptions(opt) try: config, args = opt.parse_args() startTime = None if (config.timing): startTime = datetime.datetime.utcnow() PostProcessorOptErrors(opt, config, args) setPostProcessFlags(config, args) ActiveTypeHelper.setPlaceholderActiveType( UnitPostProcessor._replacement_type) ActiveTypeHelper.setPlaceholderActiveType( UnitPostProcessor._abstract_type) # set options for splitting compile units if config.width: splitUnits = True unitNameWidth = config.width else: splitUnits = False DebugManager.debug("running for <input_file>:" + args[0] + " and the following options: " + str(config)) inputFile = args[0] initTriples = caselessDict() initModuleNames = set() if splitUnits: (base, ext) = os.path.splitext(inputFile) unitNumExt = "%0" + str(unitNameWidth) + "d" unit_num = 0 unitStartTime = None if (config.timing): unitStartTime = datetime.datetime.utcnow() for aUnit in fortUnitIterator(inputFile, config.inputFormat): if (config.explicitInit or config.activeVariablesFile): UnitPostProcessor(aUnit).getInitDecls( initTriples, initModuleNames) outputFile = base + unitNumExt % unit_num + ext unit_num += 1 ourOutFileHandle = open(outputFile, 'w') ourOutFileNameList.append(outputFile) UnitPostProcessor(aUnit).processUnit().printit( ourOutFileHandle) ourOutFileHandle.close() globalTypeTable.cleanUpUnitTypeEntries() globalTypeTable.verifyTypeEntries() if (config.progress): msg = 'SourceProcessing: PROGRESS: done with unit ' + aUnit.uinfo.name if (config.timing): nTime = datetime.datetime.utcnow() msg += ' took: ' + str(nTime - unitStartTime) unitStartTime = nTime print msg # add new init procedures & global init procedure at end if (config.explicitInit): addInitProcedures(initTriples,initModuleNames,base=base,unitNumExt=unitNumExt,\ unit_num=unit_num,ext=ext,splitUnits=splitUnits) makeOut = open('postProcess.make', 'w') makeOut.write("POSTPROCESSEDFILES=") for outFileName in ourOutFileNameList: makeOut.write(" \\\n" + outFileName) makeOut.write("\n") makeOut.close() # SEPARATE OUTPUT INTO FILES AS SPECIFIED BY PRAGMAS elif config.separateOutput: ourOutFileHandle = None setFormat = False if config.outputFormat == None: setFormat = True unitStartTime = None if (config.timing): unitStartTime = datetime.datetime.utcnow() for aUnit in fortUnitIterator(inputFile, config.inputFormat): if (config.progress): print 'SourceProcessing: PROGRESS: start with unit ' + aUnit.uinfo.name if (config.explicitInit or config.activeVariablesFile): UnitPostProcessor(aUnit).getInitDecls( initTriples, initModuleNames) # We expect to find file pragmas in the cmnt section of units exclusively if aUnit.cmnt: if (re.search('openad xxx file_start', aUnit.cmnt.rawline, re.IGNORECASE)): # close the previous output file (if any) if ourOutFileHandle: ourOutFileHandle.close() # extract the new output file location (and add path and filename suffixes) (head, tail) = os.path.split( aUnit.cmnt.rawline.split('start [')[1].split(']') [0]) (fileName, fileExtension) = os.path.splitext(tail) outputDirectory = config.pathPrefix + head + config.pathSuffix if outputDirectory == '': outputDirectory = './' if not os.path.exists(outputDirectory): os.makedirs(outputDirectory) if (not setFormat): fileExtension = Ffile.get_extension( config.outputFormat) else: config.outputFormat = Ffile.get_format( fileExtension) setOutputFormat(config.outputFormat) newOutputFile = os.path.join( outputDirectory, fileName + config.filenameSuffix + fileExtension) ourOutFileNameList.append(newOutputFile) ourOutFileHandle = open(newOutputFile, 'w') elif not ourOutFileHandle: raise PostProcessError( 'option separateOutput specified, no output file can be determined for the first unit', 0) # postprocess the unit and print it UnitPostProcessor(aUnit).processUnit().printit( ourOutFileHandle) globalTypeTable.cleanUpUnitTypeEntries() globalTypeTable.verifyTypeEntries() if (config.progress): msg = 'SourceProcessing: PROGRESS: done with unit ' + aUnit.uinfo.name if (config.timing): nTime = datetime.datetime.utcnow() msg += ' took: ' + str(nTime - unitStartTime) unitStartTime = nTime print msg # add new init procedures & global init procedure after module declarations if (config.explicitInit): addInitProcedures(initTriples, initModuleNames, ourOutFileHandle) ourOutFileHandle.close() else: ourOutFileHandle = None if config.outputFile: ourOutFileHandle = open(config.outputFile, 'w') ourOutFileNameList.append(config.outputFile) else: ourOutFileHandle = sys.stdout for aUnit in fortUnitIterator(inputFile, config.inputFormat): if (config.explicitInit or config.activeVariablesFile): UnitPostProcessor(aUnit).getInitDecls( initTriples, initModuleNames) UnitPostProcessor(aUnit).processUnit().printit( ourOutFileHandle) globalTypeTable.cleanUpUnitTypeEntries() globalTypeTable.verifyTypeEntries() # add new init procedures & global init procedure after module declarations if (config.explicitInit): addInitProcedures(initTriples, initModuleNames, ourOutFileHandle) if config.outputFile: ourOutFileHandle.close() UnitPostProcessor.activeDecls(initTriples, initModuleNames) if (config.timing): print 'SourceProcessing: timing: ' + str( datetime.datetime.utcnow() - startTime) except (PostProcessError, UserError, AssemblerException, ListAssemblerException, ParseError, LogicError, ScanError), e: sys.stderr.write(str(e) + '\n') cleanup(config) return 1
def fortPreUnitIterator(fileName,free): if free: inputFormat = 'free' else: inputFormat = 'fixed' return vgen(unit,buf_iter(Ffile.file(fileName,parse_cmnt,parse_stmts,inputFormat).lines))