source: trunk/essentials/sys-devel/m4/src/macro.c@ 3137

Last change on this file since 3137 was 3090, checked in by bird, 19 years ago

m4 1.4.8

File size: 11.7 KB
Line 
1/* GNU m4 -- A simple macro processor
2
3 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2006 Free Software
4 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA
20*/
21
22/* This file contains the functions, that performs the basic argument
23 parsing and macro expansion. */
24
25#include "m4.h"
26
27static void expand_macro (symbol *);
28static void expand_token (struct obstack *, token_type, token_data *);
29
30/* Current recursion level in expand_macro (). */
31int expansion_level = 0;
32
33/* The number of the current call of expand_macro (). */
34static int macro_call_id = 0;
35
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. */
45static struct obstack argc_stack;
46
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. */
51static struct obstack argv_stack;
52
53/*----------------------------------------------------------------------.
54| This function read all input, and expands each token, one at a time. |
55`----------------------------------------------------------------------*/
56
57void
58expand_input (void)
59{
60 token_type t;
61 token_data td;
62
63 obstack_init (&argc_stack);
64 obstack_init (&argv_stack);
65
66 while ((t = next_token (&td)) != TOKEN_EOF)
67 expand_token ((struct obstack *) NULL, t, &td);
68
69 obstack_free (&argc_stack, NULL);
70 obstack_free (&argv_stack, NULL);
71}
72
73
74
75/*------------------------------------------------------------------------.
76| Expand one token, according to its type. Potential macro names |
77| (TOKEN_WORD) are looked up in the symbol table, to see if they have a |
78| macro definition. If they have, they are expanded as macros, otherwise |
79| the text are just copied to the output. |
80`------------------------------------------------------------------------*/
81
82static void
83expand_token (struct obstack *obs, token_type t, token_data *td)
84{
85 symbol *sym;
86
87 switch (t)
88 { /* TOKSW */
89 case TOKEN_EOF:
90 case TOKEN_MACDEF:
91 break;
92
93 case TOKEN_OPEN:
94 case TOKEN_COMMA:
95 case TOKEN_CLOSE:
96 case TOKEN_SIMPLE:
97 case TOKEN_STRING:
98 shipout_text (obs, TOKEN_DATA_TEXT (td), strlen (TOKEN_DATA_TEXT (td)));
99 break;
100
101 case TOKEN_WORD:
102 sym = lookup_symbol (TOKEN_DATA_TEXT (td), SYMBOL_LOOKUP);