1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 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.
20 #include <yaz/yaz-iconv.h>
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);
52 #define YYPARSE_PARAM parm
53 #define YYLEX_PARAM parm
55 int yylex(YYSTYPE *lval, void *vp);
60 %token PREFIX_NAME SIMPLE_STRING AND OR NOT PROX GE LE NE EXACT SORTBY
65 $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem,
66 "cql.serverChoice", "=", 0);
67 ((CQL_parser) parm)->top = 0;
69 cql_node_destroy($$.rel);
72 $3.cql->u.sort.search = $2.cql;
73 ((CQL_parser) parm)->top = $3.cql;
75 ((CQL_parser) parm)->top = $2.cql;
86 sortSpec: sortSpec singleSpec {
88 $$.cql->u.sort.next = $2.cql;
95 singleSpec: index modifiers {
96 $$.cql = cql_node_mk_sort(((CQL_parser) parm)->nmem, $1.buf, $2.cql);
102 cql_node_destroy($1.cql);
110 '>' searchTerm '=' searchTerm {
113 $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem,
114 $6.cql, $2.buf, $4.buf);
119 $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem,
127 scopedClause boolean modifiers {
130 struct cql_node *cn = cql_node_mk_boolean(((CQL_parser) parm)->nmem,
133 cn->u.boolean.modifiers = $3.cql;
134 cn->u.boolean.left = $1.cql;
135 cn->u.boolean.right = $5.cql;
149 searchTerm extraTerms {
150 struct cql_node *st = cql_node_dup(((CQL_parser) parm)->nmem, $0.rel);
151 st->u.st.extra_terms = $2.cql;
152 st->u.st.term = nmem_strdup(((CQL_parser)parm)->nmem, $1.buf);
156 index relation modifiers {
157 $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem, $1.buf, $2.buf, 0);
158 $$.rel->u.st.modifiers = $3.cql;
161 cql_node_destroy($4.rel);
166 SIMPLE_STRING extraTerms {
167 struct cql_node *st = cql_node_mk_sc(((CQL_parser) parm)->nmem,
168 /* index */ 0, /* rel */ 0, $1.buf);
169 st->u.st.extra_terms = $2.cql;
177 /* unary NOT search SIMPLE_STRING here .. */
180 AND | OR | NOT | PROX ;
182 modifiers: modifiers '/' searchTerm
184 struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem,
187 mod->u.st.modifiers = $1.cql;
191 modifiers '/' searchTerm relation_symbol searchTerm
193 struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem,
194 $3.buf, $4.buf, $5.buf);
196 mod->u.st.modifiers = $1.cql;
205 relation: PREFIX_NAME | relation_symbol;
238 * putb is a utility that puts one character to the string
239 * in current lexical token. This routine deallocates as
240 * necessary using NMEM.
243 static void putb(YYSTYPE *lval, CQL_parser cp, int c)
245 if (lval->len+1 >= lval->size)
248 nmem_malloc(cp->nmem, (lval->size = lval->len * 2 + 20));
249 memcpy(nb, lval->buf, lval->len);
253 lval->buf[lval->len++] = c;
254 lval->buf[lval->len] = '\0';
259 * yylex returns next token for Bison to be read. In this
260 * case one of the CQL terminals are returned.
262 int yylex(YYSTYPE *lval, void *vp)
264 CQL_parser cp = (CQL_parser) vp;
270 lval->buf = (char *) nmem_malloc(cp->nmem, lval->size);
274 c = cp->getbyte(cp->client_data);
279 } while (yaz_isspace(c));
280 if (strchr("()=></", c))
286 c1 = cp->getbyte(cp->client_data);
293 cp->ungetbyte(c1, cp->client_data);
297 c1 = cp->getbyte(cp->client_data);
304 cp->ungetbyte(c1, cp->client_data);
308 c1 = cp->getbyte(cp->client_data);
320 cp->ungetbyte(c1, cp->client_data);
326 while ((c = cp->getbyte(cp->client_data)) != 0 && c != '"')
331 c = cp->getbyte(cp->client_data);
338 return SIMPLE_STRING;
342 int relation_like = 0;
343 while (c != 0 && !strchr(" \n()=<>/", c))
350 c = cp->getbyte(cp->client_data);
355 c = cp->getbyte(cp->client_data);
359 printf ("got %s\n", lval->buf);
362 cp->ungetbyte(c, cp->client_data);
363 if (!cql_strcmp(lval->buf, "and"))
368 if (!cql_strcmp(lval->buf, "or"))
373 if (!cql_strcmp(lval->buf, "not"))
378 if (!cql_strcmp(lval->buf, "prox"))
383 if (!cql_strcmp(lval->buf, "sortby"))
385 lval->buf = "sortby";
390 if (!cql_strcmp(lval->buf, "all"))
392 if (!cql_strcmp(lval->buf, "any"))
394 if (!cql_strcmp(lval->buf, "adj"))
399 return SIMPLE_STRING;
403 int cql_parser_stream(CQL_parser cp,
404 int (*getbyte)(void *client_data),
405 void (*ungetbyte)(int b, void *client_data),
408 nmem_reset(cp->nmem);
409 cp->getbyte = getbyte;
410 cp->ungetbyte = ungetbyte;
411 cp->client_data = client_data;
412 cql_node_destroy(cp->top);
419 CQL_parser cql_parser_create(void)
421 CQL_parser cp = (CQL_parser) xmalloc(sizeof(*cp));
429 cp->nmem = nmem_create();
434 void cql_parser_destroy(CQL_parser cp)
436 cql_node_destroy(cp->top);
437 nmem_destroy(cp->nmem);
441 struct cql_node *cql_parser_result(CQL_parser cp)
446 void cql_parser_strict(CQL_parser cp, int mode)
454 * c-file-style: "Stroustrup"
455 * indent-tabs-mode: nil
457 * vim: shiftwidth=4 tabstop=8 expandtab