1 package com.github.javaparser;
2 
3 import static com.github.javaparser.GeneratedJavaParserConstants.*;
4 import static com.github.javaparser.utils.Utils.EOL;
5 
6 /**
7  * Complements GeneratedJavaParserConstants
8  */
9 public class TokenTypes {
isWhitespace(int kind)10     public static boolean isWhitespace(int kind) {
11         return getCategory(kind).isWhitespace();
12     }
13 
14     /**
15      * @deprecated use isEndOfLineToken
16      */
17     @Deprecated
isEndOfLineCharacter(int kind)18     public static boolean isEndOfLineCharacter(int kind) {
19         return isEndOfLineToken(kind);
20     }
21 
isEndOfLineToken(int kind)22     public static boolean isEndOfLineToken(int kind) {
23         return getCategory(kind).isEndOfLine();
24     }
25 
isWhitespaceOrComment(int kind)26     public static boolean isWhitespaceOrComment(int kind) {
27         return getCategory(kind).isWhitespaceOrComment();
28     }
29 
isSpaceOrTab(int kind)30     public static boolean isSpaceOrTab(int kind) {
31         return getCategory(kind).isWhitespaceButNotEndOfLine();
32     }
33 
isComment(int kind)34     public static boolean isComment(int kind) {
35         return getCategory(kind).isComment();
36     }
37 
38     /**
39      * @deprecated use eolTokenKind
40      */
41     @Deprecated
eolToken()42     public static int eolToken() {
43         return eolTokenKind();
44     }
45 
46     /**
47      * @return the kind of EOL token to use on the platform you're running on.
48      */
eolTokenKind()49     public static int eolTokenKind() {
50         if (EOL.equals("\n")) {
51             return UNIX_EOL;
52         }
53         if (EOL.equals("\r\n")) {
54             return WINDOWS_EOL;
55         }
56         if (EOL.equals("\r")) {
57             return OLD_MAC_EOL;
58         }
59         throw new AssertionError("Unknown EOL character sequence");
60     }
61 
62     /**
63      * @return the token kind for a single space.
64      */
spaceTokenKind()65     public static int spaceTokenKind() {
66         return SPACE;
67     }
68 
69     /**
70      * @deprecated use spaceTokenKind
71      */
72     @Deprecated
spaceToken()73     public static int spaceToken() {
74         return spaceTokenKind();
75     }
76 
77     /**
78      * Category of a token, a little more detailed than
79      * <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.5">The JLS</a>.
80      */
getCategory(int kind)81     public static JavaToken.Category getCategory(int kind) {
82         switch (kind) {
83             case WINDOWS_EOL:
84             case UNIX_EOL:
85             case OLD_MAC_EOL:
86                 return JavaToken.Category.EOL;
87             case EOF:
88             case SPACE:
89             case CTRL_Z:
90                 return JavaToken.Category.WHITESPACE_NO_EOL;
91             case SINGLE_LINE_COMMENT:
92             case JAVADOC_COMMENT:
93             case MULTI_LINE_COMMENT:
94                 return JavaToken.Category.COMMENT;
95             case ABSTRACT:
96             case ASSERT:
97             case BOOLEAN:
98             case BREAK:
99             case BYTE:
100             case CASE:
101             case CATCH:
102             case CHAR:
103             case CLASS:
104             case CONST:
105             case CONTINUE:
106             case _DEFAULT:
107             case DO:
108             case DOUBLE:
109             case ELSE:
110             case ENUM:
111             case EXTENDS:
112             case FALSE:
113             case FINAL:
114             case FINALLY:
115             case FLOAT:
116             case FOR:
117             case GOTO:
118             case IF:
119             case IMPLEMENTS:
120             case IMPORT:
121             case INSTANCEOF:
122             case INT:
123             case INTERFACE:
124             case LONG:
125             case NATIVE:
126             case NEW:
127             case NULL:
128             case PACKAGE:
129             case PRIVATE:
130             case PROTECTED:
131             case PUBLIC:
132             case RETURN:
133             case SHORT:
134             case STATIC:
135             case STRICTFP:
136             case SUPER:
137             case SWITCH:
138             case SYNCHRONIZED:
139             case THIS:
140             case THROW:
141             case THROWS:
142             case TRANSIENT:
143             case TRUE:
144             case TRY:
145             case VOID:
146             case VOLATILE:
147             case WHILE:
148             case REQUIRES:
149             case TO:
150             case WITH:
151             case OPEN:
152             case OPENS:
153             case USES:
154             case MODULE:
155             case EXPORTS:
156             case PROVIDES:
157             case TRANSITIVE:
158                 return JavaToken.Category.KEYWORD;
159             case LONG_LITERAL:
160             case INTEGER_LITERAL:
161             case DECIMAL_LITERAL:
162             case HEX_LITERAL:
163             case OCTAL_LITERAL:
164             case BINARY_LITERAL:
165             case FLOATING_POINT_LITERAL:
166             case DECIMAL_FLOATING_POINT_LITERAL:
167             case DECIMAL_EXPONENT:
168             case HEXADECIMAL_FLOATING_POINT_LITERAL:
169             case HEXADECIMAL_EXPONENT:
170             case CHARACTER_LITERAL:
171             case STRING_LITERAL:
172                 return JavaToken.Category.LITERAL;
173             case IDENTIFIER:
174                 return JavaToken.Category.IDENTIFIER;
175             case LPAREN:
176             case RPAREN:
177             case LBRACE:
178             case RBRACE:
179             case LBRACKET:
180             case RBRACKET:
181             case SEMICOLON:
182             case COMMA:
183             case DOT:
184             case AT:
185                 return JavaToken.Category.SEPARATOR;
186             case ASSIGN:
187             case LT:
188             case BANG:
189             case TILDE:
190             case HOOK:
191             case COLON:
192             case EQ:
193             case LE:
194             case GE:
195             case NE:
196             case SC_OR:
197             case SC_AND:
198             case INCR:
199             case DECR:
200             case PLUS:
201             case MINUS:
202             case STAR:
203             case SLASH:
204             case BIT_AND:
205             case BIT_OR:
206             case XOR:
207             case REM:
208             case LSHIFT:
209             case PLUSASSIGN:
210             case MINUSASSIGN:
211             case STARASSIGN:
212             case SLASHASSIGN:
213             case ANDASSIGN:
214             case ORASSIGN:
215             case XORASSIGN:
216             case REMASSIGN:
217             case LSHIFTASSIGN:
218             case RSIGNEDSHIFTASSIGN:
219             case RUNSIGNEDSHIFTASSIGN:
220             case ELLIPSIS:
221             case ARROW:
222             case DOUBLECOLON:
223             case RUNSIGNEDSHIFT:
224             case RSIGNEDSHIFT:
225             case GT:
226                 return JavaToken.Category.OPERATOR;
227             // The following are tokens that are only used internally by the lexer
228             case ENTER_JAVADOC_COMMENT:
229             case ENTER_MULTILINE_COMMENT:
230             case COMMENT_CONTENT:
231             case HEX_DIGITS:
232             case LETTER:
233             case UNICODE_ESCAPE:
234             case PART_LETTER:
235             default:
236                 throw new AssertionError("Invalid token kind " + kind);
237         }
238     }
239 }
240