1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 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.
23 /* returns type of current lookahead */
24 #define KIND (cclp->look_token->kind)
26 /* move one token forward */
27 #define ADVANCE cclp->look_token = cclp->look_token->next
30 * qual_val_type: test for existance of attribute type/value pair.
32 * type: Type of attribute to search for
33 * value: Value of attribute to seach for
34 * return: 1 if found; 0 otherwise.
36 static int qual_val_type(ccl_qualifier_t *qa, int type, int value,
43 for (i = 0; qa[i]; i++)
45 struct ccl_rpn_attr *q = ccl_qual_get_attr(qa[i]);
48 if (q->type == type && q->kind == CCL_RPN_ATTR_NUMERIC &&
49 q->value.numeric == value)
62 * strxcat: concatenate strings.
63 * n: Null-terminated Destination string
64 * src: Source string to be appended (not null-terminated)
65 * len: Length of source string.
67 static void strxcat(char *n, const char *src, int len)
77 * copy_token_name: Return copy of CCL token name
78 * tp: Pointer to token info.
79 * return: malloc(3) allocated copy of token name.
81 static char *copy_token_name(struct ccl_token *tp)
83 char *str = (char *)xmalloc(tp->len + 1);
85 memcpy(str, tp->name, tp->len);
91 * mk_node: Create RPN node.
93 * return: pointer to allocated node.
95 struct ccl_rpn_node *ccl_rpn_node_create(enum ccl_rpn_kind kind)
97 struct ccl_rpn_node *p;
98 p = (struct ccl_rpn_node *)xmalloc(sizeof(*p));
105 p->u.t.attr_list = 0;
116 * ccl_rpn_delete: Delete RPN tree.
117 * rpn: Pointer to tree.
119 void ccl_rpn_delete(struct ccl_rpn_node *rpn)
121 struct ccl_rpn_attr *attr, *attr1;
129 ccl_rpn_delete(rpn->u.p[0]);
130 ccl_rpn_delete(rpn->u.p[1]);
133 xfree(rpn->u.t.term);
134 xfree(rpn->u.t.qual);
135 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
138 if (attr->kind == CCL_RPN_ATTR_STRING)
139 xfree(attr->value.str);
146 xfree(rpn->u.setname);
149 ccl_rpn_delete(rpn->u.p[0]);
150 ccl_rpn_delete(rpn->u.p[1]);
151 ccl_rpn_delete(rpn->u.p[2]);
157 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa);
159 static int is_term_ok(int look, int *list)
161 for (;*list >= 0; list++)
167 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa);
169 static struct ccl_rpn_attr *add_attr_node(struct ccl_rpn_node *p,
170 const char *set, int type)
172 struct ccl_rpn_attr *n;
174 n = (struct ccl_rpn_attr *)xmalloc(sizeof(*n));
177 n->set = xstrdup(set);
181 n->next = p->u.t.attr_list;
182 p->u.t.attr_list = n;
188 * add_attr_numeric: Add attribute (type/value) to RPN term node.
189 * p: RPN node of type term.
190 * type: Type of attribute
191 * value: Value of attribute
192 * set: Attribute set name
194 void ccl_add_attr_numeric(struct ccl_rpn_node *p, const char *set,
197 struct ccl_rpn_attr *n;
199 n = add_attr_node(p, set, type);
200 n->kind = CCL_RPN_ATTR_NUMERIC;
201 n->value.numeric = value;
204 void ccl_add_attr_string(struct ccl_rpn_node *p, const char *set,
205 int type, char *value)
207 struct ccl_rpn_attr *n;
209 n = add_attr_node(p, set, type);
210 n->kind = CCL_RPN_ATTR_STRING;
211 n->value.str = xstrdup(value);
216 * search_term: Parse CCL search term.
218 * qa: Qualifier attributes already applied.
219 * term_list: tokens we accept as terms in context
220 * multi: whether we accept "multiple" tokens
221 * return: pointer to node(s); NULL on error.
223 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
225 int *term_list, int multi)
227 struct ccl_rpn_node *p_top = 0;
228 struct ccl_token *lookahead = cclp->look_token;
232 const char **truncation_aliases;
233 const char *t_default[2];
236 ccl_qual_search_special(cclp->bibset, "truncation");
237 if (!truncation_aliases)
239 truncation_aliases = t_default;
244 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
246 if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
250 struct ccl_rpn_node *p;
253 int relation_value = -1;
254 int position_value = -1;
255 int structure_value = -1;
256 int truncation_value = -1;
257 int completeness_value = -1;
262 if (and_list || or_list || !multi)
265 /* ignore commas when dealing with and-lists .. */
266 if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
268 lookahead = lookahead->next;
272 /* go through each TERM token. If no truncation attribute is yet
273 met, then look for left/right truncation markers (?) and
274 set left_trunc/right_trunc/mid_trunc accordingly */
275 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
277 for (i = 0; i<lookahead->len; i++)
278 if (lookahead->name[i] == ' ')
280 len += 1+lookahead->len+lookahead->ws_prefix_len;
281 left_trunc = lookahead->left_trunc;
282 right_trunc = lookahead->right_trunc;
283 lookahead = lookahead->next;
287 break; /* no more terms . stop . */
289 /* create the term node, but wait a moment before adding the term */
290 p = ccl_rpn_node_create(CCL_RPN_TERM);
291 p->u.t.attr_list = NULL;
295 const char *n = ccl_qual_get_name(qa[0]);
297 p->u.t.qual = xstrdup(n);
300 /* go through all attributes and add them to the attribute list */
301 for (i=0; qa && qa[i]; i++)
303 struct ccl_rpn_attr *attr;
305 for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
308 case CCL_RPN_ATTR_STRING:
309 ccl_add_attr_string(p, attr->set, attr->type,
312 case CCL_RPN_ATTR_NUMERIC:
313 if (attr->value.numeric > 0)
314 { /* deal only with REAL attributes (positive) */
318 if (relation_value != -1)
320 relation_value = attr->value.numeric;
323 if (position_value != -1)
325 position_value = attr->value.numeric;
328 if (structure_value != -1)
330 structure_value = attr->value.numeric;
333 if (truncation_value != -1)
335 truncation_value = attr->value.numeric;
338 if (completeness_value != -1)
340 completeness_value = attr->value.numeric;
343 ccl_add_attr_numeric(p, attr->set, attr->type,
344 attr->value.numeric);
348 /* len now holds the number of characters in the RPN term */
349 /* no holds the number of CCL tokens (1 or more) */
351 if (structure_value == -1 &&
352 qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
353 { /* no structure attribute met. Apply either structure attribute
354 WORD or PHRASE depending on number of CCL tokens */
355 if (no == 1 && no_spaces == 0)
356 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
358 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
361 /* make the RPN token */
362 p->u.t.term = (char *)xmalloc(len);
363 ccl_assert(p->u.t.term);
364 p->u.t.term[0] = '\0';
365 for (i = 0; i<no; i++)
367 const char *src_str = cclp->look_token->name;
368 size_t src_len = cclp->look_token->len;
370 if (p->u.t.term[0] && cclp->look_token->ws_prefix_len)
372 size_t len = strlen(p->u.t.term);
373 memcpy(p->u.t.term + len, cclp->look_token->ws_prefix_buf,
374 cclp->look_token->ws_prefix_len);
375 p->u.t.term[len + cclp->look_token->ws_prefix_len] = '\0';
377 strxcat(p->u.t.term, src_str, src_len);
381 /* make the top node point to us.. */
384 struct ccl_rpn_node *tmp;
387 tmp = ccl_rpn_node_create(CCL_RPN_OR);
389 tmp = ccl_rpn_node_create(CCL_RPN_AND);
391 tmp = ccl_rpn_node_create(CCL_RPN_AND);
401 if (left_trunc && right_trunc)
403 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
406 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
410 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
412 else if (right_trunc)
414 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
417 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
421 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
425 if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
428 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
432 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
436 if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
438 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
444 cclp->error_code = CCL_ERR_TERM_EXPECTED;
448 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
450 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
451 return search_term_x(cclp, qa, list, 0);
455 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
456 ccl_qualifier_t *ap, char *attset)
459 struct ccl_rpn_node *p;
461 if (cclp->look_token->len == 1)
463 if (cclp->look_token->name[0] == '<')
465 else if (cclp->look_token->name[0] == '=')
467 else if (cclp->look_token->name[0] == '>')
470 else if (cclp->look_token->len == 2)
472 if (!memcmp(cclp->look_token->name, "<=", 2))
474 else if (!memcmp(cclp->look_token->name, ">=", 2))
476 else if (!memcmp(cclp->look_token->name, "<>", 2))
481 cclp->error_code = CCL_ERR_BAD_RELATION;
484 ADVANCE; /* skip relation */
486 qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
488 /* allow - inside term and treat it as range _always_ */
489 /* relation is =. Extract "embedded" - to separate terms */
490 if (KIND == CCL_TOK_TERM)
493 for (i = 0; i<cclp->look_token->len; i++)
495 if (cclp->look_token->name[i] == '-')
499 if (cclp->look_token->len > 1 && i == 0)
501 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
503 ntoken->kind = CCL_TOK_TERM;
504 ntoken->name = cclp->look_token->name + 1;
505 ntoken->len = cclp->look_token->len - 1;
507 cclp->look_token->len = 1;
508 cclp->look_token->name = "-";
510 else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
512 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
514 ntoken->kind = CCL_TOK_TERM;
518 (cclp->look_token->len)--;
520 else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
522 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
523 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
525 ntoken1->kind = CCL_TOK_TERM; /* generate - */
529 ntoken2->kind = CCL_TOK_TERM; /* generate yy */
530 ntoken2->name = cclp->look_token->name + (i+1);
531 ntoken2->len = cclp->look_token->len - (i+1);
533 cclp->look_token->len = i; /* adjust xx */
535 else if (i == cclp->look_token->len &&
536 cclp->look_token->next &&
537 cclp->look_token->next->kind == CCL_TOK_TERM &&
538 cclp->look_token->next->len > 1 &&
539 cclp->look_token->next->name[0] == '-')
542 /* we _know_ that xx does not have - in it */
543 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
545 ntoken->kind = CCL_TOK_TERM; /* generate - */
549 (ntoken->next->name)++; /* adjust yy */
550 (ntoken->next->len)--;
556 KIND == CCL_TOK_TERM &&
557 cclp->look_token->next && cclp->look_token->next->len == 1 &&
558 cclp->look_token->next->name[0] == '-')
560 struct ccl_rpn_node *p1;
561 if (!(p1 = search_term(cclp, ap)))
563 ADVANCE; /* skip '-' */
564 if (KIND == CCL_TOK_TERM) /* = term - term ? */
566 struct ccl_rpn_node *p2;
568 if (!(p2 = search_term(cclp, ap)))
573 p = ccl_rpn_node_create(CCL_RPN_AND);
575 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
577 ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
582 ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
587 cclp->look_token->len == 1 &&
588 cclp->look_token->name[0] == '-') /* = - term ? */
591 if (!(p = search_term(cclp, ap)))
593 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
596 else if (KIND == CCL_TOK_LP)
599 if (!(p = find_spec(cclp, ap)))
601 if (KIND != CCL_TOK_RP)
603 cclp->error_code = CCL_ERR_RP_EXPECTED;
612 if (!(p = search_terms(cclp, ap)))
614 ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
617 cclp->error_code = CCL_ERR_TERM_EXPECTED;
622 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
625 struct ccl_rpn_node *p;
627 if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
628 || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
629 return qualifiers_order(cclp, ap, attset);
631 /* unordered relation */
632 if (KIND != CCL_TOK_EQ)
634 cclp->error_code = CCL_ERR_EQ_EXPECTED;
638 if (KIND == CCL_TOK_LP)
641 if (!(p = find_spec(cclp, ap)))
645 if (KIND != CCL_TOK_RP)
647 cclp->error_code = CCL_ERR_RP_EXPECTED;
654 p = search_terms(cclp, ap);
659 * qualifier_list: Parse CCL qualifiers and search terms.
661 * la: Token pointer to RELATION token.
662 * qa: Qualifier attributes already applied.
663 * return: pointer to node(s); NULL on error.
665 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
666 struct ccl_token *la,
669 struct ccl_token *lookahead = cclp->look_token;
670 struct ccl_token *look_start = cclp->look_token;
672 struct ccl_rpn_node *node = 0;
673 const char **field_str;
681 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
685 for (lookahead = cclp->look_token; lookahead != la;
686 lookahead=lookahead->next)
689 for (i=0; qa[i]; i++)
691 ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
694 field_str = ccl_qual_search_special(cclp->bibset, "field");
697 if (!strcmp(field_str[0], "or"))
699 else if (!strcmp(field_str[0], "merge"))
704 /* consider each field separately and OR */
705 lookahead = look_start;
706 while (lookahead != la)
710 while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
711 lookahead->len, seq)) != 0)
713 struct ccl_rpn_node *node_sub;
714 cclp->look_token = la;
716 node_sub = qualifier_relation(cclp, ap);
719 ccl_rpn_delete(node);
725 struct ccl_rpn_node *node_this =
726 ccl_rpn_node_create(CCL_RPN_OR);
727 node_this->u.p[0] = node;
728 node_this->u.p[1] = node_sub;
737 cclp->look_token = lookahead;
738 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
742 lookahead = lookahead->next;
743 if (lookahead->kind == CCL_TOK_COMMA)
744 lookahead = lookahead->next;
749 /* merge attributes from ALL fields - including inherited ones */
752 struct ccl_rpn_node *node_sub;
754 lookahead = look_start;
755 for (i = 0; lookahead != la; i++)
757 ap[i] = ccl_qual_search(cclp, lookahead->name,
758 lookahead->len, seq);
761 if (!ap[i] && seq > 0)
762 ap[i] = ccl_qual_search(cclp, lookahead->name,
766 cclp->look_token = lookahead;
767 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
771 lookahead = lookahead->next;
772 if (lookahead->kind == CCL_TOK_COMMA)
773 lookahead = lookahead->next;
777 ccl_qualifier_t *qa0 = qa;
787 cclp->look_token = lookahead;
789 node_sub = qualifier_relation(cclp, ap);
792 ccl_rpn_delete(node);
797 struct ccl_rpn_node *node_this =
798 ccl_rpn_node_create(CCL_RPN_OR);
799 node_this->u.p[0] = node;
800 node_this->u.p[1] = node_sub;
814 * search_terms: Parse CCL search terms - including proximity.
816 * qa: Qualifier attributes already applied.
817 * return: pointer to node(s); NULL on error.
819 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
821 static int list[] = {
822 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ, CCL_TOK_REL, CCL_TOK_SET, -1};
823 struct ccl_rpn_node *p1, *p2, *pn;
824 p1 = search_term_x(cclp, qa, list, 1);
829 if (KIND == CCL_TOK_PROX)
831 struct ccl_rpn_node *p_prox = 0;
832 /* ! word order specified */
833 /* % word order not specified */
834 p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
835 p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
836 memcpy(p_prox->u.t.term, cclp->look_token->name,
837 cclp->look_token->len);
838 p_prox->u.t.term[cclp->look_token->len] = 0;
839 p_prox->u.t.attr_list = 0;
842 p2 = search_term_x(cclp, qa, list, 1);
848 pn = ccl_rpn_node_create(CCL_RPN_PROX);
854 else if (is_term_ok(KIND, list))
856 p2 = search_term_x(cclp, qa, list, 1);
862 pn = ccl_rpn_node_create(CCL_RPN_PROX);
875 * search_elements: Parse CCL search elements
877 * qa: Qualifier attributes already applied.
878 * return: pointer to node(s); NULL on error.
880 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
883 struct ccl_rpn_node *p1;
884 struct ccl_token *lookahead;
885 if (KIND == CCL_TOK_LP)
888 p1 = find_spec(cclp, qa);
891 if (KIND != CCL_TOK_RP)
893 cclp->error_code = CCL_ERR_RP_EXPECTED;
900 else if (KIND == CCL_TOK_SET)
903 if (KIND == CCL_TOK_EQ)
905 if (KIND != CCL_TOK_TERM)
907 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
910 p1 = ccl_rpn_node_create(CCL_RPN_SET);
911 p1->u.setname = copy_token_name(cclp->look_token);
915 lookahead = cclp->look_token;
917 while (lookahead->kind==CCL_TOK_TERM)
919 lookahead = lookahead->next;
920 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
921 return qualifier_list(cclp, lookahead, qa);
922 if (lookahead->kind != CCL_TOK_COMMA)
924 lookahead = lookahead->next;
927 return search_terms(cclp, qa);
930 ccl_qualifier_t qa[2];
931 struct ccl_rpn_node *node = 0;
933 lookahead = cclp->look_token;
938 struct ccl_rpn_node *node_sub;
939 qa[0] = ccl_qual_search(cclp, "term", 4, seq);
943 cclp->look_token = lookahead;
945 node_sub = search_terms(cclp, qa);
948 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;
957 node_this->u.p[2] = 0;
964 node = search_terms(cclp, 0);
970 * find_spec: Parse CCL find specification
972 * qa: Qualifier attributes already applied.
973 * return: pointer to node(s); NULL on error.
975 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
977 struct ccl_rpn_node *p1, *p2, *pn;
978 if (!(p1 = search_elements(cclp, qa)))
986 p2 = search_elements(cclp, qa);
992 pn = ccl_rpn_node_create(CCL_RPN_AND);
1000 p2 = search_elements(cclp, qa);
1006 pn = ccl_rpn_node_create(CCL_RPN_OR);
1014 p2 = search_elements(cclp, qa);
1020 pn = ccl_rpn_node_create(CCL_RPN_NOT);
1032 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1034 struct ccl_rpn_node *p;
1035 struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1036 p = ccl_parser_find_token(cclp, list);
1037 ccl_token_del(list);
1041 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1042 struct ccl_token *list)
1044 struct ccl_rpn_node *p;
1046 cclp->look_token = list;
1047 p = find_spec(cclp, NULL);
1048 if (p && KIND != CCL_TOK_EOL)
1050 if (KIND == CCL_TOK_RP)
1051 cclp->error_code = CCL_ERR_BAD_RP;
1053 cclp->error_code = CCL_ERR_OP_EXPECTED;
1057 cclp->error_pos = cclp->look_token->name;
1059 cclp->error_code = CCL_ERR_OK;
1061 cclp->error_code = cclp->error_code;
1066 * ccl_find_str: Parse CCL find - string representation
1067 * bibset: Bibset to be used for the parsing
1068 * str: String to be parsed
1069 * error: Pointer to integer. Holds error no. on completion.
1070 * pos: Pointer to char position. Holds approximate error position.
1071 * return: RPN tree on successful completion; NULL otherwise.
1073 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1074 int *error, int *pos)
1076 CCL_parser cclp = ccl_parser_create(bibset);
1077 struct ccl_token *list;
1078 struct ccl_rpn_node *p;
1080 list = ccl_parser_tokenize(cclp, str);
1081 p = ccl_parser_find_token(cclp, list);
1083 *error = cclp->error_code;
1085 *pos = cclp->error_pos - str;
1086 ccl_parser_destroy(cclp);
1087 ccl_token_del(list);
1094 * c-file-style: "Stroustrup"
1095 * indent-tabs-mode: nil
1097 * vim: shiftwidth=4 tabstop=8 expandtab