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);
134 * ccl_rpn_delete: Delete RPN tree.
135 * rpn: Pointer to tree.
137 void ccl_rpn_delete(struct ccl_rpn_node *rpn)
139 struct ccl_rpn_attr *attr, *attr1;
147 ccl_rpn_delete(rpn->u.p[0]);
148 ccl_rpn_delete(rpn->u.p[1]);
151 xfree(rpn->u.t.term);
152 xfree(rpn->u.t.qual);
153 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
156 if (attr->kind == CCL_RPN_ATTR_STRING)
157 xfree(attr->value.str);
164 xfree(rpn->u.setname);
167 ccl_rpn_delete(rpn->u.p[0]);
168 ccl_rpn_delete(rpn->u.p[1]);
169 ccl_rpn_delete(rpn->u.p[2]);
175 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa);
177 static int is_term_ok(int look, int *list)
179 for (; *list >= 0; list++)
185 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa);
187 static struct ccl_rpn_attr *add_attr_node(struct ccl_rpn_node *p,
188 const char *set, int type)
190 struct ccl_rpn_attr *n = (struct ccl_rpn_attr *) xmalloc(sizeof(*n));
193 n->set = xstrdup(set);
197 n->next = p->u.t.attr_list;
198 p->u.t.attr_list = n;
203 * add_attr_numeric: Add attribute (type/value) to RPN term node.
204 * p: RPN node of type term.
205 * type: Type of attribute
206 * value: Value of attribute
207 * set: Attribute set name
209 void ccl_add_attr_numeric(struct ccl_rpn_node *p, const char *set,
212 struct ccl_rpn_attr *n = add_attr_node(p, set, type);
213 n->kind = CCL_RPN_ATTR_NUMERIC;
214 n->value.numeric = value;
217 void ccl_set_attr_numeric(struct ccl_rpn_node *p, const char *set,
220 struct ccl_rpn_attr *n;
221 for (n = p->u.t.attr_list; n; n = n->next)
225 n->set = set ? xstrdup(set) : 0;
226 if (n->kind == CCL_RPN_ATTR_STRING)
228 n->kind = CCL_RPN_ATTR_NUMERIC;
229 n->value.numeric = value;
232 ccl_add_attr_numeric(p, set, type, value);
235 void ccl_add_attr_string(struct ccl_rpn_node *p, const char *set,
236 int type, char *value)
238 struct ccl_rpn_attr *n = add_attr_node(p, set, type);
239 n->kind = CCL_RPN_ATTR_STRING;
240 n->value.str = xstrdup(value);
243 static size_t cmp_operator(const char **aliases, const char *input)
245 for (; *aliases; aliases++)
247 const char *cp = *aliases;
249 for (i = 0; *cp && *cp == input[i]; i++, cp++)
258 #define REGEX_CHARS "^[]{}()|.*+?!$"
259 #define CCL_CHARS "#?\\"
261 static int has_ccl_masking(const char *src_str,
263 const char **truncation_aliases,
264 const char **mask_aliases)
269 for (j = 0; j < src_len; j++)
272 if (j > 0 && src_str[j-1] == '\\')
274 else if (src_str[j] == '"')
275 quote_mode = !quote_mode;
276 else if (!quote_mode &&
277 (op_size = cmp_operator(truncation_aliases,
280 else if (!quote_mode &&
281 (op_size = cmp_operator(mask_aliases,
288 static int append_term(CCL_parser cclp, const char *src_str, size_t src_len,
289 char *dst_term, int regex_trunc, int z3958_trunc,
290 const char **truncation_aliases,
291 const char **mask_aliases,
292 int is_first, int is_last,
293 int *left_trunc, int *right_trunc)
298 for (j = 0; j < src_len; j++)
301 if (j > 0 && src_str[j-1] == '\\')
303 if (regex_trunc && strchr(REGEX_CHARS "\\", src_str[j]))
304 strcat(dst_term, "\\");
305 else if (z3958_trunc && strchr(CCL_CHARS "\\", src_str[j]))
306 strcat(dst_term, "\\");
307 strxcat(dst_term, src_str + j, 1);
309 else if (src_str[j] == '"')
310 quote_mode = !quote_mode;
311 else if (!quote_mode &&
312 (op_size = cmp_operator(truncation_aliases,
316 j += (op_size - 1); /* j++ in for loop */
318 strcat(dst_term, ".*");
319 else if (z3958_trunc)
320 strcat(dst_term, "?");
321 else if (is_first && j == 0)
323 else if (is_last && j == src_len - 1)
327 cclp->error_code = CCL_ERR_TRUNC_NOT_EMBED;
331 else if (!quote_mode &&
332 (op_size = cmp_operator(mask_aliases, src_str + j)))
334 j += (op_size - 1); /* j++ in for loop */
336 strcat(dst_term, ".");
337 else if (z3958_trunc)
338 strcat(dst_term, "#");
341 cclp->error_code = CCL_ERR_TRUNC_NOT_SINGLE;
345 else if (src_str[j] != '\\')
347 if (regex_trunc && strchr(REGEX_CHARS, src_str[j]))
348 strcat(dst_term, "\\");
349 else if (z3958_trunc && strchr(CCL_CHARS, src_str[j]))
350 strcat(dst_term, "\\");
351 strxcat(dst_term, src_str + j, 1);
358 static struct ccl_rpn_node *ccl_term_one_use(CCL_parser cclp,
359 struct ccl_token *lookahead0,
360 struct ccl_rpn_attr *attr_use,
366 struct ccl_rpn_node *p;
368 int relation_value = -1;
369 int position_value = -1;
370 int structure_value = -1;
371 int truncation_value = -1;
372 int completeness_value = -1;
378 int is_ccl_masked = 0;
380 struct ccl_token *lookahead = lookahead0;
381 const char **truncation_aliases;
382 const char *t_default[2];
383 const char **mask_aliases;
384 const char *m_default[2];
388 ccl_qual_search_special(cclp->bibset, "truncation");
389 if (!truncation_aliases)
391 truncation_aliases = t_default;
396 ccl_qual_search_special(cclp->bibset, "mask");
399 mask_aliases = m_default;
403 for (i = 0; i < no; i++)
405 if (has_ccl_masking(lookahead->name, lookahead->len,
410 term_len += 1 + lookahead->len + lookahead->ws_prefix_len;
411 lookahead = lookahead->next;
413 lookahead = lookahead0;
415 p = ccl_rpn_node_create(CCL_RPN_TERM);
416 p->u.t.attr_list = NULL;
420 const char *n = ccl_qual_get_name(qa[0]);
422 p->u.t.qual = xstrdup(n);
424 /* go through all attributes and add them to the attribute list */
425 for (i = 0; qa && qa[i]; i++)
427 struct ccl_rpn_attr *attr;
428 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
429 if (attr->type != 1 || !attr_use || attr == attr_use)
433 case CCL_RPN_ATTR_STRING:
434 ccl_add_attr_string(p, attr->set, attr->type,
437 case CCL_RPN_ATTR_NUMERIC:
438 if (attr->value.numeric > 0)
439 { /* deal only with REAL attributes (positive) */
443 if (relation_value != -1)
445 relation_value = attr->value.numeric;
448 if (position_value != -1)
450 position_value = attr->value.numeric;
453 if (structure_value != -1)
455 structure_value = attr->value.numeric;
458 if (truncation_value != -1)
460 truncation_value = attr->value.numeric;
463 if (completeness_value != -1)
465 completeness_value = attr->value.numeric;
468 ccl_add_attr_numeric(p, attr->set, attr->type,
469 attr->value.numeric);
475 if (structure_value == -1 && (
477 qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
481 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
483 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
485 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_REGEX,
489 regex_trunc = 1; /* regex trunc (102) allowed */
491 else if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_Z3958,
495 z3958_trunc = 1; /* Z39.58 trunc (CCL) trunc allowed */
497 /* make the RPN token */
498 p->u.t.term = (char *)xmalloc(term_len * 2 + 2);
499 ccl_assert(p->u.t.term);
500 p->u.t.term[0] = '\0';
502 for (i = 0; i < no; i++)
504 const char *src_str = lookahead->name;
505 size_t src_len = lookahead->len;
507 if (p->u.t.term[0] && lookahead->ws_prefix_len)
509 strxcat(p->u.t.term, lookahead->ws_prefix_buf,
510 lookahead->ws_prefix_len);
512 if (append_term(cclp, src_str, src_len, p->u.t.term, regex_trunc,
513 z3958_trunc, truncation_aliases, mask_aliases,
515 &left_trunc, &right_trunc))
520 lookahead = lookahead->next;
522 if (left_trunc && right_trunc)
524 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
527 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
531 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
533 else if (right_trunc)
535 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
538 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
542 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
546 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
549 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
553 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
555 else if (regex_trunc)
557 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 102);
559 else if (z3958_trunc)
561 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 104);
565 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
567 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
572 static struct ccl_rpn_node *ccl_term_multi_use(CCL_parser cclp,
573 struct ccl_token *lookahead0,
579 struct ccl_rpn_node *p = 0;
581 for (i = 0; qa && qa[i]; i++)
583 struct ccl_rpn_attr *attr;
584 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
585 if (attr->type == 1 && i == 0)
587 struct ccl_rpn_node *tmp2;
588 tmp2 = ccl_term_one_use(cclp, lookahead0,
590 is_phrase, auto_group);
596 p = ccl_rpn_node_mkbool(p, tmp2, CCL_RPN_OR);
600 p = ccl_term_one_use(cclp, lookahead0,
601 0 /* attr: no use */, qa, no,
602 is_phrase, auto_group);
606 static struct ccl_rpn_node *split_recur(CCL_parser cclp, ccl_qualifier_t *qa,
607 struct ccl_token **ar, size_t sz,
611 struct ccl_rpn_node *p_top = 0;
613 for (l = 1; l <= sz && l <= sub_len; l++)
615 struct ccl_rpn_node *p2 = ccl_term_multi_use(cclp, ar[0],
621 ccl_rpn_delete(p_top);
626 struct ccl_rpn_node *p1 = split_recur(cclp, qa, ar + l, sz - l,
633 p2 = ccl_rpn_node_mkbool(p2, p1, CCL_RPN_AND);
635 p_top = ccl_rpn_node_mkbool(p_top, p2, CCL_RPN_OR);
641 static struct ccl_rpn_node *search_term_split_list(CCL_parser cclp,
643 int *term_list, int multi)
645 struct ccl_rpn_node *p;
646 struct ccl_token **ar;
647 struct ccl_token *lookahead = cclp->look_token;
648 size_t i, sz, sub_len;
649 for (sz = 0; is_term_ok(lookahead->kind, term_list); sz++)
650 lookahead = lookahead->next;
653 cclp->error_code = CCL_ERR_TERM_EXPECTED;
656 ar = (struct ccl_token **) xmalloc(sizeof(*lookahead) * sz);
657 lookahead = cclp->look_token;
658 for (i = 0; is_term_ok(lookahead->kind, term_list); i++)
661 lookahead = lookahead->next;
663 /* choose sub phrase carefully to avoid huge expansions */
670 p = split_recur(cclp, qa, ar, sz, sub_len);
672 for (i = 0; i < sz; i++)
678 * search_term: Parse CCL search term.
680 * qa: Qualifier attributes already applied.
681 * term_list: tokens we accept as terms in context
682 * multi: whether we accept "multiple" tokens
683 * return: pointer to node(s); NULL on error.
685 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
687 int *term_list, int multi)
689 struct ccl_rpn_node *p_top = 0;
690 struct ccl_token *lookahead = cclp->look_token;
695 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
697 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AUTO_GROUP, 0))
699 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
701 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_SPLIT_LIST, 0))
703 return search_term_split_list(cclp, qa, term_list, multi);
707 struct ccl_rpn_node *p = 0;
711 if (and_list || or_list || !multi)
714 /* ignore commas when dealing with and-lists .. */
715 if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
717 lookahead = lookahead->next;
721 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
723 int this_is_phrase = 0;
724 for (i = 0; i<lookahead->len; i++)
725 if (lookahead->name[i] == ' ')
729 if (no > 0 && (is_phrase || is_phrase != this_is_phrase))
731 is_phrase = this_is_phrase;
733 else if (this_is_phrase || no > 0)
735 lookahead = lookahead->next;
739 break; /* no more terms . stop . */
740 p = ccl_term_multi_use(cclp, cclp->look_token, qa, no,
741 is_phrase, auto_group);
742 for (i = 0; i < no; i++)
746 p_top = ccl_rpn_node_mkbool(p_top, p, or_list ? CCL_RPN_OR : CCL_RPN_AND);
751 cclp->error_code = CCL_ERR_TERM_EXPECTED;
755 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
757 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
758 return search_term_x(cclp, qa, list, 0);
762 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
765 if (KIND == CCL_TOK_LP)
767 struct ccl_rpn_node *p;
769 if (!(p = find_spec(cclp, qa)))
771 if (KIND != CCL_TOK_RP)
773 cclp->error_code = CCL_ERR_RP_EXPECTED;
782 static int list[] = {
783 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
784 CCL_TOK_REL, CCL_TOK_SET, -1};
786 return search_term_x(cclp, qa, list, 1);
792 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
793 ccl_qualifier_t *ap, char *attset)
796 struct ccl_rpn_node *p;
798 if (cclp->look_token->len == 1)
800 if (cclp->look_token->name[0] == '<')
802 else if (cclp->look_token->name[0] == '=')
804 else if (cclp->look_token->name[0] == '>')
807 else if (cclp->look_token->len == 2)
809 if (!memcmp(cclp->look_token->name, "<=", 2))
811 else if (!memcmp(cclp->look_token->name, ">=", 2))
813 else if (!memcmp(cclp->look_token->name, "<>", 2))
818 cclp->error_code = CCL_ERR_BAD_RELATION;
821 ADVANCE; /* skip relation */
823 qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
825 /* allow - inside term and treat it as range _always_ */
826 /* relation is =. Extract "embedded" - to separate terms */
827 if (KIND == CCL_TOK_TERM)
831 for (i = 0; i<cclp->look_token->len; i++)
833 if (i > 0 && cclp->look_token->name[i] == '\\')
835 else if (cclp->look_token->name[i] == '"')
836 quote_mode = !quote_mode;
837 else if (cclp->look_token->name[i] == '-' && !quote_mode)
841 if (cclp->look_token->len > 1 && i == 0)
843 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
845 ntoken->kind = CCL_TOK_TERM;
846 ntoken->name = cclp->look_token->name + 1;
847 ntoken->len = cclp->look_token->len - 1;
849 cclp->look_token->len = 1;
850 cclp->look_token->name = "-";
852 else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
854 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
856 ntoken->kind = CCL_TOK_TERM;
860 (cclp->look_token->len)--;
862 else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
864 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
865 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
867 ntoken1->kind = CCL_TOK_TERM; /* generate - */
871 ntoken2->kind = CCL_TOK_TERM; /* generate yy */
872 ntoken2->name = cclp->look_token->name + (i+1);
873 ntoken2->len = cclp->look_token->len - (i+1);
875 cclp->look_token->len = i; /* adjust xx */
877 else if (i == cclp->look_token->len &&
878 cclp->look_token->next &&
879 cclp->look_token->next->kind == CCL_TOK_TERM &&
880 cclp->look_token->next->len > 1 &&
881 cclp->look_token->next->name[0] == '-')
884 /* we _know_ that xx does not have - in it */
885 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
887 ntoken->kind = CCL_TOK_TERM; /* generate - */
891 (ntoken->next->name)++; /* adjust yy */
892 (ntoken->next->len)--;
898 KIND == CCL_TOK_TERM &&
899 cclp->look_token->next && cclp->look_token->next->len == 1 &&
900 cclp->look_token->next->name[0] == '-')
902 struct ccl_rpn_node *p1;
903 if (!(p1 = search_term(cclp, ap)))
905 ADVANCE; /* skip '-' */
906 if (KIND == CCL_TOK_TERM) /* = term - term ? */
908 struct ccl_rpn_node *p2;
910 if (!(p2 = search_term(cclp, ap)))
915 p = ccl_rpn_node_create(CCL_RPN_AND);
917 ccl_set_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
919 ccl_set_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
924 ccl_set_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
929 cclp->look_token->len == 1 &&
930 cclp->look_token->name[0] == '-') /* = - term ? */
933 if (!(p = search_term(cclp, ap)))
935 ccl_set_attr_numeric(p, attset, CCL_BIB1_REL, 2);
940 if (!(p = search_terms(cclp, ap)))
943 !qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_OMIT_EQUALS, 0))
944 ccl_set_attr_numeric(p, attset, CCL_BIB1_REL, rel);
951 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
955 if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
956 || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
957 return qualifiers_order(cclp, ap, attset);
959 /* unordered relation */
960 if (KIND != CCL_TOK_EQ)
962 cclp->error_code = CCL_ERR_EQ_EXPECTED;
966 return search_terms(cclp, ap);
970 * qualifier_list: Parse CCL qualifiers and search terms.
972 * la: Token pointer to RELATION token.
973 * qa: Qualifier attributes already applied.
974 * return: pointer to node(s); NULL on error.
976 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
977 struct ccl_token *la,
980 struct ccl_token *lookahead = cclp->look_token;
981 struct ccl_token *look_start = cclp->look_token;
983 struct ccl_rpn_node *node = 0;
984 const char **field_str;
992 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
996 for (lookahead = cclp->look_token; lookahead != la;
997 lookahead=lookahead->next)
1000 for (i=0; qa[i]; i++)
1002 ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
1005 field_str = ccl_qual_search_special(cclp->bibset, "field");
1008 if (!strcmp(field_str[0], "or"))
1010 else if (!strcmp(field_str[0], "merge"))
1015 /* consider each field separately and OR */
1016 lookahead = look_start;
1017 while (lookahead != la)
1021 while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
1022 lookahead->len, seq)) != 0)
1024 struct ccl_rpn_node *node_sub;
1025 cclp->look_token = la;
1027 node_sub = qualifier_relation(cclp, ap);
1030 ccl_rpn_delete(node);
1034 node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1039 cclp->look_token = lookahead;
1040 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1044 lookahead = lookahead->next;
1045 if (lookahead->kind == CCL_TOK_COMMA)
1046 lookahead = lookahead->next;
1051 /* merge attributes from ALL fields - including inherited ones */
1054 struct ccl_rpn_node *node_sub;
1056 lookahead = look_start;
1057 for (i = 0; lookahead != la; i++)
1059 ap[i] = ccl_qual_search(cclp, lookahead->name,
1060 lookahead->len, seq);
1063 if (!ap[i] && seq > 0)
1064 ap[i] = ccl_qual_search(cclp, lookahead->name,
1068 cclp->look_token = lookahead;
1069 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1073 lookahead = lookahead->next;
1074 if (lookahead->kind == CCL_TOK_COMMA)
1075 lookahead = lookahead->next;
1079 ccl_qualifier_t *qa0 = qa;
1089 cclp->look_token = lookahead;
1091 node_sub = qualifier_relation(cclp, ap);
1094 ccl_rpn_delete(node);
1097 node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1107 * search_terms: Parse CCL search terms - including proximity.
1109 * qa: Qualifier attributes already applied.
1110 * return: pointer to node(s); NULL on error.
1112 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
1114 static int list[] = {
1115 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
1116 CCL_TOK_REL, CCL_TOK_SET, -1};
1117 struct ccl_rpn_node *p1, *p2, *pn;
1118 p1 = search_terms2(cclp, qa);
1123 if (KIND == CCL_TOK_PROX)
1125 struct ccl_rpn_node *p_prox = 0;
1126 /* ! word order specified */
1127 /* % word order not specified */
1128 p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
1129 p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
1130 memcpy(p_prox->u.t.term, cclp->look_token->name,
1131 cclp->look_token->len);
1132 p_prox->u.t.term[cclp->look_token->len] = 0;
1133 p_prox->u.t.attr_list = 0;
1136 p2 = search_terms2(cclp, qa);
1142 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1145 pn->u.p[2] = p_prox;
1148 else if (is_term_ok(KIND, list))
1150 p2 = search_terms2(cclp, qa);
1156 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1169 * search_elements: Parse CCL search elements
1171 * qa: Qualifier attributes already applied.
1172 * return: pointer to node(s); NULL on error.
1174 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1175 ccl_qualifier_t *qa)
1177 struct ccl_rpn_node *p1;
1178 struct ccl_token *lookahead;
1179 if (KIND == CCL_TOK_SET)
1182 if (KIND == CCL_TOK_EQ)
1184 if (KIND != CCL_TOK_TERM)
1186 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1189 p1 = ccl_rpn_node_create(CCL_RPN_SET);
1190 p1->u.setname = copy_token_name(cclp->look_token);
1194 lookahead = cclp->look_token;
1196 while (lookahead->kind==CCL_TOK_TERM)
1198 lookahead = lookahead->next;
1199 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1200 return qualifier_list(cclp, lookahead, qa);
1201 if (lookahead->kind != CCL_TOK_COMMA)
1203 lookahead = lookahead->next;
1205 if (qa || lookahead->kind == CCL_TOK_LP)
1206 return search_terms(cclp, qa);
1209 ccl_qualifier_t qa[2];
1210 struct ccl_rpn_node *node = 0;
1212 lookahead = cclp->look_token;
1215 for(seq = 0; ;seq++)
1217 struct ccl_rpn_node *node_sub;
1218 qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1222 cclp->look_token = lookahead;
1224 node_sub = search_terms(cclp, qa);
1227 ccl_rpn_delete(node);
1230 node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1233 node = search_terms(cclp, 0);
1239 * find_spec: Parse CCL find specification
1241 * qa: Qualifier attributes already applied.
1242 * return: pointer to node(s); NULL on error.
1244 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1246 struct ccl_rpn_node *p1, *p2;
1247 if (!(p1 = search_elements(cclp, qa)))
1255 p2 = search_elements(cclp, qa);
1261 p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_AND);
1265 p2 = search_elements(cclp, qa);
1271 p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_OR);
1275 p2 = search_elements(cclp, qa);
1281 p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_NOT);
1289 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1291 struct ccl_rpn_node *p;
1292 struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1293 p = ccl_parser_find_token(cclp, list);
1294 ccl_token_del(list);
1298 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1299 struct ccl_token *list)
1301 struct ccl_rpn_node *p;
1303 cclp->look_token = list;
1304 p = find_spec(cclp, NULL);
1305 if (p && KIND != CCL_TOK_EOL)
1307 if (KIND == CCL_TOK_RP)
1308 cclp->error_code = CCL_ERR_BAD_RP;
1310 cclp->error_code = CCL_ERR_OP_EXPECTED;
1314 cclp->error_pos = cclp->look_token->name;
1316 cclp->error_code = CCL_ERR_OK;
1318 cclp->error_code = cclp->error_code;
1323 * ccl_find_str: Parse CCL find - string representation
1324 * bibset: Bibset to be used for the parsing
1325 * str: String to be parsed
1326 * error: Pointer to integer. Holds error no. on completion.
1327 * pos: Pointer to char position. Holds approximate error position.
1328 * return: RPN tree on successful completion; NULL otherwise.
1330 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1331 int *error, int *pos)
1333 CCL_parser cclp = ccl_parser_create(bibset);
1334 struct ccl_token *list;
1335 struct ccl_rpn_node *p;
1337 list = ccl_parser_tokenize(cclp, str);
1338 p = ccl_parser_find_token(cclp, list);
1340 *error = cclp->error_code;
1342 *pos = cclp->error_pos - str;
1343 ccl_parser_destroy(cclp);
1344 ccl_token_del(list);
1351 * c-file-style: "Stroustrup"
1352 * indent-tabs-mode: nil
1354 * vim: shiftwidth=4 tabstop=8 expandtab