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_add_attr_string(struct ccl_rpn_node *p, const char *set,
218 int type, char *value)
220 struct ccl_rpn_attr *n = add_attr_node(p, set, type);
221 n->kind = CCL_RPN_ATTR_STRING;
222 n->value.str = xstrdup(value);
225 static size_t cmp_operator(const char **aliases, const char *input)
227 for (; *aliases; aliases++)
229 const char *cp = *aliases;
231 for (i = 0; *cp && *cp == input[i]; i++, cp++)
240 #define REGEX_CHARS "^[]{}()|.*+?!$"
241 #define CCL_CHARS "#?\\"
243 static int has_ccl_masking(const char *src_str,
245 const char **truncation_aliases,
246 const char **mask_aliases)
251 for (j = 0; j < src_len; j++)
254 if (j > 0 && src_str[j-1] == '\\')
256 else if (src_str[j] == '"')
257 quote_mode = !quote_mode;
258 else if (!quote_mode &&
259 (op_size = cmp_operator(truncation_aliases,
262 else if (!quote_mode &&
263 (op_size = cmp_operator(mask_aliases,
270 static int append_term(CCL_parser cclp, const char *src_str, size_t src_len,
271 char *dst_term, int regex_trunc, int z3958_trunc,
272 const char **truncation_aliases,
273 const char **mask_aliases,
274 int is_first, int is_last,
275 int *left_trunc, int *right_trunc)
280 for (j = 0; j < src_len; j++)
283 if (j > 0 && src_str[j-1] == '\\')
285 if (regex_trunc && strchr(REGEX_CHARS "\\", src_str[j]))
286 strcat(dst_term, "\\");
287 else if (z3958_trunc && strchr(CCL_CHARS "\\", src_str[j]))
288 strcat(dst_term, "\\");
289 strxcat(dst_term, src_str + j, 1);
291 else if (src_str[j] == '"')
292 quote_mode = !quote_mode;
293 else if (!quote_mode &&
294 (op_size = cmp_operator(truncation_aliases,
298 j += (op_size - 1); /* j++ in for loop */
300 strcat(dst_term, ".*");
301 else if (z3958_trunc)
302 strcat(dst_term, "?");
303 else if (is_first && j == 0)
305 else if (is_last && j == src_len - 1)
309 cclp->error_code = CCL_ERR_TRUNC_NOT_EMBED;
313 else if (!quote_mode &&
314 (op_size = cmp_operator(mask_aliases, src_str + j)))
316 j += (op_size - 1); /* j++ in for loop */
318 strcat(dst_term, ".");
319 else if (z3958_trunc)
320 strcat(dst_term, "#");
323 cclp->error_code = CCL_ERR_TRUNC_NOT_SINGLE;
327 else if (src_str[j] != '\\')
329 if (regex_trunc && strchr(REGEX_CHARS, src_str[j]))
330 strcat(dst_term, "\\");
331 else if (z3958_trunc && strchr(CCL_CHARS, src_str[j]))
332 strcat(dst_term, "\\");
333 strxcat(dst_term, src_str + j, 1);
340 static struct ccl_rpn_node *ccl_term_one_use(CCL_parser cclp,
341 struct ccl_token *lookahead0,
342 struct ccl_rpn_attr *attr_use,
348 struct ccl_rpn_node *p;
350 int relation_value = -1;
351 int position_value = -1;
352 int structure_value = -1;
353 int truncation_value = -1;
354 int completeness_value = -1;
360 int is_ccl_masked = 0;
362 struct ccl_token *lookahead = lookahead0;
363 const char **truncation_aliases;
364 const char *t_default[2];
365 const char **mask_aliases;
366 const char *m_default[2];
370 ccl_qual_search_special(cclp->bibset, "truncation");
371 if (!truncation_aliases)
373 truncation_aliases = t_default;
378 ccl_qual_search_special(cclp->bibset, "mask");
381 mask_aliases = m_default;
385 for (i = 0; i < no; i++)
387 if (has_ccl_masking(lookahead->name, lookahead->len,
392 term_len += 1 + lookahead->len + lookahead->ws_prefix_len;
393 lookahead = lookahead->next;
395 lookahead = lookahead0;
397 p = ccl_rpn_node_create(CCL_RPN_TERM);
398 p->u.t.attr_list = NULL;
402 const char *n = ccl_qual_get_name(qa[0]);
404 p->u.t.qual = xstrdup(n);
406 /* go through all attributes and add them to the attribute list */
407 for (i = 0; qa && qa[i]; i++)
409 struct ccl_rpn_attr *attr;
410 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
411 if (attr->type != 1 || !attr_use || attr == attr_use)
415 case CCL_RPN_ATTR_STRING:
416 ccl_add_attr_string(p, attr->set, attr->type,
419 case CCL_RPN_ATTR_NUMERIC:
420 if (attr->value.numeric > 0)
421 { /* deal only with REAL attributes (positive) */
425 if (relation_value != -1)
427 relation_value = attr->value.numeric;
430 if (position_value != -1)
432 position_value = attr->value.numeric;
435 if (structure_value != -1)
437 structure_value = attr->value.numeric;
440 if (truncation_value != -1)
442 truncation_value = attr->value.numeric;
445 if (completeness_value != -1)
447 completeness_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)
592 struct ccl_rpn_node *p_top = 0;
594 for (l = 1; l <= sz; l++)
596 struct ccl_rpn_node *p2 = ccl_term_multi_use(cclp, ar[0],
602 ccl_rpn_delete(p_top);
607 struct ccl_rpn_node *p1 = split_recur(cclp, qa, ar + l, sz - l);
613 p2 = ccl_rpn_node_mkbool(p2, p1, CCL_RPN_AND);
615 p_top = ccl_rpn_node_mkbool(p_top, p2, CCL_RPN_OR);
621 static struct ccl_rpn_node *search_term_split_list(CCL_parser cclp,
623 int *term_list, int multi)
625 struct ccl_rpn_node *p;
626 struct ccl_token **ar;
627 struct ccl_token *lookahead = cclp->look_token;
629 for (sz = 0; is_term_ok(lookahead->kind, term_list); sz++)
630 lookahead = lookahead->next;
633 cclp->error_code = CCL_ERR_TERM_EXPECTED;
636 ar = (struct ccl_token **) xmalloc(sizeof(*lookahead) * sz);
637 lookahead = cclp->look_token;
638 for (i = 0; is_term_ok(lookahead->kind, term_list); i++)
641 lookahead = lookahead->next;
643 p = split_recur(cclp, qa, ar, sz);
645 for (i = 0; i < sz; i++)
651 * search_term: Parse CCL search term.
653 * qa: Qualifier attributes already applied.
654 * term_list: tokens we accept as terms in context
655 * multi: whether we accept "multiple" tokens
656 * return: pointer to node(s); NULL on error.
658 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
660 int *term_list, int multi)
662 struct ccl_rpn_node *p_top = 0;
663 struct ccl_token *lookahead = cclp->look_token;
668 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
670 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AUTO_GROUP, 0))
672 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
674 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_SPLIT_LIST, 0))
676 return search_term_split_list(cclp, qa, term_list, multi);
680 struct ccl_rpn_node *p = 0;
684 if (and_list || or_list || !multi)
687 /* ignore commas when dealing with and-lists .. */
688 if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
690 lookahead = lookahead->next;
694 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
696 int this_is_phrase = 0;
697 for (i = 0; i<lookahead->len; i++)
698 if (lookahead->name[i] == ' ')
702 if (no > 0 && (is_phrase || is_phrase != this_is_phrase))
704 is_phrase = this_is_phrase;
706 else if (this_is_phrase || no > 0)
708 lookahead = lookahead->next;
712 break; /* no more terms . stop . */
713 p = ccl_term_multi_use(cclp, cclp->look_token, qa, no,
714 is_phrase, auto_group);
715 for (i = 0; i < no; i++)
719 p_top = ccl_rpn_node_mkbool(p_top, p, or_list ? CCL_RPN_OR : CCL_RPN_AND);
724 cclp->error_code = CCL_ERR_TERM_EXPECTED;
728 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
730 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
731 return search_term_x(cclp, qa, list, 0);
735 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
738 if (KIND == CCL_TOK_LP)
740 struct ccl_rpn_node *p;
742 if (!(p = find_spec(cclp, qa)))
744 if (KIND != CCL_TOK_RP)
746 cclp->error_code = CCL_ERR_RP_EXPECTED;
755 static int list[] = {
756 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
757 CCL_TOK_REL, CCL_TOK_SET, -1};
759 return search_term_x(cclp, qa, list, 1);
765 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
766 ccl_qualifier_t *ap, char *attset)
769 struct ccl_rpn_node *p;
771 if (cclp->look_token->len == 1)
773 if (cclp->look_token->name[0] == '<')
775 else if (cclp->look_token->name[0] == '=')
777 else if (cclp->look_token->name[0] == '>')
780 else if (cclp->look_token->len == 2)
782 if (!memcmp(cclp->look_token->name, "<=", 2))
784 else if (!memcmp(cclp->look_token->name, ">=", 2))
786 else if (!memcmp(cclp->look_token->name, "<>", 2))
791 cclp->error_code = CCL_ERR_BAD_RELATION;
794 ADVANCE; /* skip relation */
796 qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
798 /* allow - inside term and treat it as range _always_ */
799 /* relation is =. Extract "embedded" - to separate terms */
800 if (KIND == CCL_TOK_TERM)
804 for (i = 0; i<cclp->look_token->len; i++)
806 if (i > 0 && cclp->look_token->name[i] == '\\')
808 else if (cclp->look_token->name[i] == '"')
809 quote_mode = !quote_mode;
810 else if (cclp->look_token->name[i] == '-' && !quote_mode)
814 if (cclp->look_token->len > 1 && i == 0)
816 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
818 ntoken->kind = CCL_TOK_TERM;
819 ntoken->name = cclp->look_token->name + 1;
820 ntoken->len = cclp->look_token->len - 1;
822 cclp->look_token->len = 1;
823 cclp->look_token->name = "-";
825 else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
827 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
829 ntoken->kind = CCL_TOK_TERM;
833 (cclp->look_token->len)--;
835 else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
837 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
838 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
840 ntoken1->kind = CCL_TOK_TERM; /* generate - */
844 ntoken2->kind = CCL_TOK_TERM; /* generate yy */
845 ntoken2->name = cclp->look_token->name + (i+1);
846 ntoken2->len = cclp->look_token->len - (i+1);
848 cclp->look_token->len = i; /* adjust xx */
850 else if (i == cclp->look_token->len &&
851 cclp->look_token->next &&
852 cclp->look_token->next->kind == CCL_TOK_TERM &&
853 cclp->look_token->next->len > 1 &&
854 cclp->look_token->next->name[0] == '-')
857 /* we _know_ that xx does not have - in it */
858 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
860 ntoken->kind = CCL_TOK_TERM; /* generate - */
864 (ntoken->next->name)++; /* adjust yy */
865 (ntoken->next->len)--;
871 KIND == CCL_TOK_TERM &&
872 cclp->look_token->next && cclp->look_token->next->len == 1 &&
873 cclp->look_token->next->name[0] == '-')
875 struct ccl_rpn_node *p1;
876 if (!(p1 = search_term(cclp, ap)))
878 ADVANCE; /* skip '-' */
879 if (KIND == CCL_TOK_TERM) /* = term - term ? */
881 struct ccl_rpn_node *p2;
883 if (!(p2 = search_term(cclp, ap)))
888 p = ccl_rpn_node_create(CCL_RPN_AND);
890 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
892 ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
897 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
902 cclp->look_token->len == 1 &&
903 cclp->look_token->name[0] == '-') /* = - term ? */
906 if (!(p = search_term(cclp, ap)))
908 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
913 if (!(p = search_terms(cclp, ap)))
916 !qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_OMIT_EQUALS, 0))
917 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
924 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
928 if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
929 || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
930 return qualifiers_order(cclp, ap, attset);
932 /* unordered relation */
933 if (KIND != CCL_TOK_EQ)
935 cclp->error_code = CCL_ERR_EQ_EXPECTED;
939 return search_terms(cclp, ap);
943 * qualifier_list: Parse CCL qualifiers and search terms.
945 * la: Token pointer to RELATION token.
946 * qa: Qualifier attributes already applied.
947 * return: pointer to node(s); NULL on error.
949 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
950 struct ccl_token *la,
953 struct ccl_token *lookahead = cclp->look_token;
954 struct ccl_token *look_start = cclp->look_token;
956 struct ccl_rpn_node *node = 0;
957 const char **field_str;
965 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
969 for (lookahead = cclp->look_token; lookahead != la;
970 lookahead=lookahead->next)
973 for (i=0; qa[i]; i++)
975 ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
978 field_str = ccl_qual_search_special(cclp->bibset, "field");
981 if (!strcmp(field_str[0], "or"))
983 else if (!strcmp(field_str[0], "merge"))
988 /* consider each field separately and OR */
989 lookahead = look_start;
990 while (lookahead != la)
994 while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
995 lookahead->len, seq)) != 0)
997 struct ccl_rpn_node *node_sub;
998 cclp->look_token = la;
1000 node_sub = qualifier_relation(cclp, ap);
1003 ccl_rpn_delete(node);
1007 node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1012 cclp->look_token = lookahead;
1013 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1017 lookahead = lookahead->next;
1018 if (lookahead->kind == CCL_TOK_COMMA)
1019 lookahead = lookahead->next;
1024 /* merge attributes from ALL fields - including inherited ones */
1027 struct ccl_rpn_node *node_sub;
1029 lookahead = look_start;
1030 for (i = 0; lookahead != la; i++)
1032 ap[i] = ccl_qual_search(cclp, lookahead->name,
1033 lookahead->len, seq);
1036 if (!ap[i] && seq > 0)
1037 ap[i] = ccl_qual_search(cclp, lookahead->name,
1041 cclp->look_token = lookahead;
1042 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1046 lookahead = lookahead->next;
1047 if (lookahead->kind == CCL_TOK_COMMA)
1048 lookahead = lookahead->next;
1052 ccl_qualifier_t *qa0 = qa;
1062 cclp->look_token = lookahead;
1064 node_sub = qualifier_relation(cclp, ap);
1067 ccl_rpn_delete(node);
1070 node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1080 * search_terms: Parse CCL search terms - including proximity.
1082 * qa: Qualifier attributes already applied.
1083 * return: pointer to node(s); NULL on error.
1085 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
1087 static int list[] = {
1088 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
1089 CCL_TOK_REL, CCL_TOK_SET, -1};
1090 struct ccl_rpn_node *p1, *p2, *pn;
1091 p1 = search_terms2(cclp, qa);
1096 if (KIND == CCL_TOK_PROX)
1098 struct ccl_rpn_node *p_prox = 0;
1099 /* ! word order specified */
1100 /* % word order not specified */
1101 p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
1102 p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
1103 memcpy(p_prox->u.t.term, cclp->look_token->name,
1104 cclp->look_token->len);
1105 p_prox->u.t.term[cclp->look_token->len] = 0;
1106 p_prox->u.t.attr_list = 0;
1109 p2 = search_terms2(cclp, qa);
1115 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1118 pn->u.p[2] = p_prox;
1121 else if (is_term_ok(KIND, list))
1123 p2 = search_terms2(cclp, qa);
1129 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1142 * search_elements: Parse CCL search elements
1144 * qa: Qualifier attributes already applied.
1145 * return: pointer to node(s); NULL on error.
1147 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1148 ccl_qualifier_t *qa)
1150 struct ccl_rpn_node *p1;
1151 struct ccl_token *lookahead;
1152 if (KIND == CCL_TOK_SET)
1155 if (KIND == CCL_TOK_EQ)
1157 if (KIND != CCL_TOK_TERM)
1159 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1162 p1 = ccl_rpn_node_create(CCL_RPN_SET);
1163 p1->u.setname = copy_token_name(cclp->look_token);
1167 lookahead = cclp->look_token;
1169 while (lookahead->kind==CCL_TOK_TERM)
1171 lookahead = lookahead->next;
1172 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1173 return qualifier_list(cclp, lookahead, qa);
1174 if (lookahead->kind != CCL_TOK_COMMA)
1176 lookahead = lookahead->next;
1178 if (qa || lookahead->kind == CCL_TOK_LP)
1179 return search_terms(cclp, qa);
1182 ccl_qualifier_t qa[2];
1183 struct ccl_rpn_node *node = 0;
1185 lookahead = cclp->look_token;
1188 for(seq = 0; ;seq++)
1190 struct ccl_rpn_node *node_sub;
1191 qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1195 cclp->look_token = lookahead;
1197 node_sub = search_terms(cclp, qa);
1200 ccl_rpn_delete(node);
1203 node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1206 node = search_terms(cclp, 0);
1212 * find_spec: Parse CCL find specification
1214 * qa: Qualifier attributes already applied.
1215 * return: pointer to node(s); NULL on error.
1217 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1219 struct ccl_rpn_node *p1, *p2;
1220 if (!(p1 = search_elements(cclp, qa)))
1228 p2 = search_elements(cclp, qa);
1234 p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_AND);
1238 p2 = search_elements(cclp, qa);
1244 p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_OR);
1248 p2 = search_elements(cclp, qa);
1254 p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_NOT);
1262 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1264 struct ccl_rpn_node *p;
1265 struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1266 p = ccl_parser_find_token(cclp, list);
1267 ccl_token_del(list);
1271 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1272 struct ccl_token *list)
1274 struct ccl_rpn_node *p;
1276 cclp->look_token = list;
1277 p = find_spec(cclp, NULL);
1278 if (p && KIND != CCL_TOK_EOL)
1280 if (KIND == CCL_TOK_RP)
1281 cclp->error_code = CCL_ERR_BAD_RP;
1283 cclp->error_code = CCL_ERR_OP_EXPECTED;
1287 cclp->error_pos = cclp->look_token->name;
1289 cclp->error_code = CCL_ERR_OK;
1291 cclp->error_code = cclp->error_code;
1296 * ccl_find_str: Parse CCL find - string representation
1297 * bibset: Bibset to be used for the parsing
1298 * str: String to be parsed
1299 * error: Pointer to integer. Holds error no. on completion.
1300 * pos: Pointer to char position. Holds approximate error position.
1301 * return: RPN tree on successful completion; NULL otherwise.
1303 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1304 int *error, int *pos)
1306 CCL_parser cclp = ccl_parser_create(bibset);
1307 struct ccl_token *list;
1308 struct ccl_rpn_node *p;
1310 list = ccl_parser_tokenize(cclp, str);
1311 p = ccl_parser_find_token(cclp, list);
1313 *error = cclp->error_code;
1315 *pos = cclp->error_pos - str;
1316 ccl_parser_destroy(cclp);
1317 ccl_token_del(list);
1324 * c-file-style: "Stroustrup"
1325 * indent-tabs-mode: nil
1327 * vim: shiftwidth=4 tabstop=8 expandtab