]> git.saurik.com Git - bison.git/blame - src/muscle_tab.c
When reducing initial empty rules, Bison parser read an initial
[bison.git] / src / muscle_tab.c
CommitLineData
e00b6826
PE
1/* Muscle table manager for Bison.
2
beda758b 3 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
f753cd62
MA
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
f753cd62 22#include "system.h"
8322e8f5
PE
23
24#include <hash.h>
25#include <quotearg.h>
26
f753cd62
MA
27#include "files.h"
28#include "muscle_tab.h"
f508cb0a 29#include "getargs.h"
f753cd62 30
8322e8f5
PE
31typedef struct
32{
33 const char *key;
34 char *value;
35} muscle_entry;
592e8d4d
AD
36
37/* An obstack used to create some entries. */
38struct obstack muscle_obstack;
39
beda758b
AD
40/* Initial capacity of muscles hash table. */
41#define HT_INITIAL_CAPACITY 257
f753cd62 42
beda758b 43struct hash_table *muscle_table = NULL;
f753cd62 44
beda758b
AD
45static bool
46hash_compare_muscles (void const *x, void const *y)
f753cd62 47{
8322e8f5
PE
48 muscle_entry const *m1 = x;
49 muscle_entry const *m2 = y;
5dd5fd4a 50 return strcmp (m1->key, m2->key) == 0;
f753cd62
MA
51}
52
beda758b
AD
53static unsigned int
54hash_muscle (const void *x, unsigned int tablesize)
f753cd62 55{
8322e8f5 56 muscle_entry const *m = x;
beda758b 57 return hash_string (m->key, tablesize);
f753cd62
MA
58}
59
592e8d4d
AD
60/*-----------------------------------------------------------------.
61| Create the MUSCLE_TABLE, and initialize it with default values. |
62| Also set up the MUSCLE_OBSTACK. |
63`-----------------------------------------------------------------*/
64
f753cd62
MA
65void
66muscle_init (void)
67{
ae7453f2
AD
68 /* Initialize the muscle obstack. */
69 obstack_init (&muscle_obstack);
70
beda758b 71 muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle,
592e8d4d 72 hash_compare_muscles, free);
f753cd62
MA
73
74 /* Version and input file. */
ae7453f2 75 MUSCLE_INSERT_STRING ("version", VERSION);
95612cfa 76 MUSCLE_INSERT_C_STRING ("filename", grammar_file);
f753cd62
MA
77}
78
592e8d4d
AD
79
80/*------------------------------------------------------------.
81| Free all the memory consumed by the muscle machinery only. |
82`------------------------------------------------------------*/
83
84void
85muscle_free (void)
86{
87 hash_free (muscle_table);
88 obstack_free (&muscle_obstack, NULL);
89}
90
91
92
ae7453f2
AD
93/*------------------------------------------------------------.
94| Insert (KEY, VALUE). If KEY already existed, overwrite the |
95| previous value. |
96`------------------------------------------------------------*/
97
a870c567 98void
ae7453f2 99muscle_insert (const char *key, char *value)
f753cd62 100{
8322e8f5
PE
101 muscle_entry probe;
102 muscle_entry *entry = NULL;
e9bca3ad 103
ae7453f2
AD
104 probe.key = key;
105 entry = hash_lookup (muscle_table, &probe);
beda758b
AD
106
107 if (!entry)
108 {
109 /* First insertion in the hash. */
e00b6826 110 MALLOC (entry, 1);
beda758b
AD
111 entry->key = key;
112 hash_insert (muscle_table, entry);
113 }
114 entry->value = value;
f753cd62
MA
115}
116
ae7453f2
AD
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
124void
125muscle_grow (const char *key, const char *val, const char *separator)
126{
8322e8f5
PE
127 muscle_entry probe;
128 muscle_entry *entry = NULL;
ae7453f2
AD
129
130 probe.key = key;
131 entry = hash_lookup (muscle_table, &probe);
132
133 if (!entry)
134 {
135 /* First insertion in the hash. */
e00b6826 136 MALLOC (entry, 1);
ae7453f2
AD
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;
ae7453f2
AD
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);
ae7453f2
AD
152 obstack_free (&muscle_obstack, new_val);
153 }
154}
155
156
cd3684cf
AD
157/*------------------------------------------------------------------.
158| Append VALUE to the current value of KEY, using muscle_grow. But |
159| in addition, issue a synchronization line for the location LOC. |
160`------------------------------------------------------------------*/
161
162void
163muscle_code_grow (const char *key, const char *val, location loc)
164{
165 char *extension = NULL;
166 obstack_fgrow1 (&muscle_obstack, "]b4_syncline([[%d]], [[", loc.start.line);
167 MUSCLE_OBSTACK_SGROW (&muscle_obstack,
168 quotearg_style (c_quoting_style, loc.start.file));
169 obstack_sgrow (&muscle_obstack, "]])[\n");
170 obstack_sgrow (&muscle_obstack, val);
171 obstack_1grow (&muscle_obstack, 0);
172 extension = obstack_finish (&muscle_obstack);
173 muscle_grow (key, extension, "");
174}
175
176
ae7453f2
AD
177/*-------------------------------------------------------------------.
178| MUSCLE is an M4 list of pairs. Create or extend it with the pair |
179| (A1, A2). Note that because the muscle values are output *double* |
180| quoted, one needs to strip the first level of quotes to reach the |
181| list itself. |
182`-------------------------------------------------------------------*/
183
184void muscle_pair_list_grow (const char *muscle,
185 const char *a1, const char *a2)
186{
66d30cd4 187 char *pair;
ae7453f2
AD
188 obstack_fgrow2 (&muscle_obstack, "[[[%s]], [[%s]]]", a1, a2);
189 obstack_1grow (&muscle_obstack, 0);
66d30cd4
AD
190 pair = obstack_finish (&muscle_obstack);
191 muscle_grow (muscle, pair, ",\n");
192 obstack_free (&muscle_obstack, pair);
ae7453f2
AD
193}
194
195/*-------------------------------.
196| Find the value of muscle KEY. |
197`-------------------------------*/
198
199char*
f753cd62
MA
200muscle_find (const char *key)
201{
8322e8f5
PE
202 muscle_entry probe;
203 muscle_entry *result = NULL;
e9bca3ad 204
ae7453f2
AD
205 probe.key = key;
206 result = hash_lookup (muscle_table, &probe);
beda758b 207 return result ? result->value : NULL;
f753cd62 208}
be2a1a68
AD
209
210
ae7453f2
AD
211/*------------------------------------------------.
212| Output the definition of ENTRY as a m4_define. |
213`------------------------------------------------*/
be2a1a68 214
e00b6826 215static inline bool
8322e8f5 216muscle_m4_output (muscle_entry *entry, FILE *out)
be2a1a68
AD
217{
218 fprintf (out, "m4_define([b4_%s],\n", entry->key);
ae7453f2 219 fprintf (out, "[[%s]])\n\n\n", entry->value);
e00b6826
PE
220 return true;
221}
222
223static bool
224muscle_m4_output_processor (void *entry, void *out)
225{
226 return muscle_m4_output (entry, out);
be2a1a68
AD
227}
228
229
ae7453f2
AD
230/*----------------------------------------------------------------.
231| Output the definition of all the current muscles into a list of |
232| m4_defines. |
233`----------------------------------------------------------------*/
be2a1a68
AD
234
235void
236muscles_m4_output (FILE *out)
237{
e00b6826 238 hash_do_for_each (muscle_table, muscle_m4_output_processor, out);
be2a1a68 239}