Various Java skeleton improvements.

* NEWS: Document them.

General Java skeleton improvements.
* configure.ac (gt_JAVACOMP): Request target of 1.4, which allows
using gcj < 4.3 in the testsuite, according to comments in
gnulib/m4/javacomp.m4.
* data/java.m4 (stype, parser_class_name, lex_throws, throws,
location_type, position_type): Remove extraneous brackets from
b4_percent_define_default.
(b4_lex_param, b4_parse_param): Remove extraneous brackets from
m4_define and m4_define_default.
* data/lalr1.java (b4_pre_prologue): Change to b4_user_post_prologue,
which marks the end of user code with appropriate syncline, like all
the other skeletons.
(b4_user_post_prologue): Add.  Don't silently drop.
(yylex): Remove.
(parse): Inline yylex.
* doc/bison.texinfo (bisonVersion, bisonSkeleton): Document.
(%{...%}): Fix typo of %code imports.
* tests/java.at (AT_JAVA_COMPILE): Add "java" keyword.
Support annotations on parser class with %define annotations.
* data/lalr1.java (annotations): Add to parser class modifier.
* doc/bison.texinfo (Java Parser Interface): Document
%define annotations.
(Java Declarations Summary): Document %define annotations.
* tests/java.at (Java parser class modifiers): Test annotations.
Do not generate code for %error-verbose unless requested.
* data/lalr1.java (errorVerbose): Rename to yyErrorVerbose.
Make private.  Make conditional on %error-verbose.
(getErrorVerbose, setErrorVerbose): New.
(yytnamerr_): Make conditional on %error-verbose.
(yysyntax_error): Make some code conditional on %error-verbose.
* doc/bison.texinfo (Java Bison Interface): Remove the parts
about %error-verbose having no effect.
(getErrorVerbose, setErrorVerbose): Document.
Move constants for token names to Lexer interface.
* data/lalr1.java (Lexer): Move EOF, b4_token_enums(b4_tokens) here.
* data/java.m4 (b4_token_enum): Indent for move to Lexer interface.
(parse): Qualify EOF to Lexer.EOF.
* doc/bison.texinfo (Java Parser Interface): Move documentation of
EOF and token names to Java Lexer Interface.
* tests/java.at (_AT_DATA_JAVA_CALC_Y): Remove Calc qualifier.
Make yyerror public.
* data/lalr1.java (Lexer.yyerror): Use longer parameter name.
(yyerror): Change to public.  Add Javadoc comments.  Use longer
parameter names.  Make the body rather than the declarator
conditional on %locations.
* doc/bison.texinfo (yyerror): Document.  Don't mark as protected.
Allow user to add code to the constructor with %code init.
* data/java.m4 (b4_init_throws): New, for %define init_throws.
* data/lalr1.java (YYParser.YYParser): Add b4_init_throws.
Add %code init to the front of the constructor body.
* doc/bison.texinfo (YYParser.YYParser): Document %code init
and %define init_throws.
(Java Declarations Summary): Document %code init and
%define init_throws.
* tests/java.at (Java %parse-param and %lex-param): Adjust grep.
(Java constructor init and init_throws): Add tests.
This commit is contained in:
Di-an Jan
2008-11-10 14:29:07 +01:00
committed by Paolo Bonzini
parent 42f832d680
commit 1979121c96
7 changed files with 282 additions and 102 deletions

View File

@@ -125,8 +125,8 @@ m4_define([b4_null], [null])
# ---------------------------------------
# Output the definition of this token as an enum.
m4_define([b4_token_enum],
[ /** Token number, to be returned by the scanner. */
public static final int $1 = $2;
[ /** Token number, to be returned by the scanner. */
static final int $1 = $2;
])
@@ -154,24 +154,27 @@ m4_define([b4_case], [ case $1:
## ---------------- ##
m4_define([b4_yystype], [b4_percent_define_get([[stype]])])
b4_percent_define_default([[stype]], [[Object]])])
b4_percent_define_default([[stype]], [[Object]])
# %name-prefix
m4_define_default([b4_prefix], [[YY]])
b4_percent_define_default([[parser_class_name]], [b4_prefix[]Parser])])
b4_percent_define_default([[parser_class_name]], [b4_prefix[]Parser])
m4_define([b4_parser_class_name], [b4_percent_define_get([[parser_class_name]])])
b4_percent_define_default([[lex_throws]], [[java.io.IOException]])])
b4_percent_define_default([[lex_throws]], [[java.io.IOException]])
m4_define([b4_lex_throws], [b4_percent_define_get([[lex_throws]])])
b4_percent_define_default([[throws]], [])])
b4_percent_define_default([[throws]], [])
m4_define([b4_throws], [b4_percent_define_get([[throws]])])
b4_percent_define_default([[location_type]], [Location])])
b4_percent_define_default([[init_throws]], [])
m4_define([b4_init_throws], [b4_percent_define_get([[init_throws]])])
b4_percent_define_default([[location_type]], [Location])
m4_define([b4_location_type], [b4_percent_define_get([[location_type]])])
b4_percent_define_default([[position_type]], [Position])])
b4_percent_define_default([[position_type]], [Position])
m4_define([b4_position_type], [b4_percent_define_get([[position_type]])])
@@ -218,9 +221,9 @@ m4_define([b4_rhs_location],
# it to be single quoted. Same for b4_parse_param.
# TODO: should be in bison.m4
m4_define_default([b4_lex_param], [[]]))
m4_define([b4_lex_param], b4_lex_param))
m4_define([b4_parse_param], b4_parse_param))
m4_define_default([b4_lex_param], [[]])
m4_define([b4_lex_param], b4_lex_param)
m4_define([b4_parse_param], b4_parse_param)
# b4_lex_param_decl
# -------------------

