summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2009-09-28 10:58:10 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2009-09-28 10:58:28 -0700
commitf3fcb96084fd7edf69bcd23e7d1dd3887794858c (patch)
treea74e5a17a67ff3871793fba8856324a41d029c9f /lib
parentccacb1f69c1097d42a4171fc39111b89b65d7d54 (diff)
downloadpsych-f3fcb96084fd7edf69bcd23e7d1dd3887794858c.zip
moving constants around, starting the to_ruby visitor
Diffstat (limited to 'lib')
-rw-r--r--lib/psych.rb28
-rw-r--r--lib/psych/handler.rb6
-rw-r--r--lib/psych/nodes/mapping.rb7
-rw-r--r--lib/psych/nodes/node.rb2
-rw-r--r--lib/psych/nodes/scalar.rb10
-rw-r--r--lib/psych/nodes/sequence.rb7
-rw-r--r--lib/psych/nodes/stream.rb11
-rw-r--r--lib/psych/visitable.rb7
-rw-r--r--lib/psych/visitors.rb2
-rw-r--r--lib/psych/visitors/to_ruby.rb11
-rw-r--r--lib/psych/visitors/visitor.rb17
11 files changed, 77 insertions, 31 deletions
diff --git a/lib/psych.rb b/lib/psych.rb
index df5486a..bdc2458 100644
--- a/lib/psych.rb
+++ b/lib/psych.rb
@@ -1,3 +1,5 @@
+require 'psych/visitable'
+
require 'psych/nodes/node'
require 'psych/nodes/stream'
require 'psych/nodes/document'
@@ -6,6 +8,8 @@ require 'psych/nodes/scalar'
require 'psych/nodes/mapping'
require 'psych/nodes/alias'
+require 'psych/visitors'
+
require 'psych/handler'
require 'psych/tree_builder'
require 'psych/parser'
@@ -15,30 +19,6 @@ module Psych
VERSION = '1.0.0'
LIBYAML_VERSION = Psych.libyaml_version.join '.'
- # Encodings supported by Psych (and libyaml)
- ANY_ENCODING = 1
- UTF8_ENCODING = 2
- UTF16LE_ENCODING = 3
- UTF16BE_ENCODING = 4
-
- # Scalar Styles
- ANY_SCALAR_STYLE = 0
- PLAIN_SCALAR_STYLE = 1
- SINGLE_QUOTED_SCALAR_STYLE = 2
- DOUBLE_QUOTED_SCALAR_STYLE = 3
- LITERAL_SCALAR_STYLE = 4
- FOLDED_SCALAR_STYLE = 5
-
- # Sequence Styles
- ANY_SEQUENCE_STYLE = 0
- BLOCK_SEQUENCE_STYLE = 1
- FLOW_SEQUENCE_STYLE = 2
-
- # Mapping Styles
- ANY_MAPPING_STYLE = 0
- BLOCK_MAPPING_STYLE = 1
- FLOW_MAPPING_STYLE = 2
-
def self.parse thing
Psych::Parser.new.parse thing
end
diff --git a/lib/psych/handler.rb b/lib/psych/handler.rb
index 5324174..84ce4c2 100644
--- a/lib/psych/handler.rb
+++ b/lib/psych/handler.rb
@@ -26,12 +26,12 @@ module Psych
###
# Called when a scalar +value+ is found. The scalar may have an
# +anchor+, a +tag+, be implicitly +plain+ or implicitly +quoted+
- def scalar value, anchor = nil, tag = nil, plain = true, quoted = true, style = ANY_SCALAR_STYLE
+ def scalar value, anchor, tag, plain, quoted, style
end
###
# Called when a sequence is started.
- def start_sequence anchor = nil, tag = nil, implicit = true, style = BLOCK_SEQUENCE_STYLE
+ def start_sequence anchor, tag, implicit, style
end
###
@@ -41,7 +41,7 @@ module Psych
###
# Called when a map starts
- def start_mapping anchor = nil, tag = nil, implicit = true, style = BLOCK_MAPPING_STYLE
+ def start_mapping anchor, tag, implicit, style
end
###
diff --git a/lib/psych/nodes/mapping.rb b/lib/psych/nodes/mapping.rb
index 0f47e89..2c56ec1 100644
--- a/lib/psych/nodes/mapping.rb
+++ b/lib/psych/nodes/mapping.rb
@@ -3,6 +3,11 @@ module Psych
###
# This class represents a {YAML Mapping}[http://yaml.org/spec/1.1/#mapping].
class Mapping < Psych::Nodes::Node
+ # Mapping Styles
+ ANY = 0
+ BLOCK = 1
+ FLOW = 2
+
# The optional anchor for this mapping
attr_accessor :anchor
@@ -15,7 +20,7 @@ module Psych
# The style of this mapping
attr_accessor :style
- def initialize anchor, tag, implicit, style
+ def initialize anchor = nil, tag = nil, implicit = true, style = BLOCK
super()
@anchor = anchor
@tag = tag
diff --git a/lib/psych/nodes/node.rb b/lib/psych/nodes/node.rb
index cb8f632..ce1bfe8 100644
--- a/lib/psych/nodes/node.rb
+++ b/lib/psych/nodes/node.rb
@@ -4,6 +4,8 @@ module Psych
# The base class for any Node in a YAML parse tree. This class should
# never be instantiated.
class Node
+ include Psych::Visitable
+
attr_reader :children
def initialize
diff --git a/lib/psych/nodes/scalar.rb b/lib/psych/nodes/scalar.rb
index bfa494f..b84e0ed 100644
--- a/lib/psych/nodes/scalar.rb
+++ b/lib/psych/nodes/scalar.rb
@@ -3,6 +3,14 @@ module Psych
###
# This class represents a {YAML Scalar}[http://yaml.org/spec/1.1/#id858081].
class Scalar < Psych::Nodes::Node
+ # Scalar Styles
+ ANY = 0
+ PLAIN = 1
+ SINGLE_QUOTED = 2
+ DOUBLE_QUOTED = 3
+ LITERAL = 4
+ FOLDED = 5
+
# The scalar value
attr_accessor :value
@@ -21,7 +29,7 @@ module Psych
# The style of this scalar
attr_accessor :style
- def initialize value, anchor, tag, plain, quoted, style
+ def initialize value, anchor = nil, tag = nil, plain = true, quoted = true, style = ANY
super()
@value = value
@anchor = anchor
diff --git a/lib/psych/nodes/sequence.rb b/lib/psych/nodes/sequence.rb
index aea97dc..a0fdb54 100644
--- a/lib/psych/nodes/sequence.rb
+++ b/lib/psych/nodes/sequence.rb
@@ -30,6 +30,11 @@ module Psych
# ]
#
class Sequence < Psych::Nodes::Node
+ # Sequence Styles
+ ANY = 0
+ BLOCK = 1
+ FLOW = 2
+
# The anchor for this sequence (if any)
attr_accessor :anchor
@@ -42,7 +47,7 @@ module Psych
# The sequece style used
attr_accessor :style
- def initialize anchor, tag, implicit, style
+ def initialize anchor = nil, tag = nil, implicit = true, style = BLOCK
super()
@anchor = anchor
@tag = tag
diff --git a/lib/psych/nodes/stream.rb b/lib/psych/nodes/stream.rb
index 911bea8..59493a6 100644
--- a/lib/psych/nodes/stream.rb
+++ b/lib/psych/nodes/stream.rb
@@ -4,8 +4,17 @@ module Psych
# Represents a YAML stream. This is the root node for any YAML parse
# tree.
class Stream < Psych::Nodes::Node
+
+ # Encodings supported by Psych (and libyaml)
+ ANY = 0
+ UTF8 = 1
+ UTF16LE = 2
+ UTF16BE = 3
+
+ # The encoding used for this stream
attr_reader :encoding
- def initialize encoding
+
+ def initialize encoding = UTF8
super()
@encoding = encoding
end
diff --git a/lib/psych/visitable.rb b/lib/psych/visitable.rb
new file mode 100644
index 0000000..72b3073
--- /dev/null
+++ b/lib/psych/visitable.rb
@@ -0,0 +1,7 @@
+module Psych
+ module Visitable
+ def accept target
+ target.accept self
+ end
+ end
+end
diff --git a/lib/psych/visitors.rb b/lib/psych/visitors.rb
new file mode 100644
index 0000000..45b4b58
--- /dev/null
+++ b/lib/psych/visitors.rb
@@ -0,0 +1,2 @@
+require 'psych/visitors/visitor'
+require 'psych/visitors/to_ruby'
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
new file mode 100644
index 0000000..bd04dda
--- /dev/null
+++ b/lib/psych/visitors/to_ruby.rb
@@ -0,0 +1,11 @@
+module Psych
+ module Visitors
+ ###
+ # This class walks a YAML AST, converting each node to ruby
+ class ToRuby < Psych::Visitors::Visitor
+ visitor_for(Nodes::Scalar) do |o|
+ o.value
+ end
+ end
+ end
+end
diff --git a/lib/psych/visitors/visitor.rb b/lib/psych/visitors/visitor.rb
new file mode 100644
index 0000000..8bc1965
--- /dev/null
+++ b/lib/psych/visitors/visitor.rb
@@ -0,0 +1,17 @@
+module Psych
+ module Visitors
+ class Visitor
+ def self.visitor_for *klasses, &block
+ klasses.each do |klass|
+ method_name = klass.name.split('::').join('_')
+ define_method(:"visit_#{method_name}", block)
+ end
+ end
+
+ def accept target
+ method_name = target.class.name.split('::').join('_')
+ send(:"visit_#{method_name}", target)
+ end
+ end
+ end
+end