1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements PQF parsing
17 #include <yaz/proto.h>
18 #include <yaz/oid_db.h>
19 #include <yaz/pquery.h>
21 struct yaz_pqf_parser {
22 const char *query_buf;
23 const char *query_ptr;
35 static Z_RPNStructure *rpn_structure(struct yaz_pqf_parser *li, ODR o,
36 int num_attr, int max_attr,
37 Odr_int *attr_list, char **attr_clist,
40 static Odr_oid *query_oid_getvalbyname(struct yaz_pqf_parser *li, ODR o)
44 if (li->lex_len >= sizeof(buf)-1)
46 memcpy(buf, li->lex_buf, li->lex_len);
47 buf[li->lex_len] = '\0';
48 return yaz_string_to_oid_odr(yaz_oid_std(), CLASS_ATTSET, buf, o);
51 static int compare_term(struct yaz_pqf_parser *li, const char *src,
54 size_t len=strlen(src);
56 if (li->lex_len == len+off && !memcmp(li->lex_buf+off, src, len-off))
61 static int query_token(struct yaz_pqf_parser *li)
64 const char *sep_match;
65 const char **qptr = &li->query_ptr;
72 if ((sep_match = strchr(li->left_sep, **qptr)))
74 sep_char = li->right_sep[sep_match - li->left_sep];
79 if (**qptr == li->escape_char && yaz_isdigit((*qptr)[1]))
85 while (**qptr && **qptr != sep_char)
87 if (**qptr == '\\' && (*qptr)[1])
97 if (sep_char == ' ' &&
98 li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
100 if (compare_term(li, "and", 1))
102 if (compare_term(li, "or", 1))
104 if (compare_term(li, "not", 1))
106 if (compare_term(li, "attr", 1))
108 if (compare_term(li, "set", 1))
110 if (compare_term(li, "attrset", 1))
112 if (compare_term(li, "prox", 1))
114 if (compare_term(li, "term", 1))
120 static int lex(struct yaz_pqf_parser *li)
122 return li->query_look = query_token(li);
125 static int escape_string(char *out_buf, const char *in, int len)
130 if (*in == '\\' && len > 0)
185 return out - out_buf;
188 static int p_query_parse_attr(struct yaz_pqf_parser *li, ODR o,
189 int num_attr, Odr_int *attr_list,
190 char **attr_clist, Odr_oid **attr_set)
195 if (!(cp = strchr(li->lex_buf, '=')) ||
196 (size_t) (cp-li->lex_buf) > li->lex_len)
198 attr_set[num_attr] = query_oid_getvalbyname(li, o);
199 if (attr_set[num_attr] == 0)
201 li->error = YAZ_PQF_ERROR_ATTSET;
206 li->error = YAZ_PQF_ERROR_MISSING;
209 if (!(cp = strchr(li->lex_buf, '=')))
211 li->error = YAZ_PQF_ERROR_BADATTR;
218 attr_set[num_attr] = attr_set[num_attr-1];
220 attr_set[num_attr] = 0;
222 if (*li->lex_buf < '0' || *li->lex_buf > '9')
224 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
227 attr_list[2*num_attr] = odr_atoi(li->lex_buf);
230 /* inspect value .. and make it a integer if it appears to be */
231 for (i = cp - li->lex_buf; i < li->lex_len; i++)
232 if (li->lex_buf[i] < '0' || li->lex_buf[i] > '9')
234 int len = li->lex_len - (cp - li->lex_buf);
235 attr_list[2*num_attr+1] = 0;
236 attr_clist[num_attr] = (char *) odr_malloc(o, len+1);
237 len = escape_string(attr_clist[num_attr], cp, len);
238 attr_clist[num_attr][len] = '\0';
241 attr_list[2*num_attr+1] = odr_atoi(cp);
242 attr_clist[num_attr] = 0;
246 static Z_AttributeList *get_attributeList(ODR o,
247 int num_attr, Odr_int *attr_list,
248 char **attr_clist, Odr_oid **attr_set)
252 Z_AttributeElement **elements;
253 Z_AttributeList *attributes= (Z_AttributeList *)
254 odr_malloc(o, sizeof(*attributes));
255 attributes->num_attributes = num_attr;
258 attributes->attributes = (Z_AttributeElement**)odr_nullval();
261 elements = (Z_AttributeElement**)
262 odr_malloc(o, num_attr * sizeof(*elements));
264 attr_tmp = (Odr_int *)odr_malloc(o, num_attr * 2 * sizeof(*attr_tmp));
265 memcpy(attr_tmp, attr_list, num_attr * 2 * sizeof(*attr_tmp));
266 for (i = num_attr; --i >= 0; )
269 for (j = i+1; j<num_attr; j++)
270 if (attr_tmp[2*j] == attr_tmp[2*i])
275 (Z_AttributeElement*)odr_malloc(o,sizeof(**elements));
276 elements[k]->attributeType = &attr_tmp[2*i];
277 elements[k]->attributeSet = attr_set[i];
281 elements[k]->which = Z_AttributeValue_complex;
282 elements[k]->value.complex = (Z_ComplexAttribute *)
283 odr_malloc(o, sizeof(Z_ComplexAttribute));
284 elements[k]->value.complex->num_list = 1;
285 elements[k]->value.complex->list =
286 (Z_StringOrNumeric **)
287 odr_malloc(o, 1 * sizeof(Z_StringOrNumeric *));
288 elements[k]->value.complex->list[0] =
289 (Z_StringOrNumeric *)
290 odr_malloc(o, sizeof(Z_StringOrNumeric));
291 elements[k]->value.complex->list[0]->which =
292 Z_StringOrNumeric_string;
293 elements[k]->value.complex->list[0]->u.string =
295 elements[k]->value.complex->semanticAction = 0;
296 elements[k]->value.complex->num_semanticAction = 0;
300 elements[k]->which = Z_AttributeValue_numeric;
301 elements[k]->value.numeric = &attr_tmp[2*i+1];
305 attributes->num_attributes = k;
306 attributes->attributes = elements;
310 Z_Term *z_Term_create(ODR o, int term_type, const char *buf, size_t len)
312 Z_Term *term = (Z_Term *)odr_malloc(o, sizeof(*term));
313 Odr_oct *term_octet = (Odr_oct *)odr_malloc(o, sizeof(*term_octet));
314 term_octet->buf = (unsigned char *)odr_malloc(o, 1 + len);
315 memcpy(term_octet->buf, buf, len);
316 term_octet->size = term_octet->len = len;
317 term_octet->buf[term_octet->size] = 0; /* null terminate */
322 term->which = Z_Term_general;
323 term->u.general = term_octet;
325 case Z_Term_characterString:
326 term->which = Z_Term_characterString;
327 term->u.characterString = (char*) term_octet->buf;
328 /* null terminated above */
331 term->which = Z_Term_numeric;
332 term->u.numeric = odr_intdup(o, odr_atoi((const char*) term_octet->buf));
335 term->which = Z_Term_null;
336 term->u.null = odr_nullval();
338 case Z_Term_external:
339 term->which = Z_Term_external;
340 term->u.external = 0;
343 term->which = Z_Term_null;
344 term->u.null = odr_nullval();
350 static Z_AttributesPlusTerm *rpn_term_attributes(
351 struct yaz_pqf_parser *li, ODR o, Z_AttributeList *attributes)
353 char *es_str = odr_malloc(o, li->lex_len+1);
354 int es_len = escape_string(es_str, li->lex_buf, li->lex_len);
355 Z_Term *term = z_Term_create(o, li->term_type, es_str, es_len);
356 Z_AttributesPlusTerm *zapt = (Z_AttributesPlusTerm *)
357 odr_malloc(o, sizeof(*zapt));
360 zapt->attributes = attributes;
364 static Z_AttributesPlusTerm *rpn_term(struct yaz_pqf_parser *li, ODR o,
365 int num_attr, Odr_int *attr_list,
366 char **attr_clist, Odr_oid **attr_set)
368 return rpn_term_attributes(li, o, get_attributeList(o, num_attr, attr_list, attr_clist, attr_set));
371 static Z_Operand *rpn_simple(struct yaz_pqf_parser *li, ODR o,
372 int num_attr, Odr_int *attr_list,
378 zo = (Z_Operand *)odr_malloc(o, sizeof(*zo));
379 switch (li->query_look)
382 zo->which = Z_Operand_APT;
383 if (!(zo->u.attributesPlusTerm =
384 rpn_term(li, o, num_attr, attr_list, attr_clist, attr_set)))
392 li->error = YAZ_PQF_ERROR_MISSING;
395 zo->which = Z_Operand_resultSetId;
396 zo->u.resultSetId = (char *)odr_malloc(o, li->lex_len+1);
397 memcpy(zo->u.resultSetId, li->lex_buf, li->lex_len);
398 zo->u.resultSetId[li->lex_len] = '\0';
402 /* we're only called if one of the above types are seens so
403 this shouldn't happen */
404 li->error = YAZ_PQF_ERROR_INTERNAL;
410 static Z_ProximityOperator *rpn_proximity(struct yaz_pqf_parser *li, ODR o)
412 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc(o, sizeof(*p));
416 li->error = YAZ_PQF_ERROR_MISSING;
419 if (*li->lex_buf == '1')
420 p->exclusion = odr_booldup(o, 1);
421 else if (*li->lex_buf == '0')
422 p->exclusion = odr_booldup(o, 0);
423 else if (*li->lex_buf == 'v' || *li->lex_buf == 'n')
427 li->error = YAZ_PQF_ERROR_PROXIMITY;
433 li->error = YAZ_PQF_ERROR_MISSING;
436 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
437 p->distance = odr_intdup(o, odr_atoi(li->lex_buf));
440 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
446 li->error = YAZ_PQF_ERROR_MISSING;
449 if (*li->lex_buf == '1')
450 p->ordered = odr_booldup(o, 1);
451 else if (*li->lex_buf == '0')
452 p->ordered = odr_booldup(o, 0);
455 li->error = YAZ_PQF_ERROR_PROXIMITY;
461 li->error = YAZ_PQF_ERROR_MISSING;
464 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
465 p->relationType = odr_intdup(o, odr_atoi(li->lex_buf));
468 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
474 li->error = YAZ_PQF_ERROR_MISSING;
477 if (*li->lex_buf == 'k')
478 p->which = Z_ProximityOperator_known;
479 else if (*li->lex_buf == 'p')
480 p->which = Z_ProximityOperator_private;
482 p->which = atoi(li->lex_buf);
484 if (p->which != Z_ProximityOperator_known
485 && p->which != Z_ProximityOperator_private)
487 li->error = YAZ_PQF_ERROR_PROXIMITY;
493 li->error = YAZ_PQF_ERROR_MISSING;
496 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
497 p->u.known = odr_intdup(o, odr_atoi(li->lex_buf));
500 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
506 static Z_Complex *rpn_complex(struct yaz_pqf_parser *li, ODR o,
507 int num_attr, int max_attr,
508 Odr_int *attr_list, char **attr_clist,
514 zc = (Z_Complex *)odr_malloc(o, sizeof(*zc));
515 zo = (Z_Operator *)odr_malloc(o, sizeof(*zo));
517 switch (li->query_look)
520 zo->which = Z_Operator_and;
521 zo->u.op_and = odr_nullval();
524 zo->which = Z_Operator_or;
525 zo->u.op_or = odr_nullval();
528 zo->which = Z_Operator_and_not;
529 zo->u.and_not = odr_nullval();
532 zo->which = Z_Operator_prox;
533 zo->u.prox = rpn_proximity(li, o);
538 /* we're only called if one of the above types are seens so
539 this shouldn't happen */
540 li->error = YAZ_PQF_ERROR_INTERNAL;
545 rpn_structure(li, o, num_attr, max_attr, attr_list,
546 attr_clist, attr_set)))
549 rpn_structure(li, o, num_attr, max_attr, attr_list,
550 attr_clist, attr_set)))
555 static void rpn_term_type(struct yaz_pqf_parser *li)
559 if (compare_term(li, "general", 0))
560 li->term_type = Z_Term_general;
561 else if (compare_term(li, "numeric", 0))
562 li->term_type = Z_Term_numeric;
563 else if (compare_term(li, "string", 0))
564 li->term_type = Z_Term_characterString;
565 else if (compare_term(li, "oid", 0))
566 li->term_type = Z_Term_oid;
567 else if (compare_term(li, "datetime", 0))
568 li->term_type = Z_Term_dateTime;
569 else if (compare_term(li, "null", 0))
570 li->term_type = Z_Term_null;
572 else if (compare_term(li, "range", 0))
574 /* prepare for external: range search .. */
575 li->term_type = Z_Term_external;
576 li->external_type = VAL_MULTISRCH2;
582 static Z_RPNStructure *rpn_structure(struct yaz_pqf_parser *li, ODR o,
583 int num_attr, int max_attr,
590 sz = (Z_RPNStructure *)odr_malloc(o, sizeof(*sz));
591 switch (li->query_look)
597 sz->which = Z_RPNStructure_complex;
598 if (!(sz->u.complex =
599 rpn_complex(li, o, num_attr, max_attr, attr_list,
600 attr_clist, attr_set)))
605 sz->which = Z_RPNStructure_simple;
607 rpn_simple(li, o, num_attr, attr_list,
608 attr_clist, attr_set)))
615 li->error = YAZ_PQF_ERROR_MISSING;
618 if (num_attr >= max_attr)
620 li->error = YAZ_PQF_ERROR_TOOMANY;
623 if (!p_query_parse_attr(li, o, num_attr, attr_list,
624 attr_clist, attr_set))
629 rpn_structure(li, o, num_attr, max_attr, attr_list,
630 attr_clist, attr_set);
635 rpn_structure(li, o, num_attr, max_attr, attr_list,
636 attr_clist, attr_set);
637 case 0: /* operator/operand expected! */
638 li->error = YAZ_PQF_ERROR_MISSING;
644 static Z_RPNQuery *p_query_rpn_mk(ODR o, struct yaz_pqf_parser *li)
647 Odr_int attr_array[1024];
648 char *attr_clist[512];
649 Odr_oid *attr_set[512];
650 Odr_oid *top_set = 0;
652 zq = (Z_RPNQuery *)odr_malloc(o, sizeof(*zq));
654 if (li->query_look == 'r')
657 top_set = query_oid_getvalbyname(li, o);
660 li->error = YAZ_PQF_ERROR_ATTSET;
667 top_set = odr_oiddup(o, yaz_oid_attset_bib_1);
670 zq->attributeSetId = top_set;
672 if (!zq->attributeSetId)
674 li->error = YAZ_PQF_ERROR_ATTSET;
678 if (!(zq->RPNStructure = rpn_structure(li, o, 0, 512,
679 attr_array, attr_clist, attr_set)))
683 li->error = YAZ_PQF_ERROR_EXTRA;
689 static void pqf_parser_begin(struct yaz_pqf_parser *li, const char *buf)
691 li->query_buf = li->query_ptr = buf;
695 Z_RPNQuery *p_query_rpn(ODR o, const char *qbuf)
697 struct yaz_pqf_parser li;
701 li.right_sep = "}\"";
702 li.escape_char = '@';
703 li.term_type = Z_Term_general;
705 pqf_parser_begin(&li, qbuf);
706 return p_query_rpn_mk(o, &li);
709 static Z_AttributeList *p_query_scan_attributes_mk(struct yaz_pqf_parser *li,
711 Odr_oid **attributeSetP)
713 Odr_int attr_list[1024];
714 char *attr_clist[512];
715 Odr_oid *attr_set[512];
718 Odr_oid *top_set = 0;
721 if (li->query_look == 'r')
724 top_set = query_oid_getvalbyname(li, o);
727 li->error = YAZ_PQF_ERROR_ATTSET;
734 top_set = odr_oiddup(o, yaz_oid_attset_bib_1);
736 *attributeSetP = top_set;
740 if (li->query_look == 'l')
745 li->error = YAZ_PQF_ERROR_MISSING;
748 if (num_attr >= max_attr)
750 li->error = YAZ_PQF_ERROR_TOOMANY;
753 if (!p_query_parse_attr(li, o, num_attr, attr_list,
754 attr_clist, attr_set))
759 else if (li->query_look == 'y')
767 return get_attributeList(o, num_attr, attr_list, attr_clist, attr_set);
770 static Z_AttributesPlusTerm *p_query_scan_mk(struct yaz_pqf_parser *li,
772 Odr_oid **attributeSetP)
774 Z_AttributeList *attr_list = p_query_scan_attributes_mk(li, o, attributeSetP);
775 Z_AttributesPlusTerm *apt;
779 li->error = YAZ_PQF_ERROR_MISSING;
782 apt = rpn_term_attributes(li, o, attr_list);
786 if (li->query_look != 0)
788 li->error = YAZ_PQF_ERROR_EXTRA;
794 YAZ_PQF_Parser yaz_pqf_create(void)
796 YAZ_PQF_Parser p = (YAZ_PQF_Parser) xmalloc(sizeof(*p));
800 p->right_sep = "}\"";
801 p->escape_char = '@';
802 p->term_type = Z_Term_general;
807 void yaz_pqf_destroy(YAZ_PQF_Parser p)
812 Z_RPNQuery *yaz_pqf_parse(YAZ_PQF_Parser p, ODR o, const char *qbuf)
816 pqf_parser_begin(p, qbuf);
817 return p_query_rpn_mk(o, p);
820 Z_AttributesPlusTerm *yaz_pqf_scan(YAZ_PQF_Parser p, ODR o,
821 Odr_oid **attributeSetP,
826 pqf_parser_begin(p, qbuf);
827 return p_query_scan_mk(p, o, attributeSetP);
830 Z_AttributeList *yaz_pqf_scan_attribute_list(YAZ_PQF_Parser p, ODR o,
831 Odr_oid **attributeSetP,
836 pqf_parser_begin(p, qbuf);
837 return p_query_scan_attributes_mk(p, o, attributeSetP);
840 static Z_FacetField* parse_facet(ODR odr, const char *facet)
842 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
843 struct yaz_pqf_parser *li = pqf_parser;
844 Odr_oid *attributeSetId;
845 Z_FacetField *facet_field = 0;
846 Z_AttributeList *attribute_list;
848 pqf_parser_begin(pqf_parser, facet);
849 attribute_list = p_query_scan_attributes_mk(li, odr, &attributeSetId);
852 facet_field = (Z_FacetField *) odr_malloc(odr, sizeof(*facet_field));
853 facet_field->attributes = attribute_list;
854 facet_field->num_terms = 0;
855 facet_field->terms = odr_malloc(odr, 10 * sizeof(*facet_field->terms));
856 while (li->query_look == 't')
858 if (facet_field->num_terms < 10)
860 char *es_str = odr_malloc(odr, li->lex_len+1);
861 int es_len = escape_string(es_str, li->lex_buf, li->lex_len);
862 Z_Term *term = z_Term_create(odr, li->term_type, es_str, es_len);
864 facet_field->terms[facet_field->num_terms] =
865 (Z_FacetTerm *) odr_malloc(odr, sizeof(Z_FacetTerm));
866 facet_field->terms[facet_field->num_terms]->term = term;
867 facet_field->terms[facet_field->num_terms]->count =
869 facet_field->num_terms++;
874 yaz_pqf_destroy(pqf_parser);
878 Z_FacetList *yaz_pqf_parse_facet_list(ODR o, const char *qbuf)
883 nmem_strsplit(odr_getmem(o), ",", qbuf, &darray, &num);
887 Z_FacetList *fl = (Z_FacetList*) odr_malloc(o, sizeof(*fl));
889 fl->elements = (Z_FacetField **)
890 odr_malloc(o, num * sizeof(*fl->elements));
891 for (i = 0; i < num; i++)
893 fl->elements[i] = parse_facet(o, darray[i]);
894 if (!fl->elements[i])
903 int yaz_pqf_error(YAZ_PQF_Parser p, const char **msg, size_t *off)
907 case YAZ_PQF_ERROR_NONE:
908 *msg = "no error"; break;
909 case YAZ_PQF_ERROR_EXTRA:
910 *msg = "extra token"; break;
911 case YAZ_PQF_ERROR_MISSING:
912 *msg = "missing token"; break;
913 case YAZ_PQF_ERROR_ATTSET:
914 *msg = "unknown attribute set"; break;
915 case YAZ_PQF_ERROR_TOOMANY:
916 *msg = "too many attributes"; break;
917 case YAZ_PQF_ERROR_BADATTR:
918 *msg = "bad attribute specification"; break;
919 case YAZ_PQF_ERROR_INTERNAL:
920 *msg = "internal error"; break;
921 case YAZ_PQF_ERROR_PROXIMITY:
922 *msg = "proximity error"; break;
923 case YAZ_PQF_ERROR_BAD_INTEGER:
924 *msg = "bad integer"; break;
926 *msg = "unknown error"; break;
928 *off = p->query_ptr - p->query_buf;
934 * c-file-style: "Stroustrup"
935 * indent-tabs-mode: nil
937 * vim: shiftwidth=4 tabstop=8 expandtab