2007-10-16 05:36:50 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* test_parser.c
|
|
|
|
* Simple example of a text search parser
|
|
|
|
*
|
2012-01-02 07:01:58 +08:00
|
|
|
* Copyright (c) 2007-2012, PostgreSQL Global Development Group
|
2007-10-16 05:36:50 +08:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/test_parser/test_parser.c
|
2007-10-16 05:36:50 +08:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "fmgr.h"
|
|
|
|
|
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* types
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* self-defined type */
|
2007-11-16 05:14:46 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *buffer; /* text to parse */
|
|
|
|
int len; /* length of the text in buffer */
|
|
|
|
int pos; /* position of the parser */
|
2009-06-11 22:49:15 +08:00
|
|
|
} ParserState;
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
/* copy-paste from wparser.h of tsearch2 */
|
2007-11-16 05:14:46 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int lexid;
|
|
|
|
char *alias;
|
|
|
|
char *descr;
|
2007-11-16 06:25:18 +08:00
|
|
|
} LexDescr;
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* prototypes
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(testprs_start);
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum testprs_start(PG_FUNCTION_ARGS);
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(testprs_getlexeme);
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum testprs_getlexeme(PG_FUNCTION_ARGS);
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(testprs_end);
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum testprs_end(PG_FUNCTION_ARGS);
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(testprs_lextype);
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum testprs_lextype(PG_FUNCTION_ARGS);
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* functions
|
|
|
|
*/
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum
|
|
|
|
testprs_start(PG_FUNCTION_ARGS)
|
2007-10-16 05:36:50 +08:00
|
|
|
{
|
|
|
|
ParserState *pst = (ParserState *) palloc0(sizeof(ParserState));
|
2007-11-16 05:14:46 +08:00
|
|
|
|
2007-10-16 05:36:50 +08:00
|
|
|
pst->buffer = (char *) PG_GETARG_POINTER(0);
|
|
|
|
pst->len = PG_GETARG_INT32(1);
|
|
|
|
pst->pos = 0;
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(pst);
|
|
|
|
}
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum
|
|
|
|
testprs_getlexeme(PG_FUNCTION_ARGS)
|
2007-10-16 05:36:50 +08:00
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
ParserState *pst = (ParserState *) PG_GETARG_POINTER(0);
|
|
|
|
char **t = (char **) PG_GETARG_POINTER(1);
|
|
|
|
int *tlen = (int *) PG_GETARG_POINTER(2);
|
2012-01-10 08:56:27 +08:00
|
|
|
int startpos = pst->pos;
|
2007-10-16 05:36:50 +08:00
|
|
|
int type;
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
*t = pst->buffer + pst->pos;
|
2007-10-16 05:36:50 +08:00
|
|
|
|
2012-01-10 08:56:27 +08:00
|
|
|
if (pst->pos < pst->len &&
|
|
|
|
(pst->buffer)[pst->pos] == ' ')
|
2007-10-16 05:36:50 +08:00
|
|
|
{
|
|
|
|
/* blank type */
|
|
|
|
type = 12;
|
2012-01-10 08:56:27 +08:00
|
|
|
/* go to the next non-space character */
|
|
|
|
while (pst->pos < pst->len &&
|
|
|
|
(pst->buffer)[pst->pos] == ' ')
|
2007-10-16 05:36:50 +08:00
|
|
|
(pst->pos)++;
|
2007-11-16 05:14:46 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-16 05:36:50 +08:00
|
|
|
/* word type */
|
|
|
|
type = 3;
|
2012-01-10 08:56:27 +08:00
|
|
|
/* go to the next space character */
|
|
|
|
while (pst->pos < pst->len &&
|
|
|
|
(pst->buffer)[pst->pos] != ' ')
|
2007-10-16 05:36:50 +08:00
|
|
|
(pst->pos)++;
|
|
|
|
}
|
|
|
|
|
2012-01-10 08:56:27 +08:00
|
|
|
*tlen = pst->pos - startpos;
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
/* we are finished if (*tlen == 0) */
|
|
|
|
if (*tlen == 0)
|
2007-11-16 05:14:46 +08:00
|
|
|
type = 0;
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
PG_RETURN_INT32(type);
|
|
|
|
}
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum
|
|
|
|
testprs_end(PG_FUNCTION_ARGS)
|
2007-10-16 05:36:50 +08:00
|
|
|
{
|
|
|
|
ParserState *pst = (ParserState *) PG_GETARG_POINTER(0);
|
2007-11-16 05:14:46 +08:00
|
|
|
|
2007-10-16 05:36:50 +08:00
|
|
|
pfree(pst);
|
|
|
|
PG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum
|
|
|
|
testprs_lextype(PG_FUNCTION_ARGS)
|
2007-10-16 05:36:50 +08:00
|
|
|
{
|
|
|
|
/*
|
2007-11-16 05:14:46 +08:00
|
|
|
* Remarks: - we have to return the blanks for headline reason - we use
|
|
|
|
* the same lexids like Teodor in the default word parser; in this way we
|
|
|
|
* can reuse the headline function of the default word parser.
|
2007-10-16 05:36:50 +08:00
|
|
|
*/
|
2007-11-16 05:14:46 +08:00
|
|
|
LexDescr *descr = (LexDescr *) palloc(sizeof(LexDescr) * (2 + 1));
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
/* there are only two types in this parser */
|
|
|
|
descr[0].lexid = 3;
|
|
|
|
descr[0].alias = pstrdup("word");
|
|
|
|
descr[0].descr = pstrdup("Word");
|
|
|
|
descr[1].lexid = 12;
|
|
|
|
descr[1].alias = pstrdup("blank");
|
|
|
|
descr[1].descr = pstrdup("Space symbols");
|
|
|
|
descr[2].lexid = 0;
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(descr);
|
|
|
|
}
|