1 # Process this -*- Autotest -*- file with autom4te.
3 # Macros for the GNU Bison Test suite.
5 # Copyright (C) 2003-2015, 2018-2020 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 m4_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 # <http://lists.gnu.org/archive/html/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])
161 ## ------------------------------- ##
162 ## Macros decoding Bison options. ##
163 ## ------------------------------- ##
165 # AT_LOC_PUSHDEF(FIRST-LINE, FIRST-COLUMN, LAST-LINE, LAST-COLUMN)
166 # ----------------------------------------------------------------
167 # Pushdef AT(_LOC)?_(FIRST|LAST)_(LINE|COLUMN).
168 m4_define([AT_LOC_PUSHDEF],
169 [m4_pushdef([AT_FIRST_LINE], [$1])
170 m4_pushdef([AT_FIRST_COLUMN], [$2])
171 m4_pushdef([AT_LAST_LINE], [$3])
172 m4_pushdef([AT_LAST_COLUMN], [$4])
173 m4_pushdef([AT_LOC_FIRST_LINE], [AT_LOC.AT_FIRST_LINE])
174 m4_pushdef([AT_LOC_FIRST_COLUMN], [AT_LOC.AT_FIRST_COLUMN])
175 m4_pushdef([AT_LOC_LAST_LINE], [AT_LOC.AT_LAST_LINE])
176 m4_pushdef([AT_LOC_LAST_COLUMN], [AT_LOC.AT_LAST_COLUMN])])
180 # Popdef AT(_LOC)?_(FIRST|LAST)_(LINE|COLUMN).
181 m4_define([AT_LOC_POPDEF],
182 [m4_popdef([AT_LOC_FIRST_LINE])
183 m4_popdef([AT_LOC_FIRST_COLUMN])
184 m4_popdef([AT_LOC_LAST_LINE])
185 m4_popdef([AT_LOC_LAST_COLUMN])
186 m4_popdef([AT_FIRST_LINE])
187 m4_popdef([AT_FIRST_COLUMN])
188 m4_popdef([AT_LAST_LINE])
189 m4_popdef([AT_LAST_COLUMN])
194 # AT_BISON_OPTION_PUSHDEFS([BISON-OPTIONS])
195 # -----------------------------------------
196 m4_define([AT_BISON_OPTION_PUSHDEFS],
197 [m4_divert_text([KILL],
198 [_AT_BISON_OPTION_PUSHDEFS($[1], $[2], [$1])])])
201 # _AT_BISON_OPTION_PUSHDEFS($1, $2, [BISON-OPTIONS])
202 # --------------------------------------------------
203 # This macro works around the impossibility to define macros
204 # inside macros, because issuing '[$1]' is not possible in M4 :(.
205 # This sucks hard, GNU M4 should really provide M5-like $$1.
206 m4_define([_AT_BISON_OPTION_PUSHDEFS],
207 [m4_pushdef([AT_BISON_OPTIONS], [$3])
209 m4_if([$1$2], $[1]$[2], [],
210 [m4_fatal([$0: invalid arguments: $@])])dnl
211 m4_pushdef([AT_AUTOMOVE_IF],
212 [m4_bmatch([$3], [%define api\.value\.automove], [$1], [$2])])
213 m4_pushdef([AT_DEFINES_IF],
214 [m4_bmatch([$3], [%defines], [$1], [$2])])
215 m4_pushdef([AT_DEBUG_IF],
216 [m4_bmatch([$3], [%debug\|%define parse.trace], [$1], [$2])])
217 m4_pushdef([AT_ERROR_CUSTOM_IF],
218 [m4_bmatch([$3], [%define parse\.error custom], [$1], [$2])])
219 m4_pushdef([AT_ERROR_DETAILED_IF],
220 [m4_bmatch([$3], [%define parse\.error detailed], [$1], [$2])])
221 m4_pushdef([AT_ERROR_VERBOSE_IF],
222 [m4_bmatch([$3], [%define parse\.error verbose], [$1], [$2])])
223 m4_pushdef([AT_ERROR_SIMPLE_IF],
224 [AT_ERROR_CUSTOM_IF([$2], [AT_ERROR_DETAILED_IF([$2], [AT_ERROR_VERBOSE_IF([$2], [$1])], [$1])], [$1])])
225 m4_pushdef([AT_CXX_IF],
226 [m4_bmatch([$3], [%language "[Cc]\+\+"\|%skeleton "[a-z0-9]+\.cc"], [$1], [$2])])
227 m4_pushdef([AT_D_IF],
228 [m4_bmatch([$3], [%language "[Dd]"\|%skeleton "[a-z0-9]+\.d"], [$1], [$2])])
229 m4_pushdef([AT_JAVA_IF],
230 [m4_bmatch([$3], [%language "[Jj][Aa][Vv][Aa]"\|%skeleton "[a-z0-9]+\.java"], [$1], [$2])])
231 # The target language: "c", "c++", or "java".
232 m4_pushdef([AT_LANG],
237 m4_pushdef([AT_C_IF],
238 [m4_if(AT_LANG, [c], [$1], [$2])])
239 m4_pushdef([AT_GLR_IF],
240 [m4_bmatch([$3], [%glr-parser\|%skeleton "glr.*"], [$1], [$2])])
241 m4_pushdef([AT_LALR1_CC_IF],
242 [AT_CXX_IF([AT_GLR_IF([$2], [$1])], [$2])])
243 m4_pushdef([AT_GLR_CC_IF],
244 [AT_CXX_IF([AT_GLR_IF([$1], [$2])], [$2])])
246 m4_pushdef([AT_YACC_IF],
247 [m4_bmatch([$3], [%language\|%glr-parser\|%skeleton], [$2], [$1])])
248 m4_pushdef([AT_LAC_IF],
249 [m4_bmatch([$3], [%define parse.lac full], [$1], [$2])])
250 m4_pushdef([AT_LEXPARAM_IF],
251 [m4_bmatch([$3], [%lex-param], [$1], [$2])])
252 m4_pushdef([AT_LOCATION_IF],
253 [m4_bmatch([$3], [%locations], [$1], [$2])])
254 # Whether we use the Span location type (see calc.at), whose members are
255 # not named like those of the default location type (on purpose, to make
256 # sure we can use a user type).
257 m4_pushdef([AT_LOCATION_TYPE_SPAN_IF],
258 [m4_bmatch([$3], [%define \(api\.location\.type\|location_type\) \{Span\}], [$1], [$2])])
259 m4_pushdef([AT_PARAM_IF],
260 [m4_bmatch([$3], [%parse-param], [$1], [$2])])
262 # Comma-terminated list of formals parse-parameters.
263 # E.g., %parse-param { int x } %parse-param {int y} -> "int x, int y, ".
264 m4_bpatsubst([$3], [%parse-param { *\([^{}]*[^{} ]\) *}{ *\([^{}]*[^{} ]\) *}{ *\([^{}]*[^{} ]\) *}],
265 [m4_append([AT_PARSE_PARAMS], [\1, \2, \3], [, ])])
266 m4_bpatsubst([$3], [%parse-param { *\([^{}]*[^{} ]\) *}{ *\([^{}]*[^{} ]\) *}\([^{].*\)?$],
267 [m4_append([AT_PARSE_PARAMS], [\1, \2], [, ])])
268 m4_rpatsubst([$3], [%parse-param { *\([^{}]*[^{} ]\) *}\([^{}].*\)?$],
269 [m4_append([AT_PARSE_PARAMS], [\1], [, ])\2])
270 m4_ifndef([AT_PARSE_PARAMS],
271 [m4_define([AT_PARSE_PARAMS])])
273 m4_pushdef([AT_PARSER_CLASS],
274 [m4_bmatch([$3], [%define *api\.parser\.class {\([^\}]*\)}],
275 [m4_bregexp([$3], [%define *api\.parser\.class {\([^\}]*\)}], [\1])],
276 [AT_API_PREFIX[]Parser])])
278 m4_pushdef([AT_PURE_IF],
279 [m4_bmatch([$3], [%define *api\.pure\|%pure-parser],
280 [m4_bmatch([$3], [%define *api\.pure *false], [$2], [$1])],
282 m4_pushdef([AT_PUSH_IF],
283 [m4_bmatch([$3], [%define api.push-pull \(both\|pull\)], [$1], [$2])])
285 m4_pushdef([AT_NAME_PREFIX],
286 [m4_bmatch([$3], [\(%define api\.prefix\|%name-prefix\) .*],
288 [\(%define api\.prefix\|%name-prefix\) [\{\"]\([^\"\}]*\)[\"\}]],
290 [AT_JAVA_IF([YY], [yy])])])
291 # AT_NAMESPACE: also consider api.prefix. FIXME: Stop that confusion.
292 m4_pushdef([AT_NAMESPACE],
293 [m4_bmatch([$3], [\(%define api\.\(namespace\|prefix\)\|%name-prefix\) .*],
295 [\(%define api\.\(namespace\|prefix\)\|%name-prefix\) [\{\"]\([^\"\}]*\)[\"\}]],
298 m4_pushdef([AT_TOKEN_CTOR_IF],
299 [m4_bmatch([$3], [%define api\.token\.constructor], [$1], [$2])])
300 m4_pushdef([AT_TOKEN_PREFIX],
301 [m4_bmatch([$3], [%define api\.token\.prefix {.*}],
302 [m4_bregexp([$3], [%define api\.token\.prefix {\(.*\)}], [\1])])])
303 m4_pushdef([AT_TOKEN_RAW_IF],
304 [m4_bmatch([$3], [%define api\.token\.raw], [$1], [$2])])
305 m4_pushdef([AT_VARIANT_IF],
306 [m4_bmatch([$3], [%define api\.value\.type variant], [$1], [$2])])
307 m4_pushdef([AT_API_prefix],
308 [m4_bmatch([$3], [%define api\.prefix {.*}],
309 [m4_bregexp([$3], [%define api\.prefix {\([^\}]*\)}], [\1])],
310 [AT_JAVA_IF([YY], [yy])])])
311 m4_pushdef([AT_API_PREFIX],
312 [m4_toupper(AT_API_prefix)])
313 # yyerror receives the location if %location, and if the parser is pure. For
314 # historical reasons, with the "yacc.c" skeleton, the location is not passed
315 # unless an additional "%parse-param" is present, or if the purity is defined
317 m4_pushdef([AT_YYERROR_ARG_LOC_IF],
318 [AT_LOCATION_IF([AT_PURE_IF([m4_bmatch([$3],
319 m4_quote(m4_join([\|],
320 [%define api\.pure full],
323 [%skeleton "?glr.c"?])),
328 # yyerror always sees the locations (when activated) if the parser is impure.
329 # When the parser is pure, yyerror sees the location if it is received as an
331 m4_pushdef([AT_YYERROR_SEES_LOC_IF],
332 [AT_LOCATION_IF([AT_YACC_IF([AT_PURE_IF([AT_YYERROR_ARG_LOC_IF([$1], [$2])],
337 # The interface is pure: either because %define api.pure, or because we
338 # are using the C++ parsers.
339 m4_pushdef([AT_PURE_LEX_IF],
341 [AT_CXX_IF([$1], [$2])])])
343 m4_pushdef([AT_YYSTYPE],
344 [AT_CXX_IF([AT_NAMESPACE[::parser::semantic_type]],
345 [AT_API_PREFIX[STYPE]])])
346 m4_pushdef([AT_YYLTYPE],
347 [AT_CXX_IF([AT_NAMESPACE[::parser::location_type]],
348 [AT_API_PREFIX[LTYPE]])])
351 [m4_pushdef([AT_LOC], [[(]AT_NAME_PREFIX[lloc)]])
352 m4_pushdef([AT_VAL], [[(]AT_NAME_PREFIX[lval)]])
353 m4_pushdef([AT_YYLEX_FORMALS], [])
354 m4_pushdef([AT_YYLEX_RETURN], [yy::parser::symbol_type])
355 m4_pushdef([AT_YYLEX_ARGS], [])
356 m4_pushdef([AT_USE_LEX_ARGS], [])
357 m4_pushdef([AT_YYLEX_PRE_FORMALS], [])
358 m4_pushdef([AT_YYLEX_PRE_ARGS], [])],
360 [m4_pushdef([AT_LOC], [(*llocp)])
361 m4_pushdef([AT_VAL], [(*lvalp)])
362 m4_pushdef([AT_YYLEX_FORMALS],
363 [AT_YYSTYPE *lvalp[]AT_LOCATION_IF([, AT_YYLTYPE *llocp])])
364 m4_pushdef([AT_YYLEX_RETURN], [int])
365 m4_pushdef([AT_YYLEX_ARGS],
366 [lvalp[]AT_LOCATION_IF([, llocp])])
367 m4_pushdef([AT_USE_LEX_ARGS],
368 [(void) lvalp;AT_LOCATION_IF([(void) llocp;])])
369 m4_pushdef([AT_YYLEX_PRE_FORMALS],
370 [AT_YYLEX_FORMALS, ])
371 m4_pushdef([AT_YYLEX_PRE_ARGS],
374 [m4_pushdef([AT_LOC], [[(]AT_NAME_PREFIX[lloc)]])
375 m4_pushdef([AT_VAL], [[(]AT_NAME_PREFIX[lval)]])
376 m4_pushdef([AT_YYLEX_FORMALS], [void])
377 m4_pushdef([AT_YYLEX_RETURN], [int])
378 m4_pushdef([AT_YYLEX_ARGS], [])
379 m4_pushdef([AT_USE_LEX_ARGS], [])
380 m4_pushdef([AT_YYLEX_PRE_FORMALS], [])
381 m4_pushdef([AT_YYLEX_PRE_ARGS], [])
385 # Handle the different types of location components.
386 AT_LOCATION_TYPE_SPAN_IF(
387 [AT_LOC_PUSHDEF([first.l], [first.c], [last.l], [last.c])],
388 [AT_CXX_IF([AT_LOC_PUSHDEF([begin.line], [begin.column], [end.line], [end.column])],
389 [AT_LOC_PUSHDEF([first_line], [first_column], [last_line], [last_column])])])
392 AT_GLR_IF([AT_KEYWORDS([glr])])
393 AT_PUSH_IF([AT_KEYWORDS([push])])
394 ])# _AT_BISON_OPTION_PUSHDEFS
397 # AT_BISON_OPTION_POPDEFS
398 # -----------------------
399 m4_define([AT_BISON_OPTION_POPDEFS],
400 [m4_divert_text([KILL],
401 [m4_popdef([AT_BISON_OPTIONS])
403 m4_popdef([AT_YYLEX_PRE_ARGS])
404 m4_popdef([AT_YYLEX_PRE_FORMALS])
405 m4_popdef([AT_USE_LEX_ARGS])
406 m4_popdef([AT_YYLEX_ARGS])
407 m4_popdef([AT_YYLEX_FORMALS])
408 m4_popdef([AT_YYLTYPE])
409 m4_popdef([AT_YYSTYPE])
412 m4_popdef([AT_PURE_LEX_IF])
413 m4_popdef([AT_YYERROR_SEES_LOC_IF])
414 m4_popdef([AT_YYERROR_ARG_LOC_IF])
415 m4_popdef([AT_API_PREFIX])
416 m4_popdef([AT_API_prefix])
417 m4_popdef([AT_VARIANT_IF])
418 m4_popdef([AT_TOKEN_RAW_IF])
419 m4_popdef([AT_TOKEN_PREFIX])
420 m4_popdef([AT_TOKEN_CTOR_IF])
421 m4_popdef([AT_NAMESPACE])
422 m4_popdef([AT_NAME_PREFIX])
423 m4_popdef([AT_LOCATION_TYPE_SPAN_IF])
424 m4_popdef([AT_LOCATION_IF])
425 m4_undefine([AT_PARSE_PARAMS])
426 m4_popdef([AT_PUSH_IF])
427 m4_popdef([AT_PURE_IF])
428 m4_popdef([AT_PARSER_CLASS])
429 m4_popdef([AT_PARAM_IF])
430 m4_popdef([AT_LEXPARAM_IF])
431 m4_popdef([AT_LAC_IF])
432 m4_popdef([AT_YACC_IF])
433 m4_popdef([AT_GLR_IF])
434 m4_popdef([AT_CXX_IF])
437 m4_popdef([AT_JAVA_IF])
438 m4_popdef([AT_GLR_CC_IF])
439 m4_popdef([AT_LALR1_CC_IF])
440 m4_popdef([AT_ERROR_SIMPLE_IF])
441 m4_popdef([AT_ERROR_VERBOSE_IF])
442 m4_popdef([AT_ERROR_DETAILED_IF])
443 m4_popdef([AT_ERROR_CUSTOM_IF])
444 m4_popdef([AT_DEBUG_IF])
445 m4_popdef([AT_DEFINES_IF])
446 m4_popdef([AT_AUTOMOVE_IF])
448 ])# AT_BISON_OPTION_POPDEFS
452 ## -------------------------- ##
453 ## Generating Grammar Files. ##
454 ## -------------------------- ##
456 # AT_LANG_CASE(LANG1, IF-LANG1, LANG2, IF-LANG2, ..., DEFAULT)
457 # ------------------------------------------------------------
458 m4_define([AT_LANG_CASE],
459 [m4_case(AT_LANG, $@)])
462 # AT_LANG_MATCH(LANG1, IF-LANG1, LANG2, IF-LANG2, ..., DEFAULT)
463 # ------------------------------------------------------------
464 m4_define([AT_LANG_MATCH],
465 [m4_bmatch(AT_LANG, $@)])
468 # _AT_LANG_DISPATCH(LANG, MACRO, ARGS)
469 # ------------------------------------
470 # Call the specialization of MACRO for LANG with ARGS. Complain if
472 m4_define([_AT_LANG_DISPATCH],
474 [m4_indir([$2($1)], m4_shift2($@))],
475 [m4_fatal([$2: unknown language: $1])])])
478 # AT_LANG_DISPATCH(MACRO, ARGS)
479 # -----------------------------
480 # Call the specialization of MACRO for AT_LANG with ARGS. Complain if
482 m4_define([AT_LANG_DISPATCH],
483 [_AT_LANG_DISPATCH(AT_LANG, $@)])
487 # AT_DATA_SOURCE_PROLOGUE
488 # -----------------------
489 # The prologue that should be included in any source code that is
490 # meant to be compiled. Keep atlocal.in sync (BISON_CXX_WORKS).
491 m4_define([AT_DATA_SOURCE_PROLOGUE],
492 [[/* Adjust to the compiler.
493 We used to do it here, but each time we add a new line,
494 we have to adjust all the line numbers in error messages.
495 It's simpler to use a constant include to a varying file. */
496 #include <testsuite.h>
499 # AT_DATA_GRAMMAR_PROLOGUE
500 # ------------------------
501 # The prologue that should be included in any grammar whose parser is
502 # meant to be compiled.
503 m4_define([AT_DATA_GRAMMAR_PROLOGUE],
504 [[%code top { /* -*- ]AT_LANG[ -*- */
505 ]AT_DATA_SOURCE_PROLOGUE[]dnl
509 # AT_DATA_SOURCE(NAME, CONTENT)
510 # -----------------------------
511 # Generate the file NAME, whose CONTENT is preceded by
512 # AT_DATA_SOURCE_PROLOGUE.
513 m4_define([AT_DATA_SOURCE],
515 [AT_DATA_SOURCE_PROLOGUE
520 # AT_DATA_GRAMMAR(NAME, CONTENT)
521 # ------------------------------
522 # Generate the file NAME, whose CONTENT is preceded by
523 # AT_DATA_GRAMMAR_PROLOGUE.
524 m4_define([AT_DATA_GRAMMAR], [AT_LANG_DISPATCH([$0], $@)])
528 # AT_YYLEX_DECLARE_EXTERN
530 # AT_YYLEX_DEFINE([INPUT], [ACTION])
531 # ----------------------------------
532 # INPUT can be empty, or in double quotes, or a list (in braces).
533 # ACTION may compute yylval for instance, using "res" as token kind,
534 # and "toknum" as the number of calls to yylex (starting at 0).
535 m4_define([AT_YYLEX_PROTOTYPE],
536 [AT_YYLEX_RETURN AT_NAME_PREFIX[]lex (]AT_YYLEX_FORMALS[)[]dnl
539 m4_define([AT_YYLEX_DECLARE_EXTERN],
540 [AT_YYLEX_PROTOTYPE;dnl
543 m4_define([AT_YYLEX_DECLARE],
544 [static AT_YYLEX_DECLARE_EXTERN[]dnl
548 # AT_YYLEX_DEFINE([INPUT], [ACTION])
549 m4_define([AT_YYLEX_DEFINE], [AT_LANG_DISPATCH([$0], $@)])
553 # AT_YYERROR_PROTOTYPE
554 # AT_YYERROR_DECLARE_EXTERN
557 # -------------------------
558 # Must be called inside a AT_BISON_OPTION_PUSHDEFS/POPDEFS pair.
559 m4_define([AT_YYERROR_FORMALS], [AT_LANG_DISPATCH([$0], $@)])
560 m4_define([AT_YYERROR_PROTOTYPE],[AT_LANG_DISPATCH([$0], $@)])
561 m4_define([AT_YYERROR_DECLARE_EXTERN], [AT_LANG_DISPATCH([$0], $@)])
562 m4_define([AT_YYERROR_DECLARE], [AT_LANG_DISPATCH([$0], $@)])
563 m4_define([AT_YYERROR_DEFINE], [AT_LANG_DISPATCH([$0], $@)])
567 m4_define([AT_MAIN_DEFINE], [AT_LANG_DISPATCH([$0], $@)])
575 # AT_DATA_GRAMMAR(c)(NAME, CONTENT)
576 # ---------------------------------
577 # Generate the file NAME, with CONTENT.
578 m4_define([AT_DATA_GRAMMAR(c)],
580 [AT_DATA_GRAMMAR_PROLOGUE
584 m4_define([AT_YYERROR_FORMALS(c)],
585 [AT_YYERROR_ARG_LOC_IF([AT_YYLTYPE const * const llocp, ])AT_PARAM_IF([AT_PARSE_PARAMS, ])[const char *msg]])
587 m4_define([AT_YYERROR_PROTOTYPE(c)],
588 [[void ]AT_NAME_PREFIX[error (]AT_YYERROR_FORMALS[)]])
590 m4_define([AT_YYERROR_DECLARE_EXTERN(c)],
591 [AT_YYERROR_PROTOTYPE;])
593 m4_define([AT_YYERROR_DECLARE(c)],
596 #if defined ]AT_YYLTYPE[_IS_TRIVIAL && ]AT_YYLTYPE[_IS_TRIVIAL
597 static int location_print (FILE *yyo, ]AT_YYLTYPE[ const * const yylocp);
598 # ifndef LOCATION_PRINT
599 # define LOCATION_PRINT(File, Loc) location_print (File, &(Loc))
603 static ]AT_YYERROR_DECLARE_EXTERN])
606 # "%define parse.error custom" uses a different format, easy to check.
607 # The "verbose" one can be computed from it (see _AT_CHECK_CALC_ERROR).
608 m4_define([AT_YYERROR_DEFINE(c)],
610 # if defined ]AT_YYLTYPE[_IS_TRIVIAL && ]AT_YYLTYPE[_IS_TRIVIAL
611 /* Print *YYLOCP on YYO. */
612 __attribute__((__unused__))
614 location_print (FILE *yyo, ]AT_YYLTYPE[ const * const yylocp)
617 int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
618 if (0 <= yylocp->first_line)
620 res += fprintf (yyo, "%d", yylocp->first_line);
621 if (0 <= yylocp->first_column)
622 res += fprintf (yyo, ".%d", yylocp->first_column);
624 if (0 <= yylocp->last_line)
626 if (yylocp->first_line < yylocp->last_line)
628 res += fprintf (yyo, "-%d", yylocp->last_line);
630 res += fprintf (yyo, ".%d", end_col);
632 else if (0 <= end_col && yylocp->first_column < end_col)
633 res += fprintf (yyo, "-%d", end_col);
640 ]AT_ERROR_CUSTOM_IF([[
642 yyreport_syntax_error (const yypcontext_t *ctx]AT_PARAM_IF([, AT_PARSE_PARAMS])[)
644 int res = 0;]AT_PARAM_IF([m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
645 [[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\),* *], [
646 YYUSE (\1);])])[]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs],[[
648 ++*nerrs;]])[]AT_LOCATION_IF([[
649 LOCATION_PRINT (stderr, *yypcontext_location (ctx));
650 fprintf (stderr, ": ");]])[
651 fprintf (stderr, "syntax error");
653 yysymbol_kind_t la = yypcontext_token (ctx);
654 if (la != YYSYMBOL_YYEMPTY)
655 fprintf (stderr, " on token [%s]", yysymbol_name (la));
658 enum { TOKENMAX = 10 };
659 yysymbol_kind_t expected[TOKENMAX];
660 int n = yypcontext_expected_tokens (ctx, expected, TOKENMAX);
661 /* Forward errors to yyparse. */
666 fprintf (stderr, " (expected:");
667 for (int i = 0; i < n; ++i)
668 fprintf (stderr, " [%s]", yysymbol_name (expected[i]));
669 fprintf (stderr, ")");
672 fprintf (stderr, "\n");
677 /* A C error reporting function. */
679 ]AT_YYERROR_PROTOTYPE[
680 {]m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
681 [[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\),* *], [
683 AT_YYERROR_SEES_LOC_IF([[
684 LOCATION_PRINT (stderr, ]AT_LOC[);
685 fprintf (stderr, ": ");]])[
686 fprintf (stderr, "%s\n", msg);]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs],[[
692 m4_define([AT_YYLEX_DEFINE(c)],
693 [[#include <assert.h>
697 ]m4_bmatch([$1], [^\(".*"\)?$],
698 [[static char const input[] = ]m4_default([$1], [""])],
699 [[static int const input[] = ]$1])[;
700 static int toknum = 0;
703 enum { input_elts = sizeof input / sizeof input[0] };
705 assert (0 <= toknum && toknum < input_elts);
706 res = input[toknum++];
707 ]$2[;]AT_TOKEN_CTOR_IF([], [[
709 ]AT_LOC_FIRST_LINE[ = ]AT_LOC_LAST_LINE[ = 1;
710 ]AT_LOC_FIRST_COLUMN[ = ]AT_LOC_LAST_COLUMN[ = toknum;]])[
715 m4_define([AT_MAIN_DEFINE(c)],
716 [[#include <stdlib.h> /* getenv. */
717 #include <string.h> /* strcmp. */
719 main (int argc, char const* argv[])
721 yydebug = !!getenv ("YYDEBUG");
722 for (int i = 1; i < argc; ++i)
723 if (!strcmp (argv[i], "-p")
724 || !strcmp (argv[i], "-d") || !strcmp (argv[i], "--debug"))
726 else if (!strcmp (argv[i], "-s") || !strcmp (argv[i], "--stat"))
730 return ]AT_NAME_PREFIX[parse ();
733 m4_define([AT_LANG_FOR_EACH_STD(c)],
742 # AT_DATA_GRAMMAR(NAME, CONTENT)
743 # ------------------------------
744 m4_copy([AT_DATA_GRAMMAR(c)], [AT_DATA_GRAMMAR(c++)])
746 # No need to declare, it's part of the class interface.
747 m4_define([AT_YYERROR_DECLARE(c++)], [])
748 m4_define([AT_YYERROR_DECLARE_EXTERN(c++)], [])
750 m4_define([AT_YYERROR_DEFINE(c++)],
751 [[/* A C++ error reporting function. */
753 ]AT_NAMESPACE[::parser::error (]AT_LOCATION_IF([[const location_type& l, ]])[const std::string& m)
754 {]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs],[[
757 std::cerr << ]AT_LOCATION_IF([l << ": " << ])[m << '\n';
758 }]AT_ERROR_CUSTOM_IF([[
760 ]AT_NAMESPACE[::parser::report_syntax_error (const context& ctx) const
761 {]AT_PARAM_IF([m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
762 [[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\),* *], [
763 YYUSE (\1);])])[]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs],[[
765 ++*nerrs;]])[]AT_LOCATION_IF([[
766 std::cerr << ctx.location () << ": ";]])[
767 std::cerr << "syntax error";
769 symbol_kind_type la = ctx.token ();
770 if (la != symbol_kind::S_YYEMPTY)
771 std::cerr << " on token [" << symbol_name (la) << ']';
774 enum { TOKENMAX = 10 };
775 symbol_kind_type expected[TOKENMAX];
776 int n = ctx.expected_tokens (expected, TOKENMAX);
779 std::cerr << " (expected:";
780 for (int i = 0; i < n; ++i)
781 std::cerr << " [" << symbol_name (expected[i]) << ']';
789 # AT_YYLEX_DEFINE(c++)([INPUT], [ACTION])
790 # ---------------------------------------
792 m4_copy([AT_YYLEX_DEFINE(c)], [AT_YYLEX_DEFINE(c++)])
795 m4_define([AT_MAIN_DEFINE(c++)],
796 [[#include <cstdlib> // getenv.
797 #include <cstring> // strcmp.
799 main (int argc, char const* argv[])
801 ]AT_NAMESPACE[::parser p;]AT_DEBUG_IF([[
802 int debug = !!getenv ("YYDEBUG");
803 for (int i = 1; i < argc; ++i)
804 if (!strcmp (argv[i], "-p")
805 || !strcmp (argv[i], "-d") || !strcmp (argv[i], "--debug"))
807 else if (!strcmp (argv[i], "-s") || !strcmp (argv[i], "--stat"))
809 p.set_debug_level (debug);]], [[
815 m4_define([AT_LANG_FOR_EACH_STD(c++)],
816 [AT_FOR_EACH_CXX([$1])])
818 m4_define([AT_FOR_EACH_CXX],
819 [[at_for_each_std_CXXFLAGS_save=$CXXFLAGS
821 ${CXX98_CXXFLAGS:+"$CXX98_CXXFLAGS"} \
822 ${CXX03_CXXFLAGS:+"$CXX03_CXXFLAGS"} \
823 ${CXX11_CXXFLAGS:+"$CXX11_CXXFLAGS"} \
824 ${CXX14_CXXFLAGS:+"$CXX14_CXXFLAGS"} \
825 ${CXX17_CXXFLAGS:+"$CXX17_CXXFLAGS"} \
826 ${CXX2A_CXXFLAGS:+"$CXX2A_CXXFLAGS"}
828 ]AS_ECHO(["======== Testing with C++ standard flags: '$at_cxx_std'"])[
829 CXXFLAGS="$at_for_each_std_CXXFLAGS_save $at_std"
832 CXXFLAGS=$at_for_each_std_CXXFLAGS_save
841 # AT_DATA_GRAMMAR(NAME, CONTENT)
842 # ------------------------------
843 m4_copy([AT_DATA], [AT_DATA_GRAMMAR(d)])
846 # No need to declare, it's part of the class interface.
847 m4_define([AT_YYERROR_DECLARE(d)], [])
848 m4_define([AT_YYERROR_DECLARE_EXTERN(d)], [])
850 m4_define([AT_YYERROR_DEFINE(d)],
851 [[/* An error reporting function. */
852 public void yyerror (]AT_LOCATION_IF([[YYLocation l, ]])[string m)
854 stderr.writeln (]AT_LOCATION_IF([[l, ": ", ]])[m);
858 m4_define([AT_MAIN_DEFINE(d)],
861 auto l = new ]AT_API_prefix[Lexer ();
862 auto p = new ]AT_API_PREFIX[Parser (l);
873 # AT_DATA_GRAMMAR(NAME, CONTENT)
874 # ------------------------------
875 m4_copy([AT_DATA], [AT_DATA_GRAMMAR(java)])
877 # No need to declare, it's part of the class interface.
878 m4_define([AT_YYERROR_DECLARE(java)], [])
879 m4_define([AT_YYERROR_DECLARE_EXTERN(java)], [])
881 # AT_JAVA_POSITION_DEFINE
882 # -----------------------
883 m4_define([AT_JAVA_POSITION_DEFINE],
886 public int column = 1;
894 public Position (int l, int t)
900 public Position (Position p)
906 public void set (Position p)
912 public boolean equals (Position l)
914 return l.line == line && l.column == column;
917 public String toString ()
919 return Integer.toString (line) + "." + Integer.toString (column);
933 class PositionReader extends BufferedReader {
935 private Position position = new Position ();
936 private Position previousPosition = new Position ();
938 public PositionReader (Reader reader) {
942 public int read () throws IOException {
943 int res = super.read ();
944 previousPosition.set (position);
947 if (c == '\r' || c == '\n') {
951 position.column += 1;
957 public Position getPosition () {
961 public Position getPreviousPosition () {
962 return previousPosition;
967 # AT_YYERROR_DEFINE(java)
968 # -----------------------
969 # FIXME: We should not hard-code "Calc".
970 m4_define([AT_YYERROR_DEFINE(java)],
971 [AT_LOCATION_IF([[public void yyerror (Calc.Location l, String m)
972 {]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs], [[
976 System.err.println(m);
978 System.err.println(l + ": " + m);
981 public void yyerror (String m)
982 {]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs],[[
985 System.err.println (m);
989 ]AT_ERROR_CUSTOM_IF([[
990 public void reportSyntaxError(Calc.Context ctx) {
991 System.err.print(]AT_LOCATION_IF([[ctx.getLocation() + ": "
992 + ]])["syntax error");
994 Calc.SymbolKind token = ctx.getToken();
996 System.err.print(" on token @<:@" + token.getName() + "@:>@");
999 Calc.SymbolKind[] arg = new Calc.SymbolKind[ctx.NTOKENS];
1000 int n = ctx.getExpectedTokens(arg, ctx.NTOKENS);
1002 System.err.print(" (expected:");
1003 for (int i = 0; i < n; ++i)
1004 System.err.print(" @<:@" + arg[i].getName() + "@:>@");
1005 System.err.print(")");
1008 System.err.println("");
1013 m4_define([AT_YYLEX_DEFINE(java)],
1019 public String input = "]$1[";
1020 public int index = 0;
1023 if (index < input.length ())
1024 return input.charAt (index++);
1028 public Object getLVal ()
1034 m4_define([AT_MAIN_DEFINE(java)],
1037 public static void main (String[] args) throws IOException
1039 ]AT_API_prefix[Parser p = new ]AT_API_prefix[Parser ();
1040 boolean success = p.parse ();
1046 m4_define([AT_LANG_FOR_EACH_STD(java)],
1051 ## --------------- ##
1052 ## Running Bison. ##
1053 ## --------------- ##
1055 # AT_BISON_CHECK(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1056 # -------------------------------------------------
1057 # High-level routine that may call bison several times, under different
1060 # Check Bison by invoking 'bison BISON_ARGS'. BISON_ARGS should not contain
1061 # shell constructs (such as redirection or pipes) that would prevent
1062 # appending additional command-line arguments for bison. OTHER_AT_CHECK_ARGS
1063 # are the usual remaining arguments to AT_CHECK: STATUS, STDOUT, etc.
1065 # This macro or AT_BISON_CHECK_NO_XML should always be used whenever invoking
1066 # Bison in the test suite. For now it ensures that:
1068 # 1. Valgrind doesn't report reachable memory when Bison is expected to have
1069 # a non-zero exit status since Bison doesn't always try to free all memory
1072 # 2. In the case of maintainer-xml-check, XML/XSLT output is compared with
1073 # --graph and --report=all output for every working grammar.
1075 # 3. If stderr contains a warning, -Werror and --warnings=error
1076 # convert the warning to an error.
1078 # 4. If stderr contains a warning, -Wnone and --warnings=none suppress it.
1079 m4_define([AT_BISON_CHECK],
1080 [m4_null_if([$2], [AT_BISON_CHECK_XML($@)])
1081 AT_BISON_CHECK_NO_XML($@)])
1083 # AT_BISON_CHECK_(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1084 # --------------------------------------------------
1085 # Low-level macro to run bison once.
1086 m4_define([AT_BISON_CHECK_],
1087 [AT_CHECK(AT_SET_ENV[[ bison --color=no -fno-caret ]]$@)])
1090 # AT_BISON_CHECK_WARNINGS(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1091 # ----------------------------------------------------------
1092 # Check that warnings (if some are expected) are correctly
1093 # turned into errors with -Werror, etc.
1095 # When -Wno-error is used, the rules are really different, don't try.
1096 m4_define([AT_BISON_CHECK_WARNINGS],
1097 [m4_if(m4_bregexp([$4], [: warning: ]), [-1], [],
1098 m4_bregexp([$1], [-Wno-error=]), [-1],
1099 [m4_null_if([$2], [AT_BISON_CHECK_WARNINGS_($@)])])])
1101 m4_define([AT_BISON_CHECK_WARNINGS_],
1102 [[# Defining POSIXLY_CORRECT causes bison to complain if options are
1103 # added after the grammar file name, so skip these checks in that
1105 if test "$POSIXLY_CORRECT_IS_EXPORTED" = false; then
1106 ]AT_SAVE_SPECIAL_FILES[
1108 # To avoid expanding it repeatedly, store specified stdout.
1109 ]AT_DATA([expout], [$3])[
1112 ]AT_BISON_CHECK_([$1[ -Werror]], [[1]], [expout], [stderr])[
1114 if test x"$PERL" != x; then
1115 # Build expected stderr up to and including the "warnings being
1116 # treated as errors" message.
1117 ]AT_DATA([[experr]], [$4])[
1118 "$PERL" -pi -e 's{(.*): warning:}{$][1: error:};' \
1119 -e 's{\[-W(.*)\]$}{@<:@-Werror=$][1@:>@}' \
1121 ]AT_CHECK([[sed 's,.*/$,,' stderr 1>&2]], [[0]], [[]], [experr])[
1123 # Now check --warnings=error.
1125 ]AT_BISON_CHECK_([$1[ --warnings=error]], [[1]], [expout], [experr])[
1128 # Now check -Wnone and --warnings=none by making sure that
1129 # -Werror doesn't change the exit status when -Wnone or
1130 # --warnings=none is specified. With traces disabled, there should
1131 # be no output on stderr.
1132 ]AT_BISON_CHECK_([$1[ -Wnone,none -Werror --trace=none]], [[0]], [expout])[
1133 ]AT_BISON_CHECK_([$1[ --warnings=none -Werror --trace=none]], [[0]], [expout])[
1135 ]AT_RESTORE_SPECIAL_FILES[
1139 # AT_BISON_CHECK_NO_XML(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1140 # --------------------------------------------------------
1141 # Same as AT_BISON_CHECK except don't perform XML/XSLT checks. This is useful
1142 # when a tortured grammar's XML is known to be too large for xsltproc to
1144 m4_define([AT_BISON_CHECK_NO_XML],
1145 [AT_CHECK(AT_SET_ENV_IF([$2]) [[bison --color=no -fno-caret ]]$@)
1146 AT_BISON_CHECK_WARNINGS($@)])
1148 # AT_BISON_CHECK_XML(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
1149 # -----------------------------------------------------
1150 # Run AT_BISON_CHECK's XML/XSLT checks if $BISON_TEST_XML=1 and $XSLTPROC is
1151 # defined. It doesn't make sense to invoke this macro if Bison is expected to
1152 # have a non-zero exit status.
1153 m4_define([AT_BISON_CHECK_XML],
1154 [[if test x"$BISON_TEST_XML" = x1 && test x"$XSLTPROC" != x""; then]
1155 AT_SAVE_SPECIAL_FILES
1157 m4_pushdef([AT_BISON_ARGS],
1158 [m4_bpatsubsts([[$1]],
1159 [--report(-file)?=[^][ ]*], [],
1160 [--graph=[^][ ]*], [],
1161 [--xml=[^][ ]*], [])])dnl
1162 # Don't combine these Bison invocations since we want to be sure that
1163 # --report=all isn't required to get the full XML file.
1164 AT_BISON_CHECK_([[--report=all --report-file=xml-tests/test.output \
1165 --graph=xml-tests/test.gv ]]AT_BISON_ARGS,
1166 [[0]], [ignore], [ignore])
1167 AT_BISON_CHECK_([[--xml=xml-tests/test.xml ]]AT_BISON_ARGS,
1168 [[0]], [ignore], [ignore])
1169 m4_popdef([AT_BISON_ARGS])dnl
1170 [cp xml-tests/test.output expout]
1171 AT_CHECK([[$XSLTPROC \
1172 `]]AT_SET_ENV[[ bison --print-datadir`/xslt/xml2text.xsl \
1173 xml-tests/test.xml]], [[0]], [stdout])
1174 # xml2text and xml2dot always use '•', while --report uses '•' or '.'
1175 # depending on the locale, and the test suite is run with the plain
1177 AT_CHECK([[sed -e 's/•/./g' stdout]], [], [expout])
1178 [sort xml-tests/test.gv > expout]
1179 AT_CHECK([[$XSLTPROC \
1180 `]]AT_SET_ENV[[ bison --print-datadir`/xslt/xml2dot.xsl \
1181 xml-tests/test.xml | sort | sed -e 's/•/./g']],
1183 [rm -rf xml-tests expout]
1184 AT_RESTORE_SPECIAL_FILES
1188 # AT_SET_ENV_IF(EXIT-STATUS)
1189 # --------------------------
1190 # Put this before a Bison invocation to set the environment to:
1191 # - define COLUMNS to make the test suite independent of the user's
1193 # - keep Valgrind from complaining about reachable memory (when
1194 # EXIT-STATUS is not 0).
1196 # Do not quote invocations of this macro within the first argument of AT_CHECK.
1197 # The triple quoting below will cause test cases to fail if you do. If you do
1198 # so anyway but also decrease the quoting below to avoid that problem, AT_CHECK
1199 # will then fail to shell-escape its contents when attempting to print them.
1200 # The testsuite verbose output, at least, will be incorrect, but nothing may
1201 # fail to make sure you notice.
1202 m4_define([AT_SET_ENV_IF],
1203 [[[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; ]]])])
1209 m4_define([AT_SET_ENV],
1210 [AT_SET_ENV_IF([1])])
1212 ## ------------------------ ##
1213 ## Compiling C, C++ Files. ##
1214 ## ------------------------ ##
1217 # AT_COMPILE(OUTPUT, [SOURCES = OUTPUT.c], [EXTRA-COMPILER-FLAGS])
1218 # ----------------------------------------------------------------
1219 # Compile SOURCES into OUTPUT.
1221 # If OUTPUT does not contain '.', assume that we are linking too,
1222 # otherwise pass "-c"; this is a hack. The default SOURCES is OUTPUT
1223 # with trailing .o removed, and ".c" appended.
1224 m4_define([AT_COMPILE],
1225 [AT_SKIP_IF([[! $BISON_C_WORKS]])
1226 AT_CHECK(m4_join([ ],
1227 [$CC $CFLAGS $CPPFLAGS $3],
1228 [m4_bmatch([$1], [[.]], [-c], [$LDFLAGS])],
1230 [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).c])],
1231 [m4_bmatch([$1], [[.]], [], [$LIBS])]),
1232 0, [ignore], [ignore])])
1235 # AT_COMPILE_CXX(OUTPUT, [SOURCES = OUTPUT.cc], [EXTRA-COMPILER-FLAGS])
1236 # ---------------------------------------------------------------------
1237 # Compile SOURCES into OUTPUT. If the C++ compiler does not work,
1240 # If OUTPUT does not contain '.', assume that we are linking too,
1241 # otherwise pass "-c"; this is a hack. The default SOURCES is OUTPUT
1242 # with trailing ".o" removed, and ".cc" appended.
1243 m4_define([AT_COMPILE_CXX],
1245 AT_SKIP_IF([[! $BISON_CXX_WORKS]])
1246 AT_CHECK(m4_join([ ],
1247 [$CXX $CXXFLAGS $CPPFLAGS $3],
1248 [m4_bmatch([$1], [[.]], [-c], [$LDFLAGS])],
1250 [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).cc])],
1251 [m4_bmatch([$1], [[.]], [], [$LIBS])]),
1252 0, [ignore], [ignore])])
1255 # AT_COMPILE_D(OUTPUT, [SOURCES = OUTPUT.d], [EXTRA-COMPILER-FLAGS])
1256 # -------------------------------------------------------------------
1257 # Compile SOURCES into OUTPUT. If the C++ compiler does not work,
1260 # If OUTPUT does not contain '.', assume that we are linking too,
1261 # otherwise pass "-c"; this is a hack. The default SOURCES is OUTPUT
1262 # with trailing ".o" removed, and ".cc" appended.
1263 m4_define([AT_COMPILE_D],
1265 AT_SKIP_IF([[! $BISON_DC_WORKS]])
1266 AT_CHECK(m4_join([ ],
1268 [m4_bmatch([$1], [[.]], [-c])],
1270 [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).d])]),
1271 0, [ignore], [ignore])])
1274 # AT_JAVA_COMPILE(SOURCES)
1275 # ------------------------
1276 # Compile SOURCES into Java class files. Skip the test if java or javac
1278 m4_define([AT_JAVA_COMPILE],
1280 AT_SKIP_IF([[test -z "$CONF_JAVAC"]])
1281 AT_SKIP_IF([[test -z "$CONF_JAVA"]])
1282 AT_CHECK([[$SHELL ../../../javacomp.sh ]$1],
1283 [[0]], [ignore], [ignore])])
1286 # AT_LANG_FOR_EACH_STD(BODY)
1287 # --------------------------
1288 m4_define([AT_LANG_FOR_EACH_STD], [AT_LANG_DISPATCH([$0], $@)])
1291 # AT_LANG_COMPILE(OUTPUT, [SOURCES = OUTPUT.c], [EXTRA-COMPILER-FLAGS])
1292 # ---------------------------------------------------------------------
1293 # Compile SOURCES into OUTPUT. Skip if compiler does not work.
1295 # If OUTPUT does not contain '.', assume that we are linking too,
1296 # otherwise pass "-c"; this is a hack. The default SOURCES is OUTPUT
1297 # with trailing .o removed, and ".c"/".cc" appended.
1298 m4_define([AT_LANG_COMPILE], [AT_LANG_DISPATCH([$0], $@)])
1299 m4_define([AT_LANG_COMPILE(c)], [AT_COMPILE([$1], [$2], [$3])])
1300 m4_define([AT_LANG_COMPILE(c++)], [AT_COMPILE_CXX([$1], [$2], [$3])])
1301 m4_define([AT_LANG_COMPILE(d)], [AT_COMPILE_D([$1], [$2], [$3])])
1302 m4_define([AT_LANG_COMPILE(java)], [AT_JAVA_COMPILE([$1.java], [$2], [$3])])
1307 # The file extension corresponding to the language: c, cc, or java.
1308 m4_define([AT_LANG_EXT], [AT_LANG_DISPATCH([$0], $@)])
1309 m4_define([AT_LANG_EXT(c)], [c])
1310 m4_define([AT_LANG_EXT(c++)], [cc])
1311 m4_define([AT_LANG_EXT(d)], [d])
1312 m4_define([AT_LANG_EXT(java)], [java])
1317 # The header file extension corresponding to the language: c, cc.
1318 m4_define([AT_LANG_HDR], [AT_LANG_DISPATCH([$0], $@)])
1319 m4_define([AT_LANG_HDR(c)], [h])
1320 m4_define([AT_LANG_HDR(c++)], [hh])
1323 # AT_FULL_COMPILE(OUTPUT, [OTHER1], [OTHER2],
1324 # [$4: EXTRA-COMPILER-FLAGS, [$5: EXTRA-BISON-FLAGS])
1325 # -------------------------------------------------------------------
1326 # Compile OUTPUT.y to OUTPUT.c, OUTPUT.cc, or OUTPUT.java, and then
1327 # compile it to OUTPUT or OUTPUT.class. If OTHER is specified, compile
1328 # OUTPUT-OTHER.c, OUTPUT-OTHER.cc, or OUTPUT-OTHER.java to OUTPUT or
1329 # OUTPUT.java along with it. Relies on AT_CXX_IF and
1331 m4_define([AT_FULL_COMPILE],
1332 [AT_BISON_CHECK([$5 -o $1.AT_LANG_EXT $1.y])
1333 AT_LANG_COMPILE([$1],
1336 m4_ifval($2, [[$1-$2.]AT_LANG_EXT]),
1337 m4_ifval($3, [[$1-$3.]AT_LANG_EXT])),
1342 # AT_SKIP_IF_CANNOT_LINK_C_AND_CXX
1343 # --------------------------------
1344 # Check that we can link together C and C++ objects.
1345 m4_define([AT_SKIP_IF_CANNOT_LINK_C_AND_CXX],
1346 [AT_DATA([c-and-cxx.h],
1347 [[#ifdef __cplusplus
1351 int fortytwo (void);
1357 [[#include "c-and-cxx.h"
1361 return fortytwo () == 42 ? 0 : 1;
1364 AT_DATA([cxx-only.cc],
1365 [[#include "c-and-cxx.h"
1371 AT_COMPILE([c-only.o], [c-only.c])
1372 AT_COMPILE_CXX([cxx-only.o], [cxx-only.cc])
1373 AT_CHECK([$CXX $CXXFLAGS $CPPFLAGS $LDFLAGS c-only.o cxx-only.o -o c-and-cxx ||
1374 exit 77], [0], [ignore], [ignore])
1375 AT_C_PARSER_CHECK([c-and-cxx])
1379 # AT_REQUIRE_CXX_STD(STD, [IF-FAIL = SKIP])
1380 # -----------------------------------------
1381 # Skip unless this compiler supports at least C++ STD (e.g., "11",
1383 m4_define([AT_REQUIRE_CXX_STD],
1384 [AT_DATA([check.cc],
1387 #if !defined __cplusplus || __cplusplus < ]m4_case([$1],
1394 [m4_fatal([$0: invalid arguments: $@])])[
1401 AT_COMPILE_CXX([check])
1402 AT_CHECK([@&t@./check], [ignore])
1404 [if test $at_status != 0; then
1407 [AT_SKIP_IF([test $at_status != 0])])
1411 # AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR
1412 # ------------------------------------
1413 # Check that we can expect exceptions to be handled properly.
1414 # GCC 4.3 and 4.4 fail https://trac.macports.org/ticket/40853.
1415 m4_define([AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR],
1416 [AT_DATA_SOURCE([exceptions.cc],
1417 [[#include <iostream>
1418 #include <stdexcept>
1424 throw std::runtime_error ("foo");
1428 std::cerr << "Inner caught\n";
1441 std::cerr << "Outer caught\n";
1447 AT_COMPILE_CXX([exceptions])
1448 # The "empty" quadrigraph is to protect from cfg.mk's sc_at_parser_check.
1449 AT_CHECK([@&t@./exceptions || exit 77], [0], [], [ignore])
1453 ## ---------------------------- ##
1454 ## Running a generated parser. ##
1455 ## ---------------------------- ##
1458 # AT_C_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
1459 # --------------------------------------------------------------
1460 # So that we can run './testsuite PREPARSER='valgrind -q' for instance.
1462 # Get rid of spurious messages when compiled with --coverage:
1463 # +profiling:/[...]/lib/fprintf.gcda:Merge mismatch for summaries
1464 m4_define([AT_C_PARSER_CHECK],
1465 [AT_CHECK([$5 $PREPARSER ./$1], [$2], [$3], [stderr])
1466 AT_CHECK([sed >&2 -e '/^profiling:.*:Merge mismatch for summaries/d' stderr],
1471 # AT_JAVA_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
1472 # -----------------------------------------------------------------
1473 m4_define([AT_JAVA_PARSER_CHECK],
1474 [AT_CHECK([$5[ $SHELL ../../../javaexec.sh ]$1], [$2], [$3], [$4])])
1477 # AT_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
1478 # ------------------------------------------------------------
1479 # If we know we are in Java (via AT_BISON_OPTION_PUSHDEFS/POPDEFS),
1480 # run AT_JAVA_PARSER_CHECK, otherwise AT_C_PARSER_CHECK.
1481 m4_define([AT_PARSER_CHECK],
1482 [m4_ifdef([AT_JAVA_IF],
1483 [AT_JAVA_IF([AT_JAVA_PARSER_CHECK($@)],
1484 [AT_C_PARSER_CHECK($@)])],
1485 [AT_C_PARSER_CHECK($@)])])
1488 # AT_TEST_TABLES_AND_PARSE(TITLE, COND-VALUE, TEST-SPEC,
1489 # DECLS, GRAMMAR, INPUT,
1490 # BISON-STDERR, TABLES-OR-LAST-STATE,
1492 # [PARSER-EXIT-VALUE],
1493 # [PARSER-STDOUT], [PARSER-STDERR])
1494 # -------------------------------------------------------------
1495 # Using TITLE as the test group title, check the generated parser tables
1496 # and parser for a specified grammar file under a condition labeled by
1499 # TEST-SPEC is a comma-delimited list of attributes of this test. Each
1500 # recognized attribute is described below where it is relevant.
1502 # Insert DECLS and GRAMMAR into the declarations and grammar section of
1503 # the grammar file. Insert basic yyerror, yylex, and main function
1504 # definitions as well. Hardcode yylex to return the (possibly empty)
1505 # comma-delimited series of tokens in INPUT followed by token 0.
1507 # If TEST-SPEC contains the attribute no-xml, then invoke bison using
1508 # AT_BISON_CHECK_NO_XML. Otherwise, invoke bison using AT_BISON_CHECK.
1509 # On the bison command-line, specify `--report=all --defines'. Check
1510 # that Bison exits with value 0, has no stdout, and has stderr
1513 # If TEST-SPEC contains the attribute 'last-state', check that the value
1514 # of TABLES-OR-LAST-STATE is the index of the last state generated for
1515 # the grammar; in other words, check the number of states (minus one).
1516 # Otherwise, check that everything in the '.output' file starting with
1517 # the definition of state 0 is the same as the entire value of
1518 # TABLES-OR-LAST-STATE.
1520 # Expand the M4 in OTHER-CHECKS to perform additional checks of the
1521 # '.output' file, which is named 'input.output', and/or grammar file,
1522 # which is named 'input.y'.
1524 # Finally, compile the generated parser and then run it using
1525 # AT_PARSER_CHECK with PARSER-EXIT-VALUE, PARSER-STDOUT, and
1526 # PARSER-STDERR as the 2nd-4th arguments.
1528 # As a precondition, you must properly double-quote all arguments that
1529 # are to be interpreted as strings.
1531 # AT_COND_CASE (when appearing in single-quoted segments of arguments)
1532 # invokes m4_case with its own arguments but COND-VALUE inserted as the
1533 # first argument. This is useful, for example, when wrapping multiple
1534 # AT_TEST_TABLES_AND_PARSE invocations, each representing a different
1535 # condition, in another macro.
1539 # # AT_TEST_SYNTAX_ERROR(DESCRIPTION, DECLS, GRAMMAR, INPUT, LAST-STATE,
1540 # # PARSER-EXIT-VALUE, PARSER-STDOUT, PARSER-STDERR)
1541 # # ---------------------------------------------------------------------
1542 # m4_define([AT_TEST_SYNTAX_ERROR],
1544 # AT_TEST_TABLES_AND_PARSE([$1[ with %define parse.error verbose]], [[verbose]],
1546 # [[%define parse.error verbose ]$2], [$3], [$4],
1547 # [[]], [$5], [], [$6], [$7], [$8])
1548 # AT_TEST_TABLES_AND_PARSE([$1[ with no %define parse.error verbose]], [[no verbose]],
1551 # [[]], [$5], [], [$6], [$7], [$8])
1554 # AT_TEST_SYNTAX_ERROR([[Single Char Grammar]],
1555 # [[%token 'b']], [[start: 'a' ;]], [['a', 'b']],
1558 # [AT_COND_CASE([[no verbose]],
1561 # [[syntax error, unexpected 'b', expecting $end
1563 m4_define([AT_TEST_TABLES_AND_PARSE],
1564 [m4_pushdef([AT_COND_CASE], [m4_case([$2], $][@)])
1567 AT_BISON_OPTION_PUSHDEFS([$4])
1568 AT_DATA_GRAMMAR([[input.y]],
1570 ]AT_YYERROR_DECLARE[
1585 static int const input[] = {
1586 ]m4_if([$6], [], [], [$6], [[]], [], [$6[, ]])[0
1588 static int const *inputp = input;
1595 # In some versions of Autoconf, AT_CHECK invokes AS_ESCAPE before
1596 # expanding macros, so it corrupts some special characters in the
1597 # macros. To avoid this, expand now and pass it the result with proper
1598 # string quotation. Assume args 7 through 12 expand to properly quoted
1601 m4_if(m4_index(m4_quote($3), [no-xml]), -1,
1603 [AT_BISON_CHECK_NO_XML])([[-Wall --report=all,no-cex --defines -o input.c input.y]],
1604 [0], [], m4_dquote($7))
1606 m4_if(m4_index(m4_quote($3), [last-state]), -1,
1607 [AT_CHECK([[sed -n '/^State 0$/,$p' input.output]], [[0]],
1609 [AT_CHECK([[sed -n 's/^State //p' input.output | tail -1]], [[0]],
1615 # Canonical LR generates very large tables, resulting in very long
1616 # files with #line directives that may overflow what the standards
1617 # (C90 and C++98) guarantee: 32767. In that case, GCC's -pedantic
1618 # will issue an error.
1620 # There is no "" around `wc` since some wc indent the result.
1621 m4_bmatch([$4], [%define lr.type canonical-lr],
1622 [if test 32767 -lt `wc -l < input.c`; then
1623 CFLAGS=`echo " $CFLAGS " | sed -e 's/ -pedantic / /'`
1624 CXXFLAGS=`echo " $CXXFLAGS " | sed -e 's/ -pedantic / /'`
1626 AT_COMPILE([[input]])
1628 AT_PARSER_CHECK([[input]],
1629 m4_ifval([$10], [m4_dquote($10)]),
1630 m4_ifval([$11], [m4_dquote($11)]),
1631 m4_ifval([$12], [m4_dquote($12)]))
1633 AT_BISON_OPTION_POPDEFS
1636 m4_popdef([AT_COND_CASE])])
1640 # AT_SETS_CHECK(INPUT, SETS, EXPECTED)
1641 # ------------------------------------
1642 # Extract the information about the grammar sets from a bison
1643 # trace output (INPUT), and compare to EXPECTED.
1645 # And remember, there is no alternation in portable sed.
1646 m4_define([AT_SETS_CHECK],
1647 [AT_DATA([extract.sed],
1649 ]m4_foreach([m4_Set], [$2], [[
1651 :]m4_substr(m4_Set, 0, 7)[
1654 /^ *$/ !b ]m4_substr(m4_Set, 0, 7)[
1658 AT_CHECK([sed -f extract.sed $1], 0, [$3])
1664 ## ----------------------- ##
1665 ## Launch the test suite. ##
1666 ## ----------------------- ##
1672 # Redefine AT_SETUP to be more concise. Must be done after AT_INIT.
1673 m4_copy_force([AT_SETUP], [B4_SETUP])
1674 m4_define([AT_SETUP],
1675 [B4_SETUP(m4_expand([AT_SETUP_STRIP([[$1]])]))])
1677 # Cannot assign CC and CFLAGS here, since atlocal is loaded after
1678 # options are processed, so we don't know the value of CXX and
1681 # Note that it also means that command line values for CXX and
1682 # CXXFLAGS will not be propagated to CC and CFLAGS.
1683 AT_ARG_OPTION([compile-c-with-cxx],
1684 [compile C parsers with the C++ compiler])