1                                                            -*- Autoconf -*-
2
3# Java language support for Bison
4
5# Copyright (C) 2007-2015, 2018-2021 Free Software Foundation, Inc.
6
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.
11#
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.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
20m4_include(b4_skeletonsdir/[c-like.m4])
21
22
23# b4_list2(LIST1, LIST2)
24# ----------------------
25# Join two lists with a comma if necessary.
26m4_define([b4_list2],
27          [$1[]m4_ifval(m4_quote($1), [m4_ifval(m4_quote($2), [[, ]])])[]$2])
28
29
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,
33# otherwise NOT.
34m4_define([b4_percent_define_get3],
35          [m4_ifval(m4_quote(b4_percent_define_get([$1])),
36                [$2[]b4_percent_define_get([$1])[]$3], [$4])])
37
38
39
40# b4_flag_value(BOOLEAN-FLAG)
41# ---------------------------
42m4_define([b4_flag_value], [b4_flag_if([$1], [true], [false])])
43
44
45# b4_parser_class_declaration
46# ---------------------------
47# The declaration of the parser class ("class YYParser"), with all its
48# qualifiers/annotations.
49b4_percent_define_default([[api.parser.abstract]], [[false]])
50b4_percent_define_default([[api.parser.final]],    [[false]])
51b4_percent_define_default([[api.parser.public]],   [[false]])
52b4_percent_define_default([[api.parser.strictfp]], [[false]])
53
54m4_define([b4_parser_class_declaration],
55[b4_percent_define_get3([api.parser.annotations], [], [ ])dnl
56b4_percent_define_flag_if([api.parser.public],   [public ])dnl
57b4_percent_define_flag_if([api.parser.abstract], [abstract ])dnl
58b4_percent_define_flag_if([api.parser.final],    [final ])dnl
59b4_percent_define_flag_if([api.parser.strictfp], [strictfp ])dnl
60[class ]b4_parser_class[]dnl
61b4_percent_define_get3([api.parser.extends], [ extends ])dnl
62b4_percent_define_get3([api.parser.implements], [ implements ])])
63
64
65# b4_lexer_if(TRUE, FALSE)
66# ------------------------
67m4_define([b4_lexer_if],
68[b4_percent_code_ifdef([[lexer]], [$1], [$2])])
69
70
71# b4_identification
72# -----------------
73m4_define([b4_identification],
74[[  /** Version number for the Bison executable that generated this parser.  */
75  public static final String bisonVersion = "]b4_version_string[";
76
77  /** Name of the skeleton that generated this parser.  */
78  public static final String bisonSkeleton = ]b4_skeleton[;
79]])
80
81
82## ------------ ##
83## Data types.  ##
84## ------------ ##
85
86# b4_int_type(MIN, MAX)
87# ---------------------
88# Return the smallest int type able to handle numbers ranging from
89# MIN to MAX (included).
90m4_define([b4_int_type],
91[m4_if(b4_ints_in($@,   [-128],   [127]), [1], [byte],
92       b4_ints_in($@, [-32768], [32767]), [1], [short],
93                                               [int])])
94
95# b4_int_type_for(NAME)
96# ---------------------
97# Return the smallest int type able to handle numbers ranging from
98# 'NAME_min' to 'NAME_max' (included).
99m4_define([b4_int_type_for],
100[b4_int_type($1_min, $1_max)])
101
102# b4_null
103# -------
104m4_define([b4_null], [null])
105
106
107# b4_typed_parser_table_define(TYPE, NAME, DATA, COMMENT)
108# -------------------------------------------------------
109# We use intermediate functions (e.g., yypact_init) to work around the
110# 64KB limit for JVM methods.  See
111# https://lists.gnu.org/r/help-bison/2008-11/msg00004.html.
112m4_define([b4_typed_parser_table_define],
113[m4_ifval([$4], [b4_comment([$4])
114  ])dnl
115[private static final ]$1[[] yy$2_ = yy$2_init();
116  private static final ]$1[[] yy$2_init()
117  {
118    return new ]$1[[]
119    {
120  ]$3[
121    };
122  }]])
123
124
125# b4_integral_parser_table_define(NAME, DATA, COMMENT)
126#-----------------------------------------------------
127m4_define([b4_integral_parser_table_define],
128[b4_typed_parser_table_define([b4_int_type_for([$2])], [$1], [$2], [$3])])
129
130
131## ------------- ##
132## Token kinds.  ##
133## ------------- ##
134
135
136# b4_token_enum(TOKEN-NUM)
137# ------------------------
138# Output the definition of this token as an enum.
139m4_define([b4_token_enum],
140[b4_token_visible_if([$1],
141    [m4_format([[    /** Token %s, to be returned by the scanner.  */
142    static final int %s = %s%s;
143]],
144               b4_symbol([$1], [tag]),
145               b4_symbol([$1], [id]),
146               b4_symbol([$1], b4_api_token_raw_if([[number]], [[code]])))])])
147
148
149# b4_token_enums
150# --------------
151# Output the definition of the tokens (if there are) as enums.
152m4_define([b4_token_enums],
153[b4_any_token_visible_if([    /* Token kinds.  */
154b4_symbol_foreach([b4_token_enum])])])
155
156
157
158## -------------- ##
159## Symbol kinds.  ##
160## -------------- ##
161
162
163# b4_symbol_kind(NUM)
164# -------------------
165m4_define([b4_symbol_kind],
166[SymbolKind.b4_symbol_kind_base($@)])
167
168
169# b4_symbol_enum(SYMBOL-NUM)
170# --------------------------
171# Output the definition of this symbol as an enum.
172m4_define([b4_symbol_enum],
173[m4_format([    %-30s %s],
174           m4_format([[%s(%s)%s]],
175                     b4_symbol([$1], [kind_base]),
176                     [$1],
177                     m4_if([$1], b4_last_symbol, [[;]], [[,]])),
178           [b4_symbol_tag_comment([$1])])])
179
180
181# b4_declare_symbol_enum
182# ----------------------
183# The definition of the symbol internal numbers as an enum.
184m4_define([b4_declare_symbol_enum],
185[[  public enum SymbolKind
186  {
187]b4_symbol_foreach([b4_symbol_enum])[
188
189    private final int yycode_;
190
191    SymbolKind (int n) {
192      this.yycode_ = n;
193    }
194
195    private static final SymbolKind[] values_ = {
196      ]m4_map_args_sep([b4_symbol_kind(], [)], [,
197      ], b4_symbol_numbers)[
198    };
199
200    static final SymbolKind get(int code) {
201      return values_[code];
202    }
203
204    public final int getCode() {
205      return this.yycode_;
206    }
207
208]b4_parse_error_bmatch(
209[simple\|verbose],
210[[    /* Return YYSTR after stripping away unnecessary quotes and
211       backslashes, so that it's suitable for yyerror.  The heuristic is
212       that double-quoting is unnecessary unless the string contains an
213       apostrophe, a comma, or backslash (other than backslash-backslash).
214       YYSTR is taken from yytname.  */
215    private static String yytnamerr_(String yystr)
216    {
217      if (yystr.charAt (0) == '"')
218        {
219          StringBuffer yyr = new StringBuffer();
220          strip_quotes: for (int i = 1; i < yystr.length(); i++)
221            switch (yystr.charAt(i))
222              {
223              case '\'':
224              case ',':
225                break strip_quotes;
226
227              case '\\':
228                if (yystr.charAt(++i) != '\\')
229                  break strip_quotes;
230                /* Fall through.  */
231              default:
232                yyr.append(yystr.charAt(i));
233                break;
234
235              case '"':
236                return yyr.toString();
237              }
238        }
239      return yystr;
240    }
241
242    /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
243       First, the terminals, then, starting at \a YYNTOKENS_, nonterminals.  */
244    ]b4_typed_parser_table_define([String], [tname], [b4_tname])[
245
246    /* The user-facing name of this symbol.  */
247    public final String getName() {
248      return yytnamerr_(yytname_[yycode_]);
249    }
250]],
251[custom\|detailed],
252[[    /* YYNAMES_[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
253       First, the terminals, then, starting at \a YYNTOKENS_, nonterminals.  */
254    ]b4_typed_parser_table_define([String], [names], [b4_symbol_names])[
255
256    /* The user-facing name of this symbol.  */
257    public final String getName() {
258      return yynames_[yycode_];
259    }]])[
260  };
261]])])
262
263
264
265# b4_case(ID, CODE, [COMMENTS])
266# -----------------------------
267# We need to fool Java's stupid unreachable code detection.
268m4_define([b4_case],
269[  case $1:m4_ifval([$3], [ b4_comment([$3])])
270  if (yyn == $1)
271    $2;
272  break;
273])
274
275
276# b4_predicate_case(LABEL, CONDITIONS)
277# ------------------------------------
278m4_define([b4_predicate_case],
279[  case $1:
280     if (! ($2)) YYERROR;
281    break;
282])
283
284
285## -------- ##
286## Checks.  ##
287## -------- ##
288
289b4_percent_define_check_kind([[api.value.type]],    [code], [deprecated])
290
291b4_percent_define_check_kind([[annotations]],       [code], [deprecated])
292b4_percent_define_check_kind([[extends]],           [code], [deprecated])
293b4_percent_define_check_kind([[implements]],        [code], [deprecated])
294b4_percent_define_check_kind([[init_throws]],       [code], [deprecated])
295b4_percent_define_check_kind([[lex_throws]],        [code], [deprecated])
296b4_percent_define_check_kind([[api.parser.class]],  [code], [deprecated])
297b4_percent_define_check_kind([[throws]],            [code], [deprecated])
298
299
300
301## ---------------- ##
302## Default values.  ##
303## ---------------- ##
304
305m4_define([b4_yystype], [b4_percent_define_get([[api.value.type]])])
306b4_percent_define_default([[api.value.type]], [[Object]])
307b4_percent_define_default([[api.symbol.prefix]], [[S_]])
308
309# b4_api_prefix, b4_api_PREFIX
310# ----------------------------
311# Corresponds to %define api.prefix
312b4_percent_define_default([[api.prefix]], [[YY]])
313m4_define([b4_api_prefix],
314[b4_percent_define_get([[api.prefix]])])
315m4_define([b4_api_PREFIX],
316[m4_toupper(b4_api_prefix)])
317
318# b4_prefix
319# ---------
320# If the %name-prefix is not given, it is api.prefix.
321m4_define_default([b4_prefix], [b4_api_prefix])
322
323b4_percent_define_default([[api.parser.class]], [b4_prefix[]Parser])
324m4_define([b4_parser_class], [b4_percent_define_get([[api.parser.class]])])
325
326b4_percent_define_default([[lex_throws]], [[java.io.IOException]])
327m4_define([b4_lex_throws], [b4_percent_define_get([[lex_throws]])])
328
329b4_percent_define_default([[throws]], [])
330m4_define([b4_throws], [b4_percent_define_get([[throws]])])
331
332b4_percent_define_default([[init_throws]], [])
333m4_define([b4_init_throws], [b4_percent_define_get([[init_throws]])])
334
335b4_percent_define_default([[api.location.type]], [Location])
336m4_define([b4_location_type], [b4_percent_define_get([[api.location.type]])])
337
338b4_percent_define_default([[api.position.type]], [Position])
339m4_define([b4_position_type], [b4_percent_define_get([[api.position.type]])])
340
341
342## ----------------- ##
343## Semantic Values.  ##
344## ----------------- ##
345
346
347# b4_symbol_translate(STRING)
348# ---------------------------
349# Used by "bison" in the array of symbol names to mark those that
350# require translation.
351m4_define([b4_symbol_translate],
352[[i18n($1)]])
353
354
355# b4_trans(STRING)
356# ----------------
357# Translate a string if i18n is enabled.  Avoid collision with b4_translate.
358m4_define([b4_trans],
359[b4_has_translations_if([i18n($1)], [$1])])
360
361
362
363# b4_symbol_value(VAL, [SYMBOL-NUM], [TYPE-TAG])
364# ----------------------------------------------
365# See README.
366m4_define([b4_symbol_value],
367[m4_ifval([$3],
368          [(($3)($1))],
369          [m4_ifval([$2],
370                    [b4_symbol_if([$2], [has_type],
371                                  [((b4_symbol([$2], [type]))($1))],
372                                  [$1])],
373                    [$1])])])
374
375
376# b4_lhs_value([SYMBOL-NUM], [TYPE])
377# ----------------------------------
378# See README.
379m4_define([b4_lhs_value], [yyval])
380
381
382# b4_rhs_data(RULE-LENGTH, POS)
383# -----------------------------
384# See README.
385m4_define([b4_rhs_data],
386[yystack.valueAt (b4_subtract($@))])
387
388# b4_rhs_value(RULE-LENGTH, POS, SYMBOL-NUM, [TYPE])
389# --------------------------------------------------
390# See README.
391#
392# In this simple implementation, %token and %type have class names
393# between the angle brackets.
394m4_define([b4_rhs_value],
395[b4_symbol_value([b4_rhs_data([$1], [$2])], [$3], [$4])])
396
397
398# b4_lhs_location()
399# -----------------
400# Expansion of @$.
401m4_define([b4_lhs_location],
402[(yyloc)])
403
404
405# b4_rhs_location(RULE-LENGTH, POS)
406# ---------------------------------
407# Expansion of @POS, where the current rule has RULE-LENGTH symbols
408# on RHS.
409m4_define([b4_rhs_location],
410[yystack.locationAt (b4_subtract($@))])
411
412
413# b4_lex_param
414# b4_parse_param
415# --------------
416# If defined, b4_lex_param arrives double quoted, but below we prefer
417# it to be single quoted.  Same for b4_parse_param.
418
419# TODO: should be in bison.m4
420m4_define_default([b4_lex_param], [[]])
421m4_define([b4_lex_param], b4_lex_param)
422m4_define([b4_parse_param], b4_parse_param)
423
424# b4_lex_param_decl
425# -----------------
426# Extra formal arguments of the constructor.
427m4_define([b4_lex_param_decl],
428[m4_ifset([b4_lex_param],
429          [b4_remove_comma([$1],
430                           b4_param_decls(b4_lex_param))],
431          [$1])])
432
433m4_define([b4_param_decls],
434          [m4_map([b4_param_decl], [$@])])
435m4_define([b4_param_decl], [, $1])
436
437m4_define([b4_remove_comma], [m4_ifval(m4_quote($1), [$1, ], [])m4_shift2($@)])
438
439
440
441# b4_parse_param_decl
442# -------------------
443# Extra formal arguments of the constructor.
444m4_define([b4_parse_param_decl],
445[m4_ifset([b4_parse_param],
446          [b4_remove_comma([$1],
447                           b4_param_decls(b4_parse_param))],
448          [$1])])
449
450
451
452# b4_lex_param_call
453# -----------------
454# Delegating the lexer parameters to the lexer constructor.
455m4_define([b4_lex_param_call],
456          [m4_ifset([b4_lex_param],
457                    [b4_remove_comma([$1],
458                                     b4_param_calls(b4_lex_param))],
459                    [$1])])
460m4_define([b4_param_calls],
461          [m4_map([b4_param_call], [$@])])
462m4_define([b4_param_call], [, $2])
463
464
465
466# b4_parse_param_cons
467# -------------------
468# Extra initialisations of the constructor.
469m4_define([b4_parse_param_cons],
470          [m4_ifset([b4_parse_param],
471                    [b4_constructor_calls(b4_parse_param)])])
472
473m4_define([b4_constructor_calls],
474          [m4_map([b4_constructor_call], [$@])])
475m4_define([b4_constructor_call],
476          [this.$2 = $2;
477          ])
478
479
480
481# b4_parse_param_vars
482# -------------------
483# Extra instance variables.
484m4_define([b4_parse_param_vars],
485          [m4_ifset([b4_parse_param],
486                    [
487    /* User arguments.  */
488b4_var_decls(b4_parse_param)])])
489
490m4_define([b4_var_decls],
491          [m4_map_sep([b4_var_decl], [
492], [$@])])
493m4_define([b4_var_decl],
494          [    protected final $1;])
495
496
497
498# b4_maybe_throws(THROWS)
499# -----------------------
500# Expand to either an empty string or "throws THROWS".
501m4_define([b4_maybe_throws],
502          [m4_ifval($1, [ throws $1])])
503