From 73d4aafdc458c93edb5823fac1def53ea79e309b Mon Sep 17 00:00:00 2001 From: rexim Date: Sat, 25 Aug 2018 23:24:07 +0700 Subject: [PATCH] (#288) Extend ParseResult with error_cursor --- src/script/parser.c | 28 ++++++++++++++++------------ src/script/parser.h | 8 ++++++-- src/script_test.c | 2 +- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/script/parser.c b/src/script/parser.c index 630ba9c3..bdc52455 100644 --- a/src/script/parser.c +++ b/src/script/parser.c @@ -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 */ diff --git a/src/script/parser.h b/src/script/parser.h index 40b6c6a0..b2141ae3 100644 --- a/src/script/parser.h +++ b/src/script/parser.h @@ -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, diff --git a/src/script_test.c b/src/script_test.c index f45475cc..96c77fcd 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", result.error); + fprintf(stderr, "[ERROR] %s on character %ld", result.error, result.error_cursor); } else { print_expr_as_sexpr(result.expr); } -- 2.44.0