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);
331 static struct ccl_rpn_node *ccl_term_one_use(CCL_parser cclp,
332 struct ccl_rpn_attr *attr_use,
334 int no, int term_len,
335 const char **truncation_aliases,
336 const char **mask_aliases,
341 struct ccl_rpn_node *p;
343 int relation_value = -1;
344 int position_value = -1;
345 int structure_value = -1;
346 int truncation_value = -1;
347 int completeness_value = -1;
354 struct ccl_token *lookahead = cclp->look_token;
356 p = ccl_rpn_node_create(CCL_RPN_TERM);
357 p->u.t.attr_list = NULL;
361 const char *n = ccl_qual_get_name(qa[0]);
363 p->u.t.qual = xstrdup(n);
365 /* go through all attributes and add them to the attribute list */
366 for (i = 0; qa && qa[i]; i++)
368 struct ccl_rpn_attr *attr;
369 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
370 if (attr->type != 1 || attr == attr_use)
374 case CCL_RPN_ATTR_STRING:
375 ccl_add_attr_string(p, attr->set, attr->type,
378 case CCL_RPN_ATTR_NUMERIC:
379 if (attr->value.numeric > 0)
380 { /* deal only with REAL attributes (positive) */
384 if (relation_value != -1)
386 relation_value = attr->value.numeric;
389 if (position_value != -1)
391 position_value = attr->value.numeric;
394 if (structure_value != -1)
396 structure_value = attr->value.numeric;
399 if (truncation_value != -1)
401 truncation_value = attr->value.numeric;
404 if (completeness_value != -1)
406 completeness_value = attr->value.numeric;
409 ccl_add_attr_numeric(p, attr->set, attr->type,
410 attr->value.numeric);
416 if (structure_value == -1 && (
418 qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
422 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
424 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
426 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_REGEX,
430 regex_trunc = 1; /* regex trunc (102) allowed */
432 else if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_Z3958,
436 z3958_trunc = 1; /* Z39.58 trunc (CCL) trunc allowed */
438 /* make the RPN token */
439 p->u.t.term = (char *)xmalloc(term_len * 2 + 2);
440 ccl_assert(p->u.t.term);
441 p->u.t.term[0] = '\0';
443 for (i = 0; i < no; i++)
445 const char *src_str = lookahead->name;
446 size_t src_len = lookahead->len;
448 if (p->u.t.term[0] && lookahead->ws_prefix_len)
450 strxcat(p->u.t.term, lookahead->ws_prefix_buf,
451 lookahead->ws_prefix_len);
453 if (append_term(cclp, src_str, src_len, p->u.t.term, regex_trunc,
454 z3958_trunc, truncation_aliases, mask_aliases,
456 &left_trunc, &right_trunc))
461 lookahead = lookahead->next;
463 if (left_trunc && right_trunc)
465 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
468 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
472 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
474 else if (right_trunc)
476 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
479 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
483 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
487 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
490 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
494 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
496 else if (regex_trunc)
498 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 102);
500 else if (z3958_trunc)
502 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 104);
506 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
508 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
515 * search_term: Parse CCL search term.
517 * qa: Qualifier attributes already applied.
518 * term_list: tokens we accept as terms in context
519 * multi: whether we accept "multiple" tokens
520 * return: pointer to node(s); NULL on error.
522 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
524 int *term_list, int multi)
526 struct ccl_rpn_node *p_top = 0;
527 struct ccl_token *lookahead = cclp->look_token;
531 const char **truncation_aliases;
532 const char *t_default[2];
533 const char **mask_aliases;
534 const char *m_default[2];
537 ccl_qual_search_special(cclp->bibset, "truncation");
538 if (!truncation_aliases)
540 truncation_aliases = t_default;
546 ccl_qual_search_special(cclp->bibset, "mask");
549 mask_aliases = m_default;
555 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
557 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AUTO_GROUP, 0))
559 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
563 struct ccl_rpn_node *p = 0;
567 int is_ccl_masked = 0;
571 int relation_value = -1;
572 int position_value = -1;
573 int structure_value = -1;
574 int truncation_value = -1;
575 int completeness_value = -1;
582 if (and_list || or_list || !multi)
585 /* ignore commas when dealing with and-lists .. */
586 if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
588 lookahead = lookahead->next;
592 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
594 int this_is_phrase = 0;
595 for (i = 0; i<lookahead->len; i++)
596 if (lookahead->name[i] == ' ')
599 if (has_ccl_masking(lookahead->name, lookahead->len,
606 if (no > 0 && (is_phrase || is_phrase != this_is_phrase))
608 is_phrase = this_is_phrase;
610 else if (this_is_phrase || no > 0)
612 len += 1+lookahead->len+lookahead->ws_prefix_len;
613 lookahead = lookahead->next;
617 break; /* no more terms . stop . */
620 /* go through all attributes and add them to the attribute list */
621 for (i = 0; qa && qa[i]; i++)
623 struct ccl_rpn_attr *attr;
625 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
628 struct ccl_rpn_node *tmp2;
629 tmp2 = ccl_term_one_use(cclp, attr, qa, no, len,
630 truncation_aliases, mask_aliases,
631 is_phrase, is_ccl_masked,
642 struct ccl_rpn_node *tmp1;
643 tmp1 = ccl_rpn_node_create(CCL_RPN_OR);
652 p = ccl_term_one_use(cclp, 0 /* attr: no use */, qa, no, len,
653 truncation_aliases, mask_aliases,
654 is_phrase, is_ccl_masked, auto_group);
658 for (i = 0; i < no; i++)
661 /* create the term node, but wait a moment before adding the term */
662 p = ccl_rpn_node_create(CCL_RPN_TERM);
663 p->u.t.attr_list = NULL;
667 const char *n = ccl_qual_get_name(qa[0]);
669 p->u.t.qual = xstrdup(n);
672 /* go through all attributes and add them to the attribute list */
673 for (i=0; qa && qa[i]; i++)
675 struct ccl_rpn_attr *attr;
677 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
680 case CCL_RPN_ATTR_STRING:
681 ccl_add_attr_string(p, attr->set, attr->type,
684 case CCL_RPN_ATTR_NUMERIC:
685 if (attr->value.numeric > 0)
686 { /* deal only with REAL attributes (positive) */
690 if (relation_value != -1)
692 relation_value = attr->value.numeric;
695 if (position_value != -1)
697 position_value = attr->value.numeric;
700 if (structure_value != -1)
702 structure_value = attr->value.numeric;
705 if (truncation_value != -1)
707 truncation_value = attr->value.numeric;
710 if (completeness_value != -1)
712 completeness_value = attr->value.numeric;
715 ccl_add_attr_numeric(p, attr->set, attr->type,
716 attr->value.numeric);
721 if (structure_value == -1 && (
723 qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
727 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
729 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
732 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_REGEX,
736 regex_trunc = 1; /* regex trunc (102) allowed */
738 else if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_Z3958,
742 z3958_trunc = 1; /* Z39.58 trunc (CCL) trunc allowed */
745 /* make the RPN token */
746 p->u.t.term = (char *)xmalloc(len * 2 + 2);
747 ccl_assert(p->u.t.term);
748 p->u.t.term[0] = '\0';
749 for (i = 0; i<no; i++)
751 const char *src_str = cclp->look_token->name;
752 size_t src_len = cclp->look_token->len;
754 if (p->u.t.term[0] && cclp->look_token->ws_prefix_len)
756 strxcat(p->u.t.term, cclp->look_token->ws_prefix_buf,
757 cclp->look_token->ws_prefix_len);
759 if (append_term(cclp, src_str, src_len, p->u.t.term, regex_trunc,
760 z3958_trunc, truncation_aliases, mask_aliases,
762 &left_trunc, &right_trunc))
769 if (left_trunc && right_trunc)
771 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
774 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
778 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
780 else if (right_trunc)
782 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
785 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
789 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
793 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
796 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
800 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
802 else if (regex_trunc)
804 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 102);
806 else if (z3958_trunc)
808 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 104);
812 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
814 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
817 /* make the top node point to us.. */
820 struct ccl_rpn_node *tmp;
823 tmp = ccl_rpn_node_create(CCL_RPN_OR);
825 tmp = ccl_rpn_node_create(CCL_RPN_AND);
827 tmp = ccl_rpn_node_create(CCL_RPN_AND);
840 cclp->error_code = CCL_ERR_TERM_EXPECTED;
844 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
846 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
847 return search_term_x(cclp, qa, list, 0);
851 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
854 if (KIND == CCL_TOK_LP)
856 struct ccl_rpn_node *p;
858 if (!(p = find_spec(cclp, qa)))
860 if (KIND != CCL_TOK_RP)
862 cclp->error_code = CCL_ERR_RP_EXPECTED;
871 static int list[] = {
872 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
873 CCL_TOK_REL, CCL_TOK_SET, -1};
875 return search_term_x(cclp, qa, list, 1);
882 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
883 ccl_qualifier_t *ap, char *attset)
886 struct ccl_rpn_node *p;
888 if (cclp->look_token->len == 1)
890 if (cclp->look_token->name[0] == '<')
892 else if (cclp->look_token->name[0] == '=')
894 else if (cclp->look_token->name[0] == '>')
897 else if (cclp->look_token->len == 2)
899 if (!memcmp(cclp->look_token->name, "<=", 2))
901 else if (!memcmp(cclp->look_token->name, ">=", 2))
903 else if (!memcmp(cclp->look_token->name, "<>", 2))
908 cclp->error_code = CCL_ERR_BAD_RELATION;
911 ADVANCE; /* skip relation */
913 qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
915 /* allow - inside term and treat it as range _always_ */
916 /* relation is =. Extract "embedded" - to separate terms */
917 if (KIND == CCL_TOK_TERM)
921 for (i = 0; i<cclp->look_token->len; i++)
923 if (i > 0 && cclp->look_token->name[i] == '\\')
925 else if (cclp->look_token->name[i] == '"')
926 quote_mode = !quote_mode;
927 else if (cclp->look_token->name[i] == '-' && !quote_mode)
931 if (cclp->look_token->len > 1 && i == 0)
933 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
935 ntoken->kind = CCL_TOK_TERM;
936 ntoken->name = cclp->look_token->name + 1;
937 ntoken->len = cclp->look_token->len - 1;
939 cclp->look_token->len = 1;
940 cclp->look_token->name = "-";
942 else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
944 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
946 ntoken->kind = CCL_TOK_TERM;
950 (cclp->look_token->len)--;
952 else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
954 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
955 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
957 ntoken1->kind = CCL_TOK_TERM; /* generate - */
961 ntoken2->kind = CCL_TOK_TERM; /* generate yy */
962 ntoken2->name = cclp->look_token->name + (i+1);
963 ntoken2->len = cclp->look_token->len - (i+1);
965 cclp->look_token->len = i; /* adjust xx */
967 else if (i == cclp->look_token->len &&
968 cclp->look_token->next &&
969 cclp->look_token->next->kind == CCL_TOK_TERM &&
970 cclp->look_token->next->len > 1 &&
971 cclp->look_token->next->name[0] == '-')
974 /* we _know_ that xx does not have - in it */
975 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
977 ntoken->kind = CCL_TOK_TERM; /* generate - */
981 (ntoken->next->name)++; /* adjust yy */
982 (ntoken->next->len)--;
988 KIND == CCL_TOK_TERM &&
989 cclp->look_token->next && cclp->look_token->next->len == 1 &&
990 cclp->look_token->next->name[0] == '-')
992 struct ccl_rpn_node *p1;
993 if (!(p1 = search_term(cclp, ap)))
995 ADVANCE; /* skip '-' */
996 if (KIND == CCL_TOK_TERM) /* = term - term ? */
998 struct ccl_rpn_node *p2;
1000 if (!(p2 = search_term(cclp, ap)))
1005 p = ccl_rpn_node_create(CCL_RPN_AND);
1007 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
1009 ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
1014 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
1018 else if (rel == 3 &&
1019 cclp->look_token->len == 1 &&
1020 cclp->look_token->name[0] == '-') /* = - term ? */
1023 if (!(p = search_term(cclp, ap)))
1025 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
1030 if (!(p = search_terms(cclp, ap)))
1033 !qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_OMIT_EQUALS, 0))
1034 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
1041 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
1045 if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
1046 || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
1047 return qualifiers_order(cclp, ap, attset);
1049 /* unordered relation */
1050 if (KIND != CCL_TOK_EQ)
1052 cclp->error_code = CCL_ERR_EQ_EXPECTED;
1056 return search_terms(cclp, ap);
1060 * qualifier_list: Parse CCL qualifiers and search terms.
1062 * la: Token pointer to RELATION token.
1063 * qa: Qualifier attributes already applied.
1064 * return: pointer to node(s); NULL on error.
1066 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
1067 struct ccl_token *la,
1068 ccl_qualifier_t *qa)
1070 struct ccl_token *lookahead = cclp->look_token;
1071 struct ccl_token *look_start = cclp->look_token;
1072 ccl_qualifier_t *ap;
1073 struct ccl_rpn_node *node = 0;
1074 const char **field_str;
1082 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
1086 for (lookahead = cclp->look_token; lookahead != la;
1087 lookahead=lookahead->next)
1090 for (i=0; qa[i]; i++)
1092 ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
1095 field_str = ccl_qual_search_special(cclp->bibset, "field");
1098 if (!strcmp(field_str[0], "or"))
1100 else if (!strcmp(field_str[0], "merge"))
1105 /* consider each field separately and OR */
1106 lookahead = look_start;
1107 while (lookahead != la)
1111 while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
1112 lookahead->len, seq)) != 0)
1114 struct ccl_rpn_node *node_sub;
1115 cclp->look_token = la;
1117 node_sub = qualifier_relation(cclp, ap);
1120 ccl_rpn_delete(node);
1126 struct ccl_rpn_node *node_this =
1127 ccl_rpn_node_create(CCL_RPN_OR);
1128 node_this->u.p[0] = node;
1129 node_this->u.p[1] = node_sub;
1138 cclp->look_token = lookahead;
1139 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1143 lookahead = lookahead->next;
1144 if (lookahead->kind == CCL_TOK_COMMA)
1145 lookahead = lookahead->next;
1150 /* merge attributes from ALL fields - including inherited ones */
1153 struct ccl_rpn_node *node_sub;
1155 lookahead = look_start;
1156 for (i = 0; lookahead != la; i++)
1158 ap[i] = ccl_qual_search(cclp, lookahead->name,
1159 lookahead->len, seq);
1162 if (!ap[i] && seq > 0)
1163 ap[i] = ccl_qual_search(cclp, lookahead->name,
1167 cclp->look_token = lookahead;
1168 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1172 lookahead = lookahead->next;
1173 if (lookahead->kind == CCL_TOK_COMMA)
1174 lookahead = lookahead->next;
1178 ccl_qualifier_t *qa0 = qa;
1188 cclp->look_token = lookahead;
1190 node_sub = qualifier_relation(cclp, ap);
1193 ccl_rpn_delete(node);
1198 struct ccl_rpn_node *node_this =
1199 ccl_rpn_node_create(CCL_RPN_OR);
1200 node_this->u.p[0] = node;
1201 node_this->u.p[1] = node_sub;
1215 * search_terms: Parse CCL search terms - including proximity.
1217 * qa: Qualifier attributes already applied.
1218 * return: pointer to node(s); NULL on error.
1220 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
1222 static int list[] = {
1223 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
1224 CCL_TOK_REL, CCL_TOK_SET, -1};
1225 struct ccl_rpn_node *p1, *p2, *pn;
1226 p1 = search_terms2(cclp, qa);
1231 if (KIND == CCL_TOK_PROX)
1233 struct ccl_rpn_node *p_prox = 0;
1234 /* ! word order specified */
1235 /* % word order not specified */
1236 p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
1237 p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
1238 memcpy(p_prox->u.t.term, cclp->look_token->name,
1239 cclp->look_token->len);
1240 p_prox->u.t.term[cclp->look_token->len] = 0;
1241 p_prox->u.t.attr_list = 0;
1244 p2 = search_terms2(cclp, qa);
1250 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1253 pn->u.p[2] = p_prox;
1256 else if (is_term_ok(KIND, list))
1258 p2 = search_terms2(cclp, qa);
1264 pn = ccl_rpn_node_create(CCL_RPN_PROX);
1277 * search_elements: Parse CCL search elements
1279 * qa: Qualifier attributes already applied.
1280 * return: pointer to node(s); NULL on error.
1282 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1283 ccl_qualifier_t *qa)
1285 struct ccl_rpn_node *p1;
1286 struct ccl_token *lookahead;
1287 if (KIND == CCL_TOK_SET)
1290 if (KIND == CCL_TOK_EQ)
1292 if (KIND != CCL_TOK_TERM)
1294 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1297 p1 = ccl_rpn_node_create(CCL_RPN_SET);
1298 p1->u.setname = copy_token_name(cclp->look_token);
1302 lookahead = cclp->look_token;
1304 while (lookahead->kind==CCL_TOK_TERM)
1306 lookahead = lookahead->next;
1307 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1308 return qualifier_list(cclp, lookahead, qa);
1309 if (lookahead->kind != CCL_TOK_COMMA)
1311 lookahead = lookahead->next;
1313 if (qa || lookahead->kind == CCL_TOK_LP)
1314 return search_terms(cclp, qa);
1317 ccl_qualifier_t qa[2];
1318 struct ccl_rpn_node *node = 0;
1320 lookahead = cclp->look_token;
1323 for(seq = 0; ;seq++)
1325 struct ccl_rpn_node *node_sub;
1326 qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1330 cclp->look_token = lookahead;
1332 node_sub = search_terms(cclp, qa);
1335 ccl_rpn_delete(node);
1340 struct ccl_rpn_node *node_this =
1341 ccl_rpn_node_create(CCL_RPN_OR);
1342 node_this->u.p[0] = node;
1343 node_this->u.p[1] = node_sub;
1344 node_this->u.p[2] = 0;
1351 node = search_terms(cclp, 0);
1357 * find_spec: Parse CCL find specification
1359 * qa: Qualifier attributes already applied.
1360 * return: pointer to node(s); NULL on error.
1362 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1364 struct ccl_rpn_node *p1, *p2, *pn;
1365 if (!(p1 = search_elements(cclp, qa)))
1373 p2 = search_elements(cclp, qa);
1379 pn = ccl_rpn_node_create(CCL_RPN_AND);
1387 p2 = search_elements(cclp, qa);
1393 pn = ccl_rpn_node_create(CCL_RPN_OR);
1401 p2 = search_elements(cclp, qa);
1407 pn = ccl_rpn_node_create(CCL_RPN_NOT);
1419 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1421 struct ccl_rpn_node *p;
1422 struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1423 p = ccl_parser_find_token(cclp, list);
1424 ccl_token_del(list);
1428 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1429 struct ccl_token *list)
1431 struct ccl_rpn_node *p;
1433 cclp->look_token = list;
1434 p = find_spec(cclp, NULL);
1435 if (p && KIND != CCL_TOK_EOL)
1437 if (KIND == CCL_TOK_RP)
1438 cclp->error_code = CCL_ERR_BAD_RP;
1440 cclp->error_code = CCL_ERR_OP_EXPECTED;
1444 cclp->error_pos = cclp->look_token->name;
1446 cclp->error_code = CCL_ERR_OK;
1448 cclp->error_code = cclp->error_code;
1453 * ccl_find_str: Parse CCL find - string representation
1454 * bibset: Bibset to be used for the parsing
1455 * str: String to be parsed
1456 * error: Pointer to integer. Holds error no. on completion.
1457 * pos: Pointer to char position. Holds approximate error position.
1458 * return: RPN tree on successful completion; NULL otherwise.
1460 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1461 int *error, int *pos)
1463 CCL_parser cclp = ccl_parser_create(bibset);
1464 struct ccl_token *list;
1465 struct ccl_rpn_node *p;
1467 list = ccl_parser_tokenize(cclp, str);
1468 p = ccl_parser_find_token(cclp, list);
1470 *error = cclp->error_code;
1472 *pos = cclp->error_pos - str;
1473 ccl_parser_destroy(cclp);
1474 ccl_token_del(list);
1481 * c-file-style: "Stroustrup"
1482 * indent-tabs-mode: nil
1484 * vim: shiftwidth=4 tabstop=8 expandtab