module documentation

crackfortran --- read fortran (77,90) code and extract declaration information.

Copyright 1999-2004 Pearu Peterson all rights reserved, Pearu Peterson <pearu@ioc.ee> Permission to use, modify, and distribute this software is given under the terms of the NumPy License.

NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. $Date: 2005/09/27 07:13:49 $ Pearu Peterson

Usage of crackfortran:

Command line keys: -quiet,-verbose,-fix,-f77,-f90,-show,-h <pyffilename>
-m <module name for f77 routines>,--ignore-contains

Functions: crackfortran, crack2fortran The following Fortran statements/constructions are supported (or will be if needed):

block data,byte,call,character,common,complex,contains,data, dimension,double complex,double precision,end,external,function, implicit,integer,intent,interface,intrinsic, logical,module,optional,parameter,private,public, program,real,(sequence?),subroutine,type,use,virtual, include,pythonmodule

Note: 'virtual' is mapped to 'dimension'. Note: 'implicit integer (z) static (z)' is 'implicit static (z)' (this is minor bug). Note: code after 'contains' will be ignored until its scope ends. Note: 'common' statement is extended: dimensions are moved to variable definitions Note: f2py directive: <commentchar>f2py<line> is read as <line> Note: pythonmodule is introduced to represent Python module

Usage:

postlist=crackfortran(files) postlist contains declaration information read from the list of files files. crack2fortran(postlist) returns a fortran code to be saved to pyf-file

postlist has the following structure:

*** it is a list of dictionaries containing `blocks':
B = {'block','body','vars','parent_block'[,'name','prefix','args','result',
'implicit','externals','interfaced','common','sortvars', 'commonvars','note']}
B['block'] = 'interface' | 'function' | 'subroutine' | 'module' |
'program' | 'block data' | 'type' | 'pythonmodule' | 'abstract interface'

B['body'] --- list containing `subblocks' with the same structure as `blocks' B['parent_block'] --- dictionary of a parent block:

C['body'][<index>]['parent_block'] is C

B['vars'] --- dictionary of variable definitions B['sortvars'] --- dictionary of variable definitions sorted by dependence (independent first) B['name'] --- name of the block (not if B['block']=='interface') B['prefix'] --- prefix string (only if B['block']=='function') B['args'] --- list of argument names if B['block']== 'function' | 'subroutine' B['result'] --- name of the return value (only if B['block']=='function') B['implicit'] --- dictionary {'a':<variable definition>,'b':...} | None B['externals'] --- list of variables being external B['interfaced'] --- list of variables being external and defined B['common'] --- dictionary of common blocks (list of objects) B['commonvars'] --- list of variables used in common blocks (dimensions are moved to variable definitions) B['from'] --- string showing the 'parents' of the current block B['use'] --- dictionary of modules used in current block:

{<modulename>:{['only':<0|1>],['map':{<local_name1>:<use_name1>,...}]}}

B['note'] --- list of LaTeX comments on the block B['f2pyenhancements'] --- optional dictionary

{'threadsafe':'','fortranname':<name>,
'callstatement':<C-expr>|<multi-line block>, 'callprotoargument':<C-expr-list>, 'usercode':<multi-line block>|<list of multi-line blocks>, 'pymethoddef:<multi-line block>' }

B['entry'] --- dictionary {entryname:argslist,..} B['varnames'] --- list of variable names given in the order of reading the

Fortran code, useful for derived types.

B['saved_interface'] --- a string of scanned routine signature, defines explicit interface

*** Variable definition is a dictionary

D = B['vars'][<variable name>] = {'typespec'[,'attrspec','kindselector','charselector','=','typename']} D['typespec'] = 'byte' | 'character' | 'complex' | 'double complex' |

'double precision' | 'integer' | 'logical' | 'real' | 'type'
D['attrspec'] --- list of attributes (e.g. 'dimension(<arrayspec>)',
'external','intent(in|out|inout|hide|c|callback|cache|aligned4|aligned8|aligned16)', 'optional','required', etc)
K = D['kindselector'] = {['*','kind']} (only if D['typespec'] =
'complex' | 'integer' | 'logical' | 'real' )
C = D['charselector'] = {['*','len','kind']}
(only if D['typespec']=='character')

