Explorar o código

ncd: NCDConfigTokenizer: report line and line character numbers

ambrop7 %!s(int64=14) %!d(string=hai) anos
pai
achega
1017cab63d
Modificáronse 4 ficheiros con 22 adicións e 12 borrados
  1. 2 2
      examples/ncd_tokenizer_test.c
  2. 4 4
      ncd/NCDConfigParser.c
  3. 15 5
      ncd/NCDConfigTokenizer.c
  4. 1 1
      ncd/NCDConfigTokenizer.h

+ 2 - 2
examples/ncd_tokenizer_test.c

@@ -30,10 +30,10 @@
 
 int error;
 
-static int tokenizer_output (void *user, int token, char *value, size_t pos)
+static int tokenizer_output (void *user, int token, char *value, size_t line, size_t line_char)
 {
     if (token == NCD_ERROR) {
-        printf("error at %zd\n", pos);
+        printf("line %zu, character %zu: tokenizer error\n", line, line_char);
         error = 1;
         return 0;
     }

+ 4 - 4
ncd/NCDConfigParser.c

@@ -41,7 +41,7 @@ struct parser_state {
     void *parser;
 };
 
-static int tokenizer_output (void *user, int token, char *value, size_t position)
+static int tokenizer_output (void *user, int token, char *value, size_t line, size_t line_char)
 {
     struct parser_state *state = (struct parser_state *)user;
     ASSERT(!state->out.out_of_memory)
@@ -49,7 +49,7 @@ static int tokenizer_output (void *user, int token, char *value, size_t position
     ASSERT(!state->error)
     
     if (token == NCD_ERROR) {
-        BLog(BLOG_ERROR, "tokenizer error at %zu", position);
+        BLog(BLOG_ERROR, "line %zu, character %zu: tokenizer error", line, line_char);
         state->error = 1;
         return 0;
     }
@@ -113,13 +113,13 @@ static int tokenizer_output (void *user, int token, char *value, size_t position
     
     // if we got syntax error, stop parsing
     if (state->out.syntax_error) {
-        BLog(BLOG_ERROR, "syntax error at %zu", position);
+        BLog(BLOG_ERROR, "line %zu, character %zu: syntax error", line, line_char);
         state->error = 1;
         return 0;
     }
     
     if (state->out.out_of_memory) {
-        BLog(BLOG_ERROR, "out of memory at %zu", position);
+        BLog(BLOG_ERROR, "line %zu, character %zu: out of memory", line, line_char);
         state->error = 1;
         return 0;
     }

+ 15 - 5
ncd/NCDConfigTokenizer.c

@@ -56,7 +56,8 @@ static int string_equals (char *str, int str_len, char *needle)
 
 void NCDConfigTokenizer_Tokenize (char *str, size_t left, NCDConfigTokenizer_output output, void *user)
 {
-    size_t position = 0;
+    size_t line = 1;
+    size_t line_char = 1;
     
     while (left > 0) {
         size_t l;
@@ -203,21 +204,30 @@ void NCDConfigTokenizer_Tokenize (char *str, size_t left, NCDConfigTokenizer_out
     out:
         // report error
         if (error) {
-            output(user, NCD_ERROR, NULL, position);
+            output(user, NCD_ERROR, NULL, line, line_char);
             return;
         }
         
         // output token
         if (token) {
-            if (!output(user, token, token_val, position)) {
+            if (!output(user, token, token_val, line, line_char)) {
                 return;
             }
         }
         
+        // update line/char counters
+        for (size_t i = 0; i < l; i++) {
+            if (str[i] == '\n') {
+                line++;
+                line_char = 1;
+            } else {
+                line_char++;
+            }
+        }
+        
         str += l;
         left -= l;
-        position += l;
     }
     
-    output(user, NCD_EOF, NULL, position);
+    output(user, NCD_EOF, NULL, line, line_char);
 }

+ 1 - 1
ncd/NCDConfigTokenizer.h

@@ -38,7 +38,7 @@
 #define NCD_TOKEN_ARROW 11
 #define NCD_TOKEN_TEMPLATE 12
 
-typedef int (*NCDConfigTokenizer_output) (void *user, int token, char *value, size_t position);
+typedef int (*NCDConfigTokenizer_output) (void *user, int token, char *value, size_t line, size_t line_char);
 
 void NCDConfigTokenizer_Tokenize (char *str, size_t str_len, NCDConfigTokenizer_output output, void *user);