psych 2.0.12 → 5.2.3

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.
Files changed (115) hide show
  1. checksums.yaml +5 -5
  2. data/CONTRIBUTING.md +24 -0
  3. data/{ext/psych/yaml/LICENSE → LICENSE} +9 -7
  4. data/README.md +80 -0
  5. data/ext/psych/depend +14 -0
  6. data/ext/psych/extconf.rb +43 -28
  7. data/ext/psych/psych.c +6 -4
  8. data/ext/psych/psych.h +0 -3
  9. data/ext/psych/psych_emitter.c +165 -132
  10. data/ext/psych/psych_parser.c +298 -331
  11. data/ext/psych/psych_to_ruby.c +0 -5
  12. data/ext/psych/psych_yaml_tree.c +0 -13
  13. data/lib/psych/class_loader.rb +11 -8
  14. data/lib/psych/coder.rb +1 -0
  15. data/lib/psych/core_ext.rb +3 -19
  16. data/lib/psych/exception.rb +17 -2
  17. data/lib/psych/handler.rb +8 -2
  18. data/lib/psych/handlers/document_stream.rb +2 -1
  19. data/lib/psych/handlers/recorder.rb +2 -1
  20. data/lib/psych/json/ruby_events.rb +1 -0
  21. data/lib/psych/json/stream.rb +3 -2
  22. data/lib/psych/json/tree_builder.rb +2 -1
  23. data/lib/psych/json/yaml_events.rb +1 -0
  24. data/lib/psych/nodes/alias.rb +3 -0
  25. data/lib/psych/nodes/document.rb +3 -0
  26. data/lib/psych/nodes/mapping.rb +3 -0
  27. data/lib/psych/nodes/node.rb +25 -5
  28. data/lib/psych/nodes/scalar.rb +4 -1
  29. data/lib/psych/nodes/sequence.rb +3 -0
  30. data/lib/psych/nodes/stream.rb +3 -0
  31. data/lib/psych/nodes.rb +8 -7
  32. data/lib/psych/omap.rb +1 -0
  33. data/lib/psych/parser.rb +14 -0
  34. data/lib/psych/scalar_scanner.rb +41 -49
  35. data/lib/psych/set.rb +1 -0
  36. data/lib/psych/stream.rb +1 -0
  37. data/lib/psych/streaming.rb +1 -0
  38. data/lib/psych/syntax_error.rb +2 -1
  39. data/lib/psych/tree_builder.rb +48 -7
  40. data/lib/psych/versions.rb +10 -0
  41. data/lib/psych/visitors/depth_first.rb +1 -0
  42. data/lib/psych/visitors/emitter.rb +1 -0
  43. data/lib/psych/visitors/json_tree.rb +2 -1
  44. data/lib/psych/visitors/to_ruby.rb +64 -33
  45. data/lib/psych/visitors/visitor.rb +18 -3
  46. data/lib/psych/visitors/yaml_tree.rb +128 -149
  47. data/lib/psych/visitors.rb +7 -6
  48. data/lib/psych/y.rb +1 -0
  49. data/lib/psych.rb +360 -95
  50. metadata +36 -169
  51. data/.autotest +0 -18
  52. data/.gemtest +0 -0
  53. data/.travis.yml +0 -11
  54. data/CHANGELOG.rdoc +0 -562
  55. data/Manifest.txt +0 -112
  56. data/README.rdoc +0 -71
  57. data/Rakefile +0 -74
  58. data/ext/psych/yaml/api.c +0 -1415
  59. data/ext/psych/yaml/config.h +0 -10
  60. data/ext/psych/yaml/dumper.c +0 -394
  61. data/ext/psych/yaml/emitter.c +0 -2329
  62. data/ext/psych/yaml/loader.c +0 -459
  63. data/ext/psych/yaml/parser.c +0 -1370
  64. data/ext/psych/yaml/reader.c +0 -469
  65. data/ext/psych/yaml/scanner.c +0 -3583
  66. data/ext/psych/yaml/writer.c +0 -141
  67. data/ext/psych/yaml/yaml.h +0 -1971
  68. data/ext/psych/yaml/yaml_private.h +0 -664
  69. data/lib/psych/deprecated.rb +0 -85
  70. data/test/psych/handlers/test_recorder.rb +0 -25
  71. data/test/psych/helper.rb +0 -114
  72. data/test/psych/json/test_stream.rb +0 -109
  73. data/test/psych/nodes/test_enumerable.rb +0 -43
  74. data/test/psych/test_alias_and_anchor.rb +0 -96
  75. data/test/psych/test_array.rb +0 -57
  76. data/test/psych/test_boolean.rb +0 -36
  77. data/test/psych/test_class.rb +0 -36
  78. data/test/psych/test_coder.rb +0 -184
  79. data/test/psych/test_date_time.rb +0 -38
  80. data/test/psych/test_deprecated.rb +0 -214
  81. data/test/psych/test_document.rb +0 -46
  82. data/test/psych/test_emitter.rb +0 -93
  83. data/test/psych/test_encoding.rb +0 -259
  84. data/test/psych/test_exception.rb +0 -157
  85. data/test/psych/test_hash.rb +0 -94
  86. data/test/psych/test_json_tree.rb +0 -65
  87. data/test/psych/test_marshalable.rb +0 -54
  88. data/test/psych/test_merge_keys.rb +0 -180
  89. data/test/psych/test_nil.rb +0 -18
  90. data/test/psych/test_null.rb +0 -19
  91. data/test/psych/test_numeric.rb +0 -45
  92. data/test/psych/test_object.rb +0 -44
  93. data/test/psych/test_object_references.rb +0 -71
  94. data/test/psych/test_omap.rb +0 -75
  95. data/test/psych/test_parser.rb +0 -339
  96. data/test/psych/test_psych.rb +0 -168
  97. data/test/psych/test_safe_load.rb +0 -97
  98. data/test/psych/test_scalar.rb +0 -11
  99. data/test/psych/test_scalar_scanner.rb +0 -106
  100. data/test/psych/test_serialize_subclasses.rb +0 -38
  101. data/test/psych/test_set.rb +0 -49
  102. data/test/psych/test_stream.rb +0 -93
  103. data/test/psych/test_string.rb +0 -226
  104. data/test/psych/test_struct.rb +0 -49
  105. data/test/psych/test_symbol.rb +0 -25
  106. data/test/psych/test_tainted.rb +0 -130
  107. data/test/psych/test_to_yaml_properties.rb +0 -63
  108. data/test/psych/test_tree_builder.rb +0 -79
  109. data/test/psych/test_yaml.rb +0 -1288
  110. data/test/psych/test_yamldbm.rb +0 -193
  111. data/test/psych/test_yamlstore.rb +0 -85
  112. data/test/psych/visitors/test_depth_first.rb +0 -49
  113. data/test/psych/visitors/test_emitter.rb +0 -144
  114. data/test/psych/visitors/test_to_ruby.rb +0 -326
  115. data/test/psych/visitors/test_yaml_tree.rb +0 -173
