Closed GoogleCodeExporter closed 9 years ago
Note that not all Statement instances have content attribute,
and if they have, then the content attribute does not need to be a list
(in the Comment case the content attribute is a string).
The proper way to check if a statement has "substatements" is to
check if the statement is BeginStatement instance.
I have implemented a walk function that recursivly yields statements
and their depths from the block till given depth. See
fparser.api.walk.__doc__
for more information.
Original comment by pearu.peterson
on 25 Mar 2010 at 10:21
Thank you for your suggestions. I understand what you said.
To make things easier, I think your fparser should include an API
which makes array of statement classes.
API should be together with some simple examples.
I now have def stackclass(block) below.
I hope your package will include something like this.
Then people do "f_arrayc.py *.F", and understand how it works.
takao
--------f_arrayc.py -----------
#!/usr/bin/env python
import sys,os
thisdir= os.path.dirname(os.path.abspath(__file__))
sys.path.append(thisdir+'/f2py/fparser')
from base_classes import EndStatement,classes
def stackclass(block):
arrayc=[]
toreprx(block,arrayc)
return arrayc
def toreprx(block,arrayc, depth=-1, incrtab=''):
if isinstance(block, classes.BeginStatement):
for c in block.content:
#print 'xxxxxx ', depth, c.__class__.__name__
depthx=depth
if isinstance(c, classes.EndStatement):
depthx=depth+1
arrayc.append([depthx,c])
toreprx(c,arrayc,depth-1,incrtab)
return
########## main #########
from api import parse
from readfortran import *
from parsefortran import FortranParser
#nargv = len(sys.argv) -1
argset= sys.argv[1:]
print argset
from inspect import *
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()
parser.analyze()
arrayc = stackclass(parser.block)
for c in arrayc:
if(isinstance(c[1],classes.Comment)):
print c[0],c[1].__class__.__name__,c[1].item.comment, c[1].item.span
else:
print c[0],c[1].__class__.__name__,c[1].item.line, c[1].item.span
#print dir(c[1].item)
print '---- @@@@@ '+ffile+' @@@@@ end -----'
print
print
--------end of f_arrayc.py -----------
Original comment by TakaoKot...@gmail.com
on 26 Mar 2010 at 9:46
fparser.api.walk is doing already this, try:
..
for c in fparser.api.walk(parser.block):
..
That is, list(fparser.api.walk(parser.block)) should be equal
to stackclass(parser.block).
Using walk function is more memory efficient.
Original comment by pearu.peterson
on 26 Mar 2010 at 10:15
I see. I should have examined your code first.
Thank you.
Original comment by TakaoKot...@gmail.com
on 26 Mar 2010 at 12:05
Original comment by pearu.peterson
on 26 Mar 2010 at 7:58
Original issue reported on code.google.com by
TakaoKot...@gmail.com
on 24 Mar 2010 at 11:56Attachments: