wparser_def.c 59.8 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * wparser_def.c
4
 *		Default text search parser
5
 *
Bruce Momjian's avatar
Bruce Momjian committed
6
 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 8 9
 *
 *
 * IDENTIFICATION
10
 *	  $PostgreSQL: pgsql/src/backend/tsearch/wparser_def.c,v 1.22 2009/03/10 17:32:14 teodor Exp $
11 12 13 14 15 16
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

17
#include "commands/defrem.h"
18 19 20 21 22 23 24
#include "tsearch/ts_locale.h"
#include "tsearch/ts_public.h"
#include "tsearch/ts_type.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"


25 26 27 28
/* Define me to enable tracing of parser behavior */
/* #define WPARSER_TRACE */


29
/* Output token categories */
30

31 32 33 34 35 36 37
#define ASCIIWORD		1
#define WORD_T			2
#define NUMWORD			3
#define EMAIL			4
#define URL_T			5
#define HOST			6
#define SCIENTIFIC		7
38
#define VERSIONNUMBER	8
39 40 41 42 43
#define NUMPARTHWORD	9
#define PARTHWORD		10
#define ASCIIPARTHWORD	11
#define SPACE			12
#define TAG_T			13
44
#define PROTOCOL		14
45 46 47
#define NUMHWORD		15
#define ASCIIHWORD		16
#define HWORD			17
48
#define URLPATH			18
49 50 51
#define FILEPATH		19
#define DECIMAL			20
#define SIGNEDINT		21
Bruce Momjian's avatar
Bruce Momjian committed
52
#define UNSIGNEDINT		22
53
#define XMLENTITY		23
54 55 56

#define LASTNUM			23

Bruce Momjian's avatar
Bruce Momjian committed
57
static const char *const tok_alias[] = {
58
	"",
59
	"asciiword",
60
	"word",
61
	"numword",
62 63 64 65 66
	"email",
	"url",
	"host",
	"sfloat",
	"version",
67 68 69
	"hword_numpart",
	"hword_part",
	"hword_asciipart",
70 71 72
	"blank",
	"tag",
	"protocol",
73 74
	"numhword",
	"asciihword",
75
	"hword",
76
	"url_path",
77 78 79 80 81 82 83
	"file",
	"float",
	"int",
	"uint",
	"entity"
};

Bruce Momjian's avatar
Bruce Momjian committed
84
static const char *const lex_descr[] = {
85 86 87 88 89 90 91 92 93 94 95 96 97
	"",
	"Word, all ASCII",
	"Word, all letters",
	"Word, letters and digits",
	"Email address",
	"URL",
	"Host",
	"Scientific notation",
	"Version number",
	"Hyphenated word part, letters and digits",
	"Hyphenated word part, all letters",
	"Hyphenated word part, all ASCII",
	"Space symbols",
98
	"XML tag",
99 100 101 102
	"Protocol head",
	"Hyphenated word, letters and digits",
	"Hyphenated word, all ASCII",
	"Hyphenated word, all letters",
103
	"URL path",
104 105 106 107
	"File or path name",
	"Decimal notation",
	"Signed integer",
	"Unsigned integer",
108
	"XML entity"
109 110 111 112 113
};


/* Parser states */

114 115 116
typedef enum
{
	TPS_Base = 0,
117 118 119
	TPS_InNumWord,
	TPS_InAsciiWord,
	TPS_InWord,
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	TPS_InUnsignedInt,
	TPS_InSignedIntFirst,
	TPS_InSignedInt,
	TPS_InSpace,
	TPS_InUDecimalFirst,
	TPS_InUDecimal,
	TPS_InDecimalFirst,
	TPS_InDecimal,
	TPS_InVerVersion,
	TPS_InSVerVersion,
	TPS_InVersionFirst,
	TPS_InVersion,
	TPS_InMantissaFirst,
	TPS_InMantissaSign,
	TPS_InMantissa,
135 136 137 138 139 140 141
	TPS_InXMLEntityFirst,
	TPS_InXMLEntity,
	TPS_InXMLEntityNumFirst,
	TPS_InXMLEntityNum,
	TPS_InXMLEntityHexNumFirst,
	TPS_InXMLEntityHexNum,
	TPS_InXMLEntityEnd,
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
	TPS_InTagFirst,
	TPS_InXMLBegin,
	TPS_InTagCloseFirst,
	TPS_InTagName,
	TPS_InTagBeginEnd,
	TPS_InTag,
	TPS_InTagEscapeK,
	TPS_InTagEscapeKK,
	TPS_InTagBackSleshed,
	TPS_InTagEnd,
	TPS_InCommentFirst,
	TPS_InCommentLast,
	TPS_InComment,
	TPS_InCloseCommentFirst,
	TPS_InCloseCommentLast,
	TPS_InCommentEnd,
	TPS_InHostFirstDomain,
	TPS_InHostDomainSecond,
	TPS_InHostDomain,
	TPS_InPortFirst,
	TPS_InPort,
	TPS_InHostFirstAN,
	TPS_InHost,
	TPS_InEmail,
	TPS_InFileFirst,
	TPS_InFileTwiddle,
	TPS_InPathFirst,
	TPS_InPathFirstFirst,
	TPS_InPathSecond,
	TPS_InFile,
	TPS_InFileNext,
173 174 175
	TPS_InURLPathFirst,
	TPS_InURLPathStart,
	TPS_InURLPath,
176 177 178 179
	TPS_InFURL,
	TPS_InProtocolFirst,
	TPS_InProtocolSecond,
	TPS_InProtocolEnd,
180 181 182 183 184 185
	TPS_InHyphenAsciiWordFirst,
	TPS_InHyphenAsciiWord,
	TPS_InHyphenWordFirst,
	TPS_InHyphenWord,
	TPS_InHyphenNumWordFirst,
	TPS_InHyphenNumWord,
186
	TPS_InHyphenDigitLookahead,
187 188
	TPS_InParseHyphen,
	TPS_InParseHyphenHyphen,
189 190 191
	TPS_InHyphenWordPart,
	TPS_InHyphenAsciiWordPart,
	TPS_InHyphenNumWordPart,
192 193
	TPS_InHyphenUnsignedInt,
	TPS_Null					/* last state (fake value) */
194
} TParserState;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

/* forward declaration */
struct TParser;

typedef int (*TParserCharTest) (struct TParser *);		/* any p_is* functions
														 * except p_iseq */
typedef void (*TParserSpecial) (struct TParser *);		/* special handler for
														 * special cases... */

typedef struct
{
	TParserCharTest isclass;
	char		c;
	uint16		flags;
	TParserState tostate;
	int			type;
	TParserSpecial special;
212
} TParserStateActionItem;
213

214 215 216 217 218 219 220 221 222 223
/* Flag bits in TParserStateActionItem.flags */
#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

224 225 226
typedef struct TParserPosition
{
	int			posbyte;		/* position of parser in bytes */
227
	int			poschar;		/* position of parser in characters */
228
	int			charlen;		/* length of current char */
229 230
	int			lenbytetoken;	/* length of token-so-far in bytes */
	int			lenchartoken;	/* and in chars */
231 232
	TParserState state;
	struct TParserPosition *prev;
233
	const TParserStateActionItem *pushedAtAction;
234
} TParserPosition;
235 236 237 238 239 240

typedef struct TParser
{
	/* string and position information */
	char	   *str;			/* multibyte string */
	int			lenstr;			/* length of mbstring */
241
#ifdef USE_WIDE_UPPER_LOWER
242
	wchar_t    *wstr;			/* wide character string */
243 244
	pg_wchar   *pgwstr;			/* wide character string for C-locale */
	bool		usewide;
245 246 247 248 249 250 251 252 253 254 255 256
#endif

	/* State of parse */
	int			charmaxlen;
	TParserPosition *state;
	bool		ignore;
	bool		wanthost;

	/* silly char */
	char		c;

	/* out */
257 258 259
	char	   *token;
	int			lenbytetoken;
	int			lenchartoken;
260
	int			type;
261
} TParser;
262

263 264

/* forward decls here */
265
static bool TParserGet(TParser *prs);
266 267


268
static TParserPosition *
269
newTParserPosition(TParserPosition *prev)
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
{
	TParserPosition *res = (TParserPosition *) palloc(sizeof(TParserPosition));

	if (prev)
		memcpy(res, prev, sizeof(TParserPosition));
	else
		memset(res, 0, sizeof(TParserPosition));

	res->prev = prev;

	res->pushedAtAction = NULL;

	return res;
}

static TParser *
TParserInit(char *str, int len)
{
	TParser    *prs = (TParser *) palloc0(sizeof(TParser));

	prs->charmaxlen = pg_database_encoding_max_length();
	prs->str = str;
	prs->lenstr = len;

294
#ifdef USE_WIDE_UPPER_LOWER
295 296 297 298 299 300 301

	/*
	 * Use wide char code only when max encoding length > 1.
	 */
	if (prs->charmaxlen > 1)
	{
		prs->usewide = true;
302 303 304 305 306 307 308 309 310 311 312 313 314 315
		if ( lc_ctype_is_c() )
		{
			/*
			 * char2wchar doesn't work for C-locale and
			 * sizeof(pg_wchar) could be not equal to sizeof(wchar_t)
			 */
			prs->pgwstr = (pg_wchar*) palloc(sizeof(pg_wchar) * (prs->lenstr + 1));
			pg_mb2wchar_with_len(prs->str, prs->pgwstr, prs->lenstr);
		}
		else
		{
			prs->wstr = (wchar_t *) palloc(sizeof(wchar_t) * (prs->lenstr + 1));
			char2wchar(prs->wstr, prs->lenstr + 1, prs->str, prs->lenstr);
		}
316 317 318
	}
	else
		prs->usewide = false;
319
#endif
320 321 322 323

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

324 325 326 327
#ifdef WPARSER_TRACE
	fprintf(stderr, "parsing \"%.*s\"\n", len, str);
#endif

328 329 330 331
	return prs;
}

