tomlp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tomlp.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sandeep Ravichandran
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,45 @@
1
+ # TOMLp
2
+
3
+ TOMLp is a library to parse the [TOML configuration format](https://github.com/mojombo/toml).
4
+
5
+ It has a fairly exhaustive test coverage and is compliant with the latest revision of TOML specifications.
6
+
7
+ _**This is not yet production ready.**_
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'tomlp'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tomlp
22
+
23
+ ## Usage
24
+
25
+ Straightforward.
26
+
27
+ content = '
28
+ [key]
29
+ something = "value"
30
+ '
31
+
32
+ TOMLP.parse(content)
33
+ # => {"key" => {"something" => "value"}
34
+
35
+ Or you could also directly load the contents from a file.
36
+
37
+ TOMLP.load("path_to_your_toml_file")
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ require "tomlp/version"
2
+ require "tomlp/parser"
3
+
4
+ module TOMLP
5
+ VERSION = "0.0.1"
6
+
7
+ extend self
8
+
9
+ # Public: Parse the text passed as arguement
10
+ #
11
+ # content - A String containing the entire content of the File in text format
12
+ #
13
+ # Returns the parsed Data Structure
14
+ def parse(content)
15
+ TOMLP::Parser.new(content).parse
16
+ end
17
+
18
+ # Public: Parse the contents of the file that is passed as arguement
19
+ #
20
+ # filename - A String containing the filename that needs to be parsed
21
+ #
22
+ # Returns the parsed Data Structure
23
+ def load(filename)
24
+ file_content = File.open(filename).read
25
+ TOMLP::Parser.new(file_content).parse
26
+ end
27
+ end
@@ -0,0 +1,41 @@
1
+ require 'treetop'
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__), 'token_extensions.rb'))
4
+
5
+ module TOMLP
6
+
7
+ require 'treetop'
8
+ Treetop.load(File.expand_path(File.join(File.dirname(__FILE__), 'tomlp_grammar.treetop')))
9
+
10
+ # Methods which help in parsing the content
11
+ #
12
+ class Parser
13
+
14
+ @@parser = TomlParser.new
15
+
16
+ # Public: Initialize a Parser.
17
+ #
18
+ # name - A String stroring the contents of the file
19
+ def initialize(content)
20
+ @content = content.dup
21
+ end
22
+
23
+ # Public: Method to parse the document.
24
+ #
25
+ # Returns the parsed Data Structure
26
+ def parse
27
+ @content += "\n" unless @content.end_with?("\n")
28
+ tree = @@parser.parse(@content)
29
+ Parser.clean_tree(tree)
30
+ tree.evaluate
31
+ end
32
+
33
+ private
34
+
35
+ def self.clean_tree(root_node)
36
+ return if(root_node.elements.nil?)
37
+ root_node.elements.delete_if{|node| node.class.name == "Treetop::Runtime::SyntaxNode" }
38
+ root_node.elements.each {|node| self.clean_tree(node) }
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,184 @@
1
+ module Toml
2
+
3
+ require 'date'
4
+
5
+ class IntegerLiteral < Treetop::Runtime::SyntaxNode
6
+ def evaluate
7
+ return self.text_value.to_i
8
+ end
9
+ end
10
+
11
+ class StringLiteral < Treetop::Runtime::SyntaxNode
12
+ def evaluate
13
+ string = self.text_value
14
+ output = ""
15
+ length = self.text_value.length
16
+ count = 0
17
+ while count < length
18
+ if string[count] == "\\"
19
+ count += 1
20
+ case string[count]
21
+ when "t"
22
+ output << "\t"
23
+ when "n"
24
+ output << "\n"
25
+ when "n"
26
+ output << '"'
27
+ when '"'
28
+ output << "\\"
29
+ when "r"
30
+ output << "\r"
31
+ end
32
+ elsif string[count] != "\""
33
+ output << string[count]
34
+ end
35
+ count += 1
36
+ end
37
+ return output
38
+ end
39
+ end
40
+
41
+ class FloatLiteral < Treetop::Runtime::SyntaxNode
42
+ def evaluate
43
+ return self.text_value.to_f
44
+ end
45
+ end
46
+
47
+ class BooleanLiteral < Treetop::Runtime::SyntaxNode
48
+ def evaluate
49
+ return eval self.text_value
50
+ end
51
+ end
52
+
53
+ class TrueLiteral < Treetop::Runtime::SyntaxNode
54
+ def evaluate
55
+ return eval self.text_value
56
+ end
57
+ end
58
+
59
+ class FalseLiteral < Treetop::Runtime::SyntaxNode
60
+ def evaluate
61
+ return eval self.text_value
62
+ end
63
+ end
64
+
65
+ class DateTime < Treetop::Runtime::SyntaxNode
66
+ def evaluate
67
+ return ::DateTime.parse self.text_value
68
+ end
69
+ end
70
+
71
+ class ArrayDS < Treetop::Runtime::SyntaxNode
72
+ def evaluate
73
+ #array = self.text_value
74
+ #array[0..-2] if array[-1] == ","
75
+ return eval self.text_value
76
+ end
77
+ end
78
+
79
+ class Identifier < Treetop::Runtime::SyntaxNode
80
+ def evaluate
81
+ return self.text_value
82
+ end
83
+ end
84
+
85
+ class AssignmentOperator < Treetop::Runtime::SyntaxNode
86
+ def evaluate
87
+ return
88
+ end
89
+ end
90
+
91
+ class KeyValue < Treetop::Runtime::SyntaxNode
92
+ def evaluate
93
+ values = self.elements.map {|x| x.evaluate}
94
+ key_value = {}
95
+ key_value[values[0]] = values[2]
96
+ return key_value
97
+ end
98
+ end
99
+
100
+ class KeyValueList < Treetop::Runtime::SyntaxNode
101
+ def evaluate
102
+ key_value_list = {}
103
+ self.elements.each do |x|
104
+ key_value_list.merge!(x.evaluate)
105
+ end
106
+ return key_value_list
107
+ end
108
+ end
109
+
110
+ class KeyGroup < Treetop::Runtime::SyntaxNode
111
+ def evaluate
112
+ self.elements.map {|x| x.evaluate}
113
+ end
114
+ end
115
+
116
+ class KeyName < Treetop::Runtime::SyntaxNode
117
+ def evaluate
118
+ self.text_value.split(".")
119
+ end
120
+ end
121
+
122
+ class CommentLine < Treetop::Runtime::SyntaxNode
123
+ def evaluate
124
+ return self.text_value
125
+ end
126
+ end
127
+
128
+ class Expression < Treetop::Runtime::SyntaxNode
129
+ def evaluate
130
+ return self.text_value
131
+ end
132
+ end
133
+
134
+ class Body < Treetop::Runtime::SyntaxNode
135
+
136
+ def evaluate
137
+ parse(self.elements.map {|x| x.evaluate})
138
+ end
139
+
140
+ # TODO:: Refactor this method
141
+ def parse(ds)
142
+ final_hash = {}
143
+ current_key, inner_key = nil, nil
144
+ current_hash = final_hash
145
+ ds.each_with_index do |value, index|
146
+ if value.is_a? Hash
147
+ if current_key == nil
148
+ current_hash.merge!(value)
149
+ elsif inner_key
150
+ current_hash[inner_key].merge!(value)
151
+ else
152
+ current_hash[current_key].merge!(value)
153
+ end
154
+ elsif value.is_a? Array
155
+ value = value.first
156
+ parent_key = value.count > 1 ? value[-2] : value.first
157
+ if parent_key == current_key
158
+ current_hash = final_hash[current_key]
159
+ inner_key = value.last
160
+ current_hash[inner_key] = {}
161
+ elsif inner_key && parent_key == inner_key
162
+ current_hash = current_hash[inner_key]
163
+ inner_key = value.last
164
+ current_hash[inner_key] = {}
165
+ elsif value.count > 1
166
+ value.each_with_index do |key, nindex|
167
+ current_hash[key] = {}
168
+ current_hash = current_hash[key] if nindex + 1 < value.count
169
+ end
170
+ inner_key = value.last
171
+ current_key = value.first
172
+ else
173
+ current_hash = final_hash
174
+ current_key = value.first
175
+ inner_key = nil
176
+ final_hash[current_key] = {}
177
+ end
178
+ end
179
+ end
180
+ final_hash
181
+ end
182
+ end
183
+
184
+ end
@@ -0,0 +1,135 @@
1
+ grammar Toml
2
+
3
+ rule body
4
+ ( key_groups / key_value / comment_line / end_of_line )+ <Body>
5
+ end
6
+
7
+ ##################
8
+ # Key groups
9
+
10
+ rule key_groups
11
+ space? '[' key_name ']' end_of_line <KeyGroup>
12
+ end
13
+
14
+ rule key_value_list
15
+ key_value+ <KeyValueList>
16
+ end
17
+
18
+ ##################
19
+ # Key values
20
+
21
+ rule key_value
22
+ space? identifier space? assignment_operator space? value (comment / end_of_line) <KeyValue>
23
+ end
24
+
25
+ rule key_name
26
+ identifier ('.' identifier)* <KeyName>
27
+ end
28
+
29
+ rule value
30
+ primitive / array
31
+ end
32
+
33
+ ##################
34
+ # Primitives
35
+
36
+ rule primitive
37
+ float / datetime / boolean / integer / string
38
+ end
39
+
40
+ rule boolean
41
+ true / false
42
+ end
43
+
44
+ rule integer
45
+ ('+' / '-')? [0-9]+ <IntegerLiteral>
46
+ end
47
+
48
+ rule float
49
+ ('+' / '-')? [0-9]+ '.' [0-9]+ <FloatLiteral>
50
+ end
51
+
52
+ rule string
53
+ '"' ([^"\\] / "\\" . )* '"' <StringLiteral>
54
+ end
55
+
56
+ rule identifier
57
+ '$'? [a-zA-Z] [a-zA-Z0-9_]* ('?' / '!')? <Identifier>
58
+ end
59
+
60
+ ##################
61
+ # Array
62
+
63
+ rule array
64
+ '[' array_break (value array_break ',' array_break )* value array_break ']' <ArrayDS>
65
+ end
66
+
67
+ rule array_break
68
+ (space? / end_of_line?)
69
+ end
70
+
71
+ ##################
72
+ # Boolean
73
+
74
+ rule true
75
+ "true" <TrueLiteral>
76
+ end
77
+
78
+ rule false
79
+ "false" <FalseLiteral>
80
+ end
81
+
82
+ ##################
83
+ # DateTime
84
+
85
+ rule datetime
86
+ date 'T' time 'Z' <DateTime>
87
+ end
88
+
89
+ rule date
90
+ [0-9]4..4 '-' [0-9]2..2 '-' [0-9]2..2
91
+ end
92
+
93
+ rule time
94
+ [0-9]2..2 ':' [0-9]2..2 ':' [0-9]2..2
95
+ end
96
+
97
+ ##################
98
+ # Operators
99
+
100
+ rule assignment_operator
101
+ '=' <AssignmentOperator>
102
+ end
103
+
104
+ ##################
105
+ # Comments
106
+
107
+ rule comment_line
108
+ comment ( "\n" / end_of_file )
109
+ end
110
+
111
+ rule comment
112
+ space? '#' [^"\n"]*
113
+ end
114
+
115
+ ##################
116
+ # Whitespace
117
+
118
+ rule space
119
+ [\s]+
120
+ end
121
+
122
+ ##################
123
+ # End of line
124
+
125
+ rule end_of_line
126
+ ["^\n"]
127
+ end
128
+
129
+ ##################
130
+ # End of file
131
+
132
+ rule end_of_file
133
+ !.
134
+ end
135
+ end
@@ -0,0 +1,3 @@
1
+ module Tomlp
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
@@ -0,0 +1,77 @@
1
+ require "spec_helper"
2
+ require "tomlp"
3
+
4
+ describe TOMLP do
5
+
6
+ before do
7
+ filepath = File.join(File.dirname(__FILE__), 'spec.toml')
8
+ @content = File.open(filepath).read
9
+ @parsed = TOMLP.parse(@content)
10
+ end
11
+
12
+ describe "key values" do
13
+ describe "primitives" do
14
+ it "should parse integer" do
15
+ @parsed["number"].should == 38
16
+ end
17
+
18
+ it "should parse string" do
19
+ @parsed["string"].should == "This is a string."
20
+ end
21
+
22
+ it "should parse true" do
23
+ @parsed["true"].should == true
24
+ end
25
+
26
+ it "should parse false" do
27
+ @parsed["false"].should == false
28
+ end
29
+
30
+ it "should parse float" do
31
+ @parsed["float"].should == 0.57721
32
+ end
33
+
34
+ it "should parse datetime" do
35
+ @parsed["datetime"].should == DateTime.parse("1988-05-28")
36
+ end
37
+
38
+ it "should parse arrays" do
39
+ @parsed["array"].should == [1, 2, 3]
40
+ end
41
+ end
42
+ end
43
+
44
+ describe "integers" do
45
+ it "should parse positive integers" do
46
+ @parsed["integer"]["positive"].should == 38
47
+ end
48
+
49
+ it "should parse negative integers" do
50
+ @parsed["integer"]["negative"].should == -38
51
+ end
52
+ end
53
+
54
+ describe "arrays" do
55
+ it "should parse differnt primitives" do
56
+ @parsed["arrays"]["normal"].should == ["a","b","c"]
57
+ end
58
+
59
+ it "should parse multi arrays" do
60
+ @parsed["arrays"]["multi"].should == [[1,2], 100, 20]
61
+ end
62
+
63
+ it "should parse arrays with line break" do
64
+ @parsed["arrays"]["break"].should == ["first", "second", "third"]
65
+ end
66
+ end
67
+
68
+ describe "keygroups" do
69
+ it "should parse keygroups in one line" do
70
+ @parsed["key"]["group"]["value"].should == 1
71
+ end
72
+
73
+ it "should parse nested keygroups" do
74
+ @parsed["keygroup"]["nested"]["new_value"].should == 12
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,39 @@
1
+ # Global
2
+
3
+ specification = "TOML"
4
+
5
+ string = "This is a string."
6
+
7
+ number = 38
8
+
9
+ float = 0.57721
10
+
11
+ true = true
12
+ false = false #comment at the end of a line
13
+
14
+ datetime = 1988-05-28T00:00:00Z
15
+
16
+ array = [1,2,3]
17
+
18
+ # Keygroups
19
+ [integer]
20
+ positive = 38
21
+ negative = -38
22
+
23
+ [arrays]
24
+ normal = ["a","b", "c"]
25
+ multi = [[1,2], 100, 20]
26
+ break = [
27
+ "first",
28
+ "second",
29
+ "third"
30
+ ]
31
+
32
+ # Nested Keygroups in one line
33
+ [key.group]
34
+ value = 1
35
+
36
+ # Nested keygroups
37
+ [keygroup]
38
+ [keygroup.nested]
39
+ new_value = 12
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tomlp/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "tomlp"
8
+ gem.version = Tomlp::VERSION
9
+ gem.authors = ["sandeepravi"]
10
+ gem.email = ["sandeepravi.bits@gmail.com"]
11
+ gem.summary = "A ruby parser for the TOML library."
12
+ gem.description = %q{Parse the TOML specificatin}
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
+
20
+ gem.add_dependency 'treetop'
21
+
22
+ gem.add_development_dependency "rspec"
23
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tomlp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - sandeepravi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: treetop
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Parse the TOML specificatin
47
+ email:
48
+ - sandeepravi.bits@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/tomlp.rb
60
+ - lib/tomlp/parser.rb
61
+ - lib/tomlp/token_extensions.rb
62
+ - lib/tomlp/tomlp_grammar.treetop
63
+ - lib/tomlp/version.rb
64
+ - spec/spec_helper.rb
65
+ - spec/tomlp/parser_spec.rb
66
+ - spec/tomlp/spec.toml
67
+ - tomlp.gemspec
68
+ homepage: ''
69
+ licenses: []
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.24
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: A ruby parser for the TOML library.
92
+ test_files:
93
+ - spec/spec_helper.rb
94
+ - spec/tomlp/parser_spec.rb
95
+ - spec/tomlp/spec.toml