parser.c 35.4 KB
Newer Older
Teodor Sigaev's avatar
Teodor Sigaev committed
1 2 3 4 5 6 7 8 9 10 11
#include "postgres.h"

#include "utils/builtins.h"
#include "utils/pg_locale.h"
#include "mb/pg_wchar.h"

#include "deflex.h"
#include "parser.h"
#include "ts_locale.h"


12 13 14 15
static TParserPosition *
newTParserPosition(TParserPosition * prev)
{
	TParserPosition *res = (TParserPosition *) palloc(sizeof(TParserPosition));
Teodor Sigaev's avatar
Teodor Sigaev committed
16

17
	if (prev)
Teodor Sigaev's avatar
Teodor Sigaev committed
18 19 20 21 22 23 24 25 26 27 28
		memcpy(res, prev, sizeof(TParserPosition));
	else
		memset(res, 0, sizeof(TParserPosition));

	res->prev = prev;

	res->pushedAtAction = NULL;

	return res;
}

29 30 31 32
TParser *
TParserInit(char *str, int len)
{
	TParser    *prs = (TParser *) palloc0(sizeof(TParser));
Teodor Sigaev's avatar
Teodor Sigaev committed
33 34

	prs->charmaxlen = pg_database_encoding_max_length();
35
	prs->str = str;
Teodor Sigaev's avatar
Teodor Sigaev committed
36 37 38
	prs->lenstr = len;

#ifdef TS_USE_WIDE
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

	/*
	 * Use wide char code only when max encoding length > 1 and ctype != C.
	 * Some operating systems fail with multi-byte encodings and a C locale.
	 * Also, for a C locale there is no need to process as multibyte. From
	 * backend/utils/adt/oracle_compat.c Teodor
	 */

	if (prs->charmaxlen > 1 && !lc_ctype_is_c())
	{
		prs->usewide = true;
		prs->wstr = (wchar_t *) palloc(sizeof(wchar_t) * prs->lenstr);
		prs->lenwstr = char2wchar(prs->wstr, prs->str, prs->lenstr);
	}
	else
Teodor Sigaev's avatar
Teodor Sigaev committed
54
#endif
55
		prs->usewide = false;
Teodor Sigaev's avatar
Teodor Sigaev committed
56 57 58 59 60 61 62 63

	prs->state = newTParserPosition(NULL);
	prs->state->state = TPS_Base;

	return prs;
}

void
64 65 66 67
TParserClose(TParser * prs)
{
	while (prs->state)
	{
Teodor Sigaev's avatar
Teodor Sigaev committed
68
		TParserPosition *ptr = prs->state->prev;
69 70

		pfree(prs->state);
Teodor Sigaev's avatar
Teodor Sigaev committed
71 72 73
		prs->state = ptr;
	}

74 75 76
	if (prs->wstr)
		pfree(prs->wstr);
	pfree(prs);
Teodor Sigaev's avatar
Teodor Sigaev committed
77 78 79 80 81 82 83
}

/*
 * defining support function, equvalent is* macroses, but
 * working with any possible encodings and locales
 */

84
#ifdef TS_USE_WIDE
Teodor Sigaev's avatar
Teodor Sigaev committed
85

86
#define p_iswhat(type)										\
Teodor Sigaev's avatar
Teodor Sigaev committed
87 88 89
static int											\
p_is##type(TParser *prs) {									\
	Assert( prs->state );									\
90
	return ( ( prs->usewide ) ? isw##type( (wint_t)*( prs->wstr + prs->state->poschar ) ) : \
Teodor Sigaev's avatar
Teodor Sigaev committed
91
		is##type( (unsigned char)*( prs->str + prs->state->posbyte ) ) );		\
92
}	\
Teodor Sigaev's avatar
Teodor Sigaev committed
93 94 95 96 97 98 99 100 101 102 103
												\
static int											\
p_isnot##type(TParser *prs) {									\
	return !p_is##type(prs);								\
}



/* p_iseq should be used only for ascii symbols */

static int
104 105 106 107
p_iseq(TParser * prs, char c)
{
	Assert(prs->state);
	return ((prs->state->charlen == 1 && *(prs->str + prs->state->posbyte) == c)) ? 1 : 0;
Teodor Sigaev's avatar
Teodor Sigaev committed
108
}
109
#else							/* TS_USE_WIDE */
Teodor Sigaev's avatar
Teodor Sigaev committed
110

111
#define p_iswhat(type)										\
Teodor Sigaev's avatar
Teodor Sigaev committed
112 113 114
static int											\
p_is##type(TParser *prs) {									\
	Assert( prs->state );									\
115
	return is##type( (unsigned char)*( prs->str + prs->state->posbyte ) );			\
116
}	\
Teodor Sigaev's avatar
Teodor Sigaev committed
117 118 119 120 121 122 123 124
												\
static int											\
p_isnot##type(TParser *prs) {									\
	return !p_is##type(prs);								\
}


static int
125 126 127 128
p_iseq(TParser * prs, char c)
{
	Assert(prs->state);
	return (*(prs->str + prs->state->posbyte) == c) ? 1 : 0;
Teodor Sigaev's avatar
Teodor Sigaev committed
129
}
130
#endif   /* TS_USE_WIDE */
Teodor Sigaev's avatar
Teodor Sigaev committed
131 132 133 134 135 136 137 138 139 140 141 142

p_iswhat(alnum)
p_iswhat(alpha)
p_iswhat(digit)
p_iswhat(lower)
p_iswhat(print)
p_iswhat(punct)
p_iswhat(space)
p_iswhat(upper)
p_iswhat(xdigit)

static int
143 144 145 146
p_isEOF(TParser * prs)
{
	Assert(prs->state);
	return (prs->state->posbyte == prs->lenstr || prs->state->charlen == 0) ? 1 : 0;
Teodor Sigaev's avatar
Teodor Sigaev committed
147 148 149
}

static int
150 151 152
p_iseqC(TParser * prs)
{
	return p_iseq(prs, prs->c);
Teodor Sigaev's avatar
Teodor Sigaev committed
153 154 155
}

static int
156 157
p_isneC(TParser * prs)
{
Teodor Sigaev's avatar
Teodor Sigaev committed
158 159 160 161
	return !p_iseq(prs, prs->c);
}

static int
162 163 164
p_isascii(TParser * prs)
{
	return (prs->state->charlen == 1 && isascii((unsigned char) *(prs->str + prs->state->posbyte))) ? 1 : 0;
Teodor Sigaev's avatar
Teodor Sigaev committed
165 166 167
}

static int
168 169 170
p_islatin(TParser * prs)
{
	return (p_isalpha(prs) && p_isascii(prs)) ? 1 : 0;
Teodor Sigaev's avatar
Teodor Sigaev committed
171 172 173
}

static int
174 175 176
p_isnonlatin(TParser * prs)
{
	return (p_isalpha(prs) && !p_isascii(prs)) ? 1 : 0;
Teodor Sigaev's avatar
Teodor Sigaev committed
177 178
}

