001/* PtLeafNode represents leaf nodes in the parse tree.
002
003 Copyright (c) 1998-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 BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027
028 Created : May 1998
029
030 */
031package ptolemy.data.expr;
032
033import java.util.Map;
034
035import ptolemy.kernel.util.IllegalActionException;
036
037//////////////////////////////////////////////////////////////////////////
038//// ASTPtLeafNode
039
040/**
041 The parse tree created from the expression string consists of a
042 hierarchy of node objects. This class represents the leaf nodes of the
043 tree.
044
045 @author Neil Smyth
046 @version $Id$
047 @since Ptolemy II 0.2
048 @Pt.ProposedRating Yellow (nsmyth)
049 @Pt.AcceptedRating Red (cxh)
050 @see ptolemy.data.expr.ASTPtRootNode
051 @see ptolemy.data.expr.PtParser
052 @see ptolemy.data.Token
053 */
054public class ASTPtLeafNode extends ASTPtRootNode {
055    public ASTPtLeafNode(int id) {
056        super(id);
057    }
058
059    public ASTPtLeafNode(PtParser p, int id) {
060        super(p, id);
061    }
062
063    /** Return the name that this node refers to.  This may be a
064     *  literal value, such as "5", or a reference to another object,
065     *  such as the name of a variable in scope.
066     *  @return The name to which this node refers.
067     */
068    public String getName() {
069        return _name;
070    }
071
072    /** Return true if this node is (hierarchically) congruent to the
073     *  given node, under the given renaming of bound identifiers.
074     *  Derived classes should extend this method to add additional
075     *  necessary congruency checks.
076     *  @param node The node to compare to.
077     *  @param renaming A map from String to String that gives a
078     *  renaming from identifiers in this node to identifiers in the
079     *  given node.
080     */
081    @Override
082    public boolean isCongruent(ASTPtRootNode node, Map renaming) {
083        if (!super.isCongruent(node, renaming)) {
084            return false;
085        }
086
087        // Both must be constant or not.
088        if (isConstant() != node.isConstant()) {
089            return false;
090        }
091
092        if (isConstant()) {
093            // If constant, then check the value
094            return getToken().equals(node.getToken());
095        } else {
096            // Else, check the name.
097            String checkName = (String) renaming.get(getName());
098
099            if (checkName == null) {
100                checkName = getName();
101            }
102
103            if (!checkName.equals(((ASTPtLeafNode) node).getName())) {
104                return false;
105            } else {
106                return true;
107            }
108        }
109    }
110
111    /** Return true if the leaf is an identifier that must be
112     *  evaluated in scope.
113     *  @return true if the leaf is not a constant.
114     */
115    public boolean isIdentifier() {
116        return !isConstant();
117    }
118
119    /** Return a string representation
120     */
121    @Override
122    public String toString() {
123        return super.toString() + ":" + _name;
124    }
125
126    /** Traverse this node with the given visitor.
127     */
128    @Override
129    public void visit(ParseTreeVisitor visitor) throws IllegalActionException {
130        visitor.visitLeafNode(this);
131    }
132
133    /** The identifier that this leaf node refers to.
134     */
135    protected String _name;
136}