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])])
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])],
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])])
307 m4_pushdef([AT_NAME_PREFIX],
308 [m4_bmatch([$3], [\(%define api\.prefix\|%name-prefix\) .*],
310 [\(%define api\.prefix\|%name-prefix\) [\{\"]\([^\"\}]*\)[\"\}]],
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\) .*],
317 [\(%define api\.\(namespace\|prefix\)\|%name-prefix\) [\{\"]\([^\"\}]*\)[\"\}]],
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
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],
349 [%skeleton "?glr.c"?])),
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
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])],
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],
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]])])
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], [])],
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],
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], [])
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])
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])
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])
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
514 m4_define([_AT_LANG_DISPATCH],
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
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],
557 [AT_DATA_SOURCE_PROLOGUE
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], $@)])
570 # AT_YYLEX_DECLARE_EXTERN
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], $@)])
595 # AT_YYERROR_PROTOTYPE
596 # AT_YYERROR_DECLARE_EXTERN
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], $@)])
609 m4_define([AT_MAIN_DEFINE], [AT_LANG_DISPATCH([$0], $@)])
617 # AT_DATA_GRAMMAR(c)(NAME, CONTENT)
618 # ---------------------------------
619 # Generate the file NAME, with CONTENT.
620 m4_define([AT_DATA_GRAMMAR(c)],
622 [AT_DATA_GRAMMAR_PROLOGUE
626 # AT_LOCATION_PRINT_DECLARE
627 # -------------------------
628 m4_define([AT_LOCATION_PRINT_DECLARE],
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))
639 # AT_LOCATION_PRINT_DEFINE
640 # ------------------------
641 m4_define([AT_LOCATION_PRINT_DEFINE],
643 # if defined ]AT_YYLTYPE[_IS_TRIVIAL && ]AT_YYLTYPE[_IS_TRIVIAL
644 /* Print *YYLOCP on YYO. */
645 __attribute__((__unused__))
647 location_print (FILE *yyo, ]AT_YYLTYPE[ const * const yylocp)
650 int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
651 if (0 <= yylocp->first_line)
653 res += fprintf (yyo, "%d", yylocp->first_line);
654 if (0 <= yylocp->first_column)
655 res += fprintf (yyo, ".%d", yylocp->first_column);
657 if (0 <= yylocp->last_line)
659 if (yylocp->first_line < yylocp->last_line)
661 res += fprintf (yyo, "-%d", yylocp->last_line);
663 res += fprintf (yyo, ".%d", end_col);
665 else if (0 <= end_col && yylocp->first_column < end_col)
666 res += fprintf (yyo, "-%d", end_col);
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)],
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], [[
702 ++*nerrs;]])[]AT_LOCATION_IF([[
703 LOCATION_PRINT (stderr, *yypcontext_location (ctx));
704 fprintf (stderr, ": ");]])[
705 fprintf (stderr, "syntax error");
707 yysymbol_kind_t la = yypcontext_token (ctx);
708 if (la != YYSYMBOL_YYEMPTY)
709 fprintf (stderr, " on token [%s]", yysymbol_name (la));
712 enum { TOKENMAX = 10 };
713 yysymbol_kind_t expected[TOKENMAX];
714 int n = yypcontext_expected_tokens (ctx, expected, TOKENMAX);
715 /* Forward errors to yyparse. */
720 fprintf (stderr, " (expected:");
721 for (int i = 0; i < n; ++i)
722 fprintf (stderr, " [%s]", yysymbol_name (expected[i]));
723 fprintf (stderr, ")");
726 fprintf (stderr, "\n");
731 /* A C error reporting function. */
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]*\),* *], [
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], [[
746 m4_define([AT_YYLEX_DEFINE(c)],
747 [[#include <assert.h>
751 ]m4_bmatch([$1], [^\(".*"\)?$],
752 [[static char const input[] = ]m4_default([$1], [""])],
753 [[static int const input[] = ]$1])[;
754 static int toknum = 0;
757 enum { input_elts = sizeof input / sizeof input[0] };
759 assert (0 <= toknum && toknum < input_elts);
760 res = input[toknum++];
761 ]$2[;]AT_TOKEN_CTOR_IF([], [[
763 ]AT_LOC_FIRST_LINE[ = ]AT_LOC_LAST_LINE[ = 1;
764 ]AT_LOC_FIRST_COLUMN[ = ]AT_LOC_LAST_COLUMN[ = toknum;]])[
769 m4_define([AT_MAIN_DEFINE(c)],
770 [[#include <stdlib.h> /* getenv. */
771 #include <string.h> /* strcmp. */
773 main (int argc, char const* argv[])
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"))
780 else if (!strcmp (argv[i], "-s") || !strcmp (argv[i], "--stat"))
784 return ]AT_NAME_PREFIX[parse ();
787 m4_define([AT_LANG_FOR_EACH_STD(c)],
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. */
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], [[
811 std::cerr << ]AT_LOCATION_IF([l << ": " << ])[m << '\n';
812 }]AT_ERROR_CUSTOM_IF([[
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], [[
819 ++*nerrs;]])[]AT_LOCATION_IF([[
820 std::cerr << ctx.location () << ": ";]])[
821 std::cerr << "syntax error";
823 symbol_kind_type la = ctx.token ();
824 if (la != symbol_kind::S_YYEMPTY)
825 std::cerr << " on token [" << symbol_name (la) << ']';
828 enum { TOKENMAX = 10 };
829 symbol_kind_type expected[TOKENMAX];
830 int n = ctx.expected_tokens (expected, TOKENMAX);
833 std::cerr << " (expected:";
834 for (int i = 0; i < n; ++i)
835 std::cerr << " [" << symbol_name (expected[i]) << ']';
843 # AT_YYLEX_DEFINE(c++)([INPUT], [ACTION])
844 # ---------------------------------------
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"))
861 else if (!strcmp (argv[i], "-s") || !strcmp (argv[i], "--stat"))
863 p.set_debug_level (debug);]], [[
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
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"
886 CXXFLAGS=$at_for_each_std_CXXFLAGS_save
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)
916 foreach (i; format("%s", token))
924 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.
934 msg ~=]AT_LOCATION_IF([[ ctx.getLocation().toString() ~ ": " ~]])[ "syntax error";
936 ]AT_API_PREFIX[Parser.SymbolKind token = ctx.getToken();
937 msg ~= " on token @<:@" ~ transformToken(token) ~ "@:>@";
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);
945 msg ~= " (expected:";
946 for (int i = 0; i < n; ++i)
947 msg ~= " @<:@" ~ transformToken(arg[i]) ~ "@:>@";
956 # FIXME: this does not work. It is a stub copied blindly from Java.
957 m4_define([AT_YYLEX_DEFINE(d)],
963 public string input = ]$1[;
964 public int index = 0;
966 if (index < input.length())
967 return input.charAt(index++);
971 public Object getLVal() {
972 ]m4_ifval([$2], [$2], [return null])[;
976 m4_define([AT_MAIN_DEFINE(d)],
979 auto l = new ]AT_API_prefix[Lexer ();
980 auto p = new ]AT_API_PREFIX[Parser (l);
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],
1003 public int line = 1;
1004 public int column = 1;
1012 public Position (int l, int t)
1018 public Position (Position p)
1024 public void set (Position p)
1030 public boolean equals (Position l)
1032 return l.line == line && l.column == column;
1035 public String toString ()
1037 return Integer.toString (line) + "." + Integer.toString (column);
1045 public int column ()
1051 class PositionReader extends BufferedReader {
1053 private Position position = new Position ();
1054 private Position previousPosition = new Position ();
1056 public PositionReader (Reader reader) {
1060 public int read () throws IOException {
1061 int res = super.read ();
1062 previousPosition.set (position);
1065 if (c == '\r' || c == '\n') {
1067 position.column = 1;
1069 position.column += 1;
1075 public Position getPosition () {
1079 public Position getPreviousPosition () {
1080 return previousPosition;
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], [[
1094 System.err.println(m);
1096 System.err.println(l + ": " + m);
1099 public void yyerror (String m)
1100 {]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
1103 System.err.println (m);
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";
1113 Calc.SymbolKind token = ctx.getToken();
1115 msg += " on token @<:@" + token.getName() + "@:>@";
1118 Calc.SymbolKind[] arg = new Calc.SymbolKind[ctx.NTOKENS];
1119 int n = ctx.getExpectedTokens(arg, ctx.NTOKENS);
1121 msg += " (expected:";
1122 for (int i = 0; i < n; ++i)
1123 msg += " @<:@" + arg[i].getName() + "@:>@";
1127 System.err.println(msg);
1132 m4_define([AT_YYLEX_DEFINE(java)],
1138 public String input = ]$1[;
1139 public int index = 0;
1140 public int yylex() {
1141 if (index < input.length())
1142 return input.charAt(index++);
1146 public Object getLVal() {
1147 ]m4_ifval([$2], [$2], [return null])[;
1151 m4_define([AT_MAIN_DEFINE(java)],
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();
1162 m4_define([AT_LANG_FOR_EACH_STD(java)],
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
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
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
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])[
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@:>@}' \
1237 ]AT_CHECK([[sed 's,.*/$,,' stderr 1>&2]], [[0]], [[]], [experr])[
1239 # Now check --warnings=error.
1241 ]AT_BISON_CHECK_([$1[ --warnings=error]], [[1]], [expout], [experr])[
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[
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
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
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
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']],
1299 [rm -rf xml-tests expout]
1300 AT_RESTORE_SPECIAL_FILES
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
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; ]]])])
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])],
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,
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],
1361 AT_SKIP_IF([[! $BISON_CXX_WORKS]])
1362 AT_CHECK(m4_join([ ],
1363 [$CXX $CXXFLAGS $CPPFLAGS $3],
1364 [m4_bmatch([$1], [[.]], [-c], [$LDFLAGS])],
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,
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],
1381 AT_SKIP_IF([[! $BISON_DC_WORKS]])
1382 AT_CHECK(m4_join([ ],
1384 [m4_bmatch([$1], [[.]], [-c])],
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
1394 m4_define([AT_JAVA_COMPILE],
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])])
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])
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
1447 m4_define([AT_FULL_COMPILE],
1448 [AT_BISON_CHECK([$5 -o $1.AT_LANG_EXT $1.y])
1449 AT_LANG_COMPILE([$1],
1452 m4_ifval($2, [[$1-$2.]AT_LANG_EXT]),
1453 m4_ifval($3, [[$1-$3.]AT_LANG_EXT])),
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
1467 int fortytwo (void);
1473 [[#include "c-and-cxx.h"
1477 return fortytwo () == 42 ? 0 : 1;
1480 AT_DATA([cxx-only.cc],
1481 [[#include "c-and-cxx.h"
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",
1499 m4_define([AT_REQUIRE_CXX_STD],
1500 [AT_DATA([check.cc],
1503 #if !defined __cplusplus || __cplusplus < ]m4_case([$1],
1510 [m4_fatal([$0: invalid arguments: $@])])[
1517 AT_COMPILE_CXX([check])
1518 AT_CHECK([@&t@./check], [ignore])
1520 [if test $at_status != 0; then
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>
1540 throw std::runtime_error ("foo");
1544 std::cerr << "Inner caught\n";
1557 std::cerr << "Outer caught\n";
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],
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,
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
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
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.
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],
1660 # AT_TEST_TABLES_AND_PARSE([$1[ with %define parse.error verbose]], [[verbose]],
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]],
1667 # [[]], [$5], [], [$6], [$7], [$8])
1670 # AT_TEST_SYNTAX_ERROR([[Single Char Grammar]],
1671 # [[%token 'b']], [[start: 'a' ;]], [['a', 'b']],
1674 # [AT_COND_CASE([[no verbose]],
1677 # [[syntax error, unexpected 'b', expecting $end
1679 m4_define([AT_TEST_TABLES_AND_PARSE],
1680 [m4_pushdef([AT_COND_CASE], [m4_case([$2], $][@)])
1683 AT_BISON_OPTION_PUSHDEFS([$4])
1684 AT_DATA_GRAMMAR([[input.y]],
1686 ]AT_YYERROR_DECLARE[
1701 static int const input[] = {
1702 ]m4_if([$6], [], [], [$6], [[]], [], [$6[, ]])[0
1704 static int const *inputp = input;
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
1717 m4_if(m4_index(m4_quote($3), [no-xml]), -1,
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]],
1725 [AT_CHECK([[sed -n 's/^State //p' input.output | tail -1]], [[0]],
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 / /'`
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
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],
1765 ]m4_foreach([m4_Set], [$2], [[
1767 :]m4_substr(m4_Set, 0, 7)[
1770 /^ *$/ !b ]m4_substr(m4_Set, 0, 7)[
1774 AT_CHECK([sed -f extract.sed $1], 0, [$3])
1780 ## ----------------------- ##
1781 ## Launch the test suite. ##
1782 ## ----------------------- ##
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
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])