diff options
author | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-10-04 17:53:41 +0000 |
---|---|---|
committer | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-10-04 17:53:41 +0000 |
commit | 137c547c3ef8167630170275ba2ae5a14fa0af95 (patch) | |
tree | 6faa8acdcfe3ac9f79b33cca6b9e5971903ace9e /ext/psych/parser.c | |
parent | 77e58a3398cde8a0e9b0cb07ffe20e6137efe41c (diff) | |
download | ruby-137c547c3ef8167630170275ba2ae5a14fa0af95.tar.gz |
* ext/psych/lib/psych/syntax_error.rb: Add file, line, offset, and
message attributes during parse failure.
* ext/psych/parser.c: Update parser to raise exception with correct
values.
* test/psych/test_exception.rb: corresponding tests.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33404 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/psych/parser.c')
-rw-r--r-- | ext/psych/parser.c | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/ext/psych/parser.c b/ext/psych/parser.c index f3928a9510..1a6e281f43 100644 --- a/ext/psych/parser.c +++ b/ext/psych/parser.c @@ -59,6 +59,23 @@ static VALUE allocate(VALUE klass) return Data_Wrap_Struct(klass, 0, dealloc, parser); } +static VALUE make_exception(yaml_parser_t * parser, VALUE path) +{ + VALUE exception; + size_t line, column; + + line = parser->context_mark.line + 1; + column = parser->context_mark.column + 1; + + return rb_funcall(ePsychSyntaxError, rb_intern("new"), 6, + path, + INT2NUM(line), + INT2NUM(column), + INT2NUM(parser->problem_offset), + parser->problem ? rb_usascii_str_new2(parser->problem) : Qnil, + parser->context ? rb_usascii_str_new2(parser->context) : Qnil); +} + /* * call-seq: * parser.parse(yaml) @@ -98,21 +115,18 @@ static VALUE parse(VALUE self, VALUE yaml) while(!done) { if(!yaml_parser_parse(parser, &event)) { - VALUE path; - size_t line = parser->context_mark.line + 1; - size_t column = parser->context_mark.column + 1; + VALUE path, exception; if(rb_respond_to(yaml, id_path)) path = rb_funcall(yaml, id_path, 0); else path = rb_str_new2("<unknown>"); + exception = make_exception(parser, path); yaml_parser_delete(parser); yaml_parser_initialize(parser); - rb_raise(ePsychSyntaxError, "(%s): couldn't parse YAML at line %d column %d", - StringValuePtr(path), - (int)line, (int)column); + rb_exc_raise(exception); } switch(event.type) { @@ -376,6 +390,7 @@ void Init_psych_parser() /* UTF-16-BE Encoding with BOM */ rb_define_const(cPsychParser, "UTF16BE", INT2NUM(YAML_UTF16BE_ENCODING)); + rb_require("psych/syntax_error"); ePsychSyntaxError = rb_define_class_under(mPsych, "SyntaxError", rb_eSyntaxError); rb_define_method(cPsychParser, "parse", parse, 1); |