179
void		_make_compiler_happy(void);
Teodor Sigaev's avatar
Teodor Sigaev committed
180
void
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
_make_compiler_happy(void)
{
	p_isalnum(NULL);
	p_isnotalnum(NULL);
	p_isalpha(NULL);
	p_isnotalpha(NULL);
	p_isdigit(NULL);
	p_isnotdigit(NULL);
	p_islower(NULL);
	p_isnotlower(NULL);
	p_isprint(NULL);
	p_isnotprint(NULL);
	p_ispunct(NULL);
	p_isnotpunct(NULL);
	p_isspace(NULL);
	p_isnotspace(NULL);
	p_isupper(NULL);
	p_isnotupper(NULL);
	p_isxdigit(NULL);
	p_isnotxdigit(NULL);
	p_isEOF(NULL);
	p_iseqC(NULL);
	p_isneC(NULL);
Teodor Sigaev's avatar
Teodor Sigaev committed
204 205 206 207
}


static void
208 209 210 211 212 213
SpecialTags(TParser * prs)
{
	switch (prs->state->lencharlexeme)
	{
		case 8:			/* </script */
			if (pg_strncasecmp(prs->lexeme, "</script", 8) == 0)
Teodor Sigaev's avatar
Teodor Sigaev committed
214 215
				prs->ignore = false;
			break;
216 217
		case 7:			/* <script || </style */
			if (pg_strncasecmp(prs->lexeme, "</style", 7) == 0)
Teodor Sigaev's avatar
Teodor Sigaev committed
218
				prs->ignore = false;
219
			else if (pg_strncasecmp(prs->lexeme, "<script", 7) == 0)
Teodor Sigaev's avatar
Teodor Sigaev committed
220 221
				prs->ignore = true;
			break;
222 223
		case 6:			/* <style */
			if (pg_strncasecmp(prs->lexeme, "<style", 6) == 0)
Teodor Sigaev's avatar
Teodor Sigaev committed
224 225
				prs->ignore = true;
			break;
226 227
		default:
			break;
Teodor Sigaev's avatar
Teodor Sigaev committed
228 229 230 231
	}
}

static void
232 233
SpecialFURL(TParser * prs)
{
Teodor Sigaev's avatar
Teodor Sigaev committed
234 235 236 237 238 239
	prs->wanthost = true;
	prs->state->posbyte -= prs->state->lenbytelexeme;
	prs->state->poschar -= prs->state->lencharlexeme;
}

static void
240 241
SpecialHyphen(TParser * prs)
{
Teodor Sigaev's avatar
Teodor Sigaev committed
242 243 244 245 246
	prs->state->posbyte -= prs->state->lenbytelexeme;
	prs->state->poschar -= prs->state->lencharlexeme;
}

static int
247 248 249 250
p_isstophost(TParser * prs)
{
	if (prs->wanthost)
	{
Teodor Sigaev's avatar
Teodor Sigaev committed
251 252 253 254 255 256 257
		prs->wanthost = false;
		return 1;
	}
	return 0;
}

static int
258 259
p_isignore(TParser * prs)
{
Teodor Sigaev's avatar
Teodor Sigaev committed
260 261 262 263
	return (prs->ignore) ? 1 : 0;
}

static int
264 265 266 267
p_ishost(TParser * prs)
{
	TParser    *tmpprs = TParserInit(prs->str + prs->state->posbyte, prs->lenstr - prs->state->posbyte);
	int			res = 0;
Teodor Sigaev's avatar
Teodor Sigaev committed
268

269 270
	if (TParserGet(tmpprs) && tmpprs->type == HOST)
	{
Teodor Sigaev's avatar
Teodor Sigaev committed
271 272 273 274
		prs->state->posbyte += tmpprs->lenbytelexeme;
		prs->state->poschar += tmpprs->lencharlexeme;
		prs->state->lenbytelexeme += tmpprs->lenbytelexeme;
		prs->state->lencharlexeme += tmpprs->lencharlexeme;
275
		prs->state->charlen = tmpprs->state->charlen;
Teodor Sigaev's avatar
Teodor Sigaev committed
276 277 278 279 280 281 282 283
		res = 1;
	}
	TParserClose(tmpprs);

	return res;
}

static int
284 285 286 287
p_isURI(TParser * prs)
{
	TParser    *tmpprs = TParserInit(prs->str + prs->state->posbyte, prs->lenstr - prs->state->posbyte);
	int			res = 0;
Teodor Sigaev's avatar
Teodor Sigaev committed
288

289
	tmpprs->state = newTParserPosition(tmpprs->state);
Teodor Sigaev's avatar
Teodor Sigaev committed
290 291
	tmpprs->state->state = TPS_InFileFirst;

292 293
	if (TParserGet(tmpprs) && (tmpprs->type == URI || tmpprs->type == FILEPATH))
	{
Teodor Sigaev's avatar
Teodor Sigaev committed
294 295 296 297
		prs->state->posbyte += tmpprs->lenbytelexeme;
		prs->state->poschar += tmpprs->lencharlexeme;
		prs->state->lenbytelexeme += tmpprs->lenbytelexeme;
		prs->state->lencharlexeme += tmpprs->lencharlexeme;
298
		prs->state->charlen = tmpprs->state->charlen;
Teodor Sigaev's avatar
Teodor Sigaev committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
		res = 1;
	}
	TParserClose(tmpprs);

	return res;
}

/*
 * Table of state/action of parser
 */

#define A_NEXT		0x0000
#define A_BINGO		0x0001
#define A_POP		0x0002
#define A_PUSH		0x0004
#define A_RERUN		0x0008
#define A_CLEAR		0x0010
#define A_MERGE		0x0020
#define A_CLRALL	0x0040

