How to use the fparser.readfortran.FortranStringReader 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 / f2pygen.py View on Github external
def __init__(self, parent):

        if not isinstance(parent, ModuleGen) and not isinstance(parent,
                                                                SubroutineGen):
            raise Exception(
                "The parent of ImplicitNoneGen must be a module or a "
                "subroutine, but found {0}".format(type(parent)))
        reader = FortranStringReader("IMPLICIT NONE\n")
        reader.set_mode(True, True)  # free form, strict
        subline = reader.next()

        from fparser.typedecl_statements import Implicit
        my_imp_none = Implicit(parent.root, subline)

        BaseGen.__init__(self, parent, my_imp_none)
github pearu / f2py / fparser / api.py View on Github external
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
github stfc / PSyclone / src / f2pygen.py View on Github external
def adduse(name, parent, only=False, funcnames=None):
    ''' Adds a use statement with the specified name to the supplied object.
    This routine is required when modifying an existing AST (e.g. when
    modifying a kernel). The classes are used when creating an AST from
    scratch (for the PSy layer). '''
    reader = FortranStringReader("use kern,only : func1_kern=>func1")
    reader.set_mode(True, True)  # free form, strict
    myline = reader.next()

    from fparser.block_statements import Use
    # find an appropriate place to add in our use statement
    import fparser
    while not (isinstance(parent, fparser.block_statements.Program) or
               isinstance(parent, fparser.block_statements.Module) or
               isinstance(parent, fparser.block_statements.Subroutine)):
        parent = parent.parent
    use = Use(parent, myline)
    use.name = name
    use.isonly = only
    if funcnames is None:
        funcnames = []
        use.isonly = False
github stfc / PSyclone / src / f2pygen.py View on Github external
def __init__(self, parent, datatype="", entity_decls=None, intent="",
                 pointer=False, kind="", dimension="", allocatable=False):
        if entity_decls is None:
            raise RuntimeError(
                "Cannot create a variable declaration without specifying the "
                "name(s) of the variable(s)")

        if datatype.lower() == "integer":
            from fparser.typedecl_statements import Integer
            reader = FortranStringReader("integer :: vanilla")
            reader.set_mode(True, False)  # free form, strict
            myline = reader.next()
            self._decl = Integer(parent.root, myline)
        elif datatype.lower() == "real":
            from fparser.typedecl_statements import Real
            reader = FortranStringReader("real :: vanilla")
            reader.set_mode(True, False)  # free form, strict
            myline = reader.next()
            self._decl = Real(parent.root, myline)
        else:
            raise RuntimeError(
                "f2pygen:DeclGen:init: Only integer and real are currently"
                " supported and you specified '{0}'".format(datatype))
        # make a copy of entity_decls as we may modify it
        local_entity_decls = entity_decls[:]
        self._decl.entity_decls = local_entity_decls
        my_attrspec = []
        if intent != "":
            my_attrspec.append("intent({0})".format(intent))
        if pointer is not False:
            my_attrspec.append("pointer")
        if allocatable is not False:
github pearu / f2py / fparser / parsefortran.py View on Github external
def test_pyf():
    string = """
python module foo
  interface tere
    subroutine bar
    real r
    end subroutine bar
  end interface tere
end python module foo
"""
    reader = FortranStringReader(string, True, True)
    parser = FortranParser(reader)
    block = parser.parse()
    print(block)
github stfc / PSyclone / src / psyclone / f2pygen2.py View on Github external
def __init__(self, parent, lhs="", rhs="", pointer=False):
        '''
        Construct an object for generating an assignment
        :param parent: assignment in the fparser2 AST
        :type parent: :py:class:`fparser.Fortran2003.Assignment_Stmt`
        '''
        if pointer:
            reader = FortranStringReader("lhs=>rhs")
        else:
            reader = FortranStringReader("lhs=rhs")
        reader.set_mode(True, True)  # free form, strict
        myline = reader.next()
        self._assign = parent
        # TODO extract lhs and rhs from fparser2 ast if needed
        self._assign.expr = rhs
        self._assign.variable = lhs
        BaseGen.__init__(self, parent, self._assign)
github stfc / PSyclone / src / f2pygen.py View on Github external
def __init__(self, parent, name="", args=None):

        reader = FortranStringReader("call vanilla(vanilla_arg)")
        reader.set_mode(True, True)  # free form, strict
        myline = reader.next()

        from fparser.block_statements import Call
        self._call = Call(parent.root, myline)
        self._call.designator = name
        if args is None:
            args = []
        self._call.items = args

        BaseGen.__init__(self, parent, self._call)
github stfc / PSyclone / src / f2pygen.py View on Github external
def __init__(self, parent, expr="UNSET", typeselect=False):
        ''' construct a ... '''
        from fparser.block_statements import EndSelect
        self._typeselect = typeselect
        reader = FortranStringReader(
            "SELECT CASE (x)\nCASE (1)\nCASE DEFAULT\nEND SELECT")
        reader.set_mode(True, True)  # free form, strict
        select_line = reader.next()
        self._case_line = reader.next()
        self._case_default_line = reader.next()
        end_select_line = reader.next()
        if self._typeselect:
            select = SelectType(parent.root, select_line)
        else:
            select = SelectCase(parent.root, select_line)
        endselect = EndSelect(select, end_select_line)
        select.expr = expr
        select.content.append(endselect)
        BaseGen.__init__(self, parent, select)
github stfc / PSyclone / src / f2pygen.py View on Github external
def __init__(self, parent, name="", only=False, funcnames=None):
        reader = FortranStringReader("use kern,only : func1_kern=>func1")
        reader.set_mode(True, True)  # free form, strict
        myline = reader.next()
        root = parent.root
        from fparser.block_statements import Use
        use = Use(root, myline)
        use.name = name
        use.isonly = only
        if funcnames is None:
            funcnames = []
            use.isonly = False
        local_funcnames = funcnames[:]
        use.items = local_funcnames
        BaseGen.__init__(self, parent, use)
github stfc / PSyclone / src / psyclone / f2pygen2.py View on Github external
def __init__(self, parent, name, implicitnone=False):
        '''
        :param str name: Name of the program unit
        :param bool implicitnone: Whether or not to include implicit none
        '''
        ProgUnitGen.__init__(self, None, parent)
        reader = FortranStringReader(
            "program {0}\nend program".format(name))
        reader.set_mode(True, True)  # free form, strict
        from fparser import Fortran2003
        self._node = Fortran2003.Program(reader)