1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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;
116 static struct ccl_rpn_node *ccl_rpn_dup(struct ccl_rpn_node *rpn)
118 struct ccl_rpn_node *n;
119 struct ccl_rpn_attr *attr, **attrp;
122 n = ccl_rpn_node_create(rpn->kind);
128 n->u.p[0] = ccl_rpn_dup(rpn->u.p[0]);
129 n->u.p[1] = ccl_rpn_dup(rpn->u.p[1]);
132 n->u.t.term = xstrdup(rpn->u.t.term);
133 n->u.t.qual = rpn->u.t.qual ? xstrdup(rpn->u.t.qual) : 0;
134 attrp = &n->u.t.attr_list;
135 for (attr = rpn->u.t.attr_list; attr; attr = attr->next)
137 *attrp = (struct ccl_rpn_attr *) xmalloc(sizeof(**attrp));
138 (*attrp)->kind = attr->kind;
139 (*attrp)->type = attr->type;
140 if (attr->kind == CCL_RPN_ATTR_STRING)
141 (*attrp)->value.str = xstrdup(attr->value.str);
143 (*attrp)->value.numeric = attr->value.numeric;
145 (*attrp)->set = xstrdup(attr->set);
148 attrp = &(*attrp)->next;
153 n->u.setname = xstrdup(rpn->u.setname);
156 n->u.p[0] = ccl_rpn_dup(rpn->u.p[0]);
157 n->u.p[1] = ccl_rpn_dup(rpn->u.p[1]);
158 n->u.p[2] = ccl_rpn_dup(rpn->u.p[2]);
165 * ccl_rpn_delete: Delete RPN tree.
166 * rpn: Pointer to tree.
168 void ccl_rpn_delete(struct ccl_rpn_node *rpn)
170 struct ccl_rpn_attr *attr, *attr1;
178 ccl_rpn_delete(rpn->u.p[0]);
179 ccl_rpn_delete(rpn->u.p[1]);
182 xfree(rpn->u.t.term);
183 xfree(rpn->u.t.qual);
184 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
187 if (attr->kind == CCL_RPN_ATTR_STRING)
188 xfree(attr->value.str);
195 xfree(rpn->u.setname);
198 ccl_rpn_delete(rpn->u.p[0]);
199 ccl_rpn_delete(rpn->u.p[1]);
200 ccl_rpn_delete(rpn->u.p[2]);
206 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa);
208 static int is_term_ok(int look, int *list)
210 for (; *list >= 0; list++)
216 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa);
218 static struct ccl_rpn_attr *add_attr_node(struct ccl_rpn_node *p,
219 const char *set, int type)
221 struct ccl_rpn_attr *n = (struct ccl_rpn_attr *) xmalloc(sizeof(*n));
224 n->set = xstrdup(set);
228 n->next = p->u.t.attr_list;
229 p->u.t.attr_list = n;
234 * add_attr_numeric: Add attribute (type/value) to RPN term node.
235 * p: RPN node of type term.
236 * type: Type of attribute
237 * value: Value of attribute
238 * set: Attribute set name
240 void ccl_add_attr_numeric(struct ccl_rpn_node *p, const char *set,
243 struct ccl_rpn_attr *n = add_attr_node(p, set, type);
244 n->kind = CCL_RPN_ATTR_NUMERIC;
245 n->value.numeric = value;
248 void ccl_add_attr_string(struct ccl_rpn_node *p, const char *set,
249 int type, char *value)
251 struct ccl_rpn_attr *n = add_attr_node(p, set, type);
252 n->kind = CCL_RPN_ATTR_STRING;
253 n->value.str = xstrdup(value);
256 static size_t cmp_operator(const char **aliases, const char *input)
258 for (; *aliases; aliases++)
260 const char *cp = *aliases;
262 for (i = 0; *cp && *cp == input[i]; i++, cp++)
271 #define REGEX_CHARS "^[]{}()|.*+?!$"
272 #define CCL_CHARS "#?\\"
274 static int has_ccl_masking(const char *src_str,
276 const char **truncation_aliases,
277 const char **mask_aliases)
282 for (j = 0; j < src_len; j++)
285 if (j > 0 && src_str[j-1] == '\\')
287 else if (src_str[j] == '"')
288 quote_mode = !quote_mode;
289 else if (!quote_mode &&
290 (op_size = cmp_operator(truncation_aliases,
293 else if (!quote_mode &&
294 (op_size = cmp_operator(mask_aliases,
301 static int append_term(CCL_parser cclp, const char *src_str, size_t src_len,
302 char *dst_term, int regex_trunc, int z3958_trunc,
303 const char **truncation_aliases,
304 const char **mask_aliases,
305 int is_first, int is_last,
306 int *left_trunc, int *right_trunc)
311 for (j = 0; j < src_len; j++)
314 if (j > 0 && src_str[j-1] == '\\')
316 if (regex_trunc && strchr(REGEX_CHARS "\\", src_str[j]))
317 strcat(dst_term, "\\");
318 else if (z3958_trunc && strchr(CCL_CHARS "\\", src_str[j]))
319 strcat(dst_term, "\\");
320 strxcat(dst_term, src_str + j, 1);
322 else if (src_str[j] == '"')
323 quote_mode = !quote_mode;
324 else if (!quote_mode &&
325 (op_size = cmp_operator(truncation_aliases,
329 j += (op_size - 1); /* j++ in for loop */
331 strcat(dst_term, ".*");
332 else if (z3958_trunc)
333 strcat(dst_term, "?");
334 else if (is_first && j == 0)
336 else if (is_last && j == src_len - 1)
340 cclp->error_code = CCL_ERR_TRUNC_NOT_EMBED;
344 else if (!quote_mode &&
345 (op_size = cmp_operator(mask_aliases, src_str + j)))
347 j += (op_size - 1); /* j++ in for loop */
349 strcat(dst_term, ".");
350 else if (z3958_trunc)
351 strcat(dst_term, "#");
354 cclp->error_code = CCL_ERR_TRUNC_NOT_SINGLE;
358 else if (src_str[j] != '\\')
360 if (regex_trunc && strchr(REGEX_CHARS, src_str[j]))
361 strcat(dst_term, "\\");
362 else if (z3958_trunc && strchr(CCL_CHARS, src_str[j]))
363 strcat(dst_term, "\\");
364 strxcat(dst_term, src_str + j, 1);
371 static struct ccl_rpn_node *ccl_term_one_use(CCL_parser cclp,
372 struct ccl_token *lookahead0,
373 struct ccl_rpn_attr *attr_use,
379 struct ccl_rpn_node *p;
381 int relation_value = -1;
382 int position_value = -1;
383 int structure_value = -1;
384 int truncation_value = -1;
385 int completeness_value = -1;
391 int is_ccl_masked = 0;
393 struct ccl_token *lookahead = lookahead0;
394 const char **truncation_aliases;
395 const char *t_default[2];
396 const char **mask_aliases;
397 const char *m_default[2];
401 ccl_qual_search_special(cclp->bibset, "truncation");
402 if (!truncation_aliases)
404 truncation_aliases = t_default;
409 ccl_qual_search_special(cclp->bibset, "mask");
412 mask_aliases = m_default;
416 for (i = 0; i < no; i++)
418 if (has_ccl_masking(lookahead->name, lookahead->len,
423 term_len += 1 + lookahead->len + lookahead->ws_prefix_len;
424 lookahead = lookahead->next;
426 lookahead = lookahead0;
428 p = ccl_rpn_node_create(CCL_RPN_TERM);
429 p->u.t.attr_list = NULL;
433 const char *n = ccl_qual_get_name(qa[0]);
435 p->u.t.qual = xstrdup(n);
437 /* go through all attributes and add them to the attribute list */
438 for (i = 0; qa && qa[i]; i++)
440 struct ccl_rpn_attr *attr;
441 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
442 if (attr->type != 1 || !attr_use || attr == attr_use)
446 case CCL_RPN_ATTR_STRING:
447 ccl_add_attr_string(p, attr->set, attr->type,
450 case CCL_RPN_ATTR_NUMERIC:
451 if (attr->value.numeric > 0)
452 { /* deal only with REAL attributes (positive) */
456 if (relation_value != -1)
458 relation_value = attr->value.numeric;
461 if (position_value != -1)
463 position_value = attr->value.numeric;
466 if (structure_value != -1)
468 structure_value = attr->value.numeric;
471 if (truncation_value != -1)
473 truncation_value = attr->value.numeric;
476 if (completeness_value != -1)
478 completeness_value = attr->value.numeric;
481 ccl_add_attr_numeric(p, attr->set, attr->type,
482 attr->value.numeric);
488 if (structure_value == -1 && (
490 qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
494 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
496 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
498 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_REGEX,
502 regex_trunc = 1; /* regex trunc (102) allowed */
504 else if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_Z3958,
508 z3958_trunc = 1; /* Z39.58 trunc (CCL) trunc allowed */
510 /* make the RPN token */
511 p->u.t.term = (char *)xmalloc(term_len * 2 + 2);
512 ccl_assert(p->u.t.term);
513 p->u.t.term[0] = '\0';
515 for (i = 0; i < no; i++)
517 const char *src_str = lookahead->name;
518 size_t src_len = lookahead->len;
520 if (p->u.t.term[0] && lookahead->ws_prefix_len)
522 strxcat(p->u.t.term, lookahead->ws_prefix_buf,
523 lookahead->ws_prefix_len);
525 if (append_term(cclp, src_str, src_len, p->u.t.term, regex_trunc,
526 z3958_trunc, truncation_aliases, mask_aliases,
528 &left_trunc, &right_trunc))
533 lookahead = lookahead->next;
535 if (left_trunc && right_trunc)
537 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
540 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
544 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
546 else if (right_trunc)
548 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
551 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
555 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
559 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
562 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
566 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
568 else if (regex_trunc)
570 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 102);
572 else if (z3958_trunc)
574 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 104);
578 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
580 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
585 static struct ccl_rpn_node *split_recur(CCL_parser cclp, ccl_qualifier_t *qa,
586 struct ccl_rpn_node *parent,
587 struct ccl_token **ar, size_t sz)
590 struct ccl_rpn_node *p_top = 0;
592 for (l = 1; l <= sz; l++)
594 struct ccl_rpn_node *p1;
595 struct ccl_rpn_node *p2 = ccl_term_one_use(cclp, ar[0],
604 struct ccl_rpn_node *tmp = ccl_rpn_node_create(CCL_RPN_AND);
605 tmp->u.p[0] = l > 1 ? ccl_rpn_dup(parent) : parent;
610 p1 = split_recur(cclp, qa, p2, ar + l, sz - l);
615 struct ccl_rpn_node *tmp = ccl_rpn_node_create(CCL_RPN_OR);
627 static struct ccl_rpn_node *search_term_split_list(CCL_parser cclp,
629 int *term_list, int multi)
631 struct ccl_rpn_node *p;
632 struct ccl_token **ar;
633 struct ccl_token *lookahead = cclp->look_token;
635 for (sz = 0; is_term_ok(lookahead->kind, term_list); sz++)
636 lookahead = lookahead->next;
639 cclp->error_code = CCL_ERR_TERM_EXPECTED;
642 ar = (struct ccl_token **) xmalloc(sizeof(*lookahead) * sz);
643 lookahead = cclp->look_token;
644 for (i = 0; is_term_ok(lookahead->kind, term_list); i++)
647 lookahead = lookahead->next;
649 p = split_recur(cclp, qa, 0, ar, sz);
651 for (i = 0; i < sz; i++)
657 * search_term: Parse CCL search term.
659 * qa: Qualifier attributes already applied.
660 * term_list: tokens we accept as terms in context
661 * multi: whether we accept "multiple" tokens
662 * return: pointer to node(s); NULL on error.
664 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
666 int *term_list, int multi)
668 struct ccl_rpn_node *p_top = 0;
669 struct ccl_token *lookahead = cclp->look_token;
674 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
676 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AUTO_GROUP, 0))
678 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
680 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_SPLIT_LIST, 0))
682 return search_term_split_list(cclp, qa, term_list, multi);
686 struct ccl_rpn_node *p = 0;
690 if (and_list || or_list || !multi)
693 /* ignore commas when dealing with and-lists .. */
694 if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
696 lookahead = lookahead->next;
700 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
702 int this_is_phrase = 0;
703 for (i = 0; i<lookahead->len; i++)
704 if (lookahead->name[i] == ' ')
708 if (no > 0 && (is_phrase || is_phrase != this_is_phrase))
710 is_phrase = this_is_phrase;
712 else if (this_is_phrase || no > 0)
714 lookahead = lookahead->next;
718 break; /* no more terms . stop . */
720 /* go through all attributes and add them to the attribute list */
721 for (i = 0; qa && qa[i]; i++)
723 struct ccl_rpn_attr *attr;
725 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
728 struct ccl_rpn_node *tmp2;
729 tmp2 = ccl_term_one_use(cclp, cclp->look_token,
731 is_phrase, auto_group);
741 struct ccl_rpn_node *tmp1;
742 tmp1 = ccl_rpn_node_create(CCL_RPN_OR);
750 p = ccl_term_one_use(cclp, cclp->look_token,
751 0 /* attr: no use */, qa, no,
752 is_phrase, auto_group);
753 for (i = 0; i < no; i++)
757 /* make the top node point to us.. */
760 struct ccl_rpn_node *tmp;
763 tmp = ccl_rpn_node_create(CCL_RPN_OR);
765 tmp = ccl_rpn_node_create(CCL_RPN_AND);
767 tmp = ccl_rpn_node_create(CCL_RPN_AND);
780 cclp->error_code = CCL_ERR_TERM_EXPECTED;
784 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
786 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
787 return search_term_x(cclp, qa, list, 0);
791 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
794 if (KIND == CCL_TOK_LP)
796 struct ccl_rpn_node *p;
798 if (!(p = find_spec(cclp, qa)))
800 if (KIND != CCL_TOK_RP)
802 cclp->error_code = CCL_ERR_RP_EXPECTED;
811 static int list[] = {
812 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
813 CCL_TOK_REL, CCL_TOK_SET, -1};
815 return search_term_x(cclp, qa, list, 1);
821 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
822 ccl_qualifier_t *ap, char *attset)
825 struct ccl_rpn_node *p;
827 if (cclp->look_token->len == 1)
829 if (cclp->look_token->name[0] == '<')
831 else if (cclp->look_token->name[0] == '=')
833 else if (cclp->look_token->name[0] == '>')
836 else if (cclp->look_token->len == 2)
838 if (!memcmp(cclp->look_token->name, "<=", 2))
840 else if (!memcmp(cclp->look_token->name, ">=", 2))
842 else if (!memcmp(cclp->look_token->name, "<>", 2))
847 cclp->error_code = CCL_ERR_BAD_RELATION;
850 ADVANCE; /* skip relation */
852 qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
854 /* allow - inside term and treat it as range _always_ */
855 /* relation is =. Extract "embedded" - to separate terms */
856 if (KIND == CCL_TOK_TERM)
860 for (i = 0; i<cclp->look_token->len; i++)
862 if (i > 0 && cclp->look_token->name[i] == '\\')
864 else if (cclp->look_token->name[i] == '"')
865 quote_mode = !quote_mode;
866 else if (cclp->look_token->name[i] == '-' && !quote_mode)
870 if (cclp->look_token->len > 1 && i == 0)
872 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
874 ntoken->kind = CCL_TOK_TERM;
875 ntoken->name = cclp->look_token->name + 1;
876 ntoken->len = cclp->look_token->len - 1;
878 cclp->look_token->len = 1;
879 cclp->look_token->name = "-";
881 else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
883 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
885 ntoken->kind = CCL_TOK_TERM;
889 (cclp->look_token->len)--;
891 else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
893 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
894 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
896 ntoken1->kind = CCL_TOK_TERM; /* generate - */
900 ntoken2->kind = CCL_TOK_TERM; /* generate yy */
901 ntoken2->name = cclp->look_token->name + (i+1);
902 ntoken2->len = cclp->look_token->len - (i+1);
904 cclp->look_token->len = i; /* adjust xx */
906 else if (i == cclp->look_token->len &&
907 cclp->look_token->next &&
908 cclp->look_token->next->kind == CCL_TOK_TERM &&
909 cclp->look_token->next->len > 1 &&
910 cclp->look_token->next->name[0] == '-')
913 /* we _know_ that xx does not have - in it */
914 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
916 ntoken->kind = CCL_TOK_TERM; /* generate - */
920 (ntoken->next->name)++; /* adjust yy */
921 (ntoken->next->len)--;
927 KIND == CCL_TOK_TERM &&
928 cclp->look_token->next && cclp->look_token->next->len == 1 &&
929 cclp->look_token->next->name[0] == '-')
931 struct ccl_rpn_node *p1;
932 if (!(p1 = search_term(cclp, ap)))
934 ADVANCE; /* skip '-' */
935 if (KIND == CCL_TOK_TERM) /* = term - term ? */
937 struct ccl_rpn_node *p2;
939 if (!(p2 = search_term(cclp, ap)))
944 p = ccl_rpn_node_create(CCL_RPN_AND);
946 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
948 ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
953 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
958 cclp->look_token->len == 1 &&
959 cclp->look_token->name[0] == '-') /* = - term ? */
962 if (!(p = search_term(cclp, ap)))
964 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
969 if (!(p = search_terms(cclp, ap)))
972 !qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_OMIT_EQUALS, 0))
973 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
980 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
984 if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
985 || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
986 return qualifiers_order(cclp, ap, attset);
988 /* unordered relation */
989 if (KIND != CCL_TOK_EQ)
991 cclp->error_code = CCL_ERR_EQ_EXPECTED;
995 return search_terms(cclp, ap);
999 * qualifier_list: Parse CCL qualifiers and search terms.
1001 * la: Token pointer to RELATION token.
1002 * qa: Qualifier attributes already applied.
1003 * return: pointer to node(s); NULL on error.
1005 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
1006 struct ccl_token *la,
1007 ccl_qualifier_t *qa)
1009 struct ccl_token *lookahead = cclp->look_token;
1010 struct ccl_token *look_start = cclp->look_token;
1011 ccl_qualifier_t *ap;
1012 struct ccl_rpn_node *node = 0;
1013 const char **field_str;
1021 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
1025 for (lookahead = cclp->look_token; lookahead != la;
1026 lookahead=lookahead->next)
1029 for (i=0; qa[i]; i++)
1031 ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
1034 field_str = ccl_qual_search_special(cclp->bibset, "field");
1037 if (!strcmp(field_str[0], "or"))
1039 else if (!strcmp(field_str[0], "merge"))
1044 /* consider each field separately and OR */
1045 lookahead = look_start;
1046 while (lookahead != la)
1050 while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
1051 lookahead->len, seq)) != 0)
1053 struct ccl_rpn_node *node_sub;
1054 cclp->look_token = la;
1056 node_sub = qualifier_relation(cclp, ap);
1059 ccl_rpn_delete(node);
1065 struct ccl_rpn_node *node_this =
1066 ccl_rpn_node_create(CCL_RPN_OR);
1067 node_this->u.p[0] = node;
1068 node_this->u.p[1] = node_sub;
1077 cclp->look_token = lookahead;
1078 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1082 lookahead = lookahead->next;
1083 if (lookahead->kind == CCL_TOK_COMMA)
1084 lookahead = lookahead->next;
1089 /* merge attributes from ALL fields - including inherited ones */
1092 struct ccl_rpn_node *node_sub;
1094 lookahead = look_start;
1095 for (i = 0; lookahead != la; i++)
1097 ap[i] = ccl_qual_search(cclp, lookahead->name,
1098 lookahead->len, seq);
1101 if (!ap[i] && seq > 0)
1102 ap[i] = ccl_qual_search(cclp, lookahead->name,
1106 cclp->look_token = lookahead;
1107 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1111 lookahead = lookahead->next;
1112 if (lookahead->kind == CCL_TOK_COMMA)
1113 lookahead = lookahead->next;
1117 ccl_qualifier_t *qa0 = qa;
1127 cclp->look_token = lookahead;
1129 node_sub = qualifier_relation(cclp, ap);
1132 ccl_rpn_delete(node);
1137 struct ccl_rpn_node *node_this =
1138 ccl_rpn_node_create(CCL_RPN_OR);
1139 node_this->u.p[0] = node;
1140 node_this->u.p[1] = node_sub;
1154 * search_terms: Parse CCL search terms - including proximity.
1156 * qa: Qualifier attributes already applied.
1157 * return: pointer to node(s); NULL on error.
1159 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
1161 static int list[] = {
1162 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
1163 CCL_TOK_REL, CCL_TOK_SET, -1};
1164 struct ccl_rpn_node *p1, *p2, *pn;
1165 p1 = search_terms2(cclp, qa);
1170 if (KIND == CCL_TOK_PROX)
1172 struct ccl_rpn_node *p_prox = 0;
1173 /* ! word order specified */
1174 /* % word order not specified */
1175 p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
1176 p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
1177 memcpy(p_prox->u.t.term, cclp->look_token->name,
1178 cclp->look_token->len);
1179 p_prox->u.t.term[cclp->look_token->len] = 0;
1180 p_prox->u.t.attr_list = 0;
1183 p2 = search_terms2(cclp, qa);
1189 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1192 pn->u.p[2] = p_prox;
1195 else if (is_term_ok(KIND, list))
1197 p2 = search_terms2(cclp, qa);
1203 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1216 * search_elements: Parse CCL search elements
1218 * qa: Qualifier attributes already applied.
1219 * return: pointer to node(s); NULL on error.
1221 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1222 ccl_qualifier_t *qa)
1224 struct ccl_rpn_node *p1;
1225 struct ccl_token *lookahead;
1226 if (KIND == CCL_TOK_SET)
1229 if (KIND == CCL_TOK_EQ)
1231 if (KIND != CCL_TOK_TERM)
1233 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1236 p1 = ccl_rpn_node_create(CCL_RPN_SET);
1237 p1->u.setname = copy_token_name(cclp->look_token);
1241 lookahead = cclp->look_token;
1243 while (lookahead->kind==CCL_TOK_TERM)
1245 lookahead = lookahead->next;
1246 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1247 return qualifier_list(cclp, lookahead, qa);
1248 if (lookahead->kind != CCL_TOK_COMMA)
1250 lookahead = lookahead->next;
1252 if (qa || lookahead->kind == CCL_TOK_LP)
1253 return search_terms(cclp, qa);
1256 ccl_qualifier_t qa[2];
1257 struct ccl_rpn_node *node = 0;
1259 lookahead = cclp->look_token;
1262 for(seq = 0; ;seq++)
1264 struct ccl_rpn_node *node_sub;
1265 qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1269 cclp->look_token = lookahead;
1271 node_sub = search_terms(cclp, qa);
1274 ccl_rpn_delete(node);
1279 struct ccl_rpn_node *node_this =
1280 ccl_rpn_node_create(CCL_RPN_OR);
1281 node_this->u.p[0] = node;
1282 node_this->u.p[1] = node_sub;
1283 node_this->u.p[2] = 0;
1290 node = search_terms(cclp, 0);
1296 * find_spec: Parse CCL find specification
1298 * qa: Qualifier attributes already applied.
1299 * return: pointer to node(s); NULL on error.
1301 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1303 struct ccl_rpn_node *p1, *p2, *pn;
1304 if (!(p1 = search_elements(cclp, qa)))
1312 p2 = search_elements(cclp, qa);
1318 pn = ccl_rpn_node_create(CCL_RPN_AND);
1326 p2 = search_elements(cclp, qa);
1332 pn = ccl_rpn_node_create(CCL_RPN_OR);
1340 p2 = search_elements(cclp, qa);
1346 pn = ccl_rpn_node_create(CCL_RPN_NOT);
1358 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1360 struct ccl_rpn_node *p;
1361 struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1362 p = ccl_parser_find_token(cclp, list);
1363 ccl_token_del(list);
1367 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1368 struct ccl_token *list)
1370 struct ccl_rpn_node *p;
1372 cclp->look_token = list;
1373 p = find_spec(cclp, NULL);
1374 if (p && KIND != CCL_TOK_EOL)
1376 if (KIND == CCL_TOK_RP)
1377 cclp->error_code = CCL_ERR_BAD_RP;
1379 cclp->error_code = CCL_ERR_OP_EXPECTED;
1383 cclp->error_pos = cclp->look_token->name;
1385 cclp->error_code = CCL_ERR_OK;
1387 cclp->error_code = cclp->error_code;
1392 * ccl_find_str: Parse CCL find - string representation
1393 * bibset: Bibset to be used for the parsing
1394 * str: String to be parsed
1395 * error: Pointer to integer. Holds error no. on completion.
1396 * pos: Pointer to char position. Holds approximate error position.
1397 * return: RPN tree on successful completion; NULL otherwise.
1399 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1400 int *error, int *pos)
1402 CCL_parser cclp = ccl_parser_create(bibset);
1403 struct ccl_token *list;
1404 struct ccl_rpn_node *p;
1406 list = ccl_parser_tokenize(cclp, str);
1407 p = ccl_parser_find_token(cclp, list);
1409 *error = cclp->error_code;
1411 *pos = cclp->error_pos - str;
1412 ccl_parser_destroy(cclp);
1413 ccl_token_del(list);
1420 * c-file-style: "Stroustrup"
1421 * indent-tabs-mode: nil
1423 * vim: shiftwidth=4 tabstop=8 expandtab