001/* ASTPtSumNode represent sum(+, -) 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.ArrayList;
034import java.util.Iterator;
035import java.util.List;
036import java.util.Map;
037
038import ptolemy.kernel.util.IllegalActionException;
039
040//////////////////////////////////////////////////////////////////////////
041//// ASTPtSumNode
042
043/**
044 The parse tree created from the expression string consists of a
045 hierarchy of node objects. This class represents sum(+, -) nodes in
046 the parse tree.
047
048 @author Neil Smyth, Steve Neuendorffer
049 @version $Id$
050 @since Ptolemy II 0.2
051 @Pt.ProposedRating Yellow (nsmyth)
052 @Pt.AcceptedRating Red (cxh)
053 @see ptolemy.data.expr.ASTPtRootNode
054 @see ptolemy.data.expr.PtParser
055 @see ptolemy.data.Token
056 */
057public class ASTPtSumNode extends ASTPtRootNode {
058    public ASTPtSumNode(int id) {
059        super(id);
060    }
061
062    public ASTPtSumNode(PtParser p, int id) {
063        super(p, id);
064    }
065
066    /** Clone the parse tree node by invoking the clone() method of
067     *  the base class. The new node copies the list of operators (+, -)
068     *  represented by this node.
069     *  @return A new parse tree node.
070     *  @exception CloneNotSupportedException If the superclass clone()
071     *   method throws it.
072     */
073    @Override
074    public Object clone() throws CloneNotSupportedException {
075        ASTPtSumNode newNode = (ASTPtSumNode) super.clone();
076        newNode._lexicalTokens = (ArrayList<Token>) _lexicalTokens.clone();
077        return newNode;
078    }
079
080    /** Return the list of lexical tokens that were used to make this node.
081     *  @return The list of lexical tokens that were used to make this node.
082     */
083    public List<Token> getLexicalTokenList() {
084        return _lexicalTokens;
085    }
086
087    /** Return true if this node is (hierarchically) congruent to the
088     *  given node, under the given renaming of bound identifiers.
089     *  Derived classes should extend this method to add additional
090     *  necessary congruency checks.
091     *  @param node The node to compare to.
092     *  @param renaming A map from String to String that gives a
093     *  renaming from identifiers in this node to identifiers in the
094     *  given node.
095     */
096    @Override
097    public boolean isCongruent(ASTPtRootNode node, Map renaming) {
098        if (!super.isCongruent(node, renaming)) {
099            return false;
100        }
101
102        // The operators must be the same.
103        Iterator<Token> nodeTokens = ((ASTPtSumNode) node)._lexicalTokens
104                .iterator();
105
106        for (Token token : _lexicalTokens) {
107            Token nodeToken = nodeTokens.next();
108
109            if (token.kind != nodeToken.kind) {
110                return false;
111            }
112
113            if (!token.image.equals(nodeToken.image)) {
114                return false;
115            }
116        }
117
118        return true;
119    }
120
121    /** Close this node.
122     */
123    @Override
124    public void jjtClose() {
125        super.jjtClose();
126        _lexicalTokens.trimToSize();
127    }
128
129    /** Traverse this node with the given visitor.
130     */
131    @Override
132    public void visit(ParseTreeVisitor visitor) throws IllegalActionException {
133        visitor.visitSumNode(this);
134    }
135
136    protected ArrayList<Token> _lexicalTokens = new ArrayList<Token>();
137}