| 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 |
#ifndef V8_REGEXP_REGEXP_AST_H_ |
| 6 |
#define V8_REGEXP_REGEXP_AST_H_ |
| 7 |
|
| 8 |
#include <optional> |
| 9 |
|
| 10 |
#include "irregexp/RegExpShim.h" |
| 11 |
|
| 12 |
#ifdef V8_INTL_SUPPORT |
| 13 |
#include "unicode/uniset.h" |
| 14 |
#endif // V8_INTL_SUPPORT |
| 15 |
|
| 16 |
namespace v8::internal { |
| 17 |
|
| 18 |
#define FOR_EACH_REG_EXP_TREE_TYPE(VISIT) \ |
| 19 |
VISIT(Disjunction) \ |
| 20 |
VISIT(Alternative) \ |
| 21 |
VISIT(Assertion) \ |
| 22 |
VISIT(ClassRanges) \ |
| 23 |
VISIT(ClassSetOperand) \ |
| 24 |
VISIT(ClassSetExpression) \ |
| 25 |
VISIT(Atom) \ |
| 26 |
VISIT(Quantifier) \ |
| 27 |
VISIT(Capture) \ |
| 28 |
VISIT(Group) \ |
| 29 |
VISIT(Lookaround) \ |
| 30 |
VISIT(BackReference) \ |
| 31 |
VISIT(Empty) \ |
| 32 |
VISIT(Text) |
| 33 |
|
| 34 |
#define FORWARD_DECLARE(Name) class RegExp##Name; |
| 35 |
FOR_EACH_REG_EXP_TREE_TYPE(FORWARD_DECLARE) |
| 36 |
#undef FORWARD_DECLARE |
| 37 |
|
| 38 |
class RegExpCompiler; |
| 39 |
class RegExpNode; |
| 40 |
class RegExpTree; |
| 41 |
|
| 42 |
class RegExpVisitor { |
| 43 |
public: |
| 44 |
virtual ~RegExpVisitor() = default; |
| 45 |
#define MAKE_CASE(Name) \ |
| 46 |
virtual void* Visit##Name(RegExp##Name*, void* data) = 0; |
| 47 |
FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE) |
| 48 |
#undef MAKE_CASE |
| 49 |
}; |
| 50 |
|
| 51 |
// A simple closed interval. |
| 52 |
class Interval { |
| 53 |
public: |
| 54 |
Interval() : from_(kNone), to_(kNone - 1) {} // '- 1' for branchless size(). |
| 55 |
Interval(int from, int to) : from_(from), to_(to) {} |
| 56 |
Interval Union(Interval that) { |
| 57 |
if (that.from_ == kNone) return *this; |
| 58 |
if (from_ == kNone) return that; |
| 59 |
return Interval(std::min(from_, that.from_), std::max(to_, that.to_)); |
| 60 |
} |
| 61 |
|
| 62 |
static Interval Empty() { return Interval(); } |
| 63 |
|
| 64 |
bool Contains(int value) const { return (from_ <= value) && (value <= to_); } |
| 65 |
bool is_empty() const { return from_ == kNone; } |
| 66 |
int from() const { return from_; } |
| 67 |
int to() const { return to_; } |
| 68 |
int size() const { return to_ - from_ + 1; } |
| 69 |
|
| 70 |
static constexpr int kNone = -1; |
| 71 |
|
| 72 |
private: |
| 73 |
int from_; |
| 74 |
int to_; |
| 75 |
}; |
| 76 |
|
| 77 |
// Named standard character sets. |
| 78 |
enum class StandardCharacterSet : char { |
| 79 |
kWhitespace = 's', // Like /\s/. |
| 80 |
kNotWhitespace = 'S', // Like /\S/. |
| 81 |
kWord = 'w', // Like /\w/. |
| 82 |
kNotWord = 'W', // Like /\W/. |
| 83 |
kDigit = 'd', // Like /\d/. |
| 84 |
kNotDigit = 'D', // Like /\D/. |
| 85 |
kLineTerminator = 'n', // The inverse of /./. |
| 86 |
kNotLineTerminator = '.', // Like /./. |
| 87 |
kEverything = '*', // Matches every character, like /./s. |
| 88 |
}; |
| 89 |
|
| 90 |
// Represents code points (with values up to 0x10FFFF) in the range from from_ |
| 91 |
// to to_, both ends are inclusive. |
| 92 |
class CharacterRange { |
| 93 |
public: |
| 94 |
CharacterRange() = default; |
| 95 |
// For compatibility with the CHECK_OK macro. |
| 96 |
CharacterRange(void* null) { DCHECK_NULL(null); } // NOLINT |
| 97 |
|
| 98 |
static inline CharacterRange Singleton(base::uc32 value) { |
| 99 |
return CharacterRange(value, value); |
| 100 |
} |
| 101 |
static inline CharacterRange Range(base::uc32 from, base::uc32 to) { |
| 102 |
DCHECK(0 <= from && to <= kMaxCodePoint); |
| 103 |
DCHECK(static_cast<uint32_t>(from) <= static_cast<uint32_t>(to)); |
| 104 |
return CharacterRange(from, to); |
| 105 |
} |
| 106 |
static inline CharacterRange Everything() { |
| 107 |
return CharacterRange(0, kMaxCodePoint); |
| 108 |
} |
| 109 |
|
| 110 |
static inline ZoneList<CharacterRange>* List(Zone* zone, |
| 111 |
CharacterRange range) { |
| 112 |
ZoneList<CharacterRange>* list = |
| 113 |
zone->New<ZoneList<CharacterRange>>(1, zone); |
| 114 |
list->Add(range, zone); |
| 115 |
return list; |
| 116 |
} |
| 117 |
|
| 118 |
// Add class escapes. Add case equivalent closure for \w and \W if necessary. |
| 119 |
V8_EXPORT_PRIVATE static void AddClassEscape( |
| 120 |
StandardCharacterSet standard_character_set, |
| 121 |
ZoneList<CharacterRange>* ranges, bool add_unicode_case_equivalents, |
| 122 |
Zone* zone); |
| 123 |
// Add case equivalents to ranges. Only used for /i, not for /ui or /vi, as |
| 124 |
// the semantics for unicode mode are slightly different. |
| 125 |
// See https://tc39.es/ecma262/#sec-runtime-semantics-canonicalize-ch Note 4. |
| 126 |
V8_EXPORT_PRIVATE static void AddCaseEquivalents( |
| 127 |
Isolate* isolate, Zone* zone, ZoneList<CharacterRange>* ranges, |
| 128 |
bool is_one_byte); |
| 129 |
// Add case equivalent code points to ranges. Only used for /ui and /vi, not |
| 130 |
// for /i, as the semantics for non-unicode mode are slightly different. |
| 131 |
// See https://tc39.es/ecma262/#sec-runtime-semantics-canonicalize-ch Note 4. |
| 132 |
static void AddUnicodeCaseEquivalents(ZoneList<CharacterRange>* ranges, |
| 133 |
Zone* zone); |
| 134 |
|
| 135 |
bool Contains(base::uc32 i) const { return from_ <= i && i <= to_; } |
| 136 |
base::uc32 from() const { return from_; } |
| 137 |
base::uc32 to() const { return to_; } |
| 138 |
bool IsEverything(base::uc32 max) const { return from_ == 0 && to_ >= max; } |
| 139 |
bool IsSingleton() const { return from_ == to_; } |
| 140 |
|
| 141 |
// Whether a range list is in canonical form: Ranges ordered by from value, |
| 142 |
// and ranges non-overlapping and non-adjacent. |
| 143 |
V8_EXPORT_PRIVATE static bool IsCanonical( |
| 144 |
const ZoneList<CharacterRange>* ranges); |
| 145 |
// Convert range list to canonical form. The characters covered by the ranges |
| 146 |
// will still be the same, but no character is in more than one range, and |
| 147 |
// adjacent ranges are merged. The resulting list may be shorter than the |
| 148 |
// original, but cannot be longer. |
| 149 |
static void Canonicalize(ZoneList<CharacterRange>* ranges); |
| 150 |
// Negate the contents of a character range in canonical form. |
| 151 |
static void Negate(const ZoneList<CharacterRange>* src, |
| 152 |
ZoneList<CharacterRange>* dst, Zone* zone); |
| 153 |
// Intersect the contents of two character ranges in canonical form. |
| 154 |
static void Intersect(const ZoneList<CharacterRange>* lhs, |
| 155 |
const ZoneList<CharacterRange>* rhs, |
| 156 |
ZoneList<CharacterRange>* dst, Zone* zone); |
| 157 |
// Subtract the contents of |to_remove| from the contents of |src|. |
| 158 |
static void Subtract(const ZoneList<CharacterRange>* src, |
| 159 |
const ZoneList<CharacterRange>* to_remove, |
| 160 |
ZoneList<CharacterRange>* dst, Zone* zone); |
| 161 |
// Remove all ranges outside the one-byte range. |
| 162 |
static void ClampToOneByte(ZoneList<CharacterRange>* ranges); |
| 163 |
// Checks if two ranges (both need to be canonical) are equal. |
| 164 |
static bool Equals(const ZoneList<CharacterRange>* lhs, |
| 165 |
const ZoneList<CharacterRange>* rhs); |
| 166 |
|
| 167 |
private: |
| 168 |
CharacterRange(base::uc32 from, base::uc32 to) : from_(from), to_(to) {} |
| 169 |
|
| 170 |
static constexpr int kMaxCodePoint = 0x10ffff; |
| 171 |
|
| 172 |
base::uc32 from_ = 0; |
| 173 |
base::uc32 to_ = 0; |
| 174 |
}; |
| 175 |
|
| 176 |
inline bool operator==(const CharacterRange& lhs, const CharacterRange& rhs) { |
| 177 |
return lhs.from() == rhs.from() && lhs.to() == rhs.to(); |
| 178 |
} |
| 179 |
inline bool operator!=(const CharacterRange& lhs, const CharacterRange& rhs) { |
| 180 |
return !operator==(lhs, rhs); |
| 181 |
} |
| 182 |
|
| 183 |
#define DECL_BOILERPLATE(Name) \ |
| 184 |
void* Accept(RegExpVisitor* visitor, void* data) override; \ |
| 185 |
RegExpNode* ToNode(RegExpCompiler* compiler, RegExpNode* on_success) \ |
| 186 |
override; \ |
| 187 |
RegExp##Name* As##Name() override; \ |
| 188 |
bool Is##Name() override |
| 189 |
|
| 190 |
class RegExpTree : public ZoneObject { |
| 191 |
public: |
| 192 |
static const int kInfinity = kMaxInt; |
| 193 |
virtual ~RegExpTree() = default; |
| 194 |
virtual void* Accept(RegExpVisitor* visitor, void* data) = 0; |
| 195 |
virtual RegExpNode* ToNode(RegExpCompiler* compiler, |
| 196 |
RegExpNode* on_success) = 0; |
| 197 |
virtual bool IsTextElement() { return false; } |
| 198 |
virtual bool IsAnchoredAtStart() { return false; } |
| 199 |
virtual bool IsAnchoredAtEnd() { return false; } |
| 200 |
virtual int min_match() = 0; |
| 201 |
virtual int max_match() = 0; |
| 202 |
// Returns the interval of registers used for captures within this |
| 203 |
// expression. |
| 204 |
virtual Interval CaptureRegisters() { return Interval::Empty(); } |
| 205 |
virtual void AppendToText(RegExpText* text, Zone* zone); |
| 206 |
V8_EXPORT_PRIVATE std::ostream& Print(std::ostream& os, Zone* zone); |
| 207 |
#define MAKE_ASTYPE(Name) \ |
| 208 |
virtual RegExp##Name* As##Name(); \ |
| 209 |
virtual bool Is##Name(); |
| 210 |
FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE) |
| 211 |
#undef MAKE_ASTYPE |
| 212 |
}; |
| 213 |
|
| 214 |
class RegExpDisjunction final : public RegExpTree { |
| 215 |
public: |
| 216 |
explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives); |
| 217 |
|
| 218 |
DECL_BOILERPLATE(Disjunction); |
| 219 |
|
| 220 |
Interval CaptureRegisters() override; |
| 221 |
bool IsAnchoredAtStart() override; |
| 222 |
bool IsAnchoredAtEnd() override; |
| 223 |
int min_match() override { return min_match_; } |
| 224 |
int max_match() override { return max_match_; } |
| 225 |
ZoneList<RegExpTree*>* alternatives() const { return alternatives_; } |
| 226 |
|
| 227 |
private: |
| 228 |
bool SortConsecutiveAtoms(RegExpCompiler* compiler); |
| 229 |
void RationalizeConsecutiveAtoms(RegExpCompiler* compiler); |
| 230 |
void FixSingleCharacterDisjunctions(RegExpCompiler* compiler); |
| 231 |
ZoneList<RegExpTree*>* alternatives_; |
| 232 |
int min_match_; |
| 233 |
int max_match_; |
| 234 |
}; |
| 235 |
|
| 236 |
class RegExpAlternative final : public RegExpTree { |
| 237 |
public: |
| 238 |
explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes); |
| 239 |
|
| 240 |
DECL_BOILERPLATE(Alternative); |
| 241 |
|
| 242 |
Interval CaptureRegisters() override; |
| 243 |
bool IsAnchoredAtStart() override; |
| 244 |
bool IsAnchoredAtEnd() override; |
| 245 |
int min_match() override { return min_match_; } |
| 246 |
int max_match() override { return max_match_; } |
| 247 |
ZoneList<RegExpTree*>* nodes() const { return nodes_; } |
| 248 |
|
| 249 |
private: |
| 250 |
ZoneList<RegExpTree*>* nodes_; |
| 251 |
int min_match_; |
| 252 |
int max_match_; |
| 253 |
}; |
| 254 |
|
| 255 |
class RegExpAssertion final : public RegExpTree { |
| 256 |
public: |
| 257 |
enum class Type { |
| 258 |
START_OF_LINE = 0, |
| 259 |
START_OF_INPUT = 1, |
| 260 |
END_OF_LINE = 2, |
| 261 |
END_OF_INPUT = 3, |
| 262 |
BOUNDARY = 4, |
| 263 |
NON_BOUNDARY = 5, |
| 264 |
LAST_ASSERTION_TYPE = NON_BOUNDARY, |
| 265 |
}; |
| 266 |
explicit RegExpAssertion(Type type) : assertion_type_(type) {} |
| 267 |
|
| 268 |
DECL_BOILERPLATE(Assertion); |
| 269 |
|
| 270 |
bool IsAnchoredAtStart() override; |
| 271 |
bool IsAnchoredAtEnd() override; |
| 272 |
int min_match() override { return 0; } |
| 273 |
int max_match() override { return 0; } |
| 274 |
Type assertion_type() const { return assertion_type_; } |
| 275 |
|
| 276 |
private: |
| 277 |
const Type assertion_type_; |
| 278 |
}; |
| 279 |
|
| 280 |
class CharacterSet final { |
| 281 |
public: |
| 282 |
explicit CharacterSet(StandardCharacterSet standard_set_type) |
| 283 |
: standard_set_type_(standard_set_type) {} |
| 284 |
explicit CharacterSet(ZoneList<CharacterRange>* ranges) : ranges_(ranges) {} |
| 285 |
|
| 286 |
ZoneList<CharacterRange>* ranges(Zone* zone); |
| 287 |
StandardCharacterSet standard_set_type() const { |
| 288 |
return standard_set_type_.value(); |
| 289 |
} |
| 290 |
void set_standard_set_type(StandardCharacterSet standard_set_type) { |
| 291 |
standard_set_type_ = standard_set_type; |
| 292 |
} |
| 293 |
bool is_standard() const { return standard_set_type_.has_value(); } |
| 294 |
V8_EXPORT_PRIVATE void Canonicalize(); |
| 295 |
|
| 296 |
private: |
| 297 |
ZoneList<CharacterRange>* ranges_ = nullptr; |
| 298 |
std::optional<StandardCharacterSet> standard_set_type_; |
| 299 |
}; |
| 300 |
|
| 301 |
class RegExpClassRanges final : public RegExpTree { |
| 302 |
public: |
| 303 |
// NEGATED: The character class is negated and should match everything but |
| 304 |
// the specified ranges. |
| 305 |
// CONTAINS_SPLIT_SURROGATE: The character class contains part of a split |
| 306 |
// surrogate and should not be unicode-desugared (crbug.com/641091). |
| 307 |
// IS_CASE_FOLDED: If case folding is required (/i), it was already |
| 308 |
// performed on individual ranges and should not be applied again. |
| 309 |
enum Flag { |
| 310 |
NEGATED = 1 << 0, |
| 311 |
CONTAINS_SPLIT_SURROGATE = 1 << 1, |
| 312 |
IS_CASE_FOLDED = 1 << 2, |
| 313 |
}; |
| 314 |
using ClassRangesFlags = base::Flags<Flag>; |
| 315 |
|
| 316 |
RegExpClassRanges(Zone* zone, ZoneList<CharacterRange>* ranges, |
| 317 |
ClassRangesFlags class_ranges_flags = ClassRangesFlags()) |
| 318 |
: set_(ranges), class_ranges_flags_(class_ranges_flags) { |
| 319 |
// Convert the empty set of ranges to the negated Everything() range. |
| 320 |
if (ranges->is_empty()) { |
| 321 |
ranges->Add(CharacterRange::Everything(), zone); |
| 322 |
class_ranges_flags_ ^= NEGATED; |
| 323 |
} |
| 324 |
} |
| 325 |
explicit RegExpClassRanges(StandardCharacterSet standard_set_type) |
| 326 |
: set_(standard_set_type), class_ranges_flags_() {} |
| 327 |
|
| 328 |
DECL_BOILERPLATE(ClassRanges); |
| 329 |
|
| 330 |
bool IsTextElement() override { return true; } |
| 331 |
int min_match() override { return 1; } |
| 332 |
// The character class may match two code units for unicode regexps. |
| 333 |
// TODO(yangguo): we should split this class for usage in TextElement, and |
| 334 |
// make max_match() dependent on the character class content. |
| 335 |
int max_match() override { return 2; } |
| 336 |
|
| 337 |
void AppendToText(RegExpText* text, Zone* zone) override; |
| 338 |
|
| 339 |
// TODO(lrn): Remove need for complex version if is_standard that |
| 340 |
// recognizes a mangled standard set and just do { return set_.is_special(); } |
| 341 |
bool is_standard(Zone* zone); |
| 342 |
// Returns a value representing the standard character set if is_standard() |
| 343 |
// returns true. |
| 344 |
StandardCharacterSet standard_type() const { |
| 345 |
return set_.standard_set_type(); |
| 346 |
} |
| 347 |
|
| 348 |
CharacterSet character_set() const { return set_; } |
| 349 |
ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); } |
| 350 |
|
| 351 |
bool is_negated() const { return (class_ranges_flags_ & NEGATED) != 0; } |
| 352 |
bool contains_split_surrogate() const { |
| 353 |
return (class_ranges_flags_ & CONTAINS_SPLIT_SURROGATE) != 0; |
| 354 |
} |
| 355 |
bool is_case_folded() const { |
| 356 |
return (class_ranges_flags_ & IS_CASE_FOLDED) != 0; |
| 357 |
} |
| 358 |
|
| 359 |
private: |
| 360 |
CharacterSet set_; |
| 361 |
ClassRangesFlags class_ranges_flags_; |
| 362 |
}; |
| 363 |
|
| 364 |
struct CharacterClassStringLess { |
| 365 |
bool operator()(base::Vector<const base::uc32> lhs, |
| 366 |
base::Vector<const base::uc32> rhs) const { |
| 367 |
// Longer strings first so we generate matches for the largest string |
| 368 |
// possible. |
| 369 |
if (lhs.length() != rhs.length()) { |
| 370 |
return lhs.length() > rhs.length(); |
| 371 |
} |
| 372 |
for (int i = 0; i < lhs.length(); i++) { |
| 373 |
if (lhs[i] != rhs[i]) { |
| 374 |
return lhs[i] < rhs[i]; |
| 375 |
} |
| 376 |
} |
| 377 |
return false; |
| 378 |
} |
| 379 |
}; |
| 380 |
|
| 381 |
// A type used for strings as part of character classes (only possible in |
| 382 |
// unicode sets mode). |
| 383 |
// We use a ZoneMap instead of an UnorderedZoneMap because we need to match |
| 384 |
// the longest alternatives first. By using a ZoneMap with the custom comparator |
| 385 |
// we can avoid sorting before assembling the code. |
| 386 |
// Strings are likely short (the largest string in current unicode properties |
| 387 |
// consists of 10 code points). |
| 388 |
using CharacterClassStrings = ZoneMap<base::Vector<const base::uc32>, |
| 389 |
RegExpTree*, CharacterClassStringLess>; |
| 390 |
|
| 391 |
// TODO(pthier): If we are sure we don't want to use icu::UnicodeSets |
| 392 |
// (performance evaluation pending), this class can be merged with |
| 393 |
// RegExpClassRanges. |
| 394 |
class RegExpClassSetOperand final : public RegExpTree { |
| 395 |
public: |
| 396 |
RegExpClassSetOperand(ZoneList<CharacterRange>* ranges, |
| 397 |
CharacterClassStrings* strings); |
| 398 |
|
| 399 |
DECL_BOILERPLATE(ClassSetOperand); |
| 400 |
|
| 401 |
bool IsTextElement() override { return true; } |
| 402 |
int min_match() override { return min_match_; } |
| 403 |
int max_match() override { return max_match_; } |
| 404 |
|
| 405 |
void Union(RegExpClassSetOperand* other, Zone* zone); |
| 406 |
void Intersect(RegExpClassSetOperand* other, |
| 407 |
ZoneList<CharacterRange>* temp_ranges, Zone* zone); |
| 408 |
void Subtract(RegExpClassSetOperand* other, |
| 409 |
ZoneList<CharacterRange>* temp_ranges, Zone* zone); |
| 410 |
|
| 411 |
bool has_strings() const { return strings_ != nullptr && !strings_->empty(); } |
| 412 |
ZoneList<CharacterRange>* ranges() { return ranges_; } |
| 413 |
CharacterClassStrings* strings() { |
| 414 |
DCHECK_NOT_NULL(strings_); |
| 415 |
return strings_; |
| 416 |
} |
| 417 |
|
| 418 |
private: |
| 419 |
ZoneList<CharacterRange>* ranges_; |
| 420 |
CharacterClassStrings* strings_; |
| 421 |
int min_match_; |
| 422 |
int max_match_; |
| 423 |
}; |
| 424 |
|
| 425 |
class RegExpClassSetExpression final : public RegExpTree { |
| 426 |
public: |
| 427 |
enum class OperationType { kUnion, kIntersection, kSubtraction }; |
| 428 |
|
| 429 |
RegExpClassSetExpression(OperationType op, bool is_negated, |
| 430 |
bool may_contain_strings, |
| 431 |
ZoneList<RegExpTree*>* operands); |
| 432 |
|
| 433 |
DECL_BOILERPLATE(ClassSetExpression); |
| 434 |
|
| 435 |
// Create an empty class set expression (matches everything if |is_negated|, |
| 436 |
// nothing otherwise). |
| 437 |
static RegExpClassSetExpression* Empty(Zone* zone, bool is_negated); |
| 438 |
|
| 439 |
bool IsTextElement() override { return true; } |
| 440 |
int min_match() override { return 0; } |
| 441 |
int max_match() override { return max_match_; } |
| 442 |
|
| 443 |
OperationType operation() const { return operation_; } |
| 444 |
bool is_negated() const { return is_negated_; } |
| 445 |
bool may_contain_strings() const { return may_contain_strings_; } |
| 446 |
const ZoneList<RegExpTree*>* operands() const { return operands_; } |
| 447 |
ZoneList<RegExpTree*>* operands() { return operands_; } |
| 448 |
|
| 449 |
private: |
| 450 |
// Recursively evaluates the tree rooted at |root|, computing the valid |
| 451 |
// CharacterRanges and strings after applying all set operations. |
| 452 |
// The original tree will be modified by this method, so don't store pointers |
| 453 |
// to inner nodes of the tree somewhere else! |
| 454 |
// Modifying the tree in-place saves memory and speeds up multiple calls of |
| 455 |
// the method (e.g. when unrolling quantifiers). |
| 456 |
// |temp_ranges| is used for intermediate results, passed as parameter to |
| 457 |
// avoid allocating new lists all the time. |
| 458 |
static RegExpClassSetOperand* ComputeExpression( |
| 459 |
RegExpTree* root, ZoneList<CharacterRange>* temp_ranges, Zone* zone); |
| 460 |
|
| 461 |
const OperationType operation_; |
| 462 |
bool is_negated_; |
| 463 |
const bool may_contain_strings_; |
| 464 |
ZoneList<RegExpTree*>* operands_ = nullptr; |
| 465 |
int max_match_; |
| 466 |
}; |
| 467 |
|
| 468 |
class RegExpAtom final : public RegExpTree { |
| 469 |
public: |
| 470 |
explicit RegExpAtom(base::Vector<const base::uc16> data) : data_(data) {} |
| 471 |
|
| 472 |
DECL_BOILERPLATE(Atom); |
| 473 |
|
| 474 |
bool IsTextElement() override { return true; } |
| 475 |
int min_match() override { return data_.length(); } |
| 476 |
int max_match() override { return data_.length(); } |
| 477 |
void AppendToText(RegExpText* text, Zone* zone) override; |
| 478 |
|
| 479 |
base::Vector<const base::uc16> data() const { return data_; } |
| 480 |
int length() const { return data_.length(); } |
| 481 |
|
| 482 |
private: |
| 483 |
base::Vector<const base::uc16> data_; |
| 484 |
}; |
| 485 |
|
| 486 |
class TextElement final { |
| 487 |
public: |
| 488 |
enum TextType { ATOM, CLASS_RANGES }; |
| 489 |
|
| 490 |
static TextElement Atom(RegExpAtom* atom); |
| 491 |
static TextElement ClassRanges(RegExpClassRanges* class_ranges); |
| 492 |
|
| 493 |
int cp_offset() const { return cp_offset_; } |
| 494 |
void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; } |
| 495 |
int length() const; |
| 496 |
|
| 497 |
TextType text_type() const { return text_type_; } |
| 498 |
|
| 499 |
RegExpTree* tree() const { return tree_; } |
| 500 |
|
| 501 |
RegExpAtom* atom() const { |
| 502 |
DCHECK(text_type() == ATOM); |
| 503 |
return reinterpret_cast<RegExpAtom*>(tree()); |
| 504 |
} |
| 505 |
|
| 506 |
RegExpClassRanges* class_ranges() const { |
| 507 |
DCHECK(text_type() == CLASS_RANGES); |
| 508 |
return reinterpret_cast<RegExpClassRanges*>(tree()); |
| 509 |
} |
| 510 |
|
| 511 |
private: |
| 512 |
TextElement(TextType text_type, RegExpTree* tree) |
| 513 |
: cp_offset_(-1), text_type_(text_type), tree_(tree) {} |
| 514 |
|
| 515 |
int cp_offset_; |
| 516 |
TextType text_type_; |
| 517 |
RegExpTree* tree_; |
| 518 |
}; |
| 519 |
|
| 520 |
class RegExpText final : public RegExpTree { |
| 521 |
public: |
| 522 |
explicit RegExpText(Zone* zone) : elements_(2, zone) {} |
| 523 |
|
| 524 |
DECL_BOILERPLATE(Text); |
| 525 |
|
| 526 |
bool IsTextElement() override { return true; } |
| 527 |
int min_match() override { return length_; } |
| 528 |
int max_match() override { return length_; } |
| 529 |
void AppendToText(RegExpText* text, Zone* zone) override; |
| 530 |
void AddElement(TextElement elm, Zone* zone) { |
| 531 |
elements_.Add(elm, zone); |
| 532 |
length_ += elm.length(); |
| 533 |
} |
| 534 |
ZoneList<TextElement>* elements() { return &elements_; } |
| 535 |
|
| 536 |
private: |
| 537 |
ZoneList<TextElement> elements_; |
| 538 |
int length_ = 0; |
| 539 |
}; |
| 540 |
|
| 541 |
class RegExpQuantifier final : public RegExpTree { |
| 542 |
public: |
| 543 |
enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE }; |
| 544 |
RegExpQuantifier(int min, int max, QuantifierType type, int index, |
| 545 |
RegExpTree* body) |
| 546 |
: body_(body), |
| 547 |
min_(min), |
| 548 |
max_(max), |
| 549 |
quantifier_type_(type), |
| 550 |
index_(index) { |
| 551 |
if (min > 0 && body->min_match() > kInfinity / min) { |
| 552 |
min_match_ = kInfinity; |
| 553 |
} else { |
| 554 |
min_match_ = min * body->min_match(); |
| 555 |
} |
| 556 |
if (max > 0 && body->max_match() > kInfinity / max) { |
| 557 |
max_match_ = kInfinity; |
| 558 |
} else { |
| 559 |
max_match_ = max * body->max_match(); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
DECL_BOILERPLATE(Quantifier); |
| 564 |
|
| 565 |
static RegExpNode* ToNode(int min, int max, bool is_greedy, RegExpTree* body, |
| 566 |
RegExpCompiler* compiler, RegExpNode* on_success, |
| 567 |
bool not_at_start = false); |
| 568 |
Interval CaptureRegisters() override; |
| 569 |
int min_match() override { return min_match_; } |
| 570 |
int max_match() override { return max_match_; } |
| 571 |
int min() const { return min_; } |
| 572 |
int max() const { return max_; } |
| 573 |
QuantifierType quantifier_type() const { return quantifier_type_; } |
| 574 |
int index() const { return index_; } |
| 575 |
bool is_possessive() const { return quantifier_type_ == POSSESSIVE; } |
| 576 |
bool is_non_greedy() const { return quantifier_type_ == NON_GREEDY; } |
| 577 |
bool is_greedy() const { return quantifier_type_ == GREEDY; } |
| 578 |
RegExpTree* body() const { return body_; } |
| 579 |
|
| 580 |
private: |
| 581 |
RegExpTree* body_; |
| 582 |
int min_; |
| 583 |
int max_; |
| 584 |
int min_match_; |
| 585 |
int max_match_; |
| 586 |
QuantifierType quantifier_type_; |
| 587 |
int index_; |
| 588 |
}; |
| 589 |
|
| 590 |
class RegExpCapture final : public RegExpTree { |
| 591 |
public: |
| 592 |
explicit RegExpCapture(int index) |
| 593 |
: body_(nullptr), |
| 594 |
index_(index), |
| 595 |
min_match_(0), |
| 596 |
max_match_(0), |
| 597 |
name_(nullptr) {} |
| 598 |
|
| 599 |
DECL_BOILERPLATE(Capture); |
| 600 |
|
| 601 |
static RegExpNode* ToNode(RegExpTree* body, int index, |
| 602 |
RegExpCompiler* compiler, RegExpNode* on_success); |
| 603 |
bool IsAnchoredAtStart() override; |
| 604 |
bool IsAnchoredAtEnd() override; |
| 605 |
Interval CaptureRegisters() override; |
| 606 |
int min_match() override { return min_match_; } |
| 607 |
int max_match() override { return max_match_; } |
| 608 |
RegExpTree* body() { return body_; } |
| 609 |
void set_body(RegExpTree* body) { |
| 610 |
body_ = body; |
| 611 |
min_match_ = body->min_match(); |
| 612 |
max_match_ = body->max_match(); |
| 613 |
} |
| 614 |
int index() const { return index_; } |
| 615 |
const ZoneVector<base::uc16>* name() const { return name_; } |
| 616 |
void set_name(const ZoneVector<base::uc16>* name) { name_ = name; } |
| 617 |
static int StartRegister(int index) { return index * 2; } |
| 618 |
static int EndRegister(int index) { return index * 2 + 1; } |
| 619 |
|
| 620 |
private: |
| 621 |
RegExpTree* body_ = nullptr; |
| 622 |
int index_; |
| 623 |
int min_match_ = 0; |
| 624 |
int max_match_ = 0; |
| 625 |
const ZoneVector<base::uc16>* name_ = nullptr; |
| 626 |
}; |
| 627 |
|
| 628 |
class RegExpGroup final : public RegExpTree { |
| 629 |
public: |
| 630 |
explicit RegExpGroup(RegExpTree* body, RegExpFlags flags) |
| 631 |
: body_(body), |
| 632 |
flags_(flags), |
| 633 |
min_match_(body->min_match()), |
| 634 |
max_match_(body->max_match()) {} |
| 635 |
|
| 636 |
DECL_BOILERPLATE(Group); |
| 637 |
|
| 638 |
bool IsAnchoredAtStart() override { return body_->IsAnchoredAtStart(); } |
| 639 |
bool IsAnchoredAtEnd() override { return body_->IsAnchoredAtEnd(); } |
| 640 |
int min_match() override { return min_match_; } |
| 641 |
int max_match() override { return max_match_; } |
| 642 |
Interval CaptureRegisters() override { return body_->CaptureRegisters(); } |
| 643 |
RegExpTree* body() const { return body_; } |
| 644 |
RegExpFlags flags() const { return flags_; } |
| 645 |
|
| 646 |
private: |
| 647 |
RegExpTree* body_; |
| 648 |
const RegExpFlags flags_; |
| 649 |
int min_match_; |
| 650 |
int max_match_; |
| 651 |
}; |
| 652 |
|
| 653 |
class RegExpLookaround final : public RegExpTree { |
| 654 |
public: |
| 655 |
enum Type { LOOKAHEAD, LOOKBEHIND }; |
| 656 |
|
| 657 |
RegExpLookaround(RegExpTree* body, bool is_positive, int capture_count, |
| 658 |
int capture_from, Type type, int index) |
| 659 |
: body_(body), |
| 660 |
is_positive_(is_positive), |
| 661 |
capture_count_(capture_count), |
| 662 |
capture_from_(capture_from), |
| 663 |
type_(type), |
| 664 |
index_(index) {} |
| 665 |
|
| 666 |
DECL_BOILERPLATE(Lookaround); |
| 667 |
|
| 668 |
Interval CaptureRegisters() override; |
| 669 |
bool IsAnchoredAtStart() override; |
| 670 |
int min_match() override { return 0; } |
| 671 |
int max_match() override { return 0; } |
| 672 |
RegExpTree* body() const { return body_; } |
| 673 |
bool is_positive() const { return is_positive_; } |
| 674 |
int capture_count() const { return capture_count_; } |
| 675 |
int capture_from() const { return capture_from_; } |
| 676 |
Type type() const { return type_; } |
| 677 |
int index() const { return index_; } |
| 678 |
|
| 679 |
class Builder { |
| 680 |
public: |
| 681 |
Builder(bool is_positive, RegExpNode* on_success, |
| 682 |
int stack_pointer_register, int position_register, |
| 683 |
int capture_register_count = 0, int capture_register_start = 0); |
| 684 |
RegExpNode* on_match_success() const { return on_match_success_; } |
| 685 |
RegExpNode* ForMatch(RegExpNode* match); |
| 686 |
|
| 687 |
private: |
| 688 |
bool is_positive_; |
| 689 |
RegExpNode* on_match_success_; |
| 690 |
RegExpNode* on_success_; |
| 691 |
int stack_pointer_register_; |
| 692 |
int position_register_; |
| 693 |
}; |
| 694 |
|
| 695 |
private: |
| 696 |
RegExpTree* body_; |
| 697 |
bool is_positive_; |
| 698 |
int capture_count_; |
| 699 |
int capture_from_; |
| 700 |
Type type_; |
| 701 |
int index_; |
| 702 |
}; |
| 703 |
|
| 704 |
class RegExpBackReference final : public RegExpTree { |
| 705 |
public: |
| 706 |
explicit RegExpBackReference(Zone* zone) : captures_(1, zone) {} |
| 707 |
explicit RegExpBackReference(RegExpCapture* capture, Zone* zone) |
| 708 |
: captures_(1, zone) { |
| 709 |
captures_.Add(capture, zone); |
| 710 |
} |
| 711 |
|
| 712 |
DECL_BOILERPLATE(BackReference); |
| 713 |
|
| 714 |
int min_match() override { return 0; } |
| 715 |
// The back reference may be recursive, e.g. /(\2)(\1)/. To avoid infinite |
| 716 |
// recursion, we give up. Ignorance is bliss. |
| 717 |
int max_match() override { return kInfinity; } |
| 718 |
const ZoneList<RegExpCapture*>* captures() const { return &captures_; } |
| 719 |
void add_capture(RegExpCapture* capture, Zone* zone) { |
| 720 |
captures_.Add(capture, zone); |
| 721 |
} |
| 722 |
const ZoneVector<base::uc16>* name() const { return name_; } |
| 723 |
void set_name(const ZoneVector<base::uc16>* name) { name_ = name; } |
| 724 |
|
| 725 |
private: |
| 726 |
ZoneList<RegExpCapture*> captures_; |
| 727 |
const ZoneVector<base::uc16>* name_ = nullptr; |
| 728 |
}; |
| 729 |
|
| 730 |
class RegExpEmpty final : public RegExpTree { |
| 731 |
public: |
| 732 |
DECL_BOILERPLATE(Empty); |
| 733 |
int min_match() override { return 0; } |
| 734 |
int max_match() override { return 0; } |
| 735 |
}; |
| 736 |
|
| 737 |
} // namespace v8::internal |
| 738 |
|
| 739 |
#undef DECL_BOILERPLATE |
| 740 |
|
| 741 |
#endif // V8_REGEXP_REGEXP_AST_H_ |