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));
316 term->which = Z_Term_general;
317 term->u.general = odr_create_Odr_oct(o, buf, len);
319 case Z_Term_characterString:
320 term->which = Z_Term_characterString;
321 term->u.characterString = odr_strdupn(o, buf, len);
324 term->which = Z_Term_numeric;
325 term->u.numeric = odr_intdup(o, odr_atoi(odr_strdupn(o, buf, len)));
328 term->which = Z_Term_null;
329 term->u.null = odr_nullval();
331 case Z_Term_external:
332 term->which = Z_Term_external;
333 term->u.external = 0;
336 term->which = Z_Term_null;
337 term->u.null = odr_nullval();
343 static Z_AttributesPlusTerm *rpn_term_attributes(
344 struct yaz_pqf_parser *li, ODR o, Z_AttributeList *attributes)
346 char *es_str = odr_malloc(o, li->lex_len+1);
347 int es_len = escape_string(es_str, li->lex_buf, li->lex_len);
348 Z_Term *term = z_Term_create(o, li->term_type, es_str, es_len);
349 Z_AttributesPlusTerm *zapt = (Z_AttributesPlusTerm *)
350 odr_malloc(o, sizeof(*zapt));
353 zapt->attributes = attributes;
357 static Z_AttributesPlusTerm *rpn_term(struct yaz_pqf_parser *li, ODR o,
358 int num_attr, Odr_int *attr_list,
359 char **attr_clist, Odr_oid **attr_set)
361 return rpn_term_attributes(li, o, get_attributeList(o, num_attr, attr_list, attr_clist, attr_set));
364 static Z_Operand *rpn_simple(struct yaz_pqf_parser *li, ODR o,
365 int num_attr, Odr_int *attr_list,
371 zo = (Z_Operand *)odr_malloc(o, sizeof(*zo));
372 switch (li->query_look)
375 zo->which = Z_Operand_APT;
376 if (!(zo->u.attributesPlusTerm =
377 rpn_term(li, o, num_attr, attr_list, attr_clist, attr_set)))
385 li->error = YAZ_PQF_ERROR_MISSING;
388 zo->which = Z_Operand_resultSetId;
389 zo->u.resultSetId = (char *)odr_malloc(o, li->lex_len+1);
390 memcpy(zo->u.resultSetId, li->lex_buf, li->lex_len);
391 zo->u.resultSetId[li->lex_len] = '\0';
395 /* we're only called if one of the above types are seens so
396 this shouldn't happen */
397 li->error = YAZ_PQF_ERROR_INTERNAL;
403 static Z_ProximityOperator *rpn_proximity(struct yaz_pqf_parser *li, ODR o)
405 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc(o, sizeof(*p));
409 li->error = YAZ_PQF_ERROR_MISSING;
412 if (*li->lex_buf == '1')
413 p->exclusion = odr_booldup(o, 1);
414 else if (*li->lex_buf == '0')
415 p->exclusion = odr_booldup(o, 0);
416 else if (*li->lex_buf == 'v' || *li->lex_buf == 'n')
420 li->error = YAZ_PQF_ERROR_PROXIMITY;
426 li->error = YAZ_PQF_ERROR_MISSING;
429 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
430 p->distance = odr_intdup(o, odr_atoi(li->lex_buf));
433 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
439 li->error = YAZ_PQF_ERROR_MISSING;
442 if (*li->lex_buf == '1')
443 p->ordered = odr_booldup(o, 1);
444 else if (*li->lex_buf == '0')
445 p->ordered = odr_booldup(o, 0);
448 li->error = YAZ_PQF_ERROR_PROXIMITY;
454 li->error = YAZ_PQF_ERROR_MISSING;
457 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
458 p->relationType = odr_intdup(o, odr_atoi(li->lex_buf));
461 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
467 li->error = YAZ_PQF_ERROR_MISSING;
470 if (*li->lex_buf == 'k')
471 p->which = Z_ProximityOperator_known;
472 else if (*li->lex_buf == 'p')
473 p->which = Z_ProximityOperator_private;
475 p->which = atoi(li->lex_buf);
477 if (p->which != Z_ProximityOperator_known
478 && p->which != Z_ProximityOperator_private)
480 li->error = YAZ_PQF_ERROR_PROXIMITY;
486 li->error = YAZ_PQF_ERROR_MISSING;
489 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
490 p->u.known = odr_intdup(o, odr_atoi(li->lex_buf));
493 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
499 static Z_Complex *rpn_complex(struct yaz_pqf_parser *li, ODR o,
500 int num_attr, int max_attr,
501 Odr_int *attr_list, char **attr_clist,
507 zc = (Z_Complex *)odr_malloc(o, sizeof(*zc));
508 zo = (Z_Operator *)odr_malloc(o, sizeof(*zo));
510 switch (li->query_look)
513 zo->which = Z_Operator_and;
514 zo->u.op_and = odr_nullval();
517 zo->which = Z_Operator_or;
518 zo->u.op_or = odr_nullval();
521 zo->which = Z_Operator_and_not;
522 zo->u.and_not = odr_nullval();
525 zo->which = Z_Operator_prox;
526 zo->u.prox = rpn_proximity(li, o);
531 /* we're only called if one of the above types are seens so
532 this shouldn't happen */
533 li->error = YAZ_PQF_ERROR_INTERNAL;
538 rpn_structure(li, o, num_attr, max_attr, attr_list,
539 attr_clist, attr_set)))
542 rpn_structure(li, o, num_attr, max_attr, attr_list,
543 attr_clist, attr_set)))
548 static void rpn_term_type(struct yaz_pqf_parser *li)
552 if (compare_term(li, "general", 0))
553 li->term_type = Z_Term_general;
554 else if (compare_term(li, "numeric", 0))
555 li->term_type = Z_Term_numeric;
556 else if (compare_term(li, "string", 0))
557 li->term_type = Z_Term_characterString;
558 else if (compare_term(li, "oid", 0))
559 li->term_type = Z_Term_oid;
560 else if (compare_term(li, "datetime", 0))
561 li->term_type = Z_Term_dateTime;
562 else if (compare_term(li, "null", 0))
563 li->term_type = Z_Term_null;
565 else if (compare_term(li, "range", 0))
567 /* prepare for external: range search .. */
568 li->term_type = Z_Term_external;
569 li->external_type = VAL_MULTISRCH2;
575 static Z_RPNStructure *rpn_structure(struct yaz_pqf_parser *li, ODR o,
576 int num_attr, int max_attr,
583 sz = (Z_RPNStructure *)odr_malloc(o, sizeof(*sz));
584 switch (li->query_look)
590 sz->which = Z_RPNStructure_complex;
591 if (!(sz->u.complex =
592 rpn_complex(li, o, num_attr, max_attr, attr_list,
593 attr_clist, attr_set)))
598 sz->which = Z_RPNStructure_simple;
600 rpn_simple(li, o, num_attr, attr_list,
601 attr_clist, attr_set)))
608 li->error = YAZ_PQF_ERROR_MISSING;
611 if (num_attr >= max_attr)
613 li->error = YAZ_PQF_ERROR_TOOMANY;
616 if (!p_query_parse_attr(li, o, num_attr, attr_list,
617 attr_clist, attr_set))
622 rpn_structure(li, o, num_attr, max_attr, attr_list,
623 attr_clist, attr_set);
628 rpn_structure(li, o, num_attr, max_attr, attr_list,
629 attr_clist, attr_set);
630 case 0: /* operator/operand expected! */
631 li->error = YAZ_PQF_ERROR_MISSING;
637 static Z_RPNQuery *p_query_rpn_mk(ODR o, struct yaz_pqf_parser *li)
640 Odr_int attr_array[1024];
641 char *attr_clist[512];
642 Odr_oid *attr_set[512];
643 Odr_oid *top_set = 0;
645 zq = (Z_RPNQuery *)odr_malloc(o, sizeof(*zq));
647 if (li->query_look == 'r')
650 top_set = query_oid_getvalbyname(li, o);
653 li->error = YAZ_PQF_ERROR_ATTSET;
660 top_set = odr_oiddup(o, yaz_oid_attset_bib_1);
663 zq->attributeSetId = top_set;
665 if (!zq->attributeSetId)
667 li->error = YAZ_PQF_ERROR_ATTSET;
671 if (!(zq->RPNStructure = rpn_structure(li, o, 0, 512,
672 attr_array, attr_clist, attr_set)))
676 li->error = YAZ_PQF_ERROR_EXTRA;
682 static void pqf_parser_begin(struct yaz_pqf_parser *li, const char *buf)
684 li->query_buf = li->query_ptr = buf;
688 Z_RPNQuery *p_query_rpn(ODR o, const char *qbuf)
690 struct yaz_pqf_parser li;
694 li.right_sep = "}\"";
695 li.escape_char = '@';
696 li.term_type = Z_Term_general;
698 pqf_parser_begin(&li, qbuf);
699 return p_query_rpn_mk(o, &li);
702 static Z_AttributeList *p_query_scan_attributes_mk(struct yaz_pqf_parser *li,
704 Odr_oid **attributeSetP)
706 Odr_int attr_list[1024];
707 char *attr_clist[512];
708 Odr_oid *attr_set[512];
711 Odr_oid *top_set = 0;
714 if (li->query_look == 'r')
717 top_set = query_oid_getvalbyname(li, o);
720 li->error = YAZ_PQF_ERROR_ATTSET;
727 top_set = odr_oiddup(o, yaz_oid_attset_bib_1);
729 *attributeSetP = top_set;
733 if (li->query_look == 'l')
738 li->error = YAZ_PQF_ERROR_MISSING;
741 if (num_attr >= max_attr)
743 li->error = YAZ_PQF_ERROR_TOOMANY;
746 if (!p_query_parse_attr(li, o, num_attr, attr_list,
747 attr_clist, attr_set))
752 else if (li->query_look == 'y')
760 return get_attributeList(o, num_attr, attr_list, attr_clist, attr_set);
763 static Z_AttributesPlusTerm *p_query_scan_mk(struct yaz_pqf_parser *li,
765 Odr_oid **attributeSetP)
767 Z_AttributeList *attr_list = p_query_scan_attributes_mk(li, o, attributeSetP);
768 Z_AttributesPlusTerm *apt;
772 li->error = YAZ_PQF_ERROR_MISSING;
775 apt = rpn_term_attributes(li, o, attr_list);
779 if (li->query_look != 0)
781 li->error = YAZ_PQF_ERROR_EXTRA;
787 YAZ_PQF_Parser yaz_pqf_create(void)
789 YAZ_PQF_Parser p = (YAZ_PQF_Parser) xmalloc(sizeof(*p));
793 p->right_sep = "}\"";
794 p->escape_char = '@';
795 p->term_type = Z_Term_general;
800 void yaz_pqf_destroy(YAZ_PQF_Parser p)
805 Z_RPNQuery *yaz_pqf_parse(YAZ_PQF_Parser p, ODR o, const char *qbuf)
809 pqf_parser_begin(p, qbuf);
810 return p_query_rpn_mk(o, p);
813 Z_AttributesPlusTerm *yaz_pqf_scan(YAZ_PQF_Parser p, ODR o,
814 Odr_oid **attributeSetP,
819 pqf_parser_begin(p, qbuf);
820 return p_query_scan_mk(p, o, attributeSetP);
823 Z_AttributeList *yaz_pqf_scan_attribute_list(YAZ_PQF_Parser p, ODR o,
824 Odr_oid **attributeSetP,
829 pqf_parser_begin(p, qbuf);
830 return p_query_scan_attributes_mk(p, o, attributeSetP);
833 static Z_FacetField* parse_facet(ODR odr, const char *facet)
835 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
836 struct yaz_pqf_parser *li = pqf_parser;
837 Odr_oid *attributeSetId;
838 Z_FacetField *facet_field = 0;
839 Z_AttributeList *attribute_list;
841 pqf_parser_begin(pqf_parser, facet);
842 attribute_list = p_query_scan_attributes_mk(li, odr, &attributeSetId);
845 facet_field = (Z_FacetField *) odr_malloc(odr, sizeof(*facet_field));
846 facet_field->attributes = attribute_list;
847 facet_field->num_terms = 0;
848 facet_field->terms = odr_malloc(odr, 10 * sizeof(*facet_field->terms));
849 while (li->query_look == 't')
851 if (facet_field->num_terms < 10)
853 char *es_str = odr_malloc(odr, li->lex_len+1);
854 int es_len = escape_string(es_str, li->lex_buf, li->lex_len);
855 Z_Term *term = z_Term_create(odr, li->term_type, es_str, es_len);
857 facet_field->terms[facet_field->num_terms] =
858 (Z_FacetTerm *) odr_malloc(odr, sizeof(Z_FacetTerm));
859 facet_field->terms[facet_field->num_terms]->term = term;
860 facet_field->terms[facet_field->num_terms]->count =
862 facet_field->num_terms++;
867 yaz_pqf_destroy(pqf_parser);
871 Z_FacetList *yaz_pqf_parse_facet_list(ODR o, const char *qbuf)
876 nmem_strsplit(odr_getmem(o), ",", qbuf, &darray, &num);
880 Z_FacetList *fl = (Z_FacetList*) odr_malloc(o, sizeof(*fl));
882 fl->elements = (Z_FacetField **)
883 odr_malloc(o, num * sizeof(*fl->elements));
884 for (i = 0; i < num; i++)
886 fl->elements[i] = parse_facet(o, darray[i]);
887 if (!fl->elements[i])
896 int yaz_pqf_error(YAZ_PQF_Parser p, const char **msg, size_t *off)
900 case YAZ_PQF_ERROR_NONE:
901 *msg = "no error"; break;
902 case YAZ_PQF_ERROR_EXTRA:
903 *msg = "extra token"; break;
904 case YAZ_PQF_ERROR_MISSING:
905 *msg = "missing token"; break;
906 case YAZ_PQF_ERROR_ATTSET:
907 *msg = "unknown attribute set"; break;
908 case YAZ_PQF_ERROR_TOOMANY:
909 *msg = "too many attributes"; break;
910 case YAZ_PQF_ERROR_BADATTR:
911 *msg = "bad attribute specification"; break;
912 case YAZ_PQF_ERROR_INTERNAL:
913 *msg = "internal error"; break;
914 case YAZ_PQF_ERROR_PROXIMITY:
915 *msg = "proximity error"; break;
916 case YAZ_PQF_ERROR_BAD_INTEGER:
917 *msg = "bad integer"; break;
919 *msg = "unknown error"; break;
921 *off = p->query_ptr - p->query_buf;
927 * c-file-style: "Stroustrup"
928 * indent-tabs-mode: nil
930 * vim: shiftwidth=4 tabstop=8 expandtab