]> git.saurik.com Git - bison.git/blob - src/muscle_tab.c
* NEWS: Version 2.0b.
[bison.git] / src / muscle_tab.c
1 /* Muscle table manager for Bison.
2
3 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software
4 Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 Bison is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 Bison is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include "system.h"
24
25 #include <hash.h>
26 #include <quotearg.h>
27
28 #include "files.h"
29 #include "muscle_tab.h"
30 #include "getargs.h"
31
32 typedef struct
33 {
34 const char *key;
35 char *value;
36 } muscle_entry;
37
38 /* An obstack used to create some entries. */
39 struct obstack muscle_obstack;
40
41 /* Initial capacity of muscles hash table. */
42 #define HT_INITIAL_CAPACITY 257
43
44 static struct hash_table *muscle_table = NULL;
45
46 static bool
47 hash_compare_muscles (void const *x, void const *y)
48 {
49 muscle_entry const *m1 = x;
50 muscle_entry const *m2 = y;
51 return strcmp (m1->key, m2->key) == 0;
52 }
53
54 static size_t
55 hash_muscle (const void *x, size_t tablesize)
56 {
57 muscle_entry const *m = x;
58 return hash_string (m->key, tablesize);
59 }
60
61 /*-----------------------------------------------------------------.
62 | Create the MUSCLE_TABLE, and initialize it with default values. |
63 | Also set up the MUSCLE_OBSTACK. |
64 `-----------------------------------------------------------------*/
65
66 void
67 muscle_init (void)
68 {
69 /* Initialize the muscle obstack. */
70 obstack_init (&muscle_obstack);
71
72 muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle,
73 hash_compare_muscles, free);
74
75 /* Version and input file. */
76 MUSCLE_INSERT_STRING ("version", VERSION);
77 MUSCLE_INSERT_C_STRING ("filename", grammar_file);
78 }
79
80
81 /*------------------------------------------------------------.
82 | Free all the memory consumed by the muscle machinery only. |
83 `------------------------------------------------------------*/
84
85 void
86 muscle_free (void)
87 {
88 hash_free (muscle_table);
89 obstack_free (&muscle_obstack, NULL);
90 }
91
92
93
94 /*------------------------------------------------------------.
95 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
96 | previous value. |
97 `------------------------------------------------------------*/
98
99 void
100 muscle_insert (const char *key, char *value)
101 {
102 muscle_entry probe;
103 muscle_entry *entry;
104
105 probe.key = key;
106 entry = hash_lookup (muscle_table, &probe);
107
108 if (!entry)
109 {
110 /* First insertion in the hash. */
111 entry = xmalloc (sizeof *entry);
112 entry->key = key;
113 hash_insert (muscle_table, entry);
114 }
115 entry->value = value;
116 }
117
118
119 /*-------------------------------------------------------------------.
120 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
121 | previous value. Uses MUSCLE_OBSTACK. De-allocates the previously |
122 | associated value. VALUE and SEPARATOR are copied. |
123 `-------------------------------------------------------------------*/
124
125 void
126 muscle_grow (const char *key, const char *val, const char *separator)
127 {
128 muscle_entry probe;
129 muscle_entry *entry = NULL;
130
131 probe.key = key;
132 entry = hash_lookup (muscle_table, &probe);
133
134 if (!entry)
135 {
136 /* First insertion in the hash. */
137 entry = xmalloc (sizeof *entry);
138 entry->key = key;
139 hash_insert (muscle_table, entry);
140 entry->value = xstrdup (val);
141 }
142 else
143 {
144 /* Grow the current value. */
145 char *new_val;
146 obstack_sgrow (&muscle_obstack, entry->value);
147 free (entry->value);
148 obstack_sgrow (&muscle_obstack, separator);
149 obstack_sgrow (&muscle_obstack, val);
150 obstack_1grow (&muscle_obstack, 0);
151 new_val = obstack_finish (&muscle_obstack);
152 entry->value = xstrdup (new_val);
153 obstack_free (&muscle_obstack, new_val);
154 }
155 }
156
157
158 /*------------------------------------------------------------------.
159 | Append VALUE to the current value of KEY, using muscle_grow. But |
160 | in addition, issue a synchronization line for the location LOC. |
161 `------------------------------------------------------------------*/
162
163 void
164 muscle_code_grow (const char *key, const char *val, location loc)
165 {
166 char *extension = NULL;
167 obstack_fgrow1 (&muscle_obstack, "]b4_syncline(%d, [[", loc.start.line);
168 MUSCLE_OBSTACK_SGROW (&muscle_obstack,
169 quotearg_style (c_quoting_style, loc.start.file));
170 obstack_sgrow (&muscle_obstack, "]])[\n");
171 obstack_sgrow (&muscle_obstack, val);
172 obstack_1grow (&muscle_obstack, 0);
173 extension = obstack_finish (&muscle_obstack);
174 muscle_grow (key, extension, "");
175 }
176
177
178 /*-------------------------------------------------------------------.
179 | MUSCLE is an M4 list of pairs. Create or extend it with the pair |
180 | (A1, A2). Note that because the muscle values are output *double* |
181 | quoted, one needs to strip the first level of quotes to reach the |
182 | list itself. |
183 `-------------------------------------------------------------------*/
184
185 void muscle_pair_list_grow (const char *muscle,
186 const char *a1, const char *a2)
187 {
188 char *pair;
189 obstack_fgrow2 (&muscle_obstack, "[[[%s]], [[%s]]]", a1, a2);
190 obstack_1grow (&muscle_obstack, 0);
191 pair = obstack_finish (&muscle_obstack);
192 muscle_grow (muscle, pair, ",\n");
193 obstack_free (&muscle_obstack, pair);
194 }
195
196 /*-------------------------------.
197 | Find the value of muscle KEY. |
198 `-------------------------------*/
199
200 char*
201 muscle_find (const char *key)
202 {
203 muscle_entry probe;
204 muscle_entry *result = NULL;
205
206 probe.key = key;
207 result = hash_lookup (muscle_table, &probe);
208 return result ? result->value : NULL;
209 }
210
211
212 /*------------------------------------------------.
213 | Output the definition of ENTRY as a m4_define. |
214 `------------------------------------------------*/
215
216 static inline bool
217 muscle_m4_output (muscle_entry *entry, FILE *out)
218 {
219 fprintf (out, "m4_define([b4_%s],\n", entry->key);
220 fprintf (out, "[[%s]])\n\n\n", entry->value);
221 return true;
222 }
223
224 static bool
225 muscle_m4_output_processor (void *entry, void *out)
226 {
227 return muscle_m4_output (entry, out);
228 }
229
230
231 /*----------------------------------------------------------------.
232 | Output the definition of all the current muscles into a list of |
233 | m4_defines. |
234 `----------------------------------------------------------------*/
235
236 void
237 muscles_m4_output (FILE *out)
238 {
239 hash_do_for_each (muscle_table, muscle_m4_output_processor, out);
240 }