bison: avoid using yysyntax_error_arguments

* src/parse-gram.y (yyreport_syntax_error): Use yyparse_context_token
and yyexpected_tokens.
This commit is contained in:
Akim Demaille
2020-03-21 10:22:56 +01:00
parent 00b0d02955
commit 4192de1f41
2 changed files with 60 additions and 28 deletions

View File

@@ -1237,7 +1237,7 @@ int yydebug;
*YYTOP, and *YYCAPACITY to reflect the new capacity and memory *YYTOP, and *YYCAPACITY to reflect the new capacity and memory
location. If *YYBOTTOM != YYBOTTOM_NO_FREE, then free the old stack location. If *YYBOTTOM != YYBOTTOM_NO_FREE, then free the old stack
using YYSTACK_FREE. Return 0 if successful or if no reallocation is using YYSTACK_FREE. Return 0 if successful or if no reallocation is
required. Return 1 if memory is exhausted. */ required. Return -2 if memory is exhausted. */
static int static int
yy_lac_stack_realloc (YYPTRDIFF_T *yycapacity, YYPTRDIFF_T yyadd, yy_lac_stack_realloc (YYPTRDIFF_T *yycapacity, YYPTRDIFF_T yyadd,
#if GRAM_DEBUG #if GRAM_DEBUG
@@ -1262,7 +1262,7 @@ yy_lac_stack_realloc (YYPTRDIFF_T *yycapacity, YYPTRDIFF_T yyadd,
{ {
YYDPRINTF ((stderr, "%smax size exceeded%s", yydebug_prefix, YYDPRINTF ((stderr, "%smax size exceeded%s", yydebug_prefix,
yydebug_suffix)); yydebug_suffix));
return 1; return -2;
} }
if (YYMAXDEPTH < yyalloc) if (YYMAXDEPTH < yyalloc)
yyalloc = YYMAXDEPTH; yyalloc = YYMAXDEPTH;
@@ -1274,7 +1274,7 @@ yy_lac_stack_realloc (YYPTRDIFF_T *yycapacity, YYPTRDIFF_T yyadd,
{ {
YYDPRINTF ((stderr, "%srealloc failed%s", yydebug_prefix, YYDPRINTF ((stderr, "%srealloc failed%s", yydebug_prefix,
yydebug_suffix)); yydebug_suffix));
return 1; return -2;
} }
if (*yytop != yytop_empty) if (*yytop != yytop_empty)
{ {
@@ -1325,7 +1325,7 @@ do { \
yy_lac_established = 1; \ yy_lac_established = 1; \
switch (yy_lac (yyesa, &yyes, &yyes_capacity, yyssp, yytoken)) \ switch (yy_lac (yyesa, &yyes, &yyes_capacity, yyssp, yytoken)) \
{ \ { \
case 2: \ case -2: \
goto yyexhaustedlab; \ goto yyexhaustedlab; \
case 1: \ case 1: \
goto yyerrlab; \ goto yyerrlab; \
@@ -1360,7 +1360,7 @@ do { \
/* Given the stack whose top is *YYSSP, return 0 iff YYTOKEN can /* Given the stack whose top is *YYSSP, return 0 iff YYTOKEN can
eventually (after perhaps some reductions) be shifted, return 1 if eventually (after perhaps some reductions) be shifted, return 1 if
not, or return 2 if memory is exhausted. As preconditions and not, or return -2 if memory is exhausted. As preconditions and
postconditions: *YYES_CAPACITY is the allocated size of the array to postconditions: *YYES_CAPACITY is the allocated size of the array to
which *YYES points, and either *YYES = YYESA or *YYES points to an which *YYES points, and either *YYES = YYESA or *YYES points to an
array allocated with YYSTACK_ALLOC. yy_lac may overwrite the array allocated with YYSTACK_ALLOC. yy_lac may overwrite the
@@ -1460,7 +1460,7 @@ yy_lac (yy_state_t *yyesa, yy_state_t **yyes,
yyes, yyesa, &yyesp, yyes_prev)) yyes, yyesa, &yyesp, yyes_prev))
{ {
YYDPRINTF ((stderr, "\n")); YYDPRINTF ((stderr, "\n"));
return 2; return -2;
} }
YY_IGNORE_USELESS_CAST_BEGIN YY_IGNORE_USELESS_CAST_BEGIN
*++yyesp = YY_CAST (yy_state_t, yystate); *++yyesp = YY_CAST (yy_state_t, yystate);
@@ -1485,7 +1485,9 @@ typedef struct
/* Put in YYARG at most YYARGN of the expected tokens given the /* Put in YYARG at most YYARGN of the expected tokens given the
current YYCTX, and return the number of tokens stored in YYARG. If current YYCTX, and return the number of tokens stored in YYARG. If
YYARG is null, return the number of expected tokens (guaranteed to YYARG is null, return the number of expected tokens (guaranteed to
be less than YYNTOKENS). */ be less than YYNTOKENS). Return -2 on memory exhaustion. Return 0
if there are more than YYARGN expected tokens, yet fill YYARG up to
YYARGN. */
static int static int
yyexpected_tokens (const yyparse_context_t *yyctx, yyexpected_tokens (const yyparse_context_t *yyctx,
int yyarg[], int yyargn) int yyarg[], int yyargn)
@@ -1498,7 +1500,7 @@ yyexpected_tokens (const yyparse_context_t *yyctx,
if (yyx != YYTERROR && yyx != YYUNDEFTOK) if (yyx != YYTERROR && yyx != YYUNDEFTOK)
switch (yy_lac (yyctx->yyesa, yyctx->yyes, yyctx->yyes_capacity, yyctx->yyssp, yyx)) switch (yy_lac (yyctx->yyesa, yyctx->yyes, yyctx->yyes_capacity, yyctx->yyssp, yyx))
{ {
case 2: case -2:
return -2; return -2;
case 1: case 1:
continue; continue;
@@ -1515,6 +1517,10 @@ yyexpected_tokens (const yyparse_context_t *yyctx,
static int
yysyntax_error_arguments (const yyparse_context_t *yyctx,
int yyarg[], int yyargn) YY_ATTRIBUTE_UNUSED;
static int static int
yysyntax_error_arguments (const yyparse_context_t *yyctx, yysyntax_error_arguments (const yyparse_context_t *yyctx,
int yyarg[], int yyargn) int yyarg[], int yyargn)
@@ -1558,7 +1564,17 @@ yysyntax_error_arguments (const yyparse_context_t *yyctx,
} }
/* The location of this context. */ /* The token type of the lookahead of this context. */
static int
yyparse_context_token (const yyparse_context_t *yyctx) YY_ATTRIBUTE_UNUSED;
static int
yyparse_context_token (const yyparse_context_t *yyctx)
{
return yyctx->yytoken;
}
/* The location of the lookahead of this context. */
static YYLTYPE * static YYLTYPE *
yyparse_context_location (const yyparse_context_t *yyctx) YY_ATTRIBUTE_UNUSED; yyparse_context_location (const yyparse_context_t *yyctx) YY_ATTRIBUTE_UNUSED;
@@ -2714,18 +2730,26 @@ yyreturn:
int int
yyreport_syntax_error (const yyparse_context_t *ctx) yyreport_syntax_error (const yyparse_context_t *ctx)
{ {
enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 }; int res = 0;
/* Arguments of format: reported tokens (one for the "unexpected", /* Arguments of format: reported tokens (one for the "unexpected",
one per "expected"). */ one per "expected"). */
int arg[YYERROR_VERBOSE_ARGS_MAXIMUM]; enum { ARGS_MAX = 5 };
int n = yysyntax_error_arguments (ctx, arg, YYERROR_VERBOSE_ARGS_MAXIMUM); const char *argv[ARGS_MAX];
if (n == -2) int argc = 0;
return 2; int unexpected = yyparse_context_token (ctx);
const char *argv[YYERROR_VERBOSE_ARGS_MAXIMUM]; if (unexpected != YYEMPTY)
for (int i = 0; i < n; ++i) {
argv[i] = yysymbol_name (arg[i]); argv[argc++] = yysymbol_name (unexpected);
syntax_error (*yyparse_context_location (ctx), n, argv); int expected[ARGS_MAX - 1];
return 0; int nexpected = yyexpected_tokens (ctx, expected, ARGS_MAX - 1);
if (nexpected < 0)
res = nexpected;
else
for (int i = 0; i < nexpected; ++i)
argv[argc++] = yysymbol_name (expected[i]);
}
syntax_error (*yyparse_context_location (ctx), argc, argv);
return res;
} }

View File

@@ -801,18 +801,26 @@ epilogue.opt:
int int
yyreport_syntax_error (const yyparse_context_t *ctx) yyreport_syntax_error (const yyparse_context_t *ctx)
{ {
enum { ARGS_MAX = 5 }; int res = 0;
/* Arguments of format: reported tokens (one for the "unexpected", /* Arguments of format: reported tokens (one for the "unexpected",
one per "expected"). */ one per "expected"). */
int arg[ARGS_MAX]; enum { ARGS_MAX = 5 };
int n = yysyntax_error_arguments (ctx, arg, ARGS_MAX);
if (n < 0)
return n;
const char *argv[ARGS_MAX]; const char *argv[ARGS_MAX];
for (int i = 0; i < n; ++i) int argc = 0;
argv[i] = yysymbol_name (arg[i]); int unexpected = yyparse_context_token (ctx);
syntax_error (*yyparse_context_location (ctx), n, argv); if (unexpected != YYEMPTY)
return 0; {
argv[argc++] = yysymbol_name (unexpected);
int expected[ARGS_MAX - 1];
int nexpected = yyexpected_tokens (ctx, expected, ARGS_MAX - 1);
if (nexpected < 0)
res = nexpected;
else
for (int i = 0; i < nexpected; ++i)
argv[argc++] = yysymbol_name (expected[i]);
}
syntax_error (*yyparse_context_location (ctx), argc, argv);
return res;
} }