@@ -21,11 +21,7 @@ static VALUE build_exception(VALUE self, VALUE klass, VALUE mesg)
21
21
  */
22
22
  static VALUE path2class(VALUE self, VALUE path)
23
23
  {
24
- #ifdef HAVE_RUBY_ENCODING_H
25
24
  return rb_path_to_class(path);
26
- #else
27
- return rb_path2class(StringValuePtr(path));
28
- #endif
29
25
  }
30
26
 
31
27
  void Init_psych_to_ruby(void)
@@ -40,4 +36,3 @@ void Init_psych_to_ruby(void)
40
36
  rb_define_private_method(cPsychVisitorsToRuby, "build_exception", build_exception, 2);
41
37
  rb_define_private_method(class_loader, "path2class", path2class, 1);
42
38
  }
43
- /* vim: set noet sws=4 sw=4: */
@@ -2,23 +2,10 @@
2
2
 
3
3
  VALUE cPsychVisitorsYamlTree;
4
4
 
5
- /*
6
- * call-seq: private_iv_get(target, prop)
7
- *
8
- * Get the private instance variable +prop+ from +target+
9
- */
10
- static VALUE private_iv_get(VALUE self, VALUE target, VALUE prop)
11
- {
12
- return rb_attr_get(target, rb_intern(StringValuePtr(prop)));
13
- }
14
-
15
5
  void Init_psych_yaml_tree(void)
