| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/regexp/regexp-parser.h" | 5 #include "src/regexp/regexp-parser.h" |
| 6 | 6 |
| 7 #include "src/char-predicates-inl.h" | 7 #include "src/char-predicates-inl.h" |
| 8 #include "src/factory.h" | 8 #include "src/factory.h" |
| 9 #include "src/isolate.h" | 9 #include "src/isolate.h" |
| 10 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 builder->AddAtom(atom); | 336 builder->AddAtom(atom); |
| 337 } | 337 } |
| 338 break; | 338 break; |
| 339 } | 339 } |
| 340 uc32 first_digit = Next(); | 340 uc32 first_digit = Next(); |
| 341 if (first_digit == '8' || first_digit == '9') { | 341 if (first_digit == '8' || first_digit == '9') { |
| 342 // If the 'u' flag is present, only syntax characters can be | 342 // If the 'u' flag is present, only syntax characters can be |
| 343 // escaped, | 343 // escaped, |
| 344 // no other identity escapes are allowed. If the 'u' flag is not | 344 // no other identity escapes are allowed. If the 'u' flag is not |
| 345 // present, all identity escapes are allowed. | 345 // present, all identity escapes are allowed. |
| 346 if (!FLAG_harmony_unicode_regexps || !unicode_) { | 346 if (!unicode_) { |
| 347 builder->AddCharacter(first_digit); | 347 builder->AddCharacter(first_digit); |
| 348 Advance(2); | 348 Advance(2); |
| 349 } else { | 349 } else { |
| 350 return ReportError(CStrVector("Invalid escape")); | 350 return ReportError(CStrVector("Invalid escape")); |
| 351 } | 351 } |
| 352 break; | 352 break; |
| 353 } | 353 } |
| 354 } | 354 } |
| 355 // FALLTHROUGH | 355 // FALLTHROUGH |
| 356 case '0': { | 356 case '0': { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 Advance(2); | 397 Advance(2); |
| 398 builder->AddCharacter(controlLetter & 0x1f); | 398 builder->AddCharacter(controlLetter & 0x1f); |
| 399 } | 399 } |
| 400 break; | 400 break; |
| 401 } | 401 } |
| 402 case 'x': { | 402 case 'x': { |
| 403 Advance(2); | 403 Advance(2); |
| 404 uc32 value; | 404 uc32 value; |
| 405 if (ParseHexEscape(2, &value)) { | 405 if (ParseHexEscape(2, &value)) { |
| 406 builder->AddCharacter(value); | 406 builder->AddCharacter(value); |
| 407 } else if (!FLAG_harmony_unicode_regexps || !unicode_) { | 407 } else if (!unicode_) { |
| 408 builder->AddCharacter('x'); | 408 builder->AddCharacter('x'); |
| 409 } else { | 409 } else { |
| 410 // If the 'u' flag is present, invalid escapes are not treated as | 410 // If the 'u' flag is present, invalid escapes are not treated as |
| 411 // identity escapes. | 411 // identity escapes. |
| 412 return ReportError(CStrVector("Invalid escape")); | 412 return ReportError(CStrVector("Invalid escape")); |
| 413 } | 413 } |
| 414 break; | 414 break; |
| 415 } | 415 } |
| 416 case 'u': { | 416 case 'u': { |
| 417 Advance(2); | 417 Advance(2); |
| 418 uc32 value; | 418 uc32 value; |
| 419 if (ParseUnicodeEscape(&value)) { | 419 if (ParseUnicodeEscape(&value)) { |
| 420 builder->AddCharacter(value); | 420 builder->AddCharacter(value); |
| 421 } else if (!FLAG_harmony_unicode_regexps || !unicode_) { | 421 } else if (!unicode_) { |
| 422 builder->AddCharacter('u'); | 422 builder->AddCharacter('u'); |
| 423 } else { | 423 } else { |
| 424 // If the 'u' flag is present, invalid escapes are not treated as | 424 // If the 'u' flag is present, invalid escapes are not treated as |
| 425 // identity escapes. | 425 // identity escapes. |
| 426 return ReportError(CStrVector("Invalid unicode escape")); | 426 return ReportError(CStrVector("Invalid unicode escape")); |
| 427 } | 427 } |
| 428 break; | 428 break; |
| 429 } | 429 } |
| 430 default: | 430 default: |
| 431 Advance(); | 431 Advance(); |
| 432 // If the 'u' flag is present, only syntax characters can be | 432 // If the 'u' flag is present, only syntax characters can be |
| 433 // escaped, no | 433 // escaped, no |
| 434 // other identity escapes are allowed. If the 'u' flag is not | 434 // other identity escapes are allowed. If the 'u' flag is not |
| 435 // present, | 435 // present, |
| 436 // all identity escapes are allowed. | 436 // all identity escapes are allowed. |
| 437 if (!FLAG_harmony_unicode_regexps || !unicode_ || | 437 if (!unicode_ || IsSyntaxCharacter(current())) { |
| 438 IsSyntaxCharacter(current())) { | |
| 439 builder->AddCharacter(current()); | 438 builder->AddCharacter(current()); |
| 440 Advance(); | 439 Advance(); |
| 441 } else { | 440 } else { |
| 442 return ReportError(CStrVector("Invalid escape")); | 441 return ReportError(CStrVector("Invalid escape")); |
| 443 } | 442 } |
| 444 break; | 443 break; |
| 445 } | 444 } |
| 446 break; | 445 break; |
| 447 case '{': { | 446 case '{': { |
| 448 int dummy; | 447 int dummy; |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 } | 728 } |
| 730 *value = val; | 729 *value = val; |
| 731 return true; | 730 return true; |
| 732 } | 731 } |
| 733 | 732 |
| 734 | 733 |
| 735 bool RegExpParser::ParseUnicodeEscape(uc32* value) { | 734 bool RegExpParser::ParseUnicodeEscape(uc32* value) { |
| 736 // Accept both \uxxxx and \u{xxxxxx} (if harmony unicode escapes are | 735 // Accept both \uxxxx and \u{xxxxxx} (if harmony unicode escapes are |
| 737 // allowed). In the latter case, the number of hex digits between { } is | 736 // allowed). In the latter case, the number of hex digits between { } is |
| 738 // arbitrary. \ and u have already been read. | 737 // arbitrary. \ and u have already been read. |
| 739 if (current() == '{' && FLAG_harmony_unicode_regexps && unicode_) { | 738 if (current() == '{' && unicode_) { |
| 740 int start = position(); | 739 int start = position(); |
| 741 Advance(); | 740 Advance(); |
| 742 if (ParseUnlimitedLengthHexNumber(0x10ffff, value)) { | 741 if (ParseUnlimitedLengthHexNumber(0x10ffff, value)) { |
| 743 if (current() == '}') { | 742 if (current() == '}') { |
| 744 Advance(); | 743 Advance(); |
| 745 return true; | 744 return true; |
| 746 } | 745 } |
| 747 } | 746 } |
| 748 Reset(start); | 747 Reset(start); |
| 749 return false; | 748 return false; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 824 // For compatibility, we interpret a decimal escape that isn't | 823 // For compatibility, we interpret a decimal escape that isn't |
| 825 // a back reference (and therefore either \0 or not valid according | 824 // a back reference (and therefore either \0 or not valid according |
| 826 // to the specification) as a 1..3 digit octal character code. | 825 // to the specification) as a 1..3 digit octal character code. |
| 827 return ParseOctalLiteral(); | 826 return ParseOctalLiteral(); |
| 828 case 'x': { | 827 case 'x': { |
| 829 Advance(); | 828 Advance(); |
| 830 uc32 value; | 829 uc32 value; |
| 831 if (ParseHexEscape(2, &value)) { | 830 if (ParseHexEscape(2, &value)) { |
| 832 return value; | 831 return value; |
| 833 } | 832 } |
| 834 if (!FLAG_harmony_unicode_regexps || !unicode_) { | 833 if (!unicode_) { |
| 835 // If \x is not followed by a two-digit hexadecimal, treat it | 834 // If \x is not followed by a two-digit hexadecimal, treat it |
| 836 // as an identity escape. | 835 // as an identity escape. |
| 837 return 'x'; | 836 return 'x'; |
| 838 } | 837 } |
| 839 // If the 'u' flag is present, invalid escapes are not treated as | 838 // If the 'u' flag is present, invalid escapes are not treated as |
| 840 // identity escapes. | 839 // identity escapes. |
| 841 ReportError(CStrVector("Invalid escape")); | 840 ReportError(CStrVector("Invalid escape")); |
| 842 return 0; | 841 return 0; |
| 843 } | 842 } |
| 844 case 'u': { | 843 case 'u': { |
| 845 Advance(); | 844 Advance(); |
| 846 uc32 value; | 845 uc32 value; |
| 847 if (ParseUnicodeEscape(&value)) { | 846 if (ParseUnicodeEscape(&value)) { |
| 848 return value; | 847 return value; |
| 849 } | 848 } |
| 850 if (!FLAG_harmony_unicode_regexps || !unicode_) { | 849 if (!unicode_) { |
| 851 return 'u'; | 850 return 'u'; |
| 852 } | 851 } |
| 853 // If the 'u' flag is present, invalid escapes are not treated as | 852 // If the 'u' flag is present, invalid escapes are not treated as |
| 854 // identity escapes. | 853 // identity escapes. |
| 855 ReportError(CStrVector("Invalid unicode escape")); | 854 ReportError(CStrVector("Invalid unicode escape")); |
| 856 return 0; | 855 return 0; |
| 857 } | 856 } |
| 858 default: { | 857 default: { |
| 859 uc32 result = current(); | 858 uc32 result = current(); |
| 860 // If the 'u' flag is present, only syntax characters can be escaped, no | 859 // If the 'u' flag is present, only syntax characters can be escaped, no |
| 861 // other identity escapes are allowed. If the 'u' flag is not present, all | 860 // other identity escapes are allowed. If the 'u' flag is not present, all |
| 862 // identity escapes are allowed. | 861 // identity escapes are allowed. |
| 863 if (!FLAG_harmony_unicode_regexps || !unicode_ || | 862 if (!unicode_ || IsSyntaxCharacter(result)) { |
| 864 IsSyntaxCharacter(result)) { | |
| 865 Advance(); | 863 Advance(); |
| 866 return result; | 864 return result; |
| 867 } | 865 } |
| 868 ReportError(CStrVector("Invalid escape")); | 866 ReportError(CStrVector("Invalid escape")); |
| 869 return 0; | 867 return 0; |
| 870 } | 868 } |
| 871 } | 869 } |
| 872 return 0; | 870 return 0; |
| 873 } | 871 } |
| 874 | 872 |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1145 UNREACHABLE(); | 1143 UNREACHABLE(); |
| 1146 return; | 1144 return; |
| 1147 } | 1145 } |
| 1148 terms_.Add(new (zone()) RegExpQuantifier(min, max, quantifier_type, atom), | 1146 terms_.Add(new (zone()) RegExpQuantifier(min, max, quantifier_type, atom), |
| 1149 zone()); | 1147 zone()); |
| 1150 LAST(ADD_TERM); | 1148 LAST(ADD_TERM); |
| 1151 } | 1149 } |
| 1152 | 1150 |
| 1153 } // namespace internal | 1151 } // namespace internal |
| 1154 } // namespace v8 | 1152 } // namespace v8 |
| OLD | NEW |