bison.m4 36 KB

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