| 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 |
| 3 |
// found in the LICENSE file. |
| 4 |
|
| 5 |
#include "irregexp/imported/regexp-parser.h" |
| 6 |
|
| 7 |
#include "irregexp/imported/regexp-ast.h" |
| 8 |
#include "irregexp/imported/regexp-macro-assembler.h" |
| 9 |
#include "irregexp/imported/regexp.h" |
| 10 |
|
| 11 |
#ifdef V8_INTL_SUPPORT |
| 12 |
#include "unicode/uniset.h" |
| 13 |
#include "unicode/unistr.h" |
| 14 |
#include "unicode/usetiter.h" |
| 15 |
#include "unicode/utf16.h" // For U16_NEXT |
| 16 |
#endif // V8_INTL_SUPPORT |
| 17 |
|
| 18 |
namespace v8 { |
| 19 |
namespace internal { |
| 20 |
|
| 21 |
namespace { |
| 22 |
|
| 23 |
// Whether we're currently inside the ClassEscape production |
| 24 |
// (tc39.es/ecma262/#prod-annexB-CharacterEscape). |
| 25 |
enum class InClassEscapeState { |
| 26 |
kInClass, |
| 27 |
kNotInClass, |
| 28 |
}; |
| 29 |
|
| 30 |
// The production used to derive ClassSetOperand. |
| 31 |
enum class ClassSetOperandType { |
| 32 |
kClassSetCharacter, |
| 33 |
kClassStringDisjunction, |
| 34 |
kNestedClass, |
| 35 |
kCharacterClassEscape, // \ CharacterClassEscape is a special nested class, |
| 36 |
// as we can fold it directly into another range. |
| 37 |
kClassSetRange |
| 38 |
}; |
| 39 |
|
| 40 |
class RegExpTextBuilder { |
| 41 |
public: |
| 42 |
using SmallRegExpTreeVector = SmallZoneVector<RegExpTree*, 8>; |
| 43 |
|
| 44 |
RegExpTextBuilder(Zone* zone, SmallRegExpTreeVector* terms_storage, |
| 45 |
RegExpFlags flags) |
| 46 |
: zone_(zone), flags_(flags), terms_(terms_storage), text_(zone) {} |
| 47 |
void AddCharacter(base::uc16 character); |
| 48 |
void AddUnicodeCharacter(base::uc32 character); |
| 49 |
void AddEscapedUnicodeCharacter(base::uc32 character); |
| 50 |
void AddAtom(RegExpTree* atom); |
| 51 |
void AddTerm(RegExpTree* term); |
| 52 |
void AddClassRanges(RegExpClassRanges* cc); |
| 53 |
void FlushPendingSurrogate(); |
| 54 |
void FlushText(); |
| 55 |
RegExpTree* PopLastAtom(); |
| 56 |
RegExpTree* ToRegExp(); |
| 57 |
|
| 58 |
private: |
| 59 |
static const base::uc16 kNoPendingSurrogate = 0; |
| 60 |
|
| 61 |
void AddLeadSurrogate(base::uc16 lead_surrogate); |
| 62 |
void AddTrailSurrogate(base::uc16 trail_surrogate); |
| 63 |
void FlushCharacters(); |
| 64 |
bool NeedsDesugaringForUnicode(RegExpClassRanges* cc); |
| 65 |
bool NeedsDesugaringForIgnoreCase(base::uc32 c); |
| 66 |
void AddClassRangesForDesugaring(base::uc32 c); |
| 67 |
bool ignore_case() const { return IsIgnoreCase(flags_); } |
| 68 |
bool IsUnicodeMode() const { |
| 69 |
// Either /v or /u enable UnicodeMode |
| 70 |
// https://tc39.es/ecma262/#sec-parsepattern |
| 71 |
return IsUnicode(flags_) || IsUnicodeSets(flags_); |
| 72 |
} |
| 73 |
Zone* zone() const { return zone_; } |
| 74 |
|
| 75 |
Zone* const zone_; |
| 76 |
const RegExpFlags flags_; |
| 77 |
ZoneList<base::uc16>* characters_ = nullptr; |
| 78 |
base::uc16 pending_surrogate_ = kNoPendingSurrogate; |
| 79 |
SmallRegExpTreeVector* terms_; |
| 80 |
SmallRegExpTreeVector text_; |
| 81 |
}; |
| 82 |
|
| 83 |
void RegExpTextBuilder::AddLeadSurrogate(base::uc16 lead_surrogate) { |
| 84 |
DCHECK(unibrow::Utf16::IsLeadSurrogate(lead_surrogate)); |
| 85 |
FlushPendingSurrogate(); |
| 86 |
// Hold onto the lead surrogate, waiting for a trail surrogate to follow. |
| 87 |
pending_surrogate_ = lead_surrogate; |
| 88 |
} |
| 89 |
|
| 90 |
void RegExpTextBuilder::AddTrailSurrogate(base::uc16 trail_surrogate) { |
| 91 |
DCHECK(unibrow::Utf16::IsTrailSurrogate(trail_surrogate)); |
| 92 |
if (pending_surrogate_ != kNoPendingSurrogate) { |
| 93 |
base::uc16 lead_surrogate = pending_surrogate_; |
| 94 |
pending_surrogate_ = kNoPendingSurrogate; |
| 95 |
DCHECK(unibrow::Utf16::IsLeadSurrogate(lead_surrogate)); |
| 96 |
base::uc32 combined = |
| 97 |
unibrow::Utf16::CombineSurrogatePair(lead_surrogate, trail_surrogate); |
| 98 |
if (NeedsDesugaringForIgnoreCase(combined)) { |
| 99 |
AddClassRangesForDesugaring(combined); |
| 100 |
} else { |
| 101 |
ZoneList<base::uc16> surrogate_pair(2, zone()); |
| 102 |
surrogate_pair.Add(lead_surrogate, zone()); |
| 103 |
surrogate_pair.Add(trail_surrogate, zone()); |
| 104 |
RegExpAtom* atom = |
| 105 |
zone()->New<RegExpAtom>(surrogate_pair.ToConstVector()); |
| 106 |
AddAtom(atom); |
| 107 |
} |
| 108 |
} else { |
| 109 |
pending_surrogate_ = trail_surrogate; |
| 110 |
FlushPendingSurrogate(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
void RegExpTextBuilder::FlushPendingSurrogate() { |
| 115 |
if (pending_surrogate_ != kNoPendingSurrogate) { |
| 116 |
DCHECK(IsUnicodeMode()); |
| 117 |
base::uc32 c = pending_surrogate_; |
| 118 |
pending_surrogate_ = kNoPendingSurrogate; |
| 119 |
AddClassRangesForDesugaring(c); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
void RegExpTextBuilder::FlushCharacters() { |
| 124 |
FlushPendingSurrogate(); |
| 125 |
if (characters_ != nullptr) { |
| 126 |
RegExpTree* atom = zone()->New<RegExpAtom>(characters_->ToConstVector()); |
| 127 |
characters_ = nullptr; |
| 128 |
text_.emplace_back(atom); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
void RegExpTextBuilder::FlushText() { |
| 133 |
FlushCharacters(); |
| 134 |
size_t num_text = text_.size(); |
| 135 |
if (num_text == 0) { |
| 136 |
return; |
| 137 |
} else if (num_text == 1) { |
| 138 |
terms_->emplace_back(text_.back()); |
| 139 |
} else { |
| 140 |
RegExpText* text = zone()->New<RegExpText>(zone()); |
| 141 |
for (size_t i = 0; i < num_text; i++) { |
| 142 |
text_[i]->AppendToText(text, zone()); |
| 143 |
} |
| 144 |
terms_->emplace_back(text); |
| 145 |
} |
| 146 |
text_.clear(); |
| 147 |
} |
| 148 |
|
| 149 |
void RegExpTextBuilder::AddCharacter(base::uc16 c) { |
| 150 |
FlushPendingSurrogate(); |
| 151 |
if (characters_ == nullptr) { |
| 152 |
characters_ = zone()->New<ZoneList<base::uc16>>(4, zone()); |
| 153 |
} |
| 154 |
characters_->Add(c, zone()); |
| 155 |
} |
| 156 |
|
| 157 |
void RegExpTextBuilder::AddUnicodeCharacter(base::uc32 c) { |
| 158 |
if (c > static_cast<base::uc32>(unibrow::Utf16::kMaxNonSurrogateCharCode)) { |
| 159 |
DCHECK(IsUnicodeMode()); |
| 160 |
AddLeadSurrogate(unibrow::Utf16::LeadSurrogate(c)); |
| 161 |
AddTrailSurrogate(unibrow::Utf16::TrailSurrogate(c)); |
| 162 |
} else if (IsUnicodeMode() && unibrow::Utf16::IsLeadSurrogate(c)) { |
| 163 |
AddLeadSurrogate(c); |
| 164 |
} else if (IsUnicodeMode() && unibrow::Utf16::IsTrailSurrogate(c)) { |
| 165 |
AddTrailSurrogate(c); |
| 166 |
} else { |
| 167 |
AddCharacter(static_cast<base::uc16>(c)); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
void RegExpTextBuilder::AddEscapedUnicodeCharacter(base::uc32 character) { |
| 172 |
// A lead or trail surrogate parsed via escape sequence will not |
| 173 |
// pair up with any preceding lead or following trail surrogate. |
| 174 |
FlushPendingSurrogate(); |
| 175 |
AddUnicodeCharacter(character); |
| 176 |
FlushPendingSurrogate(); |
| 177 |
} |
| 178 |
|
| 179 |
void RegExpTextBuilder::AddClassRanges(RegExpClassRanges* cr) { |
| 180 |
if (NeedsDesugaringForUnicode(cr)) { |
| 181 |
// With /u or /v, character class needs to be desugared, so it |
| 182 |
// must be a standalone term instead of being part of a RegExpText. |
| 183 |
AddTerm(cr); |
| 184 |
} else { |
| 185 |
AddAtom(cr); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void RegExpTextBuilder::AddClassRangesForDesugaring(base::uc32 c) { |
| 190 |
AddTerm(zone()->New<RegExpClassRanges>( |
| 191 |
zone(), CharacterRange::List(zone(), CharacterRange::Singleton(c)))); |
| 192 |
} |
| 193 |
|
| 194 |
void RegExpTextBuilder::AddAtom(RegExpTree* atom) { |
| 195 |
DCHECK(atom->IsTextElement()); |
| 196 |
FlushCharacters(); |
| 197 |
text_.emplace_back(atom); |
| 198 |
} |
| 199 |
|
| 200 |
void RegExpTextBuilder::AddTerm(RegExpTree* term) { |
| 201 |
DCHECK(term->IsTextElement()); |
| 202 |
FlushText(); |
| 203 |
terms_->emplace_back(term); |
| 204 |
} |
| 205 |
|
| 206 |
bool RegExpTextBuilder::NeedsDesugaringForUnicode(RegExpClassRanges* cc) { |
| 207 |
if (!IsUnicodeMode()) return false; |
| 208 |
// TODO(yangguo): we could be smarter than this. Case-insensitivity does not |
| 209 |
// necessarily mean that we need to desugar. It's probably nicer to have a |
| 210 |
// separate pass to figure out unicode desugarings. |
| 211 |
if (ignore_case()) return true; |
| 212 |
ZoneList<CharacterRange>* ranges = cc->ranges(zone()); |
| 213 |
CharacterRange::Canonicalize(ranges); |
| 214 |
|
| 215 |
if (cc->is_negated()) { |
| 216 |
ZoneList<CharacterRange>* negated_ranges = |
| 217 |
zone()->New<ZoneList<CharacterRange>>(ranges->length(), zone()); |
| 218 |
CharacterRange::Negate(ranges, negated_ranges, zone()); |
| 219 |
ranges = negated_ranges; |
| 220 |
} |
| 221 |
|
| 222 |
for (int i = ranges->length() - 1; i >= 0; i--) { |
| 223 |
base::uc32 from = ranges->at(i).from(); |
| 224 |
base::uc32 to = ranges->at(i).to(); |
| 225 |
// Check for non-BMP characters. |
| 226 |
if (to >= kNonBmpStart) return true; |
| 227 |
// Check for lone surrogates. |
| 228 |
if (from <= kTrailSurrogateEnd && to >= kLeadSurrogateStart) return true; |
| 229 |
} |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
// We only use this for characters made of surrogate pairs. All other |
| 234 |
// characters outside of character classes are made case independent in the |
| 235 |
// code generation. |
| 236 |
bool RegExpTextBuilder::NeedsDesugaringForIgnoreCase(base::uc32 c) { |
| 237 |
#ifdef V8_INTL_SUPPORT |
| 238 |
if (IsUnicodeMode() && ignore_case()) { |
| 239 |
icu::UnicodeSet set(c, c); |
| 240 |
set.closeOver(USET_CASE_INSENSITIVE); |
| 241 |
set.removeAllStrings(); |
| 242 |
return set.size() > 1; |
| 243 |
} |
| 244 |
// In the case where ICU is not included, we act as if the unicode flag is |
| 245 |
// not set, and do not desugar. |
| 246 |
#endif // V8_INTL_SUPPORT |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
RegExpTree* RegExpTextBuilder::PopLastAtom() { |
| 251 |
FlushPendingSurrogate(); |
| 252 |
RegExpTree* atom; |
| 253 |
if (characters_ != nullptr) { |
| 254 |
base::Vector<const base::uc16> char_vector = characters_->ToConstVector(); |
| 255 |
int num_chars = char_vector.length(); |
| 256 |
if (num_chars > 1) { |
| 257 |
base::Vector<const base::uc16> prefix = |
| 258 |
char_vector.SubVector(0, num_chars - 1); |
| 259 |
text_.emplace_back(zone()->New<RegExpAtom>(prefix)); |
| 260 |
char_vector = char_vector.SubVector(num_chars - 1, num_chars); |
| 261 |
} |
| 262 |
characters_ = nullptr; |
| 263 |
atom = zone()->New<RegExpAtom>(char_vector); |
| 264 |
return atom; |
| 265 |
} else if (!text_.empty()) { |
| 266 |
atom = text_.back(); |
| 267 |
text_.pop_back(); |
| 268 |
return atom; |
| 269 |
} |
| 270 |
return nullptr; |
| 271 |
} |
| 272 |
|
| 273 |
RegExpTree* RegExpTextBuilder::ToRegExp() { |
| 274 |
FlushText(); |
| 275 |
size_t num_alternatives = terms_->size(); |
| 276 |
if (num_alternatives == 0) return zone()->New<RegExpEmpty>(); |
| 277 |
if (num_alternatives == 1) return terms_->back(); |
| 278 |
return zone()->New<RegExpAlternative>(zone()->New<ZoneList<RegExpTree*>>( |
| 279 |
base::VectorOf(terms_->begin(), terms_->size()), zone())); |
| 280 |
} |
| 281 |
|
| 282 |
// Accumulates RegExp atoms and assertions into lists of terms and alternatives. |
| 283 |
class RegExpBuilder { |
| 284 |
public: |
| 285 |
RegExpBuilder(Zone* zone, RegExpFlags flags) |
| 286 |
: zone_(zone), |
| 287 |
flags_(flags), |
| 288 |
terms_(zone), |
| 289 |
alternatives_(zone), |
| 290 |
text_builder_(RegExpTextBuilder{zone, &terms_, flags}) {} |
| 291 |
void AddCharacter(base::uc16 character); |
| 292 |
void AddUnicodeCharacter(base::uc32 character); |
| 293 |
void AddEscapedUnicodeCharacter(base::uc32 character); |
| 294 |
// "Adds" an empty expression. Does nothing except consume a |
| 295 |
// following quantifier |
| 296 |
void AddEmpty(); |
| 297 |
void AddClassRanges(RegExpClassRanges* cc); |
| 298 |
void AddAtom(RegExpTree* tree); |
| 299 |
void AddTerm(RegExpTree* tree); |
| 300 |
void AddAssertion(RegExpTree* tree); |
| 301 |
void NewAlternative(); // '|' |
| 302 |
bool AddQuantifierToAtom(int min, int max, int index, |
| 303 |
RegExpQuantifier::QuantifierType type); |
| 304 |
void FlushText(); |
| 305 |
RegExpTree* ToRegExp(); |
| 306 |
RegExpFlags flags() const { return flags_; } |
| 307 |
|
| 308 |
bool ignore_case() const { return IsIgnoreCase(flags_); } |
| 309 |
bool multiline() const { return IsMultiline(flags_); } |
| 310 |
bool dotall() const { return IsDotAll(flags_); } |
| 311 |
|
| 312 |
private: |
| 313 |
void FlushTerms(); |
| 314 |
bool IsUnicodeMode() const { |
| 315 |
// Either /v or /u enable UnicodeMode |
| 316 |
// https://tc39.es/ecma262/#sec-parsepattern |
| 317 |
return IsUnicode(flags_) || IsUnicodeSets(flags_); |
| 318 |
} |
| 319 |
Zone* zone() const { return zone_; } |
| 320 |
RegExpTextBuilder& text_builder() { return text_builder_; } |
| 321 |
|
| 322 |
Zone* const zone_; |
| 323 |
bool pending_empty_ = false; |
| 324 |
const RegExpFlags flags_; |
| 325 |
|
| 326 |
using SmallRegExpTreeVector = SmallZoneVector<RegExpTree*, 8>; |
| 327 |
SmallRegExpTreeVector terms_; |
| 328 |
SmallRegExpTreeVector alternatives_; |
| 329 |
RegExpTextBuilder text_builder_; |
| 330 |
}; |
| 331 |
|
| 332 |
enum SubexpressionType { |
| 333 |
INITIAL, |
| 334 |
CAPTURE, // All positive values represent captures. |
| 335 |
POSITIVE_LOOKAROUND, |
| 336 |
NEGATIVE_LOOKAROUND, |
| 337 |
GROUPING |
| 338 |
}; |
| 339 |
|
| 340 |
class RegExpParserState : public ZoneObject { |
| 341 |
public: |
| 342 |
// Push a state on the stack. |
| 343 |
RegExpParserState(RegExpParserState* previous_state, |
| 344 |
SubexpressionType group_type, |
| 345 |
RegExpLookaround::Type lookaround_type, |
| 346 |
int disjunction_capture_index, |
| 347 |
const ZoneVector<base::uc16>* capture_name, |
| 348 |
RegExpFlags flags, Zone* zone) |
| 349 |
: previous_state_(previous_state), |
| 350 |
builder_(zone, flags), |
| 351 |
group_type_(group_type), |
| 352 |
lookaround_type_(lookaround_type), |
| 353 |
disjunction_capture_index_(disjunction_capture_index), |
| 354 |
capture_name_(capture_name) { |
| 355 |
if (previous_state != nullptr) { |
| 356 |
non_participating_capture_group_interval_ = |
| 357 |
previous_state->non_participating_capture_group_interval(); |
| 358 |
} |
| 359 |
} |
| 360 |
// Parser state of containing expression, if any. |
| 361 |
RegExpParserState* previous_state() const { return previous_state_; } |
| 362 |
bool IsSubexpression() { return previous_state_ != nullptr; } |
| 363 |
// RegExpBuilder building this regexp's AST. |
| 364 |
RegExpBuilder* builder() { return &builder_; } |
| 365 |
// Type of regexp being parsed (parenthesized group or entire regexp). |
| 366 |
SubexpressionType group_type() const { return group_type_; } |
| 367 |
// Lookahead or Lookbehind. |
| 368 |
RegExpLookaround::Type lookaround_type() const { return lookaround_type_; } |
| 369 |
// Index in captures array of first capture in this sub-expression, if any. |
| 370 |
// Also the capture index of this sub-expression itself, if group_type |
| 371 |
// is CAPTURE. |
| 372 |
int capture_index() const { return disjunction_capture_index_; } |
| 373 |
// The name of the current sub-expression, if group_type is CAPTURE. Only |
| 374 |
// used for named captures. |
| 375 |
const ZoneVector<base::uc16>* capture_name() const { return capture_name_; } |
| 376 |
std::pair<int, int> non_participating_capture_group_interval() const { |
| 377 |
return non_participating_capture_group_interval_; |
| 378 |
} |
| 379 |
|
| 380 |
bool IsNamedCapture() const { return capture_name_ != nullptr; } |
| 381 |
|
| 382 |
// Check whether the parser is inside a capture group with the given index. |
| 383 |
bool IsInsideCaptureGroup(int index) const { |
| 384 |
for (const RegExpParserState* s = this; s != nullptr; |
| 385 |
s = s->previous_state()) { |
| 386 |
if (s->group_type() != CAPTURE) continue; |
| 387 |
// Return true if we found the matching capture index. |
| 388 |
if (index == s->capture_index()) return true; |
| 389 |
// Abort if index is larger than what has been parsed up till this state. |
| 390 |
if (index > s->capture_index()) return false; |
| 391 |
} |
| 392 |
return false; |
| 393 |
} |
| 394 |
|
| 395 |
// Check whether the parser is inside a capture group with the given name. |
| 396 |
bool IsInsideCaptureGroup(const ZoneVector<base::uc16>* name) const { |
| 397 |
DCHECK_NOT_NULL(name); |
| 398 |
for (const RegExpParserState* s = this; s != nullptr; |
| 399 |
s = s->previous_state()) { |
| 400 |
if (s->capture_name() == nullptr) continue; |
| 401 |
if (*s->capture_name() == *name) return true; |
| 402 |
} |
| 403 |
return false; |
| 404 |
} |
| 405 |
|
| 406 |
void NewAlternative(int captures_started) { |
| 407 |
if (non_participating_capture_group_interval().second != 0) { |
| 408 |
// Extend the non-participating interval. |
| 409 |
non_participating_capture_group_interval_.second = captures_started; |
| 410 |
} else { |
| 411 |
// Create new non-participating interval from the start of the current |
| 412 |
// enclosing group to all captures created within that group so far. |
| 413 |
non_participating_capture_group_interval_ = |
| 414 |
std::make_pair(capture_index(), captures_started); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
private: |
| 419 |
// Linked list implementation of stack of states. |
| 420 |
RegExpParserState* const previous_state_; |
| 421 |
// Builder for the stored disjunction. |
| 422 |
RegExpBuilder builder_; |
| 423 |
// Stored disjunction type (capture, look-ahead or grouping), if any. |
| 424 |
const SubexpressionType group_type_; |
| 425 |
// Stored read direction. |
| 426 |
const RegExpLookaround::Type lookaround_type_; |
| 427 |
// Stored disjunction's capture index (if any). |
| 428 |
const int disjunction_capture_index_; |
| 429 |
// Stored capture name (if any). |
| 430 |
const ZoneVector<base::uc16>* const capture_name_; |
| 431 |
// Interval of (named) capture indices ]from, to] that are not participating |
| 432 |
// in the current state (i.e. they cannot match). |
| 433 |
// Capture indices are not participating if they were created in a different |
| 434 |
// alternative. |
| 435 |
std::pair<int, int> non_participating_capture_group_interval_; |
| 436 |
}; |
| 437 |
|
| 438 |
template <class CharT> |
| 439 |
class RegExpParserImpl final { |
| 440 |
private: |
| 441 |
RegExpParserImpl(const CharT* input, int input_length, RegExpFlags flags, |
| 442 |
uintptr_t stack_limit, Zone* zone, |
| 443 |
const DisallowGarbageCollection& no_gc); |
| 444 |
|
| 445 |
bool Parse(RegExpCompileData* result); |
| 446 |
|
| 447 |
RegExpTree* ParsePattern(); |
| 448 |
RegExpTree* ParseDisjunction(); |
| 449 |
RegExpTree* ParseGroup(); |
| 450 |
|
| 451 |
// Parses a {...,...} quantifier and stores the range in the given |
| 452 |
// out parameters. |
| 453 |
bool ParseIntervalQuantifier(int* min_out, int* max_out); |
| 454 |
|
| 455 |
// Checks whether the following is a length-digit hexadecimal number, |
| 456 |
// and sets the value if it is. |
| 457 |
bool ParseHexEscape(int length, base::uc32* value); |
| 458 |
bool ParseUnicodeEscape(base::uc32* value); |
| 459 |
bool ParseUnlimitedLengthHexNumber(int max_value, base::uc32* value); |
| 460 |
|
| 461 |
bool ParsePropertyClassName(ZoneVector<char>* name_1, |
| 462 |
ZoneVector<char>* name_2); |
| 463 |
bool AddPropertyClassRange(ZoneList<CharacterRange>* add_to_range, |
| 464 |
CharacterClassStrings* add_to_strings, bool negate, |
| 465 |
const ZoneVector<char>& name_1, |
| 466 |
const ZoneVector<char>& name_2); |
| 467 |
|
| 468 |
RegExpTree* ParseClassRanges(ZoneList<CharacterRange>* ranges, |
| 469 |
bool add_unicode_case_equivalents); |
| 470 |
// Parse inside a class. Either add escaped class to the range, or return |
| 471 |
// false and pass parsed single character through |char_out|. |
| 472 |
void ParseClassEscape(ZoneList<CharacterRange>* ranges, Zone* zone, |
| 473 |
bool add_unicode_case_equivalents, base::uc32* char_out, |
| 474 |
bool* is_class_escape); |
| 475 |
// Returns true iff parsing was successful. |
| 476 |
bool TryParseCharacterClassEscape(base::uc32 next, |
| 477 |
InClassEscapeState in_class_escape_state, |
| 478 |
ZoneList<CharacterRange>* ranges, |
| 479 |
CharacterClassStrings* strings, Zone* zone, |
| 480 |
bool add_unicode_case_equivalents); |
| 481 |
RegExpTree* ParseClassStringDisjunction(ZoneList<CharacterRange>* ranges, |
| 482 |
CharacterClassStrings* strings); |
| 483 |
RegExpTree* ParseClassSetOperand(const RegExpBuilder* builder, |
| 484 |
ClassSetOperandType* type_out); |
| 485 |
RegExpTree* ParseClassSetOperand(const RegExpBuilder* builder, |
| 486 |
ClassSetOperandType* type_out, |
| 487 |
ZoneList<CharacterRange>* ranges, |
| 488 |
CharacterClassStrings* strings, |
| 489 |
base::uc32* character); |
| 490 |
base::uc32 ParseClassSetCharacter(); |
| 491 |
// Parses and returns a single escaped character. |
| 492 |
base::uc32 ParseCharacterEscape(InClassEscapeState in_class_escape_state, |
| 493 |
bool* is_escaped_unicode_character); |
| 494 |
|
| 495 |
void AddMaybeSimpleCaseFoldedRange(ZoneList<CharacterRange>* ranges, |
| 496 |
CharacterRange new_range); |
| 497 |
|
| 498 |
RegExpTree* ParseClassUnion(const RegExpBuilder* builder, bool is_negated, |
| 499 |
RegExpTree* first_operand, |
| 500 |
ClassSetOperandType first_operand_type, |
| 501 |
ZoneList<CharacterRange>* ranges, |
| 502 |
CharacterClassStrings* strings, |
| 503 |
base::uc32 first_character); |
| 504 |
RegExpTree* ParseClassIntersection(const RegExpBuilder* builder, |
| 505 |
bool is_negated, RegExpTree* first_operand, |
| 506 |
ClassSetOperandType first_operand_type); |
| 507 |
RegExpTree* ParseClassSubtraction(const RegExpBuilder* builder, |
| 508 |
bool is_negated, RegExpTree* first_operand, |
| 509 |
ClassSetOperandType first_operand_type); |
| 510 |
RegExpTree* ParseCharacterClass(const RegExpBuilder* state); |
| 511 |
|
| 512 |
base::uc32 ParseOctalLiteral(); |
| 513 |
|
| 514 |
// Tries to parse the input as a back reference. If successful it |
| 515 |
// stores the result in the output parameter and returns true. If |
| 516 |
// it fails it will push back the characters read so the same characters |
| 517 |
// can be reparsed. |
| 518 |
bool ParseBackReferenceIndex(int* index_out); |
| 519 |
|
| 520 |
RegExpTree* ReportError(RegExpError error); |
| 521 |
void Advance(); |
| 522 |
void Advance(int dist); |
| 523 |
void RewindByOneCodepoint(); // Rewinds to before the previous Advance(). |
| 524 |
void Reset(int pos); |
| 525 |
|
| 526 |
// Reports whether the pattern might be used as a literal search string. |
| 527 |
// Only use if the result of the parse is a single atom node. |
| 528 |
bool simple() const { return simple_; } |
| 529 |
bool contains_anchor() const { return contains_anchor_; } |
| 530 |
void set_contains_anchor() { contains_anchor_ = true; } |
| 531 |
int captures_started() const { return captures_started_; } |
| 532 |
int position() const { |
| 533 |
const bool current_is_surrogate = |
| 534 |
current() != kEndMarker && |
| 535 |
current() > unibrow::Utf16::kMaxNonSurrogateCharCode; |
| 536 |
const int rewind_bytes = current_is_surrogate ? 2 : 1; |
| 537 |
return next_pos_ - rewind_bytes; |
| 538 |
} |
| 539 |
bool failed() const { return failed_; } |
| 540 |
RegExpFlags flags() const { return flags_; } |
| 541 |
bool IsUnicodeMode() const { |
| 542 |
// Either /v or /u enable UnicodeMode |
| 543 |
// https://tc39.es/ecma262/#sec-parsepattern |
| 544 |
return IsUnicode(flags()) || IsUnicodeSets(flags()) || force_unicode_; |
| 545 |
} |
| 546 |
bool unicode_sets() const { return IsUnicodeSets(flags()); } |
| 547 |
bool ignore_case() const { return IsIgnoreCase(flags()); } |
| 548 |
|
| 549 |
static bool IsSyntaxCharacterOrSlash(base::uc32 c); |
| 550 |
static bool IsClassSetSyntaxCharacter(base::uc32 c); |
| 551 |
static bool IsClassSetReservedPunctuator(base::uc32 c); |
| 552 |
bool IsClassSetReservedDoublePunctuator(base::uc32 c); |
| 553 |
|
| 554 |
static const base::uc32 kEndMarker = (1 << 21); |
| 555 |
|
| 556 |
private: |
| 557 |
// Return the 1-indexed RegExpCapture object, allocate if necessary. |
| 558 |
RegExpCapture* GetCapture(int index); |
| 559 |
|
| 560 |
// Creates a new named capture at the specified index. Must be called exactly |
| 561 |
// once for each named capture. Fails if a capture with the same name is |
| 562 |
// encountered. |
| 563 |
bool CreateNamedCaptureAtIndex(const RegExpParserState* state, int index); |
| 564 |
|
| 565 |
// Parses the name of a capture group (?<name>pattern). The name must adhere |
| 566 |
// to IdentifierName in the ECMAScript standard. |
| 567 |
const ZoneVector<base::uc16>* ParseCaptureGroupName(); |
| 568 |
|
| 569 |
bool ParseNamedBackReference(RegExpBuilder* builder, |
| 570 |
RegExpParserState* state); |
| 571 |
RegExpParserState* ParseOpenParenthesis(RegExpParserState* state); |
| 572 |
|
| 573 |
// After the initial parsing pass, patch corresponding RegExpCapture objects |
| 574 |
// into all RegExpBackReferences. This is done after initial parsing in order |
| 575 |
// to avoid complicating cases in which references comes before the capture. |
| 576 |
void PatchNamedBackReferences(); |
| 577 |
|
| 578 |
ZoneVector<RegExpCapture*>* GetNamedCaptures(); |
| 579 |
|
| 580 |
// Returns true iff the pattern contains named captures. May call |
| 581 |
// ScanForCaptures to look ahead at the remaining pattern. |
| 582 |
bool HasNamedCaptures(InClassEscapeState in_class_escape_state); |
| 583 |
|
| 584 |
Zone* zone() const { return zone_; } |
| 585 |
|
| 586 |
base::uc32 current() const { return current_; } |
| 587 |
bool has_more() const { return has_more_; } |
| 588 |
bool has_next() const { return next_pos_ < input_length(); } |
| 589 |
base::uc32 Next(); |
| 590 |
template <bool update_position> |
| 591 |
base::uc32 ReadNext(); |
| 592 |
CharT InputAt(int index) const { |
| 593 |
DCHECK(0 <= index && index < input_length()); |
| 594 |
return input_[index]; |
| 595 |
} |
| 596 |
int input_length() const { return input_length_; } |
| 597 |
void ScanForCaptures(InClassEscapeState in_class_escape_state); |
| 598 |
|
| 599 |
struct RegExpCaptureNameLess { |
| 600 |
bool operator()(const RegExpCapture* lhs, const RegExpCapture* rhs) const { |
| 601 |
DCHECK_NOT_NULL(lhs); |
| 602 |
DCHECK_NOT_NULL(rhs); |
| 603 |
return *lhs->name() < *rhs->name(); |
| 604 |
} |
| 605 |
}; |
| 606 |
|
| 607 |
class ForceUnicodeScope final { |
| 608 |
public: |
| 609 |
explicit ForceUnicodeScope(RegExpParserImpl<CharT>* parser) |
| 610 |
: parser_(parser) { |
| 611 |
DCHECK(!parser_->force_unicode_); |
| 612 |
parser_->force_unicode_ = true; |
| 613 |
} |
| 614 |
~ForceUnicodeScope() { |
| 615 |
DCHECK(parser_->force_unicode_); |
| 616 |
parser_->force_unicode_ = false; |
| 617 |
} |
| 618 |
|
| 619 |
private: |
| 620 |
RegExpParserImpl<CharT>* const parser_; |
| 621 |
}; |
| 622 |
|
| 623 |
const DisallowGarbageCollection no_gc_; |
| 624 |
Zone* const zone_; |
| 625 |
RegExpError error_ = RegExpError::kNone; |
| 626 |
int error_pos_ = 0; |
| 627 |
ZoneList<RegExpCapture*>* captures_; |
| 628 |
// Maps capture names to a list of capture indices with this name. |
| 629 |
ZoneMap<RegExpCapture*, ZoneList<int>*, RegExpCaptureNameLess>* |
| 630 |
named_captures_; |
| 631 |
ZoneList<RegExpBackReference*>* named_back_references_; |
| 632 |
const CharT* const input_; |
| 633 |
const int input_length_; |
| 634 |
base::uc32 current_; |
| 635 |
RegExpFlags flags_; |
| 636 |
bool force_unicode_ = false; // Force parser to act as if unicode were set. |
| 637 |
int next_pos_; |
| 638 |
int captures_started_; |
| 639 |
int capture_count_; // Only valid after we have scanned for captures. |
| 640 |
int quantifier_count_; |
| 641 |
int lookaround_count_; // Only valid after we have scanned for lookbehinds. |
| 642 |
bool has_more_; |
| 643 |
bool simple_; |
| 644 |
bool contains_anchor_; |
| 645 |
bool is_scanned_for_captures_; |
| 646 |
bool has_named_captures_; // Only valid after we have scanned for captures. |
| 647 |
bool failed_; |
| 648 |
const uintptr_t stack_limit_; |
| 649 |
|
| 650 |
friend class v8::internal::RegExpParser; |
| 651 |
}; |
| 652 |
|
| 653 |
template <class CharT> |
| 654 |
RegExpParserImpl<CharT>::RegExpParserImpl( |
| 655 |
const CharT* input, int input_length, RegExpFlags flags, |
| 656 |
uintptr_t stack_limit, Zone* zone, const DisallowGarbageCollection& no_gc) |
| 657 |
: zone_(zone), |
| 658 |
captures_(nullptr), |
| 659 |
named_captures_(nullptr), |
| 660 |
named_back_references_(nullptr), |
| 661 |
input_(input), |
| 662 |
input_length_(input_length), |
| 663 |
current_(kEndMarker), |
| 664 |
flags_(flags), |
| 665 |
next_pos_(0), |
| 666 |
captures_started_(0), |
| 667 |
capture_count_(0), |
| 668 |
quantifier_count_(0), |
| 669 |
lookaround_count_(0), |
| 670 |
has_more_(true), |
| 671 |
simple_(false), |
| 672 |
contains_anchor_(false), |
| 673 |
is_scanned_for_captures_(false), |
| 674 |
has_named_captures_(false), |
| 675 |
failed_(false), |
| 676 |
stack_limit_(stack_limit) { |
| 677 |
Advance(); |
| 678 |
} |
| 679 |
|
| 680 |
template <> |
| 681 |
template <bool update_position> |
| 682 |
inline base::uc32 RegExpParserImpl<uint8_t>::ReadNext() { |
| 683 |
int position = next_pos_; |
| 684 |
base::uc16 c0 = InputAt(position); |
| 685 |
position++; |
| 686 |
DCHECK(!unibrow::Utf16::IsLeadSurrogate(c0)); |
| 687 |
if (update_position) next_pos_ = position; |
| 688 |
return c0; |
| 689 |
} |
| 690 |
|
| 691 |
template <> |
| 692 |
template <bool update_position> |
| 693 |
inline base::uc32 RegExpParserImpl<base::uc16>::ReadNext() { |
| 694 |
int position = next_pos_; |
| 695 |
base::uc16 c0 = InputAt(position); |
| 696 |
base::uc32 result = c0; |
| 697 |
position++; |
| 698 |
// Read the whole surrogate pair in case of unicode mode, if possible. |
| 699 |
if (IsUnicodeMode() && position < input_length() && |
| 700 |
unibrow::Utf16::IsLeadSurrogate(c0)) { |
| 701 |
base::uc16 c1 = InputAt(position); |
| 702 |
if (unibrow::Utf16::IsTrailSurrogate(c1)) { |
| 703 |
result = unibrow::Utf16::CombineSurrogatePair(c0, c1); |
| 704 |
position++; |
| 705 |
} |
| 706 |
} |
| 707 |
if (update_position) next_pos_ = position; |
| 708 |
return result; |
| 709 |
} |
| 710 |
|
| 711 |
template <class CharT> |
| 712 |
base::uc32 RegExpParserImpl<CharT>::Next() { |
| 713 |
if (has_next()) { |
| 714 |
return ReadNext<false>(); |
| 715 |
} else { |
| 716 |
return kEndMarker; |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
template <class CharT> |
| 721 |
void RegExpParserImpl<CharT>::Advance() { |
| 722 |
if (has_next()) { |
| 723 |
if (GetCurrentStackPosition() < stack_limit_) { |
| 724 |
if (v8_flags.correctness_fuzzer_suppressions) { |
| 725 |
FATAL("Aborting on stack overflow"); |
| 726 |
} |
| 727 |
ReportError(RegExpError::kStackOverflow); |
| 728 |
} else { |
| 729 |
current_ = ReadNext<true>(); |
| 730 |
} |
| 731 |
} else { |
| 732 |
current_ = kEndMarker; |
| 733 |
// Advance so that position() points to 1-after-the-last-character. This is |
| 734 |
// important so that Reset() to this position works correctly. |
| 735 |
next_pos_ = input_length() + 1; |
| 736 |
has_more_ = false; |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
template <class CharT> |
| 741 |
void RegExpParserImpl<CharT>::RewindByOneCodepoint() { |
| 742 |
if (!has_more()) return; |
| 743 |
// Rewinds by one code point, i.e.: two code units if `current` is outside |
| 744 |
// the basic multilingual plane (= composed of a lead and trail surrogate), |
| 745 |
// or one code unit otherwise. |
| 746 |
const int rewind_by = |
| 747 |
current() > unibrow::Utf16::kMaxNonSurrogateCharCode ? -2 : -1; |
| 748 |
Advance(rewind_by); // Undo the last Advance. |
| 749 |
} |
| 750 |
|
| 751 |
template <class CharT> |
| 752 |
void RegExpParserImpl<CharT>::Reset(int pos) { |
| 753 |
next_pos_ = pos; |
| 754 |
has_more_ = (pos < input_length()); |
| 755 |
Advance(); |
| 756 |
} |
| 757 |
|
| 758 |
template <class CharT> |
| 759 |
void RegExpParserImpl<CharT>::Advance(int dist) { |
| 760 |
next_pos_ += dist - 1; |
| 761 |
Advance(); |
| 762 |
} |
| 763 |
|
| 764 |
// static |
| 765 |
template <class CharT> |
| 766 |
bool RegExpParserImpl<CharT>::IsSyntaxCharacterOrSlash(base::uc32 c) { |
| 767 |
switch (c) { |
| 768 |
case '^': |
| 769 |
case '$': |
| 770 |
case '\\': |
| 771 |
case '.': |
| 772 |
case '*': |
| 773 |
case '+': |
| 774 |
case '?': |
| 775 |
case '(': |
| 776 |
case ')': |
| 777 |
case '[': |
| 778 |
case ']': |
| 779 |
case '{': |
| 780 |
case '}': |
| 781 |
case '|': |
| 782 |
case '/': |
| 783 |
return true; |
| 784 |
default: |
| 785 |
break; |
| 786 |
} |
| 787 |
return false; |
| 788 |
} |
| 789 |
|
| 790 |
// static |
| 791 |
template <class CharT> |
| 792 |
bool RegExpParserImpl<CharT>::IsClassSetSyntaxCharacter(base::uc32 c) { |
| 793 |
switch (c) { |
| 794 |
case '(': |
| 795 |
case ')': |
| 796 |
case '[': |
| 797 |
case ']': |
| 798 |
case '{': |
| 799 |
case '}': |
| 800 |
case '/': |
| 801 |
case '-': |
| 802 |
case '\\': |
| 803 |
case '|': |
| 804 |
return true; |
| 805 |
default: |
| 806 |
break; |
| 807 |
} |
| 808 |
return false; |
| 809 |
} |
| 810 |
|
| 811 |
// static |
| 812 |
template <class CharT> |
| 813 |
bool RegExpParserImpl<CharT>::IsClassSetReservedPunctuator(base::uc32 c) { |
| 814 |
switch (c) { |
| 815 |
case '&': |
| 816 |
case '-': |
| 817 |
case '!': |
| 818 |
case '#': |
| 819 |
case '%': |
| 820 |
case ',': |
| 821 |
case ':': |
| 822 |
case ';': |
| 823 |
case '<': |
| 824 |
case '=': |
| 825 |
case '>': |
| 826 |
case '@': |
| 827 |
case '`': |
| 828 |
case '~': |
| 829 |
return true; |
| 830 |
default: |
| 831 |
break; |
| 832 |
} |
| 833 |
return false; |
| 834 |
} |
| 835 |
|
| 836 |
template <class CharT> |
| 837 |
bool RegExpParserImpl<CharT>::IsClassSetReservedDoublePunctuator(base::uc32 c) { |
| 838 |
#define DOUBLE_PUNCTUATOR_CASE(Char) \ |
| 839 |
case Char: \ |
| 840 |
return Next() == Char |
| 841 |
|
| 842 |
switch (c) { |
| 843 |
DOUBLE_PUNCTUATOR_CASE('&'); |
| 844 |
DOUBLE_PUNCTUATOR_CASE('!'); |
| 845 |
DOUBLE_PUNCTUATOR_CASE('#'); |
| 846 |
DOUBLE_PUNCTUATOR_CASE('$'); |
| 847 |
DOUBLE_PUNCTUATOR_CASE('%'); |
| 848 |
DOUBLE_PUNCTUATOR_CASE('*'); |
| 849 |
DOUBLE_PUNCTUATOR_CASE('+'); |
| 850 |
DOUBLE_PUNCTUATOR_CASE(','); |
| 851 |
DOUBLE_PUNCTUATOR_CASE('.'); |
| 852 |
DOUBLE_PUNCTUATOR_CASE(':'); |
| 853 |
DOUBLE_PUNCTUATOR_CASE(';'); |
| 854 |
DOUBLE_PUNCTUATOR_CASE('<'); |
| 855 |
DOUBLE_PUNCTUATOR_CASE('='); |
| 856 |
DOUBLE_PUNCTUATOR_CASE('>'); |
| 857 |
DOUBLE_PUNCTUATOR_CASE('?'); |
| 858 |
DOUBLE_PUNCTUATOR_CASE('@'); |
| 859 |
DOUBLE_PUNCTUATOR_CASE('^'); |
| 860 |
DOUBLE_PUNCTUATOR_CASE('`'); |
| 861 |
DOUBLE_PUNCTUATOR_CASE('~'); |
| 862 |
default: |
| 863 |
break; |
| 864 |
} |
| 865 |
#undef DOUBLE_PUNCTUATOR_CASE |
| 866 |
|
| 867 |
return false; |
| 868 |
} |
| 869 |
|
| 870 |
template <class CharT> |
| 871 |
RegExpTree* RegExpParserImpl<CharT>::ReportError(RegExpError error) { |
| 872 |
if (failed_) return nullptr; // Do not overwrite any existing error. |
| 873 |
failed_ = true; |
| 874 |
error_ = error; |
| 875 |
error_pos_ = position(); |
| 876 |
// Zip to the end to make sure no more input is read. |
| 877 |
current_ = kEndMarker; |
| 878 |
next_pos_ = input_length(); |
| 879 |
has_more_ = false; |
| 880 |
return nullptr; |
| 881 |
} |
| 882 |
|
| 883 |
#define CHECK_FAILED /**/); \ |
| 884 |
if (failed_) return nullptr; \ |
| 885 |
((void)0 |
| 886 |
|
| 887 |
// Pattern :: |
| 888 |
// Disjunction |
| 889 |
template <class CharT> |
| 890 |
RegExpTree* RegExpParserImpl<CharT>::ParsePattern() { |
| 891 |
RegExpTree* result = ParseDisjunction(CHECK_FAILED); |
| 892 |
PatchNamedBackReferences(CHECK_FAILED); |
| 893 |
DCHECK(!has_more()); |
| 894 |
// If the result of parsing is a literal string atom, and it has the |
| 895 |
// same length as the input, then the atom is identical to the input. |
| 896 |
if (result->IsAtom() && result->AsAtom()->length() == input_length()) { |
| 897 |
simple_ = true; |
| 898 |
} |
| 899 |
return result; |
| 900 |
} |
| 901 |
|
| 902 |
// Disjunction :: |
| 903 |
// Alternative |
| 904 |
// Alternative | Disjunction |
| 905 |
// Alternative :: |
| 906 |
// [empty] |
| 907 |
// Term Alternative |
| 908 |
// Term :: |
| 909 |
// Assertion |
| 910 |
// Atom |
| 911 |
// Atom Quantifier |
| 912 |
template <class CharT> |
| 913 |
RegExpTree* RegExpParserImpl<CharT>::ParseDisjunction() { |
| 914 |
// Used to store current state while parsing subexpressions. |
| 915 |
RegExpParserState initial_state(nullptr, INITIAL, RegExpLookaround::LOOKAHEAD, |
| 916 |
0, nullptr, flags(), zone()); |
| 917 |
RegExpParserState* state = &initial_state; |
| 918 |
// Cache the builder in a local variable for quick access. |
| 919 |
RegExpBuilder* builder = initial_state.builder(); |
| 920 |
while (true) { |
| 921 |
switch (current()) { |
| 922 |
case kEndMarker: |
| 923 |
if (failed()) return nullptr; // E.g. the initial Advance failed. |
| 924 |
if (state->IsSubexpression()) { |
| 925 |
// Inside a parenthesized group when hitting end of input. |
| 926 |
return ReportError(RegExpError::kUnterminatedGroup); |
| 927 |
} |
| 928 |
DCHECK_EQ(INITIAL, state->group_type()); |
| 929 |
// Parsing completed successfully. |
| 930 |
return builder->ToRegExp(); |
| 931 |
case ')': { |
| 932 |
if (!state->IsSubexpression()) { |
| 933 |
return ReportError(RegExpError::kUnmatchedParen); |
| 934 |
} |
| 935 |
DCHECK_NE(INITIAL, state->group_type()); |
| 936 |
|
| 937 |
Advance(); |
| 938 |
// End disjunction parsing and convert builder content to new single |
| 939 |
// regexp atom. |
| 940 |
RegExpTree* body = builder->ToRegExp(); |
| 941 |
|
| 942 |
int end_capture_index = captures_started(); |
| 943 |
|
| 944 |
int capture_index = state->capture_index(); |
| 945 |
SubexpressionType group_type = state->group_type(); |
| 946 |
|
| 947 |
// Build result of subexpression. |
| 948 |
if (group_type == CAPTURE) { |
| 949 |
if (state->IsNamedCapture()) { |
| 950 |
CreateNamedCaptureAtIndex(state, capture_index CHECK_FAILED); |
| 951 |
} |
| 952 |
RegExpCapture* capture = GetCapture(capture_index); |
| 953 |
capture->set_body(body); |
| 954 |
body = capture; |
| 955 |
} else if (group_type == GROUPING) { |
| 956 |
body = zone()->template New<RegExpGroup>(body, builder->flags()); |
| 957 |
} else { |
| 958 |
DCHECK(group_type == POSITIVE_LOOKAROUND || |
| 959 |
group_type == NEGATIVE_LOOKAROUND); |
| 960 |
bool is_positive = (group_type == POSITIVE_LOOKAROUND); |
| 961 |
body = zone()->template New<RegExpLookaround>( |
| 962 |
body, is_positive, end_capture_index - capture_index, |
| 963 |
capture_index, state->lookaround_type(), lookaround_count_); |
| 964 |
lookaround_count_++; |
| 965 |
} |
| 966 |
|
| 967 |
// Restore previous state. |
| 968 |
state = state->previous_state(); |
| 969 |
builder = state->builder(); |
| 970 |
|
| 971 |
builder->AddAtom(body); |
| 972 |
// For compatibility with JSC and ES3, we allow quantifiers after |
| 973 |
// lookaheads, and break in all cases. |
| 974 |
break; |
| 975 |
} |
| 976 |
case '|': { |
| 977 |
Advance(); |
| 978 |
state->NewAlternative(captures_started()); |
| 979 |
builder->NewAlternative(); |
| 980 |
continue; |
| 981 |
} |
| 982 |
case '*': |
| 983 |
case '+': |
| 984 |
case '?': |
| 985 |
return ReportError(RegExpError::kNothingToRepeat); |
| 986 |
case '^': { |
| 987 |
Advance(); |
| 988 |
builder->AddAssertion(zone()->template New<RegExpAssertion>( |
| 989 |
builder->multiline() ? RegExpAssertion::Type::START_OF_LINE |
| 990 |
: RegExpAssertion::Type::START_OF_INPUT)); |
| 991 |
set_contains_anchor(); |
| 992 |
continue; |
| 993 |
} |
| 994 |
case '$': { |
| 995 |
Advance(); |
| 996 |
RegExpAssertion::Type assertion_type = |
| 997 |
builder->multiline() ? RegExpAssertion::Type::END_OF_LINE |
| 998 |
: RegExpAssertion::Type::END_OF_INPUT; |
| 999 |
builder->AddAssertion( |
| 1000 |
zone()->template New<RegExpAssertion>(assertion_type)); |
| 1001 |
continue; |
| 1002 |
} |
| 1003 |
case '.': { |
| 1004 |
Advance(); |
| 1005 |
ZoneList<CharacterRange>* ranges = |
| 1006 |
zone()->template New<ZoneList<CharacterRange>>(2, zone()); |
| 1007 |
|
| 1008 |
if (builder->dotall()) { |
| 1009 |
// Everything. |
| 1010 |
CharacterRange::AddClassEscape(StandardCharacterSet::kEverything, |
| 1011 |
ranges, false, zone()); |
| 1012 |
} else { |
| 1013 |
// Everything except \x0A, \x0D, \u2028 and \u2029. |
| 1014 |
CharacterRange::AddClassEscape( |
| 1015 |
StandardCharacterSet::kNotLineTerminator, ranges, false, zone()); |
| 1016 |
} |
| 1017 |
|
| 1018 |
RegExpClassRanges* cc = |
| 1019 |
zone()->template New<RegExpClassRanges>(zone(), ranges); |
| 1020 |
builder->AddClassRanges(cc); |
| 1021 |
break; |
| 1022 |
} |
| 1023 |
case '(': { |
| 1024 |
state = ParseOpenParenthesis(state CHECK_FAILED); |
| 1025 |
builder = state->builder(); |
| 1026 |
flags_ = builder->flags(); |
| 1027 |
continue; |
| 1028 |
} |
| 1029 |
case '[': { |
| 1030 |
RegExpTree* cc = ParseCharacterClass(builder CHECK_FAILED); |
| 1031 |
if (cc->IsClassRanges()) { |
| 1032 |
builder->AddClassRanges(cc->AsClassRanges()); |
| 1033 |
} else { |
| 1034 |
DCHECK(cc->IsClassSetExpression()); |
| 1035 |
builder->AddTerm(cc); |
| 1036 |
} |
| 1037 |
break; |
| 1038 |
} |
| 1039 |
// Atom :: |
| 1040 |
// \ AtomEscape |
| 1041 |
case '\\': |
| 1042 |
switch (Next()) { |
| 1043 |
case kEndMarker: |
| 1044 |
return ReportError(RegExpError::kEscapeAtEndOfPattern); |
| 1045 |
// AtomEscape :: |
| 1046 |
// [+UnicodeMode] DecimalEscape |
| 1047 |
// [~UnicodeMode] DecimalEscape but only if the CapturingGroupNumber |
| 1048 |
// of DecimalEscape is ≤ NcapturingParens |
| 1049 |
// CharacterEscape (some cases of this mixed in too) |
| 1050 |
// |
| 1051 |
// TODO(jgruber): It may make sense to disentangle all the different |
| 1052 |
// cases and make the structure mirror the spec, e.g. for AtomEscape: |
| 1053 |
// |
| 1054 |
// if (TryParseDecimalEscape(...)) return; |
| 1055 |
// if (TryParseCharacterClassEscape(...)) return; |
| 1056 |
// if (TryParseCharacterEscape(...)) return; |
| 1057 |
// if (TryParseGroupName(...)) return; |
| 1058 |
case '1': |
| 1059 |
case '2': |
| 1060 |
case '3': |
| 1061 |
case '4': |
| 1062 |
case '5': |
| 1063 |
case '6': |
| 1064 |
case '7': |
| 1065 |
case '8': |
| 1066 |
case '9': { |
| 1067 |
int index = 0; |
| 1068 |
const bool is_backref = |
| 1069 |
ParseBackReferenceIndex(&index CHECK_FAILED); |
| 1070 |
if (is_backref) { |
| 1071 |
if (state->IsInsideCaptureGroup(index)) { |
| 1072 |
// The back reference is inside the capture group it refers to. |
| 1073 |
// Nothing can possibly have been captured yet, so we use empty |
| 1074 |
// instead. This ensures that, when checking a back reference, |
| 1075 |
// the capture registers of the referenced capture are either |
| 1076 |
// both set or both cleared. |
| 1077 |
builder->AddEmpty(); |
| 1078 |
} else { |
| 1079 |
RegExpCapture* capture = GetCapture(index); |
| 1080 |
RegExpTree* atom = |
| 1081 |
zone()->template New<RegExpBackReference>(capture, zone()); |
| 1082 |
builder->AddAtom(atom); |
| 1083 |
} |
| 1084 |
break; |
| 1085 |
} |
| 1086 |
// With /u and /v, no identity escapes except for syntax characters |
| 1087 |
// are allowed. Otherwise, all identity escapes are allowed. |
| 1088 |
if (IsUnicodeMode()) { |
| 1089 |
return ReportError(RegExpError::kInvalidEscape); |
| 1090 |
} |
| 1091 |
base::uc32 first_digit = Next(); |
| 1092 |
if (first_digit == '8' || first_digit == '9') { |
| 1093 |
builder->AddCharacter(first_digit); |
| 1094 |
Advance(2); |
| 1095 |
break; |
| 1096 |
} |
| 1097 |
[[fallthrough]]; |
| 1098 |
} |
| 1099 |
case '0': { |
| 1100 |
Advance(); |
| 1101 |
if (IsUnicodeMode() && Next() >= '0' && Next() <= '9') { |
| 1102 |
// Decimal escape with leading 0 are not parsed as octal. |
| 1103 |
return ReportError(RegExpError::kInvalidDecimalEscape); |
| 1104 |
} |
| 1105 |
base::uc32 octal = ParseOctalLiteral(); |
| 1106 |
builder->AddCharacter(octal); |
| 1107 |
break; |
| 1108 |
} |
| 1109 |
case 'b': |
| 1110 |
Advance(2); |
| 1111 |
builder->AddAssertion(zone()->template New<RegExpAssertion>( |
| 1112 |
RegExpAssertion::Type::BOUNDARY)); |
| 1113 |
continue; |
| 1114 |
case 'B': |
| 1115 |
Advance(2); |
| 1116 |
builder->AddAssertion(zone()->template New<RegExpAssertion>( |
| 1117 |
RegExpAssertion::Type::NON_BOUNDARY)); |
| 1118 |
continue; |
| 1119 |
// AtomEscape :: |
| 1120 |
// CharacterClassEscape |
| 1121 |
case 'd': |
| 1122 |
case 'D': |
| 1123 |
case 's': |
| 1124 |
case 'S': |
| 1125 |
case 'w': |
| 1126 |
case 'W': { |
| 1127 |
base::uc32 next = Next(); |
| 1128 |
ZoneList<CharacterRange>* ranges = |
| 1129 |
zone()->template New<ZoneList<CharacterRange>>(2, zone()); |
| 1130 |
bool add_unicode_case_equivalents = |
| 1131 |
IsUnicodeMode() && ignore_case(); |
| 1132 |
bool parsed_character_class_escape = TryParseCharacterClassEscape( |
| 1133 |
next, InClassEscapeState::kNotInClass, ranges, nullptr, zone(), |
| 1134 |
add_unicode_case_equivalents CHECK_FAILED); |
| 1135 |
|
| 1136 |
if (parsed_character_class_escape) { |
| 1137 |
RegExpClassRanges* cc = |
| 1138 |
zone()->template New<RegExpClassRanges>(zone(), ranges); |
| 1139 |
builder->AddClassRanges(cc); |
| 1140 |
} else { |
| 1141 |
CHECK(!IsUnicodeMode()); |
| 1142 |
Advance(2); |
| 1143 |
builder->AddCharacter(next); // IdentityEscape. |
| 1144 |
} |
| 1145 |
break; |
| 1146 |
} |
| 1147 |
case 'p': |
| 1148 |
case 'P': { |
| 1149 |
base::uc32 next = Next(); |
| 1150 |
ZoneList<CharacterRange>* ranges = |
| 1151 |
zone()->template New<ZoneList<CharacterRange>>(2, zone()); |
| 1152 |
CharacterClassStrings* strings = nullptr; |
| 1153 |
if (unicode_sets()) { |
| 1154 |
strings = zone()->template New<CharacterClassStrings>(zone()); |
| 1155 |
} |
| 1156 |
bool add_unicode_case_equivalents = ignore_case(); |
| 1157 |
bool parsed_character_class_escape = TryParseCharacterClassEscape( |
| 1158 |
next, InClassEscapeState::kNotInClass, ranges, strings, zone(), |
| 1159 |
add_unicode_case_equivalents CHECK_FAILED); |
| 1160 |
|
| 1161 |
if (parsed_character_class_escape) { |
| 1162 |
if (unicode_sets()) { |
| 1163 |
RegExpClassSetOperand* op = |
| 1164 |
zone()->template New<RegExpClassSetOperand>(ranges, |
| 1165 |
strings); |
| 1166 |
builder->AddTerm(op); |
| 1167 |
} else { |
| 1168 |
RegExpClassRanges* cc = |
| 1169 |
zone()->template New<RegExpClassRanges>(zone(), ranges); |
| 1170 |
builder->AddClassRanges(cc); |
| 1171 |
} |
| 1172 |
} else { |
| 1173 |
CHECK(!IsUnicodeMode()); |
| 1174 |
Advance(2); |
| 1175 |
builder->AddCharacter(next); // IdentityEscape. |
| 1176 |
} |
| 1177 |
break; |
| 1178 |
} |
| 1179 |
// AtomEscape :: |
| 1180 |
// k GroupName |
| 1181 |
case 'k': { |
| 1182 |
// Either an identity escape or a named back-reference. The two |
| 1183 |
// interpretations are mutually exclusive: '\k' is interpreted as |
| 1184 |
// an identity escape for non-Unicode patterns without named |
| 1185 |
// capture groups, and as the beginning of a named back-reference |
| 1186 |
// in all other cases. |
| 1187 |
const bool has_named_captures = |
| 1188 |
HasNamedCaptures(InClassEscapeState::kNotInClass CHECK_FAILED); |
| 1189 |
if (IsUnicodeMode() || has_named_captures) { |
| 1190 |
Advance(2); |
| 1191 |
ParseNamedBackReference(builder, state CHECK_FAILED); |
| 1192 |
break; |
| 1193 |
} |
| 1194 |
} |
| 1195 |
[[fallthrough]]; |
| 1196 |
// AtomEscape :: |
| 1197 |
// CharacterEscape |
| 1198 |
default: { |
| 1199 |
bool is_escaped_unicode_character = false; |
| 1200 |
base::uc32 c = ParseCharacterEscape( |
| 1201 |
InClassEscapeState::kNotInClass, |
| 1202 |
&is_escaped_unicode_character CHECK_FAILED); |
| 1203 |
if (is_escaped_unicode_character) { |
| 1204 |
builder->AddEscapedUnicodeCharacter(c); |
| 1205 |
} else { |
| 1206 |
builder->AddCharacter(c); |
| 1207 |
} |
| 1208 |
break; |
| 1209 |
} |
| 1210 |
} |
| 1211 |
break; |
| 1212 |
case '{': { |
| 1213 |
int dummy; |
| 1214 |
bool parsed = ParseIntervalQuantifier(&dummy, &dummy CHECK_FAILED); |
| 1215 |
if (parsed) return ReportError(RegExpError::kNothingToRepeat); |
| 1216 |
[[fallthrough]]; |
| 1217 |
} |
| 1218 |
case '}': |
| 1219 |
case ']': |
| 1220 |
if (IsUnicodeMode()) { |
| 1221 |
return ReportError(RegExpError::kLoneQuantifierBrackets); |
| 1222 |
} |
| 1223 |
[[fallthrough]]; |
| 1224 |
default: |
| 1225 |
builder->AddUnicodeCharacter(current()); |
| 1226 |
Advance(); |
| 1227 |
break; |
| 1228 |
} // end switch(current()) |
| 1229 |
|
| 1230 |
int min; |
| 1231 |
int max; |
| 1232 |
switch (current()) { |
| 1233 |
// QuantifierPrefix :: |
| 1234 |
// * |
| 1235 |
// + |
| 1236 |
// ? |
| 1237 |
// { |
| 1238 |
case '*': |
| 1239 |
min = 0; |
| 1240 |
max = RegExpTree::kInfinity; |
| 1241 |
Advance(); |
| 1242 |
break; |
| 1243 |
case '+': |
| 1244 |
min = 1; |
| 1245 |
max = RegExpTree::kInfinity; |
| 1246 |
Advance(); |
| 1247 |
break; |
| 1248 |
case '?': |
| 1249 |
min = 0; |
| 1250 |
max = 1; |
| 1251 |
Advance(); |
| 1252 |
break; |
| 1253 |
case '{': |
| 1254 |
if (ParseIntervalQuantifier(&min, &max)) { |
| 1255 |
if (max < min) { |
| 1256 |
return ReportError(RegExpError::kRangeOutOfOrder); |
| 1257 |
} |
| 1258 |
break; |
| 1259 |
} else if (IsUnicodeMode()) { |
| 1260 |
// Incomplete quantifiers are not allowed. |
| 1261 |
return ReportError(RegExpError::kIncompleteQuantifier); |
| 1262 |
} |
| 1263 |
continue; |
| 1264 |
default: |
| 1265 |
continue; |
| 1266 |
} |
| 1267 |
RegExpQuantifier::QuantifierType quantifier_type = RegExpQuantifier::GREEDY; |
| 1268 |
if (current() == '?') { |
| 1269 |
quantifier_type = RegExpQuantifier::NON_GREEDY; |
| 1270 |
Advance(); |
| 1271 |
} else if (v8_flags.regexp_possessive_quantifier && current() == '+') { |
| 1272 |
// v8_flags.regexp_possessive_quantifier is a debug-only flag. |
| 1273 |
quantifier_type = RegExpQuantifier::POSSESSIVE; |
| 1274 |
Advance(); |
| 1275 |
} |
| 1276 |
if (!builder->AddQuantifierToAtom(min, max, quantifier_count_, |
| 1277 |
quantifier_type)) { |
| 1278 |
return ReportError(RegExpError::kInvalidQuantifier); |
| 1279 |
} |
| 1280 |
++quantifier_count_; |
| 1281 |
} |
| 1282 |
} |
| 1283 |
|
| 1284 |
template <class CharT> |
| 1285 |
RegExpParserState* RegExpParserImpl<CharT>::ParseOpenParenthesis( |
| 1286 |
RegExpParserState* state) { |
| 1287 |
RegExpLookaround::Type lookaround_type = state->lookaround_type(); |
| 1288 |
bool is_named_capture = false; |
| 1289 |
const ZoneVector<base::uc16>* capture_name = nullptr; |
| 1290 |
SubexpressionType subexpr_type = CAPTURE; |
| 1291 |
RegExpFlags flags = state->builder()->flags(); |
| 1292 |
bool parsing_modifiers = false; |
| 1293 |
bool modifiers_polarity = true; |
| 1294 |
RegExpFlags modifiers; |
| 1295 |
Advance(); |
| 1296 |
if (current() == '?') { |
| 1297 |
do { |
| 1298 |
base::uc32 next = Next(); |
| 1299 |
switch (next) { |
| 1300 |
case '-': |
| 1301 |
if (!v8_flags.js_regexp_modifiers) { |
| 1302 |
ReportError(RegExpError::kInvalidGroup); |
| 1303 |
return nullptr; |
| 1304 |
} |
| 1305 |
Advance(); |
| 1306 |
parsing_modifiers = true; |
| 1307 |
if (modifiers_polarity == false) { |
| 1308 |
ReportError(RegExpError::kMultipleFlagDashes); |
| 1309 |
return nullptr; |
| 1310 |
} |
| 1311 |
modifiers_polarity = false; |
| 1312 |
break; |
| 1313 |
case 'm': |
| 1314 |
case 'i': |
| 1315 |
case 's': { |
| 1316 |
if (!v8_flags.js_regexp_modifiers) { |
| 1317 |
ReportError(RegExpError::kInvalidGroup); |
| 1318 |
return nullptr; |
| 1319 |
} |
| 1320 |
Advance(); |
| 1321 |
parsing_modifiers = true; |
| 1322 |
RegExpFlag flag = TryRegExpFlagFromChar(next).value(); |
| 1323 |
if ((modifiers & flag) != 0) { |
| 1324 |
ReportError(RegExpError::kRepeatedFlag); |
| 1325 |
return nullptr; |
| 1326 |
} |
| 1327 |
modifiers |= flag; |
| 1328 |
flags.set(flag, modifiers_polarity); |
| 1329 |
break; |
| 1330 |
} |
| 1331 |
case ':': |
| 1332 |
Advance(2); |
| 1333 |
parsing_modifiers = false; |
| 1334 |
subexpr_type = GROUPING; |
| 1335 |
break; |
| 1336 |
case '=': |
| 1337 |
Advance(2); |
| 1338 |
if (parsing_modifiers) { |
| 1339 |
DCHECK(v8_flags.js_regexp_modifiers); |
| 1340 |
ReportError(RegExpError::kInvalidGroup); |
| 1341 |
return nullptr; |
| 1342 |
} |
| 1343 |
lookaround_type = RegExpLookaround::LOOKAHEAD; |
| 1344 |
subexpr_type = POSITIVE_LOOKAROUND; |
| 1345 |
break; |
| 1346 |
case '!': |
| 1347 |
Advance(2); |
| 1348 |
if (parsing_modifiers) { |
| 1349 |
DCHECK(v8_flags.js_regexp_modifiers); |
| 1350 |
ReportError(RegExpError::kInvalidGroup); |
| 1351 |
return nullptr; |
| 1352 |
} |
| 1353 |
lookaround_type = RegExpLookaround::LOOKAHEAD; |
| 1354 |
subexpr_type = NEGATIVE_LOOKAROUND; |
| 1355 |
break; |
| 1356 |
case '<': |
| 1357 |
Advance(); |
| 1358 |
if (parsing_modifiers) { |
| 1359 |
DCHECK(v8_flags.js_regexp_modifiers); |
| 1360 |
ReportError(RegExpError::kInvalidGroup); |
| 1361 |
return nullptr; |
| 1362 |
} |
| 1363 |
if (Next() == '=') { |
| 1364 |
Advance(2); |
| 1365 |
lookaround_type = RegExpLookaround::LOOKBEHIND; |
| 1366 |
subexpr_type = POSITIVE_LOOKAROUND; |
| 1367 |
break; |
| 1368 |
} else if (Next() == '!') { |
| 1369 |
Advance(2); |
| 1370 |
lookaround_type = RegExpLookaround::LOOKBEHIND; |
| 1371 |
subexpr_type = NEGATIVE_LOOKAROUND; |
| 1372 |
break; |
| 1373 |
} |
| 1374 |
is_named_capture = true; |
| 1375 |
has_named_captures_ = true; |
| 1376 |
Advance(); |
| 1377 |
break; |
| 1378 |
default: |
| 1379 |
ReportError(RegExpError::kInvalidGroup); |
| 1380 |
return nullptr; |
| 1381 |
} |
| 1382 |
} while (parsing_modifiers); |
| 1383 |
} |
| 1384 |
if (modifiers_polarity == false) { |
| 1385 |
// We encountered a dash. |
| 1386 |
if (modifiers == 0) { |
| 1387 |
ReportError(RegExpError::kInvalidFlagGroup); |
| 1388 |
return nullptr; |
| 1389 |
} |
| 1390 |
} |
| 1391 |
if (subexpr_type == CAPTURE) { |
| 1392 |
if (captures_started_ >= RegExpMacroAssembler::kMaxCaptures) { |
| 1393 |
ReportError(RegExpError::kTooManyCaptures); |
| 1394 |
return nullptr; |
| 1395 |
} |
| 1396 |
captures_started_++; |
| 1397 |
|
| 1398 |
if (is_named_capture) { |
| 1399 |
capture_name = ParseCaptureGroupName(CHECK_FAILED); |
| 1400 |
} |
| 1401 |
} |
| 1402 |
// Store current state and begin new disjunction parsing. |
| 1403 |
return zone()->template New<RegExpParserState>( |
| 1404 |
state, subexpr_type, lookaround_type, captures_started_, capture_name, |
| 1405 |
flags, zone()); |
| 1406 |
} |
| 1407 |
|
| 1408 |
// In order to know whether an escape is a backreference or not we have to scan |
| 1409 |
// the entire regexp and find the number of capturing parentheses. However we |
| 1410 |
// don't want to scan the regexp twice unless it is necessary. This mini-parser |
| 1411 |
// is called when needed. It can see the difference between capturing and |
| 1412 |
// noncapturing parentheses and can skip character classes and backslash-escaped |
| 1413 |
// characters. |
| 1414 |
// |
| 1415 |
// Important: The scanner has to be in a consistent state when calling |
| 1416 |
// ScanForCaptures, e.g. not in the middle of an escape sequence '\[' or while |
| 1417 |
// parsing a nested class. |
| 1418 |
template <class CharT> |
| 1419 |
void RegExpParserImpl<CharT>::ScanForCaptures( |
| 1420 |
InClassEscapeState in_class_escape_state) { |
| 1421 |
DCHECK(!is_scanned_for_captures_); |
| 1422 |
const int saved_position = position(); |
| 1423 |
// Start with captures started previous to current position |
| 1424 |
int capture_count = captures_started(); |
| 1425 |
// When we start inside a character class, skip everything inside the class. |
| 1426 |
if (in_class_escape_state == InClassEscapeState::kInClass) { |
| 1427 |
// \k is always invalid within a class in unicode mode, thus we should never |
| 1428 |
// call ScanForCaptures within a class. |
| 1429 |
DCHECK(!IsUnicodeMode()); |
| 1430 |
int c; |
| 1431 |
while ((c = current()) != kEndMarker) { |
| 1432 |
Advance(); |
| 1433 |
if (c == '\\') { |
| 1434 |
Advance(); |
| 1435 |
} else { |
| 1436 |
if (c == ']') break; |
| 1437 |
} |
| 1438 |
} |
| 1439 |
} |
| 1440 |
// Add count of captures after this position. |
| 1441 |
int n; |
| 1442 |
while ((n = current()) != kEndMarker) { |
| 1443 |
Advance(); |
| 1444 |
switch (n) { |
| 1445 |
case '\\': |
| 1446 |
Advance(); |
| 1447 |
break; |
| 1448 |
case '[': { |
| 1449 |
int class_nest_level = 0; |
| 1450 |
int c; |
| 1451 |
while ((c = current()) != kEndMarker) { |
| 1452 |
Advance(); |
| 1453 |
if (c == '\\') { |
| 1454 |
Advance(); |
| 1455 |
} else if (c == '[') { |
| 1456 |
// With /v, '[' inside a class is treated as a nested class. |
| 1457 |
// Without /v, '[' is a normal character. |
| 1458 |
if (unicode_sets()) class_nest_level++; |
| 1459 |
} else if (c == ']') { |
| 1460 |
if (class_nest_level == 0) break; |
| 1461 |
class_nest_level--; |
| 1462 |
} |
| 1463 |
} |
| 1464 |
break; |
| 1465 |
} |
| 1466 |
case '(': |
| 1467 |
if (current() == '?') { |
| 1468 |
// At this point we could be in |
| 1469 |
// * a non-capturing group '(:', |
| 1470 |
// * a lookbehind assertion '(?<=' '(?<!' |
| 1471 |
// * or a named capture '(?<'. |
| 1472 |
// |
| 1473 |
// Of these, only named captures are capturing groups. |
| 1474 |
|
| 1475 |
Advance(); |
| 1476 |
if (current() != '<') break; |
| 1477 |
|
| 1478 |
Advance(); |
| 1479 |
if (current() == '=' || current() == '!') break; |
| 1480 |
|
| 1481 |
// Found a possible named capture. It could turn out to be a syntax |
| 1482 |
// error (e.g. an unterminated or invalid name), but that distinction |
| 1483 |
// does not matter for our purposes. |
| 1484 |
has_named_captures_ = true; |
| 1485 |
} |
| 1486 |
capture_count++; |
| 1487 |
break; |
| 1488 |
} |
| 1489 |
} |
| 1490 |
capture_count_ = capture_count; |
| 1491 |
is_scanned_for_captures_ = true; |
| 1492 |
Reset(saved_position); |
| 1493 |
} |
| 1494 |
|
| 1495 |
template <class CharT> |
| 1496 |
bool RegExpParserImpl<CharT>::ParseBackReferenceIndex(int* index_out) { |
| 1497 |
DCHECK_EQ('\\', current()); |
| 1498 |
DCHECK('1' <= Next() && Next() <= '9'); |
| 1499 |
// Try to parse a decimal literal that is no greater than the total number |
| 1500 |
// of left capturing parentheses in the input. |
| 1501 |
int start = position(); |
| 1502 |
int value = Next() - '0'; |
| 1503 |
Advance(2); |
| 1504 |
while (true) { |
| 1505 |
base::uc32 c = current(); |
| 1506 |
if (IsDecimalDigit(c)) { |
| 1507 |
value = 10 * value + (c - '0'); |
| 1508 |
if (value > RegExpMacroAssembler::kMaxCaptures) { |
| 1509 |
Reset(start); |
| 1510 |
return false; |
| 1511 |
} |
| 1512 |
Advance(); |
| 1513 |
} else { |
| 1514 |
break; |
| 1515 |
} |
| 1516 |
} |
| 1517 |
if (value > captures_started()) { |
| 1518 |
if (!is_scanned_for_captures_) { |
| 1519 |
ScanForCaptures(InClassEscapeState::kNotInClass); |
| 1520 |
} |
| 1521 |
if (value > capture_count_) { |
| 1522 |
Reset(start); |
| 1523 |
return false; |
| 1524 |
} |
| 1525 |
} |
| 1526 |
*index_out = value; |
| 1527 |
return true; |
| 1528 |
} |
| 1529 |
|
| 1530 |
namespace { |
| 1531 |
|
| 1532 |
void push_code_unit(ZoneVector<base::uc16>* v, uint32_t code_unit) { |
| 1533 |
if (code_unit <= unibrow::Utf16::kMaxNonSurrogateCharCode) { |
| 1534 |
v->push_back(code_unit); |
| 1535 |
} else { |
| 1536 |
v->push_back(unibrow::Utf16::LeadSurrogate(code_unit)); |
| 1537 |
v->push_back(unibrow::Utf16::TrailSurrogate(code_unit)); |
| 1538 |
} |
| 1539 |
} |
| 1540 |
|
| 1541 |
} // namespace |
| 1542 |
|
| 1543 |
template <class CharT> |
| 1544 |
const ZoneVector<base::uc16>* RegExpParserImpl<CharT>::ParseCaptureGroupName() { |
| 1545 |
// Due to special Advance requirements (see the next comment), rewind by one |
| 1546 |
// such that names starting with a surrogate pair are parsed correctly for |
| 1547 |
// patterns where the unicode flag is unset. |
| 1548 |
// |
| 1549 |
// Note that we use this odd pattern of rewinding the last advance in order |
| 1550 |
// to adhere to the common parser behavior of expecting `current` to point at |
| 1551 |
// the first candidate character for a function (e.g. when entering ParseFoo, |
| 1552 |
// `current` should point at the first character of Foo). |
| 1553 |
RewindByOneCodepoint(); |
| 1554 |
|
| 1555 |
ZoneVector<base::uc16>* name = |
| 1556 |
zone()->template New<ZoneVector<base::uc16>>(zone()); |
| 1557 |
|
| 1558 |
{ |
| 1559 |
// Advance behavior inside this function is tricky since |
| 1560 |
// RegExpIdentifierName explicitly enables unicode (in spec terms, sets +U) |
| 1561 |
// and thus allows surrogate pairs and \u{}-style escapes even in |
| 1562 |
// non-unicode patterns. Therefore Advance within the capture group name |
| 1563 |
// has to force-enable unicode, and outside the name revert to default |
| 1564 |
// behavior. |
| 1565 |
ForceUnicodeScope force_unicode(this); |
| 1566 |
|
| 1567 |
bool at_start = true; |
| 1568 |
while (true) { |
| 1569 |
Advance(); |
| 1570 |
base::uc32 c = current(); |
| 1571 |
|
| 1572 |
// Convert unicode escapes. |
| 1573 |
if (c == '\\' && Next() == 'u') { |
| 1574 |
Advance(2); |
| 1575 |
if (!ParseUnicodeEscape(&c)) { |
| 1576 |
ReportError(RegExpError::kInvalidUnicodeEscape); |
| 1577 |
return nullptr; |
| 1578 |
} |
| 1579 |
RewindByOneCodepoint(); |
| 1580 |
} |
| 1581 |
|
| 1582 |
// The backslash char is misclassified as both ID_Start and ID_Continue. |
| 1583 |
if (c == '\\') { |
| 1584 |
ReportError(RegExpError::kInvalidCaptureGroupName); |
| 1585 |
return nullptr; |
| 1586 |
} |
| 1587 |
|
| 1588 |
if (at_start) { |
| 1589 |
if (!IsIdentifierStart(c)) { |
| 1590 |
ReportError(RegExpError::kInvalidCaptureGroupName); |
| 1591 |
return nullptr; |
| 1592 |
} |
| 1593 |
push_code_unit(name, c); |
| 1594 |
at_start = false; |
| 1595 |
} else { |
| 1596 |
if (c == '>') { |
| 1597 |
break; |
| 1598 |
} else if (IsIdentifierPart(c)) { |
| 1599 |
push_code_unit(name, c); |
| 1600 |
} else { |
| 1601 |
ReportError(RegExpError::kInvalidCaptureGroupName); |
| 1602 |
return nullptr; |
| 1603 |
} |
| 1604 |
} |
| 1605 |
} |
| 1606 |
} |
| 1607 |
|
| 1608 |
// This final advance goes back into the state of pointing at the next |
| 1609 |
// relevant char, which the rest of the parser expects. See also the previous |
| 1610 |
// comments in this function. |
| 1611 |
Advance(); |
| 1612 |
return name; |
| 1613 |
} |
| 1614 |
|
| 1615 |
template <class CharT> |
| 1616 |
bool RegExpParserImpl<CharT>::CreateNamedCaptureAtIndex( |
| 1617 |
const RegExpParserState* state, int index) { |
| 1618 |
const ZoneVector<base::uc16>* name = state->capture_name(); |
| 1619 |
const std::pair<int, int> non_participating_capture_group_interval = |
| 1620 |
state->non_participating_capture_group_interval(); |
| 1621 |
DCHECK(0 < index && index <= captures_started_); |
| 1622 |
DCHECK_NOT_NULL(name); |
| 1623 |
|
| 1624 |
RegExpCapture* capture = GetCapture(index); |
| 1625 |
DCHECK_NULL(capture->name()); |
| 1626 |
|
| 1627 |
capture->set_name(name); |
| 1628 |
|
| 1629 |
if (named_captures_ == nullptr) { |
| 1630 |
named_captures_ = zone_->template New< |
| 1631 |
ZoneMap<RegExpCapture*, ZoneList<int>*, RegExpCaptureNameLess>>(zone()); |
| 1632 |
} else { |
| 1633 |
// Check for duplicates and bail if we find any. |
| 1634 |
const auto& named_capture_it = named_captures_->find(capture); |
| 1635 |
if (named_capture_it != named_captures_->end()) { |
| 1636 |
if (v8_flags.js_regexp_duplicate_named_groups) { |
| 1637 |
ZoneList<int>* named_capture_indices = named_capture_it->second; |
| 1638 |
DCHECK_NOT_NULL(named_capture_indices); |
| 1639 |
DCHECK(!named_capture_indices->is_empty()); |
| 1640 |
for (int named_index : *named_capture_indices) { |
| 1641 |
if (named_index < non_participating_capture_group_interval.first || |
| 1642 |
named_index > non_participating_capture_group_interval.second) { |
| 1643 |
ReportError(RegExpError::kDuplicateCaptureGroupName); |
| 1644 |
return false; |
| 1645 |
} |
| 1646 |
} |
| 1647 |
} else { |
| 1648 |
ReportError(RegExpError::kDuplicateCaptureGroupName); |
| 1649 |
return false; |
| 1650 |
} |
| 1651 |
} |
| 1652 |
} |
| 1653 |
|
| 1654 |
auto entry = named_captures_->try_emplace( |
| 1655 |
capture, zone()->template New<ZoneList<int>>(1, zone())); |
| 1656 |
entry.first->second->Add(index, zone()); |
| 1657 |
return true; |
| 1658 |
} |
| 1659 |
|
| 1660 |
template <class CharT> |
| 1661 |
bool RegExpParserImpl<CharT>::ParseNamedBackReference( |
| 1662 |
RegExpBuilder* builder, RegExpParserState* state) { |
| 1663 |
// The parser is assumed to be on the '<' in \k<name>. |
| 1664 |
if (current() != '<') { |
| 1665 |
ReportError(RegExpError::kInvalidNamedReference); |
| 1666 |
return false; |
| 1667 |
} |
| 1668 |
|
| 1669 |
Advance(); |
| 1670 |
const ZoneVector<base::uc16>* name = ParseCaptureGroupName(); |
| 1671 |
if (name == nullptr) { |
| 1672 |
return false; |
| 1673 |
} |
| 1674 |
|
| 1675 |
if (state->IsInsideCaptureGroup(name)) { |
| 1676 |
builder->AddEmpty(); |
| 1677 |
} else { |
| 1678 |
RegExpBackReference* atom = |
| 1679 |
zone()->template New<RegExpBackReference>(zone()); |
| 1680 |
atom->set_name(name); |
| 1681 |
|
| 1682 |
builder->AddAtom(atom); |
| 1683 |
|
| 1684 |
if (named_back_references_ == nullptr) { |
| 1685 |
named_back_references_ = |
| 1686 |
zone()->template New<ZoneList<RegExpBackReference*>>(1, zone()); |
| 1687 |
} |
| 1688 |
named_back_references_->Add(atom, zone()); |
| 1689 |
} |
| 1690 |
|
| 1691 |
return true; |
| 1692 |
} |
| 1693 |
|
| 1694 |
template <class CharT> |
| 1695 |
void RegExpParserImpl<CharT>::PatchNamedBackReferences() { |
| 1696 |
if (named_back_references_ == nullptr) return; |
| 1697 |
|
| 1698 |
if (named_captures_ == nullptr) { |
| 1699 |
ReportError(RegExpError::kInvalidNamedCaptureReference); |
| 1700 |
return; |
| 1701 |
} |
| 1702 |
|
| 1703 |
// Look up and patch the actual capture for each named back reference. |
| 1704 |
|
| 1705 |
for (int i = 0; i < named_back_references_->length(); i++) { |
| 1706 |
RegExpBackReference* ref = named_back_references_->at(i); |
| 1707 |
|
| 1708 |
// Capture used to search the named_captures_ by name, index of the |
| 1709 |
// capture is never used. |
| 1710 |
static const int kInvalidIndex = 0; |
| 1711 |
RegExpCapture* search_capture = |
| 1712 |
zone()->template New<RegExpCapture>(kInvalidIndex); |
| 1713 |
DCHECK_NULL(search_capture->name()); |
| 1714 |
search_capture->set_name(ref->name()); |
| 1715 |
|
| 1716 |
const auto& capture_it = named_captures_->find(search_capture); |
| 1717 |
if (capture_it == named_captures_->end()) { |
| 1718 |
ReportError(RegExpError::kInvalidNamedCaptureReference); |
| 1719 |
return; |
| 1720 |
} |
| 1721 |
|
| 1722 |
DCHECK_IMPLIES(!v8_flags.js_regexp_duplicate_named_groups, |
| 1723 |
capture_it->second->length() == 1); |
| 1724 |
for (int index : *capture_it->second) { |
| 1725 |
ref->add_capture(GetCapture(index), zone()); |
| 1726 |
} |
| 1727 |
} |
| 1728 |
} |
| 1729 |
|
| 1730 |
template <class CharT> |
| 1731 |
RegExpCapture* RegExpParserImpl<CharT>::GetCapture(int index) { |
| 1732 |
// The index for the capture groups are one-based. Its index in the list is |
| 1733 |
// zero-based. |
| 1734 |
const int known_captures = |
| 1735 |
is_scanned_for_captures_ ? capture_count_ : captures_started_; |
| 1736 |
SBXCHECK(index >= 1 && index <= known_captures); |
| 1737 |
if (captures_ == nullptr) { |
| 1738 |
captures_ = |
| 1739 |
zone()->template New<ZoneList<RegExpCapture*>>(known_captures, zone()); |
| 1740 |
} |
| 1741 |
while (captures_->length() < known_captures) { |
| 1742 |
captures_->Add(zone()->template New<RegExpCapture>(captures_->length() + 1), |
| 1743 |
zone()); |
| 1744 |
} |
| 1745 |
return captures_->at(index - 1); |
| 1746 |
} |
| 1747 |
|
| 1748 |
template <class CharT> |
| 1749 |
ZoneVector<RegExpCapture*>* RegExpParserImpl<CharT>::GetNamedCaptures() { |
| 1750 |
if (named_captures_ == nullptr) { |
| 1751 |
return nullptr; |
| 1752 |
} |
| 1753 |
DCHECK(!named_captures_->empty()); |
| 1754 |
|
| 1755 |
ZoneVector<RegExpCapture*>* flattened_named_captures = |
| 1756 |
zone()->template New<ZoneVector<RegExpCapture*>>(zone()); |
| 1757 |
for (auto capture : *named_captures_) { |
| 1758 |
DCHECK_IMPLIES(!v8_flags.js_regexp_duplicate_named_groups, |
| 1759 |
capture.second->length() == 1); |
| 1760 |
for (int index : *capture.second) { |
| 1761 |
flattened_named_captures->push_back(GetCapture(index)); |
| 1762 |
} |
| 1763 |
} |
| 1764 |
return flattened_named_captures; |
| 1765 |
} |
| 1766 |
|
| 1767 |
template <class CharT> |
| 1768 |
bool RegExpParserImpl<CharT>::HasNamedCaptures( |
| 1769 |
InClassEscapeState in_class_escape_state) { |
| 1770 |
if (has_named_captures_ || is_scanned_for_captures_) { |
| 1771 |
return has_named_captures_; |
| 1772 |
} |
| 1773 |
|
| 1774 |
ScanForCaptures(in_class_escape_state); |
| 1775 |
DCHECK(is_scanned_for_captures_); |
| 1776 |
return has_named_captures_; |
| 1777 |
} |
| 1778 |
|
| 1779 |
// QuantifierPrefix :: |
| 1780 |
// { DecimalDigits } |
| 1781 |
// { DecimalDigits , } |
| 1782 |
// { DecimalDigits , DecimalDigits } |
| 1783 |
// |
| 1784 |
// Returns true if parsing succeeds, and set the min_out and max_out |
| 1785 |
// values. Values are truncated to RegExpTree::kInfinity if they overflow. |
| 1786 |
template <class CharT> |
| 1787 |
bool RegExpParserImpl<CharT>::ParseIntervalQuantifier(int* min_out, |
| 1788 |
int* max_out) { |
| 1789 |
DCHECK_EQ(current(), '{'); |
| 1790 |
int start = position(); |
| 1791 |
Advance(); |
| 1792 |
int min = 0; |
| 1793 |
if (!IsDecimalDigit(current())) { |
| 1794 |
Reset(start); |
| 1795 |
return false; |
| 1796 |
} |
| 1797 |
while (IsDecimalDigit(current())) { |
| 1798 |
int next = current() - '0'; |
| 1799 |
if (min > (RegExpTree::kInfinity - next) / 10) { |
| 1800 |
// Overflow. Skip past remaining decimal digits and return -1. |
| 1801 |
do { |
| 1802 |
Advance(); |
| 1803 |
} while (IsDecimalDigit(current())); |
| 1804 |
min = RegExpTree::kInfinity; |
| 1805 |
break; |
| 1806 |
} |
| 1807 |
min = 10 * min + next; |
| 1808 |
Advance(); |
| 1809 |
} |
| 1810 |
int max = 0; |
| 1811 |
if (current() == '}') { |
| 1812 |
max = min; |
| 1813 |
Advance(); |
| 1814 |
} else if (current() == ',') { |
| 1815 |
Advance(); |
| 1816 |
if (current() == '}') { |
| 1817 |
max = RegExpTree::kInfinity; |
| 1818 |
Advance(); |
| 1819 |
} else { |
| 1820 |
while (IsDecimalDigit(current())) { |
| 1821 |
int next = current() - '0'; |
| 1822 |
if (max > (RegExpTree::kInfinity - next) / 10) { |
| 1823 |
do { |
| 1824 |
Advance(); |
| 1825 |
} while (IsDecimalDigit(current())); |
| 1826 |
max = RegExpTree::kInfinity; |
| 1827 |
break; |
| 1828 |
} |
| 1829 |
max = 10 * max + next; |
| 1830 |
Advance(); |
| 1831 |
} |
| 1832 |
if (current() != '}') { |
| 1833 |
Reset(start); |
| 1834 |
return false; |
| 1835 |
} |
| 1836 |
Advance(); |
| 1837 |
} |
| 1838 |
} else { |
| 1839 |
Reset(start); |
| 1840 |
return false; |
| 1841 |
} |
| 1842 |
*min_out = min; |
| 1843 |
*max_out = max; |
| 1844 |
return true; |
| 1845 |
} |
| 1846 |
|
| 1847 |
template <class CharT> |
| 1848 |
base::uc32 RegExpParserImpl<CharT>::ParseOctalLiteral() { |
| 1849 |
DCHECK(('0' <= current() && current() <= '7') || !has_more()); |
| 1850 |
// For compatibility with some other browsers (not all), we parse |
| 1851 |
// up to three octal digits with a value below 256. |
| 1852 |
// ES#prod-annexB-LegacyOctalEscapeSequence |
| 1853 |
base::uc32 value = current() - '0'; |
| 1854 |
Advance(); |
| 1855 |
if ('0' <= current() && current() <= '7') { |
| 1856 |
value = value * 8 + current() - '0'; |
| 1857 |
Advance(); |
| 1858 |
if (value < 32 && '0' <= current() && current() <= '7') { |
| 1859 |
value = value * 8 + current() - '0'; |
| 1860 |
Advance(); |
| 1861 |
} |
| 1862 |
} |
| 1863 |
return value; |
| 1864 |
} |
| 1865 |
|
| 1866 |
template <class CharT> |
| 1867 |
bool RegExpParserImpl<CharT>::ParseHexEscape(int length, base::uc32* value) { |
| 1868 |
int start = position(); |
| 1869 |
base::uc32 val = 0; |
| 1870 |
for (int i = 0; i < length; ++i) { |
| 1871 |
base::uc32 c = current(); |
| 1872 |
int d = base::HexValue(c); |
| 1873 |
if (d < 0) { |
| 1874 |
Reset(start); |
| 1875 |
return false; |
| 1876 |
} |
| 1877 |
val = val * 16 + d; |
| 1878 |
Advance(); |
| 1879 |
} |
| 1880 |
*value = val; |
| 1881 |
return true; |
| 1882 |
} |
| 1883 |
|
| 1884 |
// This parses RegExpUnicodeEscapeSequence as described in ECMA262. |
| 1885 |
template <class CharT> |
| 1886 |
bool RegExpParserImpl<CharT>::ParseUnicodeEscape(base::uc32* value) { |
| 1887 |
// Accept both \uxxxx and \u{xxxxxx} (if harmony unicode escapes are |
| 1888 |
// allowed). In the latter case, the number of hex digits between { } is |
| 1889 |
// arbitrary. \ and u have already been read. |
| 1890 |
if (current() == '{' && IsUnicodeMode()) { |
| 1891 |
int start = position(); |
| 1892 |
Advance(); |
| 1893 |
if (ParseUnlimitedLengthHexNumber(0x10FFFF, value)) { |
| 1894 |
if (current() == '}') { |
| 1895 |
Advance(); |
| 1896 |
return true; |
| 1897 |
} |
| 1898 |
} |
| 1899 |
Reset(start); |
| 1900 |
return false; |
| 1901 |
} |
| 1902 |
// \u but no {, or \u{...} escapes not allowed. |
| 1903 |
bool result = ParseHexEscape(4, value); |
| 1904 |
if (result && IsUnicodeMode() && unibrow::Utf16::IsLeadSurrogate(*value) && |
| 1905 |
current() == '\\') { |
| 1906 |
// Attempt to read trail surrogate. |
| 1907 |
int start = position(); |
| 1908 |
if (Next() == 'u') { |
| 1909 |
Advance(2); |
| 1910 |
base::uc32 trail; |
| 1911 |
if (ParseHexEscape(4, &trail) && |
| 1912 |
unibrow::Utf16::IsTrailSurrogate(trail)) { |
| 1913 |
*value = unibrow::Utf16::CombineSurrogatePair( |
| 1914 |
static_cast<base::uc16>(*value), static_cast<base::uc16>(trail)); |
| 1915 |
return true; |
| 1916 |
} |
| 1917 |
} |
| 1918 |
Reset(start); |
| 1919 |
} |
| 1920 |
return result; |
| 1921 |
} |
| 1922 |
|
| 1923 |
#ifdef V8_INTL_SUPPORT |
| 1924 |
|
| 1925 |
namespace { |
| 1926 |
|
| 1927 |
bool IsExactPropertyAlias(const char* property_name, UProperty property) { |
| 1928 |
const char* short_name = u_getPropertyName(property, U_SHORT_PROPERTY_NAME); |
| 1929 |
if (short_name != nullptr && strcmp(property_name, short_name) == 0) |
| 1930 |
return true; |
| 1931 |
for (int i = 0;; i++) { |
| 1932 |
const char* long_name = u_getPropertyName( |
| 1933 |
property, static_cast<UPropertyNameChoice>(U_LONG_PROPERTY_NAME + i)); |
| 1934 |
if (long_name == nullptr) break; |
| 1935 |
if (strcmp(property_name, long_name) == 0) return true; |
| 1936 |
} |
| 1937 |
return false; |
| 1938 |
} |
| 1939 |
|
| 1940 |
bool IsExactPropertyValueAlias(const char* property_value_name, |
| 1941 |
UProperty property, int32_t property_value) { |
| 1942 |
const char* short_name = |
| 1943 |
u_getPropertyValueName(property, property_value, U_SHORT_PROPERTY_NAME); |
| 1944 |
if (short_name != nullptr && strcmp(property_value_name, short_name) == 0) { |
| 1945 |
return true; |
| 1946 |
} |
| 1947 |
for (int i = 0;; i++) { |
| 1948 |
const char* long_name = u_getPropertyValueName( |
| 1949 |
property, property_value, |
| 1950 |
static_cast<UPropertyNameChoice>(U_LONG_PROPERTY_NAME + i)); |
| 1951 |
if (long_name == nullptr) break; |
| 1952 |
if (strcmp(property_value_name, long_name) == 0) return true; |
| 1953 |
} |
| 1954 |
return false; |
| 1955 |
} |
| 1956 |
|
| 1957 |
void ExtractStringsFromUnicodeSet(const icu::UnicodeSet& set, |
| 1958 |
CharacterClassStrings* strings, |
| 1959 |
RegExpFlags flags, Zone* zone) { |
| 1960 |
DCHECK(set.hasStrings()); |
| 1961 |
DCHECK(IsUnicodeSets(flags)); |
| 1962 |
DCHECK_NOT_NULL(strings); |
| 1963 |
|
| 1964 |
RegExpTextBuilder::SmallRegExpTreeVector string_storage(zone); |
| 1965 |
RegExpTextBuilder string_builder(zone, &string_storage, flags); |
| 1966 |
const bool needs_case_folding = IsIgnoreCase(flags); |
| 1967 |
icu::UnicodeSetIterator iter(set); |
| 1968 |
iter.skipToStrings(); |
| 1969 |
while (iter.next()) { |
| 1970 |
const icu::UnicodeString& s = iter.getString(); |
| 1971 |
const char16_t* p = s.getBuffer(); |
| 1972 |
int32_t length = s.length(); |
| 1973 |
ZoneList<base::uc32>* string = |
| 1974 |
zone->template New<ZoneList<base::uc32>>(length, zone); |
| 1975 |
for (int32_t i = 0; i < length;) { |
| 1976 |
UChar32 c; |
| 1977 |
U16_NEXT(p, i, length, c); |
| 1978 |
string_builder.AddUnicodeCharacter(c); |
| 1979 |
if (needs_case_folding) { |
| 1980 |
c = u_foldCase(c, U_FOLD_CASE_DEFAULT); |
| 1981 |
} |
| 1982 |
string->Add(c, zone); |
| 1983 |
} |
| 1984 |
strings->emplace(string->ToVector(), string_builder.ToRegExp()); |
| 1985 |
string_storage.clear(); |
| 1986 |
} |
| 1987 |
} |
| 1988 |
|
| 1989 |
bool LookupPropertyValueName(UProperty property, |
| 1990 |
const char* property_value_name, bool negate, |
| 1991 |
ZoneList<CharacterRange>* result_ranges, |
| 1992 |
CharacterClassStrings* result_strings, |
| 1993 |
RegExpFlags flags, Zone* zone) { |
| 1994 |
UProperty property_for_lookup = property; |
| 1995 |
if (property_for_lookup == UCHAR_SCRIPT_EXTENSIONS) { |
| 1996 |
// For the property Script_Extensions, we have to do the property value |
| 1997 |
// name lookup as if the property is Script. |
| 1998 |
property_for_lookup = UCHAR_SCRIPT; |
| 1999 |
} |
| 2000 |
int32_t property_value = |
| 2001 |
u_getPropertyValueEnum(property_for_lookup, property_value_name); |
| 2002 |
if (property_value == UCHAR_INVALID_CODE) return false; |
| 2003 |
|
| 2004 |
// We require the property name to match exactly to one of the property value |
| 2005 |
// aliases. However, u_getPropertyValueEnum uses loose matching. |
| 2006 |
if (!IsExactPropertyValueAlias(property_value_name, property_for_lookup, |
| 2007 |
property_value)) { |
| 2008 |
return false; |
| 2009 |
} |
| 2010 |
|
| 2011 |
UErrorCode ec = U_ZERO_ERROR; |
| 2012 |
icu::UnicodeSet set; |
| 2013 |
set.applyIntPropertyValue(property, property_value, ec); |
| 2014 |
bool success = ec == U_ZERO_ERROR && !set.isEmpty(); |
| 2015 |
|
| 2016 |
if (success) { |
| 2017 |
if (set.hasStrings()) { |
| 2018 |
ExtractStringsFromUnicodeSet(set, result_strings, flags, zone); |
| 2019 |
} |
| 2020 |
const bool needs_case_folding = IsUnicodeSets(flags) && IsIgnoreCase(flags); |
| 2021 |
if (needs_case_folding) set.closeOver(USET_SIMPLE_CASE_INSENSITIVE); |
| 2022 |
set.removeAllStrings(); |
| 2023 |
if (negate) set.complement(); |
| 2024 |
for (int i = 0; i < set.getRangeCount(); i++) { |
| 2025 |
result_ranges->Add( |
| 2026 |
CharacterRange::Range(set.getRangeStart(i), set.getRangeEnd(i)), |
| 2027 |
zone); |
| 2028 |
} |
| 2029 |
} |
| 2030 |
return success; |
| 2031 |
} |
| 2032 |
|
| 2033 |
template <size_t N> |
| 2034 |
inline bool NameEquals(const char* name, const char (&literal)[N]) { |
| 2035 |
return strncmp(name, literal, N + 1) == 0; |
| 2036 |
} |
| 2037 |
|
| 2038 |
bool LookupSpecialPropertyValueName(const char* name, |
| 2039 |
ZoneList<CharacterRange>* result, |
| 2040 |
bool negate, RegExpFlags flags, |
| 2041 |
Zone* zone) { |
| 2042 |
if (NameEquals(name, "Any")) { |
| 2043 |
if (negate) { |
| 2044 |
// Leave the list of character ranges empty, since the negation of 'Any' |
| 2045 |
// is the empty set. |
| 2046 |
} else { |
| 2047 |
result->Add(CharacterRange::Everything(), zone); |
| 2048 |
} |
| 2049 |
} else if (NameEquals(name, "ASCII")) { |
| 2050 |
result->Add(negate ? CharacterRange::Range(0x80, String::kMaxCodePoint) |
| 2051 |
: CharacterRange::Range(0x0, 0x7F), |
| 2052 |
zone); |
| 2053 |
} else if (NameEquals(name, "Assigned")) { |
| 2054 |
return LookupPropertyValueName(UCHAR_GENERAL_CATEGORY, "Unassigned", |
| 2055 |
!negate, result, nullptr, flags, zone); |
| 2056 |
} else { |
| 2057 |
return false; |
| 2058 |
} |
| 2059 |
return true; |
| 2060 |
} |
| 2061 |
|
| 2062 |
// Explicitly allowlist supported binary properties. The spec forbids supporting |
| 2063 |
// properties outside of this set to ensure interoperability. |
| 2064 |
bool IsSupportedBinaryProperty(UProperty property, bool unicode_sets) { |
| 2065 |
switch (property) { |
| 2066 |
case UCHAR_ALPHABETIC: |
| 2067 |
// 'Any' is not supported by ICU. See LookupSpecialPropertyValueName. |
| 2068 |
// 'ASCII' is not supported by ICU. See LookupSpecialPropertyValueName. |
| 2069 |
case UCHAR_ASCII_HEX_DIGIT: |
| 2070 |
// 'Assigned' is not supported by ICU. See LookupSpecialPropertyValueName. |
| 2071 |
case UCHAR_BIDI_CONTROL: |
| 2072 |
case UCHAR_BIDI_MIRRORED: |
| 2073 |
case UCHAR_CASE_IGNORABLE: |
| 2074 |
case UCHAR_CASED: |
| 2075 |
case UCHAR_CHANGES_WHEN_CASEFOLDED: |
| 2076 |
case UCHAR_CHANGES_WHEN_CASEMAPPED: |
| 2077 |
case UCHAR_CHANGES_WHEN_LOWERCASED: |
| 2078 |
case UCHAR_CHANGES_WHEN_NFKC_CASEFOLDED: |
| 2079 |
case UCHAR_CHANGES_WHEN_TITLECASED: |
| 2080 |
case UCHAR_CHANGES_WHEN_UPPERCASED: |
| 2081 |
case UCHAR_DASH: |
| 2082 |
case UCHAR_DEFAULT_IGNORABLE_CODE_POINT: |
| 2083 |
case UCHAR_DEPRECATED: |
| 2084 |
case UCHAR_DIACRITIC: |
| 2085 |
case UCHAR_EMOJI: |
| 2086 |
case UCHAR_EMOJI_COMPONENT: |
| 2087 |
case UCHAR_EMOJI_MODIFIER_BASE: |
| 2088 |
case UCHAR_EMOJI_MODIFIER: |
| 2089 |
case UCHAR_EMOJI_PRESENTATION: |
| 2090 |
case UCHAR_EXTENDED_PICTOGRAPHIC: |
| 2091 |
case UCHAR_EXTENDER: |
| 2092 |
case UCHAR_GRAPHEME_BASE: |
| 2093 |
case UCHAR_GRAPHEME_EXTEND: |
| 2094 |
case UCHAR_HEX_DIGIT: |
| 2095 |
case UCHAR_ID_CONTINUE: |
| 2096 |
case UCHAR_ID_START: |
| 2097 |
case UCHAR_IDEOGRAPHIC: |
| 2098 |
case UCHAR_IDS_BINARY_OPERATOR: |
| 2099 |
case UCHAR_IDS_TRINARY_OPERATOR: |
| 2100 |
case UCHAR_JOIN_CONTROL: |
| 2101 |
case UCHAR_LOGICAL_ORDER_EXCEPTION: |
| 2102 |
case UCHAR_LOWERCASE: |
| 2103 |
case UCHAR_MATH: |
| 2104 |
case UCHAR_NONCHARACTER_CODE_POINT: |
| 2105 |
case UCHAR_PATTERN_SYNTAX: |
| 2106 |
case UCHAR_PATTERN_WHITE_SPACE: |
| 2107 |
case UCHAR_QUOTATION_MARK: |
| 2108 |
case UCHAR_RADICAL: |
| 2109 |
case UCHAR_REGIONAL_INDICATOR: |
| 2110 |
case UCHAR_S_TERM: |
| 2111 |
case UCHAR_SOFT_DOTTED: |
| 2112 |
case UCHAR_TERMINAL_PUNCTUATION: |
| 2113 |
case UCHAR_UNIFIED_IDEOGRAPH: |
| 2114 |
case UCHAR_UPPERCASE: |
| 2115 |
case UCHAR_VARIATION_SELECTOR: |
| 2116 |
case UCHAR_WHITE_SPACE: |
| 2117 |
case UCHAR_XID_CONTINUE: |
| 2118 |
case UCHAR_XID_START: |
| 2119 |
return true; |
| 2120 |
case UCHAR_BASIC_EMOJI: |
| 2121 |
case UCHAR_EMOJI_KEYCAP_SEQUENCE: |
| 2122 |
case UCHAR_RGI_EMOJI_MODIFIER_SEQUENCE: |
| 2123 |
case UCHAR_RGI_EMOJI_FLAG_SEQUENCE: |
| 2124 |
case UCHAR_RGI_EMOJI_TAG_SEQUENCE: |
| 2125 |
case UCHAR_RGI_EMOJI_ZWJ_SEQUENCE: |
| 2126 |
case UCHAR_RGI_EMOJI: |
| 2127 |
return unicode_sets; |
| 2128 |
default: |
| 2129 |
break; |
| 2130 |
} |
| 2131 |
return false; |
| 2132 |
} |
| 2133 |
|
| 2134 |
bool IsBinaryPropertyOfStrings(UProperty property) { |
| 2135 |
switch (property) { |
| 2136 |
case UCHAR_BASIC_EMOJI: |
| 2137 |
case UCHAR_EMOJI_KEYCAP_SEQUENCE: |
| 2138 |
case UCHAR_RGI_EMOJI_MODIFIER_SEQUENCE: |
| 2139 |
case UCHAR_RGI_EMOJI_FLAG_SEQUENCE: |
| 2140 |
case UCHAR_RGI_EMOJI_TAG_SEQUENCE: |
| 2141 |
case UCHAR_RGI_EMOJI_ZWJ_SEQUENCE: |
| 2142 |
case UCHAR_RGI_EMOJI: |
| 2143 |
return true; |
| 2144 |
default: |
| 2145 |
break; |
| 2146 |
} |
| 2147 |
return false; |
| 2148 |
} |
| 2149 |
|
| 2150 |
bool IsUnicodePropertyValueCharacter(char c) { |
| 2151 |
// https://tc39.github.io/proposal-regexp-unicode-property-escapes/ |
| 2152 |
// |
| 2153 |
// Note that using this to validate each parsed char is quite conservative. |
| 2154 |
// A possible alternative solution would be to only ensure the parsed |
| 2155 |
// property name/value candidate string does not contain '\0' characters and |
| 2156 |
// let ICU lookups trigger the final failure. |
| 2157 |
if ('a' <= c && c <= 'z') return true; |
| 2158 |
if ('A' <= c && c <= 'Z') return true; |
| 2159 |
if ('0' <= c && c <= '9') return true; |
| 2160 |
return (c == '_'); |
| 2161 |
} |
| 2162 |
|
| 2163 |
} // namespace |
| 2164 |
|
| 2165 |
template <class CharT> |
| 2166 |
bool RegExpParserImpl<CharT>::ParsePropertyClassName(ZoneVector<char>* name_1, |
| 2167 |
ZoneVector<char>* name_2) { |
| 2168 |
DCHECK(name_1->empty()); |
| 2169 |
DCHECK(name_2->empty()); |
| 2170 |
// Parse the property class as follows: |
| 2171 |
// - In \p{name}, 'name' is interpreted |
| 2172 |
// - either as a general category property value name. |
| 2173 |
// - or as a binary property name. |
| 2174 |
// - In \p{name=value}, 'name' is interpreted as an enumerated property name, |
| 2175 |
// and 'value' is interpreted as one of the available property value names. |
| 2176 |
// - Aliases in PropertyAlias.txt and PropertyValueAlias.txt can be used. |
| 2177 |
// - Loose matching is not applied. |
| 2178 |
if (current() == '{') { |
| 2179 |
// Parse \p{[PropertyName=]PropertyNameValue} |
| 2180 |
for (Advance(); current() != '}' && current() != '='; Advance()) { |
| 2181 |
if (!IsUnicodePropertyValueCharacter(current())) return false; |
| 2182 |
if (!has_next()) return false; |
| 2183 |
name_1->push_back(static_cast<char>(current())); |
| 2184 |
} |
| 2185 |
if (current() == '=') { |
| 2186 |
for (Advance(); current() != '}'; Advance()) { |
| 2187 |
if (!IsUnicodePropertyValueCharacter(current())) return false; |
| 2188 |
if (!has_next()) return false; |
| 2189 |
name_2->push_back(static_cast<char>(current())); |
| 2190 |
} |
| 2191 |
name_2->push_back(0); // null-terminate string. |
| 2192 |
} |
| 2193 |
} else { |
| 2194 |
return false; |
| 2195 |
} |
| 2196 |
Advance(); |
| 2197 |
name_1->push_back(0); // null-terminate string. |
| 2198 |
|
| 2199 |
DCHECK(name_1->size() - 1 == std::strlen(name_1->data())); |
| 2200 |
DCHECK(name_2->empty() || name_2->size() - 1 == std::strlen(name_2->data())); |
| 2201 |
return true; |
| 2202 |
} |
| 2203 |
|
| 2204 |
template <class CharT> |
| 2205 |
bool RegExpParserImpl<CharT>::AddPropertyClassRange( |
| 2206 |
ZoneList<CharacterRange>* add_to_ranges, |
| 2207 |
CharacterClassStrings* add_to_strings, bool negate, |
| 2208 |
const ZoneVector<char>& name_1, const ZoneVector<char>& name_2) { |
| 2209 |
if (name_2.empty()) { |
| 2210 |
// First attempt to interpret as general category property value name. |
| 2211 |
const char* name = name_1.data(); |
| 2212 |
if (LookupPropertyValueName(UCHAR_GENERAL_CATEGORY_MASK, name, negate, |
| 2213 |
add_to_ranges, add_to_strings, flags(), |
| 2214 |
zone())) { |
| 2215 |
return true; |
| 2216 |
} |
| 2217 |
// Interpret "Any", "ASCII", and "Assigned". |
| 2218 |
if (LookupSpecialPropertyValueName(name, add_to_ranges, negate, flags(), |
| 2219 |
zone())) { |
| 2220 |
return true; |
| 2221 |
} |
| 2222 |
// Then attempt to interpret as binary property name with value name 'Y'. |
| 2223 |
UProperty property = u_getPropertyEnum(name); |
| 2224 |
if (!IsSupportedBinaryProperty(property, unicode_sets())) return false; |
| 2225 |
if (!IsExactPropertyAlias(name, property)) return false; |
| 2226 |
// Negation of properties with strings is not allowed. |
| 2227 |
// See |
| 2228 |
// https://tc39.es/ecma262/#sec-static-semantics-maycontainstrings |
| 2229 |
if (negate && IsBinaryPropertyOfStrings(property)) return false; |
| 2230 |
if (unicode_sets()) { |
| 2231 |
// In /v mode we can't simple lookup the "false" binary property values, |
| 2232 |
// as the spec requires us to perform case folding before calculating the |
| 2233 |
// complement. |
| 2234 |
// See https://tc39.es/ecma262/#sec-compiletocharset |
| 2235 |
// UnicodePropertyValueExpression :: LoneUnicodePropertyNameOrValue |
| 2236 |
return LookupPropertyValueName(property, "Y", negate, add_to_ranges, |
| 2237 |
add_to_strings, flags(), zone()); |
| 2238 |
} else { |
| 2239 |
return LookupPropertyValueName(property, negate ? "N" : "Y", false, |
| 2240 |
add_to_ranges, add_to_strings, flags(), |
| 2241 |
zone()); |
| 2242 |
} |
| 2243 |
} else { |
| 2244 |
// Both property name and value name are specified. Attempt to interpret |
| 2245 |
// the property name as enumerated property. |
| 2246 |
const char* property_name = name_1.data(); |
| 2247 |
const char* value_name = name_2.data(); |
| 2248 |
UProperty property = u_getPropertyEnum(property_name); |
| 2249 |
if (!IsExactPropertyAlias(property_name, property)) return false; |
| 2250 |
if (property == UCHAR_GENERAL_CATEGORY) { |
| 2251 |
// We want to allow aggregate value names such as "Letter". |
| 2252 |
property = UCHAR_GENERAL_CATEGORY_MASK; |
| 2253 |
} else if (property != UCHAR_SCRIPT && |
| 2254 |
property != UCHAR_SCRIPT_EXTENSIONS) { |
| 2255 |
return false; |
| 2256 |
} |
| 2257 |
return LookupPropertyValueName(property, value_name, negate, add_to_ranges, |
| 2258 |
add_to_strings, flags(), zone()); |
| 2259 |
} |
| 2260 |
} |
| 2261 |
|
| 2262 |
#else // V8_INTL_SUPPORT |
| 2263 |
|
| 2264 |
template <class CharT> |
| 2265 |
bool RegExpParserImpl<CharT>::ParsePropertyClassName(ZoneVector<char>* name_1, |
| 2266 |
ZoneVector<char>* name_2) { |
| 2267 |
return false; |
| 2268 |
} |
| 2269 |
|
| 2270 |
template <class CharT> |
| 2271 |
bool RegExpParserImpl<CharT>::AddPropertyClassRange( |
| 2272 |
ZoneList<CharacterRange>* add_to_ranges, |
| 2273 |
CharacterClassStrings* add_to_strings, bool negate, |
| 2274 |
const ZoneVector<char>& name_1, const ZoneVector<char>& name_2) { |
| 2275 |
return false; |
| 2276 |
} |
| 2277 |
|
| 2278 |
#endif // V8_INTL_SUPPORT |
| 2279 |
|
| 2280 |
template <class CharT> |
| 2281 |
bool RegExpParserImpl<CharT>::ParseUnlimitedLengthHexNumber(int max_value, |
| 2282 |
base::uc32* value) { |
| 2283 |
base::uc32 x = 0; |
| 2284 |
int d = base::HexValue(current()); |
| 2285 |
if (d < 0) { |
| 2286 |
return false; |
| 2287 |
} |
| 2288 |
while (d >= 0) { |
| 2289 |
x = x * 16 + d; |
| 2290 |
if (x > static_cast<base::uc32>(max_value)) { |
| 2291 |
return false; |
| 2292 |
} |
| 2293 |
Advance(); |
| 2294 |
d = base::HexValue(current()); |
| 2295 |
} |
| 2296 |
*value = x; |
| 2297 |
return true; |
| 2298 |
} |
| 2299 |
|
| 2300 |
// https://tc39.es/ecma262/#prod-CharacterEscape |
| 2301 |
template <class CharT> |
| 2302 |
base::uc32 RegExpParserImpl<CharT>::ParseCharacterEscape( |
| 2303 |
InClassEscapeState in_class_escape_state, |
| 2304 |
bool* is_escaped_unicode_character) { |
| 2305 |
DCHECK_EQ('\\', current()); |
| 2306 |
DCHECK(has_next()); |
| 2307 |
|
| 2308 |
Advance(); |
| 2309 |
|
| 2310 |
const base::uc32 c = current(); |
| 2311 |
switch (c) { |
| 2312 |
// CharacterEscape :: |
| 2313 |
// ControlEscape :: one of |
| 2314 |
// f n r t v |
| 2315 |
case 'f': |
| 2316 |
Advance(); |
| 2317 |
return '\f'; |
| 2318 |
case 'n': |
| 2319 |
Advance(); |
| 2320 |
return '\n'; |
| 2321 |
case 'r': |
| 2322 |
Advance(); |
| 2323 |
return '\r'; |
| 2324 |
case 't': |
| 2325 |
Advance(); |
| 2326 |
return '\t'; |
| 2327 |
case 'v': |
| 2328 |
Advance(); |
| 2329 |
return '\v'; |
| 2330 |
// CharacterEscape :: |
| 2331 |
// c ControlLetter |
| 2332 |
case 'c': { |
| 2333 |
base::uc32 controlLetter = Next(); |
| 2334 |
base::uc32 letter = controlLetter & ~('A' ^ 'a'); |
| 2335 |
if (letter >= 'A' && letter <= 'Z') { |
| 2336 |
Advance(2); |
| 2337 |
// Control letters mapped to ASCII control characters in the range |
| 2338 |
// 0x00-0x1F. |
| 2339 |
return controlLetter & 0x1F; |
| 2340 |
} |
| 2341 |
if (IsUnicodeMode()) { |
| 2342 |
// With /u and /v, invalid escapes are not treated as identity escapes. |
| 2343 |
ReportError(RegExpError::kInvalidUnicodeEscape); |
| 2344 |
return 0; |
| 2345 |
} |
| 2346 |
if (in_class_escape_state == InClassEscapeState::kInClass) { |
| 2347 |
// Inside a character class, we also accept digits and underscore as |
| 2348 |
// control characters, unless with /u or /v. See Annex B: |
| 2349 |
// ES#prod-annexB-ClassControlLetter |
| 2350 |
if ((controlLetter >= '0' && controlLetter <= '9') || |
| 2351 |
controlLetter == '_') { |
| 2352 |
Advance(2); |
| 2353 |
return controlLetter & 0x1F; |
| 2354 |
} |
| 2355 |
} |
| 2356 |
// We match JSC in reading the backslash as a literal |
| 2357 |
// character instead of as starting an escape. |
| 2358 |
return '\\'; |
| 2359 |
} |
| 2360 |
// CharacterEscape :: |
| 2361 |
// 0 [lookahead ∉ DecimalDigit] |
| 2362 |
// [~UnicodeMode] LegacyOctalEscapeSequence |
| 2363 |
case '0': |
| 2364 |
// \0 is interpreted as NUL if not followed by another digit. |
| 2365 |
if (Next() < '0' || Next() > '9') { |
| 2366 |
Advance(); |
| 2367 |
return 0; |
| 2368 |
} |
| 2369 |
[[fallthrough]]; |
| 2370 |
case '1': |
| 2371 |
case '2': |
| 2372 |
case '3': |
| 2373 |
case '4': |
| 2374 |
case '5': |
| 2375 |
case '6': |
| 2376 |
case '7': |
| 2377 |
// For compatibility, we interpret a decimal escape that isn't |
| 2378 |
// a back reference (and therefore either \0 or not valid according |
| 2379 |
// to the specification) as a 1..3 digit octal character code. |
| 2380 |
// ES#prod-annexB-LegacyOctalEscapeSequence |
| 2381 |
if (IsUnicodeMode()) { |
| 2382 |
// With /u or /v, decimal escape is not interpreted as octal character |
| 2383 |
// code. |
| 2384 |
ReportError(RegExpError::kInvalidDecimalEscape); |
| 2385 |
return 0; |
| 2386 |
} |
| 2387 |
return ParseOctalLiteral(); |
| 2388 |
// CharacterEscape :: |
| 2389 |
// HexEscapeSequence |
| 2390 |
case 'x': { |
| 2391 |
Advance(); |
| 2392 |
base::uc32 value; |
| 2393 |
if (ParseHexEscape(2, &value)) return value; |
| 2394 |
if (IsUnicodeMode()) { |
| 2395 |
// With /u or /v, invalid escapes are not treated as identity escapes. |
| 2396 |
ReportError(RegExpError::kInvalidEscape); |
| 2397 |
return 0; |
| 2398 |
} |
| 2399 |
// If \x is not followed by a two-digit hexadecimal, treat it |
| 2400 |
// as an identity escape. |
| 2401 |
return 'x'; |
| 2402 |
} |
| 2403 |
// CharacterEscape :: |
| 2404 |
// RegExpUnicodeEscapeSequence [?UnicodeMode] |
| 2405 |
case 'u': { |
| 2406 |
Advance(); |
| 2407 |
base::uc32 value; |
| 2408 |
if (ParseUnicodeEscape(&value)) { |
| 2409 |
*is_escaped_unicode_character = true; |
| 2410 |
return value; |
| 2411 |
} |
| 2412 |
if (IsUnicodeMode()) { |
| 2413 |
// With /u or /v, invalid escapes are not treated as identity escapes. |
| 2414 |
ReportError(RegExpError::kInvalidUnicodeEscape); |
| 2415 |
return 0; |
| 2416 |
} |
| 2417 |
// If \u is not followed by a two-digit hexadecimal, treat it |
| 2418 |
// as an identity escape. |
| 2419 |
return 'u'; |
| 2420 |
} |
| 2421 |
default: |
| 2422 |
break; |
| 2423 |
} |
| 2424 |
|
| 2425 |
// CharacterEscape :: |
| 2426 |
// IdentityEscape[?UnicodeMode, ?N] |
| 2427 |
// |
| 2428 |
// * With /u, no identity escapes except for syntax characters are |
| 2429 |
// allowed. |
| 2430 |
// * With /v, no identity escapes except for syntax characters and |
| 2431 |
// ClassSetReservedPunctuators (if within a class) are allowed. |
| 2432 |
// * Without /u or /v: |
| 2433 |
// * '\c' is not an IdentityEscape. |
| 2434 |
// * '\k' is not an IdentityEscape when named captures exist. |
| 2435 |
// * Otherwise, all identity escapes are allowed. |
| 2436 |
if (unicode_sets() && in_class_escape_state == InClassEscapeState::kInClass) { |
| 2437 |
if (IsClassSetReservedPunctuator(c)) { |
| 2438 |
Advance(); |
| 2439 |
return c; |
| 2440 |
} |
| 2441 |
} |
| 2442 |
if (IsUnicodeMode()) { |
| 2443 |
if (!IsSyntaxCharacterOrSlash(c)) { |
| 2444 |
ReportError(RegExpError::kInvalidEscape); |
| 2445 |
return 0; |
| 2446 |
} |
| 2447 |
Advance(); |
| 2448 |
return c; |
| 2449 |
} |
| 2450 |
DCHECK(!IsUnicodeMode()); |
| 2451 |
if (c == 'c') { |
| 2452 |
ReportError(RegExpError::kInvalidEscape); |
| 2453 |
return 0; |
| 2454 |
} |
| 2455 |
Advance(); |
| 2456 |
// Note: It's important to Advance before the HasNamedCaptures call s.t. we |
| 2457 |
// don't start scanning in the middle of an escape. |
| 2458 |
if (c == 'k' && HasNamedCaptures(in_class_escape_state)) { |
| 2459 |
ReportError(RegExpError::kInvalidEscape); |
| 2460 |
return 0; |
| 2461 |
} |
| 2462 |
return c; |
| 2463 |
} |
| 2464 |
|
| 2465 |
// https://tc39.es/ecma262/#prod-ClassRanges |
| 2466 |
template <class CharT> |
| 2467 |
RegExpTree* RegExpParserImpl<CharT>::ParseClassRanges( |
| 2468 |
ZoneList<CharacterRange>* ranges, bool add_unicode_case_equivalents) { |
| 2469 |
base::uc32 char_1, char_2; |
| 2470 |
bool is_class_1, is_class_2; |
| 2471 |
while (has_more() && current() != ']') { |
| 2472 |
ParseClassEscape(ranges, zone(), add_unicode_case_equivalents, &char_1, |
| 2473 |
&is_class_1 CHECK_FAILED); |
| 2474 |
// ClassAtom |
| 2475 |
if (current() == '-') { |
| 2476 |
Advance(); |
| 2477 |
if (!has_more()) { |
| 2478 |
// If we reach the end we break out of the loop and let the |
| 2479 |
// following code report an error. |
| 2480 |
break; |
| 2481 |
} else if (current() == ']') { |
| 2482 |
if (!is_class_1) ranges->Add(CharacterRange::Singleton(char_1), zone()); |
| 2483 |
ranges->Add(CharacterRange::Singleton('-'), zone()); |
| 2484 |
break; |
| 2485 |
} |
| 2486 |
ParseClassEscape(ranges, zone(), add_unicode_case_equivalents, &char_2, |
| 2487 |
&is_class_2 CHECK_FAILED); |
| 2488 |
if (is_class_1 || is_class_2) { |
| 2489 |
// Either end is an escaped character class. Treat the '-' verbatim. |
| 2490 |
if (IsUnicodeMode()) { |
| 2491 |
// ES2015 21.2.2.15.1 step 1. |
| 2492 |
return ReportError(RegExpError::kInvalidCharacterClass); |
| 2493 |
} |
| 2494 |
if (!is_class_1) ranges->Add(CharacterRange::Singleton(char_1), zone()); |
| 2495 |
ranges->Add(CharacterRange::Singleton('-'), zone()); |
| 2496 |
if (!is_class_2) ranges->Add(CharacterRange::Singleton(char_2), zone()); |
| 2497 |
continue; |
| 2498 |
} |
| 2499 |
// ES2015 21.2.2.15.1 step 6. |
| 2500 |
if (char_1 > char_2) { |
| 2501 |
return ReportError(RegExpError::kOutOfOrderCharacterClass); |
| 2502 |
} |
| 2503 |
ranges->Add(CharacterRange::Range(char_1, char_2), zone()); |
| 2504 |
} else { |
| 2505 |
if (!is_class_1) ranges->Add(CharacterRange::Singleton(char_1), zone()); |
| 2506 |
} |
| 2507 |
} |
| 2508 |
return nullptr; |
| 2509 |
} |
| 2510 |
|
| 2511 |
// https://tc39.es/ecma262/#prod-ClassEscape |
| 2512 |
template <class CharT> |
| 2513 |
void RegExpParserImpl<CharT>::ParseClassEscape( |
| 2514 |
ZoneList<CharacterRange>* ranges, Zone* zone, |
| 2515 |
bool add_unicode_case_equivalents, base::uc32* char_out, |
| 2516 |
bool* is_class_escape) { |
| 2517 |
*is_class_escape = false; |
| 2518 |
|
| 2519 |
if (current() != '\\') { |
| 2520 |
// Not a ClassEscape. |
| 2521 |
*char_out = current(); |
| 2522 |
Advance(); |
| 2523 |
return; |
| 2524 |
} |
| 2525 |
|
| 2526 |
const base::uc32 next = Next(); |
| 2527 |
switch (next) { |
| 2528 |
case 'b': |
| 2529 |
*char_out = '\b'; |
| 2530 |
Advance(2); |
| 2531 |
return; |
| 2532 |
case '-': |
| 2533 |
if (IsUnicodeMode()) { |
| 2534 |
*char_out = next; |
| 2535 |
Advance(2); |
| 2536 |
return; |
| 2537 |
} |
| 2538 |
break; |
| 2539 |
case kEndMarker: |
| 2540 |
ReportError(RegExpError::kEscapeAtEndOfPattern); |
| 2541 |
return; |
| 2542 |
default: |
| 2543 |
break; |
| 2544 |
} |
| 2545 |
|
| 2546 |
static constexpr InClassEscapeState kInClassEscape = |
| 2547 |
InClassEscapeState::kInClass; |
| 2548 |
*is_class_escape = |
| 2549 |
TryParseCharacterClassEscape(next, kInClassEscape, ranges, nullptr, zone, |
| 2550 |
add_unicode_case_equivalents); |
| 2551 |
if (*is_class_escape) return; |
| 2552 |
|
| 2553 |
bool dummy = false; // Unused. |
| 2554 |
*char_out = ParseCharacterEscape(kInClassEscape, &dummy); |
| 2555 |
} |
| 2556 |
|
| 2557 |
// https://tc39.es/ecma262/#prod-CharacterClassEscape |
| 2558 |
template <class CharT> |
| 2559 |
bool RegExpParserImpl<CharT>::TryParseCharacterClassEscape( |
| 2560 |
base::uc32 next, InClassEscapeState in_class_escape_state, |
| 2561 |
ZoneList<CharacterRange>* ranges, CharacterClassStrings* strings, |
| 2562 |
Zone* zone, bool add_unicode_case_equivalents) { |
| 2563 |
DCHECK_EQ(current(), '\\'); |
| 2564 |
DCHECK_EQ(Next(), next); |
| 2565 |
|
| 2566 |
switch (next) { |
| 2567 |
case 'd': |
| 2568 |
case 'D': |
| 2569 |
case 's': |
| 2570 |
case 'S': |
| 2571 |
case 'w': |
| 2572 |
case 'W': |
| 2573 |
CharacterRange::AddClassEscape(static_cast<StandardCharacterSet>(next), |
| 2574 |
ranges, add_unicode_case_equivalents, |
| 2575 |
zone); |
| 2576 |
Advance(2); |
| 2577 |
return true; |
| 2578 |
case 'p': |
| 2579 |
case 'P': { |
| 2580 |
if (!IsUnicodeMode()) return false; |
| 2581 |
bool negate = next == 'P'; |
| 2582 |
Advance(2); |
| 2583 |
ZoneVector<char> name_1(zone); |
| 2584 |
ZoneVector<char> name_2(zone); |
| 2585 |
if (!ParsePropertyClassName(&name_1, &name_2) || |
| 2586 |
!AddPropertyClassRange(ranges, strings, negate, name_1, name_2)) { |
| 2587 |
ReportError(in_class_escape_state == InClassEscapeState::kInClass |
| 2588 |
? RegExpError::kInvalidClassPropertyName |
| 2589 |
: RegExpError::kInvalidPropertyName); |
| 2590 |
} |
| 2591 |
return true; |
| 2592 |
} |
| 2593 |
default: |
| 2594 |
return false; |
| 2595 |
} |
| 2596 |
} |
| 2597 |
|
| 2598 |
namespace { |
| 2599 |
|
| 2600 |
// Add |string| to |ranges| if length of |string| == 1, otherwise add |string| |
| 2601 |
// to |strings|. |
| 2602 |
void AddClassString(ZoneList<base::uc32>* normalized_string, |
| 2603 |
RegExpTree* regexp_string, ZoneList<CharacterRange>* ranges, |
| 2604 |
CharacterClassStrings* strings, Zone* zone) { |
| 2605 |
if (normalized_string->length() == 1) { |
| 2606 |
ranges->Add(CharacterRange::Singleton(normalized_string->at(0)), zone); |
| 2607 |
} else { |
| 2608 |
strings->emplace(normalized_string->ToVector(), regexp_string); |
| 2609 |
} |
| 2610 |
} |
| 2611 |
|
| 2612 |
} // namespace |
| 2613 |
|
| 2614 |
// https://tc39.es/ecma262/#prod-ClassStringDisjunction |
| 2615 |
template <class CharT> |
| 2616 |
RegExpTree* RegExpParserImpl<CharT>::ParseClassStringDisjunction( |
| 2617 |
ZoneList<CharacterRange>* ranges, CharacterClassStrings* strings) { |
| 2618 |
DCHECK(unicode_sets()); |
| 2619 |
DCHECK_EQ(current(), '\\'); |
| 2620 |
DCHECK_EQ(Next(), 'q'); |
| 2621 |
Advance(2); |
| 2622 |
if (current() != '{') { |
| 2623 |
// Identity escape of 'q' is not allowed in unicode mode. |
| 2624 |
return ReportError(RegExpError::kInvalidEscape); |
| 2625 |
} |
| 2626 |
Advance(); |
| 2627 |
|
| 2628 |
ZoneList<base::uc32>* string = |
| 2629 |
zone()->template New<ZoneList<base::uc32>>(4, zone()); |
| 2630 |
RegExpTextBuilder::SmallRegExpTreeVector string_storage(zone()); |
| 2631 |
RegExpTextBuilder string_builder(zone(), &string_storage, flags()); |
| 2632 |
|
| 2633 |
while (has_more() && current() != '}') { |
| 2634 |
if (current() == '|') { |
| 2635 |
AddClassString(string, string_builder.ToRegExp(), ranges, strings, |
| 2636 |
zone()); |
| 2637 |
string = zone()->template New<ZoneList<base::uc32>>(4, zone()); |
| 2638 |
string_storage.clear(); |
| 2639 |
Advance(); |
| 2640 |
} else { |
| 2641 |
base::uc32 c = ParseClassSetCharacter(CHECK_FAILED); |
| 2642 |
if (ignore_case()) { |
| 2643 |
#ifdef V8_INTL_SUPPORT |
| 2644 |
c = u_foldCase(c, U_FOLD_CASE_DEFAULT); |
| 2645 |
#else |
| 2646 |
c = AsciiAlphaToLower(c); |
| 2647 |
#endif |
| 2648 |
} |
| 2649 |
string->Add(c, zone()); |
| 2650 |
string_builder.AddUnicodeCharacter(c); |
| 2651 |
} |
| 2652 |
} |
| 2653 |
|
| 2654 |
AddClassString(string, string_builder.ToRegExp(), ranges, strings, zone()); |
| 2655 |
CharacterRange::Canonicalize(ranges); |
| 2656 |
|
| 2657 |
// We don't need to handle missing closing '}' here. |
| 2658 |
// If the character class is correctly closed, ParseClassSetCharacter will |
| 2659 |
// report an error. |
| 2660 |
Advance(); |
| 2661 |
return nullptr; |
| 2662 |
} |
| 2663 |
|
| 2664 |
// https://tc39.es/ecma262/#prod-ClassSetOperand |
| 2665 |
// Tree returned based on type_out: |
| 2666 |
// * kNestedClass: RegExpClassSetExpression |
| 2667 |
// * For all other types: RegExpClassSetOperand |
| 2668 |
template <class CharT> |
| 2669 |
RegExpTree* RegExpParserImpl<CharT>::ParseClassSetOperand( |
| 2670 |
const RegExpBuilder* builder, ClassSetOperandType* type_out) { |
| 2671 |
ZoneList<CharacterRange>* ranges = |
| 2672 |
zone()->template New<ZoneList<CharacterRange>>(1, zone()); |
| 2673 |
CharacterClassStrings* strings = |
| 2674 |
zone()->template New<CharacterClassStrings>(zone()); |
| 2675 |
base::uc32 character; |
| 2676 |
RegExpTree* tree = ParseClassSetOperand(builder, type_out, ranges, strings, |
| 2677 |
&character CHECK_FAILED); |
| 2678 |
DCHECK_IMPLIES(*type_out != ClassSetOperandType::kNestedClass, |
| 2679 |
tree == nullptr); |
| 2680 |
DCHECK_IMPLIES(*type_out == ClassSetOperandType::kClassSetCharacter, |
| 2681 |
ranges->is_empty()); |
| 2682 |
DCHECK_IMPLIES(*type_out == ClassSetOperandType::kClassSetCharacter, |
| 2683 |
strings->empty()); |
| 2684 |
DCHECK_IMPLIES(*type_out == ClassSetOperandType::kNestedClass, |
| 2685 |
ranges->is_empty()); |
| 2686 |
DCHECK_IMPLIES(*type_out == ClassSetOperandType::kNestedClass, |
| 2687 |
strings->empty()); |
| 2688 |
DCHECK_IMPLIES(*type_out == ClassSetOperandType::kNestedClass, |
| 2689 |
tree->IsClassSetExpression()); |
| 2690 |
// ClassSetRange is only used within ClassSetUnion(). |
| 2691 |
DCHECK_NE(*type_out, ClassSetOperandType::kClassSetRange); |
| 2692 |
// There are no restrictions for kCharacterClassEscape. |
| 2693 |
// CharacterClassEscape includes \p{}, which can contain ranges, strings or |
| 2694 |
// both and \P{}, which could contain nothing (i.e. \P{Any}). |
| 2695 |
if (tree == nullptr) { |
| 2696 |
if (*type_out == ClassSetOperandType::kClassSetCharacter) { |
| 2697 |
AddMaybeSimpleCaseFoldedRange(ranges, |
| 2698 |
CharacterRange::Singleton(character)); |
| 2699 |
} |
| 2700 |
tree = zone()->template New<RegExpClassSetOperand>(ranges, strings); |
| 2701 |
} |
| 2702 |
return tree; |
| 2703 |
} |
| 2704 |
|
| 2705 |
// https://tc39.es/ecma262/#prod-ClassSetOperand |
| 2706 |
// Based on |type_out| either a tree is returned or |
| 2707 |
// |ranges|/|strings|/|character| modified. If a tree is returned, |
| 2708 |
// ranges/strings are not modified. If |type_out| is kNestedClass, a tree of |
| 2709 |
// type RegExpClassSetExpression is returned. If | type_out| is |
| 2710 |
// kClassSetCharacter, |character| is set and nullptr returned. For all other |
| 2711 |
// types, |ranges|/|strings|/|character| is modified and nullptr is returned. |
| 2712 |
template <class CharT> |
| 2713 |
RegExpTree* RegExpParserImpl<CharT>::ParseClassSetOperand( |
| 2714 |
const RegExpBuilder* builder, ClassSetOperandType* type_out, |
| 2715 |
ZoneList<CharacterRange>* ranges, CharacterClassStrings* strings, |
| 2716 |
base::uc32* character) { |
| 2717 |
DCHECK(unicode_sets()); |
| 2718 |
base::uc32 c = current(); |
| 2719 |
if (c == '\\') { |
| 2720 |
const base::uc32 next = Next(); |
| 2721 |
if (next == 'q') { |
| 2722 |
*type_out = ClassSetOperandType::kClassStringDisjunction; |
| 2723 |
ParseClassStringDisjunction(ranges, strings CHECK_FAILED); |
| 2724 |
return nullptr; |
| 2725 |
} |
| 2726 |
static constexpr InClassEscapeState kInClassEscape = |
| 2727 |
InClassEscapeState::kInClass; |
| 2728 |
const bool add_unicode_case_equivalents = ignore_case(); |
| 2729 |
if (TryParseCharacterClassEscape(next, kInClassEscape, ranges, strings, |
| 2730 |
zone(), add_unicode_case_equivalents)) { |
| 2731 |
*type_out = ClassSetOperandType::kCharacterClassEscape; |
| 2732 |
return nullptr; |
| 2733 |
} |
| 2734 |
} |
| 2735 |
|
| 2736 |
if (c == '[') { |
| 2737 |
*type_out = ClassSetOperandType::kNestedClass; |
| 2738 |
return ParseCharacterClass(builder); |
| 2739 |
} |
| 2740 |
|
| 2741 |
*type_out = ClassSetOperandType::kClassSetCharacter; |
| 2742 |
c = ParseClassSetCharacter(CHECK_FAILED); |
| 2743 |
*character = c; |
| 2744 |
return nullptr; |
| 2745 |
} |
| 2746 |
|
| 2747 |
template <class CharT> |
| 2748 |
base::uc32 RegExpParserImpl<CharT>::ParseClassSetCharacter() { |
| 2749 |
DCHECK(unicode_sets()); |
| 2750 |
const base::uc32 c = current(); |
| 2751 |
if (c == '\\') { |
| 2752 |
const base::uc32 next = Next(); |
| 2753 |
switch (next) { |
| 2754 |
case 'b': |
| 2755 |
Advance(2); |
| 2756 |
return '\b'; |
| 2757 |
case kEndMarker: |
| 2758 |
ReportError(RegExpError::kEscapeAtEndOfPattern); |
| 2759 |
return 0; |
| 2760 |
} |
| 2761 |
static constexpr InClassEscapeState kInClassEscape = |
| 2762 |
InClassEscapeState::kInClass; |
| 2763 |
|
| 2764 |
bool dummy = false; // Unused. |
| 2765 |
return ParseCharacterEscape(kInClassEscape, &dummy); |
| 2766 |
} |
| 2767 |
if (IsClassSetSyntaxCharacter(c)) { |
| 2768 |
ReportError(RegExpError::kInvalidCharacterInClass); |
| 2769 |
return 0; |
| 2770 |
} |
| 2771 |
if (IsClassSetReservedDoublePunctuator(c)) { |
| 2772 |
ReportError(RegExpError::kInvalidClassSetOperation); |
| 2773 |
return 0; |
| 2774 |
} |
| 2775 |
Advance(); |
| 2776 |
return c; |
| 2777 |
} |
| 2778 |
|
| 2779 |
namespace { |
| 2780 |
|
| 2781 |
bool MayContainStrings(ClassSetOperandType type, RegExpTree* operand) { |
| 2782 |
switch (type) { |
| 2783 |
case ClassSetOperandType::kClassSetCharacter: |
| 2784 |
case ClassSetOperandType::kClassSetRange: |
| 2785 |
return false; |
| 2786 |
case ClassSetOperandType::kCharacterClassEscape: |
| 2787 |
case ClassSetOperandType::kClassStringDisjunction: |
| 2788 |
return operand->AsClassSetOperand()->has_strings(); |
| 2789 |
case ClassSetOperandType::kNestedClass: |
| 2790 |
if (operand->IsClassRanges()) return false; |
| 2791 |
return operand->AsClassSetExpression()->may_contain_strings(); |
| 2792 |
} |
| 2793 |
} |
| 2794 |
|
| 2795 |
} // namespace |
| 2796 |
|
| 2797 |
template <class CharT> |
| 2798 |
void RegExpParserImpl<CharT>::AddMaybeSimpleCaseFoldedRange( |
| 2799 |
ZoneList<CharacterRange>* ranges, CharacterRange new_range) { |
| 2800 |
DCHECK(unicode_sets()); |
| 2801 |
if (ignore_case()) { |
| 2802 |
ZoneList<CharacterRange>* new_ranges = |
| 2803 |
zone()->template New<ZoneList<CharacterRange>>(2, zone()); |
| 2804 |
new_ranges->Add(new_range, zone()); |
| 2805 |
CharacterRange::AddUnicodeCaseEquivalents(new_ranges, zone()); |
| 2806 |
ranges->AddAll(*new_ranges, zone()); |
| 2807 |
} else { |
| 2808 |
ranges->Add(new_range, zone()); |
| 2809 |
} |
| 2810 |
CharacterRange::Canonicalize(ranges); |
| 2811 |
} |
| 2812 |
|
| 2813 |
// https://tc39.es/ecma262/#prod-ClassUnion |
| 2814 |
template <class CharT> |
| 2815 |
RegExpTree* RegExpParserImpl<CharT>::ParseClassUnion( |
| 2816 |
const RegExpBuilder* builder, bool is_negated, RegExpTree* first_operand, |
| 2817 |
ClassSetOperandType first_operand_type, ZoneList<CharacterRange>* ranges, |
| 2818 |
CharacterClassStrings* strings, base::uc32 character) { |
| 2819 |
DCHECK(unicode_sets()); |
| 2820 |
ZoneList<RegExpTree*>* operands = |
| 2821 |
zone()->template New<ZoneList<RegExpTree*>>(2, zone()); |
| 2822 |
bool may_contain_strings = false; |
| 2823 |
// Add the lhs to operands if necessary. |
| 2824 |
// Either the lhs values were added to |ranges|/|strings| (in which case |
| 2825 |
// |first_operand| is nullptr), or the lhs was evaluated to a tree and passed |
| 2826 |
// as |first_operand| (in which case |ranges| and |strings| are empty). |
| 2827 |
if (first_operand != nullptr) { |
| 2828 |
may_contain_strings = MayContainStrings(first_operand_type, first_operand); |
| 2829 |
operands->Add(first_operand, zone()); |
| 2830 |
} |
| 2831 |
ClassSetOperandType last_type = first_operand_type; |
| 2832 |
while (has_more() && current() != ']') { |
| 2833 |
if (current() == '-') { |
| 2834 |
// Mix of ClassSetRange and ClassSubtraction is not allowed. |
| 2835 |
if (Next() == '-') { |
| 2836 |
return ReportError(RegExpError::kInvalidClassSetOperation); |
| 2837 |
} |
| 2838 |
Advance(); |
| 2839 |
if (!has_more()) { |
| 2840 |
// If we reach the end we break out of the loop and let the |
| 2841 |
// following code report an error. |
| 2842 |
break; |
| 2843 |
} |
| 2844 |
// If the lhs and rhs around '-' are both ClassSetCharacters, they |
| 2845 |
// represent a character range. |
| 2846 |
// In case one of them is not a ClassSetCharacter, it is a syntax error, |
| 2847 |
// as '-' can not be used unescaped within a class with /v. |
| 2848 |
// See |
| 2849 |
// https://tc39.es/ecma262/#prod-ClassSetRange |
| 2850 |
if (last_type != ClassSetOperandType::kClassSetCharacter) { |
| 2851 |
return ReportError(RegExpError::kInvalidCharacterClass); |
| 2852 |
} |
| 2853 |
base::uc32 from = character; |
| 2854 |
ParseClassSetOperand(builder, &last_type, ranges, strings, |
| 2855 |
&character CHECK_FAILED); |
| 2856 |
if (last_type != ClassSetOperandType::kClassSetCharacter) { |
| 2857 |
return ReportError(RegExpError::kInvalidCharacterClass); |
| 2858 |
} |
| 2859 |
if (from > character) { |
| 2860 |
return ReportError(RegExpError::kOutOfOrderCharacterClass); |
| 2861 |
} |
| 2862 |
AddMaybeSimpleCaseFoldedRange(ranges, |
| 2863 |
CharacterRange::Range(from, character)); |
| 2864 |
last_type = ClassSetOperandType::kClassSetRange; |
| 2865 |
} else { |
| 2866 |
DCHECK_NE(current(), '-'); |
| 2867 |
if (last_type == ClassSetOperandType::kClassSetCharacter) { |
| 2868 |
AddMaybeSimpleCaseFoldedRange(ranges, |
| 2869 |
CharacterRange::Singleton(character)); |
| 2870 |
} |
| 2871 |
RegExpTree* operand = ParseClassSetOperand( |
| 2872 |
builder, &last_type, ranges, strings, &character CHECK_FAILED); |
| 2873 |
if (operand != nullptr) { |
| 2874 |
may_contain_strings |= MayContainStrings(last_type, operand); |
| 2875 |
// Add the range we started building as operand and reset the current |
| 2876 |
// range. |
| 2877 |
if (!ranges->is_empty() || !strings->empty()) { |
| 2878 |
may_contain_strings |= !strings->empty(); |
| 2879 |
operands->Add( |
| 2880 |
zone()->template New<RegExpClassSetOperand>(ranges, strings), |
| 2881 |
zone()); |
| 2882 |
ranges = zone()->template New<ZoneList<CharacterRange>>(2, zone()); |
| 2883 |
strings = zone()->template New<CharacterClassStrings>(zone()); |
| 2884 |
} |
| 2885 |
operands->Add(operand, zone()); |
| 2886 |
} |
| 2887 |
} |
| 2888 |
} |
| 2889 |
|
| 2890 |
if (!has_more()) { |
| 2891 |
return ReportError(RegExpError::kUnterminatedCharacterClass); |
| 2892 |
} |
| 2893 |
|
| 2894 |
if (last_type == ClassSetOperandType::kClassSetCharacter) { |
| 2895 |
AddMaybeSimpleCaseFoldedRange(ranges, CharacterRange::Singleton(character)); |
| 2896 |
} |
| 2897 |
|
| 2898 |
// Add the range we started building as operand. |
| 2899 |
if (!ranges->is_empty() || !strings->empty()) { |
| 2900 |
may_contain_strings |= !strings->empty(); |
| 2901 |
operands->Add(zone()->template New<RegExpClassSetOperand>(ranges, strings), |
| 2902 |
zone()); |
| 2903 |
} |
| 2904 |
|
| 2905 |
DCHECK_EQ(current(), ']'); |
| 2906 |
Advance(); |
| 2907 |
|
| 2908 |
if (is_negated && may_contain_strings) { |
| 2909 |
return ReportError(RegExpError::kNegatedCharacterClassWithStrings); |
| 2910 |
} |
| 2911 |
|
| 2912 |
if (operands->is_empty()) { |
| 2913 |
// Return empty expression if no operands were added (e.g. [\P{Any}] |
| 2914 |
// produces an empty range). |
| 2915 |
DCHECK(ranges->is_empty()); |
| 2916 |
DCHECK(strings->empty()); |
| 2917 |
return RegExpClassSetExpression::Empty(zone(), is_negated); |
| 2918 |
} |
| 2919 |
|
| 2920 |
return zone()->template New<RegExpClassSetExpression>( |
| 2921 |
RegExpClassSetExpression::OperationType::kUnion, is_negated, |
| 2922 |
may_contain_strings, operands); |
| 2923 |
} |
| 2924 |
|
| 2925 |
// https://tc39.es/ecma262/#prod-ClassIntersection |
| 2926 |
template <class CharT> |
| 2927 |
RegExpTree* RegExpParserImpl<CharT>::ParseClassIntersection( |
| 2928 |
const RegExpBuilder* builder, bool is_negated, RegExpTree* first_operand, |
| 2929 |
ClassSetOperandType first_operand_type) { |
| 2930 |
DCHECK(unicode_sets()); |
| 2931 |
DCHECK(current() == '&' && Next() == '&'); |
| 2932 |
bool may_contain_strings = |
| 2933 |
MayContainStrings(first_operand_type, first_operand); |
| 2934 |
ZoneList<RegExpTree*>* operands = |
| 2935 |
zone()->template New<ZoneList<RegExpTree*>>(2, zone()); |
| 2936 |
operands->Add(first_operand, zone()); |
| 2937 |
while (has_more() && current() != ']') { |
| 2938 |
if (current() != '&' || Next() != '&') { |
| 2939 |
return ReportError(RegExpError::kInvalidClassSetOperation); |
| 2940 |
} |
| 2941 |
Advance(2); |
| 2942 |
// [lookahead ≠ &] |
| 2943 |
if (current() == '&') { |
| 2944 |
return ReportError(RegExpError::kInvalidCharacterInClass); |
| 2945 |
} |
| 2946 |
|
| 2947 |
ClassSetOperandType operand_type; |
| 2948 |
RegExpTree* operand = |
| 2949 |
ParseClassSetOperand(builder, &operand_type CHECK_FAILED); |
| 2950 |
may_contain_strings &= MayContainStrings(operand_type, operand); |
| 2951 |
operands->Add(operand, zone()); |
| 2952 |
} |
| 2953 |
if (!has_more()) { |
| 2954 |
return ReportError(RegExpError::kUnterminatedCharacterClass); |
| 2955 |
} |
| 2956 |
if (is_negated && may_contain_strings) { |
| 2957 |
return ReportError(RegExpError::kNegatedCharacterClassWithStrings); |
| 2958 |
} |
| 2959 |
DCHECK_EQ(current(), ']'); |
| 2960 |
Advance(); |
| 2961 |
return zone()->template New<RegExpClassSetExpression>( |
| 2962 |
RegExpClassSetExpression::OperationType::kIntersection, is_negated, |
| 2963 |
may_contain_strings, operands); |
| 2964 |
} |
| 2965 |
|
| 2966 |
// https://tc39.es/ecma262/#prod-ClassSubtraction |
| 2967 |
template <class CharT> |
| 2968 |
RegExpTree* RegExpParserImpl<CharT>::ParseClassSubtraction( |
| 2969 |
const RegExpBuilder* builder, bool is_negated, RegExpTree* first_operand, |
| 2970 |
ClassSetOperandType first_operand_type) { |
| 2971 |
DCHECK(unicode_sets()); |
| 2972 |
DCHECK(current() == '-' && Next() == '-'); |
| 2973 |
const bool may_contain_strings = |
| 2974 |
MayContainStrings(first_operand_type, first_operand); |
| 2975 |
if (is_negated && may_contain_strings) { |
| 2976 |
return ReportError(RegExpError::kNegatedCharacterClassWithStrings); |
| 2977 |
} |
| 2978 |
ZoneList<RegExpTree*>* operands = |
| 2979 |
zone()->template New<ZoneList<RegExpTree*>>(2, zone()); |
| 2980 |
operands->Add(first_operand, zone()); |
| 2981 |
while (has_more() && current() != ']') { |
| 2982 |
if (current() != '-' || Next() != '-') { |
| 2983 |
return ReportError(RegExpError::kInvalidClassSetOperation); |
| 2984 |
} |
| 2985 |
Advance(2); |
| 2986 |
ClassSetOperandType dummy; // unused |
| 2987 |
RegExpTree* operand = ParseClassSetOperand(builder, &dummy CHECK_FAILED); |
| 2988 |
operands->Add(operand, zone()); |
| 2989 |
} |
| 2990 |
if (!has_more()) { |
| 2991 |
return ReportError(RegExpError::kUnterminatedCharacterClass); |
| 2992 |
} |
| 2993 |
DCHECK_EQ(current(), ']'); |
| 2994 |
Advance(); |
| 2995 |
return zone()->template New<RegExpClassSetExpression>( |
| 2996 |
RegExpClassSetExpression::OperationType::kSubtraction, is_negated, |
| 2997 |
may_contain_strings, operands); |
| 2998 |
} |
| 2999 |
|
| 3000 |
// https://tc39.es/ecma262/#prod-CharacterClass |
| 3001 |
template <class CharT> |
| 3002 |
RegExpTree* RegExpParserImpl<CharT>::ParseCharacterClass( |
| 3003 |
const RegExpBuilder* builder) { |
| 3004 |
DCHECK_EQ(current(), '['); |
| 3005 |
Advance(); |
| 3006 |
bool is_negated = false; |
| 3007 |
if (current() == '^') { |
| 3008 |
is_negated = true; |
| 3009 |
Advance(); |
| 3010 |
} |
| 3011 |
ZoneList<CharacterRange>* ranges = |
| 3012 |
zone()->template New<ZoneList<CharacterRange>>(2, zone()); |
| 3013 |
if (current() == ']') { |
| 3014 |
Advance(); |
| 3015 |
if (unicode_sets()) { |
| 3016 |
return RegExpClassSetExpression::Empty(zone(), is_negated); |
| 3017 |
} else { |
| 3018 |
RegExpClassRanges::ClassRangesFlags class_ranges_flags; |
| 3019 |
if (is_negated) class_ranges_flags = RegExpClassRanges::NEGATED; |
| 3020 |
return zone()->template New<RegExpClassRanges>(zone(), ranges, |
| 3021 |
class_ranges_flags); |
| 3022 |
} |
| 3023 |
} |
| 3024 |
|
| 3025 |
if (!unicode_sets()) { |
| 3026 |
bool add_unicode_case_equivalents = IsUnicodeMode() && ignore_case(); |
| 3027 |
ParseClassRanges(ranges, add_unicode_case_equivalents CHECK_FAILED); |
| 3028 |
if (!has_more()) { |
| 3029 |
return ReportError(RegExpError::kUnterminatedCharacterClass); |
| 3030 |
} |
| 3031 |
DCHECK_EQ(current(), ']'); |
| 3032 |
Advance(); |
| 3033 |
RegExpClassRanges::ClassRangesFlags character_class_flags; |
| 3034 |
if (is_negated) character_class_flags = RegExpClassRanges::NEGATED; |
| 3035 |
return zone()->template New<RegExpClassRanges>(zone(), ranges, |
| 3036 |
character_class_flags); |
| 3037 |
} else { |
| 3038 |
ClassSetOperandType operand_type; |
| 3039 |
CharacterClassStrings* strings = |
| 3040 |
zone()->template New<CharacterClassStrings>(zone()); |
| 3041 |
base::uc32 character; |
| 3042 |
RegExpTree* operand = ParseClassSetOperand( |
| 3043 |
builder, &operand_type, ranges, strings, &character CHECK_FAILED); |
| 3044 |
switch (current()) { |
| 3045 |
case '-': |
| 3046 |
if (Next() == '-') { |
| 3047 |
if (operand == nullptr) { |
| 3048 |
if (operand_type == ClassSetOperandType::kClassSetCharacter) { |
| 3049 |
AddMaybeSimpleCaseFoldedRange( |
| 3050 |
ranges, CharacterRange::Singleton(character)); |
| 3051 |
} |
| 3052 |
operand = |
| 3053 |
zone()->template New<RegExpClassSetOperand>(ranges, strings); |
| 3054 |
} |
| 3055 |
return ParseClassSubtraction(builder, is_negated, operand, |
| 3056 |
operand_type); |
| 3057 |
} |
| 3058 |
// ClassSetRange is handled in ParseClassUnion(). |
| 3059 |
break; |
| 3060 |
case '&': |
| 3061 |
if (Next() == '&') { |
| 3062 |
if (operand == nullptr) { |
| 3063 |
if (operand_type == ClassSetOperandType::kClassSetCharacter) { |
| 3064 |
AddMaybeSimpleCaseFoldedRange( |
| 3065 |
ranges, CharacterRange::Singleton(character)); |
| 3066 |
} |
| 3067 |
operand = |
| 3068 |
zone()->template New<RegExpClassSetOperand>(ranges, strings); |
| 3069 |
} |
| 3070 |
return ParseClassIntersection(builder, is_negated, operand, |
| 3071 |
operand_type); |
| 3072 |
} |
| 3073 |
} |
| 3074 |
return ParseClassUnion(builder, is_negated, operand, operand_type, ranges, |
| 3075 |
strings, character); |
| 3076 |
} |
| 3077 |
} |
| 3078 |
|
| 3079 |
#undef CHECK_FAILED |
| 3080 |
|
| 3081 |
template <class CharT> |
| 3082 |
bool RegExpParserImpl<CharT>::Parse(RegExpCompileData* result) { |
| 3083 |
DCHECK_NOT_NULL(result); |
| 3084 |
RegExpTree* tree = ParsePattern(); |
| 3085 |
|
| 3086 |
if (failed()) { |
| 3087 |
DCHECK_NULL(tree); |
| 3088 |
DCHECK_NE(error_, RegExpError::kNone); |
| 3089 |
result->error = error_; |
| 3090 |
result->error_pos = error_pos_; |
| 3091 |
return false; |
| 3092 |
} |
| 3093 |
|
| 3094 |
DCHECK_NOT_NULL(tree); |
| 3095 |
DCHECK_EQ(error_, RegExpError::kNone); |
| 3096 |
if (v8_flags.trace_regexp_parser) { |
| 3097 |
StdoutStream os; |
| 3098 |
tree->Print(os, zone()); |
| 3099 |
os << "\n"; |
| 3100 |
} |
| 3101 |
|
| 3102 |
result->tree = tree; |
| 3103 |
const int capture_count = captures_started(); |
| 3104 |
result->simple = tree->IsAtom() && simple() && capture_count == 0; |
| 3105 |
result->contains_anchor = contains_anchor(); |
| 3106 |
result->capture_count = capture_count; |
| 3107 |
result->named_captures = GetNamedCaptures(); |
| 3108 |
return true; |
| 3109 |
} |
| 3110 |
|
| 3111 |
void RegExpBuilder::FlushText() { text_builder().FlushText(); } |
| 3112 |
|
| 3113 |
void RegExpBuilder::AddCharacter(base::uc16 c) { |
| 3114 |
pending_empty_ = false; |
| 3115 |
text_builder().AddCharacter(c); |
| 3116 |
} |
| 3117 |
|
| 3118 |
void RegExpBuilder::AddUnicodeCharacter(base::uc32 c) { |
| 3119 |
pending_empty_ = false; |
| 3120 |
text_builder().AddUnicodeCharacter(c); |
| 3121 |
} |
| 3122 |
|
| 3123 |
void RegExpBuilder::AddEscapedUnicodeCharacter(base::uc32 character) { |
| 3124 |
pending_empty_ = false; |
| 3125 |
text_builder().AddEscapedUnicodeCharacter(character); |
| 3126 |
} |
| 3127 |
|
| 3128 |
void RegExpBuilder::AddEmpty() { |
| 3129 |
text_builder().FlushPendingSurrogate(); |
| 3130 |
pending_empty_ = true; |
| 3131 |
} |
| 3132 |
|
| 3133 |
void RegExpBuilder::AddClassRanges(RegExpClassRanges* cc) { |
| 3134 |
pending_empty_ = false; |
| 3135 |
text_builder().AddClassRanges(cc); |
| 3136 |
} |
| 3137 |
|
| 3138 |
void RegExpBuilder::AddAtom(RegExpTree* term) { |
| 3139 |
if (term->IsEmpty()) { |
| 3140 |
AddEmpty(); |
| 3141 |
return; |
| 3142 |
} |
| 3143 |
pending_empty_ = false; |
| 3144 |
if (term->IsTextElement()) { |
| 3145 |
text_builder().AddAtom(term); |
| 3146 |
} else { |
| 3147 |
FlushText(); |
| 3148 |
terms_.emplace_back(term); |
| 3149 |
} |
| 3150 |
} |
| 3151 |
|
| 3152 |
void RegExpBuilder::AddTerm(RegExpTree* term) { |
| 3153 |
DCHECK(!term->IsEmpty()); |
| 3154 |
pending_empty_ = false; |
| 3155 |
if (term->IsTextElement()) { |
| 3156 |
text_builder().AddTerm(term); |
| 3157 |
} else { |
| 3158 |
FlushText(); |
| 3159 |
terms_.emplace_back(term); |
| 3160 |
} |
| 3161 |
} |
| 3162 |
|
| 3163 |
void RegExpBuilder::AddAssertion(RegExpTree* assert) { |
| 3164 |
FlushText(); |
| 3165 |
pending_empty_ = false; |
| 3166 |
terms_.emplace_back(assert); |
| 3167 |
} |
| 3168 |
|
| 3169 |
void RegExpBuilder::NewAlternative() { FlushTerms(); } |
| 3170 |
|
| 3171 |
void RegExpBuilder::FlushTerms() { |
| 3172 |
FlushText(); |
| 3173 |
size_t num_terms = terms_.size(); |
| 3174 |
RegExpTree* alternative; |
| 3175 |
if (num_terms == 0) { |
| 3176 |
alternative = zone()->New<RegExpEmpty>(); |
| 3177 |
} else if (num_terms == 1) { |
| 3178 |
alternative = terms_.back(); |
| 3179 |
} else { |
| 3180 |
alternative = |
| 3181 |
zone()->New<RegExpAlternative>(zone()->New<ZoneList<RegExpTree*>>( |
| 3182 |
base::VectorOf(terms_.begin(), terms_.size()), zone())); |
| 3183 |
} |
| 3184 |
alternatives_.emplace_back(alternative); |
| 3185 |
terms_.clear(); |
| 3186 |
} |
| 3187 |
|
| 3188 |
RegExpTree* RegExpBuilder::ToRegExp() { |
| 3189 |
FlushTerms(); |
| 3190 |
size_t num_alternatives = alternatives_.size(); |
| 3191 |
if (num_alternatives == 0) return zone()->New<RegExpEmpty>(); |
| 3192 |
if (num_alternatives == 1) return alternatives_.back(); |
| 3193 |
return zone()->New<RegExpDisjunction>(zone()->New<ZoneList<RegExpTree*>>( |
| 3194 |
base::VectorOf(alternatives_.begin(), alternatives_.size()), zone())); |
| 3195 |
} |
| 3196 |
|
| 3197 |
bool RegExpBuilder::AddQuantifierToAtom( |
| 3198 |
int min, int max, int index, |
| 3199 |
RegExpQuantifier::QuantifierType quantifier_type) { |
| 3200 |
if (pending_empty_) { |
| 3201 |
pending_empty_ = false; |
| 3202 |
return true; |
| 3203 |
} |
| 3204 |
RegExpTree* atom = text_builder().PopLastAtom(); |
| 3205 |
if (atom != nullptr) { |
| 3206 |
FlushText(); |
| 3207 |
} else if (!terms_.empty()) { |
| 3208 |
atom = terms_.back(); |
| 3209 |
terms_.pop_back(); |
| 3210 |
if (atom->IsLookaround()) { |
| 3211 |
// With /u or /v, lookarounds are not quantifiable. |
| 3212 |
if (IsUnicodeMode()) return false; |
| 3213 |
// Lookbehinds are not quantifiable. |
| 3214 |
if (atom->AsLookaround()->type() == RegExpLookaround::LOOKBEHIND) { |
| 3215 |
return false; |
| 3216 |
} |
| 3217 |
} |
| 3218 |
if (atom->max_match() == 0) { |
| 3219 |
// Guaranteed to only match an empty string. |
| 3220 |
if (min == 0) { |
| 3221 |
return true; |
| 3222 |
} |
| 3223 |
terms_.emplace_back(atom); |
| 3224 |
return true; |
| 3225 |
} |
| 3226 |
} else { |
| 3227 |
// Only call immediately after adding an atom or character! |
| 3228 |
UNREACHABLE(); |
| 3229 |
} |
| 3230 |
terms_.emplace_back( |
| 3231 |
zone()->New<RegExpQuantifier>(min, max, quantifier_type, index, atom)); |
| 3232 |
return true; |
| 3233 |
} |
| 3234 |
|
| 3235 |
template class RegExpParserImpl<uint8_t>; |
| 3236 |
template class RegExpParserImpl<base::uc16>; |
| 3237 |
|
| 3238 |
} // namespace |
| 3239 |
|
| 3240 |
// static |
| 3241 |
bool RegExpParser::ParseRegExpFromHeapString(Isolate* isolate, Zone* zone, |
| 3242 |
DirectHandle<String> input, |
| 3243 |
RegExpFlags flags, |
| 3244 |
RegExpCompileData* result) { |
| 3245 |
DisallowGarbageCollection no_gc; |
| 3246 |
uintptr_t stack_limit = isolate->stack_guard()->real_climit(); |
| 3247 |
String::FlatContent content = input->GetFlatContent(no_gc); |
| 3248 |
if (content.IsOneByte()) { |
| 3249 |
base::Vector<const uint8_t> v = content.ToOneByteVector(); |
| 3250 |
return RegExpParserImpl<uint8_t>{v.begin(), v.length(), flags, |
| 3251 |
stack_limit, zone, no_gc} |
| 3252 |
.Parse(result); |
| 3253 |
} else { |
| 3254 |
base::Vector<const base::uc16> v = content.ToUC16Vector(); |
| 3255 |
return RegExpParserImpl<base::uc16>{v.begin(), v.length(), flags, |
| 3256 |
stack_limit, zone, no_gc} |
| 3257 |
.Parse(result); |
| 3258 |
} |
| 3259 |
} |
| 3260 |
|
| 3261 |
// static |
| 3262 |
template <class CharT> |
| 3263 |
bool RegExpParser::VerifyRegExpSyntax(Zone* zone, uintptr_t stack_limit, |
| 3264 |
const CharT* input, int input_length, |
| 3265 |
RegExpFlags flags, |
| 3266 |
RegExpCompileData* result, |
| 3267 |
const DisallowGarbageCollection& no_gc) { |
| 3268 |
return RegExpParserImpl<CharT>{input, input_length, flags, |
| 3269 |
stack_limit, zone, no_gc} |
| 3270 |
.Parse(result); |
| 3271 |
} |
| 3272 |
|
| 3273 |
template bool RegExpParser::VerifyRegExpSyntax<uint8_t>( |
| 3274 |
Zone*, uintptr_t, const uint8_t*, int, RegExpFlags, RegExpCompileData*, |
| 3275 |
const DisallowGarbageCollection&); |
| 3276 |
template bool RegExpParser::VerifyRegExpSyntax<base::uc16>( |
| 3277 |
Zone*, uintptr_t, const base::uc16*, int, RegExpFlags, RegExpCompileData*, |
| 3278 |
const DisallowGarbageCollection&); |
| 3279 |
|
| 3280 |
} // namespace internal |
| 3281 |
} // namespace v8 |