1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements CCL parse tree printing
9 * This source file implements functions to parse and print
10 * a CCL node tree (as a result of parsing).
17 #include <yaz/querytowrbuf.h>
20 static void ccl_pquery_indent(WRBUF w, struct ccl_rpn_node *p, int indent);
22 static void ccl_pquery_complex(WRBUF w, struct ccl_rpn_node *p, int indent)
24 int sep_char = indent == -1 ? ' ' : '\n';
25 int next_indent = indent == -1 ? indent : indent+1;
29 wrbuf_puts(w, "@and");
35 wrbuf_puts(w, "@not");
38 if (p->u.p[2] && p->u.p[2]->kind == CCL_RPN_TERM)
40 const char *cp = p->u.p[2]->u.t.term;
41 /* exlusion distance ordered relation which-code unit-code */
44 /* word order specified */
45 if (isdigit(((const unsigned char *) cp)[1]))
46 wrbuf_printf(w, "@prox 0 %s 1 2 k 2", cp+1);
48 wrbuf_printf(w, "@prox 0 1 1 2 k 2");
52 /* word order not specified */
53 if (isdigit(((const unsigned char *) cp)[1]))
54 wrbuf_printf(w, "@prox 0 %s 0 2 k 2", cp+1);
56 wrbuf_printf(w, "@prox 0 1 0 2 k 2");
60 wrbuf_puts(w, "@prox 0 2 0 1 k 2");
63 wrbuf_puts(w, "@ bad op (unknown)");
65 wrbuf_putc(w, sep_char);
66 ccl_pquery_indent(w, p->u.p[0], next_indent);
67 ccl_pquery_indent(w, p->u.p[1], next_indent);
70 static void ccl_prterm(WRBUF w, const char *term)
72 yaz_encode_pqf_term(w, term, strlen(term));
75 static void ccl_pquery_indent(WRBUF w, struct ccl_rpn_node *p, int indent)
77 struct ccl_rpn_attr *att;
84 for (i = 0; i < indent; i++)
93 ccl_pquery_complex(w, p, indent);
96 wrbuf_puts(w, "@set ");
97 ccl_prterm(w, p->u.setname);
102 for (att = p->u.t.attr_list; att; att = att->next)
105 wrbuf_puts(w, "@attr ");
108 wrbuf_puts(w, att->set);
113 case CCL_RPN_ATTR_NUMERIC:
114 sprintf(tmpattr, "%d=%d ", att->type, att->value.numeric);
115 wrbuf_puts(w, tmpattr);
117 case CCL_RPN_ATTR_STRING:
118 sprintf(tmpattr, "%d=", att->type);
119 wrbuf_puts(w, tmpattr);
120 wrbuf_puts(w, att->value.str);
125 ccl_prterm(w, p->u.t.term);
132 void ccl_pquery(WRBUF w, struct ccl_rpn_node *p)
134 ccl_pquery_indent(w, p, -1);
137 void ccl_pr_tree(struct ccl_rpn_node *rpn, FILE *fd_out)
139 WRBUF w = wrbuf_alloc();
141 ccl_pquery_indent(w, rpn, 0);
143 fputs(wrbuf_cstr(w), fd_out);
150 * indent-tabs-mode: nil
152 * vim: shiftwidth=4 tabstop=8 expandtab