tomlrb 1.2.7 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tomlrb.rb +5 -2
- data/lib/tomlrb/generated_parser.rb +376 -257
- data/lib/tomlrb/handler.rb +161 -3
- data/lib/tomlrb/local_date.rb +33 -0
- data/lib/tomlrb/local_date_time.rb +40 -0
- data/lib/tomlrb/local_time.rb +38 -0
- data/lib/tomlrb/parser.rb +1 -1
- data/lib/tomlrb/parser.y +100 -25
- data/lib/tomlrb/scanner.rb +35 -21
- data/lib/tomlrb/string_utils.rb +7 -1
- data/lib/tomlrb/version.rb +1 -1
- metadata +7 -15
- data/.gitignore +0 -9
- data/.travis.yml +0 -5
- data/CHANGELOG.md +0 -12
- data/Gemfile +0 -13
- data/README.md +0 -80
- data/Rakefile +0 -14
- data/benchmarks/bench.rb +0 -22
- data/bin/console +0 -10
- data/bin/setup +0 -6
- data/tomlrb.gemspec +0 -22
data/lib/tomlrb/scanner.rb
CHANGED
@@ -2,53 +2,67 @@ require 'strscan'
|
|
2
2
|
|
3
3
|
module Tomlrb
|
4
4
|
class Scanner
|
5
|
-
COMMENT =
|
5
|
+
COMMENT = /#[^\u0000-\u0008\u000A-\u001F\u007F]*/
|
6
6
|
IDENTIFIER = /[A-Za-z0-9_-]+/
|
7
|
-
SPACE = /[ \t
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
SPACE = /[ \t]/
|
8
|
+
NEWLINE = /(?:[ \t]*(?:\r?\n)[ \t]*)+/
|
9
|
+
STRING_BASIC = /(["])(?:\\?[^\u0000-\u0008\u000A-\u001F\u007F])*?\1/
|
10
|
+
STRING_MULTI = /"{3}([^\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]*?(?<!\\)"{3,5})/m
|
11
|
+
STRING_LITERAL = /(['])(?:\\?[^\u0000-\u0008\u000A-\u001F\u007F])*?\1/
|
12
|
+
STRING_LITERAL_MULTI = /'{3}([^\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]*?'{3,5})/m
|
12
13
|
DATETIME = /(-?\d{4})-(\d{2})-(\d{2})(?:(?:t|\s)(\d{2}):(\d{2}):(\d{2}(?:\.\d+)?))?(z|[-+]\d{2}:\d{2})?/i
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
LOCAL_TIME = /(\d{2}):(\d{2}):(\d{2}(?:\.\d+)?)/
|
15
|
+
FLOAT = /[+-]?(?:(?:\d|[1-9](?:_?\d)*)\.\d(?:_?\d)*|\d+(?=[eE]))(?:[eE][+-]?[0-9_]+)?(?!\w)/
|
16
|
+
FLOAT_KEYWORD = /[+-]?(?:inf|nan)\b/
|
17
|
+
INTEGER = /[+-]?([1-9](_?\d)*|0)(?![A-Za-z0-9_-]+)/
|
18
|
+
NON_DEC_INTEGER = /0(?:x[0-9A-Fa-f][0-9A-Fa-f_]*|o[0-7][0-7_]*|b[01][01_]*)/
|
19
|
+
BOOLEAN = /true|false/
|
17
20
|
|
18
21
|
def initialize(io)
|
19
22
|
@ss = StringScanner.new(io.read)
|
23
|
+
@eos = false
|
20
24
|
end
|
21
25
|
|
22
26
|
def next_token
|
23
|
-
return if @ss.eos?
|
24
|
-
|
25
27
|
case
|
28
|
+
when @ss.scan(NEWLINE) then [:NEWLINE, nil]
|
26
29
|
when @ss.scan(SPACE) then next_token
|
27
30
|
when @ss.scan(COMMENT) then next_token
|
28
31
|
when @ss.scan(DATETIME) then process_datetime
|
32
|
+
when @ss.scan(LOCAL_TIME) then process_local_time
|
29
33
|
when text = @ss.scan(STRING_MULTI) then [:STRING_MULTI, text[3..-4]]
|
30
34
|
when text = @ss.scan(STRING_BASIC) then [:STRING_BASIC, text[1..-2]]
|
31
35
|
when text = @ss.scan(STRING_LITERAL_MULTI) then [:STRING_LITERAL_MULTI, text[3..-4]]
|
32
36
|
when text = @ss.scan(STRING_LITERAL) then [:STRING_LITERAL, text[1..-2]]
|
33
37
|
when text = @ss.scan(FLOAT) then [:FLOAT, text]
|
38
|
+
when text = @ss.scan(FLOAT_KEYWORD) then [:FLOAT_KEYWORD, text]
|
34
39
|
when text = @ss.scan(INTEGER) then [:INTEGER, text]
|
35
|
-
when text = @ss.scan(
|
36
|
-
when text = @ss.scan(
|
40
|
+
when text = @ss.scan(NON_DEC_INTEGER) then [:NON_DEC_INTEGER, text]
|
41
|
+
when text = @ss.scan(BOOLEAN) then [:BOOLEAN, text]
|
37
42
|
when text = @ss.scan(IDENTIFIER) then [:IDENTIFIER, text]
|
38
|
-
|
39
|
-
|
40
|
-
[x, x]
|
43
|
+
when @ss.eos? then process_eos
|
44
|
+
else x = @ss.getch; [x, x]
|
41
45
|
end
|
42
46
|
end
|
43
47
|
|
44
48
|
def process_datetime
|
45
|
-
if @ss[7]
|
46
|
-
offset = '+00:00'
|
47
|
-
else
|
49
|
+
if @ss[7]
|
48
50
|
offset = @ss[7].gsub('Z', '+00:00')
|
49
51
|
end
|
50
|
-
args = [@ss[1], @ss[2], @ss[3], @ss[4]
|
52
|
+
args = [@ss[1], @ss[2], @ss[3], @ss[4], @ss[5], @ss[6], offset]
|
51
53
|
[:DATETIME, args]
|
52
54
|
end
|
55
|
+
|
56
|
+
def process_local_time
|
57
|
+
args = [@ss[1], @ss[2], @ss[3].to_f]
|
58
|
+
[:LOCAL_TIME, args]
|
59
|
+
end
|
60
|
+
|
61
|
+
def process_eos
|
62
|
+
return if @eos
|
63
|
+
|
64
|
+
@eos = true
|
65
|
+
[:EOS, nil]
|
66
|
+
end
|
53
67
|
end
|
54
68
|
end
|
data/lib/tomlrb/string_utils.rb
CHANGED
@@ -12,7 +12,13 @@ module Tomlrb
|
|
12
12
|
}.freeze
|
13
13
|
|
14
14
|
def self.multiline_replacements(str)
|
15
|
-
strip_spaces(str).gsub(
|
15
|
+
strip_spaces(str).gsub(/\\+\s*\n\s*/) {|matched|
|
16
|
+
if matched.match(/\\+/)[0].length.odd?
|
17
|
+
matched.gsub(/\\\s*\n\s*/, '')
|
18
|
+
else
|
19
|
+
matched
|
20
|
+
end
|
21
|
+
}
|
16
22
|
end
|
17
23
|
|
18
24
|
def self.replace_escaped_chars(str)
|
data/lib/tomlrb/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tomlrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Francois Bernier
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A racc based toml parser
|
14
14
|
email:
|
@@ -17,25 +17,18 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
-
- ".gitignore"
|
21
|
-
- ".travis.yml"
|
22
|
-
- CHANGELOG.md
|
23
|
-
- Gemfile
|
24
20
|
- LICENSE.txt
|
25
|
-
- README.md
|
26
|
-
- Rakefile
|
27
|
-
- benchmarks/bench.rb
|
28
|
-
- bin/console
|
29
|
-
- bin/setup
|
30
21
|
- lib/tomlrb.rb
|
31
22
|
- lib/tomlrb/generated_parser.rb
|
32
23
|
- lib/tomlrb/handler.rb
|
24
|
+
- lib/tomlrb/local_date.rb
|
25
|
+
- lib/tomlrb/local_date_time.rb
|
26
|
+
- lib/tomlrb/local_time.rb
|
33
27
|
- lib/tomlrb/parser.rb
|
34
28
|
- lib/tomlrb/parser.y
|
35
29
|
- lib/tomlrb/scanner.rb
|
36
30
|
- lib/tomlrb/string_utils.rb
|
37
31
|
- lib/tomlrb/version.rb
|
38
|
-
- tomlrb.gemspec
|
39
32
|
homepage: https://github.com/fbernier/tomlrb
|
40
33
|
licenses:
|
41
34
|
- MIT
|
@@ -55,8 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
48
|
- !ruby/object:Gem::Version
|
56
49
|
version: '0'
|
57
50
|
requirements: []
|
58
|
-
|
59
|
-
rubygems_version: 2.7.6
|
51
|
+
rubygems_version: 3.1.4
|
60
52
|
signing_key:
|
61
53
|
specification_version: 4
|
62
54
|
summary: A racc based toml parser
|
data/.gitignore
DELETED
data/.travis.yml
DELETED
data/CHANGELOG.md
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
### 1.2.7 - 2018-07-12
|
2
|
-
|
3
|
-
* Datetime should be UTC when no offset or timezone are specified
|
4
|
-
|
5
|
-
### 1.2.6 - 2017-10-23
|
6
|
-
|
7
|
-
* Fix issue where an unclosed table could make the parsing loop infinitely.
|
8
|
-
* Proper string escaping
|
9
|
-
|
10
|
-
### 1.1.3 - 2015-11-24
|
11
|
-
|
12
|
-
* Bare integers can be used as keys as per the spec
|
data/Gemfile
DELETED
data/README.md
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
# Tomlrb
|
2
|
-
|
3
|
-
[![Code Climate](https://codeclimate.com/github/fbernier/tomlrb/badges/gpa.svg)](https://codeclimate.com/github/fbernier/tomlrb) [![Build Status](https://travis-ci.org/fbernier/tomlrb.svg)](https://travis-ci.org/fbernier/tomlrb) [![Gem Version](https://badge.fury.io/rb/tomlrb.svg)](http://badge.fury.io/rb/tomlrb)
|
4
|
-
|
5
|
-
A Racc based [TOML](https://github.com/toml-lang/toml) Ruby parser supporting the 0.4.0 version of the spec.
|
6
|
-
|
7
|
-
|
8
|
-
## TODO
|
9
|
-
|
10
|
-
* Better tests
|
11
|
-
* Dumper
|
12
|
-
|
13
|
-
## Installation
|
14
|
-
|
15
|
-
Add this line to your application's Gemfile:
|
16
|
-
|
17
|
-
```ruby
|
18
|
-
gem 'tomlrb'
|
19
|
-
```
|
20
|
-
|
21
|
-
And then execute:
|
22
|
-
|
23
|
-
$ bundle
|
24
|
-
|
25
|
-
Or install it yourself as:
|
26
|
-
|
27
|
-
$ gem install tomlrb
|
28
|
-
|
29
|
-
## Usage
|
30
|
-
|
31
|
-
```ruby
|
32
|
-
Tomlrb.parse("[toml]\na = [\"array\", 123]")
|
33
|
-
```
|
34
|
-
|
35
|
-
or
|
36
|
-
|
37
|
-
```ruby
|
38
|
-
Tomlrb.load_file('my_file', symbolize_keys: true)
|
39
|
-
```
|
40
|
-
|
41
|
-
## Benchmark
|
42
|
-
|
43
|
-
You can run the benchmark against the only other v0.4.0 compliant parser to my knowledge with `ruby benchmarks/bench.rb`.
|
44
|
-
|
45
|
-
Here are the results on my machine:
|
46
|
-
|
47
|
-
```
|
48
|
-
Warming up --------------------------------------
|
49
|
-
emancu/toml-rb 1.000 i/100ms
|
50
|
-
fbernier/tomlrb 65.000 i/100ms
|
51
|
-
Calculating -------------------------------------
|
52
|
-
emancu/toml-rb 19.199 (± 5.2%) i/s - 96.000 in 5.034377s
|
53
|
-
fbernier/tomlrb 653.650 (± 4.0%) i/s - 3.315k in 5.081117s
|
54
|
-
|
55
|
-
Comparison:
|
56
|
-
fbernier/tomlrb: 653.6 i/s
|
57
|
-
emancu/toml-rb: 19.2 i/s - 34.05x slower
|
58
|
-
```
|
59
|
-
|
60
|
-
## Development
|
61
|
-
|
62
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
63
|
-
|
64
|
-
Do not forget to regenerate the parser when you modify rules in the `parser.y` file using `rake compile`.
|
65
|
-
|
66
|
-
Run the tests using `rake test`.
|
67
|
-
|
68
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
69
|
-
|
70
|
-
## Contributing
|
71
|
-
|
72
|
-
1. Fork it ( https://github.com/[my-github-username]/tomlrb/fork )
|
73
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
74
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
75
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
76
|
-
5. Create a new Pull Request
|
77
|
-
|
78
|
-
## Thanks
|
79
|
-
|
80
|
-
Thanks to [@jpbougie](https://github.com/jpbougie) for the crash course on the Chomsky hierarchy and general tips.
|
data/Rakefile
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
require 'rake/testtask'
|
3
|
-
|
4
|
-
Rake::TestTask.new do |t|
|
5
|
-
t.libs << "test"
|
6
|
-
t.test_files = FileList['test/test*.rb']
|
7
|
-
t.verbose = true
|
8
|
-
end
|
9
|
-
|
10
|
-
task default: [:test]
|
11
|
-
|
12
|
-
task :compile do
|
13
|
-
sh 'racc lib/tomlrb/parser.y -o lib/tomlrb/generated_parser.rb'
|
14
|
-
end
|
data/benchmarks/bench.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'benchmark/ips'
|
2
|
-
require_relative '../lib/tomlrb'
|
3
|
-
begin
|
4
|
-
require 'toml-rb'
|
5
|
-
rescue LoadError
|
6
|
-
puts "Install toml-rb using 'gem install toml-rb' first."
|
7
|
-
end
|
8
|
-
|
9
|
-
data = File.read(File.join(__dir__, '../test/example-v0.4.0.toml'))
|
10
|
-
|
11
|
-
Benchmark.ips do |x|
|
12
|
-
|
13
|
-
x.report("emancu/toml-rb") do
|
14
|
-
TomlRB.parse(data)
|
15
|
-
end
|
16
|
-
|
17
|
-
x.report("fbernier/tomlrb") do
|
18
|
-
Tomlrb.parse(data)
|
19
|
-
end
|
20
|
-
|
21
|
-
x.compare!
|
22
|
-
end
|
data/bin/console
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "tomlrb"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
require "byebug"
|
data/bin/setup
DELETED
data/tomlrb.gemspec
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'tomlrb/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "tomlrb"
|
8
|
-
spec.version = Tomlrb::VERSION
|
9
|
-
spec.authors = ["Francois Bernier"]
|
10
|
-
spec.email = ["frankbernier@gmail.com"]
|
11
|
-
|
12
|
-
spec.summary = %q{A racc based toml parser}
|
13
|
-
spec.description = %q{A racc based toml parser}
|
14
|
-
spec.homepage = "https://github.com/fbernier/tomlrb"
|
15
|
-
spec.license = "MIT"
|
16
|
-
|
17
|
-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
-
spec.bindir = "exe"
|
19
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
-
spec.require_paths = ["lib"]
|
21
|
-
spec.required_ruby_version = '>= 2.0'
|
22
|
-
end
|