summaryrefslogtreecommitdiff
path: root/numpy/f2py/lib/block.py
blob: 09d39d0e2fdf0ae8434bcc2c0a794b1d9fb3cdea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python
"""
Defines Block classes.

Permission to use, modify, and distribute this software is given under the
terms of the NumPy License. See http://scipy.org.
NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.

Author: Pearu Peterson <pearu@cens.ioc.ee>
Created: May 2006

"""

__all__ = ['Block','ModuleBlock','PythonModuleBlock','InterfaceBlock',
           'SubroutineBlock','FunctionBlock','TypeBlock', 'ProgramBlock',
           'BlockDataBlock','DoBlock','IfThenBlock','SelectBlock',
           'StatementBlock']

import re
import sys

from readfortran import Line
from splitline import string_replace_map
from stmt import statements, end_stmts, block_stmts

class Block:
    """
    Block instance has attributes:
      content - list of Line or Statement instances
      name    - name of the block, unnamed blocks are named
                with the line label
      parent  - Block or FortranParser instance
      item    - Line instance containing the block start statement
      get_item, put_item - methods to retrive/submit Line instaces
                from/to Fortran reader.
      isvalid - boolean, when False, the Block instance will be ignored.
    """

    classes = {}
    
    end_re = re.compile(r'end\Z', re.I).match

    def __init__(self, parent, item = None):
        """
        parent - Block or FortanParser instance having the
                 following attributes: reader, get_item, put_item
        item   - Line instance containing the beginning of block
                 statement.
        """
        self.parent = parent
        self.reader = parent.reader
        self.get_item = parent.get_item # get line function
        self.put_item = parent.put_item # put line function

        self.content = []
        self.name = None

        self.item = item
        if item is None:
            return
        stmt = self.stmt_cls(self, item)
        self.isvalid = stmt.isvalid
        if self.isvalid:
            self.content.append(stmt)
            self.name = stmt.name
            self.fill() # read block content

    def get_name(self):
        if self.__class__ is Block: return '__F2PY_MAIN__'
        if not hasattr(self,'name') or self.name is None: return ''
        return self.name

    def __str__(self):
        l=[]
        for c in self.content:
            l.append(str(c))
        return '\n'.join(l)

    def isstmt(self, item):
        """
        Check is item is blocks start statement, if it is, read the block.
        """
        line = item.get_line()
        mode = item.reader.mode
        classes = self.classes[mode] + statements[self.__class__.__name__]
        for cls in classes:
            if issubclass(cls, Block):
                match_cmd = cls.stmt_cls.start_re
            else:
                match_cmd = cls.start_re
            if match_cmd(line):
                subblock = cls(self, item)
                if subblock.isvalid:
                    self.content.append(subblock)
                    return True
        return False

    def isendblock(self, item):
        line = item.get_line()
        if self.__class__ is Block:
            # MAIN block does not define start/end line conditions,
            # so it should never end until all lines are read.
            # However, sometimes F77 programs lack the PROGRAM statement,
            # and here we fix that:
            if self.reader.isfix77:
                m = self.end_re(line)
                if m:
                    message = self.reader.format_message(\
                        'WARNING',
                        'assuming the end of undefined PROGRAM statement',
                        item.span[0],item.span[1])
                    print >> sys.stderr, message
                    l = Line('program UNDEFINED',(0,0),None,self.reader)
                    p = Program(self,l)
                    p.content.extend(self.content)
                    self.content[:] = [p]
                    return True
            return False
        cls = self.end_stmt_cls
        if cls.start_re(line):
            stmt = cls(self, item)
            if stmt.isvalid:
                self.content.append(stmt)
                return True
        return False

    def fill(self):
        """
        Fills blocks content until the end of block statement.
        """
        end_flag = self.__class__ is Block
        item = self.get_item()
        while item is not None:
            if isinstance(item, Line):
                # handle end of a block
                if self.isendblock(item):
                    end_flag = True
                    break
                elif not self.isstmt(item):
                    # put unknown item's to content.
                    self.content.append(item)
            item = self.get_item()
        if not end_flag:
            message = self.reader.format_message(\
                        'WARNING',
                        'failed to find the end of block for %s'\
                        % (self.__class__.__name__),
                        self.item.span[0],self.item.span[1])
            print >> sys.stderr, message
            sys.stderr.flush()
        return

class ProgramUnit(Block):
    """
    <main program>
    <external subprogram (function | subroutine)>
    <module>
    <block data>    
    """

class ProgramBlock(ProgramUnit):
    """
    program [name]
      <specification part>
      <execution part>
      <internal subprogram part>
    end [program [name]]
    """
    classes = {}
    
class ModuleBlock(ProgramUnit):
    """
    module <name>
      <specification part>
      <module subprogram part>
    end [module [name]]
    """
    classes = {}
    
class BlockDataBlock(ProgramUnit):
    """
    block data [name]
    end [block data [name]]
    """
    classes = {}
    
class InterfaceBlock(ProgramUnit):
    """
    abstract interface | interface [<generic-spec>]
      <interface specification>
    end interface [<generic spec>]
    """
    classes = {}

class PythonModuleBlock(ProgramUnit):
    """
    python module <name>
      ..
    end [python module [<name>]]
    """

class SubroutineBlock(ProgramUnit):
    """
    [prefix] subroutine <name> [ ( [<dummy-arg-list>] ) [<proc-language-binding-spec>]]
      <specification-part>
      <execution-part>
      <internal-subprogram part>
    end [subroutine [name]]
    """
    classes = {}
    
class FunctionBlock(ProgramUnit):
    classes = {}
    
class TypeBlock(Block):
    """
    type [[type-attr-spec-list] ::] <name> [(type-param-name-list)]
      <type-param-def-stmt>
      <private-or-sequence>
      <component-part>
      <type-bound-procedure-part>
    end type [name]
    """
    classes = {}

class StatementBlock(Block):
    """
    <start stmt-block>
      <statements>
    <end stmt-block>
    """
    classes = {}
    
class DoBlock(StatementBlock):

    begin_re = re.compile(r'do\b\s*(?P<label>\d*)', re.I).match

    def __init__(self, parent, item):
        label = self.begin_re(item.get_line()).group('label').strip()
        self.endlabel = label
        StatementBlock.__init__(self, parent, item)

    def isendblock(self, item):
        if self.endlabel:
            # Handle:
            #   do 1, i=1,n
            #   ..
            # 1 continue 
            if item.label==self.endlabel:
                # item may contain computational statemets
                self.content.append(item)
                # the same item label may be used for different block ends
                self.put_item(item)       
                return True
        else:
            return StatementBlock.isendblock(self, item)
        return False

class IfThenBlock(StatementBlock):

    pass
    
class SelectBlock(StatementBlock):

    pass