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
Holds a Fortran source line. |
|
Holds a Fortran comment. |
|
Holds PYF file multiline. |
|
Constructs a FortranFileReader object from a file. |
|
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
- 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.
- 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.
- 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:
prefix (str) – the prefix of the line(s)
block (List[
fparser.common.readfortran.Line
]) – list of linessuffix (str) – the suffix of the block of lines
linenospan (Tuple[int, int]) – starting and ending line numbers
reader (
fparser.common.readfortran.FortranReaderBase
) – the current reader instance.
- 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')
- 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)