summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2009-09-27 16:40:06 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2009-09-27 16:40:06 -0700
commit7d3bb196d32afb5f9fe7ec54eb7f7f1c6fb41a86 (patch)
treea57f77ac61a9f015f1b63bfe33b398062a7ae9d2
parent1e1b03f061be4333e2d2a8a4b2b176bcab20a023 (diff)
downloadpsych-7d3bb196d32afb5f9fe7ec54eb7f7f1c6fb41a86.zip
adding yaml syntax error
-rw-r--r--ext/psych/parser.c12
-rw-r--r--test/psych/test_parser.rb6
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