bison.m4 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. -*- Autoconf -*-
  2. # Language-independent M4 Macros for Bison.
  3. # Copyright (C) 2002, 2004-2015, 2018-2021 Free Software Foundation,
  4. # Inc.
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. # m4_gsub(STRING, RE1, SUBST1, RE2, SUBST2, ...)
  18. # ----------------------------------------------
  19. # m4 equivalent of
  20. #
  21. # $_ = STRING;
  22. # s/RE1/SUBST1/g;
  23. # s/RE2/SUBST2/g;
  24. # ...
  25. #
  26. # Really similar to m4_bpatsubsts, but behaves properly with quotes.
  27. # See m4.at's "Generating Comments". Super inelegant, but so far, I
  28. # did not find any better solution.
  29. m4_define([b4_gsub],
  30. [m4_bpatsubst(m4_bpatsubst(m4_bpatsubst([[[[$1]]]],
  31. [$2], [$3]),
  32. [$4], [$5]),
  33. [$6], [$7])])
  34. # m4_shift2 and m4_shift3 are provided by m4sugar.
  35. m4_define([m4_shift4], [m4_shift(m4_shift(m4_shift(m4_shift($@))))])
  36. ## ---------------- ##
  37. ## Identification. ##
  38. ## ---------------- ##
  39. # b4_generated_by
  40. # ---------------
  41. m4_define([b4_generated_by],
  42. [b4_comment([A Bison parser, made by GNU Bison b4_version_string.])
  43. ])
  44. # b4_copyright(TITLE, [YEARS])
  45. # ----------------------------
  46. # If YEARS are not defined, use b4_copyright_years.
  47. m4_define([b4_copyright],
  48. [b4_generated_by
  49. b4_comment([$1
  50. ]m4_dquote(m4_text_wrap([Copyright (C)
  51. ]m4_ifval([$2], [[$2]], [m4_defn([b4_copyright_years])])[
  52. Free Software Foundation, Inc.]))[
  53. This program is free software: you can redistribute it and/or modify
  54. it under the terms of the GNU General Public License as published by
  55. the Free Software Foundation, either version 3 of the License, or
  56. (at your option) any later version.
  57. This program is distributed in the hope that it will be useful,
  58. but WITHOUT ANY WARRANTY; without even the implied warranty of
  59. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  60. GNU General Public License for more details.
  61. You should have received a copy of the GNU General Public License
  62. along with this program. If not, see <https://www.gnu.org/licenses/>.])
  63. b4_comment([As a special exception, you may create a larger work that contains
  64. part or all of the Bison parser skeleton and distribute that work
  65. under terms of your choice, so long as that work isn't itself a
  66. parser generator using the skeleton or a modified version thereof
  67. as a parser skeleton. Alternatively, if you modify or redistribute
  68. the parser skeleton itself, you may (at your option) remove this
  69. special exception, which will cause the skeleton and the resulting
  70. Bison output files to be licensed under the GNU General Public
  71. License without this special exception.
  72. This special exception was added by the Free Software Foundation in
  73. version 2.2 of Bison.])
  74. ])
  75. # b4_disclaimer
  76. # -------------
  77. # Issue a warning about private implementation details.
  78. m4_define([b4_disclaimer],
  79. [b4_comment([DO NOT RELY ON FEATURES THAT ARE NOT DOCUMENTED in the manual,
  80. especially those whose name start with YY_ or yy_. They are
  81. private implementation details that can be changed or removed.])
  82. ])
  83. # b4_required_version_if(VERSION, IF_NEWER, IF_OLDER)
  84. # ---------------------------------------------------
  85. # If the version %require'd by the user is VERSION (or newer) expand
  86. # IF_NEWER, otherwise IF_OLDER. VERSION should be an integer, e.g.,
  87. # 302 for 3.2.
  88. m4_define([b4_required_version_if],
  89. [m4_if(m4_eval($1 <= b4_required_version),
  90. [1], [$2], [$3])])
  91. ## -------- ##
  92. ## Output. ##
  93. ## -------- ##
  94. # b4_output_begin(FILE1, FILE2)
  95. # -----------------------------
  96. # Enable output, i.e., send to diversion 0, expand after "#", and
  97. # generate the tag to output into FILE. Must be followed by EOL.
  98. # FILE is FILE1 concatenated to FILE2. FILE2 can be empty, or be
  99. # absolute: do the right thing.
  100. m4_define([b4_output_begin],
  101. [m4_changecom()
  102. m4_divert_push(0)dnl
  103. @output(m4_unquote([$1])@,m4_unquote([$2])@)@dnl
  104. ])
  105. # b4_output_end
  106. # -------------
  107. # Output nothing, restore # as comment character (no expansions after #).
  108. m4_define([b4_output_end],
  109. [m4_divert_pop(0)
  110. m4_changecom([#])
  111. ])
  112. # b4_divert_kill(CODE)
  113. # --------------------
  114. # Expand CODE for its side effects, discard its output.
  115. m4_define([b4_divert_kill],
  116. [m4_divert_text([KILL], [$1])])
  117. # b4_define_silent(MACRO, CODE)
  118. # -----------------------------
  119. # Same as m4_define, but throw away the expansion of CODE.
  120. m4_define([b4_define_silent],
  121. [m4_define([$1], [b4_divert_kill([$2])])])
  122. ## ---------------- ##
  123. ## Error handling. ##
  124. ## ---------------- ##
  125. # The following error handling macros print error directives that should not
  126. # become arguments of other macro invocations since they would likely then be
  127. # mangled. Thus, they print to stdout directly.
  128. # b4_cat(TEXT)
  129. # ------------
  130. # Write TEXT to stdout. Precede the final newline with an @ so that it's
  131. # escaped. For example:
  132. #
  133. # b4_cat([[@complain(invalid input@)]])
  134. m4_define([b4_cat],
  135. [m4_syscmd([cat <<'_m4eof'
  136. ]m4_bpatsubst(m4_dquote($1), [_m4eof], [_m4@`eof])[@
  137. _m4eof
  138. ])dnl
  139. m4_if(m4_sysval, [0], [], [m4_fatal([$0: cannot write to stdout])])])
  140. # b4_error(KIND, START, END, FORMAT, [ARG1], [ARG2], ...)
  141. # -------------------------------------------------------
  142. # Write @KIND(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
  143. #
  144. # For example:
  145. #
  146. # b4_error([[complain]], [[input.y:2.3]], [[input.y:5.4]],
  147. # [[invalid %s]], [[foo]])
  148. m4_define([b4_error],
  149. [b4_cat([[@complain][(]$1[@,]$2[@,]$3[@,]$4[]]dnl
  150. [m4_if([$#], [4], [],
  151. [m4_foreach([b4_arg],
  152. m4_dquote(m4_shift4($@)),
  153. [[@,]b4_arg])])[@)]])])
  154. # b4_warn(FORMAT, [ARG1], [ARG2], ...)
  155. # ------------------------------------
  156. # Write @warn(FORMAT@,ARG1@,ARG2@,...@) to stdout.
  157. #
  158. # For example:
  159. #
  160. # b4_warn([[invalid value for '%s': %s]], [[foo]], [[3]])
  161. #
  162. # As a simple test suite, this:
  163. #
  164. # m4_divert(-1)
  165. # m4_define([asdf], [ASDF])
  166. # m4_define([fsa], [FSA])
  167. # m4_define([fdsa], [FDSA])
  168. # b4_warn_at([[[asdf), asdf]]], [[[fsa), fsa]]], [[[fdsa), fdsa]]])
  169. # b4_warn_at([[asdf), asdf]], [[fsa), fsa]], [[fdsa), fdsa]])
  170. # b4_warn_at()
  171. # b4_warn_at(1)
  172. # b4_warn_at(1, 2)
  173. #
  174. # Should produce this without newlines:
  175. #
  176. # @warn_at([asdf), asdf]@,@,@,[fsa), fsa]@,[fdsa), fdsa]@)
  177. # @warn(asdf), asdf@,@,@,fsa), fsa@,fdsa), fdsa@)
  178. # @warn(@)
  179. # @warn(1@)
  180. # @warn(1@,2@)
  181. m4_define([b4_warn],
  182. [b4_warn_at([], [], $@)])
  183. # b4_warn_at(START, END, FORMAT, [ARG1], [ARG2], ...)
  184. # ---------------------------------------------------
  185. # Write @warn(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
  186. #
  187. # For example:
  188. #
  189. # b4_warn_at([[input.y:2.3]], [[input.y:5.4]], [[invalid %s]], [[foo]])
  190. m4_define([b4_warn_at],
  191. [b4_error([[warn]], $@)])
  192. # b4_complain(FORMAT, [ARG1], [ARG2], ...)
  193. # ----------------------------------------
  194. # Bounce to b4_complain_at.
  195. #
  196. # See b4_warn example.
  197. m4_define([b4_complain],
  198. [b4_complain_at([], [], $@)])
  199. # b4_complain_at(START, END, FORMAT, [ARG1], [ARG2], ...)
  200. # -------------------------------------------------------
  201. # Write @complain(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
  202. #
  203. # See b4_warn_at example.
  204. m4_define([b4_complain_at],
  205. [b4_error([[complain]], $@)])
  206. # b4_fatal(FORMAT, [ARG1], [ARG2], ...)
  207. # -------------------------------------
  208. # Bounce to b4_fatal_at.
  209. #
  210. # See b4_warn example.
  211. m4_define([b4_fatal],
  212. [b4_fatal_at([], [], $@)])
  213. # b4_fatal_at(START, END, FORMAT, [ARG1], [ARG2], ...)
  214. # ----------------------------------------------------
  215. # Write @fatal(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout and exit.
  216. #
  217. # See b4_warn_at example.
  218. m4_define([b4_fatal_at],
  219. [b4_error([[fatal]], $@)dnl
  220. m4_exit(1)])
  221. ## ------------ ##
  222. ## Data Types. ##
  223. ## ------------ ##
  224. # b4_ints_in(INT1, INT2, LOW, HIGH)
  225. # ---------------------------------
  226. # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
  227. m4_define([b4_ints_in],
  228. [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
  229. # b4_subtract(LHS, RHS)
  230. # ---------------------
  231. # Evaluate LHS - RHS if they are integer literals, otherwise expand
  232. # to (LHS) - (RHS).
  233. m4_define([b4_subtract],
  234. [m4_bmatch([$1$2], [^[0123456789]*$],
  235. [m4_eval([$1 - $2])],
  236. [($1) - ($2)])])
  237. # b4_join(ARG1, ...)
  238. # _b4_join(ARG1, ...)
  239. # -------------------
  240. # Join with comma, skipping empty arguments.
  241. # b4_join calls itself recursively until it sees the first non-empty
  242. # argument, then calls _b4_join (i.e., `_$0`) which prepends each
  243. # non-empty argument with a comma.
  244. m4_define([b4_join],
  245. [m4_if([$#$1],
  246. [1], [],
  247. [m4_ifval([$1],
  248. [$1[]_$0(m4_shift($@))],
  249. [$0(m4_shift($@))])])])
  250. # _b4_join(ARGS1, ...)
  251. # --------------------
  252. m4_define([_b4_join],
  253. [m4_if([$#$1],
  254. [1], [],
  255. [m4_ifval([$1], [, $1])[]$0(m4_shift($@))])])
  256. # b4_integral_parser_tables_map(MACRO)
  257. # -------------------------------------
  258. # Map MACRO on all the integral tables. MACRO is expected to have
  259. # the signature MACRO(TABLE-NAME, CONTENT, COMMENT).
  260. m4_define([b4_integral_parser_tables_map],
  261. [$1([pact], [b4_pact],
  262. [[YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
  263. STATE-NUM.]])
  264. $1([defact], [b4_defact],
  265. [[YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
  266. Performed when YYTABLE does not specify something else to do. Zero
  267. means the default is an error.]])
  268. $1([pgoto], [b4_pgoto], [[YYPGOTO[NTERM-NUM].]])
  269. $1([defgoto], [b4_defgoto], [[YYDEFGOTO[NTERM-NUM].]])
  270. $1([table], [b4_table],
  271. [[YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If
  272. positive, shift that token. If negative, reduce the rule whose
  273. number is the opposite. If YYTABLE_NINF, syntax error.]])
  274. $1([check], [b4_check])
  275. $1([stos], [b4_stos],
  276. [[YYSTOS[STATE-NUM] -- The (internal number of the) accessing
  277. symbol of state STATE-NUM.]])
  278. $1([r1], [b4_r1],
  279. [[YYR1[YYN] -- Symbol number of symbol that rule YYN derives.]])
  280. $1([r2], [b4_r2],
  281. [[YYR2[YYN] -- Number of symbols on the right hand side of rule YYN.]])
  282. ])
  283. # b4_parser_tables_declare
  284. # b4_parser_tables_define
  285. # ------------------------
  286. # Define/declare the (deterministic) parser tables.
  287. m4_define([b4_parser_tables_declare],
  288. [b4_integral_parser_tables_map([b4_integral_parser_table_declare])])
  289. m4_define([b4_parser_tables_define],
  290. [b4_integral_parser_tables_map([b4_integral_parser_table_define])])
  291. ## ------------------ ##
  292. ## Decoding options. ##
  293. ## ------------------ ##
  294. # b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
  295. # -----------------------------------
  296. # Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
  297. m4_define([b4_flag_if],
  298. [m4_case(b4_$1_flag,
  299. [0], [$3],
  300. [1], [$2],
  301. [m4_fatal([invalid $1 value: ]b4_$1_flag)])])
  302. # b4_define_flag_if(FLAG)
  303. # -----------------------
  304. # Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
  305. # value of the Boolean FLAG.
  306. m4_define([b4_define_flag_if],
  307. [_b4_define_flag_if($[1], $[2], [$1])])
  308. # _b4_define_flag_if($1, $2, FLAG)
  309. # --------------------------------
  310. # Work around the impossibility to define macros inside macros,
  311. # because issuing '[$1]' is not possible in M4. GNU M4 should provide
  312. # $$1 a la M5/TeX.
  313. m4_define([_b4_define_flag_if],
  314. [m4_if([$1$2], $[1]$[2], [],
  315. [m4_fatal([$0: Invalid arguments: $@])])dnl
  316. m4_define([b4_$3_if],
  317. [b4_flag_if([$3], [$1], [$2])])])
  318. # b4_FLAG_if(IF-TRUE, IF-FALSE)
  319. # -----------------------------
  320. # Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
  321. b4_define_flag_if([defines]) # Whether headers are requested.
  322. b4_define_flag_if([glr]) # Whether a GLR parser is requested.
  323. b4_define_flag_if([has_translations]) # Whether some tokens are internationalized.
  324. b4_define_flag_if([nondeterministic]) # Whether conflicts should be handled.
  325. b4_define_flag_if([token_table]) # Whether yytoken_table is demanded.
  326. b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated.
  327. # b4_glr_cc_if([IF-TRUE], [IF-FALSE])
  328. # -----------------------------------
  329. m4_define([b4_glr_cc_if],
  330. [m4_if(b4_skeleton, ["glr.cc"], $@)])
  331. ## --------- ##
  332. ## Symbols. ##
  333. ## --------- ##
  334. # For a description of the Symbol handling, see README.md.
  335. #
  336. # The following macros provide access to symbol related values.
  337. # __b4_symbol(NUM, FIELD)
  338. # -----------------------
  339. # Fetch FIELD of symbol #NUM. Fail if undefined.
  340. m4_define([__b4_symbol],
  341. [m4_indir([b4_symbol($1, $2)])])
  342. # _b4_symbol(NUM, FIELD)
  343. # ----------------------
  344. # Fetch FIELD of symbol #NUM (or "orig NUM", see README.md).
  345. # Fail if undefined.
  346. m4_define([_b4_symbol],
  347. [m4_ifdef([b4_symbol($1, number)],
  348. [__b4_symbol(m4_indir([b4_symbol($1, number)]), $2)],
  349. [__b4_symbol([$1], [$2])])])
  350. # b4_symbol_token_kind(NUM)
  351. # -------------------------
  352. # The token kind of this symbol.
  353. m4_define([b4_symbol_token_kind],
  354. [b4_percent_define_get([api.token.prefix])dnl
  355. _b4_symbol([$1], [id])])
  356. # b4_symbol_kind_base(NUM)
  357. # ------------------------
  358. # Build the name of the kind of this symbol. It must always exist,
  359. # otherwise some symbols might not be represented in the enum, which
  360. # might be compiled into too small a type to contain all the symbol
  361. # numbers.
  362. m4_define([b4_symbol_prefix], [b4_percent_define_get([api.symbol.prefix])])
  363. m4_define([b4_symbol_kind_base],
  364. [b4_percent_define_get([api.symbol.prefix])dnl
  365. m4_case([$1],
  366. [-2], [[YYEMPTY]],
  367. [0], [[YYEOF]],
  368. [1], [[YYerror]],
  369. [2], [[YYUNDEF]],
  370. [m4_case(b4_symbol([$1], [tag]),
  371. [$accept], [[YYACCEPT]],
  372. [b4_symbol_if([$1], [has_id], _b4_symbol([$1], [id]),
  373. [m4_bpatsubst([$1-][]_b4_symbol([$1], [tag]), [[^a-zA-Z_0-9]+], [_])])])])])
  374. # b4_symbol_kind(NUM)
  375. # -------------------
  376. # Same as b4_symbol_kind, but possibly with a prefix in some
  377. # languages. E.g., EOF's kind_base and kind are YYSYMBOL_YYEOF in C,
  378. # but are S_YYEMPTY and symbol_kind::S_YYEMPTY in C++.
  379. m4_copy([b4_symbol_kind_base], [b4_symbol_kind])
  380. # b4_symbol_slot(NUM)
  381. # -------------------
  382. # The name of union member that contains the value of these symbols.
  383. # Currently, we are messy, this should actually be type_tag, but type_tag
  384. # has several meanings.
  385. m4_define([b4_symbol_slot],
  386. [m4_case(b4_percent_define_get([[api.value.type]]),
  387. [union], [b4_symbol([$1], [type_tag])],
  388. [variant], [b4_symbol([$1], [type_tag])],
  389. [b4_symbol([$1], [type])])])
  390. # b4_symbol(NUM, FIELD)
  391. # ---------------------
  392. # Fetch FIELD of symbol #NUM (or "orig NUM"). Fail if undefined.
  393. #
  394. # If FIELD = id, prepend the token prefix.
  395. m4_define([b4_symbol],
  396. [m4_case([$2],
  397. [id], [b4_symbol_token_kind([$1])],
  398. [kind_base], [b4_symbol_kind_base([$1])],
  399. [kind], [b4_symbol_kind([$1])],
  400. [slot], [b4_symbol_slot([$1])],
  401. [_b4_symbol($@)])])
  402. # b4_symbol_if(NUM, FIELD, IF-TRUE, IF-FALSE)
  403. # -------------------------------------------
  404. # If FIELD about symbol #NUM is 1 expand IF-TRUE, if is 0, expand IF-FALSE.
  405. # Otherwise an error.
  406. m4_define([b4_symbol_if],
  407. [m4_case(b4_symbol([$1], [$2]),
  408. [1], [$3],
  409. [0], [$4],
  410. [m4_fatal([$0: field $2 of $1 is not a Boolean:] b4_symbol([$1], [$2]))])])
  411. # b4_symbol_tag_comment(SYMBOL-NUM)
  412. # ---------------------------------
  413. # Issue a comment giving the tag of symbol NUM.
  414. m4_define([b4_symbol_tag_comment],
  415. [b4_comment([b4_symbol([$1], [tag])])
  416. ])
  417. # b4_symbol_action(SYMBOL-NUM, ACTION)
  418. # ------------------------------------
  419. # Run the action ACTION ("destructor" or "printer") for SYMBOL-NUM.
  420. m4_define([b4_symbol_action],
  421. [b4_symbol_if([$1], [has_$2],
  422. [b4_dollar_pushdef([(*yyvaluep)],
  423. [$1],
  424. [],
  425. [(*yylocationp)])dnl
  426. _b4_symbol_case([$1])[]dnl
  427. b4_syncline([b4_symbol([$1], [$2_line])], [b4_symbol([$1], [$2_file])])dnl
  428. b4_symbol([$1], [$2])
  429. b4_syncline([@oline@], [@ofile@])dnl
  430. break;
  431. b4_dollar_popdef[]dnl
  432. ])])
  433. # b4_symbol_destructor(SYMBOL-NUM)
  434. # b4_symbol_printer(SYMBOL-NUM)
  435. # --------------------------------
  436. m4_define([b4_symbol_destructor], [b4_symbol_action([$1], [destructor])])
  437. m4_define([b4_symbol_printer], [b4_symbol_action([$1], [printer])])
  438. # b4_symbol_actions(ACTION, [KIND = yykind])
  439. # ------------------------------------------
  440. # Emit the symbol actions for ACTION ("destructor" or "printer").
  441. # Dispatch on KIND.
  442. m4_define([b4_symbol_actions],
  443. [m4_pushdef([b4_actions_], m4_expand([b4_symbol_foreach([b4_symbol_$1])]))dnl
  444. m4_ifval(m4_defn([b4_actions_]),
  445. [switch (m4_default([$2], [yykind]))
  446. {
  447. m4_defn([b4_actions_])[]dnl
  448. default:
  449. break;
  450. }dnl
  451. ],
  452. [YY_USE (m4_default([$2], [yykind]));])dnl
  453. m4_popdef([b4_actions_])dnl
  454. ])
  455. # _b4_symbol_case(SYMBOL-NUM)
  456. # ---------------------------
  457. # Issue a "case NUM" for SYMBOL-NUM. Ends with its EOL to make it
  458. # easier to use with m4_map, but then, use []dnl to suppress the last
  459. # one.
  460. m4_define([_b4_symbol_case],
  461. [case b4_symbol([$1], [kind]): b4_symbol_tag_comment([$1])])
  462. ])
  463. # b4_symbol_foreach(MACRO)
  464. # ------------------------
  465. # Invoke MACRO(SYMBOL-NUM) for each SYMBOL-NUM.
  466. m4_define([b4_symbol_foreach],
  467. [m4_map([$1], m4_defn([b4_symbol_numbers]))])
  468. # b4_symbol_map(MACRO)
  469. # --------------------
  470. # Return a list (possibly empty elements) of MACRO invoked for each
  471. # SYMBOL-NUM.
  472. m4_define([b4_symbol_map],
  473. [m4_map_args_sep([$1(], [)], [,], b4_symbol_numbers)])
  474. # b4_token_visible_if(NUM, IF-TRUE, IF-FALSE)
  475. # -------------------------------------------
  476. # Whether NUM denotes a token kind that has an exported definition
  477. # (i.e., shows in enum yytokentype).
  478. m4_define([b4_token_visible_if],
  479. [b4_symbol_if([$1], [is_token],
  480. [b4_symbol_if([$1], [has_id], [$2], [$3])],
  481. [$3])])
  482. # b4_token_has_definition(NUM)
  483. # ----------------------------
  484. # 1 if NUM is visible, nothing otherwise.
  485. m4_define([b4_token_has_definition],
  486. [b4_token_visible_if([$1], [1])])
  487. # b4_any_token_visible_if([IF-TRUE], [IF-FALSE])
  488. # ----------------------------------------------
  489. # Whether there is a token that needs to be defined.
  490. m4_define([b4_any_token_visible_if],
  491. [m4_ifval(b4_symbol_foreach([b4_token_has_definition]),
  492. [$1], [$2])])
  493. # b4_token_format(FORMAT, NUM)
  494. # ----------------------------
  495. # If token NUM has a visible ID, format FORMAT with ID, USER_NUMBER.
  496. m4_define([b4_token_format],
  497. [b4_token_visible_if([$2],
  498. [m4_format([[$1]],
  499. b4_symbol([$2], [id]),
  500. b4_symbol([$2], b4_api_token_raw_if([[number]], [[code]])))])])
  501. # b4_last_enum_token
  502. # ------------------
  503. # The code of the last token visible token.
  504. m4_define([_b4_last_enum_token],
  505. [b4_token_visible_if([$1],
  506. [m4_define([b4_last_enum_token], [$1])])])
  507. b4_symbol_foreach([_b4_last_enum_token])
  508. # b4_last_symbol
  509. # --------------
  510. # The code of the last symbol.
  511. m4_define([b4_last_symbol], m4_eval(b4_tokens_number + b4_nterms_number - 1))
  512. ## ------- ##
  513. ## Types. ##
  514. ## ------- ##
  515. # _b4_type_action(NUMS)
  516. # ---------------------
  517. # Run actions for the symbol NUMS that all have the same type-name.
  518. # Skip NUMS that have no type-name.
  519. #
  520. # To specify the action to run, define b4_dollar_dollar(SYMBOL-NUM,
  521. # TAG, TYPE).
  522. m4_define([_b4_type_action],
  523. [b4_symbol_if([$1], [has_type],
  524. [m4_map([ _b4_symbol_case], [$@])[]dnl
  525. b4_dollar_dollar([b4_symbol([$1], [number])],
  526. [b4_symbol([$1], [tag])],
  527. [b4_symbol([$1], [type])]);
  528. break;
  529. ])])
  530. # b4_type_foreach(MACRO, [SEP])
  531. # -----------------------------
  532. # Invoke MACRO(SYMBOL-NUMS) for each set of SYMBOL-NUMS for each type set.
  533. m4_define([b4_type_foreach],
  534. [m4_map_sep([$1], [$2], m4_defn([b4_type_names]))])
  535. ## ----------- ##
  536. ## Synclines. ##
  537. ## ----------- ##
  538. # b4_basename(NAME)
  539. # -----------------
  540. # Similar to POSIX basename; the differences don't matter here.
  541. # Beware that NAME is not evaluated.
  542. m4_define([b4_basename],
  543. [m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])
  544. # b4_syncline(LINE, FILE)dnl
  545. # --------------------------
  546. # Should always be following by a dnl.
  547. #
  548. # Emit "#line LINE FILE /* __LINE__ __FILE__ */".
  549. m4_define([b4_syncline],
  550. [b4_flag_if([synclines],
  551. [b4_sync_start([$1], [$2])[]dnl
  552. b4_sync_end([__line__], [b4_basename(m4_quote(__file__))])
  553. ])])
  554. # b4_sync_start(LINE, FILE)
  555. # -----------------------
  556. # Syncline for the new place. Typically a directive for the compiler.
  557. m4_define([b4_sync_start], [b4_comment([$2:$1])])
  558. # b4_sync_end(LINE, FILE)
  559. # -----------------------
  560. # Syncline for the current place, which ends. Typically a comment
  561. # left for the reader.
  562. m4_define([b4_sync_end], [ b4_comment([$2:$1])]
  563. )
  564. # This generates dependencies on the Bison skeletons hence lots of
  565. # useless 'git diff'. This location is useless for the regular
  566. # user (who does not care about the skeletons) and is actually not
  567. # useful for Bison developers too (I, Akim, never used this to locate
  568. # the code in skeletons that generated output). So disable it
  569. # completely. If someone thinks this was actually useful, a %define
  570. # variable should be provided to control the level of verbosity of
  571. # '#line', in replacement of --no-lines.
  572. m4_define([b4_sync_end])
  573. # b4_user_code(USER-CODE)
  574. # -----------------------
  575. # Emit code from the user, ending it with synclines.
  576. m4_define([b4_user_code],
  577. [$1
  578. b4_syncline([@oline@], [@ofile@])])
  579. # b4_define_user_code(MACRO, COMMENT)
  580. # -----------------------------------
  581. # From b4_MACRO, if defined, build b4_user_MACRO that includes the synclines.
  582. m4_define([b4_define_user_code],
  583. [m4_define([b4_user_$1],
  584. [m4_ifdef([b4_$1],
  585. [m4_ifval([$2],
  586. [b4_comment([$2])
  587. ])b4_user_code([b4_$1])])])])
  588. # b4_user_actions
  589. # b4_user_initial_action
  590. # b4_user_post_prologue
  591. # b4_user_pre_prologue
  592. # b4_user_union_members
  593. # ----------------------
  594. # Macros that issue user code, ending with synclines.
  595. b4_define_user_code([actions])
  596. b4_define_user_code([initial_action], [User initialization code.])
  597. b4_define_user_code([post_prologue], [Second part of user prologue.])
  598. b4_define_user_code([pre_prologue], [First part of user prologue.])
  599. b4_define_user_code([union_members])
  600. # b4_check_user_names(WHAT, USER-LIST, BISON-NAMESPACE)
  601. # -----------------------------------------------------
  602. # Complain if any name of type WHAT is used by the user (as recorded in
  603. # USER-LIST) but is not used by Bison (as recorded by macros in the
  604. # namespace BISON-NAMESPACE).
  605. #
  606. # USER-LIST must expand to a list specifying all user occurrences of all names
  607. # of type WHAT. Each item in the list must be a triplet specifying one
  608. # occurrence: name, start boundary, and end boundary. Empty string names are
  609. # fine. An empty list is fine.
  610. #
  611. # For example, to define b4_foo_user_names to be used for USER-LIST with three
  612. # name occurrences and with correct quoting:
  613. #
  614. # m4_define([b4_foo_user_names],
  615. # [[[[[[bar]], [[parser.y:1.7]], [[parser.y:1.16]]]],
  616. # [[[[bar]], [[parser.y:5.7]], [[parser.y:5.16]]]],
  617. # [[[[baz]], [[parser.y:8.7]], [[parser.y:8.16]]]]]])
  618. #
  619. # The macro BISON-NAMESPACE(bar) must be defined iff the name bar of type WHAT
  620. # is used by Bison (in the front-end or in the skeleton). Empty string names
  621. # are fine, but it would be ugly for Bison to actually use one.
  622. #
  623. # For example, to use b4_foo_bison_names for BISON-NAMESPACE and define that
  624. # the names bar and baz are used by Bison:
  625. #
  626. # m4_define([b4_foo_bison_names(bar)])
  627. # m4_define([b4_foo_bison_names(baz)])
  628. #
  629. # To invoke b4_check_user_names with TYPE foo, with USER-LIST
  630. # b4_foo_user_names, with BISON-NAMESPACE b4_foo_bison_names, and with correct
  631. # quoting:
  632. #
  633. # b4_check_user_names([[foo]], [b4_foo_user_names],
  634. # [[b4_foo_bison_names]])
  635. m4_define([b4_check_user_names],
  636. [m4_foreach([b4_occurrence], $2,
  637. [m4_pushdef([b4_occurrence], b4_occurrence)dnl
  638. m4_pushdef([b4_user_name], m4_car(b4_occurrence))dnl
  639. m4_pushdef([b4_start], m4_car(m4_shift(b4_occurrence)))dnl
  640. m4_pushdef([b4_end], m4_shift2(b4_occurrence))dnl
  641. m4_ifndef($3[(]m4_quote(b4_user_name)[)],
  642. [b4_complain_at([b4_start], [b4_end],
  643. [[%s '%s' is not used]],
  644. [$1], [b4_user_name])])[]dnl
  645. m4_popdef([b4_occurrence])dnl
  646. m4_popdef([b4_user_name])dnl
  647. m4_popdef([b4_start])dnl
  648. m4_popdef([b4_end])dnl
  649. ])])
  650. ## --------------------- ##
  651. ## b4_percent_define_*. ##
  652. ## --------------------- ##
  653. # b4_percent_define_use(VARIABLE)
  654. # -------------------------------
  655. # Declare that VARIABLE was used.
  656. m4_define([b4_percent_define_use],
  657. [m4_define([b4_percent_define_bison_variables(]$1[)])dnl
  658. ])
  659. # b4_percent_define_get(VARIABLE, [DEFAULT])
  660. # ------------------------------------------
  661. # Mimic muscle_percent_define_get in ../src/muscle-tab.h. That is, if
  662. # the %define variable VARIABLE is defined, emit its value. Contrary
  663. # to its C counterpart, return DEFAULT otherwise. Also, record
  664. # Bison's usage of VARIABLE by defining
  665. # b4_percent_define_bison_variables(VARIABLE).
  666. #
  667. # For example:
  668. #
  669. # b4_percent_define_get([[foo]])
  670. m4_define([b4_percent_define_get],
  671. [b4_percent_define_use([$1])dnl
  672. _b4_percent_define_ifdef([$1],
  673. [m4_indir([b4_percent_define(]$1[)])],
  674. [$2])])
  675. # b4_percent_define_get_loc(VARIABLE)
  676. # -----------------------------------
  677. # Mimic muscle_percent_define_get_loc in ../src/muscle-tab.h exactly. That is,
  678. # if the %define variable VARIABLE is undefined, complain fatally since that's
  679. # a Bison or skeleton error. Otherwise, return its definition location in a
  680. # form appropriate for the first two arguments of b4_warn_at, b4_complain_at, or
  681. # b4_fatal_at. Don't record this as a Bison usage of VARIABLE as there's no
  682. # reason to suspect that the user-supplied value has yet influenced the output.
  683. #
  684. # For example:
  685. #
  686. # b4_complain_at(b4_percent_define_get_loc([[foo]]), [[invalid foo]])
  687. m4_define([b4_percent_define_get_loc],
  688. [m4_ifdef([b4_percent_define_loc(]$1[)],
  689. [m4_pushdef([b4_loc], m4_indir([b4_percent_define_loc(]$1[)]))dnl
  690. b4_loc[]dnl
  691. m4_popdef([b4_loc])],
  692. [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
  693. # b4_percent_define_get_kind(VARIABLE)
  694. # ------------------------------------
  695. # Get the kind (code, keyword, string) of VARIABLE, i.e., how its
  696. # value was defined (braces, not delimiters, quotes).
  697. #
  698. # If the %define variable VARIABLE is undefined, complain fatally
  699. # since that's a Bison or skeleton error. Don't record this as a
  700. # Bison usage of VARIABLE as there's no reason to suspect that the
  701. # user-supplied value has yet influenced the output.
  702. m4_define([b4_percent_define_get_kind],
  703. [m4_ifdef([b4_percent_define_kind(]$1[)],
  704. [m4_indir([b4_percent_define_kind(]$1[)])],
  705. [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
  706. # b4_percent_define_get_syncline(VARIABLE)dnl
  707. # -------------------------------------------
  708. # Should always be following by a dnl.
  709. #
  710. # Mimic muscle_percent_define_get_syncline in ../src/muscle-tab.h exactly.
  711. # That is, if the %define variable VARIABLE is undefined, complain fatally
  712. # since that's a Bison or skeleton error. Otherwise, return its definition
  713. # location as a b4_syncline invocation. Don't record this as a Bison usage of
  714. # VARIABLE as there's no reason to suspect that the user-supplied value has yet
  715. # influenced the output.
  716. #
  717. # For example:
  718. #
  719. # b4_percent_define_get_syncline([[foo]])
  720. m4_define([b4_percent_define_get_syncline],
  721. [m4_ifdef([b4_percent_define_syncline(]$1[)],
  722. [m4_indir([b4_percent_define_syncline(]$1[)])],
  723. [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
  724. # _b4_percent_define_ifdef(VARIABLE, IF-TRUE, [IF-FALSE])
  725. # ------------------------------------------------------
  726. # If the %define variable VARIABLE is defined, expand IF-TRUE, else expand
  727. # IF-FALSE. Don't record usage of VARIABLE.
  728. #
  729. # For example:
  730. #
  731. # _b4_percent_define_ifdef([[foo]], [[it's defined]], [[it's undefined]])
  732. m4_define([_b4_percent_define_ifdef],
  733. [m4_ifdef([b4_percent_define(]$1[)],
  734. [$2],
  735. [$3])])
  736. # b4_percent_define_ifdef(VARIABLE, IF-TRUE, [IF-FALSE])
  737. # ------------------------------------------------------
  738. # Mimic muscle_percent_define_ifdef in ../src/muscle-tab.h exactly. That is,
  739. # if the %define variable VARIABLE is defined, expand IF-TRUE, else expand
  740. # IF-FALSE. Also, record Bison's usage of VARIABLE by defining
  741. # b4_percent_define_bison_variables(VARIABLE).
  742. #
  743. # For example:
  744. #
  745. # b4_percent_define_ifdef([[foo]], [[it's defined]], [[it's undefined]])
  746. m4_define([b4_percent_define_ifdef],
  747. [_b4_percent_define_ifdef([$1],
  748. [b4_percent_define_use([$1])$2],
  749. [$3])])
  750. # b4_percent_define_check_file_complain(VARIABLE)
  751. # -----------------------------------------------
  752. # Warn about %define variable VARIABLE having an incorrect
  753. # value.
  754. m4_define([b4_percent_define_check_file_complain],
  755. [b4_complain_at(b4_percent_define_get_loc([$1]),
  756. [[%%define variable '%s' requires 'none' or '"..."' values]],
  757. [$1])])
  758. # b4_percent_define_check_file(MACRO, VARIABLE, DEFAULT)
  759. # ------------------------------------------------------
  760. # If the %define variable VARIABLE:
  761. # - is undefined, then if DEFAULT is non-empty, define MACRO to DEFAULT
  762. # - is a string, define MACRO to its value
  763. # - is the keyword 'none', do nothing
  764. # - otherwise, warn about the incorrect value.
  765. m4_define([b4_percent_define_check_file],
  766. [b4_percent_define_ifdef([$2],
  767. [m4_case(b4_percent_define_get_kind([$2]),
  768. [string],
  769. [m4_define([$1], b4_percent_define_get([$2]))],
  770. [keyword],
  771. [m4_if(b4_percent_define_get([$2]), [none], [],
  772. [b4_percent_define_check_file_complain([$2])])],
  773. [b4_percent_define_check_file_complain([$2])])
  774. ],
  775. [m4_ifval([$3],
  776. [m4_define([$1], [$3])])])
  777. ])
  778. ## --------- ##
  779. ## Options. ##
  780. ## --------- ##
  781. # b4_percent_define_flag_if(VARIABLE, IF-TRUE, [IF-FALSE])
  782. # --------------------------------------------------------
  783. # Mimic muscle_percent_define_flag_if in ../src/muscle-tab.h exactly. That is,
  784. # if the %define variable VARIABLE is defined to "" or "true", expand IF-TRUE.
  785. # If it is defined to "false", expand IF-FALSE. Complain if it is undefined
  786. # (a Bison or skeleton error since the default value should have been set
  787. # already) or defined to any other value (possibly a user error). Also, record
  788. # Bison's usage of VARIABLE by defining
  789. # b4_percent_define_bison_variables(VARIABLE).
  790. #
  791. # For example:
  792. #
  793. # b4_percent_define_flag_if([[foo]], [[it's true]], [[it's false]])
  794. m4_define([b4_percent_define_flag_if],
  795. [b4_percent_define_ifdef([$1],
  796. [m4_case(b4_percent_define_get([$1]),
  797. [], [$2], [true], [$2], [false], [$3],
  798. [m4_expand_once([b4_complain_at(b4_percent_define_get_loc([$1]),
  799. [[invalid value for %%define Boolean variable '%s']],
  800. [$1])],
  801. [[b4_percent_define_flag_if($1)]])])],
  802. [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
  803. # b4_percent_define_default(VARIABLE, DEFAULT, [KIND = keyword])
  804. # --------------------------------------------------------------
  805. # Mimic muscle_percent_define_default in ../src/muscle-tab.h exactly. That is,
  806. # if the %define variable VARIABLE is undefined, set its value to DEFAULT.
  807. # Don't record this as a Bison usage of VARIABLE as there's no reason to
  808. # suspect that the value has yet influenced the output.
  809. #
  810. # For example:
  811. #
  812. # b4_percent_define_default([[foo]], [[default value]])
  813. m4_define([b4_percent_define_default],
  814. [_b4_percent_define_ifdef([$1], [],
  815. [m4_define([b4_percent_define(]$1[)], [$2])dnl
  816. m4_define([b4_percent_define_kind(]$1[)],
  817. [m4_default([$3], [keyword])])dnl
  818. m4_define([b4_percent_define_loc(]$1[)],
  819. [[[[<skeleton default value>:-1.-1]],
  820. [[<skeleton default value>:-1.-1]]]])dnl
  821. m4_define([b4_percent_define_syncline(]$1[)], [[]])])])
  822. # b4_percent_define_if_define(NAME, [VARIABLE = NAME])
  823. # ----------------------------------------------------
  824. # Define b4_NAME_if that executes its $1 or $2 depending whether
  825. # VARIABLE was %defined. The characters '.' and `-' in VARIABLE are mapped
  826. # to '_'.
  827. m4_define([_b4_percent_define_if_define],
  828. [m4_define(m4_bpatsubst([b4_$1_if], [[-.]], [_]),
  829. [b4_percent_define_flag_if(m4_default([$2], [$1]),
  830. [$3], [$4])])])
  831. m4_define([b4_percent_define_if_define],
  832. [b4_percent_define_default([m4_default([$2], [$1])], [[false]])
  833. _b4_percent_define_if_define([$1], [$2], $[1], $[2])])
  834. # b4_percent_define_check_kind(VARIABLE, KIND, [DIAGNOSTIC = complain])
  835. # ---------------------------------------------------------------------
  836. m4_define([b4_percent_define_check_kind],
  837. [_b4_percent_define_ifdef([$1],
  838. [m4_if(b4_percent_define_get_kind([$1]), [$2], [],
  839. [b4_error([m4_default([$3], [complain])],
  840. b4_percent_define_get_loc([$1]),
  841. [m4_case([$2],
  842. [code], [[%%define variable '%s' requires '{...}' values]],
  843. [keyword], [[%%define variable '%s' requires keyword values]],
  844. [string], [[%%define variable '%s' requires '"..."' values]])],
  845. [$1])])])dnl
  846. ])
  847. # b4_percent_define_check_values(VALUES)
  848. # --------------------------------------
  849. # Mimic muscle_percent_define_check_values in ../src/muscle-tab.h exactly
  850. # except that the VALUES structure is more appropriate for M4. That is, VALUES
  851. # is a list of sublists of strings. For each sublist, the first string is the
  852. # name of a %define variable, and all remaining strings in that sublist are the
  853. # valid values for that variable. Complain if such a variable is undefined (a
  854. # Bison error since the default value should have been set already) or defined
  855. # to any other value (possibly a user error). Don't record this as a Bison
  856. # usage of the variable as there's no reason to suspect that the value has yet
  857. # influenced the output.
  858. #
  859. # For example:
  860. #
  861. # b4_percent_define_check_values([[[[foo]], [[foo-value1]], [[foo-value2]]]],
  862. # [[[[bar]], [[bar-value1]]]])
  863. m4_define([b4_percent_define_check_values],
  864. [m4_foreach([b4_sublist], m4_quote($@),
  865. [_b4_percent_define_check_values(b4_sublist)])])
  866. m4_define([_b4_percent_define_check_values],
  867. [_b4_percent_define_ifdef([$1],
  868. [b4_percent_define_check_kind(]$1[, [keyword], [deprecated])dnl
  869. m4_pushdef([b4_good_value], [0])dnl
  870. m4_if($#, 1, [],
  871. [m4_foreach([b4_value], m4_dquote(m4_shift($@)),
  872. [m4_if(m4_indir([b4_percent_define(]$1[)]), b4_value,
  873. [m4_define([b4_good_value], [1])])])])dnl
  874. m4_if(b4_good_value, [0],
  875. [b4_complain_at(b4_percent_define_get_loc([$1]),
  876. [[invalid value for %%define variable '%s': '%s']],
  877. [$1],
  878. m4_dquote(m4_indir([b4_percent_define(]$1[)])))
  879. m4_foreach([b4_value], m4_dquote(m4_shift($@)),
  880. [b4_error([[note]], b4_percent_define_get_loc([$1]), []
  881. [[accepted value: '%s']],
  882. m4_dquote(b4_value))])])dnl
  883. m4_popdef([b4_good_value])],
  884. [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
  885. # b4_percent_code_get([QUALIFIER])
  886. # --------------------------------
  887. # If any %code blocks for QUALIFIER are defined, emit them beginning with a
  888. # comment and ending with synclines and a newline. If QUALIFIER is not
  889. # specified or empty, do this for the unqualified %code blocks. Also, record
  890. # Bison's usage of QUALIFIER (if specified) by defining
  891. # b4_percent_code_bison_qualifiers(QUALIFIER).
  892. #
  893. # For example, to emit any unqualified %code blocks followed by any %code
  894. # blocks for the qualifier foo:
  895. #
  896. # b4_percent_code_get
  897. # b4_percent_code_get([[foo]])
  898. m4_define([b4_percent_code_get],
  899. [m4_pushdef([b4_macro_name], [[b4_percent_code(]$1[)]])dnl
  900. m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])dnl
  901. m4_ifdef(b4_macro_name,
  902. [b4_comment(m4_if([$#], [0], [[[Unqualified %code blocks.]]],
  903. [[["%code ]$1[" blocks.]]]))
  904. b4_user_code([m4_indir(b4_macro_name)])])dnl
  905. m4_popdef([b4_macro_name])])
  906. # b4_percent_code_ifdef(QUALIFIER, IF-TRUE, [IF-FALSE])
  907. # -----------------------------------------------------
  908. # If any %code blocks for QUALIFIER (or unqualified %code blocks if
  909. # QUALIFIER is empty) are defined, expand IF-TRUE, else expand IF-FALSE.
  910. # Also, record Bison's usage of QUALIFIER (if specified) by defining
  911. # b4_percent_code_bison_qualifiers(QUALIFIER).
  912. m4_define([b4_percent_code_ifdef],
  913. [m4_ifdef([b4_percent_code(]$1[)],
  914. [m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])$2],
  915. [$3])])
  916. ## ------------------ ##
  917. ## Common variables. ##
  918. ## ------------------ ##
  919. # b4_parse_assert_if([IF-ASSERTIONS-ARE-USED], [IF-NOT])
  920. # b4_parse_trace_if([IF-DEBUG-TRACES-ARE-ENABLED], [IF-NOT])
  921. # b4_token_ctor_if([IF-YYLEX-RETURNS-A-TOKEN], [IF-NOT])
  922. # ----------------------------------------------------------
  923. b4_percent_define_if_define([api.token.raw])
  924. b4_percent_define_if_define([token_ctor], [api.token.constructor])
  925. b4_percent_define_if_define([locations]) # Whether locations are tracked.
  926. b4_percent_define_if_define([parse.assert])
  927. b4_percent_define_if_define([parse.trace])
  928. # b4_bison_locations_if([IF-TRUE])
  929. # --------------------------------
  930. # Expand IF-TRUE if using locations, and using the default location
  931. # type.
  932. m4_define([b4_bison_locations_if],
  933. [b4_locations_if([b4_percent_define_ifdef([[api.location.type]], [], [$1])])])
  934. # %define parse.error "(custom|detailed|simple|verbose)"
  935. # ------------------------------------------------------
  936. b4_percent_define_default([[parse.error]], [[simple]])
  937. b4_percent_define_check_values([[[[parse.error]],
  938. [[custom]], [[detailed]], [[simple]], [[verbose]]]])
  939. # b4_parse_error_case(CASE1, THEN1, CASE2, THEN2, ..., ELSE)
  940. # ----------------------------------------------------------
  941. m4_define([b4_parse_error_case],
  942. [m4_case(b4_percent_define_get([[parse.error]]), $@)])
  943. # b4_parse_error_bmatch(PATTERN1, THEN1, PATTERN2, THEN2, ..., ELSE)
  944. # ------------------------------------------------------------------
  945. m4_define([b4_parse_error_bmatch],
  946. [m4_bmatch(b4_percent_define_get([[parse.error]]), $@)])
  947. # b4_variant_if([IF-VARIANT-ARE-USED], [IF-NOT])
  948. # ----------------------------------------------
  949. b4_percent_define_if_define([variant])
  950. m4_define([b4_variant_flag], [[0]])
  951. b4_percent_define_ifdef([[api.value.type]],
  952. [m4_case(b4_percent_define_get_kind([[api.value.type]]), [keyword],
  953. [m4_case(b4_percent_define_get([[api.value.type]]), [variant],
  954. [m4_define([b4_variant_flag], [[1]])])])])
  955. b4_define_flag_if([variant])
  956. ## ----------------------------------------------------------- ##
  957. ## After processing the skeletons, check that all the user's ##
  958. ## %define variables and %code qualifiers were used by Bison. ##
  959. ## ----------------------------------------------------------- ##
  960. m4_define([b4_check_user_names_wrap],
  961. [m4_ifdef([b4_percent_]$1[_user_]$2[s],
  962. [b4_check_user_names([[%]$1 $2],
  963. [b4_percent_]$1[_user_]$2[s],
  964. [[b4_percent_]$1[_bison_]$2[s]])])])
  965. m4_wrap_lifo([
  966. b4_check_user_names_wrap([[define]], [[variable]])
  967. b4_check_user_names_wrap([[code]], [[qualifier]])
  968. ])
  969. ## ---------------- ##
  970. ## Default values. ##
  971. ## ---------------- ##
  972. # m4_define_default([b4_lex_param], []) dnl breaks other skeletons
  973. m4_define_default([b4_epilogue], [])
  974. m4_define_default([b4_parse_param], [])
  975. # The initial column and line.
  976. m4_define_default([b4_location_initial_column], [1])
  977. m4_define_default([b4_location_initial_line], [1])
  978. ## --------------- ##
  979. ## Sanity checks. ##
  980. ## --------------- ##
  981. # api.location.type={...} (C, C++ and Java).
  982. b4_percent_define_check_kind([api.location.type], [code], [deprecated])
  983. # api.position.type={...} (Java).
  984. b4_percent_define_check_kind([api.position.type], [code], [deprecated])
  985. # api.prefix >< %name-prefix.
  986. b4_percent_define_check_kind([api.prefix], [code], [deprecated])
  987. b4_percent_define_ifdef([api.prefix],
  988. [m4_ifdef([b4_prefix],
  989. [b4_complain_at(b4_percent_define_get_loc([api.prefix]),
  990. [['%s' and '%s' cannot be used together]],
  991. [%name-prefix],
  992. [%define api.prefix])])])
  993. # api.token.prefix={...}
  994. # Make it a warning for those who used betas of Bison 3.0.
  995. b4_percent_define_check_kind([api.token.prefix], [code], [deprecated])
  996. # api.value.type >< %union.
  997. b4_percent_define_ifdef([api.value.type],
  998. [m4_ifdef([b4_union_members],
  999. [b4_complain_at(b4_percent_define_get_loc([api.value.type]),
  1000. [['%s' and '%s' cannot be used together]],
  1001. [%union],
  1002. [%define api.value.type])])])
  1003. # api.value.type=union >< %yacc.
  1004. b4_percent_define_ifdef([api.value.type],
  1005. [m4_if(b4_percent_define_get([api.value.type]), [union],
  1006. [b4_yacc_if(dnl
  1007. [b4_complain_at(b4_percent_define_get_loc([api.value.type]),
  1008. [['%s' and '%s' cannot be used together]],
  1009. [%yacc],
  1010. [%define api.value.type "union"])])])])
  1011. # api.value.union.name.
  1012. b4_percent_define_check_kind([api.value.union.name], [keyword])
  1013. # parse.error (custom|detailed) >< token-table.
  1014. b4_token_table_if(
  1015. [b4_parse_error_bmatch([custom\|detailed],
  1016. [b4_complain_at(b4_percent_define_get_loc([parse.error]),
  1017. [['%s' and '%s' cannot be used together]],
  1018. [%token-table],
  1019. [%define parse.error (custom|detailed)])])])