toml-ruby 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1 @@
1
+ 1.9.3-p374
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in toml-ruby.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'minitest'
8
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dirk Gadsden
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Toml::Ruby
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'toml-ruby'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install toml-ruby
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ require "toml-ruby/parser"
2
+ require "toml-ruby/version"
3
+
4
+ module Toml
5
+ def self.load(io)
6
+ Parser.new.parse(io)
7
+ end
8
+ end
@@ -0,0 +1,114 @@
1
+ module Toml
2
+ class Parser
3
+ def initialize
4
+ # pass
5
+ end
6
+ def parse(io)
7
+ @key_group = []
8
+ @doc = Hash.new
9
+ io.each_line {|line| parse_line(line.strip) }
10
+ return @doc
11
+ end
12
+ def parse_line(line)
13
+ return if line.start_with? '#'
14
+
15
+ if line =~ /^\[([^\]]+)\]/
16
+ @key_group = $1.split('.')
17
+ elsif line =~/^(\S+)\s*=\s*([^#]+)\s*/
18
+ key = $1
19
+ pos, value = parse_value($2)
20
+ # TODO: Make sure has parsed entire line?
21
+ set(key, value)
22
+ elsif !line.empty?
23
+ raise "Syntax error: '#{line.inspect}'"
24
+ end
25
+ end# parse_line
26
+
27
+ def set(key, value)
28
+ # Destination hash
29
+ hash = @doc
30
+ # Nest into the key group
31
+ path = @key_group.dup
32
+ while k = path.shift
33
+ if hash[k].is_a? Hash
34
+ # pass
35
+ else
36
+ hash[k] = Hash.new
37
+ end
38
+ hash = hash[k]
39
+ end
40
+ # TODO: Detect if overwriting previous keys.
41
+ hash[key] = value
42
+ end
43
+
44
+ def parse_string(val)
45
+ e = val.length
46
+ s = 1
47
+ o = []
48
+ while s < e
49
+ # TODO: Formalize escape codes
50
+ if val[s] == "\\"
51
+ s += 1
52
+ case val[s]
53
+ when "t"
54
+ o << "\t"
55
+ when "n"
56
+ o << "\n"
57
+ when "\\"
58
+ o << "\\"
59
+ when '"'
60
+ o << '"'
61
+ when "r"
62
+ o << "\r"
63
+ when "0"
64
+ o << "\0"
65
+ else
66
+ raise "Unexpected escape character: '\\#{val[s]}'"
67
+ end
68
+ elsif val[s] == '"'
69
+ break
70
+ else
71
+ o << val[s]
72
+ end
73
+ s += 1
74
+ end
75
+ if s == e
76
+ raise "Unexpected end of string"
77
+ end
78
+ return [s + 1, o.join]
79
+ end
80
+
81
+ def parse_value(val)
82
+ val.strip! # Make sure no whitespace.
83
+
84
+ # TODO: Refactor this rotting code cesspool.
85
+ if val.start_with? '"'
86
+ return parse_string(val)
87
+ elsif val.start_with? '['
88
+ a = []
89
+ p = 1
90
+ while (val.slice!(0, p) && (val.strip! || true) && !val.start_with?("]"))
91
+ val.slice!(0) if val[0] == "," # Remove comma if it's there.
92
+ p, v = parse_value(val)
93
+ a << v
94
+ end
95
+ p += 1 # Move past the closing ]
96
+ return [p, a]
97
+ elsif val =~ /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)/
98
+ # Date: 1979-05-27T07:32:00Z
99
+ return [$1.length, DateTime.iso8601($1)]
100
+ elsif val =~ /^(-?\d+\.\d+)/
101
+ return [$1.length, $1.to_f]
102
+ elsif val =~ /^(-?\d+)/
103
+ return [$1.length, $1.to_i]
104
+ elsif val =~ /^(true)/
105
+ return [$1.length, true]
106
+ elsif val =~ /^(false)/
107
+ return [$1.length, false]
108
+ else
109
+ raise "Unrecognized expression: '#{val}'"
110
+ end
111
+ end
112
+
113
+ end
114
+ end
@@ -0,0 +1,3 @@
1
+ module Toml
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # Comment
2
+
3
+ # String
4
+ string = "string\n\t\"string"
5
+
6
+ # Integer
7
+ integer = 42
8
+
9
+ # Float
10
+ pi = 3.14159
11
+
12
+ # DateTime
13
+ datetime = 1979-05-27T07:32:00Z
14
+
15
+ # Simple array
16
+ simple_array = [1, 2, 3]
17
+
18
+ # Nested array
19
+ nested_array = [[[1], 2], 3]
20
+
21
+ # Keygroup
22
+ [a.b.c]
23
+ d = "test"
24
+
25
+ [e]
26
+ f = "test"
@@ -0,0 +1,38 @@
1
+
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+
5
+ require 'toml-ruby'
6
+ require 'minitest/autorun'
7
+
8
+ class TestParser < MiniTest::Unit::TestCase
9
+ def setup
10
+ filepath = File.join(File.dirname(__FILE__), 'spec.toml')
11
+ @doc = Toml.load(File.open(filepath))
12
+ end
13
+
14
+ def test_string
15
+ assert_equal @doc["string"], "string\n\t\"string"
16
+ end
17
+ def test_integer
18
+ assert_equal @doc["integer"], 42
19
+ end
20
+ def test_float
21
+ assert_equal @doc["pi"], 3.14159
22
+ end
23
+ def test_datetime
24
+ assert_equal @doc["datetime"], DateTime.iso8601("1979-05-27T07:32:00Z")
25
+ end
26
+ def test_simple_array
27
+ assert_equal @doc["simple_array"], [1, 2, 3]
28
+ end
29
+ def test_nested_array
30
+ assert_equal @doc["nested_array"], [[[1], 2], 3]
31
+ end
32
+ def test_simple_keygroup
33
+ assert_equal @doc["e"]["f"], "test"
34
+ end
35
+ def test_nested_keygroup
36
+ assert_equal @doc["a"]["b"]["c"]["d"], "test"
37
+ end
38
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'toml-ruby/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "toml-ruby"
8
+ gem.version = Toml::VERSION
9
+ gem.authors = ["Dirk Gadsden"]
10
+ gem.email = ["dirk@esherido.com"]
11
+ gem.description = "Library for parsing TOML inifile format"
12
+ gem.summary = "Library for parsing TOML inifile format"
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toml-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dirk Gadsden
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-24 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Library for parsing TOML inifile format
15
+ email:
16
+ - dirk@esherido.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rbenv-version
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/toml-ruby.rb
28
+ - lib/toml-ruby/parser.rb
29
+ - lib/toml-ruby/version.rb
30
+ - test/spec.toml
31
+ - test/test.rb
32
+ - toml-ruby.gemspec
33
+ homepage: ''
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.23
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Library for parsing TOML inifile format
57
+ test_files:
58
+ - test/spec.toml
59
+ - test/test.rb