D['='] --- initialization expression string D['typename'] --- name of the type if D['typespec']=='type' D['dimension'] --- list of dimension bounds D['intent'] --- list of intent specifications D['depend'] --- list of variable names on which current variable depends on D['check'] --- list of C-expressions; if C-expr returns zero, exception is raised D['note'] --- list of LaTeX comments on the variable

*** Meaning of kind/char selectors (few examples):
D['typespec>']*K['*'] D['typespec'](kind=K['kind']) character*C['*'] character(len=C['len'],kind=C['kind']) (see also fortran type declaration statement formats below)

Fortran 90 type declaration statement format (F77 is subset of F90)

(Main source: IBM XL Fortran 5.1 Language Reference Manual) type declaration = <typespec> [[<attrspec>]::] <entitydecl> <typespec> = byte |

character[<charselector>] | complex[<kindselector>] | double complex | double precision | integer[<kindselector>] | logical[<kindselector>] | real[<kindselector>] | type(<typename>)
<charselector> = * <charlen> |
([len=]<len>[,[kind=]<kind>]) | (kind=<kind>[,len=<len>])
<kindselector> = * <intlen> |
([kind=]<kind>)
<attrspec> = comma separated list of attributes.

Only the following attributes are used in building up the interface:

external (parameter --- affects '=' key) optional intent

Other attributes are ignored.

<intentspec> = in | out | inout <arrayspec> = comma separated list of dimension bounds. <entitydecl> = <name> [[*<charlen>][(<arrayspec>)] | [(<arrayspec>)]*<charlen>]

[/<init_expr>/ | =<init_expr>] [,<entitydecl>]

In addition, the following attributes are used: check,depend,note

TODO:
  • Apply 'parameter' attribute (e.g. 'integer parameter :: i=2' 'real x(i)'
    -> 'real x(2)')

The above may be solved by creating appropriate preprocessor program, for example.

