]> git.saurik.com Git - bison.git/blob - src/muscle_tab.c
* src/print.c (print_shifts, print_gotos): Merge into...
[bison.git] / src / muscle_tab.c
1 /* Macro 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 muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle,
60 hash_compare_muscles, free);
61
62 /* Version and input file. */
63 muscle_insert ("version", VERSION);
64 muscle_insert ("filename", infile);
65
66 /* FIXME: there should probably be no default here, only in the
67 skeletons. */
68
69 /* Types. */
70 muscle_insert ("ltype", "yyltype");
71
72 /* Default #line formatting. */
73 muscle_insert ("linef", "#line %d %s\n");
74
75 /* Stack parameters. */
76 muscle_insert ("maxdepth", "10000");
77 muscle_insert ("initdepth", "200");
78
79 /* C++ macros. */
80 muscle_insert ("name", "Parser");
81
82 /* Initialize the muscle obstack. */
83 obstack_init (&muscle_obstack);
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 void
101 muscle_insert (const char *key, const char *value)
102 {
103 muscle_entry_t pair;
104 muscle_entry_t *entry = NULL;
105
106 pair.key = key;
107 entry = hash_lookup (muscle_table, &pair);
108
109 if (!entry)
110 {
111 /* First insertion in the hash. */
112 entry = XMALLOC (muscle_entry_t, 1);
113 entry->key = key;
114 hash_insert (muscle_table, entry);
115 }
116 entry->value = value;
117 }
118
119 const char*
120 muscle_find (const char *key)
121 {
122 muscle_entry_t pair;
123 muscle_entry_t *result = NULL;
124
125 pair.key = key;
126 result = hash_lookup (muscle_table, &pair);
127 return result ? result->value : NULL;
128 }
129
130
131 /* Output the definition of all the current muscles into a list of
132 m4_defines. */
133
134 static int
135 muscle_m4_output (muscle_entry_t *entry, FILE *out)
136 {
137 fprintf (out, "m4_define([b4_%s],\n", entry->key);
138 fprintf (out, " [[%s]])\n\n\n", entry->value);
139 return 1;
140 }
141
142
143 /* Output the definition of all the current muscles into a list of
144 m4_defines. */
145
146 void
147 muscles_m4_output (FILE *out)
148 {
149 hash_do_for_each (muscle_table,
150 (Hash_processor) muscle_m4_output,
151 out);
152 }