LCOV - code coverage report
Current view: top level - src/backend/tsearch - wparser_def.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 511 584 87.5 %
Date: 2021-12-09 04:09:06 Functions: 38 53 71.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * wparser_def.c
       4             :  *      Default text search parser
       5             :  *
       6             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
       7             :  *
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/tsearch/wparser_def.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : 
      15             : #include "postgres.h"
      16             : 
      17             : #include <limits.h>
      18             : 
      19             : #include "catalog/pg_collation.h"
      20             : #include "commands/defrem.h"
      21             : #include "tsearch/ts_locale.h"
      22             : #include "tsearch/ts_public.h"
      23             : #include "tsearch/ts_type.h"
      24             : #include "tsearch/ts_utils.h"
      25             : #include "utils/builtins.h"
      26             : 
      27             : 
      28             : /* Define me to enable tracing of parser behavior */
      29             : /* #define WPARSER_TRACE */
      30             : 
      31             : 
      32             : /* Output token categories */
      33             : 
      34             : #define ASCIIWORD       1
      35             : #define WORD_T          2
      36             : #define NUMWORD         3
      37             : #define EMAIL           4
      38             : #define URL_T           5
      39             : #define HOST            6
      40             : #define SCIENTIFIC      7
      41             : #define VERSIONNUMBER   8
      42             : #define NUMPARTHWORD    9
      43             : #define PARTHWORD       10
      44             : #define ASCIIPARTHWORD  11
      45             : #define SPACE           12
      46             : #define TAG_T           13
      47             : #define PROTOCOL        14
      48             : #define NUMHWORD        15
      49             : #define ASCIIHWORD      16
      50             : #define HWORD           17
      51             : #define URLPATH         18
      52             : #define FILEPATH        19
      53             : #define DECIMAL_T       20
      54             : #define SIGNEDINT       21
      55             : #define UNSIGNEDINT     22
      56             : #define XMLENTITY       23
      57             : 
      58             : #define LASTNUM         23
      59             : 
      60             : static const char *const tok_alias[] = {
      61             :     "",
      62             :     "asciiword",
      63             :     "word",
      64             :     "numword",
      65             :     "email",
      66             :     "url",
      67             :     "host",
      68             :     "sfloat",
      69             :     "version",
      70             :     "hword_numpart",
      71             :     "hword_part",
      72             :     "hword_asciipart",
      73             :     "blank",
      74             :     "tag",
      75             :     "protocol",
      76             :     "numhword",
      77             :     "asciihword",
      78             :     "hword",
      79             :     "url_path",
      80             :     "file",
      81             :     "float",
      82             :     "int",
      83             :     "uint",
      84             :     "entity"
      85             : };
      86             : 
      87             : static const char *const lex_descr[] = {
      88             :     "",
      89             :     "Word, all ASCII",
      90             :     "Word, all letters",
      91             :     "Word, letters and digits",
      92             :     "Email address",
      93             :     "URL",
      94             :     "Host",
      95             :     "Scientific notation",
      96             :     "Version number",
      97             :     "Hyphenated word part, letters and digits",
      98             :     "Hyphenated word part, all letters",
      99             :     "Hyphenated word part, all ASCII",
     100             :     "Space symbols",
     101             :     "XML tag",
     102             :     "Protocol head",
     103             :     "Hyphenated word, letters and digits",
     104             :     "Hyphenated word, all ASCII",
     105             :     "Hyphenated word, all letters",
     106             :     "URL path",
     107             :     "File or path name",
     108             :     "Decimal notation",
     109             :     "Signed integer",
     110             :     "Unsigned integer",
     111             :     "XML entity"
     112             : };
     113             : 
     114             : 
     115             : /* Parser states */
     116             : 
     117             : typedef enum
     118             : {
     119             :     TPS_Base = 0,
     120             :     TPS_InNumWord,
     121             :     TPS_InAsciiWord,
     122             :     TPS_InWord,
     123             :     TPS_InUnsignedInt,
     124             :     TPS_InSignedIntFirst,
     125             :     TPS_InSignedInt,
     126             :     TPS_InSpace,
     127             :     TPS_InUDecimalFirst,
     128             :     TPS_InUDecimal,
     129             :     TPS_InDecimalFirst,
     130             :     TPS_InDecimal,
     131             :     TPS_InVerVersion,
     132             :     TPS_InSVerVersion,
     133             :     TPS_InVersionFirst,
     134             :     TPS_InVersion,
     135             :     TPS_InMantissaFirst,
     136             :     TPS_InMantissaSign,
     137             :     TPS_InMantissa,
     138             :     TPS_InXMLEntityFirst,
     139             :     TPS_InXMLEntity,
     140             :     TPS_InXMLEntityNumFirst,
     141             :     TPS_InXMLEntityNum,
     142             :     TPS_InXMLEntityHexNumFirst,
     143             :     TPS_InXMLEntityHexNum,
     144             :     TPS_InXMLEntityEnd,
     145             :     TPS_InTagFirst,
     146             :     TPS_InXMLBegin,
     147             :     TPS_InTagCloseFirst,
     148             :     TPS_InTagName,
     149             :     TPS_InTagBeginEnd,
     150             :     TPS_InTag,
     151             :     TPS_InTagEscapeK,
     152             :     TPS_InTagEscapeKK,
     153             :     TPS_InTagBackSleshed,
     154             :     TPS_InTagEnd,
     155             :     TPS_InCommentFirst,
     156             :     TPS_InCommentLast,
     157             :     TPS_InComment,
     158             :     TPS_InCloseCommentFirst,
     159             :     TPS_InCloseCommentLast,
     160             :     TPS_InCommentEnd,
     161             :     TPS_InHostFirstDomain,
     162             :     TPS_InHostDomainSecond,
     163             :     TPS_InHostDomain,
     164             :     TPS_InPortFirst,
     165             :     TPS_InPort,
     166             :     TPS_InHostFirstAN,
     167             :     TPS_InHost,
     168             :     TPS_InEmail,
     169             :     TPS_InFileFirst,
     170             :     TPS_InFileTwiddle,
     171             :     TPS_InPathFirst,
     172             :     TPS_InPathFirstFirst,
     173             :     TPS_InPathSecond,
     174             :     TPS_InFile,
     175             :     TPS_InFileNext,
     176             :     TPS_InURLPathFirst,
     177             :     TPS_InURLPathStart,
     178             :     TPS_InURLPath,
     179             :     TPS_InFURL,
     180             :     TPS_InProtocolFirst,
     181             :     TPS_InProtocolSecond,
     182             :     TPS_InProtocolEnd,
     183             :     TPS_InHyphenAsciiWordFirst,
     184             :     TPS_InHyphenAsciiWord,
     185             :     TPS_InHyphenWordFirst,
     186             :     TPS_InHyphenWord,
     187             :     TPS_InHyphenNumWordFirst,
     188             :     TPS_InHyphenNumWord,
     189             :     TPS_InHyphenDigitLookahead,
     190             :     TPS_InParseHyphen,
     191             :     TPS_InParseHyphenHyphen,
     192             :     TPS_InHyphenWordPart,
     193             :     TPS_InHyphenAsciiWordPart,
     194             :     TPS_InHyphenNumWordPart,
     195             :     TPS_InHyphenUnsignedInt,
     196             :     TPS_Null                    /* last state (fake value) */
     197             : } TParserState;
     198             : 
     199             : /* forward declaration */
     200             : struct TParser;
     201             : 
     202             : typedef int (*TParserCharTest) (struct TParser *);  /* any p_is* functions
     203             :                                                      * except p_iseq */
     204             : typedef void (*TParserSpecial) (struct TParser *);  /* special handler for
     205             :                                                      * special cases... */
     206             : 
     207             : typedef struct
     208             : {
     209             :     TParserCharTest isclass;
     210             :     char        c;
     211             :     uint16      flags;
     212             :     TParserState tostate;
     213             :     int         type;
     214             :     TParserSpecial special;
     215             : } TParserStateActionItem;
     216             : 
     217             : /* Flag bits in TParserStateActionItem.flags */
     218             : #define A_NEXT      0x0000
     219             : #define A_BINGO     0x0001
     220             : #define A_POP       0x0002
     221             : #define A_PUSH      0x0004
     222             : #define A_RERUN     0x0008
     223             : #define A_CLEAR     0x0010
     224             : #define A_MERGE     0x0020
     225             : #define A_CLRALL    0x0040
     226             : 
     227             : typedef struct TParserPosition
     228             : {
     229             :     int         posbyte;        /* position of parser in bytes */
     230             :     int         poschar;        /* position of parser in characters */
     231             :     int         charlen;        /* length of current char */
     232             :     int         lenbytetoken;   /* length of token-so-far in bytes */
     233             :     int         lenchartoken;   /* and in chars */
     234             :     TParserState state;
     235             :     struct TParserPosition *prev;
     236             :     const TParserStateActionItem *pushedAtAction;
     237             : } TParserPosition;
     238             : 
     239             : typedef struct TParser
     240             : {
     241             :     /* string and position information */
     242             :     char       *str;            /* multibyte string */
     243             :     int         lenstr;         /* length of mbstring */
     244             :     wchar_t    *wstr;           /* wide character string */
     245             :     pg_wchar   *pgwstr;         /* wide character string for C-locale */
     246             :     bool        usewide;
     247             : 
     248             :     /* State of parse */
     249             :     int         charmaxlen;
     250             :     TParserPosition *state;
     251             :     bool        ignore;
     252             :     bool        wanthost;
     253             : 
     254             :     /* silly char */
     255             :     char        c;
     256             : 
     257             :     /* out */
     258             :     char       *token;
     259             :     int         lenbytetoken;
     260             :     int         lenchartoken;
     261             :     int         type;
     262             : } TParser;
     263             : 
     264             : 
     265             : /* forward decls here */
     266             : static bool TParserGet(TParser *prs);
     267             : 
     268             : 
     269             : static TParserPosition *
     270        6340 : newTParserPosition(TParserPosition *prev)
     271             : {
     272        6340 :     TParserPosition *res = (TParserPosition *) palloc(sizeof(TParserPosition));
     273             : 
     274        6340 :     if (prev)
     275        3132 :         memcpy(res, prev, sizeof(TParserPosition));
     276             :     else
     277        3208 :         memset(res, 0, sizeof(TParserPosition));
     278             : 
     279        6340 :     res->prev = prev;
     280             : 
     281        6340 :     res->pushedAtAction = NULL;
     282             : 
     283        6340 :     return res;
     284             : }
     285             : 
     286             : static TParser *
     287        3048 : TParserInit(char *str, int len)
     288             : {
     289        3048 :     TParser    *prs = (TParser *) palloc0(sizeof(TParser));
     290             : 
     291        3048 :     prs->charmaxlen = pg_database_encoding_max_length();
     292        3048 :     prs->str = str;
     293        3048 :     prs->lenstr = len;
     294             : 
     295             :     /*
     296             :      * Use wide char code only when max encoding length > 1.
     297             :      */
     298        3048 :     if (prs->charmaxlen > 1)
     299             :     {
     300        3048 :         Oid         collation = DEFAULT_COLLATION_OID;  /* TODO */
     301        3048 :         pg_locale_t mylocale = 0;   /* TODO */
     302             : 
     303        3048 :         prs->usewide = true;
     304        3048 :         if (lc_ctype_is_c(collation))
     305             :         {
     306             :             /*
     307             :              * char2wchar doesn't work for C-locale and sizeof(pg_wchar) could
     308             :              * be different from sizeof(wchar_t)
     309             :              */
     310           0 :             prs->pgwstr = (pg_wchar *) palloc(sizeof(pg_wchar) * (prs->lenstr + 1));
     311           0 :             pg_mb2wchar_with_len(prs->str, prs->pgwstr, prs->lenstr);
     312             :         }
     313             :         else
     314             :         {
     315        3048 :             prs->wstr = (wchar_t *) palloc(sizeof(wchar_t) * (prs->lenstr + 1));
     316        3048 :             char2wchar(prs->wstr, prs->lenstr + 1, prs->str, prs->lenstr,
     317             :                        mylocale);
     318             :         }
     319             :     }
     320             :     else
     321           0 :         prs->usewide = false;
     322             : 
     323        3048 :     prs->state = newTParserPosition(NULL);
     324        3048 :     prs->state->state = TPS_Base;
     325             : 
     326             : #ifdef WPARSER_TRACE
     327             :     fprintf(stderr, "parsing \"%.*s\"\n", len, str);
     328             : #endif
     329             : 
     330        3048 :     return prs;
     331             : }
     332             : 
     333             : /*
     334             :  * As an alternative to a full TParserInit one can create a
     335             :  * TParserCopy which basically is a regular TParser without a private
     336             :  * copy of the string - instead it uses the one from another TParser.
     337             :  * This is useful because at some places TParsers are created
     338             :  * recursively and the repeated copying around of the strings can
     339             :  * cause major inefficiency if the source string is long.
     340             :  * The new parser starts parsing at the original's current position.
     341             :  *
     342             :  * Obviously one must not close the original TParser before the copy.
     343             :  */
     344             : static TParser *
     345         160 : TParserCopyInit(const TParser *orig)
     346             : {
     347         160 :     TParser    *prs = (TParser *) palloc0(sizeof(TParser));
     348             : 
     349         160 :     prs->charmaxlen = orig->charmaxlen;
     350         160 :     prs->str = orig->str + orig->state->posbyte;
     351         160 :     prs->lenstr = orig->lenstr - orig->state->posbyte;
     352         160 :     prs->usewide = orig->usewide;
     353             : 
     354         160 :     if (orig->pgwstr)
     355           0 :         prs->pgwstr = orig->pgwstr + orig->state->poschar;
     356         160 :     if (orig->wstr)
     357         160 :         prs->wstr = orig->wstr + orig->state->poschar;
     358             : 
     359         160 :     prs->state = newTParserPosition(NULL);
     360         160 :     prs->state->state = TPS_Base;
     361             : 
     362             : #ifdef WPARSER_TRACE
     363             :     fprintf(stderr, "parsing copy of \"%.*s\"\n", prs->lenstr, prs->str);
     364             : #endif
     365             : 
     366         160 :     return prs;
     367             : }
     368             : 
     369             : 
     370             : static void
     371        3048 : TParserClose(TParser *prs)
     372             : {
     373        6096 :     while (prs->state)
     374             :     {
     375        3048 :         TParserPosition *ptr = prs->state->prev;
     376             : 
     377        3048 :         pfree(prs->state);
     378        3048 :         prs->state = ptr;
     379             :     }
     380             : 
     381        3048 :     if (prs->wstr)
     382        3048 :         pfree(prs->wstr);
     383        3048 :     if (prs->pgwstr)
     384           0 :         pfree(prs->pgwstr);
     385             : 
     386             : #ifdef WPARSER_TRACE
     387             :     fprintf(stderr, "closing parser\n");
     388             : #endif
     389        3048 :     pfree(prs);
     390        3048 : }
     391             : 
     392             : /*
     393             :  * Close a parser created with TParserCopyInit
     394             :  */
     395             : static void
     396         160 : TParserCopyClose(TParser *prs)
     397             : {
     398         408 :     while (prs->state)
     399             :     {
     400         248 :         TParserPosition *ptr = prs->state->prev;
     401             : 
     402         248 :         pfree(prs->state);
     403         248 :         prs->state = ptr;
     404             :     }
     405             : 
     406             : #ifdef WPARSER_TRACE
     407             :     fprintf(stderr, "closing parser copy\n");
     408             : #endif
     409         160 :     pfree(prs);
     410         160 : }
     411             : 
     412             : 
     413             : /*
     414             :  * Character-type support functions, equivalent to is* macros, but
     415             :  * working with any possible encodings and locales. Notes:
     416             :  *  - with multibyte encoding and C-locale isw* function may fail
     417             :  *    or give wrong result.
     418             :  *  - multibyte encoding and C-locale often are used for
     419             :  *    Asian languages.
     420             :  *  - if locale is C then we use pgwstr instead of wstr.
     421             :  */
     422             : 
     423             : #define p_iswhat(type, nonascii)                                            \
     424             :                                                                             \
     425             : static int                                                                  \
     426             : p_is##type(TParser *prs)                                                    \
     427             : {                                                                           \
     428             :     Assert(prs->state);                                                      \
     429             :     if (prs->usewide)                                                        \
     430             :     {                                                                       \
     431             :         if (prs->pgwstr)                                                 \
     432             :         {                                                                   \
     433             :             unsigned int c = *(prs->pgwstr + prs->state->poschar);         \
     434             :             if (c > 0x7f)                                                    \
     435             :                 return nonascii;                                            \
     436             :             return is##type(c);                                             \
     437             :         }                                                                   \
     438             :         return isw##type(*(prs->wstr + prs->state->poschar));              \
     439             :     }                                                                       \
     440             :     return is##type(*(unsigned char *) (prs->str + prs->state->posbyte));  \
     441             : }                                                                           \
     442             :                                                                             \
     443             : static int                                                                  \
     444             : p_isnot##type(TParser *prs)                                                 \
     445             : {                                                                           \
     446             :     return !p_is##type(prs);                                                \
     447             : }
     448             : 
     449             : /*
     450             :  * In C locale with a multibyte encoding, any non-ASCII symbol is considered
     451             :  * an alpha character, but not a member of other char classes.
     452             :  */
     453       13356 : p_iswhat(alnum, 1)
     454       52912 : p_iswhat(alpha, 1)
     455       20820 : p_iswhat(digit, 0)
     456           0 : p_iswhat(lower, 0)
     457           0 : p_iswhat(print, 0)
     458           0 : p_iswhat(punct, 0)
     459         452 : p_iswhat(space, 0)
     460           0 : p_iswhat(upper, 0)
     461          12 : p_iswhat(xdigit, 0)
     462             : 
     463             : /* p_iseq should be used only for ascii symbols */
     464             : 
     465             : static int
     466      127152 : p_iseq(TParser *prs, char c)
     467             : {
     468             :     Assert(prs->state);
     469      127152 :     return ((prs->state->charlen == 1 && *(prs->str + prs->state->posbyte) == c)) ? 1 : 0;
     470             : }
     471             : 
     472             : static int
     473       57188 : p_isEOF(TParser *prs)
     474             : {
     475             :     Assert(prs->state);
     476       57188 :     return (prs->state->posbyte == prs->lenstr || prs->state->charlen == 0) ? 1 : 0;
     477             : }
     478             : 
     479             : static int
     480      127152 : p_iseqC(TParser *prs)
     481             : {
     482      127152 :     return p_iseq(prs, prs->c);
     483             : }
     484             : 
     485             : static int
     486           0 : p_isneC(TParser *prs)
     487             : {
     488           0 :     return !p_iseq(prs, prs->c);
     489             : }
     490             : 
     491             : static int
     492       41680 : p_isascii(TParser *prs)
     493             : {
     494       41680 :     return (prs->state->charlen == 1 && isascii((unsigned char) *(prs->str + prs->state->posbyte))) ? 1 : 0;
     495             : }
     496             : 
     497             : static int
     498       41680 : p_isasclet(TParser *prs)
     499             : {
     500       41680 :     return (p_isascii(prs) && p_isalpha(prs)) ? 1 : 0;
     501             : }
     502             : 
     503             : static int
     504        1772 : p_isurlchar(TParser *prs)
     505             : {
     506             :     char        ch;
     507             : 
     508             :     /* no non-ASCII need apply */
     509        1772 :     if (prs->state->charlen != 1)
     510           0 :         return 0;
     511        1772 :     ch = *(prs->str + prs->state->posbyte);
     512             :     /* no spaces or control characters */
     513        1772 :     if (ch <= 0x20 || ch >= 0x7F)
     514         156 :         return 0;
     515             :     /* reject characters disallowed by RFC 3986 */
     516        1616 :     switch (ch)
     517             :     {
     518          16 :         case '"':
     519             :         case '<':
     520             :         case '>':
     521             :         case '\\':
     522             :         case '^':
     523             :         case '`':
     524             :         case '{':
     525             :         case '|':
     526             :         case '}':
     527          16 :             return 0;
     528             :     }
     529        1600 :     return 1;
     530             : }
     531             : 
     532             : 
     533             : /* deliberately suppress unused-function complaints for the above */
     534             : void        _make_compiler_happy(void);
     535             : void
     536           0 : _make_compiler_happy(void)
     537             : {
     538           0 :     p_isalnum(NULL);
     539           0 :     p_isnotalnum(NULL);
     540           0 :     p_isalpha(NULL);
     541           0 :     p_isnotalpha(NULL);
     542           0 :     p_isdigit(NULL);
     543           0 :     p_isnotdigit(NULL);
     544           0 :     p_islower(NULL);
     545           0 :     p_isnotlower(NULL);
     546           0 :     p_isprint(NULL);
     547           0 :     p_isnotprint(NULL);
     548           0 :     p_ispunct(NULL);
     549           0 :     p_isnotpunct(NULL);
     550           0 :     p_isspace(NULL);
     551           0 :     p_isnotspace(NULL);
     552           0 :     p_isupper(NULL);
     553           0 :     p_isnotupper(NULL);
     554           0 :     p_isxdigit(NULL);
     555           0 :     p_isnotxdigit(NULL);
     556           0 :     p_isEOF(NULL);
     557           0 :     p_iseqC(NULL);
     558           0 :     p_isneC(NULL);
     559           0 : }
     560             : 
     561             : 
     562             : static void
     563         168 : SpecialTags(TParser *prs)
     564             : {
     565         168 :     switch (prs->state->lenchartoken)
     566             :     {
     567           4 :         case 8:                 /* </script */
     568           4 :             if (pg_strncasecmp(prs->token, "</script", 8) == 0)
     569           4 :                 prs->ignore = false;
     570           4 :             break;
     571          16 :         case 7:                 /* <script || </style */
     572          16 :             if (pg_strncasecmp(prs->token, "</style", 7) == 0)
     573           0 :                 prs->ignore = false;
     574          16 :             else if (pg_strncasecmp(prs->token, "<script", 7) == 0)
     575           4 :                 prs->ignore = true;
     576          16 :             break;
     577          12 :         case 6:                 /* <style */
     578          12 :             if (pg_strncasecmp(prs->token, "<style", 6) == 0)
     579           0 :                 prs->ignore = true;
     580          12 :             break;
     581         136 :         default:
     582         136 :             break;
     583             :     }
     584         168 : }
     585             : 
     586             : static void
     587          88 : SpecialFURL(TParser *prs)
     588             : {
     589          88 :     prs->wanthost = true;
     590          88 :     prs->state->posbyte -= prs->state->lenbytetoken;
     591          88 :     prs->state->poschar -= prs->state->lenchartoken;
     592          88 : }
     593             : 
     594             : static void
     595          24 : SpecialHyphen(TParser *prs)
     596             : {
     597          24 :     prs->state->posbyte -= prs->state->lenbytetoken;
     598          24 :     prs->state->poschar -= prs->state->lenchartoken;
     599          24 : }
     600             : 
     601             : static void
     602           0 : SpecialVerVersion(TParser *prs)
     603             : {
     604           0 :     prs->state->posbyte -= prs->state->lenbytetoken;
     605           0 :     prs->state->poschar -= prs->state->lenchartoken;
     606           0 :     prs->state->lenbytetoken = 0;
     607           0 :     prs->state->lenchartoken = 0;
     608           0 : }
     609             : 
     610             : static int
     611         320 : p_isstophost(TParser *prs)
     612             : {
     613         320 :     if (prs->wanthost)
     614             :     {
     615         136 :         prs->wanthost = false;
     616         136 :         return 1;
     617             :     }
     618         184 :     return 0;
     619             : }
     620             : 
     621             : static int
     622       19992 : p_isignore(TParser *prs)
     623             : {
     624       19992 :     return (prs->ignore) ? 1 : 0;
     625             : }
     626             : 
     627             : static int
     628          60 : p_ishost(TParser *prs)
     629             : {
     630          60 :     TParser    *tmpprs = TParserCopyInit(prs);
     631          60 :     int         res = 0;
     632             : 
     633          60 :     tmpprs->wanthost = true;
     634             : 
     635          60 :     if (TParserGet(tmpprs) && tmpprs->type == HOST)
     636             :     {
     637          48 :         prs->state->posbyte += tmpprs->lenbytetoken;
     638          48 :         prs->state->poschar += tmpprs->lenchartoken;
     639          48 :         prs->state->lenbytetoken += tmpprs->lenbytetoken;
     640          48 :         prs->state->lenchartoken += tmpprs->lenchartoken;
     641          48 :         prs->state->charlen = tmpprs->state->charlen;
     642          48 :         res = 1;
     643             :     }
     644          60 :     TParserCopyClose(tmpprs);
     645             : 
     646          60 :     return res;
     647             : }
     648             : 
     649             : static int
     650         100 : p_isURLPath(TParser *prs)
     651             : {
     652         100 :     TParser    *tmpprs = TParserCopyInit(prs);
     653         100 :     int         res = 0;
     654             : 
     655         100 :     tmpprs->state = newTParserPosition(tmpprs->state);
     656         100 :     tmpprs->state->state = TPS_InURLPathFirst;
     657             : 
     658         100 :     if (TParserGet(tmpprs) && tmpprs->type == URLPATH)
     659             :     {
     660          88 :         prs->state->posbyte += tmpprs->lenbytetoken;
     661          88 :         prs->state->poschar += tmpprs->lenchartoken;
     662          88 :         prs->state->lenbytetoken += tmpprs->lenbytetoken;
     663          88 :         prs->state->lenchartoken += tmpprs->lenchartoken;
     664          88 :         prs->state->charlen = tmpprs->state->charlen;
     665          88 :         res = 1;
     666             :     }
     667         100 :     TParserCopyClose(tmpprs);
     668             : 
     669         100 :     return res;
     670             : }
     671             : 
     672             : /*
     673             :  * returns true if current character has zero display length or
     674             :  * it's a special sign in several languages. Such characters
     675             :  * aren't a word-breaker although they aren't an isalpha.
     676             :  * In beginning of word they aren't a part of it.
     677             :  */
     678             : static int
     679        4700 : p_isspecial(TParser *prs)
     680             : {
     681             :     /*
     682             :      * pg_dsplen could return -1 which means error or control character
     683             :      */
     684        4700 :     if (pg_dsplen(prs->str + prs->state->posbyte) == 0)
     685           0 :         return 1;
     686             : 
     687             :     /*
     688             :      * Unicode Characters in the 'Mark, Spacing Combining' Category That
     689             :      * characters are not alpha although they are not breakers of word too.
     690             :      * Check that only in utf encoding, because other encodings aren't
     691             :      * supported by postgres or even exists.
     692             :      */
     693        4700 :     if (GetDatabaseEncoding() == PG_UTF8 && prs->usewide)
     694             :     {
     695             :         static const pg_wchar strange_letter[] = {
     696             :             /*
     697             :              * use binary search, so elements should be ordered
     698             :              */
     699             :             0x0903,             /* DEVANAGARI SIGN VISARGA */
     700             :             0x093E,             /* DEVANAGARI VOWEL SIGN AA */
     701             :             0x093F,             /* DEVANAGARI VOWEL SIGN I */
     702             :             0x0940,             /* DEVANAGARI VOWEL SIGN II */
     703             :             0x0949,             /* DEVANAGARI VOWEL SIGN CANDRA O */
     704             :             0x094A,             /* DEVANAGARI VOWEL SIGN SHORT O */
     705             :             0x094B,             /* DEVANAGARI VOWEL SIGN O */
     706             :             0x094C,             /* DEVANAGARI VOWEL SIGN AU */
     707             :             0x0982,             /* BENGALI SIGN ANUSVARA */
     708             :             0x0983,             /* BENGALI SIGN VISARGA */
     709             :             0x09BE,             /* BENGALI VOWEL SIGN AA */
     710             :             0x09BF,             /* BENGALI VOWEL SIGN I */
     711             :             0x09C0,             /* BENGALI VOWEL SIGN II */
     712             :             0x09C7,             /* BENGALI VOWEL SIGN E */
     713             :             0x09C8,             /* BENGALI VOWEL SIGN AI */
     714             :             0x09CB,             /* BENGALI VOWEL SIGN O */
     715             :             0x09CC,             /* BENGALI VOWEL SIGN AU */
     716             :             0x09D7,             /* BENGALI AU LENGTH MARK */
     717             :             0x0A03,             /* GURMUKHI SIGN VISARGA */
     718             :             0x0A3E,             /* GURMUKHI VOWEL SIGN AA */
     719             :             0x0A3F,             /* GURMUKHI VOWEL SIGN I */
     720             :             0x0A40,             /* GURMUKHI VOWEL SIGN II */
     721             :             0x0A83,             /* GUJARATI SIGN VISARGA */
     722             :             0x0ABE,             /* GUJARATI VOWEL SIGN AA */
     723             :             0x0ABF,             /* GUJARATI VOWEL SIGN I */
     724             :             0x0AC0,             /* GUJARATI VOWEL SIGN II */
     725             :             0x0AC9,             /* GUJARATI VOWEL SIGN CANDRA O */
     726             :             0x0ACB,             /* GUJARATI VOWEL SIGN O */
     727             :             0x0ACC,             /* GUJARATI VOWEL SIGN AU */
     728             :             0x0B02,             /* ORIYA SIGN ANUSVARA */
     729             :             0x0B03,             /* ORIYA SIGN VISARGA */
     730             :             0x0B3E,             /* ORIYA VOWEL SIGN AA */
     731             :             0x0B40,             /* ORIYA VOWEL SIGN II */
     732             :             0x0B47,             /* ORIYA VOWEL SIGN E */
     733             :             0x0B48,             /* ORIYA VOWEL SIGN AI */
     734             :             0x0B4B,             /* ORIYA VOWEL SIGN O */
     735             :             0x0B4C,             /* ORIYA VOWEL SIGN AU */
     736             :             0x0B57,             /* ORIYA AU LENGTH MARK */
     737             :             0x0BBE,             /* TAMIL VOWEL SIGN AA */
     738             :             0x0BBF,             /* TAMIL VOWEL SIGN I */
     739             :             0x0BC1,             /* TAMIL VOWEL SIGN U */
     740             :             0x0BC2,             /* TAMIL VOWEL SIGN UU */
     741             :             0x0BC6,             /* TAMIL VOWEL SIGN E */
     742             :             0x0BC7,             /* TAMIL VOWEL SIGN EE */
     743             :             0x0BC8,             /* TAMIL VOWEL SIGN AI */
     744             :             0x0BCA,             /* TAMIL VOWEL SIGN O */
     745             :             0x0BCB,             /* TAMIL VOWEL SIGN OO */
     746             :             0x0BCC,             /* TAMIL VOWEL SIGN AU */
     747             :             0x0BD7,             /* TAMIL AU LENGTH MARK */
     748             :             0x0C01,             /* TELUGU SIGN CANDRABINDU */
     749             :             0x0C02,             /* TELUGU SIGN ANUSVARA */
     750             :             0x0C03,             /* TELUGU SIGN VISARGA */
     751             :             0x0C41,             /* TELUGU VOWEL SIGN U */
     752             :             0x0C42,             /* TELUGU VOWEL SIGN UU */
     753             :             0x0C43,             /* TELUGU VOWEL SIGN VOCALIC R */
     754             :             0x0C44,             /* TELUGU VOWEL SIGN VOCALIC RR */
     755             :             0x0C82,             /* KANNADA SIGN ANUSVARA */
     756             :             0x0C83,             /* KANNADA SIGN VISARGA */
     757             :             0x0CBE,             /* KANNADA VOWEL SIGN AA */
     758             :             0x0CC0,             /* KANNADA VOWEL SIGN II */
     759             :             0x0CC1,             /* KANNADA VOWEL SIGN U */
     760             :             0x0CC2,             /* KANNADA VOWEL SIGN UU */
     761             :             0x0CC3,             /* KANNADA VOWEL SIGN VOCALIC R */
     762             :             0x0CC4,             /* KANNADA VOWEL SIGN VOCALIC RR */
     763             :             0x0CC7,             /* KANNADA VOWEL SIGN EE */
     764             :             0x0CC8,             /* KANNADA VOWEL SIGN AI */
     765             :             0x0CCA,             /* KANNADA VOWEL SIGN O */
     766             :             0x0CCB,             /* KANNADA VOWEL SIGN OO */
     767             :             0x0CD5,             /* KANNADA LENGTH MARK */
     768             :             0x0CD6,             /* KANNADA AI LENGTH MARK */
     769             :             0x0D02,             /* MALAYALAM SIGN ANUSVARA */
     770             :             0x0D03,             /* MALAYALAM SIGN VISARGA */
     771             :             0x0D3E,             /* MALAYALAM VOWEL SIGN AA */
     772             :             0x0D3F,             /* MALAYALAM VOWEL SIGN I */
     773             :             0x0D40,             /* MALAYALAM VOWEL SIGN II */
     774             :             0x0D46,             /* MALAYALAM VOWEL SIGN E */
     775             :             0x0D47,             /* MALAYALAM VOWEL SIGN EE */
     776             :             0x0D48,             /* MALAYALAM VOWEL SIGN AI */
     777             :             0x0D4A,             /* MALAYALAM VOWEL SIGN O */
     778             :             0x0D4B,             /* MALAYALAM VOWEL SIGN OO */
     779             :             0x0D4C,             /* MALAYALAM VOWEL SIGN AU */
     780             :             0x0D57,             /* MALAYALAM AU LENGTH MARK */
     781             :             0x0D82,             /* SINHALA SIGN ANUSVARAYA */
     782             :             0x0D83,             /* SINHALA SIGN VISARGAYA */
     783             :             0x0DCF,             /* SINHALA VOWEL SIGN AELA-PILLA */
     784             :             0x0DD0,             /* SINHALA VOWEL SIGN KETTI AEDA-PILLA */
     785             :             0x0DD1,             /* SINHALA VOWEL SIGN DIGA AEDA-PILLA */
     786             :             0x0DD8,             /* SINHALA VOWEL SIGN GAETTA-PILLA */
     787             :             0x0DD9,             /* SINHALA VOWEL SIGN KOMBUVA */
     788             :             0x0DDA,             /* SINHALA VOWEL SIGN DIGA KOMBUVA */
     789             :             0x0DDB,             /* SINHALA VOWEL SIGN KOMBU DEKA */
     790             :             0x0DDC,             /* SINHALA VOWEL SIGN KOMBUVA HAA AELA-PILLA */
     791             :             0x0DDD,             /* SINHALA VOWEL SIGN KOMBUVA HAA DIGA
     792             :                                  * AELA-PILLA */
     793             :             0x0DDE,             /* SINHALA VOWEL SIGN KOMBUVA HAA GAYANUKITTA */
     794             :             0x0DDF,             /* SINHALA VOWEL SIGN GAYANUKITTA */
     795             :             0x0DF2,             /* SINHALA VOWEL SIGN DIGA GAETTA-PILLA */
     796             :             0x0DF3,             /* SINHALA VOWEL SIGN DIGA GAYANUKITTA */
     797             :             0x0F3E,             /* TIBETAN SIGN YAR TSHES */
     798             :             0x0F3F,             /* TIBETAN SIGN MAR TSHES */
     799             :             0x0F7F,             /* TIBETAN SIGN RNAM BCAD */
     800             :             0x102B,             /* MYANMAR VOWEL SIGN TALL AA */
     801             :             0x102C,             /* MYANMAR VOWEL SIGN AA */
     802             :             0x1031,             /* MYANMAR VOWEL SIGN E */
     803             :             0x1038,             /* MYANMAR SIGN VISARGA */
     804             :             0x103B,             /* MYANMAR CONSONANT SIGN MEDIAL YA */
     805             :             0x103C,             /* MYANMAR CONSONANT SIGN MEDIAL RA */
     806             :             0x1056,             /* MYANMAR VOWEL SIGN VOCALIC R */
     807             :             0x1057,             /* MYANMAR VOWEL SIGN VOCALIC RR */
     808             :             0x1062,             /* MYANMAR VOWEL SIGN SGAW KAREN EU */
     809             :             0x1063,             /* MYANMAR TONE MARK SGAW KAREN HATHI */
     810             :             0x1064,             /* MYANMAR TONE MARK SGAW KAREN KE PHO */
     811             :             0x1067,             /* MYANMAR VOWEL SIGN WESTERN PWO KAREN EU */
     812             :             0x1068,             /* MYANMAR VOWEL SIGN WESTERN PWO KAREN UE */
     813             :             0x1069,             /* MYANMAR SIGN WESTERN PWO KAREN TONE-1 */
     814             :             0x106A,             /* MYANMAR SIGN WESTERN PWO KAREN TONE-2 */
     815             :             0x106B,             /* MYANMAR SIGN WESTERN PWO KAREN TONE-3 */
     816             :             0x106C,             /* MYANMAR SIGN WESTERN PWO KAREN TONE-4 */
     817             :             0x106D,             /* MYANMAR SIGN WESTERN PWO KAREN TONE-5 */
     818             :             0x1083,             /* MYANMAR VOWEL SIGN SHAN AA */
     819             :             0x1084,             /* MYANMAR VOWEL SIGN SHAN E */
     820             :             0x1087,             /* MYANMAR SIGN SHAN TONE-2 */
     821             :             0x1088,             /* MYANMAR SIGN SHAN TONE-3 */
     822             :             0x1089,             /* MYANMAR SIGN SHAN TONE-5 */
     823             :             0x108A,             /* MYANMAR SIGN SHAN TONE-6 */
     824             :             0x108B,             /* MYANMAR SIGN SHAN COUNCIL TONE-2 */
     825             :             0x108C,             /* MYANMAR SIGN SHAN COUNCIL TONE-3 */
     826             :             0x108F,             /* MYANMAR SIGN RUMAI PALAUNG TONE-5 */
     827             :             0x17B6,             /* KHMER VOWEL SIGN AA */
     828             :             0x17BE,             /* KHMER VOWEL SIGN OE */
     829             :             0x17BF,             /* KHMER VOWEL SIGN YA */
     830             :             0x17C0,             /* KHMER VOWEL SIGN IE */
     831             :             0x17C1,             /* KHMER VOWEL SIGN E */
     832             :             0x17C2,             /* KHMER VOWEL SIGN AE */
     833             :             0x17C3,             /* KHMER VOWEL SIGN AI */
     834             :             0x17C4,             /* KHMER VOWEL SIGN OO */
     835             :             0x17C5,             /* KHMER VOWEL SIGN AU */
     836             :             0x17C7,             /* KHMER SIGN REAHMUK */
     837             :             0x17C8,             /* KHMER SIGN YUUKALEAPINTU */
     838             :             0x1923,             /* LIMBU VOWEL SIGN EE */
     839             :             0x1924,             /* LIMBU VOWEL SIGN AI */
     840             :             0x1925,             /* LIMBU VOWEL SIGN OO */
     841             :             0x1926,             /* LIMBU VOWEL SIGN AU */
     842             :             0x1929,             /* LIMBU SUBJOINED LETTER YA */
     843             :             0x192A,             /* LIMBU SUBJOINED LETTER RA */
     844             :             0x192B,             /* LIMBU SUBJOINED LETTER WA */
     845             :             0x1930,             /* LIMBU SMALL LETTER KA */
     846             :             0x1931,             /* LIMBU SMALL LETTER NGA */
     847             :             0x1933,             /* LIMBU SMALL LETTER TA */
     848             :             0x1934,             /* LIMBU SMALL LETTER NA */
     849             :             0x1935,             /* LIMBU SMALL LETTER PA */
     850             :             0x1936,             /* LIMBU SMALL LETTER MA */
     851             :             0x1937,             /* LIMBU SMALL LETTER RA */
     852             :             0x1938,             /* LIMBU SMALL LETTER LA */
     853             :             0x19B0,             /* NEW TAI LUE VOWEL SIGN VOWEL SHORTENER */
     854             :             0x19B1,             /* NEW TAI LUE VOWEL SIGN AA */
     855             :             0x19B2,             /* NEW TAI LUE VOWEL SIGN II */
     856             :             0x19B3,             /* NEW TAI LUE VOWEL SIGN U */
     857             :             0x19B4,             /* NEW TAI LUE VOWEL SIGN UU */
     858             :             0x19B5,             /* NEW TAI LUE VOWEL SIGN E */
     859             :             0x19B6,             /* NEW TAI LUE VOWEL SIGN AE */
     860             :             0x19B7,             /* NEW TAI LUE VOWEL SIGN O */
     861             :             0x19B8,             /* NEW TAI LUE VOWEL SIGN OA */
     862             :             0x19B9,             /* NEW TAI LUE VOWEL SIGN UE */
     863             :             0x19BA,             /* NEW TAI LUE VOWEL SIGN AY */
     864             :             0x19BB,             /* NEW TAI LUE VOWEL SIGN AAY */
     865             :             0x19BC,             /* NEW TAI LUE VOWEL SIGN UY */
     866             :             0x19BD,             /* NEW TAI LUE VOWEL SIGN OY */
     867             :             0x19BE,             /* NEW TAI LUE VOWEL SIGN OAY */
     868             :             0x19BF,             /* NEW TAI LUE VOWEL SIGN UEY */
     869             :             0x19C0,             /* NEW TAI LUE VOWEL SIGN IY */
     870             :             0x19C8,             /* NEW TAI LUE TONE MARK-1 */
     871             :             0x19C9,             /* NEW TAI LUE TONE MARK-2 */
     872             :             0x1A19,             /* BUGINESE VOWEL SIGN E */
     873             :             0x1A1A,             /* BUGINESE VOWEL SIGN O */
     874             :             0x1A1B,             /* BUGINESE VOWEL SIGN AE */
     875             :             0x1B04,             /* BALINESE SIGN BISAH */
     876             :             0x1B35,             /* BALINESE VOWEL SIGN TEDUNG */
     877             :             0x1B3B,             /* BALINESE VOWEL SIGN RA REPA TEDUNG */
     878             :             0x1B3D,             /* BALINESE VOWEL SIGN LA LENGA TEDUNG */
     879             :             0x1B3E,             /* BALINESE VOWEL SIGN TALING */
     880             :             0x1B3F,             /* BALINESE VOWEL SIGN TALING REPA */
     881             :             0x1B40,             /* BALINESE VOWEL SIGN TALING TEDUNG */
     882             :             0x1B41,             /* BALINESE VOWEL SIGN TALING REPA TEDUNG */
     883             :             0x1B43,             /* BALINESE VOWEL SIGN PEPET TEDUNG */
     884             :             0x1B44,             /* BALINESE ADEG ADEG */
     885             :             0x1B82,             /* SUNDANESE SIGN PANGWISAD */
     886             :             0x1BA1,             /* SUNDANESE CONSONANT SIGN PAMINGKAL */
     887             :             0x1BA6,             /* SUNDANESE VOWEL SIGN PANAELAENG */
     888             :             0x1BA7,             /* SUNDANESE VOWEL SIGN PANOLONG */
     889             :             0x1BAA,             /* SUNDANESE SIGN PAMAAEH */
     890             :             0x1C24,             /* LEPCHA SUBJOINED LETTER YA */
     891             :             0x1C25,             /* LEPCHA SUBJOINED LETTER RA */
     892             :             0x1C26,             /* LEPCHA VOWEL SIGN AA */
     893             :             0x1C27,             /* LEPCHA VOWEL SIGN I */
     894             :             0x1C28,             /* LEPCHA VOWEL SIGN O */
     895             :             0x1C29,             /* LEPCHA VOWEL SIGN OO */
     896             :             0x1C2A,             /* LEPCHA VOWEL SIGN U */
     897             :             0x1C2B,             /* LEPCHA VOWEL SIGN UU */
     898             :             0x1C34,             /* LEPCHA CONSONANT SIGN NYIN-DO */
     899             :             0x1C35,             /* LEPCHA CONSONANT SIGN KANG */
     900             :             0xA823,             /* SYLOTI NAGRI VOWEL SIGN A */
     901             :             0xA824,             /* SYLOTI NAGRI VOWEL SIGN I */
     902             :             0xA827,             /* SYLOTI NAGRI VOWEL SIGN OO */
     903             :             0xA880,             /* SAURASHTRA SIGN ANUSVARA */
     904             :             0xA881,             /* SAURASHTRA SIGN VISARGA */
     905             :             0xA8B4,             /* SAURASHTRA CONSONANT SIGN HAARU */
     906             :             0xA8B5,             /* SAURASHTRA VOWEL SIGN AA */
     907             :             0xA8B6,             /* SAURASHTRA VOWEL SIGN I */
     908             :             0xA8B7,             /* SAURASHTRA VOWEL SIGN II */
     909             :             0xA8B8,             /* SAURASHTRA VOWEL SIGN U */
     910             :             0xA8B9,             /* SAURASHTRA VOWEL SIGN UU */
     911             :             0xA8BA,             /* SAURASHTRA VOWEL SIGN VOCALIC R */
     912             :             0xA8BB,             /* SAURASHTRA VOWEL SIGN VOCALIC RR */
     913             :             0xA8BC,             /* SAURASHTRA VOWEL SIGN VOCALIC L */
     914             :             0xA8BD,             /* SAURASHTRA VOWEL SIGN VOCALIC LL */
     915             :             0xA8BE,             /* SAURASHTRA VOWEL SIGN E */
     916             :             0xA8BF,             /* SAURASHTRA VOWEL SIGN EE */
     917             :             0xA8C0,             /* SAURASHTRA VOWEL SIGN AI */
     918             :             0xA8C1,             /* SAURASHTRA VOWEL SIGN O */
     919             :             0xA8C2,             /* SAURASHTRA VOWEL SIGN OO */
     920             :             0xA8C3,             /* SAURASHTRA VOWEL SIGN AU */
     921             :             0xA952,             /* REJANG CONSONANT SIGN H */
     922             :             0xA953,             /* REJANG VIRAMA */
     923             :             0xAA2F,             /* CHAM VOWEL SIGN O */
     924             :             0xAA30,             /* CHAM VOWEL SIGN AI */
     925             :             0xAA33,             /* CHAM CONSONANT SIGN YA */
     926             :             0xAA34,             /* CHAM CONSONANT SIGN RA */
     927             :             0xAA4D              /* CHAM CONSONANT SIGN FINAL H */
     928             :         };
     929        4700 :         const pg_wchar *StopLow = strange_letter,
     930        4700 :                    *StopHigh = strange_letter + lengthof(strange_letter),
     931             :                    *StopMiddle;
     932             :         pg_wchar    c;
     933             : 
     934        4700 :         if (prs->pgwstr)
     935           0 :             c = *(prs->pgwstr + prs->state->poschar);
     936             :         else
     937        4700 :             c = (pg_wchar) *(prs->wstr + prs->state->poschar);
     938             : 
     939       42300 :         while (StopLow < StopHigh)
     940             :         {
     941       37600 :             StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
     942       37600 :             if (*StopMiddle == c)
     943           0 :                 return 1;
     944       37600 :             else if (*StopMiddle < c)
     945           0 :                 StopLow = StopMiddle + 1;
     946             :             else
     947       37600 :                 StopHigh = StopMiddle;
     948             :         }
     949             :     }
     950             : 
     951        4700 :     return 0;
     952             : }
     953             : 
     954             : /*
     955             :  * Table of state/action of parser
     956             :  */
     957             : 
     958             : static const TParserStateActionItem actionTPS_Base[] = {
     959             :     {p_isEOF, 0, A_NEXT, TPS_Null, 0, NULL},
     960             :     {p_iseqC, '<', A_PUSH, TPS_InTagFirst, 0, NULL},
     961             :     {p_isignore, 0, A_NEXT, TPS_InSpace, 0, NULL},
     962             :     {p_isasclet, 0, A_NEXT, TPS_InAsciiWord, 0, NULL},
     963             :     {p_isalpha, 0, A_NEXT, TPS_InWord, 0, NULL},
     964             :     {p_isdigit, 0, A_NEXT, TPS_InUnsignedInt, 0, NULL},
     965             :     {p_iseqC, '-', A_PUSH, TPS_InSignedIntFirst, 0, NULL},
     966             :     {p_iseqC, '+', A_PUSH, TPS_InSignedIntFirst, 0, NULL},
     967             :     {p_iseqC, '&', A_PUSH, TPS_InXMLEntityFirst, 0, NULL},
     968             :     {p_iseqC, '~', A_PUSH, TPS_InFileTwiddle, 0, NULL},
     969             :     {p_iseqC, '/', A_PUSH, TPS_InFileFirst, 0, NULL},
     970             :     {p_iseqC, '.', A_PUSH, TPS_InPathFirstFirst, 0, NULL},
     971             :     {NULL, 0, A_NEXT, TPS_InSpace, 0, NULL}
     972             : };
     973             : 
     974             : 
     975             : static const TParserStateActionItem actionTPS_InNumWord[] = {
     976             :     {p_isEOF, 0, A_BINGO, TPS_Base, NUMWORD, NULL},
     977             :     {p_isalnum, 0, A_NEXT, TPS_InNumWord, 0, NULL},
     978             :     {p_isspecial, 0, A_NEXT, TPS_InNumWord, 0, NULL},
     979             :     {p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
     980             :     {p_iseqC, '/', A_PUSH, TPS_InFileFirst, 0, NULL},
     981             :     {p_iseqC, '.', A_PUSH, TPS_InFileNext, 0, NULL},
     982             :     {p_iseqC, '-', A_PUSH, TPS_InHyphenNumWordFirst, 0, NULL},
     983             :     {NULL, 0, A_BINGO, TPS_Base, NUMWORD, NULL}
     984             : };
     985             : 
     986             : static const TParserStateActionItem actionTPS_InAsciiWord[] = {
     987             :     {p_isEOF, 0, A_BINGO, TPS_Base, ASCIIWORD, NULL},
     988             :     {p_isasclet, 0, A_NEXT, TPS_Null, 0, NULL},
     989             :     {p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
     990             :     {p_iseqC, '.', A_PUSH, TPS_InFileNext, 0, NULL},
     991             :     {p_iseqC, '-', A_PUSH, TPS_InHostFirstAN, 0, NULL},
     992             :     {p_iseqC, '-', A_PUSH, TPS_InHyphenAsciiWordFirst, 0, NULL},
     993             :     {p_iseqC, '_', A_PUSH, TPS_InHostFirstAN, 0, NULL},
     994             :     {p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
     995             :     {p_iseqC, ':', A_PUSH, TPS_InProtocolFirst, 0, NULL},
     996             :     {p_iseqC, '/', A_PUSH, TPS_InFileFirst, 0, NULL},
     997             :     {p_isdigit, 0, A_PUSH, TPS_InHost, 0, NULL},
     998             :     {p_isdigit, 0, A_NEXT, TPS_InNumWord, 0, NULL},
     999             :     {p_isalpha, 0, A_NEXT, TPS_InWord, 0, NULL},
    1000             :     {p_isspecial, 0, A_NEXT, TPS_InWord, 0, NULL},
    1001             :     {NULL, 0, A_BINGO, TPS_Base, ASCIIWORD, NULL}
    1002             : };
    1003             : 
    1004             : static const TParserStateActionItem actionTPS_InWord[] = {
    1005             :     {p_isEOF, 0, A_BINGO, TPS_Base, WORD_T, NULL},
    1006             :     {p_isalpha, 0, A_NEXT, TPS_Null, 0, NULL},
    1007             :     {p_isspecial, 0, A_NEXT, TPS_Null, 0, NULL},
    1008             :     {p_isdigit, 0, A_NEXT, TPS_InNumWord, 0, NULL},
    1009             :     {p_iseqC, '-', A_PUSH, TPS_InHyphenWordFirst, 0, NULL},
    1010             :     {NULL, 0, A_BINGO, TPS_Base, WORD_T, NULL}
    1011             : };
    1012             : 
    1013             : static const TParserStateActionItem actionTPS_InUnsignedInt[] = {
    1014             :     {p_isEOF, 0, A_BINGO, TPS_Base, UNSIGNEDINT, NULL},
    1015             :     {p_isdigit, 0, A_NEXT, TPS_Null, 0, NULL},
    1016             :     {p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
    1017             :     {p_iseqC, '.', A_PUSH, TPS_InUDecimalFirst, 0, NULL},
    1018             :     {p_iseqC, 'e', A_PUSH, TPS_InMantissaFirst, 0, NULL},
    1019             :     {p_iseqC, 'E', A_PUSH, TPS_InMantissaFirst, 0, NULL},
    1020             :     {p_iseqC, '-', A_PUSH, TPS_InHostFirstAN, 0, NULL},
    1021             :     {p_iseqC, '_', A_PUSH, TPS_InHostFirstAN, 0, NULL},
    1022             :     {p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
    1023             :     {p_isasclet, 0, A_PUSH, TPS_InHost, 0, NULL},
    1024             :     {p_isalpha, 0, A_NEXT, TPS_InNumWord, 0, NULL},
    1025             :     {p_isspecial, 0, A_NEXT, TPS_InNumWord, 0, NULL},
    1026             :     {p_iseqC, '/', A_PUSH, TPS_InFileFirst, 0, NULL},
    1027             :     {NULL, 0, A_BINGO, TPS_Base, UNSIGNEDINT, NULL}
    1028             : };
    1029             : 
    1030             : static const TParserStateActionItem actionTPS_InSignedIntFirst[] = {
    1031             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1032             :     {p_isdigit, 0, A_NEXT | A_CLEAR, TPS_InSignedInt, 0, NULL},
    1033             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1034             : };
    1035             : 
    1036             : static const TParserStateActionItem actionTPS_InSignedInt[] = {
    1037             :     {p_isEOF, 0, A_BINGO, TPS_Base, SIGNEDINT, NULL},
    1038             :     {p_isdigit, 0, A_NEXT, TPS_Null, 0, NULL},
    1039             :     {p_iseqC, '.', A_PUSH, TPS_InDecimalFirst, 0, NULL},
    1040             :     {p_iseqC, 'e', A_PUSH, TPS_InMantissaFirst, 0, NULL},
    1041             :     {p_iseqC, 'E', A_PUSH, TPS_InMantissaFirst, 0, NULL},
    1042             :     {NULL, 0, A_BINGO, TPS_Base, SIGNEDINT, NULL}
    1043             : };
    1044             : 
    1045             : static const TParserStateActionItem actionTPS_InSpace[] = {
    1046             :     {p_isEOF, 0, A_BINGO, TPS_Base, SPACE, NULL},
    1047             :     {p_iseqC, '<', A_BINGO, TPS_Base, SPACE, NULL},
    1048             :     {p_isignore, 0, A_NEXT, TPS_Null, 0, NULL},
    1049             :     {p_iseqC, '-', A_BINGO, TPS_Base, SPACE, NULL},
    1050             :     {p_iseqC, '+', A_BINGO, TPS_Base, SPACE, NULL},
    1051             :     {p_iseqC, '&', A_BINGO, TPS_Base, SPACE, NULL},
    1052             :     {p_iseqC, '/', A_BINGO, TPS_Base, SPACE, NULL},
    1053             :     {p_isnotalnum, 0, A_NEXT, TPS_InSpace, 0, NULL},
    1054             :     {NULL, 0, A_BINGO, TPS_Base, SPACE, NULL}
    1055             : };
    1056             : 
    1057             : static const TParserStateActionItem actionTPS_InUDecimalFirst[] = {
    1058             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1059             :     {p_isdigit, 0, A_CLEAR, TPS_InUDecimal, 0, NULL},
    1060             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1061             : };
    1062             : 
    1063             : static const TParserStateActionItem actionTPS_InUDecimal[] = {
    1064             :     {p_isEOF, 0, A_BINGO, TPS_Base, DECIMAL_T, NULL},
    1065             :     {p_isdigit, 0, A_NEXT, TPS_InUDecimal, 0, NULL},
    1066             :     {p_iseqC, '.', A_PUSH, TPS_InVersionFirst, 0, NULL},
    1067             :     {p_iseqC, 'e', A_PUSH, TPS_InMantissaFirst, 0, NULL},
    1068             :     {p_iseqC, 'E', A_PUSH, TPS_InMantissaFirst, 0, NULL},
    1069             :     {NULL, 0, A_BINGO, TPS_Base, DECIMAL_T, NULL}
    1070             : };
    1071             : 
    1072             : static const TParserStateActionItem actionTPS_InDecimalFirst[] = {
    1073             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1074             :     {p_isdigit, 0, A_CLEAR, TPS_InDecimal, 0, NULL},
    1075             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1076             : };
    1077             : 
    1078             : static const TParserStateActionItem actionTPS_InDecimal[] = {
    1079             :     {p_isEOF, 0, A_BINGO, TPS_Base, DECIMAL_T, NULL},
    1080             :     {p_isdigit, 0, A_NEXT, TPS_InDecimal, 0, NULL},
    1081             :     {p_iseqC, '.', A_PUSH, TPS_InVerVersion, 0, NULL},
    1082             :     {p_iseqC, 'e', A_PUSH, TPS_InMantissaFirst, 0, NULL},
    1083             :     {p_iseqC, 'E', A_PUSH, TPS_InMantissaFirst, 0, NULL},
    1084             :     {NULL, 0, A_BINGO, TPS_Base, DECIMAL_T, NULL}
    1085             : };
    1086             : 
    1087             : static const TParserStateActionItem actionTPS_InVerVersion[] = {
    1088             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1089             :     {p_isdigit, 0, A_RERUN, TPS_InSVerVersion, 0, SpecialVerVersion},
    1090             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1091             : };
    1092             : 
    1093             : static const TParserStateActionItem actionTPS_InSVerVersion[] = {
    1094             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1095             :     {p_isdigit, 0, A_BINGO | A_CLRALL, TPS_InUnsignedInt, SPACE, NULL},
    1096             :     {NULL, 0, A_NEXT, TPS_Null, 0, NULL}
    1097             : };
    1098             : 
    1099             : 
    1100             : static const TParserStateActionItem actionTPS_InVersionFirst[] = {
    1101             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1102             :     {p_isdigit, 0, A_CLEAR, TPS_InVersion, 0, NULL},
    1103             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1104             : };
    1105             : 
    1106             : static const TParserStateActionItem actionTPS_InVersion[] = {
    1107             :     {p_isEOF, 0, A_BINGO, TPS_Base, VERSIONNUMBER, NULL},
    1108             :     {p_isdigit, 0, A_NEXT, TPS_InVersion, 0, NULL},
    1109             :     {p_iseqC, '.', A_PUSH, TPS_InVersionFirst, 0, NULL},
    1110             :     {NULL, 0, A_BINGO, TPS_Base, VERSIONNUMBER, NULL}
    1111             : };
    1112             : 
    1113             : static const TParserStateActionItem actionTPS_InMantissaFirst[] = {
    1114             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1115             :     {p_isdigit, 0, A_CLEAR, TPS_InMantissa, 0, NULL},
    1116             :     {p_iseqC, '+', A_NEXT, TPS_InMantissaSign, 0, NULL},
    1117             :     {p_iseqC, '-', A_NEXT, TPS_InMantissaSign, 0, NULL},
    1118             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1119             : };
    1120             : 
    1121             : static const TParserStateActionItem actionTPS_InMantissaSign[] = {
    1122             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1123             :     {p_isdigit, 0, A_CLEAR, TPS_InMantissa, 0, NULL},
    1124             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1125             : };
    1126             : 
    1127             : static const TParserStateActionItem actionTPS_InMantissa[] = {
    1128             :     {p_isEOF, 0, A_BINGO, TPS_Base, SCIENTIFIC, NULL},
    1129             :     {p_isdigit, 0, A_NEXT, TPS_InMantissa, 0, NULL},
    1130             :     {NULL, 0, A_BINGO, TPS_Base, SCIENTIFIC, NULL}
    1131             : };
    1132             : 
    1133             : static const TParserStateActionItem actionTPS_InXMLEntityFirst[] = {
    1134             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1135             :     {p_iseqC, '#', A_NEXT, TPS_InXMLEntityNumFirst, 0, NULL},
    1136             :     {p_isasclet, 0, A_NEXT, TPS_InXMLEntity, 0, NULL},
    1137             :     {p_iseqC, ':', A_NEXT, TPS_InXMLEntity, 0, NULL},
    1138             :     {p_iseqC, '_', A_NEXT, TPS_InXMLEntity, 0, NULL},
    1139             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1140             : };
    1141             : 
    1142             : static const TParserStateActionItem actionTPS_InXMLEntity[] = {
    1143             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1144             :     {p_isalnum, 0, A_NEXT, TPS_InXMLEntity, 0, NULL},
    1145             :     {p_iseqC, ':', A_NEXT, TPS_InXMLEntity, 0, NULL},
    1146             :     {p_iseqC, '_', A_NEXT, TPS_InXMLEntity, 0, NULL},
    1147             :     {p_iseqC, '.', A_NEXT, TPS_InXMLEntity, 0, NULL},
    1148             :     {p_iseqC, '-', A_NEXT, TPS_InXMLEntity, 0, NULL},
    1149             :     {p_iseqC, ';', A_NEXT, TPS_InXMLEntityEnd, 0, NULL},
    1150             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1151             : };
    1152             : 
    1153             : static const TParserStateActionItem actionTPS_InXMLEntityNumFirst[] = {
    1154             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1155             :     {p_iseqC, 'x', A_NEXT, TPS_InXMLEntityHexNumFirst, 0, NULL},
    1156             :     {p_iseqC, 'X', A_NEXT, TPS_InXMLEntityHexNumFirst, 0, NULL},
    1157             :     {p_isdigit, 0, A_NEXT, TPS_InXMLEntityNum, 0, NULL},
    1158             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1159             : };
    1160             : 
    1161             : static const TParserStateActionItem actionTPS_InXMLEntityHexNumFirst[] = {
    1162             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1163             :     {p_isxdigit, 0, A_NEXT, TPS_InXMLEntityHexNum, 0, NULL},
    1164             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1165             : };
    1166             : 
    1167             : static const TParserStateActionItem actionTPS_InXMLEntityNum[] = {
    1168             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1169             :     {p_isdigit, 0, A_NEXT, TPS_InXMLEntityNum, 0, NULL},
    1170             :     {p_iseqC, ';', A_NEXT, TPS_InXMLEntityEnd, 0, NULL},
    1171             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1172             : };
    1173             : 
    1174             : static const TParserStateActionItem actionTPS_InXMLEntityHexNum[] = {
    1175             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1176             :     {p_isxdigit, 0, A_NEXT, TPS_InXMLEntityHexNum, 0, NULL},
    1177             :     {p_iseqC, ';', A_NEXT, TPS_InXMLEntityEnd, 0, NULL},
    1178             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1179             : };
    1180             : 
    1181             : static const TParserStateActionItem actionTPS_InXMLEntityEnd[] = {
    1182             :     {NULL, 0, A_BINGO | A_CLEAR, TPS_Base, XMLENTITY, NULL}
    1183             : };
    1184             : 
    1185             : static const TParserStateActionItem actionTPS_InTagFirst[] = {
    1186             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1187             :     {p_iseqC, '/', A_PUSH, TPS_InTagCloseFirst, 0, NULL},
    1188             :     {p_iseqC, '!', A_PUSH, TPS_InCommentFirst, 0, NULL},
    1189             :     {p_iseqC, '?', A_PUSH, TPS_InXMLBegin, 0, NULL},
    1190             :     {p_isasclet, 0, A_PUSH, TPS_InTagName, 0, NULL},
    1191             :     {p_iseqC, ':', A_PUSH, TPS_InTagName, 0, NULL},
    1192             :     {p_iseqC, '_', A_PUSH, TPS_InTagName, 0, NULL},
    1193             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1194             : };
    1195             : 
    1196             : static const TParserStateActionItem actionTPS_InXMLBegin[] = {
    1197             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1198             :     /* <?xml ... */
    1199             :     /* XXX do we wants states for the m and l ?  Right now this accepts <?xZ */
    1200             :     {p_iseqC, 'x', A_NEXT, TPS_InTag, 0, NULL},
    1201             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1202             : };
    1203             : 
    1204             : static const TParserStateActionItem actionTPS_InTagCloseFirst[] = {
    1205             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1206             :     {p_isasclet, 0, A_NEXT, TPS_InTagName, 0, NULL},
    1207             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1208             : };
    1209             : 
    1210             : static const TParserStateActionItem actionTPS_InTagName[] = {
    1211             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1212             :     /* <br/> case */
    1213             :     {p_iseqC, '/', A_NEXT, TPS_InTagBeginEnd, 0, NULL},
    1214             :     {p_iseqC, '>', A_NEXT, TPS_InTagEnd, 0, SpecialTags},
    1215             :     {p_isspace, 0, A_NEXT, TPS_InTag, 0, SpecialTags},
    1216             :     {p_isalnum, 0, A_NEXT, TPS_Null, 0, NULL},
    1217             :     {p_iseqC, ':', A_NEXT, TPS_Null, 0, NULL},
    1218             :     {p_iseqC, '_', A_NEXT, TPS_Null, 0, NULL},
    1219             :     {p_iseqC, '.', A_NEXT, TPS_Null, 0, NULL},
    1220             :     {p_iseqC, '-', A_NEXT, TPS_Null, 0, NULL},
    1221             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1222             : };
    1223             : 
    1224             : static const TParserStateActionItem actionTPS_InTagBeginEnd[] = {
    1225             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1226             :     {p_iseqC, '>', A_NEXT, TPS_InTagEnd, 0, NULL},
    1227             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1228             : };
    1229             : 
    1230             : static const TParserStateActionItem actionTPS_InTag[] = {
    1231             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1232             :     {p_iseqC, '>', A_NEXT, TPS_InTagEnd, 0, SpecialTags},
    1233             :     {p_iseqC, '\'', A_NEXT, TPS_InTagEscapeK, 0, NULL},
    1234             :     {p_iseqC, '"', A_NEXT, TPS_InTagEscapeKK, 0, NULL},
    1235             :     {p_isasclet, 0, A_NEXT, TPS_Null, 0, NULL},
    1236             :     {p_isdigit, 0, A_NEXT, TPS_Null, 0, NULL},
    1237             :     {p_iseqC, '=', A_NEXT, TPS_Null, 0, NULL},
    1238             :     {p_iseqC, '-', A_NEXT, TPS_Null, 0, NULL},
    1239             :     {p_iseqC, '_', A_NEXT, TPS_Null, 0, NULL},
    1240             :     {p_iseqC, '#', A_NEXT, TPS_Null, 0, NULL},
    1241             :     {p_iseqC, '/', A_NEXT, TPS_Null, 0, NULL},
    1242             :     {p_iseqC, ':', A_NEXT, TPS_Null, 0, NULL},
    1243             :     {p_iseqC, '.', A_NEXT, TPS_Null, 0, NULL},
    1244             :     {p_iseqC, '&', A_NEXT, TPS_Null, 0, NULL},
    1245             :     {p_iseqC, '?', A_NEXT, TPS_Null, 0, NULL},
    1246             :     {p_iseqC, '%', A_NEXT, TPS_Null, 0, NULL},
    1247             :     {p_iseqC, '~', A_NEXT, TPS_Null, 0, NULL},
    1248             :     {p_isspace, 0, A_NEXT, TPS_Null, 0, SpecialTags},
    1249             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1250             : };
    1251             : 
    1252             : static const TParserStateActionItem actionTPS_InTagEscapeK[] = {
    1253             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1254             :     {p_iseqC, '\\', A_PUSH, TPS_InTagBackSleshed, 0, NULL},
    1255             :     {p_iseqC, '\'', A_NEXT, TPS_InTag, 0, NULL},
    1256             :     {NULL, 0, A_NEXT, TPS_InTagEscapeK, 0, NULL}
    1257             : };
    1258             : 
    1259             : static const TParserStateActionItem actionTPS_InTagEscapeKK[] = {
    1260             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1261             :     {p_iseqC, '\\', A_PUSH, TPS_InTagBackSleshed, 0, NULL},
    1262             :     {p_iseqC, '"', A_NEXT, TPS_InTag, 0, NULL},
    1263             :     {NULL, 0, A_NEXT, TPS_InTagEscapeKK, 0, NULL}
    1264             : };
    1265             : 
    1266             : static const TParserStateActionItem actionTPS_InTagBackSleshed[] = {
    1267             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1268             :     {NULL, 0, A_MERGE, TPS_Null, 0, NULL}
    1269             : };
    1270             : 
    1271             : static const TParserStateActionItem actionTPS_InTagEnd[] = {
    1272             :     {NULL, 0, A_BINGO | A_CLRALL, TPS_Base, TAG_T, NULL}
    1273             : };
    1274             : 
    1275             : static const TParserStateActionItem actionTPS_InCommentFirst[] = {
    1276             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1277             :     {p_iseqC, '-', A_NEXT, TPS_InCommentLast, 0, NULL},
    1278             :     /* <!DOCTYPE ...> */
    1279             :     {p_iseqC, 'D', A_NEXT, TPS_InTag, 0, NULL},
    1280             :     {p_iseqC, 'd', A_NEXT, TPS_InTag, 0, NULL},
    1281             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1282             : };
    1283             : 
    1284             : static const TParserStateActionItem actionTPS_InCommentLast[] = {
    1285             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1286             :     {p_iseqC, '-', A_NEXT, TPS_InComment, 0, NULL},
    1287             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1288             : };
    1289             : 
    1290             : static const TParserStateActionItem actionTPS_InComment[] = {
    1291             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1292             :     {p_iseqC, '-', A_NEXT, TPS_InCloseCommentFirst, 0, NULL},
    1293             :     {NULL, 0, A_NEXT, TPS_Null, 0, NULL}
    1294             : };
    1295             : 
    1296             : static const TParserStateActionItem actionTPS_InCloseCommentFirst[] = {
    1297             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1298             :     {p_iseqC, '-', A_NEXT, TPS_InCloseCommentLast, 0, NULL},
    1299             :     {NULL, 0, A_NEXT, TPS_InComment, 0, NULL}
    1300             : };
    1301             : 
    1302             : static const TParserStateActionItem actionTPS_InCloseCommentLast[] = {
    1303             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1304             :     {p_iseqC, '-', A_NEXT, TPS_Null, 0, NULL},
    1305             :     {p_iseqC, '>', A_NEXT, TPS_InCommentEnd, 0, NULL},
    1306             :     {NULL, 0, A_NEXT, TPS_InComment, 0, NULL}
    1307             : };
    1308             : 
    1309             : static const TParserStateActionItem actionTPS_InCommentEnd[] = {
    1310             :     {NULL, 0, A_BINGO | A_CLRALL, TPS_Base, TAG_T, NULL}
    1311             : };
    1312             : 
    1313             : static const TParserStateActionItem actionTPS_InHostFirstDomain[] = {
    1314             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1315             :     {p_isasclet, 0, A_NEXT, TPS_InHostDomainSecond, 0, NULL},
    1316             :     {p_isdigit, 0, A_NEXT, TPS_InHost, 0, NULL},
    1317             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1318             : };
    1319             : 
    1320             : static const TParserStateActionItem actionTPS_InHostDomainSecond[] = {
    1321             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1322             :     {p_isasclet, 0, A_NEXT, TPS_InHostDomain, 0, NULL},
    1323             :     {p_isdigit, 0, A_PUSH, TPS_InHost, 0, NULL},
    1324             :     {p_iseqC, '-', A_PUSH, TPS_InHostFirstAN, 0, NULL},
    1325             :     {p_iseqC, '_', A_PUSH, TPS_InHostFirstAN, 0, NULL},
    1326             :     {p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
    1327             :     {p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
    1328             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1329             : };
    1330             : 
    1331             : static const TParserStateActionItem actionTPS_InHostDomain[] = {
    1332             :     {p_isEOF, 0, A_BINGO | A_CLRALL, TPS_Base, HOST, NULL},
    1333             :     {p_isasclet, 0, A_NEXT, TPS_InHostDomain, 0, NULL},
    1334             :     {p_isdigit, 0, A_PUSH, TPS_InHost, 0, NULL},
    1335             :     {p_iseqC, ':', A_PUSH, TPS_InPortFirst, 0, NULL},
    1336             :     {p_iseqC, '-', A_PUSH, TPS_InHostFirstAN, 0, NULL},
    1337             :     {p_iseqC, '_', A_PUSH, TPS_InHostFirstAN, 0, NULL},
    1338             :     {p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
    1339             :     {p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
    1340             :     {p_isdigit, 0, A_POP, TPS_Null, 0, NULL},
    1341             :     {p_isstophost, 0, A_BINGO | A_CLRALL, TPS_InURLPathStart, HOST, NULL},
    1342             :     {p_iseqC, '/', A_PUSH, TPS_InFURL, 0, NULL},
    1343             :     {NULL, 0, A_BINGO | A_CLRALL, TPS_Base, HOST, NULL}
    1344             : };
    1345             : 
    1346             : static const TParserStateActionItem actionTPS_InPortFirst[] = {
    1347             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1348             :     {p_isdigit, 0, A_NEXT, TPS_InPort, 0, NULL},
    1349             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1350             : };
    1351             : 
    1352             : static const TParserStateActionItem actionTPS_InPort[] = {
    1353             :     {p_isEOF, 0, A_BINGO | A_CLRALL, TPS_Base, HOST, NULL},
    1354             :     {p_isdigit, 0, A_NEXT, TPS_InPort, 0, NULL},
    1355             :     {p_isstophost, 0, A_BINGO | A_CLRALL, TPS_InURLPathStart, HOST, NULL},
    1356             :     {p_iseqC, '/', A_PUSH, TPS_InFURL, 0, NULL},
    1357             :     {NULL, 0, A_BINGO | A_CLRALL, TPS_Base, HOST, NULL}
    1358             : };
    1359             : 
    1360             : static const TParserStateActionItem actionTPS_InHostFirstAN[] = {
    1361             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1362             :     {p_isdigit, 0, A_NEXT, TPS_InHost, 0, NULL},
    1363             :     {p_isasclet, 0, A_NEXT, TPS_InHost, 0, NULL},
    1364             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1365             : };
    1366             : 
    1367             : static const TParserStateActionItem actionTPS_InHost[] = {
    1368             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1369             :     {p_isdigit, 0, A_NEXT, TPS_InHost, 0, NULL},
    1370             :     {p_isasclet, 0, A_NEXT, TPS_InHost, 0, NULL},
    1371             :     {p_iseqC, '@', A_PUSH, TPS_InEmail, 0, NULL},
    1372             :     {p_iseqC, '.', A_PUSH, TPS_InHostFirstDomain, 0, NULL},
    1373             :     {p_iseqC, '-', A_PUSH, TPS_InHostFirstAN, 0, NULL},
    1374             :     {p_iseqC, '_', A_PUSH, TPS_InHostFirstAN, 0, NULL},
    1375             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1376             : };
    1377             : 
    1378             : static const TParserStateActionItem actionTPS_InEmail[] = {
    1379             :     {p_isstophost, 0, A_POP, TPS_Null, 0, NULL},
    1380             :     {p_ishost, 0, A_BINGO | A_CLRALL, TPS_Base, EMAIL, NULL},
    1381             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1382             : };
    1383             : 
    1384             : static const TParserStateActionItem actionTPS_InFileFirst[] = {
    1385             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1386             :     {p_isasclet, 0, A_NEXT, TPS_InFile, 0, NULL},
    1387             :     {p_isdigit, 0, A_NEXT, TPS_InFile, 0, NULL},
    1388             :     {p_iseqC, '.', A_NEXT, TPS_InPathFirst, 0, NULL},
    1389             :     {p_iseqC, '_', A_NEXT, TPS_InFile, 0, NULL},
    1390             :     {p_iseqC, '~', A_PUSH, TPS_InFileTwiddle, 0, NULL},
    1391             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1392             : };
    1393             : 
    1394             : static const TParserStateActionItem actionTPS_InFileTwiddle[] = {
    1395             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1396             :     {p_isasclet, 0, A_NEXT, TPS_InFile, 0, NULL},
    1397             :     {p_isdigit, 0, A_NEXT, TPS_InFile, 0, NULL},
    1398             :     {p_iseqC, '_', A_NEXT, TPS_InFile, 0, NULL},
    1399             :     {p_iseqC, '/', A_NEXT, TPS_InFileFirst, 0, NULL},
    1400             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1401             : };
    1402             : 
    1403             : static const TParserStateActionItem actionTPS_InPathFirst[] = {
    1404             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1405             :     {p_isasclet, 0, A_NEXT, TPS_InFile, 0, NULL},
    1406             :     {p_isdigit, 0, A_NEXT, TPS_InFile, 0, NULL},
    1407             :     {p_iseqC, '_', A_NEXT, TPS_InFile, 0, NULL},
    1408             :     {p_iseqC, '.', A_NEXT, TPS_InPathSecond, 0, NULL},
    1409             :     {p_iseqC, '/', A_NEXT, TPS_InFileFirst, 0, NULL},
    1410             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1411             : };
    1412             : 
    1413             : static const TParserStateActionItem actionTPS_InPathFirstFirst[] = {
    1414             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1415             :     {p_iseqC, '.', A_NEXT, TPS_InPathSecond, 0, NULL},
    1416             :     {p_iseqC, '/', A_NEXT, TPS_InFileFirst, 0, NULL},
    1417             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1418             : };
    1419             : 
    1420             : static const TParserStateActionItem actionTPS_InPathSecond[] = {
    1421             :     {p_isEOF, 0, A_BINGO | A_CLEAR, TPS_Base, FILEPATH, NULL},
    1422             :     {p_iseqC, '/', A_NEXT | A_PUSH, TPS_InFileFirst, 0, NULL},
    1423             :     {p_iseqC, '/', A_BINGO | A_CLEAR, TPS_Base, FILEPATH, NULL},
    1424             :     {p_isspace, 0, A_BINGO | A_CLEAR, TPS_Base, FILEPATH, NULL},
    1425             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1426             : };
    1427             : 
    1428             : static const TParserStateActionItem actionTPS_InFile[] = {
    1429             :     {p_isEOF, 0, A_BINGO, TPS_Base, FILEPATH, NULL},
    1430             :     {p_isasclet, 0, A_NEXT, TPS_InFile, 0, NULL},
    1431             :     {p_isdigit, 0, A_NEXT, TPS_InFile, 0, NULL},
    1432             :     {p_iseqC, '.', A_PUSH, TPS_InFileNext, 0, NULL},
    1433             :     {p_iseqC, '_', A_NEXT, TPS_InFile, 0, NULL},
    1434             :     {p_iseqC, '-', A_NEXT, TPS_InFile, 0, NULL},
    1435             :     {p_iseqC, '/', A_PUSH, TPS_InFileFirst, 0, NULL},
    1436             :     {NULL, 0, A_BINGO, TPS_Base, FILEPATH, NULL}
    1437             : };
    1438             : 
    1439             : static const TParserStateActionItem actionTPS_InFileNext[] = {
    1440             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1441             :     {p_isasclet, 0, A_CLEAR, TPS_InFile, 0, NULL},
    1442             :     {p_isdigit, 0, A_CLEAR, TPS_InFile, 0, NULL},
    1443             :     {p_iseqC, '_', A_CLEAR, TPS_InFile, 0, NULL},
    1444             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1445             : };
    1446             : 
    1447             : static const TParserStateActionItem actionTPS_InURLPathFirst[] = {
    1448             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1449             :     {p_isurlchar, 0, A_NEXT, TPS_InURLPath, 0, NULL},
    1450             :     {NULL, 0, A_POP, TPS_Null, 0, NULL},
    1451             : };
    1452             : 
    1453             : static const TParserStateActionItem actionTPS_InURLPathStart[] = {
    1454             :     {NULL, 0, A_NEXT, TPS_InURLPath, 0, NULL}
    1455             : };
    1456             : 
    1457             : static const TParserStateActionItem actionTPS_InURLPath[] = {
    1458             :     {p_isEOF, 0, A_BINGO, TPS_Base, URLPATH, NULL},
    1459             :     {p_isurlchar, 0, A_NEXT, TPS_InURLPath, 0, NULL},
    1460             :     {NULL, 0, A_BINGO, TPS_Base, URLPATH, NULL}
    1461             : };
    1462             : 
    1463             : static const TParserStateActionItem actionTPS_InFURL[] = {
    1464             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1465             :     {p_isURLPath, 0, A_BINGO | A_CLRALL, TPS_Base, URL_T, SpecialFURL},
    1466             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1467             : };
    1468             : 
    1469             : static const TParserStateActionItem actionTPS_InProtocolFirst[] = {
    1470             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1471             :     {p_iseqC, '/', A_NEXT, TPS_InProtocolSecond, 0, NULL},
    1472             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1473             : };
    1474             : 
    1475             : static const TParserStateActionItem actionTPS_InProtocolSecond[] = {
    1476             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1477             :     {p_iseqC, '/', A_NEXT, TPS_InProtocolEnd, 0, NULL},
    1478             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1479             : };
    1480             : 
    1481             : static const TParserStateActionItem actionTPS_InProtocolEnd[] = {
    1482             :     {NULL, 0, A_BINGO | A_CLRALL, TPS_Base, PROTOCOL, NULL}
    1483             : };
    1484             : 
    1485             : static const TParserStateActionItem actionTPS_InHyphenAsciiWordFirst[] = {
    1486             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1487             :     {p_isasclet, 0, A_NEXT, TPS_InHyphenAsciiWord, 0, NULL},
    1488             :     {p_isalpha, 0, A_NEXT, TPS_InHyphenWord, 0, NULL},
    1489             :     {p_isdigit, 0, A_NEXT, TPS_InHyphenDigitLookahead, 0, NULL},
    1490             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1491             : };
    1492             : 
    1493             : static const TParserStateActionItem actionTPS_InHyphenAsciiWord[] = {
    1494             :     {p_isEOF, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, ASCIIHWORD, SpecialHyphen},
    1495             :     {p_isasclet, 0, A_NEXT, TPS_InHyphenAsciiWord, 0, NULL},
    1496             :     {p_isalpha, 0, A_NEXT, TPS_InHyphenWord, 0, NULL},
    1497             :     {p_isspecial, 0, A_NEXT, TPS_InHyphenWord, 0, NULL},
    1498             :     {p_isdigit, 0, A_NEXT, TPS_InHyphenNumWord, 0, NULL},
    1499             :     {p_iseqC, '-', A_PUSH, TPS_InHyphenAsciiWordFirst, 0, NULL},
    1500             :     {NULL, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, ASCIIHWORD, SpecialHyphen}
    1501             : };
    1502             : 
    1503             : static const TParserStateActionItem actionTPS_InHyphenWordFirst[] = {
    1504             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1505             :     {p_isalpha, 0, A_NEXT, TPS_InHyphenWord, 0, NULL},
    1506             :     {p_isdigit, 0, A_NEXT, TPS_InHyphenDigitLookahead, 0, NULL},
    1507             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1508             : };
    1509             : 
    1510             : static const TParserStateActionItem actionTPS_InHyphenWord[] = {
    1511             :     {p_isEOF, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, HWORD, SpecialHyphen},
    1512             :     {p_isalpha, 0, A_NEXT, TPS_InHyphenWord, 0, NULL},
    1513             :     {p_isspecial, 0, A_NEXT, TPS_InHyphenWord, 0, NULL},
    1514             :     {p_isdigit, 0, A_NEXT, TPS_InHyphenNumWord, 0, NULL},
    1515             :     {p_iseqC, '-', A_PUSH, TPS_InHyphenWordFirst, 0, NULL},
    1516             :     {NULL, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, HWORD, SpecialHyphen}
    1517             : };
    1518             : 
    1519             : static const TParserStateActionItem actionTPS_InHyphenNumWordFirst[] = {
    1520             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1521             :     {p_isalpha, 0, A_NEXT, TPS_InHyphenNumWord, 0, NULL},
    1522             :     {p_isdigit, 0, A_NEXT, TPS_InHyphenDigitLookahead, 0, NULL},
    1523             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1524             : };
    1525             : 
    1526             : static const TParserStateActionItem actionTPS_InHyphenNumWord[] = {
    1527             :     {p_isEOF, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, NUMHWORD, SpecialHyphen},
    1528             :     {p_isalnum, 0, A_NEXT, TPS_InHyphenNumWord, 0, NULL},
    1529             :     {p_isspecial, 0, A_NEXT, TPS_InHyphenNumWord, 0, NULL},
    1530             :     {p_iseqC, '-', A_PUSH, TPS_InHyphenNumWordFirst, 0, NULL},
    1531             :     {NULL, 0, A_BINGO | A_CLRALL, TPS_InParseHyphen, NUMHWORD, SpecialHyphen}
    1532             : };
    1533             : 
    1534             : static const TParserStateActionItem actionTPS_InHyphenDigitLookahead[] = {
    1535             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1536             :     {p_isdigit, 0, A_NEXT, TPS_InHyphenDigitLookahead, 0, NULL},
    1537             :     {p_isalpha, 0, A_NEXT, TPS_InHyphenNumWord, 0, NULL},
    1538             :     {p_isspecial, 0, A_NEXT, TPS_InHyphenNumWord, 0, NULL},
    1539             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1540             : };
    1541             : 
    1542             : static const TParserStateActionItem actionTPS_InParseHyphen[] = {
    1543             :     {p_isEOF, 0, A_RERUN, TPS_Base, 0, NULL},
    1544             :     {p_isasclet, 0, A_NEXT, TPS_InHyphenAsciiWordPart, 0, NULL},
    1545             :     {p_isalpha, 0, A_NEXT, TPS_InHyphenWordPart, 0, NULL},
    1546             :     {p_isdigit, 0, A_PUSH, TPS_InHyphenUnsignedInt, 0, NULL},
    1547             :     {p_iseqC, '-', A_PUSH, TPS_InParseHyphenHyphen, 0, NULL},
    1548             :     {NULL, 0, A_RERUN, TPS_Base, 0, NULL}
    1549             : };
    1550             : 
    1551             : static const TParserStateActionItem actionTPS_InParseHyphenHyphen[] = {
    1552             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1553             :     {p_isalnum, 0, A_BINGO | A_CLEAR, TPS_InParseHyphen, SPACE, NULL},
    1554             :     {p_isspecial, 0, A_BINGO | A_CLEAR, TPS_InParseHyphen, SPACE, NULL},
    1555             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1556             : };
    1557             : 
    1558             : static const TParserStateActionItem actionTPS_InHyphenWordPart[] = {
    1559             :     {p_isEOF, 0, A_BINGO, TPS_Base, PARTHWORD, NULL},
    1560             :     {p_isalpha, 0, A_NEXT, TPS_InHyphenWordPart, 0, NULL},
    1561             :     {p_isspecial, 0, A_NEXT, TPS_InHyphenWordPart, 0, NULL},
    1562             :     {p_isdigit, 0, A_NEXT, TPS_InHyphenNumWordPart, 0, NULL},
    1563             :     {NULL, 0, A_BINGO, TPS_InParseHyphen, PARTHWORD, NULL}
    1564             : };
    1565             : 
    1566             : static const TParserStateActionItem actionTPS_InHyphenAsciiWordPart[] = {
    1567             :     {p_isEOF, 0, A_BINGO, TPS_Base, ASCIIPARTHWORD, NULL},
    1568             :     {p_isasclet, 0, A_NEXT, TPS_InHyphenAsciiWordPart, 0, NULL},
    1569             :     {p_isalpha, 0, A_NEXT, TPS_InHyphenWordPart, 0, NULL},
    1570             :     {p_isspecial, 0, A_NEXT, TPS_InHyphenWordPart, 0, NULL},
    1571             :     {p_isdigit, 0, A_NEXT, TPS_InHyphenNumWordPart, 0, NULL},
    1572             :     {NULL, 0, A_BINGO, TPS_InParseHyphen, ASCIIPARTHWORD, NULL}
    1573             : };
    1574             : 
    1575             : static const TParserStateActionItem actionTPS_InHyphenNumWordPart[] = {
    1576             :     {p_isEOF, 0, A_BINGO, TPS_Base, NUMPARTHWORD, NULL},
    1577             :     {p_isalnum, 0, A_NEXT, TPS_InHyphenNumWordPart, 0, NULL},
    1578             :     {p_isspecial, 0, A_NEXT, TPS_InHyphenNumWordPart, 0, NULL},
    1579             :     {NULL, 0, A_BINGO, TPS_InParseHyphen, NUMPARTHWORD, NULL}
    1580             : };
    1581             : 
    1582             : static const TParserStateActionItem actionTPS_InHyphenUnsignedInt[] = {
    1583             :     {p_isEOF, 0, A_POP, TPS_Null, 0, NULL},
    1584             :     {p_isdigit, 0, A_NEXT, TPS_Null, 0, NULL},
    1585             :     {p_isalpha, 0, A_CLEAR, TPS_InHyphenNumWordPart, 0, NULL},
    1586             :     {p_isspecial, 0, A_CLEAR, TPS_InHyphenNumWordPart, 0, NULL},
    1587             :     {NULL, 0, A_POP, TPS_Null, 0, NULL}
    1588             : };
    1589             : 
    1590             : 
    1591             : /*
    1592             :  * main table of per-state parser actions
    1593             :  */
    1594             : typedef struct
    1595             : {
    1596             :     const TParserStateActionItem *action;   /* the actual state info */
    1597             :     TParserState state;         /* only for Assert crosscheck */
    1598             : #ifdef WPARSER_TRACE
    1599             :     const char *state_name;     /* only for debug printout */
    1600             : #endif
    1601             : } TParserStateAction;
    1602             : 
    1603             : #ifdef WPARSER_TRACE
    1604             : #define TPARSERSTATEACTION(state) \
    1605             :     { CppConcat(action,state), state, CppAsString(state) }
    1606             : #else
    1607             : #define TPARSERSTATEACTION(state) \
    1608             :     { CppConcat(action,state), state }
    1609             : #endif
    1610             : 
    1611             : /*
    1612             :  * order must be the same as in typedef enum {} TParserState!!
    1613             :  */
    1614             : 
    1615             : static const TParserStateAction Actions[] = {
    1616             :     TPARSERSTATEACTION(TPS_Base),
    1617             :     TPARSERSTATEACTION(TPS_InNumWord),
    1618             :     TPARSERSTATEACTION(TPS_InAsciiWord),
    1619             :     TPARSERSTATEACTION(TPS_InWord),
    1620             :     TPARSERSTATEACTION(TPS_InUnsignedInt),
    1621             :     TPARSERSTATEACTION(TPS_InSignedIntFirst),
    1622             :     TPARSERSTATEACTION(TPS_InSignedInt),
    1623             :     TPARSERSTATEACTION(TPS_InSpace),
    1624             :     TPARSERSTATEACTION(TPS_InUDecimalFirst),
    1625             :     TPARSERSTATEACTION(TPS_InUDecimal),
    1626             :     TPARSERSTATEACTION(TPS_InDecimalFirst),
    1627             :     TPARSERSTATEACTION(TPS_InDecimal),
    1628             :     TPARSERSTATEACTION(TPS_InVerVersion),
    1629             :     TPARSERSTATEACTION(TPS_InSVerVersion),
    1630             :     TPARSERSTATEACTION(TPS_InVersionFirst),
    1631             :     TPARSERSTATEACTION(TPS_InVersion),
    1632             :     TPARSERSTATEACTION(TPS_InMantissaFirst),
    1633             :     TPARSERSTATEACTION(TPS_InMantissaSign),
    1634             :     TPARSERSTATEACTION(TPS_InMantissa),
    1635             :     TPARSERSTATEACTION(TPS_InXMLEntityFirst),
    1636             :     TPARSERSTATEACTION(TPS_InXMLEntity),
    1637             :     TPARSERSTATEACTION(TPS_InXMLEntityNumFirst),
    1638             :     TPARSERSTATEACTION(TPS_InXMLEntityNum),
    1639             :     TPARSERSTATEACTION(TPS_InXMLEntityHexNumFirst),
    1640             :     TPARSERSTATEACTION(TPS_InXMLEntityHexNum),
    1641             :     TPARSERSTATEACTION(TPS_InXMLEntityEnd),
    1642             :     TPARSERSTATEACTION(TPS_InTagFirst),
    1643             :     TPARSERSTATEACTION(TPS_InXMLBegin),
    1644             :     TPARSERSTATEACTION(TPS_InTagCloseFirst),
    1645             :     TPARSERSTATEACTION(TPS_InTagName),
    1646             :     TPARSERSTATEACTION(TPS_InTagBeginEnd),
    1647             :     TPARSERSTATEACTION(TPS_InTag),
    1648             :     TPARSERSTATEACTION(TPS_InTagEscapeK),
    1649             :     TPARSERSTATEACTION(TPS_InTagEscapeKK),
    1650             :     TPARSERSTATEACTION(TPS_InTagBackSleshed),
    1651             :     TPARSERSTATEACTION(TPS_InTagEnd),
    1652             :     TPARSERSTATEACTION(TPS_InCommentFirst),
    1653             :     TPARSERSTATEACTION(TPS_InCommentLast),
    1654             :     TPARSERSTATEACTION(TPS_InComment),
    1655             :     TPARSERSTATEACTION(TPS_InCloseCommentFirst),
    1656             :     TPARSERSTATEACTION(TPS_InCloseCommentLast),
    1657             :     TPARSERSTATEACTION(TPS_InCommentEnd),
    1658             :     TPARSERSTATEACTION(TPS_InHostFirstDomain),
    1659             :     TPARSERSTATEACTION(TPS_InHostDomainSecond),
    1660             :     TPARSERSTATEACTION(TPS_InHostDomain),
    1661             :     TPARSERSTATEACTION(TPS_InPortFirst),
    1662             :     TPARSERSTATEACTION(TPS_InPort),
    1663             :     TPARSERSTATEACTION(TPS_InHostFirstAN),
    1664             :     TPARSERSTATEACTION(TPS_InHost),
    1665             :     TPARSERSTATEACTION(TPS_InEmail),
    1666             :     TPARSERSTATEACTION(TPS_InFileFirst),
    1667             :     TPARSERSTATEACTION(TPS_InFileTwiddle),
    1668             :     TPARSERSTATEACTION(TPS_InPathFirst),
    1669             :     TPARSERSTATEACTION(TPS_InPathFirstFirst),
    1670             :     TPARSERSTATEACTION(TPS_InPathSecond),
    1671             :     TPARSERSTATEACTION(TPS_InFile),
    1672             :     TPARSERSTATEACTION(TPS_InFileNext),
    1673             :     TPARSERSTATEACTION(TPS_InURLPathFirst),
    1674             :     TPARSERSTATEACTION(TPS_InURLPathStart),
    1675             :     TPARSERSTATEACTION(TPS_InURLPath),
    1676             :     TPARSERSTATEACTION(TPS_InFURL),
    1677             :     TPARSERSTATEACTION(TPS_InProtocolFirst),
    1678             :     TPARSERSTATEACTION(TPS_InProtocolSecond),
    1679             :     TPARSERSTATEACTION(TPS_InProtocolEnd),
    1680             :     TPARSERSTATEACTION(TPS_InHyphenAsciiWordFirst),
    1681             :     TPARSERSTATEACTION(TPS_InHyphenAsciiWord),
    1682             :     TPARSERSTATEACTION(TPS_InHyphenWordFirst),
    1683             :     TPARSERSTATEACTION(TPS_InHyphenWord),
    1684             :     TPARSERSTATEACTION(TPS_InHyphenNumWordFirst),
    1685             :     TPARSERSTATEACTION(TPS_InHyphenNumWord),
    1686             :     TPARSERSTATEACTION(TPS_InHyphenDigitLookahead),
    1687             :     TPARSERSTATEACTION(TPS_InParseHyphen),
    1688             :     TPARSERSTATEACTION(TPS_InParseHyphenHyphen),
    1689             :     TPARSERSTATEACTION(TPS_InHyphenWordPart),
    1690             :     TPARSERSTATEACTION(TPS_InHyphenAsciiWordPart),
    1691             :     TPARSERSTATEACTION(TPS_InHyphenNumWordPart),
    1692             :     TPARSERSTATEACTION(TPS_InHyphenUnsignedInt)
    1693             : };
    1694             : 
    1695             : 
    1696             : static bool
    1697       16792 : TParserGet(TParser *prs)
    1698             : {
    1699       16792 :     const TParserStateActionItem *item = NULL;
    1700             : 
    1701             :     Assert(prs->state);
    1702             : 
    1703       16792 :     if (prs->state->posbyte >= prs->lenstr)
    1704        3048 :         return false;
    1705             : 
    1706       13744 :     prs->token = prs->str + prs->state->posbyte;
    1707       13744 :     prs->state->pushedAtAction = NULL;
    1708             : 
    1709             :     /* look at string */
    1710       58932 :     while (prs->state->posbyte <= prs->lenstr)
    1711             :     {
    1712       58932 :         if (prs->state->posbyte == prs->lenstr)
    1713        3140 :             prs->state->charlen = 0;
    1714             :         else
    1715      111584 :             prs->state->charlen = (prs->charmaxlen == 1) ? prs->charmaxlen :
    1716       55792 :                 pg_mblen(prs->str + prs->state->posbyte);
    1717             : 
    1718             :         Assert(prs->state->posbyte + prs->state->charlen <= prs->lenstr);
    1719             :         Assert(prs->state->state >= TPS_Base && prs->state->state < TPS_Null);
    1720             :         Assert(Actions[prs->state->state].state == prs->state->state);
    1721             : 
    1722       58932 :         if (prs->state->pushedAtAction)
    1723             :         {
    1724             :             /* After a POP, pick up at the next test */
    1725        1392 :             item = prs->state->pushedAtAction + 1;
    1726        1392 :             prs->state->pushedAtAction = NULL;
    1727             :         }
    1728             :         else
    1729             :         {
    1730       57540 :             item = Actions[prs->state->state].action;
    1731             :             Assert(item != NULL);
    1732             :         }
    1733             : 
    1734             :         /* find action by character class */
    1735      309592 :         while (item->isclass)
    1736             :         {
    1737      292412 :             prs->c = item->c;
    1738      292412 :             if (item->isclass(prs) != 0)
    1739       41752 :                 break;
    1740      250660 :             item++;
    1741             :         }
    1742             : 
    1743             : #ifdef WPARSER_TRACE
    1744             :         {
    1745             :             TParserPosition *ptr;
    1746             : 
    1747             :             fprintf(stderr, "state ");
    1748             :             /* indent according to stack depth */
    1749             :             for (ptr = prs->state->prev; ptr; ptr = ptr->prev)
    1750             :                 fprintf(stderr, "  ");
    1751             :             fprintf(stderr, "%s ", Actions[prs->state->state].state_name);
    1752             :             if (prs->state->posbyte < prs->lenstr)
    1753             :                 fprintf(stderr, "at %c", *(prs->str + prs->state->posbyte));
    1754             :             else
    1755             :                 fprintf(stderr, "at EOF");
    1756             :             fprintf(stderr, " matched rule %d flags%s%s%s%s%s%s%s%s%s%s%s\n",
    1757             :                     (int) (item - Actions[prs->state->state].action),
    1758             :                     (item->flags & A_BINGO) ? " BINGO" : "",
    1759             :                     (item->flags & A_POP) ? " POP" : "",
    1760             :                     (item->flags & A_PUSH) ? " PUSH" : "",
    1761             :                     (item->flags & A_RERUN) ? " RERUN" : "",
    1762             :                     (item->flags & A_CLEAR) ? " CLEAR" : "",
    1763             :                     (item->flags & A_MERGE) ? " MERGE" : "",
    1764             :                     (item->flags & A_CLRALL) ? " CLRALL" : "",
    1765             :                     (item->tostate != TPS_Null) ? " tostate " : "",
    1766             :                     (item->tostate != TPS_Null) ? Actions[item->tostate].state_name : "",
    1767             :                     (item->type > 0) ? " type " : "",
    1768             :                     tok_alias[item->type]);
    1769             :         }
    1770             : #endif
    1771             : 
    1772             :         /* call special handler if exists */
    1773       58932 :         if (item->special)
    1774         280 :             item->special(prs);
    1775             : 
    1776             :         /* BINGO, token is found */
    1777       58932 :         if (item->flags & A_BINGO)
    1778             :         {
    1779             :             Assert(item->type > 0);
    1780       13744 :             prs->lenbytetoken = prs->state->lenbytetoken;
    1781       13744 :             prs->lenchartoken = prs->state->lenchartoken;
    1782       13744 :             prs->state->lenbytetoken = prs->state->lenchartoken = 0;
    1783       13744 :             prs->type = item->type;
    1784             :         }
    1785             : 
    1786             :         /* do various actions by flags */
    1787       58932 :         if (item->flags & A_POP)
    1788             :         {                       /* pop stored state in stack */
    1789        1404 :             TParserPosition *ptr = prs->state->prev;
    1790             : 
    1791        1404 :             pfree(prs->state);
    1792        1404 :             prs->state = ptr;
    1793             :             Assert(prs->state);
    1794             :         }
    1795       57528 :         else if (item->flags & A_PUSH)
    1796             :         {                       /* push (store) state in stack */
    1797        3032 :             prs->state->pushedAtAction = item;    /* remember where we push */
    1798        3032 :             prs->state = newTParserPosition(prs->state);
    1799             :         }
    1800       54496 :         else if (item->flags & A_CLEAR)
    1801             :         {                       /* clear previous pushed state */
    1802             :             TParserPosition *ptr;
    1803             : 
    1804             :             Assert(prs->state->prev);
    1805         308 :             ptr = prs->state->prev->prev;
    1806         308 :             pfree(prs->state->prev);
    1807         308 :             prs->state->prev = ptr;
    1808             :         }
    1809       54188 :         else if (item->flags & A_CLRALL)
    1810             :         {                       /* clear all previous pushed state */
    1811             :             TParserPosition *ptr;
    1812             : 
    1813        1852 :             while (prs->state->prev)
    1814             :             {
    1815        1332 :                 ptr = prs->state->prev->prev;
    1816        1332 :                 pfree(prs->state->prev);
    1817        1332 :                 prs->state->prev = ptr;
    1818             :             }
    1819             :         }
    1820       53668 :         else if (item->flags & A_MERGE)
    1821             :         {                       /* merge posinfo with current and pushed state */
    1822           0 :             TParserPosition *ptr = prs->state;
    1823             : 
    1824             :             Assert(prs->state->prev);
    1825           0 :             prs->state = prs->state->prev;
    1826             : 
    1827           0 :             prs->state->posbyte = ptr->posbyte;
    1828           0 :             prs->state->poschar = ptr->poschar;
    1829           0 :             prs->state->charlen = ptr->charlen;
    1830           0 :             prs->state->lenbytetoken = ptr->lenbytetoken;
    1831           0 :             prs->state->lenchartoken = ptr->lenchartoken;
    1832           0 :             pfree(ptr);
    1833             :         }
    1834             : 
    1835             :         /* set new state if pointed */
    1836       58932 :         if (item->tostate != TPS_Null)
    1837       38356 :             prs->state->state = item->tostate;
    1838             : 
    1839             :         /* check for go away */
    1840       58932 :         if ((item->flags & A_BINGO) ||
    1841       45188 :             (prs->state->posbyte >= prs->lenstr &&
    1842           0 :              (item->flags & A_RERUN) == 0))
    1843             :             break;
    1844             : 
    1845             :         /* go to beginning of loop if we should rerun or we just restore state */
    1846       45188 :         if (item->flags & (A_RERUN | A_POP))
    1847        1420 :             continue;
    1848             : 
    1849             :         /* move forward */
    1850       43768 :         if (prs->state->charlen)
    1851             :         {
    1852       43768 :             prs->state->posbyte += prs->state->charlen;
    1853       43768 :             prs->state->lenbytetoken += prs->state->charlen;
    1854       43768 :             prs->state->poschar++;
    1855       43768 :             prs->state->lenchartoken++;
    1856             :         }
    1857             :     }
    1858             : 
    1859       13744 :     return (item && (item->flags & A_BINGO));
    1860             : }
    1861             : 
    1862             : Datum
    1863       40876 : prsd_lextype(PG_FUNCTION_ARGS)
    1864             : {
    1865       40876 :     LexDescr   *descr = (LexDescr *) palloc(sizeof(LexDescr) * (LASTNUM + 1));
    1866             :     int         i;
    1867             : 
    1868      981024 :     for (i = 1; i <= LASTNUM; i++)
    1869             :     {
    1870      940148 :         descr[i - 1].lexid = i;
    1871      940148 :         descr[i - 1].alias = pstrdup(tok_alias[i]);
    1872      940148 :         descr[i - 1].descr = pstrdup(lex_descr[i]);
    1873             :     }
    1874             : 
    1875       40876 :     descr[LASTNUM].lexid = 0;
    1876             : 
    1877       40876 :     PG_RETURN_POINTER(descr);
    1878             : }
    1879             : 
    1880             : Datum
    1881        3048 : prsd_start(PG_FUNCTION_ARGS)
    1882             : {
    1883        3048 :     PG_RETURN_POINTER(TParserInit((char *) PG_GETARG_POINTER(0), PG_GETARG_INT32(1)));
    1884             : }
    1885             : 
    1886             : Datum
    1887       16632 : prsd_nexttoken(PG_FUNCTION_ARGS)
    1888             : {
    1889       16632 :     TParser    *p = (TParser *) PG_GETARG_POINTER(0);
    1890       16632 :     char      **t = (char **) PG_GETARG_POINTER(1);
    1891       16632 :     int        *tlen = (int *) PG_GETARG_POINTER(2);
    1892             : 
    1893       16632 :     if (!TParserGet(p))
    1894        3048 :         PG_RETURN_INT32(0);
    1895             : 
    1896       13584 :     *t = p->token;
    1897       13584 :     *tlen = p->lenbytetoken;
    1898             : 
    1899       13584 :     PG_RETURN_INT32(p->type);
    1900             : }
    1901             : 
    1902             : Datum
    1903        3048 : prsd_end(PG_FUNCTION_ARGS)
    1904             : {
    1905        3048 :     TParser    *p = (TParser *) PG_GETARG_POINTER(0);
    1906             : 
    1907        3048 :     TParserClose(p);
    1908        3048 :     PG_RETURN_VOID();
    1909             : }
    1910             : 
    1911             : 
    1912             : /*
    1913             :  * ts_headline support begins here
    1914             :  */
    1915             : 
    1916             : /* token type classification macros */
    1917             : #define LEAVETOKEN(x)   ( (x)==SPACE )
    1918             : #define COMPLEXTOKEN(x) ( (x)==URL_T || (x)==NUMHWORD || (x)==ASCIIHWORD || (x)==HWORD )
    1919             : #define ENDPUNCTOKEN(x) ( (x)==SPACE )
    1920             : 
    1921             : #define TS_IDIGNORE(x)  ( (x)==TAG_T || (x)==PROTOCOL || (x)==SPACE || (x)==XMLENTITY )
    1922             : #define HLIDREPLACE(x)  ( (x)==TAG_T )
    1923             : #define HLIDSKIP(x)     ( (x)==URL_T || (x)==NUMHWORD || (x)==ASCIIHWORD || (x)==HWORD )
    1924             : #define XMLHLIDSKIP(x)  ( (x)==URL_T || (x)==NUMHWORD || (x)==ASCIIHWORD || (x)==HWORD )
    1925             : #define NONWORDTOKEN(x) ( (x)==SPACE || HLIDREPLACE(x) || HLIDSKIP(x) )
    1926             : #define NOENDTOKEN(x)   ( NONWORDTOKEN(x) || (x)==SCIENTIFIC || (x)==VERSIONNUMBER || (x)==DECIMAL_T || (x)==SIGNEDINT || (x)==UNSIGNEDINT || TS_IDIGNORE(x) )
    1927             : 
    1928             : /*
    1929             :  * Macros useful in headline selection.  These rely on availability of
    1930             :  * "HeadlineParsedText *prs" describing some text, and "int shortword"
    1931             :  * describing the "short word" length parameter.
    1932             :  */
    1933             : 
    1934             : /* Interesting words are non-repeated search terms */
    1935             : #define INTERESTINGWORD(j) \
    1936             :     (prs->words[j].item && !prs->words[j].repeated)
    1937             : 
    1938             : /* Don't want to end at a non-word or a short word, unless interesting */
    1939             : #define BADENDPOINT(j) \
    1940             :     ((NOENDTOKEN(prs->words[j].type) || prs->words[j].len <= shortword) && \
    1941             :      !INTERESTINGWORD(j))
    1942             : 
    1943             : typedef struct
    1944             : {
    1945             :     /* one cover (well, really one fragment) for mark_hl_fragments */
    1946             :     int32       startpos;       /* fragment's starting word index */
    1947             :     int32       endpos;         /* ending word index (inclusive) */
    1948             :     int32       poslen;         /* number of interesting words */
    1949             :     int32       curlen;         /* total number of words */
    1950             :     bool        chosen;         /* chosen? */
    1951             :     bool        excluded;       /* excluded? */
    1952             : } CoverPos;
    1953             : 
    1954             : typedef struct
    1955             : {
    1956             :     /* callback data for checkcondition_HL */
    1957             :     HeadlineWordEntry *words;
    1958             :     int         len;
    1959             : } hlCheck;
    1960             : 
    1961             : 
    1962             : /*
    1963             :  * TS_execute callback for matching a tsquery operand to headline words
    1964             :  */
    1965             : static TSTernaryValue
    1966         778 : checkcondition_HL(void *opaque, QueryOperand *val, ExecPhraseData *data)
    1967             : {
    1968         778 :     hlCheck    *checkval = (hlCheck *) opaque;
    1969             :     int         i;
    1970             : 
    1971             :     /* scan words array for marching items */
    1972        4746 :     for (i = 0; i < checkval->len; i++)
    1973             :     {
    1974        4154 :         if (checkval->words[i].item == val)
    1975             :         {
    1976             :             /* if data == NULL, don't need to report positions */
    1977         430 :             if (!data)
    1978         186 :                 return TS_YES;
    1979             : 
    1980         244 :             if (!data->pos)
    1981             :             {
    1982         196 :                 data->pos = palloc(sizeof(WordEntryPos) * checkval->len);
    1983         196 :                 data->allocated = true;
    1984         196 :                 data->npos = 1;
    1985         196 :                 data->pos[0] = checkval->words[i].pos;
    1986             :             }
    1987          48 :             else if (data->pos[data->npos - 1] < checkval->words[i].pos)
    1988             :             {
    1989          48 :                 data->pos[data->npos++] = checkval->words[i].pos;
    1990             :             }
    1991             :         }
    1992             :     }
    1993             : 
    1994         592 :     if (data && data->npos > 0)
    1995         196 :         return TS_YES;
    1996             : 
    1997         396 :     return TS_NO;
    1998             : }
    1999             : 
    2000             : /*
    2001             :  * hlFirstIndex: find first index >= pos containing any word used in query
    2002             :  *
    2003             :  * Returns -1 if no such index
    2004             :  */
    2005             : static int
    2006         724 : hlFirstIndex(HeadlineParsedText *prs, int pos)
    2007             : {
    2008             :     int         i;
    2009             : 
    2010        6800 :     for (i = pos; i < prs->curwords; i++)
    2011             :     {
    2012        6538 :         if (prs->words[i].item != NULL)
    2013         462 :             return i;
    2014             :     }
    2015         262 :     return -1;
    2016             : }
    2017             : 
    2018             : /*
    2019             :  * hlCover: try to find a substring of prs' word list that satisfies query
    2020             :  *
    2021             :  * At entry, *p must be the first word index to consider (initialize this
    2022             :  * to zero, or to the next index after a previous successful search).
    2023             :  * We will consider all substrings starting at or after that word, and
    2024             :  * containing no more than max_cover words.  (We need a length limit to
    2025             :  * keep this from taking O(N^2) time for a long document with many query
    2026             :  * words but few complete matches.  Actually, since checkcondition_HL is
    2027             :  * roughly O(N) in the length of the substring being checked, it's even
    2028             :  * worse than that.)
    2029             :  *
    2030             :  * On success, sets *p to first word index and *q to last word index of the
    2031             :  * cover substring, and returns true.
    2032             :  *
    2033             :  * The result is a minimal cover, in the sense that both *p and *q will be
    2034             :  * words used in the query.
    2035             :  */
    2036             : static bool
    2037         296 : hlCover(HeadlineParsedText *prs, TSQuery query, int max_cover,
    2038             :         int *p, int *q)
    2039             : {
    2040             :     int         pmin,
    2041             :                 pmax,
    2042             :                 nextpmin,
    2043             :                 nextpmax;
    2044             :     hlCheck     ch;
    2045             : 
    2046             :     /*
    2047             :      * We look for the earliest, shortest substring of prs->words that
    2048             :      * satisfies the query.  Both the pmin and pmax indices must be words
    2049             :      * appearing in the query; there's no point in trying endpoints in between
    2050             :      * such points.
    2051             :      */
    2052         296 :     pmin = hlFirstIndex(prs, *p);
    2053         492 :     while (pmin >= 0)
    2054             :     {
    2055             :         /* This useless assignment just keeps stupider compilers quiet */
    2056         282 :         nextpmin = -1;
    2057             :         /* Consider substrings starting at pmin */
    2058         282 :         ch.words = &(prs->words[pmin]);
    2059             :         /* Consider the length-one substring first, then longer substrings */
    2060         282 :         pmax = pmin;
    2061             :         do
    2062             :         {
    2063             :             /* Try to match query against pmin .. pmax substring */
    2064         514 :             ch.len = pmax - pmin + 1;
    2065         514 :             if (TS_execute(GETQUERY(query), &ch,
    2066             :                            TS_EXEC_EMPTY, checkcondition_HL))
    2067             :             {
    2068          86 :                 *p = pmin;
    2069          86 :                 *q = pmax;
    2070          86 :                 return true;
    2071             :             }
    2072             :             /* Nope, so advance pmax to next feasible endpoint */
    2073         428 :             nextpmax = hlFirstIndex(prs, pmax + 1);
    2074             : 
    2075             :             /*
    2076             :              * If this is our first advance past pmin, then the result is also
    2077             :              * the next feasible value of pmin; remember it to save a
    2078             :              * redundant search.
    2079             :              */
    2080         428 :             if (pmax == pmin)
    2081         272 :                 nextpmin = nextpmax;
    2082         428 :             pmax = nextpmax;
    2083             :         }
    2084         428 :         while (pmax >= 0 && pmax - pmin < max_cover);
    2085             :         /* No luck here, so try next feasible startpoint */
    2086         196 :         pmin = nextpmin;
    2087             :     }
    2088         210 :     return false;
    2089             : }
    2090             : 
    2091             : /*
    2092             :  * Apply suitable highlight marking to words selected by headline selector
    2093             :  *
    2094             :  * The words from startpos to endpos inclusive are marked per highlightall
    2095             :  */
    2096             : static void
    2097         222 : mark_fragment(HeadlineParsedText *prs, bool highlightall,
    2098             :               int startpos, int endpos)
    2099             : {
    2100             :     int         i;
    2101             : 
    2102        2732 :     for (i = startpos; i <= endpos; i++)
    2103             :     {
    2104        2510 :         if (prs->words[i].item)
    2105         254 :             prs->words[i].selected = 1;
    2106        2510 :         if (!highlightall)
    2107             :         {
    2108        2346 :             if (HLIDREPLACE(prs->words[i].type))
    2109           0 :                 prs->words[i].replace = 1;
    2110        2346 :             else if (HLIDSKIP(prs->words[i].type))
    2111           0 :                 prs->words[i].skip = 1;
    2112             :         }
    2113             :         else
    2114             :         {
    2115         164 :             if (XMLHLIDSKIP(prs->words[i].type))
    2116           4 :                 prs->words[i].skip = 1;
    2117             :         }
    2118             : 
    2119        2510 :         prs->words[i].in = (prs->words[i].repeated) ? 0 : 1;
    2120             :     }
    2121         222 : }
    2122             : 
    2123             : /*
    2124             :  * split a cover substring into fragments not longer than max_words
    2125             :  *
    2126             :  * At entry, *startpos and *endpos are the (remaining) bounds of the cover
    2127             :  * substring.  They are updated to hold the bounds of the next fragment.
    2128             :  *
    2129             :  * *curlen and *poslen are set to the fragment's length, in words and
    2130             :  * interesting words respectively.
    2131             :  */
    2132             : static void
    2133          24 : get_next_fragment(HeadlineParsedText *prs, int *startpos, int *endpos,
    2134             :                   int *curlen, int *poslen, int max_words)
    2135             : {
    2136             :     int         i;
    2137             : 
    2138             :     /*
    2139             :      * Objective: select a fragment of words between startpos and endpos such
    2140             :      * that it has at most max_words and both ends have query words. If the
    2141             :      * startpos and endpos are the endpoints of the cover and the cover has
    2142             :      * fewer words than max_words, then this function should just return the
    2143             :      * cover
    2144             :      */
    2145             :     /* first move startpos to an item */
    2146         592 :     for (i = *startpos; i <= *endpos; i++)
    2147             :     {
    2148         592 :         *startpos = i;
    2149         592 :         if (INTERESTINGWORD(i))
    2150          24 :             break;
    2151             :     }
    2152             :     /* cut endpos to have only max_words */
    2153          24 :     *curlen = 0;
    2154          24 :     *poslen = 0;
    2155         640 :     for (i = *startpos; i <= *endpos && *curlen < max_words; i++)
    2156             :     {
    2157         616 :         if (!NONWORDTOKEN(prs->words[i].type))
    2158         320 :             *curlen += 1;
    2159         616 :         if (INTERESTINGWORD(i))
    2160          36 :             *poslen += 1;
    2161             :     }
    2162             :     /* if the cover was cut then move back endpos to a query item */
    2163          24 :     if (*endpos > i)
    2164             :     {
    2165           8 :         *endpos = i;
    2166         560 :         for (i = *endpos; i >= *startpos; i--)
    2167             :         {
    2168         560 :             *endpos = i;
    2169         560 :             if (INTERESTINGWORD(i))
    2170           8 :                 break;
    2171         552 :             if (!NONWORDTOKEN(prs->words[i].type))
    2172         272 :                 *curlen -= 1;
    2173             :         }
    2174             :     }
    2175          24 : }
    2176             : 
    2177             : /*
    2178             :  * Headline selector used when MaxFragments > 0
    2179             :  *
    2180             :  * Note: in this mode, highlightall is disregarded for phrase selection;
    2181             :  * it only controls presentation details.
    2182             :  */
    2183             : static void
    2184          20 : mark_hl_fragments(HeadlineParsedText *prs, TSQuery query, bool highlightall,
    2185             :                   int shortword, int min_words,
    2186             :                   int max_words, int max_fragments, int max_cover)
    2187             : {
    2188             :     int32       poslen,
    2189             :                 curlen,
    2190             :                 i,
    2191             :                 f,
    2192          20 :                 num_f = 0;
    2193             :     int32       stretch,
    2194             :                 maxstretch,
    2195             :                 posmarker;
    2196             : 
    2197          20 :     int32       startpos = 0,
    2198          20 :                 endpos = 0,
    2199          20 :                 p = 0,
    2200          20 :                 q = 0;
    2201             : 
    2202          20 :     int32       numcovers = 0,
    2203          20 :                 maxcovers = 32;
    2204             : 
    2205             :     int32       minI,
    2206             :                 minwords,
    2207             :                 maxitems;
    2208             :     CoverPos   *covers;
    2209             : 
    2210          20 :     covers = palloc(maxcovers * sizeof(CoverPos));
    2211             : 
    2212             :     /* get all covers */
    2213          36 :     while (hlCover(prs, query, max_cover, &p, &q))
    2214             :     {
    2215          16 :         startpos = p;
    2216          16 :         endpos = q;
    2217             : 
    2218             :         /*
    2219             :          * Break the cover into smaller fragments such that each fragment has
    2220             :          * at most max_words. Also ensure that each end of each fragment is a
    2221             :          * query word. This will allow us to stretch the fragment in either
    2222             :          * direction
    2223             :          */
    2224             : 
    2225          40 :         while (startpos <= endpos)
    2226             :         {
    2227          24 :             get_next_fragment(prs, &startpos, &endpos, &curlen, &poslen, max_words);
    2228          24 :             if (numcovers >= maxcovers)
    2229             :             {
    2230           0 :                 maxcovers *= 2;
    2231           0 :                 covers = repalloc(covers, sizeof(CoverPos) * maxcovers);
    2232             :             }
    2233          24 :             covers[numcovers].startpos = startpos;
    2234          24 :             covers[numcovers].endpos = endpos;
    2235          24 :             covers[numcovers].curlen = curlen;
    2236          24 :             covers[numcovers].poslen = poslen;
    2237          24 :             covers[numcovers].chosen = false;
    2238          24 :             covers[numcovers].excluded = false;
    2239          24 :             numcovers++;
    2240          24 :             startpos = endpos + 1;
    2241          24 :             endpos = q;
    2242             :         }
    2243             : 
    2244             :         /* move p to generate the next cover */
    2245          16 :         p++;
    2246             :     }
    2247             : 
    2248             :     /* choose best covers */
    2249          44 :     for (f = 0; f < max_fragments; f++)
    2250             :     {
    2251          32 :         maxitems = 0;
    2252          32 :         minwords = PG_INT32_MAX;
    2253          32 :         minI = -1;
    2254             : 
    2255             :         /*
    2256             :          * Choose the cover that contains max items. In case of tie choose the
    2257             :          * one with smaller number of words.
    2258             :          */
    2259          76 :         for (i = 0; i < numcovers; i++)
    2260             :         {
    2261          44 :             if (!covers[i].chosen && !covers[i].excluded &&
    2262          32 :                 (maxitems < covers[i].poslen ||
    2263           8 :                  (maxitems == covers[i].poslen &&
    2264           8 :                   minwords > covers[i].curlen)))
    2265             :             {
    2266          24 :                 maxitems = covers[i].poslen;
    2267          24 :                 minwords = covers[i].curlen;
    2268          24 :                 minI = i;
    2269             :             }
    2270             :         }
    2271             :         /* if a cover was found mark it */
    2272          32 :         if (minI >= 0)
    2273             :         {
    2274          24 :             covers[minI].chosen = true;
    2275             :             /* adjust the size of cover */
    2276          24 :             startpos = covers[minI].startpos;
    2277          24 :             endpos = covers[minI].endpos;
    2278          24 :             curlen = covers[minI].curlen;
    2279             :             /* stretch the cover if cover size is lower than max_words */
    2280          24 :             if (curlen < max_words)
    2281             :             {
    2282             :                 /* divide the stretch on both sides of cover */
    2283          24 :                 maxstretch = (max_words - curlen) / 2;
    2284             : 
    2285             :                 /*
    2286             :                  * first stretch the startpos stop stretching if 1. we hit the
    2287             :                  * beginning of document 2. exceed maxstretch 3. we hit an
    2288             :                  * already marked fragment
    2289             :                  */
    2290          24 :                 stretch = 0;
    2291          24 :                 posmarker = startpos;
    2292         400 :                 for (i = startpos - 1; i >= 0 && stretch < maxstretch && !prs->words[i].in; i--)
    2293             :                 {
    2294         376 :                     if (!NONWORDTOKEN(prs->words[i].type))
    2295             :                     {
    2296         180 :                         curlen++;
    2297         180 :                         stretch++;
    2298             :                     }
    2299         376 :                     posmarker = i;
    2300             :                 }
    2301             :                 /* cut back startpos till we find a good endpoint */
    2302          88 :                 for (i = posmarker; i < startpos && BADENDPOINT(i); i++)
    2303             :                 {
    2304          64 :                     if (!NONWORDTOKEN(prs->words[i].type))
    2305          24 :                         curlen--;
    2306             :                 }
    2307          24 :                 startpos = i;
    2308             :                 /* now stretch the endpos as much as possible */
    2309          24 :                 posmarker = endpos;
    2310         644 :                 for (i = endpos + 1; i < prs->curwords && curlen < max_words && !prs->words[i].in; i++)
    2311             :                 {
    2312         620 :                     if (!NONWORDTOKEN(prs->words[i].type))
    2313         308 :                         curlen++;
    2314         620 :                     posmarker = i;
    2315             :                 }
    2316             :                 /* cut back endpos till we find a good endpoint */
    2317          60 :                 for (i = posmarker; i > endpos && BADENDPOINT(i); i--)
    2318             :                 {
    2319          36 :                     if (!NONWORDTOKEN(prs->words[i].type))
    2320          16 :                         curlen--;
    2321             :                 }
    2322          24 :                 endpos = i;
    2323             :             }
    2324          24 :             covers[minI].startpos = startpos;
    2325          24 :             covers[minI].endpos = endpos;
    2326          24 :             covers[minI].curlen = curlen;
    2327             :             /* Mark the chosen fragments (covers) */
    2328          24 :             mark_fragment(prs, highlightall, startpos, endpos);
    2329          24 :             num_f++;
    2330             :             /* Exclude covers overlapping this one from future consideration */
    2331          64 :             for (i = 0; i < numcovers; i++)
    2332             :             {
    2333          40 :                 if (i != minI &&
    2334          16 :                     ((covers[i].startpos >= startpos &&
    2335           8 :                       covers[i].startpos <= endpos) ||
    2336          16 :                      (covers[i].endpos >= startpos &&
    2337           8 :                       covers[i].endpos <= endpos) ||
    2338          16 :                      (covers[i].startpos < startpos &&
    2339           8 :                       covers[i].endpos > endpos)))
    2340           0 :                     covers[i].excluded = true;
    2341             :             }
    2342             :         }
    2343             :         else
    2344           8 :             break;              /* no selectable covers remain */
    2345             :     }
    2346             : 
    2347             :     /* show the first min_words words if we have not marked anything */
    2348          20 :     if (num_f <= 0)
    2349             :     {
    2350           4 :         startpos = endpos = curlen = 0;
    2351         124 :         for (i = 0; i < prs->curwords && curlen < min_words; i++)
    2352             :         {
    2353         120 :             if (!NONWORDTOKEN(prs->words[i].type))
    2354          60 :                 curlen++;
    2355         120 :             endpos = i;
    2356             :         }
    2357           4 :         mark_fragment(prs, highlightall, startpos, endpos);
    2358             :     }
    2359             : 
    2360          20 :     pfree(covers);
    2361          20 : }
    2362             : 
    2363             : /*
    2364             :  * Headline selector used when MaxFragments == 0
    2365             :  */
    2366             : static void
    2367         194 : mark_hl_words(HeadlineParsedText *prs, TSQuery query, bool highlightall,
    2368             :               int shortword, int min_words, int max_words, int max_cover)
    2369             : {
    2370         194 :     int         p = 0,
    2371         194 :                 q = 0;
    2372         194 :     int         bestb = -1,
    2373         194 :                 beste = -1;
    2374         194 :     int         bestlen = -1;
    2375         194 :     bool        bestcover = false;
    2376             :     int         pose,
    2377             :                 posb,
    2378             :                 poslen,
    2379             :                 curlen;
    2380             :     bool        poscover;
    2381             :     int         i;
    2382             : 
    2383         194 :     if (!highlightall)
    2384             :     {
    2385             :         /* examine all covers, select a headline using the best one */
    2386         260 :         while (hlCover(prs, query, max_cover, &p, &q))
    2387             :         {
    2388             :             /*
    2389             :              * Count words (curlen) and interesting words (poslen) within
    2390             :              * cover, but stop once we reach max_words.  This step doesn't
    2391             :              * consider whether that's a good stopping point.  posb and pose
    2392             :              * are set to the start and end indexes of the possible headline.
    2393             :              */
    2394          70 :             curlen = 0;
    2395          70 :             poslen = 0;
    2396          70 :             posb = pose = p;
    2397         540 :             for (i = p; i <= q && curlen < max_words; i++)
    2398             :             {
    2399         470 :                 if (!NONWORDTOKEN(prs->words[i].type))
    2400         270 :                     curlen++;
    2401         470 :                 if (INTERESTINGWORD(i))
    2402         162 :                     poslen++;
    2403         470 :                 pose = i;
    2404             :             }
    2405             : 
    2406          70 :             if (curlen < max_words)
    2407             :             {
    2408             :                 /*
    2409             :                  * We have room to lengthen the headline, so search forward
    2410             :                  * until it's full or we find a good stopping point.  We'll
    2411             :                  * reconsider the word at "q", then move forward.
    2412             :                  */
    2413         648 :                 for (i = i - 1; i < prs->curwords && curlen < max_words; i++)
    2414             :                 {
    2415         646 :                     if (i > q)
    2416             :                     {
    2417         592 :                         if (!NONWORDTOKEN(prs->words[i].type))
    2418         296 :                             curlen++;
    2419         592 :                         if (INTERESTINGWORD(i))
    2420          24 :                             poslen++;
    2421             :                     }
    2422         646 :                     pose = i;
    2423         646 :                     if (BADENDPOINT(i))
    2424         398 :                         continue;
    2425         248 :                     if (curlen >= min_words)
    2426          52 :                         break;
    2427             :                 }
    2428          54 :                 if (curlen < min_words)
    2429             :                 {
    2430             :                     /*
    2431             :                      * Reached end of text and our headline is still shorter
    2432             :                      * than min_words, so try to extend it to the left.
    2433             :                      */
    2434           6 :                     for (i = p - 1; i >= 0; i--)
    2435             :                     {
    2436           4 :                         if (!NONWORDTOKEN(prs->words[i].type))
    2437           2 :                             curlen++;
    2438           4 :                         if (INTERESTINGWORD(i))
    2439           0 :                             poslen++;
    2440           4 :                         if (curlen >= max_words)
    2441           0 :                             break;
    2442           4 :                         if (BADENDPOINT(i))
    2443           2 :                             continue;
    2444           2 :                         if (curlen >= min_words)
    2445           0 :                             break;
    2446             :                     }
    2447           2 :                     posb = (i >= 0) ? i : 0;
    2448             :                 }
    2449             :             }
    2450             :             else
    2451             :             {
    2452             :                 /*
    2453             :                  * Can't make headline longer, so consider making it shorter
    2454             :                  * if needed to avoid a bad endpoint.
    2455             :                  */
    2456          16 :                 if (i > q)
    2457           4 :                     i = q;
    2458          32 :                 for (; curlen > min_words; i--)
    2459             :                 {
    2460          28 :                     if (!BADENDPOINT(i))
    2461             :                         break;
    2462          16 :                     if (!NONWORDTOKEN(prs->words[i].type))
    2463           4 :                         curlen--;
    2464          16 :                     if (INTERESTINGWORD(i))
    2465           0 :                         poslen--;
    2466          16 :                     pose = i - 1;
    2467             :                 }
    2468             :             }
    2469             : 
    2470             :             /*
    2471             :              * Check whether the proposed headline includes the original
    2472             :              * cover; it might not if we trimmed it due to max_words.
    2473             :              */
    2474          70 :             poscover = (posb <= p && pose >= q);
    2475             : 
    2476             :             /*
    2477             :              * Adopt this headline if it's better than the last one, giving
    2478             :              * highest priority to headlines including the cover, then to
    2479             :              * headlines with more interesting words, then to headlines with
    2480             :              * good stopping points.  (Since bestlen is initially -1, we will
    2481             :              * certainly adopt the first headline.)
    2482             :              */
    2483          70 :             if (poscover > bestcover ||
    2484          32 :                 (poscover == bestcover && poslen > bestlen) ||
    2485          16 :                 (poscover == bestcover && poslen == bestlen &&
    2486           8 :                  !BADENDPOINT(pose) && BADENDPOINT(beste)))
    2487             :             {
    2488          54 :                 bestb = posb;
    2489          54 :                 beste = pose;
    2490          54 :                 bestlen = poslen;
    2491          54 :                 bestcover = poscover;
    2492             :             }
    2493             : 
    2494             :             /* move p to generate the next cover */
    2495          70 :             p++;
    2496             :         }
    2497             : 
    2498             :         /*
    2499             :          * If we found nothing acceptable, select min_words words starting at
    2500             :          * the beginning.
    2501             :          */
    2502         190 :         if (bestlen < 0)
    2503             :         {
    2504         152 :             curlen = 0;
    2505         152 :             pose = 0;
    2506         672 :             for (i = 0; i < prs->curwords && curlen < min_words; i++)
    2507             :             {
    2508         520 :                 if (!NONWORDTOKEN(prs->words[i].type))
    2509         336 :                     curlen++;
    2510         520 :                 pose = i;
    2511             :             }
    2512         152 :             bestb = 0;
    2513         152 :             beste = pose;
    2514             :         }
    2515             :     }
    2516             :     else
    2517             :     {
    2518             :         /* highlightall mode: headline is whole document */
    2519           4 :         bestb = 0;
    2520           4 :         beste = prs->curwords - 1;
    2521             :     }
    2522             : 
    2523         194 :     mark_fragment(prs, highlightall, bestb, beste);
    2524         194 : }
    2525             : 
    2526             : /*
    2527             :  * Default parser's prsheadline function
    2528             :  */
    2529             : Datum
    2530         214 : prsd_headline(PG_FUNCTION_ARGS)
    2531             : {
    2532         214 :     HeadlineParsedText *prs = (HeadlineParsedText *) PG_GETARG_POINTER(0);
    2533         214 :     List       *prsoptions = (List *) PG_GETARG_POINTER(1);
    2534         214 :     TSQuery     query = PG_GETARG_TSQUERY(2);
    2535             : 
    2536             :     /* default option values: */
    2537         214 :     int         min_words = 15;
    2538         214 :     int         max_words = 35;
    2539         214 :     int         shortword = 3;
    2540         214 :     int         max_fragments = 0;
    2541         214 :     bool        highlightall = false;
    2542             :     int         max_cover;
    2543             :     ListCell   *l;
    2544             : 
    2545             :     /* Extract configuration option values */
    2546         214 :     prs->startsel = NULL;
    2547         214 :     prs->stopsel = NULL;
    2548         214 :     prs->fragdelim = NULL;
    2549         442 :     foreach(l, prsoptions)
    2550             :     {
    2551         228 :         DefElem    *defel = (DefElem *) lfirst(l);
    2552         228 :         char       *val = defGetString(defel);
    2553             : 
    2554         228 :         if (pg_strcasecmp(defel->defname, "MaxWords") == 0)
    2555          20 :             max_words = pg_strtoint32(val);
    2556         208 :         else if (pg_strcasecmp(defel->defname, "MinWords") == 0)
    2557          20 :             min_words = pg_strtoint32(val);
    2558         188 :         else if (pg_strcasecmp(defel->defname, "ShortWord") == 0)
    2559           0 :             shortword = pg_strtoint32(val);
    2560         188 :         else if (pg_strcasecmp(defel->defname, "MaxFragments") == 0)
    2561          20 :             max_fragments = pg_strtoint32(val);
    2562         168 :         else if (pg_strcasecmp(defel->defname, "StartSel") == 0)
    2563          80 :             prs->startsel = pstrdup(val);
    2564          88 :         else if (pg_strcasecmp(defel->defname, "StopSel") == 0)
    2565          80 :             prs->stopsel = pstrdup(val);
    2566           8 :         else if (pg_strcasecmp(defel->defname, "FragmentDelimiter") == 0)
    2567           4 :             prs->fragdelim = pstrdup(val);
    2568           4 :         else if (pg_strcasecmp(defel->defname, "HighlightAll") == 0)
    2569          12 :             highlightall = (pg_strcasecmp(val, "1") == 0 ||
    2570           8 :                             pg_strcasecmp(val, "on") == 0 ||
    2571           4 :                             pg_strcasecmp(val, "true") == 0 ||
    2572           0 :                             pg_strcasecmp(val, "t") == 0 ||
    2573           8 :                             pg_strcasecmp(val, "y") == 0 ||
    2574           0 :                             pg_strcasecmp(val, "yes") == 0);
    2575             :         else
    2576           0 :             ereport(ERROR,
    2577             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2578             :                      errmsg("unrecognized headline parameter: \"%s\"",
    2579             :                             defel->defname)));
    2580             :     }
    2581             : 
    2582             :     /*
    2583             :      * We might eventually make max_cover a user-settable parameter, but for
    2584             :      * now, just compute a reasonable value based on max_words and
    2585             :      * max_fragments.
    2586             :      */
    2587         214 :     max_cover = Max(max_words * 10, 100);
    2588         214 :     if (max_fragments > 0)
    2589          20 :         max_cover *= max_fragments;
    2590             : 
    2591             :     /* in HighlightAll mode these parameters are ignored */
    2592         214 :     if (!highlightall)
    2593             :     {
    2594         210 :         if (min_words >= max_words)
    2595           0 :             ereport(ERROR,
    2596             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2597             :                      errmsg("MinWords should be less than MaxWords")));
    2598         210 :         if (min_words <= 0)
    2599           0 :             ereport(ERROR,
    2600             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2601             :                      errmsg("MinWords should be positive")));
    2602         210 :         if (shortword < 0)
    2603           0 :             ereport(ERROR,
    2604             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2605             :                      errmsg("ShortWord should be >= 0")));
    2606         210 :         if (max_fragments < 0)
    2607           0 :             ereport(ERROR,
    2608             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2609             :                      errmsg("MaxFragments should be >= 0")));
    2610             :     }
    2611             : 
    2612             :     /* Apply appropriate headline selector */
    2613         214 :     if (max_fragments == 0)
    2614         194 :         mark_hl_words(prs, query, highlightall, shortword,
    2615             :                       min_words, max_words, max_cover);
    2616             :     else
    2617          20 :         mark_hl_fragments(prs, query, highlightall, shortword,
    2618             :                           min_words, max_words, max_fragments, max_cover);
    2619             : 
    2620             :     /* Fill in default values for string options */
    2621         214 :     if (!prs->startsel)
    2622         134 :         prs->startsel = pstrdup("<b>");
    2623         214 :     if (!prs->stopsel)
    2624         134 :         prs->stopsel = pstrdup("</b>");
    2625         214 :     if (!prs->fragdelim)
    2626         210 :         prs->fragdelim = pstrdup(" ... ");
    2627             : 
    2628             :     /* Caller will need these lengths, too */
    2629         214 :     prs->startsellen = strlen(prs->startsel);
    2630         214 :     prs->stopsellen = strlen(prs->stopsel);
    2631         214 :     prs->fragdelimlen = strlen(prs->fragdelim);
    2632             : 
    2633         214 :     PG_RETURN_POINTER(prs);
    2634             : }

Generated by: LCOV version 1.14