Variable analyzeargs​_re​_1 Undocumented
Variable badnames Undocumented
Variable beforethisafter Undocumented
Variable beginpattern Undocumented
Variable beginpattern77 Undocumented
Variable beginpattern90 Undocumented
Variable callfunpattern Undocumented
Variable callnameargspattern Undocumented
Variable callpattern Undocumented
Variable charselector Undocumented
Variable commonpattern Undocumented
Variable containspattern Undocumented
Variable crackline​_re​_1 Undocumented
Variable currentfilename Undocumented
Variable datapattern Undocumented
Variable debug Undocumented
Variable defaultimplicitrules Undocumented
Variable determineexprtype​_re​_1 Undocumented
Variable determineexprtype​_re​_2 Undocumented
Variable determineexprtype​_re​_3 Undocumented
Variable determineexprtype​_re​_4 Undocumented
Variable determineexprtype​_re​_5 Undocumented
Variable dimensionpattern Undocumented
Variable dolowercase Undocumented
Variable endifpattern Undocumented
Variable endifs Undocumented
Variable endpattern Undocumented
Variable entrypattern Undocumented
Variable expectbegin Undocumented
Variable externalpattern Undocumented
Variable f2pyenhancementspattern Undocumented
Variable f77modulename Undocumented
Variable f90modulevars Undocumented
Variable filepositiontext Undocumented
Variable formatpattern Undocumented
Variable fortrantypes Undocumented
Variable functionpattern Undocumented
Variable getlincoef​_re​_1 Undocumented
Variable gotnextfile Undocumented
Variable groupbegins77 Undocumented
Variable groupbegins90 Undocumented
Variable groupcache Undocumented
Variable groupcounter Undocumented
Variable groupends Undocumented
Variable grouplist Undocumented
Variable groupname Undocumented
Variable ignorecontains Undocumented
Variable implicitpattern Undocumented
Variable include​_paths Undocumented
Variable intentpattern Undocumented
Variable intrinsicpattern Undocumented
Variable invbadnames Undocumented
Variable is​_f​_file Undocumented
Variable kindselector Undocumented
Variable lenarraypattern Undocumented
Variable lenkindpattern Undocumented
Variable multilinepattern Undocumented
Variable nameargspattern Undocumented
Variable namepattern Undocumented
Variable neededmodule Undocumented
Variable onlyfuncs Undocumented
Variable optionalpattern Undocumented
Variable parameterpattern Undocumented
Variable previous​_context Undocumented
Variable privatepattern Undocumented
Variable publicpattern Undocumented
Variable pyffilename Undocumented
Variable quiet Undocumented
Variable real16pattern Undocumented
Variable real8pattern Undocumented
Variable requiredpattern Undocumented
Variable selectpattern Undocumented
Variable skipblocksuntil Undocumented
Variable skipemptyends Undocumented
Variable skipfuncs Undocumented
Variable skipfunctions Undocumented
Variable sourcecodeform Undocumented
Variable strictf77 Undocumented
Variable subroutinepattern Undocumented
Variable tabchar Undocumented
Variable typespattern Undocumented
Variable typespattern4implicit Undocumented
Variable usepattern Undocumented
Variable usermodules Undocumented
Variable verbose Undocumented
Variable word​_pattern Undocumented
Function ​_calc​_depend​_dict Undocumented
Function ​_ensure​_exprdict Undocumented
Function ​_eval​_length Undocumented
Function ​_eval​_scalar Undocumented
Function ​_get​_depend​_dict Undocumented
Function ​_is​_intent​_callback Undocumented
Function ​_kind​_func Undocumented
Function ​_resolvenameargspattern Undocumented
Function ​_selected​_int​_kind​_func Undocumented
Function ​_selected​_real​_kind​_func Undocumented
Function ​_simplifyargs Undocumented
Function analyzeargs Undocumented
Function analyzebody Undocumented
Function analyzecommon Undocumented
Function analyzeline Undocumented
Function analyzevars Undocumented
Function appenddecl Undocumented
Function appendmultiline Undocumented
Function buildimplicitrules Undocumented
Function common2fortran Undocumented
Function crack2fortran Undocumented
Function crack2fortrangen Undocumented
Function crackfortran Undocumented
Function crackline reset=-1 --- initialize reset=0 --- crack the line reset=1 --- final check if mismatch of blocks occurred
Function cracktypespec Undocumented
Function cracktypespec0 Undocumented
Function determineexprtype Undocumented
Function expr2name Undocumented
Function get​_parameters Undocumented
Function get​_sorted​_names
Function get​_usedict Undocumented
Function get​_useparameters Undocumented
Function getblockname Undocumented
Function getextension Undocumented
Function getlincoef Obtain a and b when e == "a*x+b", where x is a symbol in xset.
Function is​_free​_format Check if file is in free format Fortran.
Function markinnerspaces The function replace all spaces in the input variable line which are surrounded with quotation marks, with the triplet "@_@".
Function markoutercomma Undocumented
Function markouterparen Undocumented
Function myeval Like eval but returns only integers and floats
Function outmess Undocumented
Function postcrack TODO: function return values determine expression types if in argument list
Function postcrack2 Undocumented
Function readfortrancode No summary
Function removespaces Undocumented
Function reset​_global​_f2py​_vars Undocumented
Function rmbadname Undocumented
Function rmbadname1 Undocumented
Function setattrspec Undocumented
Function setcharselector Undocumented
Function setkindselector Undocumented
Function setmesstext Undocumented
Function sortvarnames Undocumented
Function split​_by​_unquoted Splits the line into (line[:i], line[i:]), where i is the index of first occurrence of one of the characters not within quotes, or len(line) if no such index exists
Function true​_intent​_list Undocumented
Function undo​_rmbadname Undocumented
Function undo​_rmbadname1 Undocumented
Function unmarkouterparen Undocumented
Function updatevars Undocumented
Function use2fortran Undocumented
Function vars2fortran TODO: public sub ...
Variable ​_free​_f90​_start Undocumented
Variable ​_has​_f90​_header Undocumented
Variable ​_has​_f​_header Undocumented
Variable ​_has​_fix​_header Undocumented
Variable ​_intentcallbackpattern Undocumented
Variable ​_is​_kind​_number Undocumented
analyzeargs_re_1 =

