Update CQL parser to use CQL 1.1 modifiers for booleans and
[yaz-moved-to-github.git] / src / cql.y
index c98696d..0a81738 100644 (file)
--- a/src/cql.y
+++ b/src/cql.y
@@ -1,5 +1,5 @@
-/* $Id: cql.y,v 1.1 2003-10-27 12:21:30 adam Exp $
-   Copyright (C) 2002-2003
+/* $Id: cql.y,v 1.2 2004-03-10 16:34:29 adam Exp $
+   Copyright (C) 2002-2004
    Index Data Aps
 
 This file is part of the YAZ toolkit.
@@ -45,12 +45,12 @@ See the file LICENSE.
 
 %pure_parser
 %token TERM AND OR NOT PROX GE LE NE
-%expect 8
+%expect 9
 
 %%
 
 top: { 
-    $$.rel = cql_node_mk_sc("srw.serverChoice", "scr", 0);
+    $$.rel = cql_node_mk_sc("cql.serverChoice", "scr", 0);
     ((CQL_parser) parm)->top = 0;
 } cqlQuery1 {
     cql_node_destroy($$.rel);
@@ -68,14 +68,14 @@ cqlQuery1: cqlQuery
 cqlQuery: 
   searchClause
 |
-  cqlQuery boolean { 
-      $$.rel = $0.rel; 
+  cqlQuery boolean modifiers { 
+      $$.rel = $0.rel;
   } searchClause {
       struct cql_node *cn = cql_node_mk_boolean($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;
   }
@@ -95,12 +95,12 @@ searchClause:
       $$.cql = st;
   }
 | 
-  index relation {
-      $$.rel = $2.rel;
-      $$.rel->u.st.index = strdup($1.buf);
+  index relation modifiers {
+      $$.rel = cql_node_mk_sc($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;
@@ -117,91 +117,40 @@ searchClause:
 /* unary NOT search TERM here .. */
 
 boolean: 
-  AND | OR | NOT | PROX proxqualifiers {
-      $$ = $1;
-      $$.rel = $2.rel;
-  }
+  AND | OR | NOT | PROX 
   ;
 
-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;
-}
-;
-
-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);
+modifiers: modifiers '/' searchTerm
+{ 
+    struct cql_node *mod = cql_node_mk_sc($3.buf, "=", 0);
 
-    st->u.st.modifiers = $2.cql;
-    $$.rel = st;
+    mod->u.st.modifiers = $1.cql;
+    $$.cql = mod;
 }
-;
-
-modifiers: '/' searchTerm modifiers
-{ 
-    struct cql_node *mod = cql_node_mk_mod(0, $2.buf);
+|
+modifiers '/' searchTerm mrelation searchTerm
+{
+    struct cql_node *mod = cql_node_mk_sc($3.buf, $4.buf, $5.buf);
 
-    mod->u.mod.next = $3.cql;
+    mod->u.st.modifiers = $1.cql;
     $$.cql = mod;
 }
-|  
+|
 { 
     $$.cql = 0;
 }
 ;
 
-baseRelation: 
+mrelation:
+  '=' 
+| '>' 
+| '<'
+| GE
+| LE
+| NE
+;
+
+relation: 
   '=' 
 | '>' 
 | '<'
@@ -229,7 +178,111 @@ int yyerror(char *s)
     return 0;
 }
 
-#include "lexer.h"
+/*
+ * bison lexer for CQL.
+ */
+
+static void putb(YYSTYPE *lval, CQL_parser cp, int c)
+{
+    if (lval->len+1 >= lval->size)
+    {
+        char *nb = 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';
+}
+
+
+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 = 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 (isspace(c));
+    if (strchr("()=></", c))
+    {
+        int c1;
+        putb(lval, cp, c);
+        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)) != EOF && c != '"')
+        {
+            if (c == '\\')
+                c = cp->getbyte(cp->client_data);
+            putb(lval, cp, c);
+        }
+        putb(lval, cp, 0);
+    }
+    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);
+        }
+#if YYDEBUG
+        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;
+    }
+    return TERM;
+}
 
 
 int cql_parser_stream(CQL_parser cp,
@@ -237,6 +290,7 @@ 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;