all: fix the interface of yyexpected_tokens

The user gives yyexpected_tokens a limit: the max number of tokens she
wants to hear about.  That's because an error message that reports a
bazillion of possible tokens is useless.

In that case yyexpected_tokens returned 0, so the user would not know
if there are too many expected tokens or none (yes, that's possible).

There are several ways to tell the user in which situation she's in:

- return some E2MANY, a negative value.  Then it makes the pattern

    int argsize = yypcontext_expected_tokens (ctx, arg, ARGS_MAX);
    if (argsize < 0)
      return argsize;

  no longer valid, as for E2MANY (i) the user must generate the error
  message anyway, and (ii) she should not return E2MANY

- return ARGS_MAX + 1.  Then it makes it dangerous for the user, as
  she has to iterate update `min (ARGS_MAX, argsize)`.

Returning 0 is definitely simpler and safer for the user, as it tells
her "this is not an error, just generate your message without a list
of expecting tokens".  So let's still return 0, but set arg[0] to the
empty token when the list is really empty.

* data/skeletons/glr.c, data/skeletons/lalr1.cc, data/skeletons/lalr1.java
* data/skeletons/yacc.c (yyexpected_tokens): Put the empty symbol
first if there are no possible tokens at all.
* examples/c/bistromathic/parse.y: Demonstrate how to use that.
This commit is contained in:
Akim Demaille
2020-04-29 07:58:51 +02:00
committed by Akim Demaille
parent cb9f4cb543
commit d9a9b054ae
7 changed files with 29 additions and 13 deletions

View File

@@ -1158,6 +1158,8 @@ yypcontext_expected_tokens (const yypcontext_t *yyctx,
yyarg[yycount++] = YY_CAST (yysymbol_kind_t, yyx);
}
}]])[
if (yyarg && yycount == 0 && 0 < yyargn)
yyarg[0] = ]b4_symbol(-2, kind)[;
return yycount;
}