gnulib: update
[bison.git] / tests / local.at
blob01cf5845ae019add4097bda46361a3dca7e18d30
1 # Process this -*- Autotest -*- file with autom4te.
3 # Macros for the GNU Bison Test suite.
5 # Copyright (C) 2003-2015, 2018-2021 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 <https://www.gnu.org/licenses/>.
20 m4_version_prereq([2.58])
23 # m4_null_if(VAL, IF-TRUE, IF-FALSE)
24 # ----------------------------------
25 # If VAL evaluates to empty or 0, run IF-TRUE, otherwise IF-FALSE.
26 m4_define([m4_null_if],
27 [m4_case(m4_quote($1),
28          [0], [$2],
29          [],  [$2],
30          [$3])])
33 # m4_rpatsubst(STRING, PATTERN, REPLACEMENT)
34 # ------------------------------------------
35 # As long as PATTERN matches STRING, replace occurrences of PATTERN by
36 # REPLACEMENT.
38 # Contrary to m4_bpatsubst, matches PATTERN from previous replacements.
39 m4_define([m4_rpatsubst],
40 [m4_if(m4_bregexp([$1], [$2]),
41        [-1], [$1],
42        [m4_rpatsubst(m4_quote(m4_bpatsubst([$1], [$2], [$3])), [$2], [$3])])])
45 # AT_SETUP_STRIP(TITLE)
46 # ---------------------
47 # Abbreviate the TITLE to be passed to AT_SETUP.  Remove new-lines
48 # that completely break AT_SETUP.
49 m4_define([AT_SETUP_STRIP],
50 [m4_bpatsubsts([$1],
51       [%\(language\|skeleton\) "?\([^\" ]*\)"?],
52         [\2],
53       [%define "?\([-A-Za-z0-9_.]+\)"? \({[^\}]+}\|"[^\"]+"\|[-A-Za-z0-9_.]+\)],
54         [\1=\2],
55       [%define "?\([-A-Za-z0-9_.]+\)"?],
56         [\1],
57       [ *
58 + *],    [ ])dnl
62 # AT_DATA_NO_FINAL_EOL(FILE, CONTENT)
63 # -----------------------------------
64 # Same as AT_DATA, except that CONTENT has no the final end of line.
66 # FIXME: AT_DATA or some variant of AT_DATA may eventually permit
67 # the final newline to be omitted.  See the threads starting at
68 # <https://lists.gnu.org/r/bison-patches/2009-07/msg00019.html>.
69 m4_define([AT_DATA_NO_FINAL_EOL],
70 [AT_DATA([$1], [$2
72 AT_REQUIRE([
73 set x `LC_ALL=C ls -l '$1'` &&
74   size=$][6 &&
75   { test $size -eq 0 || dd obs=1 seek=`expr $size - 1` if=/dev/null of='$1'; }],
76   [], [ignore], [ignore])
81 ## ------------- ##
82 ## Basic tests.  ##
83 ## ------------- ##
86 # AT_DIFF_U_CHECK(DIFF-ARGS, EXPECTED-DIFF)
87 # -----------------------------------------
88 # If diff -u works as we expect, use it, with headers stripped.
89 m4_define([AT_DIFF_U_CHECK],
90 [if $DIFF_U_WORKS; then
91   AT_CHECK([diff -u $1 | sed -n '/^@@/,$p' | sed 's/^ $//'], [0], [$2])
96 # AT_PERL_CHECK(PERL-ARGS, ...)
97 # -----------------------------
98 # If Perl is available, run this test.
99 m4_define([AT_PERL_CHECK],
100 [if test x"$PERL" != x; then
101   AT_CHECK(["$PERL" $1], [$2], [$3], [$4])
106 # AT_REQUIRE(CMD, ...)
107 # --------------------
108 # Same as AT_CHECK(...) but skip this test if we failed.
109 m4_define([AT_REQUIRE],
110 [AT_CHECK([$1 || exit 77], [$2], [$3], [$4])])
113 # AT_PERL_REQUIRE(PERL-ARGS, ...)
114 # -------------------------------
115 # Run this Perl program, or skip the test if Perl is not available.
116 m4_define([AT_PERL_REQUIRE],
117 [AT_REQUIRE(["$PERL" $1], [$2], [$3], [$4])])
120 # AT_MATCHES_CHECK(FILE, PERL-REGEXP, COUNT)
121 # ------------------------------------------
122 # Expect COUNT matches of the PERL-REGEXP in FILE.  The file is
123 # taken in "slurp" mode, i.e., one can match end-of-lines.
124 m4_define([AT_MATCHES_CHECK],
125 [AT_PERL_CHECK([-0777 -ne '
126     my $count = 0;
127     s{$2}{ ++$count; "" }gem;
128     printf "$count\n";' $1], [0], [$3
129 ])])
132 # AT_SAVE_SPECIAL_FILES / AT_RESTORE_SPECIAL_FILES
133 # ------------------------------------------------
134 # Don't interfere with caller's files.
135 m4_divert_text([PREPARE_TESTS],
136 [at_save_special_files ()
138   for at_save_file in stderr experr expout
139   do
140     test ! -f at-bison-check-$at_save_file.bak ||
141       as_fn_error 1 "fatal error: back-up on top of a back-up"
142     test ! -f $at_save_file || mv $at_save_file at-bison-check-$at_save_file.bak
143   done
146 at_restore_special_files ()
148   for at_save_file in stderr experr expout
149   do
150     test ! -f at-bison-check-$at_save_file.bak ||
151       mv at-bison-check-$at_save_file.bak $at_save_file
152   done
156 m4_define([AT_SAVE_SPECIAL_FILES],    [at_save_special_files])
157 m4_define([AT_RESTORE_SPECIAL_FILES], [at_restore_special_files])
160 # AT_FOR_EACH_SKEL(BODY)
161 # ----------------------
162 m4_define([AT_FOR_EACH_SKEL],
163 [m4_foreach([b4_skel],
164         [[yacc.c], [glr.c], [lalr1.cc], [glr.cc], [glr2.cc], [lalr1.d], [lalr1.java]],
165         [$1])
170 ## ------------------------------- ##
171 ## Macros decoding Bison options.  ##
172 ## ------------------------------- ##
174 # AT_LOC_PUSHDEF(FIRST-LINE, FIRST-COLUMN, LAST-LINE, LAST-COLUMN)
175 # ----------------------------------------------------------------
176 # Pushdef AT(_LOC)?_(FIRST|LAST)_(LINE|COLUMN).
177 m4_define([AT_LOC_PUSHDEF],
178 [m4_pushdef([AT_FIRST_LINE],  [$1])
179 m4_pushdef([AT_FIRST_COLUMN], [$2])
180 m4_pushdef([AT_LAST_LINE],    [$3])
181 m4_pushdef([AT_LAST_COLUMN],  [$4])
182 m4_pushdef([AT_LOC_FIRST_LINE],   [AT_LOC.AT_FIRST_LINE])
183 m4_pushdef([AT_LOC_FIRST_COLUMN], [AT_LOC.AT_FIRST_COLUMN])
184 m4_pushdef([AT_LOC_LAST_LINE],    [AT_LOC.AT_LAST_LINE])
185 m4_pushdef([AT_LOC_LAST_COLUMN],  [AT_LOC.AT_LAST_COLUMN])])
187 # AT_LOC_POPDEF
188 # -------------
189 # Popdef AT(_LOC)?_(FIRST|LAST)_(LINE|COLUMN).
190 m4_define([AT_LOC_POPDEF],
191 [m4_popdef([AT_LOC_FIRST_LINE])
192 m4_popdef([AT_LOC_FIRST_COLUMN])
193 m4_popdef([AT_LOC_LAST_LINE])
194 m4_popdef([AT_LOC_LAST_COLUMN])
195 m4_popdef([AT_FIRST_LINE])
196 m4_popdef([AT_FIRST_COLUMN])
197 m4_popdef([AT_LAST_LINE])
198 m4_popdef([AT_LAST_COLUMN])
203 # AT_BISON_OPTION_PUSHDEFS([BISON-OPTIONS])
204 # -----------------------------------------
205 m4_define([AT_BISON_OPTION_PUSHDEFS],
206 [m4_divert_text([KILL],
207                 [_AT_BISON_OPTION_PUSHDEFS($[1], $[2], [$1])])])
210 # _AT_BISON_OPTION_PUSHDEFS($1, $2, [BISON-OPTIONS])
211 # --------------------------------------------------
212 # This macro works around the impossibility to define macros
213 # inside macros, because issuing '[$1]' is not possible in M4 :(.
214 # This sucks hard, GNU M4 should really provide M5-like $$1.
215 m4_define([_AT_BISON_OPTION_PUSHDEFS],
216 [m4_pushdef([AT_BISON_OPTIONS], [$3])
218 m4_if([$1$2], $[1]$[2], [],
219        [m4_fatal([$0: invalid arguments: $@])])dnl
220 m4_pushdef([AT_AUTOMOVE_IF],
221 [m4_bmatch([$3], [%define api\.value\.automove], [$1], [$2])])
222 m4_pushdef([AT_HEADER_IF],
223 [m4_bmatch([$3], [%defines\|%header], [$1], [$2])])
224 m4_pushdef([AT_DEBUG_IF],
225            [m4_bmatch([$3], [%debug\|%define parse.trace], [$1], [$2])])
226 m4_pushdef([AT_ERROR_CUSTOM_IF],
227            [m4_bmatch([$3], [%define parse\.error custom], [$1], [$2])])
228 m4_pushdef([AT_ERROR_DETAILED_IF],
229            [m4_bmatch([$3], [%define parse\.error detailed], [$1], [$2])])
230 m4_pushdef([AT_ERROR_VERBOSE_IF],
231            [m4_bmatch([$3], [%define parse\.error verbose], [$1], [$2])])
232 m4_pushdef([AT_ERROR_SIMPLE_IF],
233            [AT_ERROR_CUSTOM_IF([$2], [AT_ERROR_DETAILED_IF([$2], [AT_ERROR_VERBOSE_IF([$2], [$1])], [$1])], [$1])])
235 m4_pushdef([AT_CXX_IF],
236            [m4_bmatch([$3], [%language "[Cc]\+\+"\|%skeleton "[a-z0-9]+\.cc"], [$1], [$2])])
237 m4_pushdef([AT_D_IF],
238            [m4_bmatch([$3], [%language "[Dd]"\|%skeleton "[a-z0-9]+\.d"], [$1], [$2])])
239 m4_pushdef([AT_JAVA_IF],
240            [m4_bmatch([$3], [%language "[Jj][Aa][Vv][Aa]"\|%skeleton "[a-z0-9]+\.java"], [$1], [$2])])
241 # The target language: "c", "c++", or "java".
242 m4_pushdef([AT_LANG],
243 [AT_JAVA_IF([java],
244             [AT_CXX_IF([c++],
245                        [AT_D_IF([d],
246                                 [c])])])])
247 m4_pushdef([AT_C_IF],
248 [m4_if(AT_LANG, [c], [$1], [$2])])
250 m4_pushdef([AT_GLR_IF],
251            [m4_bmatch([$3], [%glr-parser\|%skeleton "glr.*"], [$1], [$2])])
252 m4_pushdef([AT_LALR1_CC_IF],
253            [AT_CXX_IF([AT_GLR_IF([$2], [$1])], [$2])])
254 m4_pushdef([AT_GLR_CC_IF],
255            [AT_CXX_IF([AT_GLR_IF([$1], [$2])], [$2])])
256 m4_pushdef([AT_GLR2_CC_IF],
257            [m4_bmatch([$3], [%skeleton "glr2\.cc"], [$1], [$2])])
258 # Using yacc.c?
259 m4_pushdef([AT_YACC_C_IF],
260 [m4_bmatch([$3], [%language\|%glr-parser\|%skeleton], [$2], [$1])])
263 m4_pushdef([AT_LAC_IF],
264 [m4_bmatch([$3], [%define parse.lac full], [$1], [$2])])
265 m4_pushdef([AT_LEXPARAM_IF],
266 [m4_bmatch([$3], [%lex-param], [$1], [$2])])
267 m4_pushdef([AT_LOCATION_IF],
268 [m4_bmatch([$3], [%locations], [$1], [$2])])
269 # Whether we use the Span location type (see calc.at), whose members are
270 # not named like those of the default location type (on purpose, to make
271 # sure we can use a user type).
272 m4_pushdef([AT_LOCATION_TYPE_SPAN_IF],
273 [m4_bmatch([$3], [%define \(api\.location\.type\|location_type\) \{Span\}], [$1], [$2])])
274 m4_pushdef([AT_MULTISTART_IF],
275 [m4_bmatch([$3], [%start [_a-zA-Z]+ [_a-zA-Z]+], [$1], [$2])])
276 m4_pushdef([AT_PARAM_IF],
277 [m4_bmatch([$3], [%parse-param], [$1], [$2])])
279 # Comma-terminated list of formals parse-parameters.
280 # E.g., %parse-param { int x } %parse-param {int y} -> "int x, int y, ".
281 m4_bpatsubst([$3], [%parse-param { *\([^{}]*[^{} ]\) *}{ *\([^{}]*[^{} ]\) *}{ *\([^{}]*[^{} ]\) *}],
282              [m4_append([AT_PARSE_PARAMS], [\1, \2, \3], [, ])])
283 m4_bpatsubst([$3], [%parse-param { *\([^{}]*[^{} ]\) *}{ *\([^{}]*[^{} ]\) *}\([^{].*\)?$],
284              [m4_append([AT_PARSE_PARAMS], [\1, \2], [, ])])
285 m4_rpatsubst([$3], [%parse-param { *\([^{}]*[^{} ]\) *}\([^{}].*\)?$],
286              [m4_append([AT_PARSE_PARAMS], [\1], [, ])\2])
287 m4_ifndef([AT_PARSE_PARAMS],
288           [m4_define([AT_PARSE_PARAMS])])
290 m4_pushdef([AT_PARSER_CLASS],
291 [m4_bmatch([$3], [%define  *api\.parser\.class {\([^\}]*\)}],
292            [m4_bregexp([$3], [%define  *api\.parser\.class {\([^\}]*\)}], [\1])],
293            [AT_JAVA_IF([AT_API_PREFIX[]Parser], [parser])])])
295 m4_pushdef([AT_PURE_IF],
296 [m4_bmatch([$3], [%define  *api\.pure\|%pure-parser],
297            [m4_bmatch([$3], [%define  *api\.pure *false], [$2], [$1])],
298            [$2])])
299 m4_pushdef([AT_PUSH_IF],
300 [m4_bmatch([$3], [%define api.push-pull \(both\|pull\)], [$1], [$2])])
301 # Whether we use %define api.value.type union
302 m4_pushdef([AT_VALUE_UNION_IF],
303 [m4_bmatch([$3], [%define api\.value\.type union], [$1], [$2])])
306 # AT_NAME_PREFIX.
307 m4_pushdef([AT_NAME_PREFIX],
308 [m4_bmatch([$3], [\(%define api\.prefix\|%name-prefix\) .*],
309    [m4_bregexp([$3],
310      [\(%define api\.prefix\|%name-prefix\) [\{\"]\([^\"\}]*\)[\"\}]],
311      [\2])],
312    [AT_JAVA_IF([YY], [yy])])])
313 # AT_NAMESPACE: also consider api.prefix.  FIXME: Stop that confusion.
314 m4_pushdef([AT_NAMESPACE],
315 [m4_bmatch([$3], [\(%define api\.\(namespace\|prefix\)\|%name-prefix\) .*],
316    [m4_bregexp([$3],
317      [\(%define api\.\(namespace\|prefix\)\|%name-prefix\) [\{\"]\([^\"\}]*\)[\"\}]],
318      [\3])],
319    [yy])])
320 m4_pushdef([AT_TOKEN_CTOR_IF],
321 [m4_bmatch([$3], [%define api\.token\.constructor], [$1], [$2])])
322 m4_pushdef([AT_TOKEN_PREFIX],
323 [m4_bmatch([$3], [%define api\.token\.prefix {.*}],
324            [m4_bregexp([$3], [%define api\.token\.prefix {\(.*\)}], [\1])])])
325 m4_pushdef([AT_TOKEN_RAW_IF],
326 [m4_bmatch([$3], [%define api\.token\.raw], [$1], [$2])])
327 m4_pushdef([AT_UNION_IF],
328 [m4_bmatch([$3], [%define api\.value\.type union], [$1], [$2])])
329 m4_pushdef([AT_VARIANT_IF],
330 [m4_bmatch([$3], [%define api\.value\.type variant], [$1], [$2])])
331 m4_pushdef([AT_UNION_IF],
332 [m4_bmatch([$3], [%define api\.value\.type union], [$1], [$2])])
333 m4_pushdef([AT_API_prefix],
334 [m4_bmatch([$3], [%define api\.prefix {.*}],
335            [m4_bregexp([$3], [%define api\.prefix {\([^\}]*\)}], [\1])],
336            [AT_JAVA_IF([YY], [yy])])])
337 m4_pushdef([AT_API_PREFIX],
338 [m4_toupper(AT_API_prefix)])
339 # yyerror receives the location if %location, and if the parser is pure. For
340 # historical reasons, with the "yacc.c" skeleton, the location is not passed
341 # unless an additional "%parse-param" is present, or if the purity is defined
342 # as "full".
343 m4_pushdef([AT_YYERROR_ARG_LOC_IF],
344 [AT_LOCATION_IF([AT_PURE_IF([m4_bmatch([$3],
345                                        m4_quote(m4_join([\|],
346                                                         [%define api\.pure full],
347                                                         [%glr-parser],
348                                                         [%parse-param],
349                                                         [%skeleton "?glr.c"?])),
350                                        [$1], [$2])],
351                             [$2])],
352                     [$2])])
354 # yyerror always sees the locations (when activated) if the parser is impure.
355 # When the parser is pure, yyerror sees the location if it is received as an
356 # argument.
357 m4_pushdef([AT_YYERROR_SEES_LOC_IF],
358 [AT_LOCATION_IF([AT_YACC_C_IF([AT_PURE_IF([AT_YYERROR_ARG_LOC_IF([$1], [$2])],
359                                         [$1])],
360                             [$1])],
361                 [$2])])
363 # The interface is pure: either because %define api.pure, or because we
364 # are using the C++ parsers.
365 m4_pushdef([AT_PURE_LEX_IF],
366 [AT_PURE_IF([$1],
367             [AT_CXX_IF([$1], [$2])])])
369 # Whether token translation is supported.
370 m4_pushdef([AT_TOKEN_TRANSLATE_IF],
371 [AT_ERROR_CUSTOM_IF([$1], [AT_ERROR_DETAILED_IF([$1], [$2])])])
373 m4_pushdef([AT_TOKEN],
374            [AT_CXX_IF([AT_CXX_IF([AT_NAMESPACE::AT_PARSER_CLASS::token::])[]AT_TOKEN_PREFIX[$1]],
375                       [AT_TOKEN_PREFIX[$1]])])
378 m4_pushdef([AT_YYSTYPE],
379 [AT_CXX_IF([AT_NAMESPACE[::]AT_PARSER_CLASS[::semantic_type]],
380                [AT_API_PREFIX[STYPE]])])
381 m4_pushdef([AT_YYLTYPE],
382 [AT_CXX_IF([AT_NAMESPACE[::]AT_PARSER_CLASS[::location_type]],
383                [AT_API_PREFIX[LTYPE]])])
385 AT_TOKEN_CTOR_IF(
386 [m4_pushdef([AT_LOC], [[(]AT_NAME_PREFIX[lloc)]])
387  m4_pushdef([AT_VAL], [[(]AT_NAME_PREFIX[lval)]])
388  m4_pushdef([AT_YYLEX_FORMALS],     [])
389  m4_pushdef([AT_YYLEX_RETURN],      [AT_NAMESPACE::AT_PARSER_CLASS::symbol_type])
390  m4_pushdef([AT_YYLEX_ARGS],        [])
391  m4_pushdef([AT_USE_LEX_ARGS],      [])
392  m4_pushdef([AT_YYLEX_PRE_FORMALS], [])
393  m4_pushdef([AT_YYLEX_PRE_ARGS],    [])],
394 [AT_PURE_LEX_IF(
395 [m4_pushdef([AT_LOC], [(*llocp)])
396  m4_pushdef([AT_VAL], [(*lvalp)])
397  m4_pushdef([AT_YYLEX_FORMALS],
398             [AT_YYSTYPE *lvalp[]AT_LOCATION_IF([, AT_YYLTYPE *llocp])])
399  m4_pushdef([AT_YYLEX_RETURN], [int])
400  m4_pushdef([AT_YYLEX_ARGS],
401             [lvalp[]AT_LOCATION_IF([, llocp])])
402  m4_pushdef([AT_USE_LEX_ARGS],
403             [(void) lvalp;AT_LOCATION_IF([(void) llocp;])])
404  m4_pushdef([AT_YYLEX_PRE_FORMALS],
405             [AT_YYLEX_FORMALS, ])
406  m4_pushdef([AT_YYLEX_PRE_ARGS],
407             [AT_YYLEX_ARGS, ])
409 [m4_pushdef([AT_LOC], [[(]AT_NAME_PREFIX[lloc)]])
410  m4_pushdef([AT_VAL], [[(]AT_NAME_PREFIX[lval)]])
411  m4_pushdef([AT_YYLEX_FORMALS],     [void])
412  m4_pushdef([AT_YYLEX_RETURN],      [int])
413  m4_pushdef([AT_YYLEX_ARGS],        [])
414  m4_pushdef([AT_USE_LEX_ARGS],      [])
415  m4_pushdef([AT_YYLEX_PRE_FORMALS], [])
416  m4_pushdef([AT_YYLEX_PRE_ARGS],    [])
417 ])])
420 # Handle the different types of location components.
421 AT_LOCATION_TYPE_SPAN_IF(
422     [AT_LOC_PUSHDEF([first.l], [first.c], [last.l], [last.c])],
423     [AT_CXX_IF([AT_LOC_PUSHDEF([begin.line], [begin.column], [end.line], [end.column])],
424                [AT_LOC_PUSHDEF([first_line], [first_column], [last_line], [last_column])])])
427 AT_GLR_IF([AT_KEYWORDS([glr])])
428 AT_MULTISTART_IF([AT_KEYWORDS([multistart])])
429 AT_PUSH_IF([AT_KEYWORDS([push])])
430 ])# _AT_BISON_OPTION_PUSHDEFS
433 # AT_BISON_OPTION_POPDEFS
434 # -----------------------
435 m4_define([AT_BISON_OPTION_POPDEFS],
436 [m4_divert_text([KILL],
437 [m4_popdef([AT_BISON_OPTIONS])
439 m4_popdef([AT_YYLEX_PRE_ARGS])
440 m4_popdef([AT_YYLEX_PRE_FORMALS])
441 m4_popdef([AT_USE_LEX_ARGS])
442 m4_popdef([AT_YYLEX_ARGS])
443 m4_popdef([AT_YYLEX_FORMALS])
444 m4_popdef([AT_YYLTYPE])
445 m4_popdef([AT_YYSTYPE])
446 m4_popdef([AT_VAL])
447 m4_popdef([AT_LOC])
448 m4_popdef([AT_TOKEN])
449 m4_popdef([AT_TOKEN_TRANSLATE_IF])
450 m4_popdef([AT_PURE_LEX_IF])
451 m4_popdef([AT_YYERROR_SEES_LOC_IF])
452 m4_popdef([AT_YYERROR_ARG_LOC_IF])
453 m4_popdef([AT_API_PREFIX])
454 m4_popdef([AT_API_prefix])
455 m4_popdef([AT_VARIANT_IF])
456 m4_popdef([AT_UNION_IF])
457 m4_popdef([AT_TOKEN_RAW_IF])
458 m4_popdef([AT_TOKEN_PREFIX])
459 m4_popdef([AT_TOKEN_CTOR_IF])
460 m4_popdef([AT_NAMESPACE])
461 m4_popdef([AT_NAME_PREFIX])
462 m4_popdef([AT_LOCATION_TYPE_SPAN_IF])
463 m4_popdef([AT_LOCATION_IF])
464 m4_undefine([AT_PARSE_PARAMS])
465 m4_popdef([AT_VALUE_UNION_IF])
466 m4_popdef([AT_PUSH_IF])
467 m4_popdef([AT_PURE_IF])
468 m4_popdef([AT_PARSER_CLASS])
469 m4_popdef([AT_PARAM_IF])
470 m4_popdef([AT_MULTISTART_IF])
471 m4_popdef([AT_LEXPARAM_IF])
472 m4_popdef([AT_LAC_IF])
473 m4_popdef([AT_YACC_C_IF])
474 m4_popdef([AT_GLR_IF])
475 m4_popdef([AT_CXX_IF])
476 m4_popdef([AT_C_IF])
477 m4_popdef([AT_LANG])
478 m4_popdef([AT_JAVA_IF])
479 m4_popdef([AT_GLR2_CC_IF])
480 m4_popdef([AT_GLR_CC_IF])
481 m4_popdef([AT_LALR1_CC_IF])
482 m4_popdef([AT_ERROR_SIMPLE_IF])
483 m4_popdef([AT_ERROR_VERBOSE_IF])
484 m4_popdef([AT_ERROR_DETAILED_IF])
485 m4_popdef([AT_ERROR_CUSTOM_IF])
486 m4_popdef([AT_DEBUG_IF])
487 m4_popdef([AT_HEADER_IF])
488 m4_popdef([AT_AUTOMOVE_IF])
489 AT_LOC_POPDEF])dnl
490 ])# AT_BISON_OPTION_POPDEFS
494 ## -------------------------- ##
495 ## Generating Grammar Files.  ##
496 ## -------------------------- ##
498 # AT_LANG_CASE(LANG1, IF-LANG1, LANG2, IF-LANG2, ..., DEFAULT)
499 # ------------------------------------------------------------
500 m4_define([AT_LANG_CASE],
501 [m4_case(AT_LANG, $@)])
504 # AT_LANG_MATCH(LANG1, IF-LANG1, LANG2, IF-LANG2, ..., DEFAULT)
505 # ------------------------------------------------------------
506 m4_define([AT_LANG_MATCH],
507 [m4_bmatch(AT_LANG, $@)])
510 # _AT_LANG_DISPATCH(LANG, MACRO, ARGS)
511 # ------------------------------------
512 # Call the specialization of MACRO for LANG with ARGS.  Complain if
513 # unavailable.
514 m4_define([_AT_LANG_DISPATCH],
515 [m4_ifdef([$2($1)],
516        [m4_indir([$2($1)], m4_shift2($@))],
517        [m4_fatal([$2: unknown language: $1])])])
520 # AT_LANG_DISPATCH(MACRO, ARGS)
521 # -----------------------------
522 # Call the specialization of MACRO for AT_LANG with ARGS.  Complain if
523 # unavailable.
524 m4_define([AT_LANG_DISPATCH],
525 [_AT_LANG_DISPATCH(AT_LANG, $@)])
529 # AT_DATA_SOURCE_PROLOGUE
530 # -----------------------
531 # The prologue that should be included in any source code that is
532 # meant to be compiled.  Keep atlocal.in sync (BISON_CXX_WORKS).
533 m4_define([AT_DATA_SOURCE_PROLOGUE],
534 [[/* Adjust to the compiler.
535   We used to do it here, but each time we add a new line,
536   we have to adjust all the line numbers in error messages.
537   It's simpler to use a constant include to a varying file.  */
538 #include <testsuite.h>
541 # AT_DATA_GRAMMAR_PROLOGUE
542 # ------------------------
543 # The prologue that should be included in any grammar whose parser is
544 # meant to be compiled.
545 m4_define([AT_DATA_GRAMMAR_PROLOGUE],
546 [[%code top { /* -*- ]AT_LANG[ -*- */
547 ]AT_DATA_SOURCE_PROLOGUE[]dnl
551 # AT_DATA_SOURCE(NAME, CONTENT)
552 # -----------------------------
553 # Generate the file NAME, whose CONTENT is preceded by
554 # AT_DATA_SOURCE_PROLOGUE.
555 m4_define([AT_DATA_SOURCE],
556 [AT_DATA([$1],
557 [AT_DATA_SOURCE_PROLOGUE
558 $2])
562 # AT_DATA_GRAMMAR(NAME, CONTENT)
563 # ------------------------------
564 # Generate the file NAME, whose CONTENT is preceded by
565 # AT_DATA_GRAMMAR_PROLOGUE.
566 m4_define([AT_DATA_GRAMMAR], [AT_LANG_DISPATCH([$0], $@)])
569 # AT_YYLEX_PROTOTYPE
570 # AT_YYLEX_DECLARE_EXTERN
571 # AT_YYLEX_DECLARE
572 # AT_YYLEX_DEFINE([INPUT], [ACTION])
573 # ----------------------------------
574 # INPUT can be empty, or in double quotes, or a list (in braces).
575 # ACTION may compute yylval for instance, using "res" as token kind,
576 # and "toknum" as the number of calls to yylex (starting at 0).
577 m4_define([AT_YYLEX_PROTOTYPE],
578 [AT_YYLEX_RETURN AT_NAME_PREFIX[]lex (]AT_YYLEX_FORMALS[)[]dnl
581 m4_define([AT_YYLEX_DECLARE_EXTERN],
582 [AT_YYLEX_PROTOTYPE;dnl
585 m4_define([AT_YYLEX_DECLARE],
586 [static AT_YYLEX_DECLARE_EXTERN[]dnl
590 # AT_YYLEX_DEFINE([INPUT], [ACTION])
591 m4_define([AT_YYLEX_DEFINE],  [AT_LANG_DISPATCH([$0], $@)])
594 # AT_YYERROR_FORMALS
595 # AT_YYERROR_PROTOTYPE
596 # AT_YYERROR_DECLARE_EXTERN
597 # AT_YYERROR_DECLARE
598 # AT_YYERROR_DEFINE
599 # -------------------------
600 # Must be called inside a AT_BISON_OPTION_PUSHDEFS/POPDEFS pair.
601 m4_define([AT_YYERROR_FORMALS],        [AT_LANG_DISPATCH([$0], $@)])
602 m4_define([AT_YYERROR_PROTOTYPE],      [AT_LANG_DISPATCH([$0], $@)])
603 m4_define([AT_YYERROR_DECLARE_EXTERN], [AT_LANG_DISPATCH([$0], $@)])
604 m4_define([AT_YYERROR_DECLARE],        [AT_LANG_DISPATCH([$0], $@)])
605 m4_define([AT_YYERROR_DEFINE],         [AT_LANG_DISPATCH([$0], $@)])
607 # AT_MAIN_DEFINE
608 # --------------
609 m4_define([AT_MAIN_DEFINE],   [AT_LANG_DISPATCH([$0], $@)])
613 ## --- ##
614 ## C.  ##
615 ## --- ##
617 # AT_DATA_GRAMMAR(c)(NAME, CONTENT)
618 # ---------------------------------
619 # Generate the file NAME, with CONTENT.
620 m4_define([AT_DATA_GRAMMAR(c)],
621 [AT_DATA([$1],
622 [AT_DATA_GRAMMAR_PROLOGUE
623 $2])
626 # AT_LOCATION_PRINT_DECLARE
627 # -------------------------
628 m4_define([AT_LOCATION_PRINT_DECLARE],
629 [AT_LOCATION_IF([[
630 #if defined ]AT_YYLTYPE[_IS_TRIVIAL && ]AT_YYLTYPE[_IS_TRIVIAL
631 static int location_print (FILE *yyo, ]AT_YYLTYPE[ const * const yylocp);
632 # ifndef LOCATION_PRINT
633 #  define LOCATION_PRINT(File, Loc) location_print (File, &(Loc))
634 # endif
635 #endif
636 ]])dnl
639 # AT_LOCATION_PRINT_DEFINE
640 # ------------------------
641 m4_define([AT_LOCATION_PRINT_DEFINE],
642 [AT_LOCATION_IF([[
643 # if defined ]AT_YYLTYPE[_IS_TRIVIAL && ]AT_YYLTYPE[_IS_TRIVIAL
644 /* Print *YYLOCP on YYO. */
645 __attribute__((__unused__))
646 static int
647 location_print (FILE *yyo, ]AT_YYLTYPE[ const * const yylocp)
649   int res = 0;
650   int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
651   if (0 <= yylocp->first_line)
652     {
653       res += fprintf (yyo, "%d", yylocp->first_line);
654       if (0 <= yylocp->first_column)
655         res += fprintf (yyo, ".%d", yylocp->first_column);
656     }
657   if (0 <= yylocp->last_line)
658     {
659       if (yylocp->first_line < yylocp->last_line)
660         {
661           res += fprintf (yyo, "-%d", yylocp->last_line);
662           if (0 <= end_col)
663             res += fprintf (yyo, ".%d", end_col);
664         }
665       else if (0 <= end_col && yylocp->first_column < end_col)
666         res += fprintf (yyo, "-%d", end_col);
667     }
668   return res;
670 #endif
671 ]])])
674 m4_define([AT_YYERROR_FORMALS(c)],
675 [AT_YYERROR_ARG_LOC_IF([AT_YYLTYPE const * const llocp, ])AT_PARAM_IF([AT_PARSE_PARAMS, ])[const char *msg]])
677 m4_define([AT_YYERROR_PROTOTYPE(c)],
678 [[void ]AT_NAME_PREFIX[error (]AT_YYERROR_FORMALS[)]])
680 m4_define([AT_YYERROR_DECLARE_EXTERN(c)],
681 [AT_YYERROR_PROTOTYPE;])
683 m4_define([AT_YYERROR_DECLARE(c)],
684 [[#include <stdio.h>
685 ]AT_LOCATION_PRINT_DECLARE[
686 static ]AT_YYERROR_DECLARE_EXTERN])
689 # "%define parse.error custom" uses a different format, easy to check.
690 # The "verbose" one can be computed from it (see _AT_CHECK_CALC_ERROR).
691 m4_define([AT_YYERROR_DEFINE(c)],
692 [AT_LOCATION_PRINT_DEFINE[
694 ]AT_ERROR_CUSTOM_IF([[
696 yyreport_syntax_error (const yypcontext_t *ctx]AT_PARAM_IF([, AT_PARSE_PARAMS])[)
698   int res = 0;]AT_PARAM_IF([m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
699               [[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\),* *], [
700   YY_USE (\1);])])[]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
701   ++global_nerrs;
702   ++*nerrs;]])[]AT_LOCATION_IF([[
703   LOCATION_PRINT (stderr, *yypcontext_location (ctx));
704   fprintf (stderr, ": ");]])[
705   fprintf (stderr, "syntax error");
706   {
707     yysymbol_kind_t la = yypcontext_token (ctx);
708     if (la != YYSYMBOL_YYEMPTY)
709       fprintf (stderr, " on token [%s]", yysymbol_name (la));
710   }
711   {
712     enum { TOKENMAX = 10 };
713     yysymbol_kind_t expected[TOKENMAX];
714     int n = yypcontext_expected_tokens (ctx, expected, TOKENMAX);
715     /* Forward errors to yyparse.  */
716     if (n <= 0)
717       res = n;
718     else
719       {
720         fprintf (stderr, " (expected:");
721         for (int i = 0; i < n; ++i)
722           fprintf (stderr, " [%s]", yysymbol_name (expected[i]));
723         fprintf (stderr, ")");
724       }
725   }
726   fprintf (stderr, "\n");
727   return res;
729 ]])[
731 /* A C error reporting function.  */
732 static
733 ]AT_YYERROR_PROTOTYPE[
734 {]m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
735               [[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\),* *], [
736   YY_USE (\1);])dnl
737 AT_YYERROR_SEES_LOC_IF([[
738   LOCATION_PRINT (stderr, ]AT_LOC[);
739   fprintf (stderr, ": ");]])[
740   fprintf (stderr, "%s\n", msg);]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
741   ++global_nerrs;
742   ++*nerrs;]])[
743 }]])])
746 m4_define([AT_YYLEX_DEFINE(c)],
747 [[#include <assert.h>
748 static
749 ]AT_YYLEX_PROTOTYPE[
751   ]m4_bmatch([$1], [^\(".*"\)?$],
752              [[static char const input[] = ]m4_default([$1], [""])],
753              [[static int const input[] = ]$1])[;
754   static int toknum = 0;
755   int res;
756   ]AT_USE_LEX_ARGS[
757   enum { input_elts = sizeof input / sizeof input[0] };
758   (void) input_elts;
759   assert (0 <= toknum && toknum < input_elts);
760   res = input[toknum++];
761   ]$2[;]AT_TOKEN_CTOR_IF([], [[
762   ]AT_LOCATION_IF([[
763   ]AT_LOC_FIRST_LINE[ = ]AT_LOC_LAST_LINE[ = 1;
764   ]AT_LOC_FIRST_COLUMN[ = ]AT_LOC_LAST_COLUMN[ = toknum;]])[
765   return res;]])[
766 }]])
769 m4_define([AT_MAIN_DEFINE(c)],
770 [[#include <stdlib.h> /* getenv. */
771 #include <string.h> /* strcmp. */
773 main (int argc, char const* argv[])
774 {]AT_DEBUG_IF([[
775   yydebug = !!getenv ("YYDEBUG");
776   for (int i = 1; i < argc; ++i)
777     if (!strcmp (argv[i], "-p")
778         || !strcmp (argv[i], "-d") || !strcmp (argv[i], "--debug"))
779       yydebug |= 1;
780     else if (!strcmp (argv[i], "-s") || !strcmp (argv[i], "--stat"))
781       yydebug |= 2;]], [[
782   (void) argc;
783   (void) argv;]])[
784   return ]AT_NAME_PREFIX[parse ();
785 }]])
787 m4_define([AT_LANG_FOR_EACH_STD(c)],
788 [$1])
791 ## ----- ##
792 ## C++.  ##
793 ## ----- ##
796 # AT_DATA_GRAMMAR(NAME, CONTENT)
797 # ------------------------------
798 m4_copy([AT_DATA_GRAMMAR(c)], [AT_DATA_GRAMMAR(c++)])
800 # No need to declare, it's part of the class interface.
801 m4_define([AT_YYERROR_DECLARE(c++)],        [])
802 m4_define([AT_YYERROR_DECLARE_EXTERN(c++)], [])
804 m4_define([AT_YYERROR_DEFINE(c++)],
805 [[/* A C++ error reporting function.  */
806 void
807 ]AT_NAMESPACE[::]AT_PARSER_CLASS[::error (]AT_LOCATION_IF([[const location_type& l, ]])[const std::string& m)
808 {]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
809   ++global_nerrs;
810   ++*nerrs;]])[
811   std::cerr << ]AT_LOCATION_IF([l << ": " << ])[m << '\n';
812 }]AT_ERROR_CUSTOM_IF([[
813 void
814 ]AT_NAMESPACE[::]AT_PARSER_CLASS[::report_syntax_error (const context& ctx) const
815 {]AT_PARAM_IF([m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
816               [[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\),* *], [
817   YY_USE (\1);])])[]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
818   ++global_nerrs;
819   ++*nerrs;]])[]AT_LOCATION_IF([[
820   std::cerr << ctx.location () << ": ";]])[
821   std::cerr << "syntax error";
822   {
823     symbol_kind_type la = ctx.token ();
824     if (la != symbol_kind::S_YYEMPTY)
825       std::cerr << " on token [" << symbol_name (la) << ']';
826   }
827   {
828     enum { TOKENMAX = 10 };
829     symbol_kind_type expected[TOKENMAX];
830     int n = ctx.expected_tokens (expected, TOKENMAX);
831     if (0 < n)
832       {
833         std::cerr << " (expected:";
834         for (int i = 0; i < n; ++i)
835           std::cerr << " [" << symbol_name (expected[i]) << ']';
836         std::cerr << ')';
837       }
838   }
839   std::cerr << '\n';
840 }]])])
843 # AT_YYLEX_DEFINE(c++)([INPUT], [ACTION])
844 # ---------------------------------------
845 # Same as in C.
846 m4_copy([AT_YYLEX_DEFINE(c)], [AT_YYLEX_DEFINE(c++)])
849 m4_define([AT_MAIN_DEFINE(c++)],
850 [[#include <cstdlib> // getenv.
851 #include <cstring> // strcmp.
853 main (int argc, char const* argv[])
855   ]AT_NAMESPACE[::]AT_PARSER_CLASS[ p;]AT_DEBUG_IF([[
856   int debug = !!getenv ("YYDEBUG");
857   for (int i = 1; i < argc; ++i)
858     if (!strcmp (argv[i], "-p")
859         || !strcmp (argv[i], "-d") || !strcmp (argv[i], "--debug"))
860       debug |= 1;
861     else if (!strcmp (argv[i], "-s") || !strcmp (argv[i], "--stat"))
862       debug |= 2;
863   p.set_debug_level (debug);]], [[
864   (void) argc;
865   (void) argv;]])[
866   return p.parse ();
867 }]])
869 m4_define([AT_LANG_FOR_EACH_STD(c++)],
870 [AT_FOR_EACH_CXX([$1])])
872 m4_define([AT_FOR_EACH_CXX],
873 [[at_for_each_std_CXXFLAGS_save=$CXXFLAGS
874 for at_std in '' \
875               ${CXX98_CXXFLAGS:+"$CXX98_CXXFLAGS"} \
876               ${CXX03_CXXFLAGS:+"$CXX03_CXXFLAGS"} \
877               ${CXX11_CXXFLAGS:+"$CXX11_CXXFLAGS"} \
878               ${CXX14_CXXFLAGS:+"$CXX14_CXXFLAGS"} \
879               ${CXX17_CXXFLAGS:+"$CXX17_CXXFLAGS"} \
880               ${CXX2A_CXXFLAGS:+"$CXX2A_CXXFLAGS"}
882   ]AS_ECHO(["======== Testing with C++ standard flags: '$at_cxx_std'"])[
883   CXXFLAGS="$at_for_each_std_CXXFLAGS_save $at_std"
884   ]$1[
885 done
886 CXXFLAGS=$at_for_each_std_CXXFLAGS_save
891 ## --- ##
892 ## D.  ##
893 ## --- ##
895 # AT_DATA_GRAMMAR(NAME, CONTENT)
896 # ------------------------------
897 m4_copy([AT_DATA], [AT_DATA_GRAMMAR(d)])
900 # No need to declare, it's part of the class interface.
901 m4_define([AT_YYERROR_DECLARE(d)],        [])
902 m4_define([AT_YYERROR_DECLARE_EXTERN(d)], [])
904 m4_define([AT_YYERROR_DEFINE(d)],
905 [[/* An error reporting function.  */
906 public void yyerror (]AT_LOCATION_IF([[const YYLocation l, ]])[string m)
908   stderr.writeln (]AT_LOCATION_IF([[l, ": ", ]])[m);
910 ]AT_ERROR_CUSTOM_IF([[
911 // In the case of D, there are no single quotes around the symbols
912 // so they need to be added here
913 public string transformToken(]AT_API_PREFIX[Parser.SymbolKind token)
915   string res;
916   foreach (i; format("%s", token))
917   {
918     if (i == '\"')
919       res ~= '\'';
920     else
921       res ~= i;
922   }
923   if (res.length == 1)
924     return '\'' ~ res ~ '\'';
925   else
926     return res;
929 public void reportSyntaxError(]AT_API_PREFIX[Parser.Context ctx)
931   // Buffer and print the message at the end, to avoid being intertwined
932   // with debug traces from getExpectedTokens.
933   string msg;
934   msg ~=]AT_LOCATION_IF([[ ctx.getLocation().toString() ~ ": " ~]])[ "syntax error";
935   {
936     ]AT_API_PREFIX[Parser.SymbolKind token = ctx.getToken();
937     msg ~= " on token @<:@" ~ transformToken(token) ~ "@:>@";
938   }
939   {
940     immutable int argmax = 7;
941     ]AT_API_PREFIX[Parser.SymbolKind[] arg = new ]AT_API_PREFIX[Parser.SymbolKind[argmax];
942     int n = ctx.getExpectedTokens(arg, argmax);
943     if (0 < n)
944     {
945       msg ~= " (expected:";
946       for (int i = 0; i < n; ++i)
947         msg ~= " @<:@" ~ transformToken(arg[i]) ~ "@:>@";
948       msg ~= ")";
949     }
950   }
951   stderr.writeln(msg);
953 ]])[]])
956 # FIXME: this does not work.  It is a stub copied blindly from Java.
957 m4_define([AT_YYLEX_DEFINE(d)],
959   /*--------.
960   | yylex.  |
961   `--------*/
963   public string input = ]$1[;
964   public int index = 0;
965   public int yylex() {
966     if (index < input.length())
967       return input.charAt(index++);
968     else
969       return 0;
970   }
971   public Object getLVal() {
972     ]m4_ifval([$2], [$2], [return null])[;
973   }
976 m4_define([AT_MAIN_DEFINE(d)],
977 [[int main ()
979   auto l = new ]AT_API_prefix[Lexer ();
980   auto p = new ]AT_API_PREFIX[Parser (l);
981   return p.parse ();
982 }]])
986 ## ------ ##
987 ## Java.  ##
988 ## ------ ##
991 # AT_DATA_GRAMMAR(NAME, CONTENT)
992 # ------------------------------
993 m4_copy([AT_DATA], [AT_DATA_GRAMMAR(java)])
995 # No need to declare, it's part of the class interface.
996 m4_define([AT_YYERROR_DECLARE(java)],        [])
997 m4_define([AT_YYERROR_DECLARE_EXTERN(java)], [])
999 # AT_JAVA_POSITION_DEFINE
1000 # -----------------------
1001 m4_define([AT_JAVA_POSITION_DEFINE],
1002 [[class Position {
1003   public int line = 1;
1004   public int column = 1;
1006   public Position ()
1007   {
1008     line = 1;
1009     column = 1;
1010   }
1012   public Position (int l, int t)
1013   {
1014     line = l;
1015     column = t;
1016   }
1018   public Position (Position p)
1019   {
1020     line = p.line;
1021     column = p.column;
1022   }
1024   public void set (Position p)
1025   {
1026     line = p.line;
1027     column = p.column;
1028   }
1030   public boolean equals (Position l)
1031   {
1032     return l.line == line && l.column == column;
1033   }
1035   public String toString ()
1036   {
1037     return Integer.toString (line) + "." + Integer.toString (column);
1038   }
1040   public int line ()
1041   {
1042     return line;
1043   }
1045   public int column ()
1046   {
1047     return column;
1048   }
1051 class PositionReader extends BufferedReader {
1053   private Position position = new Position ();
1054   private Position previousPosition = new Position ();
1056   public PositionReader (Reader reader) {
1057     super (reader);
1058   }
1060   public int read () throws IOException {
1061     int res = super.read ();
1062     previousPosition.set (position);
1063     if (res > -1) {
1064       char c = (char)res;
1065       if (c == '\r' || c == '\n') {
1066         position.line += 1;
1067         position.column = 1;
1068       } else {
1069         position.column += 1;
1070       }
1071     }
1072     return res;
1073   }
1075   public Position getPosition () {
1076     return position;
1077   }
1079   public Position getPreviousPosition () {
1080     return previousPosition;
1081   }
1082 }]])
1085 # AT_YYERROR_DEFINE(java)
1086 # -----------------------
1087 # FIXME: We should not hard-code "Calc".
1088 m4_define([AT_YYERROR_DEFINE(java)],
1089 [AT_LOCATION_IF([[public void yyerror (Calc.Location l, String m)
1090   {]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
1091     ++global_nerrs;
1092     ++*nerrs;]])[
1093     if (l == null)
1094       System.err.println(m);
1095     else
1096       System.err.println(l + ": " + m);
1097   }
1098 ]], [[
1099   public void yyerror (String m)
1100   {]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
1101     ++global_nerrs;
1102     ++*nerrs;]])[
1103     System.err.println (m);
1104   }
1105 ]])[
1107 ]AT_ERROR_CUSTOM_IF([[
1108   public void reportSyntaxError(Calc.Context ctx) {
1109     // Buffer and print the message at the end, to avoid being intertwined
1110     // with debug traces from getExpectedTokens.
1111     String msg = ]AT_LOCATION_IF([[ctx.getLocation() + ": " + ]])["syntax error";
1112     {
1113       Calc.SymbolKind token = ctx.getToken();
1114       if (token != null)
1115         msg += " on token @<:@" + token.getName() + "@:>@";
1116     }
1117     {
1118       Calc.SymbolKind[] arg = new Calc.SymbolKind[ctx.NTOKENS];
1119       int n = ctx.getExpectedTokens(arg, ctx.NTOKENS);
1120       if (0 < n) {
1121         msg += " (expected:";
1122         for (int i = 0; i < n; ++i)
1123           msg += " @<:@" + arg[i].getName() + "@:>@";
1124         msg += ")";
1125       }
1126     }
1127     System.err.println(msg);
1128   }
1132 m4_define([AT_YYLEX_DEFINE(java)],
1134   /*--------.
1135   | yylex.  |
1136   `--------*/
1138   public String input = ]$1[;
1139   public int index = 0;
1140   public int yylex() {
1141     if (index < input.length())
1142       return input.charAt(index++);
1143     else
1144       return 0;
1145   }
1146   public Object getLVal() {
1147     ]m4_ifval([$2], [$2], [return null])[;
1148   }
1151 m4_define([AT_MAIN_DEFINE(java)],
1152 [[class input
1154   public static void main(String[] args) throws IOException {
1155     ]AT_API_prefix[Parser p = new ]AT_API_prefix[Parser();
1156     boolean success = p.parse();
1157     if (!success)
1158       System.exit(1);
1159   }
1160 }]])
1162 m4_define([AT_LANG_FOR_EACH_STD(java)],
1163 [$1])
1167 ## --------------- ##
1168 ## Running Bison.  ##
1169 ## --------------- ##
1171 # AT_BISON_CHECK(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1172 # -------------------------------------------------
1173 # High-level routine that may call bison several times, under different
1174 # conditions.
1176 # Check Bison by invoking 'bison BISON_ARGS'.  BISON_ARGS should not contain
1177 # shell constructs (such as redirection or pipes) that would prevent
1178 # appending additional command-line arguments for bison. OTHER_AT_CHECK_ARGS
1179 # are the usual remaining arguments to AT_CHECK: STATUS, STDOUT, etc.
1181 # This macro or AT_BISON_CHECK_NO_XML should always be used whenever invoking
1182 # Bison in the test suite.  For now it ensures that:
1184 #   1. Valgrind doesn't report reachable memory when Bison is expected to have
1185 #      a non-zero exit status since Bison doesn't always try to free all memory
1186 #      in that case.
1188 #   2. In the case of maintainer-xml-check, XML/XSLT output is compared with
1189 #      --graph and --report=all output for every working grammar.
1191 #   3. If stderr contains a warning, -Werror and --warnings=error
1192 #      convert the warning to an error.
1194 #   4. If stderr contains a warning, -Wnone and --warnings=none suppress it.
1195 m4_define([AT_BISON_CHECK],
1196 [m4_null_if([$2], [AT_BISON_CHECK_XML($@)])
1197 AT_BISON_CHECK_NO_XML($@)])
1199 # AT_BISON_CHECK_(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1200 # --------------------------------------------------
1201 # Low-level macro to run bison once.
1202 m4_define([AT_BISON_CHECK_],
1203 [AT_CHECK(AT_SET_ENV[[ bison --color=no -fno-caret ]]$@)])
1206 # AT_BISON_CHECK_WARNINGS(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1207 # ----------------------------------------------------------
1208 # Check that warnings (if some are expected) are correctly
1209 # turned into errors with -Werror, etc.
1211 # When -Wno-error is used, the rules are really different, don't try.
1212 m4_define([AT_BISON_CHECK_WARNINGS],
1213 [m4_if(m4_bregexp([$4], [: warning: ]), [-1], [],
1214        m4_bregexp([$1], [-Wno-error=]), [-1],
1215                   [m4_null_if([$2], [AT_BISON_CHECK_WARNINGS_($@)])])])
1217 m4_define([AT_BISON_CHECK_WARNINGS_],
1218 [[# Defining POSIXLY_CORRECT causes bison to complain if options are
1219 # added after the grammar file name, so skip these checks in that
1220 # case.
1221 if test "$POSIXLY_CORRECT_IS_EXPORTED" = false; then
1222   ]AT_SAVE_SPECIAL_FILES[
1224   # To avoid expanding it repeatedly, store specified stdout.
1225   ]AT_DATA([expout], [$3])[
1227   # Run with -Werror.
1228   ]AT_BISON_CHECK_([$1[ -Werror]], [[1]], [expout], [stderr])[
1230   if test x"$PERL" != x; then
1231     # Build expected stderr up to and including the "warnings being
1232     # treated as errors" message.
1233     ]AT_DATA([[experr]], [$4])[
1234     "$PERL" -pi -e 's{(.*): warning:}{$][1: error:};' \
1235                 -e 's{\[-W(.*)\]$}{@<:@-Werror=$][1@:>@}' \
1236                     experr
1237     ]AT_CHECK([[sed 's,.*/$,,' stderr 1>&2]], [[0]], [[]], [experr])[
1239     # Now check --warnings=error.
1240     cp stderr experr
1241     ]AT_BISON_CHECK_([$1[ --warnings=error]], [[1]], [expout], [experr])[
1242   fi
1244   # Now check -Wnone and --warnings=none by making sure that
1245   # -Werror doesn't change the exit status when -Wnone or
1246   # --warnings=none is specified.  With traces disabled, there should
1247   # be no output on stderr.
1248   ]AT_BISON_CHECK_([$1[ -Wnone,none -Werror --trace=none]], [[0]], [expout])[
1249   ]AT_BISON_CHECK_([$1[ --warnings=none -Werror --trace=none]], [[0]], [expout])[
1251   ]AT_RESTORE_SPECIAL_FILES[
1252 fi]dnl
1255 # AT_BISON_CHECK_NO_XML(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1256 # --------------------------------------------------------
1257 # Same as AT_BISON_CHECK except don't perform XML/XSLT checks.  This is useful
1258 # when a tortured grammar's XML is known to be too large for xsltproc to
1259 # handle.
1260 m4_define([AT_BISON_CHECK_NO_XML],
1261 [AT_CHECK(AT_SET_ENV_IF([$2]) [[bison --color=no -fno-caret ]]$@)
1262 AT_BISON_CHECK_WARNINGS($@)])
1264 # AT_BISON_CHECK_XML(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1265 # -----------------------------------------------------
1266 # Run AT_BISON_CHECK's XML/XSLT checks if $BISON_TEST_XML=1 and $XSLTPROC is
1267 # defined.  It doesn't make sense to invoke this macro if Bison is expected to
1268 # have a non-zero exit status.
1269 m4_define([AT_BISON_CHECK_XML],
1270 [[if test x"$BISON_TEST_XML" = x1 && test x"$XSLTPROC" != x""; then]
1271   AT_SAVE_SPECIAL_FILES
1272   [mkdir xml-tests]
1273   m4_pushdef([AT_BISON_ARGS],
1274              [m4_bpatsubsts([[$1]],
1275                             [--report(-file)?=[^][ ]*], [],
1276                             [--graph=[^][ ]*], [],
1277                             [--xml=[^][ ]*], [])])dnl
1278   # Don't combine these Bison invocations since we want to be sure that
1279   # --report=all isn't required to get the full XML file.
1280   AT_BISON_CHECK_([[--report=all --report-file=xml-tests/test.output \
1281              --graph=xml-tests/test.gv ]]AT_BISON_ARGS,
1282            [[0]], [ignore], [ignore])
1283   AT_BISON_CHECK_([[--xml=xml-tests/test.xml ]]AT_BISON_ARGS,
1284            [[0]], [ignore], [ignore])
1285   m4_popdef([AT_BISON_ARGS])dnl
1286   [cp xml-tests/test.output expout]
1287   AT_CHECK([[$XSLTPROC \
1288              `]]AT_SET_ENV[[ bison --print-datadir`/xslt/xml2text.xsl \
1289              xml-tests/test.xml]], [[0]], [stdout])
1290   # xml2text and xml2dot always use '•', while --report uses '•' or '.'
1291   # depending on the locale, and the test suite is run with the plain
1292   # C locale.
1293   AT_CHECK([[sed -e 's/•/./g' stdout]], [], [expout])
1294   [sort xml-tests/test.gv > expout]
1295   AT_CHECK([[$XSLTPROC \
1296              `]]AT_SET_ENV[[ bison --print-datadir`/xslt/xml2dot.xsl \
1297              xml-tests/test.xml | sort | sed -e 's/•/./g']],
1298            [[0]], [stdout])
1299   [rm -rf xml-tests expout]
1300   AT_RESTORE_SPECIAL_FILES
1301 [fi]])
1304 # AT_SET_ENV_IF(EXIT-STATUS)
1305 # --------------------------
1306 # Put this before a Bison invocation to set the environment to:
1307 # - define COLUMNS to make the test suite independent of the user's
1308 #   environment;
1309 # - keep Valgrind from complaining about reachable memory (when
1310 #   EXIT-STATUS is not 0).
1312 # Do not quote invocations of this macro within the first argument of AT_CHECK.
1313 # The triple quoting below will cause test cases to fail if you do.  If you do
1314 # so anyway but also decrease the quoting below to avoid that problem, AT_CHECK
1315 # will then fail to shell-escape its contents when attempting to print them.
1316 # The testsuite verbose output, at least, will be incorrect, but nothing may
1317 # fail to make sure you notice.
1318 m4_define([AT_SET_ENV_IF],
1319 [[[COLUMNS=1000; export COLUMNS; NO_TERM_HYPERLINKS=1; export NO_TERM_HYPERLINKS;]] m4_null_if($1, [], [[[VALGRIND_OPTS="$VALGRIND_OPTS --leak-check=summary --show-reachable=no"; export VALGRIND_OPTS; ]]])])
1322 # AT_SET_ENV
1323 # ----------
1324 # See above.
1325 m4_define([AT_SET_ENV],
1326 [AT_SET_ENV_IF([1])])
1328 ## ------------------------ ##
1329 ## Compiling C, C++ Files.  ##
1330 ## ------------------------ ##
1333 # AT_COMPILE(OUTPUT, [SOURCES = OUTPUT.c], [EXTRA-COMPILER-FLAGS])
1334 # ----------------------------------------------------------------
1335 # Compile SOURCES into OUTPUT.
1337 # If OUTPUT does not contain '.', assume that we are linking too,
1338 # otherwise pass "-c"; this is a hack.  The default SOURCES is OUTPUT
1339 # with trailing .o removed, and ".c" appended.
1340 m4_define([AT_COMPILE],
1341 [AT_SKIP_IF([[! $BISON_C_WORKS]])
1342 AT_CHECK(m4_join([ ],
1343                  [$CC $CFLAGS $CPPFLAGS $3],
1344                  [m4_bmatch([$1], [[.]], [-c], [$LDFLAGS])],
1345                  [-o $1],
1346                  [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).c])],
1347                  [m4_bmatch([$1], [[.]], [], [$LIBS])]),
1348          0, [ignore], [ignore])])
1351 # AT_COMPILE_CXX(OUTPUT, [SOURCES = OUTPUT.cc], [EXTRA-COMPILER-FLAGS])
1352 # ---------------------------------------------------------------------
1353 # Compile SOURCES into OUTPUT.  If the C++ compiler does not work,
1354 # ignore the test.
1356 # If OUTPUT does not contain '.', assume that we are linking too,
1357 # otherwise pass "-c"; this is a hack.  The default SOURCES is OUTPUT
1358 # with trailing ".o" removed, and ".cc" appended.
1359 m4_define([AT_COMPILE_CXX],
1360 [AT_KEYWORDS(c++)
1361 AT_SKIP_IF([[! $BISON_CXX_WORKS]])
1362 AT_CHECK(m4_join([ ],
1363                  [$CXX $CXXFLAGS $CPPFLAGS $3],
1364                  [m4_bmatch([$1], [[.]], [-c], [$LDFLAGS])],
1365                  [-o $1],
1366                  [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).cc])],
1367                  [m4_bmatch([$1], [[.]], [], [$LIBS])]),
1368          0, [ignore], [ignore])])
1371 # AT_COMPILE_D(OUTPUT, [SOURCES = OUTPUT.d], [EXTRA-COMPILER-FLAGS])
1372 # -------------------------------------------------------------------
1373 # Compile SOURCES into OUTPUT.  If the C++ compiler does not work,
1374 # ignore the test.
1376 # If OUTPUT does not contain '.', assume that we are linking too,
1377 # otherwise pass "-c"; this is a hack.  The default SOURCES is OUTPUT
1378 # with trailing ".o" removed, and ".cc" appended.
1379 m4_define([AT_COMPILE_D],
1380 [AT_KEYWORDS(d)
1381 AT_SKIP_IF([[! $BISON_DC_WORKS]])
1382 AT_CHECK(m4_join([ ],
1383                  [$DC $DCFLAGS $3],
1384                  [m4_bmatch([$1], [[.]], [-c])],
1385                  [-of$1],
1386                  [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).d])]),
1387          0, [ignore], [ignore])])
1390 # AT_JAVA_COMPILE(SOURCES)
1391 # ------------------------
1392 # Compile SOURCES into Java class files.  Skip the test if java or javac
1393 # is not installed.
1394 m4_define([AT_JAVA_COMPILE],
1395 [AT_KEYWORDS(java)
1396 AT_SKIP_IF([[test -z "$CONF_JAVAC"]])
1397 AT_SKIP_IF([[test -z "$CONF_JAVA"]])
1398 AT_CHECK([[$SHELL ../../../javacomp.sh ]$1],
1399          [[0]], [ignore], [ignore])])
1402 # AT_LANG_FOR_EACH_STD(BODY)
1403 # --------------------------
1404 m4_define([AT_LANG_FOR_EACH_STD],  [AT_LANG_DISPATCH([$0], $@)])
1407 # AT_LANG_COMPILE(OUTPUT, [SOURCES = OUTPUT.c], [EXTRA-COMPILER-FLAGS])
1408 # ---------------------------------------------------------------------
1409 # Compile SOURCES into OUTPUT.  Skip if compiler does not work.
1411 # If OUTPUT does not contain '.', assume that we are linking too,
1412 # otherwise pass "-c"; this is a hack.  The default SOURCES is OUTPUT
1413 # with trailing .o removed, and ".c"/".cc" appended.
1414 m4_define([AT_LANG_COMPILE],  [AT_LANG_DISPATCH([$0], $@)])
1415 m4_define([AT_LANG_COMPILE(c)],    [AT_COMPILE([$1], [$2], [$3])])
1416 m4_define([AT_LANG_COMPILE(c++)],  [AT_COMPILE_CXX([$1], [$2], [$3])])
1417 m4_define([AT_LANG_COMPILE(d)],    [AT_COMPILE_D([$1], [$2], [$3])])
1418 m4_define([AT_LANG_COMPILE(java)], [AT_JAVA_COMPILE([$1.java], [$2], [$3])])
1421 # AT_LANG_EXT
1422 # -----------
1423 # The file extension corresponding to the language: c, cc, or java.
1424 m4_define([AT_LANG_EXT],  [AT_LANG_DISPATCH([$0], $@)])
1425 m4_define([AT_LANG_EXT(c)],    [c])
1426 m4_define([AT_LANG_EXT(c++)],  [cc])
1427 m4_define([AT_LANG_EXT(d)],    [d])
1428 m4_define([AT_LANG_EXT(java)], [java])
1431 # AT_LANG_HDR
1432 # -----------
1433 # The header file extension corresponding to the language: c, cc.
1434 m4_define([AT_LANG_HDR],  [AT_LANG_DISPATCH([$0], $@)])
1435 m4_define([AT_LANG_HDR(c)],    [h])
1436 m4_define([AT_LANG_HDR(c++)],  [hh])
1439 # AT_FULL_COMPILE(OUTPUT, [OTHER1], [OTHER2],
1440 #                 [$4: EXTRA-COMPILER-FLAGS], [$5: EXTRA-BISON-FLAGS])
1441 # --------------------------------------------------------------------
1442 # Compile OUTPUT.y to OUTPUT.c, OUTPUT.cc, or OUTPUT.java, and then
1443 # compile it to OUTPUT or OUTPUT.class.  If OTHER is specified, compile
1444 # OUTPUT-OTHER.c, OUTPUT-OTHER.cc, or OUTPUT-OTHER.java to OUTPUT or
1445 # OUTPUT.java along with it.  Relies on AT_CXX_IF and
1446 # AT_JAVA_IF.
1447 m4_define([AT_FULL_COMPILE],
1448 [AT_BISON_CHECK([$5 -o $1.AT_LANG_EXT $1.y])
1449  AT_LANG_COMPILE([$1],
1450                  m4_join([ ],
1451                          [$1.AT_LANG_EXT],
1452                          m4_ifval($2, [[$1-$2.]AT_LANG_EXT]),
1453                          m4_ifval($3, [[$1-$3.]AT_LANG_EXT])),
1454                  [$4])])
1458 # AT_SKIP_IF_CANNOT_LINK_C_AND_CXX
1459 # --------------------------------
1460 # Check that we can link together C and C++ objects.
1461 m4_define([AT_SKIP_IF_CANNOT_LINK_C_AND_CXX],
1462 [AT_DATA([c-and-cxx.h],
1463 [[#ifdef __cplusplus
1464 extern "C"
1466 #endif
1467   int fortytwo (void);
1468 #ifdef __cplusplus
1470 #endif
1472 AT_DATA([c-only.c],
1473 [[#include "c-and-cxx.h"
1475 main (void)
1477   return fortytwo () == 42 ? 0 : 1;
1480 AT_DATA([cxx-only.cc],
1481 [[#include "c-and-cxx.h"
1482 int fortytwo ()
1484   return 42;
1487 AT_COMPILE([c-only.o], [c-only.c])
1488 AT_COMPILE_CXX([cxx-only.o], [cxx-only.cc])
1489 AT_CHECK([$CXX $CXXFLAGS $CPPFLAGS $LDFLAGS c-only.o cxx-only.o -o c-and-cxx ||
1490           exit 77], [0], [ignore], [ignore])
1491 AT_C_PARSER_CHECK([c-and-cxx])
1495 # AT_REQUIRE_CXX_STD(STD, [IF-FAIL = SKIP])
1496 # -----------------------------------------
1497 # Skip unless this compiler supports at least C++ STD (e.g., "11",
1498 # "14", etc.).
1499 m4_define([AT_REQUIRE_CXX_STD],
1500 [AT_DATA([check.cc],
1501 [[int main ()
1503 #if !defined __cplusplus || __cplusplus < ]m4_case([$1],
1504     [98], [199711],
1505     [03], [199711],
1506     [11], [201103],
1507     [14], [201402],
1508     [17], [201703],
1509     [2a], [201709],
1510     [m4_fatal([$0: invalid arguments: $@])])[
1511   return 1;
1512 #else
1513   return 0;
1514 #endif
1517 AT_COMPILE_CXX([check])
1518 AT_CHECK([@&t@./check], [ignore])
1519 m4_ifval([$2],
1520          [if test $at_status != 0; then
1521             $2
1522           fi],
1523          [AT_SKIP_IF([test $at_status != 0])])
1527 # AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR
1528 # ------------------------------------
1529 # Check that we can expect exceptions to be handled properly.
1530 # GCC 4.3 and 4.4 fail https://trac.macports.org/ticket/40853.
1531 m4_define([AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR],
1532 [AT_DATA_SOURCE([exceptions.cc],
1533 [[#include <iostream>
1534 #include <stdexcept>
1536 void foo ()
1538   try
1539     {
1540       throw std::runtime_error ("foo");
1541     }
1542   catch (...)
1543     {
1544       std::cerr << "Inner caught\n";
1545       throw;
1546     }
1549 int main ()
1551   try
1552     {
1553       foo ();
1554     }
1555   catch (...)
1556     {
1557       std::cerr << "Outer caught\n";
1558       return 0;
1559     }
1560   return 1;
1563 AT_COMPILE_CXX([exceptions])
1564 # The "empty" quadrigraph is to protect from cfg.mk's sc_at_parser_check.
1565 AT_CHECK([@&t@./exceptions || exit 77], [0], [], [ignore])
1569 ## ---------------------------- ##
1570 ## Running a generated parser.  ##
1571 ## ---------------------------- ##
1574 # AT_C_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
1575 # --------------------------------------------------------------
1576 # So that we can run './testsuite PREPARSER='valgrind -q' for instance.
1578 # Get rid of spurious messages when compiled with --coverage:
1579 # +profiling:/[...]/lib/fprintf.gcda:Merge mismatch for summaries
1580 m4_define([AT_C_PARSER_CHECK],
1581 [AT_CHECK([$5 $PREPARSER ./$1], [$2], [$3], [stderr])
1582 AT_CHECK([sed >&2 -e '/^profiling:.*:Merge mismatch for summaries/d' stderr],
1583          [0], [], [$4])
1587 # AT_JAVA_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
1588 # -----------------------------------------------------------------
1589 m4_define([AT_JAVA_PARSER_CHECK],
1590 [AT_CHECK([$5[ $SHELL ../../../javaexec.sh ]$1], [$2], [$3], [$4])])
1593 # AT_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
1594 # ------------------------------------------------------------
1595 # If we know we are in Java (via AT_BISON_OPTION_PUSHDEFS/POPDEFS),
1596 # run AT_JAVA_PARSER_CHECK, otherwise AT_C_PARSER_CHECK.
1597 m4_define([AT_PARSER_CHECK],
1598 [m4_ifdef([AT_JAVA_IF],
1599           [AT_JAVA_IF([AT_JAVA_PARSER_CHECK($@)],
1600                       [AT_C_PARSER_CHECK($@)])],
1601           [AT_C_PARSER_CHECK($@)])])
1604 # AT_TEST_TABLES_AND_PARSE(TITLE, COND-VALUE, TEST-SPEC,
1605 #                          DECLS, GRAMMAR, INPUT,
1606 #                          BISON-STDERR, TABLES-OR-LAST-STATE,
1607 #                          [OTHER-CHECKS],
1608 #                          [PARSER-EXIT-VALUE],
1609 #                          [PARSER-STDOUT], [PARSER-STDERR])
1610 # -------------------------------------------------------------
1611 # Using TITLE as the test group title, check the generated parser tables
1612 # and parser for a specified grammar file under a condition labeled by
1613 # COND-VALUE.
1615 # TEST-SPEC is a comma-delimited list of attributes of this test.  Each
1616 # recognized attribute is described below where it is relevant.
1618 # Insert DECLS and GRAMMAR into the declarations and grammar section of
1619 # the grammar file.  Insert basic yyerror, yylex, and main function
1620 # definitions as well.  Hardcode yylex to return the (possibly empty)
1621 # comma-delimited series of tokens in INPUT followed by token 0.
1623 # If TEST-SPEC contains the attribute no-xml, then invoke bison using
1624 # AT_BISON_CHECK_NO_XML.  Otherwise, invoke bison using AT_BISON_CHECK.
1625 # On the bison command-line, specify `--report=all --header'.  Check
1626 # that Bison exits with value 0, has no stdout, and has stderr
1627 # BISON-STDERR.
1629 # If TEST-SPEC contains the attribute 'last-state', check that the value
1630 # of TABLES-OR-LAST-STATE is the index of the last state generated for
1631 # the grammar; in other words, check the number of states (minus one).
1632 # Otherwise, check that everything in the '.output' file starting with
1633 # the definition of state 0 is the same as the entire value of
1634 # TABLES-OR-LAST-STATE.
1636 # Expand the M4 in OTHER-CHECKS to perform additional checks of the
1637 # '.output' file, which is named 'input.output', and/or grammar file,
1638 # which is named 'input.y'.
1640 # Finally, compile the generated parser and then run it using
1641 # AT_PARSER_CHECK with PARSER-EXIT-VALUE, PARSER-STDOUT, and
1642 # PARSER-STDERR as the 2nd-4th arguments.
1644 # As a precondition, you must properly double-quote all arguments that
1645 # are to be interpreted as strings.
1647 # AT_COND_CASE (when appearing in single-quoted segments of arguments)
1648 # invokes m4_case with its own arguments but COND-VALUE inserted as the
1649 # first argument.  This is useful, for example, when wrapping multiple
1650 # AT_TEST_TABLES_AND_PARSE invocations, each representing a different
1651 # condition, in another macro.
1653 # For example:
1655 #   # AT_TEST_SYNTAX_ERROR(DESCRIPTION, DECLS, GRAMMAR, INPUT, LAST-STATE,
1656 #   #                      PARSER-EXIT-VALUE, PARSER-STDOUT, PARSER-STDERR)
1657 #   # ---------------------------------------------------------------------
1658 #   m4_define([AT_TEST_SYNTAX_ERROR],
1659 #   [
1660 #     AT_TEST_TABLES_AND_PARSE([$1[ with %define parse.error verbose]], [[verbose]],
1661 #                              [[last-state]],
1662 #                              [[%define parse.error verbose ]$2], [$3], [$4],
1663 #                              [[]], [$5], [], [$6], [$7], [$8])
1664 #     AT_TEST_TABLES_AND_PARSE([$1[ with no %define parse.error verbose]], [[no verbose]],
1665 #                              [[last-state]],
1666 #                              [$2], [$3], [$4],
1667 #                              [[]], [$5], [], [$6], [$7], [$8])
1668 #   ])
1670 #   AT_TEST_SYNTAX_ERROR([[Single Char Grammar]],
1671 #                        [[%token 'b']], [[start: 'a' ;]], [['a', 'b']],
1672 #                        [[3]],
1673 #                        [[1]], [[]],
1674 #                        [AT_COND_CASE([[no verbose]],
1675 #                                      [[syntax error
1676 #   ]],
1677 #                                      [[syntax error, unexpected 'b', expecting $end
1678 #   ]])])
1679 m4_define([AT_TEST_TABLES_AND_PARSE],
1680 [m4_pushdef([AT_COND_CASE], [m4_case([$2], $][@)])
1682 AT_SETUP([$1])
1683 AT_BISON_OPTION_PUSHDEFS([$4])
1684 AT_DATA_GRAMMAR([[input.y]],
1685 [[%code {
1686   ]AT_YYERROR_DECLARE[
1687   ]AT_YYLEX_DECLARE[
1690 ]$4[
1694 ]$5[
1697 ]AT_YYERROR_DEFINE[
1698 static int
1699 yylex (void)
1701   static int const input[] = {
1702     ]m4_if([$6], [], [], [$6], [[]], [], [$6[, ]])[0
1703   };
1704   static int const *inputp = input;
1705   return *inputp++;
1708 ]AT_MAIN_DEFINE[
1711 # In some versions of Autoconf, AT_CHECK invokes AS_ESCAPE before
1712 # expanding macros, so it corrupts some special characters in the
1713 # macros.  To avoid this, expand now and pass it the result with proper
1714 # string quotation.  Assume args 7 through 12 expand to properly quoted
1715 # strings.
1717 m4_if(m4_index(m4_quote($3), [no-xml]), -1,
1718       [AT_BISON_CHECK],
1719       [AT_BISON_CHECK_NO_XML])([[-Wall --report=all,no-cex --header -o input.c input.y]],
1720                                [0], [], m4_dquote($7))
1722 m4_if(m4_index(m4_quote($3), [last-state]), -1,
1723       [AT_CHECK([[sed -n '/^State 0$/,$p' input.output]], [[0]],
1724                 m4_dquote($8))],
1725       [AT_CHECK([[sed -n 's/^State //p' input.output | tail -1]], [[0]],
1726                 m4_dquote($8)[[
1727 ]])])
1731 # Canonical LR generates very large tables, resulting in very long
1732 # files with #line directives that may overflow what the standards
1733 # (C90 and C++98) guarantee: 32767.  In that case, GCC's -pedantic
1734 # will issue an error.
1736 # There is no "" around `wc` since some wc indent the result.
1737 m4_bmatch([$4], [%define lr.type canonical-lr],
1738 [if test 32767 -lt `wc -l < input.c`; then
1739   CFLAGS=`echo " $CFLAGS " | sed -e 's/ -pedantic / /'`
1740   CXXFLAGS=`echo " $CXXFLAGS " | sed -e 's/ -pedantic / /'`
1741 fi])
1742 AT_COMPILE([[input]])
1744 AT_PARSER_CHECK([[input]],
1745                 m4_ifval([$10], [m4_dquote($10)]),
1746                 m4_ifval([$11], [m4_dquote($11)]),
1747                 m4_ifval([$12], [m4_dquote($12)]))
1749 AT_BISON_OPTION_POPDEFS
1750 AT_CLEANUP
1752 m4_popdef([AT_COND_CASE])])
1756 # AT_SETS_CHECK(INPUT, SETS, EXPECTED)
1757 # ------------------------------------
1758 # Extract the information about the grammar sets from a bison
1759 # trace output (INPUT), and compare to EXPECTED.
1761 # And remember, there is no alternation in portable sed.
1762 m4_define([AT_SETS_CHECK],
1763 [AT_DATA([extract.sed],
1764 [[#n
1765 ]m4_foreach([m4_Set], [$2], [[
1766 /^]m4_Set[/ {
1767    :]m4_substr(m4_Set, 0, 7)[
1768    p
1769    n
1770    /^ *$/ !b ]m4_substr(m4_Set, 0, 7)[
1772 ]])])
1774 AT_CHECK([sed -f extract.sed $1], 0, [$3])
1780 ## ----------------------- ##
1781 ## Launch the test suite.  ##
1782 ## ----------------------- ##
1784 AT_INIT
1786 # AT_SETUP([TITLE])
1787 # -----------------
1788 # Redefine AT_SETUP to be more concise.  Must be done after AT_INIT.
1789 m4_copy_force([AT_SETUP], [B4_SETUP])
1790 m4_define([AT_SETUP],
1791 [B4_SETUP(m4_expand([AT_SETUP_STRIP([[$1]])]))])
1793 # Cannot assign CC and CFLAGS here, since atlocal is loaded after
1794 # options are processed, so we don't know the value of CXX and
1795 # CXXFLAGS yet.
1797 # Note that it also means that command line values for CXX and
1798 # CXXFLAGS will not be propagated to CC and CFLAGS.
1799 AT_ARG_OPTION([compile-c-with-cxx],
1800               [compile C parsers with the C++ compiler])
1802 AT_COLOR_TESTS
1804 AT_TESTED([bison])