fortio-namelist 1.0.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/NOTE.md +33 -0
- data/README.md +365 -33
- data/fortio-namelist.gemspec +7 -4
- data/lib/fortio-namelist/fortran_namelist.rb +86 -43
- data/lib/fortio-namelist/fortran_namelist.tab.rb +260 -235
- data/lib/fortio-namelist/fortran_namelist.y +103 -70
- data/spec/array_spec.rb +160 -0
- data/spec/dump_spec.rb +51 -0
- data/spec/empty_spec.rb +66 -0
- data/spec/identifier_spec.rb +81 -0
- data/spec/scalar_spec.rb +196 -0
- data/spec/structure_spec.rb +162 -0
- metadata +28 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa95f21c798ef2256d484af31914c2688bd6b210f2a0704b69b13646eb16b338
|
4
|
+
data.tar.gz: 18f9dac9edbe377bc934985b703b34793737476d143c9787852b84564ca011b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a841ab206370ee553dd92850da27009ed9dbd97ce43545843e5ff190afc5ae46407d30440f7f900708088de95c01598b4b0550c17a48b5b69a4ce205fc0fca14
|
7
|
+
data.tar.gz: f9f6da26c88005cd5c49ad8bb6a916d0731e98c0fd34f6f360d7760cb1fc9727209c4b313d33d4c3adfafbee2bcc8bf09c849dcbdb3a1901c2118e540d3e99d8
|
data/NOTE.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
NOTE
|
3
|
+
----
|
4
|
+
|
5
|
+
### グループ名のプレフィックス
|
6
|
+
|
7
|
+
'&'と'$'が使える。グループの終端を表す'end'のプレフィックスも同様であるが、
|
8
|
+
始端と終端のプレフィックスのペアのチェックはしない。
|
9
|
+
|
10
|
+
### 文字列の配列
|
11
|
+
|
12
|
+
v1 = "a", "b", 'c' ! 引用符あり文字列の配列
|
13
|
+
v2 = a, b, c ! 引用符なし文字列の配列
|
14
|
+
v3 = a, 0_b, _c ! 引用符なし文字列の配列
|
15
|
+
|
16
|
+
ただし、引用符あり文字列と引用符なし文字列を混在させることはできない。
|
17
|
+
|
18
|
+
v4 = "a", b, "c" ! エラー
|
19
|
+
v5 = "a", 0_b, c ! エラー
|
20
|
+
|
21
|
+
### 論理型リテラル
|
22
|
+
|
23
|
+
v1 = .true. ! true
|
24
|
+
v2 = .t ! true
|
25
|
+
v3 = .t_1 ! true
|
26
|
+
v4 = t ! true
|
27
|
+
|
28
|
+
v4 = .false. ! false
|
29
|
+
v5 = .f ! false
|
30
|
+
v5 = .f_1 ! false
|
31
|
+
v6 = f ! false
|
32
|
+
|
33
|
+
もちろん大文字でもよい。
|
data/README.md
CHANGED
@@ -1,7 +1,16 @@
|
|
1
1
|
fortio-namelist
|
2
2
|
===============
|
3
3
|
|
4
|
-
This is a library for reading and writing
|
4
|
+
This is a Ruby library for reading and writing Fortran's namelist.
|
5
|
+
This library allows you to read a namelist string as a Hash object,
|
6
|
+
or dump a Hash object to a namelist string.
|
7
|
+
|
8
|
+
Features
|
9
|
+
--------
|
10
|
+
|
11
|
+
* Flexible parsing of (too free) namelist format using Racc
|
12
|
+
* Reading namelist string as a Ruby's Hash object
|
13
|
+
* Converting a Hash object to a namelist string (specifying the format)
|
5
14
|
|
6
15
|
Installation
|
7
16
|
------------
|
@@ -14,51 +23,374 @@ To use the library in your Ruby script,
|
|
14
23
|
require "fortio-namelist"
|
15
24
|
```
|
16
25
|
|
17
|
-
|
18
|
-
|
26
|
+
Usage
|
27
|
+
------
|
28
|
+
|
29
|
+
### Useful methods
|
30
|
+
|
31
|
+
To read and write a namelist, it is sufficient to use only the following two methods.
|
32
|
+
|
33
|
+
FortIO::Namelist.parse(input, group: nil)
|
34
|
+
FortIO::Namelist.dump(root, **format_options)
|
35
|
+
|
36
|
+
### Reading namelist
|
37
|
+
|
38
|
+
To create a Hash object with namelist structure by reading a namelist string, use the following method.
|
39
|
+
|
40
|
+
FortIO::Namelist.parse(input, group: nil)
|
41
|
+
|
42
|
+
The argument `input` is given as a string, but it also accepts objects with a method `#read` which returns a string like an IO object ('duck typing').
|
43
|
+
|
44
|
+
To read only a specific group, give a group name to the keyword argument `group`. If `group` is omitted, all namelist groups included in `input` will be read. To load multiple groups, give an array of group names to `group`.
|
19
45
|
|
20
|
-
|
46
|
+
The Hash object of the return value has a two-level structure as follows.
|
47
|
+
|
48
|
+
{
|
49
|
+
group1: {
|
50
|
+
var11: value11,
|
51
|
+
var12: value12,
|
52
|
+
: :
|
53
|
+
}
|
54
|
+
group2: {
|
55
|
+
var21: value21,
|
56
|
+
var22: value22,
|
57
|
+
: :
|
58
|
+
}
|
59
|
+
:
|
60
|
+
:
|
61
|
+
}
|
62
|
+
|
63
|
+
The group and variable names are converted to lowercase Symbol objects and stored as keys in the hash. The value can be Ruby's String, Integer, Float, Complex, TrueClass, or FalseClass objects, depending on the literal in the namelist. In the case that the value is an array, it will be expressed as an Array in Ruby. We chose symbol as the key of the hash object because it is thought to be compatible with the pattern matching introduced in Ruby 2.7.
|
64
|
+
|
65
|
+
Example:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
require 'fortio-namelist'
|
69
|
+
|
70
|
+
input = %{
|
71
|
+
&group1
|
72
|
+
var1 = 11
|
73
|
+
var2 = 12
|
74
|
+
/
|
75
|
+
&group2
|
76
|
+
var1 = 12
|
77
|
+
var2 = 22
|
78
|
+
/
|
79
|
+
&group3
|
80
|
+
var1 = 31
|
81
|
+
var2 = 32
|
82
|
+
/
|
83
|
+
}
|
84
|
+
|
85
|
+
### read all groups
|
86
|
+
root = FortIO::Namelist.parse(input)
|
87
|
+
# => {:group1=>{:var1=>11, :var2=>12},
|
88
|
+
# :group2=>{:var1=>12, :var2=>22},
|
89
|
+
# :group3=>{:var1=>31, :var2=>32}}
|
90
|
+
|
91
|
+
### read only "group2"
|
92
|
+
root = FortIO::Namelist.parse(input, group: "group2")
|
93
|
+
# => {:group2=>{:var1=>12, :var2=>22}}
|
94
|
+
|
95
|
+
### read only "group1" and "group3"
|
96
|
+
root = FortIO::Namelist.parse(input, group: ["group1", "group3"])
|
97
|
+
# => {:group1=>{:var1=>11, :var2=>12},
|
98
|
+
# :group3=>{:var1=>31, :var2=>32}}
|
99
|
+
|
100
|
+
```
|
21
101
|
|
22
|
-
|
102
|
+
### Generating namelist string from Hash object with namelist structure
|
23
103
|
|
24
|
-
|
25
|
-
FortIO::Namelist.generate(hash, name: "namelist", array_format: 'stream')
|
104
|
+
To generate a namelist string from a Hash object with a namelist structure, use the following method.
|
26
105
|
|
27
|
-
# 複数のデータセットを含むHashからnamelist形式を生成する
|
28
106
|
FortIO::Namelist.dump(root, **format_options)
|
29
107
|
|
30
|
-
|
31
|
-
FortIO::Namelist.filter(input, **format_options) { |hash| ... }
|
108
|
+
The argument `root` is given as a Hash object. The return value is a namelist string.
|
32
109
|
|
33
|
-
|
110
|
+
Example:
|
34
111
|
|
35
|
-
|
36
|
-
|
112
|
+
```ruby
|
113
|
+
require 'fortio-namelist'
|
37
114
|
|
38
|
-
|
39
|
-
|
115
|
+
root = {group1: {var1: 11, var2: 12},
|
116
|
+
group2: {var1: 12, var2: 22},
|
117
|
+
group3: {var1: 31, var2: 32}}
|
40
118
|
|
41
|
-
|
42
|
-
|
119
|
+
puts FortIO::Namelist.dump(root)
|
120
|
+
```
|
43
121
|
|
44
|
-
|
45
|
-
'left'
|
46
|
-
'right'
|
47
|
-
Integer
|
122
|
+
This script print a namelist format string to stdout.
|
48
123
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
124
|
+
&group1
|
125
|
+
var1 = 11,
|
126
|
+
var2 = 12
|
127
|
+
/
|
128
|
+
&group2
|
129
|
+
var1 = 12,
|
130
|
+
var2 = 22
|
131
|
+
/
|
132
|
+
&group3
|
133
|
+
var1 = 31,
|
134
|
+
var2 = 32
|
135
|
+
/
|
136
|
+
|
137
|
+
### Format options for `FortIO::Namelist.dump`
|
138
|
+
|
139
|
+
You can finely control the output namelist string with the following keyword arguments (the first one is the default).
|
140
|
+
|
141
|
+
* `array_style` : the notation for array elements
|
142
|
+
* `logical_format` : boolean literals
|
143
|
+
* `float_format` : the notation for floating point numbers
|
144
|
+
* `alignment` : how variable identifiers are aligned
|
145
|
+
* `uppercase` : whether variable names, etc. should be uppercase or lowercase.
|
146
|
+
* `separator` : the separator between variable definitions
|
147
|
+
* `group_end` : the group terminator
|
148
|
+
* `indent` : the indentation for variable definition
|
149
|
+
|
150
|
+
|
151
|
+
#### `array_style` specifies the notation for array elements
|
152
|
+
|
153
|
+
* 'stream' : (default)
|
154
|
+
* 'index' :
|
155
|
+
|
156
|
+
Example:
|
59
157
|
|
158
|
+
```ruby
|
159
|
+
root = {group: {var1: [1,2,3], var2: ["a","b","c"]}}
|
160
|
+
|
161
|
+
puts FortIO::Namelist.dump(root, array_style: 'stream')
|
162
|
+
# =>
|
163
|
+
# &group
|
164
|
+
# var1 = 1, 2, 3,
|
165
|
+
# var2 = 'a', 'b', 'c'
|
166
|
+
# /
|
167
|
+
|
168
|
+
puts FortIO::Namelist.dump(root, array_style: 'index')
|
169
|
+
# =>
|
170
|
+
# &group
|
171
|
+
# var1(1) = 1,
|
172
|
+
# var1(2) = 2,
|
173
|
+
# var1(3) = 3,
|
174
|
+
# var2(1) = 'a',
|
175
|
+
# var2(2) = 'b',
|
176
|
+
# var2(3) = 'c'
|
177
|
+
# /
|
178
|
+
```
|
179
|
+
|
180
|
+
#### `logical_format` specifies boolean literals
|
181
|
+
|
182
|
+
* 'normal' : normal notation like `.true.`, `.false` (default)
|
183
|
+
* 'short' : short notation like `t`, `f`
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
root = {group: {var1: true, var2: false}}
|
187
|
+
|
188
|
+
puts FortIO::Namelist.dump(root, logical_format: 'normal')
|
189
|
+
# =>
|
190
|
+
# &group
|
191
|
+
# var1 = .true.,
|
192
|
+
# var2 = .false.
|
193
|
+
# /
|
194
|
+
|
195
|
+
puts FortIO::Namelist.dump(root, logical_format: 'short')
|
196
|
+
# =>
|
197
|
+
# &group
|
198
|
+
# var1 = t,
|
199
|
+
# var2 = f
|
200
|
+
# /
|
201
|
+
```
|
60
202
|
|
61
|
-
|
203
|
+
#### `float_format` specifies the notation for floating point numbers
|
62
204
|
|
205
|
+
* 'normal' : format with "%g" (default)
|
206
|
+
* 'd0' : format with "%g" followed by 'd0'
|
207
|
+
* 'exp' : exponential notation
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
root = {group: {var1: 1.0, var2: 12.75, var3: 50.0e-8}}
|
211
|
+
|
212
|
+
puts FortIO::Namelist.dump(root, float_format: 'normal')
|
213
|
+
# =>
|
214
|
+
# &group
|
215
|
+
# var1 = 1,
|
216
|
+
# var2 = 12.75,
|
217
|
+
# var3 = 5d-07
|
218
|
+
# /
|
219
|
+
|
220
|
+
puts FortIO::Namelist.dump(root, float_format: 'd0')
|
221
|
+
# =>
|
222
|
+
# &group
|
223
|
+
# var1 = 1d0,
|
224
|
+
# var2 = 12.75d0,
|
225
|
+
# var3 = 5d-07
|
226
|
+
# /
|
227
|
+
|
228
|
+
puts FortIO::Namelist.dump(root, float_format: 'exp')
|
229
|
+
# =>
|
230
|
+
# &group
|
231
|
+
# var1 = 1d+00,
|
232
|
+
# var2 = 1.275d+01,
|
233
|
+
# var3 = 5d-07
|
234
|
+
# /
|
235
|
+
```
|
236
|
+
|
237
|
+
#### `alignment` specifies how variable identifiers are aligned
|
238
|
+
|
239
|
+
* 'left' : aligned, left-justified, position of '=' can be specified by number eg. 'left:7' (default)
|
240
|
+
* 'right' : aligned, right-justified, position of '=' can be specified by number eg. 'right:7'
|
241
|
+
* 'none' : not aligned
|
242
|
+
* 'stream' : not aligned, stream style, length of each line can be specified by number eg. 'stream:70'
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
root = {group: {var1: 1, variable2: [1,2,3], v3: true}}
|
246
|
+
|
247
|
+
puts FortIO::Namelist.dump(root, alignment: 'left')
|
248
|
+
# =>
|
249
|
+
# &group
|
250
|
+
# var1 = 1,
|
251
|
+
# variable2 = 1, 2, 3,
|
252
|
+
# v3 = .true.
|
253
|
+
# /
|
254
|
+
|
255
|
+
puts FortIO::Namelist.dump(root, alignment: 'left:7')
|
256
|
+
# =>
|
257
|
+
# &group
|
258
|
+
# var1 = 1,
|
259
|
+
# variable2 = 1, 2, 3,
|
260
|
+
# v3 = .true.
|
261
|
+
# /
|
262
|
+
|
263
|
+
puts FortIO::Namelist.dump(root, alignment: 'right')
|
264
|
+
# =>
|
265
|
+
# &group
|
266
|
+
# var1 = 1,
|
267
|
+
# variable2 = 1, 2, 3,
|
268
|
+
# v3 = .true.
|
269
|
+
# /
|
270
|
+
|
271
|
+
puts FortIO::Namelist.dump(root, alignment: 'right:7')
|
272
|
+
# =>
|
273
|
+
# &group
|
274
|
+
# var1 = 1,
|
275
|
+
# variable2 = 1, 2, 3,
|
276
|
+
# v3 = .true.
|
277
|
+
# /
|
278
|
+
|
279
|
+
puts FortIO::Namelist.dump(root, alignment: 'none')
|
280
|
+
# =>
|
281
|
+
# &group
|
282
|
+
# var1 = 1,
|
283
|
+
# variable2 = 1, 2, 3,
|
284
|
+
# v3 = .true.
|
285
|
+
# /
|
286
|
+
|
287
|
+
puts FortIO::Namelist.dump(root, alignment: 'stream')
|
288
|
+
# =>
|
289
|
+
# &group
|
290
|
+
# var1 = 1, variable2 = 1, 2, 3, v3 = .true.,
|
291
|
+
# /
|
292
|
+
```
|
293
|
+
|
294
|
+
#### `uppercase` specifies whether variable names, etc. should be uppercase or lowercase.
|
295
|
+
|
296
|
+
* false : (default)
|
297
|
+
* true :
|
298
|
+
|
299
|
+
```ruby
|
300
|
+
root = {group: {var1: 1, var2: "a"}}
|
301
|
+
|
302
|
+
puts FortIO::Namelist.dump(root, uppercase: false)
|
303
|
+
# =>
|
304
|
+
# &group
|
305
|
+
# var1 = 1,
|
306
|
+
# val2 = 'a'
|
307
|
+
# /
|
308
|
+
|
309
|
+
puts FortIO::Namelist.dump(root, uppercase: true)
|
310
|
+
# =>
|
311
|
+
# &GROUP
|
312
|
+
# VAR1 = 1,
|
313
|
+
# VAL2 = 'a'
|
314
|
+
# /
|
315
|
+
```
|
316
|
+
|
317
|
+
#### `separator` specifies the separator between variable definitions
|
318
|
+
|
319
|
+
* "comma", "," : comma + NL separeted (default)
|
320
|
+
* "nl", "\n" : NL separated
|
321
|
+
|
322
|
+
```ruby
|
323
|
+
root = {group1: {var1: 1, var2: "a", var3: true},
|
324
|
+
group2: {var1: 2, var2: "b", var3: false}}
|
325
|
+
|
326
|
+
puts FortIO::Namelist.dump(root, separator: "comma")
|
327
|
+
# =>
|
328
|
+
# &group1
|
329
|
+
# var1 = 1,
|
330
|
+
# var2 = 'a',
|
331
|
+
# var3 = .true.
|
332
|
+
# /
|
333
|
+
# &group2
|
334
|
+
# var1 = 2,
|
335
|
+
# var2 = 'b',
|
336
|
+
# var3 = .false.
|
337
|
+
# /
|
338
|
+
|
339
|
+
puts FortIO::Namelist.dump(root, separator: "nl")
|
340
|
+
# =>
|
341
|
+
# &group1
|
342
|
+
# var1 = 1
|
343
|
+
# var2 = 'a'
|
344
|
+
# var3 = .true.
|
345
|
+
# /
|
346
|
+
# &group2
|
347
|
+
# var1 = 2
|
348
|
+
# var2 = 'b'
|
349
|
+
# var3 = .false.
|
350
|
+
# /
|
351
|
+
```
|
352
|
+
|
353
|
+
#### `group_end` specifies the group terminator
|
354
|
+
|
355
|
+
* "slash", "/" : end with `/` (default)
|
356
|
+
* "end" : end with `&end`
|
357
|
+
|
358
|
+
```ruby
|
359
|
+
root = {group: {var1: true, var2: false}}
|
360
|
+
|
361
|
+
puts FortIO::Namelist.dump(root, group_end: 'slash')
|
362
|
+
# =>
|
363
|
+
# &group
|
364
|
+
# var1 = .true.,
|
365
|
+
# var2 = .false.
|
366
|
+
# /
|
367
|
+
|
368
|
+
puts FortIO::Namelist.dump(root, group_end: 'end')
|
369
|
+
# =>
|
370
|
+
# &group
|
371
|
+
# var1 = .true.,
|
372
|
+
# var2 = .false.
|
373
|
+
# &end
|
374
|
+
```
|
375
|
+
|
376
|
+
#### `indent` specifies the indentation for variable definition
|
63
377
|
|
64
|
-
|
378
|
+
* ' '*2 : two spaces (default)
|
379
|
+
|
380
|
+
```ruby
|
381
|
+
root = {group: {var1: true, var2: false}}
|
382
|
+
|
383
|
+
puts FortIO::Namelist.dump(root, indent: ' '*2)
|
384
|
+
# =>
|
385
|
+
# &group
|
386
|
+
# var1 = .true.,
|
387
|
+
# var2 = .false.
|
388
|
+
# /
|
389
|
+
|
390
|
+
puts FortIO::Namelist.dump(root, indent: ' '*4)
|
391
|
+
# =>
|
392
|
+
# &group
|
393
|
+
# var1 = .true.,
|
394
|
+
# var2 = .false.
|
395
|
+
# /
|
396
|
+
```
|