toml-rb 1.1.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 1ef52970c82352fb2fa2fedeca2d023122c6b6db
4
- data.tar.gz: c9ea0ca9350b7b7ae41ee91a9f135330d51aad74
2
+ SHA256:
3
+ metadata.gz: 0e2a2f012c91d9664b1c1656ed84aa47e1011223a2d7bad0504fe809cf71a7ea
4
+ data.tar.gz: 7db3fb54e407e205089396c79cd2f418ed54ffd88ad3d5b03e404132f2ec9756
5
5
  SHA512:
6
- metadata.gz: a323eeb7e8d9cce983b7ce35897bb9583f164213a45175a91724a9b02743e567c9576ba8753545fe73edd09335d68a1dcc4a34d33dd9f03f91bc0bce9e9a929f
7
- data.tar.gz: e5091b85b319f06e742da736213a8586b89dc9f96f4cd41de565eb23e63169d7db6b49922e663c44cf19f0087b9e87ea0021bb0a897bea7f4c528ff3f90707b9
6
+ metadata.gz: 56ae22471a087c85acd872a7f7f354c4cdf0c430654d8bdfc277911015a94312960e22b82f79f7f772f62d0e73a8bb3faab8f2b7b7cc688a75343ea5fbef9fa9
7
+ data.tar.gz: 72d80c5c8713fd40d50e9863d3ea684c0999c991c050a4db0ba49f9a9db5fe0c6f373646b4256ca62127be54626582ba15ac2e492da0930103f9d62dbf4681dc
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ The MIT License (MIT)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md CHANGED
@@ -4,11 +4,11 @@ toml-rb
4
4
  [![Gem Version](https://badge.fury.io/rb/toml-rb.svg)](http://badge.fury.io/rb/toml-rb)
5
5
  [![Build Status](https://travis-ci.org/emancu/toml-rb.svg)](https://travis-ci.org/emancu/toml-rb)
6
6
  [![Code Climate](https://codeclimate.com/github/emancu/toml-rb/badges/gpa.svg)](https://codeclimate.com/github/emancu/toml-rb)
7
- [![Dependency Status](https://gemnasium.com/emancu/toml-rb.svg)](https://gemnasium.com/emancu/toml-rb)
7
+ [![RubyGem](https://img.shields.io/gem/dt/toml-rb.svg)](https://rubygems.org/gems/toml-rb)
8
8
 
9
- A [TomlRB](https://github.com/toml-lang/toml) parser using [Citrus](http://mjackson.github.io/citrus) library.
9
+ A [TOML](https://github.com/toml-lang/toml) parser using [Citrus](http://mjackson.github.io/citrus) library.
10
10
 
11
- TomlRB specs supported: `0.4.0`
11
+ TOML specs supported: `0.4.0`
12
12
 
13
13
  Installation
14
14
  ------------
@@ -3,10 +3,11 @@ require 'citrus'
3
3
  require_relative "toml-rb/errors"
4
4
  require_relative "toml-rb/array"
5
5
  require_relative "toml-rb/string"
6
+ require_relative "toml-rb/datetime"
7
+ require_relative "toml-rb/table"
6
8
  require_relative "toml-rb/table_array"
7
9
  require_relative "toml-rb/inline_table"
8
10
  require_relative "toml-rb/keyvalue"
9
- require_relative "toml-rb/keygroup"
10
11
  require_relative "toml-rb/parser"
11
12
  require_relative "toml-rb/dumper"
12
13
 
@@ -1,14 +1,8 @@
1
1
  module TomlRB
2
2
  module ArrayParser
3
3
  def value
4
- elements = captures[:elements].first
5
- return [] unless elements
6
-
7
- if elements.captures.key? :string
8
- elements.captures[:string].map(&:value)
9
- else
10
- eval(to_str)
11
- end
4
+ elements = captures[:array_elements].first
5
+ return elements ? elements.value : []
12
6
  end
13
7
  end
14
8
  end
@@ -0,0 +1,38 @@
1
+ module TomlRB
2
+ module OffsetDateTimeParser
3
+ def value
4
+ skeleton = captures[:datetime_skeleton].first
5
+ year, mon, day, hour, min, sec, sec_frac = skeleton.value
6
+ offset = captures[:date_offset].first || "+00:00"
7
+ sec = "#{sec}.#{sec_frac}".to_f
8
+
9
+ Time.new(year, mon, day, hour, min, sec, offset.to_s)
10
+ end
11
+ end
12
+
13
+ module LocalDateTimeParser
14
+ def value
15
+ year, mon, day = captures[:date_skeleton].first.value
16
+ hour, min, sec, sec_frac = captures[:time_skeleton].first.value
17
+ usec = sec_frac.to_s.ljust(6, '0')
18
+
19
+ Time.local(year, mon, day, hour, min, sec, usec)
20
+ end
21
+ end
22
+
23
+ module LocalDateParser
24
+ def value
25
+ year, mon, day = captures[:date_skeleton].first.value
26
+ Time.local(year,mon,day)
27
+ end
28
+ end
29
+
30
+ module LocalTimeParser
31
+ def value
32
+ hour, min, sec, sec_frac = captures[:time_skeleton].first.value
33
+ usec = sec_frac.to_s.ljust(6, '0')
34
+
35
+ Time.at(3600 * hour.to_i + 60 * min.to_i + sec.to_i, usec.to_i)
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,5 @@
1
+ require 'date'
2
+
1
3
  module TomlRB
2
4
  class Dumper
3
5
  attr_reader :toml_str
@@ -85,8 +87,14 @@ module TomlRB
85
87
  end
86
88
 
87
89
  def to_toml(obj)
88
- if obj.is_a? Time
90
+ if obj.is_a?(Time) || obj.is_a?(DateTime)
89
91
  obj.strftime('%Y-%m-%dT%H:%M:%SZ')
92
+ elsif obj.is_a?(Date)
93
+ obj.strftime('%Y-%m-%d')
94
+ elsif obj.is_a? Regexp
95
+ obj.inspect.inspect
96
+ elsif obj.is_a? String
97
+ obj.inspect.gsub(/\\(#[@{])/, '\1')
90
98
  else
91
99
  obj.inspect
92
100
  end
@@ -1,39 +1,37 @@
1
1
  grammar TomlRB::Arrays
2
2
  include TomlRB::Primitive
3
-
4
- rule array
5
- ("[" array_comments (elements)? space ","? array_comments "]" indent?) <TomlRB::ArrayParser>
6
- end
7
-
3
+
8
4
  rule array_comments
9
5
  (indent? (comment indent?)*)
10
6
  end
11
7
 
12
- rule elements
13
- float_array | string_array | array_array | integer_array | datetime_array | bool_array
14
- end
15
-
16
8
  rule float_array
17
- ((exponent | float) (space "," array_comments (exponent | float))*)
9
+ (float (space "," array_comments float)*) {
10
+ captures[:float].map(&:value)
11
+ }
18
12
  end
19
13
 
20
14
  rule string_array
21
- (string (space "," array_comments string)*)
22
- end
23
-
24
- rule array_array
25
- (array (space "," array_comments array)*)
15
+ (string (space "," array_comments string)*) {
16
+ captures[:string].map(&:value)
17
+ }
26
18
  end
27
19
 
28
20
  rule integer_array
29
- (integer (space "," array_comments integer)*)
21
+ (integer (space "," array_comments integer)*) {
22
+ captures[:integer].map(&:value)
23
+ }
30
24
  end
31
25
 
32
26
  rule datetime_array
33
- (datetime (space "," array_comments datetime)*)
27
+ (datetime (space "," array_comments datetime)*) {
28
+ captures[:datetime].map(&:value)
29
+ }
34
30
  end
35
31
 
36
32
  rule bool_array
37
- (bool (space "," array_comments bool)*)
33
+ (bool (space "," array_comments bool)*) {
34
+ captures[:bool].map(&:value)
35
+ }
38
36
  end
39
37
  end
@@ -3,15 +3,15 @@ grammar TomlRB::Document
3
3
  include TomlRB::Arrays
4
4
 
5
5
  rule document
6
- (comment | table_array | keygroup | keyvalue | line_break)*
6
+ (comment | table_array | table | keyvalue | line_break)*
7
7
  end
8
8
 
9
9
  rule table_array
10
- (space? '[[' stripped_key ("." stripped_key)* ']]' comment?) <TomlRB::TableArrayParser>
10
+ (space? '[[' stripped_key ']]' comment?) <TomlRB::TableArrayParser>
11
11
  end
12
12
 
13
- rule keygroup
14
- (space? '[' stripped_key ("." stripped_key)* ']' comment?) <TomlRB::KeygroupParser>
13
+ rule table
14
+ (space? '[' stripped_key ']' comment?) <TomlRB::TableParser>
15
15
  end
16
16
 
17
17
  rule keyvalue
@@ -19,15 +19,27 @@ grammar TomlRB::Document
19
19
  end
20
20
 
21
21
  rule inline_table
22
- (space? '{' (keyvalue (',' keyvalue)*)? space? '}' ) <TomlRB::InlineTableParser>
22
+ (space? '{' (keyvalue? (',' keyvalue)*)? space? '}' ) <TomlRB::InlineTableParser>
23
23
  end
24
24
 
25
25
  rule inline_table_array
26
- ("[" array_comments inline_table_array_elements space ","? array_comments "]" indent?) <TomlRB::InlineTableArrayParser>
26
+ (inline_table (space "," array_comments inline_table)*) {
27
+ captures[:inline_table].map(&:value).map(&:value)
28
+ }
27
29
  end
28
30
 
29
- rule inline_table_array_elements
30
- (inline_table (space "," array_comments inline_table)*)
31
+ rule array
32
+ ("[" array_comments (array_elements)? space ","? array_comments "]" indent?) <TomlRB::ArrayParser>
33
+ end
34
+
35
+ rule array_elements
36
+ inline_table_array | float_array | string_array | array_array | integer_array | datetime_array | bool_array
37
+ end
38
+
39
+ rule array_array
40
+ (array (space "," array_comments array)*) {
41
+ captures[:array].map(&:value)
42
+ }
31
43
  end
32
44
 
33
45
  rule toml_values
@@ -2,7 +2,7 @@ grammar TomlRB::Primitive
2
2
  include TomlRB::Helper
3
3
 
4
4
  rule primitive
5
- string | bool | datetime | number
5
+ datetime | bool | number | string
6
6
  end
7
7
 
8
8
  ##
@@ -34,50 +34,97 @@ grammar TomlRB::Primitive
34
34
  ##
35
35
 
36
36
  rule datetime
37
- ( skeleton:(datetime_skeleton | date_skeleton) ("Z" | date_offset | [,\.] frac:(/\d/1*6) date_offset?)? ) {
38
- skeleton = captures[:skeleton].first
39
- y,m,d,h,mi,s = skeleton.value
40
- offset = captures[:date_offset].first || "+00:00"
41
- sec_frac = captures[:frac].first || 0
42
- s = "#{s}.#{sec_frac}".to_f
43
-
44
- Time.new(*[y,m,d,h,mi,s,offset.to_s])
45
- }
37
+ offset_datetime | local_datetime | local_date | local_time
38
+ end
39
+
40
+ rule offset_datetime
41
+ ( skeleton:datetime_skeleton ("Z" | date_offset) ) <TomlRB::OffsetDateTimeParser>
42
+ end
43
+
44
+ rule local_datetime
45
+ datetime_skeleton <TomlRB::LocalDateTimeParser>
46
+ end
47
+
48
+ rule local_date
49
+ (date_skeleton space) <TomlRB::LocalDateParser>
50
+ end
51
+
52
+ rule local_time
53
+ (time_skeleton space) <TomlRB::LocalTimeParser>
46
54
  end
47
55
 
48
56
  rule datetime_skeleton
49
- (y:/\d\d\d\d/ "-" m:/\d\d/ "-" d:/\d\d/ "T" h:/\d\d/ ":" mi:/\d\d/ ":" s:/\d\d/) {
50
- [:y,:m,:d,:h,:mi,:s].map{ |s| capture(s).value }
57
+ (date_skeleton ("T"|" ") time_skeleton) {
58
+ capture(:date_skeleton).value + capture(:time_skeleton).value
51
59
  }
52
60
  end
53
61
 
54
- rule date_offset
55
- offset:(sign /\d\d/ ":" /\d\d/)
62
+ rule date_skeleton
63
+ (year:/\d\d\d\d/ "-" mon:/\d\d/ "-" day:/\d\d/) {
64
+ [:year,:mon,:day].map{ |s| capture(s).value }
65
+ }
56
66
  end
57
67
 
58
- rule date_skeleton
59
- (y:/\d\d\d\d/ "-" m:/\d\d/ "-" d:/\d\d/) {
60
- [:y,:m,:d].map{ |s| capture(s).value } + [0, 0, 0]
68
+ rule time_skeleton
69
+ ( hour:([0-2][0-9]) ":" mim:([0-6][0-9]) ":" sec:([0-6][0-9]) ([,\.] sec_frac:(/\d/1*6))? ) {
70
+ [:hour,:mim,:sec].map{ |s| capture(s).value } + [capture(:sec_frac) || '0']
61
71
  }
62
72
  end
63
73
 
74
+ rule date_offset
75
+ offset:(sign /\d\d/ ":" /\d\d/)
76
+ end
77
+
64
78
  ##
65
79
  # Number rules
66
80
  ##
67
81
 
68
82
  rule number
69
- exponent | float | integer
83
+ float | integer
70
84
  end
71
85
 
72
86
  rule float
73
- (integer '.' integer) { to_str.to_f }
87
+ inf | nan | exponential_float | fractional_float
88
+ end
89
+
90
+ rule inf
91
+ (s:sign? 'inf') {
92
+ sign = (capture(:s).value != '-') ? 1 : -1
93
+ sign * Float::INFINITY
94
+ }
74
95
  end
75
96
 
76
- rule exponent
77
- ( (float | integer) [eE] integer) { to_str.to_f }
97
+ rule nan
98
+ (sign? 'nan') { Float::NAN }
99
+ end
100
+
101
+ rule exponential_float
102
+ ((fractional_float | demical_integer) [eE] demical_integer) { to_str.to_f }
103
+ end
104
+
105
+ rule fractional_float
106
+ (demical_integer '.' [0-9_]+) {
107
+ to_str.to_f
108
+ }
78
109
  end
79
110
 
80
111
  rule integer
112
+ hexadecimal_integer | octal_integer | binary_integer | demical_integer
113
+ end
114
+
115
+ rule hexadecimal_integer
116
+ ('0x' [a-fA-F0-9_]+) { to_str.to_i(16) }
117
+ end
118
+
119
+ rule octal_integer
120
+ ('0o' [0-7_]+) { to_str.to_i(8) }
121
+ end
122
+
123
+ rule binary_integer
124
+ ('0b' [01_]+) { to_str.to_i(2) }
125
+ end
126
+
127
+ rule demical_integer
81
128
  (sign? [0-9_]+) { to_str.to_i }
82
129
  end
83
130
 
@@ -106,7 +153,7 @@ grammar TomlRB::Primitive
106
153
  ##
107
154
 
108
155
  rule key
109
- quoted_key | bare_key
156
+ dotted_key
110
157
  end
111
158
 
112
159
  rule bare_key
@@ -114,6 +161,16 @@ grammar TomlRB::Primitive
114
161
  end
115
162
 
116
163
  rule quoted_key
117
- string
164
+ basic_string | literal_string
165
+ end
166
+
167
+ rule single_key
168
+ quoted_key | bare_key
169
+ end
170
+
171
+ rule dotted_key
172
+ (space? single_key space? ("." space? single_key space?)* ) {
173
+ captures[:single_key].map(&:value)
174
+ }
118
175
  end
119
176
  end
@@ -1,57 +1,21 @@
1
1
  module TomlRB
2
2
  class InlineTable
3
- attr_reader :symbolize_keys
4
-
5
3
  def initialize(keyvalue_pairs)
6
4
  @pairs = keyvalue_pairs
7
- @symbolize_keys = false
8
5
  end
9
6
 
10
- def value(symbolize_keys = false)
11
- if (@symbolize_keys = symbolize_keys)
12
- tuple = ->(kv) { [kv.key.to_sym, visit_value(kv.value)] }
13
- else
14
- tuple = ->(kv) { [kv.key, visit_value(kv.value)] }
15
- end
16
-
17
- Hash[@pairs.map(&tuple)]
7
+ def accept_visitor(keyvalue)
8
+ value keyvalue.symbolize_keys
18
9
  end
19
10
 
20
- def visit_inline_table(inline_table)
11
+ def value(symbolize_keys=false)
21
12
  result = {}
22
-
23
- inline_table.value(@symbolize_keys).each do |k, v|
24
- result[key k] = visit_value v
13
+ @pairs.each do |kv|
14
+ update = kv.assign({}, [], symbolize_keys)
15
+ result.merge!(update) { |key, _, _| fail ValueOverwriteError.new(key) }
25
16
  end
26
-
27
17
  result
28
18
  end
29
-
30
- def accept_visitor(keyvalue)
31
- keyvalue.visit_inline_table self
32
- end
33
-
34
- private
35
-
36
- def visit_value(a_value)
37
- return a_value unless a_value.respond_to? :accept_visitor
38
-
39
- a_value.accept_visitor self
40
- end
41
-
42
- def key(a_key)
43
- symbolize_keys ? a_key.to_sym : a_key
44
- end
45
- end
46
-
47
- class InlineTableArray
48
- def initialize(inline_tables)
49
- @inline_tables = inline_tables
50
- end
51
-
52
- def value(symbolize_keys = false)
53
- @inline_tables.map { |it| it.value(symbolize_keys) }
54
- end
55
19
  end
56
20
 
57
21
  module InlineTableParser
@@ -59,14 +23,4 @@ module TomlRB
59
23
  TomlRB::InlineTable.new captures[:keyvalue].map(&:value)
60
24
  end
61
25
  end
62
-
63
- module InlineTableArrayParser
64
- def value
65
- tables = captures[:inline_table_array_elements].map do |x|
66
- x.captures[:inline_table]
67
- end
68
-
69
- TomlRB::InlineTableArray.new(tables.flatten.map(&:value)).value
70
- end
71
- end
72
26
  end