Work on fixing CCL quote problem, bug #3539.
[yaz-moved-to-github.git] / src / cclfind.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5 /** 
6  * \file cclfind.c
7  * \brief Implements parsing of a CCL FIND query.
8  *
9  * This source file implements parsing of a CCL Query (ISO8777).
10  * The parser uses predictive parsing, but it does several tokens
11  * of lookahead in the handling of relational operations.. So
12  * it's not really pure.
13  */
14
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "cclp.h"
19
20 /* returns type of current lookahead */
21 #define KIND (cclp->look_token->kind)
22
23 /* move one token forward */
24 #define ADVANCE cclp->look_token = cclp->look_token->next
25
26 /**
27  * qual_val_type: test for existance of attribute type/value pair.
28  * qa:     Attribute array
29  * type:   Type of attribute to search for
30  * value:  Value of attribute to seach for
31  * return: 1 if found; 0 otherwise.
32  */
33 static int qual_val_type(ccl_qualifier_t *qa, int type, int value,
34                          char **attset)
35 {
36     int i;
37
38     if (!qa)
39         return 0;
40     for (i = 0; qa[i]; i++)
41     {
42         struct ccl_rpn_attr *q = ccl_qual_get_attr(qa[i]);
43         while (q)
44         {
45             if (q->type == type && q->kind == CCL_RPN_ATTR_NUMERIC &&
46                 q->value.numeric == value)
47             {
48                 if (attset)
49                     *attset = q->set;
50                 return 1;
51             }
52             q = q->next;
53         }
54     }
55     return 0;
56 }
57
58 /**
59  * strxcat: concatenate strings.
60  * n:      Null-terminated Destination string 
61  * src:    Source string to be appended (not null-terminated)
62  * len:    Length of source string.
63  */
64 static void strxcat(char *n, const char *src, int len)
65 {
66     while (*n)
67         n++;
68     while (--len >= 0)
69         *n++ = *src++;
70     *n = '\0';
71 }
72
73 /**
74  * copy_token_name: Return copy of CCL token name
75  * tp:      Pointer to token info.
76  * return:  malloc(3) allocated copy of token name.
77  */
78 static char *copy_token_name(struct ccl_token *tp)
79 {
80     char *str = (char *)xmalloc(tp->len + 1);
81     ccl_assert(str);
82     memcpy(str, tp->name, tp->len);
83     str[tp->len] = '\0';
84     return str;
85 }
86
87 /**
88  * mk_node: Create RPN node.
89  * kind:   Type of node.
90  * return: pointer to allocated node.
91  */
92 struct ccl_rpn_node *ccl_rpn_node_create(enum ccl_rpn_kind kind)
93 {
94     struct ccl_rpn_node *p;
95     p = (struct ccl_rpn_node *)xmalloc(sizeof(*p));
96     ccl_assert(p);
97     p->kind = kind;
98
99     switch(kind)
100     {
101     case CCL_RPN_TERM:
102         p->u.t.attr_list = 0;
103         p->u.t.term = 0;
104         p->u.t.qual = 0;
105         break;
106     default:
107         break;
108     }
109     return p;
110 }
111
112 /**
113  * ccl_rpn_delete: Delete RPN tree.
114  * rpn:   Pointer to tree.
115  */
116 void ccl_rpn_delete(struct ccl_rpn_node *rpn)
117 {
118     struct ccl_rpn_attr *attr, *attr1;
119     if (!rpn)
120         return;
121     switch (rpn->kind)
122     {
123     case CCL_RPN_AND:
124     case CCL_RPN_OR:
125     case CCL_RPN_NOT:
126         ccl_rpn_delete(rpn->u.p[0]);
127         ccl_rpn_delete(rpn->u.p[1]);
128         break;
129     case CCL_RPN_TERM:
130         xfree(rpn->u.t.term);
131         xfree(rpn->u.t.qual);
132         for (attr = rpn->u.t.attr_list; attr; attr = attr1)
133         {
134             attr1 = attr->next;
135             if (attr->kind == CCL_RPN_ATTR_STRING)
136                 xfree(attr->value.str);
137             if (attr->set)
138                 xfree(attr->set);
139             xfree(attr);
140         }
141         break;
142     case CCL_RPN_SET:
143         xfree(rpn->u.setname);
144         break;
145     case CCL_RPN_PROX:
146         ccl_rpn_delete(rpn->u.p[0]);
147         ccl_rpn_delete(rpn->u.p[1]);
148         ccl_rpn_delete(rpn->u.p[2]);
149         break;
150     }
151     xfree(rpn);
152 }
153
154 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa);
155
156 static int is_term_ok(int look, int *list)
157 {
158     for (;*list >= 0; list++)
159         if (look == *list)
160             return 1;
161     return 0;
162 }
163
164 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa);
165
166 static struct ccl_rpn_attr *add_attr_node(struct ccl_rpn_node *p,
167                                            const char *set, int type)
168 {
169     struct ccl_rpn_attr *n;
170     
171     n = (struct ccl_rpn_attr *)xmalloc(sizeof(*n));
172     ccl_assert(n);
173     if (set)
174         n->set = xstrdup(set);
175     else
176         n->set = 0;
177     n->type = type;
178     n->next = p->u.t.attr_list;
179     p->u.t.attr_list = n;
180     
181     return n;
182 }
183
184 /**
185  * add_attr_numeric: Add attribute (type/value) to RPN term node.
186  * p:     RPN node of type term.
187  * type:  Type of attribute
188  * value: Value of attribute
189  * set: Attribute set name
190  */
191 void ccl_add_attr_numeric(struct ccl_rpn_node *p, const char *set,
192                           int type, int value)
193 {
194     struct ccl_rpn_attr *n;
195
196     n = add_attr_node(p, set, type);
197     n->kind = CCL_RPN_ATTR_NUMERIC;
198     n->value.numeric = value;
199 }
200
201 void ccl_add_attr_string(struct ccl_rpn_node *p, const char *set,
202                          int type, char *value)
203 {
204     struct ccl_rpn_attr *n;
205
206     n = add_attr_node(p, set, type);
207     n->kind = CCL_RPN_ATTR_STRING;
208     n->value.str = xstrdup(value);
209 }
210
211
212 /**
213  * search_term: Parse CCL search term. 
214  * cclp:   CCL Parser
215  * qa:     Qualifier attributes already applied.
216  * term_list: tokens we accept as terms in context
217  * multi:  whether we accept "multiple" tokens
218  * return: pointer to node(s); NULL on error.
219  */
220 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
221                                           ccl_qualifier_t *qa,
222                                           int *term_list, int multi)
223 {
224     struct ccl_rpn_node *p_top = 0;
225     struct ccl_token *lookahead = cclp->look_token;
226     int and_list = 0;
227     int or_list = 0;
228     char *attset;
229     const char **truncation_aliases;
230     const char *t_default[2];
231
232     truncation_aliases =
233         ccl_qual_search_special(cclp->bibset, "truncation");
234     if (!truncation_aliases)
235     {
236         truncation_aliases = t_default;
237         t_default[0] = "?";
238         t_default[1] = 0;
239     }
240
241     if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
242         and_list = 1;
243     if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
244         or_list = 1;
245     while (1)
246     {
247         struct ccl_rpn_node *p;
248         size_t no, i;
249         int no_spaces = 0;
250         int relation_value = -1;
251         int position_value = -1;
252         int structure_value = -1;
253         int truncation_value = -1;
254         int completeness_value = -1;
255         int len = 0;
256         int left_trunc = 0;
257         int right_trunc = 0;
258         size_t max = 200;
259         if (and_list || or_list || !multi)
260             max = 1;
261         
262         /* ignore commas when dealing with and-lists .. */
263         if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
264         {
265             lookahead = lookahead->next;
266             ADVANCE;
267             continue;
268         }
269         /* go through each TERM token. If no truncation attribute is yet
270            met, then look for left/right truncation markers (?) and
271            set left_trunc/right_trunc/mid_trunc accordingly */
272         for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
273         {
274             for (i = 0; i<lookahead->len; i++)
275                 if (lookahead->name[i] == ' ')
276                     no_spaces++;
277             len += 1+lookahead->len+lookahead->ws_prefix_len;
278             left_trunc = lookahead->left_trunc;
279             right_trunc = lookahead->right_trunc;
280             lookahead = lookahead->next;
281         }
282
283         if (len == 0)
284             break;      /* no more terms . stop . */
285                 
286         /* create the term node, but wait a moment before adding the term */
287         p = ccl_rpn_node_create(CCL_RPN_TERM);
288         p->u.t.attr_list = NULL;
289         p->u.t.term = NULL;
290         if (qa && qa[0])
291         {
292             const char *n = ccl_qual_get_name(qa[0]);
293             if (n)
294                 p->u.t.qual = xstrdup(n);
295         }
296
297         /* go through all attributes and add them to the attribute list */
298         for (i=0; qa && qa[i]; i++)
299         {
300             struct ccl_rpn_attr *attr;
301             
302             for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
303                 switch(attr->kind)
304                 {
305                 case CCL_RPN_ATTR_STRING:
306                     ccl_add_attr_string(p, attr->set, attr->type,
307                                         attr->value.str);
308                     break;
309                 case CCL_RPN_ATTR_NUMERIC:
310                     if (attr->value.numeric > 0)
311                     {   /* deal only with REAL attributes (positive) */
312                         switch (attr->type)
313                         {
314                         case CCL_BIB1_REL:
315                             if (relation_value != -1)
316                                 continue;
317                             relation_value = attr->value.numeric;
318                             break;
319                         case CCL_BIB1_POS:
320                             if (position_value != -1)
321                                 continue;
322                             position_value = attr->value.numeric;
323                             break;
324                         case CCL_BIB1_STR:
325                             if (structure_value != -1)
326                                 continue;
327                             structure_value = attr->value.numeric;
328                             break;
329                         case CCL_BIB1_TRU:
330                             if (truncation_value != -1)
331                                 continue;
332                             truncation_value = attr->value.numeric;
333                             break;
334                         case CCL_BIB1_COM:
335                             if (completeness_value != -1)
336                                 continue;
337                             completeness_value = attr->value.numeric;
338                             break;
339                         }
340                         ccl_add_attr_numeric(p, attr->set, attr->type,
341                                              attr->value.numeric);
342                     }
343                 }
344         }
345         /* len now holds the number of characters in the RPN term */
346         /* no holds the number of CCL tokens (1 or more) */
347         
348         if (structure_value == -1 && 
349             qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
350         {   /* no structure attribute met. Apply either structure attribute 
351                WORD or PHRASE depending on number of CCL tokens */
352             if (no == 1 && no_spaces == 0)
353                 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
354             else
355                 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
356         }
357
358         /* make the RPN token */
359         p->u.t.term = (char *)xmalloc(len);
360         ccl_assert(p->u.t.term);
361         p->u.t.term[0] = '\0';
362         for (i = 0; i<no; i++)
363         {
364             const char *src_str = cclp->look_token->name;
365             size_t src_len = cclp->look_token->len;
366             
367             if (p->u.t.term[0] && cclp->look_token->ws_prefix_len)
368             {
369                 size_t len = strlen(p->u.t.term);
370                 memcpy(p->u.t.term + len, cclp->look_token->ws_prefix_buf,
371                        cclp->look_token->ws_prefix_len);
372                 p->u.t.term[len + cclp->look_token->ws_prefix_len] = '\0';
373             }
374             strxcat(p->u.t.term, src_str, src_len);
375             ADVANCE;
376         }
377
378         /* make the top node point to us.. */
379         if (p_top)
380         {
381             struct ccl_rpn_node *tmp;
382
383             if (or_list)
384                 tmp = ccl_rpn_node_create(CCL_RPN_OR);
385             else if (and_list)
386                 tmp = ccl_rpn_node_create(CCL_RPN_AND);
387             else
388                 tmp = ccl_rpn_node_create(CCL_RPN_AND);
389             tmp->u.p[0] = p_top;
390             tmp->u.p[1] = p;
391
392             p_top = tmp;
393         }
394         else
395             p_top = p;
396
397
398         if (left_trunc && right_trunc)
399         {
400             if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
401                                 &attset))
402             {
403                 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
404                 ccl_rpn_delete(p);
405                 return NULL;
406             }
407             ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
408         }
409         else if (right_trunc)
410         {
411             if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
412                                  &attset))
413             {
414                 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
415                 ccl_rpn_delete(p);
416                 return NULL;
417             }
418             ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
419         }
420         else if (left_trunc)
421         {
422             if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
423                                 &attset))
424             {
425                 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
426                 ccl_rpn_delete(p);
427                 return NULL;
428             }
429             ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
430         }
431         else
432         {
433             if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
434                                &attset))
435                 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
436         }
437         if (!multi)
438             break;
439     }
440     if (!p_top)
441         cclp->error_code = CCL_ERR_TERM_EXPECTED;
442     return p_top;
443 }
444
445 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
446 {
447     static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
448     return search_term_x(cclp, qa, list, 0);
449 }
450
451 static
452 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
453                                       ccl_qualifier_t *ap, char *attset)
454 {
455     int rel = 0;
456     struct ccl_rpn_node *p;
457
458     if (cclp->look_token->len == 1)
459     {
460         if (cclp->look_token->name[0] == '<')
461             rel = 1;
462         else if (cclp->look_token->name[0] == '=')
463             rel = 3;
464         else if (cclp->look_token->name[0] == '>')
465             rel = 5;
466     }
467     else if (cclp->look_token->len == 2)
468     {
469         if (!memcmp(cclp->look_token->name, "<=", 2))
470             rel = 2;
471         else if (!memcmp(cclp->look_token->name, ">=", 2))
472             rel = 4;
473         else if (!memcmp(cclp->look_token->name, "<>", 2))
474             rel = 6;
475     }
476     if (!rel)
477     {
478         cclp->error_code = CCL_ERR_BAD_RELATION;
479         return NULL;
480     }
481     ADVANCE;  /* skip relation */
482     if (rel == 3 &&
483         qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
484     {
485         /* allow - inside term and treat it as range _always_ */
486         /* relation is =. Extract "embedded" - to separate terms */
487         if (KIND == CCL_TOK_TERM)
488         {
489             size_t i;
490             for (i = 0; i<cclp->look_token->len; i++)
491             {
492                 if (cclp->look_token->name[i] == '-')
493                     break;
494             }
495             
496             if (cclp->look_token->len > 1 && i == 0)
497             {   /*  -xx*/
498                 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
499
500                 ntoken->kind = CCL_TOK_TERM;
501                 ntoken->name = cclp->look_token->name + 1;
502                 ntoken->len = cclp->look_token->len - 1;
503
504                 cclp->look_token->len = 1;
505                 cclp->look_token->name = "-";
506             }
507             else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
508             {   /* xx- */
509                 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
510
511                 ntoken->kind = CCL_TOK_TERM;
512                 ntoken->name = "-";
513                 ntoken->len = 1;
514
515                 (cclp->look_token->len)--;
516             }
517             else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
518             {   /* xx-yy */
519                 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
520                 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
521
522                 ntoken1->kind = CCL_TOK_TERM;  /* generate - */
523                 ntoken1->name = "-";
524                 ntoken1->len = 1;
525
526                 ntoken2->kind = CCL_TOK_TERM;  /* generate yy */
527                 ntoken2->name = cclp->look_token->name + (i+1);
528                 ntoken2->len = cclp->look_token->len - (i+1);
529
530                 cclp->look_token->len = i;     /* adjust xx */
531             }
532             else if (i == cclp->look_token->len &&
533                      cclp->look_token->next &&
534                      cclp->look_token->next->kind == CCL_TOK_TERM &&
535                      cclp->look_token->next->len > 1 &&
536                      cclp->look_token->next->name[0] == '-')
537                      
538             {   /* xx -yy */
539                 /* we _know_ that xx does not have - in it */
540                 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
541
542                 ntoken->kind = CCL_TOK_TERM;    /* generate - */
543                 ntoken->name = "-";
544                 ntoken->len = 1;
545
546                 (ntoken->next->name)++;        /* adjust yy */
547                 (ntoken->next->len)--; 
548             }
549         }
550     }
551         
552     if (rel == 3 &&
553         KIND == CCL_TOK_TERM &&
554         cclp->look_token->next && cclp->look_token->next->len == 1 &&
555         cclp->look_token->next->name[0] == '-')
556     {
557         struct ccl_rpn_node *p1;
558         if (!(p1 = search_term(cclp, ap)))
559             return NULL;
560         ADVANCE;                   /* skip '-' */
561         if (KIND == CCL_TOK_TERM)  /* = term - term  ? */
562         {
563             struct ccl_rpn_node *p2;
564             
565             if (!(p2 = search_term(cclp, ap)))
566             {
567                 ccl_rpn_delete(p1);
568                 return NULL;
569             }
570             p = ccl_rpn_node_create(CCL_RPN_AND);
571             p->u.p[0] = p1;
572             ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
573             p->u.p[1] = p2;
574             ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
575             return p;
576         }
577         else                       /* = term -    */
578         {
579             ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
580             return p1;
581         }
582     }
583     else if (rel == 3 &&
584              cclp->look_token->len == 1 &&
585              cclp->look_token->name[0] == '-')   /* = - term  ? */
586     {
587         ADVANCE;
588         if (!(p = search_term(cclp, ap)))
589             return NULL;
590         ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
591         return p;
592     }
593     else if (KIND == CCL_TOK_LP)
594     {
595         ADVANCE;
596         if (!(p = find_spec(cclp, ap)))
597             return NULL;
598         if (KIND != CCL_TOK_RP)
599         {
600             cclp->error_code = CCL_ERR_RP_EXPECTED;
601             ccl_rpn_delete(p);
602             return NULL;
603         }
604         ADVANCE;
605         return p;
606     }
607     else
608     {
609         if (!(p = search_terms(cclp, ap)))
610             return NULL;
611         ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
612         return p;
613     }
614     cclp->error_code = CCL_ERR_TERM_EXPECTED;
615     return NULL;
616 }
617
618 static
619 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
620 {
621     char *attset;
622     struct ccl_rpn_node *p;
623     
624     if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
625         || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
626         return qualifiers_order(cclp, ap, attset);
627
628     /* unordered relation */
629     if (KIND != CCL_TOK_EQ)
630     {
631         cclp->error_code = CCL_ERR_EQ_EXPECTED;
632         return NULL;
633     }
634     ADVANCE;
635     if (KIND == CCL_TOK_LP)
636     {
637         ADVANCE;
638         if (!(p = find_spec(cclp, ap)))
639         {
640             return NULL;
641         }
642         if (KIND != CCL_TOK_RP)
643         {
644             cclp->error_code = CCL_ERR_RP_EXPECTED;
645             ccl_rpn_delete(p);
646             return NULL;
647         }
648         ADVANCE;
649     }
650     else
651         p = search_terms(cclp, ap);
652     return p;
653 }
654
655 /**
656  * qualifier_list: Parse CCL qualifiers and search terms. 
657  * cclp:   CCL Parser
658  * la:     Token pointer to RELATION token.
659  * qa:     Qualifier attributes already applied.
660  * return: pointer to node(s); NULL on error.
661  */
662 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp, 
663                                            struct ccl_token *la,
664                                            ccl_qualifier_t *qa)
665 {
666     struct ccl_token *lookahead = cclp->look_token;
667     struct ccl_token *look_start = cclp->look_token;
668     ccl_qualifier_t *ap;
669     struct ccl_rpn_node *node = 0;
670     const char **field_str;
671     int no = 0;
672     int seq = 0;
673     int i;
674     int mode_merge = 1;
675 #if 0
676     if (qa)
677     {
678         cclp->error_code = CCL_ERR_DOUBLE_QUAL;
679         return NULL;
680     }
681 #endif
682     for (lookahead = cclp->look_token; lookahead != la;
683          lookahead=lookahead->next)
684         no++;
685     if (qa)
686         for (i=0; qa[i]; i++)
687             no++;
688     ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
689     ccl_assert(ap);
690
691     field_str = ccl_qual_search_special(cclp->bibset, "field");
692     if (field_str)
693     {
694         if (!strcmp(field_str[0], "or"))
695             mode_merge = 0;
696         else if (!strcmp(field_str[0], "merge"))
697             mode_merge = 1;
698     }
699     if (!mode_merge)
700     {
701         /* consider each field separately and OR */
702         lookahead = look_start;
703         while (lookahead != la)
704         {
705             ap[1] = 0;
706             seq = 0;
707             while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
708                                             lookahead->len, seq)) != 0)
709             {
710                 struct ccl_rpn_node *node_sub;
711                 cclp->look_token = la;
712                 
713                 node_sub = qualifier_relation(cclp, ap);
714                 if (!node_sub)
715                 {
716                     ccl_rpn_delete(node);
717                     xfree(ap);
718                     return 0;
719                 }
720                 if (node)
721                 {
722                     struct ccl_rpn_node *node_this = 
723                         ccl_rpn_node_create(CCL_RPN_OR);
724                     node_this->u.p[0] = node;
725                     node_this->u.p[1] = node_sub;
726                     node = node_this;
727                 }
728                 else
729                     node = node_sub;
730                 seq++;
731             }
732             if (seq == 0)
733             {
734                 cclp->look_token = lookahead;
735                 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
736                 xfree(ap);
737                 return NULL;
738             }
739             lookahead = lookahead->next;
740             if (lookahead->kind == CCL_TOK_COMMA)
741                 lookahead = lookahead->next;
742         }
743     }
744     else
745     {
746         /* merge attributes from ALL fields - including inherited ones */
747         while (1)
748         {
749             struct ccl_rpn_node *node_sub;
750             int found = 0;
751             lookahead = look_start;
752             for (i = 0; lookahead != la; i++)
753             {
754                 ap[i] = ccl_qual_search(cclp, lookahead->name,
755                                          lookahead->len, seq);
756                 if (ap[i])
757                     found++;
758                 if (!ap[i] && seq > 0)
759                     ap[i] = ccl_qual_search(cclp, lookahead->name,
760                                              lookahead->len, 0);
761                 if (!ap[i])
762                 {
763                     cclp->look_token = lookahead;
764                     cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
765                     xfree(ap);
766                     return NULL;
767                 }
768                 lookahead = lookahead->next;
769                 if (lookahead->kind == CCL_TOK_COMMA)
770                     lookahead = lookahead->next;
771             }
772             if (qa)
773             {
774                 ccl_qualifier_t *qa0 = qa;
775                 
776                 while (*qa0)
777                     ap[i++] = *qa0++;
778             }
779             ap[i] = NULL;
780             
781             if (!found)
782                 break;
783             
784             cclp->look_token = lookahead;
785             
786             node_sub = qualifier_relation(cclp, ap);
787             if (!node_sub)
788             {
789                 ccl_rpn_delete(node);
790                 break;
791             }
792             if (node)
793             {
794                 struct ccl_rpn_node *node_this = 
795                     ccl_rpn_node_create(CCL_RPN_OR);
796                 node_this->u.p[0] = node;
797                 node_this->u.p[1] = node_sub;
798                 node = node_this;
799             }
800             else
801                 node = node_sub;
802             seq++;
803         }
804     }
805     xfree(ap);
806     return node;
807 }
808
809
810 /**
811  * search_terms: Parse CCL search terms - including proximity.
812  * cclp:   CCL Parser
813  * qa:     Qualifier attributes already applied.
814  * return: pointer to node(s); NULL on error.
815  */
816 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
817 {
818     static int list[] = {
819         CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ, CCL_TOK_REL, CCL_TOK_SET, -1};
820     struct ccl_rpn_node *p1, *p2, *pn;
821     p1 = search_term_x(cclp, qa, list, 1);
822     if (!p1)
823         return NULL;
824     while (1)
825     {
826         if (KIND == CCL_TOK_PROX)
827         {
828             struct ccl_rpn_node *p_prox = 0;
829             /* ! word order specified */
830             /* % word order not specified */
831             p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
832             p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
833             memcpy(p_prox->u.t.term, cclp->look_token->name,
834                    cclp->look_token->len);
835             p_prox->u.t.term[cclp->look_token->len] = 0;
836             p_prox->u.t.attr_list = 0;
837
838             ADVANCE;
839             p2 = search_term_x(cclp, qa, list, 1);
840             if (!p2)
841             {
842                 ccl_rpn_delete(p1);
843                 return NULL;
844             }
845             pn = ccl_rpn_node_create(CCL_RPN_PROX);
846             pn->u.p[0] = p1;
847             pn->u.p[1] = p2;
848             pn->u.p[2] = p_prox;
849             p1 = pn;
850         }
851         else if (is_term_ok(KIND, list))
852         {
853             p2 = search_term_x(cclp, qa, list, 1);
854             if (!p2)
855             {
856                 ccl_rpn_delete(p1);
857                 return NULL;
858             }
859             pn = ccl_rpn_node_create(CCL_RPN_PROX);
860             pn->u.p[0] = p1;
861             pn->u.p[1] = p2;
862             pn->u.p[2] = 0;
863             p1 = pn;
864         }
865         else
866             break;
867     }
868     return p1;
869 }
870
871 /**
872  * search_elements: Parse CCL search elements
873  * cclp:   CCL Parser
874  * qa:     Qualifier attributes already applied.
875  * return: pointer to node(s); NULL on error.
876  */
877 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
878                                             ccl_qualifier_t *qa)
879 {
880     struct ccl_rpn_node *p1;
881     struct ccl_token *lookahead;
882     if (KIND == CCL_TOK_LP)
883     {
884         ADVANCE;
885         p1 = find_spec(cclp, qa);
886         if (!p1)
887             return NULL;
888         if (KIND != CCL_TOK_RP)
889         {
890             cclp->error_code = CCL_ERR_RP_EXPECTED;
891             ccl_rpn_delete(p1);
892             return NULL;
893         }
894         ADVANCE;
895         return p1;
896     }
897     else if (KIND == CCL_TOK_SET)
898     {
899         ADVANCE;
900         if (KIND == CCL_TOK_EQ)
901             ADVANCE;
902         if (KIND != CCL_TOK_TERM)
903         {
904             cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
905             return NULL;
906         }
907         p1 = ccl_rpn_node_create(CCL_RPN_SET);
908         p1->u.setname = copy_token_name(cclp->look_token);
909         ADVANCE;
910         return p1;
911     }
912     lookahead = cclp->look_token;
913
914     while (lookahead->kind==CCL_TOK_TERM)
915     {
916         lookahead = lookahead->next;
917         if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
918             return qualifier_list(cclp, lookahead, qa);
919         if (lookahead->kind != CCL_TOK_COMMA)
920             break;
921         lookahead = lookahead->next;
922     }
923     if (qa)
924         return search_terms(cclp, qa);
925     else
926     {
927         ccl_qualifier_t qa[2];
928         struct ccl_rpn_node *node = 0;
929         int seq;
930         lookahead = cclp->look_token;
931
932         qa[1] = 0;
933         for(seq = 0; ;seq++)
934         {
935             struct ccl_rpn_node *node_sub;
936             qa[0] = ccl_qual_search(cclp, "term", 4, seq);
937             if (!qa[0])
938                 break;
939
940             cclp->look_token = lookahead;
941
942             node_sub = search_terms(cclp, qa);
943             if (!node_sub)
944             {
945                 ccl_rpn_delete(node);
946                 return 0;
947             }
948             if (node)
949             {
950                 struct ccl_rpn_node *node_this = 
951                     ccl_rpn_node_create(CCL_RPN_OR);
952                 node_this->u.p[0] = node;
953                 node_this->u.p[1] = node_sub;
954                 node_this->u.p[2] = 0;
955                 node = node_this;
956             }
957             else
958                 node = node_sub;
959         }
960         if (!node)
961             node = search_terms(cclp, 0);
962         return node;
963     }
964 }
965
966 /**
967  * find_spec: Parse CCL find specification
968  * cclp:   CCL Parser
969  * qa:     Qualifier attributes already applied.
970  * return: pointer to node(s); NULL on error.
971  */
972 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
973 {
974     struct ccl_rpn_node *p1, *p2, *pn;
975     if (!(p1 = search_elements(cclp, qa)))
976         return NULL;
977     while (1)
978     {
979         switch (KIND)
980         {
981         case CCL_TOK_AND:
982             ADVANCE;
983             p2 = search_elements(cclp, qa);
984             if (!p2)
985             {
986                 ccl_rpn_delete(p1);
987                 return NULL;
988             }
989             pn = ccl_rpn_node_create(CCL_RPN_AND);
990             pn->u.p[0] = p1;
991             pn->u.p[1] = p2;
992             pn->u.p[2] = 0;
993             p1 = pn;
994             continue;
995         case CCL_TOK_OR:
996             ADVANCE;
997             p2 = search_elements(cclp, qa);
998             if (!p2)
999             {
1000                 ccl_rpn_delete(p1);
1001                 return NULL;
1002             }
1003             pn = ccl_rpn_node_create(CCL_RPN_OR);
1004             pn->u.p[0] = p1;
1005             pn->u.p[1] = p2;
1006             pn->u.p[2] = 0;
1007             p1 = pn;
1008             continue;
1009         case CCL_TOK_NOT:
1010             ADVANCE;
1011             p2 = search_elements(cclp, qa);
1012             if (!p2)
1013             {
1014                 ccl_rpn_delete(p1);
1015                 return NULL;
1016             }
1017             pn = ccl_rpn_node_create(CCL_RPN_NOT);
1018             pn->u.p[0] = p1;
1019             pn->u.p[1] = p2;
1020             pn->u.p[2] = 0;
1021             p1 = pn;
1022             continue;
1023         }
1024         break;
1025     }
1026     return p1;
1027 }
1028
1029 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1030 {
1031     struct ccl_rpn_node *p;
1032     struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1033     p = ccl_parser_find_token(cclp, list);
1034     ccl_token_del(list);
1035     return p;
1036 }
1037
1038 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp, 
1039                                            struct ccl_token *list)
1040 {
1041     struct ccl_rpn_node *p;
1042
1043     cclp->look_token = list;
1044     p = find_spec(cclp, NULL);
1045     if (p && KIND != CCL_TOK_EOL)
1046     {
1047         if (KIND == CCL_TOK_RP)
1048             cclp->error_code = CCL_ERR_BAD_RP;
1049         else
1050             cclp->error_code = CCL_ERR_OP_EXPECTED;
1051         ccl_rpn_delete(p);
1052         p = NULL;
1053     }
1054     cclp->error_pos = cclp->look_token->name;
1055     if (p)
1056         cclp->error_code = CCL_ERR_OK;
1057     else
1058         cclp->error_code = cclp->error_code;
1059     return p;
1060 }
1061
1062 /**
1063  * ccl_find_str: Parse CCL find - string representation
1064  * bibset:  Bibset to be used for the parsing
1065  * str:     String to be parsed
1066  * error:   Pointer to integer. Holds error no. on completion.
1067  * pos:     Pointer to char position. Holds approximate error position.
1068  * return:  RPN tree on successful completion; NULL otherwise.
1069  */
1070 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1071                                   int *error, int *pos)
1072 {
1073     CCL_parser cclp = ccl_parser_create(bibset);
1074     struct ccl_token *list;
1075     struct ccl_rpn_node *p;
1076
1077     list = ccl_parser_tokenize(cclp, str);
1078     p = ccl_parser_find_token(cclp, list);
1079
1080     *error = cclp->error_code;
1081     if (*error)
1082         *pos = cclp->error_pos - str;
1083     ccl_parser_destroy(cclp);
1084     ccl_token_del(list);
1085     return p;
1086 }
1087
1088 /*
1089  * Local variables:
1090  * c-basic-offset: 4
1091  * c-file-style: "Stroustrup"
1092  * indent-tabs-mode: nil
1093  * End:
1094  * vim: shiftwidth=4 tabstop=8 expandtab
1095  */
1096