1 /* CCL find (to rpn conversion)
5 * Revision 1.11 1995/02/23 08:31:59 adam
8 * Revision 1.9 1995/02/16 13:20:06 adam
11 * Revision 1.8 1995/02/14 19:59:42 adam
12 * Removed a syntax error.
14 * Revision 1.7 1995/02/14 19:55:10 adam
15 * Header files ccl.h/cclp.h are gone! They have been merged an
16 * moved to ../include/ccl.h.
17 * Node kind(s) in ccl_rpn_node have changed names.
19 * Revision 1.6 1995/02/14 16:20:55 adam
20 * Qualifiers are read from a file now.
22 * Revision 1.5 1995/02/14 14:12:41 adam
23 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
25 * Revision 1.4 1995/02/14 13:16:29 adam
26 * Left and/or right truncation implemented.
28 * Revision 1.3 1995/02/14 10:25:56 adam
29 * The constructions 'qualifier rel term ...' implemented.
31 * Revision 1.2 1995/02/13 15:15:07 adam
32 * Added handling of qualifiers. Not finished yet.
34 * Revision 1.1 1995/02/13 12:35:20 adam
35 * First version of CCL. Qualifiers aren't handled yet.
46 static struct ccl_token *look_token;
48 static CCL_bibset bibset;
50 #define KIND (look_token->kind)
51 #define ADVANCE look_token = look_token->next
52 #define ADVX(x) x=(x)->next
54 static struct ccl_rpn_attr *qual_val (struct ccl_rpn_attr *list, int type)
58 if (list->type == type)
65 static int qual_val_type (struct ccl_rpn_attr *list, int type, int value)
69 if (list->type == type && list->value == value)
76 static void strxcat (char *n, const char *src, int len)
85 static char *copy_token_name (struct ccl_token *tp)
87 char *str = malloc (tp->len + 1);
89 memcpy (str, tp->name, tp->len);
94 static struct ccl_rpn_node *mk_node (enum rpn_node_kind kind)
96 struct ccl_rpn_node *p;
97 p = malloc (sizeof(*p));
103 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
105 struct ccl_rpn_attr *attr, *attr1;
113 ccl_rpn_delete (rpn->u.p[0]);
114 ccl_rpn_delete (rpn->u.p[1]);
117 free (rpn->u.t.term);
118 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
125 free (rpn->u.setname);
128 ccl_rpn_delete (rpn->u.p[0]);
129 ccl_rpn_delete (rpn->u.p[1]);
135 static struct ccl_rpn_node *find_spec (struct ccl_rpn_attr **qa);
136 static struct ccl_rpn_node *search_terms (struct ccl_rpn_attr **qa);
138 static void add_attr (struct ccl_rpn_node *p, int type, int value)
140 struct ccl_rpn_attr *n;
142 n = malloc (sizeof(*n));
146 n->next = p->u.t.attr_list;
147 p->u.t.attr_list = n;
150 static struct ccl_rpn_node *search_term (struct ccl_rpn_attr **qa)
152 struct ccl_rpn_node *p;
153 struct ccl_rpn_attr *attr;
154 struct ccl_token *lookahead = look_token;
161 if (KIND != CCL_TOK_TERM)
163 ccl_error = CCL_ERR_TERM_EXPECTED;
166 for (no = 0; lookahead->kind == CCL_TOK_TERM; no++)
168 for (i = 0; i<lookahead->len; i++)
169 if (lookahead->name[i] == '?')
171 if (no == 0 && i == 0 && lookahead->len >= 1)
173 else if (lookahead->next->kind != CCL_TOK_TERM &&
174 i == lookahead->len-1 && i >= 1)
179 len += 1+lookahead->len;
180 lookahead = lookahead->next;
182 p = mk_node (CCL_RPN_TERM);
183 p->u.t.term = malloc (len);
184 assert (p->u.t.term);
185 p->u.t.attr_list = NULL;
186 p->u.t.term[0] = '\0';
187 for (i = 0; i<no; i++)
189 const char *src_str = look_token->name;
190 int src_len = look_token->len;
192 if (i == 0 && left_trunc)
197 else if (i == no-1 && right_trunc)
200 strcat (p->u.t.term, " ");
201 strxcat (p->u.t.term, src_str, src_len);
207 for (i=0; qa[i]; i++)
209 struct ccl_rpn_attr *attr;
211 for (attr = qa[i]; attr; attr = attr->next)
213 add_attr (p, attr->type, attr->value);
218 attr = ccl_qual_search (bibset, "term", 4);
219 if (attr && qual_val_type (attr, CCL_BIB1_STR, CCL_BIB1_STR_WP))
222 add_attr (p, CCL_BIB1_STR, 2);
224 add_attr (p, CCL_BIB1_STR, 1);
226 if (left_trunc && right_trunc)
228 if (attr && !qual_val_type (attr, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH))
230 ccl_error = CCL_ERR_TRUNC_NOT_BOTH;
236 add_attr (p, CCL_BIB1_TRU, 3);
238 else if (right_trunc)
240 if (attr && !qual_val_type (attr, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT))
242 ccl_error = CCL_ERR_TRUNC_NOT_RIGHT;
248 add_attr (p, CCL_BIB1_TRU, 1);
252 if (attr && !qual_val_type (attr, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT))
254 ccl_error = CCL_ERR_TRUNC_NOT_LEFT;
260 add_attr (p, CCL_BIB1_TRU, 2);
264 if (attr && qual_val_type (attr, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE))
265 add_attr (p, CCL_BIB1_TRU, 100);
270 static struct ccl_rpn_node *qualifiers (struct ccl_token *la,
271 struct ccl_rpn_attr **qa)
273 struct ccl_token *lookahead = look_token;
274 struct ccl_rpn_attr **ap;
277 struct ccl_rpn_attr *attr;
281 ccl_error = CCL_ERR_DOUBLE_QUAL;
284 for (lookahead = look_token; lookahead != la; lookahead=lookahead->next)
286 ap = malloc (no * sizeof(*ap));
288 for (i=0; look_token != la; i++)
290 ap[i] = ccl_qual_search (bibset, look_token->name, look_token->len);
293 ccl_error = CCL_ERR_UNKNOWN_QUAL;
298 if (KIND == CCL_TOK_COMMA)
302 if (! (attr = qual_val (ap[0], CCL_BIB1_REL)) ||
303 attr->value != CCL_BIB1_REL_ORDER)
305 /* unordered relation */
306 struct ccl_rpn_node *p;
307 if (KIND != CCL_TOK_EQ)
309 ccl_error = CCL_ERR_EQ_EXPECTED;
314 if (KIND == CCL_TOK_LP)
317 if (!(p = find_spec (ap)))
322 if (KIND != CCL_TOK_RP)
324 ccl_error = CCL_ERR_RP_EXPECTED;
332 p = search_terms (ap);
337 if (look_token->len == 1)
339 if (look_token->name[0] == '<')
341 else if (look_token->name[0] == '=')
343 else if (look_token->name[0] == '>')
346 else if (look_token->len == 2)
348 if (!memcmp (look_token->name, "<=", 2))
350 else if (!memcmp (look_token->name, ">=", 2))
352 else if (!memcmp (look_token->name, "<>", 2))
356 ccl_error = CCL_ERR_BAD_RELATION;
359 struct ccl_rpn_node *p;
361 ADVANCE; /* skip relation */
362 if (KIND == CCL_TOK_TERM)
364 struct ccl_rpn_node *p1;
365 p1 = search_term (ap);
366 if (KIND == CCL_TOK_MINUS)
368 ADVANCE; /* skip '-' */
369 if (KIND == CCL_TOK_TERM) /* = term - term ? */
371 struct ccl_rpn_node *p2;
373 p2 = search_term (ap);
374 p = mk_node (CCL_RPN_AND);
376 add_attr (p1, CCL_BIB1_REL, 4);
378 add_attr (p2, CCL_BIB1_REL, 2);
384 add_attr (p1, CCL_BIB1_REL, 4);
391 add_attr (p1, CCL_BIB1_REL, rel);
396 else if (KIND == CCL_TOK_MINUS) /* = - term ? */
399 p = search_term (ap);
400 add_attr (p, CCL_BIB1_REL, 2);
404 ccl_error = CCL_ERR_TERM_EXPECTED;
410 static struct ccl_rpn_node *search_terms (struct ccl_rpn_attr **qa)
412 struct ccl_rpn_node *p1, *p2, *pn;
413 p1 = search_term (qa);
418 if (KIND == CCL_TOK_PROX)
421 p2 = search_term (qa);
427 pn = mk_node (CCL_RPN_PROX);
432 else if (KIND == CCL_TOK_TERM)
434 p2 = search_term (qa);
440 pn = mk_node (CCL_RPN_PROX);
451 static struct ccl_rpn_node *search_elements (struct ccl_rpn_attr **qa)
453 struct ccl_rpn_node *p1;
454 struct ccl_token *lookahead;
455 if (KIND == CCL_TOK_LP)
461 if (KIND != CCL_TOK_RP)
463 ccl_error = CCL_ERR_RP_EXPECTED;
470 else if (KIND == CCL_TOK_SET)
473 if (KIND == CCL_TOK_EQ)
475 if (KIND != CCL_TOK_TERM)
477 ccl_error = CCL_ERR_SETNAME_EXPECTED;
480 p1 = mk_node (CCL_RPN_SET);
481 p1->u.setname = copy_token_name (look_token);
485 lookahead = look_token;
487 while (lookahead->kind==CCL_TOK_TERM || lookahead->kind==CCL_TOK_COMMA)
488 lookahead = lookahead->next;
489 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
490 return qualifiers (lookahead, qa);
491 return search_terms (qa);
494 static struct ccl_rpn_node *find_spec (struct ccl_rpn_attr **qa)
496 struct ccl_rpn_node *p1, *p2, *pn;
497 if (!(p1 = search_elements (qa)))
505 p2 = search_elements (qa);
511 pn = mk_node (CCL_RPN_AND);
518 p2 = search_elements (qa);
524 pn = mk_node (CCL_RPN_OR);
531 p2 = search_elements (qa);
537 pn = mk_node (CCL_RPN_NOT);
548 struct ccl_rpn_node *ccl_find (CCL_bibset abibset, struct ccl_token *list,
549 int *error, const char **pos)
551 struct ccl_rpn_node *p;
555 p = find_spec (NULL);
556 if (p && KIND != CCL_TOK_EOL)
558 if (KIND == CCL_TOK_RP)
559 ccl_error = CCL_ERR_BAD_RP;
561 ccl_error = CCL_ERR_OP_EXPECTED;
565 *pos = look_token->name;
573 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
574 int *error, int *pos)
576 struct ccl_token *list, *li;
577 struct ccl_rpn_node *rpn;
578 const char *char_pos;
580 list = ccl_tokenize (str);
582 for (li = list; li; li = li->next)
583 printf ("kind=%d, str='%.*s'\n", li->kind, li->len, li->name);
585 rpn = ccl_find (bibset, list, error, &char_pos);
587 *pos = char_pos - str;