001/*
002 * Anarres C Preprocessor
003 * Copyright (c) 2007-2015, Shevek
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
014 * or implied.  See the License for the specific language governing
015 * permissions and limitations under the License.
016 */
017package org.anarres.cpp;
018
019import javax.annotation.CheckForNull;
020import javax.annotation.Nonnull;
021
022/**
023 * A Preprocessor token.
024 *
025 * @see Preprocessor
026 */
027public final class Token {
028
029    // public static final int  EOF        = -1;
030    private final int type;
031    private int line;
032    private int column;
033    private final Object value;
034    private final String text;
035
036    public Token(int type, int line, int column,
037            String text, Object value) {
038        this.type = type;
039        this.line = line;
040        this.column = column;
041        this.text = text;
042        this.value = value;
043    }
044
045    public Token(int type, int line, int column, String text) {
046        this(type, line, column, text, null);
047    }
048
049    /* pp */ Token(int type, String text, Object value) {
050        this(type, -1, -1, text, value);
051    }
052
053    /* pp */ Token(int type, String text) {
054        this(type, text, null);
055    }
056
057    /* pp */ Token(int type) {
058        this(type, TokenType.getTokenText(type));
059    }
060
061    /**
062     * Returns the semantic type of this token.
063     *
064     * @return the semantic type of this token.
065     * @see #getTokenName(int)
066     */
067    public int getType() {
068        return type;
069    }
070
071    /* pp */ void setLocation(int line, int column) {
072        this.line = line;
073        this.column = column;
074    }
075
076    /**
077     * Returns the line at which this token started.
078     *
079     * Lines are numbered from 1.
080     *
081     * @return the line at which this token started.
082     * @see LexerSource#getLine()
083     */
084    // Not @Nonnegative - might not have been assigned?
085    public int getLine() {
086        return line;
087    }
088
089    /**
090     * Returns the column at which this token started.
091     *
092     * Columns are numbered from 0.
093     *
094     * @return the column at which this token started.
095     * @see LexerSource#getColumn()
096     */
097    // Not @Nonnegative - might not have been assigned?
098    public int getColumn() {
099        return column;
100    }
101
102    /**
103     * Returns the original or generated text of this token.
104     *
105     * This is distinct from the semantic value of the token.
106     *
107     * @return the original or generated text of this token.
108     * @see #getValue()
109     */
110    // Not @Nonnull - might not have been assigned?
111    public String getText() {
112        return text;
113    }
114
115    /**
116     * Returns the semantic value of this token.
117     *
118     * For strings, this is the parsed String.
119     * For integers, this is an Integer object.
120     * For other token types, as appropriate.
121     *
122     * @return the semantic value of this token, or null.
123     * @see #getText()
124     */
125    // @CheckForNull    // Not useful to annotate, as we have usually checked the type before calling this.
126    public Object getValue() {
127        return value;
128    }
129
130    /**
131     * Returns a description of this token, for debugging purposes.
132     */
133    @Override
134    public String toString() {
135        StringBuilder buf = new StringBuilder();
136
137        buf.append('[').append(getTokenName(type));
138        if (line != -1) {
139            buf.append('@').append(line);
140            if (column != -1)
141                buf.append(',').append(column);
142        }
143        buf.append("]:");
144        if (text != null)
145            buf.append('"').append(text).append('"');
146        else if (type > 3 && type < 256)
147            buf.append((char) type);
148        else
149            buf.append('<').append(type).append('>');
150        if (value != null)
151            buf.append('=').append(value);
152        return buf.toString();
153    }
154
155    /**
156     * Returns the descriptive name of the given token type.
157     *
158     * This is mostly used for stringification and debugging.
159     *
160     * @param type The type constant from this class to name.
161     * @return the descriptive name of the given token type.
162     * @see Token#getType()
163     */
164    @Nonnull
165    public static String getTokenName(int type) {
166        return TokenType.getTokenName(type);
167    }
168
169    public static final int AND_EQ = 257;
170    public static final int ARROW = 258;
171    public static final int CHARACTER = 259;
172    public static final int CCOMMENT = 260;
173    public static final int CPPCOMMENT = 261;
174    public static final int DEC = 262;
175    public static final int DIV_EQ = 263;
176    public static final int ELLIPSIS = 264;
177    public static final int EOF = 265;
178    public static final int EQ = 266;
179    public static final int GE = 267;
180    public static final int HASH = 268;
181    public static final int HEADER = 269;
182    public static final int IDENTIFIER = 270;
183    public static final int INC = 271;
184    public static final int NUMBER = 272;
185    public static final int LAND = 273;
186    public static final int LAND_EQ = 274;
187    public static final int LE = 275;
188    public static final int LITERAL = 276;
189    public static final int LOR = 277;
190    public static final int LOR_EQ = 278;
191    public static final int LSH = 279;
192    public static final int LSH_EQ = 280;
193    public static final int MOD_EQ = 281;
194    public static final int MULT_EQ = 282;
195    public static final int NE = 283;
196    public static final int NL = 284;
197    public static final int OR_EQ = 285;
198    public static final int PASTE = 286;
199    public static final int PLUS_EQ = 287;
200    public static final int RANGE = 288;
201    public static final int RSH = 289;
202    public static final int RSH_EQ = 290;
203    public static final int SQSTRING = 291;
204    public static final int STRING = 292;
205    public static final int SUB_EQ = 293;
206    public static final int WHITESPACE = 294;
207    public static final int XOR_EQ = 295;
208    public static final int M_ARG = 296;
209    public static final int M_PASTE = 297;
210    public static final int M_STRING = 298;
211    public static final int P_LINE = 299;
212    public static final int INVALID = 300;
213
214    /** The position-less space token. */
215    /* pp */ static final Token space = new Token(WHITESPACE, -1, -1, " ");
216}