1 /*-------------------------------------------------------------------------
4 * Simple example of a text search parser
6 * Copyright (c) 2007-2014, PostgreSQL Global Development Group
9 * contrib/test_parser/test_parser.c
11 *-------------------------------------------------------------------------
23 /* self-defined type */
26 char *buffer; /* text to parse */
27 int len; /* length of the text in buffer */
28 int pos; /* position of the parser */
31 /* copy-paste from wparser.h of tsearch2 */
42 PG_FUNCTION_INFO_V1(testprs_start);
43 PG_FUNCTION_INFO_V1(testprs_getlexeme);
44 PG_FUNCTION_INFO_V1(testprs_end);
45 PG_FUNCTION_INFO_V1(testprs_lextype);
48 testprs_start(PG_FUNCTION_ARGS)
50 ParserState *pst = (ParserState *) palloc0(sizeof(ParserState));
52 pst->buffer = (char *) PG_GETARG_POINTER(0);
53 pst->len = PG_GETARG_INT32(1);
56 PG_RETURN_POINTER(pst);
60 testprs_getlexeme(PG_FUNCTION_ARGS)
62 ParserState *pst = (ParserState *) PG_GETARG_POINTER(0);
63 char **t = (char **) PG_GETARG_POINTER(1);
64 int *tlen = (int *) PG_GETARG_POINTER(2);
65 int startpos = pst->pos;
68 *t = pst->buffer + pst->pos;
70 if (pst->pos < pst->len &&
71 (pst->buffer)[pst->pos] == ' ')
75 /* go to the next non-space character */
76 while (pst->pos < pst->len &&
77 (pst->buffer)[pst->pos] == ' ')
84 /* go to the next space character */
85 while (pst->pos < pst->len &&
86 (pst->buffer)[pst->pos] != ' ')
90 *tlen = pst->pos - startpos;
92 /* we are finished if (*tlen == 0) */
96 PG_RETURN_INT32(type);
100 testprs_end(PG_FUNCTION_ARGS)
102 ParserState *pst = (ParserState *) PG_GETARG_POINTER(0);
109 testprs_lextype(PG_FUNCTION_ARGS)
112 * Remarks: - we have to return the blanks for headline reason - we use
113 * the same lexids like Teodor in the default word parser; in this way we
114 * can reuse the headline function of the default word parser.
116 LexDescr *descr = (LexDescr *) palloc(sizeof(LexDescr) * (2 + 1));
118 /* there are only two types in this parser */
120 descr[0].alias = pstrdup("word");
121 descr[0].descr = pstrdup("Word");
123 descr[1].alias = pstrdup("blank");
124 descr[1].descr = pstrdup("Space symbols");
127 PG_RETURN_POINTER(descr);