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,
43 for (i = 0; qa[i]; i++)
46 struct ccl_rpn_attr *q = ccl_qual_get_attr(qa[i]);
47 for (; q; q = q->next)
49 if (q->type == type && q->kind == CCL_RPN_ATTR_NUMERIC)
52 if (q->value.numeric == value)
67 * strxcat: concatenate strings.
68 * n: Null-terminated Destination string
69 * src: Source string to be appended (not null-terminated)
70 * len: Length of source string.
72 static void strxcat(char *n, const char *src, int len)
82 * copy_token_name: Return copy of CCL token name
83 * tp: Pointer to token info.
84 * return: malloc(3) allocated copy of token name.
86 static char *copy_token_name(struct ccl_token *tp)
88 char *str = (char *)xmalloc(tp->len + 1);
90 memcpy(str, tp->name, tp->len);
96 * mk_node: Create RPN node.
98 * return: pointer to allocated node.
100 struct ccl_rpn_node *ccl_rpn_node_create(enum ccl_rpn_kind kind)
102 struct ccl_rpn_node *p;
103 p = (struct ccl_rpn_node *)xmalloc(sizeof(*p));
110 p->u.t.attr_list = 0;
120 static struct ccl_rpn_node *ccl_rpn_node_mkbool(struct ccl_rpn_node *l,
121 struct ccl_rpn_node *r,
122 enum ccl_rpn_kind op)
126 struct ccl_rpn_node *tmp = ccl_rpn_node_create(op);
138 * ccl_rpn_delete: Delete RPN tree.
139 * rpn: Pointer to tree.
141 void ccl_rpn_delete(struct ccl_rpn_node *rpn)
143 struct ccl_rpn_attr *attr, *attr1;
151 ccl_rpn_delete(rpn->u.p[0]);
152 ccl_rpn_delete(rpn->u.p[1]);
155 xfree(rpn->u.t.term);
156 xfree(rpn->u.t.qual);
157 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
160 if (attr->kind == CCL_RPN_ATTR_STRING)
161 xfree(attr->value.str);
168 xfree(rpn->u.setname);
171 ccl_rpn_delete(rpn->u.p[0]);
172 ccl_rpn_delete(rpn->u.p[1]);
173 ccl_rpn_delete(rpn->u.p[2]);
179 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa);
181 static int is_term_ok(int look, int *list)
183 for (; *list >= 0; list++)
189 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa);
191 static struct ccl_rpn_attr *add_attr_node(struct ccl_rpn_node *p,
192 const char *set, int type)
194 struct ccl_rpn_attr *n = (struct ccl_rpn_attr *) xmalloc(sizeof(*n));
197 n->set = xstrdup(set);
201 n->next = p->u.t.attr_list;
202 p->u.t.attr_list = n;
207 * add_attr_numeric: Add attribute (type/value) to RPN term node.
208 * p: RPN node of type term.
209 * type: Type of attribute
210 * value: Value of attribute
211 * set: Attribute set name
213 void ccl_add_attr_numeric(struct ccl_rpn_node *p, const char *set,
216 struct ccl_rpn_attr *n = add_attr_node(p, set, type);
217 n->kind = CCL_RPN_ATTR_NUMERIC;
218 n->value.numeric = value;
221 void ccl_set_attr_numeric(struct ccl_rpn_node *p, const char *set,
224 struct ccl_rpn_attr *n;
225 for (n = p->u.t.attr_list; n; n = n->next)
229 n->set = set ? xstrdup(set) : 0;
230 if (n->kind == CCL_RPN_ATTR_STRING)
232 n->kind = CCL_RPN_ATTR_NUMERIC;
233 n->value.numeric = value;
236 ccl_add_attr_numeric(p, set, type, value);
239 void ccl_add_attr_string(struct ccl_rpn_node *p, const char *set,
240 int type, char *value)
242 struct ccl_rpn_attr *n = add_attr_node(p, set, type);
243 n->kind = CCL_RPN_ATTR_STRING;
244 n->value.str = xstrdup(value);
247 static size_t cmp_operator(const char **aliases, const char *input)
249 for (; *aliases; aliases++)
251 const char *cp = *aliases;
253 for (i = 0; *cp && *cp == input[i]; i++, cp++)
262 #define REGEX_CHARS "^[]{}()|.*+?!$"
263 #define CCL_CHARS "#?\\"
265 static int has_ccl_masking(const char *src_str,
267 const char **truncation_aliases,
268 const char **mask_aliases)
273 for (j = 0; j < src_len; j++)
276 if (j > 0 && src_str[j-1] == '\\')
278 else if (src_str[j] == '"')
279 quote_mode = !quote_mode;
280 else if (!quote_mode &&
281 (op_size = cmp_operator(truncation_aliases,
284 else if (!quote_mode &&
285 (op_size = cmp_operator(mask_aliases,
292 static int append_term(CCL_parser cclp, const char *src_str, size_t src_len,
293 char *dst_term, int regex_trunc, int z3958_trunc,
294 const char **truncation_aliases,
295 const char **mask_aliases,
296 int is_first, int is_last,
297 int *left_trunc, int *right_trunc)
302 for (j = 0; j < src_len; j++)
305 if (j > 0 && src_str[j-1] == '\\')
307 if (regex_trunc && strchr(REGEX_CHARS "\\", src_str[j]))
308 strcat(dst_term, "\\");
309 else if (z3958_trunc && strchr(CCL_CHARS "\\", src_str[j]))
310 strcat(dst_term, "\\");
311 strxcat(dst_term, src_str + j, 1);
313 else if (src_str[j] == '"')
314 quote_mode = !quote_mode;
315 else if (!quote_mode &&
316 (op_size = cmp_operator(truncation_aliases,
320 j += (op_size - 1); /* j++ in for loop */
322 strcat(dst_term, ".*");
323 else if (z3958_trunc)
324 strcat(dst_term, "?");
325 else if (is_first && j == 0)
327 else if (is_last && j == src_len - 1)
331 cclp->error_code = CCL_ERR_TRUNC_NOT_EMBED;
335 else if (!quote_mode &&
336 (op_size = cmp_operator(mask_aliases, src_str + j)))
338 j += (op_size - 1); /* j++ in for loop */
340 strcat(dst_term, ".");
341 else if (z3958_trunc)
342 strcat(dst_term, "#");
345 cclp->error_code = CCL_ERR_TRUNC_NOT_SINGLE;
349 else if (src_str[j] != '\\')
351 if (regex_trunc && strchr(REGEX_CHARS, src_str[j]))
352 strcat(dst_term, "\\");
353 else if (z3958_trunc && strchr(CCL_CHARS, src_str[j]))
354 strcat(dst_term, "\\");
355 strxcat(dst_term, src_str + j, 1);
362 static struct ccl_rpn_node *ccl_term_one_use(CCL_parser cclp,
363 struct ccl_token *lookahead0,
364 struct ccl_rpn_attr *attr_use,
370 struct ccl_rpn_node *p;
372 int structure_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)
435 case CCL_RPN_ATTR_STRING:
436 ccl_add_attr_string(p, attr->set, attr->type,
439 case CCL_RPN_ATTR_NUMERIC:
440 if (attr->value.numeric > 0)
441 { /* deal only with REAL attributes (positive) */
445 if (structure_value != -1)
447 structure_value = attr->value.numeric;
450 ccl_add_attr_numeric(p, attr->set, attr->type,
451 attr->value.numeric);
457 if (structure_value == -1 && (
459 qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
463 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
465 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
467 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_REGEX,
471 regex_trunc = 1; /* regex trunc (102) allowed */
473 else if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_Z3958,
477 z3958_trunc = 1; /* Z39.58 trunc (CCL) trunc allowed */
479 /* make the RPN token */
480 p->u.t.term = (char *)xmalloc(term_len * 2 + 2);
481 ccl_assert(p->u.t.term);
482 p->u.t.term[0] = '\0';
484 for (i = 0; i < no; i++)
486 const char *src_str = lookahead->name;
487 size_t src_len = lookahead->len;
489 if (p->u.t.term[0] && lookahead->ws_prefix_len)
491 strxcat(p->u.t.term, lookahead->ws_prefix_buf,
492 lookahead->ws_prefix_len);
494 if (append_term(cclp, src_str, src_len, p->u.t.term, regex_trunc,
495 z3958_trunc, truncation_aliases, mask_aliases,
497 &left_trunc, &right_trunc))
502 lookahead = lookahead->next;
504 if (left_trunc && right_trunc)
506 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
509 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
513 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
515 else if (right_trunc)
517 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
520 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
524 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
528 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
531 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
535 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
537 else if (regex_trunc)
539 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 102);
541 else if (z3958_trunc)
543 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 104);
547 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
549 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
554 static struct ccl_rpn_node *ccl_term_multi_use(CCL_parser cclp,
555 struct ccl_token *lookahead0,
561 struct ccl_rpn_node *p = 0;
563 for (i = 0; qa && qa[i]; i++)
565 struct ccl_rpn_attr *attr;
566 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
567 if (attr->type == 1 && i == 0)
569 struct ccl_rpn_node *tmp2;
570 tmp2 = ccl_term_one_use(cclp, lookahead0,
572 is_phrase, auto_group);
578 p = ccl_rpn_node_mkbool(p, tmp2, CCL_RPN_OR);
582 p = ccl_term_one_use(cclp, lookahead0,
583 0 /* attr: no use */, qa, no,
584 is_phrase, auto_group);
588 static struct ccl_rpn_node *split_recur(CCL_parser cclp, ccl_qualifier_t *qa,
589 struct ccl_token **ar, size_t sz,
593 struct ccl_rpn_node *p_top = 0;
595 for (l = 1; l <= sz && l <= sub_len; l++)
597 struct ccl_rpn_node *p2 = ccl_term_multi_use(cclp, ar[0],
603 ccl_rpn_delete(p_top);
608 struct ccl_rpn_node *p1 = split_recur(cclp, qa, ar + l, sz - l,
615 p2 = ccl_rpn_node_mkbool(p2, p1, CCL_RPN_AND);
617 p_top = ccl_rpn_node_mkbool(p_top, p2, CCL_RPN_OR);
623 static struct ccl_rpn_node *search_term_split_list(CCL_parser cclp,
625 int *term_list, int multi)
627 struct ccl_rpn_node *p;
628 struct ccl_token **ar;
629 struct ccl_token *lookahead = cclp->look_token;
630 size_t i, sz, sub_len;
631 for (sz = 0; is_term_ok(lookahead->kind, term_list); sz++)
632 lookahead = lookahead->next;
635 cclp->error_code = CCL_ERR_TERM_EXPECTED;
638 ar = (struct ccl_token **) xmalloc(sizeof(*lookahead) * sz);
639 lookahead = cclp->look_token;
640 for (i = 0; is_term_ok(lookahead->kind, term_list); i++)
643 lookahead = lookahead->next;
645 /* choose sub phrase carefully to avoid huge expansions */
652 p = split_recur(cclp, qa, ar, sz, sub_len);
654 for (i = 0; i < sz; i++)
660 * search_term: Parse CCL search term.
662 * qa: Qualifier attributes already applied.
663 * term_list: tokens we accept as terms in context
664 * multi: whether we accept "multiple" tokens
665 * return: pointer to node(s); NULL on error.
667 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
669 int *term_list, int multi)
671 struct ccl_rpn_node *p_top = 0;
672 struct ccl_token *lookahead = cclp->look_token;
677 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
679 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AUTO_GROUP, 0))
681 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
683 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_SPLIT_LIST, 0))
685 return search_term_split_list(cclp, qa, term_list, multi);
689 struct ccl_rpn_node *p = 0;
693 if (and_list || or_list || !multi)
696 /* ignore commas when dealing with and-lists .. */
697 if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
699 lookahead = lookahead->next;
703 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
705 int this_is_phrase = 0;
706 for (i = 0; i<lookahead->len; i++)
707 if (lookahead->name[i] == ' ')
711 if (no > 0 && (is_phrase || is_phrase != this_is_phrase))
713 is_phrase = this_is_phrase;
715 else if (this_is_phrase || no > 0)
717 lookahead = lookahead->next;
721 break; /* no more terms . stop . */
722 p = ccl_term_multi_use(cclp, cclp->look_token, qa, no,
723 is_phrase, auto_group);
724 for (i = 0; i < no; i++)
728 p_top = ccl_rpn_node_mkbool(p_top, p, or_list ? CCL_RPN_OR : CCL_RPN_AND);
733 cclp->error_code = CCL_ERR_TERM_EXPECTED;
737 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
739 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
740 return search_term_x(cclp, qa, list, 0);
744 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
747 if (KIND == CCL_TOK_LP)
749 struct ccl_rpn_node *p;
751 if (!(p = find_spec(cclp, qa)))
753 if (KIND != CCL_TOK_RP)
755 cclp->error_code = CCL_ERR_RP_EXPECTED;
764 static int list[] = {
765 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
766 CCL_TOK_REL, CCL_TOK_SET, -1};
768 return search_term_x(cclp, qa, list, 1);
774 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
775 ccl_qualifier_t *ap, char *attset)
778 struct ccl_rpn_node *p;
780 if (cclp->look_token->len == 1)
782 if (cclp->look_token->name[0] == '<')
784 else if (cclp->look_token->name[0] == '=')
786 else if (cclp->look_token->name[0] == '>')
789 else if (cclp->look_token->len == 2)
791 if (!memcmp(cclp->look_token->name, "<=", 2))
793 else if (!memcmp(cclp->look_token->name, ">=", 2))
795 else if (!memcmp(cclp->look_token->name, "<>", 2))
800 cclp->error_code = CCL_ERR_BAD_RELATION;
803 ADVANCE; /* skip relation */
805 qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
807 /* allow - inside term and treat it as range _always_ */
808 /* relation is =. Extract "embedded" - to separate terms */
809 if (KIND == CCL_TOK_TERM)
813 for (i = 0; i<cclp->look_token->len; i++)
815 if (i > 0 && cclp->look_token->name[i] == '\\')
817 else if (cclp->look_token->name[i] == '"')
818 quote_mode = !quote_mode;
819 else if (cclp->look_token->name[i] == '-' && !quote_mode)
823 if (cclp->look_token->len > 1 && i == 0)
825 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
827 ntoken->kind = CCL_TOK_TERM;
828 ntoken->name = cclp->look_token->name + 1;
829 ntoken->len = cclp->look_token->len - 1;
831 cclp->look_token->len = 1;
832 cclp->look_token->name = "-";
834 else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
836 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
838 ntoken->kind = CCL_TOK_TERM;
842 (cclp->look_token->len)--;
844 else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
846 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
847 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
849 ntoken1->kind = CCL_TOK_TERM; /* generate - */
853 ntoken2->kind = CCL_TOK_TERM; /* generate yy */
854 ntoken2->name = cclp->look_token->name + (i+1);
855 ntoken2->len = cclp->look_token->len - (i+1);
857 cclp->look_token->len = i; /* adjust xx */
859 else if (i == cclp->look_token->len &&
860 cclp->look_token->next &&
861 cclp->look_token->next->kind == CCL_TOK_TERM &&
862 cclp->look_token->next->len > 1 &&
863 cclp->look_token->next->name[0] == '-')
866 /* we _know_ that xx does not have - in it */
867 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
869 ntoken->kind = CCL_TOK_TERM; /* generate - */
873 (ntoken->next->name)++; /* adjust yy */
874 (ntoken->next->len)--;
880 KIND == CCL_TOK_TERM &&
881 cclp->look_token->next && cclp->look_token->next->len == 1 &&
882 cclp->look_token->next->name[0] == '-')
884 struct ccl_rpn_node *p1;
885 if (!(p1 = search_term(cclp, ap)))
887 ADVANCE; /* skip '-' */
888 if (KIND == CCL_TOK_TERM) /* = term - term ? */
890 struct ccl_rpn_node *p2;
892 if (!(p2 = search_term(cclp, ap)))
897 p = ccl_rpn_node_create(CCL_RPN_AND);
899 ccl_set_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
901 ccl_set_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
906 ccl_set_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
911 cclp->look_token->len == 1 &&
912 cclp->look_token->name[0] == '-') /* = - term ? */
915 if (!(p = search_term(cclp, ap)))
917 ccl_set_attr_numeric(p, attset, CCL_BIB1_REL, 2);
922 if (!(p = search_terms(cclp, ap)))
925 !qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_OMIT_EQUALS, 0))
926 ccl_set_attr_numeric(p, attset, CCL_BIB1_REL, rel);
933 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
937 if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
938 || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
939 return qualifiers_order(cclp, ap, attset);
941 /* unordered relation */
942 if (KIND != CCL_TOK_EQ)
944 cclp->error_code = CCL_ERR_EQ_EXPECTED;
948 return search_terms(cclp, ap);
952 * qualifier_list: Parse CCL qualifiers and search terms.
954 * la: Token pointer to RELATION token.
955 * qa: Qualifier attributes already applied.
956 * return: pointer to node(s); NULL on error.
958 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
959 struct ccl_token *la,
962 struct ccl_token *lookahead = cclp->look_token;
963 struct ccl_token *look_start = cclp->look_token;
965 struct ccl_rpn_node *node = 0;
966 const char **field_str;
974 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
978 for (lookahead = cclp->look_token; lookahead != la;
979 lookahead=lookahead->next)
982 for (i=0; qa[i]; i++)
984 ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
987 field_str = ccl_qual_search_special(cclp->bibset, "field");
990 if (!strcmp(field_str[0], "or"))
992 else if (!strcmp(field_str[0], "merge"))
997 /* consider each field separately and OR */
998 lookahead = look_start;
999 while (lookahead != la)
1003 while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
1004 lookahead->len, seq)) != 0)
1006 struct ccl_rpn_node *node_sub;
1007 cclp->look_token = la;
1009 node_sub = qualifier_relation(cclp, ap);
1012 ccl_rpn_delete(node);
1016 node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1021 cclp->look_token = lookahead;
1022 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1026 lookahead = lookahead->next;
1027 if (lookahead->kind == CCL_TOK_COMMA)
1028 lookahead = lookahead->next;
1033 /* merge attributes from ALL fields - including inherited ones */
1036 struct ccl_rpn_node *node_sub;
1038 lookahead = look_start;
1039 for (i = 0; lookahead != la; i++)
1041 ap[i] = ccl_qual_search(cclp, lookahead->name,
1042 lookahead->len, seq);
1045 if (!ap[i] && seq > 0)
1046 ap[i] = ccl_qual_search(cclp, lookahead->name,
1050 cclp->look_token = lookahead;
1051 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1055 lookahead = lookahead->next;
1056 if (lookahead->kind == CCL_TOK_COMMA)
1057 lookahead = lookahead->next;
1061 ccl_qualifier_t *qa0 = qa;
1071 cclp->look_token = lookahead;
1073 node_sub = qualifier_relation(cclp, ap);
1076 ccl_rpn_delete(node);
1079 node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1089 * search_terms: Parse CCL search terms - including proximity.
1091 * qa: Qualifier attributes already applied.
1092 * return: pointer to node(s); NULL on error.
1094 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
1096 static int list[] = {
1097 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
1098 CCL_TOK_REL, CCL_TOK_SET, -1};
1099 struct ccl_rpn_node *p1, *p2, *pn;
1100 p1 = search_terms2(cclp, qa);
1105 if (KIND == CCL_TOK_PROX)
1107 struct ccl_rpn_node *p_prox = 0;
1108 /* ! word order specified */
1109 /* % word order not specified */
1110 p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
1111 p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
1112 memcpy(p_prox->u.t.term, cclp->look_token->name,
1113 cclp->look_token->len);
1114 p_prox->u.t.term[cclp->look_token->len] = 0;
1115 p_prox->u.t.attr_list = 0;
1118 p2 = search_terms2(cclp, qa);
1124 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1127 pn->u.p[2] = p_prox;
1130 else if (is_term_ok(KIND, list))
1132 p2 = search_terms2(cclp, qa);
1138 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1151 * search_elements: Parse CCL search elements
1153 * qa: Qualifier attributes already applied.
1154 * return: pointer to node(s); NULL on error.
1156 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1157 ccl_qualifier_t *qa)
1159 struct ccl_rpn_node *p1;
1160 struct ccl_token *lookahead;
1161 if (KIND == CCL_TOK_SET)
1164 if (KIND == CCL_TOK_EQ)
1166 if (KIND != CCL_TOK_TERM)
1168 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1171 p1 = ccl_rpn_node_create(CCL_RPN_SET);
1172 p1->u.setname = copy_token_name(cclp->look_token);
1176 lookahead = cclp->look_token;
1178 while (lookahead->kind==CCL_TOK_TERM)
1180 lookahead = lookahead->next;
1181 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1182 return qualifier_list(cclp, lookahead, qa);
1183 if (lookahead->kind != CCL_TOK_COMMA)
1185 lookahead = lookahead->next;
1187 if (qa || lookahead->kind == CCL_TOK_LP)
1188 return search_terms(cclp, qa);
1191 ccl_qualifier_t qa[2];
1192 struct ccl_rpn_node *node = 0;
1194 lookahead = cclp->look_token;
1197 for(seq = 0; ;seq++)
1199 struct ccl_rpn_node *node_sub;
1200 qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1204 cclp->look_token = lookahead;
1206 node_sub = search_terms(cclp, qa);
1209 ccl_rpn_delete(node);
1212 node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1215 node = search_terms(cclp, 0);
1221 * find_spec: Parse CCL find specification
1223 * qa: Qualifier attributes already applied.
1224 * return: pointer to node(s); NULL on error.
1226 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1228 struct ccl_rpn_node *p1, *p2;
1229 if (!(p1 = search_elements(cclp, qa)))
1237 p2 = search_elements(cclp, qa);
1243 p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_AND);
1247 p2 = search_elements(cclp, qa);
1253 p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_OR);
1257 p2 = search_elements(cclp, qa);
1263 p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_NOT);
1271 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1273 struct ccl_rpn_node *p;
1274 struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1275 p = ccl_parser_find_token(cclp, list);
1276 ccl_token_del(list);
1280 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1281 struct ccl_token *list)
1283 struct ccl_rpn_node *p;
1285 cclp->look_token = list;
1286 p = find_spec(cclp, NULL);
1287 if (p && KIND != CCL_TOK_EOL)
1289 if (KIND == CCL_TOK_RP)
1290 cclp->error_code = CCL_ERR_BAD_RP;
1292 cclp->error_code = CCL_ERR_OP_EXPECTED;
1296 cclp->error_pos = cclp->look_token->name;
1298 cclp->error_code = CCL_ERR_OK;
1300 cclp->error_code = cclp->error_code;
1305 * ccl_find_str: Parse CCL find - string representation
1306 * bibset: Bibset to be used for the parsing
1307 * str: String to be parsed
1308 * error: Pointer to integer. Holds error no. on completion.
1309 * pos: Pointer to char position. Holds approximate error position.
1310 * return: RPN tree on successful completion; NULL otherwise.
1312 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1313 int *error, int *pos)
1315 CCL_parser cclp = ccl_parser_create(bibset);
1316 struct ccl_token *list;
1317 struct ccl_rpn_node *p;
1319 list = ccl_parser_tokenize(cclp, str);
1320 p = ccl_parser_find_token(cclp, list);
1322 *error = cclp->error_code;
1324 *pos = cclp->error_pos - str;
1325 ccl_parser_destroy(cclp);
1326 ccl_token_del(list);
1333 * c-file-style: "Stroustrup"
1334 * indent-tabs-mode: nil
1336 * vim: shiftwidth=4 tabstop=8 expandtab