16
6
  {
17
7
  VALUE psych = rb_define_module("Psych");
18
8
  VALUE visitors = rb_define_module_under(psych, "Visitors");
19
9
  VALUE visitor = rb_define_class_under(visitors, "Visitor", rb_cObject);
20
10
  cPsychVisitorsYamlTree = rb_define_class_under(visitors, "YAMLTree", visitor);
21
-
22
- rb_define_private_method(cPsychVisitorsYamlTree, "private_iv_get", private_iv_get, 2);
23
11
  }
24
- /* vim: set noet sws=4 sw=4: */
@@ -1,5 +1,6 @@
1
- require 'psych/omap'
2
- require 'psych/set'
1
+ # frozen_string_literal: true
2
+ require_relative 'omap'
3
+ require_relative 'set'
3
4
 
4
5
  module Psych
5
6
  class ClassLoader # :nodoc:
@@ -34,9 +35,11 @@ module Psych
34
35
 
35
36
  constants.each do |const|
36
37
  konst = const_get const
37
- define_method(const.to_s.downcase) do
38
- load konst
39
- end
38
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
39
+ def #{const.to_s.downcase}
40
+ load #{konst.inspect}
41
+ end
42
+ RUBY
40
43
  end
41
44
 
42
45
  private
@@ -68,7 +71,7 @@ module Psych
68
71
  rescue
69
72
  nil
70
73
  end
71
- }.compact]
74
+ }.compact].freeze
72
75
 
73
76
  class Restricted < ClassLoader
74
77
  def initialize classes, symbols
@@ -83,7 +86,7 @@ module Psych
83
86
  if @symbols.include? sym
84
87
  super
85
88
  else
86
- raise DisallowedClass, 'Symbol'
89
+ raise DisallowedClass.new('load', 'Symbol')
87
90
  end
88
91
  end
89
92
 
@@ -93,7 +96,7 @@ module Psych
93
96
  if @classes.include? klassname
94
97
  super
95
98
  else
96
- raise DisallowedClass, klassname
99
+ raise DisallowedClass.new('load', klassname)
97
100
  end
98
101
  end
99
102
  end
data/lib/psych/coder.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  ###
3
4
  # If an object defines +encode_with+, then an instance of Psych::Coder will
@@ -1,35 +1,19 @@
1
+ # frozen_string_literal: true
1
2
  class Object
2
3
  def self.yaml_tag url
3
4
  Psych.add_tag(url, self)
4
5
  end
5
6
 
6
- # FIXME: rename this to "to_yaml" when syck is removed
7
-
8
7
  ###
9
8
  # call-seq: to_yaml(options = {})
10
9
  #
11
10
  # Convert an object to YAML. See Psych.dump for more information on the
12
11
  # available +options+.
13
- def psych_to_yaml options = {}
12
+ def to_yaml options = {}
14
13
  Psych.dump self, options
15
14
  end
16
- remove_method :to_yaml rescue nil
17
- alias :to_yaml :psych_to_yaml
18
- end
19
-
20
- class Module
21
- def psych_yaml_as url
22
- return if caller[0].end_with?('rubytypes.rb')
23
- if $VERBOSE
24
- warn "#{caller[0]}: yaml_as is deprecated, please use yaml_tag"
25
- end
26
- Psych.add_tag(url, self)
27
- end
28
-
29
- remove_method :yaml_as rescue nil
30
- alias :yaml_as :psych_yaml_as
31
15
  end
32
16
 
33
17
  if defined?(::IRB)
34
- require 'psych/y'
18
+ require_relative 'y'
35
19
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  class Exception < RuntimeError
3
4
  end
@@ -5,9 +6,23 @@ module Psych
5
6
  class BadAlias < Exception
6
7
  end
7
8
 
9
+ # Subclasses `BadAlias` for backwards compatibility
10
+ class AliasesNotEnabled < BadAlias
11
+ def initialize
12
+ super "Alias parsing was not enabled. To enable it, pass `aliases: true` to `Psych::load` or `Psych::safe_load`."
13
+ end
14
+ end
15
+
16
+ # Subclasses `BadAlias` for backwards compatibility
17
+ class AnchorNotDefined < BadAlias
18
+ def initialize anchor_name
19
+ super "An alias referenced an unknown anchor: #{anchor_name}"
20
+ end
21
+ end
22
+
8
23
  class DisallowedClass < Exception
