]> git.saurik.com Git - bison.git/blame - src/struniq.c
* tests/actions.at (Actions after errors): New test case.
[bison.git] / src / struniq.c
CommitLineData
3e6656f9
AD
1/* Keeping a unique copy of strings.
2 Copyright (C) 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 "quotearg.h"
95612cfa 23#include "error.h"
3e6656f9
AD
24#include "hash.h"
25#include "struniq.h"
26
27/*-----------------------.
28| A struniq hash table. |
29`-----------------------*/
30
31/* Initial capacity of struniq hash table. */
32#define HT_INITIAL_CAPACITY 257
33
34static struct hash_table *struniqs_table = NULL;
35
36/*-------------------------------------.
37| Create the struniq for S if needed. |
38`-------------------------------------*/
39
0eae1c91 40struniq_t
3e6656f9
AD
41struniq_new (const char *s)
42{
95612cfa 43 struniq_t res = hash_lookup (struniqs_table, s);
3e6656f9
AD
44 if (!res)
45 {
46 /* First insertion in the hash. */
95612cfa 47 res = xstrdup (s);
3e6656f9
AD
48 hash_insert (struniqs_table, res);
49 }
50 return res;
51}
52
53
95612cfa
AD
54/*---------------------------------.
55| Return TRUE iff S is a struniq. |
56`---------------------------------*/
57
58bool
59struniq_assert_p (const char *s)
60{
61 if (!hash_lookup (struniqs_table, s))
62 {
63 error (0, 0, "not a struniq: %s", quotearg (s));
64 return false;
65 }
66 else
67 {
68 return true;
69 }
70}
71
72
3e6656f9
AD
73/*--------------------.
74| Print the struniq. |
75`--------------------*/
76
77static bool
78struniq_print (const struniq_t s)
79{
80 fprintf (stderr, "%s\n", s);
81 return true;
82}
83
84\f
85/*-----------------------.
86| A struniq hash table. |
87`-----------------------*/
88
89static bool
90hash_compare_struniq_t (const struniq_t m1, const struniq_t m2)
91{
92 return strcmp (m1, m2) == 0;
93}
94
95static unsigned int
96hash_struniq_t (const struniq_t m, unsigned int tablesize)
97{
98 return hash_string (m, tablesize);
99}
100
101/* A function to apply to each symbol. */
102typedef bool (*struniq_processor_t) (const struniq_t);
103
104/*----------------------------.
105| Create the struniqs table. |
106`----------------------------*/
107
108void
109struniqs_new (void)
110{
111 struniqs_table = hash_initialize (HT_INITIAL_CAPACITY,
112 NULL,
113 (Hash_hasher) hash_struniq_t,
114 (Hash_comparator) hash_compare_struniq_t,
115 (Hash_data_freer) free);
116}
117
118
119/*-------------------------------------.
120| Perform a task on all the struniqs. |
121`-------------------------------------*/
122
123static void
124struniqs_do (struniq_processor_t processor, void *processor_data)
125{
126 hash_do_for_each (struniqs_table,
127 (Hash_processor) processor,
128 processor_data);
129}
130
131
132/*-----------------.
133| Print them all. |
134`-----------------*/
135
136void
137struniqs_print (void)
138{
139 struniqs_do (struniq_print, NULL);
140}
141
142
143/*--------------------.
144| Free the struniqs. |
145`--------------------*/
146
147void
148struniqs_free (void)
149{
150 hash_free (struniqs_table);
151}