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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
"""
"""
import re
import sys
from base_classes import BeginStatement, EndStatement, Statement
from readfortran import Line
# File block
class EndSource(EndStatement):
"""
"""
match = staticmethod(lambda s: False)
class BeginSource(BeginStatement):
"""
"""
match = staticmethod(lambda s: True)
end_stmt_cls = EndSource
def process_item(self):
self.name = self.reader.name
self.fill(end_flag = True)
return
def get_classes(self):
return program_unit
def process_subitem(self, item):
# 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:
line = item.get_line()
if line=='end':
message = self.reader.format_message(\
'WARNING',
'assuming the end of undefined PROGRAM statement',
item.span[0],item.span[1])
print >> sys.stderr, message
p = Program(self)
p.content.extend(self.content)
p.content.append(EndProgram(p,item))
self.content[:] = [p]
return
return BeginStatement.process_subitem(self, item)
# Module
class EndModule(EndStatement):
match = re.compile(r'end(\s*module\s*\w*|)\Z', re.I).match
class Module(BeginStatement):
"""
MODULE <name>
..
END [MODULE [name]]
"""
match = re.compile(r'module\s*\w+\Z', re.I).match
end_stmt_cls = EndModule
def get_classes(self):
return access_spec + specification_part + module_subprogram_part
def process_item(self):
name = self.item.get_line().replace(' ','')[len(self.blocktype):].strip()
self.name = name
return BeginStatement.process_item(self)
#def __str__(self):
# s = self.get_indent_tab(deindent=True)
# s += 'MODULE '+ self.name
# return s
# Python Module
class EndPythonModule(EndStatement):
match = re.compile(r'end(\s*python\s*module\s*\w*|)\Z', re.I).match
class PythonModule(BeginStatement):
"""
PYTHON MODULE <name>
..
END [PYTHON MODULE [name]]
"""
modes = ['pyf']
match = re.compile(r'python\s*module\s*\w+\Z', re.I).match
end_stmt_cls = EndPythonModule
def get_classes(self):
return [Interface, Function, Subroutine, Module]
def process_item(self):
name = self.item.get_line().replace(' ','')[len(self.blocktype):].strip()
self.name = name
return BeginStatement.process_item(self)
# Program
class EndProgram(EndStatement):
"""
END [PROGRAM [name]]
"""
match = re.compile(r'end(\s*program\s*\w*|)\Z', re.I).match
class Program(BeginStatement):
""" PROGRAM [name]
"""
match = re.compile(r'program\s*\w*\Z', re.I).match
end_stmt_cls = EndProgram
def get_classes(self):
return specification_part + execution_part + internal_subprogram_part
def process_item(self):
name = self.item.get_line().replace(' ','')[len(self.blocktype):].strip()
if name:
self.name = name
return BeginStatement.process_item(self)
# Interface
class EndInterface(EndStatement):
match = re.compile(r'end\s*interface\s*\w*\Z', re.I).match
blocktype = 'interface'
class Interface(BeginStatement):
"""
INTERFACE [generic-spec] | ABSTRACT INTERFACE
END INTERFACE [generic-spec]
"""
modes = ['free90', 'fix90', 'pyf']
match = re.compile(r'(interface\s*\w*|abstract\s*interface)\Z',re.I).match
end_stmt_cls = EndInterface
blocktype = 'interface'
def get_classes(self):
return interface_specification
def process_item(self):
line = self.item.get_line()
self.isabstract = line.startswith('abstract')
if self.isabstract:
self.generic_spec = ''
else:
self.generic_spec = line[len(self.blocktype):].strip()
self.name = self.generic_spec # XXX
return BeginStatement.process_item(self)
def tostr(self):
if self.isabstract:
return 'ABSTRACT INTERFACE'
return 'INTERFACE '+ str(self.generic_spec)
# Subroutine
class EndSubroutine(EndStatement):
"""
END [SUBROUTINE [name]]
"""
match = re.compile(r'end(\s*subroutine\s*\w*|)\Z', re.I).match
class Subroutine(BeginStatement):
"""
[prefix] SUBROUTINE <name> [ ( [<dummy-arg-list>] ) [<proc-language-binding-spec>]]
"""
end_stmt_cls = EndSubroutine
match = re.compile(r'[\w\s]*subroutine\s*\w+', re.I).match
item_re = re.compile(r'(?P<prefix>[\w\s]*)\s*subroutine\s*(?P<name>\w+)', re.I).match
def process_item(self):
line = self.item.get_line()
m = self.item_re(line)
self.name = m.group('name')
line = line[m.end():].strip()
args = []
if line.startswith('('):
assert line.endswith(')'),`line`
for a in line.split(','):
args.append(a.strip())
self.args = args
return BeginStatement.process_item(self)
def get_classes(self):
return specification_part + execution_part + internal_subprogram_part
# Function
class EndFunction(EndStatement):
"""
END [FUNCTION [name]]
"""
match = re.compile(r'end(\s*function\s*\w*|)\Z', re.I).match
class Function(BeginStatement):
"""
[prefix] SUBROUTINE <name> [ ( [<dummy-arg-list>] ) [suffix]
"""
end_stmt_cls = EndFunction
match = re.compile(r'([\w\s]+(\(\s*\w+\s*\)|)|)\s*function\s*\w+', re.I).match
item_re = re.compile(r'(?P<prefix>([\w\s](\(\s*\w+\s*\)|))*)\s*function\s*(?P<name>\w+)\s*\((?P<args>.*)\)\s*(?P<suffix>.*)\Z', re.I).match
def process_item(self):
line = self.item.get_line()
m = self.item_re(line)
if m is None:
self.isvalid = False
return
self.name = m.group('name')
self.prefix = m.group('prefix').strip()
self.suffix = m.group('suffix').strip()
args = []
for a in m.group('args').split(','):
args.append(a.strip())
self.args = args
return BeginStatement.process_item(self)
def tostr(self):
return '%s FUNCTION %s(%s) %s' % (self.prefix, self.name,
', '.join(self.args), self.suffix)
def get_classes(self):
return specification_part + execution_part + internal_subprogram_part
# SelectCase
class EndSelect(EndStatement):
match = re.compile(r'end\s*select\s*\w*\Z', re.I).match
blocktype = 'select'
class Select(BeginStatement):
"""
[ <case-construct-name> : ] SELECT CASE ( <case-expr> )
"""
match = re.compile(r'select\s*case\s*\(.*\)\Z',re.I).match
end_stmt_cls = EndSelect
name = ''
def tostr(self):
return 'SELECT CASE ( %s )' % (self.expr)
def process_item(self):
self.expr = self.item.get_line()[6:].lstrip()[4:].lstrip()[1:-1].strip()
self.name = self.item.label
return BeginStatement.process_item(self)
def get_classes(self):
return [Case] + execution_part_construct
# IfThen
class EndIfThen(EndStatement):
match = re.compile(r'end\s*if\s*\w*\Z', re.I).match
blocktype = 'if'
class IfThen(BeginStatement):
"""
[<if-construct-name> :] IF ( <scalar-logical-expr> ) THEN
IfThen instance has the following attributes:
expr
"""
match = re.compile(r'if\s*\(.*\)\s*then\Z',re.I).match
end_stmt_cls = EndIfThen
name = ''
def tostr(self):
return 'IF (%s) THEN' % (self.expr)
def process_item(self):
item = self.item
line = item.get_line()[2:-4].strip()
assert line[0]=='(' and line[-1]==')',`line`
self.expr = line[1:-1].strip()
self.name = item.label
return BeginStatement.process_item(self)
def get_classes(self):
return [Else, ElseIf] + execution_part_construct
class If(BeginStatement):
"""
IF ( <scalar-logical-expr> ) action-stmt
"""
match = re.compile(r'if\s*\(').match
def process_item(self):
item = self.item
mode = self.reader.mode
classes = self.get_classes()
classes = [cls for cls in classes if mode in cls.modes]
line = item.get_line()[2:]
i = line.find(')')
expr = line[1:i].strip()
line = line[i+1:].strip()
if line=='then':
self.isvalid = False
return
self.expr = expr[1:-1]
newitem = item.copy(line)
for cls in classes:
if cls.match(line):
stmt = cls(self, newitem)
if stmt.isvalid:
self.content.append(stmt)
return
self.handle_unknown_item(newitem)
return
def tostr(self):
assert len(self.content)==1,`self.content`
return 'IF (%s) %s' % (self.expr, str(self.content[0]).lstrip())
def __str__(self):
return self.get_indent_tab(colon=':') + self.tostr()
def get_classes(self):
return action_stmt
# Do
class EndDo(EndStatement):
"""
"""
match = re.compile(r'end\s*do\s*\w*\Z', re.I).match
blocktype = 'do'
class Do(BeginStatement):
"""
[ <do-construct-name> : ] DO label [loopcontrol]
[ <do-construct-name> : ] DO [loopcontrol]
"""
match = re.compile(r'do\b\s*\d*',re.I).match
item_re = re.compile(r'do\b\s*(?P<label>\d*)\s*,?\s*(?P<loopcontrol>.*)\Z',re.I).match
end_stmt_cls = EndDo
name = ''
def tostr(self):
return 'DO %s %s' % (self.endlabel, self.loopcontrol)
def process_item(self):
item = self.item
line = item.get_line()
m = self.item_re(line)
self.endlabel = m.group('label').strip()
self.name = item.label
self.loopcontrol = m.group('loopcontrol').strip()
return BeginStatement.process_item(self)
def process_subitem(self, item):
r = False
if self.endlabel:
label = item.label
if label == self.endlabel:
r = True
if isinstance(self.parent, Do) and label==self.parent.endlabel:
# the same item label may be used for different block ends
self.put_item(item)
return BeginStatement.process_subitem(self, item) or r
def get_classes(self):
return execution_part_construct
class EndType(EndStatement):
"""
END TYPE [<type-name>]
"""
match = re.compile(r'end\s*type\s*\w*\Z', re.I).match
blocktype = 'type'
class Type(BeginStatement):
"""
TYPE [ [, <type-attr-spec-list>] ::] <type-name> [ ( <type-param-name-list> ) ]
<type-attr-spec> = <access-spec> | EXTENDS ( <parent-type-name> )
| ABSTRACT | BIND(C)
"""
match = re.compile(r'type\b\s*').match
end_stmt_cls = EndType
def process_item(self):
line = self.item.get_line()[4:].lstrip()
if line.startswith('('):
self.isvalid = False
return
attr_specs = []
i = line.find('::')
if i!=-1:
for s in line[:i].split(','):
s = s.strip()
if s: attr_specs.append(s)
line = line[i+2:].lstrip()
self.attr_specs = attr_specs
i = line.find('(')
if i!=-1:
self.name = line[:i].rstrip()
assert line[-1]==')',`line`
self.params = line[i+1:-1].lstrip()
else:
self.name = line
self.params = ''
return BeginStatement.process_item(self)
def tostr(self):
s = 'TYPE'
if self.attr_specs:
s += ', '.join(['']+self.attr_specs) + ' ::'
s += ' ' + self.name
if self.params:
s += ' ('+self.params+')'
return s
def get_classes(self):
return [Integer] + private_or_sequence + component_part + type_bound_procedure_part
TypeDecl = Type
###################################################
from statements import *
from typedecl_statements import *
access_spec = [Public, Private]
interface_specification = [Function, Subroutine,
ModuleProcedure
]
module_subprogram_part = [
Contains,
Function,
Subroutine
]
specification_stmt = [
# Access, Allocatable, Asynchronous, Bind, Common,
Data, Dimension,
Equivalence, #External, Intent
# Intrinsic, Namelist, Optional, Pointer, Protected,
Save, #Target, Volatile, Value
]
intrinsic_type_spec = [
Integer , Real, DoublePrecision, Complex, Character, Logical
]
declaration_type_spec = intrinsic_type_spec + [
TypeStmt,
Class
]
type_declaration_stmt = declaration_type_spec
private_or_sequence = [
Private, #Sequence
]
component_part = declaration_type_spec + [
#Procedure
]
type_bound_procedure_part = [
Contains, Private, #Procedure, Generic, Final
]
#R214
action_stmt = [
Allocate,
Assignment, #PointerAssignment,
Backspace,
Call,
Close,
Continue,
Cycle,
Deallocate,
Endfile, #EndFunction, EndProgram, EndSubroutine,
Exit,
# Flush, Forall,
Goto, If, #Inquire,
Nullify,
Open,
Print, Read,
Return,
Rewind,
Stop, #Wait, Where,
Write,
# arithmetic-if-stmt, computed-goto-stmt
]
executable_construct = action_stmt + [
# Associate, Case,
Do,
# Forall,
IfThen,
Select, #Where
]
execution_part_construct = executable_construct + [
Format, #Entry, Data
]
execution_part = execution_part_construct[:]
#C201, R208
for cls in [EndFunction, EndProgram, EndSubroutine]:
try: execution_part.remove(cls)
except ValueError: pass
internal_subprogram = [Function, Subroutine]
internal_subprogram_part = [
Contains,
] + internal_subprogram
declaration_construct = [
TypeDecl, #Entry, Enum,
Format,
Interface,
Parameter, #Procedure,
] + specification_stmt + type_declaration_stmt # stmt-function-stmt
implicit_part = [
Implicit, Parameter, Format, #Entry
]
specification_part = [
Use, #Import
] + implicit_part + declaration_construct
external_subprogram = [Function, Subroutine]
main_program = [Program] + specification_part + execution_part + internal_subprogram_part
program_unit = main_program + external_subprogram + [Module,
#BlockData
]
|