fortio-namelist 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,91 +14,98 @@
14
14
 
15
15
  class FortIO::Namelist::Parser
16
16
 
17
- prechigh
18
- nonassoc COMMA
19
- nonassoc NL
20
- left ','
21
- left '='
22
- preclow
23
-
24
17
  rule
25
18
 
26
- namelist_all :
27
- | namelist
28
- | namelist namelist_all
29
-
30
19
  namelist :
31
- header tailer
20
+ namelist group
21
+ | group
22
+
23
+ group :
24
+ group_header separator varlist separator group_end
25
+ { @root[val[0]] = val[2]; @scan.in_namelist = nil }
26
+ | group_header separator group_end
32
27
  { @root[val[0]] = []; @scan.in_namelist = nil }
33
- | header paramlist tailer
34
- { @root[val[0]] = val[1]; @scan.in_namelist = nil }
35
28
 
36
- prefix : '&'
29
+ group_prefix :
30
+ '&'
37
31
  | '$'
38
32
 
39
- header :
40
- prefix IDENT { result = val[1].downcase; @scan.in_namelist = val[1].downcase }
41
- | header NL
33
+ group_header :
34
+ group_prefix IDENT
35
+ { result = val[1].downcase; @scan.in_namelist = val[1].downcase }
42
36
 
43
- tailer :
44
- '/'
45
- | prefix IDENT { raise Racc::ParseError, "\nparse error (&)" unless val[1] =~ /\Aend\Z/i }
37
+ separator:
38
+ COMMA
39
+ | nls
40
+ | blank
46
41
 
47
- separator: COMMA
48
- | NL
42
+ nls :
43
+ NL
44
+ | nls NL
45
+
46
+ blank:
49
47
 
50
- paramlist:
51
- paramdef { result = [val[0]] }
52
- | paramlist paramdef
53
- { result = val[0] + [val[1]] }
54
- | paramlist separator
55
- { result = val[0] }
48
+ group_end :
49
+ '/'
50
+ | group_prefix IDENT
51
+ { raise Racc::ParseError, "\nparse error (&)" unless val[1] =~ /\Aend\Z/i }
56
52
 
57
- paramdef:
58
- IDENT '=' separator
59
- { result = ParamDef.new(val[0].downcase, nil, "") }
60
- | IDENT '=' NIL
53
+ varlist:
54
+ vardef { result = [val[0]] }
55
+ | varlist separator vardef
56
+ { result = val[0] + [val[2]] }
57
+
58
+ vardef:
59
+ IDENT '=' COMMA
61
60
  { result = ParamDef.new(val[0].downcase, nil, "") }
62
61
  | IDENT '=' rvalues
63
62
  { result = ParamDef.new(val[0].downcase, nil, val[2]) }
63
+ | IDENT '=' nls rvalues
64
+ { result = ParamDef.new(val[0].downcase, nil, val[3]) }
64
65
  | IDENT '(' array_spec ')' '=' rvalues
65
66
  { result = ParamDef.new(val[0].downcase, val[2], val[5]) }
66
67
 
67
- rvalues :
68
- abbreb { result = val[0] }
69
- | rvalues abbreb
68
+ rvalues :
69
+ rlist
70
+ | ident_list
71
+
72
+ rlist :
73
+ element
74
+ | NIL { result = [nil, nil] }
75
+ | rlist element
70
76
  { result = val[0] + val[1] }
71
- | rvalues ',' abbreb
72
- { result = val[0] + val[2] }
73
- | rvalues NL abbreb
77
+ | rlist ',' element
74
78
  { result = val[0] + val[2] }
75
- | rvalues NIL
79
+ | rlist NIL
76
80
  { result = val[0] + [nil] }
77
- | NIL rvalues
78
- { result = [nil] + val[1] }
79
- | ',' rvalues
80
- { result = [nil] + val[1] }
81
- | rvalues NL
82
- { result = val[0] }
83
- | IDENT
84
- { result = val[0] }
85
-
86
- abbreb :
87
- constant { result = [val[0]] }
81
+
82
+ element :
83
+ constant { result = [val[0]] }
88
84
  | DIGITS '*' constant
89
- { result = [val[2]] * val[0] }
85
+ { result = [val[2]] * val[0] }
90
86
 
91
87
  constant :
92
88
  STRING
93
89
  | LOGICAL
94
- | DIGITS
95
- | FLOAT
90
+ | real
96
91
  | complex
97
92
 
98
- real : DIGITS
93
+ real :
94
+ DIGITS
99
95
  | FLOAT
100
96
 
