4d92ff902f501407544b8af28453bb17e37c69c5
[games.git] / usr.bin / lex / sym.c
1 /* sym - symbol table routines */
2
3 /*-
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Vern Paxson.
9  * 
10  * The United States Government has rights in this work pursuant
11  * to contract no. DE-AC03-76SF00098 between the United States
12  * Department of Energy and the University of California.
13  *
14  * Redistribution and use in source and binary forms are permitted provided
15  * that: (1) source distributions retain this entire copyright notice and
16  * comment, and (2) distributions including binaries display the following
17  * acknowledgement:  ``This product includes software developed by the
18  * University of California, Berkeley and its contributors'' in the
19  * documentation or other materials provided with the distribution and in
20  * all advertising materials mentioning features or use of this software.
21  * Neither the name of the University nor the names of its contributors may
22  * be used to endorse or promote products derived from this software without
23  * specific prior written permission.
24  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
25  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  */
28
29 /* $Header: /home/daffy/u0/vern/flex/RCS/sym.c,v 2.19 95/03/04 16:11:04 vern Exp $ */
30 /* $FreeBSD: src/usr.bin/lex/sym.c,v 1.5 1999/10/27 07:56:47 obrien Exp $ */
31 /* $DragonFly: src/usr.bin/lex/sym.c,v 1.4 2005/08/04 17:31:22 drhodus Exp $ */
32
33 #include "flexdef.h"
34
35
36 /* declare functions that have forward references */
37
38 int hashfunct PROTO((char[], int));
39
40
41 struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
42 struct hash_entry *sctbl[START_COND_HASH_SIZE];
43 struct hash_entry *ccltab[CCL_HASH_SIZE];
44
45 struct hash_entry *findsym();
46
47
48 /* addsym - add symbol and definitions to symbol table
49  *
50  * -1 is returned if the symbol already exists, and the change not made.
51  */
52
53 int addsym(char *sym, char *str_def, int int_def, hash_table table,
54            int table_size)
55         {
56         int hash_val = hashfunct( sym, table_size );
57         struct hash_entry *sym_entry = table[hash_val];
58         struct hash_entry *new_entry;
59         struct hash_entry *successor;
60
61         while ( sym_entry )
62                 {
63                 if ( ! strcmp( sym, sym_entry->name ) )
64                         { /* entry already exists */
65                         return -1;
66                         }
67
68                 sym_entry = sym_entry->next;
69                 }
70
71         /* create new entry */
72         new_entry = (struct hash_entry *)
73                 flex_alloc( sizeof( struct hash_entry ) );
74
75         if ( new_entry == NULL )
76                 flexfatal( _( "symbol table memory allocation failed" ) );
77
78         if ( (successor = table[hash_val]) != 0 )
79                 {
80                 new_entry->next = successor;
81                 successor->prev = new_entry;
82                 }
83         else
84                 new_entry->next = NULL;
85
86         new_entry->prev = NULL;
87         new_entry->name = sym;
88         new_entry->str_val = str_def;
89         new_entry->int_val = int_def;
90
91         table[hash_val] = new_entry;
92
93         return 0;
94         }
95
96
97 /* cclinstal - save the text of a character class */
98
99 void cclinstal(Char *ccltxt, int cclnum)
100         {
101         /* We don't bother checking the return status because we are not
102          * called unless the symbol is new.
103          */
104         Char *copy_unsigned_string();
105
106         (void) addsym( (char *) copy_unsigned_string( ccltxt ),
107                         (char *) 0, cclnum,
108                         ccltab, CCL_HASH_SIZE );
109         }
110
111
112 /* ccllookup - lookup the number associated with character class text
113  *
114  * Returns 0 if there's no CCL associated with the text.
115  */
116
117 int ccllookup(Char *ccltxt)
118         {
119         return findsym( (char *) ccltxt, ccltab, CCL_HASH_SIZE )->int_val;
120         }
121
122
123 /* findsym - find symbol in symbol table */
124
125 struct hash_entry *findsym(char *sym, hash_table table, int table_size)
126         {
127         static struct hash_entry empty_entry =
128                 {
129                 (struct hash_entry *) 0, (struct hash_entry *) 0,
130                 (char *) 0, (char *) 0, 0,
131                 } ;
132         struct hash_entry *sym_entry =
133                 table[hashfunct( sym, table_size )];
134
135         while ( sym_entry )
136                 {
137                 if ( ! strcmp( sym, sym_entry->name ) )
138                         return sym_entry;
139                 sym_entry = sym_entry->next;
140                 }
141
142         return &empty_entry;
143         }
144
145
146 /* hashfunct - compute the hash value for "str" and hash size "hash_size" */
147
148 int hashfunct(char *str, int hash_size)
149         {
150         int hashval;
151         int locstr;
152
153         hashval = 0;
154         locstr = 0;
155
156         while ( str[locstr] )
157                 {
158                 hashval = (hashval << 1) + (unsigned char) str[locstr++];
159                 hashval %= hash_size;
160                 }
161
162         return hashval;
163         }
164
165
166 /* ndinstal - install a name definition */
167
168 void ndinstal(char *name, Char *definition)
169         {
170         char *copy_string();
171         Char *copy_unsigned_string();
172
173         if ( addsym( copy_string( name ),
174                         (char *) copy_unsigned_string( definition ), 0,
175                         ndtbl, NAME_TABLE_HASH_SIZE ) )
176                 synerr( _( "name defined twice" ) );
177         }
178
179
180 /* ndlookup - lookup a name definition
181  *
182  * Returns a nil pointer if the name definition does not exist.
183  */
184
185 Char *ndlookup(char *nd)
186         {
187         return (Char *) findsym( nd, ndtbl, NAME_TABLE_HASH_SIZE )->str_val;
188         }
189
190
191 /* scextend - increase the maximum number of start conditions */
192
193 void scextend(void)
194         {
195         current_max_scs += MAX_SCS_INCREMENT;
196
197         ++num_reallocs;
198
199         scset = reallocate_integer_array( scset, current_max_scs );
200         scbol = reallocate_integer_array( scbol, current_max_scs );
201         scxclu = reallocate_integer_array( scxclu, current_max_scs );
202         sceof = reallocate_integer_array( sceof, current_max_scs );
203         scname = reallocate_char_ptr_array( scname, current_max_scs );
204         }
205
206
207 /* scinstal - make a start condition
208  *
209  * NOTE
210  *    The start condition is "exclusive" if xcluflg is true.
211  */
212
213 void scinstal(char *str, int xcluflg)
214         {
215         char *copy_string();
216
217         /* Generate start condition definition, for use in BEGIN et al. */
218         action_define( str, lastsc );
219
220         if ( ++lastsc >= current_max_scs )
221                 scextend();
222
223         scname[lastsc] = copy_string( str );
224
225         if ( addsym( scname[lastsc], (char *) 0, lastsc,
226                         sctbl, START_COND_HASH_SIZE ) )
227                 format_pinpoint_message(
228                                 _( "start condition %s declared twice" ),
229                                         str );
230
231         scset[lastsc] = mkstate( SYM_EPSILON );
232         scbol[lastsc] = mkstate( SYM_EPSILON );
233         scxclu[lastsc] = xcluflg;
234         sceof[lastsc] = false;
235         }
236
237
238 /* sclookup - lookup the number associated with a start condition
239  *
240  * Returns 0 if no such start condition.
241  */
242
243 int sclookup(char *str)
244         {
245         return findsym( str, sctbl, START_COND_HASH_SIZE )->int_val;
246         }