OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef RUNTIME_VM_TOKEN_H_ | 5 #ifndef RUNTIME_VM_TOKEN_H_ |
6 #define RUNTIME_VM_TOKEN_H_ | 6 #define RUNTIME_VM_TOKEN_H_ |
7 | 7 |
8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
9 #include "vm/allocation.h" | 9 #include "vm/allocation.h" |
10 | 10 |
(...skipping 24 matching lines...) Expand all Loading... |
35 #define DART_TOKEN_LIST(TOK) \ | 35 #define DART_TOKEN_LIST(TOK) \ |
36 TOK(kEOS, "", 0, kNoAttribute) \ | 36 TOK(kEOS, "", 0, kNoAttribute) \ |
37 \ | 37 \ |
38 TOK(kLPAREN, "(", 0, kNoAttribute) \ | 38 TOK(kLPAREN, "(", 0, kNoAttribute) \ |
39 TOK(kRPAREN, ")", 0, kNoAttribute) \ | 39 TOK(kRPAREN, ")", 0, kNoAttribute) \ |
40 TOK(kLBRACK, "[", 0, kNoAttribute) \ | 40 TOK(kLBRACK, "[", 0, kNoAttribute) \ |
41 TOK(kRBRACK, "]", 0, kNoAttribute) \ | 41 TOK(kRBRACK, "]", 0, kNoAttribute) \ |
42 TOK(kLBRACE, "{", 0, kNoAttribute) \ | 42 TOK(kLBRACE, "{", 0, kNoAttribute) \ |
43 TOK(kRBRACE, "}", 0, kNoAttribute) \ | 43 TOK(kRBRACE, "}", 0, kNoAttribute) \ |
44 TOK(kARROW, "=>", 0, kNoAttribute) \ | 44 TOK(kARROW, "=>", 0, kNoAttribute) \ |
45 TOK(kCOLON, ":", 0, kNoAttribute) \ | 45 TOK(kCOLON, ":", 0, kNoAttribute) \ |
46 TOK(kSEMICOLON, ";", 0, kNoAttribute) \ | 46 TOK(kSEMICOLON, ";", 0, kNoAttribute) \ |
47 TOK(kPERIOD, ".", 0, kNoAttribute) \ | 47 TOK(kPERIOD, ".", 0, kNoAttribute) \ |
48 TOK(kQM_PERIOD, "?.", 0, kNoAttribute) \ | 48 TOK(kQM_PERIOD, "?.", 0, kNoAttribute) \ |
49 TOK(kINCR, "++", 0, kNoAttribute) \ | 49 TOK(kINCR, "++", 0, kNoAttribute) \ |
50 TOK(kDECR, "--", 0, kNoAttribute) \ | 50 TOK(kDECR, "--", 0, kNoAttribute) \ |
51 \ | 51 \ |
52 /* Assignment operators. */ \ | 52 /* Assignment operators. */ \ |
53 /* Please update IsAssignmentOperator() if you make */ \ | 53 /* Please update IsAssignmentOperator() if you make */ \ |
54 /* any changes to this block. */ \ | 54 /* any changes to this block. */ \ |
55 TOK(kASSIGN, "=", 2, kNoAttribute) \ | 55 TOK(kASSIGN, "=", 2, kNoAttribute) \ |
56 TOK(kASSIGN_OR, "|=", 2, kNoAttribute) \ | 56 TOK(kASSIGN_OR, "|=", 2, kNoAttribute) \ |
57 TOK(kASSIGN_XOR, "^=", 2, kNoAttribute) \ | 57 TOK(kASSIGN_XOR, "^=", 2, kNoAttribute) \ |
58 TOK(kASSIGN_AND, "&=", 2, kNoAttribute) \ | 58 TOK(kASSIGN_AND, "&=", 2, kNoAttribute) \ |
59 TOK(kASSIGN_SHL, "<<=", 2, kNoAttribute) \ | 59 TOK(kASSIGN_SHL, "<<=", 2, kNoAttribute) \ |
60 TOK(kASSIGN_SHR, ">>=", 2, kNoAttribute) \ | 60 TOK(kASSIGN_SHR, ">>=", 2, kNoAttribute) \ |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 \ | 132 \ |
133 TOK(kNEWLINE, "\n", 0, kNoAttribute) \ | 133 TOK(kNEWLINE, "\n", 0, kNoAttribute) \ |
134 TOK(kWHITESP, "", 0, kNoAttribute) \ | 134 TOK(kWHITESP, "", 0, kNoAttribute) \ |
135 TOK(kERROR, "", 0, kNoAttribute) \ | 135 TOK(kERROR, "", 0, kNoAttribute) \ |
136 TOK(kILLEGAL, "", 0, kNoAttribute) \ | 136 TOK(kILLEGAL, "", 0, kNoAttribute) \ |
137 \ | 137 \ |
138 /* Support for Dart scripts. */ \ | 138 /* Support for Dart scripts. */ \ |
139 TOK(kSCRIPTTAG, "#!", 0, kNoAttribute) \ | 139 TOK(kSCRIPTTAG, "#!", 0, kNoAttribute) \ |
140 \ | 140 \ |
141 /* Support for optimized code */ \ | 141 /* Support for optimized code */ \ |
142 TOK(kREM, "", 0, kNoAttribute) \ | 142 TOK(kREM, "", 0, kNoAttribute) |
143 | 143 |
144 // List of keywords. The list must be alphabetically ordered. The | 144 // List of keywords. The list must be alphabetically ordered. The |
145 // keyword recognition code depends on the ordering. | 145 // keyword recognition code depends on the ordering. |
146 // If you add a keyword at the beginning or end of this list, make sure | 146 // If you add a keyword at the beginning or end of this list, make sure |
147 // to update kFirstKeyword and kLastKeyword below. | 147 // to update kFirstKeyword and kLastKeyword below. |
148 #define DART_KEYWORD_LIST(KW) \ | 148 #define DART_KEYWORD_LIST(KW) \ |
149 KW(kABSTRACT, "abstract", 0, kPseudoKeyword) /* == kFirstKeyword */ \ | 149 KW(kABSTRACT, "abstract", 0, kPseudoKeyword) /* == kFirstKeyword */ \ |
150 KW(kAS, "as", 11, kPseudoKeyword) \ | 150 KW(kAS, "as", 11, kPseudoKeyword) \ |
151 KW(kASSERT, "assert", 11, kKeyword) \ | 151 KW(kASSERT, "assert", 11, kKeyword) \ |
152 KW(kBREAK, "break", 0, kKeyword) \ | 152 KW(kBREAK, "break", 0, kKeyword) \ |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 KW(kVAR, "var", 0, kKeyword) \ | 192 KW(kVAR, "var", 0, kKeyword) \ |
193 KW(kVOID, "void", 0, kKeyword) \ | 193 KW(kVOID, "void", 0, kKeyword) \ |
194 KW(kWHILE, "while", 0, kKeyword) \ | 194 KW(kWHILE, "while", 0, kKeyword) \ |
195 KW(kWITH, "with", 0, kKeyword) /* == kLastKeyword */ | 195 KW(kWITH, "with", 0, kKeyword) /* == kLastKeyword */ |
196 | 196 |
197 class String; | 197 class String; |
198 | 198 |
199 class Token { | 199 class Token { |
200 public: | 200 public: |
201 #define T(t, s, p, a) t, | 201 #define T(t, s, p, a) t, |
202 enum Kind { | 202 enum Kind { DART_TOKEN_LIST(T) DART_KEYWORD_LIST(T) kNumTokens }; |
203 DART_TOKEN_LIST(T) | |
204 DART_KEYWORD_LIST(T) | |
205 kNumTokens | |
206 }; | |
207 #undef T | 203 #undef T |
208 | 204 |
209 enum Attribute { | 205 enum Attribute { |
210 kNoAttribute = 0, | 206 kNoAttribute = 0, |
211 kKeyword = 1 << 0, | 207 kKeyword = 1 << 0, |
212 kPseudoKeyword = 1 << 1, | 208 kPseudoKeyword = 1 << 1, |
213 }; | 209 }; |
214 | 210 |
215 static const Kind kFirstKeyword = kABSTRACT; | 211 static const Kind kFirstKeyword = kABSTRACT; |
216 static const Kind kLastKeyword = kWITH; | 212 static const Kind kLastKeyword = kWITH; |
217 static const int kNumKeywords = kLastKeyword - kFirstKeyword + 1; | 213 static const int kNumKeywords = kLastKeyword - kFirstKeyword + 1; |
218 | 214 |
219 static bool IsAssignmentOperator(Kind tok) { | 215 static bool IsAssignmentOperator(Kind tok) { |
220 return kASSIGN <= tok && tok <= kASSIGN_COND; | 216 return kASSIGN <= tok && tok <= kASSIGN_COND; |
221 } | 217 } |
222 | 218 |
223 static bool IsRelationalOperator(Kind tok) { | 219 static bool IsRelationalOperator(Kind tok) { |
224 return kLT <= tok && tok <= kGTE; | 220 return kLT <= tok && tok <= kGTE; |
225 } | 221 } |
226 | 222 |
227 static bool IsEqualityOperator(Kind tok) { | 223 static bool IsEqualityOperator(Kind tok) { |
228 return kEQ <= tok && tok <= kNE_STRICT; | 224 return kEQ <= tok && tok <= kNE_STRICT; |
229 } | 225 } |
230 | 226 |
231 static bool IsStrictEqualityOperator(Kind tok) { | 227 static bool IsStrictEqualityOperator(Kind tok) { |
232 return (tok == kEQ_STRICT) || (tok == kNE_STRICT); | 228 return (tok == kEQ_STRICT) || (tok == kNE_STRICT); |
233 } | 229 } |
234 | 230 |
235 static bool IsTypeTestOperator(Kind tok) { | 231 static bool IsTypeTestOperator(Kind tok) { |
236 return (tok == kIS) || (tok == kISNOT); | 232 return (tok == kIS) || (tok == kISNOT); |
237 } | 233 } |
238 | 234 |
239 static bool IsTypeCastOperator(Kind tok) { | 235 static bool IsTypeCastOperator(Kind tok) { return tok == kAS; } |
240 return tok == kAS; | |
241 } | |
242 | 236 |
243 static bool IsIndexOperator(Kind tok) { | 237 static bool IsIndexOperator(Kind tok) { |
244 return tok == kINDEX || tok == kASSIGN_INDEX; | 238 return tok == kINDEX || tok == kASSIGN_INDEX; |
245 } | 239 } |
246 | 240 |
247 static bool IsPseudoKeyword(Kind tok) { | 241 static bool IsPseudoKeyword(Kind tok) { |
248 return (Attributes(tok) & kPseudoKeyword) != 0; | 242 return (Attributes(tok) & kPseudoKeyword) != 0; |
249 } | 243 } |
250 | 244 |
251 static bool IsKeyword(Kind tok) { | 245 static bool IsKeyword(Kind tok) { return (Attributes(tok) & kKeyword) != 0; } |
252 return (Attributes(tok) & kKeyword) != 0; | |
253 } | |
254 | 246 |
255 static bool IsIdentifier(Kind tok) { | 247 static bool IsIdentifier(Kind tok) { |
256 return (tok == kIDENT) || IsPseudoKeyword(tok); | 248 return (tok == kIDENT) || IsPseudoKeyword(tok); |
257 } | 249 } |
258 | 250 |
259 static const char* Name(Kind tok) { | 251 static const char* Name(Kind tok) { |
260 ASSERT(tok < kNumTokens); | 252 ASSERT(tok < kNumTokens); |
261 return name_[tok]; | 253 return name_[tok]; |
262 } | 254 } |
263 | 255 |
264 static const char* Str(Kind tok) { | 256 static const char* Str(Kind tok) { |
265 ASSERT(tok < kNumTokens); | 257 ASSERT(tok < kNumTokens); |
266 return tok_str_[tok]; | 258 return tok_str_[tok]; |
267 } | 259 } |
268 | 260 |
269 static int Precedence(Kind tok) { | 261 static int Precedence(Kind tok) { |
270 ASSERT(tok < kNumTokens); | 262 ASSERT(tok < kNumTokens); |
271 return precedence_[tok]; | 263 return precedence_[tok]; |
272 } | 264 } |
273 | 265 |
274 static Attribute Attributes(Kind tok) { | 266 static Attribute Attributes(Kind tok) { |
275 ASSERT(tok < kNumTokens); | 267 ASSERT(tok < kNumTokens); |
276 return attributes_[tok]; | 268 return attributes_[tok]; |
277 } | 269 } |
278 | 270 |
279 static bool CanBeOverloaded(Kind tok) { | 271 static bool CanBeOverloaded(Kind tok) { |
280 ASSERT(tok < kNumTokens); | 272 ASSERT(tok < kNumTokens); |
281 return IsRelationalOperator(tok) || | 273 return IsRelationalOperator(tok) || (tok == kEQ) || |
282 (tok == kEQ) || | 274 (tok >= kADD && tok <= kMOD) || // Arithmetic operations. |
283 (tok >= kADD && tok <= kMOD) || // Arithmetic operations. | |
284 (tok >= kBIT_OR && tok <= kSHR) || // Bit operations. | 275 (tok >= kBIT_OR && tok <= kSHR) || // Bit operations. |
285 (tok == kINDEX) || | 276 (tok == kINDEX) || (tok == kASSIGN_INDEX); |
286 (tok == kASSIGN_INDEX); | |
287 } | 277 } |
288 | 278 |
289 static bool NeedsLiteralToken(Kind tok) { | 279 static bool NeedsLiteralToken(Kind tok) { |
290 ASSERT(tok < kNumTokens); | 280 ASSERT(tok < kNumTokens); |
291 return ((tok == Token::kINTEGER) || | 281 return ((tok == Token::kINTEGER) || (tok == Token::kSTRING) || |
292 (tok == Token::kSTRING) || | 282 (tok == Token::kINTERPOL_VAR) || (tok == Token::kERROR) || |
293 (tok == Token::kINTERPOL_VAR) || | |
294 (tok == Token::kERROR) || | |
295 (tok == Token::kDOUBLE)); | 283 (tok == Token::kDOUBLE)); |
296 } | 284 } |
297 | 285 |
298 static bool IsBinaryOperator(Token::Kind token); | 286 static bool IsBinaryOperator(Token::Kind token); |
299 static bool IsUnaryOperator(Token::Kind token); | 287 static bool IsUnaryOperator(Token::Kind token); |
300 | 288 |
301 static bool IsBinaryArithmeticOperator(Token::Kind token); | 289 static bool IsBinaryArithmeticOperator(Token::Kind token); |
302 static bool IsUnaryArithmeticOperator(Token::Kind token); | 290 static bool IsUnaryArithmeticOperator(Token::Kind token); |
303 | 291 |
304 // For a comparison operation return an operation for the negated comparison: | 292 // For a comparison operation return an operation for the negated comparison: |
305 // !(a (op) b) === a (op') b | 293 // !(a (op) b) === a (op') b |
306 static Token::Kind NegateComparison(Token::Kind op) { | 294 static Token::Kind NegateComparison(Token::Kind op) { |
307 switch (op) { | 295 switch (op) { |
308 case Token::kEQ: return Token::kNE; | 296 case Token::kEQ: |
309 case Token::kNE: return Token::kEQ; | 297 return Token::kNE; |
310 case Token::kLT: return Token::kGTE; | 298 case Token::kNE: |
311 case Token::kGT: return Token::kLTE; | 299 return Token::kEQ; |
312 case Token::kLTE: return Token::kGT; | 300 case Token::kLT: |
313 case Token::kGTE: return Token::kLT; | 301 return Token::kGTE; |
314 case Token::kEQ_STRICT: return Token::kNE_STRICT; | 302 case Token::kGT: |
315 case Token::kNE_STRICT: return Token::kEQ_STRICT; | 303 return Token::kLTE; |
316 case Token::kIS: return Token::kISNOT; | 304 case Token::kLTE: |
317 case Token::kISNOT: return Token::kIS; | 305 return Token::kGT; |
| 306 case Token::kGTE: |
| 307 return Token::kLT; |
| 308 case Token::kEQ_STRICT: |
| 309 return Token::kNE_STRICT; |
| 310 case Token::kNE_STRICT: |
| 311 return Token::kEQ_STRICT; |
| 312 case Token::kIS: |
| 313 return Token::kISNOT; |
| 314 case Token::kISNOT: |
| 315 return Token::kIS; |
318 default: | 316 default: |
319 UNREACHABLE(); | 317 UNREACHABLE(); |
320 return Token::kILLEGAL; | 318 return Token::kILLEGAL; |
321 } | 319 } |
322 } | 320 } |
323 | 321 |
324 private: | 322 private: |
325 static const char* name_[]; | 323 static const char* name_[]; |
326 static const char* tok_str_[]; | 324 static const char* tok_str_[]; |
327 static const uint8_t precedence_[]; | 325 static const uint8_t precedence_[]; |
328 static const Attribute attributes_[]; | 326 static const Attribute attributes_[]; |
329 }; | 327 }; |
330 | 328 |
331 } // namespace dart | 329 } // namespace dart |
332 | 330 |
333 #endif // RUNTIME_VM_TOKEN_H_ | 331 #endif // RUNTIME_VM_TOKEN_H_ |
OLD | NEW |