fparser.api

Public API for Fortran parser.

Module content

Module Contents

Functions

get_reader(source[, isfree, isstrict, include_dirs, ...])

Returns Fortran reader instance.

parse(source[, isfree, isstrict, include_dirs, ...])

Parse input and return Statement tree. Raises an AnalyzeError if the

walk(stmt[, depth, _initial_depth])

Generate Fortran statements by walking the stmt tree until given depth.

Attributes

__autodoc__

fparser.api.__autodoc__ = ['get_reader', 'parse', 'walk'][source]
fparser.api.get_reader(source, isfree=None, isstrict=None, include_dirs=None, source_only=None, ignore_comments=True)[source]

Returns Fortran reader instance.

If source is a C filename then the functions searches for comment lines starting with /*f2py and reads following lines as PYF file content until a line */ is found.

Parameters:
  • source (str) – Specify a string or filename containing Fortran code.

  • isfree (bool) – True if Fortran is free format

  • isstrict (bool) – True if we are to strictly enforce free/fixed format

  • include_dirs (list) – Specify a list of include directories. The default list (when include_dirs=None) contains the current working directory and the directory of source.

  • source_only (list) – Specify a list of Fortran file names that are searched when the USE statement is encountered.

  • ignore_comments (bool) – Whether or not to ignore (and discard) comments when parsing the source.

Returns:

a reader instance

Return type:

fparser.common.readfortran.FortranReader

fparser.api.parse(source, isfree=None, isstrict=None, include_dirs=None, source_only=None, ignore_comments=True, analyze=True, clear_cache=True)[source]

Parse input and return Statement tree. Raises an AnalyzeError if the parser can not parse the Fortran code.

Parameters:
  • source (str) – Specify a string or filename containing Fortran code.

  • isfree (bool) – Whether the Fortran source is free-format.

  • isstrict (bool) – Whether we are to strictly enforce the isfree setting.

  • include_dirs (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 (list) – 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 analyze() method on the Fortran code tree.

  • clear_cache (bool) – Whether or not to wipe the parser cache prior to parsing. Necessary when a new tree object is required, even if the Fortran to be parsed has been seen before.

Returns:

Abstract Syntax Tree of Fortran source.

Return type:

fparser.api.BeginSource

fparser.api.walk(stmt, depth=-1, _initial_depth=None)[source]

Generate Fortran statements by walking the stmt tree until given depth.

For each block statement in stmt, the walk functions yields a tuple (statement, depth) where depth is the depth of tree stucture for statement.

Parameters

stmt : Statement depth : int

If depth is positive then walk in the tree until given depth. If depth is negative then walk the whole tree.

Returns

generator

Examples

from fparser import api
source_str = '''
subroutine foo
  integer i, r
  do i=1,100
    r = r + i
  end do
end
'''
tree = api.parse(source_str)
for stmt, depth in api.walk(tree):
    print depth, stmt.item

that will print:

1 line #2'subroutine foo'
2 line #3'integer i, r'
2 line #4'do i=1,100'
3 line #5'r = r + i'
2 line #6'end do'
1 line #7'end'