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