Class: Psych::Parser
- Defined in:
- 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 Psych::Emitter to emit the same document that was parsed.
See Psych::Handler for documentation on the events that Psych::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 Psych::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 Psych::Parser in combination with Psych::TreeBuilder to construct an AST of the parsed YAML document.
Defined Under Namespace
Classes: Mark
Constant Summary collapse
- ANY =
Let the parser choose the encoding
Any encoding
- UTF8 =
UTF-8 Encoding
INT2NUM(YAML_UTF8_ENCODING)
- UTF16LE =
UTF-16-LE Encoding with BOM
INT2NUM(YAML_UTF16LE_ENCODING)
- UTF16BE =
UTF-16-BE Encoding with BOM
INT2NUM(YAML_UTF16BE_ENCODING)
Instance Attribute Summary collapse
-
#external_encoding ⇒ Object
writeonly
Set the encoding for this parser to
encoding
. -
#handler ⇒ Object
The handler on which events will be called.
Instance Method Summary collapse
-
#initialize(handler = Handler.new) ⇒ Parser
constructor
Creates a new Psych::Parser instance with
handler
. -
#mark ⇒ Object
Returns a Psych::Parser::Mark object that contains line, column, and index information.
-
#parse(yaml, path = yaml.respond_to?(:path) ? yaml.path : "<unknown>") ⇒ Object
call-seq: parser.parse(yaml).
Constructor Details
#initialize(handler = Handler.new) ⇒ Parser
Creates a new Psych::Parser instance with handler
. YAML events will be called on handler
. See Psych::Parser for more details.
47 48 49 50 |
# File 'lib/psych/parser.rb', line 47 def initialize handler = Handler.new @handler = handler @external_encoding = ANY end |
Instance Attribute Details
#external_encoding=(value) ⇒ Object (writeonly)
Set the encoding for this parser to encoding
41 42 43 |
# File 'lib/psych/parser.rb', line 41 def external_encoding=(value) @external_encoding = value end |
#handler ⇒ Object
The handler on which events will be called
38 39 40 |
# File 'lib/psych/parser.rb', line 38 def handler @handler end |
Instance Method Details
#mark ⇒ Object
Returns a Psych::Parser::Mark object that contains line, column, and index information.
508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'ext/psych/psych_parser.c', line 508
static VALUE mark(VALUE self)
{
VALUE mark_klass;
VALUE args[3];
yaml_parser_t * parser;
TypedData_Get_Struct(self, yaml_parser_t, &psych_parser_type, parser);
mark_klass = rb_const_get_at(cPsychParser, rb_intern("Mark"));
args[0] = SIZET2NUM(parser->mark.index);
args[1] = SIZET2NUM(parser->mark.line);
args[2] = SIZET2NUM(parser->mark.column);
return rb_class_new_instance(3, args, mark_klass);
}
|
#parse(yaml, path = yaml.respond_to?(:path) ? yaml.path : "<unknown>") ⇒ Object
call-seq:
parser.parse(yaml)
Parse the YAML document contained in yaml
. Events will be called on the handler set on the parser instance.
See Psych::Parser and Psych::Parser#handler
61 62 63 |
# File 'lib/psych/parser.rb', line 61 def parse yaml, path = yaml.respond_to?(:path) ? yaml.path : "<unknown>" _native_parse @handler, yaml, path end |