1 /* $Id: cql.y,v 1.3 2004-03-15 21:39:06 adam Exp $
2 Copyright (C) 2002-2004
5 This file is part of the YAZ toolkit.
9 bison parser for CQL grammar.
16 #include <yaz/xmalloc.h>
29 int (*getbyte)(void *client_data);
30 void (*ungetbyte)(int b, void *client_data);
40 #define YYPARSE_PARAM parm
41 #define YYLEX_PARAM parm
43 int yylex(YYSTYPE *lval, void *vp);
48 %token TERM AND OR NOT PROX GE LE NE
54 $$.rel = cql_node_mk_sc("cql.serverChoice", "scr", 0);
55 ((CQL_parser) parm)->top = 0;
57 cql_node_destroy($$.rel);
58 ((CQL_parser) parm)->top = $2.cql;
64 cql_node_destroy($1.cql);
72 cqlQuery boolean modifiers {
75 struct cql_node *cn = cql_node_mk_boolean($2.buf);
77 cn->u.boolean.modifiers = $3.cql;
78 cn->u.boolean.left = $1.cql;
79 cn->u.boolean.right = $5.cql;
94 struct cql_node *st = cql_node_dup ($0.rel);
95 st->u.st.term = xstrdup($1.buf);
99 index relation modifiers {
100 $$.rel = cql_node_mk_sc($1.buf, $2.buf, 0);
101 $$.rel->u.st.modifiers = $3.cql;
104 cql_node_destroy($4.rel);
106 | '>' searchTerm '=' searchTerm {
109 $$.cql = cql_apply_prefix($6.cql, $2.buf, $4.buf);
114 $$.cql = cql_apply_prefix($4.cql, 0, $2.buf);
118 /* unary NOT search TERM here .. */
121 AND | OR | NOT | PROX
124 modifiers: modifiers '/' searchTerm
126 struct cql_node *mod = cql_node_mk_sc($3.buf, "=", 0);
128 mod->u.st.modifiers = $1.cql;
132 modifiers '/' searchTerm mrelation searchTerm
134 struct cql_node *mod = cql_node_mk_sc($3.buf, $4.buf, $5.buf);
136 mod->u.st.modifiers = $1.cql;
183 * bison lexer for CQL.
186 static void putb(YYSTYPE *lval, CQL_parser cp, int c)
188 if (lval->len+1 >= lval->size)
190 char *nb = nmem_malloc(cp->nmem, (lval->size = lval->len * 2 + 20));
191 memcpy (nb, lval->buf, lval->len);
195 lval->buf[lval->len++] = c;
196 lval->buf[lval->len] = '\0';
200 int yylex(YYSTYPE *lval, void *vp)
202 CQL_parser cp = (CQL_parser) vp;
208 lval->buf = nmem_malloc(cp->nmem, lval->size);
212 c = cp->getbyte(cp->client_data);
217 } while (isspace(c));
218 if (strchr("()=></", c))
224 c1 = cp->getbyte(cp->client_data);
231 cp->ungetbyte(c1, cp->client_data);
235 c1 = cp->getbyte(cp->client_data);
247 cp->ungetbyte(c1, cp->client_data);
253 while ((c = cp->getbyte(cp->client_data)) != EOF && c != '"')
256 c = cp->getbyte(cp->client_data);
264 while ((c = cp->getbyte(cp->client_data)) != 0 &&
265 !strchr(" \n()=<>/", c))
268 c = cp->getbyte(cp->client_data);
272 printf ("got %s\n", lval->buf);
275 cp->ungetbyte(c, cp->client_data);
276 if (!strcmp(lval->buf, "and"))
278 if (!strcmp(lval->buf, "or"))
280 if (!strcmp(lval->buf, "not"))
282 if (!strncmp(lval->buf, "prox", 4))
289 int cql_parser_stream(CQL_parser cp,
290 int (*getbyte)(void *client_data),
291 void (*ungetbyte)(int b, void *client_data),
294 nmem_reset(cp->nmem);
295 cp->getbyte = getbyte;
296 cp->ungetbyte = ungetbyte;
297 cp->client_data = client_data;
299 cql_node_destroy(cp->top);
306 CQL_parser cql_parser_create(void)
308 CQL_parser cp = (CQL_parser) xmalloc (sizeof(*cp));
316 cp->nmem = nmem_create();
320 void cql_parser_destroy(CQL_parser cp)
322 cql_node_destroy(cp->top);
323 nmem_destroy(cp->nmem);
327 struct cql_node *cql_parser_result(CQL_parser cp)