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 = (struct ccl_rpn_attr *) xmalloc(sizeof(*n));
176 n->set = xstrdup(set);
180 n->next = p->u.t.attr_list;
181 p->u.t.attr_list = n;
186 * add_attr_numeric: Add attribute (type/value) to RPN term node.
187 * p: RPN node of type term.
188 * type: Type of attribute
189 * value: Value of attribute
190 * set: Attribute set name
192 void ccl_add_attr_numeric(struct ccl_rpn_node *p, const char *set,
195 struct ccl_rpn_attr *n = add_attr_node(p, set, type);
196 n->kind = CCL_RPN_ATTR_NUMERIC;
197 n->value.numeric = value;
200 void ccl_add_attr_string(struct ccl_rpn_node *p, const char *set,
201 int type, char *value)
203 struct ccl_rpn_attr *n = add_attr_node(p, set, type);
204 n->kind = CCL_RPN_ATTR_STRING;
205 n->value.str = xstrdup(value);
208 static size_t cmp_operator(const char **aliases, const char *input)
210 for (; *aliases; aliases++)
212 const char *cp = *aliases;
214 for (i = 0; *cp && *cp == input[i]; i++, cp++)
223 #define REGEX_CHARS "^[]{}()|.*+?!$"
224 #define CCL_CHARS "#?\\"
226 static int has_ccl_masking(const char *src_str,
228 const char **truncation_aliases,
229 const char **mask_aliases)
234 for (j = 0; j < src_len; j++)
237 if (j > 0 && src_str[j-1] == '\\')
239 else if (src_str[j] == '"')
240 quote_mode = !quote_mode;
241 else if (!quote_mode &&
242 (op_size = cmp_operator(truncation_aliases,
245 else if (!quote_mode &&
246 (op_size = cmp_operator(mask_aliases,
253 static int append_term(CCL_parser cclp, const char *src_str, size_t src_len,
254 char *dst_term, int regex_trunc, int z3958_trunc,
255 const char **truncation_aliases,
256 const char **mask_aliases,
257 int is_first, int is_last,
258 int *left_trunc, int *right_trunc)
263 for (j = 0; j < src_len; j++)
266 if (j > 0 && src_str[j-1] == '\\')
268 if (regex_trunc && strchr(REGEX_CHARS "\\", src_str[j]))
269 strcat(dst_term, "\\");
270 else if (z3958_trunc && strchr(CCL_CHARS "\\", src_str[j]))
271 strcat(dst_term, "\\");
272 strxcat(dst_term, src_str + j, 1);
274 else if (src_str[j] == '"')
275 quote_mode = !quote_mode;
276 else if (!quote_mode &&
277 (op_size = cmp_operator(truncation_aliases,
281 j += (op_size - 1); /* j++ in for loop */
283 strcat(dst_term, ".*");
284 else if (z3958_trunc)
285 strcat(dst_term, "?");
286 else if (is_first && j == 0)
288 else if (is_last && j == src_len - 1)
292 cclp->error_code = CCL_ERR_TRUNC_NOT_EMBED;
296 else if (!quote_mode &&
297 (op_size = cmp_operator(mask_aliases, src_str + j)))
299 j += (op_size - 1); /* j++ in for loop */
301 strcat(dst_term, ".");
302 else if (z3958_trunc)
303 strcat(dst_term, "#");
306 cclp->error_code = CCL_ERR_TRUNC_NOT_SINGLE;
310 else if (src_str[j] != '\\')
312 if (regex_trunc && strchr(REGEX_CHARS, src_str[j]))
313 strcat(dst_term, "\\");
314 else if (z3958_trunc && strchr(CCL_CHARS, src_str[j]))
315 strcat(dst_term, "\\");
316 strxcat(dst_term, src_str + j, 1);
323 static struct ccl_rpn_node *ccl_term_one_use(CCL_parser cclp,
324 struct ccl_rpn_attr *attr_use,
326 size_t no, int term_len,
330 struct ccl_rpn_node *p;
332 int relation_value = -1;
333 int position_value = -1;
334 int structure_value = -1;
335 int truncation_value = -1;
336 int completeness_value = -1;
342 int is_ccl_masked = 0;
344 struct ccl_token *lookahead = cclp->look_token;
345 const char **truncation_aliases;
346 const char *t_default[2];
347 const char **mask_aliases;
348 const char *m_default[2];
351 ccl_qual_search_special(cclp->bibset, "truncation");
352 if (!truncation_aliases)
354 truncation_aliases = t_default;
359 ccl_qual_search_special(cclp->bibset, "mask");
362 mask_aliases = m_default;
366 for (i = 0; i < no; i++)
368 if (has_ccl_masking(lookahead->name, lookahead->len,
372 lookahead = lookahead->next;
374 lookahead = cclp->look_token;
376 p = ccl_rpn_node_create(CCL_RPN_TERM);
377 p->u.t.attr_list = NULL;
381 const char *n = ccl_qual_get_name(qa[0]);
383 p->u.t.qual = xstrdup(n);
385 /* go through all attributes and add them to the attribute list */
386 for (i = 0; qa && qa[i]; i++)
388 struct ccl_rpn_attr *attr;
389 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
390 if (attr->type != 1 || attr == attr_use)
394 case CCL_RPN_ATTR_STRING:
395 ccl_add_attr_string(p, attr->set, attr->type,
398 case CCL_RPN_ATTR_NUMERIC:
399 if (attr->value.numeric > 0)
400 { /* deal only with REAL attributes (positive) */
404 if (relation_value != -1)
406 relation_value = attr->value.numeric;
409 if (position_value != -1)
411 position_value = attr->value.numeric;
414 if (structure_value != -1)
416 structure_value = attr->value.numeric;
419 if (truncation_value != -1)
421 truncation_value = attr->value.numeric;
424 if (completeness_value != -1)
426 completeness_value = attr->value.numeric;
429 ccl_add_attr_numeric(p, attr->set, attr->type,
430 attr->value.numeric);
436 if (structure_value == -1 && (
438 qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
442 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
444 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
446 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_REGEX,
450 regex_trunc = 1; /* regex trunc (102) allowed */
452 else if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_Z3958,
456 z3958_trunc = 1; /* Z39.58 trunc (CCL) trunc allowed */
458 /* make the RPN token */
459 p->u.t.term = (char *)xmalloc(term_len * 2 + 2);
460 ccl_assert(p->u.t.term);
461 p->u.t.term[0] = '\0';
463 for (i = 0; i < no; i++)
465 const char *src_str = lookahead->name;
466 size_t src_len = lookahead->len;
468 if (p->u.t.term[0] && lookahead->ws_prefix_len)
470 strxcat(p->u.t.term, lookahead->ws_prefix_buf,
471 lookahead->ws_prefix_len);
473 if (append_term(cclp, src_str, src_len, p->u.t.term, regex_trunc,
474 z3958_trunc, truncation_aliases, mask_aliases,
476 &left_trunc, &right_trunc))
481 lookahead = lookahead->next;
483 if (left_trunc && right_trunc)
485 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
488 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
492 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
494 else if (right_trunc)
496 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
499 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
503 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
507 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
510 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
514 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
516 else if (regex_trunc)
518 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 102);
520 else if (z3958_trunc)
522 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 104);
526 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
528 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
534 * search_term: Parse CCL search term.
536 * qa: Qualifier attributes already applied.
537 * term_list: tokens we accept as terms in context
538 * multi: whether we accept "multiple" tokens
539 * return: pointer to node(s); NULL on error.
541 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
543 int *term_list, int multi)
545 struct ccl_rpn_node *p_top = 0;
546 struct ccl_token *lookahead = cclp->look_token;
551 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
553 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AUTO_GROUP, 0))
555 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
559 struct ccl_rpn_node *p = 0;
564 if (and_list || or_list || !multi)
567 /* ignore commas when dealing with and-lists .. */
568 if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
570 lookahead = lookahead->next;
574 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
576 int this_is_phrase = 0;
577 for (i = 0; i<lookahead->len; i++)
578 if (lookahead->name[i] == ' ')
582 if (no > 0 && (is_phrase || is_phrase != this_is_phrase))
584 is_phrase = this_is_phrase;
586 else if (this_is_phrase || no > 0)
588 len += 1+lookahead->len+lookahead->ws_prefix_len;
589 lookahead = lookahead->next;
593 break; /* no more terms . stop . */
595 /* go through all attributes and add them to the attribute list */
596 for (i = 0; qa && qa[i]; i++)
598 struct ccl_rpn_attr *attr;
600 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
603 struct ccl_rpn_node *tmp2;
604 tmp2 = ccl_term_one_use(cclp, attr, qa, no, len,
605 is_phrase, auto_group);
615 struct ccl_rpn_node *tmp1;
616 tmp1 = ccl_rpn_node_create(CCL_RPN_OR);
624 p = ccl_term_one_use(cclp, 0 /* attr: no use */, qa, no, len,
625 is_phrase, auto_group);
626 for (i = 0; i < no; i++)
630 /* make the top node point to us.. */
633 struct ccl_rpn_node *tmp;
636 tmp = ccl_rpn_node_create(CCL_RPN_OR);
638 tmp = ccl_rpn_node_create(CCL_RPN_AND);
640 tmp = ccl_rpn_node_create(CCL_RPN_AND);
653 cclp->error_code = CCL_ERR_TERM_EXPECTED;
657 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
659 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
660 return search_term_x(cclp, qa, list, 0);
664 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
667 if (KIND == CCL_TOK_LP)
669 struct ccl_rpn_node *p;
671 if (!(p = find_spec(cclp, qa)))
673 if (KIND != CCL_TOK_RP)
675 cclp->error_code = CCL_ERR_RP_EXPECTED;
684 static int list[] = {
685 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
686 CCL_TOK_REL, CCL_TOK_SET, -1};
688 return search_term_x(cclp, qa, list, 1);
694 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
695 ccl_qualifier_t *ap, char *attset)
698 struct ccl_rpn_node *p;
700 if (cclp->look_token->len == 1)
702 if (cclp->look_token->name[0] == '<')
704 else if (cclp->look_token->name[0] == '=')
706 else if (cclp->look_token->name[0] == '>')
709 else if (cclp->look_token->len == 2)
711 if (!memcmp(cclp->look_token->name, "<=", 2))
713 else if (!memcmp(cclp->look_token->name, ">=", 2))
715 else if (!memcmp(cclp->look_token->name, "<>", 2))
720 cclp->error_code = CCL_ERR_BAD_RELATION;
723 ADVANCE; /* skip relation */
725 qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
727 /* allow - inside term and treat it as range _always_ */
728 /* relation is =. Extract "embedded" - to separate terms */
729 if (KIND == CCL_TOK_TERM)
733 for (i = 0; i<cclp->look_token->len; i++)
735 if (i > 0 && cclp->look_token->name[i] == '\\')
737 else if (cclp->look_token->name[i] == '"')
738 quote_mode = !quote_mode;
739 else if (cclp->look_token->name[i] == '-' && !quote_mode)
743 if (cclp->look_token->len > 1 && i == 0)
745 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
747 ntoken->kind = CCL_TOK_TERM;
748 ntoken->name = cclp->look_token->name + 1;
749 ntoken->len = cclp->look_token->len - 1;
751 cclp->look_token->len = 1;
752 cclp->look_token->name = "-";
754 else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
756 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
758 ntoken->kind = CCL_TOK_TERM;
762 (cclp->look_token->len)--;
764 else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
766 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
767 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
769 ntoken1->kind = CCL_TOK_TERM; /* generate - */
773 ntoken2->kind = CCL_TOK_TERM; /* generate yy */
774 ntoken2->name = cclp->look_token->name + (i+1);
775 ntoken2->len = cclp->look_token->len - (i+1);
777 cclp->look_token->len = i; /* adjust xx */
779 else if (i == cclp->look_token->len &&
780 cclp->look_token->next &&
781 cclp->look_token->next->kind == CCL_TOK_TERM &&
782 cclp->look_token->next->len > 1 &&
783 cclp->look_token->next->name[0] == '-')
786 /* we _know_ that xx does not have - in it */
787 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
789 ntoken->kind = CCL_TOK_TERM; /* generate - */
793 (ntoken->next->name)++; /* adjust yy */
794 (ntoken->next->len)--;
800 KIND == CCL_TOK_TERM &&
801 cclp->look_token->next && cclp->look_token->next->len == 1 &&
802 cclp->look_token->next->name[0] == '-')
804 struct ccl_rpn_node *p1;
805 if (!(p1 = search_term(cclp, ap)))
807 ADVANCE; /* skip '-' */
808 if (KIND == CCL_TOK_TERM) /* = term - term ? */
810 struct ccl_rpn_node *p2;
812 if (!(p2 = search_term(cclp, ap)))
817 p = ccl_rpn_node_create(CCL_RPN_AND);
819 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
821 ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
826 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
831 cclp->look_token->len == 1 &&
832 cclp->look_token->name[0] == '-') /* = - term ? */
835 if (!(p = search_term(cclp, ap)))
837 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
842 if (!(p = search_terms(cclp, ap)))
845 !qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_OMIT_EQUALS, 0))
846 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
853 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
857 if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
858 || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
859 return qualifiers_order(cclp, ap, attset);
861 /* unordered relation */
862 if (KIND != CCL_TOK_EQ)
864 cclp->error_code = CCL_ERR_EQ_EXPECTED;
868 return search_terms(cclp, ap);
872 * qualifier_list: Parse CCL qualifiers and search terms.
874 * la: Token pointer to RELATION token.
875 * qa: Qualifier attributes already applied.
876 * return: pointer to node(s); NULL on error.
878 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
879 struct ccl_token *la,
882 struct ccl_token *lookahead = cclp->look_token;
883 struct ccl_token *look_start = cclp->look_token;
885 struct ccl_rpn_node *node = 0;
886 const char **field_str;
894 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
898 for (lookahead = cclp->look_token; lookahead != la;
899 lookahead=lookahead->next)
902 for (i=0; qa[i]; i++)
904 ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
907 field_str = ccl_qual_search_special(cclp->bibset, "field");
910 if (!strcmp(field_str[0], "or"))
912 else if (!strcmp(field_str[0], "merge"))
917 /* consider each field separately and OR */
918 lookahead = look_start;
919 while (lookahead != la)
923 while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
924 lookahead->len, seq)) != 0)
926 struct ccl_rpn_node *node_sub;
927 cclp->look_token = la;
929 node_sub = qualifier_relation(cclp, ap);
932 ccl_rpn_delete(node);
938 struct ccl_rpn_node *node_this =
939 ccl_rpn_node_create(CCL_RPN_OR);
940 node_this->u.p[0] = node;
941 node_this->u.p[1] = node_sub;
950 cclp->look_token = lookahead;
951 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
955 lookahead = lookahead->next;
956 if (lookahead->kind == CCL_TOK_COMMA)
957 lookahead = lookahead->next;
962 /* merge attributes from ALL fields - including inherited ones */
965 struct ccl_rpn_node *node_sub;
967 lookahead = look_start;
968 for (i = 0; lookahead != la; i++)
970 ap[i] = ccl_qual_search(cclp, lookahead->name,
971 lookahead->len, seq);
974 if (!ap[i] && seq > 0)
975 ap[i] = ccl_qual_search(cclp, lookahead->name,
979 cclp->look_token = lookahead;
980 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
984 lookahead = lookahead->next;
985 if (lookahead->kind == CCL_TOK_COMMA)
986 lookahead = lookahead->next;
990 ccl_qualifier_t *qa0 = qa;
1000 cclp->look_token = lookahead;
1002 node_sub = qualifier_relation(cclp, ap);
1005 ccl_rpn_delete(node);
1010 struct ccl_rpn_node *node_this =
1011 ccl_rpn_node_create(CCL_RPN_OR);
1012 node_this->u.p[0] = node;
1013 node_this->u.p[1] = node_sub;
1027 * search_terms: Parse CCL search terms - including proximity.
1029 * qa: Qualifier attributes already applied.
1030 * return: pointer to node(s); NULL on error.
1032 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
1034 static int list[] = {
1035 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
1036 CCL_TOK_REL, CCL_TOK_SET, -1};
1037 struct ccl_rpn_node *p1, *p2, *pn;
1038 p1 = search_terms2(cclp, qa);
1043 if (KIND == CCL_TOK_PROX)
1045 struct ccl_rpn_node *p_prox = 0;
1046 /* ! word order specified */
1047 /* % word order not specified */
1048 p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
1049 p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
1050 memcpy(p_prox->u.t.term, cclp->look_token->name,
1051 cclp->look_token->len);
1052 p_prox->u.t.term[cclp->look_token->len] = 0;
1053 p_prox->u.t.attr_list = 0;
1056 p2 = search_terms2(cclp, qa);
1062 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1065 pn->u.p[2] = p_prox;
1068 else if (is_term_ok(KIND, list))
1070 p2 = search_terms2(cclp, qa);
1076 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1089 * search_elements: Parse CCL search elements
1091 * qa: Qualifier attributes already applied.
1092 * return: pointer to node(s); NULL on error.
1094 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1095 ccl_qualifier_t *qa)
1097 struct ccl_rpn_node *p1;
1098 struct ccl_token *lookahead;
1099 if (KIND == CCL_TOK_SET)
1102 if (KIND == CCL_TOK_EQ)
1104 if (KIND != CCL_TOK_TERM)
1106 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1109 p1 = ccl_rpn_node_create(CCL_RPN_SET);
1110 p1->u.setname = copy_token_name(cclp->look_token);
1114 lookahead = cclp->look_token;
1116 while (lookahead->kind==CCL_TOK_TERM)
1118 lookahead = lookahead->next;
1119 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1120 return qualifier_list(cclp, lookahead, qa);
1121 if (lookahead->kind != CCL_TOK_COMMA)
1123 lookahead = lookahead->next;
1125 if (qa || lookahead->kind == CCL_TOK_LP)
1126 return search_terms(cclp, qa);
1129 ccl_qualifier_t qa[2];
1130 struct ccl_rpn_node *node = 0;
1132 lookahead = cclp->look_token;
1135 for(seq = 0; ;seq++)
1137 struct ccl_rpn_node *node_sub;
1138 qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1142 cclp->look_token = lookahead;
1144 node_sub = search_terms(cclp, qa);
1147 ccl_rpn_delete(node);
1152 struct ccl_rpn_node *node_this =
1153 ccl_rpn_node_create(CCL_RPN_OR);
1154 node_this->u.p[0] = node;
1155 node_this->u.p[1] = node_sub;
1156 node_this->u.p[2] = 0;
1163 node = search_terms(cclp, 0);
1169 * find_spec: Parse CCL find specification
1171 * qa: Qualifier attributes already applied.
1172 * return: pointer to node(s); NULL on error.
1174 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1176 struct ccl_rpn_node *p1, *p2, *pn;
1177 if (!(p1 = search_elements(cclp, qa)))
1185 p2 = search_elements(cclp, qa);
1191 pn = ccl_rpn_node_create(CCL_RPN_AND);
1199 p2 = search_elements(cclp, qa);
1205 pn = ccl_rpn_node_create(CCL_RPN_OR);
1213 p2 = search_elements(cclp, qa);
1219 pn = ccl_rpn_node_create(CCL_RPN_NOT);
1231 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1233 struct ccl_rpn_node *p;
1234 struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1235 p = ccl_parser_find_token(cclp, list);
1236 ccl_token_del(list);
1240 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1241 struct ccl_token *list)
1243 struct ccl_rpn_node *p;
1245 cclp->look_token = list;
1246 p = find_spec(cclp, NULL);
1247 if (p && KIND != CCL_TOK_EOL)
1249 if (KIND == CCL_TOK_RP)
1250 cclp->error_code = CCL_ERR_BAD_RP;
1252 cclp->error_code = CCL_ERR_OP_EXPECTED;
1256 cclp->error_pos = cclp->look_token->name;
1258 cclp->error_code = CCL_ERR_OK;
1260 cclp->error_code = cclp->error_code;
1265 * ccl_find_str: Parse CCL find - string representation
1266 * bibset: Bibset to be used for the parsing
1267 * str: String to be parsed
1268 * error: Pointer to integer. Holds error no. on completion.
1269 * pos: Pointer to char position. Holds approximate error position.
1270 * return: RPN tree on successful completion; NULL otherwise.
1272 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1273 int *error, int *pos)
1275 CCL_parser cclp = ccl_parser_create(bibset);
1276 struct ccl_token *list;
1277 struct ccl_rpn_node *p;
1279 list = ccl_parser_tokenize(cclp, str);
1280 p = ccl_parser_find_token(cclp, list);
1282 *error = cclp->error_code;
1284 *pos = cclp->error_pos - str;
1285 ccl_parser_destroy(cclp);
1286 ccl_token_del(list);
1293 * c-file-style: "Stroustrup"
1294 * indent-tabs-mode: nil
1296 * vim: shiftwidth=4 tabstop=8 expandtab