Token.java

/*
* Generated by: CongoCC Parser Generator. Token.java
*/
package de.schegge.rosinante.parser;

import de.schegge.rosinante.parser.ast.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


public class Token implements CharSequence, Node.TerminalNode {

    public enum TokenType implements Node.NodeType {
        EOF, SYNTAX, IMPORT, WEAK, PUBLIC, PACKAGE, OPTION, MESSAGE, ENUM, REPEATED,
        OPTIONAL, LPAREN, RPAREN, LBRACE, RBRACE, LBRACKET, RBRACKET, SEMICOLON, COLON,
        COMMA, DOT, DOUBLE, FLOAT, INT32, INT64, UINT32, UINT64, SINT32, SINT64, FIXED32,
        FIXEDINT64, SFIXED32, SFIXEDINT64, STRING, BYTES, BOOL, TRUE, FALSE, ZERO,
        ASSIGN, _TOKEN_40, _TOKEN_41, _TOKEN_42, WHITESPACE, SINGLE_LINE_COMMENT,
        INTEGER_LITERAL, OCT_LITERAL, HEX_LITERAL, IDENTIFIER, FULL_IDENTIFIER, TYPE_IDENTIFIER,
        STRING_LITERAL, OPTION_NAME, DUMMY, INVALID;

        public boolean isUndefined() {
            return this == DUMMY;
        }

        public boolean isInvalid() {
            return this == INVALID;
        }

        public boolean isEOF() {
            return this == EOF;
        }

    }

    private ProtocolbuffersLexer tokenSource;
    private TokenType type = TokenType.DUMMY;
    private int beginOffset;
    private int endOffset;
    private boolean unparsed;
    private Node parent;

    /**
    * It would be extremely rare that an application
    * programmer would use this method. It needs to
    * be public because it is part of the de.schegge.rosinante.parser.Node interface.
    */
    public void setBeginOffset(int beginOffset) {
        this.beginOffset = beginOffset;
    }

    /**
    * It would be extremely rare that an application
    * programmer would use this method. It needs to
    * be public because it is part of the de.schegge.rosinante.parser.Node interface.
    */
    public void setEndOffset(int endOffset) {
        this.endOffset = endOffset;
    }

    /**
    * @return the ProtocolbuffersLexer object that handles
    * location info for the tokens.
    */
    public ProtocolbuffersLexer getTokenSource() {
        return this.tokenSource;
    }

    /**
    * It should be exceedingly rare that an application
    * programmer needs to use this method.
    */
    public void setTokenSource(TokenSource tokenSource) {
        this.tokenSource = (ProtocolbuffersLexer) tokenSource;
    }

    /**
    * Return the TokenType of this Token object
    */
    @Override
    public TokenType getType() {
        return type;
    }

    protected void setType(TokenType type) {
        this.type = type;
    }

    /**
    * @return whether this Token represent actual input or was it inserted somehow?
    */
    public boolean isVirtual() {
        return type == TokenType.EOF;
    }

    /**
    * @return Did we skip this token in parsing?
    */
    public boolean isSkipped() {
        return false;
    }

    public int getBeginOffset() {
        return beginOffset;
    }

    public int getEndOffset() {
        return endOffset;
    }

    /**
    * @return the string image of the token.
    */
    @Override
    /**
    * @return the next _cached_ regular (i.e. parsed) token
    * or null
    */
    public final Token getNext() {
        return getNextParsedToken();
    }

    /**
    * @return the previous regular (i.e. parsed) token
    * or null
    */
    public final Token getPrevious() {
        Token result = previousCachedToken();
        while (result != null && result.isUnparsed()) {
            result = result.previousCachedToken();
        }
        return result;
    }

    /**
    * @return the next regular (i.e. parsed) token
    */
    private Token getNextParsedToken() {
        Token result = nextCachedToken();
        while (result != null && result.isUnparsed()) {
            result = result.nextCachedToken();
        }
        return result;
    }

    /**
    * @return the next token of any sort (parsed or unparsed or invalid)
    */
    public Token nextCachedToken() {
        if (getType() == TokenType.EOF) return null;
        ProtocolbuffersLexer tokenSource = getTokenSource();
        return tokenSource != null ? (Token) tokenSource.nextCachedToken(getEndOffset()) : null;
    }

    public Token previousCachedToken() {
        if (getTokenSource() == null) return null;
        return (Token) getTokenSource().previousCachedToken(getBeginOffset());
    }

    Token getPreviousToken() {
        return previousCachedToken();
    }

    public Token replaceType(TokenType type) {
        Token result = newToken(type, getTokenSource(), getBeginOffset(), getEndOffset());
        getTokenSource().cacheToken(result);
        return result;
    }

    public String getSource() {
        if (type == TokenType.EOF) return "";
        ProtocolbuffersLexer flm = getTokenSource();
        return flm == null ? null : flm.getText(getBeginOffset(), getEndOffset());
    }