Undocumented

badnames: dict =

Undocumented

beforethisafter =

Undocumented

beginpattern: str =

Undocumented

beginpattern77 =

Undocumented

beginpattern90 =

Undocumented

callfunpattern =

Undocumented

callnameargspattern =

Undocumented

callpattern =

Undocumented

charselector =

Undocumented

commonpattern =

Undocumented

containspattern =

Undocumented

crackline_re_1 =

Undocumented

currentfilename: str =

Undocumented

datapattern =

Undocumented

debug: list =

Undocumented

defaultimplicitrules: dict =

Undocumented

determineexprtype_re_1 =

Undocumented

determineexprtype_re_2 =

Undocumented

determineexprtype_re_3 =

Undocumented

determineexprtype_re_4 =

Undocumented

determineexprtype_re_5 =

Undocumented

dimensionpattern =

Undocumented

dolowercase: int =

Undocumented

endifpattern =

Undocumented

endifs: str =

Undocumented

endpattern =

Undocumented

entrypattern =

Undocumented

expectbegin: int =

Undocumented

externalpattern =

Undocumented

f2pyenhancementspattern =

Undocumented

f77modulename: str =

Undocumented

f90modulevars: dict =

Undocumented

filepositiontext: str =

Undocumented

formatpattern =

Undocumented

fortrantypes: str =

Undocumented

functionpattern =

Undocumented

getlincoef_re_1 =

Undocumented

gotnextfile: int =

Undocumented

groupbegins77: str =

Undocumented

groupbegins90 =

Undocumented

groupcache =

Undocumented

groupcounter: int =

Undocumented

groupends: str =

Undocumented

grouplist =

Undocumented

groupname: str =

Undocumented

ignorecontains: int =

Undocumented

implicitpattern =

Undocumented

include_paths: list =

Undocumented

intentpattern =

Undocumented

intrinsicpattern =

Undocumented

invbadnames: dict =

Undocumented

is_f_file =

Undocumented

kindselector =

Undocumented

lenarraypattern =

Undocumented

lenkindpattern =

Undocumented

multilinepattern =

Undocumented

nameargspattern =

Undocumented

namepattern =

Undocumented

neededmodule: int =

Undocumented

onlyfuncs: list =

Undocumented

optionalpattern =

Undocumented

parameterpattern =

Undocumented

previous_context =

Undocumented

privatepattern =

Undocumented

publicpattern =

Undocumented

pyffilename: str =

Undocumented

quiet: int =

Undocumented

real16pattern =

Undocumented

real8pattern =

Undocumented

requiredpattern =

Undocumented

selectpattern =

Undocumented

skipblocksuntil: int =

Undocumented

skipemptyends: int =

Undocumented

skipfuncs: list =

Undocumented

skipfunctions: list =

Undocumented

sourcecodeform: str =

Undocumented

strictf77: int =

Undocumented

subroutinepattern =

Undocumented

tabchar =

Undocumented

typespattern =

Undocumented

typespattern4implicit =

Undocumented

usepattern =

Undocumented

usermodules: list =

Undocumented

verbose: int =

Undocumented

word_pattern =

Undocumented

def _calc_depend_dict(vars):

Undocumented

def _ensure_exprdict(r):

Undocumented

def _eval_length(length, params):

Undocumented

def _eval_scalar(value, params):

Undocumented

def _get_depend_dict(name, vars, deps):

Undocumented

def _is_intent_callback(vdecl):

Undocumented

def _kind_func(string):

Undocumented

def _resolvenameargspattern(line):

Undocumented

def _selected_int_kind_func(r):

Undocumented

def _selected_real_kind_func(p, r=0, radix=0):

Undocumented

def _simplifyargs(argsline):

Undocumented

def analyzeargs(block):

Undocumented

def analyzebody(block, args, tab=''):

Undocumented

def analyzecommon(block):

Undocumented

def analyzeline(m, case, line):

