X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fcql.y;h=26702163cccff844c6e962ae3eab7d6e94962647;hp=d2cc5c5a173e1bba307ce511d88219c3cf9a3da3;hb=52188379291c43d463d4ee6742cf81a700541723;hpb=f53079fadcadb1eeef45e1e6d254801d984d6068 diff --git a/src/cql.y b/src/cql.y index d2cc5c5..2670216 100644 --- a/src/cql.y +++ b/src/cql.y @@ -1,6 +1,6 @@ -/* $Id: cql.y,v 1.6 2004-10-03 22:34:07 adam Exp $ - Copyright (C) 2002-2004 - Index Data Aps +/* $Id: cql.y,v 1.17 2008-01-06 16:22:02 adam Exp $ + Copyright (C) 2002-2006 + Index Data ApS This file is part of the YAZ toolkit. @@ -14,6 +14,7 @@ See the file LICENSE. * \brief Implements CQL parser. * * This is a YACC parser, but since it must be reentrant, Bison is required. + * The original source file is cql.y. */ #include #include @@ -22,12 +23,18 @@ See the file LICENSE. #include #include #include - + + /** Node in the LALR parse tree. */ typedef struct { + /** Inhereted attribute: relation */ struct cql_node *rel; + /** Synthesized attribute: CQL node */ struct cql_node *cql; + /** string buffer with token */ char *buf; + /** length of token */ size_t len; + /** size of buffer (len <= size) */ size_t size; } token; @@ -51,13 +58,13 @@ See the file LICENSE. %} %pure_parser -%token TERM AND OR NOT PROX GE LE NE -%expect 9 +%token DOTTERM TERM AND OR NOT PROX GE LE NE EXACT %% top: { - $$.rel = cql_node_mk_sc("cql.serverChoice", "scr", 0); + $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem, + "cql.serverChoice", "=", 0); ((CQL_parser) parm)->top = 0; } cqlQuery1 { cql_node_destroy($$.rel); @@ -72,13 +79,31 @@ cqlQuery1: cqlQuery } ; -cqlQuery: +cqlQuery: + scopedClause + | + '>' searchTerm '=' searchTerm { + $$.rel = $0.rel; + } cqlQuery { + $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem, + $6.cql, $2.buf, $4.buf); + } +| '>' searchTerm { + $$.rel = $0.rel; + } cqlQuery { + $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem, + $4.cql, 0, $2.buf); + } +; + +scopedClause: searchClause | - cqlQuery boolean modifiers { + scopedClause boolean modifiers { $$.rel = $0.rel; } searchClause { - struct cql_node *cn = cql_node_mk_boolean($2.buf); + struct cql_node *cn = cql_node_mk_boolean(((CQL_parser) parm)->nmem, + $2.buf); cn->u.boolean.modifiers = $3.cql; cn->u.boolean.left = $1.cql; @@ -96,40 +121,43 @@ searchClause: $$.cql = $3.cql; } | - searchTerm { - struct cql_node *st = cql_node_dup ($0.rel); - st->u.st.term = xstrdup($1.buf); +searchTerm extraTerms { + struct cql_node *st = cql_node_dup(((CQL_parser) parm)->nmem, $0.rel); + st->u.st.extra_terms = $2.cql; + st->u.st.term = nmem_strdup(((CQL_parser)parm)->nmem, $1.buf); $$.cql = st; } | index relation modifiers { - $$.rel = cql_node_mk_sc($1.buf, $2.buf, 0); + $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem, $1.buf, $2.buf, 0); $$.rel->u.st.modifiers = $3.cql; } searchClause { $$.cql = $5.cql; cql_node_destroy($4.rel); } -| '>' searchTerm '=' searchTerm { - $$.rel = $0.rel; - } cqlQuery { - $$.cql = cql_apply_prefix($6.cql, $2.buf, $4.buf); - } -| '>' searchTerm { - $$.rel = $0.rel; - } cqlQuery { - $$.cql = cql_apply_prefix($4.cql, 0, $2.buf); - } ; +extraTerms: +extraTerms TERM { + struct cql_node *st = cql_node_mk_sc(((CQL_parser) parm)->nmem, + /* index */ 0, /* rel */ 0, $2.buf); + st->u.st.extra_terms = $1.cql; + $$.cql = st; +} +| +{ $$.cql = 0; } +; + + /* unary NOT search TERM here .. */ boolean: - AND | OR | NOT | PROX - ; + AND | OR | NOT | PROX ; modifiers: modifiers '/' searchTerm { - struct cql_node *mod = cql_node_mk_sc($3.buf, "=", 0); + struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem, + $3.buf, 0, 0); mod->u.st.modifiers = $1.cql; $$.cql = mod; @@ -137,7 +165,8 @@ modifiers: modifiers '/' searchTerm | modifiers '/' searchTerm mrelation searchTerm { - struct cql_node *mod = cql_node_mk_sc($3.buf, $4.buf, $5.buf); + struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem, + $3.buf, $4.buf, $5.buf); mod->u.st.modifiers = $1.cql; $$.cql = mod; @@ -155,6 +184,7 @@ mrelation: | GE | LE | NE +| EXACT ; relation: @@ -164,7 +194,8 @@ relation: | GE | LE | NE -| TERM +| EXACT +| DOTTERM ; index: @@ -172,6 +203,7 @@ index: searchTerm: TERM +| DOTTERM | AND | OR | NOT @@ -185,8 +217,10 @@ int yyerror(char *s) return 0; } -/* - * bison lexer for CQL. +/** + * putb is a utility that puts one character to the string + * in current lexical token. This routine deallocates as + * necessary using NMEM. */ static void putb(YYSTYPE *lval, CQL_parser cp, int c) @@ -204,6 +238,10 @@ static void putb(YYSTYPE *lval, CQL_parser cp, int c) } +/** + * yylex returns next token for Bison to be read. In this + * case one of the CQL terminals are returned. + */ int yylex(YYSTYPE *lval, void *vp) { CQL_parser cp = (CQL_parser) vp; @@ -226,7 +264,18 @@ int yylex(YYSTYPE *lval, void *vp) { int c1; putb(lval, cp, c); - if (c == '>') + if (c == '=') + { + c1 = cp->getbyte(cp->client_data); + if (c1 == '=') + { + putb(lval, cp, c1); + return EXACT; + } + else + cp->ungetbyte(c1, cp->client_data); + } + else if (c == '>') { c1 = cp->getbyte(cp->client_data); if (c1 == '=') @@ -260,34 +309,66 @@ int yylex(YYSTYPE *lval, void *vp) while ((c = cp->getbyte(cp->client_data)) != 0 && c != '"') { if (c == '\\') + { + putb(lval, cp, c); c = cp->getbyte(cp->client_data); - putb(lval, cp, c); + if (!c) + break; + } + putb(lval, cp, c); } putb(lval, cp, 0); + return TERM; } else { - putb(lval, cp, c); - while ((c = cp->getbyte(cp->client_data)) != 0 && - !strchr(" \n()=<>/", c)) - { - if (c == '\\') - c = cp->getbyte(cp->client_data); - putb(lval, cp, c); - } + int relation_like = 0; + while (c != 0 && !strchr(" \n()=<>/", c)) + { + if (c == '.') + relation_like = 1; + if (c == '\\') + { + putb(lval, cp, c); + c = cp->getbyte(cp->client_data); + if (!c) + break; + } + putb(lval, cp, c); + c = cp->getbyte(cp->client_data); + } + putb(lval, cp, 0); #if YYDEBUG - printf ("got %s\n", lval->buf); + printf ("got %s\n", lval->buf); #endif - if (c != 0) - cp->ungetbyte(c, cp->client_data); - if (!strcmp(lval->buf, "and")) - return AND; - if (!strcmp(lval->buf, "or")) - return OR; - if (!strcmp(lval->buf, "not")) - return NOT; - if (!strncmp(lval->buf, "prox", 4)) - return PROX; + if (c != 0) + cp->ungetbyte(c, cp->client_data); + if (!cql_strcmp(lval->buf, "and")) + { + lval->buf = "and"; + return AND; + } + if (!cql_strcmp(lval->buf, "or")) + { + lval->buf = "or"; + return OR; + } + if (!cql_strcmp(lval->buf, "not")) + { + lval->buf = "not"; + return NOT; + } + if (!cql_strcmp(lval->buf, "prox")) + { + lval->buf = "prox"; + return PROX; + } + if (!cql_strcmp(lval->buf, "all")) + relation_like = 1; + if (!cql_strcmp(lval->buf, "any")) + relation_like = 1; + if (relation_like) + return DOTTERM; } return TERM; }