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