Undocumented

def analyzevars(block):

Undocumented

def appenddecl(decl, decl2, force=1):

Undocumented

def appendmultiline(group, context_name, ml):

Undocumented

def buildimplicitrules(block):

Undocumented

def common2fortran(common, tab=''):

Undocumented

def crack2fortran(block):

Undocumented

def crack2fortrangen(block, tab='\n', as_interface=False):

Undocumented

def crackfortran(files):

Undocumented

def crackline(line, reset=0):

reset=-1 --- initialize reset=0 --- crack the line reset=1 --- final check if mismatch of blocks occurred

Cracked data is saved in grouplist[0].

def cracktypespec(typespec, selector):

Undocumented

def cracktypespec0(typespec, ll):

Undocumented

def determineexprtype(expr, vars, rules={}):

Undocumented

def expr2name(a, block, args=[]):

Undocumented

def get_parameters(vars, global_params={}):

Undocumented

def get_sorted_names(vars):
def get_usedict(block):

Undocumented

def get_useparameters(block, param_map=None):

Undocumented

def getblockname(block, unknown='unknown'):

Undocumented

def getextension(name):

Undocumented

def getlincoef(e, xset):

Obtain a and b when e == "a*x+b", where x is a symbol in xset.

>>> getlincoef('2*x + 1', {'x'})
(2, 1, 'x')
>>> getlincoef('3*x + x*2 + 2 + 1', {'x'})
(5, 3, 'x')
>>> getlincoef('0', {'x'})
(0, 0, None)
>>> getlincoef('0*x', {'x'})
(0, 0, 'x')
>>> getlincoef('x*x', {'x'})
(None, None, None)

This can be tricked by sufficiently complex expressions

>>> getlincoef('(x - 0.5)*(x - 1.5)*(x - 1)*x + 2*x + 3', {'x'})
(2.0, 3.0, 'x')
def is_free_format(file):
Check if file is in free format Fortran.
def markinnerspaces(line):

The function replace all spaces in the input variable line which are surrounded with quotation marks, with the triplet "@_@".

For instance, for the input "a 'b c'" the function returns "a 'b@_@c'"

Parameters

line : str

Returns

str

def markoutercomma(line, comma=','):

Undocumented

def markouterparen(line):

Undocumented

def myeval(e, g=None, l=None):
Like eval but returns only integers and floats
def outmess(line, flag=1):

Undocumented

def postcrack(block, args=None, tab=''):
TODO:
function return values determine expression types if in argument list
def postcrack2(block, tab='', param_map=None):

Undocumented

def readfortrancode(ffile, dowithline=show, istop=1):
Read fortran codes from files and
  1. Get rid of comments, line continuations, and empty lines; lower cases.
  2. Call dowithline(line) on every line.
  3. Recursively call itself when statement "include '<filename>'" is met.
def removespaces(expr):

Undocumented

def reset_global_f2py_vars():

Undocumented

def rmbadname(names):

Undocumented

def rmbadname1(name):

Undocumented

def setattrspec(decl, attr, force=0):

Undocumented

def setcharselector(decl, sel, force=0):

Undocumented

def setkindselector(decl, sel, force=0):

Undocumented

def setmesstext(block):

Undocumented

def sortvarnames(vars):

Undocumented

def split_by_unquoted(line, characters):
Splits the line into (line[:i], line[i:]), where i is the index of first occurrence of one of the characters not within quotes, or len(line) if no such index exists
def true_intent_list(var):

Undocumented

def undo_rmbadname(names):

Undocumented

def undo_rmbadname1(name):

Undocumented

def unmarkouterparen(line):

Undocumented

def updatevars(typespec, selector, attrspec, entitydecl):

Undocumented

def use2fortran(use, tab=''):

Undocumented

def vars2fortran(block, vars, args, tab='', as_interface=False):
TODO: public sub ...
_free_f90_start =

Undocumented

_has_f90_header =

Undocumented

_has_f_header =

Undocumented

_has_fix_header =

Undocumented

_intentcallbackpattern =

Undocumented

_is_kind_number =

Undocumented