c.m4 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. -*- Autoconf -*-
  2. # C 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. m4_include(b4_pkgdatadir/[c-like.m4])
  17. # b4_tocpp(STRING)
  18. # ----------------
  19. # Convert STRING into a valid C macro name.
  20. m4_define([b4_tocpp],
  21. [m4_toupper(m4_bpatsubst(m4_quote($1), [[^a-zA-Z0-9]+], [_]))])
  22. # b4_cpp_guard(FILE)
  23. # ------------------
  24. # A valid C macro name to use as a CPP header guard for FILE.
  25. m4_define([b4_cpp_guard],
  26. [[YY_]b4_tocpp(m4_defn([b4_prefix])/[$1])[_INCLUDED]])
  27. # b4_cpp_guard_open(FILE)
  28. # b4_cpp_guard_close(FILE)
  29. # ------------------------
  30. # If FILE does not expand to nothing, open/close CPP inclusion guards for FILE.
  31. m4_define([b4_cpp_guard_open],
  32. [m4_ifval(m4_quote($1),
  33. [#ifndef b4_cpp_guard([$1])
  34. # define b4_cpp_guard([$1])])])
  35. m4_define([b4_cpp_guard_close],
  36. [m4_ifval(m4_quote($1),
  37. [#endif b4_comment([!b4_cpp_guard([$1])])])])
  38. ## ---------------- ##
  39. ## Identification. ##
  40. ## ---------------- ##
  41. # b4_identification
  42. # -----------------
  43. # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
  44. # b4_pull_flag if they use the values of the %define variables api.pure or
  45. # api.push-pull.
  46. m4_define([b4_identification],
  47. [[/* Identify Bison output. */
  48. #define YYBISON 1
  49. /* Bison version. */
  50. #define YYBISON_VERSION "]b4_version["
  51. /* Skeleton name. */
  52. #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
  53. /* Pure parsers. */
  54. #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
  55. /* Push parsers. */
  56. #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
  57. /* Pull parsers. */
  58. #define YYPULL ]b4_pull_flag])[
  59. ]])
  60. ## ---------------- ##
  61. ## Default values. ##
  62. ## ---------------- ##
  63. # b4_api_prefix, b4_api_PREFIX
  64. # ----------------------------
  65. # Corresponds to %define api.prefix
  66. b4_percent_define_default([[api.prefix]], [[yy]])
  67. m4_define([b4_api_prefix],
  68. [b4_percent_define_get([[api.prefix]])])
  69. m4_define([b4_api_PREFIX],
  70. [m4_toupper(b4_api_prefix)])
  71. # b4_prefix
  72. # ---------
  73. # If the %name-prefix is not given, it is api.prefix.
  74. m4_define_default([b4_prefix], [b4_api_prefix])
  75. # If the %union is not named, its name is YYSTYPE.
  76. m4_define_default([b4_union_name], [b4_api_PREFIX[]STYPE])
  77. ## ------------------------ ##
  78. ## Pure/impure interfaces. ##
  79. ## ------------------------ ##
  80. # b4_lex_formals
  81. # --------------
  82. # All the yylex formal arguments.
  83. # b4_lex_param arrives quoted twice, but we want to keep only one level.
  84. m4_define([b4_lex_formals],
  85. [b4_pure_if([[[[YYSTYPE *yylvalp]], [[&yylval]]][]dnl
  86. b4_locations_if([, [[YYLTYPE *yyllocp], [&yylloc]]])])dnl
  87. m4_ifdef([b4_lex_param], [, ]b4_lex_param)])
  88. # b4_lex
  89. # ------
  90. # Call yylex.
  91. m4_define([b4_lex],
  92. [b4_function_call([yylex], [int], b4_lex_formals)])
  93. # b4_user_args
  94. # ------------
  95. m4_define([b4_user_args],
  96. [m4_ifset([b4_parse_param], [, b4_args(b4_parse_param)])])
  97. # b4_parse_param
  98. # --------------
  99. # If defined, b4_parse_param arrives double quoted, but below we prefer
  100. # it to be single quoted.
  101. m4_define([b4_parse_param],
  102. b4_parse_param)
  103. # b4_parse_param_for(DECL, FORMAL, BODY)
  104. # ---------------------------------------
  105. # Iterate over the user parameters, binding the declaration to DECL,
  106. # the formal name to FORMAL, and evaluating the BODY.
  107. m4_define([b4_parse_param_for],
  108. [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
  109. [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
  110. m4_pushdef([$2], m4_shift($1_$2))dnl
  111. $3[]dnl
  112. m4_popdef([$2])dnl
  113. m4_popdef([$1])dnl
  114. ])])
  115. # b4_parse_param_use([VAL], [LOC])
  116. # --------------------------------
  117. # 'YYUSE' VAL, LOC if locations are enabled, and all the parse-params.
  118. m4_define([b4_parse_param_use],
  119. [m4_ifvaln([$1], [ YYUSE ([$1]);])dnl
  120. b4_locations_if([m4_ifvaln([$2], [ YYUSE ([$2]);])])dnl
  121. b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
  122. ])dnl
  123. ])
  124. ## ------------ ##
  125. ## Data Types. ##
  126. ## ------------ ##
  127. # b4_int_type(MIN, MAX)
  128. # ---------------------
  129. # Return the smallest int type able to handle numbers ranging from
  130. # MIN to MAX (included).
  131. m4_define([b4_int_type],
  132. [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
  133. b4_ints_in($@, [-128], [127]), [1], [signed char],
  134. b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
  135. b4_ints_in($@, [-32768], [32767]), [1], [short int],
  136. m4_eval([0 <= $1]), [1], [unsigned int],
  137. [int])])
  138. # b4_int_type_for(NAME)
  139. # ---------------------
  140. # Return the smallest int type able to handle numbers ranging from
  141. # 'NAME_min' to 'NAME_max' (included).
  142. m4_define([b4_int_type_for],
  143. [b4_int_type($1_min, $1_max)])
  144. # b4_table_value_equals(TABLE, VALUE, LITERAL)
  145. # --------------------------------------------
  146. # Without inducing a comparison warning from the compiler, check if the
  147. # literal value LITERAL equals VALUE from table TABLE, which must have
  148. # TABLE_min and TABLE_max defined.
  149. m4_define([b4_table_value_equals],
  150. [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
  151. || m4_indir([b4_]$1[_max]) < $3), [1],
  152. [[0]],
  153. [(!!(($2) == ($3)))])])
  154. ## ----------------- ##
  155. ## Compiler issues. ##
  156. ## ----------------- ##
  157. # b4_attribute_define
  158. # -------------------
  159. # Provide portability for __attribute__.
  160. m4_define([b4_attribute_define],
  161. [#ifndef __attribute__
  162. /* This feature is available in gcc versions 2.5 and later. */
  163. # if (! defined __GNUC__ || __GNUC__ < 2 \
  164. || (__GNUC__ == 2 && __GNUC_MINOR__ < 5))
  165. # define __attribute__(Spec) /* empty */
  166. # endif
  167. #endif
  168. /* Suppress unused-variable warnings by "using" E. */
  169. #if ! defined lint || defined __GNUC__
  170. # define YYUSE(E) ((void) (E))
  171. #else
  172. # define YYUSE(E) /* empty */
  173. #endif
  174. #if defined __GNUC__ && 407 <= __GNUC__ * 100 + __GNUC_MINOR__
  175. /* Suppress an incorrect diagnostic about yylval being uninitialized. */
  176. # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \
  177. _Pragma ("GCC diagnostic push") \
  178. _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")\
  179. _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
  180. # define YY_IGNORE_MAYBE_UNINITIALIZED_END \
  181. _Pragma ("GCC diagnostic pop")
  182. #else
  183. # define YY_INITIAL_VALUE(Value) Value
  184. #endif
  185. #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
  186. # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
  187. # define YY_IGNORE_MAYBE_UNINITIALIZED_END
  188. #endif
  189. #ifndef YY_INITIAL_VALUE
  190. # define YY_INITIAL_VALUE(Value) /* Nothing. */
  191. #endif
  192. ])
  193. ## ---------##
  194. ## Values. ##
  195. ## ---------##
  196. # b4_null_define
  197. # --------------
  198. # Portability issues: define a YY_NULL appropriate for the current
  199. # language (C, C++98, or C++11).
  200. m4_define([b4_null_define],
  201. [# ifndef YY_NULL
  202. # if defined __cplusplus && 201103L <= __cplusplus
  203. # define YY_NULL nullptr
  204. # else
  205. # define YY_NULL 0
  206. # endif
  207. # endif[]dnl
  208. ])
  209. # b4_null
  210. # -------
  211. # Return a null pointer constant.
  212. m4_define([b4_null], [YY_NULL])
  213. # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
  214. # -------------------------------------------------------------
  215. # Define "yy<TABLE-NAME>" whose contents is CONTENT.
  216. m4_define([b4_integral_parser_table_define],
  217. [m4_ifvaln([$3], [b4_comment([$3], [ ])])dnl
  218. static const b4_int_type_for([$2]) yy$1[[]] =
  219. {
  220. $2
  221. };dnl
  222. ])
  223. ## ------------------------- ##
  224. ## Assigning token numbers. ##
  225. ## ------------------------- ##
  226. # b4_token_define(TOKEN-NUM)
  227. # --------------------------
  228. # Output the definition of this token as #define.
  229. m4_define([b4_token_define],
  230. [b4_token_format([#define %s %s], [$1])])
  231. # b4_token_defines
  232. # ----------------
  233. # Output the definition of the tokens.
  234. m4_define([b4_token_defines],
  235. [b4_any_token_visible_if([/* Tokens. */
  236. m4_join([
  237. ], b4_symbol_map([b4_token_define]))
  238. ])])
  239. # b4_token_enum(TOKEN-NUM)
  240. # ------------------------
  241. # Output the definition of this token as an enum.
  242. m4_define([b4_token_enum],
  243. [b4_token_format([%s = %s], [$1])])
  244. # b4_token_enums
  245. # --------------
  246. # Output the definition of the tokens (if there are) as enums.
  247. m4_define([b4_token_enums],
  248. [b4_any_token_visible_if([[/* Token type. */
  249. #ifndef ]b4_api_PREFIX[TOKENTYPE
  250. # define ]b4_api_PREFIX[TOKENTYPE
  251. enum ]b4_api_prefix[tokentype
  252. {
  253. ]m4_join([,
  254. ],
  255. b4_symbol_map([b4_token_enum]))[
  256. };
  257. #endif
  258. ]])])
  259. # b4_token_enums_defines
  260. # ----------------------
  261. # Output the definition of the tokens (if there are any) as enums and,
  262. # if POSIX Yacc is enabled, as #defines.
  263. m4_define([b4_token_enums_defines],
  264. [b4_token_enums[]b4_yacc_if([b4_token_defines])])
  265. ## ----------------- ##
  266. ## Semantic Values. ##
  267. ## ----------------- ##
  268. # b4_symbol_value(VAL, [TYPE])
  269. # ----------------------------
  270. # Given a semantic value VAL ($$, $1 etc.), extract its value of type
  271. # TYPE if TYPE is given, otherwise just return VAL. The result can be
  272. # used safetly, it is put in parens to avoid nasty precedence issues.
  273. # TYPE is *not* put in braces, provide some if needed.
  274. m4_define([b4_symbol_value],
  275. [($1[]m4_ifval([$2], [.$2]))])
  276. ## ---------------------- ##
  277. ## Defining C functions. ##
  278. ## ---------------------- ##
  279. # b4_function_define(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
  280. # -----------------------------------------------------------
  281. # Declare the function NAME in C.
  282. m4_define([b4_function_define],
  283. [$2
  284. $1 (b4_formals(m4_shift2($@)))[]dnl
  285. ])
  286. # b4_formals([DECL1, NAME1], ...)
  287. # -------------------------------
  288. # The formal arguments of a C function definition.
  289. m4_define([b4_formals],
  290. [m4_if([$#], [0], [void],
  291. [$#$1], [1], [void],
  292. [m4_map_sep([b4_formal], [, ], [$@])])])
  293. m4_define([b4_formal],
  294. [$1])
  295. ## ----------------------- ##
  296. ## Declaring C functions. ##
  297. ## ----------------------- ##
  298. # b4_function_declare(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
  299. # ------------------------------------------------------------
  300. # Declare the function NAME.
  301. m4_define([b4_function_declare],
  302. [$2 $1 (b4_formals(m4_shift2($@)));[]dnl
  303. ])
  304. ## --------------------- ##
  305. ## Calling C functions. ##
  306. ## --------------------- ##
  307. # b4_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
  308. # -----------------------------------------------------------
  309. # Call the function NAME with arguments NAME1, NAME2 etc.
  310. m4_define([b4_function_call],
  311. [$1 (b4_args(m4_shift2($@)))[]dnl
  312. ])
  313. # b4_args([DECL1, NAME1], ...)
  314. # ----------------------------
  315. # Output the arguments NAME1, NAME2...
  316. m4_define([b4_args],
  317. [m4_map_sep([b4_arg], [, ], [$@])])
  318. m4_define([b4_arg],
  319. [$2])
  320. ## ----------- ##
  321. ## Synclines. ##
  322. ## ----------- ##
  323. # b4_sync_start(LINE, FILE)
  324. # -------------------------
  325. m4_define([b4_sync_start], [[#]line $1 $2])
  326. ## -------------- ##
  327. ## User actions. ##
  328. ## -------------- ##
  329. # b4_case(LABEL, STATEMENTS)
  330. # --------------------------
  331. m4_define([b4_case],
  332. [ case $1:
  333. $2
  334. b4_syncline([@oline@], [@ofile@])
  335. break;])
  336. # b4_predicate_case(LABEL, CONDITIONS)
  337. # ------------------------------------
  338. m4_define([b4_predicate_case],
  339. [ case $1:
  340. if (! ($2)) YYERROR;
  341. b4_syncline([@oline@], [@ofile@])
  342. break;])
  343. # b4_yydestruct_define
  344. # --------------------
  345. # Define the "yydestruct" function.
  346. m4_define_default([b4_yydestruct_define],
  347. [[/*-----------------------------------------------.
  348. | Release the memory associated to this symbol. |
  349. `-----------------------------------------------*/
  350. ]b4_function_define([yydestruct],
  351. [static void],
  352. [[const char *yymsg], [yymsg]],
  353. [[int yytype], [yytype]],
  354. [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
  355. b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
  356. m4_ifset([b4_parse_param], [, b4_parse_param]))[
  357. {
  358. ]b4_parse_param_use([yyvaluep], [yylocationp])dnl
  359. [ if (!yymsg)
  360. yymsg = "Deleting";
  361. YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
  362. YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
  363. ]b4_symbol_actions([destructor])[
  364. YY_IGNORE_MAYBE_UNINITIALIZED_END
  365. }]dnl
  366. ])
  367. # b4_yy_symbol_print_define
  368. # -------------------------
  369. # Define the "yy_symbol_print" function.
  370. m4_define_default([b4_yy_symbol_print_define],
  371. [[
  372. /*----------------------------------------.
  373. | Print this symbol's value on YYOUTPUT. |
  374. `----------------------------------------*/
  375. ]b4_function_define([yy_symbol_value_print],
  376. [static void],
  377. [[FILE *yyoutput], [yyoutput]],
  378. [[int yytype], [yytype]],
  379. [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
  380. b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
  381. m4_ifset([b4_parse_param], [, b4_parse_param]))[
  382. {
  383. FILE *yyo = yyoutput;
  384. ]b4_parse_param_use([yyo], [yylocationp])dnl
  385. [ if (!yyvaluep)
  386. return;]
  387. dnl glr.c does not feature yytoknum.
  388. m4_if(b4_skeleton, ["yacc.c"],
  389. [[# ifdef YYPRINT
  390. if (yytype < YYNTOKENS)
  391. YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
  392. # endif
  393. ]])dnl
  394. b4_symbol_actions([printer])[
  395. }
  396. /*--------------------------------.
  397. | Print this symbol on YYOUTPUT. |
  398. `--------------------------------*/
  399. ]b4_function_define([yy_symbol_print],
  400. [static void],
  401. [[FILE *yyoutput], [yyoutput]],
  402. [[int yytype], [yytype]],
  403. [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
  404. b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
  405. m4_ifset([b4_parse_param], [, b4_parse_param]))[
  406. {
  407. YYFPRINTF (yyoutput, "%s %s (",
  408. yytype < YYNTOKENS ? "token" : "nterm", yytname[yytype]);
  409. ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
  410. YYFPRINTF (yyoutput, ": ");
  411. ])dnl
  412. [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
  413. b4_locations_if([, yylocationp])[]b4_user_args[);
  414. YYFPRINTF (yyoutput, ")");
  415. }]dnl
  416. ])
  417. ## ---------------- ##
  418. ## api.value.type. ##
  419. ## ---------------- ##
  420. # ---------------------- #
  421. # api.value.type=union. #
  422. # ---------------------- #
  423. # b4_symbol_type_register(SYMBOL-NUM)
  424. # -----------------------------------
  425. # Symbol SYMBOL-NUM has a type (for variant) instead of a type-tag.
  426. # Extend the definition of %union's body with a field of that type,
  427. # and extend the symbol's "type" field to point to the field name,
  428. # instead of the type name.
  429. m4_define([b4_symbol_type_register],
  430. [m4_define([b4_symbol($1, type_tag)],
  431. [b4_symbol_if([$1], [has_id],
  432. [b4_symbol([$1], [id])],
  433. [yytype_[]b4_symbol([$1], [number])])])dnl
  434. m4_append([b4_user_union_members],
  435. m4_expand([
  436. b4_symbol_tag_comment([$1])dnl
  437. b4_symbol([$1], [type]) b4_symbol([$1], [type_tag]);]))
  438. ])
  439. # b4_type_define_tag(SYMBOL1-NUM, ...)
  440. # ------------------------------------
  441. # For the batch of symbols SYMBOL1-NUM... (which all have the same
  442. # type), enhance the %union definition for each of them, and set
  443. # there "type" field to the field tag name, instead of the type name.
  444. m4_define([b4_type_define_tag],
  445. [b4_symbol_if([$1], [has_type],
  446. [m4_map([b4_symbol_type_register], [$@])])
  447. ])
  448. # b4_symbol_value_union(VAL, [TYPE])
  449. # ----------------------------------
  450. # Same of b4_symbol_value, but when api.value.type=union.
  451. m4_define([b4_symbol_value_union],
  452. [m4_ifval([$2],
  453. [(*($2*)(&$1))],
  454. [$1])])
  455. ])
  456. # b4_value_type_setup_union
  457. # -------------------------
  458. # Setup support for api.value.type=union. Symbols are defined with a
  459. # type instead of a union member name: build the corresponding union,
  460. # and give the symbols their tag.
  461. m4_define([b4_value_type_setup_union],
  462. [m4_define([b4_union_members])
  463. b4_type_foreach([b4_type_define_tag])
  464. m4_copy_force([b4_symbol_value_union], [b4_symbol_value])
  465. ])
  466. # ---------------- #
  467. # api.value.type. #
  468. # ---------------- #
  469. # b4_value_type_setup_variant
  470. # ---------------------------
  471. # Setup support for api.value.type=variant. By default, fail, specialized
  472. # by other skeletons.
  473. m4_define([b4_value_type_setup_variant],
  474. [b4_complain_at(b4_percent_define_get_loc([[api.value.type]]),
  475. [['%s' does not support '%s']],
  476. [b4_skeleton],
  477. [%define api.value.type variant])])
  478. # _b4_value_type_setup_keyword
  479. # ----------------------------
  480. # api.value.type is defined with a keyword/string syntax. Check if
  481. # that is properly defined, and prepare its use.
  482. m4_define([_b4_value_type_setup_keyword],
  483. [b4_percent_define_check_values([[[[api.value.type]],
  484. [[none]],
  485. [[union]],
  486. [[union-directive]],
  487. [[variant]],
  488. [[yystype]]]])dnl
  489. m4_case(b4_percent_define_get([[api.value.type]]),
  490. [union], [b4_value_type_setup_union],
  491. [variant], [b4_value_type_setup_variant])])
  492. # b4_value_type_setup
  493. # -------------------
  494. # Check if api.value.type is properly defined, and possibly prepare
  495. # its use.
  496. b4_define_silent([b4_value_type_setup],
  497. [# Define default value.
  498. b4_percent_define_ifdef([[api.value.type]], [],
  499. [# %union => api.value.type=union-directive
  500. m4_ifdef([b4_union_members],
  501. [m4_define([b4_percent_define_kind(api.value.type)], [keyword])
  502. m4_define([b4_percent_define(api.value.type)], [union-directive])],
  503. [# no tag seen => api.value.type={int}
  504. m4_if(b4_tag_seen_flag, 0,
  505. [m4_define([b4_percent_define_kind(api.value.type)], [code])
  506. m4_define([b4_percent_define(api.value.type)], [int])],
  507. [# otherwise api.value.type=yystype
  508. m4_define([b4_percent_define_kind(api.value.type)], [keyword])
  509. m4_define([b4_percent_define(api.value.type)], [yystype])])])])
  510. # Set up.
  511. m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
  512. [keyword\|string], [_b4_value_type_setup_keyword])
  513. ])
  514. ## -------------- ##
  515. ## Declarations. ##
  516. ## -------------- ##
  517. # b4_value_type_define
  518. # --------------------
  519. m4_define([b4_value_type_define],
  520. [b4_value_type_setup[]dnl
  521. /* Value type. */
  522. m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
  523. [code],
  524. [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
  525. typedef ]b4_percent_define_get([[api.value.type]])[ ]b4_api_PREFIX[STYPE;
  526. # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1
  527. # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1
  528. #endif
  529. ]],
  530. [m4_bmatch(b4_percent_define_get([[api.value.type]]),
  531. [union\|union-directive],
  532. [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
  533. typedef union ]b4_union_name[ ]b4_api_PREFIX[STYPE;
  534. union ]b4_union_name[
  535. {
  536. ]b4_user_union_members[
  537. };
  538. # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1
  539. # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1
  540. #endif
  541. ]])])])
  542. # b4_location_type_define
  543. # -----------------------
  544. m4_define([b4_location_type_define],
  545. [[/* Location type. */
  546. #if ! defined ]b4_api_PREFIX[LTYPE && ! defined ]b4_api_PREFIX[LTYPE_IS_DECLARED
  547. typedef struct ]b4_api_PREFIX[LTYPE ]b4_api_PREFIX[LTYPE;
  548. struct ]b4_api_PREFIX[LTYPE
  549. {
  550. int first_line;
  551. int first_column;
  552. int last_line;
  553. int last_column;
  554. };
  555. # define ]b4_api_PREFIX[LTYPE_IS_DECLARED 1
  556. # define ]b4_api_PREFIX[LTYPE_IS_TRIVIAL 1
  557. #endif
  558. ]])
  559. # b4_declare_yylstype
  560. # -------------------
  561. # Declarations that might either go into the header (if --defines) or
  562. # in the parser body. Declare YYSTYPE/YYLTYPE, and yylval/yylloc.
  563. m4_define([b4_declare_yylstype],
  564. [b4_value_type_define[]b4_locations_if([
  565. b4_location_type_define])
  566. b4_pure_if([], [[extern ]b4_api_PREFIX[STYPE ]b4_prefix[lval;
  567. ]b4_locations_if([[extern ]b4_api_PREFIX[LTYPE ]b4_prefix[lloc;]])])[]dnl
  568. ])
  569. # b4_YYDEBUG_define
  570. # -----------------
  571. m4_define([b4_YYDEBUG_define],
  572. [[/* Debug traces. */
  573. ]m4_if(b4_api_prefix, [yy],
  574. [[#ifndef YYDEBUG
  575. # define YYDEBUG ]b4_parse_trace_if([1], [0])[
  576. #endif]],
  577. [[#ifndef ]b4_api_PREFIX[DEBUG
  578. # if defined YYDEBUG
  579. #if YYDEBUG
  580. # define ]b4_api_PREFIX[DEBUG 1
  581. # else
  582. # define ]b4_api_PREFIX[DEBUG 0
  583. # endif
  584. # else /* ! defined YYDEBUG */
  585. # define ]b4_api_PREFIX[DEBUG ]b4_parse_trace_if([1], [0])[
  586. # endif /* ! defined YYDEBUG */
  587. #endif /* ! defined ]b4_api_PREFIX[DEBUG */]])[]dnl
  588. ])
  589. # b4_declare_yydebug
  590. # ------------------
  591. m4_define([b4_declare_yydebug],
  592. [b4_YYDEBUG_define[
  593. #if ]b4_api_PREFIX[DEBUG
  594. extern int ]b4_prefix[debug;
  595. #endif][]dnl
  596. ])
  597. # b4_yylloc_default_define
  598. # ------------------------
  599. # Define YYLLOC_DEFAULT.
  600. m4_define([b4_yylloc_default_define],
  601. [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
  602. If N is 0, then set CURRENT to the empty location which ends
  603. the previous symbol: RHS[0] (always defined). */
  604. #ifndef YYLLOC_DEFAULT
  605. # define YYLLOC_DEFAULT(Current, Rhs, N) \
  606. do \
  607. if (N) \
  608. { \
  609. (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
  610. (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
  611. (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
  612. (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
  613. } \
  614. else \
  615. { \
  616. (Current).first_line = (Current).last_line = \
  617. YYRHSLOC (Rhs, 0).last_line; \
  618. (Current).first_column = (Current).last_column = \
  619. YYRHSLOC (Rhs, 0).last_column; \
  620. } \
  621. while (0)
  622. #endif
  623. ]])
  624. # b4_yy_location_print_define
  625. # ---------------------------
  626. # Define YY_LOCATION_PRINT.
  627. m4_define([b4_yy_location_print_define],
  628. [b4_locations_if([[
  629. /* YY_LOCATION_PRINT -- Print the location on the stream.
  630. This macro was not mandated originally: define only if we know
  631. we won't break user code: when these are the locations we know. */
  632. #ifndef YY_LOCATION_PRINT
  633. # if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
  634. /* Print *YYLOCP on YYO. Private, do not rely on its existence. */
  635. __attribute__((__unused__))
  636. ]b4_function_define([yy_location_print_],
  637. [static unsigned],
  638. [[FILE *yyo], [yyo]],
  639. [[YYLTYPE const * const yylocp], [yylocp]])[
  640. {
  641. unsigned res = 0;
  642. int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
  643. if (0 <= yylocp->first_line)
  644. {
  645. res += YYFPRINTF (yyo, "%d", yylocp->first_line);
  646. if (0 <= yylocp->first_column)
  647. res += YYFPRINTF (yyo, ".%d", yylocp->first_column);
  648. }
  649. if (0 <= yylocp->last_line)
  650. {
  651. if (yylocp->first_line < yylocp->last_line)
  652. {
  653. res += YYFPRINTF (yyo, "-%d", yylocp->last_line);
  654. if (0 <= end_col)
  655. res += YYFPRINTF (yyo, ".%d", end_col);
  656. }
  657. else if (0 <= end_col && yylocp->first_column < end_col)
  658. res += YYFPRINTF (yyo, "-%d", end_col);
  659. }
  660. return res;
  661. }
  662. # define YY_LOCATION_PRINT(File, Loc) \
  663. yy_location_print_ (File, &(Loc))
  664. # else
  665. # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
  666. # endif
  667. #endif]],
  668. [[/* This macro is provided for backward compatibility. */
  669. #ifndef YY_LOCATION_PRINT
  670. # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
  671. #endif]])
  672. ])
  673. # b4_yyloc_default
  674. # ----------------
  675. # Expand to a possible default value for yylloc.
  676. m4_define([b4_yyloc_default],
  677. [[
  678. # if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
  679. = { ]m4_join([, ],
  680. m4_defn([b4_location_initial_line]),
  681. m4_defn([b4_location_initial_column]),
  682. m4_defn([b4_location_initial_line]),
  683. m4_defn([b4_location_initial_column]))[ }
  684. # endif
  685. ]])