fparser.common.readfortran

Provides Fortran reader classes.

Overview

Provides FortranReader classes for reading Fortran codes from files and strings. FortranReader handles comments and line continuations of both fix and free format Fortran codes.

Examples:

>> from fparser.common.readfortran import FortranFileReader
>>> import os
>>> reader = FortranFileReader(os.path.expanduser('~/src/blas/daxpy.f'))
>>> print reader.next()
line #1 'subroutine daxpy(n,da,dx,incx,dy,incy)'
>>> print `reader.next()`
Comment('c     constant times a vector plus a vector.\n
c     uses unrolled loops for increments equal to one.\n
c     jack dongarra, linpack, 3/11/78.\n
c     modified 12/3/93, array(1) declarations changed to array(*)',(3, 6))
>>> print `reader.next()`
Line('double precision dx(*),dy(*),da',(8, 8),'')
>>> print `reader.next()`
Line('integer i,incx,incy,ix,iy,m,mp1,n',(9, 9),'')

Note that the .next() method may return Line, SyntaxErrorLine, Comment, MultiLine, or SyntaxErrorMultiLine instance. Let us continue with the above example session to illustrate the Line methods and attributes:

>>> item = reader.next()
>>> item
    Line('if (da .eq. 0.0d0) return',(12, 12),'')
>>> item.line
    'if (da .eq. 0.0d0) return'
>>> item.strline
    'if (F2PY_EXPR_TUPLE_5) return'
>>> item.strlinemap
    {'F2PY_EXPR_TUPLE_5': 'da .eq. 0.0d0'}
>>> item.span
    (12, 12)
>>> item.get_line()
    'if (F2PY_EXPR_TUPLE_5) return'

To read a Fortran code from a string, use FortranStringReader class:

>>> from fparser.common.sourceinfo import FortranFormat
>>> from fparser.common.readfortran import FortranStringReader
>>> code = '''
...       subroutine foo(a)
...         integer a
...         print*,"a=",a
...       end
... '''
>>> reader = FortranStringReader(code)
>>> reader.set_format(FortranFormat(False, True))
>>> reader.next()
    Line('subroutine foo(a)',(2, 2),'')
>>> reader.next()
    Line('integer a',(3, 3),'')
>>> reader.next()
    Line('print*,"a=",a',(4, 4),'')

Module Contents

Classes

Line

Holds a Fortran source line.

Comment

Holds a Fortran comment.

MultiLine

Holds PYF file multiline.

FortranFileReader

Constructs a FortranFileReader object from a file.

FortranStringReader

Reads Fortran source code as a string.

exception fparser.common.readfortran.FortranReaderError[source]

Bases: Exception

Thrown when there is an error reading the Fortran source file.

class fparser.common.readfortran.Line(line, linenospan, label, name, reader)[source]

Holds a Fortran source line.

Attributes:

line : str
  code line
span : 2-tuple
  starting and ending line numbers
label : {int, None}
  Specify statement label
name : {str, None}
  Specify construct name.
reader : FortranReaderBase
strline : {None, str}
is_f2py_directive : bool
  the line contains f2py directive
has_map()[source]

Returns true when a substitution map has been registered.

apply_map(line)[source]

Substitutes magic strings in a line with values specified in a map.

copy(line=None, apply_map=False)[source]

Creates a Line object from a string.

If no line argument is specified a copy is made of this Line.

If a substitution map is provided it is used while making the copy.

clone(line)[source]

This Line has its contents overwitten by the passed string. The incoming string has substitution applied.

__repr__()[source]

Return repr(self).

__str__()[source]

Return str(self).

isempty(ignore_comments=False)[source]
get_line(apply_map=False)[source]
parse_line(cls, parent_cls)[source]
parse_block(reader, cls, parent_cls)[source]
exception fparser.common.readfortran.SyntaxErrorLine(line, linenospan, label, name, reader, message)[source]

Bases: Line, FortranReaderError

Indicates a syntax error while processing a line.

class fparser.common.readfortran.Comment(comment, linenospan, reader)[source]

Holds a Fortran comment.

Parameters:
  • comment (str) – String containing the text of a single or multi-line comment

  • linenospan ((int, int)) – A 2-tuple containing the start and end line numbers of the comment from the input source.

  • reader (fparser.common.readfortran.FortranReaderBase) – The reader object being used to read the input source.

__repr__()[source]

Return repr(self).

isempty(ignore_comments=False)[source]

Whether or not this comment is in fact empty (or we are ignoring it). Provided for compatibility with Line.isempty()

Parameters:

ignore_comments (bool) – whether we ignore comments

Returns:

True if we are ignoring comments, False otherwise

Return type:

bool

class fparser.common.readfortran.MultiLine(prefix, block, suffix, linenospan, reader)[source]

Holds PYF file multiline.

PYF file multiline is represented as follows:

prefix+'''+lines+'''+suffix.
Parameters:
__repr__()[source]

Return repr(self).

isempty(ignore_comments=False)[source]

Returns true if there is no significant text in this multi-line string.

exception fparser.common.readfortran.SyntaxErrorMultiLine(prefix, block, suffix, linenospan, reader, message)[source]

Bases: MultiLine, FortranReaderError

Indicates a syntax error while processing Python multi-line strings.

class fparser.common.readfortran.FortranFileReader(file_candidate, include_dirs=None, source_only=None, ignore_comments=True, ignore_encoding=True, include_omp_conditional_lines=False)[source]

Bases: FortranReaderBase

Constructs a FortranFileReader object from a file.

Parameters:
  • file_candidate – A filename or file-like object.

  • include_dirs (list) – Directories in which to look for inclusions.

  • source_only (list) – Fortran source files to search for modules required by “use” statements.

  • ignore_comments (bool) – Whether or not to ignore comments

  • ignore_encoding (Optional[bool]) – whether or not to ignore Python-style encoding information (e.g. “-- fortran --”) when attempting to determine the format of the file. Default is True.

  • include_omp_conditional_lines (Optional[bool]) – whether or not the content of a line with an OMP sentinel is parsed or not. Default is False (in which case it is treated as a Comment).

For example:

>>> from fparser.common.readfortran import FortranFileReader
>>> import os
>>> reader = FortranFileReader('myfile.f90')
__del__()[source]
close_source()[source]

Called when self.source.next() raises StopIteration.

class fparser.common.readfortran.FortranStringReader(string, include_dirs=None, source_only=None, ignore_comments=True, ignore_encoding=True, include_omp_conditional_lines=False)[source]

Bases: FortranReaderBase

Reads Fortran source code as a string.

Parameters:
  • string (str) – string to read

  • include_dirs (list) – List of dirs to search for include files

  • source_only (list) – Fortran source files to search for modules required by “use” statements.

  • ignore_comments (bool) – Whether or not to ignore comments

  • ignore_encoding (Optional[bool]) – whether or not to ignore Python-style encoding information (e.g. “-- fortran --”) when attempting to determine the format of the source. Default is True.

  • include_omp_conditional_lines (Optional[bool]) – whether or not the content of a line with an OMP sentinel is parsed or not. Default is False (in which case it is treated as a Comment).

For example:

>>> from fparser.common.readfortran import FortranStringReader
>>> code = '''
         subroutine foo(a)
            integer a
            print*,"a=",a
          end
    '''
>>> reader = FortranStringReader(code)