01: /*
02: * xtc - The eXTensible Compiler
03: * Copyright (C) 2007 Robert Grimm
04: *
05: * This program is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU General Public License
07: * version 2 as published by the Free Software Foundation.
08: *
09: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with this program; if not, write to the Free Software
16: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17: * USA.
18: */
19: package xtc.tree;
20:
21: /**
22: * Visitor to strip parse trees. This visitor eliminates any
23: * formatting and tokens, replacing the former with the annotated node
24: * and the latter with the text.
25: *
26: * @author Robert Grimm
27: * @version $Revision: 1.2 $
28: */
29: public class ParseTreeStripper extends Visitor {
30:
31: /** Create a new parse tree stripper. */
32: public ParseTreeStripper() {
33: // Nothing to do.
34: }
35:
36: /** Visit the specified generic node. */
37: public GNode visit(GNode n) {
38: final int size = n.size();
39: for (int i = 0; i < size; i++) {
40: Object o = n.get(i);
41: if (o instanceof Node) {
42: o = dispatch((Node) o);
43: }
44: n.set(i, o);
45: }
46: return n;
47: }
48:
49: /** Visit the specified annotation. */
50: public Annotation visit(Annotation n) {
51: // Preserve the annotation.
52: n.setNode((Node) dispatch(n.getNode()));
53: return n;
54: }
55:
56: /** Visit the specified formatting. */
57: public Object visit(Formatting f) {
58: // Strip the formatting. Note that the returned object may be a
59: // node or a string.
60: return dispatch(f.getNode());
61: }
62:
63: /** Visit the specified token. */
64: public String visit(Token t) {
65: // Strip the token.
66: return t.getTokenText();
67: }
68:
69: }
|