Esempio n. 1
0
def str2stmt(string, isfree=True, isstrict=False):
    """ Convert Fortran code to Statement tree.
    """
    from readfortran import Line, FortranStringReader
    from parsefortran import FortranParser
    reader = FortranStringReader(string, isfree, isstrict)
    parser = FortranParser(reader)
    parser.parse()
    parser.analyze()
    block = parser.block
    while len(block.content)==1:
        block = block.content[0]
    return block
Esempio n. 2
0
def parse(input, isfree=None, isstrict=None, include_dirs = None):
    """ Parse input and return Statement tree.

    input            --- string or filename.
    isfree, isstrict --- specify input Fortran format.
                         Defaults are True, False, respectively, or
                         determined from input.
    include_dirs     --- list of include directories.
                         Default contains current working directory
                         and the directory of file name.
    """
    from parsefortran import FortranParser
    reader = get_reader(input, isfree, isstrict, include_dirs)
    parser = FortranParser(reader)
    parser.parse()
    parser.analyze()
    return parser.block
Esempio n. 3
0
File: utils.py Progetto: afcarl/f2py
def str2stmt(string, isfree=True, isstrict=False):
    """ Convert Fortran code to Statement tree.
    """
    from readfortran import Line, FortranStringReader
    from parsefortran import FortranParser
    reader = FortranStringReader(string, isfree, isstrict)
    parser = FortranParser(reader)
    parser.parse()
    parser.analyze()
    block = parser.block
    while len(block.content) == 1:
        block = block.content[0]
    return block
Esempio n. 4
0
def parse(input, isfree=None, isstrict=None, include_dirs=None):
    """ Parse input and return Statement tree.

    input            --- string or filename.
    isfree, isstrict --- specify input Fortran format.
                         Defaults are True, False, respectively, or
                         determined from input.
    include_dirs     --- list of include directories.
                         Default contains current working directory
                         and the directory of file name.
    """
    from parsefortran import FortranParser
    reader = get_reader(input, isfree, isstrict, include_dirs)
    parser = FortranParser(reader)
    parser.parse()
    parser.analyze()
    return parser.block
Esempio n. 5
0
rr = re.compile("\'.*?\'|\".*?\"")
functions = []  # save defined functions in array.
subs = []
mods = []
srcfiles = set(argset)
ranktag = ["Program", "Subroutine", "Function", "Module", "Type", "Interface"]

moddict = {}
fsdict = {}
### find subroutine and function definitions ############################
for ffile in argset:
    #    print '---- '+ffile+' start -----'
    reader = FortranFileReader(ffile)
    reader.set_mode(isfree=False, isstrict=False)
    parser = FortranParser(reader, ignore_comments=False)
    parser.parse()
    sstack = []
    deptho = -1
    inso = None
    for c in walk(parser.block):
        ins = c[0]
        depth = c[1]
        if (isinstance(ins, classes.Comment)): continue
        if (depth > deptho): sstack.append(inso)
        while len(sstack) > depth:
            sstack.pop()
        #        print len(sstack),depth, type(ins), ins.item.span,  ins.item.strline, ins.item.label #, ins.item.name
        #        deptho=depth
        #        inso=ins
        #        continue
Esempio n. 6
0
functions=[] # save defined functions in array.
subs=[]
mods=[]
srcfiles=set(argset)
ranktag=["Program", "Subroutine", "Function", "Module","Type","Interface"]

moddict={}
fsdict={}
### find subroutine and function definitions ############################
for ffile in argset:
    #print '---- '+ffile+' start -----'
    reader = FortranFileReader(ffile)
    fform=False
    if(re.search('.F90',ffile)): fform=True
    reader.set_mode(isfree=fform,isstrict=False)
    parser=FortranParser(reader,ignore_comments=False)
    parser.parse()
    sstack=[]
    deptho=-1
    inso=None
    for c in walk(parser.block):
        ins=c[0]
        depth=c[1]
        if(isinstance(ins, classes.Comment)): continue
        if(depth>deptho):         sstack.append(inso)
        while len(sstack)>depth:  sstack.pop()
#        print len(sstack),depth, type(ins), item.span,  item.strline, item.label #, item.name
#        deptho=depth
#        inso=ins
#        continue
        frame2x= [x.__class__.__name__+':'+x.name for x in sstack[1:] if x.__class__.__name__ in ranktag]
Esempio n. 7
0
#     return


for ffile in argset:
    print '@@@@@ '+ffile+' @@@@@ start -----'
    reader = FortranFileReader(ffile)
#


### test1 #############################################################
#    for i in getmembers(reader):
#        print i

### test2 #####################
    reader.set_mode(isfree=False,isstrict=False)
    parser=FortranParser(reader,ignore_comments=False)
    parser.parse()
    parser.analyze()
#    print parser.block.torepr(3)
#    print parser.block
    for i in dir(parser.block):
        print i
    print '----------------------'
    print 'aaa ',parser.block.name
    print 'aaa ',parser.block.blocktype

    ix=0
    for i in parser.block.content:
        ix=ix+1
        try:
            print 'zzzzzzzzz', ix,'  ',i.blocktype #,i.name,i.item #,i.a
Esempio n. 8
0
def parse(input, isfree=None, isstrict=None, include_dirs = None, source_only = None,
          ignore_comments = True, analyze=True):
    """ Parse input and return Statement tree.

    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.
    ignore_comments : bool
      When True then discard all comment lines in the Fortran code.
    analyze : bool
      When True then apply run analyze method on the Fortran code tree.

    Returns
    -------
    block : `fparser.api.BeginSource`

    Examples
    --------

        >>> code = '''
        ... c comment
        ...       subroutine foo(a)
        ...         integer a
        ...         print*, "a=",a
        ...       end
        ... '''
        >>> tree = parse(code,isfree=False)
        >>> print tree
              !BEGINSOURCE <cStringIO.StringI object at 0x1798030> mode=fix90
                SUBROUTINE foo(a)
                  INTEGER a
                  PRINT *, "a=", a
                END SUBROUTINE foo
        >>> print `tree`
              BeginSource
                blocktype='beginsource'
                name='<cStringIO.StringI object at 0x1798030> mode=fix90'
                a=AttributeHolder:
              external_subprogram=<dict with keys ['foo']>
                content:
                  Subroutine
                    args=['a']
                    item=Line('subroutine foo(a)',(3, 3),'')
                    a=AttributeHolder:
                variables=<dict with keys ['a']>
                    content:
                      Integer
                        selector=('', '')
                        entity_decls=['a']
                        item=Line('integer a',(4, 4),'')
                      Print
                        item=Line('print*, "a=",a',(5, 5),'')
                  EndSubroutine
                    blocktype='subroutine'
                    name='foo'
            item=Line('end',(6, 6),'')

    See also
    --------
    get_reader
    """
    from parsefortran import FortranParser
    reader = get_reader(input, isfree, isstrict, include_dirs, source_only)
    parser = FortranParser(reader, ignore_comments = ignore_comments)
    parser.parse()
    if analyze:
        parser.analyze()
    return parser.block