From: rexim Date: Sat, 25 Aug 2018 16:42:53 +0000 (+0700) Subject: (#288) implement print_parse_error X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=e38627f4887d677e696007c9eb8b58542cc6c4d3;p=nothing.git (#288) implement print_parse_error --- diff --git a/src/script/parser.c b/src/script/parser.c index bdc52455..53f2156f 100644 --- a/src/script/parser.c +++ b/src/script/parser.c @@ -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); +} diff --git a/src/script/parser.h b/src/script/parser.h index b2141ae3..b9404142 100644 --- a/src/script/parser.h +++ b/src/script/parser.h @@ -1,6 +1,7 @@ #ifndef PARSER_H_ #define PARSER_H_ +#include #include #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_ diff --git a/src/script_test.c b/src/script_test.c index 96c77fcd..d0264ce8 100644 --- a/src/script_test.c +++ b/src/script_test.c @@ -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); }