    protected Token() {
    }

    public Token(TokenType type, ProtocolbuffersLexer tokenSource, int beginOffset, int endOffset) {
        this.type = type;
        this.tokenSource = tokenSource;
        this.beginOffset = beginOffset;
        this.endOffset = endOffset;
    }

    public boolean isUnparsed() {
        return unparsed;
    }

    public void setUnparsed(boolean unparsed) {
        this.unparsed = unparsed;
    }

    /**
    * @return An iterator of the tokens preceding this one.
    */
    public Iterator<Token> precedingTokens() {
        return new Iterator<Token>() {
            Token currentPoint = Token.this;

            public boolean hasNext() {
                return currentPoint.previousCachedToken() != null;
            }

            public Token next() {
                Token previous = currentPoint.previousCachedToken();
                if (previous == null) throw new java.util.NoSuchElementException("No previous token!");
                return currentPoint = previous;
            }

        };
    }

    /**
    * @return a list of the unparsed tokens preceding this one in the order they appear in the input
    */
    public List<Token> precedingUnparsedTokens() {
        List<Token> result = new ArrayList<>();
        Token t = this.previousCachedToken();
        while (t != null && t.isUnparsed()) {
            result.add(t);
            t = t.previousCachedToken();
        }
        Collections.reverse(result);
        return result;
    }

    /**
    * @return An iterator of the (cached) tokens that follow this one.
    */
    public Iterator<Token> followingTokens() {
        return new java.util.Iterator<Token>() {
            Token currentPoint = Token.this;

            public boolean hasNext() {
                return currentPoint.nextCachedToken() != null;
            }

            public Token next() {
                Token next = currentPoint.nextCachedToken();
                if (next == null) throw new java.util.NoSuchElementException("No next token!");
                return currentPoint = next;
            }

        };
    }

    public void copyLocationInfo(Token from) {
        setTokenSource(from.getTokenSource());
        setBeginOffset(from.getBeginOffset());
        setEndOffset(from.getEndOffset());
    }

    public void copyLocationInfo(Token start, Token end) {
        setTokenSource(start.getTokenSource());
        if (tokenSource == null) setTokenSource(end.getTokenSource());
        setBeginOffset(start.getBeginOffset());
        setEndOffset(end.getEndOffset());
    }

