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 CCL parse tree printing
9 * This source file implements functions to parse and print
10 * a CCL node tree (as a result of parsing).
19 #include <yaz/querytowrbuf.h>
20 #include <yaz/yaz-iconv.h>
23 static void ccl_pquery_indent(WRBUF w, struct ccl_rpn_node *p, int indent);
25 static void ccl_pquery_complex(WRBUF w, struct ccl_rpn_node *p, int indent)
27 int sep_char = indent == -1 ? ' ' : '\n';
28 int next_indent = indent == -1 ? indent : indent+1;
32 wrbuf_puts(w, "@and");
38 wrbuf_puts(w, "@not");
41 if (p->u.p[2] && p->u.p[2]->kind == CCL_RPN_TERM)
43 const char *cp = p->u.p[2]->u.t.term;
44 /* exlusion distance ordered relation which-code unit-code */
47 /* word order specified */
48 if (yaz_isdigit(cp[1]))
49 wrbuf_printf(w, "@prox 0 %s 1 2 k 2", cp+1);
51 wrbuf_printf(w, "@prox 0 1 1 2 k 2");
55 /* word order not specified */
56 if (yaz_isdigit(cp[1]))
57 wrbuf_printf(w, "@prox 0 %s 0 2 k 2", cp+1);
59 wrbuf_printf(w, "@prox 0 1 0 2 k 2");
63 wrbuf_puts(w, "@prox 0 2 0 1 k 2");
66 wrbuf_puts(w, "@ bad op (unknown)");
68 wrbuf_putc(w, sep_char);
69 ccl_pquery_indent(w, p->u.p[0], next_indent);
70 ccl_pquery_indent(w, p->u.p[1], next_indent);
73 static void ccl_prterm(WRBUF w, const char *term)
75 yaz_encode_pqf_term(w, term, strlen(term));
78 static void ccl_pquery_indent(WRBUF w, struct ccl_rpn_node *p, int indent)
80 struct ccl_rpn_attr *att;
87 for (i = 0; i < indent; i++)
96 ccl_pquery_complex(w, p, indent);
99 wrbuf_puts(w, "@set ");
100 ccl_prterm(w, p->u.setname);
105 for (att = p->u.t.attr_list; att; att = att->next)
108 wrbuf_puts(w, "@attr ");
111 wrbuf_puts(w, att->set);
116 case CCL_RPN_ATTR_NUMERIC:
117 sprintf(tmpattr, "%d=%d ", att->type, att->value.numeric);
118 wrbuf_puts(w, tmpattr);
120 case CCL_RPN_ATTR_STRING:
121 sprintf(tmpattr, "%d=", att->type);
122 wrbuf_puts(w, tmpattr);
123 wrbuf_puts(w, att->value.str);
128 ccl_prterm(w, p->u.t.term);
135 void ccl_pquery(WRBUF w, struct ccl_rpn_node *p)
137 ccl_pquery_indent(w, p, -1);
140 void ccl_pr_tree(struct ccl_rpn_node *rpn, FILE *fd_out)
142 WRBUF w = wrbuf_alloc();
144 ccl_pquery_indent(w, rpn, 0);
146 fputs(wrbuf_cstr(w), fd_out);
153 * c-file-style: "Stroustrup"
154 * indent-tabs-mode: nil
156 * vim: shiftwidth=4 tabstop=8 expandtab