001/* ASTPtProductNode represent product(*,/,%) 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//// ASTPtProductNode
042
043/**
044 The parse tree created from the expression string consists of a
045 hierarchy of node objects. This class represents product(*,/,%) nodes in
046 the parse tree.
047
048 @author Neil Smyth, Bart Kienhuis, 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 ASTPtProductNode extends ASTPtRootNode {
058    public ASTPtProductNode(int id) {
059        super(id);
060    }
061
062    public ASTPtProductNode(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        ASTPtProductNode newNode = (ASTPtProductNode) 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 nodeTokens = ((ASTPtProductNode) node)._lexicalTokens
104                .iterator();
105
106        for (Object element : _lexicalTokens) {
107            Token token = (Token) element;
108            Token nodeToken = (Token) nodeTokens.next();
109
110            if (token.kind != nodeToken.kind) {
111                return false;
112            }
113
114            if (!token.image.equals(nodeToken.image)) {
115                return false;
116            }
117        }
118
119        return true;
120    }
121
122    /** Close this node.
123     */
124    @Override
125    public void jjtClose() {
126        super.jjtClose();
127        _lexicalTokens.trimToSize();
128    }
129
130    /** Traverse this node with the given visitor.
131     */
132    @Override
133    public void visit(ParseTreeVisitor visitor) throws IllegalActionException {
134        visitor.visitProductNode(this);
135    }
136
137    protected ArrayList<Token> _lexicalTokens = new ArrayList<Token>();
138}