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_node_mkbool(struct ccl_rpn_node *l,
117 struct ccl_rpn_node *r,
118 enum ccl_rpn_kind op)
122 struct ccl_rpn_node *tmp = ccl_rpn_node_create(op);
133 static struct ccl_rpn_node *ccl_rpn_dup(struct ccl_rpn_node *rpn)
135 struct ccl_rpn_node *n;
136 struct ccl_rpn_attr *attr, **attrp;
139 n = ccl_rpn_node_create(rpn->kind);
145 n->u.p[0] = ccl_rpn_dup(rpn->u.p[0]);
146 n->u.p[1] = ccl_rpn_dup(rpn->u.p[1]);
149 n->u.t.term = xstrdup(rpn->u.t.term);
150 n->u.t.qual = rpn->u.t.qual ? xstrdup(rpn->u.t.qual) : 0;
151 attrp = &n->u.t.attr_list;
152 for (attr = rpn->u.t.attr_list; attr; attr = attr->next)
154 *attrp = (struct ccl_rpn_attr *) xmalloc(sizeof(**attrp));
155 (*attrp)->kind = attr->kind;
156 (*attrp)->type = attr->type;
157 if (attr->kind == CCL_RPN_ATTR_STRING)
158 (*attrp)->value.str = xstrdup(attr->value.str);
160 (*attrp)->value.numeric = attr->value.numeric;
162 (*attrp)->set = xstrdup(attr->set);
165 attrp = &(*attrp)->next;
170 n->u.setname = xstrdup(rpn->u.setname);
173 n->u.p[0] = ccl_rpn_dup(rpn->u.p[0]);
174 n->u.p[1] = ccl_rpn_dup(rpn->u.p[1]);
175 n->u.p[2] = ccl_rpn_dup(rpn->u.p[2]);
182 * ccl_rpn_delete: Delete RPN tree.
183 * rpn: Pointer to tree.
185 void ccl_rpn_delete(struct ccl_rpn_node *rpn)
187 struct ccl_rpn_attr *attr, *attr1;
195 ccl_rpn_delete(rpn->u.p[0]);
196 ccl_rpn_delete(rpn->u.p[1]);
199 xfree(rpn->u.t.term);
200 xfree(rpn->u.t.qual);
201 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
204 if (attr->kind == CCL_RPN_ATTR_STRING)
205 xfree(attr->value.str);
212 xfree(rpn->u.setname);
215 ccl_rpn_delete(rpn->u.p[0]);
216 ccl_rpn_delete(rpn->u.p[1]);
217 ccl_rpn_delete(rpn->u.p[2]);
223 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa);
225 static int is_term_ok(int look, int *list)
227 for (; *list >= 0; list++)
233 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa);
235 static struct ccl_rpn_attr *add_attr_node(struct ccl_rpn_node *p,
236 const char *set, int type)
238 struct ccl_rpn_attr *n = (struct ccl_rpn_attr *) xmalloc(sizeof(*n));
241 n->set = xstrdup(set);
245 n->next = p->u.t.attr_list;
246 p->u.t.attr_list = n;
251 * add_attr_numeric: Add attribute (type/value) to RPN term node.
252 * p: RPN node of type term.
253 * type: Type of attribute
254 * value: Value of attribute
255 * set: Attribute set name
257 void ccl_add_attr_numeric(struct ccl_rpn_node *p, const char *set,
260 struct ccl_rpn_attr *n = add_attr_node(p, set, type);
261 n->kind = CCL_RPN_ATTR_NUMERIC;
262 n->value.numeric = value;
265 void ccl_add_attr_string(struct ccl_rpn_node *p, const char *set,
266 int type, char *value)
268 struct ccl_rpn_attr *n = add_attr_node(p, set, type);
269 n->kind = CCL_RPN_ATTR_STRING;
270 n->value.str = xstrdup(value);
273 static size_t cmp_operator(const char **aliases, const char *input)
275 for (; *aliases; aliases++)
277 const char *cp = *aliases;
279 for (i = 0; *cp && *cp == input[i]; i++, cp++)
288 #define REGEX_CHARS "^[]{}()|.*+?!$"
289 #define CCL_CHARS "#?\\"
291 static int has_ccl_masking(const char *src_str,
293 const char **truncation_aliases,
294 const char **mask_aliases)
299 for (j = 0; j < src_len; j++)
302 if (j > 0 && src_str[j-1] == '\\')
304 else if (src_str[j] == '"')
305 quote_mode = !quote_mode;
306 else if (!quote_mode &&
307 (op_size = cmp_operator(truncation_aliases,
310 else if (!quote_mode &&
311 (op_size = cmp_operator(mask_aliases,
318 static int append_term(CCL_parser cclp, const char *src_str, size_t src_len,
319 char *dst_term, int regex_trunc, int z3958_trunc,
320 const char **truncation_aliases,
321 const char **mask_aliases,
322 int is_first, int is_last,
323 int *left_trunc, int *right_trunc)
328 for (j = 0; j < src_len; j++)
331 if (j > 0 && src_str[j-1] == '\\')
333 if (regex_trunc && strchr(REGEX_CHARS "\\", src_str[j]))
334 strcat(dst_term, "\\");
335 else if (z3958_trunc && strchr(CCL_CHARS "\\", src_str[j]))
336 strcat(dst_term, "\\");
337 strxcat(dst_term, src_str + j, 1);
339 else if (src_str[j] == '"')
340 quote_mode = !quote_mode;
341 else if (!quote_mode &&
342 (op_size = cmp_operator(truncation_aliases,
346 j += (op_size - 1); /* j++ in for loop */
348 strcat(dst_term, ".*");
349 else if (z3958_trunc)
350 strcat(dst_term, "?");
351 else if (is_first && j == 0)
353 else if (is_last && j == src_len - 1)
357 cclp->error_code = CCL_ERR_TRUNC_NOT_EMBED;
361 else if (!quote_mode &&
362 (op_size = cmp_operator(mask_aliases, src_str + j)))
364 j += (op_size - 1); /* j++ in for loop */
366 strcat(dst_term, ".");
367 else if (z3958_trunc)
368 strcat(dst_term, "#");
371 cclp->error_code = CCL_ERR_TRUNC_NOT_SINGLE;
375 else if (src_str[j] != '\\')
377 if (regex_trunc && strchr(REGEX_CHARS, src_str[j]))
378 strcat(dst_term, "\\");
379 else if (z3958_trunc && strchr(CCL_CHARS, src_str[j]))
380 strcat(dst_term, "\\");
381 strxcat(dst_term, src_str + j, 1);
388 static struct ccl_rpn_node *ccl_term_one_use(CCL_parser cclp,
389 struct ccl_token *lookahead0,
390 struct ccl_rpn_attr *attr_use,
396 struct ccl_rpn_node *p;
398 int relation_value = -1;
399 int position_value = -1;
400 int structure_value = -1;
401 int truncation_value = -1;
402 int completeness_value = -1;
408 int is_ccl_masked = 0;
410 struct ccl_token *lookahead = lookahead0;
411 const char **truncation_aliases;
412 const char *t_default[2];
413 const char **mask_aliases;
414 const char *m_default[2];
418 ccl_qual_search_special(cclp->bibset, "truncation");
419 if (!truncation_aliases)
421 truncation_aliases = t_default;
426 ccl_qual_search_special(cclp->bibset, "mask");
429 mask_aliases = m_default;
433 for (i = 0; i < no; i++)
435 if (has_ccl_masking(lookahead->name, lookahead->len,
440 term_len += 1 + lookahead->len + lookahead->ws_prefix_len;
441 lookahead = lookahead->next;
443 lookahead = lookahead0;
445 p = ccl_rpn_node_create(CCL_RPN_TERM);
446 p->u.t.attr_list = NULL;
450 const char *n = ccl_qual_get_name(qa[0]);
452 p->u.t.qual = xstrdup(n);
454 /* go through all attributes and add them to the attribute list */
455 for (i = 0; qa && qa[i]; i++)
457 struct ccl_rpn_attr *attr;
458 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
459 if (attr->type != 1 || !attr_use || attr == attr_use)
463 case CCL_RPN_ATTR_STRING:
464 ccl_add_attr_string(p, attr->set, attr->type,
467 case CCL_RPN_ATTR_NUMERIC:
468 if (attr->value.numeric > 0)
469 { /* deal only with REAL attributes (positive) */
473 if (relation_value != -1)
475 relation_value = attr->value.numeric;
478 if (position_value != -1)
480 position_value = attr->value.numeric;
483 if (structure_value != -1)
485 structure_value = attr->value.numeric;
488 if (truncation_value != -1)
490 truncation_value = attr->value.numeric;
493 if (completeness_value != -1)
495 completeness_value = attr->value.numeric;
498 ccl_add_attr_numeric(p, attr->set, attr->type,
499 attr->value.numeric);
505 if (structure_value == -1 && (
507 qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
511 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
513 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
515 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_REGEX,
519 regex_trunc = 1; /* regex trunc (102) allowed */
521 else if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_Z3958,
525 z3958_trunc = 1; /* Z39.58 trunc (CCL) trunc allowed */
527 /* make the RPN token */
528 p->u.t.term = (char *)xmalloc(term_len * 2 + 2);
529 ccl_assert(p->u.t.term);
530 p->u.t.term[0] = '\0';
532 for (i = 0; i < no; i++)
534 const char *src_str = lookahead->name;
535 size_t src_len = lookahead->len;
537 if (p->u.t.term[0] && lookahead->ws_prefix_len)
539 strxcat(p->u.t.term, lookahead->ws_prefix_buf,
540 lookahead->ws_prefix_len);
542 if (append_term(cclp, src_str, src_len, p->u.t.term, regex_trunc,
543 z3958_trunc, truncation_aliases, mask_aliases,
545 &left_trunc, &right_trunc))
550 lookahead = lookahead->next;
552 if (left_trunc && right_trunc)
554 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
557 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
561 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
563 else if (right_trunc)
565 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
568 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
572 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
576 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
579 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
583 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
585 else if (regex_trunc)
587 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 102);
589 else if (z3958_trunc)
591 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 104);
595 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
597 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
602 static struct ccl_rpn_node *ccl_term_multi_use(CCL_parser cclp,
603 struct ccl_token *lookahead0,
609 struct ccl_rpn_node *p = 0;
611 for (i = 0; qa && qa[i]; i++)
613 struct ccl_rpn_attr *attr;
614 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
615 if (attr->type == 1 && i == 0)
617 struct ccl_rpn_node *tmp2;
618 tmp2 = ccl_term_one_use(cclp, lookahead0,
620 is_phrase, auto_group);
626 p = ccl_rpn_node_mkbool(p, tmp2, CCL_RPN_OR);
630 p = ccl_term_one_use(cclp, lookahead0,
631 0 /* attr: no use */, qa, no,
632 is_phrase, auto_group);
636 static struct ccl_rpn_node *split_recur(CCL_parser cclp, ccl_qualifier_t *qa,
637 struct ccl_token **ar, size_t sz)
640 struct ccl_rpn_node *p_top = 0;
642 for (l = 1; l <= sz; l++)
644 struct ccl_rpn_node *p2 = ccl_term_multi_use(cclp, ar[0],
650 ccl_rpn_delete(p_top);
655 struct ccl_rpn_node *p1 = split_recur(cclp, qa, ar + l, sz - l);
661 p2 = ccl_rpn_node_mkbool(p2, p1, CCL_RPN_AND);
663 p_top = ccl_rpn_node_mkbool(p_top, p2, 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, 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 p_top = ccl_rpn_node_mkbool(p_top, p, or_list ? CCL_RPN_OR : CCL_RPN_AND);
772 cclp->error_code = CCL_ERR_TERM_EXPECTED;
776 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
778 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
779 return search_term_x(cclp, qa, list, 0);
783 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
786 if (KIND == CCL_TOK_LP)
788 struct ccl_rpn_node *p;
790 if (!(p = find_spec(cclp, qa)))
792 if (KIND != CCL_TOK_RP)
794 cclp->error_code = CCL_ERR_RP_EXPECTED;
803 static int list[] = {
804 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
805 CCL_TOK_REL, CCL_TOK_SET, -1};
807 return search_term_x(cclp, qa, list, 1);
813 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
814 ccl_qualifier_t *ap, char *attset)
817 struct ccl_rpn_node *p;
819 if (cclp->look_token->len == 1)
821 if (cclp->look_token->name[0] == '<')
823 else if (cclp->look_token->name[0] == '=')
825 else if (cclp->look_token->name[0] == '>')
828 else if (cclp->look_token->len == 2)
830 if (!memcmp(cclp->look_token->name, "<=", 2))
832 else if (!memcmp(cclp->look_token->name, ">=", 2))
834 else if (!memcmp(cclp->look_token->name, "<>", 2))
839 cclp->error_code = CCL_ERR_BAD_RELATION;
842 ADVANCE; /* skip relation */
844 qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
846 /* allow - inside term and treat it as range _always_ */
847 /* relation is =. Extract "embedded" - to separate terms */
848 if (KIND == CCL_TOK_TERM)
852 for (i = 0; i<cclp->look_token->len; i++)
854 if (i > 0 && cclp->look_token->name[i] == '\\')
856 else if (cclp->look_token->name[i] == '"')
857 quote_mode = !quote_mode;
858 else if (cclp->look_token->name[i] == '-' && !quote_mode)
862 if (cclp->look_token->len > 1 && i == 0)
864 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
866 ntoken->kind = CCL_TOK_TERM;
867 ntoken->name = cclp->look_token->name + 1;
868 ntoken->len = cclp->look_token->len - 1;
870 cclp->look_token->len = 1;
871 cclp->look_token->name = "-";
873 else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
875 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
877 ntoken->kind = CCL_TOK_TERM;
881 (cclp->look_token->len)--;
883 else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
885 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
886 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
888 ntoken1->kind = CCL_TOK_TERM; /* generate - */
892 ntoken2->kind = CCL_TOK_TERM; /* generate yy */
893 ntoken2->name = cclp->look_token->name + (i+1);
894 ntoken2->len = cclp->look_token->len - (i+1);
896 cclp->look_token->len = i; /* adjust xx */
898 else if (i == cclp->look_token->len &&
899 cclp->look_token->next &&
900 cclp->look_token->next->kind == CCL_TOK_TERM &&
901 cclp->look_token->next->len > 1 &&
902 cclp->look_token->next->name[0] == '-')
905 /* we _know_ that xx does not have - in it */
906 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
908 ntoken->kind = CCL_TOK_TERM; /* generate - */
912 (ntoken->next->name)++; /* adjust yy */
913 (ntoken->next->len)--;
919 KIND == CCL_TOK_TERM &&
920 cclp->look_token->next && cclp->look_token->next->len == 1 &&
921 cclp->look_token->next->name[0] == '-')
923 struct ccl_rpn_node *p1;
924 if (!(p1 = search_term(cclp, ap)))
926 ADVANCE; /* skip '-' */
927 if (KIND == CCL_TOK_TERM) /* = term - term ? */
929 struct ccl_rpn_node *p2;
931 if (!(p2 = search_term(cclp, ap)))
936 p = ccl_rpn_node_create(CCL_RPN_AND);
938 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
940 ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
945 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
950 cclp->look_token->len == 1 &&
951 cclp->look_token->name[0] == '-') /* = - term ? */
954 if (!(p = search_term(cclp, ap)))
956 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
961 if (!(p = search_terms(cclp, ap)))
964 !qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_OMIT_EQUALS, 0))
965 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
972 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
976 if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
977 || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
978 return qualifiers_order(cclp, ap, attset);
980 /* unordered relation */
981 if (KIND != CCL_TOK_EQ)
983 cclp->error_code = CCL_ERR_EQ_EXPECTED;
987 return search_terms(cclp, ap);
991 * qualifier_list: Parse CCL qualifiers and search terms.
993 * la: Token pointer to RELATION token.
994 * qa: Qualifier attributes already applied.
995 * return: pointer to node(s); NULL on error.
997 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
998 struct ccl_token *la,
1001 struct ccl_token *lookahead = cclp->look_token;
1002 struct ccl_token *look_start = cclp->look_token;
1003 ccl_qualifier_t *ap;
1004 struct ccl_rpn_node *node = 0;
1005 const char **field_str;
1013 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
1017 for (lookahead = cclp->look_token; lookahead != la;
1018 lookahead=lookahead->next)
1021 for (i=0; qa[i]; i++)
1023 ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
1026 field_str = ccl_qual_search_special(cclp->bibset, "field");
1029 if (!strcmp(field_str[0], "or"))
1031 else if (!strcmp(field_str[0], "merge"))
1036 /* consider each field separately and OR */
1037 lookahead = look_start;
1038 while (lookahead != la)
1042 while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
1043 lookahead->len, seq)) != 0)
1045 struct ccl_rpn_node *node_sub;
1046 cclp->look_token = la;
1048 node_sub = qualifier_relation(cclp, ap);
1051 ccl_rpn_delete(node);
1055 node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1060 cclp->look_token = lookahead;
1061 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1065 lookahead = lookahead->next;
1066 if (lookahead->kind == CCL_TOK_COMMA)
1067 lookahead = lookahead->next;
1072 /* merge attributes from ALL fields - including inherited ones */
1075 struct ccl_rpn_node *node_sub;
1077 lookahead = look_start;
1078 for (i = 0; lookahead != la; i++)
1080 ap[i] = ccl_qual_search(cclp, lookahead->name,
1081 lookahead->len, seq);
1084 if (!ap[i] && seq > 0)
1085 ap[i] = ccl_qual_search(cclp, lookahead->name,
1089 cclp->look_token = lookahead;
1090 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1094 lookahead = lookahead->next;
1095 if (lookahead->kind == CCL_TOK_COMMA)
1096 lookahead = lookahead->next;
1100 ccl_qualifier_t *qa0 = qa;
1110 cclp->look_token = lookahead;
1112 node_sub = qualifier_relation(cclp, ap);
1115 ccl_rpn_delete(node);
1118 node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1128 * search_terms: Parse CCL search terms - including proximity.
1130 * qa: Qualifier attributes already applied.
1131 * return: pointer to node(s); NULL on error.
1133 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
1135 static int list[] = {
1136 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
1137 CCL_TOK_REL, CCL_TOK_SET, -1};
1138 struct ccl_rpn_node *p1, *p2, *pn;
1139 p1 = search_terms2(cclp, qa);
1144 if (KIND == CCL_TOK_PROX)
1146 struct ccl_rpn_node *p_prox = 0;
1147 /* ! word order specified */
1148 /* % word order not specified */
1149 p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
1150 p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
1151 memcpy(p_prox->u.t.term, cclp->look_token->name,
1152 cclp->look_token->len);
1153 p_prox->u.t.term[cclp->look_token->len] = 0;
1154 p_prox->u.t.attr_list = 0;
1157 p2 = search_terms2(cclp, qa);
1163 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1166 pn->u.p[2] = p_prox;
1169 else if (is_term_ok(KIND, list))
1171 p2 = search_terms2(cclp, qa);
1177 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1190 * search_elements: Parse CCL search elements
1192 * qa: Qualifier attributes already applied.
1193 * return: pointer to node(s); NULL on error.
1195 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1196 ccl_qualifier_t *qa)
1198 struct ccl_rpn_node *p1;
1199 struct ccl_token *lookahead;
1200 if (KIND == CCL_TOK_SET)
1203 if (KIND == CCL_TOK_EQ)
1205 if (KIND != CCL_TOK_TERM)
1207 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1210 p1 = ccl_rpn_node_create(CCL_RPN_SET);
1211 p1->u.setname = copy_token_name(cclp->look_token);
1215 lookahead = cclp->look_token;
1217 while (lookahead->kind==CCL_TOK_TERM)
1219 lookahead = lookahead->next;
1220 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1221 return qualifier_list(cclp, lookahead, qa);
1222 if (lookahead->kind != CCL_TOK_COMMA)
1224 lookahead = lookahead->next;
1226 if (qa || lookahead->kind == CCL_TOK_LP)
1227 return search_terms(cclp, qa);
1230 ccl_qualifier_t qa[2];
1231 struct ccl_rpn_node *node = 0;
1233 lookahead = cclp->look_token;
1236 for(seq = 0; ;seq++)
1238 struct ccl_rpn_node *node_sub;
1239 qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1243 cclp->look_token = lookahead;
1245 node_sub = search_terms(cclp, qa);
1248 ccl_rpn_delete(node);
1251 node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1254 node = search_terms(cclp, 0);
1260 * find_spec: Parse CCL find specification
1262 * qa: Qualifier attributes already applied.
1263 * return: pointer to node(s); NULL on error.
1265 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1267 struct ccl_rpn_node *p1, *p2;
1268 if (!(p1 = search_elements(cclp, qa)))
1276 p2 = search_elements(cclp, qa);
1282 p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_AND);
1286 p2 = search_elements(cclp, qa);
1292 p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_OR);
1296 p2 = search_elements(cclp, qa);
1302 p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_NOT);
1310 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1312 struct ccl_rpn_node *p;
1313 struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1314 p = ccl_parser_find_token(cclp, list);
1315 ccl_token_del(list);
1319 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1320 struct ccl_token *list)
1322 struct ccl_rpn_node *p;
1324 cclp->look_token = list;
1325 p = find_spec(cclp, NULL);
1326 if (p && KIND != CCL_TOK_EOL)
1328 if (KIND == CCL_TOK_RP)
1329 cclp->error_code = CCL_ERR_BAD_RP;
1331 cclp->error_code = CCL_ERR_OP_EXPECTED;
1335 cclp->error_pos = cclp->look_token->name;
1337 cclp->error_code = CCL_ERR_OK;
1339 cclp->error_code = cclp->error_code;
1344 * ccl_find_str: Parse CCL find - string representation
1345 * bibset: Bibset to be used for the parsing
1346 * str: String to be parsed
1347 * error: Pointer to integer. Holds error no. on completion.
1348 * pos: Pointer to char position. Holds approximate error position.
1349 * return: RPN tree on successful completion; NULL otherwise.
1351 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1352 int *error, int *pos)
1354 CCL_parser cclp = ccl_parser_create(bibset);
1355 struct ccl_token *list;
1356 struct ccl_rpn_node *p;
1358 list = ccl_parser_tokenize(cclp, str);
1359 p = ccl_parser_find_token(cclp, list);
1361 *error = cclp->error_code;
1363 *pos = cclp->error_pos - str;
1364 ccl_parser_destroy(cclp);
1365 ccl_token_del(list);
1372 * c-file-style: "Stroustrup"
1373 * indent-tabs-mode: nil
1375 * vim: shiftwidth=4 tabstop=8 expandtab