def get_reader(input, isfree=None, isstrict=None, include_dirs = None, source_only = None, ignore_comments = True): """ Returns Fortran reader instance. Parameters ---------- input : str Specify a string or filename containing Fortran code. isfree, isstrict : {None, bool} Specify input Fortran format. The values are determined from the input. If that fails then isfree=True and isstrict=False is assumed. include_dirs : {None, list} Specify a list of include directories. The default list (when include_dirs=None) contains the current working directory and the directory of ``filename``. source_only : {None, list} Specify a list of Fortran file names that are searched when the ``USE`` statement is encountered. Returns ------- reader : `FortranReader` Notes ----- If ``input`` is a C filename then the functions searches for comment lines starting with ``/*f2py`` and reads following lines as PYF file content until a line ``*/`` is found. See also -------- parse """ import os import re from readfortran import FortranFileReader, FortranStringReader if os.path.isfile(input): name,ext = os.path.splitext(input) if ext.lower() in ['.c']: # get signatures from C file comments starting with `/*f2py` and ending with `*/`. # TODO: improve parser to take line number offset making line numbers in # parser messages correct. f2py_c_comments = re.compile('/[*]\s*f2py\s.*[*]/',re.I | re.M) f = open(filename,'r') c_input = '' for s1 in f2py_c_comments.findall(f.read()): c_input += s1[2:-2].lstrip()[4:] + '\n' f.close() if isfree is None: isfree = True if isstrict is None: isstrict = True return parse(c_input, isfree, isstrict, include_dirs) reader = FortranFileReader(input, include_dirs = include_dirs, source_only = source_only) elif isinstance(input, str): reader = FortranStringReader(input, include_dirs = include_dirs, source_only = source_only) else: raise TypeError,'Expected string or filename input but got %s' % (type(input)) if isfree is None: isfree = reader.isfree if isstrict is None: isstrict = reader.isstrict reader.set_mode(isfree, isstrict) return reader
def get_reader(input, isfree=None, isstrict=None, include_dirs=None): import os import re from readfortran import FortranFileReader, FortranStringReader from parsefortran import FortranParser if os.path.isfile(input): name, ext = os.path.splitext(input) if ext.lower() in ['.c']: # get signatures from C file comments starting with `/*f2py` and ending with `*/`. # TODO: improve parser to take line number offset making line numbers in # parser messages correct. f2py_c_comments = re.compile('/[*]\s*f2py\s.*[*]/', re.I | re.M) f = open(filename, 'r') c_input = '' for s1 in f2py_c_comments.findall(f.read()): c_input += s1[2:-2].lstrip()[4:] + '\n' f.close() if isfree is None: isfree = True if isstrict is None: isstrict = True return parse(c_input, isfree, isstrict, include_dirs) reader = FortranFileReader(input, include_dirs=include_dirs) if isfree is None: isfree = reader.isfree if isstrict is None: isstrict = reader.isstrict reader.set_mode(isfree, isstrict) elif isinstance(input, str): if isfree is None: isfree = True if isstrict is None: isstrict = False reader = FortranStringReader(input, isfree, isstrict, include_dirs=include_dirs) else: raise TypeError, 'Expected string or filename input but got %s' % ( type(input)) return reader
def get_reader(input, isfree=None, isstrict=None, include_dirs = None): import os import re from readfortran import FortranFileReader, FortranStringReader from parsefortran import FortranParser if os.path.isfile(input): name,ext = os.path.splitext(input) if ext.lower() in ['.c']: # get signatures from C file comments starting with `/*f2py` and ending with `*/`. # TODO: improve parser to take line number offset making line numbers in # parser messages correct. f2py_c_comments = re.compile('/[*]\s*f2py\s.*[*]/',re.I | re.M) f = open(filename,'r') c_input = '' for s1 in f2py_c_comments.findall(f.read()): c_input += s1[2:-2].lstrip()[4:] + '\n' f.close() if isfree is None: isfree = True if isstrict is None: isstrict = True return parse(c_input, isfree, isstrict, include_dirs) reader = FortranFileReader(input, include_dirs = include_dirs) if isfree is None: isfree = reader.isfree if isstrict is None: isstrict = reader.isstrict reader.set_mode(isfree, isstrict) elif isinstance(input, str): if isfree is None: isfree = True if isstrict is None: isstrict = False reader = FortranStringReader(input, isfree, isstrict, include_dirs = include_dirs) else: raise TypeError,'Expected string or filename input but got %s' % (type(input)) return reader
def parse_all_f(): for filename in open('opt_all_f.txt'): filename = filename.strip() reader = FortranFileReader(filename) print yellow_text('Processing '+filename+' (mode=%r)' % (reader.mode)) parser = FortranParser(reader) block = parser.parse() print block
def simple_main(): import sys if not sys.argv[1:]: return parse_all_f() for filename in sys.argv[1:]: reader = FortranFileReader(filename) print yellow_text('Processing '+filename+' (mode=%r)' % (reader.mode)) parser = FortranParser(reader) parser.parse() parser.analyze() print parser.block.torepr(4)
def preprocess(): from kgen_state import SrcFile if Config.check_mode: check_mode() sys.exit(0) else: if Config.mpi['enabled']: # get path of mpif.h mpifpath = '' if os.path.isabs(Config.mpi['header']): if os.path.exists(Config.mpi['header']): mpifpath = Config.mpi['header'] else: raise UserException('Can not find %s' % Config.mpi['header']) else: for p in Config.include['path']: # KGEN addition fp = os.path.join(p, Config.mpi['header']) if os.path.exists(fp): mpifpath = fp break # collect required information if mpifpath: try: reader = FortranFileReader( mpifpath, include_dirs=Config.include['path']) spec = Specification_Part(reader) bag = {} if Config.mpi['comm'] is None: bag['key'] = 'MPI_COMM_WORLD' bag[bag['key']] = [] traverse(spec, get_MPI_PARAM, bag, subnode='content') if bag.has_key(bag['key']): Config.mpi['comm'] = bag[bag['key']][-1] else: raise UserException( 'Can not find MPI_COMM_WORLD in mpif.h') if Config.mpi['logical'] is None: bag['key'] = 'MPI_LOGICAL' bag[bag['key']] = [] traverse(spec, get_MPI_PARAM, bag, subnode='content') if bag.has_key(bag['key']): Config.mpi['logical'] = bag[bag['key']][-1] else: raise UserException( 'Can not find MPI_LOGICAL in mpif.h') if Config.mpi['status_size'] is None: bag['key'] = 'MPI_STATUS_SIZE' bag[bag['key']] = [] traverse(spec, get_MPI_PARAM, bag, subnode='content') if bag.has_key(bag['key']): Config.mpi['status_size'] = bag[bag['key']][-1] else: raise UserException( 'Can not find MPI_STATUS_SIZE in mpif.h') if Config.mpi['any_source'] is None: bag['key'] = 'MPI_ANY_SOURCE' bag[bag['key']] = [] traverse(spec, get_MPI_PARAM, bag, subnode='content') if bag.has_key(bag['key']): Config.mpi['any_source'] = bag[bag['key']][-1] else: raise UserException( 'Can not find MPI_ANY_SOURCE in mpif.h') if Config.mpi['source'] is None: bag['key'] = 'MPI_SOURCE' bag[bag['key']] = [] traverse(spec, get_MPI_PARAM, bag, subnode='content') if bag.has_key(bag['key']): Config.mpi['source'] = bag[bag['key']][-1] else: raise UserException( 'Can not find MPI_SOURCE in mpif.h') except Exception as e: raise UserException('Error occurred during reading %s.' % mpifpath) else: raise UserException( 'Can not find mpif.h. Please provide a path to the file') # parse imported source files through include.ini for path, import_type in Config.include['import'].iteritems(): if 'source' == import_type: State.imported['source'].append(SrcFile(path))
def preprocess(): from kgen_state import SrcFile if Config.check_mode: check_mode() sys.exit(0) else: if Config.mpi['enabled']: # get path of mpif.h mpifpath = '' if os.path.isabs(Config.mpi['header']): if os.path.exists(Config.mpi['header']): mpifpath = Config.mpi['header'] else: raise UserException('Can not find %s'%Config.mpi['header']) else: for p in Config.include['path']: fp = os.path.join(p, Config.mpi['header']) if os.path.exists(fp): mpifpath = fp break if not mpifpath: for incpath, incdict in Config.include['file'].items(): for p in incdict['path']: fp = os.path.join(p, Config.mpi['header']) if os.path.exists(fp): mpifpath = fp break if mpifpath: break # collect required information # TODO: FortranFileReader should be replaced with FortranStringReader after preprocessing # TODO: include keyword should be handdled properly too. if mpifpath: try: reader = FortranFileReader(mpifpath, include_dirs = Config.include['path']) spec = Specification_Part(reader) bag = {} config_name_mapping = [ ('comm', 'MPI_COMM_WORLD'), ('logical', 'MPI_LOGICAL'), ('status_size', 'MPI_STATUS_SIZE'), ('any_source', 'MPI_ANY_SOURCE'), ('source', 'MPI_SOURCE'), ] for config_key, name in config_name_mapping: if not Config.mpi.has_key(config_key) or Config.mpi[config_key] is None: bag['key'] = name bag[name] = [] traverse(spec, get_MPI_PARAM, bag, subnode='content') if len(bag[name]) > 0: Config.mpi[config_key] = bag[name][-1] else: raise UserException('Can not find {name} in mpif.h'.format(name=name)) except UserException: raise # Reraise this exception rather than catching it below except Exception as e: raise UserException('Error occurred during reading %s.'%mpifpath) else: raise UserException('Can not find mpif.h. Please provide a path to the file') # parse imported source files through include.ini for path, import_type in Config.include['import'].iteritems(): if 'source'==import_type: State.imported['source'].append(SrcFile(path))