ignore cql2pqf and cql2xcql
[yaz-moved-to-github.git] / cql / cql.y
1 /* $Id: cql.y,v 1.4 2003-04-11 15:53:39 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 EXACT ALL ANY GE LE NE SCR
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 boolean: 
118   AND | OR | NOT | PROX proxqualifiers {
119       $$ = $1;
120       $$.rel = $2.rel;
121   }
122   ;
123
124 proxqualifiers: 
125   Prelation { 
126       $$.rel = cql_node_mk_proxargs ($1.buf, 0, 0, 0);
127   }
128 |
129   PrelationO Pdistance {
130       $$.rel = cql_node_mk_proxargs ($1.buf, $2.buf, 0, 0);
131   }
132 |
133   PrelationO PdistanceO Punit {
134       $$.rel = cql_node_mk_proxargs ($1.buf, $2.buf, $3.buf, 0);
135   }
136 |
137   PrelationO PdistanceO PunitO Pordering {
138       $$.rel = cql_node_mk_proxargs ($1.buf, $2.buf, $3.buf, $4.buf);
139   }
140 |
141 { $$.rel = 0; }
142 ;
143
144 Punit: '/' searchTerm { 
145       $$ = $2;
146    }
147 ;
148
149 PunitO: '/' searchTerm {
150       $$ = $2;
151    } 
152
153 '/' { $$.buf[0] = 0; }
154 ;
155 Prelation: '/' baseRelation {
156     $$ = $2;
157 }
158 ;
159 PrelationO: '/' baseRelation {
160     $$ = $2;
161 }
162 | '/' { $$.buf[0] = 0; }
163 ;
164 Pdistance: '/' searchTerm { 
165     $$ = $2;
166 }
167 ;
168
169 PdistanceO: '/' searchTerm {
170     $$ = $2;
171 }
172 | '/' { $$.buf[0] = 0; }
173 ;
174 Pordering: '/' searchTerm { 
175     $$ = $2;
176 }
177 ;
178
179 relation: baseRelation modifiers {
180     struct cql_node *st = cql_node_mk_sc(/* index */ 0, 
181                                          /* relation */ $1.buf, 
182                                          /* term */ 0);
183
184     st->u.st.modifiers = $2.cql;
185     $$.rel = st;
186 }
187 ;
188
189 modifiers: '/' searchTerm modifiers
190
191     struct cql_node *mod = cql_node_mk_mod(0, $2.buf);
192
193     mod->u.mod.next = $3.cql;
194     $$.cql = mod;
195 }
196 |  
197
198     $$.cql = 0;
199 }
200 ;
201
202 baseRelation: 
203   '=' 
204 | '>' 
205 | '<'
206 | GE
207 | LE
208 | NE
209 | EXACT 
210 | ALL
211 | ANY
212 | SCR
213 ;
214
215 index: 
216   searchTerm;
217
218 searchTerm:
219   TERM
220 | AND
221 | OR
222 | NOT
223 | EXACT
224 | ALL
225 | ANY
226 | PROX
227 ;
228
229 %%
230
231 int yyerror(char *s)
232 {
233     return 0;
234 }
235
236 #include "lexer.c"
237
238
239 int cql_parser_stream(CQL_parser cp,
240                       int (*getbyte)(void *client_data),
241                       void (*ungetbyte)(int b, void *client_data),
242                       void *client_data)
243 {
244     cp->getbyte = getbyte;
245     cp->ungetbyte = ungetbyte;
246     cp->client_data = client_data;
247     cql_parse(cp);
248     if (cp->top)
249         return 0;
250     return -1;
251 }
252
253 CQL_parser cql_parser_create(void)
254 {
255     CQL_parser cp = (CQL_parser) malloc (sizeof(*cp));
256
257     cp->top = 0;
258     cp->getbyte = 0;
259     cp->ungetbyte = 0;
260     cp->client_data = 0;
261     cp->last_error = 0;
262     cp->last_pos = 0;
263     cp->nmem = nmem_create();
264     return cp;
265 }
266
267 void cql_parser_destroy(CQL_parser cp)
268 {
269     cql_node_destroy(cp->top);
270     nmem_destroy(cp->nmem);
271     free (cp);
272 }
273
274 struct cql_node *cql_parser_result(CQL_parser cp)
275 {
276     return cp->top;
277 }