66 """Public API for Fortran parser. 79 __autodoc__ = [
"get_reader",
"parse",
"walk"]
91 Returns Fortran reader instance. 93 If ``source`` is a C filename then the functions searches for comment 94 lines starting with ``/*f2py`` and reads following lines as PYF file 95 content until a line ``*/`` is found. 97 :param str source: Specify a string or filename containing Fortran code. 98 :param bool isfree: True if Fortran is free format 99 :param bool isstrict: True if we are to strictly enforce free/fixed format 100 :param list include_dirs: Specify a list of include directories. The 101 default list (when include_dirs=None) contains 102 the current working directory and the directory 104 :param list source_only: Specify a list of Fortran file names that are 105 searched when the ``USE`` statement is 107 :param bool ignore_comments: Whether or not to ignore (and discard) 108 comments when parsing the source. 110 :returns: a reader instance 111 :rtype: :py:class:`fparser.common.readfortran.FortranReader` 118 if os.path.isfile(source):
119 _name, ext = os.path.splitext(source)
120 if ext.lower()
in [
".c"]:
125 f2py_c_comments = re.compile(
r"/[*]\s*f2py\s.*[*]/", re.I | re.M)
126 handle = open(source,
"r") 128 for line
in f2py_c_comments.findall(handle.read()):
129 c_input += line[2:-2].lstrip()[4:] +
"\n" 135 return parse(c_input, isfree, isstrict, include_dirs)
136 reader = FortranFileReader(
138 include_dirs=include_dirs,
139 source_only=source_only,
140 ignore_comments=ignore_comments,
142 elif isinstance(source, str):
143 reader = FortranStringReader(
145 include_dirs=include_dirs,
146 source_only=source_only,
147 ignore_comments=ignore_comments,
150 raise TypeError(
"Expected string or filename input but got %s" % (type(input)))
152 isfree = reader.format.is_free
154 isstrict = reader.format.is_strict
155 reader.set_format(FortranFormat(isfree, isstrict))
165 ignore_comments=True,
170 Parse input and return Statement tree. Raises an AnalyzeError if the 171 parser can not parse the Fortran code. 173 :param str source: Specify a string or filename containing Fortran code. 174 :param bool isfree: Whether the Fortran source is free-format. 175 :param bool isstrict: Whether we are to strictly enforce the `isfree` 177 :param list include_dirs: Specify a list of include directories. The 178 default list (when include_dirs=None) contains 179 the current working directory and the directory 181 :param list source_only: A list of Fortran file names that are searched 182 when the ``USE`` statement is encountered. 183 :param bool ignore_comments: When True then discard all comment lines in 185 :param bool analyze: When True then apply analyze() method on the Fortran 187 :param bool clear_cache: Whether or not to wipe the parser cache prior 188 to parsing. Necessary when a new tree object 189 is required, even if the Fortran to be parsed has 192 :returns: Abstract Syntax Tree of Fortran source. 193 :rtype: :py:class:`fparser.api.BeginSource` 199 FortranParser.cache.clear()
207 ignore_comments=ignore_comments,
209 parser = FortranParser(reader, ignore_comments=ignore_comments)
220 def walk(stmt, depth=-1, _initial_depth=None):
221 """Generate Fortran statements by walking the stmt tree until given depth. 223 For each block statement in stmt, the walk functions yields a 224 tuple ``(statement, depth)`` where ``depth`` is the depth of tree 225 stucture for statement. 231 If depth is positive then walk in the tree until given depth. 232 If depth is negative then walk the whole tree. 243 from fparser import api 252 tree = api.parse(source_str) 253 for stmt, depth in api.walk(tree): 254 print depth, stmt.item 258 1 line #2'subroutine foo' 259 2 line #3'integer i, r' 260 2 line #4'do i=1,100' 266 if _initial_depth
is None:
269 _initial_depth = depth
270 if not isinstance(stmt, classes.BeginSource):
271 yield stmt, _initial_depth - depth
272 if isinstance(stmt, classes.BeginStatement):
273 last_stmt = stmt.content[-1]
274 last_index = len(stmt.content)
275 if isinstance(last_stmt, classes.EndStatement):
280 for substmt
in stmt.content[:last_index]:
281 for statement, statement_depth
in walk(
282 substmt, depth - 1, _initial_depth
284 yield statement, statement_depth
285 if last_stmt
is not None:
286 yield last_stmt, _initial_depth - depth