OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 Type type_; | 340 Type type_; |
341 | 341 |
342 friend class Expression; | 342 friend class Expression; |
343 }; | 343 }; |
344 | 344 |
345 // Bits 0 and 1 are used to identify the type of expression: | 345 // Bits 0 and 1 are used to identify the type of expression: |
346 // If bit 0 is set, it's an identifier. | 346 // If bit 0 is set, it's an identifier. |
347 // if bit 1 is set, it's a string literal. | 347 // if bit 1 is set, it's a string literal. |
348 // If neither is set, it's no particular type, and both set isn't | 348 // If neither is set, it's no particular type, and both set isn't |
349 // use yet. | 349 // use yet. |
350 // Bit 2 is used to mark the expression as being parenthesized, | |
351 // so "(foo)" isn't recognized as a pure identifier (and possible label). | |
352 class Expression { | 350 class Expression { |
353 public: | 351 public: |
354 static Expression Default() { | 352 static Expression Default() { |
355 return Expression(kUnknownExpression); | 353 return Expression(kUnknownExpression); |
356 } | 354 } |
357 | 355 |
358 static Expression FromIdentifier(Identifier id) { | 356 static Expression FromIdentifier(Identifier id) { |
359 return Expression(kIdentifierFlag | (id.type_ << kIdentifierShift)); | 357 return Expression(kIdentifierFlag | (id.type_ << kIdentifierShift)); |
360 } | 358 } |
361 | 359 |
(...skipping 20 matching lines...) Expand all Loading... |
382 bool IsIdentifier() { | 380 bool IsIdentifier() { |
383 return (code_ & kIdentifierFlag) != 0; | 381 return (code_ & kIdentifierFlag) != 0; |
384 } | 382 } |
385 | 383 |
386 // Only works corretly if it is actually an identifier expression. | 384 // Only works corretly if it is actually an identifier expression. |
387 PreParser::Identifier AsIdentifier() { | 385 PreParser::Identifier AsIdentifier() { |
388 return PreParser::Identifier( | 386 return PreParser::Identifier( |
389 static_cast<PreParser::Identifier::Type>(code_ >> kIdentifierShift)); | 387 static_cast<PreParser::Identifier::Type>(code_ >> kIdentifierShift)); |
390 } | 388 } |
391 | 389 |
392 bool IsParenthesized() { | |
393 // If bit 0 or 1 is set, we interpret bit 2 as meaning parenthesized. | |
394 return (code_ & 7) > 4; | |
395 } | |
396 | |
397 bool IsRawIdentifier() { | |
398 return !IsParenthesized() && IsIdentifier(); | |
399 } | |
400 | |
401 bool IsStringLiteral() { return (code_ & kStringLiteralFlag) != 0; } | 390 bool IsStringLiteral() { return (code_ & kStringLiteralFlag) != 0; } |
402 | 391 |
403 bool IsRawStringLiteral() { | |
404 return !IsParenthesized() && IsStringLiteral(); | |
405 } | |
406 | |
407 bool IsUseStrictLiteral() { | 392 bool IsUseStrictLiteral() { |
408 return (code_ & kStringLiteralMask) == kUseStrictString; | 393 return (code_ & kStringLiteralMask) == kUseStrictString; |
409 } | 394 } |
410 | 395 |
411 bool IsThis() { | 396 bool IsThis() { |
412 return code_ == kThisExpression; | 397 return code_ == kThisExpression; |
413 } | 398 } |
414 | 399 |
415 bool IsThisProperty() { | 400 bool IsThisProperty() { |
416 return code_ == kThisPropertyExpression; | 401 return code_ == kThisPropertyExpression; |
417 } | 402 } |
418 | 403 |
419 bool IsStrictFunction() { | 404 bool IsStrictFunction() { |
420 return code_ == kStrictFunctionExpression; | 405 return code_ == kStrictFunctionExpression; |
421 } | 406 } |
422 | 407 |
423 Expression Parenthesize() { | |
424 int type = code_ & 3; | |
425 if (type != 0) { | |
426 // Identifiers and string literals can be parenthesized. | |
427 // They no longer work as labels or directive prologues, | |
428 // but are still recognized in other contexts. | |
429 return Expression(code_ | kParenthesizedExpressionFlag); | |
430 } | |
431 // For other types of expressions, it's not important to remember | |
432 // the parentheses. | |
433 return *this; | |
434 } | |
435 | |
436 private: | 408 private: |
437 // First two/three bits are used as flags. | 409 // First two/three bits are used as flags. |
438 // Bit 0 and 1 represent identifiers or strings literals, and are | 410 // Bit 0 and 1 represent identifiers or strings literals, and are |
439 // mutually exclusive, but can both be absent. | 411 // mutually exclusive, but can both be absent. |
440 // If bit 0 or 1 are set, bit 2 marks that the expression has | |
441 // been wrapped in parentheses (a string literal can no longer | |
442 // be a directive prologue, and an identifier can no longer be | |
443 // a label. | |
444 enum { | 412 enum { |
445 kUnknownExpression = 0, | 413 kUnknownExpression = 0, |
446 // Identifiers | 414 // Identifiers |
447 kIdentifierFlag = 1, // Used to detect labels. | 415 kIdentifierFlag = 1, // Used to detect labels. |
448 kIdentifierShift = 3, | 416 kIdentifierShift = 3, |
449 | 417 |
450 kStringLiteralFlag = 2, // Used to detect directive prologue. | 418 kStringLiteralFlag = 2, // Used to detect directive prologue. |
451 kUnknownStringLiteral = kStringLiteralFlag, | 419 kUnknownStringLiteral = kStringLiteralFlag, |
452 kUseStrictString = kStringLiteralFlag | 8, | 420 kUseStrictString = kStringLiteralFlag | 8, |
453 kStringLiteralMask = kUseStrictString, | 421 kStringLiteralMask = kUseStrictString, |
454 | 422 |
455 // Only if identifier or string literal. | |
456 kParenthesizedExpressionFlag = 4, | |
457 | |
458 // Below here applies if neither identifier nor string literal. | 423 // Below here applies if neither identifier nor string literal. |
459 kThisExpression = 4, | 424 kThisExpression = 4, |
460 kThisPropertyExpression = 8, | 425 kThisPropertyExpression = 8, |
461 kStrictFunctionExpression = 12 | 426 kStrictFunctionExpression = 12 |
462 }; | 427 }; |
463 | 428 |
464 explicit Expression(int expression_code) : code_(expression_code) { } | 429 explicit Expression(int expression_code) : code_(expression_code) { } |
465 | 430 |
466 int code_; | 431 int code_; |
467 }; | 432 }; |
468 | 433 |
469 class Statement { | 434 class Statement { |
470 public: | 435 public: |
471 static Statement Default() { | 436 static Statement Default() { |
472 return Statement(kUnknownStatement); | 437 return Statement(kUnknownStatement); |
473 } | 438 } |
474 | 439 |
475 static Statement FunctionDeclaration() { | 440 static Statement FunctionDeclaration() { |
476 return Statement(kFunctionDeclaration); | 441 return Statement(kFunctionDeclaration); |
477 } | 442 } |
478 | 443 |
479 // Creates expression statement from expression. | 444 // Creates expression statement from expression. |
480 // Preserves being an unparenthesized string literal, possibly | 445 // Preserves being an unparenthesized string literal, possibly |
481 // "use strict". | 446 // "use strict". |
482 static Statement ExpressionStatement(Expression expression) { | 447 static Statement ExpressionStatement(Expression expression) { |
483 if (!expression.IsParenthesized()) { | 448 if (expression.IsUseStrictLiteral()) { |
484 if (expression.IsUseStrictLiteral()) { | 449 return Statement(kUseStrictExpressionStatement); |
485 return Statement(kUseStrictExpressionStatement); | 450 } |
486 } | 451 if (expression.IsStringLiteral()) { |
487 if (expression.IsStringLiteral()) { | 452 return Statement(kStringLiteralExpressionStatement); |
488 return Statement(kStringLiteralExpressionStatement); | |
489 } | |
490 } | 453 } |
491 return Default(); | 454 return Default(); |
492 } | 455 } |
493 | 456 |
494 bool IsStringLiteral() { | 457 bool IsStringLiteral() { |
495 return code_ == kStringLiteralExpressionStatement; | 458 return code_ == kStringLiteralExpressionStatement; |
496 } | 459 } |
497 | 460 |
498 bool IsUseStrictLiteral() { | 461 bool IsUseStrictLiteral() { |
499 return code_ == kUseStrictExpressionStatement; | 462 return code_ == kUseStrictExpressionStatement; |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
687 ParserRecorder* log_; | 650 ParserRecorder* log_; |
688 Scope* scope_; | 651 Scope* scope_; |
689 Scanner::Location strict_mode_violation_location_; | 652 Scanner::Location strict_mode_violation_location_; |
690 const char* strict_mode_violation_type_; | 653 const char* strict_mode_violation_type_; |
691 bool parenthesized_function_; | 654 bool parenthesized_function_; |
692 }; | 655 }; |
693 | 656 |
694 } } // v8::internal | 657 } } // v8::internal |
695 | 658 |
696 #endif // V8_PREPARSER_H | 659 #endif // V8_PREPARSER_H |
OLD | NEW |