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,
337 struct ccl_rpn_node *p;
339 int relation_value = -1;
340 int position_value = -1;
341 int structure_value = -1;
342 int truncation_value = -1;
343 int completeness_value = -1;
349 int is_ccl_masked = 0;
351 struct ccl_token *lookahead = cclp->look_token;
352 const char **truncation_aliases;
353 const char *t_default[2];
354 const char **mask_aliases;
355 const char *m_default[2];
358 ccl_qual_search_special(cclp->bibset, "truncation");
359 if (!truncation_aliases)
361 truncation_aliases = t_default;
367 ccl_qual_search_special(cclp->bibset, "mask");
370 mask_aliases = m_default;
376 for (i = 0; i < no; i++)
378 if (has_ccl_masking(lookahead->name, lookahead->len,
382 lookahead = lookahead->next;
384 lookahead = cclp->look_token;
386 p = ccl_rpn_node_create(CCL_RPN_TERM);
387 p->u.t.attr_list = NULL;
391 const char *n = ccl_qual_get_name(qa[0]);
393 p->u.t.qual = xstrdup(n);
395 /* go through all attributes and add them to the attribute list */
396 for (i = 0; qa && qa[i]; i++)
398 struct ccl_rpn_attr *attr;
399 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
400 if (attr->type != 1 || attr == attr_use)
404 case CCL_RPN_ATTR_STRING:
405 ccl_add_attr_string(p, attr->set, attr->type,
408 case CCL_RPN_ATTR_NUMERIC:
409 if (attr->value.numeric > 0)
410 { /* deal only with REAL attributes (positive) */
414 if (relation_value != -1)
416 relation_value = attr->value.numeric;
419 if (position_value != -1)
421 position_value = attr->value.numeric;
424 if (structure_value != -1)
426 structure_value = attr->value.numeric;
429 if (truncation_value != -1)
431 truncation_value = attr->value.numeric;
434 if (completeness_value != -1)
436 completeness_value = attr->value.numeric;
439 ccl_add_attr_numeric(p, attr->set, attr->type,
440 attr->value.numeric);
446 if (structure_value == -1 && (
448 qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
452 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
454 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
456 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_REGEX,
460 regex_trunc = 1; /* regex trunc (102) allowed */
462 else if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_Z3958,
466 z3958_trunc = 1; /* Z39.58 trunc (CCL) trunc allowed */
468 /* make the RPN token */
469 p->u.t.term = (char *)xmalloc(term_len * 2 + 2);
470 ccl_assert(p->u.t.term);
471 p->u.t.term[0] = '\0';
473 for (i = 0; i < no; i++)
475 const char *src_str = lookahead->name;
476 size_t src_len = lookahead->len;
478 if (p->u.t.term[0] && lookahead->ws_prefix_len)
480 strxcat(p->u.t.term, lookahead->ws_prefix_buf,
481 lookahead->ws_prefix_len);
483 if (append_term(cclp, src_str, src_len, p->u.t.term, regex_trunc,
484 z3958_trunc, truncation_aliases, mask_aliases,
486 &left_trunc, &right_trunc))
491 lookahead = lookahead->next;
493 if (left_trunc && right_trunc)
495 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
498 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
502 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
504 else if (right_trunc)
506 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
509 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
513 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
517 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
520 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
524 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
526 else if (regex_trunc)
528 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 102);
530 else if (z3958_trunc)
532 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 104);
536 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
538 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
544 * search_term: Parse CCL search term.
546 * qa: Qualifier attributes already applied.
547 * term_list: tokens we accept as terms in context
548 * multi: whether we accept "multiple" tokens
549 * return: pointer to node(s); NULL on error.
551 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
553 int *term_list, int multi)
555 struct ccl_rpn_node *p_top = 0;
556 struct ccl_token *lookahead = cclp->look_token;
561 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
563 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AUTO_GROUP, 0))
565 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
569 struct ccl_rpn_node *p = 0;
574 if (and_list || or_list || !multi)
577 /* ignore commas when dealing with and-lists .. */
578 if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
580 lookahead = lookahead->next;
584 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
586 int this_is_phrase = 0;
587 for (i = 0; i<lookahead->len; i++)
588 if (lookahead->name[i] == ' ')
592 if (no > 0 && (is_phrase || is_phrase != this_is_phrase))
594 is_phrase = this_is_phrase;
596 else if (this_is_phrase || no > 0)
598 len += 1+lookahead->len+lookahead->ws_prefix_len;
599 lookahead = lookahead->next;
603 break; /* no more terms . stop . */
605 /* go through all attributes and add them to the attribute list */
606 for (i = 0; qa && qa[i]; i++)
608 struct ccl_rpn_attr *attr;
610 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
613 struct ccl_rpn_node *tmp2;
614 tmp2 = ccl_term_one_use(cclp, attr, qa, no, len,
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 is_phrase, auto_group);
641 for (i = 0; i < no; i++)
643 /* make the top node point to us.. */
646 struct ccl_rpn_node *tmp;
649 tmp = ccl_rpn_node_create(CCL_RPN_OR);
651 tmp = ccl_rpn_node_create(CCL_RPN_AND);
653 tmp = ccl_rpn_node_create(CCL_RPN_AND);
666 cclp->error_code = CCL_ERR_TERM_EXPECTED;
670 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
672 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
673 return search_term_x(cclp, qa, list, 0);
677 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
680 if (KIND == CCL_TOK_LP)
682 struct ccl_rpn_node *p;
684 if (!(p = find_spec(cclp, qa)))
686 if (KIND != CCL_TOK_RP)
688 cclp->error_code = CCL_ERR_RP_EXPECTED;
697 static int list[] = {
698 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
699 CCL_TOK_REL, CCL_TOK_SET, -1};
701 return search_term_x(cclp, qa, list, 1);
708 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
709 ccl_qualifier_t *ap, char *attset)
712 struct ccl_rpn_node *p;
714 if (cclp->look_token->len == 1)
716 if (cclp->look_token->name[0] == '<')
718 else if (cclp->look_token->name[0] == '=')
720 else if (cclp->look_token->name[0] == '>')
723 else if (cclp->look_token->len == 2)
725 if (!memcmp(cclp->look_token->name, "<=", 2))
727 else if (!memcmp(cclp->look_token->name, ">=", 2))
729 else if (!memcmp(cclp->look_token->name, "<>", 2))
734 cclp->error_code = CCL_ERR_BAD_RELATION;
737 ADVANCE; /* skip relation */
739 qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
741 /* allow - inside term and treat it as range _always_ */
742 /* relation is =. Extract "embedded" - to separate terms */
743 if (KIND == CCL_TOK_TERM)
747 for (i = 0; i<cclp->look_token->len; i++)
749 if (i > 0 && cclp->look_token->name[i] == '\\')
751 else if (cclp->look_token->name[i] == '"')
752 quote_mode = !quote_mode;
753 else if (cclp->look_token->name[i] == '-' && !quote_mode)
757 if (cclp->look_token->len > 1 && i == 0)
759 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
761 ntoken->kind = CCL_TOK_TERM;
762 ntoken->name = cclp->look_token->name + 1;
763 ntoken->len = cclp->look_token->len - 1;
765 cclp->look_token->len = 1;
766 cclp->look_token->name = "-";
768 else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
770 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
772 ntoken->kind = CCL_TOK_TERM;
776 (cclp->look_token->len)--;
778 else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
780 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
781 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
783 ntoken1->kind = CCL_TOK_TERM; /* generate - */
787 ntoken2->kind = CCL_TOK_TERM; /* generate yy */
788 ntoken2->name = cclp->look_token->name + (i+1);
789 ntoken2->len = cclp->look_token->len - (i+1);
791 cclp->look_token->len = i; /* adjust xx */
793 else if (i == cclp->look_token->len &&
794 cclp->look_token->next &&
795 cclp->look_token->next->kind == CCL_TOK_TERM &&
796 cclp->look_token->next->len > 1 &&
797 cclp->look_token->next->name[0] == '-')
800 /* we _know_ that xx does not have - in it */
801 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
803 ntoken->kind = CCL_TOK_TERM; /* generate - */
807 (ntoken->next->name)++; /* adjust yy */
808 (ntoken->next->len)--;
814 KIND == CCL_TOK_TERM &&
815 cclp->look_token->next && cclp->look_token->next->len == 1 &&
816 cclp->look_token->next->name[0] == '-')
818 struct ccl_rpn_node *p1;
819 if (!(p1 = search_term(cclp, ap)))
821 ADVANCE; /* skip '-' */
822 if (KIND == CCL_TOK_TERM) /* = term - term ? */
824 struct ccl_rpn_node *p2;
826 if (!(p2 = search_term(cclp, ap)))
831 p = ccl_rpn_node_create(CCL_RPN_AND);
833 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
835 ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
840 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
845 cclp->look_token->len == 1 &&
846 cclp->look_token->name[0] == '-') /* = - term ? */
849 if (!(p = search_term(cclp, ap)))
851 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
856 if (!(p = search_terms(cclp, ap)))
859 !qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_OMIT_EQUALS, 0))
860 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
867 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
871 if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
872 || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
873 return qualifiers_order(cclp, ap, attset);
875 /* unordered relation */
876 if (KIND != CCL_TOK_EQ)
878 cclp->error_code = CCL_ERR_EQ_EXPECTED;
882 return search_terms(cclp, ap);
886 * qualifier_list: Parse CCL qualifiers and search terms.
888 * la: Token pointer to RELATION token.
889 * qa: Qualifier attributes already applied.
890 * return: pointer to node(s); NULL on error.
892 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
893 struct ccl_token *la,
896 struct ccl_token *lookahead = cclp->look_token;
897 struct ccl_token *look_start = cclp->look_token;
899 struct ccl_rpn_node *node = 0;
900 const char **field_str;
908 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
912 for (lookahead = cclp->look_token; lookahead != la;
913 lookahead=lookahead->next)
916 for (i=0; qa[i]; i++)
918 ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
921 field_str = ccl_qual_search_special(cclp->bibset, "field");
924 if (!strcmp(field_str[0], "or"))
926 else if (!strcmp(field_str[0], "merge"))
931 /* consider each field separately and OR */
932 lookahead = look_start;
933 while (lookahead != la)
937 while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
938 lookahead->len, seq)) != 0)
940 struct ccl_rpn_node *node_sub;
941 cclp->look_token = la;
943 node_sub = qualifier_relation(cclp, ap);
946 ccl_rpn_delete(node);
952 struct ccl_rpn_node *node_this =
953 ccl_rpn_node_create(CCL_RPN_OR);
954 node_this->u.p[0] = node;
955 node_this->u.p[1] = node_sub;
964 cclp->look_token = lookahead;
965 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
969 lookahead = lookahead->next;
970 if (lookahead->kind == CCL_TOK_COMMA)
971 lookahead = lookahead->next;
976 /* merge attributes from ALL fields - including inherited ones */
979 struct ccl_rpn_node *node_sub;
981 lookahead = look_start;
982 for (i = 0; lookahead != la; i++)
984 ap[i] = ccl_qual_search(cclp, lookahead->name,
985 lookahead->len, seq);
988 if (!ap[i] && seq > 0)
989 ap[i] = ccl_qual_search(cclp, lookahead->name,
993 cclp->look_token = lookahead;
994 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
998 lookahead = lookahead->next;
999 if (lookahead->kind == CCL_TOK_COMMA)
1000 lookahead = lookahead->next;
1004 ccl_qualifier_t *qa0 = qa;
1014 cclp->look_token = lookahead;
1016 node_sub = qualifier_relation(cclp, ap);
1019 ccl_rpn_delete(node);
1024 struct ccl_rpn_node *node_this =
1025 ccl_rpn_node_create(CCL_RPN_OR);
1026 node_this->u.p[0] = node;
1027 node_this->u.p[1] = node_sub;
1041 * search_terms: Parse CCL search terms - including proximity.
1043 * qa: Qualifier attributes already applied.
1044 * return: pointer to node(s); NULL on error.
1046 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
1048 static int list[] = {
1049 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
1050 CCL_TOK_REL, CCL_TOK_SET, -1};
1051 struct ccl_rpn_node *p1, *p2, *pn;
1052 p1 = search_terms2(cclp, qa);
1057 if (KIND == CCL_TOK_PROX)
1059 struct ccl_rpn_node *p_prox = 0;
1060 /* ! word order specified */
1061 /* % word order not specified */
1062 p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
1063 p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
1064 memcpy(p_prox->u.t.term, cclp->look_token->name,
1065 cclp->look_token->len);
1066 p_prox->u.t.term[cclp->look_token->len] = 0;
1067 p_prox->u.t.attr_list = 0;
1070 p2 = search_terms2(cclp, qa);
1076 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1079 pn->u.p[2] = p_prox;
1082 else if (is_term_ok(KIND, list))
1084 p2 = search_terms2(cclp, qa);
1090 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1103 * search_elements: Parse CCL search elements
1105 * qa: Qualifier attributes already applied.
1106 * return: pointer to node(s); NULL on error.
1108 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1109 ccl_qualifier_t *qa)
1111 struct ccl_rpn_node *p1;
1112 struct ccl_token *lookahead;
1113 if (KIND == CCL_TOK_SET)
1116 if (KIND == CCL_TOK_EQ)
1118 if (KIND != CCL_TOK_TERM)
1120 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1123 p1 = ccl_rpn_node_create(CCL_RPN_SET);
1124 p1->u.setname = copy_token_name(cclp->look_token);
1128 lookahead = cclp->look_token;
1130 while (lookahead->kind==CCL_TOK_TERM)
1132 lookahead = lookahead->next;
1133 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1134 return qualifier_list(cclp, lookahead, qa);
1135 if (lookahead->kind != CCL_TOK_COMMA)
1137 lookahead = lookahead->next;
1139 if (qa || lookahead->kind == CCL_TOK_LP)
1140 return search_terms(cclp, qa);
1143 ccl_qualifier_t qa[2];
1144 struct ccl_rpn_node *node = 0;
1146 lookahead = cclp->look_token;
1149 for(seq = 0; ;seq++)
1151 struct ccl_rpn_node *node_sub;
1152 qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1156 cclp->look_token = lookahead;
1158 node_sub = search_terms(cclp, qa);
1161 ccl_rpn_delete(node);
1166 struct ccl_rpn_node *node_this =
1167 ccl_rpn_node_create(CCL_RPN_OR);
1168 node_this->u.p[0] = node;
1169 node_this->u.p[1] = node_sub;
1170 node_this->u.p[2] = 0;
1177 node = search_terms(cclp, 0);
1183 * find_spec: Parse CCL find specification
1185 * qa: Qualifier attributes already applied.
1186 * return: pointer to node(s); NULL on error.
1188 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1190 struct ccl_rpn_node *p1, *p2, *pn;
1191 if (!(p1 = search_elements(cclp, qa)))
1199 p2 = search_elements(cclp, qa);
1205 pn = ccl_rpn_node_create(CCL_RPN_AND);
1213 p2 = search_elements(cclp, qa);
1219 pn = ccl_rpn_node_create(CCL_RPN_OR);
1227 p2 = search_elements(cclp, qa);
1233 pn = ccl_rpn_node_create(CCL_RPN_NOT);
1245 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1247 struct ccl_rpn_node *p;
1248 struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1249 p = ccl_parser_find_token(cclp, list);
1250 ccl_token_del(list);
1254 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1255 struct ccl_token *list)
1257 struct ccl_rpn_node *p;
1259 cclp->look_token = list;
1260 p = find_spec(cclp, NULL);
1261 if (p && KIND != CCL_TOK_EOL)
1263 if (KIND == CCL_TOK_RP)
1264 cclp->error_code = CCL_ERR_BAD_RP;
1266 cclp->error_code = CCL_ERR_OP_EXPECTED;
1270 cclp->error_pos = cclp->look_token->name;
1272 cclp->error_code = CCL_ERR_OK;
1274 cclp->error_code = cclp->error_code;
1279 * ccl_find_str: Parse CCL find - string representation
1280 * bibset: Bibset to be used for the parsing
1281 * str: String to be parsed
1282 * error: Pointer to integer. Holds error no. on completion.
1283 * pos: Pointer to char position. Holds approximate error position.
1284 * return: RPN tree on successful completion; NULL otherwise.
1286 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1287 int *error, int *pos)
1289 CCL_parser cclp = ccl_parser_create(bibset);
1290 struct ccl_token *list;
1291 struct ccl_rpn_node *p;
1293 list = ccl_parser_tokenize(cclp, str);
1294 p = ccl_parser_find_token(cclp, list);
1296 *error = cclp->error_code;
1298 *pos = cclp->error_pos - str;
1299 ccl_parser_destroy(cclp);
1300 ccl_token_del(list);
1307 * c-file-style: "Stroustrup"
1308 * indent-tabs-mode: nil
1310 * vim: shiftwidth=4 tabstop=8 expandtab