]>
Commit | Line | Data |
---|---|---|
08af01c2 | 1 | -*- Autoconf -*- |
90b9908d PB |
2 | |
3 | # Language-independent M4 Macros for Bison. | |
e141f4d4 | 4 | # Copyright (C) 2002, 2004-2010 Free Software Foundation, Inc. |
90b9908d | 5 | |
f16b0819 | 6 | # This program is free software: you can redistribute it and/or modify |
90b9908d | 7 | # it under the terms of the GNU General Public License as published by |
f16b0819 | 8 | # the Free Software Foundation, either version 3 of the License, or |
90b9908d | 9 | # (at your option) any later version. |
f16b0819 | 10 | # |
90b9908d PB |
11 | # This program is distributed in the hope that it will be useful, |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. | |
f16b0819 | 15 | # |
90b9908d | 16 | # You should have received a copy of the GNU General Public License |
f16b0819 | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
90b9908d PB |
18 | |
19 | ||
20 | ## ---------------- ## | |
21 | ## Identification. ## | |
22 | ## ---------------- ## | |
23 | ||
a9ce3f54 AD |
24 | # b4_copyright(TITLE, [YEARS]) |
25 | # ---------------------------- | |
26 | # If YEARS are not defined, use b4_copyright_years. | |
90b9908d PB |
27 | m4_define([b4_copyright], |
28 | [b4_comment([A Bison parser, made by GNU Bison b4_version.]) | |
29 | ||
30 | b4_comment([$1 | |
31 | ||
a9ce3f54 AD |
32 | ]m4_dquote(m4_text_wrap([Copyright (C) |
33 | ]m4_ifval([$2], [[$2]], [m4_defn([b4_copyright_years])])[ | |
34 | Free Software Foundation, Inc.]))[ | |
90b9908d | 35 | |
f16b0819 | 36 | This program is free software: you can redistribute it and/or modify |
90b9908d | 37 | it under the terms of the GNU General Public License as published by |
f16b0819 PE |
38 | the Free Software Foundation, either version 3 of the License, or |
39 | (at your option) any later version. | |
90b9908d PB |
40 | |
41 | This program is distributed in the hope that it will be useful, | |
42 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
43 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
44 | GNU General Public License for more details. | |
45 | ||
46 | You should have received a copy of the GNU General Public License | |
f16b0819 | 47 | along with this program. If not, see <http://www.gnu.org/licenses/>.]) |
90b9908d PB |
48 | |
49 | b4_comment([As a special exception, you may create a larger work that contains | |
50 | part or all of the Bison parser skeleton and distribute that work | |
51 | under terms of your choice, so long as that work isn't itself a | |
52 | parser generator using the skeleton or a modified version thereof | |
53 | as a parser skeleton. Alternatively, if you modify or redistribute | |
54 | the parser skeleton itself, you may (at your option) remove this | |
55 | special exception, which will cause the skeleton and the resulting | |
56 | Bison output files to be licensed under the GNU General Public | |
57 | License without this special exception. | |
58 | ||
59 | This special exception was added by the Free Software Foundation in | |
60 | version 2.2 of Bison.])]) | |
61 | ||
62 | ||
08af01c2 JD |
63 | ## ---------------- ## |
64 | ## Error handling. ## | |
65 | ## ---------------- ## | |
66 | ||
7dc4a694 JD |
67 | # The following error handling macros print error directives that should not |
68 | # become arguments of other macro invocations since they would likely then be | |
69 | # mangled. Thus, they print to stdout directly. | |
70 | ||
71 | # b4_cat(TEXT) | |
72 | # ------------ | |
73 | # Write TEXT to stdout. Precede the final newline with an @ so that it's | |
74 | # escaped. For example: | |
75 | # | |
76 | # b4_cat([[@complain(invalid input@)]]) | |
77 | m4_define([b4_cat], | |
78 | [m4_syscmd([cat <<'_m4eof' | |
79 | ]m4_bpatsubst(m4_dquote($1), [_m4eof], [_m4@`eof])[@ | |
66f0441d JD |
80 | _m4eof |
81 | ])dnl | |
7dc4a694 JD |
82 | m4_if(m4_sysval, [0], [], [m4_fatal([$0: cannot write to stdout])])]) |
83 | ||
08af01c2 | 84 | # b4_error(KIND, FORMAT, [ARG1], [ARG2], ...) |
7eb8a0bc | 85 | # ------------------------------------------- |
7dc4a694 JD |
86 | # Write @KIND(FORMAT@,ARG1@,ARG2@,...@) to stdout. |
87 | # | |
88 | # For example: | |
89 | # | |
90 | # b4_error([[warn]], [[invalid value for `%s': %s]], [[foo]], [[3]]) | |
08af01c2 | 91 | m4_define([b4_error], |
7dc4a694 JD |
92 | [b4_cat([[@]$1[(]$2[]]dnl |
93 | [m4_if([$#], [2], [], | |
94 | [m4_foreach([b4_arg], | |
95 | m4_dquote(m4_shift(m4_shift($@))), | |
96 | [[@,]b4_arg])])[@)]])]) | |
08af01c2 | 97 | |
3fc65ead | 98 | # b4_error_at(KIND, START, END, FORMAT, [ARG1], [ARG2], ...) |
7eb8a0bc | 99 | # ---------------------------------------------------------- |
7dc4a694 JD |
100 | # Write @KIND_at(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout. |
101 | # | |
102 | # For example: | |
103 | # | |
104 | # b4_error_at([[complain]], [[input.y:2.3]], [[input.y:5.4]], | |
105 | # [[invalid %s]], [[foo]]) | |
3fc65ead | 106 | m4_define([b4_error_at], |
7dc4a694 JD |
107 | [b4_cat([[@]$1[_at(]$2[@,]$3[@,]$4[]]dnl |
108 | [m4_if([$#], [4], [], | |
109 | [m4_foreach([b4_arg], | |
110 | m4_dquote(m4_shift(m4_shift(m4_shift(m4_shift($@))))), | |
111 | [[@,]b4_arg])])[@)]])]) | |
3fc65ead | 112 | |
08af01c2 | 113 | # b4_warn(FORMAT, [ARG1], [ARG2], ...) |
7eb8a0bc | 114 | # ------------------------------------ |
7dc4a694 JD |
115 | # Write @warn(FORMAT@,ARG1@,ARG2@,...@) to stdout. |
116 | # | |
117 | # For example: | |
118 | # | |
119 | # b4_warn([[invalid value for `%s': %s]], [[foo]], [[3]]) | |
08af01c2 JD |
120 | # |
121 | # As a simple test suite, this: | |
122 | # | |
7dc4a694 | 123 | # m4_divert(-1) |
08af01c2 JD |
124 | # m4_define([asdf], [ASDF]) |
125 | # m4_define([fsa], [FSA]) | |
126 | # m4_define([fdsa], [FDSA]) | |
127 | # b4_warn([[[asdf), asdf]]], [[[fsa), fsa]]], [[[fdsa), fdsa]]]) | |
08af01c2 | 128 | # b4_warn([[asdf), asdf]], [[fsa), fsa]], [[fdsa), fdsa]]) |
08af01c2 | 129 | # b4_warn() |
08af01c2 | 130 | # b4_warn(1) |
08af01c2 JD |
131 | # b4_warn(1, 2) |
132 | # | |
7dc4a694 | 133 | # Should produce this without newlines: |
08af01c2 JD |
134 | # |
135 | # @warn([asdf), asdf]@,[fsa), fsa]@,[fdsa), fdsa]@) | |
136 | # @warn(asdf), asdf@,fsa), fsa@,fdsa), fdsa@) | |
08af01c2 | 137 | # @warn(@) |
08af01c2 JD |
138 | # @warn(1@) |
139 | # @warn(1@,2@) | |
140 | m4_define([b4_warn], | |
141 | [b4_error([[warn]], $@)]) | |
142 | ||
3fc65ead | 143 | # b4_warn_at(START, END, FORMAT, [ARG1], [ARG2], ...) |
7eb8a0bc | 144 | # --------------------------------------------------- |
7dc4a694 JD |
145 | # Write @warn(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout. |
146 | # | |
147 | # For example: | |
148 | # | |
149 | # b4_warn_at([[input.y:2.3]], [[input.y:5.4]], [[invalid %s]], [[foo]]) | |
3fc65ead JD |
150 | m4_define([b4_warn_at], |
151 | [b4_error_at([[warn]], $@)]) | |
152 | ||
08af01c2 | 153 | # b4_complain(FORMAT, [ARG1], [ARG2], ...) |
7eb8a0bc | 154 | # ---------------------------------------- |
7dc4a694 | 155 | # Write @complain(FORMAT@,ARG1@,ARG2@,...@) to stdout. |
08af01c2 | 156 | # |
7dc4a694 | 157 | # See b4_warn example. |
08af01c2 JD |
158 | m4_define([b4_complain], |
159 | [b4_error([[complain]], $@)]) | |
160 | ||
3fc65ead | 161 | # b4_complain_at(START, END, FORMAT, [ARG1], [ARG2], ...) |
7eb8a0bc | 162 | # ------------------------------------------------------- |
7dc4a694 JD |
163 | # Write @complain(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout. |
164 | # | |
165 | # See b4_warn_at example. | |
3fc65ead JD |
166 | m4_define([b4_complain_at], |
167 | [b4_error_at([[complain]], $@)]) | |
168 | ||
08af01c2 | 169 | # b4_fatal(FORMAT, [ARG1], [ARG2], ...) |
7eb8a0bc | 170 | # ------------------------------------- |
7dc4a694 | 171 | # Write @fatal(FORMAT@,ARG1@,ARG2@,...@) to stdout and exit. |
08af01c2 | 172 | # |
7dc4a694 | 173 | # See b4_warn example. |
08af01c2 | 174 | m4_define([b4_fatal], |
7dc4a694 JD |
175 | [b4_error([[fatal]], $@)dnl |
176 | m4_exit(1)]) | |
08af01c2 | 177 | |
3fc65ead | 178 | # b4_fatal_at(START, END, FORMAT, [ARG1], [ARG2], ...) |
7eb8a0bc | 179 | # ---------------------------------------------------- |
7dc4a694 JD |
180 | # Write @fatal(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout and exit. |
181 | # | |
182 | # See b4_warn_at example. | |
3fc65ead | 183 | m4_define([b4_fatal_at], |
7dc4a694 JD |
184 | [b4_error_at([[fatal]], $@)dnl |
185 | m4_exit(1)]) | |
3fc65ead | 186 | |
08af01c2 | 187 | |
90b9908d PB |
188 | ## ---------------- ## |
189 | ## Default values. ## | |
190 | ## ---------------- ## | |
191 | ||
192 | # m4_define_default([b4_lex_param], []) dnl breaks other skeletons | |
7ecec4dd JD |
193 | m4_define_default([b4_pre_prologue], []) |
194 | m4_define_default([b4_post_prologue], []) | |
90b9908d PB |
195 | m4_define_default([b4_epilogue], []) |
196 | m4_define_default([b4_parse_param], []) | |
197 | ||
198 | # The initial column and line. | |
199 | m4_define_default([b4_location_initial_column], [1]) | |
200 | m4_define_default([b4_location_initial_line], [1]) | |
201 | ||
202 | ||
203 | ## ------------ ## | |
204 | ## Data Types. ## | |
205 | ## ------------ ## | |
206 | ||
207 | # b4_ints_in(INT1, INT2, LOW, HIGH) | |
208 | # --------------------------------- | |
209 | # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise. | |
210 | m4_define([b4_ints_in], | |
211 | [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])]) | |
212 | ||
213 | ||
bd187d7b AD |
214 | # b4_subtract(LHS, RHS) |
215 | # --------------------- | |
216 | # Evaluate LHS - RHS if they are integer literals, otherwise expand | |
217 | # to (LHS) - (RHS). | |
218 | m4_define([b4_subtract], | |
219 | [m4_bmatch([$1$2], [^[0123456789]*$], | |
220 | [m4_eval([$1 - $2])], | |
221 | [($1) - ($2)])]) | |
222 | ||
e0c653e7 AD |
223 | # b4_args(ARG1, ...) |
224 | # _b4_args(ARG1, ...) | |
225 | # ------------------- | |
226 | # Join with comma, skipping empty arguments. | |
227 | # b4_args calls itself recursively until it sees the first non-empty | |
228 | # argument, then calls _b4_args which prepends each non-empty argument | |
229 | # with a comma. | |
230 | m4_define([b4_args], | |
231 | [m4_if([$#$1], | |
232 | [1], [], | |
233 | [m4_ifval([$1], | |
234 | [$1[]_$0(m4_shift($@))], | |
235 | [$0(m4_shift($@))])])]) | |
236 | ||
237 | # _b4_args(ARGS1, ...) | |
238 | # -------------------- | |
239 | m4_define([_b4_args], | |
240 | [m4_if([$#$1], | |
241 | [1], [], | |
242 | [m4_ifval([$1], [, $1])[]$0(m4_shift($@))])]) | |
243 | ||
244 | ||
245 | ||
90b9908d | 246 | |
ba206cf4 AD |
247 | # b4_integral_parser_tables_map(MACRO) |
248 | # ------------------------------------- | |
0991e29b AD |
249 | # Map MACRO on all the integral tables. MACRO is expected to have |
250 | # the signature MACRO(TABLE-NAME, CONTENT, COMMENT). | |
ba206cf4 | 251 | m4_define([b4_integral_parser_tables_map], |
0991e29b AD |
252 | [$1([pact], [b4_pact], |
253 | [[YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing | |
254 | STATE-NUM.]]) | |
255 | ||
256 | $1([defact], [b4_defact], | |
110ef36a JD |
257 | [[YYDEFACT[S] -- default reduction number in state S. Performed when |
258 | YYTABLE does not specify something else to do. Zero means the default | |
259 | is an error.]]) | |
0991e29b AD |
260 | |
261 | $1([pgoto], [b4_pgoto], [[YYPGOTO[NTERM-NUM].]]) | |
262 | ||
263 | $1([defgoto], [b4_defgoto], [[YYDEFGOTO[NTERM-NUM].]]) | |
264 | ||
265 | $1([table], [b4_table], | |
266 | [[YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If | |
267 | positive, shift that token. If negative, reduce the rule which | |
aa0cb40d | 268 | number is the opposite. If YYTABLE_NINF, syntax error.]]) |
0991e29b AD |
269 | |
270 | $1([check], [b4_check]) | |
271 | ||
272 | $1([stos], [b4_stos], | |
273 | [[STOS_[STATE-NUM] -- The (internal number of the) accessing | |
274 | symbol of state STATE-NUM.]]) | |
275 | ||
276 | $1([r1], [b4_r1], | |
277 | [[YYR1[YYN] -- Symbol number of symbol that rule YYN derives.]]) | |
278 | ||
279 | $1([r2], [b4_r2], | |
280 | [[YYR2[YYN] -- Number of symbols composing right hand side of rule YYN.]]) | |
281 | ]) | |
282 | ||
283 | ||
ba206cf4 AD |
284 | # b4_parser_tables_declare |
285 | # b4_parser_tables_define | |
286 | # ------------------------ | |
0991e29b | 287 | # Define/declare the (deterministic) parser tables. |
ba206cf4 AD |
288 | m4_define([b4_parser_tables_declare], |
289 | [b4_integral_parser_tables_map([b4_integral_parser_table_declare])]) | |
0991e29b | 290 | |
ba206cf4 AD |
291 | m4_define([b4_parser_tables_define], |
292 | [b4_integral_parser_tables_map([b4_integral_parser_table_define])]) | |
0991e29b AD |
293 | |
294 | ||
295 | ||
90b9908d PB |
296 | ## ------------------ ## |
297 | ## Decoding options. ## | |
298 | ## ------------------ ## | |
299 | ||
300 | # b4_flag_if(FLAG, IF-TRUE, IF-FALSE) | |
301 | # ----------------------------------- | |
302 | # Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail. | |
303 | m4_define([b4_flag_if], | |
304 | [m4_case(b4_$1_flag, | |
305 | [0], [$3], | |
306 | [1], [$2], | |
307 | [m4_fatal([invalid $1 value: ]$1)])]) | |
308 | ||
309 | ||
310 | # b4_define_flag_if(FLAG) | |
311 | # ----------------------- | |
312 | # Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the | |
313 | # value of the Boolean FLAG. | |
314 | m4_define([b4_define_flag_if], | |
315 | [_b4_define_flag_if($[1], $[2], [$1])]) | |
316 | ||
317 | # _b4_define_flag_if($1, $2, FLAG) | |
318 | # -------------------------------- | |
319 | # This macro works around the impossibility to define macros | |
320 | # inside macros, because issuing `[$1]' is not possible in M4 :(. | |
321 | # This sucks hard, GNU M4 should really provide M5 like $$1. | |
322 | m4_define([_b4_define_flag_if], | |
323 | [m4_if([$1$2], $[1]$[2], [], | |
324 | [m4_fatal([$0: Invalid arguments: $@])])dnl | |
325 | m4_define([b4_$3_if], | |
326 | [b4_flag_if([$3], [$1], [$2])])]) | |
327 | ||
328 | ||
329 | # b4_FLAG_if(IF-TRUE, IF-FALSE) | |
330 | # ----------------------------- | |
331 | # Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise. | |
332 | b4_define_flag_if([defines]) # Whether headers are requested. | |
90b9908d | 333 | b4_define_flag_if([glr]) # Whether a GLR parser is requested. |
90b9908d | 334 | b4_define_flag_if([nondeterministic]) # Whether conflicts should be handled. |
90b9908d PB |
335 | b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated. |
336 | ||
337 | ||
cf6fb222 AD |
338 | ## --------- ## |
339 | ## Symbols. ## | |
340 | ## --------- ## | |
341 | ||
342 | # b4_symbol_(NUM, FIELD) | |
343 | # ---------------------- | |
344 | # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if | |
345 | # undefined. | |
346 | m4_define([b4_symbol_], | |
347 | [m4_indir([b4_symbol($1, $2)])]) | |
348 | ||
349 | ||
350 | # b4_symbol(NUM, FIELD) | |
351 | # --------------------- | |
352 | # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if | |
353 | # undefined. If FIELD = id, prepend the prefix. | |
354 | m4_define([b4_symbol], | |
355 | [m4_case([$2], | |
4c6622c2 | 356 | [id], [m4_do([b4_percent_define_get([api.tokens.prefix])], |
cf6fb222 AD |
357 | [b4_symbol_([$1], [id])])], |
358 | [b4_symbol_($@)])]) | |
359 | ||
360 | ||
361 | # b4_symbol_if(NUM, FIELD, IF-TRUE, IF-FALSE) | |
362 | # ------------------------------------------- | |
363 | # If FIELD about symbol #NUM is 1 expand IF-TRUE, if is 0, expand IF-FALSE. | |
364 | # Otherwise an error. | |
365 | m4_define([b4_symbol_if], | |
366 | [m4_case(b4_symbol([$1], [$2]), | |
367 | [1], [$3], | |
368 | [0], [$4], | |
369 | [m4_fatal([$0: field $2 of $1 is not a Boolean:] b4_symbol([$1], [$2]))])]) | |
370 | ||
371 | ||
3bb21113 AD |
372 | # b4_symbol_action_location(SYMBOL-NUM, KIND) |
373 | # ------------------------------------------- | |
374 | # Report the location of the KIND action as FILE:LINE. | |
375 | m4_define([b4_symbol_action_location], | |
376 | [b4_symbol([$1], [$2_file]):b4_syncline([b4_symbol([$1], [$2_line])])]) | |
377 | ||
378 | ||
cf6fb222 AD |
379 | # b4_symbol_action(SYMBOL-NUM, KIND) |
380 | # ---------------------------------- | |
381 | # Run the action KIND (destructor or printer) for SYMBOL-NUM. | |
382 | # Same as in C, but using references instead of pointers. | |
383 | m4_define([b4_symbol_action], | |
384 | [b4_symbol_if([$1], [has_$2], | |
385 | [m4_pushdef([b4_dollar_dollar], | |
386 | [b4_symbol_value([(*yyvaluep)], | |
387 | b4_symbol_if([$1], [has_type], | |
388 | [b4_symbol([$1], [type])]))])dnl | |
389 | m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl | |
fb7c5b1f | 390 | b4_symbol_case_([$1]) |
cf6fb222 AD |
391 | b4_syncline([b4_symbol([$1], [$2_line])], ["b4_symbol([$1], [$2_file])"]) |
392 | b4_symbol([$1], [$2]) | |
393 | b4_syncline([@oline@], [@ofile@]) | |
394 | break; | |
395 | ||
396 | m4_popdef([b4_at_dollar])dnl | |
397 | m4_popdef([b4_dollar_dollar])dnl | |
398 | ])]) | |
399 | ||
400 | ||
401 | # b4_symbol_destructor(SYMBOL-NUM) | |
402 | # b4_symbol_printer(SYMBOL-NUM) | |
403 | # -------------------------------- | |
404 | m4_define([b4_symbol_destructor], [b4_symbol_action([$1], [destructor])]) | |
405 | m4_define([b4_symbol_printer], [b4_symbol_action([$1], [printer])]) | |
406 | ||
407 | ||
408 | # b4_symbol_case_(SYMBOL-NUM) | |
409 | # --------------------------- | |
410 | # Issue a "case NUM" for SYMBOL-NUM. | |
411 | m4_define([b4_symbol_case_], | |
412 | [ case b4_symbol([$1], [number]): // b4_symbol([$1], [tag]) | |
413 | ]) | |
414 | ||
415 | ||
416 | # b4_symbol_foreach(MACRO) | |
417 | # ------------------------ | |
418 | # Invoke MACRO(SYMBOL-NUM) for each SYMBOL-NUM. | |
419 | m4_define([b4_symbol_foreach], | |
420 | [m4_map([$1], m4_defn([b4_symbol_numbers]))]) | |
421 | ||
422 | ||
423 | ## ------- ## | |
424 | ## Types. ## | |
425 | ## ------- ## | |
426 | ||
427 | # b4_type_action_(NUMS) | |
428 | # --------------------- | |
429 | # Run actions for the symbol NUMS that all have the same type-name. | |
430 | # Skip NUMS that have no type-name. | |
431 | m4_define([b4_type_action_], | |
432 | [b4_symbol_if([$1], [has_type], | |
433 | [m4_map([b4_symbol_case_], [$@])[]dnl | |
434 | b4_dollar_dollar([b4_symbol([$1], [number])], | |
435 | [b4_symbol([$1], [tag])], | |
436 | [b4_symbol([$1], [type])]); | |
437 | break; | |
438 | ||
439 | ])]) | |
440 | ||
441 | # b4_type_foreach(MACRO) | |
442 | # ---------------------- | |
443 | # Invoke MACRO(SYMBOL-NUMS) for each set of SYMBOL-NUMS for each type set. | |
444 | m4_define([b4_type_foreach], | |
445 | [m4_map([$1], m4_defn([b4_type_names]))]) | |
446 | ||
447 | ||
448 | ||
90b9908d PB |
449 | ## ----------- ## |
450 | ## Synclines. ## | |
451 | ## ----------- ## | |
452 | ||
453 | # b4_basename(NAME) | |
454 | # ----------------- | |
455 | # Similar to POSIX basename; the differences don't matter here. | |
456 | # Beware that NAME is not evaluated. | |
457 | m4_define([b4_basename], | |
458 | [m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])]) | |
459 | ||
460 | ||
461 | # b4_syncline(LINE, FILE) | |
462 | # ----------------------- | |
463 | m4_define([b4_syncline], | |
fad814bd AD |
464 | [b4_flag_if([synclines], |
465 | [b4_sync_end([__line__], [b4_basename(m4_quote(__file__))]) | |
90b9908d PB |
466 | b4_sync_start([$1], [$2])])]) |
467 | ||
468 | m4_define([b4_sync_end], [b4_comment([Line $1 of $2])]) | |
469 | m4_define([b4_sync_start], [b4_comment([Line $1 of $2])]) | |
470 | ||
471 | # b4_user_code(USER-CODE) | |
472 | # ----------------------- | |
473 | # Emit code from the user, ending it with synclines. | |
474 | m4_define([b4_user_code], | |
475 | [$1 | |
476 | b4_syncline([@oline@], [@ofile@])]) | |
477 | ||
478 | ||
479 | # b4_define_user_code(MACRO) | |
480 | # -------------------------- | |
481 | # From b4_MACRO, build b4_user_MACRO that includes the synclines. | |
482 | m4_define([b4_define_user_code], | |
483 | [m4_define([b4_user_$1], | |
484 | [b4_user_code([b4_$1])])]) | |
485 | ||
486 | ||
487 | # b4_user_actions | |
488 | # b4_user_initial_action | |
489 | # b4_user_post_prologue | |
136a0f76 | 490 | # b4_user_pre_prologue |
90b9908d PB |
491 | # b4_user_stype |
492 | # ---------------------- | |
493 | # Macros that issue user code, ending with synclines. | |
494 | b4_define_user_code([actions]) | |
495 | b4_define_user_code([initial_action]) | |
496 | b4_define_user_code([post_prologue]) | |
136a0f76 | 497 | b4_define_user_code([pre_prologue]) |
90b9908d PB |
498 | b4_define_user_code([stype]) |
499 | ||
1c7b7e1d | 500 | |
9611cfa2 | 501 | # b4_check_user_names(WHAT, USER-LIST, BISON-NAMESPACE) |
c6abeab1 JD |
502 | # ----------------------------------------------------- |
503 | # Complain if any name of type WHAT is used by the user (as recorded in | |
504 | # USER-LIST) but is not used by Bison (as recorded by macros in the | |
505 | # namespace BISON-NAMESPACE). | |
7eb8a0bc | 506 | # |
f8e7258f JD |
507 | # USER-LIST must expand to a list specifying all user occurrences of all names |
508 | # of type WHAT. Each item in the list must be a triplet specifying one | |
6afc30cc JD |
509 | # occurrence: name, start boundary, and end boundary. Empty string names are |
510 | # fine. An empty list is fine. | |
7eb8a0bc | 511 | # |
a4e25e1d | 512 | # For example, to define b4_foo_user_names to be used for USER-LIST with three |
6afc30cc | 513 | # name occurrences and with correct quoting: |
8e0a5e9e | 514 | # |
a4e25e1d | 515 | # m4_define([b4_foo_user_names], |
7eb8a0bc JD |
516 | # [[[[[[bar]], [[parser.y:1.7]], [[parser.y:1.16]]]], |
517 | # [[[[bar]], [[parser.y:5.7]], [[parser.y:5.16]]]], | |
518 | # [[[[baz]], [[parser.y:8.7]], [[parser.y:8.16]]]]]]) | |
1c7b7e1d | 519 | # |
9611cfa2 JD |
520 | # The macro BISON-NAMESPACE(bar) must be defined iff the name bar of type WHAT |
521 | # is used by Bison (in the front-end or in the skeleton). Empty string names | |
522 | # are fine, but it would be ugly for Bison to actually use one. | |
6afc30cc | 523 | # |
9611cfa2 JD |
524 | # For example, to use b4_foo_bison_names for BISON-NAMESPACE and define that |
525 | # the names bar and baz are used by Bison: | |
1c7b7e1d | 526 | # |
9611cfa2 JD |
527 | # m4_define([b4_foo_bison_names(bar)]) |
528 | # m4_define([b4_foo_bison_names(baz)]) | |
1c7b7e1d | 529 | # |
6afc30cc | 530 | # To invoke b4_check_user_names with TYPE foo, with USER-LIST |
9611cfa2 JD |
531 | # b4_foo_user_names, with BISON-NAMESPACE b4_foo_bison_names, and with correct |
532 | # quoting: | |
1c7b7e1d | 533 | # |
a4e25e1d | 534 | # b4_check_user_names([[foo]], [b4_foo_user_names], |
9611cfa2 | 535 | # [[b4_foo_bison_names]]) |
6afc30cc JD |
536 | m4_define([b4_check_user_names], |
537 | [m4_foreach([b4_occurrence], $2, | |
538 | [m4_pushdef([b4_occurrence], b4_occurrence)dnl | |
539 | m4_pushdef([b4_user_name], m4_car(b4_occurrence))dnl | |
540 | m4_pushdef([b4_start], m4_car(m4_shift(b4_occurrence)))dnl | |
541 | m4_pushdef([b4_end], m4_shift(m4_shift(b4_occurrence)))dnl | |
a4e25e1d | 542 | m4_ifndef($3[(]m4_quote(b4_user_name)[)], |
c6abeab1 JD |
543 | [b4_complain_at([b4_start], [b4_end], |
544 | [[%s `%s' is not used]], | |
545 | [$1], [b4_user_name])])[]dnl | |
6afc30cc JD |
546 | m4_popdef([b4_occurrence])dnl |
547 | m4_popdef([b4_user_name])dnl | |
548 | m4_popdef([b4_start])dnl | |
549 | m4_popdef([b4_end])dnl | |
550 | ])]) | |
551 | ||
67501061 AD |
552 | |
553 | ||
554 | ## --------------------- ## | |
555 | ## b4_percent_define_*. ## | |
556 | ## --------------------- ## | |
557 | ||
558 | ||
559 | # b4_percent_define_use(VARIABLE) | |
560 | # ------------------------------- | |
561 | # Declare that VARIABLE was used. | |
562 | m4_define([b4_percent_define_use], | |
563 | [m4_define([b4_percent_define_bison_variables(]$1[)])dnl | |
564 | ]) | |
565 | ||
a4e25e1d JD |
566 | # b4_percent_define_get(VARIABLE) |
567 | # ------------------------------- | |
416a9da6 | 568 | # Mimic muscle_percent_define_get in ../src/muscle-tab.h exactly. That is, if |
f124d423 | 569 | # the %define variable VARIABLE is defined, emit its value. Also, record |
9611cfa2 JD |
570 | # Bison's usage of VARIABLE by defining |
571 | # b4_percent_define_bison_variables(VARIABLE). | |
a4e25e1d JD |
572 | # |
573 | # For example: | |
574 | # | |
575 | # b4_percent_define_get([[foo]]) | |
576 | m4_define([b4_percent_define_get], | |
67501061 | 577 | [b4_percent_define_use([$1])dnl |
a4e25e1d JD |
578 | m4_ifdef([b4_percent_define(]$1[)], [m4_indir([b4_percent_define(]$1[)])])]) |
579 | ||
35b8730d JD |
580 | # b4_percent_define_get_loc(VARIABLE) |
581 | # ----------------------------------- | |
416a9da6 | 582 | # Mimic muscle_percent_define_get_loc in ../src/muscle-tab.h exactly. That is, |
35b8730d JD |
583 | # if the %define variable VARIABLE is undefined, complain fatally since that's |
584 | # a Bison or skeleton error. Otherwise, return its definition location in a | |
585 | # form approriate for the first two arguments of b4_warn_at, b4_complain_at, or | |
586 | # b4_fatal_at. Don't record this as a Bison usage of VARIABLE as there's no | |
587 | # reason to suspect that the user-supplied value has yet influenced the output. | |
588 | # | |
589 | # For example: | |
590 | # | |
591 | # b4_complain_at(b4_percent_define_get_loc([[foo]]), [[invalid foo]]) | |
592 | m4_define([b4_percent_define_get_loc], | |
593 | [m4_ifdef([b4_percent_define_loc(]$1[)], | |
594 | [m4_pushdef([b4_loc], m4_indir([b4_percent_define_loc(]$1[)]))dnl | |
595 | b4_loc[]dnl | |
596 | m4_popdef([b4_loc])], | |
597 | [b4_fatal([[undefined %%define variable `%s' passed to b4_percent_define_get_loc]], [$1])])]) | |
598 | ||
599 | # b4_percent_define_get_syncline(VARIABLE) | |
600 | # ---------------------------------------- | |
416a9da6 | 601 | # Mimic muscle_percent_define_get_syncline in ../src/muscle-tab.h exactly. |
35b8730d JD |
602 | # That is, if the %define variable VARIABLE is undefined, complain fatally |
603 | # since that's a Bison or skeleton error. Otherwise, return its definition | |
604 | # location as a b4_syncline invocation. Don't record this as a Bison usage of | |
605 | # VARIABLE as there's no reason to suspect that the user-supplied value has yet | |
606 | # influenced the output. | |
607 | # | |
608 | # For example: | |
609 | # | |
610 | # b4_percent_define_get_syncline([[foo]]) | |
611 | m4_define([b4_percent_define_get_syncline], | |
612 | [m4_ifdef([b4_percent_define_syncline(]$1[)], | |
613 | [m4_indir([b4_percent_define_syncline(]$1[)])], | |
614 | [b4_fatal([[undefined %%define variable `%s' passed to b4_percent_define_get_syncline]], [$1])])]) | |
615 | ||
592d0b1e PB |
616 | # b4_percent_define_ifdef(VARIABLE, IF-TRUE, [IF-FALSE]) |
617 | # ------------------------------------------------------ | |
416a9da6 | 618 | # Mimic muscle_percent_define_ifdef in ../src/muscle-tab.h exactly. That is, |
f124d423 | 619 | # if the %define variable VARIABLE is defined, expand IF-TRUE, else expand |
9611cfa2 JD |
620 | # IF-FALSE. Also, record Bison's usage of VARIABLE by defining |
621 | # b4_percent_define_bison_variables(VARIABLE). | |
622 | # | |
623 | # For example: | |
624 | # | |
625 | # b4_percent_define_ifdef([[foo]], [[it's defined]], [[it's undefined]]) | |
592d0b1e PB |
626 | m4_define([b4_percent_define_ifdef], |
627 | [m4_ifdef([b4_percent_define(]$1[)], | |
67501061 | 628 | [b4_percent_define_use([$1])$2], |
592d0b1e PB |
629 | [$3])]) |
630 | ||
b47b6ff7 | 631 | |
b47b6ff7 AD |
632 | ## --------- ## |
633 | ## Options. ## | |
634 | ## --------- ## | |
635 | ||
636 | ||
592d0b1e PB |
637 | # b4_percent_define_flag_if(VARIABLE, IF-TRUE, [IF-FALSE]) |
638 | # -------------------------------------------------------- | |
416a9da6 | 639 | # Mimic muscle_percent_define_flag_if in ../src/muscle-tab.h exactly. That is, |
9611cfa2 JD |
640 | # if the %define variable VARIABLE is defined to "" or "true", expand IF-TRUE. |
641 | # If it is defined to "false", expand IF-FALSE. Complain if it is undefined | |
642 | # (a Bison or skeleton error since the default value should have been set | |
643 | # already) or defined to any other value (possibly a user error). Also, record | |
644 | # Bison's usage of VARIABLE by defining | |
645 | # b4_percent_define_bison_variables(VARIABLE). | |
646 | # | |
647 | # For example: | |
648 | # | |
649 | # b4_percent_define_flag_if([[foo]], [[it's true]], [[it's false]]) | |
592d0b1e PB |
650 | m4_define([b4_percent_define_flag_if], |
651 | [b4_percent_define_ifdef([$1], | |
9611cfa2 JD |
652 | [m4_case(b4_percent_define_get([$1]), |
653 | [], [$2], [true], [$2], [false], [$3], | |
35b8730d JD |
654 | [m4_expand_once([b4_complain_at(b4_percent_define_get_loc([$1]), |
655 | [[invalid value for %%define Boolean variable `%s']], | |
656 | [$1])], | |
657 | [[b4_percent_define_flag_if($1)]])])], | |
9611cfa2 | 658 | [b4_fatal([[undefined %%define variable `%s' passed to b4_percent_define_flag_if]], [$1])])]) |
592d0b1e | 659 | |
b3a2272a | 660 | |
a4e25e1d JD |
661 | # b4_percent_define_default(VARIABLE, DEFAULT) |
662 | # -------------------------------------------- | |
416a9da6 | 663 | # Mimic muscle_percent_define_default in ../src/muscle-tab.h exactly. That is, |
9611cfa2 JD |
664 | # if the %define variable VARIABLE is undefined, set its value to DEFAULT. |
665 | # Don't record this as a Bison usage of VARIABLE as there's no reason to | |
666 | # suspect that the value has yet influenced the output. | |
a4e25e1d JD |
667 | # |
668 | # For example: | |
669 | # | |
670 | # b4_percent_define_default([[foo]], [[default value]]) | |
671 | m4_define([b4_percent_define_default], | |
672 | [m4_ifndef([b4_percent_define(]$1[)], | |
9611cfa2 JD |
673 | [m4_define([b4_percent_define(]$1[)], [$2])dnl |
674 | m4_define([b4_percent_define_loc(]$1[)], | |
92822aff JD |
675 | [[[[<skeleton default value>:-1.-1]], |
676 | [[<skeleton default value>:-1.-1]]]])dnl | |
677 | m4_define([b4_percent_define_syncline(]$1[)], [[]])])]) | |
6afc30cc | 678 | |
b3a2272a AD |
679 | |
680 | # b4_percent_define_if_define(VARIABLE) | |
681 | # ------------------------------------- | |
682 | # Define b4_VARIABLE_if that executes its $1 or $2 depending whether | |
4f646c37 AD |
683 | # VARIABLE was %defined. The characters `.' and `-' in VARIABLE are mapped |
684 | # to `_'. | |
b3a2272a | 685 | m4_define([b4_percent_define_if_define_], |
4f646c37 | 686 | [m4_define(m4_bpatsubst([b4_$1_if], [[-.]], [_]), |
fa819509 | 687 | [b4_percent_define_flag_if([$1], [$2], [$3])])]) |
b3a2272a AD |
688 | m4_define([b4_percent_define_if_define], |
689 | [b4_percent_define_default([[$1]], [[false]]) | |
690 | b4_percent_define_if_define_([$1], $[1], $[2])]) | |
691 | ||
692 | ||
d782395d JD |
693 | # b4_percent_define_check_values(VALUES) |
694 | # -------------------------------------- | |
416a9da6 | 695 | # Mimic muscle_percent_define_check_values in ../src/muscle-tab.h exactly |
d782395d JD |
696 | # except that the VALUES structure is more appropriate for M4. That is, VALUES |
697 | # is a list of sublists of strings. For each sublist, the first string is the | |
698 | # name of a %define variable, and all remaining strings in that sublist are the | |
699 | # valid values for that variable. Complain if such a variable is undefined (a | |
700 | # Bison error since the default value should have been set already) or defined | |
701 | # to any other value (possibly a user error). Don't record this as a Bison | |
702 | # usage of the variable as there's no reason to suspect that the value has yet | |
703 | # influenced the output. | |
704 | # | |
705 | # For example: | |
706 | # | |
707 | # b4_percent_define_check_values([[[[foo]], [[foo-value1]], [[foo-value2]]]], | |
708 | # [[[[bar]], [[bar-value1]]]]) | |
709 | m4_define([b4_percent_define_check_values], | |
710 | [m4_foreach([b4_sublist], m4_quote($@), | |
711 | [_b4_percent_define_check_values(b4_sublist)])]) | |
712 | ||
713 | m4_define([_b4_percent_define_check_values], | |
714 | [m4_ifdef([b4_percent_define(]$1[)], | |
715 | [m4_pushdef([b4_good_value], [0])dnl | |
716 | m4_if($#, 1, [], | |
717 | [m4_foreach([b4_value], m4_dquote(m4_shift($@)), | |
718 | [m4_if(m4_indir([b4_percent_define(]$1[)]), b4_value, | |
719 | [m4_define([b4_good_value], [1])])])])dnl | |
720 | m4_if(b4_good_value, [0], | |
35b8730d | 721 | [b4_complain_at(b4_percent_define_get_loc([$1]), |
d782395d JD |
722 | [[invalid value for %%define variable `%s': `%s']], |
723 | [$1], | |
f4909773 JD |
724 | m4_dquote(m4_indir([b4_percent_define(]$1[)]))) |
725 | m4_foreach([b4_value], m4_dquote(m4_shift($@)), | |
726 | [b4_complain_at(b4_percent_define_get_loc([$1]), | |
727 | [[accepted value: `%s']], | |
728 | m4_dquote(b4_value))])])dnl | |
d782395d JD |
729 | m4_popdef([b4_good_value])], |
730 | [b4_fatal([[undefined %%define variable `%s' passed to b4_percent_define_check_values]], [$1])])]) | |
731 | ||
a4e25e1d | 732 | # b4_percent_code_get([QUALIFIER]) |
6afc30cc JD |
733 | # -------------------------------- |
734 | # If any %code blocks for QUALIFIER are defined, emit them beginning with a | |
735 | # comment and ending with synclines and a newline. If QUALIFIER is not | |
592d0b1e | 736 | # specified or empty, do this for the unqualified %code blocks. Also, record |
9611cfa2 JD |
737 | # Bison's usage of QUALIFIER (if specified) by defining |
738 | # b4_percent_code_bison_qualifiers(QUALIFIER). | |
a4e25e1d JD |
739 | # |
740 | # For example, to emit any unqualified %code blocks followed by any %code | |
741 | # blocks for the qualifier foo: | |
742 | # | |
743 | # b4_percent_code_get | |
744 | # b4_percent_code_get([[foo]]) | |
745 | m4_define([b4_percent_code_get], | |
592d0b1e | 746 | [m4_pushdef([b4_macro_name], [[b4_percent_code(]$1[)]])dnl |
9611cfa2 | 747 | m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])dnl |
6afc30cc JD |
748 | m4_ifdef(b4_macro_name, |
749 | [b4_comment([m4_if([$#], [0], [[Unqualified %code]], | |
42ee26bb | 750 | [["%code ]$1["]])[ blocks.]]) |
a4e25e1d JD |
751 | b4_user_code([m4_indir(b4_macro_name)]) |
752 | ])dnl | |
6afc30cc JD |
753 | m4_popdef([b4_macro_name])]) |
754 | ||
592d0b1e PB |
755 | # b4_percent_code_ifdef(QUALIFIER, IF-TRUE, [IF-FALSE]) |
756 | # ----------------------------------------------------- | |
757 | # If any %code blocks for QUALIFIER (or unqualified %code blocks if | |
758 | # QUALIFIER is empty) are defined, expand IF-TRUE, else expand IF-FALSE. | |
9611cfa2 JD |
759 | # Also, record Bison's usage of QUALIFIER (if specified) by defining |
760 | # b4_percent_code_bison_qualifiers(QUALIFIER). | |
592d0b1e PB |
761 | m4_define([b4_percent_code_ifdef], |
762 | [m4_ifdef([b4_percent_code(]$1[)], | |
9611cfa2 | 763 | [m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])$2], |
592d0b1e PB |
764 | [$3])]) |
765 | ||
6afc30cc | 766 | |
ad597a78 AD |
767 | ## ------------------ ## |
768 | ## Common variables. ## | |
769 | ## ------------------ ## | |
770 | ||
5679f311 AD |
771 | # Default values for %define. |
772 | # --------------------------- | |
4c6622c2 AD |
773 | # If the api.tokens.prefix, it is empty. |
774 | m4_percent_define_default([[api.tokens.prefix]], [[]]) | |
5679f311 | 775 | |
ad597a78 AD |
776 | # b4_parse_assert_if([IF-ASSERTIONS-ARE-USED], [IF-NOT]) |
777 | # b4_parse_trace_if([IF-DEBUG-TRACES-ARE-ENABLED], [IF-NOT]) | |
778 | # b4_lex_symbol_if([IF-YYLEX-RETURNS-A-COMPLETE-SYMBOL], [IF-NOT]) | |
779 | # b4_variant_if([IF-VARIANT-ARE-USED], [IF-NOT]) | |
780 | # ---------------------------------------------- | |
781 | b4_percent_define_if_define([lex_symbol]) | |
782 | b4_percent_define_if_define([locations]) # Whether locations are tracked. | |
783 | b4_percent_define_if_define([parse.assert]) | |
784 | b4_percent_define_if_define([parse.trace]) | |
785 | b4_percent_define_if_define([variant]) | |
786 | ||
787 | ||
788 | # b4_error_verbose_if([IF-ERRORS-ARE-VERBOSE], [IF-NOT]) | |
789 | # ------------------------------------------------------ | |
790 | # Map %define parse.error "(simple|verbose)" to b4_error_verbose_if and | |
791 | # b4_error_verbose_flag. | |
792 | b4_percent_define_default([[parse.error]], [[simple]]) | |
793 | b4_percent_define_check_values([[[[parse.error]], | |
794 | [[simple]], [[verbose]]]]) | |
795 | m4_define([b4_error_verbose_flag], | |
82db7cdb JD |
796 | [m4_case(b4_percent_define_get([[parse.error]]), |
797 | [simple], [[0]], | |
798 | [verbose], [[1]])]) | |
ad597a78 | 799 | b4_define_flag_if([error_verbose]) |
5679f311 AD |
800 | |
801 | ||
9611cfa2 JD |
802 | ## ----------------------------------------------------------- ## |
803 | ## After processing the skeletons, check that all the user's ## | |
804 | ## %define variables and %code qualifiers were used by Bison. ## | |
805 | ## ----------------------------------------------------------- ## | |
6afc30cc | 806 | |
a4e25e1d JD |
807 | m4_define([b4_check_user_names_wrap], |
808 | [m4_ifdef([b4_percent_]$1[_user_]$2[s], | |
809 | [b4_check_user_names([[%]$1 $2], | |
810 | [b4_percent_]$1[_user_]$2[s], | |
9611cfa2 | 811 | [[b4_percent_]$1[_bison_]$2[s]])])]) |
6afc30cc | 812 | |
882a1fbf | 813 | m4_wrap_lifo([ |
a4e25e1d JD |
814 | b4_check_user_names_wrap([[define]], [[variable]]) |
815 | b4_check_user_names_wrap([[code]], [[qualifier]]) | |
6afc30cc | 816 | ]) |