    public static Token newToken(TokenType type, ProtocolbuffersLexer tokenSource, int beginOffset, int endOffset) {
        switch(type) {
            case BYTES : 
                return new FieldType(TokenType.BYTES, tokenSource, beginOffset, endOffset);
            case ZERO : 
                return new ZERO(TokenType.ZERO, tokenSource, beginOffset, endOffset);
            case INTEGER_LITERAL : 
                return new INTEGER_LITERAL(TokenType.INTEGER_LITERAL, tokenSource, beginOffset, endOffset);
            case UINT32 : 
                return new FieldType(TokenType.UINT32, tokenSource, beginOffset, endOffset);
            case OPTIONAL : 
                return new KeyWord(TokenType.OPTIONAL, tokenSource, beginOffset, endOffset);
            case SINT64 : 
                return new FieldType(TokenType.SINT64, tokenSource, beginOffset, endOffset);
            case HEX_LITERAL : 
                return new HEX_LITERAL(TokenType.HEX_LITERAL, tokenSource, beginOffset, endOffset);
            case PACKAGE : 
                return new KeyWord(TokenType.PACKAGE, tokenSource, beginOffset, endOffset);
            case SEMICOLON : 
                return new Delimiter(TokenType.SEMICOLON, tokenSource, beginOffset, endOffset);
            case WHITESPACE : 
                return new Whitespace(TokenType.WHITESPACE, tokenSource, beginOffset, endOffset);
            case ENUM : 
                return new KeyWord(TokenType.ENUM, tokenSource, beginOffset, endOffset);
            case OPTION_NAME : 
                return new OPTION_NAME(TokenType.OPTION_NAME, tokenSource, beginOffset, endOffset);
            case OPTION : 
                return new KeyWord(TokenType.OPTION, tokenSource, beginOffset, endOffset);
            case LPAREN : 
                return new Delimiter(TokenType.LPAREN, tokenSource, beginOffset, endOffset);
            case DOT : 
                return new Delimiter(TokenType.DOT, tokenSource, beginOffset, endOffset);
            case TRUE : 
                return new TRUE(TokenType.TRUE, tokenSource, beginOffset, endOffset);
            case RPAREN : 
                return new Delimiter(TokenType.RPAREN, tokenSource, beginOffset, endOffset);
            case OCT_LITERAL : 
                return new OCT_LITERAL(TokenType.OCT_LITERAL, tokenSource, beginOffset, endOffset);
            case UINT64 : 
                return new FieldType(TokenType.UINT64, tokenSource, beginOffset, endOffset);
            case SINT32 : 
                return new FieldType(TokenType.SINT32, tokenSource, beginOffset, endOffset);
            case RBRACE : 
                return new Delimiter(TokenType.RBRACE, tokenSource, beginOffset, endOffset);
            case PUBLIC : 
                return new KeyWord(TokenType.PUBLIC, tokenSource, beginOffset, endOffset);
            case FIXEDINT64 : 
                return new FieldType(TokenType.FIXEDINT64, tokenSource, beginOffset, endOffset);
            case LBRACE : 
                return new Delimiter(TokenType.LBRACE, tokenSource, beginOffset, endOffset);
            case FLOAT : 
                return new FieldType(TokenType.FLOAT, tokenSource, beginOffset, endOffset);
            case INT32 : 
                return new FieldType(TokenType.INT32, tokenSource, beginOffset, endOffset);
            case ASSIGN : 
                return new ASSIGN(TokenType.ASSIGN, tokenSource, beginOffset, endOffset);
            case FIXED32 : 
                return new FieldType(TokenType.FIXED32, tokenSource, beginOffset, endOffset);
            case SYNTAX : 
                return new KeyWord(TokenType.SYNTAX, tokenSource, beginOffset, endOffset);
            case FULL_IDENTIFIER : 
                return new FULL_IDENTIFIER(TokenType.FULL_IDENTIFIER, tokenSource, beginOffset, endOffset);
            case WEAK : 
                return new KeyWord(TokenType.WEAK, tokenSource, beginOffset, endOffset);
            case IMPORT : 
                return new KeyWord(TokenType.IMPORT, tokenSource, beginOffset, endOffset);
            case MESSAGE : 
                return new KeyWord(TokenType.MESSAGE, tokenSource, beginOffset, endOffset);
            case SINGLE_LINE_COMMENT : 
                return new SingleLineComment(TokenType.SINGLE_LINE_COMMENT, tokenSource, beginOffset, endOffset);
            case DOUBLE : 
                return new FieldType(TokenType.DOUBLE, tokenSource, beginOffset, endOffset);
            case IDENTIFIER : 
                return new IDENTIFIER(TokenType.IDENTIFIER, tokenSource, beginOffset, endOffset);
            case COMMA : 
                return new Delimiter(TokenType.COMMA, tokenSource, beginOffset, endOffset);
            case INT64 : 
                return new FieldType(TokenType.INT64, tokenSource, beginOffset, endOffset);
            case REPEATED : 
                return new KeyWord(TokenType.REPEATED, tokenSource, beginOffset, endOffset);
            case LBRACKET : 
                return new Delimiter(TokenType.LBRACKET, tokenSource, beginOffset, endOffset);
            case RBRACKET : 
                return new Delimiter(TokenType.RBRACKET, tokenSource, beginOffset, endOffset);
            case COLON : 
                return new Delimiter(TokenType.COLON, tokenSource, beginOffset, endOffset);
            case SFIXEDINT64 : 
                return new FieldType(TokenType.SFIXEDINT64, tokenSource, beginOffset, endOffset);
            case TYPE_IDENTIFIER : 
                return new TYPE_IDENTIFIER(TokenType.TYPE_IDENTIFIER, tokenSource, beginOffset, endOffset);
            case BOOL : 
                return new FieldType(TokenType.BOOL, tokenSource, beginOffset, endOffset);
            case STRING : 
                return new FieldType(TokenType.STRING, tokenSource, beginOffset, endOffset);
            case FALSE : 
                return new FALSE(TokenType.FALSE, tokenSource, beginOffset, endOffset);
            case STRING_LITERAL : 
                return new STRING_LITERAL(TokenType.STRING_LITERAL, tokenSource, beginOffset, endOffset);
            case SFIXED32 : 
                return new FieldType(TokenType.SFIXED32, tokenSource, beginOffset, endOffset);
            case INVALID : 
                return new InvalidToken(tokenSource, beginOffset, endOffset);
            default : 
                return new Token(type, tokenSource, beginOffset, endOffset);
        }
    }

    public String getLocation() {
        return getInputSource() + ":" + getBeginLine() + ":" + getBeginColumn();
    }

    public Node getParent() {
        return parent;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

    public boolean isEmpty() {
        return length() == 0;
    }

    public int length() {
        return endOffset - beginOffset;
    }

    public CharSequence subSequence(int start, int end) {
        return getTokenSource().subSequence(beginOffset + start, beginOffset + end);
    }

    public char charAt(int offset) {
        return getTokenSource().charAt(beginOffset + offset);
    }

    /**
    * @deprecated Use toString() instead
    */
    @Deprecated
    public String getImage() {
        return getSource();
    }

    @Override
    public String toString() {
        return getSource();
    }

}