X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fcql.y;h=cf6995e22468349188bfc311bb11e89a653c53ec;hp=c98696d01a781f400be53ebdcdd43f24c1ad2c00;hb=67263adf9c8efbd384b09ec71b98844ce7c7d2bf;hpb=c6e47cbbff56f39f6d81b079ebaeac41d793d4d9 diff --git a/src/cql.y b/src/cql.y index c98696d..cf6995e 100644 --- a/src/cql.y +++ b/src/cql.y @@ -1,26 +1,38 @@ -/* $Id: cql.y,v 1.1 2003-10-27 12:21:30 adam Exp $ - Copyright (C) 2002-2003 - Index Data Aps - -This file is part of the YAZ toolkit. - -See the file LICENSE. - - bison parser for CQL grammar. -*/ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2011 Index Data + * See the file LICENSE for details. + */ +/* bison parser for CQL grammar. */ %{ +/** + * \file cql.c + * \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. + */ +#if HAVE_CONFIG_H +#include +#endif #include #include #include -#include +#include +#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; @@ -44,20 +56,46 @@ See the file LICENSE. %} %pure_parser -%token TERM AND OR NOT PROX GE LE NE -%expect 8 +%token PREFIX_NAME SIMPLE_STRING AND OR NOT PROX GE LE NE EXACT SORTBY %% top: { - $$.rel = cql_node_mk_sc("srw.serverChoice", "scr", 0); + $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem, + "cql.serverChoice", "=", 0); ((CQL_parser) parm)->top = 0; -} cqlQuery1 { +} cqlQuery1 sortby { cql_node_destroy($$.rel); - ((CQL_parser) parm)->top = $2.cql; + if ($3.cql) + { + $3.cql->u.sort.search = $2.cql; + ((CQL_parser) parm)->top = $3.cql; + } else { + ((CQL_parser) parm)->top = $2.cql; + } } ; +sortby: /* empty */ + { $$.cql = 0; } +| SORTBY sortSpec { + $$.cql = $2.cql; + }; + +sortSpec: sortSpec singleSpec { + $$.cql = $1.cql; + $$.cql->u.sort.next = $2.cql; + } +| singleSpec +{ + $$.cql = $1.cql; +}; + +singleSpec: index modifiers { + $$.cql = cql_node_mk_sort(((CQL_parser) parm)->nmem, $1.buf, $2.cql); + } +; + cqlQuery1: cqlQuery | cqlQuery error { cql_node_destroy($1.cql); @@ -65,17 +103,35 @@ 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 { - $$.rel = $0.rel; + 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 = $2.rel; + cn->u.boolean.modifiers = $3.cql; cn->u.boolean.left = $1.cql; - cn->u.boolean.right = $4.cql; + cn->u.boolean.right = $5.cql; $$.cql = cn; } @@ -89,137 +145,85 @@ searchClause: $$.cql = $3.cql; } | - searchTerm { - struct cql_node *st = cql_node_dup ($0.rel); - st->u.st.term = strdup($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 { - $$.rel = $2.rel; - $$.rel->u.st.index = strdup($1.buf); + index relation modifiers { + $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem, $1.buf, $2.buf, 0); + $$.rel->u.st.modifiers = $3.cql; } searchClause { - $$.cql = $4.cql; - cql_node_destroy($2.rel); + $$.cql = $5.cql; + cql_node_destroy($4.rel); } -| '>' searchTerm '=' searchTerm { - $$.rel = $0.rel; - } cqlQuery { - $$.cql = cql_node_prefix($6.cql, $2.buf, $4.buf); - } -| '>' searchTerm { - $$.rel = $0.rel; - } cqlQuery { - $$.cql = cql_node_prefix($4.cql, 0, $2.buf); - } ; -/* unary NOT search TERM here .. */ - -boolean: - AND | OR | NOT | PROX proxqualifiers { - $$ = $1; - $$.rel = $2.rel; - } - ; - -proxqualifiers: - Prelation { - $$.rel = cql_node_mk_proxargs ($1.buf, 0, 0, 0); - } -| - PrelationO Pdistance { - $$.rel = cql_node_mk_proxargs ($1.buf, $2.buf, 0, 0); - } -| - PrelationO PdistanceO Punit { - $$.rel = cql_node_mk_proxargs ($1.buf, $2.buf, $3.buf, 0); - } -| - PrelationO PdistanceO PunitO Pordering { - $$.rel = cql_node_mk_proxargs ($1.buf, $2.buf, $3.buf, $4.buf); - } -| -{ $$.rel = 0; } -; - -Punit: '/' searchTerm { - $$ = $2; - } -; - -PunitO: '/' searchTerm { - $$ = $2; - } -| -'/' { $$.buf[0] = 0; } -; -Prelation: '/' baseRelation { - $$ = $2; -} -; -PrelationO: '/' baseRelation { - $$ = $2; -} -| '/' { $$.buf[0] = 0; } -; -Pdistance: '/' searchTerm { - $$ = $2; +extraTerms: +SIMPLE_STRING extraTerms { + struct cql_node *st = cql_node_mk_sc(((CQL_parser) parm)->nmem, + /* index */ 0, /* rel */ 0, $1.buf); + st->u.st.extra_terms = $2.cql; + $$.cql = st; } +| +{ $$.cql = 0; } ; -PdistanceO: '/' searchTerm { - $$ = $2; -} -| '/' { $$.buf[0] = 0; } -; -Pordering: '/' searchTerm { - $$ = $2; -} -; -relation: baseRelation modifiers { - struct cql_node *st = cql_node_mk_sc(/* index */ 0, - /* relation */ $1.buf, - /* term */ 0); +/* unary NOT search SIMPLE_STRING here .. */ - st->u.st.modifiers = $2.cql; - $$.rel = st; -} -; +boolean: + AND | OR | NOT | PROX ; -modifiers: '/' searchTerm modifiers +modifiers: modifiers '/' searchTerm { - struct cql_node *mod = cql_node_mk_mod(0, $2.buf); + struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem, + $3.buf, 0, 0); - mod->u.mod.next = $3.cql; + mod->u.st.modifiers = $1.cql; $$.cql = mod; } -| +| +modifiers '/' searchTerm relation_symbol searchTerm +{ + 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; +} +| { $$.cql = 0; } ; -baseRelation: +relation: PREFIX_NAME | relation_symbol; + +relation_symbol: '=' | '>' | '<' | GE | LE | NE -| TERM +| EXACT ; index: searchTerm; searchTerm: - TERM + SIMPLE_STRING +| PREFIX_NAME | AND | OR | NOT | PROX +| SORTBY ; %% @@ -229,7 +233,168 @@ int yyerror(char *s) return 0; } -#include "lexer.h" +/** + * 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) +{ + if (lval->len+1 >= lval->size) + { + char *nb = (char *) + nmem_malloc(cp->nmem, (lval->size = lval->len * 2 + 20)); + memcpy(nb, lval->buf, lval->len); + lval->buf = nb; + } + if (c) + lval->buf[lval->len++] = c; + lval->buf[lval->len] = '\0'; +} + + +/** + * 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; + int c; + lval->cql = 0; + lval->rel = 0; + lval->len = 0; + lval->size = 10; + lval->buf = (char *) nmem_malloc(cp->nmem, lval->size); + lval->buf[0] = '\0'; + do + { + c = cp->getbyte(cp->client_data); + if (c == 0) + return 0; + if (c == '\n') + return 0; + } while (yaz_isspace(c)); + if (strchr("()=>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 == '=') + { + putb(lval, cp, c1); + return GE; + } + else + cp->ungetbyte(c1, cp->client_data); + } + else if (c == '<') + { + c1 = cp->getbyte(cp->client_data); + if (c1 == '=') + { + putb(lval, cp, c1); + return LE; + } + else if (c1 == '>') + { + putb(lval, cp, c1); + return NE; + } + else + cp->ungetbyte(c1, cp->client_data); + } + return c; + } + if (c == '"') + { + while ((c = cp->getbyte(cp->client_data)) != 0 && c != '"') + { + if (c == '\\') + { + putb(lval, cp, c); + c = cp->getbyte(cp->client_data); + if (!c) + break; + } + putb(lval, cp, c); + } + putb(lval, cp, 0); + return SIMPLE_STRING; + } + else + { + 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); +#endif + 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, "sortby")) + { + lval->buf = "sortby"; + return SORTBY; + } + if (!cql_strcmp(lval->buf, "all")) + relation_like = 1; + if (!cql_strcmp(lval->buf, "any")) + relation_like = 1; + if (!cql_strcmp(lval->buf, "adj")) + relation_like = 1; + if (relation_like) + return PREFIX_NAME; + } + return SIMPLE_STRING; +} int cql_parser_stream(CQL_parser cp, @@ -237,11 +402,11 @@ int cql_parser_stream(CQL_parser cp, void (*ungetbyte)(int b, void *client_data), void *client_data) { + nmem_reset(cp->nmem); cp->getbyte = getbyte; cp->ungetbyte = ungetbyte; cp->client_data = client_data; - if (cp->top) - cql_node_destroy(cp->top); + cql_node_destroy(cp->top); cql_parse(cp); if (cp->top) return 0; @@ -250,7 +415,7 @@ int cql_parser_stream(CQL_parser cp, CQL_parser cql_parser_create(void) { - CQL_parser cp = (CQL_parser) malloc (sizeof(*cp)); + CQL_parser cp = (CQL_parser) xmalloc(sizeof(*cp)); cp->top = 0; cp->getbyte = 0; @@ -266,10 +431,19 @@ void cql_parser_destroy(CQL_parser cp) { cql_node_destroy(cp->top); nmem_destroy(cp->nmem); - free (cp); + xfree (cp); } struct cql_node *cql_parser_result(CQL_parser cp) { return cp->top; } + +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */