]> git.saurik.com Git - bison.git/blob - data/c.m4
370d8e48051fbb76e3913964e364a512acf1f2d1
[bison.git] / data / c.m4
1 m4_divert(-1) -*- Autoconf -*-
2
3 # C M4 Macros for Bison.
4 # Copyright (C) 2002 Free Software Foundation, Inc.
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # 02111-1307 USA
20
21
22 ## ----------- ##
23 ## Copyright. ##
24 ## ----------- ##
25
26 # b4_copyright(TITLE, YEARS)
27 # --------------------------
28 m4_define([b4_copyright],
29 [/* A Bison parser, made from b4_filename, by GNU bison b4_version. */
30
31 /* $1,
32 Copyright (C) $2 Free Software Foundation, Inc.
33
34 This program is free software; you can redistribute it and/or modify
35 it under the terms of the GNU General Public License as published by
36 the Free Software Foundation; either version 2, or (at your option)
37 any later version.
38
39 This program is distributed in the hope that it will be useful,
40 but WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 GNU General Public License for more details.
43
44 You should have received a copy of the GNU General Public License
45 along with this program; if not, write to the Free Software
46 Foundation, Inc., 59 Temple Place - Suite 330,
47 Boston, MA 02111-1307, USA. */])
48
49
50 ## ------------ ##
51 ## Data Types. ##
52 ## ------------ ##
53
54
55 # b4_ints_in(INT1, INT2, LOW, HIGH)
56 # ---------------------------------
57 # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
58 m4_define([b4_ints_in],
59 [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
60
61
62 # b4_int_type(MIN, MAX)
63 # ---------------------
64 # Return the smallest int type able to handle numbers ranging from
65 # MIN to MAX (included).
66 m4_define([b4_int_type],
67 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
68 b4_ints_in($@, [-128], [127]), [1], [yysigned_char],
69
70 b4_ints_in($@, [0], [65535]), [1], [unsigned short],
71 b4_ints_in($@, [-32768], [32767]), [1], [short],
72
73 m4_eval([0 <= $1]), [1], [unsigned int],
74
75 [int])])
76
77 # b4_int_type_for(NAME)
78 # ---------------------
79 # Return the smallest int type able to handle numbers ranging from
80 # `NAME_min' to `NAME_max' (included).
81 m4_define([b4_int_type_for],
82 [b4_int_type($1_min, $1_max)])
83
84
85 ## ------------------------- ##
86 ## Assigning token numbers. ##
87 ## ------------------------- ##
88
89 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
90 # -----------------------------------------
91 # Output the definition of this token as #define.
92 m4_define([b4_token_define],
93 [#define $1 $2
94 ])
95
96
97 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
98 # ---------------------------------------
99 # Output the definition of this token as an enum.
100 m4_define([b4_token_enum],
101 [$1 = $2])
102
103
104 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
105 # -------------------------------------------------------
106 # Output the definition of the tokens (if there are) as enums and #define.
107 m4_define([b4_token_defines],
108 [m4_if([$@], [[]], [],
109 [/* Tokens. */
110 #ifndef YYTOKENTYPE
111 # define YYTOKENTYPE
112 /* Put the tokens into the symbol table, so that GDB and other debuggers
113 know about them. */
114 enum yytokentype {
115 m4_map_sep([ b4_token_enum], [,
116 ],
117 [$@])
118 };
119 #endif
120 m4_map([b4_token_define], [$@])
121 ])
122 ])
123
124
125 ## --------------------- ##
126 ## Calling C functions. ##
127 ## --------------------- ##
128
129
130 # b4_c_function_call(NAME, RETURN-VALUE, [TYPE1, NAME1], ...)
131 # -----------------------------------------------------------
132 # Call the function NAME with arguments NAME1, NAME2 etc.
133 m4_define([b4_c_function_call],
134 [$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
135 ])
136
137
138 # b4_c_args([TYPE1, NAME1], ...)
139 # ------------------------------
140 # Output the arguments NAME1, NAME2...
141 m4_define([b4_c_args],
142 [m4_map_sep([b4_c_arg], [, ], [$@])])
143
144 m4_define([b4_c_arg],
145 [$2])
146
147
148 ## ---------------------------------------------- ##
149 ## Declaring C functions in both K&R and ANSI-C. ##
150 ## ---------------------------------------------- ##
151
152
153 # b4_c_function(NAME, RETURN-VALUE, [TYPE1, NAME1], ...)
154 # ------------------------------------------------------
155 # Declare the function NAME.
156 m4_define([b4_c_function],
157 [$2
158 #if defined (__STDC__) || defined (__cplusplus)
159 $1 (b4_c_ansi_args(m4_shiftn(2, $@)))
160 #else
161 $1 (b4_c_knr_arg_names(m4_shiftn(2, $@)))
162 b4_c_knr_arg_decls(m4_shiftn(2, $@))
163 #endif[]dnl
164 ])
165
166
167 # b4_c_ansi_args([TYPE1, NAME1], ...)
168 # -----------------------------------
169 # Output the arguments ANSI-C definition.
170 m4_define([b4_c_ansi_args],
171 [m4_map_sep([b4_c_ansi_arg], [, ], [$@])])
172
173 m4_define([b4_c_ansi_arg],
174 [$1 $2])
175
176
177 # b4_c_knr_args([TYPE1, NAME1], ...)
178 # ----------------------------------
179 # Output the argument names.
180 m4_define([b4_c_knr_arg_names],
181 [m4_map_sep([b4_c_knr_arg_name], [, ], [$@])])
182
183 m4_define([b4_c_knr_arg_name],
184 [$2])
185
186
187 # b4_c_knr_args([TYPE1, NAME1], ...)
188 # ----------------------------------
189 # Output the K&R argument declarations.
190 m4_define([b4_c_knr_arg_decls],
191 [m4_map_sep([b4_c_knr_arg_decl],
192 [
193 ],
194 [$@])])
195
196 m4_define([b4_c_knr_arg_decl],
197 [ $1 $2;])
198
199
200
201 ## ------------------ ##
202 ## Decoding options. ##
203 ## ------------------ ##
204
205
206 # b4_location_if(IF-TRUE, IF-FALSE)
207 # ---------------------------------
208 # Expand IF-TRUE, if locations are used, IF-FALSE otherwise.
209 m4_define([b4_location_if],
210 [m4_if(b4_locations_flag, [1],
211 [$1],
212 [$2])])
213
214
215 # b4_pure_if(IF-TRUE, IF-FALSE)
216 # -----------------------------
217 # Expand IF-TRUE, if %pure-parser, IF-FALSE otherwise.
218 m4_define([b4_pure_if],
219 [m4_if(b4_pure, [1],
220 [$1],
221 [$2])])