From e9a28d7c0eee7384cf73de5a1792745bb940e4d4 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Fri, 31 May 2013 07:04:06 +0000 Subject: [PATCH] Added another diagnostic for unexpected tokens --- diag.c | 24 ++++++++++++++++-------- gmqcc.h | 1 + parser.c | 4 +++- tests/diag/unexptok.qc | 1 + tests/diag/unexptok.tmpl | 8 ++++++++ 5 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 tests/diag/unexptok.qc create mode 100644 tests/diag/unexptok.tmpl diff --git a/diag.c b/diag.c index 81c831e..78ad7a7 100644 --- a/diag.c +++ b/diag.c @@ -139,7 +139,7 @@ static void diagnostic_feed(const char *file, size_t line, size_t column, size_t case TOKEN_EOL: /* ignore */ break; - + default: con_err("%s", read[feed]->values[itr]); break; @@ -149,26 +149,26 @@ static void diagnostic_feed(const char *file, size_t line, size_t column, size_t itr = 0; con_err("\n"); } - + switch (diagnostic) { case DIAGNOSTIC_EXPECTED_END: for (; len < vec_size(vec_last(read)->values); len++) space += strlen(vec_last(read)->values[len]); - + /* +/- 1 because of the ^ */ len = space - column - 1; space = column + 1; break; - + case DIAGNOSTIC_EXPRESSION_CASE: case DIAGNOSTIC_SEMICOLON: for (; len < vec_size(vec_last(read)->values); len++) space += strlen(vec_last(read)->values[len]); - + len = 1; space -= beg - end; break; - + case DIAGNOSTIC_EXPECTED: /* * use the column number here, it's the EASIEST method yet @@ -177,7 +177,11 @@ static void diagnostic_feed(const char *file, size_t line, size_t column, size_t space += column; len = 0; break; - + + case DIAGNOSTIC_UNEXPECTED_TOKEN: + len = column; + break; + case DIAGNOSTIC_UNEXPECTED_IDENT: /* * TODO: better way to determine the identifier. So far we @@ -257,7 +261,11 @@ void diagnostic_calculate(const char *file, size_t line, size_t column, size_t d linebeg++; marker = true; break; - + + case DIAGNOSTIC_UNEXPECTED_TOKEN: + marker = false; + break; + case DIAGNOSTIC_UNEXPECTED_IDENT: case DIAGNOSTIC_EXPECTED: marker = true; diff --git a/gmqcc.h b/gmqcc.h index 622f303..99d2555 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -1035,6 +1035,7 @@ enum { DIAGNOSTIC_SEMICOLON, DIAGNOSTIC_EXPRESSION_CASE, DIAGNOSTIC_UNEXPECTED_IDENT, + DIAGNOSTIC_UNEXPECTED_TOKEN, DIAGNOSTIC_EXPECTED, DIAGNOSTIC_EXPECTED_END }; diff --git a/parser.c b/parser.c index a4abb0e..592562a 100644 --- a/parser.c +++ b/parser.c @@ -1987,7 +1987,8 @@ static bool parse_sya_operand(parser_t *parser, shunt *sy, bool with_labels) vec_push(sy->out, syexp(parser_ctx(parser), var)); return true; } - parseerror(parser, "unexpected token `%s`", parser_tokval(parser)); + parser->diagnostic = DIAGNOSTIC_UNEXPECTED_TOKEN; + parseerror(parser, "unexpected token: `%s`", parser_tokval(parser)); return false; } @@ -6011,6 +6012,7 @@ static bool parser_global_statement(parser_t *parser) } else { + parser->diagnostic = DIAGNOSTIC_UNEXPECTED_TOKEN; parseerror(parser, "unexpected token: `%s`", parser->lex->tok.value); return false; } diff --git a/tests/diag/unexptok.qc b/tests/diag/unexptok.qc new file mode 100644 index 0000000..8202487 --- /dev/null +++ b/tests/diag/unexptok.qc @@ -0,0 +1 @@ +inte foo; diff --git a/tests/diag/unexptok.tmpl b/tests/diag/unexptok.tmpl new file mode 100644 index 0000000..878f4eb --- /dev/null +++ b/tests/diag/unexptok.tmpl @@ -0,0 +1,8 @@ +I: unexptok.qc +D: unexpected token +T: -diagnostic +C: -std=gmqcc +M: unexpected token: `inte` +M: 1: inte foo; +M: ~~~~ + -- 2.39.2