]> granicus.if.org Git - jq/commitdiff
JSON parsing error messages now specify a location of the error.
authorStephen Dolan <mu@netsoc.tcd.ie>
Thu, 27 Dec 2012 01:56:23 +0000 (01:56 +0000)
committerStephen Dolan <mu@netsoc.tcd.ie>
Thu, 27 Dec 2012 01:56:23 +0000 (01:56 +0000)
Should help with #53.

jv_parse.c
jv_parse.h

index 0d4f1549d87f6d5056c7d4b253e2fc9ddf068ea4..f55c17e512f7741c9818c72ff96e83396b14dec4 100644 (file)
@@ -26,6 +26,8 @@ void jv_parser_init(struct jv_parser* p) {
   p->curr_buf = 0;
   p->curr_buf_length = p->curr_buf_pos = p->curr_buf_is_partial = 0;
   p->bom_strip_position = 0;
+  p->line = 1;
+  p->column = 0;
   jvp_dtoa_context_init(&p->dtoa);
 }
 
@@ -294,6 +296,11 @@ static int check_done(struct jv_parser* p, jv* out) {
 }
 
 static pfunc scan(struct jv_parser* p, char ch, jv* out) {
+  p->column++;
+  if (ch == '\n') {
+    p->line++;
+    p->column = 0;
+  }
   presult answer = 0;
   if (p->st == JV_PARSER_NORMAL) {
     chclass cls = classify(ch);
@@ -373,7 +380,7 @@ jv jv_parser_next(struct jv_parser* p) {
   if (msg == OK) {
     return value;
   } else if (msg) {
-    return jv_invalid_with_msg(jv_string(msg));
+    return jv_invalid_with_msg(jv_string_fmt("%s at line %d, column %d", msg, p->line, p->column));
   } else if (p->curr_buf_is_partial) {
     assert(p->curr_buf_pos == p->curr_buf_length);
     // need another buffer
index 5958316ab206e3fd0ddb2051c07b49dbbf1cb501..1d538c12b07222232d7d3641bb1af8af86dcad46 100644 (file)
@@ -16,6 +16,8 @@ struct jv_parser {
   char* tokenbuf;
   int tokenpos;
   int tokenlen;
+
+  int line, column;
   
   struct dtoa_context dtoa;