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 *ccl_term_multi_use(CCL_parser cclp,
586 struct ccl_token *lookahead0,
592 struct ccl_rpn_node *p = 0;
594 for (i = 0; qa && qa[i]; i++)
596 struct ccl_rpn_attr *attr;
597 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
598 if (attr->type == 1 && i == 0)
600 struct ccl_rpn_node *tmp2;
601 tmp2 = ccl_term_one_use(cclp, lookahead0,
603 is_phrase, auto_group);
613 struct ccl_rpn_node *tmp1;
614 tmp1 = ccl_rpn_node_create(CCL_RPN_OR);
622 p = ccl_term_one_use(cclp, lookahead0,
623 0 /* attr: no use */, qa, no,
624 is_phrase, auto_group);
628 static struct ccl_rpn_node *split_recur(CCL_parser cclp, ccl_qualifier_t *qa,
629 struct ccl_rpn_node *parent,
630 struct ccl_token **ar, size_t sz)
633 struct ccl_rpn_node *p_top = 0;
635 for (l = 1; l <= sz; l++)
637 struct ccl_rpn_node *p1;
638 struct ccl_rpn_node *p2 = ccl_term_multi_use(cclp, ar[0],
646 struct ccl_rpn_node *tmp = ccl_rpn_node_create(CCL_RPN_AND);
647 tmp->u.p[0] = l > 1 ? ccl_rpn_dup(parent) : parent;
652 p1 = split_recur(cclp, qa, p2, ar + l, sz - l);
657 struct ccl_rpn_node *tmp = ccl_rpn_node_create(CCL_RPN_OR);
669 static struct ccl_rpn_node *search_term_split_list(CCL_parser cclp,
671 int *term_list, int multi)
673 struct ccl_rpn_node *p;
674 struct ccl_token **ar;
675 struct ccl_token *lookahead = cclp->look_token;
677 for (sz = 0; is_term_ok(lookahead->kind, term_list); sz++)
678 lookahead = lookahead->next;
681 cclp->error_code = CCL_ERR_TERM_EXPECTED;
684 ar = (struct ccl_token **) xmalloc(sizeof(*lookahead) * sz);
685 lookahead = cclp->look_token;
686 for (i = 0; is_term_ok(lookahead->kind, term_list); i++)
689 lookahead = lookahead->next;
691 p = split_recur(cclp, qa, 0, ar, sz);
693 for (i = 0; i < sz; i++)
699 * search_term: Parse CCL search term.
701 * qa: Qualifier attributes already applied.
702 * term_list: tokens we accept as terms in context
703 * multi: whether we accept "multiple" tokens
704 * return: pointer to node(s); NULL on error.
706 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
708 int *term_list, int multi)
710 struct ccl_rpn_node *p_top = 0;
711 struct ccl_token *lookahead = cclp->look_token;
716 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
718 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AUTO_GROUP, 0))
720 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
722 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_SPLIT_LIST, 0))
724 return search_term_split_list(cclp, qa, term_list, multi);
728 struct ccl_rpn_node *p = 0;
732 if (and_list || or_list || !multi)
735 /* ignore commas when dealing with and-lists .. */
736 if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
738 lookahead = lookahead->next;
742 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
744 int this_is_phrase = 0;
745 for (i = 0; i<lookahead->len; i++)
746 if (lookahead->name[i] == ' ')
750 if (no > 0 && (is_phrase || is_phrase != this_is_phrase))
752 is_phrase = this_is_phrase;
754 else if (this_is_phrase || no > 0)
756 lookahead = lookahead->next;
760 break; /* no more terms . stop . */
761 p = ccl_term_multi_use(cclp, cclp->look_token, qa, no,
762 is_phrase, auto_group);
763 for (i = 0; i < no; i++)
767 /* make the top node point to us.. */
770 struct ccl_rpn_node *tmp;
773 tmp = ccl_rpn_node_create(CCL_RPN_OR);
775 tmp = ccl_rpn_node_create(CCL_RPN_AND);
777 tmp = ccl_rpn_node_create(CCL_RPN_AND);
790 cclp->error_code = CCL_ERR_TERM_EXPECTED;
794 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
796 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
797 return search_term_x(cclp, qa, list, 0);
801 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
804 if (KIND == CCL_TOK_LP)
806 struct ccl_rpn_node *p;
808 if (!(p = find_spec(cclp, qa)))
810 if (KIND != CCL_TOK_RP)
812 cclp->error_code = CCL_ERR_RP_EXPECTED;
821 static int list[] = {
822 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
823 CCL_TOK_REL, CCL_TOK_SET, -1};
825 return search_term_x(cclp, qa, list, 1);
831 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
832 ccl_qualifier_t *ap, char *attset)
835 struct ccl_rpn_node *p;
837 if (cclp->look_token->len == 1)
839 if (cclp->look_token->name[0] == '<')
841 else if (cclp->look_token->name[0] == '=')
843 else if (cclp->look_token->name[0] == '>')
846 else if (cclp->look_token->len == 2)
848 if (!memcmp(cclp->look_token->name, "<=", 2))
850 else if (!memcmp(cclp->look_token->name, ">=", 2))
852 else if (!memcmp(cclp->look_token->name, "<>", 2))
857 cclp->error_code = CCL_ERR_BAD_RELATION;
860 ADVANCE; /* skip relation */
862 qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
864 /* allow - inside term and treat it as range _always_ */
865 /* relation is =. Extract "embedded" - to separate terms */
866 if (KIND == CCL_TOK_TERM)
870 for (i = 0; i<cclp->look_token->len; i++)
872 if (i > 0 && cclp->look_token->name[i] == '\\')
874 else if (cclp->look_token->name[i] == '"')
875 quote_mode = !quote_mode;
876 else if (cclp->look_token->name[i] == '-' && !quote_mode)
880 if (cclp->look_token->len > 1 && i == 0)
882 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
884 ntoken->kind = CCL_TOK_TERM;
885 ntoken->name = cclp->look_token->name + 1;
886 ntoken->len = cclp->look_token->len - 1;
888 cclp->look_token->len = 1;
889 cclp->look_token->name = "-";
891 else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
893 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
895 ntoken->kind = CCL_TOK_TERM;
899 (cclp->look_token->len)--;
901 else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
903 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
904 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
906 ntoken1->kind = CCL_TOK_TERM; /* generate - */
910 ntoken2->kind = CCL_TOK_TERM; /* generate yy */
911 ntoken2->name = cclp->look_token->name + (i+1);
912 ntoken2->len = cclp->look_token->len - (i+1);
914 cclp->look_token->len = i; /* adjust xx */
916 else if (i == cclp->look_token->len &&
917 cclp->look_token->next &&
918 cclp->look_token->next->kind == CCL_TOK_TERM &&
919 cclp->look_token->next->len > 1 &&
920 cclp->look_token->next->name[0] == '-')
923 /* we _know_ that xx does not have - in it */
924 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
926 ntoken->kind = CCL_TOK_TERM; /* generate - */
930 (ntoken->next->name)++; /* adjust yy */
931 (ntoken->next->len)--;
937 KIND == CCL_TOK_TERM &&
938 cclp->look_token->next && cclp->look_token->next->len == 1 &&
939 cclp->look_token->next->name[0] == '-')
941 struct ccl_rpn_node *p1;
942 if (!(p1 = search_term(cclp, ap)))
944 ADVANCE; /* skip '-' */
945 if (KIND == CCL_TOK_TERM) /* = term - term ? */
947 struct ccl_rpn_node *p2;
949 if (!(p2 = search_term(cclp, ap)))
954 p = ccl_rpn_node_create(CCL_RPN_AND);
956 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
958 ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
963 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
968 cclp->look_token->len == 1 &&
969 cclp->look_token->name[0] == '-') /* = - term ? */
972 if (!(p = search_term(cclp, ap)))
974 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
979 if (!(p = search_terms(cclp, ap)))
982 !qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_OMIT_EQUALS, 0))
983 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
990 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
994 if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
995 || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
996 return qualifiers_order(cclp, ap, attset);
998 /* unordered relation */
999 if (KIND != CCL_TOK_EQ)
1001 cclp->error_code = CCL_ERR_EQ_EXPECTED;
1005 return search_terms(cclp, ap);
1009 * qualifier_list: Parse CCL qualifiers and search terms.
1011 * la: Token pointer to RELATION token.
1012 * qa: Qualifier attributes already applied.
1013 * return: pointer to node(s); NULL on error.
1015 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
1016 struct ccl_token *la,
1017 ccl_qualifier_t *qa)
1019 struct ccl_token *lookahead = cclp->look_token;
1020 struct ccl_token *look_start = cclp->look_token;
1021 ccl_qualifier_t *ap;
1022 struct ccl_rpn_node *node = 0;
1023 const char **field_str;
1031 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
1035 for (lookahead = cclp->look_token; lookahead != la;
1036 lookahead=lookahead->next)
1039 for (i=0; qa[i]; i++)
1041 ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
1044 field_str = ccl_qual_search_special(cclp->bibset, "field");
1047 if (!strcmp(field_str[0], "or"))
1049 else if (!strcmp(field_str[0], "merge"))
1054 /* consider each field separately and OR */
1055 lookahead = look_start;
1056 while (lookahead != la)
1060 while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
1061 lookahead->len, seq)) != 0)
1063 struct ccl_rpn_node *node_sub;
1064 cclp->look_token = la;
1066 node_sub = qualifier_relation(cclp, ap);
1069 ccl_rpn_delete(node);
1075 struct ccl_rpn_node *node_this =
1076 ccl_rpn_node_create(CCL_RPN_OR);
1077 node_this->u.p[0] = node;
1078 node_this->u.p[1] = node_sub;
1087 cclp->look_token = lookahead;
1088 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1092 lookahead = lookahead->next;
1093 if (lookahead->kind == CCL_TOK_COMMA)
1094 lookahead = lookahead->next;
1099 /* merge attributes from ALL fields - including inherited ones */
1102 struct ccl_rpn_node *node_sub;
1104 lookahead = look_start;
1105 for (i = 0; lookahead != la; i++)
1107 ap[i] = ccl_qual_search(cclp, lookahead->name,
1108 lookahead->len, seq);
1111 if (!ap[i] && seq > 0)
1112 ap[i] = ccl_qual_search(cclp, lookahead->name,
1116 cclp->look_token = lookahead;
1117 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1121 lookahead = lookahead->next;
1122 if (lookahead->kind == CCL_TOK_COMMA)
1123 lookahead = lookahead->next;
1127 ccl_qualifier_t *qa0 = qa;
1137 cclp->look_token = lookahead;
1139 node_sub = qualifier_relation(cclp, ap);
1142 ccl_rpn_delete(node);
1147 struct ccl_rpn_node *node_this =
1148 ccl_rpn_node_create(CCL_RPN_OR);
1149 node_this->u.p[0] = node;
1150 node_this->u.p[1] = node_sub;
1164 * search_terms: Parse CCL search terms - including proximity.
1166 * qa: Qualifier attributes already applied.
1167 * return: pointer to node(s); NULL on error.
1169 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
1171 static int list[] = {
1172 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
1173 CCL_TOK_REL, CCL_TOK_SET, -1};
1174 struct ccl_rpn_node *p1, *p2, *pn;
1175 p1 = search_terms2(cclp, qa);
1180 if (KIND == CCL_TOK_PROX)
1182 struct ccl_rpn_node *p_prox = 0;
1183 /* ! word order specified */
1184 /* % word order not specified */
1185 p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
1186 p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
1187 memcpy(p_prox->u.t.term, cclp->look_token->name,
1188 cclp->look_token->len);
1189 p_prox->u.t.term[cclp->look_token->len] = 0;
1190 p_prox->u.t.attr_list = 0;
1193 p2 = search_terms2(cclp, qa);
1199 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1202 pn->u.p[2] = p_prox;
1205 else if (is_term_ok(KIND, list))
1207 p2 = search_terms2(cclp, qa);
1213 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1226 * search_elements: Parse CCL search elements
1228 * qa: Qualifier attributes already applied.
1229 * return: pointer to node(s); NULL on error.
1231 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1232 ccl_qualifier_t *qa)
1234 struct ccl_rpn_node *p1;
1235 struct ccl_token *lookahead;
1236 if (KIND == CCL_TOK_SET)
1239 if (KIND == CCL_TOK_EQ)
1241 if (KIND != CCL_TOK_TERM)
1243 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1246 p1 = ccl_rpn_node_create(CCL_RPN_SET);
1247 p1->u.setname = copy_token_name(cclp->look_token);
1251 lookahead = cclp->look_token;
1253 while (lookahead->kind==CCL_TOK_TERM)
1255 lookahead = lookahead->next;
1256 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1257 return qualifier_list(cclp, lookahead, qa);
1258 if (lookahead->kind != CCL_TOK_COMMA)
1260 lookahead = lookahead->next;
1262 if (qa || lookahead->kind == CCL_TOK_LP)
1263 return search_terms(cclp, qa);
1266 ccl_qualifier_t qa[2];
1267 struct ccl_rpn_node *node = 0;
1269 lookahead = cclp->look_token;
1272 for(seq = 0; ;seq++)
1274 struct ccl_rpn_node *node_sub;
1275 qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1279 cclp->look_token = lookahead;
1281 node_sub = search_terms(cclp, qa);
1284 ccl_rpn_delete(node);
1289 struct ccl_rpn_node *node_this =
1290 ccl_rpn_node_create(CCL_RPN_OR);
1291 node_this->u.p[0] = node;
1292 node_this->u.p[1] = node_sub;
1293 node_this->u.p[2] = 0;
1300 node = search_terms(cclp, 0);
1306 * find_spec: Parse CCL find specification
1308 * qa: Qualifier attributes already applied.
1309 * return: pointer to node(s); NULL on error.
1311 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1313 struct ccl_rpn_node *p1, *p2, *pn;
1314 if (!(p1 = search_elements(cclp, qa)))
1322 p2 = search_elements(cclp, qa);
1328 pn = ccl_rpn_node_create(CCL_RPN_AND);
1336 p2 = search_elements(cclp, qa);
1342 pn = ccl_rpn_node_create(CCL_RPN_OR);
1350 p2 = search_elements(cclp, qa);
1356 pn = ccl_rpn_node_create(CCL_RPN_NOT);
1368 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1370 struct ccl_rpn_node *p;
1371 struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1372 p = ccl_parser_find_token(cclp, list);
1373 ccl_token_del(list);
1377 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1378 struct ccl_token *list)
1380 struct ccl_rpn_node *p;
1382 cclp->look_token = list;
1383 p = find_spec(cclp, NULL);
1384 if (p && KIND != CCL_TOK_EOL)
1386 if (KIND == CCL_TOK_RP)
1387 cclp->error_code = CCL_ERR_BAD_RP;
1389 cclp->error_code = CCL_ERR_OP_EXPECTED;
1393 cclp->error_pos = cclp->look_token->name;
1395 cclp->error_code = CCL_ERR_OK;
1397 cclp->error_code = cclp->error_code;
1402 * ccl_find_str: Parse CCL find - string representation
1403 * bibset: Bibset to be used for the parsing
1404 * str: String to be parsed
1405 * error: Pointer to integer. Holds error no. on completion.
1406 * pos: Pointer to char position. Holds approximate error position.
1407 * return: RPN tree on successful completion; NULL otherwise.
1409 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1410 int *error, int *pos)
1412 CCL_parser cclp = ccl_parser_create(bibset);
1413 struct ccl_token *list;
1414 struct ccl_rpn_node *p;
1416 list = ccl_parser_tokenize(cclp, str);
1417 p = ccl_parser_find_token(cclp, list);
1419 *error = cclp->error_code;
1421 *pos = cclp->error_pos - str;
1422 ccl_parser_destroy(cclp);
1423 ccl_token_del(list);
1430 * c-file-style: "Stroustrup"
1431 * indent-tabs-mode: nil
1433 * vim: shiftwidth=4 tabstop=8 expandtab