3 # Java language support for Bison
 
   5 # Copyright (C) 2007-2013 Free Software Foundation, Inc.
 
   7 # This program is free software: you can redistribute it and/or modify
 
   8 # it under the terms of the GNU General Public License as published by
 
   9 # the Free Software Foundation, either version 3 of the License, or
 
  10 # (at your option) any later version.
 
  12 # This program is distributed in the hope that it will be useful,
 
  13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  15 # GNU General Public License for more details.
 
  17 # You should have received a copy of the GNU General Public License
 
  18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
  20 m4_include(b4_pkgdatadir/[c-like.m4])
 
  23 # b4_list2(LIST1, LIST2)
 
  24 # ----------------------
 
  25 # Join two lists with a comma if necessary.
 
  27           [$1[]m4_ifval(m4_quote($1), [m4_ifval(m4_quote($2), [[, ]])])[]$2])
 
  30 # b4_percent_define_get3(DEF, PRE, POST, NOT)
 
  31 # -------------------------------------------
 
  32 # Expand to the value of DEF surrounded by PRE and POST if it's %define'ed,
 
  34 m4_define([b4_percent_define_get3],
 
  35           [m4_ifval(m4_quote(b4_percent_define_get([$1])),
 
  36                 [$2[]b4_percent_define_get([$1])[]$3], [$4])])
 
  40 # b4_flag_value(BOOLEAN-FLAG)
 
  41 # ---------------------------
 
  42 m4_define([b4_flag_value], [b4_flag_if([$1], [true], [false])])
 
  45 # b4_public_if(TRUE, FALSE)
 
  46 # -------------------------
 
  47 b4_percent_define_default([[public]], [[false]])
 
  48 m4_define([b4_public_if],
 
  49 [b4_percent_define_flag_if([public], [$1], [$2])])
 
  52 # b4_abstract_if(TRUE, FALSE)
 
  53 # ---------------------------
 
  54 b4_percent_define_default([[abstract]], [[false]])
 
  55 m4_define([b4_abstract_if],
 
  56 [b4_percent_define_flag_if([abstract], [$1], [$2])])
 
  59 # b4_final_if(TRUE, FALSE)
 
  60 # ---------------------------
 
  61 b4_percent_define_default([[final]], [[false]])
 
  62 m4_define([b4_final_if],
 
  63 [b4_percent_define_flag_if([final], [$1], [$2])])
 
  66 # b4_strictfp_if(TRUE, FALSE)
 
  67 # ---------------------------
 
  68 b4_percent_define_default([[strictfp]], [[false]])
 
  69 m4_define([b4_strictfp_if],
 
  70 [b4_percent_define_flag_if([strictfp], [$1], [$2])])
 
  73 # b4_lexer_if(TRUE, FALSE)
 
  74 # ------------------------
 
  75 m4_define([b4_lexer_if],
 
  76 [b4_percent_code_ifdef([[lexer]], [$1], [$2])])
 
  81 m4_define([b4_identification],
 
  82 [  /** Version number for the Bison executable that generated this parser.  */
 
  83   public static final String bisonVersion = "b4_version";
 
  85   /** Name of the skeleton that generated this parser.  */
 
  86   public static final String bisonSkeleton = b4_skeleton;
 
  94 # b4_int_type(MIN, MAX)
 
  95 # ---------------------
 
  96 # Return the smallest int type able to handle numbers ranging from
 
  97 # MIN to MAX (included).
 
  98 m4_define([b4_int_type],
 
  99 [m4_if(b4_ints_in($@,   [-128],   [127]), [1], [byte],
 
 100        b4_ints_in($@, [-32768], [32767]), [1], [short],
 
 103 # b4_int_type_for(NAME)
 
 104 # ---------------------
 
 105 # Return the smallest int type able to handle numbers ranging from
 
 106 # 'NAME_min' to 'NAME_max' (included).
 
 107 m4_define([b4_int_type_for],
 
 108 [b4_int_type($1_min, $1_max)])
 
 112 m4_define([b4_null], [null])
 
 115 # b4_typed_parser_table_define(TYPE, NAME, DATA, COMMENT)
 
 116 # -------------------------------------------------------
 
 117 m4_define([b4_typed_parser_table_define],
 
 118 [m4_ifval([$4], [b4_comment([$4])
 
 120 [private static final ]$1[ yy$2_[] = yy$2_init();
 
 121   private static final ]$1[[] yy$2_init()
 
 130 # b4_integral_parser_table_define(NAME, DATA, COMMENT)
 
 131 #-----------------------------------------------------
 
 132 m4_define([b4_integral_parser_table_define],
 
 133 [b4_typed_parser_table_define([b4_int_type_for([$2])], [$1], [$2], [$3])])
 
 136 ## ------------------------- ##
 
 137 ## Assigning token numbers.  ##
 
 138 ## ------------------------- ##
 
 140 # b4_token_enum(TOKEN-NUM)
 
 141 # ------------------------
 
 142 # Output the definition of this token as an enum.
 
 143 m4_define([b4_token_enum],
 
 144 [b4_token_format([    /** Token number, to be returned by the scanner.  */
 
 145     static final int %s = %s;
 
 150 # Output the definition of the tokens (if there are) as enums.
 
 151 m4_define([b4_token_enums],
 
 152 [b4_any_token_visible_if([/* Tokens.  */
 
 153 b4_symbol_foreach([b4_token_enum])])])
 
 157 # We need to fool Java's stupid unreachable code detection.
 
 158 m4_define([b4_case], [  case $1:
 
 164 # b4_predicate_case(LABEL, CONDITIONS)
 
 165 # ------------------------------------
 
 166 m4_define([b4_predicate_case], [  case $1:
 
 172 ## ---------------- ##
 
 173 ## Default values.  ##
 
 174 ## ---------------- ##
 
 176 m4_define([b4_yystype], [b4_percent_define_get([[api.value.type]])])
 
 177 b4_percent_define_default([[api.value.type]], [[Object]])
 
 180 m4_define_default([b4_prefix], [[YY]])
 
 182 b4_percent_define_default([[parser_class_name]], [b4_prefix[]Parser])
 
 183 m4_define([b4_parser_class_name], [b4_percent_define_get([[parser_class_name]])])
 
 185 b4_percent_define_default([[lex_throws]], [[java.io.IOException]])
 
 186 m4_define([b4_lex_throws], [b4_percent_define_get([[lex_throws]])])
 
 188 b4_percent_define_default([[throws]], [])
 
 189 m4_define([b4_throws], [b4_percent_define_get([[throws]])])
 
 191 b4_percent_define_default([[init_throws]], [])
 
 192 m4_define([b4_init_throws], [b4_percent_define_get([[init_throws]])])
 
 194 b4_percent_define_default([[api.location.type]], [Location])
 
 195 m4_define([b4_location_type], [b4_percent_define_get([[api.location.type]])])
 
 197 b4_percent_define_default([[api.position.type]], [Position])
 
 198 m4_define([b4_position_type], [b4_percent_define_get([[api.position.type]])])
 
 201 ## ----------------- ##
 
 202 ## Semantic Values.  ##
 
 203 ## ----------------- ##
 
 206 # b4_lhs_value([TYPE])
 
 207 # --------------------
 
 208 # Expansion of $<TYPE>$.
 
 209 m4_define([b4_lhs_value], [yyval])
 
 212 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
 
 213 # --------------------------------------
 
 214 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
 
 217 # In this simple implementation, %token and %type have class names
 
 218 # between the angle brackets.
 
 219 m4_define([b4_rhs_value],
 
 220 [(m4_ifval($3, [($3)])[](yystack.valueAt ($1-($2))))])
 
 225 m4_define([b4_lhs_location],
 
 229 # b4_rhs_location(RULE-LENGTH, NUM)
 
 230 # ---------------------------------
 
 231 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
 
 233 m4_define([b4_rhs_location],
 
 234 [yystack.locationAt ($1-($2))])
 
 240 # If defined, b4_lex_param arrives double quoted, but below we prefer
 
 241 # it to be single quoted.  Same for b4_parse_param.
 
 243 # TODO: should be in bison.m4
 
 244 m4_define_default([b4_lex_param], [[]])
 
 245 m4_define([b4_lex_param], b4_lex_param)
 
 246 m4_define([b4_parse_param], b4_parse_param)
 
 250 # Extra formal arguments of the constructor.
 
 251 m4_define([b4_lex_param_decl],
 
 252 [m4_ifset([b4_lex_param],
 
 253           [b4_remove_comma([$1],
 
 254                            b4_param_decls(b4_lex_param))],
 
 257 m4_define([b4_param_decls],
 
 258           [m4_map([b4_param_decl], [$@])])
 
 259 m4_define([b4_param_decl], [, $1])
 
 261 m4_define([b4_remove_comma], [m4_ifval(m4_quote($1), [$1, ], [])m4_shift2($@)])
 
 265 # b4_parse_param_decl
 
 266 # -------------------
 
 267 # Extra formal arguments of the constructor.
 
 268 m4_define([b4_parse_param_decl],
 
 269 [m4_ifset([b4_parse_param],
 
 270           [b4_remove_comma([$1],
 
 271                            b4_param_decls(b4_parse_param))],
 
 278 # Delegating the lexer parameters to the lexer constructor.
 
 279 m4_define([b4_lex_param_call],
 
 280           [m4_ifset([b4_lex_param],
 
 281                     [b4_remove_comma([$1],
 
 282                                      b4_param_calls(b4_lex_param))],
 
 284 m4_define([b4_param_calls],
 
 285           [m4_map([b4_param_call], [$@])])
 
 286 m4_define([b4_param_call], [, $2])
 
 290 # b4_parse_param_cons
 
 291 # -------------------
 
 292 # Extra initialisations of the constructor.
 
 293 m4_define([b4_parse_param_cons],
 
 294           [m4_ifset([b4_parse_param],
 
 295                     [b4_constructor_calls(b4_parse_param)])])
 
 297 m4_define([b4_constructor_calls],
 
 298           [m4_map([b4_constructor_call], [$@])])
 
 299 m4_define([b4_constructor_call],
 
 305 # b4_parse_param_vars
 
 306 # -------------------
 
 307 # Extra instance variables.
 
 308 m4_define([b4_parse_param_vars],
 
 309           [m4_ifset([b4_parse_param],
 
 311     /* User arguments.  */
 
 312 b4_var_decls(b4_parse_param)])])
 
 314 m4_define([b4_var_decls],
 
 315           [m4_map_sep([b4_var_decl], [
 
 317 m4_define([b4_var_decl],
 
 318           [    protected final $1;])
 
 322 # b4_maybe_throws(THROWS)
 
 323 # -----------------------
 
 324 # Expand to either an empty string or "throws THROWS".
 
 325 m4_define([b4_maybe_throws],
 
 326           [m4_ifval($1, [throws $1])])