1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 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.
18 #include <yaz/xmalloc.h>
22 /** Node in the LALR parse tree. */
24 /** Inhereted attribute: relation */
26 /** Synthesized attribute: CQL node */
28 /** string buffer with token */
30 /** length of token */
32 /** size of buffer (len <= size) */
37 int (*getbyte)(void *client_data);
38 void (*ungetbyte)(int b, void *client_data);
48 #define YYPARSE_PARAM parm
49 #define YYLEX_PARAM parm
51 int yylex(YYSTYPE *lval, void *vp);
56 %token DOTTERM TERM AND OR NOT PROX GE LE NE EXACT
61 $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem,
62 "cql.serverChoice", "=", 0);
63 ((CQL_parser) parm)->top = 0;
65 cql_node_destroy($$.rel);
66 ((CQL_parser) parm)->top = $2.cql;
72 cql_node_destroy($1.cql);
80 '>' searchTerm '=' searchTerm {
83 $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem,
84 $6.cql, $2.buf, $4.buf);
89 $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem,
97 scopedClause boolean modifiers {
100 struct cql_node *cn = cql_node_mk_boolean(((CQL_parser) parm)->nmem,
103 cn->u.boolean.modifiers = $3.cql;
104 cn->u.boolean.left = $1.cql;
105 cn->u.boolean.right = $5.cql;
119 searchTerm extraTerms {
120 struct cql_node *st = cql_node_dup(((CQL_parser) parm)->nmem, $0.rel);
121 st->u.st.extra_terms = $2.cql;
122 st->u.st.term = nmem_strdup(((CQL_parser)parm)->nmem, $1.buf);
126 index relation modifiers {
127 $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem, $1.buf, $2.buf, 0);
128 $$.rel->u.st.modifiers = $3.cql;
131 cql_node_destroy($4.rel);
137 struct cql_node *st = cql_node_mk_sc(((CQL_parser) parm)->nmem,
138 /* index */ 0, /* rel */ 0, $2.buf);
139 st->u.st.extra_terms = $1.cql;
147 /* unary NOT search TERM here .. */
150 AND | OR | NOT | PROX ;
152 modifiers: modifiers '/' searchTerm
154 struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem,
157 mod->u.st.modifiers = $1.cql;
161 modifiers '/' searchTerm mrelation searchTerm
163 struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem,
164 $3.buf, $4.buf, $5.buf);
166 mod->u.st.modifiers = $1.cql;
216 * putb is a utility that puts one character to the string
217 * in current lexical token. This routine deallocates as
218 * necessary using NMEM.
221 static void putb(YYSTYPE *lval, CQL_parser cp, int c)
223 if (lval->len+1 >= lval->size)
226 nmem_malloc(cp->nmem, (lval->size = lval->len * 2 + 20));
227 memcpy (nb, lval->buf, lval->len);
231 lval->buf[lval->len++] = c;
232 lval->buf[lval->len] = '\0';
237 * yylex returns next token for Bison to be read. In this
238 * case one of the CQL terminals are returned.
240 int yylex(YYSTYPE *lval, void *vp)
242 CQL_parser cp = (CQL_parser) vp;
248 lval->buf = (char *) nmem_malloc(cp->nmem, lval->size);
252 c = cp->getbyte(cp->client_data);
257 } while (isspace(c));
258 if (strchr("()=></", c))
264 c1 = cp->getbyte(cp->client_data);
271 cp->ungetbyte(c1, cp->client_data);
275 c1 = cp->getbyte(cp->client_data);
282 cp->ungetbyte(c1, cp->client_data);
286 c1 = cp->getbyte(cp->client_data);
298 cp->ungetbyte(c1, cp->client_data);
304 while ((c = cp->getbyte(cp->client_data)) != 0 && c != '"')
309 c = cp->getbyte(cp->client_data);
320 int relation_like = 0;
321 while (c != 0 && !strchr(" \n()=<>/", c))
328 c = cp->getbyte(cp->client_data);
333 c = cp->getbyte(cp->client_data);
337 printf ("got %s\n", lval->buf);
340 cp->ungetbyte(c, cp->client_data);
341 if (!cql_strcmp(lval->buf, "and"))
346 if (!cql_strcmp(lval->buf, "or"))
351 if (!cql_strcmp(lval->buf, "not"))
356 if (!cql_strcmp(lval->buf, "prox"))
361 if (!cql_strcmp(lval->buf, "all"))
363 if (!cql_strcmp(lval->buf, "any"))
372 int cql_parser_stream(CQL_parser cp,
373 int (*getbyte)(void *client_data),
374 void (*ungetbyte)(int b, void *client_data),
377 nmem_reset(cp->nmem);
378 cp->getbyte = getbyte;
379 cp->ungetbyte = ungetbyte;
380 cp->client_data = client_data;
382 cql_node_destroy(cp->top);
389 CQL_parser cql_parser_create(void)
391 CQL_parser cp = (CQL_parser) xmalloc (sizeof(*cp));
399 cp->nmem = nmem_create();
403 void cql_parser_destroy(CQL_parser cp)
405 cql_node_destroy(cp->top);
406 nmem_destroy(cp->nmem);
410 struct cql_node *cql_parser_result(CQL_parser cp)