* src/macro.c (expand_macro): In macro expansion errors, report
[m4.git] / src / macro.c
blob5d8be55545651ccfb20201bcc9b00171f99fe66f
1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2006 Free Software
4 Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA
22 /* This file contains the functions, that performs the basic argument
23 parsing and macro expansion. */
25 #include "m4.h"
27 static void expand_macro (symbol *);
28 static void expand_token (struct obstack *, token_type, token_data *);
30 /* Current recursion level in expand_macro (). */
31 int expansion_level = 0;
33 /* The number of the current call of expand_macro (). */
34 static int macro_call_id = 0;
36 /* The shared stack of collected arguments for macro calls; as each
37 argument is collected, it is finished and its location stored in
38 argv_stack. Normally, this stack can be used simultaneously by
39 multiple macro calls; the exception is when an outer macro has
40 generated some text, then calls a nested macro, in which case the
41 nested macro must use a local stack to leave the unfinished text
42 alone. Too bad obstack.h does not provide an easy way to reopen a
43 finished object for further growth, but in practice this does not
44 hurt us too much. */
45 static struct obstack argc_stack;
47 /* The shared stack of pointers to collected arguments for macro
48 calls. This object is never finished; we exploit the fact that
49 obstack_blank is documented to take a negative size to reduce the
50 size again. */
51 static struct obstack argv_stack;
53 /*----------------------------------------------------------------------.
54 | This function read all input, and expands each token, one at a time. |
55 `----------------------------------------------------------------------*/
57 void
58 expand_input (void)
60 token_type t;
61 token_data td;
63 obstack_init (&argc_stack);
64 obstack_init (&argv_stack);
66 while ((t = next_token (&td)) != TOKEN_EOF)
67 expand_token ((struct obstack *) NULL, t, &td);
69 obstack_free (&argc_stack, NULL);
70 obstack_free (&argv_stack, NULL);
74 /*------------------------------------------------------------------------.
75 | Expand one token, according to its type. Potential macro names |
76 | (TOKEN_WORD) are looked up in the symbol table, to see if they have a |
77 | macro definition. If they have, they are expanded as macros, otherwise |
78 | the text are just copied to the output. |
79 `------------------------------------------------------------------------*/
81 static void
82 expand_token (struct obstack *obs, token_type t, token_data *td)
84 symbol *sym;
86 switch (t)
87 { /* TOKSW */
88 case TOKEN_EOF:
89 case TOKEN_MACDEF:
90 break;
92 case TOKEN_OPEN:
93 case TOKEN_COMMA:
94 case TOKEN_CLOSE:
95 case TOKEN_SIMPLE:
96 case TOKEN_STRING:
97 shipout_text (obs, TOKEN_DATA_TEXT (td), strlen (TOKEN_DATA_TEXT (td)));
98 break;
100 case TOKEN_WORD:
101 sym = lookup_symbol (TOKEN_DATA_TEXT (td), SYMBOL_LOOKUP);
102 if (sym == NULL || SYMBOL_TYPE (sym) == TOKEN_VOID
103 || (SYMBOL_TYPE (sym) == TOKEN_FUNC
104 && SYMBOL_BLIND_NO_ARGS (sym)
105 && peek_token () != TOKEN_OPEN))
107 #ifdef ENABLE_CHANGEWORD
108 shipout_text (obs, TOKEN_DATA_ORIG_TEXT (td),
109 strlen (TOKEN_DATA_ORIG_TEXT (td)));
110 #else
111 shipout_text (obs, TOKEN_DATA_TEXT (td),
112 strlen (TOKEN_DATA_TEXT (td)));
113 #endif
115 else
116 expand_macro (sym);
117 break;
119 default:
120 M4ERROR ((warning_status, 0,
121 "INTERNAL ERROR: bad token type in expand_token ()"));
122 abort ();
127 /*-------------------------------------------------------------------------.
128 | This function parses one argument to a macro call. It expects the first |
129 | left parenthesis, or the separating comma to have been read by the |
130 | caller. It skips leading whitespace, and reads and expands tokens, |
131 | until it finds a comma or an right parenthesis at the same level of |
132 | parentheses. It returns a flag indicating whether the argument read are |
133 | the last for the active macro call. The argument are build on the |
134 | obstack OBS, indirectly through expand_token (). |
135 `-------------------------------------------------------------------------*/
137 static boolean
138 expand_argument (struct obstack *obs, token_data *argp)
140 token_type t;
141 token_data td;
142 char *text;
143 int paren_level;
144 const char *file = current_file;
145 int line = current_line;
147 TOKEN_DATA_TYPE (argp) = TOKEN_VOID;
149 /* Skip leading white space. */
152 t = next_token (&td);
154 while (t == TOKEN_SIMPLE && isspace (to_uchar (*TOKEN_DATA_TEXT (&td))));
156 paren_level = 0;
158 while (1)
161 switch (t)
162 { /* TOKSW */
163 case TOKEN_COMMA:
164 case TOKEN_CLOSE:
165 if (paren_level == 0)
167 /* The argument MUST be finished, whether we want it or not. */
168 obstack_1grow (obs, '\0');
169 text = obstack_finish (obs);
171 if (TOKEN_DATA_TYPE (argp) == TOKEN_VOID)
173 TOKEN_DATA_TYPE (argp) = TOKEN_TEXT;
174 TOKEN_DATA_TEXT (argp) = text;
176 return (boolean) (t == TOKEN_COMMA);
178 /* fallthru */
179 case TOKEN_OPEN:
180 case TOKEN_SIMPLE:
181 text = TOKEN_DATA_TEXT (&td);
183 if (*text == '(')
184 paren_level++;
185 else if (*text == ')')
186 paren_level--;
187 expand_token (obs, t, &td);
188 break;
190 case TOKEN_EOF:
191 /* current_file changed to "" if we see TOKEN_EOF, use the
192 previous value we stored earlier. */
193 M4ERROR_AT_LINE ((EXIT_FAILURE, 0, file, line,
194 "ERROR: end of file in argument list"));
195 break;
197 case TOKEN_WORD:
198 case TOKEN_STRING:
199 expand_token (obs, t, &td);
200 break;
202 case TOKEN_MACDEF:
203 if (obstack_object_size (obs) == 0)
205 TOKEN_DATA_TYPE (argp) = TOKEN_FUNC;
206 TOKEN_DATA_FUNC (argp) = TOKEN_DATA_FUNC (&td);
208 break;
210 default:
211 M4ERROR ((warning_status, 0,
212 "INTERNAL ERROR: bad token type in expand_argument ()"));
213 abort ();
216 t = next_token (&td);
220 /*-------------------------------------------------------------------------.
221 | Collect all the arguments to a call of the macro SYM. The arguments are |
222 | stored on the obstack ARGUMENTS and a table of pointers to the arguments |
223 | on the obstack ARGPTR. |
224 `-------------------------------------------------------------------------*/
226 static void
227 collect_arguments (symbol *sym, struct obstack *argptr,
228 struct obstack *arguments)
230 token_data td;
231 token_data *tdp;
232 boolean more_args;
233 boolean groks_macro_args = SYMBOL_MACRO_ARGS (sym);
235 TOKEN_DATA_TYPE (&td) = TOKEN_TEXT;
236 TOKEN_DATA_TEXT (&td) = SYMBOL_NAME (sym);
237 tdp = (token_data *) obstack_copy (arguments, &td, sizeof td);
238 obstack_ptr_grow (argptr, tdp);
240 if (peek_token () == TOKEN_OPEN)
242 next_token (&td); /* gobble parenthesis */
245 more_args = expand_argument (arguments, &td);
247 if (!groks_macro_args && TOKEN_DATA_TYPE (&td) == TOKEN_FUNC)
249 TOKEN_DATA_TYPE (&td) = TOKEN_TEXT;
250 TOKEN_DATA_TEXT (&td) = "";
252 tdp = (token_data *) obstack_copy (arguments, &td, sizeof td);
253 obstack_ptr_grow (argptr, tdp);
255 while (more_args);
260 /*------------------------------------------------------------------------.
261 | The actual call of a macro is handled by call_macro (). call_macro () |
262 | is passed a symbol SYM, whose type is used to call either a builtin |
263 | function, or the user macro expansion function expand_user_macro () |
264 | (lives in builtin.c). There are ARGC arguments to the call, stored in |
265 | the ARGV table. The expansion is left on the obstack EXPANSION. Macro |
266 | tracing is also handled here. |
267 `------------------------------------------------------------------------*/
269 void
270 call_macro (symbol *sym, int argc, token_data **argv,
271 struct obstack *expansion)
273 switch (SYMBOL_TYPE (sym))
275 case TOKEN_FUNC:
276 (*SYMBOL_FUNC (sym)) (expansion, argc, argv);
277 break;
279 case TOKEN_TEXT:
280 expand_user_macro (expansion, sym, argc, argv);
281 break;
283 default:
284 M4ERROR ((warning_status, 0,
285 "INTERNAL ERROR: bad symbol type in call_macro ()"));
286 abort ();
290 /*-------------------------------------------------------------------------.
291 | The macro expansion is handled by expand_macro (). It parses the |
292 | arguments, using collect_arguments (), and builds a table of pointers to |
293 | the arguments. The arguments themselves are stored on a local obstack. |
294 | Expand_macro () uses call_macro () to do the call of the macro. |
296 | Expand_macro () is potentially recursive, since it calls expand_argument |
297 | (), which might call expand_token (), which might call expand_macro (). |
298 `-------------------------------------------------------------------------*/
300 static void
301 expand_macro (symbol *sym)
303 struct obstack arguments; /* Alternate obstack if argc_stack is busy. */
304 unsigned argv_base; /* Size of argv_stack on entry. */
305 boolean use_argc_stack = TRUE; /* Whether argc_stack is safe. */
306 token_data **argv;
307 int argc;
308 struct obstack *expansion;
309 const char *expanded;
310 boolean traced;
311 int my_call_id;
313 /* Report errors at the location where the open parenthesis (if any)
314 was found, but after expansion, restore global state back to the
315 location of the close parenthesis. This is safe since we
316 guarantee that macro expansion does not alter the state of
317 current_file/current_line (dnl, include, and sinclude are special
318 cased in the input engine to ensure this fact). */
319 const char *loc_open_file = current_file;
320 int loc_open_line = current_line;
321 const char *loc_close_file;
322 int loc_close_line;
324 SYMBOL_PENDING_EXPANSIONS (sym)++;
325 expansion_level++;
326 if (nesting_limit > 0 && expansion_level > nesting_limit)
327 M4ERROR ((EXIT_FAILURE, 0,
328 "ERROR: recursion limit of %d exceeded, use -L<N> to change it",
329 nesting_limit));
331 macro_call_id++;
332 my_call_id = macro_call_id;
334 traced = (boolean) ((debug_level & DEBUG_TRACE_ALL) || SYMBOL_TRACED (sym));
336 argv_base = obstack_object_size (&argv_stack);
337 if (obstack_object_size (&argc_stack) > 0)
339 /* We cannot use argc_stack if this is a nested invocation, and an
340 outer invocation has an unfinished argument being
341 collected. */
342 obstack_init (&arguments);
343 use_argc_stack = FALSE;
346 if (traced && (debug_level & DEBUG_TRACE_CALL))
347 trace_prepre (SYMBOL_NAME (sym), my_call_id);
349 collect_arguments (sym, &argv_stack,
350 use_argc_stack ? &argc_stack : &arguments);
352 argc = ((obstack_object_size (&argv_stack) - argv_base)
353 / sizeof (token_data *));
354 argv = (token_data **) (obstack_base (&argv_stack) + argv_base);
356 loc_close_file = current_file;
357 loc_close_line = current_line;
358 current_file = loc_open_file;
359 current_line = loc_open_line;
361 if (traced)
362 trace_pre (SYMBOL_NAME (sym), my_call_id, argc, argv);
364 expansion = push_string_init ();
365 call_macro (sym, argc, argv, expansion);
366 expanded = push_string_finish ();
368 if (traced)
369 trace_post (SYMBOL_NAME (sym), my_call_id, argc, argv, expanded);
371 current_file = loc_close_file;
372 current_line = loc_close_line;
374 --expansion_level;
375 --SYMBOL_PENDING_EXPANSIONS (sym);
377 if (SYMBOL_DELETED (sym))
378 free_symbol (sym);
380 if (use_argc_stack)
381 obstack_free (&argc_stack, argv[0]);
382 else
383 obstack_free (&arguments, NULL);
384 obstack_blank (&argv_stack, -argc * sizeof (token_data *));