-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added YAML::PullParser and use it in YAML::Parser
- Loading branch information
Ary Borenszweig
committed
Sep 4, 2015
1 parent
3900e5a
commit 170f859
Showing
5 changed files
with
252 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
require "spec" | ||
require "yaml" | ||
|
||
class YAML::PullParser | ||
def assert_stream | ||
kind.should eq(EventKind::STREAM_START) | ||
yield read_next | ||
kind.should eq(EventKind::STREAM_END) | ||
end | ||
|
||
def assert_document | ||
kind.should eq(EventKind::DOCUMENT_START) | ||
read_next | ||
yield | ||
kind.should eq(EventKind::DOCUMENT_END) | ||
read_next | ||
end | ||
|
||
def assert_sequence(anchor = nil) | ||
kind.should eq(EventKind::SEQUENCE_START) | ||
assert_anchor anchor | ||
read_next | ||
yield | ||
kind.should eq(EventKind::SEQUENCE_END) | ||
read_next | ||
end | ||
|
||
def assert_mapping(anchor = nil) | ||
kind.should eq(EventKind::MAPPING_START) | ||
assert_anchor anchor | ||
read_next | ||
yield | ||
kind.should eq(EventKind::MAPPING_END) | ||
read_next | ||
end | ||
|
||
def assert_scalar(value, anchor = nil) | ||
kind.should eq(EventKind::SCALAR) | ||
value.should eq(value) | ||
assert_anchor anchor | ||
read_next | ||
end | ||
|
||
def assert_alias(value) | ||
kind.should eq(EventKind::ALIAS) | ||
value.should eq(value) | ||
read_next | ||
end | ||
|
||
def assert_anchor(anchor) | ||
self.anchor.should eq(anchor) if anchor | ||
end | ||
end | ||
|
||
module YAML | ||
describe PullParser do | ||
it "reads empty stream" do | ||
parser = PullParser.new("") | ||
parser.assert_stream { |kind| kind.should eq(EventKind::STREAM_END) } | ||
end | ||
|
||
it "reads an empty document" do | ||
parser = PullParser.new("---\n...\n") | ||
parser.assert_stream do | ||
parser.assert_document do | ||
parser.assert_scalar "" | ||
end | ||
end | ||
end | ||
|
||
it "reads a scalar" do | ||
parser = PullParser.new("--- foo\n...\n") | ||
parser.assert_stream do | ||
parser.assert_document do | ||
parser.assert_scalar "foo" | ||
end | ||
end | ||
end | ||
|
||
it "reads a sequence" do | ||
parser = PullParser.new("---\n- 1\n- 2\n- 3\n") | ||
parser.assert_stream do | ||
parser.assert_document do | ||
parser.assert_sequence do | ||
parser.assert_scalar "1" | ||
parser.assert_scalar "2" | ||
parser.assert_scalar "3" | ||
end | ||
end | ||
end | ||
end | ||
|
||
it "reads a scalar with an anchor" do | ||
parser = PullParser.new("--- &foo bar\n...\n") | ||
parser.assert_stream do | ||
parser.assert_document do | ||
parser.assert_scalar "bar", anchor: "foo" | ||
end | ||
end | ||
end | ||
|
||
it "reads a sequence with an anchor" do | ||
parser = PullParser.new("--- &foo []\n") | ||
parser.assert_stream do | ||
parser.assert_document do | ||
parser.assert_sequence(anchor: "foo") do | ||
end | ||
end | ||
end | ||
end | ||
|
||
it "reads a mapping" do | ||
parser = PullParser.new(%(---\nfoo: 1\nbar: 2\n)) | ||
parser.assert_stream do | ||
parser.assert_document do | ||
parser.assert_mapping do | ||
parser.assert_scalar "foo" | ||
parser.assert_scalar "1" | ||
parser.assert_scalar "bar" | ||
parser.assert_scalar "2" | ||
end | ||
end | ||
end | ||
end | ||
|
||
it "reads a mapping with an anchor" do | ||
parser = PullParser.new(%(---\n&lala {}\n)) | ||
parser.assert_stream do | ||
parser.assert_document do | ||
parser.assert_mapping(anchor: "lala") do | ||
end | ||
end | ||
end | ||
end | ||
|
||
it "parses alias" do | ||
parser = PullParser.new("--- *foo\n") | ||
parser.assert_stream do | ||
parser.assert_document do | ||
parser.assert_alias "foo" | ||
end | ||
end | ||
end | ||
|
||
it "parses alias with anchor" do | ||
parser = PullParser.new("--- *foo\n") | ||
parser.assert_stream do | ||
parser.assert_document do | ||
parser.assert_alias "foo" | ||
end | ||
end | ||
end | ||
end | ||
end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
class YAML::PullParser | ||
def initialize(content) | ||
@parser = Pointer(Void).malloc(LibYAML::PARSER_SIZE) as LibYAML::Parser* | ||
@event = LibYAML::Event.new | ||
|
||
LibYAML.yaml_parser_initialize(@parser) | ||
LibYAML.yaml_parser_set_input_string(@parser, content, LibC::SizeT.cast(content.bytesize)) | ||
|
||
read_next | ||
raise "Expected STREAM_START" unless @event.type == LibYAML::EventType::STREAM_START | ||
end | ||
|
||
def kind | ||
@event.type | ||
end | ||
|
||
def value | ||
String.new(@event.data.scalar.value) | ||
end | ||
|
||
def anchor | ||
case kind | ||
when LibYAML::EventType::SCALAR | ||
scalar_anchor | ||
when LibYAML::EventType::SEQUENCE_START | ||
sequence_anchor | ||
when LibYAML::EventType::MAPPING_START | ||
mapping_anchor | ||
else | ||
nil | ||
end | ||
end | ||
|
||
def scalar_anchor | ||
read_anchor @event.data.scalar.anchor | ||
end | ||
|
||
def sequence_anchor | ||
read_anchor @event.data.sequence_start.anchor | ||
end | ||
|
||
def mapping_anchor | ||
read_anchor @event.data.mapping_start.anchor | ||
end | ||
|
||
def alias_anchor | ||
read_anchor @event.data.alias.anchor | ||
end | ||
|
||
def read_next | ||
LibYAML.yaml_event_delete(pointerof(@event)) | ||
LibYAML.yaml_parser_parse(@parser, pointerof(@event)) | ||
kind | ||
end | ||
|
||
def close | ||
LibYAML.yaml_parser_delete(@parser) | ||
LibYAML.yaml_event_delete(pointerof(@event)) | ||
end | ||
|
||
private def read_anchor(anchor) | ||
anchor ? String.new(anchor) : nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters