1 /* $Id: cql.y,v 1.6 2004-10-03 22:34:07 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.
22 #include <yaz/xmalloc.h>
35 int (*getbyte)(void *client_data);
36 void (*ungetbyte)(int b, void *client_data);
46 #define YYPARSE_PARAM parm
47 #define YYLEX_PARAM parm
49 int yylex(YYSTYPE *lval, void *vp);
54 %token TERM AND OR NOT PROX GE LE NE
60 $$.rel = cql_node_mk_sc("cql.serverChoice", "scr", 0);
61 ((CQL_parser) parm)->top = 0;
63 cql_node_destroy($$.rel);
64 ((CQL_parser) parm)->top = $2.cql;
70 cql_node_destroy($1.cql);
78 cqlQuery boolean modifiers {
81 struct cql_node *cn = cql_node_mk_boolean($2.buf);
83 cn->u.boolean.modifiers = $3.cql;
84 cn->u.boolean.left = $1.cql;
85 cn->u.boolean.right = $5.cql;
100 struct cql_node *st = cql_node_dup ($0.rel);
101 st->u.st.term = xstrdup($1.buf);
105 index relation modifiers {
106 $$.rel = cql_node_mk_sc($1.buf, $2.buf, 0);
107 $$.rel->u.st.modifiers = $3.cql;
110 cql_node_destroy($4.rel);
112 | '>' searchTerm '=' searchTerm {
115 $$.cql = cql_apply_prefix($6.cql, $2.buf, $4.buf);
120 $$.cql = cql_apply_prefix($4.cql, 0, $2.buf);
124 /* unary NOT search TERM here .. */
127 AND | OR | NOT | PROX
130 modifiers: modifiers '/' searchTerm
132 struct cql_node *mod = cql_node_mk_sc($3.buf, "=", 0);
134 mod->u.st.modifiers = $1.cql;
138 modifiers '/' searchTerm mrelation searchTerm
140 struct cql_node *mod = cql_node_mk_sc($3.buf, $4.buf, $5.buf);
142 mod->u.st.modifiers = $1.cql;
189 * bison lexer for CQL.
192 static void putb(YYSTYPE *lval, CQL_parser cp, int c)
194 if (lval->len+1 >= lval->size)
197 nmem_malloc(cp->nmem, (lval->size = lval->len * 2 + 20));
198 memcpy (nb, lval->buf, lval->len);
202 lval->buf[lval->len++] = c;
203 lval->buf[lval->len] = '\0';
207 int yylex(YYSTYPE *lval, void *vp)
209 CQL_parser cp = (CQL_parser) vp;
215 lval->buf = (char *) nmem_malloc(cp->nmem, lval->size);
219 c = cp->getbyte(cp->client_data);
224 } while (isspace(c));
225 if (strchr("()=></", c))
231 c1 = cp->getbyte(cp->client_data);
238 cp->ungetbyte(c1, cp->client_data);
242 c1 = cp->getbyte(cp->client_data);
254 cp->ungetbyte(c1, cp->client_data);
260 while ((c = cp->getbyte(cp->client_data)) != 0 && c != '"')
263 c = cp->getbyte(cp->client_data);
271 while ((c = cp->getbyte(cp->client_data)) != 0 &&
272 !strchr(" \n()=<>/", c))
275 c = cp->getbyte(cp->client_data);
279 printf ("got %s\n", lval->buf);
282 cp->ungetbyte(c, cp->client_data);
283 if (!strcmp(lval->buf, "and"))
285 if (!strcmp(lval->buf, "or"))
287 if (!strcmp(lval->buf, "not"))
289 if (!strncmp(lval->buf, "prox", 4))
296 int cql_parser_stream(CQL_parser cp,
297 int (*getbyte)(void *client_data),
298 void (*ungetbyte)(int b, void *client_data),
301 nmem_reset(cp->nmem);
302 cp->getbyte = getbyte;
303 cp->ungetbyte = ungetbyte;
304 cp->client_data = client_data;
306 cql_node_destroy(cp->top);
313 CQL_parser cql_parser_create(void)
315 CQL_parser cp = (CQL_parser) xmalloc (sizeof(*cp));
323 cp->nmem = nmem_create();
327 void cql_parser_destroy(CQL_parser cp)
329 cql_node_destroy(cp->top);
330 nmem_destroy(cp->nmem);
334 struct cql_node *cql_parser_result(CQL_parser cp)