1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements parsing of a CCL FIND query.
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.
24 /* returns type of current lookahead */
25 #define KIND (cclp->look_token->kind)
27 /* move one token forward */
28 #define ADVANCE cclp->look_token = cclp->look_token->next
31 * qual_val_type: test for existance of attribute type/value pair.
33 * type: Type of attribute to search for
34 * value: Value of attribute to seach for
35 * return: 1 if found; 0 otherwise.
37 static int qual_val_type(ccl_qualifier_t *qa, int type, int value,
44 for (i = 0; qa[i]; i++)
46 struct ccl_rpn_attr *q = ccl_qual_get_attr(qa[i]);
49 if (q->type == type && q->kind == CCL_RPN_ATTR_NUMERIC &&
50 q->value.numeric == value)
63 * strxcat: concatenate strings.
64 * n: Null-terminated Destination string
65 * src: Source string to be appended (not null-terminated)
66 * len: Length of source string.
68 static void strxcat(char *n, const char *src, int len)
78 * copy_token_name: Return copy of CCL token name
79 * tp: Pointer to token info.
80 * return: malloc(3) allocated copy of token name.
82 static char *copy_token_name(struct ccl_token *tp)
84 char *str = (char *)xmalloc(tp->len + 1);
86 memcpy(str, tp->name, tp->len);
92 * mk_node: Create RPN node.
94 * return: pointer to allocated node.
96 struct ccl_rpn_node *ccl_rpn_node_create(enum ccl_rpn_kind kind)
98 struct ccl_rpn_node *p;
99 p = (struct ccl_rpn_node *)xmalloc(sizeof(*p));
106 p->u.t.attr_list = 0;
117 * ccl_rpn_delete: Delete RPN tree.
118 * rpn: Pointer to tree.
120 void ccl_rpn_delete(struct ccl_rpn_node *rpn)
122 struct ccl_rpn_attr *attr, *attr1;
130 ccl_rpn_delete(rpn->u.p[0]);
131 ccl_rpn_delete(rpn->u.p[1]);
134 xfree(rpn->u.t.term);
135 xfree(rpn->u.t.qual);
136 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
139 if (attr->kind == CCL_RPN_ATTR_STRING)
140 xfree(attr->value.str);
147 xfree(rpn->u.setname);
150 ccl_rpn_delete(rpn->u.p[0]);
151 ccl_rpn_delete(rpn->u.p[1]);
152 ccl_rpn_delete(rpn->u.p[2]);
158 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa);
160 static int is_term_ok(int look, int *list)
162 for (;*list >= 0; list++)
168 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa);
170 static struct ccl_rpn_attr *add_attr_node(struct ccl_rpn_node *p,
171 const char *set, int type)
173 struct ccl_rpn_attr *n;
175 n = (struct ccl_rpn_attr *)xmalloc(sizeof(*n));
178 n->set = xstrdup(set);
182 n->next = p->u.t.attr_list;
183 p->u.t.attr_list = n;
189 * add_attr_numeric: Add attribute (type/value) to RPN term node.
190 * p: RPN node of type term.
191 * type: Type of attribute
192 * value: Value of attribute
193 * set: Attribute set name
195 void ccl_add_attr_numeric(struct ccl_rpn_node *p, const char *set,
198 struct ccl_rpn_attr *n;
200 n = add_attr_node(p, set, type);
201 n->kind = CCL_RPN_ATTR_NUMERIC;
202 n->value.numeric = value;
205 void ccl_add_attr_string(struct ccl_rpn_node *p, const char *set,
206 int type, char *value)
208 struct ccl_rpn_attr *n;
210 n = add_attr_node(p, set, type);
211 n->kind = CCL_RPN_ATTR_STRING;
212 n->value.str = xstrdup(value);
215 static size_t cmp_operator(const char **aliases, const char *input)
217 for (; *aliases; aliases++)
219 const char *cp = *aliases;
221 for (i = 0; *cp && *cp == input[i]; i++, cp++)
230 #define REGEX_CHARS "^[]{}()|.*+?!$"
231 #define CCL_CHARS "#?\\"
233 static int has_ccl_masking(const char *src_str,
235 const char **truncation_aliases,
236 const char **mask_aliases)
241 for (j = 0; j < src_len; j++)
244 if (j > 0 && src_str[j-1] == '\\')
246 else if (src_str[j] == '"')
247 quote_mode = !quote_mode;
248 else if (!quote_mode &&
249 (op_size = cmp_operator(truncation_aliases,
252 else if (!quote_mode &&
253 (op_size = cmp_operator(mask_aliases,
260 static int append_term(CCL_parser cclp, const char *src_str, size_t src_len,
261 char *dst_term, int regex_trunc, int z3958_trunc,
262 const char **truncation_aliases,
263 const char **mask_aliases,
264 int is_first, int is_last,
265 int *left_trunc, int *right_trunc)
270 for (j = 0; j < src_len; j++)
273 if (j > 0 && src_str[j-1] == '\\')
275 if (regex_trunc && strchr(REGEX_CHARS "\\", src_str[j]))
276 strcat(dst_term, "\\");
277 else if (z3958_trunc && strchr(CCL_CHARS "\\", src_str[j]))
278 strcat(dst_term, "\\");
279 strxcat(dst_term, src_str + j, 1);
281 else if (src_str[j] == '"')
282 quote_mode = !quote_mode;
283 else if (!quote_mode &&
284 (op_size = cmp_operator(truncation_aliases,
288 j += (op_size - 1); /* j++ in for loop */
290 strcat(dst_term, ".*");
291 else if (z3958_trunc)
292 strcat(dst_term, "?");
293 else if (is_first && j == 0)
295 else if (is_last && j == src_len - 1)
299 cclp->error_code = CCL_ERR_TRUNC_NOT_EMBED;
303 else if (!quote_mode &&
304 (op_size = cmp_operator(mask_aliases, src_str + j)))
306 j += (op_size - 1); /* j++ in for loop */
308 strcat(dst_term, ".");
309 else if (z3958_trunc)
310 strcat(dst_term, "#");
313 cclp->error_code = CCL_ERR_TRUNC_NOT_SINGLE;
317 else if (src_str[j] != '\\')
319 if (regex_trunc && strchr(REGEX_CHARS, src_str[j]))
320 strcat(dst_term, "\\");
321 else if (z3958_trunc && strchr(CCL_CHARS, src_str[j]))
322 strcat(dst_term, "\\");
323 strxcat(dst_term, src_str + j, 1);
330 * search_term: Parse CCL search term.
332 * qa: Qualifier attributes already applied.
333 * term_list: tokens we accept as terms in context
334 * multi: whether we accept "multiple" tokens
335 * return: pointer to node(s); NULL on error.
337 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
339 int *term_list, int multi)
341 struct ccl_rpn_node *p_top = 0;
342 struct ccl_token *lookahead = cclp->look_token;
347 const char **truncation_aliases;
348 const char *t_default[2];
349 const char **mask_aliases;
350 const char *m_default[2];
353 ccl_qual_search_special(cclp->bibset, "truncation");
354 if (!truncation_aliases)
356 truncation_aliases = t_default;
362 ccl_qual_search_special(cclp->bibset, "mask");
365 mask_aliases = m_default;
371 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
373 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AUTO_GROUP, 0))
375 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
379 struct ccl_rpn_node *p;
382 int is_ccl_masked = 0;
383 int relation_value = -1;
384 int position_value = -1;
385 int structure_value = -1;
386 int truncation_value = -1;
387 int completeness_value = -1;
394 if (and_list || or_list || !multi)
397 /* ignore commas when dealing with and-lists .. */
398 if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
400 lookahead = lookahead->next;
404 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
406 int this_is_phrase = 0;
407 for (i = 0; i<lookahead->len; i++)
408 if (lookahead->name[i] == ' ')
411 if (has_ccl_masking(lookahead->name, lookahead->len,
418 if (no > 0 && (is_phrase || is_phrase != this_is_phrase))
420 is_phrase = this_is_phrase;
422 else if (this_is_phrase || no > 0)
424 len += 1+lookahead->len+lookahead->ws_prefix_len;
425 lookahead = lookahead->next;
429 break; /* no more terms . stop . */
431 /* create the term node, but wait a moment before adding the term */
432 p = ccl_rpn_node_create(CCL_RPN_TERM);
433 p->u.t.attr_list = NULL;
437 const char *n = ccl_qual_get_name(qa[0]);
439 p->u.t.qual = xstrdup(n);
442 /* go through all attributes and add them to the attribute list */
443 for (i=0; qa && qa[i]; i++)
445 struct ccl_rpn_attr *attr;
447 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
450 case CCL_RPN_ATTR_STRING:
451 ccl_add_attr_string(p, attr->set, attr->type,
454 case CCL_RPN_ATTR_NUMERIC:
455 if (attr->value.numeric > 0)
456 { /* deal only with REAL attributes (positive) */
460 if (relation_value != -1)
462 relation_value = attr->value.numeric;
465 if (position_value != -1)
467 position_value = attr->value.numeric;
470 if (structure_value != -1)
472 structure_value = attr->value.numeric;
475 if (truncation_value != -1)
477 truncation_value = attr->value.numeric;
480 if (completeness_value != -1)
482 completeness_value = attr->value.numeric;
485 ccl_add_attr_numeric(p, attr->set, attr->type,
486 attr->value.numeric);
491 if (structure_value == -1 && (
493 qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
497 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
499 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
502 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_REGEX,
506 regex_trunc = 1; /* regex trunc (102) allowed */
508 else if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_Z3958,
512 z3958_trunc = 1; /* Z39.58 trunc (CCL) trunc allowed */
515 /* make the RPN token */
516 p->u.t.term = (char *)xmalloc(len * 2 + 2);
517 ccl_assert(p->u.t.term);
518 p->u.t.term[0] = '\0';
519 for (i = 0; i<no; i++)
521 const char *src_str = cclp->look_token->name;
522 size_t src_len = cclp->look_token->len;
524 if (p->u.t.term[0] && cclp->look_token->ws_prefix_len)
526 strxcat(p->u.t.term, cclp->look_token->ws_prefix_buf,
527 cclp->look_token->ws_prefix_len);
529 if (append_term(cclp, src_str, src_len, p->u.t.term, regex_trunc,
530 z3958_trunc, truncation_aliases, mask_aliases,
532 &left_trunc, &right_trunc))
539 /* make the top node point to us.. */
542 struct ccl_rpn_node *tmp;
545 tmp = ccl_rpn_node_create(CCL_RPN_OR);
547 tmp = ccl_rpn_node_create(CCL_RPN_AND);
549 tmp = ccl_rpn_node_create(CCL_RPN_AND);
559 if (left_trunc && right_trunc)
561 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
564 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
568 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
570 else if (right_trunc)
572 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
575 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
579 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
583 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
586 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
590 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
592 else if (regex_trunc)
594 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 102);
596 else if (z3958_trunc)
598 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 104);
602 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
604 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
610 cclp->error_code = CCL_ERR_TERM_EXPECTED;
614 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
616 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
617 return search_term_x(cclp, qa, list, 0);
621 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
624 if (KIND == CCL_TOK_LP)
626 struct ccl_rpn_node *p;
628 if (!(p = find_spec(cclp, qa)))
630 if (KIND != CCL_TOK_RP)
632 cclp->error_code = CCL_ERR_RP_EXPECTED;
641 static int list[] = {
642 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
643 CCL_TOK_REL, CCL_TOK_SET, -1};
645 return search_term_x(cclp, qa, list, 1);
652 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
653 ccl_qualifier_t *ap, char *attset)
656 struct ccl_rpn_node *p;
658 if (cclp->look_token->len == 1)
660 if (cclp->look_token->name[0] == '<')
662 else if (cclp->look_token->name[0] == '=')
664 else if (cclp->look_token->name[0] == '>')
667 else if (cclp->look_token->len == 2)
669 if (!memcmp(cclp->look_token->name, "<=", 2))
671 else if (!memcmp(cclp->look_token->name, ">=", 2))
673 else if (!memcmp(cclp->look_token->name, "<>", 2))
678 cclp->error_code = CCL_ERR_BAD_RELATION;
681 ADVANCE; /* skip relation */
683 qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
685 /* allow - inside term and treat it as range _always_ */
686 /* relation is =. Extract "embedded" - to separate terms */
687 if (KIND == CCL_TOK_TERM)
691 for (i = 0; i<cclp->look_token->len; i++)
693 if (i > 0 && cclp->look_token->name[i] == '\\')
695 else if (cclp->look_token->name[i] == '"')
696 quote_mode = !quote_mode;
697 else if (cclp->look_token->name[i] == '-' && !quote_mode)
701 if (cclp->look_token->len > 1 && i == 0)
703 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
705 ntoken->kind = CCL_TOK_TERM;
706 ntoken->name = cclp->look_token->name + 1;
707 ntoken->len = cclp->look_token->len - 1;
709 cclp->look_token->len = 1;
710 cclp->look_token->name = "-";
712 else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
714 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
716 ntoken->kind = CCL_TOK_TERM;
720 (cclp->look_token->len)--;
722 else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
724 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
725 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
727 ntoken1->kind = CCL_TOK_TERM; /* generate - */
731 ntoken2->kind = CCL_TOK_TERM; /* generate yy */
732 ntoken2->name = cclp->look_token->name + (i+1);
733 ntoken2->len = cclp->look_token->len - (i+1);
735 cclp->look_token->len = i; /* adjust xx */
737 else if (i == cclp->look_token->len &&
738 cclp->look_token->next &&
739 cclp->look_token->next->kind == CCL_TOK_TERM &&
740 cclp->look_token->next->len > 1 &&
741 cclp->look_token->next->name[0] == '-')
744 /* we _know_ that xx does not have - in it */
745 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
747 ntoken->kind = CCL_TOK_TERM; /* generate - */
751 (ntoken->next->name)++; /* adjust yy */
752 (ntoken->next->len)--;
758 KIND == CCL_TOK_TERM &&
759 cclp->look_token->next && cclp->look_token->next->len == 1 &&
760 cclp->look_token->next->name[0] == '-')
762 struct ccl_rpn_node *p1;
763 if (!(p1 = search_term(cclp, ap)))
765 ADVANCE; /* skip '-' */
766 if (KIND == CCL_TOK_TERM) /* = term - term ? */
768 struct ccl_rpn_node *p2;
770 if (!(p2 = search_term(cclp, ap)))
775 p = ccl_rpn_node_create(CCL_RPN_AND);
777 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
779 ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
784 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
789 cclp->look_token->len == 1 &&
790 cclp->look_token->name[0] == '-') /* = - term ? */
793 if (!(p = search_term(cclp, ap)))
795 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
800 if (!(p = search_terms(cclp, ap)))
802 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
809 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
813 if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
814 || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
815 return qualifiers_order(cclp, ap, attset);
817 /* unordered relation */
818 if (KIND != CCL_TOK_EQ)
820 cclp->error_code = CCL_ERR_EQ_EXPECTED;
824 return search_terms(cclp, ap);
828 * qualifier_list: Parse CCL qualifiers and search terms.
830 * la: Token pointer to RELATION token.
831 * qa: Qualifier attributes already applied.
832 * return: pointer to node(s); NULL on error.
834 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
835 struct ccl_token *la,
838 struct ccl_token *lookahead = cclp->look_token;
839 struct ccl_token *look_start = cclp->look_token;
841 struct ccl_rpn_node *node = 0;
842 const char **field_str;
850 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
854 for (lookahead = cclp->look_token; lookahead != la;
855 lookahead=lookahead->next)
858 for (i=0; qa[i]; i++)
860 ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
863 field_str = ccl_qual_search_special(cclp->bibset, "field");
866 if (!strcmp(field_str[0], "or"))
868 else if (!strcmp(field_str[0], "merge"))
873 /* consider each field separately and OR */
874 lookahead = look_start;
875 while (lookahead != la)
879 while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
880 lookahead->len, seq)) != 0)
882 struct ccl_rpn_node *node_sub;
883 cclp->look_token = la;
885 node_sub = qualifier_relation(cclp, ap);
888 ccl_rpn_delete(node);
894 struct ccl_rpn_node *node_this =
895 ccl_rpn_node_create(CCL_RPN_OR);
896 node_this->u.p[0] = node;
897 node_this->u.p[1] = node_sub;
906 cclp->look_token = lookahead;
907 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
911 lookahead = lookahead->next;
912 if (lookahead->kind == CCL_TOK_COMMA)
913 lookahead = lookahead->next;
918 /* merge attributes from ALL fields - including inherited ones */
921 struct ccl_rpn_node *node_sub;
923 lookahead = look_start;
924 for (i = 0; lookahead != la; i++)
926 ap[i] = ccl_qual_search(cclp, lookahead->name,
927 lookahead->len, seq);
930 if (!ap[i] && seq > 0)
931 ap[i] = ccl_qual_search(cclp, lookahead->name,
935 cclp->look_token = lookahead;
936 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
940 lookahead = lookahead->next;
941 if (lookahead->kind == CCL_TOK_COMMA)
942 lookahead = lookahead->next;
946 ccl_qualifier_t *qa0 = qa;
956 cclp->look_token = lookahead;
958 node_sub = qualifier_relation(cclp, ap);
961 ccl_rpn_delete(node);
966 struct ccl_rpn_node *node_this =
967 ccl_rpn_node_create(CCL_RPN_OR);
968 node_this->u.p[0] = node;
969 node_this->u.p[1] = node_sub;
983 * search_terms: Parse CCL search terms - including proximity.
985 * qa: Qualifier attributes already applied.
986 * return: pointer to node(s); NULL on error.
988 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
990 static int list[] = {
991 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
992 CCL_TOK_REL, CCL_TOK_SET, -1};
993 struct ccl_rpn_node *p1, *p2, *pn;
994 p1 = search_terms2(cclp, qa);
999 if (KIND == CCL_TOK_PROX)
1001 struct ccl_rpn_node *p_prox = 0;
1002 /* ! word order specified */
1003 /* % word order not specified */
1004 p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
1005 p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
1006 memcpy(p_prox->u.t.term, cclp->look_token->name,
1007 cclp->look_token->len);
1008 p_prox->u.t.term[cclp->look_token->len] = 0;
1009 p_prox->u.t.attr_list = 0;
1012 p2 = search_terms2(cclp, qa);
1018 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1021 pn->u.p[2] = p_prox;
1024 else if (is_term_ok(KIND, list))
1026 p2 = search_terms2(cclp, qa);
1032 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1045 * search_elements: Parse CCL search elements
1047 * qa: Qualifier attributes already applied.
1048 * return: pointer to node(s); NULL on error.
1050 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1051 ccl_qualifier_t *qa)
1053 struct ccl_rpn_node *p1;
1054 struct ccl_token *lookahead;
1055 if (KIND == CCL_TOK_SET)
1058 if (KIND == CCL_TOK_EQ)
1060 if (KIND != CCL_TOK_TERM)
1062 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1065 p1 = ccl_rpn_node_create(CCL_RPN_SET);
1066 p1->u.setname = copy_token_name(cclp->look_token);
1070 lookahead = cclp->look_token;
1072 while (lookahead->kind==CCL_TOK_TERM)
1074 lookahead = lookahead->next;
1075 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1076 return qualifier_list(cclp, lookahead, qa);
1077 if (lookahead->kind != CCL_TOK_COMMA)
1079 lookahead = lookahead->next;
1081 if (qa || lookahead->kind == CCL_TOK_LP)
1082 return search_terms(cclp, qa);
1085 ccl_qualifier_t qa[2];
1086 struct ccl_rpn_node *node = 0;
1088 lookahead = cclp->look_token;
1091 for(seq = 0; ;seq++)
1093 struct ccl_rpn_node *node_sub;
1094 qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1098 cclp->look_token = lookahead;
1100 node_sub = search_terms(cclp, qa);
1103 ccl_rpn_delete(node);
1108 struct ccl_rpn_node *node_this =
1109 ccl_rpn_node_create(CCL_RPN_OR);
1110 node_this->u.p[0] = node;
1111 node_this->u.p[1] = node_sub;
1112 node_this->u.p[2] = 0;
1119 node = search_terms(cclp, 0);
1125 * find_spec: Parse CCL find specification
1127 * qa: Qualifier attributes already applied.
1128 * return: pointer to node(s); NULL on error.
1130 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1132 struct ccl_rpn_node *p1, *p2, *pn;
1133 if (!(p1 = search_elements(cclp, qa)))
1141 p2 = search_elements(cclp, qa);
1147 pn = ccl_rpn_node_create(CCL_RPN_AND);
1155 p2 = search_elements(cclp, qa);
1161 pn = ccl_rpn_node_create(CCL_RPN_OR);
1169 p2 = search_elements(cclp, qa);
1175 pn = ccl_rpn_node_create(CCL_RPN_NOT);
1187 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1189 struct ccl_rpn_node *p;
1190 struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1191 p = ccl_parser_find_token(cclp, list);
1192 ccl_token_del(list);
1196 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1197 struct ccl_token *list)
1199 struct ccl_rpn_node *p;
1201 cclp->look_token = list;
1202 p = find_spec(cclp, NULL);
1203 if (p && KIND != CCL_TOK_EOL)
1205 if (KIND == CCL_TOK_RP)
1206 cclp->error_code = CCL_ERR_BAD_RP;
1208 cclp->error_code = CCL_ERR_OP_EXPECTED;
1212 cclp->error_pos = cclp->look_token->name;
1214 cclp->error_code = CCL_ERR_OK;
1216 cclp->error_code = cclp->error_code;
1221 * ccl_find_str: Parse CCL find - string representation
1222 * bibset: Bibset to be used for the parsing
1223 * str: String to be parsed
1224 * error: Pointer to integer. Holds error no. on completion.
1225 * pos: Pointer to char position. Holds approximate error position.
1226 * return: RPN tree on successful completion; NULL otherwise.
1228 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1229 int *error, int *pos)
1231 CCL_parser cclp = ccl_parser_create(bibset);
1232 struct ccl_token *list;
1233 struct ccl_rpn_node *p;
1235 list = ccl_parser_tokenize(cclp, str);
1236 p = ccl_parser_find_token(cclp, list);
1238 *error = cclp->error_code;
1240 *pos = cclp->error_pos - str;
1241 ccl_parser_destroy(cclp);
1242 ccl_token_del(list);
1249 * c-file-style: "Stroustrup"
1250 * indent-tabs-mode: nil
1252 * vim: shiftwidth=4 tabstop=8 expandtab