psych 3.0.0.beta2-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/.travis.yml +20 -0
  4. data/CHANGELOG.rdoc +576 -0
  5. data/Gemfile +3 -0
  6. data/Mavenfile +7 -0
  7. data/README.md +73 -0
  8. data/Rakefile +46 -0
  9. data/bin/console +7 -0
  10. data/bin/setup +6 -0
  11. data/ext/psych/.gitignore +11 -0
  12. data/ext/psych/depend +3 -0
  13. data/ext/psych/extconf.rb +39 -0
  14. data/ext/psych/psych.c +34 -0
  15. data/ext/psych/psych.h +17 -0
  16. data/ext/psych/psych_emitter.c +554 -0
  17. data/ext/psych/psych_emitter.h +8 -0
  18. data/ext/psych/psych_parser.c +568 -0
  19. data/ext/psych/psych_parser.h +6 -0
  20. data/ext/psych/psych_to_ruby.c +39 -0
  21. data/ext/psych/psych_to_ruby.h +8 -0
  22. data/ext/psych/psych_yaml_tree.c +24 -0
  23. data/ext/psych/psych_yaml_tree.h +8 -0
  24. data/ext/psych/yaml/LICENSE +19 -0
  25. data/ext/psych/yaml/api.c +1392 -0
  26. data/ext/psych/yaml/config.h +10 -0
  27. data/ext/psych/yaml/dumper.c +394 -0
  28. data/ext/psych/yaml/emitter.c +2329 -0
  29. data/ext/psych/yaml/loader.c +444 -0
  30. data/ext/psych/yaml/parser.c +1374 -0
  31. data/ext/psych/yaml/reader.c +469 -0
  32. data/ext/psych/yaml/scanner.c +3576 -0
  33. data/ext/psych/yaml/writer.c +141 -0
  34. data/ext/psych/yaml/yaml.h +1971 -0
  35. data/ext/psych/yaml/yaml_private.h +662 -0
  36. data/lib/psych.rb +511 -0
  37. data/lib/psych/class_loader.rb +102 -0
  38. data/lib/psych/coder.rb +95 -0
  39. data/lib/psych/core_ext.rb +19 -0
  40. data/lib/psych/exception.rb +14 -0
  41. data/lib/psych/handler.rb +250 -0
  42. data/lib/psych/handlers/document_stream.rb +23 -0
  43. data/lib/psych/handlers/recorder.rb +40 -0
  44. data/lib/psych/json/ruby_events.rb +20 -0
  45. data/lib/psych/json/stream.rb +17 -0
  46. data/lib/psych/json/tree_builder.rb +13 -0
  47. data/lib/psych/json/yaml_events.rb +30 -0
  48. data/lib/psych/nodes.rb +78 -0
  49. data/lib/psych/nodes/alias.rb +19 -0
  50. data/lib/psych/nodes/document.rb +61 -0
  51. data/lib/psych/nodes/mapping.rb +57 -0
  52. data/lib/psych/nodes/node.rb +56 -0
  53. data/lib/psych/nodes/scalar.rb +68 -0
  54. data/lib/psych/nodes/sequence.rb +82 -0
  55. data/lib/psych/nodes/stream.rb +38 -0
  56. data/lib/psych/omap.rb +5 -0
  57. data/lib/psych/parser.rb +52 -0
  58. data/lib/psych/scalar_scanner.rb +149 -0
  59. data/lib/psych/set.rb +5 -0
  60. data/lib/psych/stream.rb +38 -0
  61. data/lib/psych/streaming.rb +28 -0
  62. data/lib/psych/syntax_error.rb +22 -0
  63. data/lib/psych/tree_builder.rb +97 -0
  64. data/lib/psych/versions.rb +9 -0
  65. data/lib/psych/visitors.rb +7 -0
  66. data/lib/psych/visitors/depth_first.rb +27 -0
  67. data/lib/psych/visitors/emitter.rb +52 -0
  68. data/lib/psych/visitors/json_tree.rb +25 -0
  69. data/lib/psych/visitors/to_ruby.rb +401 -0
  70. data/lib/psych/visitors/visitor.rb +20 -0
  71. data/lib/psych/visitors/yaml_tree.rb +551 -0
  72. data/lib/psych/y.rb +10 -0
  73. data/psych.gemspec +64 -0
  74. metadata +175 -0
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+ module Psych
3
+ module Nodes
4
+ ###
5
+ # This class represents a
6
+ # {YAML sequence}[http://yaml.org/spec/1.1/#sequence/syntax].
7
+ #
8
+ # A YAML sequence is basically a list, and looks like this:
9
+ #
10
+ # %YAML 1.1
11
+ # ---
12
+ # - I am
13
+ # - a Sequence
14
+ #
15
+ # A YAML sequence may have an anchor like this:
16
+ #
17
+ # %YAML 1.1
18
+ # ---
19
+ # &A [
20
+ # "This sequence",
21
+ # "has an anchor"
22
+ # ]
23
+ #
24
+ # A YAML sequence may also have a tag like this:
25
+ #
26
+ # %YAML 1.1
27
+ # ---
28
+ # !!seq [
29
+ # "This sequence",
30
+ # "has a tag"
31
+ # ]
32
+ #
33
+ # This class represents a sequence in a YAML document. A
34
+ # Psych::Nodes::Sequence node may have 0 or more children. Valid children
35
+ # for this node are:
36
+ #
37
+ # * Psych::Nodes::Sequence
38
+ # * Psych::Nodes::Mapping
39
+ # * Psych::Nodes::Scalar
40
+ # * Psych::Nodes::Alias
41
+ class Sequence < Psych::Nodes::Node
42
+ # Any Styles, emitter chooses
43
+ ANY = 0
44
+
45
+ # Block style sequence
46
+ BLOCK = 1
47
+
48
+ # Flow style sequence
49
+ FLOW = 2
50
+
51
+ # The anchor for this sequence (if any)
52
+ attr_accessor :anchor
53
+
54
+ # The tag name for this sequence (if any)
55
+ attr_accessor :tag
56
+
57
+ # Is this sequence started implicitly?
58
+ attr_accessor :implicit
59
+
60
+ # The sequence style used
61
+ attr_accessor :style
62
+
63
+ ###
64
+ # Create a new object representing a YAML sequence.
65
+ #
66
+ # +anchor+ is the anchor associated with the sequence or nil.
67
+ # +tag+ is the tag associated with the sequence or nil.
68
+ # +implicit+ a boolean indicating whether or not the sequence was
69
+ # implicitly started.
70
+ # +style+ is an integer indicating the list style.
71
+ #
72
+ # See Psych::Handler#start_sequence
73
+ def initialize anchor = nil, tag = nil, implicit = true, style = BLOCK
74
+ super()
75
+ @anchor = anchor
76
+ @tag = tag
77
+ @implicit = implicit
78
+ @style = style
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+ module Psych
3
+ module Nodes
4
+ ###
5
+ # Represents a YAML stream. This is the root node for any YAML parse
6
+ # tree. This node must have one or more child nodes. The only valid
7
+ # child node for a Psych::Nodes::Stream node is Psych::Nodes::Document.
8
+ class Stream < Psych::Nodes::Node
9
+
10
+ # Encodings supported by Psych (and libyaml)
11
+
12
+ # Any encoding
13
+ ANY = Psych::Parser::ANY
14
+
15
+ # UTF-8 encoding
16
+ UTF8 = Psych::Parser::UTF8
17
+
18
+ # UTF-16LE encoding
19
+ UTF16LE = Psych::Parser::UTF16LE
20
+
21
+ # UTF-16BE encoding
22
+ UTF16BE = Psych::Parser::UTF16BE
23
+
24
+ # The encoding used for this stream
25
+ attr_accessor :encoding
26
+
27
+ ###
28
+ # Create a new Psych::Nodes::Stream node with an +encoding+ that
29
+ # defaults to Psych::Nodes::Stream::UTF8.
30
+ #
31
+ # See also Psych::Handler#start_stream
32
+ def initialize encoding = UTF8
33
+ super()
34
+ @encoding = encoding
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ module Psych
3
+ class Omap < ::Hash
4
+ end
5
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+ module Psych
3
+ ###
4
+ # YAML event parser class. This class parses a YAML document and calls
5
+ # events on the handler that is passed to the constructor. The events can
6
+ # be used for things such as constructing a YAML AST or deserializing YAML
7
+ # documents. It can even be fed back to Psych::Emitter to emit the same
8
+ # document that was parsed.
9
+ #
10
+ # See Psych::Handler for documentation on the events that Psych::Parser emits.
11
+ #
12
+ # Here is an example that prints out ever scalar found in a YAML document:
13
+ #
14
+ # # Handler for detecting scalar values
15
+ # class ScalarHandler < Psych::Handler
16
+ # def scalar value, anchor, tag, plain, quoted, style
17
+ # puts value
18
+ # end
19
+ # end
20
+ #
21
+ # parser = Psych::Parser.new(ScalarHandler.new)
22
+ # parser.parse(yaml_document)
23
+ #
24
+ # Here is an example that feeds the parser back in to Psych::Emitter. The
25
+ # YAML document is read from STDIN and written back out to STDERR:
26
+ #
27
+ # parser = Psych::Parser.new(Psych::Emitter.new($stderr))
28
+ # parser.parse($stdin)
29
+ #
30
+ # Psych uses Psych::Parser in combination with Psych::TreeBuilder to
31
+ # construct an AST of the parsed YAML document.
32
+
33
+ class Parser
34
+ class Mark < Struct.new(:index, :line, :column)
35
+ end
36
+
37
+ # The handler on which events will be called
38
+ attr_accessor :handler
39
+
40
+ # Set the encoding for this parser to +encoding+
41
+ attr_writer :external_encoding
42
+
43
+ ###
44
+ # Creates a new Psych::Parser instance with +handler+. YAML events will
45
+ # be called on +handler+. See Psych::Parser for more details.
46
+
47
+ def initialize handler = Handler.new
48
+ @handler = handler
49
+ @external_encoding = ANY
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+ require 'strscan'
3
+
4
+ module Psych
5
+ ###
6
+ # Scan scalars for built in types
7
+ class ScalarScanner
8
+ # Taken from http://yaml.org/type/timestamp.html
9
+ 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)?))?$/
10
+
11
+ # Taken from http://yaml.org/type/float.html
12
+ FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9]*([eE][-+][0-9]+)?(?# base 10)
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
21
+
22
+ attr_reader :class_loader
23
+
24
+ # Create a new scanner
25
+ def initialize class_loader
26
+ @string_cache = {}
27
+ @symbol_cache = {}
28
+ @class_loader = class_loader
29
+ end
30
+
31
+ # Tokenize +string+ returning the Ruby object
32
+ def tokenize string
33
+ return nil if string.empty?
34
+ return string if @string_cache.key?(string)
35
+ return @symbol_cache[string] if @symbol_cache.key?(string)
36
+
37
+ case string
38
+ # Check for a String type, being careful not to get caught by hash keys, hex values, and
39
+ # 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
45
+
46
+ case string
47
+ when /^[^ytonf~]/i
48
+ @string_cache[string] = true
49
+ string
50
+ when '~', /^null$/i
51
+ nil
52
+ when /^(yes|true|on)$/i
53
+ true
54
+ when /^(no|false|off)$/i
55
+ false
56
+ else
57
+ @string_cache[string] = true
58
+ string
59
+ end
60
+ when TIME
61
+ begin
62
+ parse_time string
63
+ rescue ArgumentError
64
+ string
65
+ end
66
+ when /^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/
67
+ require 'date'
68
+ begin
69
+ class_loader.date.strptime(string, '%Y-%m-%d')
70
+ rescue ArgumentError
71
+ string
72
+ end
73
+ when /^\.inf$/i
74
+ Float::INFINITY
75
+ when /^-\.inf$/i
76
+ -Float::INFINITY
77
+ when /^\.nan$/i
78
+ Float::NAN
79
+ when /^:./
80
+ if string =~ /^:(["'])(.*)\1/
81
+ @symbol_cache[string] = class_loader.symbolize($2.sub(/^:/, ''))
82
+ else
83
+ @symbol_cache[string] = class_loader.symbolize(string.sub(/^:/, ''))
84
+ end
85
+ when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}$/
86
+ i = 0
87
+ string.split(':').each_with_index do |n,e|
88
+ i += (n.to_i * 60 ** (e - 2).abs)
89
+ end
90
+ i
91
+ when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}\.[0-9_]*$/
92
+ i = 0
93
+ string.split(':').each_with_index do |n,e|
94
+ i += (n.to_f * 60 ** (e - 2).abs)
95
+ end
96
+ i
97
+ when FLOAT
98
+ if string =~ /\A[-+]?\.\Z/
99
+ @string_cache[string] = true
100
+ string
101
+ else
102
+ Float(string.gsub(/[,_]|\.([Ee]|$)/, '\1'))
103
+ end
104
+ else
105
+ int = parse_int string.gsub(/[,_]/, '')
106
+ return int if int
107
+
108
+ @string_cache[string] = true
109
+ string
110
+ end
111
+ end
112
+
113
+ ###
114
+ # Parse and return an int from +string+
115
+ def parse_int string
116
+ return unless INTEGER === string
117
+ Integer(string)
118
+ end
119
+
120
+ ###
121
+ # Parse and return a Time from +string+
122
+ def parse_time string
123
+ klass = class_loader.load 'Time'
124
+
125
+ date, time = *(string.split(/[ tT]/, 2))
126
+ (yy, m, dd) = date.match(/^(-?\d{4})-(\d{1,2})-(\d{1,2})/).captures.map { |x| x.to_i }
127
+ md = time.match(/(\d+:\d+:\d+)(?:\.(\d*))?\s*(Z|[-+]\d+(:\d\d)?)?/)
128
+
129
+ (hh, mm, ss) = md[1].split(':').map { |x| x.to_i }
130
+ us = (md[2] ? Rational("0.#{md[2]}") : 0) * 1000000
131
+
132
+ time = klass.utc(yy, m, dd, hh, mm, ss, us)
133
+
134
+ return time if 'Z' == md[3]
135
+ return klass.at(time.to_i, us) unless md[3]
136
+
137
+ tz = md[3].match(/^([+\-]?\d{1,2})\:?(\d{1,2})?$/)[1..-1].compact.map { |digit| Integer(digit, 10) }
138
+ offset = tz.first * 3600
139
+
140
+ if offset < 0
141
+ offset -= ((tz[1] || 0) * 60)
142
+ else
143
+ offset += ((tz[1] || 0) * 60)
144
+ end
145
+
146
+ klass.new(yy, m, dd, hh, mm, ss+us/(1_000_000r), offset)
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ module Psych
3
+ class Set < ::Hash
4
+ end
5
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+ module Psych
3
+ ###
4
+ # Psych::Stream is a streaming YAML emitter. It will not buffer your YAML,
5
+ # but send it straight to an IO.
6
+ #
7
+ # Here is an example use:
8
+ #
9
+ # stream = Psych::Stream.new($stdout)
10
+ # stream.start
11
+ # stream.push({:foo => 'bar'})
12
+ # stream.finish
13
+ #
14
+ # YAML will be immediately emitted to $stdout with no buffering.
15
+ #
16
+ # Psych::Stream#start will take a block and ensure that Psych::Stream#finish
17
+ # is called, so you can do this form:
18
+ #
19
+ # stream = Psych::Stream.new($stdout)
20
+ # stream.start do |em|
21
+ # em.push(:foo => 'bar')
22
+ # end
23
+ #
24
+ class Stream < Psych::Visitors::YAMLTree
25
+ class Emitter < Psych::Emitter # :nodoc:
26
+ def end_document implicit_end = !streaming?
27
+ super
28
+ end
29
+
30
+ def streaming?
31
+ true
32
+ end
33
+ end
34
+
35
+ include Psych::Streaming
36
+ extend Psych::Streaming::ClassMethods
37
+ end
38
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+ module Psych
3
+ module Streaming
4
+ module ClassMethods
5
+ ###
6
+ # Create a new streaming emitter. Emitter will print to +io+. See
7
+ # Psych::Stream for an example.
8
+ def new io
9
+ emitter = const_get(:Emitter).new(io)
10
+ class_loader = ClassLoader.new
11
+ ss = ScalarScanner.new class_loader
12
+ super(emitter, ss, {})
13
+ end
14
+ end
15
+
16
+ ###
17
+ # Start streaming using +encoding+
18
+ def start encoding = Nodes::Stream::UTF8
19
+ super.tap { yield self if block_given? }
20
+ ensure
21
+ finish if block_given?
22
+ end
23
+
24
+ private
25
+ def register target, obj
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ require 'psych/exception'
3
+
4
+ module Psych
5
+ class SyntaxError < Psych::Exception
6
+ attr_reader :file, :line, :column, :offset, :problem, :context
7
+
8
+ def initialize file, line, col, offset, problem, context
9
+ err = [problem, context].compact.join ' '
10
+ filename = file || '<unknown>'
11
+ message = "(%s): %s at line %d column %d" % [filename, err, line, col]
12
+
13
+ @file = file
14
+ @line = line
15
+ @column = col
16
+ @offset = offset
17
+ @problem = problem
18
+ @context = context
19
+ super(message)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+ require 'psych/handler'
3
+
4
+ module Psych
5
+ ###
6
+ # This class works in conjunction with Psych::Parser to build an in-memory
7
+ # parse tree that represents a YAML document.
8
+ #
9
+ # == Example
10
+ #
11
+ # parser = Psych::Parser.new Psych::TreeBuilder.new
12
+ # parser.parse('--- foo')
13
+ # tree = parser.handler.root
14
+ #
15
+ # See Psych::Handler for documentation on the event methods used in this
16
+ # class.
17
+ class TreeBuilder < Psych::Handler
18
+ # Returns the root node for the built tree
19
+ attr_reader :root
20
+
21
+ # Create a new TreeBuilder instance
22
+ def initialize
23
+ @stack = []
24
+ @last = nil
25
+ @root = nil
26
+ end
27
+
28
+ %w{
29
+ Sequence
30
+ Mapping
31
+ }.each do |node|
32
+ class_eval %{
33
+ def start_#{node.downcase}(anchor, tag, implicit, style)
34
+ n = Nodes::#{node}.new(anchor, tag, implicit, style)
35
+ @last.children << n
36
+ push n
37
+ end
38
+
39
+ def end_#{node.downcase}
40
+ pop
41
+ end
42
+ }
43
+ end
44
+
45
+ ###
46
+ # Handles start_document events with +version+, +tag_directives+,
47
+ # and +implicit+ styling.
48
+ #
49
+ # See Psych::Handler#start_document
50
+ def start_document version, tag_directives, implicit
51
+ n = Nodes::Document.new version, tag_directives, implicit
52
+ @last.children << n
53
+ push n
54
+ end
55
+
56
+ ###
57
+ # Handles end_document events with +version+, +tag_directives+,
58
+ # and +implicit+ styling.
59
+ #
60
+ # See Psych::Handler#start_document
61
+ def end_document implicit_end = !streaming?
62
+ @last.implicit_end = implicit_end
63
+ pop
64
+ end
65
+
66
+ def start_stream encoding
67
+ @root = Nodes::Stream.new(encoding)
68
+ push @root
69
+ end
70
+
71
+ def end_stream
72
+ pop
73
+ end
74
+
75
+ def scalar value, anchor, tag, plain, quoted, style
76
+ s = Nodes::Scalar.new(value,anchor,tag,plain,quoted,style)
77
+ @last.children << s
78
+ s
79
+ end
80
+
81
+ def alias anchor
82
+ @last.children << Nodes::Alias.new(anchor)
83
+ end
84
+
85
+ private
86
+ def push value
87
+ @stack.push value
88
+ @last = value
89
+ end
90
+
91
+ def pop
92
+ x = @stack.pop
93
+ @last = @stack.last
94
+ x
95
+ end
96
+ end
97
+ end