]> mj.ucw.cz Git - libucw.git/commitdiff
JSON: Error messages include column number
authorMartin Mares <mj@ucw.cz>
Wed, 8 Jul 2015 16:05:54 +0000 (18:05 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 8 Jul 2015 16:06:20 +0000 (18:06 +0200)
ucw-json/json.h
ucw-json/parse.c

index 503a44c0195a65e988068067ddb26b9cbe2a44d1..8e87d9cf249bce86c683615ec10e972691aea636 100644 (file)
@@ -30,6 +30,7 @@ struct json_context {
 
   struct fastbuf *in_fb;
   uint in_line;
+  uint in_column;
   bool in_eof;
   struct json_node *next_token;
   struct json_node *trivial_token;
index f0010788404e830f0667466bf464510d47b97c64..c7389c93217bff6e8fc7f0e41157e417cac0e196 100644 (file)
@@ -21,6 +21,7 @@ void json_set_input(struct json_context *js, struct fastbuf *in)
 {
   js->in_fb = in;
   js->in_line = 1;
+  js->in_column = 0;
   js->next_char = -1;
   js->next_token = NULL;
   js->in_eof = 0;
@@ -28,10 +29,9 @@ void json_set_input(struct json_context *js, struct fastbuf *in)
     js->trivial_token = json_new_node(js, JSON_INVALID);
 }
 
-// FIXME: Report column as well as line?
 static void NONRET json_parse_error(struct json_context *js, const char *msg)
 {
-  trans_throw("ucw.js.parse", js, "%s at line %u", msg, js->in_line);
+  trans_throw("ucw.js.parse", js, "%s at line %u:%u", msg, js->in_line, js->in_column);
 }
 
 static int json_get_char(struct json_context *js)
@@ -45,6 +45,7 @@ static int json_get_char(struct json_context *js)
       // FIXME: Reject alternative sequences
       return c;
     }
+  js->in_column++;
   return c;
 }
 
@@ -291,7 +292,10 @@ static struct json_node *json_read_token(struct json_context *js)
   while (c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d)
     {
       if (c == 0x0a)
-       js->in_line++;
+       {
+         js->in_line++;
+         js->in_column = 0;
+       }
       c = json_get_char(js);
     }
   if (c < 0)