fortio-namelist 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +64 -0
- data/Rakefile +12 -0
- data/fortio-namelist.gemspec +23 -0
- data/lib/fortio-namelist.rb +4 -0
- data/lib/fortio-namelist/fortran_namelist.rb +259 -0
- data/lib/fortio-namelist/fortran_namelist.tab.rb +661 -0
- data/lib/fortio-namelist/fortran_namelist.y +325 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: eec83d1e80ab4d715f85a1e51d57a233995612e323f2729018756cd773b907e2
|
4
|
+
data.tar.gz: 97f524e2c0cfaae551c79fa48154c58e3ec5c1871f7e3544779fa49bc1333714
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cf29f5ab82c904800ced9c0e4a61672e536fc9ba58c9e30b96b0ed4ffbed6b4b2a9b371a3f5ce6ec5c5fbc9f6af7269a9661aeb45f1f46a5c1a3653316d313f5
|
7
|
+
data.tar.gz: 32101d550a483bd675f2b350f022357d4784ded6bdc944cc92449f6e6c9aaa53e1a41ea26b6816284d2b96777ade58fad0f75275964089d1e87d83a2b44a89ee
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
fortio-namelist
|
2
|
+
===============
|
3
|
+
|
4
|
+
This is a library for reading and writing data in Fortran namelist format. With this library, you can read Fortran namelist format data from Ruby and convert it to Hash objects, and vice versa. Once the namelist format is read as a Hash object, it is easy to modify the contents or convert it to another format such as JSON, YAML, etc. When converting from a Hash object to namelist format, options can be specified to specify the case of variable names and the format of logical values and floating point numbers. The namelist format is often used as a configuration file to control a calculation program using Fortran. This library can be especially useful for batch processing by sequentially changing the settings of a calculation program. It can also improve the appearance of the namelist output from a Fortran program to make it easier to check. Since the namelist format and JSON (or YAML) can be converted to each other, it is also a good idea to use it to create web interfaces that facilitate the configuration of programs.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
gem install fortio-namelist
|
10
|
+
|
11
|
+
To use the library in your Ruby script,
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require "fortio-namelist"
|
15
|
+
```
|
16
|
+
|
17
|
+
Description
|
18
|
+
-----------
|
19
|
+
|
20
|
+
# Reading namelist data
|
21
|
+
|
22
|
+
FortIO::Namelist.read(input, name: nil)
|
23
|
+
|
24
|
+
# Generate namelist format from Hash object
|
25
|
+
FortIO::Namelist.generate(hash, name: "namelist", array_format: 'stream')
|
26
|
+
|
27
|
+
# 複数のデータセットを含むHashからnamelist形式を生成する
|
28
|
+
FortIO::Namelist.dump(root, **format_options)
|
29
|
+
|
30
|
+
# namelistデータを読み込み、blockで評価して、結果をnamelistに変換する
|
31
|
+
FortIO::Namelist.filter(input, **format_options) { |hash| ... }
|
32
|
+
|
33
|
+
# generate, dump, filterが受け付けるオプション
|
34
|
+
|
35
|
+
array_style: 'index'
|
36
|
+
'stream'
|
37
|
+
|
38
|
+
logical_format: 'normal'
|
39
|
+
'short'
|
40
|
+
|
41
|
+
float_format: 'normal'
|
42
|
+
'd0'
|
43
|
+
|
44
|
+
alignment: 'compact'
|
45
|
+
'left'
|
46
|
+
'right'
|
47
|
+
Integer
|
48
|
+
|
49
|
+
uppercase: false
|
50
|
+
true
|
51
|
+
|
52
|
+
comma: false
|
53
|
+
true
|
54
|
+
|
55
|
+
slash: true
|
56
|
+
false
|
57
|
+
|
58
|
+
indent: ' '*2
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
GEMSPEC = "fortio-namelist.gemspec"
|
2
|
+
|
3
|
+
task :install do
|
4
|
+
spec = eval File.read(GEMSPEC)
|
5
|
+
system %{
|
6
|
+
racc lib/fortio-namelist/fortran_namelist.y
|
7
|
+
gem build #{GEMSPEC}; gem install #{spec.full_name}.gem
|
8
|
+
}
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'rspec/core/rake_task'
|
12
|
+
RSpec::Core::RakeTask.new
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Gem::Specification::new do |s|
|
2
|
+
version = "1.0.0"
|
3
|
+
files = Dir.glob("**/*") - [
|
4
|
+
Dir.glob("fortio-namelist-*.gem"),
|
5
|
+
Dir.glob("test/**/*"),
|
6
|
+
Dir.glob("work/**/*"),
|
7
|
+
].flatten
|
8
|
+
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.name = "fortio-namelist"
|
11
|
+
s.summary = "A library for reading/writing fortran namelist file"
|
12
|
+
s.description = <<-HERE
|
13
|
+
A library for reading/writing fortran namelist file
|
14
|
+
HERE
|
15
|
+
s.version = version
|
16
|
+
s.licenses = ['MIT']
|
17
|
+
s.author = "Hiroki Motoyoshi"
|
18
|
+
s.email = ""
|
19
|
+
s.homepage = 'https://github.com/himotoyoshi/fortio-namelist'
|
20
|
+
s.files = files
|
21
|
+
s.required_ruby_version = ">= 1.8.1"
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,259 @@
|
|
1
|
+
# ----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# fortran_namelist.rb
|
4
|
+
#
|
5
|
+
# This file is part of simple-fortio library.
|
6
|
+
#
|
7
|
+
# Copyright (C) 2005-2021 Hiroki Motoyoshi
|
8
|
+
#
|
9
|
+
# ----------------------------------------------------------------------------
|
10
|
+
|
11
|
+
require_relative "fortran_namelist.tab"
|
12
|
+
|
13
|
+
module FortIO::Namelist
|
14
|
+
|
15
|
+
class Parser
|
16
|
+
|
17
|
+
ParamDef = Struct.new(:ident, :array_spec, :rval)
|
18
|
+
|
19
|
+
class ParamDef
|
20
|
+
|
21
|
+
def set (hash)
|
22
|
+
case hash[ident]
|
23
|
+
when Array
|
24
|
+
case array_spec
|
25
|
+
when nil
|
26
|
+
if rval.is_a?(Array) and rval.size == 1
|
27
|
+
hash[ident][0] = rval.first
|
28
|
+
else
|
29
|
+
hash[ident].clear
|
30
|
+
hash[ident].push(*rval)
|
31
|
+
end
|
32
|
+
else
|
33
|
+
if array_spec.first.is_a?(Integer)
|
34
|
+
if rval.size == 1
|
35
|
+
hash[ident][*array_spec] = rval.first
|
36
|
+
else
|
37
|
+
idx = array_spec.first
|
38
|
+
hash[ident][idx..idx+rval.size-1] = *rval
|
39
|
+
end
|
40
|
+
else
|
41
|
+
hash[ident][*array_spec] = rval
|
42
|
+
end
|
43
|
+
end
|
44
|
+
else
|
45
|
+
if array_spec
|
46
|
+
hash[ident] = []
|
47
|
+
set(hash)
|
48
|
+
else
|
49
|
+
if rval.is_a?(Array) and rval.size == 1
|
50
|
+
hash[ident] = rval.first
|
51
|
+
else
|
52
|
+
hash[ident] = rval
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def inspect
|
59
|
+
if array_spec
|
60
|
+
return "#{ident}(#{array_spec.inspect}) = #{rval.inspect}"
|
61
|
+
else
|
62
|
+
return "#{ident} = #{rval}"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
class Reader
|
70
|
+
|
71
|
+
def initialize (text)
|
72
|
+
@namelist = FortIO::Namelist::Parser.new.parse(text)
|
73
|
+
end
|
74
|
+
|
75
|
+
def read (name, out={})
|
76
|
+
name = name.downcase
|
77
|
+
raise "no definition of namelist '#{name}'" unless nml = @namelist[name]
|
78
|
+
nml.each do |paramdef|
|
79
|
+
paramdef.set(out)
|
80
|
+
end
|
81
|
+
return out
|
82
|
+
end
|
83
|
+
|
84
|
+
def read_all
|
85
|
+
all = {}
|
86
|
+
@namelist.each do |name, nml|
|
87
|
+
all[name] = {}
|
88
|
+
read(name, all[name])
|
89
|
+
end
|
90
|
+
return all
|
91
|
+
end
|
92
|
+
|
93
|
+
attr_reader :namelist
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
#
|
99
|
+
# class methods of FortIO::Namelist
|
100
|
+
#
|
101
|
+
|
102
|
+
#
|
103
|
+
# FortIO::Namelist.dump(hash, name: "namelist")
|
104
|
+
#
|
105
|
+
# hash -> namelist converter
|
106
|
+
#
|
107
|
+
|
108
|
+
def self.format_element (value, logical_format: 'normal', float_format: 'normal', uppercase: false)
|
109
|
+
case value
|
110
|
+
when String
|
111
|
+
if value !~ /'/
|
112
|
+
return "'" + value + "'"
|
113
|
+
else
|
114
|
+
return '"' + value.gsub(/"/, '""') + '"'
|
115
|
+
end
|
116
|
+
when Float
|
117
|
+
d = uppercase ? "D" : "d"
|
118
|
+
case float_format
|
119
|
+
when 'normal'
|
120
|
+
return ("%g" % value).sub(/e/, d)
|
121
|
+
when 'd0'
|
122
|
+
value = ("%g" % value)
|
123
|
+
return ( value =~ /e/ ) ? value.sub(/e/, d) : value + d + "0"
|
124
|
+
when 'exp'
|
125
|
+
return ("%e" % value).sub(/e/, d)
|
126
|
+
else
|
127
|
+
raise "invalid float_format"
|
128
|
+
end
|
129
|
+
when Complex
|
130
|
+
format("(%s,%s)",
|
131
|
+
format_element(value.real, float_format: float_format),
|
132
|
+
format_element(value.imag, float_format: float_format))
|
133
|
+
when TrueClass
|
134
|
+
case logical_format
|
135
|
+
when 'normal'
|
136
|
+
return uppercase ? ".TRUE." : ".true."
|
137
|
+
when 'short'
|
138
|
+
return uppercase ? "T" : "t"
|
139
|
+
else
|
140
|
+
raise "invalid logical_format"
|
141
|
+
end
|
142
|
+
when FalseClass
|
143
|
+
case logical_format
|
144
|
+
when 'normal'
|
145
|
+
return uppercase ? ".FALSE." : ".false."
|
146
|
+
when 'short'
|
147
|
+
return uppercase ? "F" : "f"
|
148
|
+
else
|
149
|
+
raise "invalid logical_format"
|
150
|
+
end
|
151
|
+
else
|
152
|
+
return value.to_s
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def self.generate (hash,
|
157
|
+
name: "namelist",
|
158
|
+
array_style: "stream",
|
159
|
+
alignment: "left",
|
160
|
+
uppercase: false,
|
161
|
+
comma: false,
|
162
|
+
slash: true,
|
163
|
+
indent: ' ',
|
164
|
+
**format_options)
|
165
|
+
format_options[:uppercase] = uppercase
|
166
|
+
list = []
|
167
|
+
hash.each do |ident, value|
|
168
|
+
case value
|
169
|
+
when Array
|
170
|
+
case array_style
|
171
|
+
when "index"
|
172
|
+
value.each_with_index do |e, i|
|
173
|
+
if e
|
174
|
+
list << ["#{ident}(#{i+1})", format_element(e, **format_options)]
|
175
|
+
end
|
176
|
+
end
|
177
|
+
when "stream"
|
178
|
+
list << [ident, value.map{ |e| format_element(e, **format_options) }.join(", ")]
|
179
|
+
else
|
180
|
+
raise "unknown array style (should be 'index' or 'stream')"
|
181
|
+
end
|
182
|
+
else
|
183
|
+
list << [ident, format_element(value, **format_options)]
|
184
|
+
end
|
185
|
+
end
|
186
|
+
if uppercase
|
187
|
+
list = list.map{|ident,value| [ident.upcase, value]}
|
188
|
+
name = name.upcase
|
189
|
+
end
|
190
|
+
if comma
|
191
|
+
nl = ",\n"
|
192
|
+
else
|
193
|
+
nl = "\n"
|
194
|
+
end
|
195
|
+
case alignment
|
196
|
+
when "none"
|
197
|
+
body = list.map { |ident, value|
|
198
|
+
format("%s%s = %s", indent, ident, value)
|
199
|
+
}.join(nl)
|
200
|
+
when "left"
|
201
|
+
ident_maxlen = list.map{|ident,value| ident.length}.max
|
202
|
+
body = list.map { |ident, value|
|
203
|
+
format("%s%-#{ident_maxlen}s = %s", indent, ident, value)
|
204
|
+
}.join(nl)
|
205
|
+
when "right"
|
206
|
+
ident_maxlen = list.map{|ident,value| ident.length}.max
|
207
|
+
body = list.map { |ident, value|
|
208
|
+
format("%s%#{ident_maxlen}s = %s", indent, ident, value)
|
209
|
+
}.join(nl)
|
210
|
+
when Integer
|
211
|
+
body = list.map { |ident, value|
|
212
|
+
format("%s%-#{alignment-2}s = %s", indent, ident, value)
|
213
|
+
}.join(nl)
|
214
|
+
else
|
215
|
+
raise "unknown enum format (should be 'normal' 'left' 'right' 'stream')"
|
216
|
+
end
|
217
|
+
if slash
|
218
|
+
tail = "/"
|
219
|
+
else
|
220
|
+
tail = "&end"
|
221
|
+
end
|
222
|
+
return ["&#{name}", body, tail, ""].join("\n")
|
223
|
+
end
|
224
|
+
|
225
|
+
def self.dump (root, **format_options)
|
226
|
+
return root.map { |name, hash| generate(hash, name: name, **format_options) }.join
|
227
|
+
end
|
228
|
+
|
229
|
+
#
|
230
|
+
# FortIO::Namelist.read(input, name: nil)
|
231
|
+
#
|
232
|
+
def self.read (input, name: nil)
|
233
|
+
case input
|
234
|
+
when String
|
235
|
+
text = input
|
236
|
+
else
|
237
|
+
text = input.read
|
238
|
+
end
|
239
|
+
if name
|
240
|
+
return FortIO::Namelist::Reader.new(text).read(name)
|
241
|
+
else
|
242
|
+
return FortIO::Namelist::Reader.new(text).read_all()
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
#
|
247
|
+
# FortIO::Namelist.filter(input) { |hash| MODIFYING HASH }
|
248
|
+
#
|
249
|
+
# input : namelist string
|
250
|
+
# return value : namelist string
|
251
|
+
#
|
252
|
+
def self.filter (input, **format_options)
|
253
|
+
config = read(input)
|
254
|
+
yield config
|
255
|
+
return dump(config, **format_options)
|
256
|
+
end
|
257
|
+
|
258
|
+
end
|
259
|
+
|
@@ -0,0 +1,661 @@
|
|
1
|
+
#
|
2
|
+
# DO NOT MODIFY!!!!
|
3
|
+
# This file is automatically generated by Racc 1.4.16
|
4
|
+
# from Racc grammar file "".
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'racc/parser.rb'
|
8
|
+
|
9
|
+
|
10
|
+
require "strscan"
|
11
|
+
require "stringio"
|
12
|
+
|
13
|
+
module FortIO
|
14
|
+
end
|
15
|
+
|
16
|
+
module FortIO::Namelist
|
17
|
+
|
18
|
+
class Scanner
|
19
|
+
|
20
|
+
def initialize (text)
|
21
|
+
@s = StringScanner.new(text)
|
22
|
+
@in_namelist = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
attr_accessor :in_namelist
|
26
|
+
|
27
|
+
def debug_info
|
28
|
+
lines = @s.string.split(/\n/)
|
29
|
+
lineno = @s.string[0...@s.pos].split(/\n/).size
|
30
|
+
info = ""
|
31
|
+
if lineno > 1
|
32
|
+
info << format(" %4i: %s\n", lineno-1, lines[lineno-2])
|
33
|
+
end
|
34
|
+
info << format(">> %4i: %s\n", lineno, lines[lineno-1])
|
35
|
+
if lineno <= lines.size - 1
|
36
|
+
info << format(" %4i: %s\n", lineno+1, lines[lineno])
|
37
|
+
end
|
38
|
+
info
|
39
|
+
end
|
40
|
+
|
41
|
+
def yylex
|
42
|
+
while @s.rest?
|
43
|
+
unless @in_namelist
|
44
|
+
case
|
45
|
+
when @s.scan(/\A([\$&])/) ### {$|&}
|
46
|
+
@in_namelist = "dummy"
|
47
|
+
return [
|
48
|
+
@s[0],
|
49
|
+
nil
|
50
|
+
]
|
51
|
+
when @s.scan(/\A[^\$&]/)
|
52
|
+
next
|
53
|
+
end
|
54
|
+
else
|
55
|
+
case
|
56
|
+
when @s.scan(/\A[+-]?(\d+)\.(\d+)?([ED][+-]?(\d+))?/i) ### float
|
57
|
+
return [ ### 1.2E+3, 1.E+3, 1.2E3
|
58
|
+
:FLOAT, ### 1.2, 1.
|
59
|
+
@s[0].sub(/D/i,'e').sub(/\.e/,".0e").to_f
|
60
|
+
]
|
61
|
+
when @s.scan(/\A[+-]?\.(\d+)([ED][+-]?(\d+))?/i) ### float
|
62
|
+
return [ ### .2E+3, -.2E+3, .2E3
|
63
|
+
:FLOAT, ### .2, -.2
|
64
|
+
@s[0].sub(/D/i,'e').sub(/\./, '0.').to_f
|
65
|
+
]
|
66
|
+
when @s.scan(/\A[+-]?(\d+)[ED][+-]?(\d+)/i) ### float
|
67
|
+
return [ ### 12E+3, 12E3, 0E0
|
68
|
+
:FLOAT,
|
69
|
+
@s[0].sub(/D/i,'e').to_f
|
70
|
+
]
|
71
|
+
when @s.scan(/\A\d+[a-z_]\w*/i) ### STRING
|
72
|
+
return [
|
73
|
+
:STRING,
|
74
|
+
@s[0]
|
75
|
+
]
|
76
|
+
when @s.scan(/\A[\-\+]?\d+/) ### digits
|
77
|
+
return [
|
78
|
+
:DIGITS,
|
79
|
+
Integer(@s[0])
|
80
|
+
]
|
81
|
+
when @s.scan(/\A'((?:''|[^'])*)'/) ### 'quoted string'
|
82
|
+
return [
|
83
|
+
:STRING,
|
84
|
+
@s[1].gsub(/''/, "'")
|
85
|
+
]
|
86
|
+
when @s.scan(/\A"((?:""|[^"])*)"/) ### 'double-quoted string'
|
87
|
+
return [
|
88
|
+
:STRING,
|
89
|
+
@s[1].gsub(/""/, '"')
|
90
|
+
]
|
91
|
+
when @s.scan(/\A,/) ### ,
|
92
|
+
@s.scan(/\A[ \t]+/)
|
93
|
+
while @s.scan(/\A\n[ \t]*/) or @s.scan(/\A\![^\n]*/)
|
94
|
+
### skip comment
|
95
|
+
end
|
96
|
+
if @s.scan(/\A\&[ \t]*\n[ \t]*\&/) ### & &
|
97
|
+
return [
|
98
|
+
',',
|
99
|
+
nil
|
100
|
+
]
|
101
|
+
elsif @s.match?(/\A[a-z]\w*/i) or @s.match?(/\A[\&\$\/\!]/)
|
102
|
+
return [
|
103
|
+
:COMMA,
|
104
|
+
nil
|
105
|
+
]
|
106
|
+
elsif @s.match?(/\A,/)
|
107
|
+
return [
|
108
|
+
:NIL,
|
109
|
+
nil
|
110
|
+
]
|
111
|
+
else
|
112
|
+
return [
|
113
|
+
',',
|
114
|
+
nil
|
115
|
+
]
|
116
|
+
end
|
117
|
+
when @s.scan(/\A\&[ \t]*\n[ \t]*\&/) ### & &
|
118
|
+
next
|
119
|
+
when @s.scan(/\A[\$&\/=\(\):*]/) ### {$|&|/|,|=|(|)|:|*}
|
120
|
+
return [
|
121
|
+
@s[0],
|
122
|
+
nil
|
123
|
+
]
|
124
|
+
when @s.scan(/\A_\w*/i) ### STRING
|
125
|
+
return [
|
126
|
+
:STRING,
|
127
|
+
@s[0]
|
128
|
+
]
|
129
|
+
when @s.scan(/\A\.t.*?\./i) ### LOGICAL true
|
130
|
+
return [
|
131
|
+
:LOGICAL,
|
132
|
+
true,
|
133
|
+
]
|
134
|
+
when @s.scan(/\A\.f.*?\./i) ### LOGICAL false
|
135
|
+
return [
|
136
|
+
:LOGICAL,
|
137
|
+
false,
|
138
|
+
]
|
139
|
+
when @s.match?(/\At[^\w]/i) ### LOGICAL true
|
140
|
+
@s.scan(/\At/i)
|
141
|
+
ms = @s[0]
|
142
|
+
if @s.match?(/\A[ \t]*=/)
|
143
|
+
return [
|
144
|
+
:IDENT,
|
145
|
+
ms
|
146
|
+
]
|
147
|
+
else
|
148
|
+
return [
|
149
|
+
:LOGICAL,
|
150
|
+
true,
|
151
|
+
]
|
152
|
+
end
|
153
|
+
when @s.match?(/\Af[^\w]/i) ### LOGICAL false
|
154
|
+
@s.scan(/\Af/i)
|
155
|
+
ms = @s[0]
|
156
|
+
if @s.match?(/\A[ \t]*=/)
|
157
|
+
return [
|
158
|
+
:IDENT,
|
159
|
+
ms
|
160
|
+
]
|
161
|
+
else
|
162
|
+
return [
|
163
|
+
:LOGICAL,
|
164
|
+
false,
|
165
|
+
]
|
166
|
+
end
|
167
|
+
when @s.scan(/\A[a-z]\w*/i) ### IDENT or LOGICAL
|
168
|
+
return [
|
169
|
+
:IDENT,
|
170
|
+
@s[0]
|
171
|
+
]
|
172
|
+
when @s.scan(/\A\n/) ### newline
|
173
|
+
return [
|
174
|
+
:NL,
|
175
|
+
nil
|
176
|
+
]
|
177
|
+
next
|
178
|
+
when @s.scan(/\A[ \t]+/) ### blank
|
179
|
+
next
|
180
|
+
when @s.scan(/\A![^\n]*?\n/) ### comment
|
181
|
+
next
|
182
|
+
else
|
183
|
+
@s.rest =~ /\A(.*)$/
|
184
|
+
raise "namelist parse error ('#{$1}')\n" + debug_info
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
module FortIO
|
194
|
+
module Namelist
|
195
|
+
class Parser < Racc::Parser
|
196
|
+
|
197
|
+
module_eval(<<'...end fortran_namelist.y/module_eval...', 'fortran_namelist.y', 115)
|
198
|
+
|
199
|
+
def parse (str)
|
200
|
+
@scan = FortIO::Namelist::Scanner.new(str)
|
201
|
+
@root = {}
|
202
|
+
begin
|
203
|
+
@yydebug = true
|
204
|
+
do_parse
|
205
|
+
rescue Racc::ParseError => err
|
206
|
+
message = ""
|
207
|
+
message << "namelist " << err.message[1..-1]
|
208
|
+
if @scan.in_namelist and @scan.in_namelist != "dummy"
|
209
|
+
message << " in &#{@scan.in_namelist} ... &end"
|
210
|
+
end
|
211
|
+
message << "\n"
|
212
|
+
message << @scan.debug_info
|
213
|
+
raise RuntimeError, message
|
214
|
+
end
|
215
|
+
return @root
|
216
|
+
end
|
217
|
+
|
218
|
+
def next_token
|
219
|
+
return @scan.yylex
|
220
|
+
end
|
221
|
+
|
222
|
+
...end fortran_namelist.y/module_eval...
|
223
|
+
##### State transition tables begin ###
|
224
|
+
|
225
|
+
racc_action_table = [
|
226
|
+
19, 20, 30, 49, 54, 40, 31, 50, 27, 38,
|
227
|
+
48, 33, 40, 34, 35, 36, 45, 44, 53, 4,
|
228
|
+
5, 24, 23, 46, 38, 45, 33, 25, 34, 35,
|
229
|
+
36, 59, 46, 38, 60, 33, 61, 34, 35, 36,
|
230
|
+
45, 44, 49, 4, 5, 40, 50, 46, 38, 17,
|
231
|
+
33, 16, 34, 35, 36, 45, 44, 65, 66, 7,
|
232
|
+
52, nil, 46, 38, nil, 33, 30, 34, 35, 36,
|
233
|
+
31, nil, 42, 38, nil, 33, 30, 34, 35, 36,
|
234
|
+
31, nil, 42, 38, nil, 33, 30, 34, 35, 36,
|
235
|
+
31, nil, 42, 38, nil, 33, 30, 34, 35, 36,
|
236
|
+
31, nil, 42, 38, nil, 33, nil, 34, 35, 36,
|
237
|
+
19, 20, nil, nil, 4, 5, 15, 12, 38, nil,
|
238
|
+
33, nil, 34, 35, 36, 38, nil, 58, nil, 34,
|
239
|
+
35, 36, 38, nil, 33, nil, 34, 35, 36, 11,
|
240
|
+
nil, nil, 4, 5, 15, 12 ]
|
241
|
+
|
242
|
+
racc_action_check = [
|
243
|
+
24, 24, 24, 38, 40, 65, 24, 38, 24, 24,
|
244
|
+
33, 24, 25, 24, 24, 24, 64, 64, 40, 0,
|
245
|
+
0, 15, 13, 64, 64, 47, 64, 15, 64, 64,
|
246
|
+
64, 51, 47, 47, 52, 47, 53, 47, 47, 47,
|
247
|
+
28, 28, 59, 2, 2, 54, 59, 28, 28, 7,
|
248
|
+
28, 6, 28, 28, 28, 41, 41, 61, 63, 1,
|
249
|
+
39, nil, 41, 41, nil, 41, 27, 41, 41, 41,
|
250
|
+
27, nil, 27, 27, nil, 27, 30, 27, 27, 27,
|
251
|
+
30, nil, 30, 30, nil, 30, 42, 30, 30, 30,
|
252
|
+
42, nil, 42, 42, nil, 42, 60, 42, 42, 42,
|
253
|
+
60, nil, 60, 60, nil, 60, nil, 60, 60, 60,
|
254
|
+
10, 10, nil, nil, 10, 10, 10, 10, 44, nil,
|
255
|
+
44, nil, 44, 44, 44, 48, nil, 48, nil, 48,
|
256
|
+
48, 48, 45, nil, 45, nil, 45, 45, 45, 3,
|
257
|
+
nil, nil, 3, 3, 3, 3 ]
|
258
|
+
|
259
|
+
racc_action_pointer = [
|
260
|
+
13, 59, 37, 136, nil, nil, 43, 49, nil, nil,
|
261
|
+
108, nil, nil, 14, nil, 16, nil, nil, nil, nil,
|
262
|
+
nil, nil, nil, nil, -2, -1, nil, 62, 37, nil,
|
263
|
+
72, nil, nil, -4, nil, nil, nil, nil, -10, 48,
|
264
|
+
0, 52, 82, nil, 107, 121, nil, 22, 114, nil,
|
265
|
+
nil, 27, 29, 23, 32, nil, nil, nil, nil, 29,
|
266
|
+
92, 53, nil, 46, 13, -8, nil, nil ]
|
267
|
+
|
268
|
+
racc_action_default = [
|
269
|
+
-1, -44, -1, -44, -6, -7, -44, -44, -3, -4,
|
270
|
+
-44, -9, -10, -44, -14, -44, -8, 68, -5, -12,
|
271
|
+
-13, -15, -16, -11, -44, -44, -17, -18, -19, -21,
|
272
|
+
-44, -29, -30, -34, -32, -33, -35, -36, -44, -44,
|
273
|
+
-40, -26, -44, -22, -44, -28, -25, -27, -44, -37,
|
274
|
+
-38, -44, -44, -44, -44, -23, -24, -31, -34, -44,
|
275
|
+
-44, -41, -42, -44, -20, -44, -39, -43 ]
|
276
|
+
|
277
|
+
racc_goto_table = [
|
278
|
+
39, 43, 28, 51, 14, 22, 9, 57, 47, 13,
|
279
|
+
1, 21, 8, 18, 43, 10, 13, 55, 56, 26,
|
280
|
+
43, nil, nil, nil, 63, nil, nil, nil, nil, 62,
|
281
|
+
nil, nil, nil, nil, nil, nil, nil, 43, 64, nil,
|
282
|
+
67 ]
|
283
|
+
|
284
|
+
racc_goto_check = [
|
285
|
+
10, 11, 9, 14, 8, 7, 4, 12, 9, 6,
|
286
|
+
1, 8, 1, 4, 11, 5, 6, 11, 11, 7,
|
287
|
+
11, nil, nil, nil, 14, nil, nil, nil, nil, 10,
|
288
|
+
nil, nil, nil, nil, nil, nil, nil, 11, 9, nil,
|
289
|
+
10 ]
|
290
|
+
|
291
|
+
racc_goto_pointer = [
|
292
|
+
nil, 10, nil, nil, 3, 12, 6, -5, 1, -22,
|
293
|
+
-25, -27, -41, nil, -35 ]
|
294
|
+
|
295
|
+
racc_goto_default = [
|
296
|
+
nil, nil, 2, 3, nil, nil, 6, nil, nil, 41,
|
297
|
+
nil, 29, 32, 37, nil ]
|
298
|
+
|
299
|
+
racc_reduce_table = [
|
300
|
+
0, 0, :racc_error,
|
301
|
+
0, 20, :_reduce_none,
|
302
|
+
1, 20, :_reduce_none,
|
303
|
+
2, 20, :_reduce_none,
|
304
|
+
2, 21, :_reduce_4,
|
305
|
+
3, 21, :_reduce_5,
|
306
|
+
1, 25, :_reduce_none,
|
307
|
+
1, 25, :_reduce_none,
|
308
|
+
2, 22, :_reduce_8,
|
309
|
+
2, 22, :_reduce_none,
|
310
|
+
1, 23, :_reduce_none,
|
311
|
+
2, 23, :_reduce_11,
|
312
|
+
1, 26, :_reduce_none,
|
313
|
+
1, 26, :_reduce_none,
|
314
|
+
1, 24, :_reduce_14,
|
315
|
+
2, 24, :_reduce_15,
|
316
|
+
2, 24, :_reduce_16,
|
317
|
+
3, 27, :_reduce_17,
|
318
|
+
3, 27, :_reduce_18,
|
319
|
+
3, 27, :_reduce_19,
|
320
|
+
6, 27, :_reduce_20,
|
321
|
+
1, 28, :_reduce_21,
|
322
|
+
2, 28, :_reduce_22,
|
323
|
+
3, 28, :_reduce_23,
|
324
|
+
3, 28, :_reduce_24,
|
325
|
+
2, 28, :_reduce_25,
|
326
|
+
2, 28, :_reduce_26,
|
327
|
+
2, 28, :_reduce_27,
|
328
|
+
2, 28, :_reduce_28,
|
329
|
+
1, 28, :_reduce_29,
|
330
|
+
1, 30, :_reduce_30,
|
331
|
+
3, 30, :_reduce_31,
|
332
|
+
1, 31, :_reduce_none,
|
333
|
+
1, 31, :_reduce_none,
|
334
|
+
1, 31, :_reduce_none,
|
335
|
+
1, 31, :_reduce_none,
|
336
|
+
1, 31, :_reduce_none,
|
337
|
+
1, 33, :_reduce_none,
|
338
|
+
1, 33, :_reduce_none,
|
339
|
+
5, 32, :_reduce_39,
|
340
|
+
1, 29, :_reduce_40,
|
341
|
+
3, 29, :_reduce_41,
|
342
|
+
3, 29, :_reduce_42,
|
343
|
+
5, 29, :_reduce_43 ]
|
344
|
+
|
345
|
+
racc_reduce_n = 44
|
346
|
+
|
347
|
+
racc_shift_n = 68
|
348
|
+
|
349
|
+
racc_token_table = {
|
350
|
+
false => 0,
|
351
|
+
:error => 1,
|
352
|
+
:COMMA => 2,
|
353
|
+
:NL => 3,
|
354
|
+
"," => 4,
|
355
|
+
"=" => 5,
|
356
|
+
"&" => 6,
|
357
|
+
"$" => 7,
|
358
|
+
:IDENT => 8,
|
359
|
+
"/" => 9,
|
360
|
+
:NIL => 10,
|
361
|
+
"(" => 11,
|
362
|
+
")" => 12,
|
363
|
+
:DIGITS => 13,
|
364
|
+
"*" => 14,
|
365
|
+
:STRING => 15,
|
366
|
+
:LOGICAL => 16,
|
367
|
+
:FLOAT => 17,
|
368
|
+
":" => 18 }
|
369
|
+
|
370
|
+
racc_nt_base = 19
|
371
|
+
|
372
|
+
racc_use_result_var = true
|
373
|
+
|
374
|
+
Racc_arg = [
|
375
|
+
racc_action_table,
|
376
|
+
racc_action_check,
|
377
|
+
racc_action_default,
|
378
|
+
racc_action_pointer,
|
379
|
+
racc_goto_table,
|
380
|
+
racc_goto_check,
|
381
|
+
racc_goto_default,
|
382
|
+
racc_goto_pointer,
|
383
|
+
racc_nt_base,
|
384
|
+
racc_reduce_table,
|
385
|
+
racc_token_table,
|
386
|
+
racc_shift_n,
|
387
|
+
racc_reduce_n,
|
388
|
+
racc_use_result_var ]
|
389
|
+
|
390
|
+
Racc_token_to_s_table = [
|
391
|
+
"$end",
|
392
|
+
"error",
|
393
|
+
"COMMA",
|
394
|
+
"NL",
|
395
|
+
"\",\"",
|
396
|
+
"\"=\"",
|
397
|
+
"\"&\"",
|
398
|
+
"\"$\"",
|
399
|
+
"IDENT",
|
400
|
+
"\"/\"",
|
401
|
+
"NIL",
|
402
|
+
"\"(\"",
|
403
|
+
"\")\"",
|
404
|
+
"DIGITS",
|
405
|
+
"\"*\"",
|
406
|
+
"STRING",
|
407
|
+
"LOGICAL",
|
408
|
+
"FLOAT",
|
409
|
+
"\":\"",
|
410
|
+
"$start",
|
411
|
+
"namelist_all",
|
412
|
+
"namelist",
|
413
|
+
"header",
|
414
|
+
"tailer",
|
415
|
+
"paramlist",
|
416
|
+
"prefix",
|
417
|
+
"separator",
|
418
|
+
"paramdef",
|
419
|
+
"rvalues",
|
420
|
+
"array_spec",
|
421
|
+
"abbreb",
|
422
|
+
"constant",
|
423
|
+
"complex",
|
424
|
+
"real" ]
|
425
|
+
|
426
|
+
Racc_debug_parser = false
|
427
|
+
|
428
|
+
##### State transition tables end #####
|
429
|
+
|
430
|
+
# reduce 0 omitted
|
431
|
+
|
432
|
+
# reduce 1 omitted
|
433
|
+
|
434
|
+
# reduce 2 omitted
|
435
|
+
|
436
|
+
# reduce 3 omitted
|
437
|
+
|
438
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 31)
|
439
|
+
def _reduce_4(val, _values, result)
|
440
|
+
@root[val[0]] = []; @scan.in_namelist = nil
|
441
|
+
result
|
442
|
+
end
|
443
|
+
.,.,
|
444
|
+
|
445
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 33)
|
446
|
+
def _reduce_5(val, _values, result)
|
447
|
+
@root[val[0]] = val[1]; @scan.in_namelist = nil
|
448
|
+
result
|
449
|
+
end
|
450
|
+
.,.,
|
451
|
+
|
452
|
+
# reduce 6 omitted
|
453
|
+
|
454
|
+
# reduce 7 omitted
|
455
|
+
|
456
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 39)
|
457
|
+
def _reduce_8(val, _values, result)
|
458
|
+
result = val[1].downcase; @scan.in_namelist = val[1].downcase
|
459
|
+
result
|
460
|
+
end
|
461
|
+
.,.,
|
462
|
+
|
463
|
+
# reduce 9 omitted
|
464
|
+
|
465
|
+
# reduce 10 omitted
|
466
|
+
|
467
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 44)
|
468
|
+
def _reduce_11(val, _values, result)
|
469
|
+
raise Racc::ParseError, "\nparse error (&)" unless val[1] =~ /\Aend\Z/i
|
470
|
+
result
|
471
|
+
end
|
472
|
+
.,.,
|
473
|
+
|
474
|
+
# reduce 12 omitted
|
475
|
+
|
476
|
+
# reduce 13 omitted
|
477
|
+
|
478
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 50)
|
479
|
+
def _reduce_14(val, _values, result)
|
480
|
+
result = [val[0]]
|
481
|
+
result
|
482
|
+
end
|
483
|
+
.,.,
|
484
|
+
|
485
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 52)
|
486
|
+
def _reduce_15(val, _values, result)
|
487
|
+
result = val[0] + [val[1]]
|
488
|
+
result
|
489
|
+
end
|
490
|
+
.,.,
|
491
|
+
|
492
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 54)
|
493
|
+
def _reduce_16(val, _values, result)
|
494
|
+
result = val[0]
|
495
|
+
result
|
496
|
+
end
|
497
|
+
.,.,
|
498
|
+
|
499
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 58)
|
500
|
+
def _reduce_17(val, _values, result)
|
501
|
+
result = ParamDef.new(val[0].downcase, nil, "")
|
502
|
+
result
|
503
|
+
end
|
504
|
+
.,.,
|
505
|
+
|
506
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 60)
|
507
|
+
def _reduce_18(val, _values, result)
|
508
|
+
result = ParamDef.new(val[0].downcase, nil, "")
|
509
|
+
result
|
510
|
+
end
|
511
|
+
.,.,
|
512
|
+
|
513
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 62)
|
514
|
+
def _reduce_19(val, _values, result)
|
515
|
+
result = ParamDef.new(val[0].downcase, nil, val[2])
|
516
|
+
result
|
517
|
+
end
|
518
|
+
.,.,
|
519
|
+
|
520
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 64)
|
521
|
+
def _reduce_20(val, _values, result)
|
522
|
+
result = ParamDef.new(val[0].downcase, val[2], val[5])
|
523
|
+
result
|
524
|
+
end
|
525
|
+
.,.,
|
526
|
+
|
527
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 67)
|
528
|
+
def _reduce_21(val, _values, result)
|
529
|
+
result = val[0]
|
530
|
+
result
|
531
|
+
end
|
532
|
+
.,.,
|
533
|
+
|
534
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 69)
|
535
|
+
def _reduce_22(val, _values, result)
|
536
|
+
result = val[0] + val[1]
|
537
|
+
result
|
538
|
+
end
|
539
|
+
.,.,
|
540
|
+
|
541
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 71)
|
542
|
+
def _reduce_23(val, _values, result)
|
543
|
+
result = val[0] + val[2]
|
544
|
+
result
|
545
|
+
end
|
546
|
+
.,.,
|
547
|
+
|
548
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 73)
|
549
|
+
def _reduce_24(val, _values, result)
|
550
|
+
result = val[0] + val[2]
|
551
|
+
result
|
552
|
+
end
|
553
|
+
.,.,
|
554
|
+
|
555
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 75)
|
556
|
+
def _reduce_25(val, _values, result)
|
557
|
+
result = val[0] + [nil]
|
558
|
+
result
|
559
|
+
end
|
560
|
+
.,.,
|
561
|
+
|
562
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 77)
|
563
|
+
def _reduce_26(val, _values, result)
|
564
|
+
result = [nil] + val[1]
|
565
|
+
result
|
566
|
+
end
|
567
|
+
.,.,
|
568
|
+
|
569
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 79)
|
570
|
+
def _reduce_27(val, _values, result)
|
571
|
+
result = [nil] + val[1]
|
572
|
+
result
|
573
|
+
end
|
574
|
+
.,.,
|
575
|
+
|
576
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 81)
|
577
|
+
def _reduce_28(val, _values, result)
|
578
|
+
result = val[0]
|
579
|
+
result
|
580
|
+
end
|
581
|
+
.,.,
|
582
|
+
|
583
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 83)
|
584
|
+
def _reduce_29(val, _values, result)
|
585
|
+
result = val[0]
|
586
|
+
result
|
587
|
+
end
|
588
|
+
.,.,
|
589
|
+
|
590
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 86)
|
591
|
+
def _reduce_30(val, _values, result)
|
592
|
+
result = [val[0]]
|
593
|
+
result
|
594
|
+
end
|
595
|
+
.,.,
|
596
|
+
|
597
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 88)
|
598
|
+
def _reduce_31(val, _values, result)
|
599
|
+
result = [val[2]] * val[0]
|
600
|
+
result
|
601
|
+
end
|
602
|
+
.,.,
|
603
|
+
|
604
|
+
# reduce 32 omitted
|
605
|
+
|
606
|
+
# reduce 33 omitted
|
607
|
+
|
608
|
+
# reduce 34 omitted
|
609
|
+
|
610
|
+
# reduce 35 omitted
|
611
|
+
|
612
|
+
# reduce 36 omitted
|
613
|
+
|
614
|
+
# reduce 37 omitted
|
615
|
+
|
616
|
+
# reduce 38 omitted
|
617
|
+
|
618
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 100)
|
619
|
+
def _reduce_39(val, _values, result)
|
620
|
+
result = Complex(val[1],val[3])
|
621
|
+
result
|
622
|
+
end
|
623
|
+
.,.,
|
624
|
+
|
625
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 103)
|
626
|
+
def _reduce_40(val, _values, result)
|
627
|
+
result = [val[0]-1]
|
628
|
+
result
|
629
|
+
end
|
630
|
+
.,.,
|
631
|
+
|
632
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 105)
|
633
|
+
def _reduce_41(val, _values, result)
|
634
|
+
result = [(val[0]-1)..(val[2]-1)]
|
635
|
+
result
|
636
|
+
end
|
637
|
+
.,.,
|
638
|
+
|
639
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 107)
|
640
|
+
def _reduce_42(val, _values, result)
|
641
|
+
result = [val[0]-1] + val[2]
|
642
|
+
result
|
643
|
+
end
|
644
|
+
.,.,
|
645
|
+
|
646
|
+
module_eval(<<'.,.,', 'fortran_namelist.y', 109)
|
647
|
+
def _reduce_43(val, _values, result)
|
648
|
+
result = [(val[0]-1)..(val[2]-1)] + val[4]
|
649
|
+
result
|
650
|
+
end
|
651
|
+
.,.,
|
652
|
+
|
653
|
+
def _reduce_none(val, _values, result)
|
654
|
+
val[0]
|
655
|
+
end
|
656
|
+
|
657
|
+
end # class Parser
|
658
|
+
end # module Namelist
|
659
|
+
end # module FortIO
|
660
|
+
|
661
|
+
|