1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
5 /* bison parser for CQL grammar. */
9 * \brief Implements CQL parser.
11 * This is a YACC parser, but since it must be reentrant, Bison is required.
12 * The original source file is cql.y.
21 #include <yaz/xmalloc.h>
25 /** Node in the LALR parse tree. */
27 /** Inhereted attribute: relation */
29 /** Synthesized attribute: CQL node */
31 /** string buffer with token */
33 /** length of token */
35 /** size of buffer (len <= size) */
40 int (*getbyte)(void *client_data);
41 void (*ungetbyte)(int b, void *client_data);
51 #define YYPARSE_PARAM parm
52 #define YYLEX_PARAM parm
54 int yylex(YYSTYPE *lval, void *vp);
59 %token DOTTERM TERM AND OR NOT PROX GE LE NE EXACT SORTBY
64 $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem,
65 "cql.serverChoice", "=", 0);
66 ((CQL_parser) parm)->top = 0;
68 cql_node_destroy($$.rel);
69 ((CQL_parser) parm)->top = $2.cql;
76 sortSpec: sortSpec singleSpec
79 singleSpec: index modifiers ;
83 cql_node_destroy($1.cql);
91 '>' searchTerm '=' searchTerm {
94 $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem,
95 $6.cql, $2.buf, $4.buf);
100 $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem,
108 scopedClause boolean modifiers {
111 struct cql_node *cn = cql_node_mk_boolean(((CQL_parser) parm)->nmem,
114 cn->u.boolean.modifiers = $3.cql;
115 cn->u.boolean.left = $1.cql;
116 cn->u.boolean.right = $5.cql;
130 searchTerm extraTerms {
131 struct cql_node *st = cql_node_dup(((CQL_parser) parm)->nmem, $0.rel);
132 st->u.st.extra_terms = $2.cql;
133 st->u.st.term = nmem_strdup(((CQL_parser)parm)->nmem, $1.buf);
137 index relation modifiers {
138 $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem, $1.buf, $2.buf, 0);
139 $$.rel->u.st.modifiers = $3.cql;
142 cql_node_destroy($4.rel);
148 struct cql_node *st = cql_node_mk_sc(((CQL_parser) parm)->nmem,
149 /* index */ 0, /* rel */ 0, $2.buf);
150 st->u.st.extra_terms = $1.cql;
158 /* unary NOT search TERM here .. */
161 AND | OR | NOT | PROX ;
163 modifiers: modifiers '/' searchTerm
165 struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem,
168 mod->u.st.modifiers = $1.cql;
172 modifiers '/' searchTerm mrelation searchTerm
174 struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem,
175 $3.buf, $4.buf, $5.buf);
177 mod->u.st.modifiers = $1.cql;
228 * putb is a utility that puts one character to the string
229 * in current lexical token. This routine deallocates as
230 * necessary using NMEM.
233 static void putb(YYSTYPE *lval, CQL_parser cp, int c)
235 if (lval->len+1 >= lval->size)
238 nmem_malloc(cp->nmem, (lval->size = lval->len * 2 + 20));
239 memcpy (nb, lval->buf, lval->len);
243 lval->buf[lval->len++] = c;
244 lval->buf[lval->len] = '\0';
249 * yylex returns next token for Bison to be read. In this
250 * case one of the CQL terminals are returned.
252 int yylex(YYSTYPE *lval, void *vp)
254 CQL_parser cp = (CQL_parser) vp;
260 lval->buf = (char *) nmem_malloc(cp->nmem, lval->size);
264 c = cp->getbyte(cp->client_data);
269 } while (isspace(c));
270 if (strchr("()=></", c))
276 c1 = cp->getbyte(cp->client_data);
283 cp->ungetbyte(c1, cp->client_data);
287 c1 = cp->getbyte(cp->client_data);
294 cp->ungetbyte(c1, cp->client_data);
298 c1 = cp->getbyte(cp->client_data);
310 cp->ungetbyte(c1, cp->client_data);
316 while ((c = cp->getbyte(cp->client_data)) != 0 && c != '"')
321 c = cp->getbyte(cp->client_data);
332 int relation_like = 0;
333 while (c != 0 && !strchr(" \n()=<>/", c))
340 c = cp->getbyte(cp->client_data);
345 c = cp->getbyte(cp->client_data);
349 printf ("got %s\n", lval->buf);
352 cp->ungetbyte(c, cp->client_data);
353 if (!cql_strcmp(lval->buf, "and"))
358 if (!cql_strcmp(lval->buf, "or"))
363 if (!cql_strcmp(lval->buf, "not"))
368 if (!cql_strcmp(lval->buf, "prox"))
373 if (!cql_strcmp(lval->buf, "sortby"))
375 lval->buf = "sortby";
378 if (!cql_strcmp(lval->buf, "all"))
380 if (!cql_strcmp(lval->buf, "any"))
389 int cql_parser_stream(CQL_parser cp,
390 int (*getbyte)(void *client_data),
391 void (*ungetbyte)(int b, void *client_data),
394 nmem_reset(cp->nmem);
395 cp->getbyte = getbyte;
396 cp->ungetbyte = ungetbyte;
397 cp->client_data = client_data;
399 cql_node_destroy(cp->top);
406 CQL_parser cql_parser_create(void)
408 CQL_parser cp = (CQL_parser) xmalloc (sizeof(*cp));
416 cp->nmem = nmem_create();
420 void cql_parser_destroy(CQL_parser cp)
422 cql_node_destroy(cp->top);
423 nmem_destroy(cp->nmem);
427 struct cql_node *cql_parser_result(CQL_parser cp)