/* SCCS - @(#)YYtoken_Stack.java 1.2 - 02/28/99 - 12:39:32 */ package pp.src.jbf; /* * @(#)Stack.java 1.12 95/08/11 * * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.util.*; /** * A Last-In-First-Out(LIFO) stack of ints. * * @version 1.12, 08/11/95 * @author Jonathan Payne */ public class YYtoken_Stack extends Vector { /** * Pushes an item onto the stack. * @param item the item to be pushed on. */ public YYtoken push(YYtoken item) { addElement(item); return item; } /** * Pops an item off the stack. * @exception EmptyStackException If the stack is empty. */ public YYtoken pop() throws EmptyStackException { YYtoken obj; int len = size(); obj = peek(); removeElementAt(len - 1); return obj; } /** * Peeks at the top of the stack. * @exception EmptyStackException If the stack is empty. */ public YYtoken peek() throws EmptyStackException { int len = size(); if (len == 0) throw new EmptyStackException(); return (YYtoken)elementAt(len - 1); } public boolean empty() { return size() == 0; } /** * Sees if an int is on the stack. * @param o the desired int * @return the distance from the top, or -1 if it is not found. */ public int search(YYtoken o) { int i = lastIndexOf(o); if (i >= 0) { return size() - i; } return -1; } /**************************************************/ public void clear() { setSize(0); } public void popn(int n) throws EmptyStackException { int len = size(); if(n < 0 || n > len) { throw new EmptyStackException(); } setSize(len - n); } public YYtoken ith(int i) throws EmptyStackException { int len = size(); if(i < 0 || i >= len) throw new EmptyStackException(); return (YYtoken)elementAt(i); } public int depth() { return size(); } public YYtoken tth(int i) throws EmptyStackException { int len = size(); i = - i; if(i < 0 || i >= len) throw new EmptyStackException(); return (YYtoken)elementAt(len - (i+1)); } }