9
- def initialize klass_name
10
- super "Tried to load unspecified class: #{klass_name}"
24
+ def initialize action, klass_name
25
+ super "Tried to #{action} unspecified class: #{klass_name}"
11
26
  end
12
27
  end
13
28
  end
data/lib/psych/handler.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  ###
3
4
  # Psych::Handler is an abstract base class that defines the events used
@@ -104,7 +105,7 @@ module Psych
104
105
  # - first element
105
106
  # - *ponies
106
107
  #
107
- # &ponies is the achor, *ponies is the alias. In this case, alias is
108
+ # &ponies is the anchor, *ponies is the alias. In this case, alias is
108
109
  # called with "ponies".
109
110
  def alias anchor
110
111
  end
@@ -118,7 +119,7 @@ module Psych
118
119
  # +tag+ is an associated tag or nil
119
120
  # +plain+ is a boolean value
120
121
  # +quoted+ is a boolean value
121
- # +style+ is an integer idicating the string style
122
+ # +style+ is an integer indicating the string style
122
123
  #
123
124
  # See the constants in Psych::Nodes::Scalar for the possible values of
124
125
  # +style+
@@ -240,6 +241,11 @@ module Psych
240
241
  def end_stream
241
242
  end
242
243
 
244
+ ###
245
+ # Called before each event with line/column information.
246
+ def event_location(start_line, start_column, end_line, end_column)
247
+ end
248
+
243
249
  ###
244
250
  # Is this handler a streaming handler?
245
251
  def streaming?
@@ -1,4 +1,5 @@
1
- require 'psych/tree_builder'
1
+ # frozen_string_literal: true
2
+ require_relative '../tree_builder'
2
3
 
3
4
  module Psych
4
5
  module Handlers
@@ -1,4 +1,5 @@
1
- require 'psych/handler'
1
+ # frozen_string_literal: true
2
+ require_relative '../handler'
2
3
 
3
4
  module Psych
4
5
  module Handlers
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  module JSON
3
4
  module RubyEvents # :nodoc:
@@ -1,5 +1,6 @@
1
- require 'psych/json/ruby_events'
2
- require 'psych/json/yaml_events'
1
+ # frozen_string_literal: true
2
+ require_relative 'ruby_events'
3
+ require_relative 'yaml_events'
3
4
 
4
5
  module Psych
5
6
  module JSON
@@ -1,4 +1,5 @@
1
- require 'psych/json/yaml_events'
1
+ # frozen_string_literal: true
2
+ require_relative 'yaml_events'
2
3
 
3
4
  module Psych
4
5
  module JSON
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  module JSON
3
4
  module YAMLEvents # :nodoc:
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  module Nodes
3
4
  ###
@@ -13,6 +14,8 @@ module Psych
13
14
  def initialize anchor
14
15
  @anchor = anchor
15
16
  end
17
+
18
+ def alias?; true; end
16
19
  end
17
20
  end
18
21
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  module Nodes
3
4
  ###
@@ -55,6 +56,8 @@ module Psych
55
56
  def root
56
57
  children.first
57
58
  end
59
+
60
+ def document?; true; end
58
61
  end
59
62
  end
60
63
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  module Nodes
3
4
  ###
@@ -51,6 +52,8 @@ module Psych
51
52
  @implicit = implicit
52
53
  @style = style
53
54
  end
55
+
56
+ def mapping?; true; end
54
57
  end
55
58
  end
56
59
  end
@@ -1,6 +1,6 @@
1
- require 'stringio'
2
- require 'psych/class_loader'
3
- require 'psych/scalar_scanner'
1
+ # frozen_string_literal: true
2
+ require_relative '../class_loader'
3
+ require_relative '../scalar_scanner'
4
4
 
5
5
  module Psych
6
6
  module Nodes
@@ -16,6 +16,18 @@ module Psych
16
16
  # An associated tag
17
17
  attr_reader :tag
18
18
 
