Skip to content

Conversation

@akimd
Copy link
Owner

@akimd akimd commented Feb 9, 2020

This is my proposal to add support for parse.error custom|detailed to Java. I'm no Java programmer, and I have absolutely no idea if this is the proper way to do it (and push parsers must be checked too). But at least it's a starting point from which to discuss.

To see it work, have a look at examples/java/calc:

%define parse.error custom
...
%code {
  static String _ (String s)
  {
    return s;
  }
}
%%
class CalcLexer implements Calc.Lexer {
...
  public void yyreportSyntaxError (Calc.Context ctx)
  {
    final int ARGMAX = 10;
    int[] arg = new int[ARGMAX];
    int n = ctx.yysyntaxErrorArguments (arg, ARGMAX);
    System.err.print (ctx.yylocation + ": syntax error");
    for (int i = 1; i < n; ++i)
      System.err.print ((i == 1 ? ": expected " : " or ")
                        + ctx.yysymbolName (arg[i]));
    if (n != 0)
      System.err.print (" before " + ctx.yysymbolName (arg[0]));
    System.err.println ("");
  }
...

akimd added 7 commits February 8, 2020 11:24
We used to emit:

    /** Token number,to be returned by the scanner.  */
    static final int NUM = 258;
    /** Token number,to be returned by the scanner.  */
    static final int NEG = 259;

with no space after the comma.  Fix that.

* data/skeletons/bison.m4 (b4_token_format): Quote where appropriate.
In Java there is no need for N_ and yytranslate_.  So instead of
hard-coding the use of N_ in the table of the symbol names, rely on
b4_symbol_translate.

* src/output.c (prepare_symbol_names): Use b4_symbol_translate instead
of N_.
* data/skeletons/c.m4 (b4_symbol_translate): New.
* data/skeletons/lalr1.java (yysymbolName): New.
Use it.
* examples/java/calc/Calc.y: Use parse.error=detailed.
* tests/calc.at: Check parse.error=detailed.
And allow syntax error messages for 'detailed' and 'verbose' to be
translated.

* data/skeletons/lalr1.java (Context, yyexpectedTokens)
(yysyntaxErrorArguments): New.
(yysyntax_error): Use it.
The error format should be translated, but contrary to the case of
C/C++, we cannot just depend on macros to adapt on the
presence/absence of '_'.  Let's consider that the message format is to
be translated iff there are some internationalized tokens.

* src/output.c (prepare_symbol_names): Define b4_has_translations.
* data/skeletons/java.m4 (b4_trans): New.
* data/skeletons/lalr1.java: Use it to emit translatable or not the
format string.
* data/skeletons/lalr1.java (Context.yytokens): New.
* data/skeletons/lalr1.java: Add support for custom parse errors.
(yyntokens_): Make it public.  Under...
(yyntokens): this name.
(Context): Capture the location too.
* examples/c/bistromathic/parse.y,
* examples/c/bistromathic/bistromathic.test:
Improve error message.
* examples/java/calc/Calc.test, examples/java/calc/Calc.y: Use custom
error messages.
* tests/calc.at, tests/local.at: Check custom error messages.
* data/skeletons/lalr1.java (yyexpectedTokens)
(yysyntaxErrorArguments): Make them methods of Context.
(Context.yysymbolName): New.
* tests/local.at: Adjust.
@akimd akimd force-pushed the yyreport_syntax_error branch from 3894402 to 80a4389 Compare February 9, 2020 14:20
@akimd akimd merged commit 80a4389 into master Feb 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants