18
18
*/
19
19
package org .netbeans .modules .php .editor .lexer .utils ;
20
20
21
+ import java .util .Collection ;
22
+ import java .util .List ;
23
+ import java .util .Set ;
24
+ import org .netbeans .api .annotations .common .NullAllowed ;
21
25
import org .netbeans .api .lexer .Token ;
22
26
import org .netbeans .api .lexer .TokenUtilities ;
23
27
import org .netbeans .modules .php .editor .lexer .PHPTokenId ;
24
28
25
29
public final class LexerUtils {
26
30
31
+ private static final Collection <PHPTokenId > WS_COMMENT_TOKENS = Set .of (
32
+ PHPTokenId .WHITESPACE ,
33
+ PHPTokenId .PHPDOC_COMMENT , PHPTokenId .PHPDOC_COMMENT_END , PHPTokenId .PHPDOC_COMMENT_START ,
34
+ PHPTokenId .PHP_COMMENT , PHPTokenId .PHP_COMMENT_END , PHPTokenId .PHP_COMMENT_START ,
35
+ PHPTokenId .PHP_LINE_COMMENT
36
+ );
37
+ private static final Collection <PHPTokenId > VISIBILITY_TOKENS = Set .of (
38
+ PHPTokenId .PHP_PUBLIC ,
39
+ PHPTokenId .PHP_PROTECTED ,
40
+ PHPTokenId .PHP_PRIVATE
41
+ );
42
+ private static final Collection <PHPTokenId > SET_VISIBILITY_TOKENS = Set .of (
43
+ PHPTokenId .PHP_PUBLIC_SET ,
44
+ PHPTokenId .PHP_PROTECTED_SET ,
45
+ PHPTokenId .PHP_PRIVATE_SET
46
+ );
47
+ private static final Collection <PHPTokenId > ALL_VISIBILITY_TOKENS = Set .of (
48
+ PHPTokenId .PHP_PUBLIC ,
49
+ PHPTokenId .PHP_PROTECTED ,
50
+ PHPTokenId .PHP_PRIVATE ,
51
+ PHPTokenId .PHP_PUBLIC_SET ,
52
+ PHPTokenId .PHP_PROTECTED_SET ,
53
+ PHPTokenId .PHP_PRIVATE_SET
54
+ );
55
+
27
56
private LexerUtils () {
28
57
}
29
58
@@ -46,4 +75,149 @@ public static boolean hasCurlyOpen(Token<? extends PHPTokenId> token) {
46
75
public static boolean isDollarCurlyOpen (Token <? extends PHPTokenId > token ) {
47
76
return token .id () == PHPTokenId .PHP_TOKEN && TokenUtilities .textEquals (token .text (), "${" ); // NOI18N
48
77
}
78
+
79
+ /**
80
+ * Check whether a token is the open parenthesis ("(").
81
+ *
82
+ * @param token a token
83
+ * @return {@code true} if a token is "(", {@code false} otherwise
84
+ */
85
+ public static boolean isOpenParen (Token <? extends PHPTokenId > token ) {
86
+ return token .id () == PHPTokenId .PHP_TOKEN && TokenUtilities .textEquals (token .text (), "(" ); // NOI18N
87
+ }
88
+
89
+ /**
90
+ * Check whether a token is the close parenthesis (")").
91
+ *
92
+ * @param token a token
93
+ * @return {@code true} if a token is ")", {@code false} otherwise
94
+ */
95
+ public static boolean isCloseParen (Token <? extends PHPTokenId > token ) {
96
+ return token .id () == PHPTokenId .PHP_TOKEN && TokenUtilities .textEquals (token .text (), ")" ); // NOI18N
97
+ }
98
+
99
+ /**
100
+ * Check whether a token is the open bracket ("[").
101
+ *
102
+ * @param token a token
103
+ * @return {@code true} if a token is "[", {@code false} otherwise
104
+ */
105
+ public static boolean isOpenBracket (Token <? extends PHPTokenId > token ) {
106
+ return token .id () == PHPTokenId .PHP_TOKEN && TokenUtilities .textEquals (token .text (), "[" ); // NOI18N
107
+ }
108
+
109
+ /**
110
+ * Check whether a token is the close bracket ("]").
111
+ *
112
+ * @param token a token
113
+ * @return {@code true} if a token is "]", {@code false} otherwise
114
+ */
115
+ public static boolean isCloseBracket (Token <? extends PHPTokenId > token ) {
116
+ return token .id () == PHPTokenId .PHP_TOKEN && TokenUtilities .textEquals (token .text (), "]" ); // NOI18N
117
+ }
118
+
119
+ /**
120
+ * Check whether a token is the comma (",").
121
+ *
122
+ * @param token a token
123
+ * @return {@code true} if a token is ",", {@code false} otherwise
124
+ */
125
+ public static boolean isComma (Token <? extends PHPTokenId > token ) {
126
+ return token .id () == PHPTokenId .PHP_TOKEN && TokenUtilities .textEquals (token .text (), "," ); // NOI18N
127
+ }
128
+
129
+ /**
130
+ * Check whether a token is the colon (":").
131
+ *
132
+ * @param token a token
133
+ * @return {@code true} if a token is ":", {@code false} otherwise
134
+ */
135
+ public static boolean isColon (Token <? extends PHPTokenId > token ) {
136
+ return token .id () == PHPTokenId .PHP_TOKEN && TokenUtilities .textEquals (token .text (), ":" ); // NOI18N
137
+ }
138
+
139
+ /**
140
+ * Check whether a token is the colon (":").
141
+ *
142
+ * @param token a token
143
+ * @return {@code true} if a token is ":", {@code false} otherwise
144
+ */
145
+ public static boolean isEqual (Token <? extends PHPTokenId > token ) {
146
+ return token .id () == PHPTokenId .PHP_OPERATOR && TokenUtilities .textEquals (token .text (), "=" ); // NOI18N
147
+ }
148
+
149
+ /**
150
+ * Check whether a token is the double arrow operator ("=>").
151
+ *
152
+ * @param token a token
153
+ * @return {@code true} if a token is "=>", {@code false} otherwise
154
+ */
155
+ public static boolean isDoubleArrow (Token <? extends PHPTokenId > token ) {
156
+ return token .id () == PHPTokenId .PHP_OPERATOR && TokenUtilities .textEquals (token .text (), "=>" ); // NOI18N
157
+ }
158
+
159
+ /**
160
+ * Check whether a token is a whitespace or a comments.
161
+ *
162
+ * @param token a token
163
+ * @return {@code true} if a token is a whitespace or a comment,
164
+ * {@code false} otherwise
165
+ */
166
+ public static boolean isWhitespaceOrCommentToken (Token <? extends PHPTokenId > token ) {
167
+ return token == null ? false : WS_COMMENT_TOKENS .contains (token .id ());
168
+ }
169
+
170
+ /**
171
+ * Check whether a token is a visibility token ({@code public},
172
+ * {@code protected}, {@code private}).
173
+ *
174
+ * @param token a token can be {@code null}
175
+ * @return {@code true} if it is a visibility token, {@code false} otherwise
176
+ */
177
+ public static boolean isVisibilityToken (@ NullAllowed Token <? extends PHPTokenId > token ) {
178
+ return token == null ? false : VISIBILITY_TOKENS .contains (token .id ());
179
+ }
180
+
181
+ /**
182
+ * Check whether a token is a set visibility token ({@code public(set)},
183
+ * {@code protected(set)}, {@code private(set)}).
184
+ *
185
+ * @param token a token can be {@code null}
186
+ * @return {@code true} if it is a set visibility token, {@code false}
187
+ * otherwise
188
+ */
189
+ public static boolean isSetVisibilityToken (@ NullAllowed Token <? extends PHPTokenId > token ) {
190
+ return token == null ? false : SET_VISIBILITY_TOKENS .contains (token .id ());
191
+ }
192
+
193
+ /**
194
+ * Check whether a token is one of all visibility tokens ({@code public},
195
+ * {@code protected}, {@code private}), ({@code public(set)},
196
+ * {@code protected(set)}, {@code private(set)}).
197
+ *
198
+ * @param token a token can be {@code null}
199
+ * @return {@code true} if it is one of all visibility tokens, {@code false}
200
+ * otherwise
201
+ */
202
+ public static boolean isGetOrSetVisibilityToken (@ NullAllowed Token <? extends PHPTokenId > token ) {
203
+ return token == null ? false : ALL_VISIBILITY_TOKENS .contains (token .id ());
204
+ }
205
+
206
+ /**
207
+ * Get whitespace and comment token ids.
208
+ *
209
+ * @return whitespace and comment token ids.
210
+ */
211
+ public static List <PHPTokenId > getWSCommentTokens () {
212
+ return List .copyOf (WS_COMMENT_TOKENS );
213
+ }
214
+
215
+ /**
216
+ * Get all visibility token ids.
217
+ *
218
+ * @return all visibility token ids
219
+ */
220
+ public static List <PHPTokenId > getAllVisibilityTokens () {
221
+ return List .copyOf (ALL_VISIBILITY_TOKENS );
222
+ }
49
223
}
0 commit comments