diff options
-rw-r--r-- | ext/psych/parser.c | 12 | ||||
-rw-r--r-- | test/psych/test_parser.rb | 6 |
2 files changed, 15 insertions, 3 deletions
diff --git a/ext/psych/parser.c b/ext/psych/parser.c index 5c73935..6fe4a67 100644 --- a/ext/psych/parser.c +++ b/ext/psych/parser.c @@ -1,5 +1,8 @@ #include <psych.h> +VALUE cPsychParser; +VALUE ePsychSyntaxError; + static VALUE parse_string(VALUE self, VALUE string) { yaml_parser_t parser; @@ -19,8 +22,12 @@ static VALUE parse_string(VALUE self, VALUE string) while(!done) { if(!yaml_parser_parse(&parser, &event)) { + size_t line = parser.mark.line; + size_t column = parser.mark.column; + yaml_parser_delete(&parser); - rb_raise(rb_eRuntimeError, "couldn't parse YAML"); + rb_raise(ePsychSyntaxError, "couldn't parse YAML at line %d column %d", + line, column); } switch(event.type) { @@ -156,11 +163,10 @@ static VALUE parse_string(VALUE self, VALUE string) return self; } -VALUE cPsychParser; - void Init_psych_parser() { cPsychParser = rb_define_class_under(mPsych, "Parser", rb_cObject); + ePsychSyntaxError = rb_define_class_under(mPsych, "SyntaxError", rb_eSyntaxError); rb_define_private_method(cPsychParser, "parse_string", parse_string, 1); } diff --git a/test/psych/test_parser.rb b/test/psych/test_parser.rb index 3bbd280..d996fe1 100644 --- a/test/psych/test_parser.rb +++ b/test/psych/test_parser.rb @@ -24,6 +24,12 @@ module Psych @parser = Psych::Parser.new EventCatcher.new end + def test_syntax_error + assert_raises(Psych::SyntaxError) do + @parser.parse("---\n\"foo\"\n\"bar\"\n") + end + end + def test_mapping_end @parser.parse("---\n!!map { key: value }") assert_called :end_mapping |