OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 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 4391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4402 uc32 c = ParseClassCharacterEscape(CHECK_FAILED); | 4402 uc32 c = ParseClassCharacterEscape(CHECK_FAILED); |
4403 return CharacterRange::Singleton(c); | 4403 return CharacterRange::Singleton(c); |
4404 } | 4404 } |
4405 } else { | 4405 } else { |
4406 Advance(); | 4406 Advance(); |
4407 return CharacterRange::Singleton(first); | 4407 return CharacterRange::Singleton(first); |
4408 } | 4408 } |
4409 } | 4409 } |
4410 | 4410 |
4411 | 4411 |
| 4412 static const uc16 kNoCharClass = 0; |
| 4413 |
| 4414 // Adds range or pre-defined character class to character ranges. |
| 4415 // If char_class is not kInvalidClass, it's interpreted as a class |
| 4416 // escape (i.e., 's' means whitespace, from '\s'). |
| 4417 static inline void AddRangeOrEscape(ZoneList<CharacterRange>* ranges, |
| 4418 uc16 char_class, |
| 4419 CharacterRange range) { |
| 4420 if (char_class != kNoCharClass) { |
| 4421 CharacterRange::AddClassEscape(char_class, ranges); |
| 4422 } else { |
| 4423 ranges->Add(range); |
| 4424 } |
| 4425 } |
| 4426 |
| 4427 |
4412 RegExpTree* RegExpParser::ParseCharacterClass() { | 4428 RegExpTree* RegExpParser::ParseCharacterClass() { |
4413 static const char* kUnterminated = "Unterminated character class"; | 4429 static const char* kUnterminated = "Unterminated character class"; |
4414 static const char* kRangeOutOfOrder = "Range out of order in character class"; | 4430 static const char* kRangeOutOfOrder = "Range out of order in character class"; |
4415 static const char* kInvalidRange = "Invalid character range"; | |
4416 | 4431 |
4417 ASSERT_EQ(current(), '['); | 4432 ASSERT_EQ(current(), '['); |
4418 Advance(); | 4433 Advance(); |
4419 bool is_negated = false; | 4434 bool is_negated = false; |
4420 if (current() == '^') { | 4435 if (current() == '^') { |
4421 is_negated = true; | 4436 is_negated = true; |
4422 Advance(); | 4437 Advance(); |
4423 } | 4438 } |
4424 // A CharacterClass is a sequence of single characters, character class | |
4425 // escapes or ranges. Ranges are on the form "x-y" where x and y are | |
4426 // single characters (and not character class escapes like \s). | |
4427 // A "-" may occur at the start or end of the character class (just after | |
4428 // "[" or "[^", or just before "]") without being considered part of a | |
4429 // range. A "-" may also appear as the beginning or end of a range. | |
4430 // I.e., [--+] is valid, so is [!--]. | |
4431 | |
4432 ZoneList<CharacterRange>* ranges = new ZoneList<CharacterRange>(2); | 4439 ZoneList<CharacterRange>* ranges = new ZoneList<CharacterRange>(2); |
4433 while (has_more() && current() != ']') { | 4440 while (has_more() && current() != ']') { |
4434 uc16 char_class = 0; | 4441 uc16 char_class = kNoCharClass; |
4435 CharacterRange first = ParseClassAtom(&char_class CHECK_FAILED); | 4442 CharacterRange first = ParseClassAtom(&char_class CHECK_FAILED); |
4436 if (char_class) { | |
4437 CharacterRange::AddClassEscape(char_class, ranges); | |
4438 if (current() == '-') { | |
4439 Advance(); | |
4440 ranges->Add(CharacterRange::Singleton('-')); | |
4441 if (current() != ']') { | |
4442 ReportError(CStrVector(kInvalidRange) CHECK_FAILED); | |
4443 } | |
4444 break; | |
4445 } | |
4446 continue; | |
4447 } | |
4448 if (current() == '-') { | 4443 if (current() == '-') { |
4449 Advance(); | 4444 Advance(); |
4450 if (current() == kEndMarker) { | 4445 if (current() == kEndMarker) { |
4451 // If we reach the end we break out of the loop and let the | 4446 // If we reach the end we break out of the loop and let the |
4452 // following code report an error. | 4447 // following code report an error. |
4453 break; | 4448 break; |
4454 } else if (current() == ']') { | 4449 } else if (current() == ']') { |
4455 ranges->Add(first); | 4450 AddRangeOrEscape(ranges, char_class, first); |
4456 ranges->Add(CharacterRange::Singleton('-')); | 4451 ranges->Add(CharacterRange::Singleton('-')); |
4457 break; | 4452 break; |
4458 } | 4453 } |
4459 CharacterRange next = ParseClassAtom(&char_class CHECK_FAILED); | 4454 uc16 char_class_2 = kNoCharClass; |
4460 if (char_class) { | 4455 CharacterRange next = ParseClassAtom(&char_class_2 CHECK_FAILED); |
4461 ReportError(CStrVector(kInvalidRange) CHECK_FAILED); | 4456 if (char_class != kNoCharClass || char_class_2 != kNoCharClass) { |
| 4457 // Either end is an escaped character class. Treat the '-' verbatim. |
| 4458 AddRangeOrEscape(ranges, char_class, first); |
| 4459 ranges->Add(CharacterRange::Singleton('-')); |
| 4460 AddRangeOrEscape(ranges, char_class_2, next); |
| 4461 continue; |
4462 } | 4462 } |
4463 if (first.from() > next.to()) { | 4463 if (first.from() > next.to()) { |
4464 return ReportError(CStrVector(kRangeOutOfOrder) CHECK_FAILED); | 4464 return ReportError(CStrVector(kRangeOutOfOrder) CHECK_FAILED); |
4465 } | 4465 } |
4466 ranges->Add(CharacterRange::Range(first.from(), next.to())); | 4466 ranges->Add(CharacterRange::Range(first.from(), next.to())); |
4467 } else { | 4467 } else { |
4468 ranges->Add(first); | 4468 AddRangeOrEscape(ranges, char_class, first); |
4469 } | 4469 } |
4470 } | 4470 } |
4471 if (!has_more()) { | 4471 if (!has_more()) { |
4472 return ReportError(CStrVector(kUnterminated) CHECK_FAILED); | 4472 return ReportError(CStrVector(kUnterminated) CHECK_FAILED); |
4473 } | 4473 } |
4474 Advance(); | 4474 Advance(); |
4475 if (ranges->length() == 0) { | 4475 if (ranges->length() == 0) { |
4476 ranges->Add(CharacterRange::Everything()); | 4476 ranges->Add(CharacterRange::Everything()); |
4477 is_negated = !is_negated; | 4477 is_negated = !is_negated; |
4478 } | 4478 } |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4658 Handle<String> source = Handle<String>(String::cast(script->source())); | 4658 Handle<String> source = Handle<String>(String::cast(script->source())); |
4659 result = parser.ParseProgram(source, info->is_global()); | 4659 result = parser.ParseProgram(source, info->is_global()); |
4660 } | 4660 } |
4661 } | 4661 } |
4662 | 4662 |
4663 info->SetFunction(result); | 4663 info->SetFunction(result); |
4664 return (result != NULL); | 4664 return (result != NULL); |
4665 } | 4665 } |
4666 | 4666 |
4667 } } // namespace v8::internal | 4667 } } // namespace v8::internal |
OLD | NEW |