Debian Codegraph @

plugins/symbolbrowser/symbols/parsers/keyword.cpp

raw symbols SCIP
1 /***************************************************************************
2 A SymbolBrowser plugin for JuffEd editor.
3
4 Copyright: 2010 Aleksander Sokoloff <[email protected]>
5
6 Some of the code based on a ctags project http://ctags.sourceforge.net/
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License.
10 version 2 as published by the Free Software Foundation.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have rceived a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 ***************************************************************************/
21
22
23 #include "keyword.h"
24
25
26 /*****************************************************************************
27 *****************************************************************************/
28 Keywords::Keywords() {
29 unsigned int i;
30 table = xMalloc (TableSize, hashEntry*);
31 for (i = 0 ; i < TableSize ; ++i)
32 table [i] = NULL;
33 }
34
35
36 /*****************************************************************************
37 *****************************************************************************/
38 Keywords::~Keywords() {
39 if (table != NULL)
40 {
41 unsigned int i;
42
43 for (i = 0 ; i < TableSize ; ++i)
44 {
45 hashEntry *entry = table[i];
46
47 while (entry != NULL)
48 {
49 hashEntry *next = entry->next;
50 eFree (entry);
51 entry = next;
52 }
53 }
54 eFree (table);
55 }
56 }
57
58
59 /*****************************************************************************
60 *****************************************************************************/
61 Keywords::hashEntry *Keywords::getHashTableEntry (unsigned long hashedValue)
62 {
63 hashEntry *entry;
64
65 Assert (hashedValue < TableSize);
66 entry = table [hashedValue];
67
68 return entry;
69 }
70
71
72 /*****************************************************************************
73 *****************************************************************************/
74 unsigned long Keywords::hashValue (const char *const string)
75 {
76 unsigned long value = 0;
77 const unsigned char *p;
78
79 Assert (string != NULL);
80
81 /* We combine the various words of the multiword key using the method
82 * described on page 512 of Vol. 3 of "The Art of Computer Programming".
83 */
84 for (p = (const unsigned char *) string ; *p != '\0' ; ++p)
85 {
86 value <<= 1;
87 if (value & 0x00000100L)
88 value = (value & 0x000000ffL) + 1L;
89 value ^= *p;
90 }
91 /* Algorithm from page 509 of Vol. 3 of "The Art of Computer Programming"
92 * Treats "value" as a 16-bit integer plus 16-bit fraction.
93 */
94 value *= 40503L; /* = 2^16 * 0.6180339887 ("golden ratio") */
95 value &= 0x0000ffffL; /* keep fractional part */
96 value >>= 16 - HASH_EXPONENT; /* scale up by hash size and move down */
97
98 return value;
99 }
100
101
102 /*****************************************************************************
103 *****************************************************************************/
104 Keywords::hashEntry *Keywords::newEntry (
105 const char *const string, Language language, int value)
106 {
107 hashEntry *const entry = xMalloc (1, hashEntry);
108
109 entry->next = NULL;
110 entry->string = string;
111 entry->language = language;
112 entry->value = value;
113
114 return entry;
115 }
116
117
118 /*****************************************************************************
119 Note that it is assumed that a "value" of zero means an undefined keyword
120 and clients of this function should observe this. Also, all keywords added
121 should be added in lower case. If we encounter a case-sensitive language
122 whose keywords are in upper case, we will need to redesign this.
123 *****************************************************************************/
124 void Keywords::addKeyword (const char *const string, Language language, int value)
125 {
126 const unsigned long hashedValue = hashValue (string);
127 hashEntry *entry = getHashTableEntry (hashedValue);
128
129 if (entry == NULL)
130 {
131 table [hashedValue] = newEntry (string, language, value);
132 }
133 else
134 {
135 hashEntry *prev = NULL;
136
137 while (entry != NULL)
138 {
139 if (language == entry->language &&
140 strcmp (string, entry->string) == 0)
141 {
142 Assert (("Already in table" == NULL));
143 }
144 prev = entry;
145 entry = entry->next;
146 }
147 if (entry == NULL)
148 {
149 Assert (prev != NULL);
150 prev->next = newEntry (string, language, value);
151 }
152 }
153 }
154
155
156 /*****************************************************************************
157 *****************************************************************************/
158 int Keywords::lookupKeyword (const char *const string, Language language)
159 {
160 const unsigned long hashedValue = hashValue (string);
161 hashEntry *entry = getHashTableEntry (hashedValue);
162 int result = -1;
163
164 while (entry != NULL)
165 {
166 if (language == entry->language && strcmp (string, entry->string) == 0)
167 {
168 result = entry->value;
169 break;
170 }
171 entry = entry->next;
172 }
173 return result;
174 }
175
176
177 /*****************************************************************************
178 *****************************************************************************/
179 int Keywords::analyzeToken (vString *const name, Language language)
180 {
181 vString *keyword = vStringNew ();
182 int result;
183 vStringCopyToLower (keyword, name);
184 result = lookupKeyword (vStringValue (keyword), language);
185 vStringDelete (keyword);
186 return result;
187 }
188
189 #ifdef DEBUG
190
191
192 /*****************************************************************************
193 *****************************************************************************/
194 void Keywords::printEntry (const hashEntry *const entry)
195 {
196 printf (" %-15s %-7s\n", entry->string, getLanguageName (entry->language));
197 }
198
199
200 /*****************************************************************************
201 *****************************************************************************/
202 unsigned int Keywords::printBucket (const unsigned int i)
203 {
204 hashEntry **const table = getHashTable ();
205 hashEntry *entry = table [i];
206 unsigned int measure = 1;
207 boolean first = true;
208
209 printf ("%2d:", i);
210 if (entry == NULL)
211 printf ("\n");
212 else while (entry != NULL)
213 {
214 if (! first)
215 printf (" ");
216 else
217 {
218 printf (" ");
219 first = false;
220 }
221 printEntry (entry);
222 entry = entry->next;
223 measure = 2 * measure;
224 }
225 return measure - 1;
226 }
227
228
229 /*****************************************************************************
230 *****************************************************************************/
231 void Keywords::printKeywordTable (void)
232 {
233 // unsigned long emptyBucketCount = 0;
234 // unsigned long measure = 0;
235 // unsigned int i;
236 //
237 // for (i = 0 ; i < TableSize ; ++i)
238 // {
239 // const unsigned int pass = printBucket (i);
240 //
241 // measure += pass;
242 // if (pass == 0)
243 // ++emptyBucketCount;
244 // }
245 //
246 // printf ("spread measure = %ld\n", measure);
247 // printf ("%ld empty buckets\n", emptyBucketCount);
248 }
249
250 #endif