How to use the fparser.two.Fortran2003.Name 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 / nemo.py View on Github external
def __init__(self, ast):
        # pylint: disable=super-init-not-called
        names = walk(ast.content, Fortran2003.Name)
        # The name of the program unit will be the first in the list
        if not names:
            raise InternalError("Found no names in supplied Fortran - should "
                                "be impossible!")
        self._name = str(names[0]) + "_psy"

        self._invokes = NemoInvokes(ast)
        self._ast = ast
github stfc / PSyclone / src / psyclone / psyir / nodes / psy_data_node.py View on Github external
# Rather than repeatedly walk the tree, we do it once for all of
        # the node types we will be interested in...
        node_list = walk([ptree], (Fortran2003.Main_Program,
                                   Fortran2003.Subroutine_Stmt,
                                   Fortran2003.Function_Stmt,
                                   Fortran2003.Specification_Part,
                                   Fortran2003.Use_Stmt,
                                   Fortran2003.Name))
        if self._module_name:
            routine_name = self._module_name
        else:
            for node in node_list:
                if isinstance(node, (Fortran2003.Main_Program,
                                     Fortran2003.Subroutine_Stmt,
                                     Fortran2003.Function_Stmt)):
                    names = walk([node], Fortran2003.Name)
                    routine_name = str(names[0]).lower()
                    break

        for node in node_list:
            if isinstance(node, Fortran2003.Specification_Part):
                spec_part = node
                break
        else:
            # This limitation will be removed when we use the Fortran
            # backend of the PSyIR (#435)
            raise NotImplementedError(
                "Addition of PSyData regions to routines without any "
                "existing declarations is not supported and '{0}' has no "
                "Specification-Part".format(routine_name))

        # TODO #703: Rename the PSyDataType instead of
github arporter / habakkuk / src / habakkuk / parse2003.py View on Github external
def load(self, parsed_loop):
        ''' Takes the supplied loop object produced by the f2003 parser
        and extracts relevant information from it to populate this object '''
        from fparser.two.Fortran2003 import Nonlabel_Do_Stmt, Name
        for node in parsed_loop.content:
            if isinstance(node, Nonlabel_Do_Stmt):
                var_name = walk_ast(node.items, [Name])
                if var_name:
                    self._var_name = str(var_name[0])
                else:
                    # Loop is just "DO" with no loop control
                    self._var_name = None