static void
332
TParserClose(TParser *prs)
333 334 335 336 337 338 339 340 341
{
	while (prs->state)
	{
		TParserPosition *ptr = prs->state->prev;

		pfree(prs->state);
		prs->state = ptr;
	}

342
#ifdef USE_WIDE_UPPER_LOWER
343 344
	if (prs->wstr)
		pfree(prs->wstr);
345 346
	if (prs->pgwstr)
		pfree(prs->pgwstr);
347 348 349 350 351 352
#endif

	pfree(prs);
}

/*
353
 * Character-type support functions, equivalent to is* macros, but
354 355 356 357 358 359
 * working with any possible encodings and locales. Notes:
 *  - with multibyte encoding and C-locale isw* function may fail
 *    or give wrong result. 
 *  - multibyte encoding and C-locale often are used for 
 *    Asian languages.
 *  - if locale is C the we use pgwstr instead of wstr
360 361
 */

362
#ifdef USE_WIDE_UPPER_LOWER
363 364 365 366 367 368 369

#define p_iswhat(type)														\
static int																	\
p_is##type(TParser *prs) {													\
	Assert( prs->state );													\
	if ( prs->usewide )														\
	{																		\
370 371
		if ( prs->pgwstr )													\
			return is##type( 0xff & *( prs->pgwstr + prs->state->poschar) );\
372 373 374 375 376
																			\
		return isw##type( *(wint_t*)( prs->wstr + prs->state->poschar ) );	\
	}																		\
																			\
	return is##type( *(unsigned char*)( prs->str + prs->state->posbyte ) ); \
377
}																			\
378 379 380 381 382 383 384
																			\
static int																	\
p_isnot##type(TParser *prs) {												\
	return !p_is##type(prs);												\
}

static int
385
p_isalnum(TParser *prs)
386 387 388 389 390
{
	Assert(prs->state);

	if (prs->usewide)
	{
391
		if (prs->pgwstr)
392
		{
393
			unsigned int c = *(prs->pgwstr + prs->state->poschar);
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

			/*
			 * any non-ascii symbol with multibyte encoding with C-locale is
			 * an alpha character
			 */
			if (c > 0x7f)
				return 1;

			return isalnum(0xff & c);
		}

		return iswalnum((wint_t) *(prs->wstr + prs->state->poschar));
	}

	return isalnum(*(unsigned char *) (prs->str + prs->state->posbyte));
}
static int
411
p_isnotalnum(TParser *prs)
412 413 414 415 416
{
	return !p_isalnum(prs);
}

static int
417
p_isalpha(TParser *prs)
418 419 420 421 422
{
	Assert(prs->state);

	if (prs->usewide)
	{
423
		if (prs->pgwstr)
424
		{
425
			unsigned int c = *(prs->pgwstr + prs->state->poschar);
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

			/*
			 * any non-ascii symbol with multibyte encoding with C-locale is
			 * an alpha character
			 */
			if (c > 0x7f)
				return 1;

			return isalpha(0xff & c);
		}

		return iswalpha((wint_t) *(prs->wstr + prs->state->poschar));
	}

	return isalpha(*(unsigned char *) (prs->str + prs->state->posbyte));
}

static int
444
p_isnotalpha(TParser *prs)
445 446 447 448 449 450 451
{
	return !p_isalpha(prs);
}

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

static int
452
p_iseq(TParser *prs, char c)
453 454 455 456
{
	Assert(prs->state);
	return ((prs->state->charlen == 1 && *(prs->str + prs->state->posbyte) == c)) ? 1 : 0;
}
457
#else							/* USE_WIDE_UPPER_LOWER */
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

#define p_iswhat(type)														\
static int																	\
p_is##type(TParser *prs) {													\
	Assert( prs->state );													\
	return is##type( (unsigned char)*( prs->str + prs->state->posbyte ) );	\
}	\
																			\
static int																	\
p_isnot##type(TParser *prs) {												\
	return !p_is##type(prs);												\
}


static int
473
p_iseq(TParser *prs, char c)
474 475 476 477 478 479 480
{
	Assert(prs->state);
	return (*(prs->str + prs->state->posbyte) == c) ? 1 : 0;
}

p_iswhat(alnum)
p_iswhat(alpha)
481
#endif   /* USE_WIDE_UPPER_LOWER */
482 483 484 485 486 487 488 489 490 491

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

static int
492
p_isEOF(TParser *prs)
493 494 495 496 497 498
{
	Assert(prs->state);
	return (prs->state->posbyte == prs->lenstr || prs->state->charlen == 0) ? 1 : 0;
}

static int
499
p_iseqC(TParser *prs)
500 501 502 503 504
{
	return p_iseq(prs, prs->c);
}

static int
505
p_isneC(TParser *prs)
506 507 508 509 510
{
	return !p_iseq(prs, prs->c);
}

static int
511
p_isascii(TParser *prs)
512 513 514 515 516
{
	return (prs->state->charlen == 1 && isascii((unsigned char) *(prs->str + prs->state->posbyte))) ? 1 : 0;
}

static int
517
p_isasclet(TParser *prs)
518
{
519
	return (p_isascii(prs) && p_isalpha(prs)) ? 1 : 0;
520 521 522
}


523
/* deliberately suppress unused-function complaints for the above */
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
void		_make_compiler_happy(void);
void
_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);
}


static void
553
SpecialTags(TParser *prs)
554
{
555
	switch (prs->state->lenchartoken)
556 557
	{
		case 8:			/* </script */
558
			if (pg_strncasecmp(prs->token, "</script", 8) == 0)
559 560 561
				prs->ignore = false;
			break;
		case 7:			/* <script || </style */
562
			if (pg_strncasecmp(prs->token, "</style", 7) == 0)
563
				prs->ignore = false;
564
			else if (pg_strncasecmp(prs->token, "<script", 7) == 0)
565 566 567
				prs->ignore = true;
			break;
		case 6:			/* <style */
568
			if (pg_strncasecmp(prs->token, "<style", 6) == 0)
569 570 571 572 573 574 575 576
				prs->ignore = true;
			break;
		default:
			break;
	}
}

static void
577
SpecialFURL(TParser *prs)
578 579
{
	prs->wanthost = true;
580 581
	prs->state->posbyte -= prs->state->lenbytetoken;
	prs->state->poschar -= prs->state->lenchartoken;
582 583 584
}

static void
585
SpecialHyphen(TParser *prs)
586
{
587 588
	prs->state->posbyte -= prs->state->lenbytetoken;
	prs->state->poschar -= prs->state->lenchartoken;
589 590 591
}

static void
592
SpecialVerVersion(TParser *prs)
593
{
594 595 596 597
	prs->state->posbyte -= prs->state->lenbytetoken;
	prs->state->poschar -= prs->state->lenchartoken;
	prs->state->lenbytetoken = 0;
	prs->state->lenchartoken = 0;
598 599 600
}

static int
601
p_isstophost(TParser *prs)
602 603 604 605 606 607 608 609 610 611
{
	if (prs->wanthost)
	{
		prs->wanthost = false;
		return 1;
	}
	return 0;
}

static int
612
p_isignore(TParser *prs)
613 614 615 616 617
{
	return (prs->ignore) ? 1 : 0;
}

static int
618
p_ishost(TParser *prs)
619 620 621 622
{
	TParser    *tmpprs = TParserInit(prs->str + prs->state->posbyte, prs->lenstr - prs->state->posbyte);
	int			res = 0;

623 624
	tmpprs->wanthost = true;

625 626
	if (TParserGet(tmpprs) && tmpprs->type == HOST)
	{
627 628 629 630
		prs->state->posbyte += tmpprs->lenbytetoken;
		prs->state->poschar += tmpprs->lenchartoken;
		prs->state->lenbytetoken += tmpprs->lenbytetoken;
		prs->state->lenchartoken += tmpprs->lenchartoken;
631 632 633 634 635 636 637 638 639
		prs->state->charlen = tmpprs->state->charlen;
		res = 1;
	}
	TParserClose(tmpprs);

	return res;
}

static int
640
p_isURLPath(TParser *prs)
641 642 643 644 645 646 647
{
	TParser    *tmpprs = TParserInit(prs->str + prs->state->posbyte, prs->lenstr - prs->state->posbyte);
	int			res = 0;

	tmpprs->state = newTParserPosition(tmpprs->state);
	tmpprs->state->state = TPS_InFileFirst;

648
	if (TParserGet(tmpprs) && (tmpprs->type == URLPATH || tmpprs->type == FILEPATH))
649
	{
650 651 652 653
		prs->state->posbyte += tmpprs->lenbytetoken;
		prs->state->poschar += tmpprs->lenchartoken;
		prs->state->lenbytetoken += tmpprs->lenbytetoken;
		prs->state->lenchartoken += tmpprs->lenchartoken;
654 655 656 657 658 659 660 661 662 663 664 665
		prs->state->charlen = tmpprs->state->charlen;
		res = 1;
	}
	TParserClose(tmpprs);

	return res;
}

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

