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 = odr_strdupn(o, li->lex_buf, li->lex_len);
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 static void pqf_parser_begin(struct yaz_pqf_parser *li, const char *buf)
682 li->query_buf = li->query_ptr = buf;
686 Z_RPNQuery *p_query_rpn(ODR o, const char *qbuf)
688 struct yaz_pqf_parser li;
692 li.right_sep = "}\"";
693 li.escape_char = '@';
694 li.term_type = Z_Term_general;
696 pqf_parser_begin(&li, qbuf);
697 return p_query_rpn_mk(o, &li);
700 static Z_AttributeList *p_query_scan_attributes_mk(struct yaz_pqf_parser *li,
702 Odr_oid **attributeSetP)
704 Odr_int attr_list[1024];
705 char *attr_clist[512];
706 Odr_oid *attr_set[512];
709 Odr_oid *top_set = 0;
712 if (li->query_look == 'r')
715 top_set = query_oid_getvalbyname(li, o);
718 li->error = YAZ_PQF_ERROR_ATTSET;
725 top_set = odr_oiddup(o, yaz_oid_attset_bib_1);
727 *attributeSetP = top_set;
731 if (li->query_look == 'l')
736 li->error = YAZ_PQF_ERROR_MISSING;
739 if (num_attr >= max_attr)
741 li->error = YAZ_PQF_ERROR_TOOMANY;
744 if (!p_query_parse_attr(li, o, num_attr, attr_list,
745 attr_clist, attr_set))
750 else if (li->query_look == 'y')
758 return get_attributeList(o, num_attr, attr_list, attr_clist, attr_set);
761 static Z_AttributesPlusTerm *p_query_scan_mk(struct yaz_pqf_parser *li,
763 Odr_oid **attributeSetP)
765 Z_AttributeList *attr_list = p_query_scan_attributes_mk(li, o, attributeSetP);
766 Z_AttributesPlusTerm *apt;
770 li->error = YAZ_PQF_ERROR_MISSING;
773 apt = rpn_term_attributes(li, o, attr_list);
777 if (li->query_look != 0)
779 li->error = YAZ_PQF_ERROR_EXTRA;
785 YAZ_PQF_Parser yaz_pqf_create(void)
787 YAZ_PQF_Parser p = (YAZ_PQF_Parser) xmalloc(sizeof(*p));
791 p->right_sep = "}\"";
792 p->escape_char = '@';
793 p->term_type = Z_Term_general;
798 void yaz_pqf_destroy(YAZ_PQF_Parser p)
803 Z_RPNQuery *yaz_pqf_parse(YAZ_PQF_Parser p, ODR o, const char *qbuf)
807 pqf_parser_begin(p, qbuf);
808 return p_query_rpn_mk(o, p);
811 Z_AttributesPlusTerm *yaz_pqf_scan(YAZ_PQF_Parser p, ODR o,
812 Odr_oid **attributeSetP,
817 pqf_parser_begin(p, qbuf);
818 return p_query_scan_mk(p, o, attributeSetP);
821 Z_AttributeList *yaz_pqf_scan_attribute_list(YAZ_PQF_Parser p, ODR o,
822 Odr_oid **attributeSetP,
827 pqf_parser_begin(p, qbuf);
828 return p_query_scan_attributes_mk(p, o, attributeSetP);
831 static Z_FacetField* parse_facet(ODR odr, const char *facet)
833 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
834 struct yaz_pqf_parser *li = pqf_parser;
835 Odr_oid *attributeSetId;
836 Z_FacetField *facet_field = 0;
837 Z_AttributeList *attribute_list;
839 pqf_parser_begin(pqf_parser, facet);
840 attribute_list = p_query_scan_attributes_mk(li, odr, &attributeSetId);
843 facet_field = (Z_FacetField *) odr_malloc(odr, sizeof(*facet_field));
844 facet_field->attributes = attribute_list;
845 facet_field->num_terms = 0;
846 facet_field->terms = odr_malloc(odr, 10 * sizeof(*facet_field->terms));
847 while (li->query_look == 't')
849 if (facet_field->num_terms < 10)
851 char *es_str = odr_malloc(odr, li->lex_len+1);
852 int es_len = escape_string(es_str, li->lex_buf, li->lex_len);
853 Z_Term *term = z_Term_create(odr, li->term_type, es_str, es_len);
855 facet_field->terms[facet_field->num_terms] =
856 (Z_FacetTerm *) odr_malloc(odr, sizeof(Z_FacetTerm));
857 facet_field->terms[facet_field->num_terms]->term = term;
858 facet_field->terms[facet_field->num_terms]->count =
860 facet_field->num_terms++;
865 yaz_pqf_destroy(pqf_parser);
869 Z_FacetList *yaz_pqf_parse_facet_list(ODR o, const char *qbuf)
874 nmem_strsplit(odr_getmem(o), ",", qbuf, &darray, &num);
878 Z_FacetList *fl = (Z_FacetList*) odr_malloc(o, sizeof(*fl));
880 fl->elements = (Z_FacetField **)
881 odr_malloc(o, num * sizeof(*fl->elements));
882 for (i = 0; i < num; i++)
884 fl->elements[i] = parse_facet(o, darray[i]);
885 if (!fl->elements[i])
894 int yaz_pqf_error(YAZ_PQF_Parser p, const char **msg, size_t *off)
898 case YAZ_PQF_ERROR_NONE:
899 *msg = "no error"; break;
900 case YAZ_PQF_ERROR_EXTRA:
901 *msg = "extra token"; break;
902 case YAZ_PQF_ERROR_MISSING:
903 *msg = "missing token"; break;
904 case YAZ_PQF_ERROR_ATTSET:
905 *msg = "unknown attribute set"; break;
906 case YAZ_PQF_ERROR_TOOMANY:
907 *msg = "too many attributes"; break;
908 case YAZ_PQF_ERROR_BADATTR:
909 *msg = "bad attribute specification"; break;
910 case YAZ_PQF_ERROR_INTERNAL:
911 *msg = "internal error"; break;
912 case YAZ_PQF_ERROR_PROXIMITY:
913 *msg = "proximity error"; break;
914 case YAZ_PQF_ERROR_BAD_INTEGER:
915 *msg = "bad integer"; break;
917 *msg = "unknown error"; break;
919 *off = p->query_ptr - p->query_buf;
925 * c-file-style: "Stroustrup"
926 * indent-tabs-mode: nil
928 * vim: shiftwidth=4 tabstop=8 expandtab