19
+ # The line number where this node start
20
+ attr_accessor :start_line
21
+
22
+ # The column number where this node start
23
+ attr_accessor :start_column
24
+
25
+ # The line number where this node ends
26
+ attr_accessor :end_line
27
+
28
+ # The column number where this node ends
29
+ attr_accessor :end_column
30
+
19
31
  # Create a new Psych::Nodes::Node
20
32
  def initialize
21
33
  @children = []
@@ -33,8 +45,8 @@ module Psych
33
45
  # Convert this node to Ruby.
34
46
  #
35
47
  # See also Psych::Visitors::ToRuby
36
- def to_ruby
37
- Visitors::ToRuby.create.accept(self)
48
+ def to_ruby(symbolize_names: false, freeze: false, strict_integer: false)
49
+ Visitors::ToRuby.create(symbolize_names: symbolize_names, freeze: freeze, strict_integer: strict_integer).accept(self)
38
50
  end
39
51
  alias :transform :to_ruby
40
52
 
@@ -43,6 +55,7 @@ module Psych
43
55
  #
44
56
  # See also Psych::Visitors::Emitter
45
57
  def yaml io = nil, options = {}
58
+ require "stringio"
46
59
  real_io = io || StringIO.new(''.encode('utf-8'))
47
60
 
48
61
  Visitors::Emitter.new(real_io, options).accept self
@@ -50,6 +63,13 @@ module Psych
50
63
  io
51
64
  end
52
65
  alias :to_yaml :yaml
66
+
67
+ def alias?; false; end
68
+ def document?; false; end
69
+ def mapping?; false; end
70
+ def scalar?; false; end
71
+ def sequence?; false; end
72
+ def stream?; false; end
53
73
  end
54
74
  end
55
75
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  module Nodes
3
4
  ###
@@ -49,7 +50,7 @@ module Psych
49
50
  # +tag+ is an associated tag or nil
50
51
  # +plain+ is a boolean value
51
52
  # +quoted+ is a boolean value
52
- # +style+ is an integer idicating the string style
53
+ # +style+ is an integer indicating the string style
53
54
  #
54
55
  # == See Also
55
56
  #
@@ -62,6 +63,8 @@ module Psych
62
63
  @quoted = quoted
63
64
  @style = style
64
65
  end
66
+
67
+ def scalar?; true; end
65
68
  end
66
69
  end
67
70
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  module Nodes
3
4
  ###
@@ -76,6 +77,8 @@ module Psych
76
77
  @implicit = implicit
77
78
  @style = style
78
79
  end
80
+
81
+ def sequence?; true; end
79
82
  end
80
83
  end
81
84
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  module Nodes
3
4
  ###
@@ -32,6 +33,8 @@ module Psych
32
33
  super()
33
34
  @encoding = encoding
34
35
  end
36
+
37
+ def stream?; true; end
35
38
  end
36
39
  end
37
40
  end
data/lib/psych/nodes.rb CHANGED
@@ -1,10 +1,11 @@
1
- require 'psych/nodes/node'
2
- require 'psych/nodes/stream'
3
- require 'psych/nodes/document'
4
- require 'psych/nodes/sequence'
5
- require 'psych/nodes/scalar'
6
- require 'psych/nodes/mapping'
7
- require 'psych/nodes/alias'
1
+ # frozen_string_literal: true
2
+ require_relative 'nodes/node'
3
+ require_relative 'nodes/stream'
4
+ require_relative 'nodes/document'
5
+ require_relative 'nodes/sequence'
6
+ require_relative 'nodes/scalar'
7
+ require_relative 'nodes/mapping'
8
+ require_relative 'nodes/alias'
8
9
 
9
10
  module Psych
10
11
  ###
data/lib/psych/omap.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  class Omap < ::Hash
3
4
  end
data/lib/psych/parser.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  ###
3
4
  # YAML event parser class. This class parses a YAML document and calls
@@ -47,5 +48,18 @@ module Psych
47
48
  @handler = handler
48
49
  @external_encoding = ANY
49
50
  end
51
+
52
+ ###
53
+ # call-seq:
54
+ # parser.parse(yaml)
55
+ #
56
+ # Parse the YAML document contained in +yaml+. Events will be called on
57
+ # the handler set on the parser instance.
58
+ #
59
+ # See Psych::Parser and Psych::Parser#handler
60
+
61
+ def parse yaml, path = yaml.respond_to?(:path) ? yaml.path : "<unknown>"
62
+ _native_parse @handler, yaml, path
63
+ end
50
64
  end
