summaryrefslogtreecommitdiffstats
path: root/keywords.c
blob: fa2f4144d692a209317c03cdbe36d60dcd8f1ce7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* C code produced by gperf version 2.1p1 (K&R C version, modified by Pavel) */
/* Command-line: pgperf -aCIptT -k1,3,$ keywords.gperf  */

#include "my-ctype.h"

	/* -*- C -*- */

/******************************************************************************
  Copyright (c) 1992, 1995, 1996 Xerox Corporation.  All rights reserved.
  Portions of this code were written by Stephen White, aka ghond.
  Use and copying of this software and preparation of derivative works based
  upon this software are permitted.  Any distribution of this software or
  derivative works must comply with all applicable United States export
  control laws.  This software is made available AS IS, and Xerox Corporation
  makes no warranty about the software, its performance or its conformity to
  any specification.  Any person obtaining a copy of this software is requested
  to send their name and post office or electronic mail address to:
    Pavel Curtis
    Xerox PARC
    3333 Coyote Hill Rd.
    Palo Alto, CA 94304
    Pavel@Xerox.Com
 *****************************************************************************/

#include "my-string.h"

#include "config.h"
#include "keywords.h"
#include "tokens.h"
#include "utils.h"


#define MIN_WORD_LENGTH 2
#define MAX_WORD_LENGTH 9
#define MIN_HASH_VALUE 3
#define MAX_HASH_VALUE 106
/*
   35 keywords
   104 is the maximum key range
 */

static int
hash(register const char *str, register int len)
{
    static const unsigned char hash_table[] =
    {
	106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
	106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
	106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
	106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
	106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
	106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
	106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
	106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
	106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
	106, 106, 106, 106, 106, 106, 106, 10, 0, 45,
	0, 0, 0, 10, 106, 45, 106, 10, 106, 35,
	5, 106, 5, 10, 0, 25, 55, 106, 35, 5,
	106, 10, 106, 106, 106, 106, 106, 106,
    };
    register int hval = len;

    switch (hval) {
    default:
    case 3:
	hval += hash_table[tolower((unsigned char) str[2])];
    case 2:
    case 1:
	hval += hash_table[tolower((unsigned char) str[0])];
    }
    return hval + hash_table[tolower((unsigned char) str[len - 1])];
}

static int
case_strcmp(register const char *str, register const char *key)
{
    int ans = 0;

    while (!(ans = tolower(*str) - (int) *key) && *str)
	str++, key++;

    return ans;
}

const struct keyword *
in_word_set(register const char *str, register int len)
{

    static const struct keyword wordlist[] =
    {
	{"",},
	{"",},
	{"",},
	{"for", DBV_Prehistory, tFOR},
	{"",},
	{"endif", DBV_Prehistory, tENDIF},
	{"endfor", DBV_Prehistory, tENDFOR},
	{"e_range", DBV_Prehistory, tERROR, E_RANGE},
	{"endwhile", DBV_Prehistory, tENDWHILE},
	{"e_recmove", DBV_Prehistory, tERROR, E_RECMOVE},
	{"",},
	{"e_none", DBV_Prehistory, tERROR, E_NONE},
	{"",},
	{"e_propnf", DBV_Prehistory, tERROR, E_PROPNF},
	{"fork", DBV_Prehistory, tFORK},
	{"break", DBV_BreakCont, tBREAK},
	{"endtry", DBV_Exceptions, tENDTRY},
	{"endfork", DBV_Prehistory, tENDFORK},
	{"",},
	{"",},
	{"",},
	{"",},
	{"finally", DBV_Exceptions, tFINALLY},
	{"",},
	{"",},
	{"",},
	{"",},
	{"e_quota", DBV_Prehistory, tERROR, E_QUOTA},
	{"",},
	{"else", DBV_Prehistory, tELSE},
	{"",},
	{"elseif", DBV_Prehistory, tELSEIF},
	{"",},
	{"any", DBV_Exceptions, tANY},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"e_div", DBV_Prehistory, tERROR, E_DIV},
	{"e_args", DBV_Prehistory, tERROR, E_ARGS},
	{"e_varnf", DBV_Prehistory, tERROR, E_VARNF},
	{"e_verbnf", DBV_Prehistory, tERROR, E_VERBNF},
	{"",},
	{"",},
	{"e_perm", DBV_Prehistory, tERROR, E_PERM},
	{"if", DBV_Prehistory, tIF},
	{"",},
	{"",},
	{"",},
	{"",},
	{"in", DBV_Prehistory, tIN},
	{"e_invind", DBV_Prehistory, tERROR, E_INVIND},
	{"",},
	{"while", DBV_Prehistory, tWHILE},
	{"e_nacc", DBV_Prehistory, tERROR, E_NACC},
	{"",},
	{"continue", DBV_BreakCont, tCONTINUE},
	{"",},
	{"",},
	{"e_type", DBV_Prehistory, tERROR, E_TYPE},
	{"e_float", DBV_Float, tERROR, E_FLOAT},
	{"e_invarg", DBV_Prehistory, tERROR, E_INVARG},
	{"",},
	{"",},
	{"return", DBV_Prehistory, tRETURN},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"try", DBV_Exceptions, tTRY},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"e_maxrec", DBV_Prehistory, tERROR, E_MAXREC},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"",},
	{"except", DBV_Exceptions, tEXCEPT},
    };

    if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH) {
	register int key = hash(str, len);

	if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE) {
	    register const char *s = wordlist[key].name;

	    if (*s == tolower(*str) && !case_strcmp(str + 1, s + 1))
		return &wordlist[key];
	}
    }
    return 0;
}

const struct keyword *
find_keyword(const char *word)
{
    return in_word_set(word, strlen(word));
}

char rcsid_keywords[] = "$Id$";

/* 
 * $Log$
 * Revision 1.3  1998/12/14 13:17:55  nop
 * Merge UNSAFE_OPTS (ref fixups); fix Log tag placement to fit CVS whims
 *
 * Revision 1.2  1997/03/03 04:18:45  nop
 * GNU Indent normalization
 *
 * Revision 1.1.1.1  1997/03/03 03:45:00  nop
 * LambdaMOO 1.8.0p5
 *
 * Revision 2.2  1996/02/08  06:33:21  pavel
 * Added `break', `continue', and E_FLOAT.  Updated copyright notice for 1996.
 * Release 1.8.0beta1.
 *
 * Revision 2.1  1995/12/11  08:15:42  pavel
 * Added #include "tokens.h" removed from keywords.h.  Release 1.8.0alpha2.
 *
 * Revision 2.0  1995/11/30  05:02:56  pavel
 * New baseline version, corresponding to release 1.8.0alpha1.
 *
 * Revision 1.1  1995/11/30  05:01:47  pavel
 * Initial revision
 */