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),
33 # m4_rpatsubst(STRING, PATTERN, REPLACEMENT)
34 # ------------------------------------------
35 # As long as PATTERN matches STRING, replace occurrences of PATTERN by
38 # Contrary to m4_bpatsubst, matches PATTERN from previous replacements.
39 m4_define([m4_rpatsubst],
40 [m4_if(m4_bregexp([$1], [$2]),
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],
51 [%\(language\|skeleton\) "?\([^\" ]*\)"?],
53 [%define "?\([-A-Za-z0-9_.]+\)"? \({[^\}]+}\|"[^\"]+"\|[-A-Za-z0-9_.]+\)],
55 [%define "?\([-A-Za-z0-9_.]+\)"?],
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],
73 set x `LC_ALL=C ls -l '$1'` &&
75 { test $size -eq 0 || dd obs=1 seek=`expr $size - 1` if=/dev/null of='$1'; }],
76 [], [ignore], [ignore])
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 '
127 s{$2}{ ++$count; "" }gem;
128 printf "$count\n";' $1], [0], [$3
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
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
146 at_restore_special_files ()
148 for at_save_file in stderr experr expout
150 test ! -f at-bison-check-$at_save_file.bak ||
151 mv at-bison-check-$at_save_file.bak $at_save_file
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]],
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])])
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],
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])])
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])])
278 m4_pushdef([AT_YACC_IF],
279 [m4_bmatch([$3], [%yacc], [$1], [$2])])
281 # Comma-terminated list of formals parse-parameters.
282 # E.g., %parse-param { int x } %parse-param {int y} -> "int x, int y, ".
283 m4_bpatsubst([$3], [%parse-param { *\([^{}]*[^{} ]\) *}{ *\([^{}]*[^{} ]\) *}{ *\([^{}]*[^{} ]\) *}],
284 [m4_append([AT_PARSE_PARAMS], [\1, \2, \3], [, ])])
285 m4_bpatsubst([$3], [%parse-param { *\([^{}]*[^{} ]\) *}{ *\([^{}]*[^{} ]\) *}\([^{].*\)?$],
286 [m4_append([AT_PARSE_PARAMS], [\1, \2], [, ])])
287 m4_rpatsubst([$3], [%parse-param { *\([^{}]*[^{} ]\) *}\([^{}].*\)?$],
288 [m4_append([AT_PARSE_PARAMS], [\1], [, ])\2])
289 m4_ifndef([AT_PARSE_PARAMS],
290 [m4_define([AT_PARSE_PARAMS])])
292 m4_pushdef([AT_PARSER_CLASS],
293 [m4_bmatch([$3], [%define *api\.parser\.class {\([^\}]*\)}],
294 [m4_bregexp([$3], [%define *api\.parser\.class {\([^\}]*\)}], [\1])],
295 [AT_JAVA_IF([AT_API_PREFIX[]Parser], [parser])])])
297 m4_pushdef([AT_PURE_IF],
298 [m4_bmatch([$3], [%define *api\.pure\|%pure-parser],
299 [m4_bmatch([$3], [%define *api\.pure *false], [$2], [$1])],
301 m4_pushdef([AT_PUSH_IF],
302 [m4_bmatch([$3], [%define api.push-pull \(both\|pull\)], [$1], [$2])])
303 # Whether we use %define api.value.type union
304 m4_pushdef([AT_VALUE_UNION_IF],
305 [m4_bmatch([$3], [%define api\.value\.type union], [$1], [$2])])
309 m4_pushdef([AT_NAME_PREFIX],
310 [m4_bmatch([$3], [\(%define api\.prefix\|%name-prefix\) .*],
312 [\(%define api\.prefix\|%name-prefix\) [\{\"]\([^\"\}]*\)[\"\}]],
314 [AT_JAVA_IF([YY], [yy])])])
315 # AT_NAMESPACE: also consider api.prefix. FIXME: Stop that confusion.
316 m4_pushdef([AT_NAMESPACE],
317 [m4_bmatch([$3], [\(%define api\.\(namespace\|prefix\)\|%name-prefix\) .*],
319 [\(%define api\.\(namespace\|prefix\)\|%name-prefix\) [\{\"]\([^\"\}]*\)[\"\}]],
322 m4_pushdef([AT_TOKEN_CTOR_IF],
323 [m4_bmatch([$3], [%define api\.token\.constructor], [$1], [$2])])
324 m4_pushdef([AT_TOKEN_PREFIX],
325 [m4_bmatch([$3], [%define api\.token\.prefix {.*}],
326 [m4_bregexp([$3], [%define api\.token\.prefix {\(.*\)}], [\1])])])
327 m4_pushdef([AT_TOKEN_RAW_IF],
328 [m4_bmatch([$3], [%define api\.token\.raw], [$1], [$2])])
329 m4_pushdef([AT_UNION_IF],
330 [m4_bmatch([$3], [%define api\.value\.type union], [$1], [$2])])
331 m4_pushdef([AT_VARIANT_IF],
332 [m4_bmatch([$3], [%define api\.value\.type variant], [$1], [$2])])
333 m4_pushdef([AT_UNION_IF],
334 [m4_bmatch([$3], [%define api\.value\.type union], [$1], [$2])])
335 m4_pushdef([AT_API_prefix],
336 [m4_bmatch([$3], [%define api\.prefix {.*}],
337 [m4_bregexp([$3], [%define api\.prefix {\([^\}]*\)}], [\1])],
338 [AT_JAVA_IF([YY], [yy])])])
339 m4_pushdef([AT_API_PREFIX],
340 [m4_toupper(AT_API_prefix)])
341 # yyerror receives the location if %location, and if the parser is pure. For
342 # historical reasons, with the "yacc.c" skeleton, the location is not passed
343 # unless an additional "%parse-param" is present, or if the purity is defined
345 m4_pushdef([AT_YYERROR_ARG_LOC_IF],
346 [AT_LOCATION_IF([AT_PURE_IF([m4_bmatch([$3],
347 m4_quote(m4_join([\|],
348 [%define api\.pure full],
351 [%skeleton "?glr.c"?])),
356 # yyerror always sees the locations (when activated) if the parser is impure.
357 # When the parser is pure, yyerror sees the location if it is received as an
359 m4_pushdef([AT_YYERROR_SEES_LOC_IF],
360 [AT_LOCATION_IF([AT_YACC_C_IF([AT_PURE_IF([AT_YYERROR_ARG_LOC_IF([$1], [$2])],
365 # The interface is pure: either because %define api.pure, or because we
366 # are using the C++ parsers.
367 m4_pushdef([AT_PURE_LEX_IF],
369 [AT_CXX_IF([$1], [$2])])])
371 # Whether token translation is supported.
372 m4_pushdef([AT_TOKEN_TRANSLATE_IF],
373 [AT_ERROR_CUSTOM_IF([$1], [AT_ERROR_DETAILED_IF([$1], [$2])])])
375 m4_pushdef([AT_TOKEN],
376 [AT_CXX_IF([AT_CXX_IF([AT_NAMESPACE::AT_PARSER_CLASS::token::])[]AT_TOKEN_PREFIX[$1]],
377 [AT_TOKEN_PREFIX[$1]])])
380 m4_pushdef([AT_YYSTYPE],
381 [AT_CXX_IF([AT_NAMESPACE[::]AT_PARSER_CLASS[::value_type]],
382 [AT_API_PREFIX[STYPE]])])
383 m4_pushdef([AT_YYLTYPE],
384 [AT_CXX_IF([AT_NAMESPACE[::]AT_PARSER_CLASS[::location_type]],
385 [AT_API_PREFIX[LTYPE]])])
388 [m4_pushdef([AT_LOC], [[(]AT_NAME_PREFIX[lloc)]])
389 m4_pushdef([AT_VAL], [[(]AT_NAME_PREFIX[lval)]])
390 m4_pushdef([AT_YYLEX_FORMALS], [])
391 m4_pushdef([AT_YYLEX_RETURN], [AT_NAMESPACE::AT_PARSER_CLASS::symbol_type])
392 m4_pushdef([AT_YYLEX_ARGS], [])
393 m4_pushdef([AT_USE_LEX_ARGS], [])
394 m4_pushdef([AT_YYLEX_PRE_FORMALS], [])
395 m4_pushdef([AT_YYLEX_PRE_ARGS], [])],
397 [m4_pushdef([AT_LOC], [(*llocp)])
398 m4_pushdef([AT_VAL], [(*lvalp)])
399 m4_pushdef([AT_YYLEX_FORMALS],
400 [AT_YYSTYPE *lvalp[]AT_LOCATION_IF([, AT_YYLTYPE *llocp])])
401 m4_pushdef([AT_YYLEX_RETURN], [int])
402 m4_pushdef([AT_YYLEX_ARGS],
403 [lvalp[]AT_LOCATION_IF([, llocp])])
404 m4_pushdef([AT_USE_LEX_ARGS],
405 [(void) lvalp;AT_LOCATION_IF([(void) llocp;])])
406 m4_pushdef([AT_YYLEX_PRE_FORMALS],
407 [AT_YYLEX_FORMALS, ])
408 m4_pushdef([AT_YYLEX_PRE_ARGS],
411 [m4_pushdef([AT_LOC], [[(]AT_NAME_PREFIX[lloc)]])
412 m4_pushdef([AT_VAL], [[(]AT_NAME_PREFIX[lval)]])
413 m4_pushdef([AT_YYLEX_FORMALS], [void])
414 m4_pushdef([AT_YYLEX_RETURN], [int])
415 m4_pushdef([AT_YYLEX_ARGS], [])
416 m4_pushdef([AT_USE_LEX_ARGS], [])
417 m4_pushdef([AT_YYLEX_PRE_FORMALS], [])
418 m4_pushdef([AT_YYLEX_PRE_ARGS], [])
422 # Handle the different types of location components.
423 AT_LOCATION_TYPE_SPAN_IF(
424 [AT_LOC_PUSHDEF([first.l], [first.c], [last.l], [last.c])],
425 [AT_CXX_IF([AT_LOC_PUSHDEF([begin.line], [begin.column], [end.line], [end.column])],
426 [AT_LOC_PUSHDEF([first_line], [first_column], [last_line], [last_column])])])
429 AT_GLR_IF([AT_KEYWORDS([glr])])
430 AT_MULTISTART_IF([AT_KEYWORDS([multistart])])
431 AT_PUSH_IF([AT_KEYWORDS([push])])
432 AT_YACC_IF([AT_KEYWORDS([yacc])])
433 ])# _AT_BISON_OPTION_PUSHDEFS
436 # AT_BISON_OPTION_POPDEFS
437 # -----------------------
438 m4_define([AT_BISON_OPTION_POPDEFS],
439 [m4_divert_text([KILL],
440 [m4_popdef([AT_BISON_OPTIONS])
442 m4_popdef([AT_YYLEX_PRE_ARGS])
443 m4_popdef([AT_YYLEX_PRE_FORMALS])
444 m4_popdef([AT_USE_LEX_ARGS])
445 m4_popdef([AT_YYLEX_ARGS])
446 m4_popdef([AT_YYLEX_FORMALS])
447 m4_popdef([AT_YYLTYPE])
448 m4_popdef([AT_YYSTYPE])
451 m4_popdef([AT_TOKEN])
452 m4_popdef([AT_TOKEN_TRANSLATE_IF])
453 m4_popdef([AT_PURE_LEX_IF])
454 m4_popdef([AT_YYERROR_SEES_LOC_IF])
455 m4_popdef([AT_YYERROR_ARG_LOC_IF])
456 m4_popdef([AT_API_PREFIX])
457 m4_popdef([AT_API_prefix])
458 m4_popdef([AT_VARIANT_IF])
459 m4_popdef([AT_UNION_IF])
460 m4_popdef([AT_TOKEN_RAW_IF])
461 m4_popdef([AT_TOKEN_PREFIX])
462 m4_popdef([AT_TOKEN_CTOR_IF])
463 m4_popdef([AT_NAMESPACE])
464 m4_popdef([AT_NAME_PREFIX])
465 m4_popdef([AT_LOCATION_TYPE_SPAN_IF])
466 m4_popdef([AT_LOCATION_IF])
467 m4_undefine([AT_PARSE_PARAMS])
468 m4_popdef([AT_VALUE_UNION_IF])
469 m4_popdef([AT_PUSH_IF])
470 m4_popdef([AT_PURE_IF])
471 m4_popdef([AT_PARSER_CLASS])
472 m4_popdef([AT_YACC_IF])
473 m4_popdef([AT_PARAM_IF])
474 m4_popdef([AT_MULTISTART_IF])
475 m4_popdef([AT_LEXPARAM_IF])
476 m4_popdef([AT_LAC_IF])
477 m4_popdef([AT_YACC_C_IF])
478 m4_popdef([AT_GLR_IF])
479 m4_popdef([AT_CXX_IF])
482 m4_popdef([AT_JAVA_IF])
483 m4_popdef([AT_GLR2_CC_IF])
484 m4_popdef([AT_GLR_CC_IF])
485 m4_popdef([AT_LALR1_CC_IF])
486 m4_popdef([AT_ERROR_SIMPLE_IF])
487 m4_popdef([AT_ERROR_VERBOSE_IF])
488 m4_popdef([AT_ERROR_DETAILED_IF])
489 m4_popdef([AT_ERROR_CUSTOM_IF])
490 m4_popdef([AT_DEBUG_IF])
491 m4_popdef([AT_HEADER_IF])
492 m4_popdef([AT_AUTOMOVE_IF])
494 ])# AT_BISON_OPTION_POPDEFS
498 ## -------------------------- ##
499 ## Generating Grammar Files. ##
500 ## -------------------------- ##
502 # AT_LANG_CASE(LANG1, IF-LANG1, LANG2, IF-LANG2, ..., DEFAULT)
503 # ------------------------------------------------------------
504 m4_define([AT_LANG_CASE],
505 [m4_case(AT_LANG, $@)])
508 # AT_LANG_MATCH(LANG1, IF-LANG1, LANG2, IF-LANG2, ..., DEFAULT)
509 # ------------------------------------------------------------
510 m4_define([AT_LANG_MATCH],
511 [m4_bmatch(AT_LANG, $@)])
514 # _AT_LANG_DISPATCH(LANG, MACRO, ARGS)
515 # ------------------------------------
516 # Call the specialization of MACRO for LANG with ARGS. Complain if
518 m4_define([_AT_LANG_DISPATCH],
520 [m4_indir([$2($1)], m4_shift2($@))],
521 [m4_fatal([$2: unknown language: $1])])])
524 # AT_LANG_DISPATCH(MACRO, ARGS)
525 # -----------------------------
526 # Call the specialization of MACRO for AT_LANG with ARGS. Complain if
528 m4_define([AT_LANG_DISPATCH],
529 [_AT_LANG_DISPATCH(AT_LANG, $@)])
533 # AT_DATA_SOURCE_PROLOGUE
534 # -----------------------
535 # The prologue that should be included in any source code that is
536 # meant to be compiled. Keep atlocal.in sync (BISON_CXX_WORKS).
537 m4_define([AT_DATA_SOURCE_PROLOGUE],
538 [[/* Adjust to the compiler.
539 We used to do it here, but each time we add a new line,
540 we have to adjust all the line numbers in error messages.
541 It's simpler to use a constant include to a varying file. */
542 #include <testsuite.h>
545 # AT_DATA_GRAMMAR_PROLOGUE
546 # ------------------------
547 # The prologue that should be included in any grammar whose parser is
548 # meant to be compiled.
549 m4_define([AT_DATA_GRAMMAR_PROLOGUE],
550 [[%code top { /* -*- ]AT_LANG[ -*- */
551 ]AT_DATA_SOURCE_PROLOGUE[]dnl
555 # AT_DATA_SOURCE(NAME, CONTENT)
556 # -----------------------------
557 # Generate the file NAME, whose CONTENT is preceded by
558 # AT_DATA_SOURCE_PROLOGUE.
559 m4_define([AT_DATA_SOURCE],
561 [AT_DATA_SOURCE_PROLOGUE
566 # AT_DATA_GRAMMAR(NAME, CONTENT)
567 # ------------------------------
568 # Generate the file NAME, whose CONTENT is preceded by
569 # AT_DATA_GRAMMAR_PROLOGUE.
570 m4_define([AT_DATA_GRAMMAR], [AT_LANG_DISPATCH([$0], $@)])
574 # AT_YYLEX_DECLARE_EXTERN
576 # AT_YYLEX_DEFINE([INPUT], [ACTION])
577 # ----------------------------------
578 # INPUT can be empty, or in double quotes, or a list (in braces).
579 # ACTION may compute yylval for instance, using "res" as token kind,
580 # and "toknum" as the number of calls to yylex (starting at 0).
581 m4_define([AT_YYLEX_PROTOTYPE],
582 [AT_YYLEX_RETURN AT_NAME_PREFIX[]lex (]AT_YYLEX_FORMALS[)[]dnl
585 m4_define([AT_YYLEX_DECLARE_EXTERN],
586 [AT_YYLEX_PROTOTYPE;dnl
589 m4_define([AT_YYLEX_DECLARE],
590 [static AT_YYLEX_DECLARE_EXTERN[]dnl
594 # AT_YYLEX_DEFINE([INPUT], [ACTION])
595 m4_define([AT_YYLEX_DEFINE], [AT_LANG_DISPATCH([$0], $@)])
599 # AT_YYERROR_PROTOTYPE
600 # AT_YYERROR_DECLARE_EXTERN
603 # -------------------------
604 # Must be called inside a AT_BISON_OPTION_PUSHDEFS/POPDEFS pair.
605 m4_define([AT_YYERROR_FORMALS], [AT_LANG_DISPATCH([$0], $@)])
606 m4_define([AT_YYERROR_PROTOTYPE], [AT_LANG_DISPATCH([$0], $@)])
607 m4_define([AT_YYERROR_DECLARE_EXTERN], [AT_LANG_DISPATCH([$0], $@)])
608 m4_define([AT_YYERROR_DECLARE], [AT_LANG_DISPATCH([$0], $@)])
609 m4_define([AT_YYERROR_DEFINE], [AT_LANG_DISPATCH([$0], $@)])
613 m4_define([AT_MAIN_DEFINE], [AT_LANG_DISPATCH([$0], $@)])
621 # AT_DATA_GRAMMAR(c)(NAME, CONTENT)
622 # ---------------------------------
623 # Generate the file NAME, with CONTENT.
624 m4_define([AT_DATA_GRAMMAR(c)],
626 [AT_DATA_GRAMMAR_PROLOGUE
630 # AT_LOCATION_PRINT_DECLARE
631 # -------------------------
632 m4_define([AT_LOCATION_PRINT_DECLARE],
634 #if defined ]AT_YYLTYPE[_IS_TRIVIAL && ]AT_YYLTYPE[_IS_TRIVIAL
635 static int location_print (FILE *yyo, ]AT_YYLTYPE[ const * const yylocp);
636 # ifndef LOCATION_PRINT
637 # define LOCATION_PRINT(File, Loc) location_print (File, &(Loc))
643 # AT_LOCATION_PRINT_DEFINE
644 # ------------------------
645 m4_define([AT_LOCATION_PRINT_DEFINE],
647 # if defined ]AT_YYLTYPE[_IS_TRIVIAL && ]AT_YYLTYPE[_IS_TRIVIAL
648 /* Print *YYLOCP on YYO. */
649 __attribute__((__unused__))
651 location_print (FILE *yyo, ]AT_YYLTYPE[ const * const yylocp)
654 int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
655 if (0 <= yylocp->first_line)
657 res += fprintf (yyo, "%d", yylocp->first_line);
658 if (0 <= yylocp->first_column)
659 res += fprintf (yyo, ".%d", yylocp->first_column);
661 if (0 <= yylocp->last_line)
663 if (yylocp->first_line < yylocp->last_line)
665 res += fprintf (yyo, "-%d", yylocp->last_line);
667 res += fprintf (yyo, ".%d", end_col);
669 else if (0 <= end_col && yylocp->first_column < end_col)
670 res += fprintf (yyo, "-%d", end_col);
678 m4_define([AT_YYERROR_FORMALS(c)],
679 [AT_YYERROR_ARG_LOC_IF([AT_YYLTYPE const * const llocp, ])AT_PARAM_IF([AT_PARSE_PARAMS, ])[const char *msg]])
681 m4_define([AT_YYERROR_PROTOTYPE(c)],
682 [[void ]AT_NAME_PREFIX[error (]AT_YYERROR_FORMALS[)]])
684 m4_define([AT_YYERROR_DECLARE_EXTERN(c)],
685 [AT_YYERROR_PROTOTYPE;])
687 m4_define([AT_YYERROR_DECLARE(c)],
689 ]AT_LOCATION_PRINT_DECLARE[
690 /* !POSIX */ static ]AT_YYERROR_DECLARE_EXTERN[]dnl
693 # "%define parse.error custom" uses a different format, easy to check.
694 # The "verbose" one can be computed from it (see _AT_CHECK_CALC_ERROR).
695 m4_define([AT_YYERROR_DEFINE(c)],
696 [AT_LOCATION_PRINT_DEFINE[
698 ]AT_ERROR_CUSTOM_IF([[
700 yyreport_syntax_error (const yypcontext_t *ctx]AT_PARAM_IF([, AT_PARSE_PARAMS])[)
702 int res = 0;]AT_PARAM_IF([m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
703 [[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\),* *], [
704 YY_USE (\1);])])[]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
706 ++*nerrs;]])[]AT_LOCATION_IF([[
707 LOCATION_PRINT (stderr, *yypcontext_location (ctx));
708 fprintf (stderr, ": ");]])[
709 fprintf (stderr, "syntax error");
711 yysymbol_kind_t la = yypcontext_token (ctx);
712 if (la != YYSYMBOL_YYEMPTY)
713 fprintf (stderr, " on token [%s]", yysymbol_name (la));
716 enum { TOKENMAX = 10 };
717 yysymbol_kind_t expected[TOKENMAX];
718 int n = yypcontext_expected_tokens (ctx, expected, TOKENMAX);
719 /* Forward errors to yyparse. */
724 fprintf (stderr, " (expected:");
725 for (int i = 0; i < n; ++i)
726 fprintf (stderr, " [%s]", yysymbol_name (expected[i]));
727 fprintf (stderr, ")");
730 fprintf (stderr, "\n");
735 /* A C error reporting function. */
737 ]AT_YYERROR_PROTOTYPE[
738 {]m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
739 [[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\),* *], [
741 AT_YYERROR_SEES_LOC_IF([[
742 LOCATION_PRINT (stderr, ]AT_LOC[);
743 fprintf (stderr, ": ");]])[
744 fprintf (stderr, "%s\n", msg);]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
750 m4_define([AT_YYLEX_DEFINE(c)],
751 [[#include <assert.h>
755 ]m4_bmatch([$1], [^\(".*"\)?$],
756 [[static char const input[] = ]m4_default([$1], [""])],
757 [[static int const input[] = ]$1])[;
758 static int toknum = 0;
761 enum { input_elts = sizeof input / sizeof input[0] };
763 assert (0 <= toknum && toknum < input_elts);
764 res = input[toknum++];
765 ]$2[;]AT_TOKEN_CTOR_IF([], [[
767 ]AT_LOC_FIRST_LINE[ = ]AT_LOC_LAST_LINE[ = 1;
768 ]AT_LOC_FIRST_COLUMN[ = ]AT_LOC_LAST_COLUMN[ = toknum;]])[
773 m4_define([AT_MAIN_DEFINE(c)],
774 [[#include <stdlib.h> /* getenv. */
775 #include <string.h> /* strcmp. */
777 main (int argc, char const* argv[])
779 yydebug = !!getenv ("YYDEBUG");
780 for (int i = 1; i < argc; ++i)
781 if (!strcmp (argv[i], "-p")
782 || !strcmp (argv[i], "-d") || !strcmp (argv[i], "--debug"))
784 else if (!strcmp (argv[i], "-s") || !strcmp (argv[i], "--stat"))
788 return ]AT_NAME_PREFIX[parse ();
791 m4_define([AT_LANG_FOR_EACH_STD(c)],
800 # AT_DATA_GRAMMAR(NAME, CONTENT)
801 # ------------------------------
802 m4_copy([AT_DATA_GRAMMAR(c)], [AT_DATA_GRAMMAR(c++)])
804 # No need to declare, it's part of the class interface.
805 m4_define([AT_YYERROR_DECLARE(c++)], [])
806 m4_define([AT_YYERROR_DECLARE_EXTERN(c++)], [])
808 m4_define([AT_YYERROR_DEFINE(c++)],
809 [[/* A C++ error reporting function. */
811 ]AT_NAMESPACE[::]AT_PARSER_CLASS[::error (]AT_LOCATION_IF([[const location_type& l, ]])[const std::string& m)
812 {]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
815 std::cerr << ]AT_LOCATION_IF([l << ": " << ])[m << '\n';
816 }]AT_ERROR_CUSTOM_IF([[
818 ]AT_NAMESPACE[::]AT_PARSER_CLASS[::report_syntax_error (const context& ctx) const
819 {]AT_PARAM_IF([m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
820 [[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\),* *], [
821 YY_USE (\1);])])[]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
823 ++*nerrs;]])[]AT_LOCATION_IF([[
824 std::cerr << ctx.location () << ": ";]])[
825 std::cerr << "syntax error";
826 if (!ctx.lookahead ().empty ())
827 std::cerr << " on token [" << ctx.lookahead ().name () << ']';
829 enum { TOKENMAX = 10 };
830 symbol_kind_type expected[TOKENMAX];
831 int n = ctx.expected_tokens (expected, TOKENMAX);
834 std::cerr << " (expected:";
835 for (int i = 0; i < n; ++i)
836 std::cerr << " [" << symbol_name (expected[i]) << ']';
844 # AT_YYLEX_DEFINE(c++)([INPUT], [ACTION])
845 # ---------------------------------------
847 m4_copy([AT_YYLEX_DEFINE(c)], [AT_YYLEX_DEFINE(c++)])
850 m4_define([AT_MAIN_DEFINE(c++)],
851 [[#include <cstdlib> // getenv.
852 #include <cstring> // strcmp.
854 main (int argc, char const* argv[])
856 ]AT_NAMESPACE[::]AT_PARSER_CLASS[ p;]AT_DEBUG_IF([[
857 int debug = !!getenv ("YYDEBUG");
858 for (int i = 1; i < argc; ++i)
859 if (!strcmp (argv[i], "-p")
860 || !strcmp (argv[i], "-d") || !strcmp (argv[i], "--debug"))
862 else if (!strcmp (argv[i], "-s") || !strcmp (argv[i], "--stat"))
864 p.set_debug_level (debug);]], [[
870 m4_define([AT_LANG_FOR_EACH_STD(c++)],
871 [AT_FOR_EACH_CXX([$1])])
873 m4_define([AT_FOR_EACH_CXX],
874 [[at_for_each_std_CXXFLAGS_save=$CXXFLAGS
876 ${CXX98_CXXFLAGS:+"$CXX98_CXXFLAGS"} \
877 ${CXX03_CXXFLAGS:+"$CXX03_CXXFLAGS"} \
878 ${CXX11_CXXFLAGS:+"$CXX11_CXXFLAGS"} \
879 ${CXX14_CXXFLAGS:+"$CXX14_CXXFLAGS"} \
880 ${CXX17_CXXFLAGS:+"$CXX17_CXXFLAGS"} \
881 ${CXX20_CXXFLAGS:+"$CXX20_CXXFLAGS"} \
882 ${CXX2B_CXXFLAGS:+"$CXX2B_CXXFLAGS"}
884 ]AS_ECHO(["======== Testing with C++ standard flags: '$at_cxx_std'"])[
885 CXXFLAGS="$at_for_each_std_CXXFLAGS_save $at_std"
888 CXXFLAGS=$at_for_each_std_CXXFLAGS_save
897 # AT_DATA_GRAMMAR(NAME, CONTENT)
898 # ------------------------------
899 m4_copy([AT_DATA], [AT_DATA_GRAMMAR(d)])
902 # No need to declare, it's part of the class interface.
903 m4_define([AT_YYERROR_DECLARE(d)], [])
904 m4_define([AT_YYERROR_DECLARE_EXTERN(d)], [])
906 m4_define([AT_YYERROR_DEFINE(d)],
907 [[/* An error reporting function. */
908 public void yyerror (]AT_LOCATION_IF([[const YYLocation l, ]])[string m)
910 stderr.writeln (]AT_LOCATION_IF([[l, ": ", ]])[m);
912 ]AT_ERROR_CUSTOM_IF([[
913 // In the case of D, there are no single quotes around the symbols
914 // so they need to be added here
915 public string transformToken(]AT_API_PREFIX[Parser.SymbolKind token)
918 foreach (i; format("%s", token))
926 return '\'' ~ res ~ '\'';
931 public void reportSyntaxError(]AT_API_PREFIX[Parser.Context ctx)
933 // Buffer and print the message at the end, to avoid being intertwined
934 // with debug traces from getExpectedTokens.
936 msg ~=]AT_LOCATION_IF([[ ctx.getLocation().toString() ~ ": " ~]])[ "syntax error";
938 ]AT_API_PREFIX[Parser.SymbolKind token = ctx.getToken();
939 msg ~= " on token @<:@" ~ transformToken(token) ~ "@:>@";
942 immutable int argmax = 7;
943 ]AT_API_PREFIX[Parser.SymbolKind[] arg = new ]AT_API_PREFIX[Parser.SymbolKind[argmax];
944 int n = ctx.getExpectedTokens(arg, argmax);
947 msg ~= " (expected:";
948 for (int i = 0; i < n; ++i)
949 msg ~= " @<:@" ~ transformToken(arg[i]) ~ "@:>@";
958 # FIXME: this does not work. It is a stub copied blindly from Java.
959 m4_define([AT_YYLEX_DEFINE(d)],
965 public string input = ]$1[;
966 public int index = 0;
968 if (index < input.length())
969 return input.charAt(index++);
973 public Object getLVal() {
974 ]m4_ifval([$2], [$2], [return null])[;
978 m4_define([AT_MAIN_DEFINE(d)],
981 auto l = new ]AT_API_prefix[Lexer ();
982 auto p = new ]AT_API_PREFIX[Parser (l);
993 # AT_DATA_GRAMMAR(NAME, CONTENT)
994 # ------------------------------
995 m4_copy([AT_DATA], [AT_DATA_GRAMMAR(java)])
997 # No need to declare, it's part of the class interface.
998 m4_define([AT_YYERROR_DECLARE(java)], [])
999 m4_define([AT_YYERROR_DECLARE_EXTERN(java)], [])
1001 # AT_JAVA_POSITION_DEFINE
1002 # -----------------------
1003 m4_define([AT_JAVA_POSITION_DEFINE],
1005 public int line = 1;
1006 public int column = 1;
1014 public Position (int l, int t)
1020 public Position (Position p)
1026 public void set (Position p)
1032 public boolean equals (Position l)
1034 return l.line == line && l.column == column;
1037 public String toString ()
1039 return Integer.toString (line) + "." + Integer.toString (column);
1047 public int column ()
1053 class PositionReader extends BufferedReader {
1055 private Position position = new Position ();
1056 private Position previousPosition = new Position ();
1058 public PositionReader (Reader reader) {
1062 public int read () throws IOException {
1063 int res = super.read ();
1064 previousPosition.set (position);
1067 if (c == '\r' || c == '\n') {
1069 position.column = 1;
1071 position.column += 1;
1077 public Position getPosition () {
1081 public Position getPreviousPosition () {
1082 return previousPosition;
1087 # AT_YYERROR_DEFINE(java)
1088 # -----------------------
1089 # FIXME: We should not hard-code "Calc".
1090 m4_define([AT_YYERROR_DEFINE(java)],
1091 [AT_LOCATION_IF([[public void yyerror (Calc.Location l, String m)
1092 {]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
1096 System.err.println(m);
1098 System.err.println(l + ": " + m);
1101 public void yyerror (String m)
1102 {]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
1105 System.err.println (m);
1109 ]AT_ERROR_CUSTOM_IF([[
1110 public void reportSyntaxError(Calc.Context ctx) {
1111 // Buffer and print the message at the end, to avoid being intertwined
1112 // with debug traces from getExpectedTokens.
1113 String msg = ]AT_LOCATION_IF([[ctx.getLocation() + ": " + ]])["syntax error";
1115 Calc.SymbolKind token = ctx.getToken();
1117 msg += " on token @<:@" + token.getName() + "@:>@";
1120 Calc.SymbolKind[] arg = new Calc.SymbolKind[ctx.NTOKENS];
1121 int n = ctx.getExpectedTokens(arg, ctx.NTOKENS);
1123 msg += " (expected:";
1124 for (int i = 0; i < n; ++i)
1125 msg += " @<:@" + arg[i].getName() + "@:>@";
1129 System.err.println(msg);
1134 m4_define([AT_YYLEX_DEFINE(java)],
1140 public String input = ]$1[;
1141 public int index = 0;
1142 public int yylex() {
1143 if (index < input.length())
1144 return input.charAt(index++);
1148 public Object getLVal() {
1149 ]m4_ifval([$2], [$2], [return null])[;
1153 m4_define([AT_MAIN_DEFINE(java)],
1156 public static void main(String[] args) throws IOException {
1157 ]AT_API_prefix[Parser p = new ]AT_API_prefix[Parser();
1158 boolean success = p.parse();
1164 m4_define([AT_LANG_FOR_EACH_STD(java)],
1169 ## --------------- ##
1170 ## Running Bison. ##
1171 ## --------------- ##
1173 # AT_BISON_CHECK(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1174 # -------------------------------------------------
1175 # High-level routine that may call bison several times, under different
1178 # Check Bison by invoking 'bison BISON_ARGS'. BISON_ARGS should not contain
1179 # shell constructs (such as redirection or pipes) that would prevent
1180 # appending additional command-line arguments for bison. OTHER_AT_CHECK_ARGS
1181 # are the usual remaining arguments to AT_CHECK: STATUS, STDOUT, etc.
1183 # This macro or AT_BISON_CHECK_NO_XML should always be used whenever invoking
1184 # Bison in the test suite. For now it ensures that:
1186 # 1. Valgrind doesn't report reachable memory when Bison is expected to have
1187 # a non-zero exit status since Bison doesn't always try to free all memory
1190 # 2. In the case of maintainer-xml-check, XML/XSLT output is compared with
1191 # --graph and --report=all output for every working grammar.
1193 # 3. If stderr contains a warning, -Werror and --warnings=error
1194 # convert the warning to an error.
1196 # 4. If stderr contains a warning, -Wnone and --warnings=none suppress it.
1197 m4_define([AT_BISON_CHECK],
1198 [m4_null_if([$2], [AT_BISON_CHECK_XML($@)])
1199 AT_BISON_CHECK_NO_XML($@)])
1201 # AT_BISON_CHECK_(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1202 # --------------------------------------------------
1203 # Low-level macro to run bison once.
1204 m4_define([AT_BISON_CHECK_],
1205 [AT_CHECK(AT_SET_ENV[[ bison --color=no -fno-caret ]]$@)])
1208 # AT_BISON_CHECK_WARNINGS(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1209 # ----------------------------------------------------------
1210 # Check that warnings (if some are expected) are correctly
1211 # turned into errors with -Werror, etc.
1213 # When -Wno-error is used, the rules are really different, don't try.
1214 m4_define([AT_BISON_CHECK_WARNINGS],
1215 [m4_if(m4_bregexp([$4], [: warning: ]), [-1], [],
1216 m4_bregexp([$1], [-Wno-error=]), [-1],
1217 [m4_null_if([$2], [AT_BISON_CHECK_WARNINGS_($@)])])])
1219 m4_define([AT_BISON_CHECK_WARNINGS_],
1220 [[# Defining POSIXLY_CORRECT causes bison to complain if options are
1221 # added after the grammar file name, so skip these checks in that
1223 if test "$POSIXLY_CORRECT_IS_EXPORTED" = false; then
1224 ]AT_SAVE_SPECIAL_FILES[
1226 # To avoid expanding it repeatedly, store specified stdout.
1227 ]AT_DATA([expout], [$3])[
1230 ]AT_BISON_CHECK_([$1[ -Werror]], [[1]], [expout], [stderr])[
1232 if test x"$PERL" != x; then
1233 # Build expected stderr up to and including the "warnings being
1234 # treated as errors" message.
1235 ]AT_DATA([[experr]], [$4])[
1236 "$PERL" -pi -e 's{(.*): warning:}{$][1: error:};' \
1237 -e 's{\[-W(.*)\]$}{@<:@-Werror=$][1@:>@}' \
1239 ]AT_CHECK([[sed 's,.*/$,,' stderr 1>&2]], [[0]], [[]], [experr])[
1241 # Now check --warnings=error.
1243 ]AT_BISON_CHECK_([$1[ --warnings=error]], [[1]], [expout], [experr])[
1246 # Now check -Wnone and --warnings=none by making sure that
1247 # -Werror doesn't change the exit status when -Wnone or
1248 # --warnings=none is specified. With traces disabled, there should
1249 # be no output on stderr.
1250 ]AT_BISON_CHECK_([$1[ -Wnone,none -Werror --trace=none]], [[0]], [expout])[
1251 ]AT_BISON_CHECK_([$1[ --warnings=none -Werror --trace=none]], [[0]], [expout])[
1253 ]AT_RESTORE_SPECIAL_FILES[
1257 # AT_BISON_CHECK_NO_XML(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1258 # --------------------------------------------------------
1259 # Same as AT_BISON_CHECK except don't perform XML/XSLT checks. This is useful
1260 # when a tortured grammar's XML is known to be too large for xsltproc to
1262 m4_define([AT_BISON_CHECK_NO_XML],
1263 [AT_CHECK(AT_SET_ENV_IF([$2]) [[bison --color=no -fno-caret ]]$@)
1264 AT_BISON_CHECK_WARNINGS($@)])
1266 # AT_BISON_CHECK_XML(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1267 # -----------------------------------------------------
1268 # Run AT_BISON_CHECK's XML/XSLT checks if $BISON_TEST_XML=1 and $XSLTPROC is
1269 # defined. It doesn't make sense to invoke this macro if Bison is expected to
1270 # have a non-zero exit status.
1271 m4_define([AT_BISON_CHECK_XML],
1272 [[if test x"$BISON_TEST_XML" = x1 && test x"$XSLTPROC" != x""; then]
1273 AT_SAVE_SPECIAL_FILES
1275 m4_pushdef([AT_BISON_ARGS],
1276 [m4_bpatsubsts([[$1]],
1277 [--report(-file)?=[^][ ]*], [],
1278 [--graph=[^][ ]*], [],
1279 [--xml=[^][ ]*], [])])dnl
1280 # Don't combine these Bison invocations since we want to be sure that
1281 # --report=all isn't required to get the full XML file.
1282 AT_BISON_CHECK_([[--report=all --report-file=xml-tests/test.output \
1283 --graph=xml-tests/test.gv ]]AT_BISON_ARGS,
1284 [[0]], [ignore], [ignore])
1285 AT_BISON_CHECK_([[--xml=xml-tests/test.xml ]]AT_BISON_ARGS,
1286 [[0]], [ignore], [ignore])
1287 m4_popdef([AT_BISON_ARGS])dnl
1288 [cp xml-tests/test.output expout]
1289 AT_CHECK([[$XSLTPROC \
1290 `]]AT_SET_ENV[[ bison --print-datadir`/xslt/xml2text.xsl \
1291 xml-tests/test.xml]], [[0]], [stdout])
1292 # xml2text and xml2dot always use '•', while --report uses '•' or '.'
1293 # depending on the locale, and the test suite is run with the plain
1295 AT_CHECK([[sed -e 's/•/./g' stdout]], [], [expout])
1296 [sort xml-tests/test.gv > expout]
1297 AT_CHECK([[$XSLTPROC \
1298 `]]AT_SET_ENV[[ bison --print-datadir`/xslt/xml2dot.xsl \
1299 xml-tests/test.xml | sort | sed -e 's/•/./g']],
1301 [rm -rf xml-tests expout]
1302 AT_RESTORE_SPECIAL_FILES
1306 # AT_SET_ENV_IF(EXIT-STATUS)
1307 # --------------------------
1308 # Put this before a Bison invocation to set the environment to:
1309 # - define COLUMNS to make the test suite independent of the user's
1311 # - keep Valgrind from complaining about reachable memory (when
1312 # EXIT-STATUS is not 0).
1314 # Do not quote invocations of this macro within the first argument of AT_CHECK.
1315 # The triple quoting below will cause test cases to fail if you do. If you do
1316 # so anyway but also decrease the quoting below to avoid that problem, AT_CHECK
1317 # will then fail to shell-escape its contents when attempting to print them.
1318 # The testsuite verbose output, at least, will be incorrect, but nothing may
1319 # fail to make sure you notice.
1320 m4_define([AT_SET_ENV_IF],
1321 [[[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; ]]])])
1327 m4_define([AT_SET_ENV],
1328 [AT_SET_ENV_IF([1])])
1330 ## ------------------------ ##
1331 ## Compiling C, C++ Files. ##
1332 ## ------------------------ ##
1335 # AT_COMPILE(OUTPUT, [SOURCES = OUTPUT.c], [EXTRA-COMPILER-FLAGS])
1336 # ----------------------------------------------------------------
1337 # Compile SOURCES into OUTPUT.
1339 # If OUTPUT does not contain '.', assume that we are linking too,
1340 # otherwise pass "-c"; this is a hack. The default SOURCES is OUTPUT
1341 # with trailing .o removed, and ".c" appended.
1342 m4_define([AT_COMPILE],
1343 [AT_SKIP_IF([[! $BISON_C_WORKS]])
1344 AT_CHECK(m4_join([ ],
1345 [$CC $CFLAGS $CPPFLAGS $3],
1346 [m4_bmatch([$1], [[.]], [-c], [$LDFLAGS])],
1348 [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).c])],
1349 [m4_bmatch([$1], [[.]], [], [$LIBS])]),
1350 0, [ignore], [ignore])])
1353 # AT_COMPILE_CXX(OUTPUT, [SOURCES = OUTPUT.cc], [EXTRA-COMPILER-FLAGS])
1354 # ---------------------------------------------------------------------
1355 # Compile SOURCES into OUTPUT. If the C++ compiler does not work,
1358 # If OUTPUT does not contain '.', assume that we are linking too,
1359 # otherwise pass "-c"; this is a hack. The default SOURCES is OUTPUT
1360 # with trailing ".o" removed, and ".cc" appended.
1361 m4_define([AT_COMPILE_CXX],
1363 AT_SKIP_IF([[! $BISON_CXX_WORKS]])
1364 m4_ifdef([AT_GLR2_CC_IF],
1365 [AT_GLR2_CC_IF([AT_SKIP_IF([[test x"$CXX11_CXXFLAGS" == x]])])])
1366 AT_CHECK(m4_join([ ],
1367 [$CXX $CXXFLAGS $CPPFLAGS $3],
1368 [m4_bmatch([$1], [[.]], [-c], [$LDFLAGS])],
1370 [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).cc])],
1371 [m4_bmatch([$1], [[.]], [], [$LIBS])]),
1372 0, [ignore], [ignore])])
1375 # AT_COMPILE_D(OUTPUT, [SOURCES = OUTPUT.d], [EXTRA-COMPILER-FLAGS])
1376 # -------------------------------------------------------------------
1377 # Compile SOURCES into OUTPUT. If the C++ compiler does not work,
1380 # If OUTPUT does not contain '.', assume that we are linking too,
1381 # otherwise pass "-c"; this is a hack. The default SOURCES is OUTPUT
1382 # with trailing ".o" removed, and ".cc" appended.
1383 m4_define([AT_COMPILE_D],
1385 AT_SKIP_IF([[! $BISON_DC_WORKS]])
1386 AT_CHECK(m4_join([ ],
1388 [m4_bmatch([$1], [[.]], [-c])],
1390 [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).d])]),
1391 0, [ignore], [ignore])])
1394 # AT_JAVA_COMPILE(SOURCES)
1395 # ------------------------
1396 # Compile SOURCES into Java class files. Skip the test if java or javac
1398 m4_define([AT_JAVA_COMPILE],
1400 AT_SKIP_IF([[test -z "$CONF_JAVAC"]])
1401 AT_SKIP_IF([[test -z "$CONF_JAVA"]])
1402 AT_CHECK([[$SHELL ../../../javacomp.sh ]$1],
1403 [[0]], [ignore], [ignore])])
1406 # AT_LANG_FOR_EACH_STD(BODY)
1407 # --------------------------
1408 m4_define([AT_LANG_FOR_EACH_STD], [AT_LANG_DISPATCH([$0], $@)])
1411 # AT_LANG_COMPILE(OUTPUT, [SOURCES = OUTPUT.c], [EXTRA-COMPILER-FLAGS])
1412 # ---------------------------------------------------------------------
1413 # Compile SOURCES into OUTPUT. Skip if compiler does not work.
1415 # If OUTPUT does not contain '.', assume that we are linking too,
1416 # otherwise pass "-c"; this is a hack. The default SOURCES is OUTPUT
1417 # with trailing .o removed, and ".c"/".cc" appended.
1418 m4_define([AT_LANG_COMPILE], [AT_LANG_DISPATCH([$0], $@)])
1419 m4_define([AT_LANG_COMPILE(c)], [AT_COMPILE([$1], [$2], [$3])])
1420 m4_define([AT_LANG_COMPILE(c++)], [AT_COMPILE_CXX([$1], [$2], [$3])])
1421 m4_define([AT_LANG_COMPILE(d)], [AT_COMPILE_D([$1], [$2], [$3])])
1422 m4_define([AT_LANG_COMPILE(java)], [AT_JAVA_COMPILE([$1.java], [$2], [$3])])
1427 # The file extension corresponding to the language: c, cc, or java.
1428 m4_define([AT_LANG_EXT], [AT_LANG_DISPATCH([$0], $@)])
1429 m4_define([AT_LANG_EXT(c)], [c])
1430 m4_define([AT_LANG_EXT(c++)], [cc])
1431 m4_define([AT_LANG_EXT(d)], [d])
1432 m4_define([AT_LANG_EXT(java)], [java])
1437 # The header file extension corresponding to the language: c, cc.
1438 m4_define([AT_LANG_HDR], [AT_LANG_DISPATCH([$0], $@)])
1439 m4_define([AT_LANG_HDR(c)], [h])
1440 m4_define([AT_LANG_HDR(c++)], [hh])
1443 # AT_FULL_COMPILE(OUTPUT, [OTHER1], [OTHER2],
1444 # [$4: EXTRA-COMPILER-FLAGS], [$5: EXTRA-BISON-FLAGS])
1445 # --------------------------------------------------------------------
1446 # Compile OUTPUT.y to OUTPUT.c, OUTPUT.cc, or OUTPUT.java, and then
1447 # compile it to OUTPUT or OUTPUT.class. If OTHER is specified, compile
1448 # OUTPUT-OTHER.c, OUTPUT-OTHER.cc, or OUTPUT-OTHER.java to OUTPUT or
1449 # OUTPUT.java along with it. Relies on AT_CXX_IF and
1451 m4_define([AT_FULL_COMPILE],
1452 [AT_BISON_CHECK([$5 -o $1.AT_LANG_EXT $1.y])
1453 AT_LANG_COMPILE([$1],
1456 m4_ifval($2, [[$1-$2.]AT_LANG_EXT]),
1457 m4_ifval($3, [[$1-$3.]AT_LANG_EXT])),
1462 # AT_SKIP_IF_CANNOT_LINK_C_AND_CXX
1463 # --------------------------------
1464 # Check that we can link together C and C++ objects.
1465 m4_define([AT_SKIP_IF_CANNOT_LINK_C_AND_CXX],
1466 [AT_DATA([c-and-cxx.h],
1467 [[#ifdef __cplusplus
1471 int fortytwo (void);
1477 [[#include "c-and-cxx.h"
1481 return fortytwo () == 42 ? 0 : 1;
1484 AT_DATA([cxx-only.cc],
1485 [[#include "c-and-cxx.h"
1491 AT_COMPILE([c-only.o], [c-only.c])
1492 AT_COMPILE_CXX([cxx-only.o], [cxx-only.cc])
1493 AT_CHECK([$CXX $CXXFLAGS $CPPFLAGS $LDFLAGS c-only.o cxx-only.o -o c-and-cxx ||
1494 exit 77], [0], [ignore], [ignore])
1495 AT_C_PARSER_CHECK([c-and-cxx])
1499 # AT_REQUIRE_CXX_STD(STD, [IF-FAIL = SKIP])
1500 # -----------------------------------------
1501 # Skip unless this compiler supports at least C++ STD (e.g., "11",
1503 m4_define([AT_REQUIRE_CXX_STD],
1504 [AT_DATA([check.cc],
1507 #if !defined __cplusplus || __cplusplus < ]m4_case([$1],
1515 [m4_fatal([$0: invalid arguments: $@])])[
1522 AT_COMPILE_CXX([check])
1523 AT_CHECK([@&t@./check], [ignore])
1525 [if test $at_status != 0; then
1528 [AT_SKIP_IF([test $at_status != 0])])
1532 # AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR
1533 # ------------------------------------
1534 # Check that we can expect exceptions to be handled properly.
1535 # GCC 4.3 and 4.4 fail https://trac.macports.org/ticket/40853.
1536 m4_define([AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR],
1537 [AT_DATA_SOURCE([exceptions.cc],
1538 [[#include <iostream>
1539 #include <stdexcept>
1545 throw std::runtime_error ("foo");
1549 std::cerr << "Inner caught\n";
1562 std::cerr << "Outer caught\n";
1568 AT_COMPILE_CXX([exceptions])
1569 # The "empty" quadrigraph is to protect from cfg.mk's sc_at_parser_check.
1570 AT_CHECK([@&t@./exceptions || exit 77], [0], [], [ignore])
1574 ## ---------------------------- ##
1575 ## Running a generated parser. ##
1576 ## ---------------------------- ##
1579 # AT_C_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
1580 # --------------------------------------------------------------
1581 # So that we can run './testsuite PREPARSER='valgrind -q' for instance.
1583 # Get rid of spurious messages when compiled with --coverage:
1584 # +profiling:/[...]/lib/fprintf.gcda:Merge mismatch for summaries
1585 m4_define([AT_C_PARSER_CHECK],
1586 [AT_CHECK([$5 $PREPARSER ./$1], [$2], [$3], [stderr])
1587 AT_CHECK([sed >&2 -e '/^profiling:.*:Merge mismatch for summaries/d' stderr],
1592 # AT_JAVA_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
1593 # -----------------------------------------------------------------
1594 m4_define([AT_JAVA_PARSER_CHECK],
1595 [AT_CHECK([$5[ $SHELL ../../../javaexec.sh ]$1], [$2], [$3], [$4])])
1598 # AT_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
1599 # ------------------------------------------------------------
1600 # If we know we are in Java (via AT_BISON_OPTION_PUSHDEFS/POPDEFS),
1601 # run AT_JAVA_PARSER_CHECK, otherwise AT_C_PARSER_CHECK.
1602 m4_define([AT_PARSER_CHECK],
1603 [m4_ifdef([AT_JAVA_IF],
1604 [AT_JAVA_IF([AT_JAVA_PARSER_CHECK($@)],
1605 [AT_C_PARSER_CHECK($@)])],
1606 [AT_C_PARSER_CHECK($@)])])
1609 # AT_TEST_TABLES_AND_PARSE(TITLE, COND-VALUE, TEST-SPEC,
1610 # DECLS, GRAMMAR, INPUT,
1611 # BISON-STDERR, TABLES-OR-LAST-STATE,
1613 # [PARSER-EXIT-VALUE],
1614 # [PARSER-STDOUT], [PARSER-STDERR])
1615 # -------------------------------------------------------------
1616 # Using TITLE as the test group title, check the generated parser tables
1617 # and parser for a specified grammar file under a condition labeled by
1620 # TEST-SPEC is a comma-delimited list of attributes of this test. Each
1621 # recognized attribute is described below where it is relevant.
1623 # Insert DECLS and GRAMMAR into the declarations and grammar section of
1624 # the grammar file. Insert basic yyerror, yylex, and main function
1625 # definitions as well. Hardcode yylex to return the (possibly empty)
1626 # comma-delimited series of tokens in INPUT followed by token 0.
1628 # If TEST-SPEC contains the attribute no-xml, then invoke bison using
1629 # AT_BISON_CHECK_NO_XML. Otherwise, invoke bison using AT_BISON_CHECK.
1630 # On the bison command-line, specify `--report=all --header'. Check
1631 # that Bison exits with value 0, has no stdout, and has stderr
1634 # If TEST-SPEC contains the attribute 'last-state', check that the value
1635 # of TABLES-OR-LAST-STATE is the index of the last state generated for
1636 # the grammar; in other words, check the number of states (minus one).
1637 # Otherwise, check that everything in the '.output' file starting with
1638 # the definition of state 0 is the same as the entire value of
1639 # TABLES-OR-LAST-STATE.
1641 # Expand the M4 in OTHER-CHECKS to perform additional checks of the
1642 # '.output' file, which is named 'input.output', and/or grammar file,
1643 # which is named 'input.y'.
1645 # Finally, compile the generated parser and then run it using
1646 # AT_PARSER_CHECK with PARSER-EXIT-VALUE, PARSER-STDOUT, and
1647 # PARSER-STDERR as the 2nd-4th arguments.
1649 # As a precondition, you must properly double-quote all arguments that
1650 # are to be interpreted as strings.
1652 # AT_COND_CASE (when appearing in single-quoted segments of arguments)
1653 # invokes m4_case with its own arguments but COND-VALUE inserted as the
1654 # first argument. This is useful, for example, when wrapping multiple
1655 # AT_TEST_TABLES_AND_PARSE invocations, each representing a different
1656 # condition, in another macro.
1660 # # AT_TEST_SYNTAX_ERROR(DESCRIPTION, DECLS, GRAMMAR, INPUT, LAST-STATE,
1661 # # PARSER-EXIT-VALUE, PARSER-STDOUT, PARSER-STDERR)
1662 # # ---------------------------------------------------------------------
1663 # m4_define([AT_TEST_SYNTAX_ERROR],
1665 # AT_TEST_TABLES_AND_PARSE([$1[ with %define parse.error verbose]], [[verbose]],
1667 # [[%define parse.error verbose ]$2], [$3], [$4],
1668 # [[]], [$5], [], [$6], [$7], [$8])
1669 # AT_TEST_TABLES_AND_PARSE([$1[ with no %define parse.error verbose]], [[no verbose]],
1672 # [[]], [$5], [], [$6], [$7], [$8])
1675 # AT_TEST_SYNTAX_ERROR([[Single Char Grammar]],
1676 # [[%token 'b']], [[start: 'a' ;]], [['a', 'b']],
1679 # [AT_COND_CASE([[no verbose]],
1682 # [[syntax error, unexpected 'b', expecting $end
1684 m4_define([AT_TEST_TABLES_AND_PARSE],
1685 [m4_pushdef([AT_COND_CASE], [m4_case([$2], $][@)])
1688 AT_BISON_OPTION_PUSHDEFS([$4])
1689 AT_DATA_GRAMMAR([[input.y]],
1691 ]AT_YYERROR_DECLARE[
1706 static int const input[] = {
1707 ]m4_if([$6], [], [], [$6], [[]], [], [$6[, ]])[0
1709 static int const *inputp = input;
1716 # In some versions of Autoconf, AT_CHECK invokes AS_ESCAPE before
1717 # expanding macros, so it corrupts some special characters in the
1718 # macros. To avoid this, expand now and pass it the result with proper
1719 # string quotation. Assume args 7 through 12 expand to properly quoted
1722 m4_if(m4_index(m4_quote($3), [no-xml]), -1,
1724 [AT_BISON_CHECK_NO_XML])([[-Wall --report=all,no-cex --header -o input.c input.y]],
1725 [0], [], m4_dquote($7))
1727 m4_if(m4_index(m4_quote($3), [last-state]), -1,
1728 [AT_CHECK([[sed -n '/^State 0$/,$p' input.output]], [[0]],
1730 [AT_CHECK([[sed -n 's/^State //p' input.output | tail -1]], [[0]],
1736 # Canonical LR generates very large tables, resulting in very long
1737 # files with #line directives that may overflow what the standards
1738 # (C90 and C++98) guarantee: 32767. In that case, GCC's -pedantic
1739 # will issue an error.
1741 # There is no "" around `wc` since some wc indent the result.
1742 m4_bmatch([$4], [%define lr.type canonical-lr],
1743 [if test 32767 -lt `wc -l < input.c`; then
1744 CFLAGS=`echo " $CFLAGS " | sed -e 's/ -pedantic / /'`
1745 CXXFLAGS=`echo " $CXXFLAGS " | sed -e 's/ -pedantic / /'`
1747 AT_COMPILE([[input]])
1749 AT_PARSER_CHECK([[input]],
1750 m4_ifval([$10], [m4_dquote($10)]),
1751 m4_ifval([$11], [m4_dquote($11)]),
1752 m4_ifval([$12], [m4_dquote($12)]))
1754 AT_BISON_OPTION_POPDEFS
1757 m4_popdef([AT_COND_CASE])])
1761 # AT_SETS_CHECK(INPUT, SETS, EXPECTED)
1762 # ------------------------------------
1763 # Extract the information about the grammar sets from a bison
1764 # trace output (INPUT), and compare to EXPECTED.
1766 # And remember, there is no alternation in portable sed.
1767 m4_define([AT_SETS_CHECK],
1768 [AT_DATA([extract.sed],
1770 ]m4_foreach([m4_Set], [$2], [[
1772 :]m4_substr(m4_Set, 0, 7)[
1775 /^ *$/ !b ]m4_substr(m4_Set, 0, 7)[
1779 AT_CHECK([sed -f extract.sed $1], 0, [$3])
1785 ## ----------------------- ##
1786 ## Launch the test suite. ##
1787 ## ----------------------- ##
1793 # Redefine AT_SETUP to be more concise. Must be done after AT_INIT.
1794 m4_copy_force([AT_SETUP], [B4_SETUP])
1795 m4_define([AT_SETUP],
1796 [B4_SETUP(m4_expand([AT_SETUP_STRIP([[$1]])]))])
1798 # Cannot assign CC and CFLAGS here, since atlocal is loaded after
1799 # options are processed, so we don't know the value of CXX and
1802 # Note that it also means that command line values for CXX and
1803 # CXXFLAGS will not be propagated to CC and CFLAGS.
1804 AT_ARG_OPTION([compile-c-with-cxx],
1805 [compile C parsers with the C++ compiler])