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 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)
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 int escape_string(char *out_buf, const char *in, int len)
130 if (*in == '\\' && len > 0)
185 return out - out_buf;
188 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 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 *) odr_malloc(o, sizeof(*attributes));
254 attributes->num_attributes = num_attr;
256 attributes->attributes = (Z_AttributeElement**)odr_nullval();
259 elements = (Z_AttributeElement**) odr_malloc (o, num_attr * sizeof(*elements));
261 attr_tmp = (Odr_int *)odr_malloc(o, num_attr * 2 * sizeof(*attr_tmp));
262 memcpy(attr_tmp, attr_list, num_attr * 2 * sizeof(*attr_tmp));
263 for (i = num_attr; --i >= 0; )
266 for (j = i+1; j<num_attr; j++)
267 if (attr_tmp[2*j] == attr_tmp[2*i])
272 (Z_AttributeElement*)odr_malloc(o,sizeof(**elements));
273 elements[k]->attributeType = &attr_tmp[2*i];
274 elements[k]->attributeSet = attr_set[i];
278 elements[k]->which = Z_AttributeValue_complex;
279 elements[k]->value.complex = (Z_ComplexAttribute *)
280 odr_malloc(o, sizeof(Z_ComplexAttribute));
281 elements[k]->value.complex->num_list = 1;
282 elements[k]->value.complex->list =
283 (Z_StringOrNumeric **)
284 odr_malloc(o, 1 * sizeof(Z_StringOrNumeric *));
285 elements[k]->value.complex->list[0] =
286 (Z_StringOrNumeric *)
287 odr_malloc(o, sizeof(Z_StringOrNumeric));
288 elements[k]->value.complex->list[0]->which =
289 Z_StringOrNumeric_string;
290 elements[k]->value.complex->list[0]->u.string =
292 elements[k]->value.complex->semanticAction = 0;
293 elements[k]->value.complex->num_semanticAction = 0;
297 elements[k]->which = Z_AttributeValue_numeric;
298 elements[k]->value.numeric = &attr_tmp[2*i+1];
302 attributes->num_attributes = k;
303 attributes->attributes = elements;
307 static Z_AttributesPlusTerm *rpn_term_attributes(struct yaz_pqf_parser *li, ODR o, Z_AttributeList *attributes) {
308 Z_AttributesPlusTerm *zapt;
312 zapt = (Z_AttributesPlusTerm *)odr_malloc(o, sizeof(*zapt));
313 term = (Z_Term *)odr_malloc(o, sizeof(*term));
315 zapt->attributes = attributes;
317 term_octet = (Odr_oct *)odr_malloc(o, sizeof(*term_octet));
318 term_octet->buf = (unsigned char *)odr_malloc(o, 1 + li->lex_len);
319 term_octet->size = term_octet->len =
320 escape_string((char *) (term_octet->buf), li->lex_buf, li->lex_len);
321 term_octet->buf[term_octet->size] = 0; /* null terminate */
323 switch (li->term_type)
326 term->which = Z_Term_general;
327 term->u.general = term_octet;
329 case Z_Term_characterString:
330 term->which = Z_Term_characterString;
331 term->u.characterString = (char*) term_octet->buf;
332 /* null terminated above */
335 term->which = Z_Term_numeric;
336 term->u.numeric = odr_intdup(o, odr_atoi((const char*) term_octet->buf));
339 term->which = Z_Term_null;
340 term->u.null = odr_nullval();
342 case Z_Term_external:
343 term->which = Z_Term_external;
344 term->u.external = 0;
347 term->which = Z_Term_null;
348 term->u.null = odr_nullval();
355 static Z_AttributesPlusTerm *rpn_term(struct yaz_pqf_parser *li, ODR o,
356 int num_attr, Odr_int *attr_list,
357 char **attr_clist, Odr_oid **attr_set)
359 return rpn_term_attributes(li, o, get_attributeList(o, num_attr, attr_list, attr_clist, attr_set));
362 static Z_Operand *rpn_simple(struct yaz_pqf_parser *li, ODR o,
363 int num_attr, Odr_int *attr_list,
369 zo = (Z_Operand *)odr_malloc(o, sizeof(*zo));
370 switch (li->query_look)
373 zo->which = Z_Operand_APT;
374 if (!(zo->u.attributesPlusTerm =
375 rpn_term(li, o, num_attr, attr_list, attr_clist, attr_set)))
383 li->error = YAZ_PQF_ERROR_MISSING;
386 zo->which = Z_Operand_resultSetId;
387 zo->u.resultSetId = (char *)odr_malloc(o, li->lex_len+1);
388 memcpy(zo->u.resultSetId, li->lex_buf, li->lex_len);
389 zo->u.resultSetId[li->lex_len] = '\0';
393 /* we're only called if one of the above types are seens so
394 this shouldn't happen */
395 li->error = YAZ_PQF_ERROR_INTERNAL;
401 static Z_ProximityOperator *rpn_proximity(struct yaz_pqf_parser *li, ODR o)
403 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc(o, sizeof(*p));
407 li->error = YAZ_PQF_ERROR_MISSING;
410 if (*li->lex_buf == '1')
411 p->exclusion = odr_booldup(o, 1);
412 else if (*li->lex_buf == '0')
413 p->exclusion = odr_booldup(o, 0);
414 else if (*li->lex_buf == 'v' || *li->lex_buf == 'n')
418 li->error = YAZ_PQF_ERROR_PROXIMITY;
424 li->error = YAZ_PQF_ERROR_MISSING;
427 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
428 p->distance = odr_intdup(o, odr_atoi(li->lex_buf));
431 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
437 li->error = YAZ_PQF_ERROR_MISSING;
440 if (*li->lex_buf == '1')
441 p->ordered = odr_booldup(o, 1);
442 else if (*li->lex_buf == '0')
443 p->ordered = odr_booldup(o, 0);
446 li->error = YAZ_PQF_ERROR_PROXIMITY;
452 li->error = YAZ_PQF_ERROR_MISSING;
455 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
456 p->relationType = odr_intdup(o, odr_atoi(li->lex_buf));
459 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
465 li->error = YAZ_PQF_ERROR_MISSING;
468 if (*li->lex_buf == 'k')
469 p->which = Z_ProximityOperator_known;
470 else if (*li->lex_buf == 'p')
471 p->which = Z_ProximityOperator_private;
473 p->which = atoi(li->lex_buf);
475 if (p->which != Z_ProximityOperator_known
476 && p->which != Z_ProximityOperator_private)
478 li->error = YAZ_PQF_ERROR_PROXIMITY;
484 li->error = YAZ_PQF_ERROR_MISSING;
487 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
488 p->u.known = odr_intdup(o, odr_atoi(li->lex_buf));
491 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
497 static Z_Complex *rpn_complex(struct yaz_pqf_parser *li, ODR o,
498 int num_attr, int max_attr,
499 Odr_int *attr_list, char **attr_clist,
505 zc = (Z_Complex *)odr_malloc(o, sizeof(*zc));
506 zo = (Z_Operator *)odr_malloc(o, sizeof(*zo));
508 switch (li->query_look)
511 zo->which = Z_Operator_and;
512 zo->u.op_and = odr_nullval();
515 zo->which = Z_Operator_or;
516 zo->u.op_or = odr_nullval();
519 zo->which = Z_Operator_and_not;
520 zo->u.and_not = odr_nullval();
523 zo->which = Z_Operator_prox;
524 zo->u.prox = rpn_proximity(li, o);
529 /* we're only called if one of the above types are seens so
530 this shouldn't happen */
531 li->error = YAZ_PQF_ERROR_INTERNAL;
536 rpn_structure(li, o, num_attr, max_attr, attr_list,
537 attr_clist, attr_set)))
540 rpn_structure(li, o, num_attr, max_attr, attr_list,
541 attr_clist, attr_set)))
546 static void rpn_term_type(struct yaz_pqf_parser *li)
550 if (compare_term(li, "general", 0))
551 li->term_type = Z_Term_general;
552 else if (compare_term(li, "numeric", 0))
553 li->term_type = Z_Term_numeric;
554 else if (compare_term(li, "string", 0))
555 li->term_type = Z_Term_characterString;
556 else if (compare_term(li, "oid", 0))
557 li->term_type = Z_Term_oid;
558 else if (compare_term(li, "datetime", 0))
559 li->term_type = Z_Term_dateTime;
560 else if (compare_term(li, "null", 0))
561 li->term_type = Z_Term_null;
563 else if (compare_term(li, "range", 0))
565 /* prepare for external: range search .. */
566 li->term_type = Z_Term_external;
567 li->external_type = VAL_MULTISRCH2;
573 static Z_RPNStructure *rpn_structure(struct yaz_pqf_parser *li, ODR o,
574 int num_attr, int max_attr,
581 sz = (Z_RPNStructure *)odr_malloc(o, sizeof(*sz));
582 switch (li->query_look)
588 sz->which = Z_RPNStructure_complex;
589 if (!(sz->u.complex =
590 rpn_complex(li, o, num_attr, max_attr, attr_list,
591 attr_clist, attr_set)))
596 sz->which = Z_RPNStructure_simple;
598 rpn_simple(li, o, num_attr, attr_list,
599 attr_clist, attr_set)))
606 li->error = YAZ_PQF_ERROR_MISSING;
609 if (num_attr >= max_attr)
611 li->error = YAZ_PQF_ERROR_TOOMANY;
614 if (!p_query_parse_attr(li, o, num_attr, attr_list,
615 attr_clist, attr_set))
620 rpn_structure(li, o, num_attr, max_attr, attr_list,
621 attr_clist, attr_set);
626 rpn_structure(li, o, num_attr, max_attr, attr_list,
627 attr_clist, attr_set);
628 case 0: /* operator/operand expected! */
629 li->error = YAZ_PQF_ERROR_MISSING;
635 static Z_RPNQuery *p_query_rpn_mk(ODR o, struct yaz_pqf_parser *li)
638 Odr_int attr_array[1024];
639 char *attr_clist[512];
640 Odr_oid *attr_set[512];
641 Odr_oid *top_set = 0;
643 zq = (Z_RPNQuery *)odr_malloc(o, sizeof(*zq));
645 if (li->query_look == 'r')
648 top_set = query_oid_getvalbyname(li, o);
651 li->error = YAZ_PQF_ERROR_ATTSET;
658 top_set = odr_oiddup(o, yaz_oid_attset_bib_1);
661 zq->attributeSetId = top_set;
663 if (!zq->attributeSetId)
665 li->error = YAZ_PQF_ERROR_ATTSET;
669 if (!(zq->RPNStructure = rpn_structure(li, o, 0, 512,
670 attr_array, attr_clist, attr_set)))
674 li->error = YAZ_PQF_ERROR_EXTRA;
680 Z_RPNQuery *p_query_rpn(ODR o, const char *qbuf)
682 struct yaz_pqf_parser li;
686 li.right_sep = "}\"";
687 li.escape_char = '@';
688 li.term_type = Z_Term_general;
689 li.query_buf = li.query_ptr = qbuf;
691 return p_query_rpn_mk(o, &li);
695 static Z_AttributeList *p_query_scan_attributes_mk(struct yaz_pqf_parser *li,
697 Odr_oid **attributeSetP)
699 Odr_int attr_list[1024];
700 char *attr_clist[512];
701 Odr_oid *attr_set[512];
704 Odr_oid *top_set = 0;
707 if (li->query_look == 'r')
710 top_set = query_oid_getvalbyname(li, o);
713 li->error = YAZ_PQF_ERROR_ATTSET;
720 top_set = odr_oiddup(o, yaz_oid_attset_bib_1);
722 *attributeSetP = top_set;
726 if (li->query_look == 'l')
731 li->error = YAZ_PQF_ERROR_MISSING;
734 if (num_attr >= max_attr)
736 li->error = YAZ_PQF_ERROR_TOOMANY;
739 if (!p_query_parse_attr(li, o, num_attr, attr_list,
740 attr_clist, attr_set))
745 else if (li->query_look == 'y')
753 return get_attributeList(o, num_attr, attr_list, attr_clist, attr_set);
756 static Z_AttributesPlusTerm *p_query_scan_mk(struct yaz_pqf_parser *li,
758 Odr_oid **attributeSetP)
760 Z_AttributeList *attr_list = p_query_scan_attributes_mk(li, o, attributeSetP);
761 Z_AttributesPlusTerm *apt;
765 li->error = YAZ_PQF_ERROR_MISSING;
768 apt = rpn_term_attributes(li, o, attr_list);
772 if (li->query_look != 0)
774 li->error = YAZ_PQF_ERROR_EXTRA;
780 YAZ_PQF_Parser yaz_pqf_create(void)
782 YAZ_PQF_Parser p = (YAZ_PQF_Parser) xmalloc(sizeof(*p));
786 p->right_sep = "}\"";
787 p->escape_char = '@';
788 p->term_type = Z_Term_general;
793 void yaz_pqf_destroy(YAZ_PQF_Parser p)
798 Z_RPNQuery *yaz_pqf_parse(YAZ_PQF_Parser p, ODR o, const char *qbuf)
802 p->query_buf = p->query_ptr = qbuf;
804 return p_query_rpn_mk(o, p);
807 Z_AttributesPlusTerm *yaz_pqf_scan(YAZ_PQF_Parser p, ODR o,
808 Odr_oid **attributeSetP,
813 p->query_buf = p->query_ptr = qbuf;
815 return p_query_scan_mk(p, o, attributeSetP);
818 Z_AttributeList *yaz_pqf_scan_attribute_list(YAZ_PQF_Parser p, ODR o,
819 Odr_oid **attributeSetP,
824 p->query_buf = p->query_ptr = qbuf;
826 return p_query_scan_attributes_mk(p, o, attributeSetP);
829 static Z_FacetField* parse_facet(ODR odr, const char *facet, int length)
831 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
832 char *buffer = odr_strdupn(odr, facet, length);
833 Odr_oid *attributeSetId;
834 Z_FacetField *facet_field = 0;
835 Z_AttributeList *attribute_list =
836 yaz_pqf_scan_attribute_list(pqf_parser, odr, &attributeSetId, buffer);
840 facet_field = odr_malloc(odr, sizeof(*facet_field));
841 facet_field->attributes = attribute_list;
842 facet_field->num_terms = 0;
843 facet_field->terms = 0;
845 yaz_pqf_destroy(pqf_parser);
849 #define FACET_DElIMITER ','
851 static int scan_facet_argument(const char *arg) {
853 int length = strlen(arg);
855 for (index = 0; index < length; index++) {
856 if (arg[index] == FACET_DElIMITER)
863 * yax_pdg_parse_facet_list: Parses a comma-separated list of AttributeList(s) into a FacetList.
864 * It does not handle the optional facet term(s).
867 Z_FacetList *yaz_pqf_parse_facet_list(ODR odr, const char *facet) {
868 Z_FacetList *facet_list = 0;
869 Z_FacetField **elements;
871 int num_elements = scan_facet_argument(facet);
872 if (num_elements == 0)
874 facet_list = odr_malloc(odr, sizeof(*facet_list));
875 facet_list->num = num_elements;
876 elements = odr_malloc(odr, num_elements * sizeof(*elements));
877 facet_list->elements = elements;
878 for (index = 0; index < num_elements;) {
879 const char *pos = strchr(facet, FACET_DElIMITER);
881 pos = facet + strlen(facet);
882 elements[index] = parse_facet(odr, (const char *) facet, (pos - facet));
883 if (elements[index]) {
888 facet_list->num = num_elements;
897 int yaz_pqf_error(YAZ_PQF_Parser p, const char **msg, size_t *off)
901 case YAZ_PQF_ERROR_NONE:
902 *msg = "no error"; break;
903 case YAZ_PQF_ERROR_EXTRA:
904 *msg = "extra token"; break;
905 case YAZ_PQF_ERROR_MISSING:
906 *msg = "missing token"; break;
907 case YAZ_PQF_ERROR_ATTSET:
908 *msg = "unknown attribute set"; break;
909 case YAZ_PQF_ERROR_TOOMANY:
910 *msg = "too many attributes"; break;
911 case YAZ_PQF_ERROR_BADATTR:
912 *msg = "bad attribute specification"; break;
913 case YAZ_PQF_ERROR_INTERNAL:
914 *msg = "internal error"; break;
915 case YAZ_PQF_ERROR_PROXIMITY:
916 *msg = "proximity error"; break;
917 case YAZ_PQF_ERROR_BAD_INTEGER:
918 *msg = "bad integer"; break;
920 *msg = "unknown error"; break;
922 *off = p->query_ptr - p->query_buf;
928 * c-file-style: "Stroustrup"
929 * indent-tabs-mode: nil
931 * vim: shiftwidth=4 tabstop=8 expandtab