101
- complex : '(' real ',' real ')' { result = Complex(val[1],val[3]) }
97
+ complex :
98
+ '(' real ',' real ')'
99
+ { result = Complex(val[1],val[3]) }
100
+
101
+ ident_list :
102
+ IDENT { result = [val[0]] }
103
+ | STRINGLIKE
104
+ { result = [val[0]] }
105
+ | ident_list ',' IDENT
106
+ { result = val[0] + [val[2]]}
107
+ | ident_list ',' STRINGLIKE
108
+ { result = val[0] + [val[2]]}
102
109
 
103
110
  array_spec :
104
111
  DIGITS { result = [val[0]-1] }
@@ -184,6 +191,21 @@ module FortIO::Namelist
184
191
  end
185
192
  else
186
193
  case
194
+ when @s.scan(/\A\(/)
195
+ return [
196
+ '(',
197
+ nil
198
+ ]
199
+ when @s.scan(/\A\)/)
200
+ return [
201
+ ')',
202
+ nil
203
+ ]
204
+ when @s.scan(/\A\:/)
205
+ return [
206
+ ':',
207
+ nil
208
+ ]
187
209
  when @s.scan(/\A[+-]?(\d+)\.(\d+)?([ED][+-]?(\d+))?/i) ### float
188
210
  return [ ### 1.2E+3, 1.E+3, 1.2E3
189
211
  :FLOAT, ### 1.2, 1.
@@ -199,9 +221,9 @@ module FortIO::Namelist
199
221
  :FLOAT,
200
222
  @s[0].sub(/D/i,'e').to_f
201
223
  ]
202
- when @s.scan(/\A\d+[a-z_]\w*/i) ### STRING
224
+ when @s.scan(/\A\d+[a-z_]\w*/i) ### STRING-Like
203
225
  return [
204
- :STRING,
226
+ :STRINGLIKE,
205
227
  @s[0]
206
228
  ]
207
229
  when @s.scan(/\A[\-\+]?\d+/) ### digits
@@ -229,6 +251,11 @@ module FortIO::Namelist
229
251
  ',',
230
252
  nil
231
253
  ]
254
+ elsif @s.match?(/\A[a-z]\w*\s*,/i)
255
+ return [
256
+ ',',
257
+ nil
258
+ ]
232
259
  elsif @s.match?(/\A[a-z]\w*/i) or @s.match?(/\A[\&\$\/\!]/)
233
260
  return [
234
261
  :COMMA,
@@ -252,9 +279,9 @@ module FortIO::Namelist
252
279
  @s[0],
253
280
  nil
254
281
  ]
255
- when @s.scan(/\A_\w*/i) ### STRING
282
+ when @s.scan(/\A_\w*/i) ### STRING-Like
256
283
  return [
257
- :STRING,
284
+ :STRINGLIKE,
258
285
  @s[0]
259
286
  ]
260
287
  when @s.scan(/\A\.t.*?\./i) ### LOGICAL true
