How to use the fparser.api.walk 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 stfc / PSyclone / src / psyclone / parse / kernel.py View on Github external
matching variable is found. The search is not case sensitive.

        :param str name: the name of the integer variable to find.

        :return: value of the specified integer variable (lower case) or None.
        :rtype: str

        :raises ParseError: if the RHS of the assignment is not a Name.

        '''
        # Ensure the Fortran2003 parser is initialised
        _ = ParserFactory().create()
        # Fortran is not case sensitive so nor is our matching
        lower_name = name.lower()

        for statement, _ in fpapi.walk(self._ktype):
            if isinstance(statement, fparser1.typedecl_statements.Integer):
                # fparser only goes down to the statement level. We use
                # fparser2 to parse the statement itself (eventually we'll
                # use fparser2 to parse the whole thing).
                assign = Fortran2003.Assignment_Stmt(
                    statement.entity_decls[0])
                if str(assign.items[0]).lower() == lower_name:
                    if not isinstance(assign.items[2], Fortran2003.Name):
                        raise ParseError(
                            "get_integer_variable: RHS of assignment is not "
                            "a variable name: '{0}'".format(str(assign)))
                    return str(assign.items[2]).lower()
        return None
github stfc / PSyclone / src / psyclone / parse / kernel.py View on Github external
def __init__(self, ast, name=None):

        if name is None:
            # if no name is supplied then use the module name to
            # determine the type name. The assumed convention is that
            # the module is called _mod and the type is called
            # _type
            found = False
            for statement, _ in fpapi.walk(ast, -1):
                if isinstance(statement, fparser1.block_statements.Module):
                    module_name = statement.name
                    found = True
                    break
            if not found:
                raise ParseError(
                    "Error KernelType, the file does not contain a module. "
                    "Is it a Kernel file?")

            mn_len = len(module_name)
            if mn_len < 5:
                raise ParseError(
                    "Error, module name '{0}' is too short to have '_mod' as "
                    "an extension. This convention is assumed.".
                    format(module_name))
            base_name = module_name.lower()[:mn_len-4]
github stfc / PSyclone / src / parse.py View on Github external
raise ParseError("OrderedDict not provided natively pre python 2.7 (you are running {0}. Try installing with 'sudo pip install ordereddict'".format(python_version))
            else:
                raise ParseError("OrderedDict not found which is unexpected as it is meant to be part of the Python library from 2.7 onwards")
    invokecalls = OrderedDict()

    container_name=None
    for child in ast.content:
        if isinstance(child,fparser.block_statements.Program) or \
           isinstance(child,fparser.block_statements.Module) or \
           isinstance(child,fparser.block_statements.Subroutine):
            container_name=child.name
            break
    if container_name is None:
        raise ParseError("Error, program, module or subroutine not found in ast")

    for statement, depth in fpapi.walk(ast, -1):
        if isinstance(statement, fparser.statements.Use):
            for name in statement.items:
                name_to_module[name] = statement.name
        if isinstance(statement, fparser.statements.Call) \
           and statement.designator == invoke_name:
            statement_kcalls = []
            for arg in statement.items:
                try:
                    parsed = expr.expression.parseString(arg)[0]
                except ParseException:
                    raise ParseError("Failed to parse string: {0}".format(arg))

                argname = parsed.name
                argargs=[]
                for a in parsed.args:
                    if type(a) is str: # a literal is being passed by argument
github stfc / PSyclone / src / parse.py View on Github external
raise ParseError("Kernel type %s binds to a specific procedure but does not use 'code' as the generic name." % \
                                     name)
                else:
                    # psyclone style
                    bname=statement.name
        if bname is None:
            raise RuntimeError("Kernel type %s does not bind a specific procedure" % \
                               name)
        if bname=='':
            raise ParseError("Internal error: empty kernel name returned for Kernel type %s." % \
                               name)
        code = None
        default_public=True
        declared_private=False
        declared_public=False
        for statement, depth in fpapi.walk(modast, -1):
            if isinstance(statement, fparser.statements.Private):
                if len(statement.items)==0:
                    default_public=False
                elif bname in statement.items:
                    declared_private=True
            if isinstance(statement, fparser.statements.Public):
                if len(statement.items)==0:
                    default_public=True
                elif bname in statement.items:
                    declared_public=True
            if isinstance(statement, fparser.block_statements.Subroutine) and \
               statement.name == bname:
                if statement.is_public():
                    declared_public=True
                code = statement
        if code is None:
github stfc / PSyclone / src / psyclone / parse_orig.py View on Github external
def get_integer_variable(self, name):
        ''' Parse the kernel meta-data and find the value of the
        integer variable with the supplied name. Return None if no
        matching variable is found.
        :param str name: the name of the integer variable to find.
        :returns: value of the specified integer variable or None.
        :rtype: str
        :raises ParseError: if the RHS of the assignment is not a Name.
        '''
        # Ensure the Fortran2003 parser is initialised
        _ = ParserFactory().create()

        for statement, _ in fpapi.walk(self._ktype, -1):
            if isinstance(statement, fparser1.typedecl_statements.Integer):
                # fparser only goes down to the statement level. We use
                # fparser2 to parse the statement itself (eventually we'll
                # use fparser2 to parse the whole thing).
                assign = Fortran2003.Assignment_Stmt(
                    statement.entity_decls[0])
                if str(assign.items[0]) == name:
                    if not isinstance(assign.items[2], Fortran2003.Name):
                        from psyclone.parse import ParseError
                        raise ParseError(
                            "get_integer_variable: RHS of assignment is not "
                            "a variable name: '{0}'".format(str(assign)))
                    return str(assign.items[2])
        return None
github stfc / PSyclone / src / psyclone / parse / kernel.py View on Github external
bname, subnames = get_kernel_interface(name, modast)
            if bname is None:
                # no interface found either
                raise ParseError(
                    "Kernel type {0} does not bind a specific procedure or \
                    provide an explicit interface".format(name))
        elif bname == '':
            raise InternalError(
                "Empty Kernel name returned for Kernel type {0}.".format(name))
        # add the name of the tbp to the list of strings to search for.
        else:
            subnames = [bname]
        # walk the AST to check the subroutine names exist.
        procedure_count = 0
        for subname in subnames:
            for statement, _ in fpapi.walk(modast):
                if isinstance(statement,
                              fparser1.block_statements.Subroutine) \
                              and statement.name.lower() \
                              == subname:
                    procedure_count = procedure_count + 1
                    if procedure_count == 1:
                        # set code to statement if there is one procedure.
                        code = statement
                    else:
                        code = None  # set to None if there is more than one.
                    print(code)
                    break
            else:
                raise ParseError(
                    "kernel.py:KernelProcedure:get_procedure: Kernel "
                    "subroutine '{0}' not found.".format(subname))
github stfc / PSyclone / src / parse.py View on Github external
def __init__(self, ast, name=None):

        if name is None:
            # if no name is supplied then use the module name to
            # determine the type name. The assumed convention is that
            # the module is called _mod and the type is called
            # _type
            found = False
            for statement, depth  in fpapi.walk(ast, -1):
                if isinstance(statement, fparser.block_statements.Module):
                    module_name = statement.name
                    found = True
                    break
            if not found:
                raise ParseError("Error KernelType, the file does not contain a module. Is it a Kernel file?")

            mn_len = len(module_name)
            if mn_len<5:
                raise ParseError("Error, module name '{0}' is too short to have '_mod' as an extension. This convention is assumed.".format(module_name))
            base_name = module_name.lower()[:mn_len-4]
            extension_name = module_name.lower()[mn_len-4:mn_len]
            if extension_name != "_mod":
                raise ParseError("Error, module name '{0}' does not have '_mod' as an extension. This convention is assumed.".format(module_name))
            name = base_name + "_type"
github stfc / PSyclone / src / parse.py View on Github external
def getKernelMetadata(self,name, ast):
        ktype = None
        for statement, depth  in fpapi.walk(ast, -1):
            if isinstance(statement, fparser.block_statements.Type) \
               and statement.name == name:
                ktype = statement
        if ktype is None:
            raise RuntimeError("Kernel type %s does not exist" % name)
        return ktype