1 /*-------------------------------------------------------------------------
4 * Simple example of a text search parser
6 * Copyright (c) 2007-2010, PostgreSQL Global Development Group
9 * $PostgreSQL: pgsql/contrib/test_parser/test_parser.c,v 1.7 2010/01/02 16:57:32 momjian Exp $
11 *-------------------------------------------------------------------------
24 /* self-defined type */
27 char *buffer; /* text to parse */
28 int len; /* length of the text in buffer */
29 int pos; /* position of the parser */
32 /* copy-paste from wparser.h of tsearch2 */
43 PG_FUNCTION_INFO_V1(testprs_start);
44 Datum testprs_start(PG_FUNCTION_ARGS);
46 PG_FUNCTION_INFO_V1(testprs_getlexeme);
47 Datum testprs_getlexeme(PG_FUNCTION_ARGS);
49 PG_FUNCTION_INFO_V1(testprs_end);
50 Datum testprs_end(PG_FUNCTION_ARGS);
52 PG_FUNCTION_INFO_V1(testprs_lextype);
53 Datum testprs_lextype(PG_FUNCTION_ARGS);
59 testprs_start(PG_FUNCTION_ARGS)
61 ParserState *pst = (ParserState *) palloc0(sizeof(ParserState));
63 pst->buffer = (char *) PG_GETARG_POINTER(0);
64 pst->len = PG_GETARG_INT32(1);
67 PG_RETURN_POINTER(pst);
71 testprs_getlexeme(PG_FUNCTION_ARGS)
73 ParserState *pst = (ParserState *) PG_GETARG_POINTER(0);
74 char **t = (char **) PG_GETARG_POINTER(1);
75 int *tlen = (int *) PG_GETARG_POINTER(2);
79 *t = pst->buffer + pst->pos;
81 if ((pst->buffer)[pst->pos] == ' ')
85 /* go to the next non-white-space character */
86 while ((pst->buffer)[pst->pos] == ' ' &&
94 /* go to the next white-space character */
95 while ((pst->buffer)[pst->pos] != ' ' &&
100 *tlen = pst->pos - *tlen;
102 /* we are finished if (*tlen == 0) */
106 PG_RETURN_INT32(type);
110 testprs_end(PG_FUNCTION_ARGS)
112 ParserState *pst = (ParserState *) PG_GETARG_POINTER(0);
119 testprs_lextype(PG_FUNCTION_ARGS)
122 * Remarks: - we have to return the blanks for headline reason - we use
123 * the same lexids like Teodor in the default word parser; in this way we
124 * can reuse the headline function of the default word parser.
126 LexDescr *descr = (LexDescr *) palloc(sizeof(LexDescr) * (2 + 1));
128 /* there are only two types in this parser */
130 descr[0].alias = pstrdup("word");
131 descr[0].descr = pstrdup("Word");
133 descr[1].alias = pstrdup("blank");
134 descr[1].descr = pstrdup("Space symbols");
137 PG_RETURN_POINTER(descr);