001/* A visitor that writes parse trees.
002
003 Copyright (c) 2002-2014 The Regents of the University of California
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA OR RESEARCH IN MOTION
012 LIMITED BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
013 INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS
014 SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA
015 OR RESEARCH IN MOTION LIMITED HAVE BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA AND RESEARCH IN MOTION LIMITED
019 SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
021 PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
022 BASIS, AND THE UNIVERSITY OF CALIFORNIA AND RESEARCH IN MOTION
023 LIMITED HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
024 ENHANCEMENTS, OR MODIFICATIONS.
025
026
027 */
028package ptolemy.data.expr;
029
030import java.io.PrintStream;
031
032import ptolemy.kernel.util.IllegalActionException;
033
034///////////////////////////////////////////////////////////////////
035//// ParseTreeDumper
036
037/**
038 This class implements a visitor that writes parse trees in a
039 debug format.
040
041 @author Steve Neuendorffer
042 @version $Id$
043 @since Ptolemy II 2.1
044 @Pt.ProposedRating Red (neuendor)
045 @Pt.AcceptedRating Red (cxh)
046 @see ptolemy.data.expr.ASTPtRootNode
047 */
048public class ParseTreeDumper extends AbstractParseTreeVisitor {
049    /** Print the contents of a parse tree.
050     *  @param root The parse tree to be displayed.
051     */
052    public void displayParseTree(ASTPtRootNode root) {
053        _prefix = "";
054
055        try {
056            root.visit(this);
057        } catch (IllegalActionException ex) {
058            _stream.println(ex);
059            ex.printStackTrace(_stream);
060        }
061    }
062
063    ///////////////////////////////////////////////////////////////////
064    ////                         public methods                    ////
065    @Override
066    public void visitArrayConstructNode(ASTPtArrayConstructNode node)
067            throws IllegalActionException {
068        _displayNode(node);
069    }
070
071    @Override
072    public void visitBitwiseNode(ASTPtBitwiseNode node)
073            throws IllegalActionException {
074        _displayNode(node);
075    }
076
077    @Override
078    public void visitFunctionApplicationNode(ASTPtFunctionApplicationNode node)
079            throws IllegalActionException {
080        _displayNode(node);
081    }
082
083    @Override
084    public void visitFunctionDefinitionNode(ASTPtFunctionDefinitionNode node)
085            throws IllegalActionException {
086        _displayNode(node);
087    }
088
089    @Override
090    public void visitFunctionalIfNode(ASTPtFunctionalIfNode node)
091            throws IllegalActionException {
092        _displayNode(node);
093    }
094
095    @Override
096    public void visitLeafNode(ASTPtLeafNode node)
097            throws IllegalActionException {
098        _displayNode(node);
099    }
100
101    @Override
102    public void visitLogicalNode(ASTPtLogicalNode node)
103            throws IllegalActionException {
104        _displayNode(node);
105    }
106
107    @Override
108    public void visitMatrixConstructNode(ASTPtMatrixConstructNode node)
109            throws IllegalActionException {
110        _displayNode(node);
111    }
112
113    @Override
114    public void visitMethodCallNode(ASTPtMethodCallNode node)
115            throws IllegalActionException {
116        _displayNode(node);
117    }
118
119    @Override
120    public void visitPowerNode(ASTPtPowerNode node)
121            throws IllegalActionException {
122        _displayNode(node);
123    }
124
125    @Override
126    public void visitProductNode(ASTPtProductNode node)
127            throws IllegalActionException {
128        _displayNode(node);
129    }
130
131    @Override
132    public void visitRecordConstructNode(ASTPtRecordConstructNode node)
133            throws IllegalActionException {
134        _displayNode(node);
135    }
136
137    @Override
138    public void visitRelationalNode(ASTPtRelationalNode node)
139            throws IllegalActionException {
140        _displayNode(node);
141    }
142
143    @Override
144    public void visitShiftNode(ASTPtShiftNode node)
145            throws IllegalActionException {
146        _displayNode(node);
147    }
148
149    @Override
150    public void visitSumNode(ASTPtSumNode node) throws IllegalActionException {
151        _displayNode(node);
152    }
153
154    @Override
155    public void visitUnaryNode(ASTPtUnaryNode node)
156            throws IllegalActionException {
157        _displayNode(node);
158    }
159
160    /** Display the given node with the current prefix, recursing into
161     *  the children of the node.
162     *  @param node The node to be displayed.
163     *  @exception IllegalActionException If there is a problem
164     *  displaying the children.
165     */
166    protected void _displayNode(ASTPtRootNode node)
167            throws IllegalActionException {
168        if (node.isEvaluated()) {
169            String str = node.toString(_prefix) + ", Token type: ";
170            str = str + node.getToken().getClass().getName() + ", Value: ";
171            _stream.println(str + node.getToken().toString());
172        } else {
173            _stream.println(node.toString(_prefix) + "  _ptToken is null");
174        }
175
176        _stream.println(" static type is " + node.getType());
177
178        if (node.jjtGetNumChildren() > 0) {
179            String oldPrefix = _prefix;
180            _prefix = " " + oldPrefix;
181
182            for (int i = 0; i < node.jjtGetNumChildren(); i++) {
183                ASTPtRootNode child = (ASTPtRootNode) node.jjtGetChild(i);
184                child.visit(this);
185            }
186
187            _prefix = oldPrefix;
188        }
189    }
190
191    private String _prefix;
192
193    private PrintStream _stream = System.out;
194}