How to use the fparser.api.parse function in fparser

To help you get started, we’ve selected a few fparser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github inducer / loopy / loopy / frontend / fortran / __init__.py View on Github external
warn("auto_dependencies is deprecated, use seq_dependencies instead",
                DeprecationWarning, stacklevel=2)
        seq_dependencies = auto_dependencies

    if seq_dependencies is None:
        seq_dependencies = True

    import logging
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
    console.setFormatter(formatter)
    logging.getLogger('fparser').addHandler(console)

    from fparser import api
    tree = api.parse(source, isfree=free_form, isstrict=strict,
            analyze=False, ignore_comments=False)

    if tree is None:
        raise LoopyError("Fortran parser was unhappy with source code "
                "and returned invalid data (Sorry!)")

    from loopy.frontend.fortran.translator import F2LoopyTranslator
    f2loopy = F2LoopyTranslator(filename, target=target)
    f2loopy(tree)

    return f2loopy.make_kernels(seq_dependencies=seq_dependencies)
github stfc / PSyclone / src / psyclone / f2pygen.py View on Github external
def __init__(self, name="", contains=True, implicitnone=True):
        from fparser import api

        code = '''\
module vanilla
'''
        if contains:
            code += '''\
contains
'''
        code += '''\
end module vanilla
'''
        tree = api.parse(code, ignore_comments=False)
        module = tree.content[0]
        module.name = name
        endmod = module.content[len(module.content)-1]
        endmod.name = name
        ProgUnitGen.__init__(self, None, module)
        if implicitnone:
            self.add(ImplicitNoneGen(self))
github pearu / f2py / fparser / block_statements.py View on Github external
def subroutine_wrapper(self):
        code = self.subroutine_wrapper_code()
        from .api import parse
        block = parse(code) # XXX: set include_dirs
        while len(block.content)==1:
            block = block.content[0]
        return block
github stfc / PSyclone / src / parse.py View on Github external
for filename in fnmatch.filter(filenames, 
                                                       search_string):
                                    matches.append(os.path.join(cdir, filename))

                    # Check that we only found one match
                    if len(matches) != 1:
                        if len(matches) == 0:
                            raise IOError("Kernel file '{0}.[fF]90' not found in {1}".\
                                          format(modulename, cdir))
                        else:
                            raise IOError("More than one match for kernel file "
                                          "'{0}.[fF]90' found!".
                                          format(modulename))
                    else:
                        try:
                            modast = fpapi.parse(matches[0])
                            # ast includes an extra comment line which
                            # contains file details. This line can be
                            # long which can cause line length
                            # issues. Therefore set the information
                            # (name) to be empty.
                            modast.name = ""
                        except:
                            raise ParseError("Failed to parse kernel code "
                                             "'{0}'. Is the Fortran correct?".
                                             format(matches[0]))
                        if line_length:
                            fll = FortLineLength()
                            with open (matches[0], "r") as myfile:
                                code_str=myfile.read()
                            if fll.long_lines(code_str):
                                raise ParseError(
github stfc / PSyclone / src / psyclone / parse / kernel.py View on Github external
# The meta-data for these lives in a Fortran module file
        # passed in to this method.
        fname = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            builtin_defs_file)
        if not os.path.isfile(fname):
            raise ParseError(
                "BuiltInKernelTypeFactory:create Kernel '{0}' is a recognised "
                "Built-in but cannot find file '{1}' containing the meta-data "
                "describing the Built-in operations for API '{2}'"
                .format(name, fname, self._type))
        # Attempt to parse the meta-data
        try:
            parsefortran.FortranParser.cache.clear()
            fparser.logging.disable(fparser.logging.CRITICAL)
            parse_tree = fpapi.parse(fname)
        except Exception:
            raise ParseError(
                "BuiltInKernelTypeFactory:create: Failed to parse the meta-"
                "data for PSyclone built-ins in file '{0}'.".format(fname))

        # Now we have the parse tree, call our parent class to create \
        # the object
        return KernelTypeFactory.create(self, parse_tree, name)
# pylint: enable=too-few-public-methods
github cython / cython / Tools / fwrap / Main.py View on Github external
projdir = setup_project(projectname, directory, outdir)

    print >>sys.stderr, "wrapping %s from %s in %s" % (filenames, directory, projdir)

    pipeline = [ KindResolutionVisitor(),
                 AutoConfigGenerator(projectname),
                 FortranWrapperGenerator(projectname),
                 CHeaderGenerator(projectname),
                 PxdGenerator(projectname),
                 CyHeaderGenerator(projectname),
                 CyImplGenerator(projectname)
               ]

    for fname in filenames:
        block = api.parse(os.path.join(directory, fname), analyze=True)
        for stage in pipeline:
            stage(block) # assumes no transformations to the parse tree.

    # write out the files.
    for stage in pipeline:
        if not stage.is_generator:
            continue
        out_fname = stage.make_fname(projectname)
        full_path = os.path.join(projdir, out_fname)
        fh = open(full_path, 'w')
        stage.copyto(fh)
        fh.close()

    # write out the makefile
    gen_makefile(projectname, filenames, directory, projdir)