]>
git.saurik.com Git - bison.git/blob - src/muscle_tab.c
1 /* Muscle table manager for Bison,
2 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
27 #include "muscle_tab.h"
36 /* An obstack used to create some entries. */
37 struct obstack muscle_obstack
;
39 /* Initial capacity of muscles hash table. */
40 #define HT_INITIAL_CAPACITY 257
42 struct hash_table
*muscle_table
= NULL
;
45 hash_compare_muscles (void const *x
, void const *y
)
47 muscle_entry
const *m1
= x
;
48 muscle_entry
const *m2
= y
;
49 return strcmp (m1
->key
, m2
->key
) == 0;
53 hash_muscle (const void *x
, unsigned int tablesize
)
55 muscle_entry
const *m
= x
;
56 return hash_string (m
->key
, tablesize
);
59 /*-----------------------------------------------------------------.
60 | Create the MUSCLE_TABLE, and initialize it with default values. |
61 | Also set up the MUSCLE_OBSTACK. |
62 `-----------------------------------------------------------------*/
67 /* Initialize the muscle obstack. */
68 obstack_init (&muscle_obstack
);
70 muscle_table
= hash_initialize (HT_INITIAL_CAPACITY
, NULL
, hash_muscle
,
71 hash_compare_muscles
, free
);
73 /* Version and input file. */
74 MUSCLE_INSERT_STRING ("version", VERSION
);
75 MUSCLE_INSERT_C_STRING ("filename", grammar_file
);
79 /*------------------------------------------------------------.
80 | Free all the memory consumed by the muscle machinery only. |
81 `------------------------------------------------------------*/
86 hash_free (muscle_table
);
87 obstack_free (&muscle_obstack
, NULL
);
92 /*------------------------------------------------------------.
93 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
95 `------------------------------------------------------------*/
98 muscle_insert (const char *key
, char *value
)
101 muscle_entry
*entry
= NULL
;
104 entry
= hash_lookup (muscle_table
, &probe
);
108 /* First insertion in the hash. */
109 entry
= XMALLOC (muscle_entry
, 1);
111 hash_insert (muscle_table
, entry
);
113 entry
->value
= value
;
117 /*-------------------------------------------------------------------.
118 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
119 | previous value. Uses MUSCLE_OBSTACK. De-allocates the previously |
120 | associated value. VALUE and SEPARATOR are copied. |
121 `-------------------------------------------------------------------*/
124 muscle_grow (const char *key
, const char *val
, const char *separator
)
127 muscle_entry
*entry
= NULL
;
130 entry
= hash_lookup (muscle_table
, &probe
);
134 /* First insertion in the hash. */
135 entry
= XMALLOC (muscle_entry
, 1);
137 hash_insert (muscle_table
, entry
);
138 entry
->value
= xstrdup (val
);
142 /* Grow the current value. */
144 obstack_sgrow (&muscle_obstack
, entry
->value
);
146 obstack_sgrow (&muscle_obstack
, separator
);
147 obstack_sgrow (&muscle_obstack
, val
);
148 obstack_1grow (&muscle_obstack
, 0);
149 new_val
= obstack_finish (&muscle_obstack
);
150 entry
->value
= xstrdup (new_val
);
151 obstack_free (&muscle_obstack
, new_val
);
156 /*-------------------------------------------------------------------.
157 | MUSCLE is an M4 list of pairs. Create or extend it with the pair |
158 | (A1, A2). Note that because the muscle values are output *double* |
159 | quoted, one needs to strip the first level of quotes to reach the |
161 `-------------------------------------------------------------------*/
163 void muscle_pair_list_grow (const char *muscle
,
164 const char *a1
, const char *a2
)
167 obstack_fgrow2 (&muscle_obstack
, "[[[%s]], [[%s]]]", a1
, a2
);
168 obstack_1grow (&muscle_obstack
, 0);
169 pair
= obstack_finish (&muscle_obstack
);
170 muscle_grow (muscle
, pair
, ",\n");
171 obstack_free (&muscle_obstack
, pair
);
174 /*-------------------------------.
175 | Find the value of muscle KEY. |
176 `-------------------------------*/
179 muscle_find (const char *key
)
182 muscle_entry
*result
= NULL
;
185 result
= hash_lookup (muscle_table
, &probe
);
186 return result
? result
->value
: NULL
;
190 /*------------------------------------------------.
191 | Output the definition of ENTRY as a m4_define. |
192 `------------------------------------------------*/
195 muscle_m4_output (muscle_entry
*entry
, FILE *out
)
197 fprintf (out
, "m4_define([b4_%s],\n", entry
->key
);
198 fprintf (out
, "[[%s]])\n\n\n", entry
->value
);
203 /*----------------------------------------------------------------.
204 | Output the definition of all the current muscles into a list of |
206 `----------------------------------------------------------------*/
209 muscles_m4_output (FILE *out
)
211 hash_do_for_each (muscle_table
,
212 (Hash_processor
) muscle_m4_output
,