]> git.saurik.com Git - bison.git/blob - src/muscle_tab.c
Prototype support of %lex-param and %parse-param.
[bison.git] / src / muscle_tab.c
1 /* Muscle table manager for Bison,
2 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison 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, or (at your option)
9 any later version.
10
11 Bison 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 Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "system.h"
22 #include "hash.h"
23 #include "files.h"
24 #include "muscle_tab.h"
25 #include "getargs.h"
26
27
28 /* An obstack used to create some entries. */
29 struct obstack muscle_obstack;
30
31 /* Initial capacity of muscles hash table. */
32 #define HT_INITIAL_CAPACITY 257
33
34 struct hash_table *muscle_table = NULL;
35
36 static bool
37 hash_compare_muscles (void const *x, void const *y)
38 {
39 const muscle_entry_t *m1 = x;
40 const muscle_entry_t *m2 = y;
41 return strcmp (m1->key, m2->key) ? FALSE : TRUE;
42 }
43
44 static unsigned int
45 hash_muscle (const void *x, unsigned int tablesize)
46 {
47 const muscle_entry_t *m = x;
48 return hash_string (m->key, tablesize);
49 }
50
51 /*-----------------------------------------------------------------.
52 | Create the MUSCLE_TABLE, and initialize it with default values. |
53 | Also set up the MUSCLE_OBSTACK. |
54 `-----------------------------------------------------------------*/
55
56 void
57 muscle_init (void)
58 {
59 /* Initialize the muscle obstack. */
60 obstack_init (&muscle_obstack);
61
62 muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle,
63 hash_compare_muscles, free);
64
65 /* Version and input file. */
66 MUSCLE_INSERT_STRING ("version", VERSION);
67 MUSCLE_INSERT_STRING ("filename", infile);
68
69 /* FIXME: there should probably be no default here, only in the
70 skeletons. */
71
72 /* Types. */
73 MUSCLE_INSERT_STRING ("ltype", "yyltype");
74
75 /* Default #line formatting. */
76 MUSCLE_INSERT_STRING ("linef", "#line %d %s\n");
77
78 /* Stack parameters. */
79 MUSCLE_INSERT_STRING ("maxdepth", "10000");
80 MUSCLE_INSERT_STRING ("initdepth", "200");
81
82 /* C++ macros. */
83 MUSCLE_INSERT_STRING ("name", "Parser");
84 }
85
86
87 /*------------------------------------------------------------.
88 | Free all the memory consumed by the muscle machinery only. |
89 `------------------------------------------------------------*/
90
91 void
92 muscle_free (void)
93 {
94 hash_free (muscle_table);
95 obstack_free (&muscle_obstack, NULL);
96 }
97
98
99
100 /*------------------------------------------------------------.
101 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
102 | previous value. |
103 `------------------------------------------------------------*/
104
105 void
106 muscle_insert (const char *key, char *value)
107 {
108 muscle_entry_t probe;
109 muscle_entry_t *entry = NULL;
110
111 probe.key = key;
112 entry = hash_lookup (muscle_table, &probe);
113
114 if (!entry)
115 {
116 /* First insertion in the hash. */
117 entry = XMALLOC (muscle_entry_t, 1);
118 entry->key = key;
119 hash_insert (muscle_table, entry);
120 }
121 entry->value = value;
122 }
123
124
125 /*-------------------------------------------------------------------.
126 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
127 | previous value. Uses MUSCLE_OBSTACK. De-allocates the previously |
128 | associated value. VALUE and SEPARATOR are copied. |
129 `-------------------------------------------------------------------*/
130
131 void
132 muscle_grow (const char *key, const char *val, const char *separator)
133 {
134 muscle_entry_t probe;
135 muscle_entry_t *entry = NULL;
136
137 probe.key = key;
138 entry = hash_lookup (muscle_table, &probe);
139
140 if (!entry)
141 {
142 /* First insertion in the hash. */
143 entry = XMALLOC (muscle_entry_t, 1);
144 entry->key = key;
145 hash_insert (muscle_table, entry);
146 entry->value = xstrdup (val);
147 }
148 else
149 {
150 /* Grow the current value. */
151 char *new_val;
152 fprintf (stderr, "<= %s + %s\n", entry->value, val);
153 obstack_sgrow (&muscle_obstack, entry->value);
154 free (entry->value);
155 obstack_sgrow (&muscle_obstack, separator);
156 obstack_sgrow (&muscle_obstack, val);
157 obstack_1grow (&muscle_obstack, 0);
158 new_val = obstack_finish (&muscle_obstack);
159 entry->value = xstrdup (new_val);
160 fprintf (stderr, "=> %s\n", new_val);
161 obstack_free (&muscle_obstack, new_val);
162 }
163 }
164
165
166 /*-------------------------------------------------------------------.
167 | MUSCLE is an M4 list of pairs. Create or extend it with the pair |
168 | (A1, A2). Note that because the muscle values are output *double* |
169 | quoted, one needs to strip the first level of quotes to reach the |
170 | list itself. |
171 `-------------------------------------------------------------------*/
172
173 void muscle_pair_list_grow (const char *muscle,
174 const char *a1, const char *a2)
175 {
176 char *value;
177 obstack_fgrow2 (&muscle_obstack, "[[[%s]], [[%s]]]", a1, a2);
178 obstack_1grow (&muscle_obstack, 0);
179 value = obstack_finish (&muscle_obstack);
180 muscle_grow (muscle, value, ",\n");
181 obstack_free (&muscle_obstack, value);
182 }
183
184 /*-------------------------------.
185 | Find the value of muscle KEY. |
186 `-------------------------------*/
187
188 char*
189 muscle_find (const char *key)
190 {
191 muscle_entry_t probe;
192 muscle_entry_t *result = NULL;
193
194 probe.key = key;
195 result = hash_lookup (muscle_table, &probe);
196 return result ? result->value : NULL;
197 }
198
199
200 /*------------------------------------------------.
201 | Output the definition of ENTRY as a m4_define. |
202 `------------------------------------------------*/
203
204 static int
205 muscle_m4_output (muscle_entry_t *entry, FILE *out)
206 {
207 fprintf (out, "m4_define([b4_%s],\n", entry->key);
208 fprintf (out, "[[%s]])\n\n\n", entry->value);
209 return 1;
210 }
211
212
213 /*----------------------------------------------------------------.
214 | Output the definition of all the current muscles into a list of |
215 | m4_defines. |
216 `----------------------------------------------------------------*/
217
218 void
219 muscles_m4_output (FILE *out)
220 {
221 hash_do_for_each (muscle_table,
222 (Hash_processor) muscle_m4_output,
223 out);
224 }