51
65
  end
@@ -1,4 +1,4 @@
1
- require 'strscan'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Psych
4
4
  ###
@@ -8,104 +8,97 @@ module Psych
8
8
  TIME = /^-?\d{4}-\d{1,2}-\d{1,2}(?:[Tt]|\s+)\d{1,2}:\d\d:\d\d(?:\.\d*)?(?:\s*(?:Z|[-+]\d{1,2}:?(?:\d\d)?))?$/
9
9
 
10
10
  # Taken from http://yaml.org/type/float.html
11
- FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9]*([eE][-+][0-9]+)?(?# base 10)
12
- |[-+]?[0-9][0-9_,]*(:[0-5]?[0-9])+\.[0-9_]*(?# base 60)
13
- |[-+]?\.(inf|Inf|INF)(?# infinity)
14
- |\.(nan|NaN|NAN)(?# not a number))$/x
15
-
16
- # Taken from http://yaml.org/type/int.html
17
- INTEGER = /^(?:[-+]?0b[0-1_]+ (?# base 2)
18
- |[-+]?0[0-7_]+ (?# base 8)
19
- |[-+]?(?:0|[1-9][0-9_]*) (?# base 10)
20
- |[-+]?0x[0-9a-fA-F_]+ (?# base 16))$/x
11
+ # Base 60, [-+]inf and NaN are handled separately
12
+ FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9]*([eE][-+][0-9]+)?(?# base 10))$/x
13
+
14
+ # Taken from http://yaml.org/type/int.html and modified to ensure at least one numerical symbol exists
15
+ INTEGER_STRICT = /^(?:[-+]?0b[_]*[0-1][0-1_]* (?# base 2)
16
+ |[-+]?0[_]*[0-7][0-7_]* (?# base 8)
17
+ |[-+]?(0|[1-9][0-9_]*) (?# base 10)
18
+ |[-+]?0x[_]*[0-9a-fA-F][0-9a-fA-F_]* (?# base 16))$/x
19
+
20
+ # Same as above, but allows commas.
21
+ # Not to YML spec, but kept for backwards compatibility
22
+ INTEGER_LEGACY = /^(?:[-+]?0b[_,]*[0-1][0-1_,]* (?# base 2)
23
+ |[-+]?0[_,]*[0-7][0-7_,]* (?# base 8)
24
+ |[-+]?(?:0|[1-9](?:[0-9]|,[0-9]|_[0-9])*) (?# base 10)
25
+ |[-+]?0x[_,]*[0-9a-fA-F][0-9a-fA-F_,]* (?# base 16))$/x
21
26
 
22
27
  attr_reader :class_loader
23
28
 
24
29
  # Create a new scanner
25
- def initialize class_loader
26
- @string_cache = {}
30
+ def initialize class_loader, strict_integer: false
27
31
  @symbol_cache = {}
28
32
  @class_loader = class_loader
33
+ @strict_integer = strict_integer
29
34
  end
30
35
 
31
36
  # Tokenize +string+ returning the Ruby object
32
37
  def tokenize string
33
38
  return nil if string.empty?
34
- return string if @string_cache.key?(string)
35
39
  return @symbol_cache[string] if @symbol_cache.key?(string)
36
-
37
- case string
40
+ integer_regex = @strict_integer ? INTEGER_STRICT : INTEGER_LEGACY
38
41
  # Check for a String type, being careful not to get caught by hash keys, hex values, and
39
42
  # special floats (e.g., -.inf).
40
- when /^[^\d\.:-]?[A-Za-z_\s!@#\$%\^&\*\(\)\{\}\<\>\|\/\\~;=]+/, /\n/
41
- if string.length > 5
42
- @string_cache[string] = true
43
- return string
44
- end
43
+ if string.match?(%r{^[^\d.:-]?[[:alpha:]_\s!@#$%\^&*(){}<>|/\\~;=]+}) || string.match?(/\n/)
44
+ return string if string.length > 5
45
45
 
46
- case string
47
- when /^[^ytonf~]/i
48
- @string_cache[string] = true
46
+ if string.match?(/^[^ytonf~]/i)
49
47
  string
50
- when '~', /^null$/i
48
+ elsif string == '~' || string.match?(/^null$/i)
51
49
  nil
52
- when /^(yes|true|on)$/i
50
+ elsif string.match?(/^(yes|true|on)$/i)
53
51
  true
54
- when /^(no|false|off)$/i
52
+ elsif string.match?(/^(no|false|off)$/i)
55
53
  false
56
54
  else
57
- @string_cache[string] = true
58
55
  string
59
56
  end
60
- when TIME
57
+ elsif string.match?(TIME)
61
58
  begin
62
59
  parse_time string
63
60
  rescue ArgumentError
64
61
  string
65
62
  end
66
- when /^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/
67
- require 'date'
63
+ elsif string.match?(/^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/)
68
64
  begin
69
- class_loader.date.strptime(string, '%Y-%m-%d')
65
+ class_loader.date.strptime(string, '%F', Date::GREGORIAN)
70
66
  rescue ArgumentError
71
67
  string
72
68
  end
73
- when /^\.inf$/i
69
+ elsif string.match?(/^\+?\.inf$/i)
74
70
  Float::INFINITY
75
- when /^-\.inf$/i
71
+ elsif string.match?(/^-\.inf$/i)
76
72
  -Float::INFINITY
77
- when /^\.nan$/i
73
+ elsif string.match?(/^\.nan$/i)
78
74
  Float::NAN
79
- when /^:./
75
+ elsif string.match?(/^:./)
80
76
  if string =~ /^:(["'])(.*)\1/
81
77
  @symbol_cache[string] = class_loader.symbolize($2.sub(/^:/, ''))
82
78
  else
83
79
  @symbol_cache[string] = class_loader.symbolize(string.sub(/^:/, ''))
84
80
  end
85
- when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+$/
81
+ elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}$/)
86
82
  i = 0
87
83
  string.split(':').each_with_index do |n,e|
88
84
  i += (n.to_i * 60 ** (e - 2).abs)
89
85
  end
90
86
  i
91
- when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+\.[0-9_]*$/
87
+ elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}\.[0-9_]*$/)
92
88
  i = 0
93
89
  string.split(':').each_with_index do |n,e|
94
90
  i += (n.to_f * 60 ** (e - 2).abs)
95
91
  end
96
92
  i
97
- when FLOAT
98
- if string =~ /\A[-+]?\.\Z/
99
- @string_cache[string] = true
93
+ elsif string.match?(FLOAT)
94
+ if string.match?(/\A[-+]?\.\Z/)
100
95
  string
101
96
  else
102
- Float(string.gsub(/[,_]|\.$/, ''))
97
+ Float(string.delete(',_').gsub(/\.([Ee]|$)/, '\1'))
103
98
  end
99
+ elsif string.match?(integer_regex)
100
+ parse_int string
104
101
  else
105
- int = parse_int string.gsub(/[,_]/, '')
106
- return int if int
107
-
108
- @string_cache[string] = true
109
102
  string
110
103
  end
111
104
  end
@@ -113,8 +106,7 @@ module Psych
113
106
  ###
114
107
  # Parse and return an int from +string+
115
108
  def parse_int string
116
- return unless INTEGER === string
117
- Integer(string)
109
+ Integer(string.delete(',_'))
118
110
  end
119
111
 
120
112
  ###
@@ -143,7 +135,7 @@ module Psych
143
135
  offset += ((tz[1] || 0) * 60)
144
136
  end
145
137
 
146
- klass.at((time - offset).to_i, us)
138
+ klass.new(yy, m, dd, hh, mm, ss+us/(1_000_000r), offset)
147
139
  end
148
140
  end
149
141
  end
data/lib/psych/set.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  class Set < ::Hash
3
4
  end
data/lib/psych/stream.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  ###
3
4
  # Psych::Stream is a streaming YAML emitter. It will not buffer your YAML,
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Psych
2
3
  module Streaming
3
4
  module ClassMethods
@@ -1,4 +1,5 @@
1
- require 'psych/exception'
1
+ # frozen_string_literal: true
2
+ require_relative 'exception'
2
3
 
3
4
  module Psych
4
5
  class SyntaxError < Psych::Exception