github stfc / PSyclone / src / psyclone / parse_orig.py View on Github external
: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
'''
        # 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, -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]).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 / psyir / frontend / fparser2.py View on Github external
# Actual_Arg_Spec_List with the first entry being the argument.
            kind_arg = intrinsics[0].items[1].items[0]

            # We currently only support integer and real literals as
            # arguments to KIND
            if isinstance(kind_arg, (Fortran2003.Int_Literal_Constant,
                                     Fortran2003.Real_Literal_Constant)):
                return get_literal_precision(kind_arg, psyir_parent)

            raise NotImplementedError(
                "Only real and integer literals are supported "
                "as arguments to the KIND intrinsic but found '{0}' in: "
                "{1}".format(type(kind_arg).__name__, str(kind_selector)))

        # We have kind=kind-param
        kind_names = walk(kind_selector.items, Fortran2003.Name)
        if not kind_names:
            raise NotImplementedError(
                "Failed to find valid Name in Fortran Kind "
                "Selector: '{0}'".format(str(kind_selector)))
        return Fparser2Reader._kind_symbol_from_name(str(kind_names[0]),
                                                     symbol_table)
github stfc / PSyclone / src / psyclone / parse / kernel.py View on Github external
"get_integer_array: array extent must be specified using "
                    "an integer literal but found '{0}' for array '{1}'".
                    format(str(dim_stmt.children[0]), name))
            # Get the declared size of the array
            array_extent = int(str(dim_stmt.children[0]))

            if not isinstance(assign.children[2],
                              Fortran2003.Array_Constructor):
                raise ParseError(
                    "get_integer_array: RHS of assignment is not "
                    "an array constructor: '{0}'".format(str(assign)))
            # fparser2 AST for Array_Constructor is:
            # Array_Constructor('[', Ac_Value_List(',', (Name('w0'),
            #                                      Name('w1'))), ']')
            # Construct a list of the names in the array constructor
            names = walk(assign.children[2].children, Fortran2003.Name)
            if not names:
                raise InternalError("Failed to parse array constructor: "
                                    "'{0}'".format(str(assign.items[2])))
            if len(names) != array_extent:
                # Ideally fparser would catch this but it isn't yet mature
                # enough.
                raise ParseError(
                    "get_integer_array: declared length of array '{0}' is {1} "
                    "but constructor only contains {2} names: '{3}'".format(
                        name, array_extent, len(names), str(assign)))
            return [str(name).lower() for name in names]
        # No matching declaration for the provided name was found
        return []
github arporter / habakkuk / src / habakkuk / make_dag.py View on Github external
# main program (might just be a subroutine in a module)
            try:
                main_prog = get_child(program, Main_Program)
                routines.append(main_prog)
            except ParseError:
                pass

            # Create a DAG for each (sub)routine
            for subroutine in routines:
                # Get the name of this (sub)routine
                substmt = walk_ast(subroutine.content,
                                   [Subroutine_Stmt, Function_Stmt,
                                    Program_Stmt])
                if isinstance(substmt[0], Function_Stmt):
                    for item in substmt[0].items:
                        if isinstance(item, Name):
                            sub_name = str(item)
                else:
                    sub_name = str(substmt[0].get_name())

                # Find the section of the tree containing the execution part
                # of the code
                try:
                    exe_part = get_child(subroutine, Execution_Part)
                except ParseError:
                    # This subroutine has no execution part so we skip it
                    # TODO log this event
                    continue

                # Make a list of all Do loops in the routine
                loops = walk_ast(exe_part.content,
                                 [Block_Nonlabel_Do_Construct])
github stfc / PSyclone / src / psyclone / parse_orig.py View on Github external
:raises InternalError: if we fail to parse the LHS of the array \
                               declaration or the array constructor.
        :raises ParseError: if the RHS of the declaration is not an array \
                            constructor.
        '''
        # Ensure the classes are setup for the Fortran2003 parser
        _ = ParserFactory().create()

        for statement, _ in fpapi.walk(self._ktype, -1):
            if not isinstance(statement, fparser1.typedecl_statements.Integer):
                # This isn't an integer declaration so skip it
                continue
            # fparser only goes down to the statement level. We use fparser2 to
            # parse the statement itself.
            assign = Fortran2003.Assignment_Stmt(statement.entity_decls[0])
            names = walk_ast(assign.items, [Fortran2003.Name])
            if not names:
                raise InternalError("Unsupported assignment statement: '{0}'".
                                    format(str(assign)))
            if str(names[0]) == name:
                # This is the variable declaration we're looking for
                if not isinstance(assign.items[2],
                                  Fortran2003.Array_Constructor):
                    from psyclone.parse import ParseError
                    raise ParseError(
                        "get_integer_array: RHS of assignment is not "
                        "an array constructor: '{0}'".format(str(assign)))
                # fparser2 AST for Array_Constructor is:
                # Array_Constructor('[', Ac_Value_List(',', (Name('w0'),
                #                                      Name('w1'))), ']')
                # Construct a list of the names in the array constructor
                names = walk_ast(assign.items[2].items, [Fortran2003.Name])
github stfc / PSyclone / src / psyclone / profiler.py View on Github external
# Ensure child nodes are up-to-date
        super(ProfileNode, self).update()

        # Get the parse tree of the routine containing this region
        # pylint: disable=protected-access
        ptree = self.root.invoke._ast
        # pylint: enable=protected-access
        # Rather than repeatedly walk the tree, we do it once for all of
        # the node types we will be interested in...
        node_list = walk_ast([ptree], [Fortran2003.Main_Program,
                                       Fortran2003.Subroutine_Stmt,
                                       Fortran2003.Function_Stmt,
                                       Fortran2003.Specification_Part,
                                       Fortran2003.Use_Stmt,
                                       Fortran2003.Name])
        if self._module_name:
            routine_name = self._module_name
        else:
            for node in node_list:
                if isinstance(node, (Fortran2003.Main_Program,
                                     Fortran2003.Subroutine_Stmt,
                                     Fortran2003.Function_Stmt)):
                    names = walk_ast([node], [Fortran2003.Name])
                    routine_name = str(names[0]).lower()
                    break

        for node in node_list:
            if isinstance(node, Fortran2003.Specification_Part):
                spec_part = node
                break
        else: