1 /* $Id: cql.y,v 1.9 2005-06-27 22:03:59 adam Exp $
2 Copyright (C) 2002-2004
5 This file is part of the YAZ toolkit.
9 bison parser for CQL grammar.
14 * \brief Implements CQL parser.
16 * This is a YACC parser, but since it must be reentrant, Bison is required.
17 * The original source file is cql.y.
23 #include <yaz/xmalloc.h>
27 /** Node in the LALR parse tree. */
29 /** Inhereted attribute: relation */
31 /** Synthesized attribute: CQL node */
33 /** string buffer with token */
35 /** length of token */
37 /** size of buffer (len <= size) */
42 int (*getbyte)(void *client_data);
43 void (*ungetbyte)(int b, void *client_data);
53 #define YYPARSE_PARAM parm
54 #define YYLEX_PARAM parm
56 int yylex(YYSTYPE *lval, void *vp);
61 %token TERM AND OR NOT PROX GE LE NE
67 $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem,
68 "cql.serverChoice", "scr", 0);
69 ((CQL_parser) parm)->top = 0;
71 cql_node_destroy($$.rel);
72 ((CQL_parser) parm)->top = $2.cql;
78 cql_node_destroy($1.cql);
86 cqlQuery boolean modifiers {
89 struct cql_node *cn = cql_node_mk_boolean(((CQL_parser) parm)->nmem,
92 cn->u.boolean.modifiers = $3.cql;
93 cn->u.boolean.left = $1.cql;
94 cn->u.boolean.right = $5.cql;
109 struct cql_node *st = cql_node_dup (((CQL_parser) parm)->nmem, $0.rel);
110 st->u.st.term = nmem_strdup(((CQL_parser)parm)->nmem, $1.buf);
114 index relation modifiers {
115 $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem, $1.buf, $2.buf, 0);
116 $$.rel->u.st.modifiers = $3.cql;
119 cql_node_destroy($4.rel);
121 | '>' searchTerm '=' searchTerm {
124 $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem,
125 $6.cql, $2.buf, $4.buf);
130 $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem,
135 /* unary NOT search TERM here .. */
138 AND | OR | NOT | PROX
141 modifiers: modifiers '/' searchTerm
143 struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem,
146 mod->u.st.modifiers = $1.cql;
150 modifiers '/' searchTerm mrelation searchTerm
152 struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem,
153 $3.buf, $4.buf, $5.buf);
155 mod->u.st.modifiers = $1.cql;
202 * putb is a utility that puts one character to the string
203 * in current lexical token. This routine deallocates as
204 * necessary using NMEM.
207 static void putb(YYSTYPE *lval, CQL_parser cp, int c)
209 if (lval->len+1 >= lval->size)
212 nmem_malloc(cp->nmem, (lval->size = lval->len * 2 + 20));
213 memcpy (nb, lval->buf, lval->len);
217 lval->buf[lval->len++] = c;
218 lval->buf[lval->len] = '\0';
223 * yylex returns next token for Bison to be read. In this
224 * case one of the CQL terminals are returned.
226 int yylex(YYSTYPE *lval, void *vp)
228 CQL_parser cp = (CQL_parser) vp;
234 lval->buf = (char *) nmem_malloc(cp->nmem, lval->size);
238 c = cp->getbyte(cp->client_data);
243 } while (isspace(c));
244 if (strchr("()=></", c))
250 c1 = cp->getbyte(cp->client_data);
257 cp->ungetbyte(c1, cp->client_data);
261 c1 = cp->getbyte(cp->client_data);
273 cp->ungetbyte(c1, cp->client_data);
279 while ((c = cp->getbyte(cp->client_data)) != 0 && c != '"')
282 c = cp->getbyte(cp->client_data);
290 while ((c = cp->getbyte(cp->client_data)) != 0 &&
291 !strchr(" \n()=<>/", c))
294 c = cp->getbyte(cp->client_data);
298 printf ("got %s\n", lval->buf);
301 cp->ungetbyte(c, cp->client_data);
302 if (!cql_strcmp(lval->buf, "and"))
304 if (!cql_strcmp(lval->buf, "or"))
306 if (!cql_strcmp(lval->buf, "not"))
308 if (!cql_strcmp(lval->buf, "prox"))
315 int cql_parser_stream(CQL_parser cp,
316 int (*getbyte)(void *client_data),
317 void (*ungetbyte)(int b, void *client_data),
320 nmem_reset(cp->nmem);
321 cp->getbyte = getbyte;
322 cp->ungetbyte = ungetbyte;
323 cp->client_data = client_data;
325 cql_node_destroy(cp->top);
332 CQL_parser cql_parser_create(void)
334 CQL_parser cp = (CQL_parser) xmalloc (sizeof(*cp));
342 cp->nmem = nmem_create();
346 void cql_parser_destroy(CQL_parser cp)
348 cql_node_destroy(cp->top);
349 nmem_destroy(cp->nmem);
353 struct cql_node *cql_parser_result(CQL_parser cp)