@@ -0,0 +1,160 @@
1
+ require "fortio-namelist"
2
+ require "rspec-power_assert"
3
+
4
+ describe FortIO::Namelist do
5
+
6
+ example "array stream" do
7
+ input = %{
8
+ &example
9
+ v1 = 1,2,3,4,5,
10
+ v2 = 6,7,8,9,10,
11
+ /
12
+ }
13
+ nml = FortIO::Namelist.read(input)
14
+ is_asserted_by { nml.has_key? "example" }
15
+ is_asserted_by { nml["example"].is_a? Hash }
16
+ is_asserted_by { nml["example"]["v1"] == [1,2,3,4,5] }
17
+ is_asserted_by { nml["example"]["v2"] == [6,7,8,9,10] }
18
+ end
19
+
20
+ example "array sequence" do
21
+ input = %{
22
+ &example v1 = 1,2,3,4,5, v2 = 6,7,8,9,10, /
23
+ }
24
+ nml = FortIO::Namelist.read(input)
25
+ is_asserted_by { nml.has_key? "example" }
26
+ is_asserted_by { nml["example"].is_a? Hash }
27
+ is_asserted_by { nml["example"]["v1"] == [1,2,3,4,5] }
28
+ is_asserted_by { nml["example"]["v2"] == [6,7,8,9,10] }
29
+ end
30
+
31
+ example "array index" do
32
+ input = %{
33
+ &example
34
+ v1(1) = 1
35
+ v1(2) = 2
36
+ v1(3) = 3
37
+ v1(4) = 4
38
+ v1(5) = 5
39
+ v2(1:2) = 6,7
40
+ v2(3:5) = 8,9,10,
41
+ /
42
+ }
43
+ nml = FortIO::Namelist.read(input)
44
+ is_asserted_by { nml.has_key? "example" }
45
+ is_asserted_by { nml["example"].is_a? Hash }
46
+ is_asserted_by { nml["example"]["v1"] == [1,2,3,4,5] }
47
+ is_asserted_by { nml["example"]["v2"] == [6,7,8,9,10] }
48
+ end
49
+
50
+ example "array partial" do
51
+ input = %{
52
+ &example
53
+ v1(2) = 2
54
+ v1(5) = 5
55
+ v2(3:5) = 8,9,10,
56
+ /
57
+ }
58
+ nml = FortIO::Namelist.read(input)
59
+ is_asserted_by { nml.has_key? "example" }
60
+ is_asserted_by { nml["example"].is_a? Hash }
61
+ is_asserted_by { nml["example"]["v1"] == [nil,2,nil,nil,5] }
62
+ is_asserted_by { nml["example"]["v2"] == [nil,nil,8,9,10] }
63
+ end
64
+
65
+ example "array extend" do
66
+ input = %{
67
+ &example
68
+ v1(2) = 2,3,4,5
69
+ /
70
+ }
71
+ nml = FortIO::Namelist.read(input)
72
+ is_asserted_by { nml.has_key? "example" }
73
+ is_asserted_by { nml["example"].is_a? Hash }
74
+ is_asserted_by { nml["example"]["v1"] == [nil,2,3,4,5] }
75
+ end
76
+
77
+ example "array reputation" do
78
+ input = %{
79
+ &example
80
+ v1(1:5) = 2,4*2
81
+ v2(2) = 4*2
82
+ v3 = 5*.true.
83
+ v4 = 5 * f
84
+ /
85
+ }
86
+ nml = FortIO::Namelist.read(input)
87
+ is_asserted_by { nml.has_key? "example" }
88
+ is_asserted_by { nml["example"].is_a? Hash }
89
+ is_asserted_by { nml["example"]["v1"] == [2,2,2,2,2] }
90
+ is_asserted_by { nml["example"]["v2"] == [nil,2,2,2,2] }
91
+ is_asserted_by { nml["example"]["v3"] == [true]*5 }
92
+ is_asserted_by { nml["example"]["v4"] == [false]*5 }
93
+ end
94
+
95
+ example "array of string" do
96
+ input = %{
97
+ &example
98
+ v1 = "a",'b',"c"
99
+ /
100
+ }
101
+ nml = FortIO::Namelist.read(input)
102
+ is_asserted_by { nml.has_key? "example" }
103
+ is_asserted_by { nml["example"].is_a? Hash }
104
+ is_asserted_by { nml["example"]["v1"] == ["a","b","c"] }
105
+ end
106
+
107
+ example "array of identifier" do
108
+ input = %{
109
+ &example
110
+ v1 = a, b, c
111
+ /
112
+ }
113
+ expect { FortIO::Namelist.read(input) }.to raise_error(RuntimeError)
114
+ end
115
+
116
+ example "array of identifier 2" do
117
+ input = %{
118
+ &example
119
+ v1 = a, 0_b, _c
120
+ /
121
+ }
122
+ nml = FortIO::Namelist.read(input)
123
+ is_asserted_by { nml.has_key? "example" }
124
+ is_asserted_by { nml["example"].is_a? Hash }
125
+ is_asserted_by { nml["example"]["v1"] == ["a","0_b","_c"] }
126
+ end
127
+
128
+ example "don't permit to mix identifier and string in array stream" do
129
+ input = %{
130
+ &example
131
+ v1 = "a", b, "c"
132
+ /
133
+ }
134
+ expect { FortIO::Namelist.read(input) }.to raise_error(RuntimeError)
135
+ end
136
+
137
+ example "don't permit to mix identifier and string in array stream 2" do
138
+ input = %{
139
+ &example
140
+ v1 = a, "b", c
141
+ /
142
+ }
143
+ expect { FortIO::Namelist.read(input) }.to raise_error(RuntimeError)
144
+ end
145
+
146
+ example "empty element" do
147
+ input = %{
148
+ &example
149
+ v1 = , , 3, , 5,
150
+ v2 = , , 3, , 5
151
+ /
152
+ }
153
+ nml = FortIO::Namelist.read(input)
154
+ is_asserted_by { nml.has_key? "example" }
155
+ is_asserted_by { nml["example"].is_a? Hash }
156
+ is_asserted_by { nml["example"]["v1"] == [nil, nil, 3, nil, 5] }
157
+ is_asserted_by { nml["example"]["v2"] == [nil, nil, 3, nil, 5] }
158
+ end
159
+
160
+ end
@@ -0,0 +1,65 @@
1
+ require "fortio-namelist"
2
+ require "rspec-power_assert"
3
+
4
+ describe FortIO::Namelist do
5
+
6
+ example "namelist prefix &" do
7
+ input = %{
8
+ &example
9
+ &end
10
+ }
11
+ nml = FortIO::Namelist.read(input)
12
+ is_asserted_by { nml.has_key? "example" }
13
+ is_asserted_by { nml["example"].is_a? Hash }
14
+ is_asserted_by { nml["example"].empty? }
15
+ end
16
+
17
+ example "namelist prefix $" do
18
+ input = %{
19
+ $example
20
+ $end
21
+ }
22
+ nml = FortIO::Namelist.read(input)
23
+ is_asserted_by { nml.has_key? "example" }
24
+ end
25
+
26
+ example "end with /" do
27
+ input = %{
28
+ &example
29
+ /
30
+ }
31
+ nml = FortIO::Namelist.read(input)
32
+ is_asserted_by { nml.has_key? "example" }
33
+ end
34
+
35
+ example "only comment" do
36
+ input = %{
37
+ &example
38
+ ! this is comment
39
+ /
40
+ }
41
+ nml = FortIO::Namelist.read(input)
42
+ is_asserted_by { nml.has_key? "example" }
43
+ end
44
+
45
+ example "only newlines" do
46
+ input = %{
47
+ &example
48
+
49
+
50
+ /
51
+ }
52
+ nml = FortIO::Namelist.read(input)
53
+ is_asserted_by { nml.has_key? "example" }
54
+ end
55
+
56
+ example "one line" do
57
+ input = %{
58
+ &example /
59
+ }
60
+ nml = FortIO::Namelist.read(input)
61
+ is_asserted_by { nml.has_key? "example" }
62
+ end
63
+
64
+
65
+ end
@@ -0,0 +1,81 @@
1
+ require "fortio-namelist"
2
+ require "rspec-power_assert"
3
+
4
+ describe FortIO::Namelist do
5
+
6
+ example "only letter" do
7
+ input = %{
8
+ &example
9
+ a = 1
10
+ ab = 1
11
+ abc = 1
12
+ /
13
+ }
14
+ nml = FortIO::Namelist.read(input)
15
+ is_asserted_by { nml.has_key? "example" }
16
+ is_asserted_by { nml["example"].is_a? Hash }
17
+ is_asserted_by { nml["example"].keys.size == 3 }
18
+ is_asserted_by { nml["example"].keys == ["a","ab","abc"] }
19
+ end
20
+
21
+ example "letter number" do
22
+ input = %{
23
+ &example
24
+ a1 = 1
25
+ ab12 = 1
26
+ abc123 = 1
27
+ /
28
+ }
29
+ nml = FortIO::Namelist.read(input)
30
+ is_asserted_by { nml.has_key? "example" }
31
+ is_asserted_by { nml["example"].is_a? Hash }
32
+ is_asserted_by { nml["example"].keys.size == 3 }
33
+ is_asserted_by { nml["example"].keys == ["a1","ab12","abc123"] }
34
+ end
35
+
36
+ example "leter number underbar" do
37
+ input = %{
38
+ &example
39
+ a_1 = 1
40
+ ab_12 = 1
41
+ abc_123 = 1
42
+ /
43
+ }
44
+ nml = FortIO::Namelist.read(input)
45
+ is_asserted_by { nml.has_key? "example" }
46
+ is_asserted_by { nml["example"].is_a? Hash }
47
+ is_asserted_by { nml["example"].keys.size == 3 }
48
+ is_asserted_by { nml["example"].keys == ["a_1","ab_12","abc_123"] }
49
+ end
50
+
51
+ example "can't start with number" do
52
+ input = %{
53
+ &example
54
+ 1a = 1
55
+ /
56
+ }
57
+ expect { FortIO::Namelist.read(input) }.to raise_error(RuntimeError)
58
+ end
59
+
60
+ example "can't start with underscore" do
61
+ input = %{
62
+ &example
63
+ _a = 1
64
+ /
65
+ }
66
+ expect { FortIO::Namelist.read(input) }.to raise_error(RuntimeError)
67
+ end
68
+
69
+ example "t and f can be used as indentifier" do
70
+ input = %{
71
+ &example
72
+ t = 1
73
+ f = 2
74
+ /
75
+ }
76
+ nml = FortIO::Namelist.read(input)
77
+ is_asserted_by { nml["example"]["t"] = 1 }
78
+ is_asserted_by { nml["example"]["f"] = 2 }
79
+ end
80
+
81
+ end