source: trunk/gsdl/packages/yaz/ccl/cclfind.c@ 1343

Last change on this file since 1343 was 1343, checked in by johnmcp, 24 years ago

Added the YAZ toolkit source to the packages directory (for z39.50 stuff)

  • Property svn:keywords set to Author Date Id Revision
File size: 23.9 KB
Line 
1/*
2 * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3 *
4 * The EUROPAGATE consortium members are:
5 *
6 * University College Dublin
7 * Danmarks Teknologiske Videnscenter
8 * An Chomhairle Leabharlanna
9 * Consejo Superior de Investigaciones Cientificas
10 *
11 * Permission to use, copy, modify, distribute, and sell this software and
12 * its documentation, in whole or in part, for any purpose, is hereby granted,
13 * provided that:
14 *
15 * 1. This copyright and permission notice appear in all copies of the
16 * software and its documentation. Notices of copyright or attribution
17 * which appear at the beginning of any file must remain unchanged.
18 *
19 * 2. The names of EUROPAGATE or the project partners may not be used to
20 * endorse or promote products derived from this software without specific
21 * prior written permission.
22 *
23 * 3. Users of this software (implementors and gateway operators) agree to
24 * inform the EUROPAGATE consortium of their use of the software. This
25 * information will be used to evaluate the EUROPAGATE project and the
26 * software, and to plan further developments. The consortium may use
27 * the information in later publications.
28 *
29 * 4. Users of this software agree to make their best efforts, when
30 * documenting their use of the software, to acknowledge the EUROPAGATE
31 * consortium, and the role played by the software in their work.
32 *
33 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34 * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36 * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37 * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38 * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39 * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40 * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41 * USE OR PERFORMANCE OF THIS SOFTWARE.
42 *
43 */
44/* CCL find (to rpn conversion)
45 * Europagate, 1995
46 *
47 * $Log$
48 * Revision 1.1 2000/08/03 03:09:56 johnmcp
49 * Added the YAZ toolkit source to the packages directory (for z39.50 stuff)
50 *
51 * Revision 1.15 2000/02/24 23:49:13 adam
52 * Fixed memory allocation problem.
53 *
54 * Revision 1.14 2000/01/31 13:15:21 adam
55 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
56 * that some characters are not surrounded by spaces in resulting term.
57 * ILL-code updates.
58 *
59 * Revision 1.13 1999/12/22 13:13:32 adam
60 * Search terms may include "operators" without causing error.
61 *
62 * Revision 1.12 1999/11/30 13:47:11 adam
63 * Improved installation. Moved header files to include/yaz.
64 *
65 * Revision 1.11 1999/03/31 11:15:37 adam
66 * Fixed memory leaks in ccl_find_str and ccl_qual_rm.
67 *
68 * Revision 1.10 1998/02/11 11:53:33 adam
69 * Changed code so that it compiles as C++.
70 *
71 * Revision 1.9 1997/09/29 08:56:37 adam
72 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
73 * and a create/destructers ccl_parser_create/ccl_parser/destory has
74 * been added.
75 *
76 * Revision 1.8 1997/09/01 08:48:11 adam
77 * New windows NT/95 port using MSV5.0. Only a few changes made
78 * to avoid warnings.
79 *
80 * Revision 1.7 1997/05/14 06:53:26 adam
81 * C++ support.
82 *
83 * Revision 1.6 1997/04/30 08:52:06 quinn
84 * Null
85 *
86 * Revision 1.5 1996/10/11 15:00:24 adam
87 * CCL parser from Europagate Email gateway 1.0.
88 *
89 * Revision 1.16 1996/01/08 08:41:13 adam
90 * Removed unused function.
91 *
92 * Revision 1.15 1995/07/20 08:14:34 adam
93 * Qualifiers were observed too often. Instead tokens are treated as
94 * qualifiers only when separated by comma.
95 *
96 * Revision 1.14 1995/05/16 09:39:26 adam
97 * LICENSE.
98 *
99 * Revision 1.13 1995/04/17 09:31:42 adam
100 * Improved handling of qualifiers. Aliases or reserved words.
101 *
102 * Revision 1.12 1995/03/20 15:27:43 adam
103 * Minor changes.
104 *
105 * Revision 1.11 1995/02/23 08:31:59 adam
106 * Changed header.
107 *
108 * Revision 1.9 1995/02/16 13:20:06 adam
109 * Spell fix.
110 *
111 * Revision 1.8 1995/02/14 19:59:42 adam
112 * Removed a syntax error.
113 *
114 * Revision 1.7 1995/02/14 19:55:10 adam
115 * Header files ccl.h/cclp.h are gone! They have been merged an
116 * moved to ../include/ccl.h.
117 * Node kind(s) in ccl_rpn_node have changed names.
118 *
119 * Revision 1.6 1995/02/14 16:20:55 adam
120 * Qualifiers are read from a file now.
121 *
122 * Revision 1.5 1995/02/14 14:12:41 adam
123 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
124 *
125 * Revision 1.4 1995/02/14 13:16:29 adam
126 * Left and/or right truncation implemented.
127 *
128 * Revision 1.3 1995/02/14 10:25:56 adam
129 * The constructions 'qualifier rel term ...' implemented.
130 *
131 * Revision 1.2 1995/02/13 15:15:07 adam
132 * Added handling of qualifiers. Not finished yet.
133 *
134 * Revision 1.1 1995/02/13 12:35:20 adam
135 * First version of CCL. Qualifiers aren't handled yet.
136 *
137 */
138
139#include <stdio.h>
140#include <stdlib.h>
141#include <string.h>
142
143#include <yaz/ccl.h>
144
145/* returns type of current lookahead */
146#define KIND (cclp->look_token->kind)
147
148/* move one token forward */
149#define ADVANCE cclp->look_token = cclp->look_token->next
150
151/*
152 * qual_val_type: test for existance of attribute type/value pair.
153 * qa: Attribute array
154 * type: Type of attribute to search for
155 * value: Value of attribute to seach for
156 * return: 1 if found; 0 otherwise.
157 */
158static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value)
159{
160 int i;
161 struct ccl_rpn_attr *q;
162
163 if (!qa)
164 return 0;
165 for (i = 0; (q=qa[i]); i++)
166 while (q)
167 {
168 if (q->type == type && q->value == value)
169 return 1;
170 q = q->next;
171 }
172 return 0;
173}
174
175/*
176 * strxcat: concatenate strings.
177 * n: Null-terminated Destination string
178 * src: Source string to be appended (not null-terminated)
179 * len: Length of source string.
180 */
181static void strxcat (char *n, const char *src, int len)
182{
183 while (*n)
184 n++;
185 while (--len >= 0)
186 *n++ = *src++;
187 *n = '\0';
188}
189
190/*
191 * copy_token_name: Return copy of CCL token name
192 * tp: Pointer to token info.
193 * return: malloc(3) allocated copy of token name.
194 */
195static char *copy_token_name (struct ccl_token *tp)
196{
197 char *str = (char *)malloc (tp->len + 1);
198 ccl_assert (str);
199 memcpy (str, tp->name, tp->len);
200 str[tp->len] = '\0';
201 return str;
202}
203
204/*
205 * mk_node: Create RPN node.
206 * kind: Type of node.
207 * return: pointer to allocated node.
208 */
209static struct ccl_rpn_node *mk_node (int kind)
210{
211 struct ccl_rpn_node *p;
212 p = (struct ccl_rpn_node *)malloc (sizeof(*p));
213 ccl_assert (p);
214 p->kind = kind;
215 return p;
216}
217
218/*
219 * ccl_rpn_delete: Delete RPN tree.
220 * rpn: Pointer to tree.
221 */
222void ccl_rpn_delete (struct ccl_rpn_node *rpn)
223{
224 struct ccl_rpn_attr *attr, *attr1;
225 if (!rpn)
226 return;
227 switch (rpn->kind)
228 {
229 case CCL_RPN_AND:
230 case CCL_RPN_OR:
231 case CCL_RPN_NOT:
232 ccl_rpn_delete (rpn->u.p[0]);
233 ccl_rpn_delete (rpn->u.p[1]);
234 break;
235 case CCL_RPN_TERM:
236 free (rpn->u.t.term);
237 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
238 {
239 attr1 = attr->next;
240 free (attr);
241 }
242 break;
243 case CCL_RPN_SET:
244 free (rpn->u.setname);
245 break;
246 case CCL_RPN_PROX:
247 ccl_rpn_delete (rpn->u.p[0]);
248 ccl_rpn_delete (rpn->u.p[1]);
249 break;
250 }
251 free (rpn);
252}
253
254static struct ccl_rpn_node *find_spec (CCL_parser cclp,
255 struct ccl_rpn_attr **qa);
256
257static int is_term_ok (int look, int *list)
258{
259 for (;*list >= 0; list++)
260 if (look == *list)
261 return 1;
262 return 0;
263}
264
265static struct ccl_rpn_node *search_terms (CCL_parser cclp,
266 struct ccl_rpn_attr **qa);
267
268/*
269 * add_attr: Add attribute (type/value) to RPN term node.
270 * p: RPN node of type term.
271 * type: Type of attribute
272 * value: Value of attribute
273 */
274static void add_attr (struct ccl_rpn_node *p, int type, int value)
275{
276 struct ccl_rpn_attr *n;
277
278 n = (struct ccl_rpn_attr *)malloc (sizeof(*n));
279 ccl_assert (n);
280 n->type = type;
281 n->value = value;
282 n->next = p->u.t.attr_list;
283 p->u.t.attr_list = n;
284}
285
286/*
287 * search_term: Parse CCL search term.
288 * cclp: CCL Parser
289 * qa: Qualifier attributes already applied.
290 * return: pointer to node(s); NULL on error.
291 */
292static struct ccl_rpn_node *search_term_x (CCL_parser cclp,
293 struct ccl_rpn_attr **qa,
294 int *term_list)
295{
296 struct ccl_rpn_attr *qa_tmp[2];
297 struct ccl_rpn_node *p;
298 struct ccl_token *lookahead = cclp->look_token;
299 int len = 0;
300 size_t no, i;
301 int left_trunc = 0;
302 int right_trunc = 0;
303 int mid_trunc = 0;
304 int relation_value = -1;
305 int position_value = -1;
306 int structure_value = -1;
307 int truncation_value = -1;
308 int completeness_value = -1;
309
310 if (!is_term_ok(KIND, term_list))
311 {
312 cclp->error_code = CCL_ERR_TERM_EXPECTED;
313 return NULL;
314 }
315 /* create the term node, but wait a moment before adding the term */
316 p = mk_node (CCL_RPN_TERM);
317 p->u.t.attr_list = NULL;
318 p->u.t.term = NULL;
319
320 if (!qa)
321 {
322 /* no qualifier(s) applied. Use 'term' if it is defined */
323
324 qa = qa_tmp;
325 ccl_assert (qa);
326 qa[0] = ccl_qual_search (cclp, "term", 4);
327 qa[1] = NULL;
328 }
329
330 /* go through all attributes and add them to the attribute list */
331 for (i=0; qa && qa[i]; i++)
332 {
333 struct ccl_rpn_attr *attr;
334
335 for (attr = qa[i]; attr; attr = attr->next)
336 if (attr->value > 0)
337 { /* deal only with REAL attributes (positive) */
338 switch (attr->type)
339 {
340 case CCL_BIB1_REL:
341 if (relation_value != -1)
342 continue;
343 relation_value = attr->value;
344 break;
345 case CCL_BIB1_POS:
346 if (position_value != -1)
347 continue;
348 position_value = attr->value;
349 break;
350 case CCL_BIB1_STR:
351 if (structure_value != -1)
352 continue;
353 structure_value = attr->value;
354 break;
355 case CCL_BIB1_TRU:
356 if (truncation_value != -1)
357 continue;
358 truncation_value = attr->value;
359 break;
360 case CCL_BIB1_COM:
361 if (completeness_value != -1)
362 continue;
363 completeness_value = attr->value;
364 break;
365 }
366 add_attr (p, attr->type, attr->value);
367 }
368 }
369 /* go through each TERM token. If no truncation attribute is yet
370 met, then look for left/right truncation markers (?) and
371 set left_trunc/right_trunc/mid_trunc accordingly */
372 for (no = 0; is_term_ok(lookahead->kind, term_list); no++)
373 {
374 for (i = 0; i<lookahead->len; i++)
375 if (truncation_value == -1 && lookahead->name[i] == '?')
376 {
377 if (no == 0 && i == 0 && lookahead->len >= 1)
378 left_trunc = 1;
379 else if (!is_term_ok(lookahead->next->kind, term_list) &&
380 i == lookahead->len-1 && i >= 1)
381 right_trunc = 1;
382 else
383 mid_trunc = 1;
384 }
385 len += 1+lookahead->len;
386 lookahead = lookahead->next;
387 }
388 /* len now holds the number of characters in the RPN term */
389 /* no holds the number of CCL tokens (1 or more) */
390
391 if (structure_value == -1 &&
392 qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP))
393 { /* no structure attribute met. Apply either structure attribute
394 WORD or PHRASE depending on number of CCL tokens */
395 if (no == 1)
396 add_attr (p, CCL_BIB1_STR, 2);
397 else
398 add_attr (p, CCL_BIB1_STR, 1);
399 }
400
401 /* make the RPN token */
402 p->u.t.term = (char *)malloc (len);
403 ccl_assert (p->u.t.term);
404 p->u.t.term[0] = '\0';
405 for (i = 0; i<no; i++)
406 {
407 const char *src_str = cclp->look_token->name;
408 int src_len = cclp->look_token->len;
409
410 if (i == 0 && left_trunc)
411 {
412 src_len--;
413 src_str++;
414 }
415 else if (i == no-1 && right_trunc)
416 src_len--;
417 if (src_len)
418 {
419 int len = strlen(p->u.t.term);
420 if (len &&
421 !strchr("-+", *src_str) &&
422 !strchr("-+", p->u.t.term[len-1]))
423 {
424 strcat (p->u.t.term, " ");
425 }
426 }
427 strxcat (p->u.t.term, src_str, src_len);
428 ADVANCE;
429 }
430 if (left_trunc && right_trunc)
431 {
432 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH))
433 {
434 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
435 ccl_rpn_delete (p);
436 return NULL;
437 }
438 add_attr (p, CCL_BIB1_TRU, 3);
439 }
440 else if (right_trunc)
441 {
442 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT))
443 {
444 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
445 ccl_rpn_delete (p);
446 return NULL;
447 }
448 add_attr (p, CCL_BIB1_TRU, 1);
449 }
450 else if (left_trunc)
451 {
452 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT))
453 {
454 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
455 ccl_rpn_delete (p);
456 return NULL;
457 }
458 add_attr (p, CCL_BIB1_TRU, 2);
459 }
460 else
461 {
462 if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE))
463 add_attr (p, CCL_BIB1_TRU, 100);
464 }
465 return p;
466}
467
468static struct ccl_rpn_node *search_term (CCL_parser cclp,
469 struct ccl_rpn_attr **qa)
470{
471 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
472 return search_term_x(cclp, qa, list);
473}
474
475/*
476 * qualifiers: Parse CCL qualifiers and search terms.
477 * cclp: CCL Parser
478 * la: Token pointer to RELATION token.
479 * qa: Qualifier attributes already applied.
480 * return: pointer to node(s); NULL on error.
481 */
482static struct ccl_rpn_node *qualifiers (CCL_parser cclp, struct ccl_token *la,
483 struct ccl_rpn_attr **qa)
484{
485 struct ccl_token *lookahead = cclp->look_token;
486 struct ccl_rpn_attr **ap;
487 int no = 0;
488 int i, rel;
489#if 0
490 if (qa)
491 {
492 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
493 return NULL;
494 }
495#endif
496 for (lookahead = cclp->look_token; lookahead != la;
497 lookahead=lookahead->next)
498 no++;
499 if (qa)
500 for (i=0; qa[i]; i++)
501 no++;
502 ap = (struct ccl_rpn_attr **)malloc ((no+1) * sizeof(*ap));
503 ccl_assert (ap);
504 for (i = 0; cclp->look_token != la; i++)
505 {
506 ap[i] = ccl_qual_search (cclp, cclp->look_token->name,
507 cclp->look_token->len);
508 if (!ap[i])
509 {
510 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
511 free (ap);
512 return NULL;
513 }
514 ADVANCE;
515 if (KIND == CCL_TOK_COMMA)
516 ADVANCE;
517 }
518 if (qa)
519 while (*qa)
520 ap[i++] = *qa++;
521 ap[i] = NULL;
522 if (!qual_val_type (ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER))
523 {
524 /* unordered relation */
525 struct ccl_rpn_node *p;
526 if (KIND != CCL_TOK_EQ)
527 {
528 cclp->error_code = CCL_ERR_EQ_EXPECTED;
529 free (ap);
530 return NULL;
531 }
532 ADVANCE;
533 if (KIND == CCL_TOK_LP)
534 {
535 ADVANCE;
536 if (!(p = find_spec (cclp, ap)))
537 {
538 free (ap);
539 return NULL;
540 }
541 if (KIND != CCL_TOK_RP)
542 {
543 cclp->error_code = CCL_ERR_RP_EXPECTED;
544 ccl_rpn_delete (p);
545 free (ap);
546 return NULL;
547 }
548 ADVANCE;
549 }
550 else
551 p = search_terms (cclp, ap);
552 free (ap);
553 return p;
554 }
555 /* ordered relation ... */
556 rel = 0;
557 if (cclp->look_token->len == 1)
558 {
559 if (cclp->look_token->name[0] == '<')
560 rel = 1;
561 else if (cclp->look_token->name[0] == '=')
562 rel = 3;
563 else if (cclp->look_token->name[0] == '>')
564 rel = 5;
565 }
566 else if (cclp->look_token->len == 2)
567 {
568 if (!memcmp (cclp->look_token->name, "<=", 2))
569 rel = 2;
570 else if (!memcmp (cclp->look_token->name, ">=", 2))
571 rel = 4;
572 else if (!memcmp (cclp->look_token->name, "<>", 2))
573 rel = 6;
574 }
575 if (!rel)
576 cclp->error_code = CCL_ERR_BAD_RELATION;
577 else
578 {
579 struct ccl_rpn_node *p;
580
581 ADVANCE; /* skip relation */
582 if (KIND == CCL_TOK_TERM &&
583 cclp->look_token->next->kind == CCL_TOK_MINUS)
584 {
585 struct ccl_rpn_node *p1;
586 if (!(p1 = search_term (cclp, ap)))
587 {
588 free (ap);
589 return NULL;
590 }
591 ADVANCE; /* skip '-' */
592 if (KIND == CCL_TOK_TERM) /* = term - term ? */
593 {
594 struct ccl_rpn_node *p2;
595
596 if (!(p2 = search_term (cclp, ap)))
597 {
598 ccl_rpn_delete (p1);
599 free (ap);
600 return NULL;
601 }
602 p = mk_node (CCL_RPN_AND);
603 p->u.p[0] = p1;
604 add_attr (p1, CCL_BIB1_REL, 4);
605 p->u.p[1] = p2;
606 add_attr (p2, CCL_BIB1_REL, 2);
607 free (ap);
608 return p;
609 }
610 else /* = term - */
611 {
612 add_attr (p1, CCL_BIB1_REL, 4);
613 free (ap);
614 return p1;
615 }
616 }
617 else if (KIND == CCL_TOK_MINUS) /* = - term ? */
618 {
619 ADVANCE;
620 if (!(p = search_term (cclp, ap)))
621 {
622 free (ap);
623 return NULL;
624 }
625 add_attr (p, CCL_BIB1_REL, 2);
626 free (ap);
627 return p;
628 }
629 else if (KIND == CCL_TOK_LP)
630 {
631 ADVANCE;
632 if (!(p = find_spec (cclp, ap)))
633 {
634 free (ap);
635 return NULL;
636 }
637 if (KIND != CCL_TOK_RP)
638 {
639 cclp->error_code = CCL_ERR_RP_EXPECTED;
640 ccl_rpn_delete (p);
641 free (ap);
642 return NULL;
643 }
644 ADVANCE;
645 free (ap);
646 return p;
647 }
648 else
649 {
650 if (!(p = search_terms (cclp, ap)))
651 {
652 free (ap);
653 return NULL;
654 }
655 add_attr (p, CCL_BIB1_REL, rel);
656 free (ap);
657 return p;
658 }
659 cclp->error_code = CCL_ERR_TERM_EXPECTED;
660 }
661 free (ap);
662 return NULL;
663}
664
665/*
666 * search_terms: Parse CCL search terms - including proximity.
667 * cclp: CCL Parser
668 * qa: Qualifier attributes already applied.
669 * return: pointer to node(s); NULL on error.
670 */
671static struct ccl_rpn_node *search_terms (CCL_parser cclp,
672 struct ccl_rpn_attr **qa)
673{
674 static int list[] = {
675 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
676 CCL_TOK_REL, CCL_TOK_MINUS, -1};
677 struct ccl_rpn_node *p1, *p2, *pn;
678 p1 = search_term_x (cclp, qa, list);
679 if (!p1)
680 return NULL;
681 while (1)
682 {
683 if (KIND == CCL_TOK_PROX)
684 {
685 ADVANCE;
686 p2 = search_term_x (cclp, qa, list);
687 if (!p2)
688 {
689 ccl_rpn_delete (p1);
690 return NULL;
691 }
692 pn = mk_node (CCL_RPN_PROX);
693 pn->u.p[0] = p1;
694 pn->u.p[1] = p2;
695 p1 = pn;
696 }
697 else if (is_term_ok(KIND, list))
698 {
699 p2 = search_term_x (cclp, qa, list);
700 if (!p2)
701 {
702 ccl_rpn_delete (p1);
703 return NULL;
704 }
705 pn = mk_node (CCL_RPN_PROX);
706 pn->u.p[0] = p1;
707 pn->u.p[1] = p2;
708 p1 = pn;
709 }
710 else
711 break;
712 }
713 return p1;
714}
715
716/*
717 * search_elements: Parse CCL search elements
718 * cclp: CCL Parser
719 * qa: Qualifier attributes already applied.
720 * return: pointer to node(s); NULL on error.
721 */
722static struct ccl_rpn_node *search_elements (CCL_parser cclp,
723 struct ccl_rpn_attr **qa)
724{
725 struct ccl_rpn_node *p1;
726 struct ccl_token *lookahead;
727 if (KIND == CCL_TOK_LP)
728 {
729 ADVANCE;
730 p1 = find_spec (cclp, qa);
731 if (!p1)
732 return NULL;
733 if (KIND != CCL_TOK_RP)
734 {
735 cclp->error_code = CCL_ERR_RP_EXPECTED;
736 ccl_rpn_delete (p1);
737 return NULL;
738 }
739 ADVANCE;
740 return p1;
741 }
742 else if (KIND == CCL_TOK_SET)
743 {
744 ADVANCE;
745 if (KIND == CCL_TOK_EQ)
746 ADVANCE;
747 if (KIND != CCL_TOK_TERM)
748 {
749 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
750 return NULL;
751 }
752 p1 = mk_node (CCL_RPN_SET);
753 p1->u.setname = copy_token_name (cclp->look_token);
754 ADVANCE;
755 return p1;
756 }
757 lookahead = cclp->look_token;
758
759 while (lookahead->kind==CCL_TOK_TERM)
760 {
761 lookahead = lookahead->next;
762 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
763 return qualifiers (cclp, lookahead, qa);
764 if (lookahead->kind != CCL_TOK_COMMA)
765 break;
766 lookahead = lookahead->next;
767 }
768 return search_terms (cclp, qa);
769}
770
771/*
772 * find_spec: Parse CCL find specification
773 * cclp: CCL Parser
774 * qa: Qualifier attributes already applied.
775 * return: pointer to node(s); NULL on error.
776 */
777static struct ccl_rpn_node *find_spec (CCL_parser cclp,
778 struct ccl_rpn_attr **qa)
779{
780 struct ccl_rpn_node *p1, *p2, *pn;
781 if (!(p1 = search_elements (cclp, qa)))
782 return NULL;
783 while (1)
784 {
785 switch (KIND)
786 {
787 case CCL_TOK_AND:
788 ADVANCE;
789 p2 = search_elements (cclp, qa);
790 if (!p2)
791 {
792 ccl_rpn_delete (p1);
793 return NULL;
794 }
795 pn = mk_node (CCL_RPN_AND);
796 pn->u.p[0] = p1;
797 pn->u.p[1] = p2;
798 p1 = pn;
799 continue;
800 case CCL_TOK_OR:
801 ADVANCE;
802 p2 = search_elements (cclp, qa);
803 if (!p2)
804 {
805 ccl_rpn_delete (p1);
806 return NULL;
807 }
808 pn = mk_node (CCL_RPN_OR);
809 pn->u.p[0] = p1;
810 pn->u.p[1] = p2;
811 p1 = pn;
812 continue;
813 case CCL_TOK_NOT:
814 ADVANCE;
815 p2 = search_elements (cclp, qa);
816 if (!p2)
817 {
818 ccl_rpn_delete (p1);
819 return NULL;
820 }
821 pn = mk_node (CCL_RPN_NOT);
822 pn->u.p[0] = p1;
823 pn->u.p[1] = p2;
824 p1 = pn;
825 continue;
826 }
827 break;
828 }
829 return p1;
830}
831
832struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list)
833{
834 struct ccl_rpn_node *p;
835
836 cclp->look_token = list;
837 p = find_spec (cclp, NULL);
838 if (p && KIND != CCL_TOK_EOL)
839 {
840 if (KIND == CCL_TOK_RP)
841 cclp->error_code = CCL_ERR_BAD_RP;
842 else
843 cclp->error_code = CCL_ERR_OP_EXPECTED;
844 ccl_rpn_delete (p);
845 p = NULL;
846 }
847 cclp->error_pos = cclp->look_token->name;
848 if (p)
849 cclp->error_code = CCL_ERR_OK;
850 else
851 cclp->error_code = cclp->error_code;
852 return p;
853}
854
855/*
856 * ccl_find: Parse CCL find - token representation
857 * bibset: Bibset to be used for the parsing
858 * list: List of tokens
859 * error: Pointer to integer. Holds error no. on completion.
860 * pos: Pointer to char position. Holds approximate error position.
861 * return: RPN tree on successful completion; NULL otherwise.
862 */
863struct ccl_rpn_node *ccl_find (CCL_bibset bibset, struct ccl_token *list,
864 int *error, const char **pos)
865{
866 struct ccl_rpn_node *p;
867 CCL_parser cclp = ccl_parser_create ();
868
869 cclp->bibset = bibset;
870
871 p = ccl_parser_find (cclp, list);
872
873 *error = cclp->error_code;
874 *pos = cclp->error_pos;
875
876 ccl_parser_destroy (cclp);
877
878 return p;
879}
880
881/*
882 * ccl_find_str: Parse CCL find - string representation
883 * bibset: Bibset to be used for the parsing
884 * str: String to be parsed
885 * error: Pointer to integer. Holds error no. on completion.
886 * pos: Pointer to char position. Holds approximate error position.
887 * return: RPN tree on successful completion; NULL otherwise.
888 */
889struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
890 int *error, int *pos)
891{
892 CCL_parser cclp = ccl_parser_create ();
893 struct ccl_token *list;
894 struct ccl_rpn_node *p;
895
896 cclp->bibset = bibset;
897
898 list = ccl_parser_tokenize (cclp, str);
899 p = ccl_parser_find (cclp, list);
900
901 *error = cclp->error_code;
902 if (*error)
903 *pos = cclp->error_pos - str;
904 ccl_parser_destroy (cclp);
905 ccl_token_del (list);
906 return p;
907}
Note: See TracBrowser for help on using the repository browser.