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