View File

@@ -29,14 +29,16 @@ b4_copyright([Skeleton implementation for Bison LALR(1) parsers in Java],
b4_percent_define_ifdef([package], [package b4_percent_define_get([package]);
])[/* First part of user declarations. */
]b4_pre_prologue
]b4_user_pre_prologue
b4_user_post_prologue
b4_percent_code_get([[imports]])
[/**
* A Bison parser, automatically generated from <tt>]m4_bpatsubst(b4_file_name, [^"\(.*\)"$], [\1])[</tt>.
*
* @@author LALR (1) parser skeleton written by Paolo Bonzini.
*/
]b4_public_if([public ])dnl
]b4_percent_define_get3([annotations], [], [ ])dnl
b4_public_if([public ])dnl
b4_abstract_if([abstract ])dnl
b4_final_if([final ])dnl
b4_strictfp_if([strictfp ])dnl
@@ -45,9 +47,22 @@ b4_percent_define_get3([extends], [ extends ])dnl
b4_percent_define_get3([implements], [ implements ])[
{
]b4_identification[
]b4_error_verbose_if([[
/** True if verbose error messages are enabled. */
public boolean errorVerbose = ]b4_flag_value([error_verbose]);
private boolean yyErrorVerbose = true;
/**
* Return whether verbose error messages are enabled.
*/
public final boolean getErrorVerbose() { return yyErrorVerbose; }
/**
* Set the verbosity of error messages.
* @@param verbose True to request verbose error messages.
*/
public final void setErrorVerbose(boolean verbose)
{ yyErrorVerbose = verbose; }
]])
b4_locations_if([[
/**
@@ -93,11 +108,6 @@ b4_locations_if([[
]])
[ /** Token returned by the scanner to signal the end of its input. */
public static final int EOF = 0;]
b4_token_enums(b4_tokens)
b4_locations_if([[
private ]b4_location_type[ yylloc (YYStack rhs, int n)
{
@@ -112,6 +122,11 @@ b4_token_enums(b4_tokens)
* parser <tt>]b4_parser_class_name[</tt>.
*/
public interface Lexer {
/** Token returned by the scanner to signal the end of its input. */
public static final int EOF = 0;
]b4_token_enums(b4_tokens)[
]b4_locations_if([[/**
* Method to retrieve the beginning position of the last scanned token.
* @@return the position at which the last scanned token starts. */
@@ -140,8 +155,8 @@ b4_token_enums(b4_tokens)
*
* ]b4_locations_if([[@@param loc The location of the element to which the
* error message is related]])[
* @@param s The string for the error message. */
void yyerror (]b4_locations_if([b4_location_type[ loc, ]])[String s);]
* @@param msg The string for the error message. */
void yyerror (]b4_locations_if([b4_location_type[ loc, ]])[String msg);]
}
b4_lexer_if([[private class YYLexer implements Lexer {
@@ -157,7 +172,9 @@ b4_lexer_if([[
/**
* Instantiates the Bison-generated parser.
*/
public ]b4_parser_class_name (b4_parse_param_decl([b4_lex_param_decl])[) {
public ]b4_parser_class_name (b4_parse_param_decl([b4_lex_param_decl])[) ]b4_maybe_throws([b4_init_throws])[
{
]b4_percent_code_get([[init]])[
this.yylexer = new YYLexer(]b4_lex_param_call[);
]b4_parse_param_cons[
}
@@ -167,7 +184,9 @@ b4_lexer_if([[
* Instantiates the Bison-generated parser.
* @@param yylexer The scanner that will supply tokens to the parser.
*/
b4_lexer_if([[protected]], [[public]]) b4_parser_class_name[ (]b4_parse_param_decl([[Lexer yylexer]])[) {
b4_lexer_if([[protected]], [[public]]) b4_parser_class_name[ (]b4_parse_param_decl([[Lexer yylexer]])[) ]b4_maybe_throws([b4_init_throws])[
{
]b4_percent_code_get([[init]])[
this.yylexer = yylexer;
]b4_parse_param_cons[
}
@@ -201,20 +220,35 @@ b4_lexer_if([[
*/
public final void setDebugLevel(int level) { yydebug = level; }
private final int yylex () ]b4_maybe_throws([b4_lex_throws]) [{
return yylexer.yylex ();
/**
* Print an error message via the lexer.
*]b4_locations_if([[ Use a <code>null</code> location.]])[
* @@param msg The error message.
*/
public final void yyerror (String msg)
{
yylexer.yyerror (]b4_locations_if([[(]b4_location_type[)null, ]])[msg);
}
protected final void yyerror (]b4_locations_if([b4_location_type[ loc, ]])[String s) {
yylexer.yyerror (]b4_locations_if([loc, ])[s);
]b4_locations_if([[
/**
* Print an error message via the lexer.
* @@param loc The location associated with the message.
* @@param msg The error message.
*/
public final void yyerror (]b4_location_type[ loc, String msg)
{
yylexer.yyerror (loc, msg);
}
]b4_locations_if([
protected final void yyerror (String s) {
yylexer.yyerror ((]b4_location_type[)null, s);
}
protected final void yyerror (]b4_position_type[ loc, String s) {
yylexer.yyerror (new ]b4_location_type[ (loc), s);
}])
/**
* Print an error message via the lexer.
* @@param pos The position associated with the message.
* @@param msg The error message.
*/
public final void yyerror (]b4_position_type[ pos, String msg)
{
yylexer.yyerror (new ]b4_location_type[ (pos), msg);
}]])
[protected final void yycdebug (String s) {
if (yydebug > 0)
@@ -372,6 +406,7 @@ b4_lexer_if([[
return YYNEWSTATE;
}
]b4_error_verbose_if([[
/* Return YYSTR after stripping away unnecessary quotes and
backslashes, so that it's suitable for yyerror. The heuristic is
that double-quoting is unnecessary unless the string contains an
@@ -406,6 +441,7 @@ b4_lexer_if([[
return yystr;
}
]])[
/*--------------------------------.
| Print this symbol on YYOUTPUT. |
@@ -498,19 +534,19 @@ m4_popdef([b4_at_dollar])])dnl
/* Read a lookahead token. */
if (yychar == yyempty_)
{
yycdebug ("Reading a token: ");
yychar = yylex ();]
yycdebug ("Reading a token: ");
yychar = yylexer.yylex ();]
b4_locations_if([[
yylloc = new ]b4_location_type[(yylexer.getStartPos (),
yylexer.getEndPos ());]])
yylloc = new ]b4_location_type[(yylexer.getStartPos (),
yylexer.getEndPos ());]])
yylval = yylexer.getLVal ();[
}
/* Convert token to internal form. */
if (yychar <= EOF)
if (yychar <= Lexer.EOF)
{
yychar = yytoken = EOF;
yycdebug ("Now at end of input.\n");
yychar = yytoken = Lexer.EOF;
yycdebug ("Now at end of input.\n");
}
else
{
@@ -591,16 +627,16 @@ m4_popdef([b4_at_dollar])])dnl
]b4_locations_if([yyerrloc = yylloc;])[
if (yyerrstatus_ == 3)
{
/* If just tried and failed to reuse lookahead token after an
error, discard it. */
/* If just tried and failed to reuse lookahead token after an
error, discard it. */
if (yychar <= EOF)
{
/* Return failure if at end of input. */
if (yychar == EOF)
return false;
}
else
if (yychar <= Lexer.EOF)
{
/* Return failure if at end of input. */
if (yychar == Lexer.EOF)
return false;
}
else
yychar = yyempty_;
}
@@ -682,8 +718,8 @@ m4_popdef([b4_at_dollar])])dnl
// Generate an error message.
private String yysyntax_error (int yystate, int tok)
{
if (errorVerbose)
{]b4_error_verbose_if([[
if (yyErrorVerbose)
{
int yyn = yypact_[yystate];
if (yypact_ninf_ < yyn && yyn <= yylast_)
@@ -716,14 +752,13 @@ m4_popdef([b4_at_dollar])])dnl
res.append (yytnamerr_ (yytname_[x]));
}
}
return res.toString ();
return res.toString ();
}
}
]])[
return "syntax error";
}
/* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
STATE-NUM. */
private static final ]b4_int_type_for([b4_pact])[ yypact_ninf_ = ]b4_pact_ninf[;