[groovy] cleanup: make TokenSets interface
[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.ContainerUtil;
23
24 import java.util.Map;
25
26 import static org.jetbrains.plugins.groovy.lang.groovydoc.lexer.GroovyDocTokenTypes.GROOVY_DOC_TOKENS;
27 import static org.jetbrains.plugins.groovy.lang.groovydoc.parser.GroovyDocElementTypes.GROOVY_DOC_COMMENT;
28 import static org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes.*;
29 import static org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes.*;
30
31 public interface TokenSets {
32
33   TokenSet COMMENTS_TOKEN_SET = TokenSet.create(
34     mSH_COMMENT,
35     mSL_COMMENT,
36     mML_COMMENT,
37     GROOVY_DOC_COMMENT
38   );
39
40   TokenSet ALL_COMMENT_TOKENS = TokenSet.orSet(COMMENTS_TOKEN_SET, GROOVY_DOC_TOKENS);
41
42   TokenSet SEPARATORS = TokenSet.create(mNLS, mSEMI);
43
44   TokenSet WHITE_SPACE_TOKEN_SET = TokenSet.create(TokenType.WHITE_SPACE);
45
46   TokenSet NUMBERS = TokenSet.create(
47     mNUM_INT,
48     mNUM_BIG_DECIMAL,
49     mNUM_BIG_INT,
50     mNUM_DOUBLE,
51     mNUM_FLOAT,
52     mNUM_LONG
53   );
54
55   TokenSet CONSTANTS = TokenSet.create(
56     mNUM_INT,
57     mNUM_BIG_DECIMAL,
58     mNUM_BIG_INT,
59     mNUM_DOUBLE,
60     mNUM_FLOAT,
61     mNUM_LONG,
62     kTRUE,
63     kFALSE,
64     kNULL,
65     mSTRING_LITERAL,
66     mGSTRING_LITERAL,
67     mREGEX_LITERAL,
68     mDOLLAR_SLASH_REGEX_LITERAL
69   );
70
71   TokenSet BUILT_IN_TYPES = TokenSet.create(
72     kVOID,
73     kBOOLEAN,
74     kBYTE,
75     kCHAR,
76     kSHORT,
77     kINT,
78     kFLOAT,
79     kLONG,
80     kDOUBLE
81   );
82
83   TokenSet PROPERTY_NAMES = TokenSet.create(
84     mIDENT,
85     mSTRING_LITERAL,
86     mGSTRING_LITERAL,
87     mREGEX_LITERAL,
88     mDOLLAR_SLASH_REGEX_LITERAL
89   );
90
91   TokenSet KEYWORDS = TokenSet.create(
92     kABSTRACT, kAS, kASSERT,
93     kBOOLEAN, kBREAK, kBYTE,
94     kCASE, kCATCH, kCHAR, kCLASS, kCONTINUE,
95     kDEF, kDEFAULT, kDO, kDOUBLE,
96     kELSE, kEXTENDS, kENUM,
97     kFALSE, kFINAL, kFLOAT, kFOR, kFINALLY,
98     kIF, kIMPLEMENTS, kIMPORT, kIN, kINSTANCEOF, kINT, kINTERFACE,
99     kLONG,
100     kNATIVE, kNEW, kNULL,
101     kPACKAGE, kPRIVATE, kPROTECTED, kPUBLIC,
102     kRETURN,
103     kSHORT, kSTATIC, kSTRICTFP, kSUPER, kSWITCH,
104     kSYNCHRONIZED,
105     kTHIS, kTHROW, kTHROWS, kTRAIT, kTRANSIENT, kTRUE, kTRY,
106     kVOID, kVOLATILE,
107     kWHILE
108   );
109
110   TokenSet REFERENCE_NAMES = TokenSet.orSet(KEYWORDS, PROPERTY_NAMES, NUMBERS);
111   TokenSet REFERENCE_NAMES_WITHOUT_NUMBERS = TokenSet.orSet(KEYWORDS, PROPERTY_NAMES);
112   TokenSet REFERENCE_NAME_PREFIXES = TokenSet.orSet(NUMBERS, KEYWORDS, TokenSet.create(mIDENT,
113                                                                                        mSTRING_LITERAL,
114                                                                                        mGSTRING_LITERAL,
115                                                                                        mGSTRING_BEGIN,
116                                                                                        mREGEX_BEGIN,
117                                                                                        mDOLLAR_SLASH_REGEX_BEGIN,
118                                                                                        mAT));
119
120   TokenSet VISIBILITY_MODIFIERS = TokenSet.create(kPRIVATE, kPROTECTED, kPUBLIC);
121
122   TokenSet MODIFIERS = TokenSet.create(
123     kABSTRACT,
124     kPRIVATE,
125     kPUBLIC,
126     kPROTECTED,
127     kSTATIC,
128     kTRANSIENT,
129     kFINAL,
130     kABSTRACT,
131     kNATIVE,
132     kSYNCHRONIZED,
133     kSTRICTFP,
134     kVOLATILE,
135     kSTRICTFP,
136     kDEF
137   );
138
139   TokenSet STRING_LITERALS = TokenSet.create(
140     mSTRING_LITERAL,
141     mGSTRING_LITERAL,
142     mGSTRING_BEGIN,
143     mGSTRING_CONTENT,
144     mGSTRING_END,
145     mREGEX_LITERAL,
146     mREGEX_BEGIN,
147     mREGEX_CONTENT,
148     mREGEX_END,
149     mDOLLAR_SLASH_REGEX_LITERAL,
150     mDOLLAR_SLASH_REGEX_BEGIN,
151     mDOLLAR_SLASH_REGEX_CONTENT,
152     mDOLLAR_SLASH_REGEX_END
153   );
154
155   TokenSet GSTRING_CONTENT_PARTS = TokenSet.create(GSTRING_CONTENT, GSTRING_INJECTION);
156
157   TokenSet FOR_IN_DELIMITERS = TokenSet.create(kIN, mCOLON);
158
159   TokenSet RELATIONS = TokenSet.create(mLT, mGT, mLE, mGE, kIN);
160
161   TokenSet WHITE_SPACES_SET = TokenSet.create(mNLS, TokenType.WHITE_SPACE);
162
163   TokenSet COMMENT_SET = TokenSet.create(mML_COMMENT, mSH_COMMENT, mSL_COMMENT, GROOVY_DOC_COMMENT);
164
165   TokenSet STRING_LITERAL_SET = TokenSet.create(mSTRING_LITERAL, mGSTRING_LITERAL, mREGEX_LITERAL, mDOLLAR_SLASH_REGEX_LITERAL);
166
167   TokenSet BRACES = TokenSet.create(mLBRACK, mRBRACK, mLPAREN, mRPAREN, mLCURLY, mRCURLY);
168
169   TokenSet UNARY_OP_SET = TokenSet.create(mBNOT, mLNOT, mMINUS, mDEC, mPLUS, mINC);
170
171   TokenSet POSTFIX_UNARY_OP_SET = TokenSet.create(mDEC, mINC);
172
173   TokenSet BINARY_OP_SET = TokenSet.create(mBAND, mBOR, mBXOR,
174                                            mDIV, mEQUAL, mGE,
175                                            mGT, mLOR, mLT,
176                                            mLE, mMINUS, kAS,
177                                            kIN,
178                                            mMOD, mPLUS, mSTAR,
179                                            mSTAR_STAR, mNOT_EQUAL,
180                                            mCOMPARE_TO, mLAND,
181                                            kINSTANCEOF,
182                                            COMPOSITE_LSHIFT_SIGN,
183                                            COMPOSITE_RSHIFT_SIGN,
184                                            COMPOSITE_TRIPLE_SHIFT_SIGN,
185                                            mREGEX_FIND, mREGEX_MATCH,
186                                            mRANGE_INCLUSIVE, mRANGE_EXCLUSIVE);
187
188   TokenSet PARENTHESIZED_BINARY_OP_SET = TokenSet.create(mEQUAL, mNOT_EQUAL);
189
190   TokenSet ASSOCIATIVE_BINARY_OP_SET = TokenSet.create(mBAND, mBOR, mBXOR, mLOR, mPLUS, mSTAR, mLAND);
191
192   TokenSet BINARY_EXPRESSIONS = TokenSet.create(ADDITIVE_EXPRESSION,
193                                                 MULTIPLICATIVE_EXPRESSION,
194                                                 POWER_EXPRESSION,
195                                                 POWER_EXPRESSION_SIMPLE,
196                                                 LOGICAL_OR_EXPRESSION,
197                                                 LOGICAL_AND_EXPRESSION,
198                                                 INCLUSIVE_OR_EXPRESSION,
199                                                 EXCLUSIVE_OR_EXPRESSION,
200                                                 AND_EXPRESSION,
201                                                 REGEX_FIND_EXPRESSION,
202                                                 REGEX_MATCH_EXPRESSION,
203                                                 EQUALITY_EXPRESSION,
204                                                 RELATIONAL_EXPRESSION,
205                                                 SHIFT_EXPRESSION,
206                                                 RANGE_EXPRESSION);
207
208   TokenSet DOTS = TokenSet.create(mSPREAD_DOT, mOPTIONAL_DOT, mMEMBER_POINTER, mDOT);
209
210   TokenSet WHITE_SPACES_OR_COMMENTS = TokenSet.orSet(WHITE_SPACES_SET, COMMENT_SET);
211
212   Map<IElementType, IElementType> ASSIGNMENTS_TO_OPERATORS = new ContainerUtil.ImmutableMapBuilder<IElementType, IElementType>()
213     .put(mMINUS_ASSIGN, mMINUS)
214     .put(mPLUS_ASSIGN, mPLUS)
215     .put(mDIV_ASSIGN, mDIV)
216     .put(mSTAR_ASSIGN, mSTAR)
217     .put(mMOD_ASSIGN, mMOD)
218     .put(mSL_ASSIGN, COMPOSITE_LSHIFT_SIGN)
219     .put(mSR_ASSIGN, COMPOSITE_RSHIFT_SIGN)
220     .put(mBSR_ASSIGN, COMPOSITE_TRIPLE_SHIFT_SIGN)
221     .put(mBAND_ASSIGN, mBAND)
222     .put(mBOR_ASSIGN, mBOR)
223     .put(mBXOR_ASSIGN, mBXOR)
224     .put(mSTAR_STAR_ASSIGN, mSTAR_STAR)
225     .build();
226
227   TokenSet ASSIGNMENTS = TokenSet.create(
228     mASSIGN,
229     mPLUS_ASSIGN,
230     mMINUS_ASSIGN,
231     mSTAR_ASSIGN,
232     mDIV_ASSIGN,
233     mMOD_ASSIGN,
234     mSL_ASSIGN,
235     mSR_ASSIGN,
236     mBSR_ASSIGN,
237     mBAND_ASSIGN,
238     mBOR_ASSIGN,
239     mBXOR_ASSIGN,
240     mSTAR_STAR_ASSIGN
241   );
242
243   TokenSet SHIFT_SIGNS = TokenSet.create(COMPOSITE_LSHIFT_SIGN, COMPOSITE_RSHIFT_SIGN, COMPOSITE_TRIPLE_SHIFT_SIGN);
244
245   TokenSet CODE_REFERENCE_ELEMENT_NAME_TOKENS = TokenSet.create(mIDENT, kDEF, kIN, kAS, kTRAIT);
246
247   TokenSet BLOCK_SET = TokenSet.create(CLOSABLE_BLOCK, BLOCK_STATEMENT, CONSTRUCTOR_BODY, OPEN_BLOCK, ENUM_BODY, CLASS_BODY);
248
249   TokenSet METHOD_DEFS = TokenSet.create(METHOD_DEFINITION, CONSTRUCTOR_DEFINITION, ANNOTATION_METHOD);
250
251   TokenSet VARIABLES = TokenSet.create(VARIABLE, FIELD);
252
253   TokenSet TYPE_ELEMENTS = TokenSet.create(CLASS_TYPE_ELEMENT, ARRAY_TYPE, BUILT_IN_TYPE, TYPE_ARGUMENT, DISJUNCTION_TYPE_ELEMENT);
254
255   TokenSet TYPE_DEFINITIONS = TokenSet.create(
256     CLASS_DEFINITION,
257     ENUM_DEFINITION,
258     INTERFACE_DEFINITION,
259     ANNOTATION_DEFINITION,
260     TRAIT_DEFINITION
261   );
262
263   TokenSet METHOD_IDENTIFIERS = TokenSet.create(mIDENT, mGSTRING_LITERAL, mSTRING_LITERAL);
264 }