]> git.lizzy.rs Git - nothing.git/commitdiff
(#288) implement print_parse_error
authorrexim <reximkut@gmail.com>
Sat, 25 Aug 2018 16:42:53 +0000 (23:42 +0700)
committerrexim <reximkut@gmail.com>
Sat, 25 Aug 2018 16:42:53 +0000 (23:42 +0700)
src/script/parser.c
src/script/parser.h
src/script_test.c

index bdc5245531eae36e2cd0df6cb0fc268030d078c9..53f2156fef78e0e45f30fddc3ae6b1aa9c790201 100644 (file)
@@ -120,4 +120,21 @@ struct ParseResult parse_failure(const char *error,
     return result;
 }
 
-/* TODO: there is no way to create a pretty report from ParseResult in case of an error */
+void print_parse_error(FILE *stream,
+                       const char *str,
+                       struct ParseResult result)
+{
+    if (!result.is_error) {
+        return;
+    }
+
+    /* TODO: print_parse_error doesn't support colors */
+    /* TODO: print_parse_error doesn't support multiple lines */
+
+    fprintf(stream, "%s\n", str);
+    for (size_t i = 0; i < result.error_cursor; ++i) {
+        fprintf(stream, " ")
+    }
+    fprintf(stream, "^\n");
+    fprintf(stream, "%s\n", result.error);
+}
index b2141ae319e53493b4c71d1a427a17a3d8233f9b..b94041420f35abed876bd15189d6d61b5d5ae57f 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef PARSER_H_
 #define PARSER_H_
 
+#include <stdio.h>
 #include <stdbool.h>
 #include "script/expr.h"
 
@@ -24,5 +25,6 @@ struct ParseResult create_expr_from_str(const char *str,
                                         size_t *cursor,
                                         size_t n);
 
+void print_parse_error(FILE *stream, const char *str, struct ParseResult result);
 
 #endif  // PARSER_H_
index 96c77fcd7de43c99b9fda92029e7956d17d8d094..d0264ce8f8eff112c4aac2fceda5e221585b29cf 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 on character %ld", result.error, result.error_cursor);
+        print_parse_error(stderr, code, result);
     } else {
         print_expr_as_sexpr(result.expr);
     }