Comment.
[yaz-moved-to-github.git] / src / cql.y
1 /* $Id: cql.y,v 1.1 2003-10-27 12:21:30 adam Exp $
2    Copyright (C) 2002-2003
3    Index Data Aps
4
5 This file is part of the YAZ toolkit.
6
7 See the file LICENSE.
8
9  bison parser for CQL grammar.
10 */
11 %{
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <yaz/nmem.h>
17 #include <yaz/cql.h>
18     
19     typedef struct {
20         struct cql_node *rel;
21         struct cql_node *cql;
22         char *buf;
23         size_t len;
24         size_t size;
25     } token;        
26
27     struct cql_parser {
28         int (*getbyte)(void *client_data);
29         void (*ungetbyte)(int b, void *client_data);
30         void *client_data;
31         int last_error;
32         int last_pos;
33         struct cql_node *top;
34         NMEM nmem;
35     };
36
37 #define YYSTYPE token
38     
39 #define YYPARSE_PARAM parm
40 #define YYLEX_PARAM parm
41     
42     int yylex(YYSTYPE *lval, void *vp);
43     int yyerror(char *s);
44 %}
45
46 %pure_parser
47 %token TERM AND OR NOT PROX GE LE NE
48 %expect 8
49
50 %%
51
52 top: { 
53     $$.rel = cql_node_mk_sc("srw.serverChoice", "scr", 0);
54     ((CQL_parser) parm)->top = 0;
55 } cqlQuery1 {
56     cql_node_destroy($$.rel);
57     ((CQL_parser) parm)->top = $2.cql; 
58 }
59 ;
60
61 cqlQuery1: cqlQuery
62 | cqlQuery error {
63     cql_node_destroy($1.cql);
64     $$.cql = 0;
65 }
66 ;
67
68 cqlQuery: 
69   searchClause
70 |
71   cqlQuery boolean { 
72       $$.rel = $0.rel; 
73   } searchClause {
74       struct cql_node *cn = cql_node_mk_boolean($2.buf);
75       
76       cn->u.boolean.modifiers = $2.rel;
77       cn->u.boolean.left = $1.cql;
78       cn->u.boolean.right = $4.cql;
79
80       $$.cql = cn;
81   }
82 ;
83
84 searchClause: 
85   '(' { 
86       $$.rel = $0.rel;
87       
88   } cqlQuery ')' {
89       $$.cql = $3.cql;
90   }
91 |
92   searchTerm {
93       struct cql_node *st = cql_node_dup ($0.rel);
94       st->u.st.term = strdup($1.buf);
95       $$.cql = st;
96   }
97
98   index relation {
99       $$.rel = $2.rel;
100       $$.rel->u.st.index = strdup($1.buf);
101   } searchClause {
102       $$.cql = $4.cql;
103       cql_node_destroy($2.rel);
104   }
105 | '>' searchTerm '=' searchTerm {
106       $$.rel = $0.rel;
107   } cqlQuery {
108     $$.cql = cql_node_prefix($6.cql, $2.buf, $4.buf);
109   }
110 | '>' searchTerm {
111       $$.rel = $0.rel;
112   } cqlQuery {
113     $$.cql = cql_node_prefix($4.cql, 0, $2.buf);
114    }
115 ;
116
117 /* unary NOT search TERM here .. */
118
119 boolean: 
120   AND | OR | NOT | PROX proxqualifiers {
121       $$ = $1;
122       $$.rel = $2.rel;
123   }
124   ;
125
126 proxqualifiers: 
127   Prelation { 
128       $$.rel = cql_node_mk_proxargs ($1.buf, 0, 0, 0);
129   }
130 |
131   PrelationO Pdistance {
132       $$.rel = cql_node_mk_proxargs ($1.buf, $2.buf, 0, 0);
133   }
134 |
135   PrelationO PdistanceO Punit {
136       $$.rel = cql_node_mk_proxargs ($1.buf, $2.buf, $3.buf, 0);
137   }
138 |
139   PrelationO PdistanceO PunitO Pordering {
140       $$.rel = cql_node_mk_proxargs ($1.buf, $2.buf, $3.buf, $4.buf);
141   }
142 |
143 { $$.rel = 0; }
144 ;
145
146 Punit: '/' searchTerm { 
147       $$ = $2;
148    }
149 ;
150
151 PunitO: '/' searchTerm {
152       $$ = $2;
153    } 
154
155 '/' { $$.buf[0] = 0; }
156 ;
157 Prelation: '/' baseRelation {
158     $$ = $2;
159 }
160 ;
161 PrelationO: '/' baseRelation {
162     $$ = $2;
163 }
164 | '/' { $$.buf[0] = 0; }
165 ;
166 Pdistance: '/' searchTerm { 
167     $$ = $2;
168 }
169 ;
170
171 PdistanceO: '/' searchTerm {
172     $$ = $2;
173 }
174 | '/' { $$.buf[0] = 0; }
175 ;
176 Pordering: '/' searchTerm { 
177     $$ = $2;
178 }
179 ;
180
181 relation: baseRelation modifiers {
182     struct cql_node *st = cql_node_mk_sc(/* index */ 0, 
183                                          /* relation */ $1.buf, 
184                                          /* term */ 0);
185
186     st->u.st.modifiers = $2.cql;
187     $$.rel = st;
188 }
189 ;
190
191 modifiers: '/' searchTerm modifiers
192
193     struct cql_node *mod = cql_node_mk_mod(0, $2.buf);
194
195     mod->u.mod.next = $3.cql;
196     $$.cql = mod;
197 }
198 |  
199
200     $$.cql = 0;
201 }
202 ;
203
204 baseRelation: 
205   '=' 
206 | '>' 
207 | '<'
208 | GE
209 | LE
210 | NE
211 | TERM
212 ;
213
214 index: 
215   searchTerm;
216
217 searchTerm:
218   TERM
219 | AND
220 | OR
221 | NOT
222 | PROX
223 ;
224
225 %%
226
227 int yyerror(char *s)
228 {
229     return 0;
230 }
231
232 #include "lexer.h"
233
234
235 int cql_parser_stream(CQL_parser cp,
236                       int (*getbyte)(void *client_data),
237                       void (*ungetbyte)(int b, void *client_data),
238                       void *client_data)
239 {
240     cp->getbyte = getbyte;
241     cp->ungetbyte = ungetbyte;
242     cp->client_data = client_data;
243     if (cp->top)
244         cql_node_destroy(cp->top);
245     cql_parse(cp);
246     if (cp->top)
247         return 0;
248     return -1;
249 }
250
251 CQL_parser cql_parser_create(void)
252 {
253     CQL_parser cp = (CQL_parser) malloc (sizeof(*cp));
254
255     cp->top = 0;
256     cp->getbyte = 0;
257     cp->ungetbyte = 0;
258     cp->client_data = 0;
259     cp->last_error = 0;
260     cp->last_pos = 0;
261     cp->nmem = nmem_create();
262     return cp;
263 }
264
265 void cql_parser_destroy(CQL_parser cp)
266 {
267     cql_node_destroy(cp->top);
268     nmem_destroy(cp->nmem);
269     free (cp);
270 }
271
272 struct cql_node *cql_parser_result(CQL_parser cp)
273 {
274     return cp->top;
275 }