]> git.lizzy.rs Git - nothing.git/commitdiff
(#288) Extend ParseResult with error_cursor
authorrexim <reximkut@gmail.com>
Sat, 25 Aug 2018 16:24:07 +0000 (23:24 +0700)
committerrexim <reximkut@gmail.com>
Sat, 25 Aug 2018 16:24:07 +0000 (23:24 +0700)
src/script/parser.c
src/script/parser.h
src/script_test.c

index 630ba9c32c11dba830225fa2db23f7c1ca0aef71..bdc5245531eae36e2cd0df6cb0fc268030d078c9 100644 (file)
@@ -25,7 +25,7 @@ struct ParseResult create_expr_from_str(const char *str,
 
     skip_whitespaces(str, cursor, n);
     if (*cursor >= n) {
-        return parse_failure("EOF");
+        return parse_failure("EOF", *cursor);
     }
 
     switch (str[*cursor]) {
@@ -38,17 +38,17 @@ struct ParseResult create_expr_from_str(const char *str,
 
         skip_whitespaces(str, cursor, n);
         if (*cursor >= n) {
-            return parse_failure("EOF");
+            return parse_failure("EOF", *cursor);
         }
 
         if (str[*cursor] != '.') {
-            return parse_failure("Expected .");
+            return parse_failure("Expected .", *cursor);
         }
         (*cursor)++;
 
         skip_whitespaces(str, cursor, n);
         if (*cursor >= n) {
-            return parse_failure("EOF");
+            return parse_failure("EOF", *cursor);
         }
 
         struct ParseResult cdr = create_expr_from_str(str, cursor, n);
@@ -58,11 +58,11 @@ struct ParseResult create_expr_from_str(const char *str,
 
         skip_whitespaces(str, cursor, n);
         if (*cursor >= n) {
-            return parse_failure("EOF");
+            return parse_failure("EOF", *cursor);
         }
 
         if (str[*cursor] != ')') {
-            return parse_failure("Expected )");
+            return parse_failure("Expected )", *cursor);
         }
 
         (*cursor)++;
@@ -72,7 +72,7 @@ struct ParseResult create_expr_from_str(const char *str,
 
     case '"': {
         /* TODO(#288): create_expr_from_str does not support strings */
-        return parse_failure("Strings are not supported");
+        return parse_failure("Strings are not supported", *cursor);
     }
 
     default: {
@@ -82,7 +82,7 @@ struct ParseResult create_expr_from_str(const char *str,
             const double x = strtod(nptr, &endptr);
 
             if (nptr == endptr) {
-                return parse_failure("Number expected");
+                return parse_failure("Number expected", *cursor);
             }
 
             *cursor += (size_t) (endptr - nptr);
@@ -90,12 +90,12 @@ struct ParseResult create_expr_from_str(const char *str,
             return parse_success(atom_as_expr(create_atom(ATOM_NUMBER, x)));
         } else if (isalpha(str[*cursor])) {
             /* TODO(#289): create_expr_from_str does not support symbols */
-            return parse_failure("Symbols are not supported");
+            return parse_failure("Symbols are not supported", *cursor);
         }
     }
     }
 
-    return parse_failure("Unexpected sequence of characters");
+    return parse_failure("Unexpected sequence of characters", *cursor);
 }
 
 struct ParseResult parse_success(struct Expr expr)
@@ -108,12 +108,16 @@ struct ParseResult parse_success(struct Expr expr)
     return result;
 }
 
-struct ParseResult parse_failure(const char *error)
+struct ParseResult parse_failure(const char *error,
+                                 size_t error_cursor)
 {
     struct ParseResult result = {
         .is_error = true,
-        .error = error
+        .error = error,
+        .error_cursor = error_cursor
     };
 
     return result;
 }
+
+/* TODO: there is no way to create a pretty report from ParseResult in case of an error */
index 40b6c6a053c8f9ad092a248a33addf978ad5caa2..b2141ae319e53493b4c71d1a427a17a3d8233f9b 100644 (file)
@@ -9,12 +9,16 @@ struct ParseResult
     bool is_error;
     union {
         struct Expr expr;
-        const char *error;
+        struct {
+            const char *error;
+            size_t error_cursor;
+        };
     };
 };
 
 struct ParseResult parse_success(struct Expr expr);
-struct ParseResult parse_failure(const char *error);
+struct ParseResult parse_failure(const char *error,
+                                 size_t error_cursor);
 
 struct ParseResult create_expr_from_str(const char *str,
                                         size_t *cursor,
index f45475cc440b94c6cede9448244d8a43360cce38..96c77fcd7de43c99b9fda92029e7956d17d8d094 100644 (file)
@@ -15,7 +15,7 @@ int main(int argc, char *argv[])
     struct ParseResult result = create_expr_from_str(code, &cursor, n);
 
     if (result.is_error) {
-        fprintf(stderr, "[ERROR] %s", result.error);
+        fprintf(stderr, "[ERROR] %s on character %ld", result.error, result.error_cursor);
     } else {
         print_expr_as_sexpr(result.expr);
     }