Error printing

now torquescript will print out a more modern error message with a pointer to the offending character.
Multi line error outputs to be added to this for context
This commit is contained in:
marauder2k7 2024-04-24 06:42:25 +01:00
parent 4f3a1f395c
commit 6966d20104
5 changed files with 1119 additions and 921 deletions

View file

@ -1,3 +1,6 @@
%define parse.error custom
%locations
%define api.header.include {"CMDgram.h"}
%{
// bison --defines=cmdgram.h --verbose -o cmdgram.cpp -p CMD CMDgram.y
@ -20,6 +23,7 @@
int outtext(char *fmt, ...);
extern int serrors;
extern char linebuf[512];
#define nil 0
#undef YY_ARGS
@ -28,6 +32,7 @@ extern int serrors;
int CMDlex();
void CMDerror(const char *, ...);
#ifdef alloca
#undef alloca
#endif
@ -616,3 +621,38 @@ aidx_expr
;
%%
int
yyreport_syntax_error (const yypcontext_t *ctx)
{
int ret = 0;
String output;
const YYLTYPE *loc = yypcontext_location (ctx);
output += "syntax error: ";
yysymbol_kind_t nxt = yypcontext_token(ctx);
if (nxt != YYSYMBOL_YYEMPTY)
output += String::ToString("unexpected: %s at column: %d", yysymbol_name(nxt), loc->first_column);
enum { TOKENMAX = 10 };
yysymbol_kind_t expected[TOKENMAX];
int exp = yypcontext_expected_tokens(ctx, expected, TOKENMAX);
if (exp < 0)
ret = exp;
else
{
for (int i = 0; i < exp; ++i)
output += String::ToString("%s %s", i == 0 ? ": expected" : "or", yysymbol_name(expected[i]));
}
if (dStrlen(linebuf) > 1)
{
output += "\n";
output += String::ToString("%5d | %s\n", loc->first_line, linebuf);
output += String::ToString("%5s | %*s", "", loc->first_column, "^");
}
yyerror(output.c_str());
return ret;
}