toml-rb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/README.md +72 -0
- data/Rakefile +6 -0
- data/lib/toml.rb +56 -0
- data/lib/toml/grammars/array.citrus +35 -0
- data/lib/toml/grammars/document.citrus +16 -0
- data/lib/toml/grammars/helper.citrus +13 -0
- data/lib/toml/grammars/primitive.citrus +50 -0
- data/lib/toml/keygroup.rb +24 -0
- data/lib/toml/keyvalue.rb +23 -0
- data/lib/toml/parser.rb +19 -0
- data/lib/toml/string.rb +27 -0
- data/test/example.toml +38 -0
- data/test/grammar_test.rb +121 -0
- data/test/hard_example.toml +23 -0
- data/test/helper.rb +2 -0
- data/test/toml_test.rb +110 -0
- data/toml-rb.gemspec +22 -0
- data/toml_parser-ruby.gemspec +28 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
OGE0NGNmZmVlODdlMTk3YTY3ZmJlZGIxM2JmNTM4MmYxZjU5ODcyYQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MjhlMzA4YTJlZTlkOTFiMzI5Zjg3MzMyZGEzZTA1NDE3ODY3YmY3OQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZWQxMTMwN2E2NjVlMzU0MGYwOTAwZjA1NTc0MzFiMjJkNjhjNDU3NzZlNGY5
|
10
|
+
MzkyMDk4OWQ4NGNlYWJjNWVlYTI0ODNmNmNiMDM2NWIzY2E2YmI5NmFlNTkx
|
11
|
+
NjVmZTYzMTFmNDRmZWVhM2JlMjUwZjgxZWE1NWFmMjczZTdkODA=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NmUzNTAwOWE5NmRkNjk2M2Y1NTgxYzBjOTYxOGVkZGMxY2VlNDA2Njc2Zjdm
|
14
|
+
YTM3MTI5ZDcwMTMyNjNkOGZjMzc4ZWI4NmM2MjZkNDc5NDBiNDljNzI1MmZk
|
15
|
+
NWQxYTQ4Mzc2NDBhYTA2N2U1YjdmOWY1YjFjYzJmN2IxYmVmZGU=
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
toml-rb
|
2
|
+
=======
|
3
|
+
|
4
|
+
Formerly known as __toml_parser-ruby__.
|
5
|
+
|
6
|
+
A [TOML](https://github.com/mojombo/toml) parser using [Citrus](http://mjijackson.com/citrus/example.html) library.
|
7
|
+
|
8
|
+
|
9
|
+
Installation
|
10
|
+
------------
|
11
|
+
|
12
|
+
$ gem install toml-rb
|
13
|
+
|
14
|
+
Usage
|
15
|
+
-----
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require 'toml'
|
19
|
+
|
20
|
+
# From a file!
|
21
|
+
path = File.join(File.dirname(__FILE__), 'path', 'to', 'file')
|
22
|
+
TOML.load_file(path)
|
23
|
+
|
24
|
+
# From a stream!
|
25
|
+
stream = <<-EOS
|
26
|
+
title = "wow!"
|
27
|
+
|
28
|
+
[awesome]
|
29
|
+
you = true
|
30
|
+
others = false
|
31
|
+
EOS
|
32
|
+
TOML.parse(stream)
|
33
|
+
|
34
|
+
# You want symbols as your keys? No problem!
|
35
|
+
TOML.load_file(path, symbolize_keys: true)
|
36
|
+
# Works the same for TOML.parse
|
37
|
+
```
|
38
|
+
|
39
|
+
Contributing
|
40
|
+
------------
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Bundle it `$ dep install` (install [dep](https://github.com/cyx/dep) if you don't have it)
|
44
|
+
3. Create your feature branch `git checkout -b my-new-feature`
|
45
|
+
4. Add tests and commit your changes `git commit -am 'Add some feature'`
|
46
|
+
5. Run tests `$ rake`
|
47
|
+
6. Push the branch `git push origin my-new-feature`
|
48
|
+
7. Create new Pull Request
|
49
|
+
|
50
|
+
License
|
51
|
+
-------
|
52
|
+
|
53
|
+
MIT License
|
54
|
+
|
55
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
56
|
+
a copy of this software and associated documentation files (the
|
57
|
+
"Software"), to deal in the Software without restriction, including
|
58
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
59
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
60
|
+
permit persons to whom the Software is furnished to do so, subject to
|
61
|
+
the following conditions:
|
62
|
+
|
63
|
+
The above copyright notice and this permission notice shall be
|
64
|
+
included in all copies or substantial portions of the Software.
|
65
|
+
|
66
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
67
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
68
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
69
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
70
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
71
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
72
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/lib/toml.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require_relative "../init"
|
2
|
+
|
3
|
+
module TOML
|
4
|
+
|
5
|
+
# Public: Returns a hash from *TOML* content.
|
6
|
+
#
|
7
|
+
# content - TOML string to be parsed.
|
8
|
+
# options - The Hash options used to refine the parser (default: {}):
|
9
|
+
# :symbolize_keys - true|false (optional).
|
10
|
+
#
|
11
|
+
#
|
12
|
+
# Examples
|
13
|
+
#
|
14
|
+
# TOML.parse('[group]')
|
15
|
+
# # => {"group"=>{}}
|
16
|
+
#
|
17
|
+
# TOML.parse('title = "TOML parser"')
|
18
|
+
# # => {"title"=>"TOML parser"}
|
19
|
+
#
|
20
|
+
# TOML.parse('[group]', symbolize_keys: true)
|
21
|
+
# # => {group: {}}
|
22
|
+
#
|
23
|
+
# TOML.parse('title = "TOML parser"', symbolize_keys: true)
|
24
|
+
# # => {title: "TOML parser"}
|
25
|
+
#
|
26
|
+
#
|
27
|
+
# Returns a Ruby hash representation of the content according to TOML spec.
|
28
|
+
# Raises ValueOverwriteError if a key is overwritten
|
29
|
+
def self.parse(content, options = {})
|
30
|
+
Parser.new(content, options).hash
|
31
|
+
end
|
32
|
+
|
33
|
+
# Public: Returns a hash from a *TOML* file.
|
34
|
+
#
|
35
|
+
# path - TOML File path
|
36
|
+
# options - The Hash options used to refine the parser (default: {}):
|
37
|
+
# :symbolize_keys - true|false (optional).
|
38
|
+
#
|
39
|
+
#
|
40
|
+
# Examples
|
41
|
+
#
|
42
|
+
# TOML.load_file('/tmp/simple.toml')
|
43
|
+
# # => {"group"=>{}}
|
44
|
+
#
|
45
|
+
# TOML.load_file('/tmp/simple.toml', symbolize_keys: true)
|
46
|
+
# # => {group: {}}
|
47
|
+
#
|
48
|
+
#
|
49
|
+
# Returns a Ruby hash representation of the content.
|
50
|
+
# Raises ValueOverwriteError if a key is overwritten
|
51
|
+
# Raises Errno::ENOENT if the file cannot be found.
|
52
|
+
# Raises Errno::EACCES if the file cannot be accessed.
|
53
|
+
def self.load_file(path, options = {})
|
54
|
+
TOML.parse(File.read(path), options)
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
grammar TomlArray
|
2
|
+
include Primitive
|
3
|
+
|
4
|
+
rule array
|
5
|
+
("[" indent? (elements)* ","? indent? comment? indent? "]" indent?) { eval(self) }
|
6
|
+
end
|
7
|
+
|
8
|
+
rule elements
|
9
|
+
float_array | string_array | array_array | integer_array | datetime_array | bool_array
|
10
|
+
end
|
11
|
+
|
12
|
+
rule float_array
|
13
|
+
(float ("," indent? float)*)
|
14
|
+
end
|
15
|
+
|
16
|
+
rule string_array
|
17
|
+
(string ("," indent? string)*)
|
18
|
+
end
|
19
|
+
|
20
|
+
rule array_array
|
21
|
+
(array ("," indent? array)*)
|
22
|
+
end
|
23
|
+
|
24
|
+
rule integer_array
|
25
|
+
(integer ("," indent? integer)*)
|
26
|
+
end
|
27
|
+
|
28
|
+
rule datetime_array
|
29
|
+
(datetime ("," indent? datetime)*)
|
30
|
+
end
|
31
|
+
|
32
|
+
rule bool_array
|
33
|
+
(bool ("," indent? bool)*)
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
grammar Document
|
2
|
+
include Primitive
|
3
|
+
include TomlArray
|
4
|
+
|
5
|
+
rule document
|
6
|
+
(comment | keygroup | keyvalue | "\n")*
|
7
|
+
end
|
8
|
+
|
9
|
+
rule keygroup
|
10
|
+
(space? '[' nested_keys:(key "."?)+ ']' space? comment?) <Keygroup>
|
11
|
+
end
|
12
|
+
|
13
|
+
rule keyvalue
|
14
|
+
(space? key space? '=' space? v:(primitive | array) comment?) <Keyvalue>
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
grammar Primitive
|
2
|
+
include Helper
|
3
|
+
|
4
|
+
rule primitive
|
5
|
+
string | bool | datetime | number
|
6
|
+
end
|
7
|
+
|
8
|
+
rule string
|
9
|
+
(/(["'])(?:\\?.)*?\1/ space) <TomlString>
|
10
|
+
end
|
11
|
+
|
12
|
+
rule bool
|
13
|
+
true | false
|
14
|
+
end
|
15
|
+
|
16
|
+
# Full Zulu form
|
17
|
+
rule datetime
|
18
|
+
(y:/\d\d\d\d/ "-" m:/\d\d/ "-" d:/\d\d/ "T" h:/\d\d/ ":" mi:/\d\d/ ":" s:/\d\d/ "Z") {
|
19
|
+
Time.utc(*[y,m,d,h,mi,s].map(&:value))
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
rule number
|
24
|
+
float | integer
|
25
|
+
end
|
26
|
+
|
27
|
+
rule float
|
28
|
+
(integer '.' integer) { to_f }
|
29
|
+
end
|
30
|
+
|
31
|
+
rule integer
|
32
|
+
(sign? [0-9]+) { to_i }
|
33
|
+
end
|
34
|
+
|
35
|
+
rule sign
|
36
|
+
'+' | '-'
|
37
|
+
end
|
38
|
+
|
39
|
+
rule key
|
40
|
+
[a-zA-Z_] [a-zA-Z_0-9#?]*
|
41
|
+
end
|
42
|
+
|
43
|
+
rule true
|
44
|
+
'true' { true }
|
45
|
+
end
|
46
|
+
|
47
|
+
rule false
|
48
|
+
'false' { false }
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module TOML
|
2
|
+
class Keygroup
|
3
|
+
def initialize(nested_keys)
|
4
|
+
@nested_keys = nested_keys
|
5
|
+
end
|
6
|
+
|
7
|
+
def navigate_keys(hash, symbolize_keys = false)
|
8
|
+
@nested_keys.each do |key|
|
9
|
+
key = symbolize_keys ? key.to_sym : key
|
10
|
+
hash[key] = {} unless hash[key]
|
11
|
+
hash = hash[key]
|
12
|
+
end
|
13
|
+
|
14
|
+
hash
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Used in toml.citrus
|
20
|
+
module Keygroup
|
21
|
+
def value
|
22
|
+
TOML::Keygroup.new(nested_keys.to_s.split("."))
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module TOML
|
2
|
+
class ValueOverwriteError < StandardError; end
|
3
|
+
|
4
|
+
class Keyvalue
|
5
|
+
def initialize(key, value)
|
6
|
+
@key, @value = key, value
|
7
|
+
end
|
8
|
+
|
9
|
+
def assign(hash, symbolize_keys = false)
|
10
|
+
raise ValueOverwriteError if hash[@key]
|
11
|
+
|
12
|
+
key = symbolize_keys ? @key.to_sym : @key
|
13
|
+
hash[key] = @value
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Used in toml.citrus
|
19
|
+
module Keyvalue
|
20
|
+
def value
|
21
|
+
TOML::Keyvalue.new(key.value, v.value)
|
22
|
+
end
|
23
|
+
end
|
data/lib/toml/parser.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module TOML
|
2
|
+
class Parser
|
3
|
+
attr_reader :hash
|
4
|
+
|
5
|
+
def initialize(content, options = {})
|
6
|
+
@hash = {}
|
7
|
+
@current = @hash
|
8
|
+
|
9
|
+
parsed = Document.parse(content)
|
10
|
+
parsed.matches.map(&:value).compact.each do |match|
|
11
|
+
if match.is_a? Keygroup
|
12
|
+
@current = match.navigate_keys(@hash, options[:symbolize_keys])
|
13
|
+
elsif match.is_a? Keyvalue
|
14
|
+
match.assign(@current, options[:symbolize_keys])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/toml/string.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Used in primitive.citrus
|
2
|
+
module TomlString
|
3
|
+
def value
|
4
|
+
aux = first.value
|
5
|
+
s = 0
|
6
|
+
o = []
|
7
|
+
while s < aux.length
|
8
|
+
if aux[s] == "\\"
|
9
|
+
s += 1
|
10
|
+
case aux[s]
|
11
|
+
when "t" then o << "\t"
|
12
|
+
when "n" then o << "\n"
|
13
|
+
when "\\" then o << "\\"
|
14
|
+
when '"' then o << '"'
|
15
|
+
when "r" then o << "\r"
|
16
|
+
when "0" then o << "\0"
|
17
|
+
else
|
18
|
+
o << '\\' << aux[s]
|
19
|
+
end
|
20
|
+
else
|
21
|
+
o << aux[s]
|
22
|
+
end
|
23
|
+
s += 1
|
24
|
+
end
|
25
|
+
o[1...-1].join
|
26
|
+
end
|
27
|
+
end
|
data/test/example.toml
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# This is a TOML document. Boom.
|
2
|
+
|
3
|
+
title = "TOML Example"
|
4
|
+
|
5
|
+
[owner]
|
6
|
+
name = "Tom Preston-Werner"
|
7
|
+
organization = "GitHub"
|
8
|
+
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
|
9
|
+
dob = 1979-05-27T07:32:00Z # First class dates? Why not?
|
10
|
+
|
11
|
+
[database]
|
12
|
+
server = "192.168.1.1"
|
13
|
+
ports = [ 8001, 8001, 8002 ]
|
14
|
+
connection_max = 5000
|
15
|
+
enabled = true
|
16
|
+
|
17
|
+
[servers]
|
18
|
+
|
19
|
+
# You can indent as you please. Tabs or spaces. TOML don't care.
|
20
|
+
[servers.alpha]
|
21
|
+
ip = "10.0.0.1"
|
22
|
+
dc = "eqdc10"
|
23
|
+
|
24
|
+
[servers.beta]
|
25
|
+
ip = "10.0.0.2"
|
26
|
+
dc = "eqdc10"
|
27
|
+
|
28
|
+
[servers]
|
29
|
+
amount = 2
|
30
|
+
|
31
|
+
[clients]
|
32
|
+
data = [ ["gamma", "delta"], [1, 2] ]
|
33
|
+
|
34
|
+
# Line breaks are OK when inside arrays
|
35
|
+
hosts = [
|
36
|
+
"alpha",
|
37
|
+
"omega"
|
38
|
+
]
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require_relative "helper"
|
2
|
+
|
3
|
+
class GrammarTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_comment
|
6
|
+
match = Document.parse(" # A comment", root: :comment)
|
7
|
+
assert_equal(nil, match.value)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_keygroup
|
11
|
+
indentation_alternatives_for('[akey]') do |str|
|
12
|
+
match = Document.parse(str, root: :keygroup)
|
13
|
+
assert_equal(TOML::Keygroup, match.value.class)
|
14
|
+
assert_equal(['akey'], match.value.instance_variable_get("@nested_keys"))
|
15
|
+
end
|
16
|
+
|
17
|
+
match = Document.parse('[owner.emancu]', root: :keygroup)
|
18
|
+
assert_equal(['owner', 'emancu'], match.value.instance_variable_get("@nested_keys"))
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_keyvalue
|
22
|
+
indentation_alternatives_for('key = "value"') do |str|
|
23
|
+
match = Document.parse(str, root: :keyvalue)
|
24
|
+
assert_equal(TOML::Keyvalue, match.value.class)
|
25
|
+
|
26
|
+
keyvalue = match.value
|
27
|
+
assert_equal('key', keyvalue.instance_variable_get("@key"))
|
28
|
+
assert_equal('value', keyvalue.instance_variable_get("@value"))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_string
|
33
|
+
match = Document.parse('"TOML-Example, should work."', root: :string)
|
34
|
+
assert_equal("TOML-Example, should work.", match.value)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_special_characters
|
38
|
+
match = Document.parse('"\0 \" \t \n \r"', root: :string)
|
39
|
+
assert_equal("\0 \" \t \n \r", match.value)
|
40
|
+
|
41
|
+
match = Document.parse('"C:\\Documents\\virus.exe"', root: :string)
|
42
|
+
assert_equal("C:\\Documents\\virus.exe", match.value)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_bool
|
46
|
+
match = Document.parse('true', root: :bool)
|
47
|
+
assert_equal(true, match.value)
|
48
|
+
|
49
|
+
match = Document.parse('false', root: :bool)
|
50
|
+
assert_equal(false, match.value)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_integer
|
54
|
+
match = Document.parse('26', root: :number)
|
55
|
+
assert_equal(26, match.value)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_float
|
59
|
+
match = Document.parse('1.69', root: :number)
|
60
|
+
assert_equal(1.69, match.value)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_signed_numbers
|
64
|
+
match = Document.parse('+26', root: :number)
|
65
|
+
assert_equal(26, match.value)
|
66
|
+
|
67
|
+
match = Document.parse('-26', root: :number)
|
68
|
+
assert_equal(-26, match.value)
|
69
|
+
|
70
|
+
match = Document.parse('1.69', root: :number)
|
71
|
+
assert_equal(1.69, match.value)
|
72
|
+
|
73
|
+
match = Document.parse('-1.69', root: :number)
|
74
|
+
assert_equal(-1.69, match.value)
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_expressions_with_comments
|
78
|
+
match = Document.parse('[shouldwork] # with comment', root: :keygroup)
|
79
|
+
assert_equal(['shouldwork'], match.value.instance_variable_get("@nested_keys"))
|
80
|
+
|
81
|
+
match = Document.parse('works = true # with comment', root: :keyvalue).value
|
82
|
+
assert_equal("works", match.instance_variable_get("@key"))
|
83
|
+
assert_equal(true, match.instance_variable_get("@value"))
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_array
|
87
|
+
match = Document.parse('[]', root: :array)
|
88
|
+
assert_equal([], match.value)
|
89
|
+
|
90
|
+
match = Document.parse('[ 2, 4]', root: :array)
|
91
|
+
assert_equal([2,4], match.value)
|
92
|
+
|
93
|
+
match = Document.parse('[ 2.4, 4.72]', root: :array)
|
94
|
+
assert_equal([2.4,4.72], match.value)
|
95
|
+
|
96
|
+
match = Document.parse('[ "hey", "TOML"]', root: :array)
|
97
|
+
assert_equal(["hey","TOML"], match.value)
|
98
|
+
|
99
|
+
match = Document.parse('[ ["hey", "TOML"], [2,4] ]', root: :array)
|
100
|
+
assert_equal([["hey","TOML"], [2,4]], match.value)
|
101
|
+
|
102
|
+
multiline_array = "[ \"hey\",\n \"ho\",\n\t \"lets\", \"go\",\n ]"
|
103
|
+
match = Document.parse(multiline_array, root: :array)
|
104
|
+
assert_equal(["hey", "ho", "lets", "go"], match.value)
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_datetime
|
108
|
+
match = Document.parse('1986-08-28T15:15:00Z', root: :datetime)
|
109
|
+
assert_equal(Time.utc(1986,8,28,15,15), match.value)
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
# Creates all the alternatives of valid indentations to test
|
115
|
+
def indentation_alternatives_for(str)
|
116
|
+
[str, " #{str}", "\t#{str}", "\t\t#{str}"].each do |alternative|
|
117
|
+
yield(alternative)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# Test file for TOML
|
2
|
+
# Only this one tries to emulate a TOML file written by a user of the kind of parser writers probably hate
|
3
|
+
# This part you'll really hate
|
4
|
+
|
5
|
+
[the]
|
6
|
+
test_string = "You'll hate me after this - #" # " Annoying, isn't it?
|
7
|
+
|
8
|
+
[the.hard]
|
9
|
+
test_array = [ "] ", " # "] # ] There you go, parse this!
|
10
|
+
test_array2 = [ "Test #11 ]proved that", "Experiment #9 was a success" ]
|
11
|
+
# You didn't think it'd as easy as chucking out the last #, did you?
|
12
|
+
another_test_string = " Same thing, but with a string #"
|
13
|
+
harder_test_string = " And when \"'s are in the string, along with # \"" # "and comments are there too"
|
14
|
+
# Things will get harder
|
15
|
+
|
16
|
+
[the.hard.bit#]
|
17
|
+
what? = "You don't think some user won't do that?"
|
18
|
+
multi_line_array = [
|
19
|
+
"]",
|
20
|
+
# ] Oh yes I did
|
21
|
+
]
|
22
|
+
|
23
|
+
# Each of the following keygroups/key value pairs should produce an error. Uncomment to them to test
|
data/test/helper.rb
ADDED
data/test/toml_test.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
class TomlTest < Test::Unit::TestCase
|
4
|
+
def test_file
|
5
|
+
path = File.join(File.dirname(__FILE__), 'example.toml')
|
6
|
+
parsed = TOML.load_file(path)
|
7
|
+
|
8
|
+
hash = {
|
9
|
+
"title" => "TOML Example",
|
10
|
+
|
11
|
+
"owner" => {
|
12
|
+
"name" => "Tom Preston-Werner",
|
13
|
+
"organization" => "GitHub",
|
14
|
+
"bio" => "GitHub Cofounder & CEO\nLikes tater tots and beer.",
|
15
|
+
"dob" => Time.utc(1979,05,27,07,32,00)
|
16
|
+
},
|
17
|
+
|
18
|
+
"database" => {
|
19
|
+
"server" => "192.168.1.1",
|
20
|
+
"ports" => [ 8001, 8001, 8002 ],
|
21
|
+
"connection_max" => 5000,
|
22
|
+
"enabled" => true
|
23
|
+
},
|
24
|
+
|
25
|
+
"servers" => {
|
26
|
+
"amount" => 2,
|
27
|
+
"alpha" => {
|
28
|
+
"ip" => "10.0.0.1",
|
29
|
+
"dc" => "eqdc10"
|
30
|
+
},
|
31
|
+
"beta" => {
|
32
|
+
"ip" => "10.0.0.2",
|
33
|
+
"dc" => "eqdc10"
|
34
|
+
}
|
35
|
+
},
|
36
|
+
|
37
|
+
"clients" => {
|
38
|
+
"data" => [["gamma", "delta"], [1, 2]],
|
39
|
+
"hosts" => ["alpha", "omega"]
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
assert_equal(hash, parsed)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_hard_example
|
47
|
+
path = File.join(File.dirname(__FILE__), 'hard_example.toml')
|
48
|
+
parsed = TOML.load_file(path)
|
49
|
+
|
50
|
+
hash = {
|
51
|
+
"the" => {
|
52
|
+
"test_string" => "You'll hate me after this - #",
|
53
|
+
"hard" => {
|
54
|
+
"test_array" => [ "] ", " # "],
|
55
|
+
"test_array2" => [ "Test #11 ]proved that", "Experiment #9 was a success" ],
|
56
|
+
"another_test_string" => " Same thing, but with a string #",
|
57
|
+
"harder_test_string" => " And when \"'s are in the string, along with # \"",
|
58
|
+
"bit#" => {
|
59
|
+
"what?" => "You don't think some user won't do that?",
|
60
|
+
"multi_line_array" => [ "]" ]
|
61
|
+
}
|
62
|
+
}
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
assert_equal(hash, parsed)
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_symbolize_keys
|
70
|
+
path = File.join(File.dirname(__FILE__), 'example.toml')
|
71
|
+
parsed = TOML.load_file(path, symbolize_keys: true)
|
72
|
+
|
73
|
+
hash = {
|
74
|
+
title: "TOML Example",
|
75
|
+
|
76
|
+
owner: {
|
77
|
+
name: "Tom Preston-Werner",
|
78
|
+
organization: "GitHub",
|
79
|
+
bio: "GitHub Cofounder & CEO\nLikes tater tots and beer.",
|
80
|
+
dob: Time.utc(1979,05,27,07,32,00)
|
81
|
+
},
|
82
|
+
|
83
|
+
database: {
|
84
|
+
server: "192.168.1.1",
|
85
|
+
ports: [ 8001, 8001, 8002 ],
|
86
|
+
connection_max: 5000,
|
87
|
+
enabled: true
|
88
|
+
},
|
89
|
+
|
90
|
+
servers: {
|
91
|
+
amount: 2,
|
92
|
+
alpha: {
|
93
|
+
ip: "10.0.0.1",
|
94
|
+
dc: "eqdc10"
|
95
|
+
},
|
96
|
+
beta: {
|
97
|
+
ip: "10.0.0.2",
|
98
|
+
dc: "eqdc10"
|
99
|
+
}
|
100
|
+
},
|
101
|
+
|
102
|
+
clients: {
|
103
|
+
data: [["gamma", "delta"], [1, 2]],
|
104
|
+
hosts: ["alpha", "omega"]
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
assert_equal(hash, parsed)
|
109
|
+
end
|
110
|
+
end
|
data/toml-rb.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'toml-rb'
|
3
|
+
s.version = '0.1.0'
|
4
|
+
s.date = Time.now.strftime('%Y-%m-%d')
|
5
|
+
s.summary = "TOML parser in ruby, for ruby."
|
6
|
+
s.description = "A TOML parser using Citrus parsing library. Formerly known as 'toml_parser-ruby'. "
|
7
|
+
s.authors = ["Emiliano Mancuso", "Lucas Tolchinsky"]
|
8
|
+
s.email = ['emiliano.mancuso@gmail.com', 'lucas.tolchinsky@gmail.com']
|
9
|
+
s.homepage = 'http://github.com/eMancu/toml-rb'
|
10
|
+
s.license = "MIT"
|
11
|
+
|
12
|
+
s.files = Dir[
|
13
|
+
"README.md",
|
14
|
+
"Rakefile",
|
15
|
+
"lib/**/*.rb",
|
16
|
+
"lib/**/*.citrus",
|
17
|
+
"*.gemspec",
|
18
|
+
"test/*.*"
|
19
|
+
]
|
20
|
+
|
21
|
+
s.add_dependency "citrus"
|
22
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'toml_parser-ruby'
|
3
|
+
s.version = '0.1.0'
|
4
|
+
s.date = Time.now.strftime('%Y-%m-%d')
|
5
|
+
s.summary = "TOML parser in ruby, for ruby."
|
6
|
+
s.description = "DEPRECATED by 'toml-rb' gem.\n A TOML parser using Citrus parsing library"
|
7
|
+
s.authors = ["Emiliano Mancuso"]
|
8
|
+
s.email = 'emiliano.mancuso@gmail.com'
|
9
|
+
s.homepage = 'http://github.com/eMancu/toml_parser-ruby'
|
10
|
+
s.license = "MIT"
|
11
|
+
|
12
|
+
s.files = Dir[
|
13
|
+
"README.md",
|
14
|
+
"Rakefile",
|
15
|
+
"lib/**/*.rb",
|
16
|
+
"*.gemspec",
|
17
|
+
"test/*.*"
|
18
|
+
]
|
19
|
+
|
20
|
+
s.add_dependency "citrus"
|
21
|
+
|
22
|
+
|
23
|
+
s.post_install_message = <<-MESSAGE
|
24
|
+
! The 'toml_parser-ruby' gem has been deprecated and has been replaced by 'toml-rb'.
|
25
|
+
! See: https://rubygems.org/gems/toml-rb
|
26
|
+
! And: https://github.com/eMancu/toml-rb
|
27
|
+
MESSAGE
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: toml-rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Emiliano Mancuso
|
8
|
+
- Lucas Tolchinsky
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: citrus
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ! '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ! '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
description: ! 'A TOML parser using Citrus parsing library. Formerly known as ''toml_parser-ruby''. '
|
29
|
+
email:
|
30
|
+
- emiliano.mancuso@gmail.com
|
31
|
+
- lucas.tolchinsky@gmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- lib/toml/keygroup.rb
|
39
|
+
- lib/toml/keyvalue.rb
|
40
|
+
- lib/toml/parser.rb
|
41
|
+
- lib/toml/string.rb
|
42
|
+
- lib/toml.rb
|
43
|
+
- lib/toml/grammars/array.citrus
|
44
|
+
- lib/toml/grammars/document.citrus
|
45
|
+
- lib/toml/grammars/helper.citrus
|
46
|
+
- lib/toml/grammars/primitive.citrus
|
47
|
+
- toml-rb.gemspec
|
48
|
+
- toml_parser-ruby.gemspec
|
49
|
+
- test/example.toml
|
50
|
+
- test/grammar_test.rb
|
51
|
+
- test/hard_example.toml
|
52
|
+
- test/helper.rb
|
53
|
+
- test/toml_test.rb
|
54
|
+
homepage: http://github.com/eMancu/toml-rb
|
55
|
+
licenses:
|
56
|
+
- MIT
|
57
|
+
metadata: {}
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 2.0.3
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: TOML parser in ruby, for ruby.
|
78
|
+
test_files: []
|