JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/TokenTypes.java
1/*
2 * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2020 The JavaParser Team.
4 *
5 * This file is part of JavaParser.
6 *
7 * JavaParser can be used either under the terms of
8 * a) the GNU Lesser General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 * b) the terms of the Apache License
12 *
13 * You should have received a copy of both licenses in LICENCE.LGPL and
14 * LICENCE.APACHE. Please refer to those files for details.
15 *
16 * JavaParser is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 */
21
22package com.github.javaparser;
23
24import com.github.javaparser.utils.LineSeparator;
25
26import static com.github.javaparser.GeneratedJavaParserConstants.*;
27
28/**
29 * Complements GeneratedJavaParserConstants
30 */
31public class TokenTypes {
32    public static boolean isWhitespace(int kind) {
33        return getCategory(kind).isWhitespace();
34    }
35
36    public static boolean isEndOfLineToken(int kind) {
37        return getCategory(kind).isEndOfLine();
38    }
39
40    public static boolean isWhitespaceOrComment(int kind) {
41        return getCategory(kind).isWhitespaceOrComment();
42    }
43
44    /**
45     * @deprecated Use {@link #isWhitespaceButNotEndOfLine(int)} which more explicitly reflects that this also includes
46     *  other whitespace e.g. {@code EOF} and {@code CTRL_Z} and a large number of other characters.
47     *  See the grammar for details of exactly which characters are included as a "space" (.
48     *  <pre>{@code
49     *   <SPACE: [" ", "\t", "\f", "\u0085", "\u00A0", "\u1680", "\u180e", "\u2000", "\u2001", "\u2002", "\u2003", "\u2004", "\u2005",
50     *       "\u2006", "\u2007", "\u2008", "\u2009", "\u200a", "\u200b", "\u200c", "\u200d", "\u2028", "\u2029", "\u202f", "\u205f", "\u2060", "\u3000", "\ufeff"]>
51     *  }</pre>
52     */
53    @Deprecated
54    public static boolean isSpaceOrTab(int kind) {
55        return isWhitespaceButNotEndOfLine(kind);
56    }
57
58    public static boolean isWhitespaceButNotEndOfLine(int kind) {
59        return getCategory(kind).isWhitespaceButNotEndOfLine();
60    }
61
62    public static boolean isComment(int kind) {
63        return getCategory(kind).isComment();
64    }
65
66    /**
67     * @return the kind of EOL token to use on the platform you're running on.
68     */
69    public static int eolTokenKind(LineSeparator lineSeparator) {
70        if (lineSeparator.equalsString(LineSeparator.LF)) {
71            return UNIX_EOL;
72        }
73        if (lineSeparator.equalsString(LineSeparator.CRLF)) {
74            return WINDOWS_EOL;
75        }
76        if (lineSeparator.equalsString(LineSeparator.CR)) {
77            return OLD_MAC_EOL;
78        }
79        throw new AssertionError("Unknown EOL character sequence");
80    }
81
82    public static int eolTokenKind() {
83        return eolTokenKind(LineSeparator.SYSTEM);
84    }
85
86    /**
87     * @return the token kind for a single space.
88     */
89    public static int spaceTokenKind() {
90        return SPACE;
91    }
92
93    /**
94     * Category of a token, a little more detailed than
95     * <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.5">The JLS</a>.
96     * FIXME: It appears that {@code ...} {@code ELLIPSIS} and {@code ::} {@code DOUBLECOLON} are (wrongly) listed in the "operators" section, rather than "separators"
97     */
98    public static JavaToken.Category getCategory(int kind) {
99        switch (kind) {
100            case WINDOWS_EOL:
101            case UNIX_EOL:
102            case OLD_MAC_EOL:
103                return JavaToken.Category.EOL;
104            case EOF:
105            case SPACE:
106            case CTRL_Z:
107                return JavaToken.Category.WHITESPACE_NO_EOL;
108            case SINGLE_LINE_COMMENT:
109            case JAVADOC_COMMENT:
110            case MULTI_LINE_COMMENT:
111                return JavaToken.Category.COMMENT;
112            case ABSTRACT:
113            case ASSERT:
114            case BOOLEAN:
115            case BREAK:
116            case BYTE:
117            case CASE:
118            case CATCH:
119            case CHAR:
120            case CLASS:
121            case CONST:
122            case CONTINUE:
123            case _DEFAULT:
124            case DO:
125            case DOUBLE:
126            case ELSE:
127            case ENUM:
128            case EXTENDS:
129            case FINAL:
130            case FINALLY:
131            case FLOAT:
132            case FOR:
133            case GOTO:
134            case IF:
135            case IMPLEMENTS:
136            case IMPORT:
137            case INSTANCEOF:
138            case INT:
139            case INTERFACE:
140            case LONG:
141            case NATIVE:
142            case NEW:
143            case PACKAGE:
144            case PRIVATE:
145            case PROTECTED:
146            case PUBLIC:
147            case RETURN:
148            case SHORT:
149            case STATIC:
150            case STRICTFP:
151            case SUPER:
152            case SWITCH:
153            case SYNCHRONIZED:
154            case THIS:
155            case THROW:
156            case THROWS:
157            case TRANSIENT:
158            case TRY:
159            case VOID:
160            case VOLATILE:
161            case WHILE:
162            case YIELD:
163            case REQUIRES:
164            case TO:
165            case WITH:
166            case OPEN:
167            case OPENS:
168            case USES:
169            case MODULE:
170            case EXPORTS:
171            case PROVIDES:
172            case TRANSITIVE:
173                return JavaToken.Category.KEYWORD;
174            case LONG_LITERAL:
175            case INTEGER_LITERAL:
176            case DECIMAL_LITERAL:
177            case HEX_LITERAL:
178            case OCTAL_LITERAL:
179            case BINARY_LITERAL:
180            case FLOATING_POINT_LITERAL:
181            case DECIMAL_FLOATING_POINT_LITERAL:
182            case DECIMAL_EXPONENT:
183            case HEXADECIMAL_FLOATING_POINT_LITERAL:
184            case HEXADECIMAL_EXPONENT:
185            case CHARACTER_LITERAL:
186            case STRING_LITERAL:
187            case TEXT_BLOCK_LITERAL:
188            case TRUE:
189            case FALSE:
190            case NULL:
191                return JavaToken.Category.LITERAL;
192            case IDENTIFIER:
193                return JavaToken.Category.IDENTIFIER;
194            case LPAREN:
195            case RPAREN:
196            case LBRACE:
197            case RBRACE:
198            case LBRACKET:
199            case RBRACKET:
200            case SEMICOLON:
201            case COMMA:
202            case DOT:
203            case AT:
204                return JavaToken.Category.SEPARATOR;
205            case ASSIGN:
206            case LT:
207            case BANG:
208            case TILDE:
209            case HOOK:
210            case COLON:
211            case EQ:
212            case LE:
213            case GE:
214            case NE:
215            case SC_OR:
216            case SC_AND:
217            case INCR:
218            case DECR:
219            case PLUS:
220            case MINUS:
221            case STAR:
222            case SLASH:
223            case BIT_AND:
224            case BIT_OR:
225            case XOR:
226            case REM:
227            case LSHIFT:
228            case PLUSASSIGN:
229            case MINUSASSIGN:
230            case STARASSIGN:
231            case SLASHASSIGN:
232            case ANDASSIGN:
233            case ORASSIGN:
234            case XORASSIGN:
235            case REMASSIGN:
236            case LSHIFTASSIGN:
237            case RSIGNEDSHIFTASSIGN:
238            case RUNSIGNEDSHIFTASSIGN:
239            case ELLIPSIS:
240            case ARROW:
241            case DOUBLECOLON:
242            case RUNSIGNEDSHIFT:
243            case RSIGNEDSHIFT:
244            case GT:
245                return JavaToken.Category.OPERATOR;
246            // The following are tokens that are only used internally by the lexer
247            case ENTER_JAVADOC_COMMENT:
248            case ENTER_MULTILINE_COMMENT:
249            case COMMENT_CONTENT:
250            case HEX_DIGITS:
251            case LETTER:
252            case UNICODE_ESCAPE:
253            case PART_LETTER:
254            case TEXT_BLOCK_CONTENT:
255            case ENTER_TEXT_BLOCK:
256            default:
257                throw new AssertionError("Invalid token kind " + kind);
258        }
259    }
260}
261
MembersX
TokenTypes:isWhitespaceOrComment
TokenTypes:isSpaceOrTab
TokenTypes:isWhitespace
TokenTypes:isComment
TokenTypes:eolTokenKind
TokenTypes:spaceTokenKind
TokenTypes:isWhitespaceButNotEndOfLine
TokenTypes:isEndOfLineToken
TokenTypes:getCategory
Members
X