Class: Psych::Parser
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Inherits: | Object |
Defined in: | ext/psych/lib/psych/parser.rb, ext/psych/psych_parser.c |
Overview
YAML event parser class. This class parses a YAML document and calls events on the handler that is passed to the constructor. The events can be used for things such as constructing a YAML AST or deserializing YAML documents. It can even be fed back to Emitter to emit the same document that was parsed.
See Handler for documentation on the events that Parser
emits.
Here is an example that prints out ever scalar found in a YAML document:
# Handler for detecting scalar values
class ScalarHandler < Psych::Handler
def scalar value, anchor, tag, plain, quoted, style
puts value
end
end
parser = Psych::Parser.new(ScalarHandler.new)
parser.parse(yaml_document)
Here is an example that feeds the parser back in to Emitter. The YAML document is read from STDIN and written back out to STDERR:
parser = Psych::Parser.new(Psych::Emitter.new($stderr))
parser.parse($stdin)
::Psych uses Parser
in combination with TreeBuilder to construct an AST of the parsed YAML document.
Constant Summary
-
ANY =
Let the parser choose the encoding
Any encoding
-
UTF16BE =
UTF-16-BE Encoding with BOM
INT2NUM(YAML_UTF16BE_ENCODING)
-
UTF16LE =
UTF-16-LE Encoding with BOM
INT2NUM(YAML_UTF16LE_ENCODING)
-
UTF8 =
UTF-8 Encoding
INT2NUM(YAML_UTF8_ENCODING)
Class Method Summary
-
.new(handler = Handler.new) ⇒ Parser
constructor
Creates a new
Parser
instance with #handler.
Instance Attribute Summary
-
#handler
rw
The handler on which events will be called.
-
#external_encoding=(value)
writeonly
Set the encoding for this parser to
encoding
Instance Method Summary
-
#mark(#) ⇒ Parser
Returns a Mark object that contains line, column, and index information.
-
#parse(yaml)
Parse the YAML document contained in
yaml
.
Constructor Details
.new(handler = Handler.new) ⇒ Parser
Instance Attribute Details
#external_encoding=(value) (writeonly)
Set the encoding for this parser to encoding
# File 'ext/psych/lib/psych/parser.rb', line 40
attr_writer :external_encoding
#handler (rw)
The handler on which events will be called
# File 'ext/psych/lib/psych/parser.rb', line 37
attr_accessor :handler
Instance Method Details
#mark(#) ⇒ Parser
Returns a Parser::Mark object that contains line, column, and index information.
#parse(yaml)
Parse the YAML document contained in yaml
. Events will be called on the handler set on the parser instance.
See Parser
and #handler