3 # Language-independent M4 Macros for Bison.
4 # Copyright (C) 2002, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
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.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 ## ---------------- ##
24 ## ---------------- ##
26 # b4_copyright(TITLE, YEARS)
27 # --------------------------
28 m4_define([b4_copyright],
29 [b4_comment([A Bison parser, made by GNU Bison b4_version.])
33 m4_text_wrap([Copyright (C) $2 Free Software Foundation, Inc.], [ ])
35 This program is free software; you can redistribute it and/or modify
36 it under the terms of the GNU General Public License as published by
37 the Free Software Foundation; either version 2, or (at your option)
40 This program is distributed in the hope that it will be useful,
41 but WITHOUT ANY WARRANTY; without even the implied warranty of
42 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 GNU General Public License for more details.
45 You should have received a copy of the GNU General Public License
46 along with this program; if not, write to the Free Software
47 Foundation, Inc., 51 Franklin Street, Fifth Floor,
48 Boston, MA 02110-1301, USA.])
50 b4_comment([As a special exception, you may create a larger work that contains
51 part or all of the Bison parser skeleton and distribute that work
52 under terms of your choice, so long as that work isn't itself a
53 parser generator using the skeleton or a modified version thereof
54 as a parser skeleton. Alternatively, if you modify or redistribute
55 the parser skeleton itself, you may (at your option) remove this
56 special exception, which will cause the skeleton and the resulting
57 Bison output files to be licensed under the GNU General Public
58 License without this special exception.
60 This special exception was added by the Free Software Foundation in
61 version 2.2 of Bison.])])
64 ## ---------------- ##
66 ## ---------------- ##
68 # b4_error(KIND, FORMAT, [ARG1], [ARG2], ...)
69 # ---------------------------------------------------------------
70 # Write @KIND(FORMAT@,ARG1@,ARG2@,...@) to diversion 0.
72 [m4_divert_push(0)[@]$1[(]$2[]m4_if([$#], [2], [],
74 m4_dquote(m4_shift(m4_shift($@))),
75 [[@,]b4_arg])])[@)]m4_divert_pop(0)])
77 # b4_warn(FORMAT, [ARG1], [ARG2], ...)
78 # --------------------------------------------------------
79 # Write @warn(FORMAT@,ARG1@,ARG2@,...@) to diversion 0.
81 # As a simple test suite, this:
83 # m4_define([asdf], [ASDF])
84 # m4_define([fsa], [FSA])
85 # m4_define([fdsa], [FDSA])
86 # b4_warn([[[asdf), asdf]]], [[[fsa), fsa]]], [[[fdsa), fdsa]]])
88 # b4_warn([[asdf), asdf]], [[fsa), fsa]], [[fdsa), fdsa]])
90 # b4_warn([asdf), asdf], [fsa), fsa], [fdsa), fdsa])
100 # Should produce this:
102 # @warn([asdf), asdf]@,[fsa), fsa]@,[fdsa), fdsa]@)
103 # @warn(asdf), asdf@,fsa), fsa@,fdsa), fdsa@)
104 # @warn(ASDF), ASDF@,FSA), FSA@,FDSA), FDSA@)
110 [b4_error([[warn]], $@)])
112 # b4_complain(FORMAT, [ARG1], [ARG2], ...)
113 # ------------------------------------------------------------
114 # Write @complain(FORMAT@,ARG1@,ARG2@,...@) to diversion 0.
116 # See the test suite for b4_warn above.
117 m4_define([b4_complain],
118 [b4_error([[complain]], $@)])
120 # b4_fatal(FORMAT, [ARG1], [ARG2], ...)
121 # ---------------------------------------------------------
122 # Write @fatal(FORMAT@,ARG1@,ARG2@,...@) to diversion 0.
124 # See the test suite for b4_warn above.
125 m4_define([b4_fatal],
126 [b4_error([[fatal]], $@)])
129 ## ---------------- ##
130 ## Default values. ##
131 ## ---------------- ##
133 # m4_define_default([b4_lex_param], []) dnl breaks other skeletons
134 m4_define_default([b4_epilogue], [])
135 m4_define_default([b4_parse_param], [])
137 # The initial column and line.
138 m4_define_default([b4_location_initial_column], [1])
139 m4_define_default([b4_location_initial_line], [1])
146 # b4_ints_in(INT1, INT2, LOW, HIGH)
147 # ---------------------------------
148 # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
149 m4_define([b4_ints_in],
150 [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
154 ## ------------------ ##
155 ## Decoding options. ##
156 ## ------------------ ##
158 # b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
159 # -----------------------------------
160 # Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
161 m4_define([b4_flag_if],
165 [m4_fatal([invalid $1 value: ]$1)])])
168 # b4_define_flag_if(FLAG)
169 # -----------------------
170 # Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
171 # value of the Boolean FLAG.
172 m4_define([b4_define_flag_if],
173 [_b4_define_flag_if($[1], $[2], [$1])])
175 # _b4_define_flag_if($1, $2, FLAG)
176 # --------------------------------
177 # This macro works around the impossibility to define macros
178 # inside macros, because issuing `[$1]' is not possible in M4 :(.
179 # This sucks hard, GNU M4 should really provide M5 like $$1.
180 m4_define([_b4_define_flag_if],
181 [m4_if([$1$2], $[1]$[2], [],
182 [m4_fatal([$0: Invalid arguments: $@])])dnl
183 m4_define([b4_$3_if],
184 [b4_flag_if([$3], [$1], [$2])])])
187 # b4_FLAG_if(IF-TRUE, IF-FALSE)
188 # -----------------------------
189 # Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
190 b4_define_flag_if([defines]) # Whether headers are requested.
191 b4_define_flag_if([error_verbose]) # Whether error are verbose.
192 b4_define_flag_if([glr]) # Whether a GLR parser is requested.
193 b4_define_flag_if([locations]) # Whether locations are tracked.
194 b4_define_flag_if([nondeterministic]) # Whether conflicts should be handled.
195 b4_define_flag_if([pull]) # Whether pull parsing is requested.
196 b4_define_flag_if([pure]) # Whether the interface is pure.
197 b4_define_flag_if([push]) # Whether push parsing is requested.
198 b4_define_flag_if([use_push_for_pull]) # Whether push parsing should be used
199 # in place of pull parsing (where
200 # available) for the sake of the test
202 b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated.
205 ## ------------------------- ##
206 ## Assigning token numbers. ##
207 ## ------------------------- ##
216 # Similar to POSIX basename; the differences don't matter here.
217 # Beware that NAME is not evaluated.
218 m4_define([b4_basename],
219 [m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])
222 # b4_syncline(LINE, FILE)
223 # -----------------------
224 m4_define([b4_syncline],
225 [b4_flag_if([synclines], [
226 b4_sync_end([__line__], [b4_basename(m4_quote(__file__))])
227 b4_sync_start([$1], [$2])])])
229 m4_define([b4_sync_end], [b4_comment([Line $1 of $2])])
230 m4_define([b4_sync_start], [b4_comment([Line $1 of $2])])
232 # b4_user_code(USER-CODE)
233 # -----------------------
234 # Emit code from the user, ending it with synclines.
235 m4_define([b4_user_code],
237 b4_syncline([@oline@], [@ofile@])])
240 # b4_define_user_code(MACRO)
241 # --------------------------
242 # From b4_MACRO, build b4_user_MACRO that includes the synclines.
243 m4_define([b4_define_user_code],
244 [m4_define([b4_user_$1],
245 [b4_user_code([b4_$1])])])
249 # b4_user_initial_action
250 # b4_user_post_prologue
251 # b4_user_pre_prologue
253 # ----------------------
254 # Macros that issue user code, ending with synclines.
255 b4_define_user_code([actions])
256 b4_define_user_code([initial_action])
257 b4_define_user_code([post_prologue])
258 b4_define_user_code([pre_prologue])
259 b4_define_user_code([stype])
261 # b4_check_percent_code_qualifiers([VAILD_QUALIFIER], [VALID_QUALIFIER], ...)
262 # ---------------------------------------------------------------------------
263 # Complain if any %code qualifier used in the grammar is not a valid qualifier.
265 # b4_used_percent_code_qualifiers must expand to a comma-delimited list of the
266 # %code qualifiers used in the grammar. The list as a whole must be quoted,
267 # but individual qualifiers in it must not be. Qualifiers will not be
268 # M4-expanded. Whitespace will not be stripped from the qualifiers. Any
269 # qualifier that is the empty string will be correctly parsed as such. If
270 # b4_used_percent_code_qualifiers expands to the empty string, then the list
271 # contains one qualifier that is the empty string. If
272 # b4_used_percent_code_qualifiers is undefined, then the list contains no
273 # qualifiers. Multiple occurrences of the same qualifier are fine.
275 # Each VALID_QUALIFIER will be M4-expanded once to define a valid qualifier. A
276 # VALID_QUALIFIER that expands to the empty string will correctly define the
277 # empty string as a valid qualifier. If b4_used_percent_code_qualifiers is
278 # invoked with empty parens, then there is one valid qualifier and it is the
279 # empty string. To specify that there are no valid qualifiers, invoke
280 # b4_check_percent_code_qualifiers without parens. Multiple occurrences of the
281 # same valid qualifier are fine.
283 # Qualifiers and valid qualifiers must not contain the characters `,' or `&'.
284 m4_define([b4_check_percent_code_qualifiers],
285 [m4_ifdef([b4_used_percent_code_qualifiers], [
286 m4_foreach([b4_qualifier],
287 m4_dquote(m4_substr(m4_split(m4_translit(m4_dquote([,]b4_used_percent_code_qualifiers), [,], [&]), [[&]]), 4)),
288 [m4_if(m4_index(m4_if($#, 0, [], [[,]m4_quote($*)[,]]),
289 [,]m4_defn([b4_qualifier])[,]),
291 [b4_complain([[`%s' is not a recognized %%code qualifier]],
292 [m4_defn([b4_qualifier])])