2 * Copyright (C) 1995-2008, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: cclptree.c,v 1.11 2008-01-09 21:32:27 adam Exp $
9 * \brief Implements CCL parse tree printing
11 * This source file implements functions to parse and print
12 * a CCL node tree (as a result of parsing).
21 static void ccl_pquery_indent(WRBUF w, struct ccl_rpn_node *p, int indent);
23 static void ccl_pquery_complex(WRBUF w, struct ccl_rpn_node *p, int indent)
25 int sep_char = indent == -1 ? ' ' : '\n';
26 int next_indent = indent == -1 ? indent : indent+1;
30 wrbuf_puts(w, "@and");
36 wrbuf_puts(w, "@not");
39 if (p->u.p[2] && p->u.p[2]->kind == CCL_RPN_TERM)
41 const char *cp = p->u.p[2]->u.t.term;
42 /* exlusion distance ordered relation which-code unit-code */
45 /* word order specified */
46 if (isdigit(((const unsigned char *) cp)[1]))
47 wrbuf_printf(w, "@prox 0 %s 1 2 k 2", cp+1);
49 wrbuf_printf(w, "@prox 0 1 1 2 k 2");
53 /* word order not specified */
54 if (isdigit(((const unsigned char *) cp)[1]))
55 wrbuf_printf(w, "@prox 0 %s 0 2 k 2", cp+1);
57 wrbuf_printf(w, "@prox 0 1 0 2 k 2");
61 wrbuf_puts(w, "@prox 0 2 0 1 k 2");
64 wrbuf_puts(w, "@ bad op (unknown)");
66 wrbuf_putc(w, sep_char);
67 ccl_pquery_indent(w, p->u.p[0], next_indent);
68 ccl_pquery_indent(w, p->u.p[1], next_indent);
71 static void ccl_prterm(WRBUF w, const char *term)
74 wrbuf_puts(w, "\"\"");
77 const char *cp = term;
80 if (*cp == ' ' || *cp == '\\')
88 static void ccl_pquery_indent(WRBUF w, struct ccl_rpn_node *p, int indent)
90 struct ccl_rpn_attr *att;
97 for (i = 0; i < indent; i++)
106 ccl_pquery_complex(w, p, indent);
109 wrbuf_puts(w, "@set ");
110 ccl_prterm(w, p->u.setname);
115 for (att = p->u.t.attr_list; att; att = att->next)
118 wrbuf_puts(w, "@attr ");
121 wrbuf_puts(w, att->set);
126 case CCL_RPN_ATTR_NUMERIC:
127 sprintf(tmpattr, "%d=%d ", att->type, att->value.numeric);
128 wrbuf_puts(w, tmpattr);
130 case CCL_RPN_ATTR_STRING:
131 sprintf(tmpattr, "%d=", att->type);
132 wrbuf_puts(w, tmpattr);
133 wrbuf_puts(w, att->value.str);
138 ccl_prterm(w, p->u.t.term);
145 void ccl_pquery(WRBUF w, struct ccl_rpn_node *p)
147 ccl_pquery_indent(w, p, -1);
150 void ccl_pr_tree(struct ccl_rpn_node *rpn, FILE *fd_out)
152 WRBUF w = wrbuf_alloc();
154 ccl_pquery_indent(w, rpn, 0);
156 fputs(wrbuf_cstr(w), fd_out);
163 * indent-tabs-mode: nil
165 * vim: shiftwidth=4 tabstop=8 expandtab