666
static const TParserStateActionItem actionTPS_Base[] = {
667 668 669
	{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},
670 671
	{p_isasclet, 0, A_NEXT, TPS_InAsciiWord, 0, NULL},
	{p_isalpha, 0, A_NEXT, TPS_InWord, 0, NULL},
672 673 674
	{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},
675
	{p_iseqC, '&', A_PUSH, TPS_InXMLEntityFirst, 0, NULL},
676 677 678 679 680 681 682
	{p_iseqC, '~', A_PUSH, TPS_InFileTwiddle, 0, NULL},
	{p_iseqC, '/', A_PUSH, TPS_InFileFirst, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InPathFirstFirst, 0, NULL},
	{NULL, 0, A_NEXT, TPS_InSpace, 0, NULL}
};


683
static const TParserStateActionItem actionTPS_InNumWord[] = {
684 685
	{p_isEOF, 0, A_BINGO, TPS_Base, NUMWORD, NULL},
	{p_isalnum, 0, A_NEXT, TPS_InNumWord, 0, NULL},
686 687 688
	{p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
	{p_iseqC, '/', A_PUSH, TPS_InFileFirst, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InFileNext, 0, NULL},
689 690
	{p_iseqC, '-', A_PUSH, TPS_InHyphenNumWordFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, NUMWORD, NULL}
691 692
};

693
static const TParserStateActionItem actionTPS_InAsciiWord[] = {
694 695
	{p_isEOF, 0, A_BINGO, TPS_Base, ASCIIWORD, NULL},
	{p_isasclet, 0, A_NEXT, TPS_Null, 0, NULL},
696 697 698
	{p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InFileNext, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHostFirstAN, 0, NULL},
699
	{p_iseqC, '-', A_PUSH, TPS_InHyphenAsciiWordFirst, 0, NULL},
700 701 702 703
	{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},
704 705 706
	{p_isdigit, 0, A_NEXT, TPS_InNumWord, 0, NULL},
	{p_isalpha, 0, A_NEXT, TPS_InWord, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, ASCIIWORD, NULL}
707 708
};

709
static const TParserStateActionItem actionTPS_InWord[] = {
710 711 712 713 714
	{p_isEOF, 0, A_BINGO, TPS_Base, WORD_T, NULL},
	{p_isalpha, 0, A_NEXT, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InNumWord, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHyphenWordFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, WORD_T, NULL}
715 716
};

717
static const TParserStateActionItem actionTPS_InUnsignedInt[] = {
718 719 720 721 722 723
	{p_isEOF, 0, A_BINGO, TPS_Base, UNSIGNEDINT, NULL},
	{p_isdigit, 0, A_NEXT, TPS_Null, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
	{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},
724 725
	{p_isasclet, 0, A_PUSH, TPS_InHost, 0, NULL},
	{p_isalpha, 0, A_NEXT, TPS_InNumWord, 0, NULL},
726 727 728 729
	{p_iseqC, '/', A_PUSH, TPS_InFileFirst, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, UNSIGNEDINT, NULL}
};

730
static const TParserStateActionItem actionTPS_InSignedIntFirst[] = {
731 732 733 734 735
	{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}
};

736
static const TParserStateActionItem actionTPS_InSignedInt[] = {
737 738 739 740 741 742 743 744
	{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}
};

745
static const TParserStateActionItem actionTPS_InSpace[] = {
746 747 748 749 750 751 752 753 754 755 756
	{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}
};

757
static const TParserStateActionItem actionTPS_InUDecimalFirst[] = {
758 759 760 761 762
	{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}
};

763
static const TParserStateActionItem actionTPS_InUDecimal[] = {
764 765 766 767 768 769 770 771
	{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}
};

772
static const TParserStateActionItem actionTPS_InDecimalFirst[] = {
773 774 775 776 777
	{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}
};

778
static const TParserStateActionItem actionTPS_InDecimal[] = {
779 780 781 782 783 784 785 786
	{p_isEOF, 0, A_BINGO, TPS_Base, DECIMAL, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InDecimal, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InVerVersion, 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}
};

787
static const TParserStateActionItem actionTPS_InVerVersion[] = {
788 789 790 791 792
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_RERUN, TPS_InSVerVersion, 0, SpecialVerVersion},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

793
static const TParserStateActionItem actionTPS_InSVerVersion[] = {
794 795 796 797 798 799
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_BINGO | A_CLRALL, TPS_InUnsignedInt, SPACE, NULL},
	{NULL, 0, A_NEXT, TPS_Null, 0, NULL}
};


800
static const TParserStateActionItem actionTPS_InVersionFirst[] = {
801 802 803 804 805
	{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}
};

806
static const TParserStateActionItem actionTPS_InVersion[] = {
807 808 809 810 811 812
	{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}
};

813
static const TParserStateActionItem actionTPS_InMantissaFirst[] = {
814 815 816 817 818 819 820
	{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}
};

821
static const TParserStateActionItem actionTPS_InMantissaSign[] = {
822 823 824 825 826
	{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}
};

827
static const TParserStateActionItem actionTPS_InMantissa[] = {
828 829 830 831 832
	{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}
};

833
static const TParserStateActionItem actionTPS_InXMLEntityFirst[] = {
834
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
835 836 837 838
	{p_iseqC, '#', A_NEXT, TPS_InXMLEntityNumFirst, 0, NULL},
	{p_isasclet, 0, A_NEXT, TPS_InXMLEntity, 0, NULL},
	{p_iseqC, ':', A_NEXT, TPS_InXMLEntity, 0, NULL},
	{p_iseqC, '_', A_NEXT, TPS_InXMLEntity, 0, NULL},
839 840 841
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

842
static const TParserStateActionItem actionTPS_InXMLEntity[] = {
843
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
844 845 846 847 848 849
	{p_isalnum, 0, A_NEXT, TPS_InXMLEntity, 0, NULL},
	{p_iseqC, ':', A_NEXT, TPS_InXMLEntity, 0, NULL},
	{p_iseqC, '_', A_NEXT, TPS_InXMLEntity, 0, NULL},
	{p_iseqC, '.', A_NEXT, TPS_InXMLEntity, 0, NULL},
	{p_iseqC, '-', A_NEXT, TPS_InXMLEntity, 0, NULL},
	{p_iseqC, ';', A_NEXT, TPS_InXMLEntityEnd, 0, NULL},
850 851 852
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

853
static const TParserStateActionItem actionTPS_InXMLEntityNumFirst[] = {
854
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
855
	{p_iseqC, 'x', A_NEXT, TPS_InXMLEntityHexNumFirst, 0, NULL},
856
	{p_iseqC, 'X', A_NEXT, TPS_InXMLEntityHexNumFirst, 0, NULL},
857
	{p_isdigit, 0, A_NEXT, TPS_InXMLEntityNum, 0, NULL},
858 859 860
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

861
static const TParserStateActionItem actionTPS_InXMLEntityHexNumFirst[] = {
862
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
863
	{p_isxdigit, 0, A_NEXT, TPS_InXMLEntityHexNum, 0, NULL},
864 865 866
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
static const TParserStateActionItem actionTPS_InXMLEntityNum[] = {
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InXMLEntityNum, 0, NULL},
	{p_iseqC, ';', A_NEXT, TPS_InXMLEntityEnd, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

static const TParserStateActionItem actionTPS_InXMLEntityHexNum[] = {
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isxdigit, 0, A_NEXT, TPS_InXMLEntityHexNum, 0, NULL},
	{p_iseqC, ';', A_NEXT, TPS_InXMLEntityEnd, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

static const TParserStateActionItem actionTPS_InXMLEntityEnd[] = {
	{NULL, 0, A_BINGO | A_CLEAR, TPS_Base, XMLENTITY, NULL}
883 884
};

885
static const TParserStateActionItem actionTPS_InTagFirst[] = {
886 887 888 889
	{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},
	{p_iseqC, '?', A_PUSH, TPS_InXMLBegin, 0, NULL},
890
	{p_isasclet, 0, A_PUSH, TPS_InTagName, 0, NULL},
891 892
	{p_iseqC, ':', A_PUSH, TPS_InTagName, 0, NULL},
	{p_iseqC, '_', A_PUSH, TPS_InTagName, 0, NULL},
893 894 895
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

896
static const TParserStateActionItem actionTPS_InXMLBegin[] = {
897 898
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	/* <?xml ... */
899
    /* XXX do we wants states for the m and l ?  Right now this accepts <?xZ */
900 901 902 903
	{p_iseqC, 'x', A_NEXT, TPS_InTag, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

904
static const TParserStateActionItem actionTPS_InTagCloseFirst[] = {
905
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
906
	{p_isasclet, 0, A_NEXT, TPS_InTagName, 0, NULL},
907 908 909
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

910
static const TParserStateActionItem actionTPS_InTagName[] = {
911 912 913 914 915
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	/* <br/> case */
	{p_iseqC, '/', A_NEXT, TPS_InTagBeginEnd, 0, NULL},
	{p_iseqC, '>', A_NEXT, TPS_InTagEnd, 0, SpecialTags},
	{p_isspace, 0, A_NEXT, TPS_InTag, 0, SpecialTags},
916 917 918 919 920
	{p_isalnum, 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},
	{p_iseqC, '-', A_NEXT, TPS_Null, 0, NULL},
921 922 923
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

924
static const TParserStateActionItem actionTPS_InTagBeginEnd[] = {
925 926 927 928 929
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '>', A_NEXT, TPS_InTagEnd, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

930
static const TParserStateActionItem actionTPS_InTag[] = {
931 932 933 934
	{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},
935
	{p_isasclet, 0, A_NEXT, TPS_Null, 0, NULL},
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
	{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},
	{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},
	{p_iseqC, '%', A_NEXT, TPS_Null, 0, NULL},
	{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}
};

951
static const TParserStateActionItem actionTPS_InTagEscapeK[] = {
952 953 954 955 956 957
	{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}
};

958
static const TParserStateActionItem actionTPS_InTagEscapeKK[] = {
959 960 961 962 963 964
	{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}
};

965
static const TParserStateActionItem actionTPS_InTagBackSleshed[] = {
966 967 968 969
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{NULL, 0, A_MERGE, TPS_Null, 0, NULL}
};

970
static const TParserStateActionItem actionTPS_InTagEnd[] = {
971
	{NULL, 0, A_BINGO | A_CLRALL, TPS_Base, TAG_T, NULL}
972 973
};

974
static const TParserStateActionItem actionTPS_InCommentFirst[] = {
975 976 977 978 979 980 981 982
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_iseqC, '-', A_NEXT, TPS_InCommentLast, 0, NULL},
	/* <!DOCTYPE ...> */
	{p_iseqC, 'D', A_NEXT, TPS_InTag, 0, NULL},
	{p_iseqC, 'd', A_NEXT, TPS_InTag, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

983
static const TParserStateActionItem actionTPS_InCommentLast[] = {
984 985 986 987 988
	{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}
};

989
static const TParserStateActionItem actionTPS_InComment[] = {
990 991 992 993 994
	{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}
};

995
static const TParserStateActionItem actionTPS_InCloseCommentFirst[] = {
996 997 998 999 1000
	{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}
};

1001
static const TParserStateActionItem actionTPS_InCloseCommentLast[] = {
1002 1003 1004 1005 1006 1007
	{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}
};

1008
static const TParserStateActionItem actionTPS_InCommentEnd[] = {
1009
	{NULL, 0, A_BINGO | A_CLRALL, TPS_Base, TAG_T, NULL}
1010 1011
};

1012
static const TParserStateActionItem actionTPS_InHostFirstDomain[] = {
1013
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
1014
	{p_isasclet, 0, A_NEXT, TPS_InHostDomainSecond, 0, NULL},
1015 1016 1017 1018
	{p_isdigit, 0, A_NEXT, TPS_InHost, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

1019
static const TParserStateActionItem actionTPS_InHostDomainSecond[] = {
1020
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
1021
	{p_isasclet, 0, A_NEXT, TPS_InHostDomain, 0, NULL},
1022 1023 1024 1025 1026 1027 1028
	{p_isdigit, 0, A_PUSH, TPS_InHost, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHostFirstAN, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
	{p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

1029
static const TParserStateActionItem actionTPS_InHostDomain[] = {
1030
	{p_isEOF, 0, A_BINGO | A_CLRALL, TPS_Base, HOST, NULL},
1031
	{p_isasclet, 0, A_NEXT, TPS_InHostDomain, 0, NULL},
1032 1033 1034 1035 1036 1037
	{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},
	{p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
	{p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
	{p_isdigit, 0, A_POP, TPS_Null, 0, NULL},
1038
	{p_isstophost, 0, A_BINGO | A_CLRALL, TPS_InURLPathStart, HOST, NULL},
1039 1040 1041 1042
	{p_iseqC, '/', A_PUSH, TPS_InFURL, 0, NULL},
	{NULL, 0, A_BINGO | A_CLRALL, TPS_Base, HOST, NULL}
};

1043
static const TParserStateActionItem actionTPS_InPortFirst[] = {
1044 1045 1046 1047 1048
	{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}
};

1049
static const TParserStateActionItem actionTPS_InPort[] = {
1050 1051
	{p_isEOF, 0, A_BINGO | A_CLRALL, TPS_Base, HOST, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InPort, 0, NULL},
1052
	{p_isstophost, 0, A_BINGO | A_CLRALL, TPS_InURLPathStart, HOST, NULL},
1053 1054 1055 1056
	{p_iseqC, '/', A_PUSH, TPS_InFURL, 0, NULL},
	{NULL, 0, A_BINGO | A_CLRALL, TPS_Base, HOST, NULL}
};

1057
static const TParserStateActionItem actionTPS_InHostFirstAN[] = {
1058 1059
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHost, 0, NULL},
1060
	{p_isasclet, 0, A_NEXT, TPS_InHost, 0, NULL},
1061 1062 1063
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

1064
static const TParserStateActionItem actionTPS_InHost[] = {
1065 1066
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHost, 0, NULL},
1067
	{p_isasclet, 0, A_NEXT, TPS_InHost, 0, NULL},
1068 1069 1070 1071 1072 1073
	{p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
	{p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHostFirstAN, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

1074
static const TParserStateActionItem actionTPS_InEmail[] = {
1075
	{p_isstophost, 0, A_POP, TPS_Null, 0, NULL},
1076 1077 1078 1079
	{p_ishost, 0, A_BINGO | A_CLRALL, TPS_Base, EMAIL, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

1080
static const TParserStateActionItem actionTPS_InFileFirst[] = {
1081
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
1082
	{p_isasclet, 0, A_NEXT, TPS_InFile, 0, NULL},
1083 1084 1085
	{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},
1086
	{p_iseqC, '?', A_PUSH, TPS_InURLPathFirst, 0, NULL},
1087 1088 1089 1090
	{p_iseqC, '~', A_PUSH, TPS_InFileTwiddle, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

1091
static const TParserStateActionItem actionTPS_InFileTwiddle[] = {
1092
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
1093
	{p_isasclet, 0, A_NEXT, TPS_InFile, 0, NULL},
1094 1095 1096 1097 1098 1099
	{p_isdigit, 0, A_NEXT, TPS_InFile, 0, NULL},
	{p_iseqC, '_', A_NEXT, TPS_InFile, 0, NULL},
	{p_iseqC, '/', A_NEXT, TPS_InFileFirst, 0, NULL},
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

1100
static const TParserStateActionItem actionTPS_InPathFirst[] = {
1101
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
1102
	{p_isasclet, 0, A_NEXT, TPS_InFile, 0, NULL},
1103 1104 1105 1106 1107 1108 1109
	{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}
};

1110
static const TParserStateActionItem actionTPS_InPathFirstFirst[] = {
1111 1112 1113 1114 1115 1116
	{p_isEOF, 0, A_POP, TPS_Null, 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}
};

1117
static const TParserStateActionItem actionTPS_InPathSecond[] = {
1118 1119 1120 1121 1122 1123 1124
	{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}
};

1125
static const TParserStateActionItem actionTPS_InFile[] = {
1126
	{p_isEOF, 0, A_BINGO, TPS_Base, FILEPATH, NULL},
1127
	{p_isasclet, 0, A_NEXT, TPS_InFile, 0, NULL},
1128 1129 1130 1131 1132
	{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},
1133
	{p_iseqC, '?', A_PUSH, TPS_InURLPathFirst, 0, NULL},
1134 1135 1136
	{NULL, 0, A_BINGO, TPS_Base, FILEPATH, NULL}
};

1137
static const TParserStateActionItem actionTPS_InFileNext[] = {
1138
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
1139
	{p_isasclet, 0, A_CLEAR, TPS_InFile, 0, NULL},
1140 1141 1142 1143 1144
	{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}
};

1145
static const TParserStateActionItem actionTPS_InURLPathFirst[] = {
1146 1147 1148
	{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},
1149
	{p_isnotspace, 0, A_CLEAR, TPS_InURLPath, 0, NULL},
1150 1151 1152
	{NULL, 0, A_POP, TPS_Null, 0, NULL},
};

1153
static const TParserStateActionItem actionTPS_InURLPathStart[] = {
1154
	{NULL, 0, A_NEXT, TPS_InURLPath, 0, NULL}
1155 1156
};

1157
static const TParserStateActionItem actionTPS_InURLPath[] = {
1158 1159 1160 1161 1162
	{p_isEOF, 0, A_BINGO, TPS_Base, URLPATH, NULL},
	{p_iseqC, '"', A_BINGO, TPS_Base, URLPATH, NULL},
	{p_iseqC, '\'', A_BINGO, TPS_Base, URLPATH, NULL},
	{p_isnotspace, 0, A_NEXT, TPS_InURLPath, 0, NULL},
	{NULL, 0, A_BINGO, TPS_Base, URLPATH, NULL}
1163 1164
};

1165
static const TParserStateActionItem actionTPS_InFURL[] = {
1166
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
1167
	{p_isURLPath, 0, A_BINGO | A_CLRALL, TPS_Base, URL_T, SpecialFURL},
1168 1169 1170
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

1171
static const TParserStateActionItem actionTPS_InProtocolFirst[] = {
1172 1173 1174 1175 1176
	{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}
};

1177
static const TParserStateActionItem actionTPS_InProtocolSecond[] = {
1178 1179 1180 1181 1182
	{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}
};

1183
static const TParserStateActionItem actionTPS_InProtocolEnd[] = {
1184 1185 1186
	{NULL, 0, A_BINGO | A_CLRALL, TPS_Base, PROTOCOL, NULL}
};

1187
static const TParserStateActionItem actionTPS_InHyphenAsciiWordFirst[] = {
1188
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
1189 1190
	{p_isasclet, 0, A_NEXT, TPS_InHyphenAsciiWord, 0, NULL},
	{p_isalpha, 0, A_NEXT, TPS_InHyphenWord, 0, NULL},
1191
	{p_isdigit, 0, A_NEXT, TPS_InHyphenDigitLookahead, 0, NULL},
1192 1193 1194
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

1195
static const TParserStateActionItem actionTPS_InHyphenAsciiWord[] = {
1196 1197 1198 1199 1200 1201
	{p_isEOF, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, ASCIIHWORD, SpecialHyphen},
	{p_isasclet, 0, A_NEXT, TPS_InHyphenAsciiWord, 0, NULL},
	{p_isalpha, 0, A_NEXT, TPS_InHyphenWord, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenNumWord, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHyphenAsciiWordFirst, 0, NULL},
	{NULL, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, ASCIIHWORD, SpecialHyphen}
1202 1203
};

1204
static const TParserStateActionItem actionTPS_InHyphenWordFirst[] = {
1205
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
1206
	{p_isalpha, 0, A_NEXT, TPS_InHyphenWord, 0, NULL},
1207
	{p_isdigit, 0, A_NEXT, TPS_InHyphenDigitLookahead, 0, NULL},
1208 1209 1210
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

1211
static const TParserStateActionItem actionTPS_InHyphenWord[] = {
1212 1213 1214 1215 1216
	{p_isEOF, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, HWORD, SpecialHyphen},
	{p_isalpha, 0, A_NEXT, TPS_InHyphenWord, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenNumWord, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHyphenWordFirst, 0, NULL},
	{NULL, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, HWORD, SpecialHyphen}
1217 1218
};

1219
static const TParserStateActionItem actionTPS_InHyphenNumWordFirst[] = {
1220
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
1221
	{p_isalpha, 0, A_NEXT, TPS_InHyphenNumWord, 0, NULL},
1222
	{p_isdigit, 0, A_NEXT, TPS_InHyphenDigitLookahead, 0, NULL},
1223 1224 1225
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

1226
static const TParserStateActionItem actionTPS_InHyphenNumWord[] = {
1227 1228 1229 1230
	{p_isEOF, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, NUMHWORD, SpecialHyphen},
	{p_isalnum, 0, A_NEXT, TPS_InHyphenNumWord, 0, NULL},
	{p_iseqC, '-', A_PUSH, TPS_InHyphenNumWordFirst, 0, NULL},
	{NULL, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, NUMHWORD, SpecialHyphen}
1231 1232
};

1233
static const TParserStateActionItem actionTPS_InHyphenDigitLookahead[] = {
1234
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
1235
	{p_isdigit, 0, A_NEXT, TPS_InHyphenDigitLookahead, 0, NULL},
1236
	{p_isalpha, 0, A_NEXT, TPS_InHyphenNumWord, 0, NULL},
1237
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
1238 1239
};

1240
static const TParserStateActionItem actionTPS_InParseHyphen[] = {
1241
	{p_isEOF, 0, A_RERUN, TPS_Base, 0, NULL},
1242 1243
	{p_isasclet, 0, A_NEXT, TPS_InHyphenAsciiWordPart, 0, NULL},
	{p_isalpha, 0, A_NEXT, TPS_InHyphenWordPart, 0, NULL},
1244
	{p_isdigit, 0, A_PUSH, TPS_InHyphenUnsignedInt, 0, NULL},
1245 1246 1247 1248
	{p_iseqC, '-', A_PUSH, TPS_InParseHyphenHyphen, 0, NULL},
	{NULL, 0, A_RERUN, TPS_Base, 0, NULL}
};

1249
static const TParserStateActionItem actionTPS_InParseHyphenHyphen[] = {
1250 1251 1252 1253 1254
	{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}
};

1255
static const TParserStateActionItem actionTPS_InHyphenWordPart[] = {
1256 1257 1258 1259
	{p_isEOF, 0, A_BINGO, TPS_Base, PARTHWORD, NULL},
	{p_isalpha, 0, A_NEXT, TPS_InHyphenWordPart, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenNumWordPart, 0, NULL},
	{NULL, 0, A_BINGO, TPS_InParseHyphen, PARTHWORD, NULL}
1260 1261
};

1262
static const TParserStateActionItem actionTPS_InHyphenAsciiWordPart[] = {
1263 1264 1265 1266 1267
	{p_isEOF, 0, A_BINGO, TPS_Base, ASCIIPARTHWORD, NULL},
	{p_isasclet, 0, A_NEXT, TPS_InHyphenAsciiWordPart, 0, NULL},
	{p_isalpha, 0, A_NEXT, TPS_InHyphenWordPart, 0, NULL},
	{p_isdigit, 0, A_NEXT, TPS_InHyphenNumWordPart, 0, NULL},
	{NULL, 0, A_BINGO, TPS_InParseHyphen, ASCIIPARTHWORD, NULL}
1268 1269
};

1270
static const TParserStateActionItem actionTPS_InHyphenNumWordPart[] = {
1271 1272 1273
	{p_isEOF, 0, A_BINGO, TPS_Base, NUMPARTHWORD, NULL},
	{p_isalnum, 0, A_NEXT, TPS_InHyphenNumWordPart, 0, NULL},
	{NULL, 0, A_BINGO, TPS_InParseHyphen, NUMPARTHWORD, NULL}
1274 1275
};

1276
static const TParserStateActionItem actionTPS_InHyphenUnsignedInt[] = {
1277
	{p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
1278 1279
	{p_isdigit, 0, A_NEXT, TPS_Null, 0, NULL},
	{p_isalpha, 0, A_CLEAR, TPS_InHyphenNumWordPart, 0, NULL},
1280 1281 1282
	{NULL, 0, A_POP, TPS_Null, 0, NULL}
};

1283 1284 1285 1286 1287 1288

/*
 * main table of per-state parser actions
 */
typedef struct
{
Bruce Momjian's avatar
Bruce Momjian committed
1289
	const TParserStateActionItem *action;		/* the actual state info */
1290 1291 1292 1293
	TParserState state;			/* only for Assert crosscheck */
#ifdef WPARSER_TRACE
	const char *state_name;		/* only for debug printout */
#endif
1294
} TParserStateAction;
1295 1296 1297 1298 1299 1300 1301 1302 1303

#ifdef WPARSER_TRACE
#define TPARSERSTATEACTION(state) \
	{ CppConcat(action,state), state, CppAsString(state) }
#else
#define TPARSERSTATEACTION(state) \
	{ CppConcat(action,state), state }
#endif

1304
/*
1305
 * order must be the same as in typedef enum {} TParserState!!
1306 1307 1308
 */

static const TParserStateAction Actions[] = {
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
	TPARSERSTATEACTION(TPS_Base),
	TPARSERSTATEACTION(TPS_InNumWord),
	TPARSERSTATEACTION(TPS_InAsciiWord),
	TPARSERSTATEACTION(TPS_InWord),
	TPARSERSTATEACTION(TPS_InUnsignedInt),
	TPARSERSTATEACTION(TPS_InSignedIntFirst),
	TPARSERSTATEACTION(TPS_InSignedInt),
	TPARSERSTATEACTION(TPS_InSpace),
	TPARSERSTATEACTION(TPS_InUDecimalFirst),
	TPARSERSTATEACTION(TPS_InUDecimal),
	TPARSERSTATEACTION(TPS_InDecimalFirst),
	TPARSERSTATEACTION(TPS_InDecimal),
	TPARSERSTATEACTION(TPS_InVerVersion),
	TPARSERSTATEACTION(TPS_InSVerVersion),
	TPARSERSTATEACTION(TPS_InVersionFirst),
	TPARSERSTATEACTION(TPS_InVersion),
	TPARSERSTATEACTION(TPS_InMantissaFirst),
	TPARSERSTATEACTION(TPS_InMantissaSign),
	TPARSERSTATEACTION(TPS_InMantissa),
1328 1329 1330 1331 1332 1333 1334
	TPARSERSTATEACTION(TPS_InXMLEntityFirst),
	TPARSERSTATEACTION(TPS_InXMLEntity),
	TPARSERSTATEACTION(TPS_InXMLEntityNumFirst),
	TPARSERSTATEACTION(TPS_InXMLEntityNum),
	TPARSERSTATEACTION(TPS_InXMLEntityHexNumFirst),
	TPARSERSTATEACTION(TPS_InXMLEntityHexNum),
	TPARSERSTATEACTION(TPS_InXMLEntityEnd),
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
	TPARSERSTATEACTION(TPS_InTagFirst),
	TPARSERSTATEACTION(TPS_InXMLBegin),
	TPARSERSTATEACTION(TPS_InTagCloseFirst),
	TPARSERSTATEACTION(TPS_InTagName),
	TPARSERSTATEACTION(TPS_InTagBeginEnd),
	TPARSERSTATEACTION(TPS_InTag),
	TPARSERSTATEACTION(TPS_InTagEscapeK),
	TPARSERSTATEACTION(TPS_InTagEscapeKK),
	TPARSERSTATEACTION(TPS_InTagBackSleshed),
	TPARSERSTATEACTION(TPS_InTagEnd),
	TPARSERSTATEACTION(TPS_InCommentFirst),
	TPARSERSTATEACTION(TPS_InCommentLast),
	TPARSERSTATEACTION(TPS_InComment),
	TPARSERSTATEACTION(TPS_InCloseCommentFirst),
	TPARSERSTATEACTION(TPS_InCloseCommentLast),
	TPARSERSTATEACTION(TPS_InCommentEnd),
	TPARSERSTATEACTION(TPS_InHostFirstDomain),
	TPARSERSTATEACTION(TPS_InHostDomainSecond),
	TPARSERSTATEACTION(TPS_InHostDomain),
	TPARSERSTATEACTION(TPS_InPortFirst),
	TPARSERSTATEACTION(TPS_InPort),
	TPARSERSTATEACTION(TPS_InHostFirstAN),
	TPARSERSTATEACTION(TPS_InHost),
	TPARSERSTATEACTION(TPS_InEmail),
	TPARSERSTATEACTION(TPS_InFileFirst),
	TPARSERSTATEACTION(TPS_InFileTwiddle),
	TPARSERSTATEACTION(TPS_InPathFirst),
	TPARSERSTATEACTION(TPS_InPathFirstFirst),
	TPARSERSTATEACTION(TPS_InPathSecond),
	TPARSERSTATEACTION(TPS_InFile),
	TPARSERSTATEACTION(TPS_InFileNext),
	TPARSERSTATEACTION(TPS_InURLPathFirst),
	TPARSERSTATEACTION(TPS_InURLPathStart),
	TPARSERSTATEACTION(TPS_InURLPath),
	TPARSERSTATEACTION(TPS_InFURL),
	TPARSERSTATEACTION(TPS_InProtocolFirst),
	TPARSERSTATEACTION(TPS_InProtocolSecond),
	TPARSERSTATEACTION(TPS_InProtocolEnd),
	TPARSERSTATEACTION(TPS_InHyphenAsciiWordFirst),
	TPARSERSTATEACTION(TPS_InHyphenAsciiWord),
	TPARSERSTATEACTION(TPS_InHyphenWordFirst),
	TPARSERSTATEACTION(TPS_InHyphenWord),
	TPARSERSTATEACTION(TPS_InHyphenNumWordFirst),
	TPARSERSTATEACTION(TPS_InHyphenNumWord),
1379
	TPARSERSTATEACTION(TPS_InHyphenDigitLookahead),
1380 1381 1382 1383 1384
	TPARSERSTATEACTION(TPS_InParseHyphen),
	TPARSERSTATEACTION(TPS_InParseHyphenHyphen),
	TPARSERSTATEACTION(TPS_InHyphenWordPart),
	TPARSERSTATEACTION(TPS_InHyphenAsciiWordPart),
	TPARSERSTATEACTION(TPS_InHyphenNumWordPart),
1385
	TPARSERSTATEACTION(TPS_InHyphenUnsignedInt)
1386 1387 1388 1389
};


static bool
1390
TParserGet(TParser *prs)
1391
{
1392
	const TParserStateActionItem *item = NULL;
1393

1394 1395
	Assert(prs->state);

1396 1397 1398
	if (prs->state->posbyte >= prs->lenstr)
		return false;

1399
	prs->token = prs->str + prs->state->posbyte;
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
	prs->state->pushedAtAction = NULL;

	/* look at string */
	while (prs->state->posbyte <= prs->lenstr)
	{
		if (prs->state->posbyte == prs->lenstr)
			prs->state->charlen = 0;
		else
			prs->state->charlen = (prs->charmaxlen == 1) ? prs->charmaxlen :
				pg_mblen(prs->str + prs->state->posbyte);

		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);

1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
		if (prs->state->pushedAtAction)
		{
			/* After a POP, pick up at the next test */
			item = prs->state->pushedAtAction + 1;
			prs->state->pushedAtAction = NULL;
		}
		else
		{
			item = Actions[prs->state->state].action;
			Assert(item != NULL);
		}
1426 1427 1428 1429 1430 1431

		/* find action by character class */
		while (item->isclass)
		{
			prs->c = item->c;
			if (item->isclass(prs) != 0)
1432
				break;
1433 1434 1435
			item++;
		}

1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
#ifdef WPARSER_TRACE
		{
			TParserPosition *ptr;

			fprintf(stderr, "state ");
			/* indent according to stack depth */
			for (ptr = prs->state->prev; ptr; ptr = ptr->prev)
				fprintf(stderr, "  ");
			fprintf(stderr, "%s ", Actions[prs->state->state].state_name);
			if (prs->state->posbyte < prs->lenstr)
				fprintf(stderr, "at %c", *(prs->str + prs->state->posbyte));
			else
				fprintf(stderr, "at EOF");
			fprintf(stderr, " matched rule %d flags%s%s%s%s%s%s%s%s%s%s%s\n",
					(int) (item - Actions[prs->state->state].action),
					(item->flags & A_BINGO) ? " BINGO" : "",
					(item->flags & A_POP) ? " POP" : "",
					(item->flags & A_PUSH) ? " PUSH" : "",
					(item->flags & A_RERUN) ? " RERUN" : "",
					(item->flags & A_CLEAR) ? " CLEAR" : "",
					(item->flags & A_MERGE) ? " MERGE" : "",
					(item->flags & A_CLRALL) ? " CLRALL" : "",
					(item->tostate != TPS_Null) ? " tostate " : "",
					(item->tostate != TPS_Null) ? Actions[item->tostate].state_name : "",
					(item->type > 0) ? " type " : "",
					tok_alias[item->type]);
		}
#endif
1464 1465 1466 1467 1468

		/* call special handler if exists */
		if (item->special)
			item->special(prs);

1469
		/* BINGO, token is found */
1470 1471 1472
		if (item->flags & A_BINGO)
		{
			Assert(item->type > 0);
1473 1474 1475
			prs->lenbytetoken = prs->state->lenbytetoken;
			prs->lenchartoken = prs->state->lenchartoken;
			prs->state->lenbytetoken = prs->state->lenchartoken = 0;
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
			prs->type = item->type;
		}

		/* do various actions by flags */
		if (item->flags & A_POP)
		{						/* pop stored state in stack */
			TParserPosition *ptr = prs->state->prev;

			pfree(prs->state);
			prs->state = ptr;
			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 */
			TParserPosition *ptr;

			Assert(prs->state->prev);
			ptr = prs->state->prev->prev;
			pfree(prs->state->prev);
			prs->state->prev = ptr;
		}
		else if (item->flags & A_CLRALL)
		{						/* clear all previous pushed state */
			TParserPosition *ptr;

			while (prs->state->prev)
			{
				ptr = prs->state->prev->prev;
				pfree(prs->state->prev);
				prs->state->prev = ptr;
			}
		}
		else if (item->flags & A_MERGE)
		{						/* merge posinfo with current and pushed state */
			TParserPosition *ptr = prs->state;

			Assert(prs->state->prev);
			prs->state = prs->state->prev;

			prs->state->posbyte = ptr->posbyte;
			prs->state->poschar = ptr->poschar;
			prs->state->charlen = ptr->charlen;
1523 1524
			prs->state->lenbytetoken = ptr->lenbytetoken;
			prs->state->lenchartoken = ptr->lenchartoken;
1525 1526 1527 1528 1529 1530 1531 1532
			pfree(ptr);
		}

		/* set new state if pointed */
		if (item->tostate != TPS_Null)
			prs->state->state = item->tostate;

		/* check for go away */
1533 1534 1535
		if ((item->flags & A_BINGO) ||
			(prs->state->posbyte >= prs->lenstr &&
			 (item->flags & A_RERUN) == 0))
1536 1537
			break;

1538
		/* go to beginning of loop if we should rerun or we just restore state */
1539 1540 1541 1542 1543 1544 1545
		if (item->flags & (A_RERUN | A_POP))
			continue;

		/* move forward */
		if (prs->state->charlen)
		{
			prs->state->posbyte += prs->state->charlen;
1546
			prs->state->lenbytetoken += prs->state->charlen;
1547
			prs->state->poschar++;
1548
			prs->state->lenchartoken++;
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
		}
	}

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

Datum
prsd_lextype(PG_FUNCTION_ARGS)
{
	LexDescr   *descr = (LexDescr *) palloc(sizeof(LexDescr) * (LASTNUM + 1));
	int			i;

	for (i = 1; i <= LASTNUM; i++)
	{
		descr[i - 1].lexid = i;
		descr[i - 1].alias = pstrdup(tok_alias[i]);
		descr[i - 1].descr = pstrdup(lex_descr[i]);
	}

	descr[LASTNUM].lexid = 0;

	PG_RETURN_POINTER(descr);
}

Datum
prsd_start(PG_FUNCTION_ARGS)
{
	PG_RETURN_POINTER(TParserInit((char *) PG_GETARG_POINTER(0), PG_GETARG_INT32(1)));
}

Datum
prsd_nexttoken(PG_FUNCTION_ARGS)
{
	TParser    *p = (TParser *) PG_GETARG_POINTER(0);
	char	  **t = (char **) PG_GETARG_POINTER(1);
	int		   *tlen = (int *) PG_GETARG_POINTER(2);

	if (!TParserGet(p))
		PG_RETURN_INT32(0);

1589 1590
	*t = p->token;
	*tlen = p->lenbytetoken;
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603

	PG_RETURN_INT32(p->type);
}

Datum
prsd_end(PG_FUNCTION_ARGS)
{
	TParser    *p = (TParser *) PG_GETARG_POINTER(0);

	TParserClose(p);
	PG_RETURN_VOID();
}

1604 1605 1606
#define LEAVETOKEN(x)	( (x)==SPACE )
#define COMPLEXTOKEN(x) ( (x)==URL_T || (x)==NUMHWORD || (x)==ASCIIHWORD || (x)==HWORD )
#define ENDPUNCTOKEN(x) ( (x)==SPACE )
1607

1608 1609 1610 1611 1612
#define TS_IDIGNORE(x)  ( (x)==TAG_T || (x)==PROTOCOL || (x)==SPACE || (x)==XMLENTITY )
#define HLIDREPLACE(x)  ( (x)==TAG_T )
#define HLIDSKIP(x)     ( (x)==URL_T || (x)==NUMHWORD || (x)==ASCIIHWORD || (x)==HWORD )
#define XMLHLIDSKIP(x)  ( (x)==URL_T || (x)==NUMHWORD || (x)==ASCIIHWORD || (x)==HWORD )
#define NONWORDTOKEN(x) ( (x)==SPACE || HLIDREPLACE(x) || HLIDSKIP(x) )
1613
#define NOENDTOKEN(x)	( NONWORDTOKEN(x) || (x)==SCIENTIFIC || (x)==VERSIONNUMBER || (x)==DECIMAL || (x)==SIGNEDINT || (x)==UNSIGNEDINT || TS_IDIGNORE(x) )
1614 1615 1616 1617 1618

typedef struct
{
	HeadlineWordEntry *words;
	int			len;
1619
} hlCheck;
1620 1621

static bool
1622
checkcondition_HL(void *checkval, QueryOperand *val)
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
{
	int			i;

	for (i = 0; i < ((hlCheck *) checkval)->len; i++)
	{
		if (((hlCheck *) checkval)->words[i].item == val)
			return true;
	}
	return false;
}


static bool
1636
hlCover(HeadlineParsedText *prs, TSQuery query, int *p, int *q)
1637 1638 1639 1640 1641 1642
{
	int			i,
				j;
	QueryItem  *item = GETQUERY(query);
	int			pos = *p;

1643
	*q = -1;
1644 1645 1646 1647
	*p = 0x7fffffff;

	for (j = 0; j < query->size; j++)
	{
1648
		if (item->type != QI_VAL)
1649 1650 1651 1652 1653 1654
		{
			item++;
			continue;
		}
		for (i = pos; i < prs->curwords; i++)
		{
1655
			if (prs->words[i].item == &item->operand)
1656 1657 1658 1659 1660 1661 1662 1663 1664
			{
				if (i > *q)
					*q = i;
				break;
			}
		}
		item++;
	}

1665
	if (*q < 0)
1666 1667 1668 1669 1670
		return false;

	item = GETQUERY(query);
	for (j = 0; j < query->size; j++)
	{
1671
		if (item->type != QI_VAL)
1672 1673 1674 1675 1676 1677
		{
			item++;
			continue;
		}
		for (i = *q; i >= pos; i--)
		{
1678
			if (prs->words[i].item == &item->operand)
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
			{
				if (i < *p)
					*p = i;
				break;
			}
		}
		item++;
	}

	if (*p <= *q)
	{
		hlCheck		ch;

		ch.words = &(prs->words[*p]);
		ch.len = *q - *p + 1;
		if (TS_execute(GETQUERY(query), &ch, false, checkcondition_HL))
			return true;
		else
		{
			(*p)++;
			return hlCover(prs, query, p, q);
		}
	}

	return false;
}

1706 1707
static void 
mark_fragment(HeadlineParsedText *prs, int highlight, int startpos, int endpos)
1708
{
1709
	int   i;
1710

1711 1712 1713 1714 1715 1716
	for (i = startpos; i <= endpos; i++)
	{
		if (prs->words[i].item)
			prs->words[i].selected = 1;
		if (highlight == 0)
		{
1717
			if (HLIDREPLACE(prs->words[i].type))
1718
				prs->words[i].replace = 1;
1719 1720
			else if ( HLIDSKIP(prs->words[i].type) )
				prs->words[i].skip = 1;
1721 1722 1723
		}
		else
		{
1724 1725
			if (XMLHLIDSKIP(prs->words[i].type))
				prs->words[i].skip = 1;
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
		}

		prs->words[i].in = (prs->words[i].repeated) ? 0 : 1;
	}
}

typedef struct 
{
	int4 startpos;
	int4 endpos;
	int4 poslen;
	int4 curlen;
	int2 in;
	int2 excluded;
} CoverPos;

static void 
get_next_fragment(HeadlineParsedText *prs, int *startpos, int *endpos,
			int *curlen, int *poslen, int max_words)
{
	int i;
	/* Objective: Generate a fragment of words between startpos and endpos 
	 * such that it has at most max_words and both ends has query words. 
	 * If the startpos and endpos are the endpoints of the cover and the 
	 * cover has fewer words than max_words, then this function should 
	 * just return the cover 
	 */
	/* first move startpos to an item */
	for(i = *startpos; i <= *endpos; i++)
	{
		*startpos = i;
		if (prs->words[i].item && !prs->words[i].repeated)
			break;
	}
	/* cut endpos to have only max_words */
	*curlen = 0;
	*poslen = 0;
	for(i = *startpos; i <= *endpos && *curlen < max_words; i++) 
	{
		if (!NONWORDTOKEN(prs->words[i].type))
			*curlen += 1;
		if (prs->words[i].item && !prs->words[i].repeated)
			*poslen += 1;
	}
	/* if the cover was cut then move back endpos to a query item */ 		
	if (*endpos > i)
	{
		*endpos = i;
		for(i = *endpos; i >= *startpos; i --)
		{
			*endpos = i;
			if (prs->words[i].item && !prs->words[i].repeated)
				break;
			if (!NONWORDTOKEN(prs->words[i].type))
				*curlen -= 1;
		}		
	}	
}

static void
mark_hl_fragments(HeadlineParsedText *prs, TSQuery query, int highlight,
                        int shortword, int min_words, 
			int max_words, int max_fragments)
{
	int4           	poslen, curlen, i, f, num_f = 0;
	int4		stretch, maxstretch, posmarker;

	int4           	startpos = 0, 
 			endpos   = 0,
			p        = 0,
			q        = 0;

	int4		numcovers = 0, 
			maxcovers = 32;

	int4          	minI, minwords, maxitems;
	CoverPos	*covers;

	covers = palloc(maxcovers * sizeof(CoverPos));
 
	/* get all covers */
	while (hlCover(prs, query, &p, &q))
	{
		startpos = p;
		endpos   = q;

		/* Break the cover into smaller fragments such that each fragment
		 * has at most max_words. Also ensure that each end of the fragment
		 * is a query word. This will allow us to stretch the fragment in 
		 * either direction
		 */

		while (startpos <= endpos)
		{
			get_next_fragment(prs, &startpos, &endpos, &curlen, &poslen, max_words);
			if (numcovers >= maxcovers)
			{
				maxcovers *= 2;
				covers     = repalloc(covers, sizeof(CoverPos) * maxcovers);
			}
			covers[numcovers].startpos = startpos;
			covers[numcovers].endpos   = endpos;
			covers[numcovers].curlen   = curlen;
			covers[numcovers].poslen   = poslen;
			covers[numcovers].in       = 0;
			covers[numcovers].excluded = 0;
			numcovers ++;
			startpos = endpos + 1;
			endpos   = q;
		}	
		/* move p to generate the next cover */
 		p++;
	}
1839

1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
	/* choose best covers */
	for (f = 0; f < max_fragments; f++)
	{
		maxitems = 0;
		minwords = 0x7fffffff;
		minI = -1;
		/* Choose the cover that contains max items.
		 * In case of tie choose the one with smaller 
		 * number of words. 
		 */
		for (i = 0; i < numcovers; i ++)
		{
			if (!covers[i].in &&  !covers[i].excluded && 
  				(maxitems < covers[i].poslen || (maxitems == covers[i].poslen
				&& minwords > covers[i].curlen)))
			{
				maxitems = covers[i].poslen;
				minwords = covers[i].curlen;
				minI     = i;
			}
		}
		/* if a cover was found mark it */
		if (minI >= 0)
		{
			covers[minI].in = 1;
			/* adjust the size of cover */
			startpos = covers[minI].startpos;
			endpos   = covers[minI].endpos;
			curlen   = covers[minI].curlen;
			/* stretch the cover if cover size is lower than max_words */
			if (curlen < max_words) 
			{
				/* divide the stretch on both sides of cover */
				maxstretch = (max_words - curlen)/2;
				/* first stretch the startpos 
				 * stop stretching if 
				 * 	1. we hit the beginning of document
				 * 	2. exceed maxstretch
				 * 	3. we hit an already marked fragment 
				 */
				stretch   = 0;
				posmarker = startpos;
				for (i = startpos - 1; i >= 0 && stretch < maxstretch && !prs->words[i].in; i--)
				{
					if (!NONWORDTOKEN(prs->words[i].type))
					{
						curlen  ++;
						stretch ++;
					}
					posmarker = i;
				}
				/* cut back startpos till we find a non short token */
				for (i = posmarker; i < startpos && (NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword); i++)
				{
					if (!NONWORDTOKEN(prs->words[i].type))
						curlen --;
				}
				startpos = i;
				/* now stretch the endpos as much as possible*/
				posmarker = endpos;
				for (i = endpos + 1; i < prs->curwords && curlen < max_words && !prs->words[i].in; i++)
				{
					if (!NONWORDTOKEN(prs->words[i].type))
						curlen  ++;
					posmarker = i;	
				}
				/* cut back endpos till we find a non-short token */
				for ( i = posmarker; i > endpos && (NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword); i--)
				{
					if (!NONWORDTOKEN(prs->words[i].type))
						curlen --;
				}
				endpos = i;
			}
			covers[minI].startpos = startpos;
			covers[minI].endpos   = endpos;
			covers[minI].curlen   = curlen;
			/* Mark the chosen fragments (covers) */
			mark_fragment(prs, highlight, startpos, endpos);
			num_f ++;
			/* exclude overlapping covers */
			for (i = 0; i < numcovers; i ++)
			{
				if (i != minI && ( (covers[i].startpos >= covers[minI].startpos && covers[i].startpos <= covers[minI].endpos)  || (covers[i].endpos >= covers[minI].startpos && covers[i].endpos <= covers[minI].endpos))) 
					covers[i].excluded = 1;
			}
		}
		else
			break;
	}

	/* show at least min_words we have not marked anything*/
	if (num_f <= 0)
	{
		startpos = endpos = curlen = 0;
		for (i = 0; i < prs->curwords && curlen < min_words; i++)
		{
			if (!NONWORDTOKEN(prs->words[i].type))
				curlen++;
			endpos = i;
		}
		mark_fragment(prs, highlight, startpos, endpos);
	}
	pfree(covers);
}
1945

1946 1947 1948 1949
static void
mark_hl_words(HeadlineParsedText *prs, TSQuery query, int highlight, 
		int shortword, int min_words, int max_words)
{
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
	int			p = 0,
				q = 0;
	int			bestb = -1,
				beste = -1;
	int			bestlen = -1;
	int			pose = 0,
				posb,
				poslen,
				curlen;

	int			i;

	if (highlight == 0)
	{
		while (hlCover(prs, query, &p, &q))
		{
			/* find cover len in words */
			curlen = 0;
			poslen = 0;
			for (i = p; i <= q && curlen < max_words; i++)
			{
				if (!NONWORDTOKEN(prs->words[i].type))
					curlen++;
				if (prs->words[i].item && !prs->words[i].repeated)
					poslen++;
				pose = i;
			}

			if (poslen < bestlen && !(NOENDTOKEN(prs->words[beste].type) || prs->words[beste].len <= shortword))
			{
				/* best already finded, so try one more cover */
				p++;
				continue;
			}

			posb = p;
			if (curlen < max_words)
			{					/* find good end */
				for (i = i - 1; i < prs->curwords && curlen < max_words; i++)
				{
					if (i != q)
					{
						if (!NONWORDTOKEN(prs->words[i].type))
							curlen++;
						if (prs->words[i].item && !prs->words[i].repeated)
							poslen++;
					}
					pose = i;
					if (NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword)
						continue;
					if (curlen >= min_words)
						break;
				}
				if (curlen < min_words && i >= prs->curwords)
				{				/* got end of text and our cover is shoter
								 * than min_words */
2006
					for (i = p - 1; i >= 0; i--)
2007 2008 2009 2010 2011
					{
						if (!NONWORDTOKEN(prs->words[i].type))
							curlen++;
						if (prs->words[i].item && !prs->words[i].repeated)
							poslen++;
2012 2013
						if ( curlen >= max_words )
							break;
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073
						if (NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword)
							continue;
						if (curlen >= min_words)
							break;
					}
					posb = (i >= 0) ? i : 0;
				}
			}
			else
			{					/* shorter cover :((( */
				for (; curlen > min_words; i--)
				{
					if (!NONWORDTOKEN(prs->words[i].type))
						curlen--;
					if (prs->words[i].item && !prs->words[i].repeated)
						poslen--;
					pose = i;
					if (NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword)
						continue;
					break;
				}
			}

			if (bestlen < 0 || (poslen > bestlen && !(NOENDTOKEN(prs->words[pose].type) || prs->words[pose].len <= shortword)) ||
				(bestlen >= 0 && !(NOENDTOKEN(prs->words[pose].type) || prs->words[pose].len <= shortword) &&
				 (NOENDTOKEN(prs->words[beste].type) || prs->words[beste].len <= shortword)))
			{
				bestb = posb;
				beste = pose;
				bestlen = poslen;
			}

			p++;
		}

		if (bestlen < 0)
		{
			curlen = 0;
			for (i = 0; i < prs->curwords && curlen < min_words; i++)
			{
				if (!NONWORDTOKEN(prs->words[i].type))
					curlen++;
				pose = i;
			}
			bestb = 0;
			beste = pose;
		}
	}
	else
	{
		bestb = 0;
		beste = prs->curwords - 1;
	}

	for (i = bestb; i <= beste; i++)
	{
		if (prs->words[i].item)
			prs->words[i].selected = 1;
		if (highlight == 0)
		{
2074
			if (HLIDREPLACE(prs->words[i].type))
2075
				prs->words[i].replace = 1;
2076 2077
			else if ( HLIDSKIP(prs->words[i].type) )
				prs->words[i].skip = 1;
2078 2079 2080
		}
		else
		{
2081 2082
			if (XMLHLIDSKIP(prs->words[i].type))
				prs->words[i].skip = 1;
2083 2084 2085 2086 2087
		}

		prs->words[i].in = (prs->words[i].repeated) ? 0 : 1;
	}

2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
}

Datum
prsd_headline(PG_FUNCTION_ARGS)
{
	HeadlineParsedText *prs = (HeadlineParsedText *) PG_GETARG_POINTER(0);
	List	   *prsoptions = (List *) PG_GETARG_POINTER(1);
	TSQuery		query = PG_GETARG_TSQUERY(2);

	/* from opt + start and and tag */
	int			min_words     = 15;
	int			max_words     = 35;
	int			shortword     = 3;
	int			max_fragments = 0;
	int			highlight     = 0;
	ListCell   *l;

	/* config */
	prs->startsel = NULL;
	prs->stopsel = NULL;
	foreach(l, prsoptions)
	{
		DefElem    *defel = (DefElem *) lfirst(l);
		char	   *val = defGetString(defel);

		if (pg_strcasecmp(defel->defname, "MaxWords") == 0)
			max_words = pg_atoi(val, sizeof(int32), 0);
		else if (pg_strcasecmp(defel->defname, "MinWords") == 0)
			min_words = pg_atoi(val, sizeof(int32), 0);
		else if (pg_strcasecmp(defel->defname, "ShortWord") == 0)
			shortword = pg_atoi(val, sizeof(int32), 0);
		else if (pg_strcasecmp(defel->defname, "MaxFragments") == 0)
			max_fragments = pg_atoi(val, sizeof(int32), 0);
		else if (pg_strcasecmp(defel->defname, "StartSel") == 0)
			prs->startsel = pstrdup(val);
		else if (pg_strcasecmp(defel->defname, "StopSel") == 0)
			prs->stopsel = pstrdup(val);
		else if (pg_strcasecmp(defel->defname, "FragmentDelimiter") == 0)
			prs->fragdelim = pstrdup(val);
		else if (pg_strcasecmp(defel->defname, "HighlightAll") == 0)
			highlight = (pg_strcasecmp(val, "1") == 0 ||
						 pg_strcasecmp(val, "on") == 0 ||
						 pg_strcasecmp(val, "true") == 0 ||
						 pg_strcasecmp(val, "t") == 0 ||
						 pg_strcasecmp(val, "y") == 0 ||
						 pg_strcasecmp(val, "yes") == 0);
		else
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
					 errmsg("unrecognized headline parameter: \"%s\"",
							defel->defname)));
	}

	if (highlight == 0)
	{
		if (min_words >= max_words)
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
					 errmsg("MinWords should be less than MaxWords")));
		if (min_words <= 0)
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
					 errmsg("MinWords should be positive")));
		if (shortword < 0)
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
					 errmsg("ShortWord should be >= 0")));
		if (max_fragments < 0)
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
					 errmsg("MaxFragments should be >= 0")));
	}				 

	if (max_fragments == 0)
		/* call the default headline generator */
		mark_hl_words(prs, query, highlight, shortword, min_words, max_words);
	else
		mark_hl_fragments(prs, query, highlight, shortword, min_words, max_words, max_fragments);

2167 2168 2169 2170
	if (!prs->startsel)
		prs->startsel = pstrdup("<b>");
	if (!prs->stopsel)
		prs->stopsel = pstrdup("</b>");
2171 2172
	if (!prs->fragdelim)
		prs->fragdelim = pstrdup(" ... ");
2173 2174
	prs->startsellen = strlen(prs->startsel);
	prs->stopsellen = strlen(prs->stopsel);
2175
	prs->fragdelimlen = strlen(prs->fragdelim);
2176 2177 2178

	PG_RETURN_POINTER(prs);
}
2179