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;
117 * ccl_rpn_delete: Delete RPN tree.
118 * rpn: Pointer to tree.
120 void ccl_rpn_delete(struct ccl_rpn_node *rpn)
122 struct ccl_rpn_attr *attr, *attr1;
130 ccl_rpn_delete(rpn->u.p[0]);
131 ccl_rpn_delete(rpn->u.p[1]);
134 xfree(rpn->u.t.term);
135 xfree(rpn->u.t.qual);
136 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
139 if (attr->kind == CCL_RPN_ATTR_STRING)
140 xfree(attr->value.str);
147 xfree(rpn->u.setname);
150 ccl_rpn_delete(rpn->u.p[0]);
151 ccl_rpn_delete(rpn->u.p[1]);
152 ccl_rpn_delete(rpn->u.p[2]);
158 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa);
160 static int is_term_ok(int look, int *list)
162 for (;*list >= 0; list++)
168 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa);
170 static struct ccl_rpn_attr *add_attr_node(struct ccl_rpn_node *p,
171 const char *set, int type)
173 struct ccl_rpn_attr *n;
175 n = (struct ccl_rpn_attr *)xmalloc(sizeof(*n));
178 n->set = xstrdup(set);
182 n->next = p->u.t.attr_list;
183 p->u.t.attr_list = n;
189 * add_attr_numeric: Add attribute (type/value) to RPN term node.
190 * p: RPN node of type term.
191 * type: Type of attribute
192 * value: Value of attribute
193 * set: Attribute set name
195 void ccl_add_attr_numeric(struct ccl_rpn_node *p, const char *set,
198 struct ccl_rpn_attr *n;
200 n = add_attr_node(p, set, type);
201 n->kind = CCL_RPN_ATTR_NUMERIC;
202 n->value.numeric = value;
205 void ccl_add_attr_string(struct ccl_rpn_node *p, const char *set,
206 int type, char *value)
208 struct ccl_rpn_attr *n;
210 n = add_attr_node(p, set, type);
211 n->kind = CCL_RPN_ATTR_STRING;
212 n->value.str = xstrdup(value);
215 static size_t cmp_operator(const char **aliases, const char *input)
217 for (; *aliases; aliases++)
219 const char *cp = *aliases;
221 for (i = 0; *cp && *cp == input[i]; i++, cp++)
230 #define REGEX_CHARS "^[]{}()|.*+?!$"
231 #define CCL_CHARS "#?\\"
233 static int has_ccl_masking(const char *src_str,
235 const char **truncation_aliases,
236 const char **mask_aliases)
241 for (j = 0; j < src_len; j++)
244 if (j > 0 && src_str[j-1] == '\\')
246 else if (src_str[j] == '"')
247 quote_mode = !quote_mode;
248 else if (!quote_mode &&
249 (op_size = cmp_operator(truncation_aliases,
252 else if (!quote_mode &&
253 (op_size = cmp_operator(mask_aliases,
260 static int append_term(CCL_parser cclp, const char *src_str, size_t src_len,
261 char *dst_term, int regex_trunc, int z3958_trunc,
262 const char **truncation_aliases,
263 const char **mask_aliases,
264 int is_first, int is_last,
265 int *left_trunc, int *right_trunc)
270 for (j = 0; j < src_len; j++)
273 if (j > 0 && src_str[j-1] == '\\')
275 if (regex_trunc && strchr(REGEX_CHARS "\\", src_str[j]))
276 strcat(dst_term, "\\");
277 else if (z3958_trunc && strchr(CCL_CHARS "\\", src_str[j]))
278 strcat(dst_term, "\\");
279 strxcat(dst_term, src_str + j, 1);
281 else if (src_str[j] == '"')
282 quote_mode = !quote_mode;
283 else if (!quote_mode &&
284 (op_size = cmp_operator(truncation_aliases,
288 j += (op_size - 1); /* j++ in for loop */
290 strcat(dst_term, ".*");
291 else if (z3958_trunc)
292 strcat(dst_term, "?");
293 else if (is_first && j == 0)
295 else if (is_last && j == src_len - 1)
299 cclp->error_code = CCL_ERR_TRUNC_NOT_EMBED;
303 else if (!quote_mode &&
304 (op_size = cmp_operator(mask_aliases, src_str + j)))
306 j += (op_size - 1); /* j++ in for loop */
308 strcat(dst_term, ".");
309 else if (z3958_trunc)
310 strcat(dst_term, "#");
313 cclp->error_code = CCL_ERR_TRUNC_NOT_SINGLE;
317 else if (src_str[j] != '\\')
319 if (regex_trunc && strchr(REGEX_CHARS, src_str[j]))
320 strcat(dst_term, "\\");
321 else if (z3958_trunc && strchr(CCL_CHARS, src_str[j]))
322 strcat(dst_term, "\\");
323 strxcat(dst_term, src_str + j, 1);
330 static struct ccl_rpn_node *ccl_term_one_use(CCL_parser cclp,
331 struct ccl_rpn_attr *attr_use,
333 size_t no, int term_len,
334 const char **truncation_aliases,
335 const char **mask_aliases,
340 struct ccl_rpn_node *p;
342 int relation_value = -1;
343 int position_value = -1;
344 int structure_value = -1;
345 int truncation_value = -1;
346 int completeness_value = -1;
353 struct ccl_token *lookahead = cclp->look_token;
355 p = ccl_rpn_node_create(CCL_RPN_TERM);
356 p->u.t.attr_list = NULL;
360 const char *n = ccl_qual_get_name(qa[0]);
362 p->u.t.qual = xstrdup(n);
364 /* go through all attributes and add them to the attribute list */
365 for (i = 0; qa && qa[i]; i++)
367 struct ccl_rpn_attr *attr;
368 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
369 if (attr->type != 1 || attr == attr_use)
373 case CCL_RPN_ATTR_STRING:
374 ccl_add_attr_string(p, attr->set, attr->type,
377 case CCL_RPN_ATTR_NUMERIC:
378 if (attr->value.numeric > 0)
379 { /* deal only with REAL attributes (positive) */
383 if (relation_value != -1)
385 relation_value = attr->value.numeric;
388 if (position_value != -1)
390 position_value = attr->value.numeric;
393 if (structure_value != -1)
395 structure_value = attr->value.numeric;
398 if (truncation_value != -1)
400 truncation_value = attr->value.numeric;
403 if (completeness_value != -1)
405 completeness_value = attr->value.numeric;
408 ccl_add_attr_numeric(p, attr->set, attr->type,
409 attr->value.numeric);
415 if (structure_value == -1 && (
417 qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
421 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
423 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
425 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_REGEX,
429 regex_trunc = 1; /* regex trunc (102) allowed */
431 else if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_Z3958,
435 z3958_trunc = 1; /* Z39.58 trunc (CCL) trunc allowed */
437 /* make the RPN token */
438 p->u.t.term = (char *)xmalloc(term_len * 2 + 2);
439 ccl_assert(p->u.t.term);
440 p->u.t.term[0] = '\0';
442 for (i = 0; i < no; i++)
444 const char *src_str = lookahead->name;
445 size_t src_len = lookahead->len;
447 if (p->u.t.term[0] && lookahead->ws_prefix_len)
449 strxcat(p->u.t.term, lookahead->ws_prefix_buf,
450 lookahead->ws_prefix_len);
452 if (append_term(cclp, src_str, src_len, p->u.t.term, regex_trunc,
453 z3958_trunc, truncation_aliases, mask_aliases,
455 &left_trunc, &right_trunc))
460 lookahead = lookahead->next;
462 if (left_trunc && right_trunc)
464 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
467 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
471 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
473 else if (right_trunc)
475 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
478 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
482 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
486 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
489 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
493 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
495 else if (regex_trunc)
497 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 102);
499 else if (z3958_trunc)
501 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 104);
505 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
507 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
513 * search_term: Parse CCL search term.
515 * qa: Qualifier attributes already applied.
516 * term_list: tokens we accept as terms in context
517 * multi: whether we accept "multiple" tokens
518 * return: pointer to node(s); NULL on error.
520 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
522 int *term_list, int multi)
524 struct ccl_rpn_node *p_top = 0;
525 struct ccl_token *lookahead = cclp->look_token;
529 const char **truncation_aliases;
530 const char *t_default[2];
531 const char **mask_aliases;
532 const char *m_default[2];
535 ccl_qual_search_special(cclp->bibset, "truncation");
536 if (!truncation_aliases)
538 truncation_aliases = t_default;
544 ccl_qual_search_special(cclp->bibset, "mask");
547 mask_aliases = m_default;
553 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
555 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AUTO_GROUP, 0))
557 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
561 struct ccl_rpn_node *p = 0;
565 int is_ccl_masked = 0;
567 if (and_list || or_list || !multi)
570 /* ignore commas when dealing with and-lists .. */
571 if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
573 lookahead = lookahead->next;
577 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
579 int this_is_phrase = 0;
580 for (i = 0; i<lookahead->len; i++)
581 if (lookahead->name[i] == ' ')
584 if (has_ccl_masking(lookahead->name, lookahead->len,
591 if (no > 0 && (is_phrase || is_phrase != this_is_phrase))
593 is_phrase = this_is_phrase;
595 else if (this_is_phrase || no > 0)
597 len += 1+lookahead->len+lookahead->ws_prefix_len;
598 lookahead = lookahead->next;
602 break; /* no more terms . stop . */
604 /* go through all attributes and add them to the attribute list */
605 for (i = 0; qa && qa[i]; i++)
607 struct ccl_rpn_attr *attr;
609 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
612 struct ccl_rpn_node *tmp2;
613 tmp2 = ccl_term_one_use(cclp, attr, qa, no, len,
614 truncation_aliases, mask_aliases,
615 is_phrase, is_ccl_masked,
626 struct ccl_rpn_node *tmp1;
627 tmp1 = ccl_rpn_node_create(CCL_RPN_OR);
636 p = ccl_term_one_use(cclp, 0 /* attr: no use */, qa, no, len,
637 truncation_aliases, mask_aliases,
638 is_phrase, is_ccl_masked, auto_group);
642 for (i = 0; i < no; i++)
644 /* make the top node point to us.. */
647 struct ccl_rpn_node *tmp;
650 tmp = ccl_rpn_node_create(CCL_RPN_OR);
652 tmp = ccl_rpn_node_create(CCL_RPN_AND);
654 tmp = ccl_rpn_node_create(CCL_RPN_AND);
667 cclp->error_code = CCL_ERR_TERM_EXPECTED;
671 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
673 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
674 return search_term_x(cclp, qa, list, 0);
678 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
681 if (KIND == CCL_TOK_LP)
683 struct ccl_rpn_node *p;
685 if (!(p = find_spec(cclp, qa)))
687 if (KIND != CCL_TOK_RP)
689 cclp->error_code = CCL_ERR_RP_EXPECTED;
698 static int list[] = {
699 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
700 CCL_TOK_REL, CCL_TOK_SET, -1};
702 return search_term_x(cclp, qa, list, 1);
709 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
710 ccl_qualifier_t *ap, char *attset)
713 struct ccl_rpn_node *p;
715 if (cclp->look_token->len == 1)
717 if (cclp->look_token->name[0] == '<')
719 else if (cclp->look_token->name[0] == '=')
721 else if (cclp->look_token->name[0] == '>')
724 else if (cclp->look_token->len == 2)
726 if (!memcmp(cclp->look_token->name, "<=", 2))
728 else if (!memcmp(cclp->look_token->name, ">=", 2))
730 else if (!memcmp(cclp->look_token->name, "<>", 2))
735 cclp->error_code = CCL_ERR_BAD_RELATION;
738 ADVANCE; /* skip relation */
740 qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
742 /* allow - inside term and treat it as range _always_ */
743 /* relation is =. Extract "embedded" - to separate terms */
744 if (KIND == CCL_TOK_TERM)
748 for (i = 0; i<cclp->look_token->len; i++)
750 if (i > 0 && cclp->look_token->name[i] == '\\')
752 else if (cclp->look_token->name[i] == '"')
753 quote_mode = !quote_mode;
754 else if (cclp->look_token->name[i] == '-' && !quote_mode)
758 if (cclp->look_token->len > 1 && i == 0)
760 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
762 ntoken->kind = CCL_TOK_TERM;
763 ntoken->name = cclp->look_token->name + 1;
764 ntoken->len = cclp->look_token->len - 1;
766 cclp->look_token->len = 1;
767 cclp->look_token->name = "-";
769 else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
771 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
773 ntoken->kind = CCL_TOK_TERM;
777 (cclp->look_token->len)--;
779 else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
781 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
782 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
784 ntoken1->kind = CCL_TOK_TERM; /* generate - */
788 ntoken2->kind = CCL_TOK_TERM; /* generate yy */
789 ntoken2->name = cclp->look_token->name + (i+1);
790 ntoken2->len = cclp->look_token->len - (i+1);
792 cclp->look_token->len = i; /* adjust xx */
794 else if (i == cclp->look_token->len &&
795 cclp->look_token->next &&
796 cclp->look_token->next->kind == CCL_TOK_TERM &&
797 cclp->look_token->next->len > 1 &&
798 cclp->look_token->next->name[0] == '-')
801 /* we _know_ that xx does not have - in it */
802 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
804 ntoken->kind = CCL_TOK_TERM; /* generate - */
808 (ntoken->next->name)++; /* adjust yy */
809 (ntoken->next->len)--;
815 KIND == CCL_TOK_TERM &&
816 cclp->look_token->next && cclp->look_token->next->len == 1 &&
817 cclp->look_token->next->name[0] == '-')
819 struct ccl_rpn_node *p1;
820 if (!(p1 = search_term(cclp, ap)))
822 ADVANCE; /* skip '-' */
823 if (KIND == CCL_TOK_TERM) /* = term - term ? */
825 struct ccl_rpn_node *p2;
827 if (!(p2 = search_term(cclp, ap)))
832 p = ccl_rpn_node_create(CCL_RPN_AND);
834 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
836 ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
841 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
846 cclp->look_token->len == 1 &&
847 cclp->look_token->name[0] == '-') /* = - term ? */
850 if (!(p = search_term(cclp, ap)))
852 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
857 if (!(p = search_terms(cclp, ap)))
860 !qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_OMIT_EQUALS, 0))
861 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
868 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
872 if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
873 || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
874 return qualifiers_order(cclp, ap, attset);
876 /* unordered relation */
877 if (KIND != CCL_TOK_EQ)
879 cclp->error_code = CCL_ERR_EQ_EXPECTED;
883 return search_terms(cclp, ap);
887 * qualifier_list: Parse CCL qualifiers and search terms.
889 * la: Token pointer to RELATION token.
890 * qa: Qualifier attributes already applied.
891 * return: pointer to node(s); NULL on error.
893 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
894 struct ccl_token *la,
897 struct ccl_token *lookahead = cclp->look_token;
898 struct ccl_token *look_start = cclp->look_token;
900 struct ccl_rpn_node *node = 0;
901 const char **field_str;
909 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
913 for (lookahead = cclp->look_token; lookahead != la;
914 lookahead=lookahead->next)
917 for (i=0; qa[i]; i++)
919 ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
922 field_str = ccl_qual_search_special(cclp->bibset, "field");
925 if (!strcmp(field_str[0], "or"))
927 else if (!strcmp(field_str[0], "merge"))
932 /* consider each field separately and OR */
933 lookahead = look_start;
934 while (lookahead != la)
938 while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
939 lookahead->len, seq)) != 0)
941 struct ccl_rpn_node *node_sub;
942 cclp->look_token = la;
944 node_sub = qualifier_relation(cclp, ap);
947 ccl_rpn_delete(node);
953 struct ccl_rpn_node *node_this =
954 ccl_rpn_node_create(CCL_RPN_OR);
955 node_this->u.p[0] = node;
956 node_this->u.p[1] = node_sub;
965 cclp->look_token = lookahead;
966 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
970 lookahead = lookahead->next;
971 if (lookahead->kind == CCL_TOK_COMMA)
972 lookahead = lookahead->next;
977 /* merge attributes from ALL fields - including inherited ones */
980 struct ccl_rpn_node *node_sub;
982 lookahead = look_start;
983 for (i = 0; lookahead != la; i++)
985 ap[i] = ccl_qual_search(cclp, lookahead->name,
986 lookahead->len, seq);
989 if (!ap[i] && seq > 0)
990 ap[i] = ccl_qual_search(cclp, lookahead->name,
994 cclp->look_token = lookahead;
995 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
999 lookahead = lookahead->next;
1000 if (lookahead->kind == CCL_TOK_COMMA)
1001 lookahead = lookahead->next;
1005 ccl_qualifier_t *qa0 = qa;
1015 cclp->look_token = lookahead;
1017 node_sub = qualifier_relation(cclp, ap);
1020 ccl_rpn_delete(node);
1025 struct ccl_rpn_node *node_this =
1026 ccl_rpn_node_create(CCL_RPN_OR);
1027 node_this->u.p[0] = node;
1028 node_this->u.p[1] = node_sub;
1042 * search_terms: Parse CCL search terms - including proximity.
1044 * qa: Qualifier attributes already applied.
1045 * return: pointer to node(s); NULL on error.
1047 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
1049 static int list[] = {
1050 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
1051 CCL_TOK_REL, CCL_TOK_SET, -1};
1052 struct ccl_rpn_node *p1, *p2, *pn;
1053 p1 = search_terms2(cclp, qa);
1058 if (KIND == CCL_TOK_PROX)
1060 struct ccl_rpn_node *p_prox = 0;
1061 /* ! word order specified */
1062 /* % word order not specified */
1063 p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
1064 p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
1065 memcpy(p_prox->u.t.term, cclp->look_token->name,
1066 cclp->look_token->len);
1067 p_prox->u.t.term[cclp->look_token->len] = 0;
1068 p_prox->u.t.attr_list = 0;
1071 p2 = search_terms2(cclp, qa);
1077 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1080 pn->u.p[2] = p_prox;
1083 else if (is_term_ok(KIND, list))
1085 p2 = search_terms2(cclp, qa);
1091 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1104 * search_elements: Parse CCL search elements
1106 * qa: Qualifier attributes already applied.
1107 * return: pointer to node(s); NULL on error.
1109 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1110 ccl_qualifier_t *qa)
1112 struct ccl_rpn_node *p1;
1113 struct ccl_token *lookahead;
1114 if (KIND == CCL_TOK_SET)
1117 if (KIND == CCL_TOK_EQ)
1119 if (KIND != CCL_TOK_TERM)
1121 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1124 p1 = ccl_rpn_node_create(CCL_RPN_SET);
1125 p1->u.setname = copy_token_name(cclp->look_token);
1129 lookahead = cclp->look_token;
1131 while (lookahead->kind==CCL_TOK_TERM)
1133 lookahead = lookahead->next;
1134 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1135 return qualifier_list(cclp, lookahead, qa);
1136 if (lookahead->kind != CCL_TOK_COMMA)
1138 lookahead = lookahead->next;
1140 if (qa || lookahead->kind == CCL_TOK_LP)
1141 return search_terms(cclp, qa);
1144 ccl_qualifier_t qa[2];
1145 struct ccl_rpn_node *node = 0;
1147 lookahead = cclp->look_token;
1150 for(seq = 0; ;seq++)
1152 struct ccl_rpn_node *node_sub;
1153 qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1157 cclp->look_token = lookahead;
1159 node_sub = search_terms(cclp, qa);
1162 ccl_rpn_delete(node);
1167 struct ccl_rpn_node *node_this =
1168 ccl_rpn_node_create(CCL_RPN_OR);
1169 node_this->u.p[0] = node;
1170 node_this->u.p[1] = node_sub;
1171 node_this->u.p[2] = 0;
1178 node = search_terms(cclp, 0);
1184 * find_spec: Parse CCL find specification
1186 * qa: Qualifier attributes already applied.
1187 * return: pointer to node(s); NULL on error.
1189 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1191 struct ccl_rpn_node *p1, *p2, *pn;
1192 if (!(p1 = search_elements(cclp, qa)))
1200 p2 = search_elements(cclp, qa);
1206 pn = ccl_rpn_node_create(CCL_RPN_AND);
1214 p2 = search_elements(cclp, qa);
1220 pn = ccl_rpn_node_create(CCL_RPN_OR);
1228 p2 = search_elements(cclp, qa);
1234 pn = ccl_rpn_node_create(CCL_RPN_NOT);
1246 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1248 struct ccl_rpn_node *p;
1249 struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1250 p = ccl_parser_find_token(cclp, list);
1251 ccl_token_del(list);
1255 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1256 struct ccl_token *list)
1258 struct ccl_rpn_node *p;
1260 cclp->look_token = list;
1261 p = find_spec(cclp, NULL);
1262 if (p && KIND != CCL_TOK_EOL)
1264 if (KIND == CCL_TOK_RP)
1265 cclp->error_code = CCL_ERR_BAD_RP;
1267 cclp->error_code = CCL_ERR_OP_EXPECTED;
1271 cclp->error_pos = cclp->look_token->name;
1273 cclp->error_code = CCL_ERR_OK;
1275 cclp->error_code = cclp->error_code;
1280 * ccl_find_str: Parse CCL find - string representation
1281 * bibset: Bibset to be used for the parsing
1282 * str: String to be parsed
1283 * error: Pointer to integer. Holds error no. on completion.
1284 * pos: Pointer to char position. Holds approximate error position.
1285 * return: RPN tree on successful completion; NULL otherwise.
1287 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1288 int *error, int *pos)
1290 CCL_parser cclp = ccl_parser_create(bibset);
1291 struct ccl_token *list;
1292 struct ccl_rpn_node *p;
1294 list = ccl_parser_tokenize(cclp, str);
1295 p = ccl_parser_find_token(cclp, list);
1297 *error = cclp->error_code;
1299 *pos = cclp->error_pos - str;
1300 ccl_parser_destroy(cclp);
1301 ccl_token_del(list);
1308 * c-file-style: "Stroustrup"
1309 * indent-tabs-mode: nil
1311 * vim: shiftwidth=4 tabstop=8 expandtab