All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Parser.TokenStack Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Parser;

// C# Translation of GoldParser, by Marcus Klimstra .

// Based on GOLDParser by Devin Cook .

/**
 *
 */
public class TokenStack {

    private java.util.ArrayList m_items;

    /*
     * constructor
     */

    /**
     *
     */
    public TokenStack() {
        m_items = new java.util.ArrayList();
    }

    /*
     * indexer
     */

    /**
     * Returns the token at the specified position from the top.
     */
    public final Token getItem(int p_index) {
        return (Token) m_items.get(p_index);
    }

    /*
     * properties
     */

    /**
     * Gets the number of items in the stack.
     */
    public final int getCount() {
        return m_items.size();
    }

    /*
     * public methods
     */

    /**
     * Removes all tokens from the stack.
     */
    public final void Clear() {
        m_items.clear();
    }

    /**
     * Pushes the specified token on the stack.
     */
    public final void PushToken(Token p_token) {
        m_items.add(p_token);
    }

    /**
     * Returns the token on top of the stack.
     */
    public final Token PeekToken() {
        int last = m_items.size() - 1;
        return (last < 0 ? null : (Token) m_items.get(last));
    }

    /**
     * Pops a token from the stack. The token on top of the stack will be removed and returned by the method.
     */
    public final Token PopToken() {
        int last = m_items.size() - 1;
        if (last < 0) {
            return null;
        }
        Token result = (Token) m_items.get(last);
        m_items.remove(last);
        return result;
    }

    /**
     * Pops the specified number of tokens from the stack and adds them to the specified Reduction.
     */
    public final void PopTokensInto(Reduction p_reduction, int p_count) {
        int start = m_items.size() - p_count;
        int end = m_items.size();

        for (int i = start; i < end; i++) {
            p_reduction.AddToken((Token) m_items.get(i));
        }


        for (int i = 0; i < p_count; i++) {
            m_items.remove(start);
        }


//        List subList = m_items.subList(start, p_count + start);
//        Object[] objs = subList.toArray();
//        for (int i = 0; i < objs.length; i++)
//        {
//            m_items.remove(objs[i]);
//        }
        //m_items.removeAll(subList);
        //m_items.removeRange(start, p_count + start);
    }

    /**
     * Returns the token at the specified position from the top. GetToken(0) returns the token on top off the stack, GetToken(1) the next one, etc.
     */
    public final Token GetToken(int p_index) {
        return (Token) m_items.get(p_index);
    }

    /**
     * Returns an IEnumerator for the tokens on the stack.
     */
    public final java.util.Iterator GetEnumerator() {
        return m_items.iterator();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy