1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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 int yylex(YYSTYPE *lval, void *vp);
53 int yyerror(void *lval, char *msg);
59 %lex-param {void *parm}
60 %parse-param {void *parm}
62 %token PREFIX_NAME SIMPLE_STRING AND OR NOT PROX GE LE NE EXACT SORTBY
67 $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem,
68 "cql.serverChoice", "=", 0);
69 ((CQL_parser) parm)->top = 0;
71 cql_node_destroy($$.rel);
74 $3.cql->u.sort.search = $2.cql;
75 ((CQL_parser) parm)->top = $3.cql;
77 ((CQL_parser) parm)->top = $2.cql;
88 sortSpec: sortSpec singleSpec {
90 $$.cql->u.sort.next = $2.cql;
97 singleSpec: index modifiers {
98 $$.cql = cql_node_mk_sort(((CQL_parser) parm)->nmem, $1.buf, $2.cql);
104 cql_node_destroy($1.cql);
112 '>' searchTerm '=' searchTerm {
115 $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem,
116 $6.cql, $2.buf, $4.buf);
121 $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem,
129 scopedClause boolean modifiers {
132 struct cql_node *cn = cql_node_mk_boolean(((CQL_parser) parm)->nmem,
135 cn->u.boolean.modifiers = $3.cql;
136 cn->u.boolean.left = $1.cql;
137 cn->u.boolean.right = $5.cql;
151 searchTerm extraTerms {
152 struct cql_node *st = cql_node_dup(((CQL_parser) parm)->nmem, $0.rel);
153 st->u.st.extra_terms = $2.cql;
154 st->u.st.term = nmem_strdup(((CQL_parser)parm)->nmem, $1.buf);
158 index relation modifiers {
159 $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem, $1.buf, $2.buf, 0);
160 $$.rel->u.st.modifiers = $3.cql;
163 cql_node_destroy($4.rel);
168 SIMPLE_STRING extraTerms {
169 struct cql_node *st = cql_node_mk_sc(((CQL_parser) parm)->nmem,
170 /* index */ 0, /* rel */ 0, $1.buf);
171 st->u.st.extra_terms = $2.cql;
179 /* unary NOT search SIMPLE_STRING here .. */
182 AND | OR | NOT | PROX ;
184 modifiers: modifiers '/' searchTerm
186 struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem,
189 mod->u.st.modifiers = $1.cql;
193 modifiers '/' searchTerm relation_symbol searchTerm
195 struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem,
196 $3.buf, $4.buf, $5.buf);
198 mod->u.st.modifiers = $1.cql;
207 relation: PREFIX_NAME | relation_symbol;
234 int yyerror(void *locp, char *s)
240 * putb is a utility that puts one character to the string
241 * in current lexical token. This routine deallocates as
242 * necessary using NMEM.
245 static void putb(YYSTYPE *lval, CQL_parser cp, int c)
247 if (lval->len+1 >= lval->size)
250 nmem_malloc(cp->nmem, (lval->size = lval->len * 2 + 20));
251 memcpy(nb, lval->buf, lval->len);
255 lval->buf[lval->len++] = c;
256 lval->buf[lval->len] = '\0';
261 * yylex returns next token for Bison to be read. In this
262 * case one of the CQL terminals are returned.
264 int yylex(YYSTYPE *lval, void *vp)
266 CQL_parser cp = (CQL_parser) vp;
272 lval->buf = (char *) nmem_malloc(cp->nmem, lval->size);
276 c = cp->getbyte(cp->client_data);
281 } while (yaz_isspace(c));
282 if (strchr("()=></", c))
288 c1 = cp->getbyte(cp->client_data);
295 cp->ungetbyte(c1, cp->client_data);
299 c1 = cp->getbyte(cp->client_data);
306 cp->ungetbyte(c1, cp->client_data);
310 c1 = cp->getbyte(cp->client_data);
322 cp->ungetbyte(c1, cp->client_data);
328 while ((c = cp->getbyte(cp->client_data)) != 0 && c != '"')
333 c = cp->getbyte(cp->client_data);
340 return SIMPLE_STRING;
344 int relation_like = 0;
345 while (c != 0 && !strchr(" \n()=<>/", c))
352 c = cp->getbyte(cp->client_data);
357 c = cp->getbyte(cp->client_data);
361 printf ("got %s\n", lval->buf);
364 cp->ungetbyte(c, cp->client_data);
365 if (!cql_strcmp(lval->buf, "and"))
370 if (!cql_strcmp(lval->buf, "or"))
375 if (!cql_strcmp(lval->buf, "not"))
380 if (!cql_strcmp(lval->buf, "prox"))
385 if (!cql_strcmp(lval->buf, "sortby"))
387 lval->buf = "sortby";
392 if (!cql_strcmp(lval->buf, "all"))
394 if (!cql_strcmp(lval->buf, "any"))
396 if (!cql_strcmp(lval->buf, "adj"))
401 return SIMPLE_STRING;
405 int cql_parser_stream(CQL_parser cp,
406 int (*getbyte)(void *client_data),
407 void (*ungetbyte)(int b, void *client_data),
410 nmem_reset(cp->nmem);
411 cp->getbyte = getbyte;
412 cp->ungetbyte = ungetbyte;
413 cp->client_data = client_data;
414 cql_node_destroy(cp->top);
421 CQL_parser cql_parser_create(void)
423 CQL_parser cp = (CQL_parser) xmalloc(sizeof(*cp));
431 cp->nmem = nmem_create();
436 void cql_parser_destroy(CQL_parser cp)
438 cql_node_destroy(cp->top);
439 nmem_destroy(cp->nmem);
443 struct cql_node *cql_parser_result(CQL_parser cp)
448 void cql_parser_strict(CQL_parser cp, int mode)
456 * c-file-style: "Stroustrup"
457 * indent-tabs-mode: nil
459 * vim: shiftwidth=4 tabstop=8 expandtab