tomlrb 1.3.0 → 2.0.4

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.
@@ -1,58 +1,92 @@
1
+ # frozen-string-literal: true
2
+
1
3
  require 'strscan'
2
4
 
3
5
  module Tomlrb
4
6
  class Scanner
5
- COMMENT = /#.*/
6
- IDENTIFIER = /[A-Za-z0-9_-]+/
7
- SPACE = /[ \t\r\n]/
8
- STRING_BASIC = /(["])(?:\\?.)*?\1/
9
- STRING_MULTI = /"{3}([\s\S]*?"{3,4})/m
10
- STRING_LITERAL = /(['])(?:\\?.)*?\1/
11
- STRING_LITERAL_MULTI = /'{3}([\s\S]*?'{3})/m
12
- DATETIME = /(-?\d{4})-(\d{2})-(\d{2})(?:(?:t|\s)(\d{2}):(\d{2}):(\d{2}(?:\.\d+)?))?(z|[-+]\d{2}:\d{2})?/i
13
- FLOAT = /[+-]?(?:[0-9_]+\.[0-9_]*|\d+(?=[eE]))(?:[eE][+-]?[0-9_]+)?/
14
- FLOAT_INF = /[+-]?inf/
15
- FLOAT_NAN = /[+-]?nan/
16
- INTEGER = /[+-]?([1-9](_?\d)*|0)(?![A-Za-z0-9_-]+)/
17
- TRUE = /true/
18
- FALSE = /false/
7
+ COMMENT =
8
+ /#[^\u0000-\u0008\u000A-\u001F\u007F]*/.freeze
9
+ IDENTIFIER =
10
+ /[A-Za-z0-9_-]+/.freeze
11
+ SPACE =
12
+ /[ \t]/.freeze
13
+ NEWLINE =
14
+ /(?:[ \t]*(?:\r?\n)[ \t]*)+/.freeze
15
+ STRING_BASIC =
16
+ /(")(?:\\?[^\u0000-\u0008\u000A-\u001F\u007F\\]|(?:\\[^\u0000-\u0008\u000A-\u001F\u007F]))*?\1/.freeze
17
+ STRING_MULTI =
18
+ /"{3}([^\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]*?(?<!\\)"{3,5})/m.freeze
19
+ STRING_LITERAL =
20
+ /(')(?:\\?[^\u0000-\u0008\u000A-\u001F\u007F])*?\1/.freeze
21
+ STRING_LITERAL_MULTI =
22
+ /'{3}([^\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]*?'{3,5})/m.freeze
23
+ DATETIME =
24
+ /(-?\d{4})-([01]\d)-([0-3]\d)(?:(?:t|\s)([0-2]\d):([0-5]\d):([0-6]\d(?:\.\d+)?))?(z|[-+][01]\d:\d{2})?/i.freeze
25
+ LOCAL_TIME =
26
+ /([0-2]\d):([0-5]\d):([0-6]\d(?:\.\d+)?)/.freeze
27
+ FLOAT =
28
+ /[+-]?(?:(?:\d|[1-9](?:_?\d)*)\.\d(?:_?\d)*|\d+(?=[eE]))(?:[eE][+-]?[0-9]+(_[0-9])*[0-9]*)?(?!\w)/.freeze
29
+ FLOAT_KEYWORD =
30
+ /[+-]?(?:inf|nan)\b/.freeze
31
+ INTEGER =
32
+ /[+-]?([1-9](_?\d)*|0)(?![A-Za-z0-9_-]+)/.freeze
33
+ NON_DEC_INTEGER =
34
+ /0(?:x[0-9A-Fa-f](_?[0-9A-Fa-f])*|o[0-7](_?[0-7])*|b[01](_?[01])*)/.freeze
35
+ BOOLEAN =
36
+ /true|false/.freeze
37
+ SPACED_ARRAY_OF_TABLES_START =
38
+ /^\[[ \t]+\[(#{IDENTIFIER}|#{STRING_BASIC}|#{STRING_LITERAL}|#{INTEGER}|#{NON_DEC_INTEGER}|#{FLOAT_KEYWORD}|#{BOOLEAN})\]\]$/.freeze
39
+ SPACED_ARRAY_OF_TABLES_END =
40
+ /^\[\[(#{IDENTIFIER}|#{STRING_BASIC}|#{STRING_LITERAL}|#{INTEGER}|#{NON_DEC_INTEGER}|#{FLOAT_KEYWORD}|#{BOOLEAN})\][ \t]+\]$/.freeze
41
+ SPACED_ARRAY_OF_TABLES_BOTH =
42
+ /^\[[ \t]+\[(#{IDENTIFIER}|#{STRING_BASIC}|#{STRING_LITERAL}|#{INTEGER}|#{NON_DEC_INTEGER}|#{FLOAT_KEYWORD}|#{BOOLEAN})\][ \t]+\]$/.freeze
19
43
 
20
44
  def initialize(io)
21
45
  @ss = StringScanner.new(io.read)
46
+ @eos = false
22
47
  end
23
48
 
24
49
  def next_token
25
- return if @ss.eos?
26
-
27
50
  case
51
+ when @ss.scan(NEWLINE) then [:NEWLINE, nil]
52
+ when @ss.scan(SPACED_ARRAY_OF_TABLES_START) then raise ParseError.new('Array of tables has spaces in starting brackets')
53
+ when @ss.scan(SPACED_ARRAY_OF_TABLES_END) then raise ParseError.new('Array of tables has spaces in ending brackets')
54
+ when @ss.scan(SPACED_ARRAY_OF_TABLES_BOTH) then raise ParseError.new('Array of tables has spaces in starting and ending brackets')
28
55
  when @ss.scan(SPACE) then next_token
29
56
  when @ss.scan(COMMENT) then next_token
30
57
  when @ss.scan(DATETIME) then process_datetime
58
+ when @ss.scan(LOCAL_TIME) then process_local_time
31
59
  when text = @ss.scan(STRING_MULTI) then [:STRING_MULTI, text[3..-4]]
32
60
  when text = @ss.scan(STRING_BASIC) then [:STRING_BASIC, text[1..-2]]
33
61
  when text = @ss.scan(STRING_LITERAL_MULTI) then [:STRING_LITERAL_MULTI, text[3..-4]]
34
62
  when text = @ss.scan(STRING_LITERAL) then [:STRING_LITERAL, text[1..-2]]
35
63
  when text = @ss.scan(FLOAT) then [:FLOAT, text]
36
- when text = @ss.scan(FLOAT_INF) then [:FLOAT_INF, text]
37
- when text = @ss.scan(FLOAT_NAN) then [:FLOAT_NAN, text]
64
+ when text = @ss.scan(FLOAT_KEYWORD) then [:FLOAT_KEYWORD, text]
38
65
  when text = @ss.scan(INTEGER) then [:INTEGER, text]
39
- when text = @ss.scan(TRUE) then [:TRUE, text]
40
- when text = @ss.scan(FALSE) then [:FALSE, text]
66
+ when text = @ss.scan(NON_DEC_INTEGER) then [:NON_DEC_INTEGER, text]
67
+ when text = @ss.scan(BOOLEAN) then [:BOOLEAN, text]
41
68
  when text = @ss.scan(IDENTIFIER) then [:IDENTIFIER, text]
42
- else
43
- x = @ss.getch
44
- [x, x]
69
+ when @ss.eos? then process_eos
70
+ else x = @ss.getch; [x, x]
45
71
  end
46
72
  end
47
73
 
48
74
  def process_datetime
49
- if @ss[7].nil?
50
- offset = '+00:00'
51
- else
52
- offset = @ss[7].gsub('Z', '+00:00')
53
- end
54
- args = [@ss[1], @ss[2], @ss[3], @ss[4] || 0, @ss[5] || 0, @ss[6].to_f, offset]
75
+ offset = @ss[7].gsub(/[zZ]/, '+00:00') if @ss[7]
76
+ args = [@ss[0], @ss[1], @ss[2], @ss[3], @ss[4], @ss[5], @ss[6], offset]
55
77
  [:DATETIME, args]
56
78
  end
79
+
80
+ def process_local_time
81
+ args = [@ss[0], @ss[1], @ss[2], @ss[3].to_f]
82
+ [:LOCAL_TIME, args]
83
+ end
84
+
85
+ def process_eos
86
+ return if @eos
87
+
88
+ @eos = true
89
+ [:EOS, nil]
90
+ end
57
91
  end
58
92
  end
@@ -1,6 +1,7 @@
1
+ # frozen-string-literal: true
2
+
1
3
  module Tomlrb
2
4
  class StringUtils
3
-
4
5
  SPECIAL_CHARS = {
5
6
  '\\t' => "\t",
6
7
  '\\b' => "\b",
@@ -12,7 +13,13 @@ module Tomlrb
12
13
  }.freeze
13
14
 
14
15
  def self.multiline_replacements(str)
15
- strip_spaces(str).gsub(/\\\n\s+/, '')
16
+ strip_spaces(str).gsub(/\\+\s*\n\s*/) do |matched|
17
+ if matched.match(/\\+/)[0].length.odd?
18
+ matched.gsub(/\\\s*\n\s*/, '')
19
+ else
20
+ matched
21
+ end
22
+ end
16
23
  end
17
24
 
18
25
  def self.replace_escaped_chars(str)
@@ -1,3 +1,5 @@
1
+ # frozen-string-literal: true
2
+
1
3
  module Tomlrb
2
- VERSION = '1.3.0'.freeze
4
+ VERSION = '2.0.4'
3
5
  end
data/lib/tomlrb.rb CHANGED
@@ -1,10 +1,15 @@
1
+ # frozen-string-literal: true
2
+
1
3
  require 'time'
2
4
  require 'stringio'
3
- require "tomlrb/version"
5
+ require 'tomlrb/version'
6
+ require 'tomlrb/local_date_time'
7
+ require 'tomlrb/local_date'
8
+ require 'tomlrb/local_time'
4
9
  require 'tomlrb/string_utils'
5
- require "tomlrb/scanner"
6
- require "tomlrb/parser"
7
- require "tomlrb/handler"
10
+ require 'tomlrb/scanner'
11
+ require 'tomlrb/parser'
12
+ require 'tomlrb/handler'
8
13
 
9
14
  module Tomlrb
10
15
  class ParseError < StandardError; end
@@ -38,7 +43,7 @@ module Tomlrb
38
43
  # By default Ruby sets the external encoding of an IO object to the
39
44
  # default external encoding. The default external encoding is set by
40
45
  # locale encoding or the interpreter -E option.
41
- tmp = File.read(path, :encoding=>'utf-8')
46
+ tmp = File.read(path, encoding: 'utf-8')
42
47
  Tomlrb.parse(tmp, **options)
43
48
  end
44
49
  end
metadata CHANGED
@@ -1,15 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tomlrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francois Bernier
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2020-03-19 00:00:00.000000000 Z
12
- dependencies: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: psych
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '4'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '4'
13
26
  description: A racc based toml parser
14
27
  email:
15
28
  - frankbernier@gmail.com
@@ -21,6 +34,9 @@ files:
21
34
  - lib/tomlrb.rb
22
35
  - lib/tomlrb/generated_parser.rb
23
36
  - lib/tomlrb/handler.rb
37
+ - lib/tomlrb/local_date.rb
38
+ - lib/tomlrb/local_date_time.rb
39
+ - lib/tomlrb/local_time.rb
24
40
  - lib/tomlrb/parser.rb
25
41
  - lib/tomlrb/parser.y
26
42
  - lib/tomlrb/scanner.rb
@@ -30,7 +46,6 @@ homepage: https://github.com/fbernier/tomlrb
30
46
  licenses:
31
47
  - MIT
32
48
  metadata: {}
33
- post_install_message:
34
49
  rdoc_options: []
35
50
  require_paths:
36
51
  - lib
@@ -45,8 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
60
  - !ruby/object:Gem::Version
46
61
  version: '0'
47
62
  requirements: []
48
- rubygems_version: 3.0.6
49
- signing_key:
63
+ rubygems_version: 3.6.9
50
64
  specification_version: 4
51
65
  summary: A racc based toml parser
52
66
  test_files: []