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
|
#!/usr/bin/env python
"""
Defines FortranParser.
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
"""
import re
import sys
import traceback
from numpy.distutils.misc_util import yellow_text, red_text
from readfortran import FortranFileReader, FortranStringReader
from block_statements import BeginSource
class FortranParser:
def __init__(self, reader):
self.reader = reader
self.isfix77 = reader.isfix77
def get_item(self):
try:
item = self.reader.next(ignore_comments = True)
return item
except StopIteration:
pass
def put_item(self, item):
self.reader.fifo_item.insert(0, item)
def parse(self):
import init
try:
main = BeginSource(self)
return main
except KeyboardInterrupt:
raise
except:
reader = self.reader
while reader is not None:
message = reader.format_message('FATAL ERROR',
'while processing line',
reader.linecount, reader.linecount)
reader.show_message(message, sys.stdout)
reader = reader.reader
traceback.print_exc(file=sys.stdout)
self.reader.show_message(red_text('STOPPED PARSING'), sys.stdout)
def test_pyf():
string = """
python module foo
interface tere
subroutine bar
real r
end subroutine bar
end interface tere
end python module foo
"""
reader = FortranStringReader(string, True, True)
parser = FortranParser(reader)
block = parser.parse()
print block
def test_free90():
string = """
module foo
subroutine bar
real r
if ( pc_get_lun() .ne. 6) &
write ( pc_get_lun(), '( &
& /, a, /, " p=", i4, " stopping c_flag=", a, &
& /, " print unit=", i8)') &
trim(title), pcpsx_i_pel(), trim(c_flag), pc_get_lun()
if (.true.) then
call smth
end if
aaa : if (.false.) then
else if (a) then aaa
else aaa
end if aaa
hey = 1
end subroutine bar
abstract interface
end interface
end module foo
"""
reader = FortranStringReader(string, True, False)
parser = FortranParser(reader)
block = parser.parse()
print block
def test_f77():
string = """\
program foo
a = 3
end
subroutine bar
end
pure function foo(a)
end
pure real*4 recursive function bar()
end
"""
reader = FortranStringReader(string, False, True)
parser = FortranParser(reader)
block = parser.parse()
print block
def simple_main():
import sys
if not sys.argv[1:]:
return parse_all_f()
for filename in sys.argv[1:]:
reader = FortranFileReader(filename)
print yellow_text('Processing '+filename+' (mode=%r)' % (reader.mode))
parser = FortranParser(reader)
block = parser.parse()
#print block
def profile_main():
import hotshot, hotshot.stats
prof = hotshot.Profile("_parsefortran.prof")
prof.runcall(simple_main)
prof.close()
stats = hotshot.stats.load("_parsefortran.prof")
stats.strip_dirs()
stats.sort_stats('time', 'calls')
stats.print_stats(30)
def parse_all_f():
for filename in open('opt_all_f90.txt'):
filename = filename.strip()
reader = FortranFileReader(filename)
#print yellow_text('Processing '+filename+' (mode=%r)' % (reader.mode))
parser = FortranParser(reader)
block = parser.parse()
if __name__ == "__main__":
#test_f77()
#test_free90()
#test_pyf()
simple_main()
#profile_main()
#parse_all_f()
|