From a59a8c0fc75f6c0339facdd90302eb352e9d8ea6 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 26 Sep 2009 23:40:46 -0700 Subject: supporting scalar tags --- ext/psych/parser.c | 7 ++++++- lib/psych/parser/handler.rb | 2 +- test/psych/test_parser.rb | 11 +++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/ext/psych/parser.c b/ext/psych/parser.c index 698eb37..6fd6cb4 100644 --- a/ext/psych/parser.c +++ b/ext/psych/parser.c @@ -78,7 +78,12 @@ static VALUE parse_string(VALUE self, VALUE string) (const char *)event.data.scalar.value, (long)event.data.scalar.length ); - rb_funcall(handler, rb_intern("scalar"), 1, val); + + VALUE tag = event.data.scalar.tag ? + rb_str_new2((const char *)event.data.scalar.tag) : + Qnil; + + rb_funcall(handler, rb_intern("scalar"), 2, tag, val); } break; case YAML_STREAM_END_EVENT: diff --git a/lib/psych/parser/handler.rb b/lib/psych/parser/handler.rb index 599dfe2..a36e8fa 100644 --- a/lib/psych/parser/handler.rb +++ b/lib/psych/parser/handler.rb @@ -26,7 +26,7 @@ module Psych ### # Called when a scalar +value+ is found - def scalar value + def scalar tag, value end ### diff --git a/test/psych/test_parser.rb b/test/psych/test_parser.rb index 7d6335b..90c19b2 100644 --- a/test/psych/test_parser.rb +++ b/test/psych/test_parser.rb @@ -28,6 +28,11 @@ module Psych assert_called :scalar, ['foo'] end + def test_scalar_with_tag + @parser.parse("---\n!!str foo\n") + assert_called :scalar, ['tag:yaml.org,2002:str', 'foo'] + end + def test_alias @parser.parse(<<-eoyml) %YAML 1.1 @@ -75,8 +80,10 @@ module Psych def assert_called call, with = nil, parser = @parser if with - assert( - parser.handler.calls.any? { |x| x.compact == [call, with] }, + call = parser.handler.calls.find { |x| + x.first == call && x.last.compact == with + } + assert(call, "#{[call,with].inspect} not in #{parser.handler.calls.inspect}" ) else -- cgit v1.2.3