static TParserStateActionItem actionTPS_Base[] = {
320 321 322 323 324 325 326 327 328 329
	{p_isEOF, 0, A_NEXT, TPS_Null, 0, NULL},
	{p_iseqC, '<', A_PUSH, TPS_InTagFirst, 0, NULL},
	{p_isignore, 0, A_NEXT, TPS_InSpace, 0, NULL},
	{p_islatin, 0, A_NEXT, TPS_InLatWord, 0, NULL},
	{p_isnonlatin, 0, A_NEXT, TPS_InCyrWord, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InUnsignedInt, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InSignedIntFirst, 0, NULL},
	{p_iseqC, '+', A_PUSH, TPS_InSignedIntFirst, 0, NULL},
	{p_iseqC, '&', A_PUSH, TPS_InHTMLEntityFirst, 0, NULL},
	{p_iseqC, '/', A_PUSH, TPS_InFileFirst, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
330
	{p_iseqC, '.', A_PUSH, TPS_InPathFirst, 0, NULL},
331 332
	{NULL, 0, A_NEXT, TPS_InSpace, 0, NULL}
};
Teodor Sigaev's avatar
Teodor Sigaev committed
333 334 335


static TParserStateActionItem actionTPS_InUWord[] = {
336 337 338 339
	{p_isEOF, 0, A_BINGO, TPS_Base, UWORD, NULL},
	{p_isalnum, 0, A_NEXT, TPS_InUWord, 0, NULL},
	{p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
	{p_iseqC, '/', A_PUSH, TPS_InFileFirst, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
340
	{p_iseqC, '.', A_PUSH, TPS_InFileNext, 0, NULL},
341 342
	{p_iseqC, '-', A_PUSH, TPS_InHyphenUWordFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, UWORD, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
343 344 345
};

static TParserStateActionItem actionTPS_InLatWord[] = {
346 347
	{p_isEOF, 0, A_BINGO, TPS_Base, LATWORD, NULL},
	{p_islatin, 0, A_NEXT, TPS_Null, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
348 349
	{p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InFileNext, 0, NULL},
350 351 352 353 354 355 356 357
	{p_iseqC, '-', A_PUSH, TPS_InHostFirstAN, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHyphenLatWordFirst, 0, NULL},
	{p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
	{p_iseqC, ':', A_PUSH, TPS_InProtocolFirst, 0, NULL},
	{p_iseqC, '/', A_PUSH, TPS_InFileFirst, 0, NULL},
	{p_isdigit, 0, A_PUSH, TPS_InHost, 0, NULL},
	{p_isalnum, 0, A_NEXT, TPS_InUWord, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, LATWORD, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
358 359 360
};

static TParserStateActionItem actionTPS_InCyrWord[] = {
361 362 363 364 365
	{p_isEOF, 0, A_BINGO, TPS_Base, CYRWORD, NULL},
	{p_isnonlatin, 0, A_NEXT, TPS_Null, 0, NULL},
	{p_isalnum, 0, A_NEXT, TPS_InUWord, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHyphenCyrWordFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, CYRWORD, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
366
};
367

Teodor Sigaev's avatar
Teodor Sigaev committed
368
static TParserStateActionItem actionTPS_InUnsignedInt[] = {
369 370
	{p_isEOF, 0, A_BINGO, TPS_Base, UNSIGNEDINT, NULL},
	{p_isdigit, 0, A_NEXT, TPS_Null, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
371
	{p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
372 373 374 375 376 377 378 379 380
	{p_iseqC, '.', A_PUSH, TPS_InUDecimalFirst, 0, NULL},
	{p_iseqC, 'e', A_PUSH, TPS_InMantissaFirst, 0, NULL},
	{p_iseqC, 'E', A_PUSH, TPS_InMantissaFirst, 0, NULL},
	{p_islatin, 0, A_PUSH, TPS_InHost, 0, NULL},
	{p_isalpha, 0, A_NEXT, TPS_InUWord, 0, NULL},
	{p_iseqC, '/', A_PUSH, TPS_InFileFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, UNSIGNEDINT, NULL}
};

Teodor Sigaev's avatar
Teodor Sigaev committed
381
static TParserStateActionItem actionTPS_InSignedIntFirst[] = {
382 383 384
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_NEXT | A_CLEAR, TPS_InSignedInt, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
385
};
386

Teodor Sigaev's avatar
Teodor Sigaev committed
387
static TParserStateActionItem actionTPS_InSignedInt[] = {
388 389 390 391 392 393 394 395
	{p_isEOF, 0, A_BINGO, TPS_Base, SIGNEDINT, NULL},
	{p_isdigit, 0, A_NEXT, TPS_Null, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InDecimalFirst, 0, NULL},
	{p_iseqC, 'e', A_PUSH, TPS_InMantissaFirst, 0, NULL},
	{p_iseqC, 'E', A_PUSH, TPS_InMantissaFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, SIGNEDINT, NULL}
};

Teodor Sigaev's avatar
Teodor Sigaev committed
396
static TParserStateActionItem actionTPS_InSpace[] = {
397 398 399 400 401 402 403 404 405
	{p_isEOF, 0, A_BINGO, TPS_Base, SPACE, NULL},
	{p_iseqC, '<', A_BINGO, TPS_Base, SPACE, NULL},
	{p_isignore, 0, A_NEXT, TPS_Null, 0, NULL},
	{p_iseqC, '-', A_BINGO, TPS_Base, SPACE, NULL},
	{p_iseqC, '+', A_BINGO, TPS_Base, SPACE, NULL},
	{p_iseqC, '&', A_BINGO, TPS_Base, SPACE, NULL},
	{p_iseqC, '/', A_BINGO, TPS_Base, SPACE, NULL},
	{p_isnotalnum, 0, A_NEXT, TPS_InSpace, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, SPACE, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
406 407 408
};

static TParserStateActionItem actionTPS_InUDecimalFirst[] = {
409 410 411
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_CLEAR, TPS_InUDecimal, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
412 413 414
};

static TParserStateActionItem actionTPS_InUDecimal[] = {
415 416 417 418 419 420
	{p_isEOF, 0, A_BINGO, TPS_Base, DECIMAL, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InUDecimal, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InVersionFirst, 0, NULL},
	{p_iseqC, 'e', A_PUSH, TPS_InMantissaFirst, 0, NULL},
	{p_iseqC, 'E', A_PUSH, TPS_InMantissaFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, DECIMAL, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
421 422 423
};

static TParserStateActionItem actionTPS_InDecimalFirst[] = {
424 425 426
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_CLEAR, TPS_InDecimal, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
427 428 429
};

static TParserStateActionItem actionTPS_InDecimal[] = {
430 431 432 433 434
	{p_isEOF, 0, A_BINGO, TPS_Base, DECIMAL, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InDecimal, 0, NULL},
	{p_iseqC, 'e', A_PUSH, TPS_InMantissaFirst, 0, NULL},
	{p_iseqC, 'E', A_PUSH, TPS_InMantissaFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, DECIMAL, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
435 436 437
};

static TParserStateActionItem actionTPS_InVersionFirst[] = {
438 439 440
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_CLEAR, TPS_InVersion, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
441 442 443
};

static TParserStateActionItem actionTPS_InVersion[] = {
444 445 446 447
	{p_isEOF, 0, A_BINGO, TPS_Base, VERSIONNUMBER, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InVersion, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InVersionFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, VERSIONNUMBER, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
448 449 450
};

static TParserStateActionItem actionTPS_InMantissaFirst[] = {
451 452 453 454 455
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_CLEAR, TPS_InMantissa, 0, NULL},
	{p_iseqC, '+', A_NEXT, TPS_InMantissaSign, 0, NULL},
	{p_iseqC, '-', A_NEXT, TPS_InMantissaSign, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
456 457 458
};

static TParserStateActionItem actionTPS_InMantissaSign[] = {
459 460 461
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_CLEAR, TPS_InMantissa, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
462 463 464
};

static TParserStateActionItem actionTPS_InMantissa[] = {
465 466 467
	{p_isEOF, 0, A_BINGO, TPS_Base, SCIENTIFIC, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InMantissa, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, SCIENTIFIC, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
468 469 470
};

static TParserStateActionItem actionTPS_InHTMLEntityFirst[] = {
471 472 473 474
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '#', A_NEXT, TPS_InHTMLEntityNumFirst, 0, NULL},
	{p_islatin, 0, A_NEXT, TPS_InHTMLEntity, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
475 476 477
};

static TParserStateActionItem actionTPS_InHTMLEntity[] = {
478 479 480 481
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_islatin, 0, A_NEXT, TPS_InHTMLEntity, 0, NULL},
	{p_iseqC, ';', A_NEXT, TPS_InHTMLEntityEnd, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
482 483 484
};

static TParserStateActionItem actionTPS_InHTMLEntityNumFirst[] = {
485 486 487
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHTMLEntityNum, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
488 489 490
};

static TParserStateActionItem actionTPS_InHTMLEntityNum[] = {
491 492 493 494
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHTMLEntityNum, 0, NULL},
	{p_iseqC, ';', A_NEXT, TPS_InHTMLEntityEnd, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
495 496 497
};

static TParserStateActionItem actionTPS_InHTMLEntityEnd[] = {
498
	{NULL, 0, A_BINGO | A_CLEAR, TPS_Base, HTMLENTITY, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
499 500 501
};

static TParserStateActionItem actionTPS_InTagFirst[] = {
502 503 504
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '/', A_PUSH, TPS_InTagCloseFirst, 0, NULL},
	{p_iseqC, '!', A_PUSH, TPS_InCommentFirst, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
505
	{p_iseqC, '?', A_PUSH, TPS_InXMLBegin, 0, NULL},
506 507
	{p_islatin, 0, A_PUSH, TPS_InTag, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
508 509
};

Teodor Sigaev's avatar
Teodor Sigaev committed
510 511 512 513 514 515 516 517
static TParserStateActionItem actionTPS_InXMLBegin[] = {
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	/* <?xml ... */
	{p_iseqC, 'x', A_NEXT, TPS_InTag, 0, NULL},
	{p_iseqC, 'X', A_NEXT, TPS_InTag, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

Teodor Sigaev's avatar
Teodor Sigaev committed
518
static TParserStateActionItem actionTPS_InTagCloseFirst[] = {
519 520 521
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_islatin, 0, A_NEXT, TPS_InTag, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
522 523 524
};

static TParserStateActionItem actionTPS_InTag[] = {
525 526 527 528 529 530 531 532 533
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '>', A_NEXT, TPS_InTagEnd, 0, SpecialTags},
	{p_iseqC, '\'', A_NEXT, TPS_InTagEscapeK, 0, NULL},
	{p_iseqC, '"', A_NEXT, TPS_InTagEscapeKK, 0, NULL},
	{p_islatin, 0, A_NEXT, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_Null, 0, NULL},
	{p_iseqC, '=', A_NEXT, TPS_Null, 0, NULL},
	{p_iseqC, '-', A_NEXT, TPS_Null, 0, NULL},
	{p_iseqC, '#', A_NEXT, TPS_Null, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
534 535 536 537 538
	{p_iseqC, '/', A_NEXT, TPS_Null, 0, NULL},
	{p_iseqC, ':', A_NEXT, TPS_Null, 0, NULL},
	{p_iseqC, '.', A_NEXT, TPS_Null, 0, NULL},
	{p_iseqC, '&', A_NEXT, TPS_Null, 0, NULL},
	{p_iseqC, '?', A_NEXT, TPS_Null, 0, NULL},
539 540 541
	{p_iseqC, '%', A_NEXT, TPS_Null, 0, NULL},
	{p_isspace, 0, A_NEXT, TPS_Null, 0, SpecialTags},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
542 543 544
};

static TParserStateActionItem actionTPS_InTagEscapeK[] = {
545 546 547 548
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '\\', A_PUSH, TPS_InTagBackSleshed, 0, NULL},
	{p_iseqC, '\'', A_NEXT, TPS_InTag, 0, NULL},
	{NULL, 0, A_NEXT, TPS_InTagEscapeK, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
549 550 551
};

static TParserStateActionItem actionTPS_InTagEscapeKK[] = {
552 553 554 555
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '\\', A_PUSH, TPS_InTagBackSleshed, 0, NULL},
	{p_iseqC, '"', A_NEXT, TPS_InTag, 0, NULL},
	{NULL, 0, A_NEXT, TPS_InTagEscapeKK, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
556 557 558
};

static TParserStateActionItem actionTPS_InTagBackSleshed[] = {
559 560
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{NULL, 0, A_MERGE, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
561 562 563
};

static TParserStateActionItem actionTPS_InTagEnd[] = {
564
	{NULL, 0, A_BINGO | A_CLRALL, TPS_Base, TAG, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
565 566 567
};

static TParserStateActionItem actionTPS_InCommentFirst[] = {
568 569
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '-', A_NEXT, TPS_InCommentLast, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
570 571 572
	/* <!DOCTYPE ...>*/
	{p_iseqC, 'D', A_NEXT, TPS_InTag, 0, NULL},
	{p_iseqC, 'd', A_NEXT, TPS_InTag, 0, NULL},
573
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
574 575 576
};

static TParserStateActionItem actionTPS_InCommentLast[] = {
577 578 579
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '-', A_NEXT, TPS_InComment, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
580 581 582
};

static TParserStateActionItem actionTPS_InComment[] = {
583 584 585
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '-', A_NEXT, TPS_InCloseCommentFirst, 0, NULL},
	{NULL, 0, A_NEXT, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
586 587 588
};

static TParserStateActionItem actionTPS_InCloseCommentFirst[] = {
589 590 591
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '-', A_NEXT, TPS_InCloseCommentLast, 0, NULL},
	{NULL, 0, A_NEXT, TPS_InComment, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
592 593 594
};

static TParserStateActionItem actionTPS_InCloseCommentLast[] = {
595 596 597 598
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '-', A_NEXT, TPS_Null, 0, NULL},
	{p_iseqC, '>', A_NEXT, TPS_InCommentEnd, 0, NULL},
	{NULL, 0, A_NEXT, TPS_InComment, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
599 600 601
};

static TParserStateActionItem actionTPS_InCommentEnd[] = {
602
	{NULL, 0, A_BINGO | A_CLRALL, TPS_Base, TAG, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
603 604
};

Teodor Sigaev's avatar
Teodor Sigaev committed
605
static TParserStateActionItem actionTPS_InHostFirstDomain[] = {
606
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
607
	{p_islatin, 0, A_NEXT, TPS_InHostDomainSecond, 0, NULL},
608 609
	{p_isdigit, 0, A_NEXT, TPS_InHost, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
610 611
};

Teodor Sigaev's avatar
Teodor Sigaev committed
612
static TParserStateActionItem actionTPS_InHostDomainSecond[] = {
613
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
614
	{p_islatin, 0, A_NEXT, TPS_InHostDomain, 0, NULL},
615 616
	{p_isdigit, 0, A_PUSH, TPS_InHost, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHostFirstAN, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
617
	{p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
618 619
	{p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
620 621
};

Teodor Sigaev's avatar
Teodor Sigaev committed
622
static TParserStateActionItem actionTPS_InHostDomain[] = {
623
	{p_isEOF, 0, A_BINGO | A_CLRALL, TPS_Base, HOST, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
624
	{p_islatin, 0, A_NEXT, TPS_InHostDomain, 0, NULL},
625 626 627
	{p_isdigit, 0, A_PUSH, TPS_InHost, 0, NULL},
	{p_iseqC, ':', A_PUSH, TPS_InPortFirst, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHostFirstAN, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
628
	{p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
629 630 631 632 633
	{p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
	{p_isdigit, 0, A_POP, TPS_Null, 0, NULL},
	{p_isstophost, 0, A_BINGO | A_CLRALL, TPS_InURIStart, HOST, NULL},
	{p_iseqC, '/', A_PUSH, TPS_InFURL, 0, NULL},
	{NULL, 0, A_BINGO | A_CLRALL, TPS_Base, HOST, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
634 635 636
};

static TParserStateActionItem actionTPS_InPortFirst[] = {
637 638 639
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InPort, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
640 641 642
};

static TParserStateActionItem actionTPS_InPort[] = {
643 644 645 646 647
	{p_isEOF, 0, A_BINGO | A_CLRALL, TPS_Base, HOST, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InPort, 0, NULL},
	{p_isstophost, 0, A_BINGO | A_CLRALL, TPS_InURIStart, HOST, NULL},
	{p_iseqC, '/', A_PUSH, TPS_InFURL, 0, NULL},
	{NULL, 0, A_BINGO | A_CLRALL, TPS_Base, HOST, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
648 649 650
};

static TParserStateActionItem actionTPS_InHostFirstAN[] = {
651 652 653 654
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHost, 0, NULL},
	{p_islatin, 0, A_NEXT, TPS_InHost, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
655 656 657
};

static TParserStateActionItem actionTPS_InHost[] = {
658 659 660 661
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHost, 0, NULL},
	{p_islatin, 0, A_NEXT, TPS_InHost, 0, NULL},
	{p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
662
	{p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
663 664
	{p_iseqC, '-', A_PUSH, TPS_InHostFirstAN, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
665 666 667
};

static TParserStateActionItem actionTPS_InEmail[] = {
668 669
	{p_ishost, 0, A_BINGO | A_CLRALL, TPS_Base, EMAIL, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
670 671 672
};

static TParserStateActionItem actionTPS_InFileFirst[] = {
673
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
674 675 676 677
	{p_islatin, 0, A_NEXT, TPS_InFile, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InFile, 0, NULL},
	{p_iseqC, '.', A_NEXT, TPS_InPathFirst, 0, NULL},
	{p_iseqC, '_', A_NEXT, TPS_InFile, 0, NULL},
678 679
	{p_iseqC, '?', A_PUSH, TPS_InURIFirst, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
680 681
};

Teodor Sigaev's avatar
Teodor Sigaev committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
static TParserStateActionItem actionTPS_InPathFirst[] = {
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_islatin, 0, A_NEXT, TPS_InFile, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InFile, 0, NULL},
	{p_iseqC, '_', A_NEXT, TPS_InFile, 0, NULL},
	{p_iseqC, '.', A_NEXT, TPS_InPathSecond, 0, NULL},
	{p_iseqC, '/', A_NEXT, TPS_InFileFirst, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

static TParserStateActionItem actionTPS_InPathSecond[] = {
	{p_isEOF, 0, A_BINGO|A_CLEAR, TPS_Base, FILEPATH, NULL},
	{p_iseqC, '/', A_NEXT|A_PUSH, TPS_InFileFirst, 0, NULL},
	{p_iseqC, '/', A_BINGO|A_CLEAR, TPS_Base, FILEPATH, NULL},
	{p_isspace, 0, A_BINGO|A_CLEAR, TPS_Base, FILEPATH, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

Teodor Sigaev's avatar
Teodor Sigaev committed
700
static TParserStateActionItem actionTPS_InFile[] = {
701 702 703 704 705 706 707 708 709
	{p_isEOF, 0, A_BINGO, TPS_Base, FILEPATH, NULL},
	{p_islatin, 0, A_NEXT, TPS_InFile, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InFile, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InFileNext, 0, NULL},
	{p_iseqC, '_', A_NEXT, TPS_InFile, 0, NULL},
	{p_iseqC, '-', A_NEXT, TPS_InFile, 0, NULL},
	{p_iseqC, '/', A_PUSH, TPS_InFileFirst, 0, NULL},
	{p_iseqC, '?', A_PUSH, TPS_InURIFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, FILEPATH, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
710 711 712
};

static TParserStateActionItem actionTPS_InFileNext[] = {
713 714 715 716 717
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_islatin, 0, A_CLEAR, TPS_InFile, 0, NULL},
	{p_isdigit, 0, A_CLEAR, TPS_InFile, 0, NULL},
	{p_iseqC, '_', A_CLEAR, TPS_InFile, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
718 719 720
};

static TParserStateActionItem actionTPS_InURIFirst[] = {
721 722 723 724 725
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '"', A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '\'', A_POP, TPS_Null, 0, NULL},
	{p_isnotspace, 0, A_CLEAR, TPS_InURI, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL},
Teodor Sigaev's avatar
Teodor Sigaev committed
726
};
727

Teodor Sigaev's avatar
Teodor Sigaev committed
728
static TParserStateActionItem actionTPS_InURIStart[] = {
729
	{NULL, 0, A_NEXT, TPS_InURI, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
730 731 732
};

static TParserStateActionItem actionTPS_InURI[] = {
733 734 735 736 737
	{p_isEOF, 0, A_BINGO, TPS_Base, URI, NULL},
	{p_iseqC, '"', A_BINGO, TPS_Base, URI, NULL},
	{p_iseqC, '\'', A_BINGO, TPS_Base, URI, NULL},
	{p_isnotspace, 0, A_NEXT, TPS_InURI, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, URI, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
738 739 740
};

static TParserStateActionItem actionTPS_InFURL[] = {
741 742 743
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isURI, 0, A_BINGO | A_CLRALL, TPS_Base, FURL, SpecialFURL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
744
};
745

Teodor Sigaev's avatar
Teodor Sigaev committed
746
static TParserStateActionItem actionTPS_InProtocolFirst[] = {
747 748 749
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '/', A_NEXT, TPS_InProtocolSecond, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
750
};
751

Teodor Sigaev's avatar
Teodor Sigaev committed
752
static TParserStateActionItem actionTPS_InProtocolSecond[] = {
753 754 755
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '/', A_NEXT, TPS_InProtocolEnd, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
756
};
757

Teodor Sigaev's avatar
Teodor Sigaev committed
758
static TParserStateActionItem actionTPS_InProtocolEnd[] = {
759
	{NULL, 0, A_BINGO | A_CLRALL, TPS_Base, PROTOCOL, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
760
};
761

Teodor Sigaev's avatar
Teodor Sigaev committed
762
static TParserStateActionItem actionTPS_InHyphenLatWordFirst[] = {
763 764 765 766 767 768
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_islatin, 0, A_NEXT, TPS_InHyphenLatWord, 0, NULL},
	{p_isnonlatin, 0, A_NEXT, TPS_InHyphenUWord, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenValue, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenUWord, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
769 770 771
};

static TParserStateActionItem actionTPS_InHyphenLatWord[] = {
772 773 774 775 776 777
	{p_isEOF, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, LATHYPHENWORD, SpecialHyphen},
	{p_islatin, 0, A_NEXT, TPS_InHyphenLatWord, 0, NULL},
	{p_isnonlatin, 0, A_NEXT, TPS_InHyphenUWord, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenUWord, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHyphenLatWordFirst, 0, NULL},
	{NULL, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, LATHYPHENWORD, SpecialHyphen}
Teodor Sigaev's avatar
Teodor Sigaev committed
778 779 780
};

static TParserStateActionItem actionTPS_InHyphenCyrWordFirst[] = {
781 782 783 784 785 786
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isnonlatin, 0, A_NEXT, TPS_InHyphenCyrWord, 0, NULL},
	{p_islatin, 0, A_NEXT, TPS_InHyphenUWord, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenValue, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenUWord, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
787 788 789
};

static TParserStateActionItem actionTPS_InHyphenCyrWord[] = {
790 791 792 793 794 795
	{p_isEOF, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, CYRHYPHENWORD, SpecialHyphen},
	{p_isnonlatin, 0, A_NEXT, TPS_InHyphenCyrWord, 0, NULL},
	{p_islatin, 0, A_NEXT, TPS_InHyphenUWord, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenUWord, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHyphenCyrWordFirst, 0, NULL},
	{NULL, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, CYRHYPHENWORD, SpecialHyphen}
Teodor Sigaev's avatar
Teodor Sigaev committed
796 797 798
};

static TParserStateActionItem actionTPS_InHyphenUWordFirst[] = {
799 800 801 802
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenValue, 0, NULL},
	{p_isalnum, 0, A_NEXT, TPS_InHyphenUWord, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
803 804 805
};

static TParserStateActionItem actionTPS_InHyphenUWord[] = {
806 807 808 809
	{p_isEOF, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, HYPHENWORD, SpecialHyphen},
	{p_isalnum, 0, A_NEXT, TPS_InHyphenUWord, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHyphenUWordFirst, 0, NULL},
	{NULL, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, HYPHENWORD, SpecialHyphen}
Teodor Sigaev's avatar
Teodor Sigaev committed
810 811 812
};

static TParserStateActionItem actionTPS_InHyphenValueFirst[] = {
813 814 815
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenValueExact, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
816 817 818
};

static TParserStateActionItem actionTPS_InHyphenValue[] = {
819 820 821 822 823 824
	{p_isEOF, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, HYPHENWORD, SpecialHyphen},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenValue, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InHyphenValueFirst, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHyphenUWordFirst, 0, NULL},
	{p_isalpha, 0, A_NEXT, TPS_InHyphenUWord, 0, NULL},
	{NULL, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, HYPHENWORD, SpecialHyphen}
Teodor Sigaev's avatar
Teodor Sigaev committed
825 826 827
};

static TParserStateActionItem actionTPS_InHyphenValueExact[] = {
828 829 830 831 832
	{p_isEOF, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, HYPHENWORD, SpecialHyphen},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenValueExact, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InHyphenValueFirst, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHyphenUWordFirst, 0, NULL},
	{NULL, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, HYPHENWORD, SpecialHyphen}
Teodor Sigaev's avatar
Teodor Sigaev committed
833 834 835
};

static TParserStateActionItem actionTPS_InParseHyphen[] = {
836 837 838 839 840 841
	{p_isEOF, 0, A_RERUN, TPS_Base, 0, NULL},
	{p_islatin, 0, A_NEXT, TPS_InHyphenLatWordPart, 0, NULL},
	{p_isnonlatin, 0, A_NEXT, TPS_InHyphenCyrWordPart, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenUnsignedInt, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InParseHyphenHyphen, 0, NULL},
	{NULL, 0, A_RERUN, TPS_Base, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
842 843 844
};

static TParserStateActionItem actionTPS_InParseHyphenHyphen[] = {
845 846 847
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isalnum, 0, A_BINGO | A_CLEAR, TPS_InParseHyphen, SPACE, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
848 849 850
};

static TParserStateActionItem actionTPS_InHyphenCyrWordPart[] = {
851 852 853 854 855
	{p_isEOF, 0, A_BINGO, TPS_Base, CYRPARTHYPHENWORD, NULL},
	{p_isnonlatin, 0, A_NEXT, TPS_InHyphenCyrWordPart, 0, NULL},
	{p_islatin, 0, A_NEXT, TPS_InHyphenUWordPart, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenUWordPart, 0, NULL},
	{NULL, 0, A_BINGO, TPS_InParseHyphen, CYRPARTHYPHENWORD, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
856 857 858
};

static TParserStateActionItem actionTPS_InHyphenLatWordPart[] = {
859 860 861 862 863
	{p_isEOF, 0, A_BINGO, TPS_Base, LATPARTHYPHENWORD, NULL},
	{p_islatin, 0, A_NEXT, TPS_InHyphenLatWordPart, 0, NULL},
	{p_isnonlatin, 0, A_NEXT, TPS_InHyphenUWordPart, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenUWordPart, 0, NULL},
	{NULL, 0, A_BINGO, TPS_InParseHyphen, LATPARTHYPHENWORD, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
864 865 866
};

static TParserStateActionItem actionTPS_InHyphenUWordPart[] = {
867 868 869
	{p_isEOF, 0, A_BINGO, TPS_Base, PARTHYPHENWORD, NULL},
	{p_isalnum, 0, A_NEXT, TPS_InHyphenUWordPart, 0, NULL},
	{NULL, 0, A_BINGO, TPS_InParseHyphen, PARTHYPHENWORD, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
870 871 872
};

static TParserStateActionItem actionTPS_InHyphenUnsignedInt[] = {
873 874 875 876 877
	{p_isEOF, 0, A_BINGO, TPS_Base, UNSIGNEDINT, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenUnsignedInt, 0, NULL},
	{p_isalpha, 0, A_NEXT, TPS_InHyphenUWordPart, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InHDecimalPartFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_InParseHyphen, UNSIGNEDINT, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
878 879 880
};

static TParserStateActionItem actionTPS_InHDecimalPartFirst[] = {
881 882 883
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_CLEAR, TPS_InHDecimalPart, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
884 885 886
};

static TParserStateActionItem actionTPS_InHDecimalPart[] = {
887 888 889 890
	{p_isEOF, 0, A_BINGO, TPS_Base, DECIMAL, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHDecimalPart, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InHVersionPartFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_InParseHyphen, DECIMAL, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
891 892 893
};

static TParserStateActionItem actionTPS_InHVersionPartFirst[] = {
894 895 896
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_CLEAR, TPS_InHVersionPart, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
897 898 899
};

static TParserStateActionItem actionTPS_InHVersionPart[] = {
900 901 902 903
	{p_isEOF, 0, A_BINGO, TPS_Base, VERSIONNUMBER, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHVersionPart, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InHVersionPartFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_InParseHyphen, VERSIONNUMBER, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
904 905
};

906
/*
Teodor Sigaev's avatar
Teodor Sigaev committed
907 908 909 910
 * order should be the same as in typedef enum {} TParserState!!
 */

static const TParserStateAction Actions[] = {
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
	{TPS_Base, actionTPS_Base},
	{TPS_InUWord, actionTPS_InUWord},
	{TPS_InLatWord, actionTPS_InLatWord},
	{TPS_InCyrWord, actionTPS_InCyrWord},
	{TPS_InUnsignedInt, actionTPS_InUnsignedInt},
	{TPS_InSignedIntFirst, actionTPS_InSignedIntFirst},
	{TPS_InSignedInt, actionTPS_InSignedInt},
	{TPS_InSpace, actionTPS_InSpace},
	{TPS_InUDecimalFirst, actionTPS_InUDecimalFirst},
	{TPS_InUDecimal, actionTPS_InUDecimal},
	{TPS_InDecimalFirst, actionTPS_InDecimalFirst},
	{TPS_InDecimal, actionTPS_InDecimal},
	{TPS_InVersionFirst, actionTPS_InVersionFirst},
	{TPS_InVersion, actionTPS_InVersion},
	{TPS_InMantissaFirst, actionTPS_InMantissaFirst},
	{TPS_InMantissaSign, actionTPS_InMantissaSign},
	{TPS_InMantissa, actionTPS_InMantissa},
	{TPS_InHTMLEntityFirst, actionTPS_InHTMLEntityFirst},
	{TPS_InHTMLEntity, actionTPS_InHTMLEntity},
	{TPS_InHTMLEntityNumFirst, actionTPS_InHTMLEntityNumFirst},
	{TPS_InHTMLEntityNum, actionTPS_InHTMLEntityNum},
	{TPS_InHTMLEntityEnd, actionTPS_InHTMLEntityEnd},
	{TPS_InTagFirst, actionTPS_InTagFirst},
Teodor Sigaev's avatar
Teodor Sigaev committed
934
	{TPS_InXMLBegin, actionTPS_InXMLBegin},
935 936 937 938 939 940 941 942 943 944 945 946
	{TPS_InTagCloseFirst, actionTPS_InTagCloseFirst},
	{TPS_InTag, actionTPS_InTag},
	{TPS_InTagEscapeK, actionTPS_InTagEscapeK},
	{TPS_InTagEscapeKK, actionTPS_InTagEscapeKK},
	{TPS_InTagBackSleshed, actionTPS_InTagBackSleshed},
	{TPS_InTagEnd, actionTPS_InTagEnd},
	{TPS_InCommentFirst, actionTPS_InCommentFirst},
	{TPS_InCommentLast, actionTPS_InCommentLast},
	{TPS_InComment, actionTPS_InComment},
	{TPS_InCloseCommentFirst, actionTPS_InCloseCommentFirst},
	{TPS_InCloseCommentLast, actionTPS_InCloseCommentLast},
	{TPS_InCommentEnd, actionTPS_InCommentEnd},
Teodor Sigaev's avatar
Teodor Sigaev committed
947 948 949
	{TPS_InHostFirstDomain, actionTPS_InHostFirstDomain},
	{TPS_InHostDomainSecond, actionTPS_InHostDomainSecond},
	{TPS_InHostDomain, actionTPS_InHostDomain},
950 951 952 953 954 955
	{TPS_InPortFirst, actionTPS_InPortFirst},
	{TPS_InPort, actionTPS_InPort},
	{TPS_InHostFirstAN, actionTPS_InHostFirstAN},
	{TPS_InHost, actionTPS_InHost},
	{TPS_InEmail, actionTPS_InEmail},
	{TPS_InFileFirst, actionTPS_InFileFirst},
Teodor Sigaev's avatar
Teodor Sigaev committed
956 957
	{TPS_InPathFirst, actionTPS_InPathFirst},
	{TPS_InPathSecond, actionTPS_InPathSecond},
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
	{TPS_InFile, actionTPS_InFile},
	{TPS_InFileNext, actionTPS_InFileNext},
	{TPS_InURIFirst, actionTPS_InURIFirst},
	{TPS_InURIStart, actionTPS_InURIStart},
	{TPS_InURI, actionTPS_InURI},
	{TPS_InFURL, actionTPS_InFURL},
	{TPS_InProtocolFirst, actionTPS_InProtocolFirst},
	{TPS_InProtocolSecond, actionTPS_InProtocolSecond},
	{TPS_InProtocolEnd, actionTPS_InProtocolEnd},
	{TPS_InHyphenLatWordFirst, actionTPS_InHyphenLatWordFirst},
	{TPS_InHyphenLatWord, actionTPS_InHyphenLatWord},
	{TPS_InHyphenCyrWordFirst, actionTPS_InHyphenCyrWordFirst},
	{TPS_InHyphenCyrWord, actionTPS_InHyphenCyrWord},
	{TPS_InHyphenUWordFirst, actionTPS_InHyphenUWordFirst},
	{TPS_InHyphenUWord, actionTPS_InHyphenUWord},
	{TPS_InHyphenValueFirst, actionTPS_InHyphenValueFirst},
	{TPS_InHyphenValue, actionTPS_InHyphenValue},
	{TPS_InHyphenValueExact, actionTPS_InHyphenValueExact},
	{TPS_InParseHyphen, actionTPS_InParseHyphen},
	{TPS_InParseHyphenHyphen, actionTPS_InParseHyphenHyphen},
	{TPS_InHyphenCyrWordPart, actionTPS_InHyphenCyrWordPart},
	{TPS_InHyphenLatWordPart, actionTPS_InHyphenLatWordPart},
	{TPS_InHyphenUWordPart, actionTPS_InHyphenUWordPart},
	{TPS_InHyphenUnsignedInt, actionTPS_InHyphenUnsignedInt},
	{TPS_InHDecimalPartFirst, actionTPS_InHDecimalPartFirst},
	{TPS_InHDecimalPart, actionTPS_InHDecimalPart},
	{TPS_InHVersionPartFirst, actionTPS_InHVersionPartFirst},
	{TPS_InHVersionPart, actionTPS_InHVersionPart},
	{TPS_Null, NULL}
Teodor Sigaev's avatar
Teodor Sigaev committed
987 988 989 990
};


bool
991 992 993
TParserGet(TParser * prs)
{
	TParserStateActionItem *item = NULL;
Teodor Sigaev's avatar
Teodor Sigaev committed
994

995
	if (prs->state->posbyte >= prs->lenstr)
Teodor Sigaev's avatar
Teodor Sigaev committed
996 997
		return false;

998 999
	Assert(prs->state);
	prs->lexeme = prs->str + prs->state->posbyte;
Teodor Sigaev's avatar
Teodor Sigaev committed
1000 1001 1002
	prs->state->pushedAtAction = NULL;

	/* look at string */
1003 1004 1005
	while (prs->state->posbyte <= prs->lenstr)
	{
		if (prs->state->posbyte == prs->lenstr)
Teodor Sigaev's avatar
Teodor Sigaev committed
1006 1007
			prs->state->charlen = 0;
		else
1008 1009
			prs->state->charlen = (prs->charmaxlen == 1) ? prs->charmaxlen :
				pg_mblen(prs->str + prs->state->posbyte);
Teodor Sigaev's avatar
Teodor Sigaev committed
1010

1011 1012 1013
		Assert(prs->state->posbyte + prs->state->charlen <= prs->lenstr);
		Assert(prs->state->state >= TPS_Base && prs->state->state < TPS_Null);
		Assert(Actions[prs->state->state].state == prs->state->state);
Teodor Sigaev's avatar
Teodor Sigaev committed
1014

1015 1016
		item = Actions[prs->state->state].action;
		Assert(item != NULL);
Teodor Sigaev's avatar
Teodor Sigaev committed
1017

1018 1019
		if (item < prs->state->pushedAtAction)
			item = prs->state->pushedAtAction;
Teodor Sigaev's avatar
Teodor Sigaev committed
1020 1021

		/* find action by character class */
1022 1023
		while (item->isclass)
		{
Teodor Sigaev's avatar
Teodor Sigaev committed
1024
			prs->c = item->c;
1025 1026 1027 1028 1029
			if (item->isclass(prs) != 0)
			{
				if (item > prs->state->pushedAtAction)	/* remember: after
														 * pushing we were by
														 * false way */
Teodor Sigaev's avatar
Teodor Sigaev committed
1030
					break;
1031
			}
Teodor Sigaev's avatar
Teodor Sigaev committed
1032 1033 1034 1035 1036 1037
			item++;
		}

		prs->state->pushedAtAction = NULL;

		/* call special handler if exists */
1038
		if (item->special)
Teodor Sigaev's avatar
Teodor Sigaev committed
1039 1040 1041
			item->special(prs);

		/* BINGO, lexeme is found */
1042 1043 1044
		if (item->flags & A_BINGO)
		{
			Assert(item->type > 0);
Teodor Sigaev's avatar
Teodor Sigaev committed
1045 1046 1047 1048
			prs->lenbytelexeme = prs->state->lenbytelexeme;
			prs->lencharlexeme = prs->state->lencharlexeme;
			prs->state->lenbytelexeme = prs->state->lencharlexeme = 0;
			prs->type = item->type;
1049
		}
Teodor Sigaev's avatar
Teodor Sigaev committed
1050

1051 1052 1053
		/* do various actions by flags */
		if (item->flags & A_POP)
		{						/* pop stored state in stack */
Teodor Sigaev's avatar
Teodor Sigaev committed
1054
			TParserPosition *ptr = prs->state->prev;
1055 1056

			pfree(prs->state);
Teodor Sigaev's avatar
Teodor Sigaev committed
1057
			prs->state = ptr;
1058 1059 1060 1061 1062 1063 1064 1065 1066
			Assert(prs->state);
		}
		else if (item->flags & A_PUSH)
		{						/* push (store) state in stack */
			prs->state->pushedAtAction = item;	/* remember where we push */
			prs->state = newTParserPosition(prs->state);
		}
		else if (item->flags & A_CLEAR)
		{						/* clear previous pushed state */
Teodor Sigaev's avatar
Teodor Sigaev committed
1067
			TParserPosition *ptr;
1068 1069

			Assert(prs->state->prev);
Teodor Sigaev's avatar
Teodor Sigaev committed
1070
			ptr = prs->state->prev->prev;
1071
			pfree(prs->state->prev);
Teodor Sigaev's avatar
Teodor Sigaev committed
1072
			prs->state->prev = ptr;
1073 1074 1075
		}
		else if (item->flags & A_CLRALL)
		{						/* clear all previous pushed state */
Teodor Sigaev's avatar
Teodor Sigaev committed
1076
			TParserPosition *ptr;
1077 1078 1079

			while (prs->state->prev)
			{
Teodor Sigaev's avatar
Teodor Sigaev committed
1080
				ptr = prs->state->prev->prev;
1081
				pfree(prs->state->prev);
Teodor Sigaev's avatar
Teodor Sigaev committed
1082 1083
				prs->state->prev = ptr;
			}
1084 1085 1086
		}
		else if (item->flags & A_MERGE)
		{						/* merge posinfo with current and pushed state */
Teodor Sigaev's avatar
Teodor Sigaev committed
1087
			TParserPosition *ptr = prs->state;
1088 1089

			Assert(prs->state->prev);
Teodor Sigaev's avatar
Teodor Sigaev committed
1090 1091 1092 1093 1094 1095 1096
			prs->state = prs->state->prev;

			prs->state->posbyte = ptr->posbyte;
			prs->state->poschar = ptr->poschar;
			prs->state->charlen = ptr->charlen;
			prs->state->lenbytelexeme = ptr->lenbytelexeme;
			prs->state->lencharlexeme = ptr->lencharlexeme;
1097
			pfree(ptr);
Teodor Sigaev's avatar
Teodor Sigaev committed
1098 1099 1100
		}

		/* set new state if pointed */
1101
		if (item->tostate != TPS_Null)
Teodor Sigaev's avatar
Teodor Sigaev committed
1102 1103
			prs->state->state = item->tostate;

1104 1105
		/* check for go away */
		if ((item->flags & A_BINGO) || (prs->state->posbyte >= prs->lenstr && (item->flags & A_RERUN) == 0))
Teodor Sigaev's avatar
Teodor Sigaev committed
1106 1107 1108
			break;

		/* go to begining of loop if we should rerun or we just restore state */
1109
		if (item->flags & (A_RERUN | A_POP))
Teodor Sigaev's avatar
Teodor Sigaev committed
1110
			continue;
1111 1112 1113 1114

		/* move forward */
		if (prs->state->charlen)
		{
Teodor Sigaev's avatar
Teodor Sigaev committed
1115 1116
			prs->state->posbyte += prs->state->charlen;
			prs->state->lenbytelexeme += prs->state->charlen;
1117 1118
			prs->state->poschar++;
			prs->state->lencharlexeme++;
Teodor Sigaev's avatar
Teodor Sigaev committed
1119
		}
1120
	}
Teodor Sigaev's avatar
Teodor Sigaev committed
1121 1122 1123

	return (item && (item->flags & A_BINGO)) ? true : false;
}