1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 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.
20 /* returns type of current lookahead */
21 #define KIND (cclp->look_token->kind)
23 /* move one token forward */
24 #define ADVANCE cclp->look_token = cclp->look_token->next
27 * qual_val_type: test for existance of attribute type/value pair.
29 * type: Type of attribute to search for
30 * value: Value of attribute to seach for
31 * return: 1 if found; 0 otherwise.
33 static int qual_val_type(ccl_qualifier_t *qa, int type, int value,
40 for (i = 0; qa[i]; i++)
42 struct ccl_rpn_attr *q = ccl_qual_get_attr(qa[i]);
45 if (q->type == type && q->kind == CCL_RPN_ATTR_NUMERIC &&
46 q->value.numeric == value)
59 * strxcat: concatenate strings.
60 * n: Null-terminated Destination string
61 * src: Source string to be appended (not null-terminated)
62 * len: Length of source string.
64 static void strxcat(char *n, const char *src, int len)
74 * copy_token_name: Return copy of CCL token name
75 * tp: Pointer to token info.
76 * return: malloc(3) allocated copy of token name.
78 static char *copy_token_name(struct ccl_token *tp)
80 char *str = (char *)xmalloc(tp->len + 1);
82 memcpy(str, tp->name, tp->len);
88 * mk_node: Create RPN node.
90 * return: pointer to allocated node.
92 struct ccl_rpn_node *ccl_rpn_node_create(enum ccl_rpn_kind kind)
94 struct ccl_rpn_node *p;
95 p = (struct ccl_rpn_node *)xmalloc(sizeof(*p));
102 p->u.t.attr_list = 0;
113 * ccl_rpn_delete: Delete RPN tree.
114 * rpn: Pointer to tree.
116 void ccl_rpn_delete(struct ccl_rpn_node *rpn)
118 struct ccl_rpn_attr *attr, *attr1;
126 ccl_rpn_delete(rpn->u.p[0]);
127 ccl_rpn_delete(rpn->u.p[1]);
130 xfree(rpn->u.t.term);
131 xfree(rpn->u.t.qual);
132 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
135 if (attr->kind == CCL_RPN_ATTR_STRING)
136 xfree(attr->value.str);
143 xfree(rpn->u.setname);
146 ccl_rpn_delete(rpn->u.p[0]);
147 ccl_rpn_delete(rpn->u.p[1]);
148 ccl_rpn_delete(rpn->u.p[2]);
154 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa);
156 static int is_term_ok(int look, int *list)
158 for (;*list >= 0; list++)
164 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa);
166 static struct ccl_rpn_attr *add_attr_node(struct ccl_rpn_node *p,
167 const char *set, int type)
169 struct ccl_rpn_attr *n;
171 n = (struct ccl_rpn_attr *)xmalloc(sizeof(*n));
174 n->set = xstrdup(set);
178 n->next = p->u.t.attr_list;
179 p->u.t.attr_list = n;
185 * add_attr_numeric: Add attribute (type/value) to RPN term node.
186 * p: RPN node of type term.
187 * type: Type of attribute
188 * value: Value of attribute
189 * set: Attribute set name
191 void ccl_add_attr_numeric(struct ccl_rpn_node *p, const char *set,
194 struct ccl_rpn_attr *n;
196 n = add_attr_node(p, set, type);
197 n->kind = CCL_RPN_ATTR_NUMERIC;
198 n->value.numeric = value;
201 void ccl_add_attr_string(struct ccl_rpn_node *p, const char *set,
202 int type, char *value)
204 struct ccl_rpn_attr *n;
206 n = add_attr_node(p, set, type);
207 n->kind = CCL_RPN_ATTR_STRING;
208 n->value.str = xstrdup(value);
213 * search_term: Parse CCL search term.
215 * qa: Qualifier attributes already applied.
216 * term_list: tokens we accept as terms in context
217 * multi: whether we accept "multiple" tokens
218 * return: pointer to node(s); NULL on error.
220 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
222 int *term_list, int multi)
224 struct ccl_rpn_node *p_top = 0;
225 struct ccl_token *lookahead = cclp->look_token;
229 const char **truncation_aliases;
230 const char *t_default[2];
233 ccl_qual_search_special(cclp->bibset, "truncation");
234 if (!truncation_aliases)
236 truncation_aliases = t_default;
241 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
243 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
247 struct ccl_rpn_node *p;
250 int relation_value = -1;
251 int position_value = -1;
252 int structure_value = -1;
253 int truncation_value = -1;
254 int completeness_value = -1;
259 if (and_list || or_list || !multi)
262 /* ignore commas when dealing with and-lists .. */
263 if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
265 lookahead = lookahead->next;
269 /* go through each TERM token. If no truncation attribute is yet
270 met, then look for left/right truncation markers (?) and
271 set left_trunc/right_trunc/mid_trunc accordingly */
272 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
274 for (i = 0; i<lookahead->len; i++)
275 if (lookahead->name[i] == ' ')
277 len += 1+lookahead->len+lookahead->ws_prefix_len;
278 left_trunc = lookahead->left_trunc;
279 right_trunc = lookahead->right_trunc;
280 lookahead = lookahead->next;
284 break; /* no more terms . stop . */
286 /* create the term node, but wait a moment before adding the term */
287 p = ccl_rpn_node_create(CCL_RPN_TERM);
288 p->u.t.attr_list = NULL;
292 const char *n = ccl_qual_get_name(qa[0]);
294 p->u.t.qual = xstrdup(n);
297 /* go through all attributes and add them to the attribute list */
298 for (i=0; qa && qa[i]; i++)
300 struct ccl_rpn_attr *attr;
302 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
305 case CCL_RPN_ATTR_STRING:
306 ccl_add_attr_string(p, attr->set, attr->type,
309 case CCL_RPN_ATTR_NUMERIC:
310 if (attr->value.numeric > 0)
311 { /* deal only with REAL attributes (positive) */
315 if (relation_value != -1)
317 relation_value = attr->value.numeric;
320 if (position_value != -1)
322 position_value = attr->value.numeric;
325 if (structure_value != -1)
327 structure_value = attr->value.numeric;
330 if (truncation_value != -1)
332 truncation_value = attr->value.numeric;
335 if (completeness_value != -1)
337 completeness_value = attr->value.numeric;
340 ccl_add_attr_numeric(p, attr->set, attr->type,
341 attr->value.numeric);
345 /* len now holds the number of characters in the RPN term */
346 /* no holds the number of CCL tokens (1 or more) */
348 if (structure_value == -1 &&
349 qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
350 { /* no structure attribute met. Apply either structure attribute
351 WORD or PHRASE depending on number of CCL tokens */
352 if (no == 1 && no_spaces == 0)
353 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
355 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
358 /* make the RPN token */
359 p->u.t.term = (char *)xmalloc(len);
360 ccl_assert(p->u.t.term);
361 p->u.t.term[0] = '\0';
362 for (i = 0; i<no; i++)
364 const char *src_str = cclp->look_token->name;
365 size_t src_len = cclp->look_token->len;
367 if (p->u.t.term[0] && cclp->look_token->ws_prefix_len)
369 size_t len = strlen(p->u.t.term);
370 memcpy(p->u.t.term + len, cclp->look_token->ws_prefix_buf,
371 cclp->look_token->ws_prefix_len);
372 p->u.t.term[len + cclp->look_token->ws_prefix_len] = '\0';
374 strxcat(p->u.t.term, src_str, src_len);
378 /* make the top node point to us.. */
381 struct ccl_rpn_node *tmp;
384 tmp = ccl_rpn_node_create(CCL_RPN_OR);
386 tmp = ccl_rpn_node_create(CCL_RPN_AND);
388 tmp = ccl_rpn_node_create(CCL_RPN_AND);
398 if (left_trunc && right_trunc)
400 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
403 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
407 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
409 else if (right_trunc)
411 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
414 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
418 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
422 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
425 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
429 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
433 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
435 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
441 cclp->error_code = CCL_ERR_TERM_EXPECTED;
445 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
447 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
448 return search_term_x(cclp, qa, list, 0);
452 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
453 ccl_qualifier_t *ap, char *attset)
456 struct ccl_rpn_node *p;
458 if (cclp->look_token->len == 1)
460 if (cclp->look_token->name[0] == '<')
462 else if (cclp->look_token->name[0] == '=')
464 else if (cclp->look_token->name[0] == '>')
467 else if (cclp->look_token->len == 2)
469 if (!memcmp(cclp->look_token->name, "<=", 2))
471 else if (!memcmp(cclp->look_token->name, ">=", 2))
473 else if (!memcmp(cclp->look_token->name, "<>", 2))
478 cclp->error_code = CCL_ERR_BAD_RELATION;
481 ADVANCE; /* skip relation */
483 qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
485 /* allow - inside term and treat it as range _always_ */
486 /* relation is =. Extract "embedded" - to separate terms */
487 if (KIND == CCL_TOK_TERM)
490 for (i = 0; i<cclp->look_token->len; i++)
492 if (cclp->look_token->name[i] == '-')
496 if (cclp->look_token->len > 1 && i == 0)
498 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
500 ntoken->kind = CCL_TOK_TERM;
501 ntoken->name = cclp->look_token->name + 1;
502 ntoken->len = cclp->look_token->len - 1;
504 cclp->look_token->len = 1;
505 cclp->look_token->name = "-";
507 else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
509 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
511 ntoken->kind = CCL_TOK_TERM;
515 (cclp->look_token->len)--;
517 else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
519 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
520 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
522 ntoken1->kind = CCL_TOK_TERM; /* generate - */
526 ntoken2->kind = CCL_TOK_TERM; /* generate yy */
527 ntoken2->name = cclp->look_token->name + (i+1);
528 ntoken2->len = cclp->look_token->len - (i+1);
530 cclp->look_token->len = i; /* adjust xx */
532 else if (i == cclp->look_token->len &&
533 cclp->look_token->next &&
534 cclp->look_token->next->kind == CCL_TOK_TERM &&
535 cclp->look_token->next->len > 1 &&
536 cclp->look_token->next->name[0] == '-')
539 /* we _know_ that xx does not have - in it */
540 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
542 ntoken->kind = CCL_TOK_TERM; /* generate - */
546 (ntoken->next->name)++; /* adjust yy */
547 (ntoken->next->len)--;
553 KIND == CCL_TOK_TERM &&
554 cclp->look_token->next && cclp->look_token->next->len == 1 &&
555 cclp->look_token->next->name[0] == '-')
557 struct ccl_rpn_node *p1;
558 if (!(p1 = search_term(cclp, ap)))
560 ADVANCE; /* skip '-' */
561 if (KIND == CCL_TOK_TERM) /* = term - term ? */
563 struct ccl_rpn_node *p2;
565 if (!(p2 = search_term(cclp, ap)))
570 p = ccl_rpn_node_create(CCL_RPN_AND);
572 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
574 ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
579 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
584 cclp->look_token->len == 1 &&
585 cclp->look_token->name[0] == '-') /* = - term ? */
588 if (!(p = search_term(cclp, ap)))
590 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
593 else if (KIND == CCL_TOK_LP)
596 if (!(p = find_spec(cclp, ap)))
598 if (KIND != CCL_TOK_RP)
600 cclp->error_code = CCL_ERR_RP_EXPECTED;
609 if (!(p = search_terms(cclp, ap)))
611 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
614 cclp->error_code = CCL_ERR_TERM_EXPECTED;
619 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
622 struct ccl_rpn_node *p;
624 if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
625 || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
626 return qualifiers_order(cclp, ap, attset);
628 /* unordered relation */
629 if (KIND != CCL_TOK_EQ)
631 cclp->error_code = CCL_ERR_EQ_EXPECTED;
635 if (KIND == CCL_TOK_LP)
638 if (!(p = find_spec(cclp, ap)))
642 if (KIND != CCL_TOK_RP)
644 cclp->error_code = CCL_ERR_RP_EXPECTED;
651 p = search_terms(cclp, ap);
656 * qualifier_list: Parse CCL qualifiers and search terms.
658 * la: Token pointer to RELATION token.
659 * qa: Qualifier attributes already applied.
660 * return: pointer to node(s); NULL on error.
662 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
663 struct ccl_token *la,
666 struct ccl_token *lookahead = cclp->look_token;
667 struct ccl_token *look_start = cclp->look_token;
669 struct ccl_rpn_node *node = 0;
670 const char **field_str;
678 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
682 for (lookahead = cclp->look_token; lookahead != la;
683 lookahead=lookahead->next)
686 for (i=0; qa[i]; i++)
688 ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
691 field_str = ccl_qual_search_special(cclp->bibset, "field");
694 if (!strcmp(field_str[0], "or"))
696 else if (!strcmp(field_str[0], "merge"))
701 /* consider each field separately and OR */
702 lookahead = look_start;
703 while (lookahead != la)
707 while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
708 lookahead->len, seq)) != 0)
710 struct ccl_rpn_node *node_sub;
711 cclp->look_token = la;
713 node_sub = qualifier_relation(cclp, ap);
716 ccl_rpn_delete(node);
722 struct ccl_rpn_node *node_this =
723 ccl_rpn_node_create(CCL_RPN_OR);
724 node_this->u.p[0] = node;
725 node_this->u.p[1] = node_sub;
734 cclp->look_token = lookahead;
735 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
739 lookahead = lookahead->next;
740 if (lookahead->kind == CCL_TOK_COMMA)
741 lookahead = lookahead->next;
746 /* merge attributes from ALL fields - including inherited ones */
749 struct ccl_rpn_node *node_sub;
751 lookahead = look_start;
752 for (i = 0; lookahead != la; i++)
754 ap[i] = ccl_qual_search(cclp, lookahead->name,
755 lookahead->len, seq);
758 if (!ap[i] && seq > 0)
759 ap[i] = ccl_qual_search(cclp, lookahead->name,
763 cclp->look_token = lookahead;
764 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
768 lookahead = lookahead->next;
769 if (lookahead->kind == CCL_TOK_COMMA)
770 lookahead = lookahead->next;
774 ccl_qualifier_t *qa0 = qa;
784 cclp->look_token = lookahead;
786 node_sub = qualifier_relation(cclp, ap);
789 ccl_rpn_delete(node);
794 struct ccl_rpn_node *node_this =
795 ccl_rpn_node_create(CCL_RPN_OR);
796 node_this->u.p[0] = node;
797 node_this->u.p[1] = node_sub;
811 * search_terms: Parse CCL search terms - including proximity.
813 * qa: Qualifier attributes already applied.
814 * return: pointer to node(s); NULL on error.
816 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
818 static int list[] = {
819 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ, CCL_TOK_REL, CCL_TOK_SET, -1};
820 struct ccl_rpn_node *p1, *p2, *pn;
821 p1 = search_term_x(cclp, qa, list, 1);
826 if (KIND == CCL_TOK_PROX)
828 struct ccl_rpn_node *p_prox = 0;
829 /* ! word order specified */
830 /* % word order not specified */
831 p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
832 p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
833 memcpy(p_prox->u.t.term, cclp->look_token->name,
834 cclp->look_token->len);
835 p_prox->u.t.term[cclp->look_token->len] = 0;
836 p_prox->u.t.attr_list = 0;
839 p2 = search_term_x(cclp, qa, list, 1);
845 pn = ccl_rpn_node_create(CCL_RPN_PROX);
851 else if (is_term_ok(KIND, list))
853 p2 = search_term_x(cclp, qa, list, 1);
859 pn = ccl_rpn_node_create(CCL_RPN_PROX);
872 * search_elements: Parse CCL search elements
874 * qa: Qualifier attributes already applied.
875 * return: pointer to node(s); NULL on error.
877 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
880 struct ccl_rpn_node *p1;
881 struct ccl_token *lookahead;
882 if (KIND == CCL_TOK_LP)
885 p1 = find_spec(cclp, qa);
888 if (KIND != CCL_TOK_RP)
890 cclp->error_code = CCL_ERR_RP_EXPECTED;
897 else if (KIND == CCL_TOK_SET)
900 if (KIND == CCL_TOK_EQ)
902 if (KIND != CCL_TOK_TERM)
904 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
907 p1 = ccl_rpn_node_create(CCL_RPN_SET);
908 p1->u.setname = copy_token_name(cclp->look_token);
912 lookahead = cclp->look_token;
914 while (lookahead->kind==CCL_TOK_TERM)
916 lookahead = lookahead->next;
917 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
918 return qualifier_list(cclp, lookahead, qa);
919 if (lookahead->kind != CCL_TOK_COMMA)
921 lookahead = lookahead->next;
924 return search_terms(cclp, qa);
927 ccl_qualifier_t qa[2];
928 struct ccl_rpn_node *node = 0;
930 lookahead = cclp->look_token;
935 struct ccl_rpn_node *node_sub;
936 qa[0] = ccl_qual_search(cclp, "term", 4, seq);
940 cclp->look_token = lookahead;
942 node_sub = search_terms(cclp, qa);
945 ccl_rpn_delete(node);
950 struct ccl_rpn_node *node_this =
951 ccl_rpn_node_create(CCL_RPN_OR);
952 node_this->u.p[0] = node;
953 node_this->u.p[1] = node_sub;
954 node_this->u.p[2] = 0;
961 node = search_terms(cclp, 0);
967 * find_spec: Parse CCL find specification
969 * qa: Qualifier attributes already applied.
970 * return: pointer to node(s); NULL on error.
972 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
974 struct ccl_rpn_node *p1, *p2, *pn;
975 if (!(p1 = search_elements(cclp, qa)))
983 p2 = search_elements(cclp, qa);
989 pn = ccl_rpn_node_create(CCL_RPN_AND);
997 p2 = search_elements(cclp, qa);
1003 pn = ccl_rpn_node_create(CCL_RPN_OR);
1011 p2 = search_elements(cclp, qa);
1017 pn = ccl_rpn_node_create(CCL_RPN_NOT);
1029 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1031 struct ccl_rpn_node *p;
1032 struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1033 p = ccl_parser_find_token(cclp, list);
1034 ccl_token_del(list);
1038 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1039 struct ccl_token *list)
1041 struct ccl_rpn_node *p;
1043 cclp->look_token = list;
1044 p = find_spec(cclp, NULL);
1045 if (p && KIND != CCL_TOK_EOL)
1047 if (KIND == CCL_TOK_RP)
1048 cclp->error_code = CCL_ERR_BAD_RP;
1050 cclp->error_code = CCL_ERR_OP_EXPECTED;
1054 cclp->error_pos = cclp->look_token->name;
1056 cclp->error_code = CCL_ERR_OK;
1058 cclp->error_code = cclp->error_code;
1063 * ccl_find_str: Parse CCL find - string representation
1064 * bibset: Bibset to be used for the parsing
1065 * str: String to be parsed
1066 * error: Pointer to integer. Holds error no. on completion.
1067 * pos: Pointer to char position. Holds approximate error position.
1068 * return: RPN tree on successful completion; NULL otherwise.
1070 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1071 int *error, int *pos)
1073 CCL_parser cclp = ccl_parser_create(bibset);
1074 struct ccl_token *list;
1075 struct ccl_rpn_node *p;
1077 list = ccl_parser_tokenize(cclp, str);
1078 p = ccl_parser_find_token(cclp, list);
1080 *error = cclp->error_code;
1082 *pos = cclp->error_pos - str;
1083 ccl_parser_destroy(cclp);
1084 ccl_token_del(list);
1091 * c-file-style: "Stroustrup"
1092 * indent-tabs-mode: nil
1094 * vim: shiftwidth=4 tabstop=8 expandtab