a800c2b09ee935973c1f3c9c0413410ad612c549
[idea/community.git] / plugins / groovy / groovy-psi / src / org / jetbrains / plugins / groovy / lang / lexer / TokenSets.java
1 /*
2  * Copyright 2000-2017 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.jetbrains.plugins.groovy.lang.lexer;
18
19 import com.intellij.psi.TokenType;
20 import com.intellij.psi.tree.IElementType;
21 import com.intellij.psi.tree.TokenSet;
22 import com.intellij.util.containers.hash.HashMap;
23 import org.jetbrains.plugins.groovy.lang.groovydoc.lexer.GroovyDocTokenTypes;
24 import org.jetbrains.plugins.groovy.lang.groovydoc.parser.GroovyDocElementTypes;
25 import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
26
27 import java.util.Map;
28
29 /**
30  * Utility classdef, tha contains various useful TokenSets
31  *
32  * @author ilyas
33  */
34 public abstract class TokenSets {
35
36   public static final TokenSet COMMENTS_TOKEN_SET = TokenSet.create(
37     GroovyTokenTypes.mSH_COMMENT,
38     GroovyTokenTypes.mSL_COMMENT,
39     GroovyTokenTypes.mML_COMMENT,
40     GroovyDocElementTypes.GROOVY_DOC_COMMENT
41   );
42   public static final TokenSet ALL_COMMENT_TOKENS = TokenSet.orSet(COMMENTS_TOKEN_SET, GroovyDocTokenTypes.GROOVY_DOC_TOKENS);
43
44   public static final TokenSet SEPARATORS = TokenSet.create(
45     GroovyTokenTypes.mNLS,
46     GroovyTokenTypes.mSEMI
47   );
48
49
50   public static final TokenSet WHITE_SPACE_TOKEN_SET = TokenSet.create(
51       TokenType.WHITE_SPACE
52   );
53
54   public static final TokenSet NUMBERS = TokenSet.create(
55     GroovyTokenTypes.mNUM_INT,
56     GroovyTokenTypes.mNUM_BIG_DECIMAL,
57     GroovyTokenTypes.mNUM_BIG_INT,
58     GroovyTokenTypes.mNUM_DOUBLE,
59     GroovyTokenTypes.mNUM_FLOAT,
60     GroovyTokenTypes.mNUM_LONG);
61
62
63   public static final TokenSet CONSTANTS = TokenSet.create(
64     GroovyTokenTypes.mNUM_INT,
65     GroovyTokenTypes.mNUM_BIG_DECIMAL,
66     GroovyTokenTypes.mNUM_BIG_INT,
67     GroovyTokenTypes.mNUM_DOUBLE,
68     GroovyTokenTypes.mNUM_FLOAT,
69     GroovyTokenTypes.mNUM_LONG,
70     GroovyTokenTypes.kTRUE,
71     GroovyTokenTypes.kFALSE,
72     GroovyTokenTypes.kNULL,
73     GroovyTokenTypes.mSTRING_LITERAL,
74     GroovyTokenTypes.mGSTRING_LITERAL,
75     GroovyTokenTypes.mREGEX_LITERAL,
76     GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL
77   );
78
79   public static final TokenSet BUILT_IN_TYPES = TokenSet.create(
80     GroovyTokenTypes.kVOID,
81     GroovyTokenTypes.kBOOLEAN,
82     GroovyTokenTypes.kBYTE,
83     GroovyTokenTypes.kCHAR,
84     GroovyTokenTypes.kSHORT,
85     GroovyTokenTypes.kINT,
86     GroovyTokenTypes.kFLOAT,
87     GroovyTokenTypes.kLONG,
88     GroovyTokenTypes.kDOUBLE
89   );
90
91   public static final TokenSet PROPERTY_NAMES =
92     TokenSet.create(GroovyTokenTypes.mIDENT, GroovyTokenTypes.mSTRING_LITERAL, GroovyTokenTypes.mGSTRING_LITERAL,
93                     GroovyTokenTypes.mREGEX_LITERAL, GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL);
94
95   public static final TokenSet KEYWORDS = TokenSet.create(GroovyTokenTypes.kABSTRACT, GroovyTokenTypes.kAS, GroovyTokenTypes.kASSERT,
96                                                           GroovyTokenTypes.kBOOLEAN, GroovyTokenTypes.kBREAK, GroovyTokenTypes.kBYTE,
97                                                           GroovyTokenTypes.kCASE, GroovyTokenTypes.kCATCH, GroovyTokenTypes.kCHAR,
98                                                           GroovyTokenTypes.kCLASS,
99                                                           GroovyTokenTypes.kCONTINUE, GroovyTokenTypes.kDEF, GroovyTokenTypes.kDEFAULT,
100                                                           GroovyTokenTypes.kDO, GroovyTokenTypes.kDOUBLE, GroovyTokenTypes.kELSE,
101                                                           GroovyTokenTypes.kEXTENDS, GroovyTokenTypes.kENUM, GroovyTokenTypes.kFALSE,
102                                                           GroovyTokenTypes.kFINAL,
103                                                           GroovyTokenTypes.kFLOAT, GroovyTokenTypes.kFOR, GroovyTokenTypes.kFINALLY,
104                                                           GroovyTokenTypes.kIF, GroovyTokenTypes.kIMPLEMENTS, GroovyTokenTypes.kIMPORT,
105                                                           GroovyTokenTypes.kIN, GroovyTokenTypes.kINSTANCEOF, GroovyTokenTypes.kINT,
106                                                           GroovyTokenTypes.kINTERFACE, GroovyTokenTypes.kLONG, GroovyTokenTypes.kNATIVE,
107                                                           GroovyTokenTypes.kNEW, GroovyTokenTypes.kNULL, GroovyTokenTypes.kPACKAGE,
108                                                           GroovyTokenTypes.kPRIVATE, GroovyTokenTypes.kPROTECTED, GroovyTokenTypes.kPUBLIC,
109                                                           GroovyTokenTypes.kRETURN, GroovyTokenTypes.kSHORT, GroovyTokenTypes.kSTATIC,
110                                                           GroovyTokenTypes.kSTRICTFP, GroovyTokenTypes.kSUPER, GroovyTokenTypes.kSWITCH,
111                                                           GroovyTokenTypes.kSYNCHRONIZED, GroovyTokenTypes.kTHIS,
112                                                           GroovyTokenTypes.kTHROW, GroovyTokenTypes.kTHROWS, GroovyTokenTypes.kTRAIT,
113                                                           GroovyTokenTypes.kTRANSIENT, GroovyTokenTypes.kTRUE, GroovyTokenTypes.kTRY,
114                                                           GroovyTokenTypes.kVOID, GroovyTokenTypes.kVOLATILE, GroovyTokenTypes.kWHILE);
115
116   public static final TokenSet REFERENCE_NAMES = TokenSet.orSet(KEYWORDS, PROPERTY_NAMES, NUMBERS);
117   public static final TokenSet REFERENCE_NAMES_WITHOUT_NUMBERS = TokenSet.orSet(KEYWORDS, PROPERTY_NAMES);
118
119   public static final TokenSet REFERENCE_NAME_PREFIXES = TokenSet.orSet(NUMBERS, KEYWORDS, TokenSet.create(GroovyTokenTypes.mIDENT,
120                                                                                                            GroovyTokenTypes.mSTRING_LITERAL,
121                                                                                                            GroovyTokenTypes.mGSTRING_LITERAL,
122                                                                                                            GroovyTokenTypes.mGSTRING_BEGIN,
123                                                                                                            GroovyTokenTypes.mREGEX_BEGIN,
124                                                                                                            GroovyTokenTypes.mDOLLAR_SLASH_REGEX_BEGIN,
125                                                                                                            GroovyTokenTypes.mAT));
126
127
128   public static final TokenSet VISIBILITY_MODIFIERS = TokenSet.create(
129     GroovyTokenTypes.kPRIVATE,
130     GroovyTokenTypes.kPROTECTED,
131     GroovyTokenTypes.kPUBLIC
132   );
133
134   public static final TokenSet MODIFIERS = TokenSet.create(
135     GroovyTokenTypes.kABSTRACT,
136     GroovyTokenTypes.kPRIVATE,
137     GroovyTokenTypes.kPUBLIC,
138     GroovyTokenTypes.kPROTECTED,
139     GroovyTokenTypes.kSTATIC,
140     GroovyTokenTypes.kTRANSIENT,
141     GroovyTokenTypes.kFINAL,
142     GroovyTokenTypes.kABSTRACT,
143     GroovyTokenTypes.kNATIVE,
144     GroovyTokenTypes.kSYNCHRONIZED,
145     GroovyTokenTypes.kSTRICTFP,
146     GroovyTokenTypes.kVOLATILE,
147     GroovyTokenTypes.kSTRICTFP,
148     GroovyTokenTypes.kDEF
149   );
150
151   public static final TokenSet STRING_LITERALS = TokenSet.create(
152     GroovyTokenTypes.mSTRING_LITERAL,
153     GroovyTokenTypes.mGSTRING_LITERAL,
154     GroovyTokenTypes.mGSTRING_BEGIN,
155     GroovyTokenTypes.mGSTRING_CONTENT,
156     GroovyTokenTypes.mGSTRING_END,
157     GroovyTokenTypes.mREGEX_LITERAL,
158     GroovyTokenTypes.mREGEX_BEGIN,
159     GroovyTokenTypes.mREGEX_CONTENT,
160     GroovyTokenTypes.mREGEX_END,
161     GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL,
162     GroovyTokenTypes.mDOLLAR_SLASH_REGEX_BEGIN,
163     GroovyTokenTypes.mDOLLAR_SLASH_REGEX_CONTENT,
164     GroovyTokenTypes.mDOLLAR_SLASH_REGEX_END
165   );
166
167   public static final TokenSet GSTRING_CONTENT_PARTS = TokenSet.create(GroovyElementTypes.GSTRING_CONTENT,
168                                                                        GroovyElementTypes.GSTRING_INJECTION);
169
170   public static final TokenSet FOR_IN_DELIMITERS = TokenSet.create(GroovyTokenTypes.kIN, GroovyTokenTypes.mCOLON);
171
172   public static final TokenSet RELATIONS = TokenSet.create(
173     GroovyTokenTypes.mLT,
174     GroovyTokenTypes.mGT,
175     GroovyTokenTypes.mLE,
176     GroovyTokenTypes.mGE,
177     GroovyTokenTypes.kIN
178   );
179   public static final TokenSet WHITE_SPACES_SET = TokenSet.create(GroovyTokenTypes.mNLS, TokenType.WHITE_SPACE);
180
181   public static final TokenSet COMMENT_SET = TokenSet.create(GroovyTokenTypes.mML_COMMENT, GroovyTokenTypes.mSH_COMMENT,
182                                                              GroovyTokenTypes.mSL_COMMENT, GroovyDocElementTypes.GROOVY_DOC_COMMENT);
183
184   public static final TokenSet STRING_LITERAL_SET = TokenSet.create(GroovyTokenTypes.mSTRING_LITERAL, GroovyTokenTypes.mGSTRING_LITERAL,
185                                                                     GroovyTokenTypes.mREGEX_LITERAL,
186                                                                     GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL);
187
188   public static final TokenSet BRACES = TokenSet.create(GroovyTokenTypes.mLBRACK, GroovyTokenTypes.mRBRACK, GroovyTokenTypes.mLPAREN,
189                                                         GroovyTokenTypes.mRPAREN, GroovyTokenTypes.mLCURLY, GroovyTokenTypes.mRCURLY);
190
191   public static final TokenSet UNARY_OP_SET = TokenSet.create(GroovyTokenTypes.mBNOT, GroovyTokenTypes.mLNOT, GroovyTokenTypes.mMINUS,
192                                                               GroovyTokenTypes.mDEC, GroovyTokenTypes.mPLUS, GroovyTokenTypes.mINC);
193
194   public static final TokenSet POSTFIX_UNARY_OP_SET = TokenSet.create(GroovyTokenTypes.mDEC, GroovyTokenTypes.mINC);
195
196   public static final TokenSet BINARY_OP_SET = TokenSet.create(GroovyTokenTypes.mBAND, GroovyTokenTypes.mBOR, GroovyTokenTypes.mBXOR,
197                                                                GroovyTokenTypes.mDIV, GroovyTokenTypes.mEQUAL, GroovyTokenTypes.mGE,
198                                                                GroovyTokenTypes.mGT, GroovyTokenTypes.mLOR, GroovyTokenTypes.mLT,
199                                                                GroovyTokenTypes.mLE, GroovyTokenTypes.mMINUS, GroovyTokenTypes.kAS,
200                                                                GroovyTokenTypes.kIN,
201                                                                GroovyTokenTypes.mMOD, GroovyTokenTypes.mPLUS, GroovyTokenTypes.mSTAR,
202                                                                GroovyTokenTypes.mSTAR_STAR, GroovyTokenTypes.mNOT_EQUAL,
203                                                                GroovyTokenTypes.mCOMPARE_TO, GroovyTokenTypes.mLAND,
204                                                                GroovyTokenTypes.kINSTANCEOF,
205                                                                GroovyElementTypes.COMPOSITE_LSHIFT_SIGN,
206                                                                GroovyElementTypes.COMPOSITE_RSHIFT_SIGN,
207                                                                GroovyElementTypes.COMPOSITE_TRIPLE_SHIFT_SIGN,
208                                                                GroovyTokenTypes.mREGEX_FIND, GroovyTokenTypes.mREGEX_MATCH,
209                                                                GroovyTokenTypes.mRANGE_INCLUSIVE, GroovyTokenTypes.mRANGE_EXCLUSIVE);
210
211   public static final TokenSet PARENTHESIZED_BINARY_OP_SET = TokenSet.create(GroovyTokenTypes.mEQUAL, GroovyTokenTypes.mNOT_EQUAL);
212
213   public static final TokenSet ASSOCIATIVE_BINARY_OP_SET = TokenSet.create(GroovyTokenTypes.mBAND, GroovyTokenTypes.mBOR,
214                                                                            GroovyTokenTypes.mBXOR,
215                                                                            GroovyTokenTypes.mLOR, GroovyTokenTypes.mPLUS,
216                                                                            GroovyTokenTypes.mSTAR,
217                                                                            GroovyTokenTypes.mLAND);
218
219
220   public static final TokenSet BINARY_EXPRESSIONS = TokenSet.create(GroovyElementTypes.ADDITIVE_EXPRESSION,
221                                                                     GroovyElementTypes.MULTIPLICATIVE_EXPRESSION,
222                                                                     GroovyElementTypes.POWER_EXPRESSION,
223                                                                     GroovyElementTypes.POWER_EXPRESSION_SIMPLE,
224                                                                     GroovyElementTypes.LOGICAL_OR_EXPRESSION,
225                                                                     GroovyElementTypes.LOGICAL_AND_EXPRESSION,
226                                                                     GroovyElementTypes.INCLUSIVE_OR_EXPRESSION,
227                                                                     GroovyElementTypes.EXCLUSIVE_OR_EXPRESSION,
228                                                                     GroovyElementTypes.AND_EXPRESSION,
229                                                                     GroovyElementTypes.REGEX_FIND_EXPRESSION,
230                                                                     GroovyElementTypes.REGEX_MATCH_EXPRESSION,
231                                                                     GroovyElementTypes.EQUALITY_EXPRESSION,
232                                                                     GroovyElementTypes.RELATIONAL_EXPRESSION,
233                                                                     GroovyElementTypes.SHIFT_EXPRESSION,
234                                                                     GroovyElementTypes.RANGE_EXPRESSION);
235
236   public static final TokenSet DOTS = TokenSet.create(GroovyTokenTypes.mSPREAD_DOT, GroovyTokenTypes.mOPTIONAL_DOT,
237                                                       GroovyTokenTypes.mMEMBER_POINTER, GroovyTokenTypes.mDOT);
238
239   public static final TokenSet WHITE_SPACES_OR_COMMENTS = TokenSet.orSet(WHITE_SPACES_SET, COMMENT_SET);
240
241   public static final Map<IElementType, IElementType> ASSIGNMENTS_TO_OPERATORS = new HashMap<>();
242   static {
243     ASSIGNMENTS_TO_OPERATORS.put(GroovyTokenTypes.mMINUS_ASSIGN, GroovyTokenTypes.mMINUS);
244     ASSIGNMENTS_TO_OPERATORS.put(GroovyTokenTypes.mPLUS_ASSIGN, GroovyTokenTypes.mPLUS);
245     ASSIGNMENTS_TO_OPERATORS.put(GroovyTokenTypes.mDIV_ASSIGN, GroovyTokenTypes.mDIV);
246     ASSIGNMENTS_TO_OPERATORS.put(GroovyTokenTypes.mSTAR_ASSIGN, GroovyTokenTypes.mSTAR);
247     ASSIGNMENTS_TO_OPERATORS.put(GroovyTokenTypes.mMOD_ASSIGN, GroovyTokenTypes.mMOD);
248     ASSIGNMENTS_TO_OPERATORS.put(GroovyTokenTypes.mSL_ASSIGN, GroovyElementTypes.COMPOSITE_LSHIFT_SIGN);
249     ASSIGNMENTS_TO_OPERATORS.put(GroovyTokenTypes.mSR_ASSIGN, GroovyElementTypes.COMPOSITE_RSHIFT_SIGN);
250     ASSIGNMENTS_TO_OPERATORS.put(GroovyTokenTypes.mBSR_ASSIGN, GroovyElementTypes.COMPOSITE_TRIPLE_SHIFT_SIGN);
251     ASSIGNMENTS_TO_OPERATORS.put(GroovyTokenTypes.mBAND_ASSIGN, GroovyTokenTypes.mBAND);
252     ASSIGNMENTS_TO_OPERATORS.put(GroovyTokenTypes.mBOR_ASSIGN, GroovyTokenTypes.mBOR);
253     ASSIGNMENTS_TO_OPERATORS.put(GroovyTokenTypes.mBXOR_ASSIGN, GroovyTokenTypes.mBXOR);
254     ASSIGNMENTS_TO_OPERATORS.put(GroovyTokenTypes.mSTAR_STAR_ASSIGN, GroovyTokenTypes.mSTAR_STAR);
255   }
256
257   public static final TokenSet ASSIGNMENTS = TokenSet.create(
258     GroovyTokenTypes.mASSIGN,
259     GroovyTokenTypes.mPLUS_ASSIGN,
260     GroovyTokenTypes.mMINUS_ASSIGN,
261     GroovyTokenTypes.mSTAR_ASSIGN,
262     GroovyTokenTypes.mDIV_ASSIGN,
263     GroovyTokenTypes.mMOD_ASSIGN,
264     GroovyTokenTypes.mSL_ASSIGN,
265     GroovyTokenTypes.mSR_ASSIGN,
266     GroovyTokenTypes.mBSR_ASSIGN,
267     GroovyTokenTypes.mBAND_ASSIGN,
268     GroovyTokenTypes.mBOR_ASSIGN,
269     GroovyTokenTypes.mBXOR_ASSIGN,
270     GroovyTokenTypes.mSTAR_STAR_ASSIGN
271   );
272
273   public static final TokenSet SHIFT_SIGNS = TokenSet.create(GroovyElementTypes.COMPOSITE_LSHIFT_SIGN,
274                                                              GroovyElementTypes.COMPOSITE_RSHIFT_SIGN,
275                                                              GroovyElementTypes.COMPOSITE_TRIPLE_SHIFT_SIGN);
276   public static final TokenSet CODE_REFERENCE_ELEMENT_NAME_TOKENS = TokenSet.create(GroovyTokenTypes.mIDENT, GroovyTokenTypes.kDEF,
277                                                                                     GroovyTokenTypes.kIN, GroovyTokenTypes.kAS,
278                                                                                     GroovyTokenTypes.kTRAIT);
279
280   public static final TokenSet BLOCK_SET = TokenSet.create(GroovyElementTypes.CLOSABLE_BLOCK, GroovyElementTypes.BLOCK_STATEMENT,
281                                                            GroovyElementTypes.CONSTRUCTOR_BODY, GroovyElementTypes.OPEN_BLOCK,
282                                                            GroovyElementTypes.ENUM_BODY, GroovyElementTypes.CLASS_BODY);
283   public static final TokenSet METHOD_DEFS = TokenSet.create(GroovyElementTypes.METHOD_DEFINITION,
284                                                              GroovyElementTypes.CONSTRUCTOR_DEFINITION,
285                                                              GroovyElementTypes.ANNOTATION_METHOD);
286   public static final TokenSet VARIABLES = TokenSet.create(GroovyElementTypes.VARIABLE, GroovyElementTypes.FIELD);
287   public static final TokenSet TYPE_ELEMENTS = TokenSet.create(GroovyElementTypes.CLASS_TYPE_ELEMENT, GroovyElementTypes.ARRAY_TYPE,
288                                                                GroovyElementTypes.BUILT_IN_TYPE, GroovyElementTypes.TYPE_ARGUMENT,
289                                                                GroovyElementTypes.DISJUNCTION_TYPE_ELEMENT);
290
291
292   public static final TokenSet TYPE_DEFINITIONS = TokenSet.create(GroovyElementTypes.CLASS_DEFINITION,
293                                                                   GroovyElementTypes.ENUM_DEFINITION,
294                                                                   GroovyElementTypes.INTERFACE_DEFINITION,
295                                                                   GroovyElementTypes.ANNOTATION_DEFINITION,
296                                                                   GroovyElementTypes.TRAIT_DEFINITION);
297
298   public static final TokenSet METHOD_IDENTIFIERS = TokenSet.create(
299     GroovyTokenTypes.mIDENT,
300     GroovyTokenTypes.mGSTRING_LITERAL,
301     GroovyTokenTypes.mSTRING_LITERAL
302   );
303 }