4 * LTTng filter XML pretty printer visitor
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "filter-ast.h"
30 #include "filter-parser.h"
32 #include <common/macros.h>
34 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
37 int recursive_visit_print(struct filter_node
*node
, FILE *stream
, int indent
);
40 void print_tabs(FILE *fd
, int depth
)
44 for (i
= 0; i
< depth
; i
++)
49 int recursive_visit_print_expression(struct filter_node
*node
,
50 FILE *stream
, int indent
)
53 fprintf(stderr
, "[error] %s: NULL child\n", __func__
);
56 switch (node
->u
.expression
.type
) {
59 fprintf(stderr
, "[error] %s: unknown expression\n", __func__
);
62 print_tabs(stream
, indent
);
63 fprintf(stream
, "<string value=\"%s\"/>\n",
64 node
->u
.expression
.u
.string
);
66 case AST_EXP_CONSTANT
:
67 print_tabs(stream
, indent
);
68 fprintf(stream
, "<constant value=\"%" PRIu64
"\"/>\n",
69 node
->u
.expression
.u
.constant
);
71 case AST_EXP_FLOAT_CONSTANT
:
72 print_tabs(stream
, indent
);
73 fprintf(stream
, "<float_constant value=\"%lg\"/>\n",
74 node
->u
.expression
.u
.float_constant
);
76 case AST_EXP_IDENTIFIER
: /* fall-through */
77 case AST_EXP_GLOBAL_IDENTIFIER
:
78 print_tabs(stream
, indent
);
79 fprintf(stream
, "<%s value=\"%s\"/>\n",
80 node
->u
.expression
.type
== AST_EXP_IDENTIFIER
?
81 "identifier" : "global_identifier",
82 node
->u
.expression
.u
.identifier
);
83 while (node
->u
.expression
.next
) {
84 print_tabs(stream
, indent
);
85 fprintf(stream
, "<link type=\"");
86 switch (node
->u
.expression
.pre_op
) {
87 case AST_LINK_UNKNOWN
:
89 fprintf(stderr
, "[error] %s: unknown link\n", __func__
);
95 fprintf(stream
, "->");
98 fprintf(stream
, "\"/>\n");
100 node
= node
->u
.expression
.next
;
101 if (node
->type
!= NODE_EXPRESSION
||
102 node
->u
.expression
.type
!= AST_EXP_IDENTIFIER
) {
103 fprintf(stderr
, "[error] %s: expecting identifier before link\n", __func__
);
107 print_tabs(stream
, indent
);
108 fprintf(stream
, "<identifier value=\"%s\"/>\n",
109 node
->u
.expression
.u
.identifier
);
113 return recursive_visit_print(node
->u
.expression
.u
.child
,
121 int recursive_visit_print(struct filter_node
*node
, FILE *stream
, int indent
)
126 fprintf(stderr
, "[error] %s: NULL child\n", __func__
);
129 switch (node
->type
) {
132 fprintf(stderr
, "[error] %s: unknown node type\n", __func__
);
135 print_tabs(stream
, indent
);
136 fprintf(stream
, "<root>\n");
137 ret
= recursive_visit_print(node
->u
.root
.child
, stream
,
139 print_tabs(stream
, indent
);
140 fprintf(stream
, "</root>\n");
142 case NODE_EXPRESSION
:
143 print_tabs(stream
, indent
);
144 fprintf(stream
, "<expression>\n");
145 ret
= recursive_visit_print_expression(node
, stream
,
147 print_tabs(stream
, indent
);
148 fprintf(stream
, "</expression>\n");
151 print_tabs(stream
, indent
);
152 fprintf(stream
, "<op type=");
153 switch (node
->u
.op
.type
) {
156 fprintf(stderr
, "[error] %s: unknown op\n", __func__
);
159 fprintf(stream
, "\"*\"");
162 fprintf(stream
, "\"/\"");
165 fprintf(stream
, "\"%%\"");
168 fprintf(stream
, "\"+\"");
171 fprintf(stream
, "\"-\"");
174 fprintf(stream
, "\">>\"");
177 fprintf(stream
, "\"<<\"");
180 fprintf(stream
, "\"&&\"");
183 fprintf(stream
, "\"||\"");
186 fprintf(stream
, "\"&\"");
189 fprintf(stream
, "\"|\"");
192 fprintf(stream
, "\"^\"");
196 fprintf(stream
, "\"==\"");
199 fprintf(stream
, "\"!=\"");
202 fprintf(stream
, "\">\"");
205 fprintf(stream
, "\"<\"");
208 fprintf(stream
, "\">=\"");
211 fprintf(stream
, "\"<=\"");
214 fprintf(stream
, ">\n");
215 ret
= recursive_visit_print(node
->u
.op
.lchild
,
219 ret
= recursive_visit_print(node
->u
.op
.rchild
,
223 print_tabs(stream
, indent
);
224 fprintf(stream
, "</op>\n");
227 print_tabs(stream
, indent
);
228 fprintf(stream
, "<unary_op type=");
229 switch (node
->u
.unary_op
.type
) {
230 case AST_UNARY_UNKNOWN
:
232 fprintf(stderr
, "[error] %s: unknown unary_op\n", __func__
);
235 fprintf(stream
, "\"+\"");
237 case AST_UNARY_MINUS
:
238 fprintf(stream
, "\"-\"");
241 fprintf(stream
, "\"!\"");
243 case AST_UNARY_BIN_NOT
:
244 fprintf(stream
, "\"~\"");
247 fprintf(stream
, ">\n");
248 ret
= recursive_visit_print(node
->u
.unary_op
.child
,
250 print_tabs(stream
, indent
);
251 fprintf(stream
, "</unary_op>\n");
258 int filter_visitor_print_xml(struct filter_parser_ctx
*ctx
, FILE *stream
,
261 return recursive_visit_print(&ctx
->ast
->root
, stream
, indent
);