4 * High efficiency lexical state parser
6 * Copyright (C)2011-2014 Andy Green <andy@warmcat.com>
10 * Usage: gcc minilex.c -o minilex && ./minilex > lextable.h
12 * Run it twice to test parsing on the generated table on stderr
19 /* set of parsable strings -- ALL LOWER CASE */
29 "sec-websocket-draft:",
32 "sec-websocket-extensions:",
33 "sec-websocket-key1:",
34 "sec-websocket-key2:",
35 "sec-websocket-protocol:",
37 "sec-websocket-accept:",
38 "sec-websocket-nonce:",
43 "access-control-request-headers:",
58 "sec-websocket-version:",
59 "sec-websocket-origin:",
61 "", /* not matchable */
69 * 0x00 - 0x07, then terminal as given in 2nd byte
71 * no match: go fwd 3 byte, match: jump fwd by amt in +1/+2 bytes
73 * no match: die, match go fwd 1 byte
76 unsigned char lextable[] = {
91 struct state state[1000];
94 #define FAIL_CHAR 0x08
96 int lextable_decode(int pos, char c)
100 if (lextable[pos] & (1 << 7)) { /* 1-byte, fail on mismatch */
101 if ((lextable[pos] & 0x7f) != c)
105 if (lextable[pos] == FAIL_CHAR)
108 } else { /* b7 = 0, end or 3-byte */
109 if (lextable[pos] < FAIL_CHAR) /* terminal marker */
112 if (lextable[pos] == c) /* goto */
113 return pos + (lextable[pos + 1]) +
114 (lextable[pos + 2] << 8);
134 while (n < sizeof(set) / sizeof(set[0])) {
140 if (set[n][0] == '\0') {
148 for (y = 0; y < state[walk].count; y++)
149 if (state[walk].c[y] == set[n][m]) {
150 /* exists -- go forward */
151 walk = state[walk].state[y];
159 /* something we didn't see before */
161 state[walk].c[state[walk].count] = set[n][m];
163 state[walk].state[state[walk].count] = next;
170 state[walk].c[0] = n++;
171 state[walk].state[0] = 0; /* terminal marker */
172 state[walk].count = 1;
176 for (n = 0; n < next; n++) {
177 state[n].bytepos = walk;
178 walk += (2 * state[n].count);
181 /* compute everyone's position first */
185 for (n = 0; n < next; n++) {
187 state[n].real_pos = pos;
189 for (m = 0; m < state[n].count; m++) {
191 if (state[n].state[m] == 0)
192 pos += 2; /* terminal marker */
193 else { /* c is a character */
194 if ((state[state[n].state[m]].bytepos -
199 if (m == state[n].count - 1)
209 for (n = 0; n < next; n++) {
210 for (m = 0; m < state[n].count; m++) {
213 fprintf(stdout, "/* pos %04x: %3d */ ",
214 state[n].real_pos, n);
216 fprintf(stdout, " ");
219 saw = state[n].state[m];
221 if (saw == 0) { // c is a terminal then
224 fprintf(stderr, "terminal too big\n");
228 fprintf(stdout, " 0x%02X, 0x%02X "
230 "/* - terminal marker %2d - */,\n",
231 y >> 8, y & 0xff, y & 0x7f);
237 /* c is a character */
240 if (prev < 32 || prev > 126)
244 if ((state[saw].bytepos - walk) == 2) {
245 fprintf(stdout, " 0x%02X /* '%c' -> */,\n",
252 j = state[saw].real_pos - pos;
256 "Jump > 64K bytes ahead (%d to %d)\n",
257 state[n].real_pos, state[saw].real_pos);
260 fprintf(stdout, " 0x%02X /* '%c' */, 0x%02X, 0x%02X "
261 "/* (to 0x%04X state %3d) */,\n",
264 state[saw].real_pos, saw);
267 if (m == state[n].count - 1) {
269 " 0x%02X, /* fail */\n",
278 fprintf(stdout, "/* total size %d bytes */\n", pos);
281 * Try to parse every legal input string
284 for (n = 0; n < sizeof(set) / sizeof(set[0]); n++) {
289 if (set[n][0] == '\0')
292 fprintf(stderr, " trying '%s'\n", set[n]);
295 walk = lextable_decode(walk, set[n][m]);
297 fprintf(stderr, "failed\n");
301 if (lextable[walk] < FAIL_CHAR) {
302 y = (lextable[walk] << 8) + lextable[walk + 1];
309 fprintf(stderr, "decode failed %d\n", y);
314 fprintf(stderr, "All decode OK\n");