/proc/self/cwd/external/re2~/re2/parse.cc
Line | Count | Source |
1 | | // Copyright 2006 The RE2 Authors. All Rights Reserved. |
2 | | // Use of this source code is governed by a BSD-style |
3 | | // license that can be found in the LICENSE file. |
4 | | |
5 | | // Regular expression parser. |
6 | | |
7 | | // The parser is a simple precedence-based parser with a |
8 | | // manual stack. The parsing work is done by the methods |
9 | | // of the ParseState class. The Regexp::Parse function is |
10 | | // essentially just a lexer that calls the ParseState method |
11 | | // for each token. |
12 | | |
13 | | // The parser recognizes POSIX extended regular expressions |
14 | | // excluding backreferences, collating elements, and collating |
15 | | // classes. It also allows the empty string as a regular expression |
16 | | // and recognizes the Perl escape sequences \d, \s, \w, \D, \S, and \W. |
17 | | // See regexp.h for rationale. |
18 | | |
19 | | #include <stddef.h> |
20 | | #include <stdint.h> |
21 | | #include <string.h> |
22 | | |
23 | | #include <algorithm> |
24 | | #include <string> |
25 | | #include <vector> |
26 | | |
27 | | #include "absl/base/macros.h" |
28 | | #include "absl/log/absl_log.h" |
29 | | #include "absl/strings/ascii.h" |
30 | | #include "absl/strings/string_view.h" |
31 | | #include "re2/pod_array.h" |
32 | | #include "re2/regexp.h" |
33 | | #include "re2/unicode_casefold.h" |
34 | | #include "re2/unicode_groups.h" |
35 | | #include "re2/walker-inl.h" |
36 | | #include "util/utf.h" |
37 | | |
38 | | #if defined(RE2_USE_ICU) |
39 | | #include "unicode/uniset.h" |
40 | | #include "unicode/unistr.h" |
41 | | #include "unicode/utypes.h" |
42 | | #endif |
43 | | |
44 | | namespace re2 { |
45 | | |
46 | | // Controls the maximum repeat count permitted by the parser. |
47 | | static int maximum_repeat_count = 1000; |
48 | | |
49 | 0 | void Regexp::FUZZING_ONLY_set_maximum_repeat_count(int i) { |
50 | 0 | maximum_repeat_count = i; |
51 | 0 | } |
52 | | |
53 | | // Regular expression parse state. |
54 | | // The list of parsed regexps so far is maintained as a vector of |
55 | | // Regexp pointers called the stack. Left parenthesis and vertical |
56 | | // bar markers are also placed on the stack, as Regexps with |
57 | | // non-standard opcodes. |
58 | | // Scanning a left parenthesis causes the parser to push a left parenthesis |
59 | | // marker on the stack. |
60 | | // Scanning a vertical bar causes the parser to pop the stack until it finds a |
61 | | // vertical bar or left parenthesis marker (not popping the marker), |
62 | | // concatenate all the popped results, and push them back on |
63 | | // the stack (DoConcatenation). |
64 | | // Scanning a right parenthesis causes the parser to act as though it |
65 | | // has seen a vertical bar, which then leaves the top of the stack in the |
66 | | // form LeftParen regexp VerticalBar regexp VerticalBar ... regexp VerticalBar. |
67 | | // The parser pops all this off the stack and creates an alternation of the |
68 | | // regexps (DoAlternation). |
69 | | |
70 | | class Regexp::ParseState { |
71 | | public: |
72 | | ParseState(ParseFlags flags, absl::string_view whole_regexp, |
73 | | RegexpStatus* status); |
74 | | ~ParseState(); |
75 | | |
76 | 0 | ParseFlags flags() { return flags_; } |
77 | 0 | int rune_max() { return rune_max_; } |
78 | | |
79 | | // Parse methods. All public methods return a bool saying |
80 | | // whether parsing should continue. If a method returns |
81 | | // false, it has set fields in *status_, and the parser |
82 | | // should return NULL. |
83 | | |
84 | | // Pushes the given regular expression onto the stack. |
85 | | // Could check for too much memory used here. |
86 | | bool PushRegexp(Regexp* re); |
87 | | |
88 | | // Pushes the literal rune r onto the stack. |
89 | | bool PushLiteral(Rune r); |
90 | | |
91 | | // Pushes a regexp with the given op (and no args) onto the stack. |
92 | | bool PushSimpleOp(RegexpOp op); |
93 | | |
94 | | // Pushes a ^ onto the stack. |
95 | | bool PushCaret(); |
96 | | |
97 | | // Pushes a \b (word == true) or \B (word == false) onto the stack. |
98 | | bool PushWordBoundary(bool word); |
99 | | |
100 | | // Pushes a $ onto the stack. |
101 | | bool PushDollar(); |
102 | | |
103 | | // Pushes a . onto the stack |
104 | | bool PushDot(); |
105 | | |
106 | | // Pushes a repeat operator regexp onto the stack. |
107 | | // A valid argument for the operator must already be on the stack. |
108 | | // s is the name of the operator, for use in error messages. |
109 | | bool PushRepeatOp(RegexpOp op, absl::string_view s, bool nongreedy); |
110 | | |
111 | | // Pushes a repetition regexp onto the stack. |
112 | | // A valid argument for the operator must already be on the stack. |
113 | | bool PushRepetition(int min, int max, absl::string_view s, bool nongreedy); |
114 | | |
115 | | // Checks whether a particular regexp op is a marker. |
116 | | bool IsMarker(RegexpOp op); |
117 | | |
118 | | // Processes a left parenthesis in the input. |
119 | | // Pushes a marker onto the stack. |
120 | | bool DoLeftParen(absl::string_view name); |
121 | | bool DoLeftParenNoCapture(); |
122 | | |
123 | | // Processes a vertical bar in the input. |
124 | | bool DoVerticalBar(); |
125 | | |
126 | | // Processes a right parenthesis in the input. |
127 | | bool DoRightParen(); |
128 | | |
129 | | // Processes the end of input, returning the final regexp. |
130 | | Regexp* DoFinish(); |
131 | | |
132 | | // Finishes the regexp if necessary, preparing it for use |
133 | | // in a more complicated expression. |
134 | | // If it is a CharClassBuilder, converts into a CharClass. |
135 | | Regexp* FinishRegexp(Regexp*); |
136 | | |
137 | | // These routines don't manipulate the parse stack |
138 | | // directly, but they do need to look at flags_. |
139 | | // ParseCharClass also manipulates the internals of Regexp |
140 | | // while creating *out_re. |
141 | | |
142 | | // Parse a character class into *out_re. |
143 | | // Removes parsed text from s. |
144 | | bool ParseCharClass(absl::string_view* s, Regexp** out_re, |
145 | | RegexpStatus* status); |
146 | | |
147 | | // Parse a character class character into *rp. |
148 | | // Removes parsed text from s. |
149 | | bool ParseCCCharacter(absl::string_view* s, Rune* rp, |
150 | | absl::string_view whole_class, |
151 | | RegexpStatus* status); |
152 | | |
153 | | // Parse a character class range into rr. |
154 | | // Removes parsed text from s. |
155 | | bool ParseCCRange(absl::string_view* s, RuneRange* rr, |
156 | | absl::string_view whole_class, |
157 | | RegexpStatus* status); |
158 | | |
159 | | // Parse a Perl flag set or non-capturing group from s. |
160 | | bool ParsePerlFlags(absl::string_view* s); |
161 | | |
162 | | // Finishes the current concatenation, |
163 | | // collapsing it into a single regexp on the stack. |
164 | | void DoConcatenation(); |
165 | | |
166 | | // Finishes the current alternation, |
167 | | // collapsing it to a single regexp on the stack. |
168 | | void DoAlternation(); |
169 | | |
170 | | // Generalized DoAlternation/DoConcatenation. |
171 | | void DoCollapse(RegexpOp op); |
172 | | |
173 | | // Maybe concatenate Literals into LiteralString. |
174 | | bool MaybeConcatString(int r, ParseFlags flags); |
175 | | |
176 | | private: |
177 | | ParseFlags flags_; |
178 | | absl::string_view whole_regexp_; |
179 | | RegexpStatus* status_; |
180 | | Regexp* stacktop_; |
181 | | int ncap_; // number of capturing parens seen |
182 | | int rune_max_; // maximum char value for this encoding |
183 | | |
184 | | ParseState(const ParseState&) = delete; |
185 | | ParseState& operator=(const ParseState&) = delete; |
186 | | }; |
187 | | |
188 | | // Pseudo-operators - only on parse stack. |
189 | | const RegexpOp kLeftParen = static_cast<RegexpOp>(kMaxRegexpOp+1); |
190 | | const RegexpOp kVerticalBar = static_cast<RegexpOp>(kMaxRegexpOp+2); |
191 | | |
192 | | Regexp::ParseState::ParseState(ParseFlags flags, |
193 | | absl::string_view whole_regexp, |
194 | | RegexpStatus* status) |
195 | 0 | : flags_(flags), whole_regexp_(whole_regexp), |
196 | 0 | status_(status), stacktop_(NULL), ncap_(0) { |
197 | 0 | if (flags_ & Latin1) |
198 | 0 | rune_max_ = 0xFF; |
199 | 0 | else |
200 | 0 | rune_max_ = Runemax; |
201 | 0 | } |
202 | | |
203 | | // Cleans up by freeing all the regexps on the stack. |
204 | 0 | Regexp::ParseState::~ParseState() { |
205 | 0 | Regexp* next; |
206 | 0 | for (Regexp* re = stacktop_; re != NULL; re = next) { |
207 | 0 | next = re->down_; |
208 | 0 | re->down_ = NULL; |
209 | 0 | if (re->op() == kLeftParen) |
210 | 0 | delete re->name_; |
211 | 0 | re->Decref(); |
212 | 0 | } |
213 | 0 | } |
214 | | |
215 | | // Finishes the regexp if necessary, preparing it for use in |
216 | | // a more complex expression. |
217 | | // If it is a CharClassBuilder, converts into a CharClass. |
218 | 0 | Regexp* Regexp::ParseState::FinishRegexp(Regexp* re) { |
219 | 0 | if (re == NULL) |
220 | 0 | return NULL; |
221 | 0 | re->down_ = NULL; |
222 | |
|
223 | 0 | if (re->op_ == kRegexpCharClass && re->ccb_ != NULL) { |
224 | 0 | CharClassBuilder* ccb = re->ccb_; |
225 | 0 | re->ccb_ = NULL; |
226 | 0 | re->cc_ = ccb->GetCharClass(); |
227 | 0 | delete ccb; |
228 | 0 | } |
229 | |
|
230 | 0 | return re; |
231 | 0 | } |
232 | | |
233 | | // Pushes the given regular expression onto the stack. |
234 | | // Could check for too much memory used here. |
235 | 0 | bool Regexp::ParseState::PushRegexp(Regexp* re) { |
236 | 0 | MaybeConcatString(-1, NoParseFlags); |
237 | | |
238 | | // Special case: a character class of one character is just |
239 | | // a literal. This is a common idiom for escaping |
240 | | // single characters (e.g., [.] instead of \.), and some |
241 | | // analysis does better with fewer character classes. |
242 | | // Similarly, [Aa] can be rewritten as a literal A with ASCII case folding. |
243 | 0 | if (re->op_ == kRegexpCharClass && re->ccb_ != NULL) { |
244 | 0 | re->ccb_->RemoveAbove(rune_max_); |
245 | 0 | if (re->ccb_->size() == 1) { |
246 | 0 | Rune r = re->ccb_->begin()->lo; |
247 | 0 | re->Decref(); |
248 | 0 | re = new Regexp(kRegexpLiteral, flags_); |
249 | 0 | re->rune_ = r; |
250 | 0 | } else if (re->ccb_->size() == 2) { |
251 | 0 | Rune r = re->ccb_->begin()->lo; |
252 | 0 | if ('A' <= r && r <= 'Z' && re->ccb_->Contains(r + 'a' - 'A')) { |
253 | 0 | re->Decref(); |
254 | 0 | re = new Regexp(kRegexpLiteral, flags_ | FoldCase); |
255 | 0 | re->rune_ = r + 'a' - 'A'; |
256 | 0 | } |
257 | 0 | } |
258 | 0 | } |
259 | |
|
260 | 0 | if (!IsMarker(re->op())) |
261 | 0 | re->simple_ = re->ComputeSimple(); |
262 | 0 | re->down_ = stacktop_; |
263 | 0 | stacktop_ = re; |
264 | 0 | return true; |
265 | 0 | } |
266 | | |
267 | | // Searches the case folding tables and returns the CaseFold* that contains r. |
268 | | // If there isn't one, returns the CaseFold* with smallest f->lo bigger than r. |
269 | | // If there isn't one, returns NULL. |
270 | 0 | const CaseFold* LookupCaseFold(const CaseFold* f, int n, Rune r) { |
271 | 0 | const CaseFold* ef = f + n; |
272 | | |
273 | | // Binary search for entry containing r. |
274 | 0 | while (n > 0) { |
275 | 0 | int m = n/2; |
276 | 0 | if (f[m].lo <= r && r <= f[m].hi) |
277 | 0 | return &f[m]; |
278 | 0 | if (r < f[m].lo) { |
279 | 0 | n = m; |
280 | 0 | } else { |
281 | 0 | f += m+1; |
282 | 0 | n -= m+1; |
283 | 0 | } |
284 | 0 | } |
285 | | |
286 | | // There is no entry that contains r, but f points |
287 | | // where it would have been. Unless f points at |
288 | | // the end of the array, it points at the next entry |
289 | | // after r. |
290 | 0 | if (f < ef) |
291 | 0 | return f; |
292 | | |
293 | | // No entry contains r; no entry contains runes > r. |
294 | 0 | return NULL; |
295 | 0 | } |
296 | | |
297 | | // Returns the result of applying the fold f to the rune r. |
298 | 0 | Rune ApplyFold(const CaseFold* f, Rune r) { |
299 | 0 | switch (f->delta) { |
300 | 0 | default: |
301 | 0 | return r + f->delta; |
302 | | |
303 | 0 | case EvenOddSkip: // even <-> odd but only applies to every other |
304 | 0 | if ((r - f->lo) % 2) |
305 | 0 | return r; |
306 | 0 | [[fallthrough]]; |
307 | 0 | case EvenOdd: // even <-> odd |
308 | 0 | if (r%2 == 0) |
309 | 0 | return r + 1; |
310 | 0 | return r - 1; |
311 | | |
312 | 0 | case OddEvenSkip: // odd <-> even but only applies to every other |
313 | 0 | if ((r - f->lo) % 2) |
314 | 0 | return r; |
315 | 0 | [[fallthrough]]; |
316 | 0 | case OddEven: // odd <-> even |
317 | 0 | if (r%2 == 1) |
318 | 0 | return r + 1; |
319 | 0 | return r - 1; |
320 | 0 | } |
321 | 0 | } |
322 | | |
323 | | // Returns the next Rune in r's folding cycle (see unicode_casefold.h). |
324 | | // Examples: |
325 | | // CycleFoldRune('A') = 'a' |
326 | | // CycleFoldRune('a') = 'A' |
327 | | // |
328 | | // CycleFoldRune('K') = 'k' |
329 | | // CycleFoldRune('k') = 0x212A (Kelvin) |
330 | | // CycleFoldRune(0x212A) = 'K' |
331 | | // |
332 | | // CycleFoldRune('?') = '?' |
333 | 0 | Rune CycleFoldRune(Rune r) { |
334 | 0 | const CaseFold* f = LookupCaseFold(unicode_casefold, num_unicode_casefold, r); |
335 | 0 | if (f == NULL || r < f->lo) |
336 | 0 | return r; |
337 | 0 | return ApplyFold(f, r); |
338 | 0 | } |
339 | | |
340 | | // Add lo-hi to the class, along with their fold-equivalent characters. |
341 | 0 | static void AddFoldedRangeLatin1(CharClassBuilder* cc, Rune lo, Rune hi) { |
342 | 0 | while (lo <= hi) { |
343 | 0 | cc->AddRange(lo, lo); |
344 | 0 | if ('A' <= lo && lo <= 'Z') { |
345 | 0 | cc->AddRange(lo - 'A' + 'a', lo - 'A' + 'a'); |
346 | 0 | } |
347 | 0 | if ('a' <= lo && lo <= 'z') { |
348 | 0 | cc->AddRange(lo - 'a' + 'A', lo - 'a' + 'A'); |
349 | 0 | } |
350 | 0 | lo++; |
351 | 0 | } |
352 | 0 | } |
353 | | |
354 | | // Add lo-hi to the class, along with their fold-equivalent characters. |
355 | | // If lo-hi is already in the class, assume that the fold-equivalent |
356 | | // chars are there too, so there's no work to do. |
357 | 0 | static void AddFoldedRange(CharClassBuilder* cc, Rune lo, Rune hi, int depth) { |
358 | | // AddFoldedRange calls itself recursively for each rune in the fold cycle. |
359 | | // Most folding cycles are small: there aren't any bigger than four in the |
360 | | // current Unicode tables. make_unicode_casefold.py checks that |
361 | | // the cycles are not too long, and we double-check here using depth. |
362 | 0 | if (depth > 10) { |
363 | 0 | ABSL_LOG(DFATAL) << "AddFoldedRange recurses too much."; |
364 | 0 | return; |
365 | 0 | } |
366 | | |
367 | 0 | if (!cc->AddRange(lo, hi)) // lo-hi was already there? we're done |
368 | 0 | return; |
369 | | |
370 | 0 | while (lo <= hi) { |
371 | 0 | const CaseFold* f = LookupCaseFold(unicode_casefold, num_unicode_casefold, lo); |
372 | 0 | if (f == NULL) // lo has no fold, nor does anything above lo |
373 | 0 | break; |
374 | 0 | if (lo < f->lo) { // lo has no fold; next rune with a fold is f->lo |
375 | 0 | lo = f->lo; |
376 | 0 | continue; |
377 | 0 | } |
378 | | |
379 | | // Add in the result of folding the range lo - f->hi |
380 | | // and that range's fold, recursively. |
381 | 0 | Rune lo1 = lo; |
382 | 0 | Rune hi1 = std::min<Rune>(hi, f->hi); |
383 | 0 | switch (f->delta) { |
384 | 0 | default: |
385 | 0 | lo1 += f->delta; |
386 | 0 | hi1 += f->delta; |
387 | 0 | break; |
388 | 0 | case EvenOdd: |
389 | 0 | if (lo1%2 == 1) |
390 | 0 | lo1--; |
391 | 0 | if (hi1%2 == 0) |
392 | 0 | hi1++; |
393 | 0 | break; |
394 | 0 | case OddEven: |
395 | 0 | if (lo1%2 == 0) |
396 | 0 | lo1--; |
397 | 0 | if (hi1%2 == 1) |
398 | 0 | hi1++; |
399 | 0 | break; |
400 | 0 | } |
401 | 0 | AddFoldedRange(cc, lo1, hi1, depth+1); |
402 | | |
403 | | // Pick up where this fold left off. |
404 | 0 | lo = f->hi + 1; |
405 | 0 | } |
406 | 0 | } |
407 | | |
408 | | // Pushes the literal rune r onto the stack. |
409 | 0 | bool Regexp::ParseState::PushLiteral(Rune r) { |
410 | | // Do case folding if needed. |
411 | 0 | if (flags_ & FoldCase) { |
412 | 0 | if (flags_ & Latin1 && (('A' <= r && r <= 'Z') || |
413 | 0 | ('a' <= r && r <= 'z'))) { |
414 | 0 | Regexp* re = new Regexp(kRegexpCharClass, flags_ & ~FoldCase); |
415 | 0 | re->ccb_ = new CharClassBuilder; |
416 | 0 | AddFoldedRangeLatin1(re->ccb_, r, r); |
417 | 0 | return PushRegexp(re); |
418 | 0 | } |
419 | 0 | if (!(flags_ & Latin1) && CycleFoldRune(r) != r) { |
420 | 0 | Regexp* re = new Regexp(kRegexpCharClass, flags_ & ~FoldCase); |
421 | 0 | re->ccb_ = new CharClassBuilder; |
422 | 0 | Rune r1 = r; |
423 | 0 | do { |
424 | 0 | if (!(flags_ & NeverNL) || r != '\n') { |
425 | 0 | re->ccb_->AddRange(r, r); |
426 | 0 | } |
427 | 0 | r = CycleFoldRune(r); |
428 | 0 | } while (r != r1); |
429 | 0 | return PushRegexp(re); |
430 | 0 | } |
431 | 0 | } |
432 | | |
433 | | // Exclude newline if applicable. |
434 | 0 | if ((flags_ & NeverNL) && r == '\n') |
435 | 0 | return PushRegexp(new Regexp(kRegexpNoMatch, flags_)); |
436 | | |
437 | | // No fancy stuff worked. Ordinary literal. |
438 | 0 | if (MaybeConcatString(r, flags_)) |
439 | 0 | return true; |
440 | | |
441 | 0 | Regexp* re = new Regexp(kRegexpLiteral, flags_); |
442 | 0 | re->rune_ = r; |
443 | 0 | return PushRegexp(re); |
444 | 0 | } |
445 | | |
446 | | // Pushes a ^ onto the stack. |
447 | 0 | bool Regexp::ParseState::PushCaret() { |
448 | 0 | if (flags_ & OneLine) { |
449 | 0 | return PushSimpleOp(kRegexpBeginText); |
450 | 0 | } |
451 | 0 | return PushSimpleOp(kRegexpBeginLine); |
452 | 0 | } |
453 | | |
454 | | // Pushes a \b or \B onto the stack. |
455 | 0 | bool Regexp::ParseState::PushWordBoundary(bool word) { |
456 | 0 | if (word) |
457 | 0 | return PushSimpleOp(kRegexpWordBoundary); |
458 | 0 | return PushSimpleOp(kRegexpNoWordBoundary); |
459 | 0 | } |
460 | | |
461 | | // Pushes a $ onto the stack. |
462 | 0 | bool Regexp::ParseState::PushDollar() { |
463 | 0 | if (flags_ & OneLine) { |
464 | | // Clumsy marker so that MimicsPCRE() can tell whether |
465 | | // this kRegexpEndText was a $ and not a \z. |
466 | 0 | Regexp::ParseFlags oflags = flags_; |
467 | 0 | flags_ = flags_ | WasDollar; |
468 | 0 | bool ret = PushSimpleOp(kRegexpEndText); |
469 | 0 | flags_ = oflags; |
470 | 0 | return ret; |
471 | 0 | } |
472 | 0 | return PushSimpleOp(kRegexpEndLine); |
473 | 0 | } |
474 | | |
475 | | // Pushes a . onto the stack. |
476 | 0 | bool Regexp::ParseState::PushDot() { |
477 | 0 | if ((flags_ & DotNL) && !(flags_ & NeverNL)) |
478 | 0 | return PushSimpleOp(kRegexpAnyChar); |
479 | | // Rewrite . into [^\n] |
480 | 0 | Regexp* re = new Regexp(kRegexpCharClass, flags_ & ~FoldCase); |
481 | 0 | re->ccb_ = new CharClassBuilder; |
482 | 0 | re->ccb_->AddRange(0, '\n' - 1); |
483 | 0 | re->ccb_->AddRange('\n' + 1, rune_max_); |
484 | 0 | return PushRegexp(re); |
485 | 0 | } |
486 | | |
487 | | // Pushes a regexp with the given op (and no args) onto the stack. |
488 | 0 | bool Regexp::ParseState::PushSimpleOp(RegexpOp op) { |
489 | 0 | Regexp* re = new Regexp(op, flags_); |
490 | 0 | return PushRegexp(re); |
491 | 0 | } |
492 | | |
493 | | // Pushes a repeat operator regexp onto the stack. |
494 | | // A valid argument for the operator must already be on the stack. |
495 | | // The char c is the name of the operator, for use in error messages. |
496 | | bool Regexp::ParseState::PushRepeatOp(RegexpOp op, absl::string_view s, |
497 | 0 | bool nongreedy) { |
498 | 0 | if (stacktop_ == NULL || IsMarker(stacktop_->op())) { |
499 | 0 | status_->set_code(kRegexpRepeatArgument); |
500 | 0 | status_->set_error_arg(s); |
501 | 0 | return false; |
502 | 0 | } |
503 | 0 | Regexp::ParseFlags fl = flags_; |
504 | 0 | if (nongreedy) |
505 | 0 | fl = fl ^ NonGreedy; |
506 | | |
507 | | // Squash **, ++ and ??. Regexp::Star() et al. handle this too, but |
508 | | // they're mostly for use during simplification, not during parsing. |
509 | 0 | if (op == stacktop_->op() && fl == stacktop_->parse_flags()) |
510 | 0 | return true; |
511 | | |
512 | | // Squash *+, *?, +*, +?, ?* and ?+. They all squash to *, so because |
513 | | // op is a repeat, we just have to check that stacktop_->op() is too, |
514 | | // then adjust stacktop_. |
515 | 0 | if ((stacktop_->op() == kRegexpStar || |
516 | 0 | stacktop_->op() == kRegexpPlus || |
517 | 0 | stacktop_->op() == kRegexpQuest) && |
518 | 0 | fl == stacktop_->parse_flags()) { |
519 | 0 | stacktop_->op_ = kRegexpStar; |
520 | 0 | return true; |
521 | 0 | } |
522 | | |
523 | 0 | Regexp* re = new Regexp(op, fl); |
524 | 0 | re->AllocSub(1); |
525 | 0 | re->down_ = stacktop_->down_; |
526 | 0 | re->sub()[0] = FinishRegexp(stacktop_); |
527 | 0 | re->simple_ = re->ComputeSimple(); |
528 | 0 | stacktop_ = re; |
529 | 0 | return true; |
530 | 0 | } |
531 | | |
532 | | // RepetitionWalker reports whether the repetition regexp is valid. |
533 | | // Valid means that the combination of the top-level repetition |
534 | | // and any inner repetitions does not exceed n copies of the |
535 | | // innermost thing. |
536 | | // This rewalks the regexp tree and is called for every repetition, |
537 | | // so we have to worry about inducing quadratic behavior in the parser. |
538 | | // We avoid this by only using RepetitionWalker when min or max >= 2. |
539 | | // In that case the depth of any >= 2 nesting can only get to 9 without |
540 | | // triggering a parse error, so each subtree can only be rewalked 9 times. |
541 | | class RepetitionWalker : public Regexp::Walker<int> { |
542 | | public: |
543 | 0 | RepetitionWalker() {} |
544 | | virtual int PreVisit(Regexp* re, int parent_arg, bool* stop); |
545 | | virtual int PostVisit(Regexp* re, int parent_arg, int pre_arg, |
546 | | int* child_args, int nchild_args); |
547 | | virtual int ShortVisit(Regexp* re, int parent_arg); |
548 | | |
549 | | private: |
550 | | RepetitionWalker(const RepetitionWalker&) = delete; |
551 | | RepetitionWalker& operator=(const RepetitionWalker&) = delete; |
552 | | }; |
553 | | |
554 | 0 | int RepetitionWalker::PreVisit(Regexp* re, int parent_arg, bool* stop) { |
555 | 0 | int arg = parent_arg; |
556 | 0 | if (re->op() == kRegexpRepeat) { |
557 | 0 | int m = re->max(); |
558 | 0 | if (m < 0) { |
559 | 0 | m = re->min(); |
560 | 0 | } |
561 | 0 | if (m > 0) { |
562 | 0 | arg /= m; |
563 | 0 | } |
564 | 0 | } |
565 | 0 | return arg; |
566 | 0 | } |
567 | | |
568 | | int RepetitionWalker::PostVisit(Regexp* re, int parent_arg, int pre_arg, |
569 | 0 | int* child_args, int nchild_args) { |
570 | 0 | int arg = pre_arg; |
571 | 0 | for (int i = 0; i < nchild_args; i++) { |
572 | 0 | if (child_args[i] < arg) { |
573 | 0 | arg = child_args[i]; |
574 | 0 | } |
575 | 0 | } |
576 | 0 | return arg; |
577 | 0 | } |
578 | | |
579 | 0 | int RepetitionWalker::ShortVisit(Regexp* re, int parent_arg) { |
580 | | // Should never be called: we use Walk(), not WalkExponential(). |
581 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
582 | | ABSL_LOG(DFATAL) << "RepetitionWalker::ShortVisit called"; |
583 | | #endif |
584 | 0 | return 0; |
585 | 0 | } |
586 | | |
587 | | // Pushes a repetition regexp onto the stack. |
588 | | // A valid argument for the operator must already be on the stack. |
589 | | bool Regexp::ParseState::PushRepetition(int min, int max, absl::string_view s, |
590 | 0 | bool nongreedy) { |
591 | 0 | if ((max != -1 && max < min) || |
592 | 0 | min > maximum_repeat_count || |
593 | 0 | max > maximum_repeat_count) { |
594 | 0 | status_->set_code(kRegexpRepeatSize); |
595 | 0 | status_->set_error_arg(s); |
596 | 0 | return false; |
597 | 0 | } |
598 | 0 | if (stacktop_ == NULL || IsMarker(stacktop_->op())) { |
599 | 0 | status_->set_code(kRegexpRepeatArgument); |
600 | 0 | status_->set_error_arg(s); |
601 | 0 | return false; |
602 | 0 | } |
603 | 0 | Regexp::ParseFlags fl = flags_; |
604 | 0 | if (nongreedy) |
605 | 0 | fl = fl ^ NonGreedy; |
606 | 0 | Regexp* re = new Regexp(kRegexpRepeat, fl); |
607 | 0 | re->min_ = min; |
608 | 0 | re->max_ = max; |
609 | 0 | re->AllocSub(1); |
610 | 0 | re->down_ = stacktop_->down_; |
611 | 0 | re->sub()[0] = FinishRegexp(stacktop_); |
612 | 0 | re->simple_ = re->ComputeSimple(); |
613 | 0 | stacktop_ = re; |
614 | 0 | if (min >= 2 || max >= 2) { |
615 | 0 | RepetitionWalker w; |
616 | 0 | if (w.Walk(stacktop_, maximum_repeat_count) == 0) { |
617 | 0 | status_->set_code(kRegexpRepeatSize); |
618 | 0 | status_->set_error_arg(s); |
619 | 0 | return false; |
620 | 0 | } |
621 | 0 | } |
622 | 0 | return true; |
623 | 0 | } |
624 | | |
625 | | // Checks whether a particular regexp op is a marker. |
626 | 0 | bool Regexp::ParseState::IsMarker(RegexpOp op) { |
627 | 0 | return op >= kLeftParen; |
628 | 0 | } |
629 | | |
630 | | // Processes a left parenthesis in the input. |
631 | | // Pushes a marker onto the stack. |
632 | 0 | bool Regexp::ParseState::DoLeftParen(absl::string_view name) { |
633 | 0 | Regexp* re = new Regexp(kLeftParen, flags_); |
634 | 0 | re->cap_ = ++ncap_; |
635 | 0 | if (name.data() != NULL) |
636 | 0 | re->name_ = new std::string(name); |
637 | 0 | return PushRegexp(re); |
638 | 0 | } |
639 | | |
640 | | // Pushes a non-capturing marker onto the stack. |
641 | 0 | bool Regexp::ParseState::DoLeftParenNoCapture() { |
642 | 0 | Regexp* re = new Regexp(kLeftParen, flags_); |
643 | 0 | re->cap_ = -1; |
644 | 0 | return PushRegexp(re); |
645 | 0 | } |
646 | | |
647 | | // Processes a vertical bar in the input. |
648 | 0 | bool Regexp::ParseState::DoVerticalBar() { |
649 | 0 | MaybeConcatString(-1, NoParseFlags); |
650 | 0 | DoConcatenation(); |
651 | | |
652 | | // Below the vertical bar is a list to alternate. |
653 | | // Above the vertical bar is a list to concatenate. |
654 | | // We just did the concatenation, so either swap |
655 | | // the result below the vertical bar or push a new |
656 | | // vertical bar on the stack. |
657 | 0 | Regexp* r1; |
658 | 0 | Regexp* r2; |
659 | 0 | if ((r1 = stacktop_) != NULL && |
660 | 0 | (r2 = r1->down_) != NULL && |
661 | 0 | r2->op() == kVerticalBar) { |
662 | 0 | Regexp* r3; |
663 | 0 | if ((r3 = r2->down_) != NULL && |
664 | 0 | (r1->op() == kRegexpAnyChar || r3->op() == kRegexpAnyChar)) { |
665 | | // AnyChar is above or below the vertical bar. Let it subsume |
666 | | // the other when the other is Literal, CharClass or AnyChar. |
667 | 0 | if (r3->op() == kRegexpAnyChar && |
668 | 0 | (r1->op() == kRegexpLiteral || |
669 | 0 | r1->op() == kRegexpCharClass || |
670 | 0 | r1->op() == kRegexpAnyChar)) { |
671 | | // Discard r1. |
672 | 0 | stacktop_ = r2; |
673 | 0 | r1->Decref(); |
674 | 0 | return true; |
675 | 0 | } |
676 | 0 | if (r1->op() == kRegexpAnyChar && |
677 | 0 | (r3->op() == kRegexpLiteral || |
678 | 0 | r3->op() == kRegexpCharClass || |
679 | 0 | r3->op() == kRegexpAnyChar)) { |
680 | | // Rearrange the stack and discard r3. |
681 | 0 | r1->down_ = r3->down_; |
682 | 0 | r2->down_ = r1; |
683 | 0 | stacktop_ = r2; |
684 | 0 | r3->Decref(); |
685 | 0 | return true; |
686 | 0 | } |
687 | 0 | } |
688 | | // Swap r1 below vertical bar (r2). |
689 | 0 | r1->down_ = r2->down_; |
690 | 0 | r2->down_ = r1; |
691 | 0 | stacktop_ = r2; |
692 | 0 | return true; |
693 | 0 | } |
694 | 0 | return PushSimpleOp(kVerticalBar); |
695 | 0 | } |
696 | | |
697 | | // Processes a right parenthesis in the input. |
698 | 0 | bool Regexp::ParseState::DoRightParen() { |
699 | | // Finish the current concatenation and alternation. |
700 | 0 | DoAlternation(); |
701 | | |
702 | | // The stack should be: LeftParen regexp |
703 | | // Remove the LeftParen, leaving the regexp, |
704 | | // parenthesized. |
705 | 0 | Regexp* r1; |
706 | 0 | Regexp* r2; |
707 | 0 | if ((r1 = stacktop_) == NULL || |
708 | 0 | (r2 = r1->down_) == NULL || |
709 | 0 | r2->op() != kLeftParen) { |
710 | 0 | status_->set_code(kRegexpUnexpectedParen); |
711 | 0 | status_->set_error_arg(whole_regexp_); |
712 | 0 | return false; |
713 | 0 | } |
714 | | |
715 | | // Pop off r1, r2. Will Decref or reuse below. |
716 | 0 | stacktop_ = r2->down_; |
717 | | |
718 | | // Restore flags from when paren opened. |
719 | 0 | Regexp* re = r2; |
720 | 0 | flags_ = re->parse_flags(); |
721 | | |
722 | | // Rewrite LeftParen as capture if needed. |
723 | 0 | if (re->cap_ > 0) { |
724 | 0 | re->op_ = kRegexpCapture; |
725 | | // re->cap_ is already set |
726 | 0 | re->AllocSub(1); |
727 | 0 | re->sub()[0] = FinishRegexp(r1); |
728 | 0 | re->simple_ = re->ComputeSimple(); |
729 | 0 | } else { |
730 | 0 | re->Decref(); |
731 | 0 | re = r1; |
732 | 0 | } |
733 | 0 | return PushRegexp(re); |
734 | 0 | } |
735 | | |
736 | | // Processes the end of input, returning the final regexp. |
737 | 0 | Regexp* Regexp::ParseState::DoFinish() { |
738 | 0 | DoAlternation(); |
739 | 0 | Regexp* re = stacktop_; |
740 | 0 | if (re != NULL && re->down_ != NULL) { |
741 | 0 | status_->set_code(kRegexpMissingParen); |
742 | 0 | status_->set_error_arg(whole_regexp_); |
743 | 0 | return NULL; |
744 | 0 | } |
745 | 0 | stacktop_ = NULL; |
746 | 0 | return FinishRegexp(re); |
747 | 0 | } |
748 | | |
749 | | // Returns the leading regexp that re starts with. |
750 | | // The returned Regexp* points into a piece of re, |
751 | | // so it must not be used after the caller calls re->Decref(). |
752 | 0 | Regexp* Regexp::LeadingRegexp(Regexp* re) { |
753 | 0 | if (re->op() == kRegexpEmptyMatch) |
754 | 0 | return NULL; |
755 | 0 | if (re->op() == kRegexpConcat && re->nsub() >= 2) { |
756 | 0 | Regexp** sub = re->sub(); |
757 | 0 | if (sub[0]->op() == kRegexpEmptyMatch) |
758 | 0 | return NULL; |
759 | 0 | return sub[0]; |
760 | 0 | } |
761 | 0 | return re; |
762 | 0 | } |
763 | | |
764 | | // Removes LeadingRegexp(re) from re and returns what's left. |
765 | | // Consumes the reference to re and may edit it in place. |
766 | | // If caller wants to hold on to LeadingRegexp(re), |
767 | | // must have already Incref'ed it. |
768 | 0 | Regexp* Regexp::RemoveLeadingRegexp(Regexp* re) { |
769 | 0 | if (re->op() == kRegexpEmptyMatch) |
770 | 0 | return re; |
771 | 0 | if (re->op() == kRegexpConcat && re->nsub() >= 2) { |
772 | 0 | Regexp** sub = re->sub(); |
773 | 0 | if (sub[0]->op() == kRegexpEmptyMatch) |
774 | 0 | return re; |
775 | 0 | sub[0]->Decref(); |
776 | 0 | sub[0] = NULL; |
777 | 0 | if (re->nsub() == 2) { |
778 | | // Collapse concatenation to single regexp. |
779 | 0 | Regexp* nre = sub[1]; |
780 | 0 | sub[1] = NULL; |
781 | 0 | re->Decref(); |
782 | 0 | return nre; |
783 | 0 | } |
784 | | // 3 or more -> 2 or more. |
785 | 0 | re->nsub_--; |
786 | 0 | memmove(sub, sub + 1, re->nsub_ * sizeof sub[0]); |
787 | 0 | return re; |
788 | 0 | } |
789 | 0 | Regexp::ParseFlags pf = re->parse_flags(); |
790 | 0 | re->Decref(); |
791 | 0 | return new Regexp(kRegexpEmptyMatch, pf); |
792 | 0 | } |
793 | | |
794 | | // Returns the leading string that re starts with. |
795 | | // The returned Rune* points into a piece of re, |
796 | | // so it must not be used after the caller calls re->Decref(). |
797 | | Rune* Regexp::LeadingString(Regexp* re, int* nrune, |
798 | 0 | Regexp::ParseFlags* flags) { |
799 | 0 | while (re->op() == kRegexpConcat && re->nsub() > 0) |
800 | 0 | re = re->sub()[0]; |
801 | |
|
802 | 0 | *flags = static_cast<Regexp::ParseFlags>(re->parse_flags_ & |
803 | 0 | (Regexp::FoldCase | Regexp::Latin1)); |
804 | |
|
805 | 0 | if (re->op() == kRegexpLiteral) { |
806 | 0 | *nrune = 1; |
807 | 0 | return &re->rune_; |
808 | 0 | } |
809 | | |
810 | 0 | if (re->op() == kRegexpLiteralString) { |
811 | 0 | *nrune = re->nrunes_; |
812 | 0 | return re->runes_; |
813 | 0 | } |
814 | | |
815 | 0 | *nrune = 0; |
816 | 0 | return NULL; |
817 | 0 | } |
818 | | |
819 | | // Removes the first n leading runes from the beginning of re. |
820 | | // Edits re in place. |
821 | 0 | void Regexp::RemoveLeadingString(Regexp* re, int n) { |
822 | | // Chase down concats to find first string. |
823 | | // For regexps generated by parser, nested concats are |
824 | | // flattened except when doing so would overflow the 16-bit |
825 | | // limit on the size of a concatenation, so we should never |
826 | | // see more than two here. |
827 | 0 | Regexp* stk[4]; |
828 | 0 | size_t d = 0; |
829 | 0 | while (re->op() == kRegexpConcat) { |
830 | 0 | if (d < ABSL_ARRAYSIZE(stk)) |
831 | 0 | stk[d++] = re; |
832 | 0 | re = re->sub()[0]; |
833 | 0 | } |
834 | | |
835 | | // Remove leading string from re. |
836 | 0 | if (re->op() == kRegexpLiteral) { |
837 | 0 | re->rune_ = 0; |
838 | 0 | re->op_ = kRegexpEmptyMatch; |
839 | 0 | } else if (re->op() == kRegexpLiteralString) { |
840 | 0 | if (n >= re->nrunes_) { |
841 | 0 | delete[] re->runes_; |
842 | 0 | re->runes_ = NULL; |
843 | 0 | re->nrunes_ = 0; |
844 | 0 | re->op_ = kRegexpEmptyMatch; |
845 | 0 | } else if (n == re->nrunes_ - 1) { |
846 | 0 | Rune rune = re->runes_[re->nrunes_ - 1]; |
847 | 0 | delete[] re->runes_; |
848 | 0 | re->runes_ = NULL; |
849 | 0 | re->nrunes_ = 0; |
850 | 0 | re->rune_ = rune; |
851 | 0 | re->op_ = kRegexpLiteral; |
852 | 0 | } else { |
853 | 0 | re->nrunes_ -= n; |
854 | 0 | memmove(re->runes_, re->runes_ + n, re->nrunes_ * sizeof re->runes_[0]); |
855 | 0 | } |
856 | 0 | } |
857 | | |
858 | | // If re is now empty, concatenations might simplify too. |
859 | 0 | while (d > 0) { |
860 | 0 | re = stk[--d]; |
861 | 0 | Regexp** sub = re->sub(); |
862 | 0 | if (sub[0]->op() == kRegexpEmptyMatch) { |
863 | 0 | sub[0]->Decref(); |
864 | 0 | sub[0] = NULL; |
865 | | // Delete first element of concat. |
866 | 0 | switch (re->nsub()) { |
867 | 0 | case 0: |
868 | 0 | case 1: |
869 | | // Impossible. |
870 | 0 | ABSL_LOG(DFATAL) << "Concat of " << re->nsub(); |
871 | 0 | re->submany_ = NULL; |
872 | 0 | re->op_ = kRegexpEmptyMatch; |
873 | 0 | break; |
874 | | |
875 | 0 | case 2: { |
876 | | // Replace re with sub[1]. |
877 | 0 | Regexp* old = sub[1]; |
878 | 0 | sub[1] = NULL; |
879 | 0 | re->Swap(old); |
880 | 0 | old->Decref(); |
881 | 0 | break; |
882 | 0 | } |
883 | | |
884 | 0 | default: |
885 | | // Slide down. |
886 | 0 | re->nsub_--; |
887 | 0 | memmove(sub, sub + 1, re->nsub_ * sizeof sub[0]); |
888 | 0 | break; |
889 | 0 | } |
890 | 0 | } |
891 | 0 | } |
892 | 0 | } |
893 | | |
894 | | // In the context of factoring alternations, a Splice is: a factored prefix or |
895 | | // merged character class computed by one iteration of one round of factoring; |
896 | | // the span of subexpressions of the alternation to be "spliced" (i.e. removed |
897 | | // and replaced); and, for a factored prefix, the number of suffixes after any |
898 | | // factoring that might have subsequently been performed on them. For a merged |
899 | | // character class, there are no suffixes, of course, so the field is ignored. |
900 | | struct Splice { |
901 | | Splice(Regexp* prefix, Regexp** sub, int nsub) |
902 | 0 | : prefix(prefix), |
903 | 0 | sub(sub), |
904 | 0 | nsub(nsub), |
905 | 0 | nsuffix(-1) {} |
906 | | |
907 | | Regexp* prefix; |
908 | | Regexp** sub; |
909 | | int nsub; |
910 | | int nsuffix; |
911 | | }; |
912 | | |
913 | | // Named so because it is used to implement an explicit stack, a Frame is: the |
914 | | // span of subexpressions of the alternation to be factored; the current round |
915 | | // of factoring; any Splices computed; and, for a factored prefix, an iterator |
916 | | // to the next Splice to be factored (i.e. in another Frame) because suffixes. |
917 | | struct Frame { |
918 | | Frame(Regexp** sub, int nsub) |
919 | 0 | : sub(sub), |
920 | 0 | nsub(nsub), |
921 | 0 | round(0) {} |
922 | | |
923 | | Regexp** sub; |
924 | | int nsub; |
925 | | int round; |
926 | | std::vector<Splice> splices; |
927 | | int spliceidx; |
928 | | }; |
929 | | |
930 | | // Bundled into a class for friend access to Regexp without needing to declare |
931 | | // (or define) Splice in regexp.h. |
932 | | class FactorAlternationImpl { |
933 | | public: |
934 | | static void Round1(Regexp** sub, int nsub, |
935 | | Regexp::ParseFlags flags, |
936 | | std::vector<Splice>* splices); |
937 | | static void Round2(Regexp** sub, int nsub, |
938 | | Regexp::ParseFlags flags, |
939 | | std::vector<Splice>* splices); |
940 | | static void Round3(Regexp** sub, int nsub, |
941 | | Regexp::ParseFlags flags, |
942 | | std::vector<Splice>* splices); |
943 | | }; |
944 | | |
945 | | // Factors common prefixes from alternation. |
946 | | // For example, |
947 | | // ABC|ABD|AEF|BCX|BCY |
948 | | // simplifies to |
949 | | // A(B(C|D)|EF)|BC(X|Y) |
950 | | // and thence to |
951 | | // A(B[CD]|EF)|BC[XY] |
952 | | // |
953 | | // Rewrites sub to contain simplified list to alternate and returns |
954 | | // the new length of sub. Adjusts reference counts accordingly |
955 | | // (incoming sub[i] decremented, outgoing sub[i] incremented). |
956 | 0 | int Regexp::FactorAlternation(Regexp** sub, int nsub, ParseFlags flags) { |
957 | 0 | std::vector<Frame> stk; |
958 | 0 | stk.emplace_back(sub, nsub); |
959 | |
|
960 | 0 | for (;;) { |
961 | 0 | auto& sub = stk.back().sub; |
962 | 0 | auto& nsub = stk.back().nsub; |
963 | 0 | auto& round = stk.back().round; |
964 | 0 | auto& splices = stk.back().splices; |
965 | 0 | auto& spliceidx = stk.back().spliceidx; |
966 | |
|
967 | 0 | if (splices.empty()) { |
968 | | // Advance to the next round of factoring. Note that this covers |
969 | | // the initialised state: when splices is empty and round is 0. |
970 | 0 | round++; |
971 | 0 | } else if (spliceidx < static_cast<int>(splices.size())) { |
972 | | // We have at least one more Splice to factor. Recurse logically. |
973 | 0 | stk.emplace_back(splices[spliceidx].sub, splices[spliceidx].nsub); |
974 | 0 | continue; |
975 | 0 | } else { |
976 | | // We have no more Splices to factor. Apply them. |
977 | 0 | auto iter = splices.begin(); |
978 | 0 | int out = 0; |
979 | 0 | for (int i = 0; i < nsub; ) { |
980 | | // Copy until we reach where the next Splice begins. |
981 | 0 | while (sub + i < iter->sub) |
982 | 0 | sub[out++] = sub[i++]; |
983 | 0 | switch (round) { |
984 | 0 | case 1: |
985 | 0 | case 2: { |
986 | | // Assemble the Splice prefix and the suffixes. |
987 | 0 | Regexp* re[2]; |
988 | 0 | re[0] = iter->prefix; |
989 | 0 | re[1] = Regexp::AlternateNoFactor(iter->sub, iter->nsuffix, flags); |
990 | 0 | sub[out++] = Regexp::Concat(re, 2, flags); |
991 | 0 | i += iter->nsub; |
992 | 0 | break; |
993 | 0 | } |
994 | 0 | case 3: |
995 | | // Just use the Splice prefix. |
996 | 0 | sub[out++] = iter->prefix; |
997 | 0 | i += iter->nsub; |
998 | 0 | break; |
999 | 0 | default: |
1000 | 0 | ABSL_LOG(DFATAL) << "unknown round: " << round; |
1001 | 0 | break; |
1002 | 0 | } |
1003 | | // If we are done, copy until the end of sub. |
1004 | 0 | if (++iter == splices.end()) { |
1005 | 0 | while (i < nsub) |
1006 | 0 | sub[out++] = sub[i++]; |
1007 | 0 | } |
1008 | 0 | } |
1009 | 0 | splices.clear(); |
1010 | 0 | nsub = out; |
1011 | | // Advance to the next round of factoring. |
1012 | 0 | round++; |
1013 | 0 | } |
1014 | | |
1015 | 0 | switch (round) { |
1016 | 0 | case 1: |
1017 | 0 | FactorAlternationImpl::Round1(sub, nsub, flags, &splices); |
1018 | 0 | break; |
1019 | 0 | case 2: |
1020 | 0 | FactorAlternationImpl::Round2(sub, nsub, flags, &splices); |
1021 | 0 | break; |
1022 | 0 | case 3: |
1023 | 0 | FactorAlternationImpl::Round3(sub, nsub, flags, &splices); |
1024 | 0 | break; |
1025 | 0 | case 4: |
1026 | 0 | if (stk.size() == 1) { |
1027 | | // We are at the top of the stack. Just return. |
1028 | 0 | return nsub; |
1029 | 0 | } else { |
1030 | | // Pop the stack and set the number of suffixes. |
1031 | | // (Note that references will be invalidated!) |
1032 | 0 | int nsuffix = nsub; |
1033 | 0 | stk.pop_back(); |
1034 | 0 | stk.back().splices[stk.back().spliceidx].nsuffix = nsuffix; |
1035 | 0 | ++stk.back().spliceidx; |
1036 | 0 | continue; |
1037 | 0 | } |
1038 | 0 | default: |
1039 | 0 | ABSL_LOG(DFATAL) << "unknown round: " << round; |
1040 | 0 | break; |
1041 | 0 | } |
1042 | | |
1043 | | // Set spliceidx depending on whether we have Splices to factor. |
1044 | 0 | if (splices.empty() || round == 3) { |
1045 | 0 | spliceidx = static_cast<int>(splices.size()); |
1046 | 0 | } else { |
1047 | 0 | spliceidx = 0; |
1048 | 0 | } |
1049 | 0 | } |
1050 | 0 | } |
1051 | | |
1052 | | void FactorAlternationImpl::Round1(Regexp** sub, int nsub, |
1053 | | Regexp::ParseFlags flags, |
1054 | 0 | std::vector<Splice>* splices) { |
1055 | | // Round 1: Factor out common literal prefixes. |
1056 | 0 | int start = 0; |
1057 | 0 | Rune* rune = NULL; |
1058 | 0 | int nrune = 0; |
1059 | 0 | Regexp::ParseFlags runeflags = Regexp::NoParseFlags; |
1060 | 0 | for (int i = 0; i <= nsub; i++) { |
1061 | | // Invariant: sub[start:i] consists of regexps that all |
1062 | | // begin with rune[0:nrune]. |
1063 | 0 | Rune* rune_i = NULL; |
1064 | 0 | int nrune_i = 0; |
1065 | 0 | Regexp::ParseFlags runeflags_i = Regexp::NoParseFlags; |
1066 | 0 | if (i < nsub) { |
1067 | 0 | rune_i = Regexp::LeadingString(sub[i], &nrune_i, &runeflags_i); |
1068 | 0 | if (runeflags_i == runeflags) { |
1069 | 0 | int same = 0; |
1070 | 0 | while (same < nrune && same < nrune_i && rune[same] == rune_i[same]) |
1071 | 0 | same++; |
1072 | 0 | if (same > 0) { |
1073 | | // Matches at least one rune in current range. Keep going around. |
1074 | 0 | nrune = same; |
1075 | 0 | continue; |
1076 | 0 | } |
1077 | 0 | } |
1078 | 0 | } |
1079 | | |
1080 | | // Found end of a run with common leading literal string: |
1081 | | // sub[start:i] all begin with rune[0:nrune], |
1082 | | // but sub[i] does not even begin with rune[0]. |
1083 | 0 | if (i == start) { |
1084 | | // Nothing to do - first iteration. |
1085 | 0 | } else if (i == start+1) { |
1086 | | // Just one: don't bother factoring. |
1087 | 0 | } else { |
1088 | 0 | Regexp* prefix = Regexp::LiteralString(rune, nrune, runeflags); |
1089 | 0 | for (int j = start; j < i; j++) |
1090 | 0 | Regexp::RemoveLeadingString(sub[j], nrune); |
1091 | 0 | splices->emplace_back(prefix, sub + start, i - start); |
1092 | 0 | } |
1093 | | |
1094 | | // Prepare for next iteration (if there is one). |
1095 | 0 | if (i < nsub) { |
1096 | 0 | start = i; |
1097 | 0 | rune = rune_i; |
1098 | 0 | nrune = nrune_i; |
1099 | 0 | runeflags = runeflags_i; |
1100 | 0 | } |
1101 | 0 | } |
1102 | 0 | } |
1103 | | |
1104 | | void FactorAlternationImpl::Round2(Regexp** sub, int nsub, |
1105 | | Regexp::ParseFlags flags, |
1106 | 0 | std::vector<Splice>* splices) { |
1107 | | // Round 2: Factor out common simple prefixes, |
1108 | | // just the first piece of each concatenation. |
1109 | | // This will be good enough a lot of the time. |
1110 | | // |
1111 | | // Complex subexpressions (e.g. involving quantifiers) |
1112 | | // are not safe to factor because that collapses their |
1113 | | // distinct paths through the automaton, which affects |
1114 | | // correctness in some cases. |
1115 | 0 | int start = 0; |
1116 | 0 | Regexp* first = NULL; |
1117 | 0 | for (int i = 0; i <= nsub; i++) { |
1118 | | // Invariant: sub[start:i] consists of regexps that all |
1119 | | // begin with first. |
1120 | 0 | Regexp* first_i = NULL; |
1121 | 0 | if (i < nsub) { |
1122 | 0 | first_i = Regexp::LeadingRegexp(sub[i]); |
1123 | 0 | if (first != NULL && |
1124 | | // first must be an empty-width op |
1125 | | // OR a char class, any char or any byte |
1126 | | // OR a fixed repeat of a literal, char class, any char or any byte. |
1127 | 0 | (first->op() == kRegexpBeginLine || |
1128 | 0 | first->op() == kRegexpEndLine || |
1129 | 0 | first->op() == kRegexpWordBoundary || |
1130 | 0 | first->op() == kRegexpNoWordBoundary || |
1131 | 0 | first->op() == kRegexpBeginText || |
1132 | 0 | first->op() == kRegexpEndText || |
1133 | 0 | first->op() == kRegexpCharClass || |
1134 | 0 | first->op() == kRegexpAnyChar || |
1135 | 0 | first->op() == kRegexpAnyByte || |
1136 | 0 | (first->op() == kRegexpRepeat && |
1137 | 0 | first->min() == first->max() && |
1138 | 0 | (first->sub()[0]->op() == kRegexpLiteral || |
1139 | 0 | first->sub()[0]->op() == kRegexpCharClass || |
1140 | 0 | first->sub()[0]->op() == kRegexpAnyChar || |
1141 | 0 | first->sub()[0]->op() == kRegexpAnyByte))) && |
1142 | 0 | Regexp::Equal(first, first_i)) |
1143 | 0 | continue; |
1144 | 0 | } |
1145 | | |
1146 | | // Found end of a run with common leading regexp: |
1147 | | // sub[start:i] all begin with first, |
1148 | | // but sub[i] does not. |
1149 | 0 | if (i == start) { |
1150 | | // Nothing to do - first iteration. |
1151 | 0 | } else if (i == start+1) { |
1152 | | // Just one: don't bother factoring. |
1153 | 0 | } else { |
1154 | 0 | Regexp* prefix = first->Incref(); |
1155 | 0 | for (int j = start; j < i; j++) |
1156 | 0 | sub[j] = Regexp::RemoveLeadingRegexp(sub[j]); |
1157 | 0 | splices->emplace_back(prefix, sub + start, i - start); |
1158 | 0 | } |
1159 | | |
1160 | | // Prepare for next iteration (if there is one). |
1161 | 0 | if (i < nsub) { |
1162 | 0 | start = i; |
1163 | 0 | first = first_i; |
1164 | 0 | } |
1165 | 0 | } |
1166 | 0 | } |
1167 | | |
1168 | | void FactorAlternationImpl::Round3(Regexp** sub, int nsub, |
1169 | | Regexp::ParseFlags flags, |
1170 | 0 | std::vector<Splice>* splices) { |
1171 | | // Round 3: Merge runs of literals and/or character classes. |
1172 | 0 | int start = 0; |
1173 | 0 | Regexp* first = NULL; |
1174 | 0 | for (int i = 0; i <= nsub; i++) { |
1175 | | // Invariant: sub[start:i] consists of regexps that all |
1176 | | // are either literals (i.e. runes) or character classes. |
1177 | 0 | Regexp* first_i = NULL; |
1178 | 0 | if (i < nsub) { |
1179 | 0 | first_i = sub[i]; |
1180 | 0 | if (first != NULL && |
1181 | 0 | (first->op() == kRegexpLiteral || |
1182 | 0 | first->op() == kRegexpCharClass) && |
1183 | 0 | (first_i->op() == kRegexpLiteral || |
1184 | 0 | first_i->op() == kRegexpCharClass)) |
1185 | 0 | continue; |
1186 | 0 | } |
1187 | | |
1188 | | // Found end of a run of Literal/CharClass: |
1189 | | // sub[start:i] all are either one or the other, |
1190 | | // but sub[i] is not. |
1191 | 0 | if (i == start) { |
1192 | | // Nothing to do - first iteration. |
1193 | 0 | } else if (i == start+1) { |
1194 | | // Just one: don't bother factoring. |
1195 | 0 | } else { |
1196 | 0 | CharClassBuilder ccb; |
1197 | 0 | for (int j = start; j < i; j++) { |
1198 | 0 | Regexp* re = sub[j]; |
1199 | 0 | if (re->op() == kRegexpCharClass) { |
1200 | 0 | CharClass* cc = re->cc(); |
1201 | 0 | for (CharClass::iterator it = cc->begin(); it != cc->end(); ++it) |
1202 | 0 | ccb.AddRangeFlags(it->lo, it->hi, re->parse_flags()); |
1203 | 0 | } else if (re->op() == kRegexpLiteral) { |
1204 | 0 | if (re->parse_flags() & Regexp::FoldCase) { |
1205 | | // AddFoldedRange() can terminate prematurely if the character class |
1206 | | // already contains the rune. For example, if it contains 'a' and we |
1207 | | // want to add folded 'a', it sees 'a' and stops without adding 'A'. |
1208 | | // To avoid that, we use an empty character class and then merge it. |
1209 | 0 | CharClassBuilder tmp; |
1210 | 0 | tmp.AddRangeFlags(re->rune(), re->rune(), re->parse_flags()); |
1211 | 0 | ccb.AddCharClass(&tmp); |
1212 | 0 | } else { |
1213 | 0 | ccb.AddRangeFlags(re->rune(), re->rune(), re->parse_flags()); |
1214 | 0 | } |
1215 | 0 | } else { |
1216 | 0 | ABSL_LOG(DFATAL) << "RE2: unexpected op: " << re->op() << " " |
1217 | 0 | << re->ToString(); |
1218 | 0 | } |
1219 | 0 | re->Decref(); |
1220 | 0 | } |
1221 | 0 | Regexp* re = Regexp::NewCharClass(ccb.GetCharClass(), flags & ~Regexp::FoldCase); |
1222 | 0 | splices->emplace_back(re, sub + start, i - start); |
1223 | 0 | } |
1224 | | |
1225 | | // Prepare for next iteration (if there is one). |
1226 | 0 | if (i < nsub) { |
1227 | 0 | start = i; |
1228 | 0 | first = first_i; |
1229 | 0 | } |
1230 | 0 | } |
1231 | 0 | } |
1232 | | |
1233 | | // Collapse the regexps on top of the stack, down to the |
1234 | | // first marker, into a new op node (op == kRegexpAlternate |
1235 | | // or op == kRegexpConcat). |
1236 | 0 | void Regexp::ParseState::DoCollapse(RegexpOp op) { |
1237 | | // Scan backward to marker, counting children of composite. |
1238 | 0 | int n = 0; |
1239 | 0 | Regexp* next = NULL; |
1240 | 0 | Regexp* sub; |
1241 | 0 | for (sub = stacktop_; sub != NULL && !IsMarker(sub->op()); sub = next) { |
1242 | 0 | next = sub->down_; |
1243 | 0 | if (sub->op_ == op) |
1244 | 0 | n += sub->nsub_; |
1245 | 0 | else |
1246 | 0 | n++; |
1247 | 0 | } |
1248 | | |
1249 | | // If there's just one child, leave it alone. |
1250 | | // (Concat of one thing is that one thing; alternate of one thing is same.) |
1251 | 0 | if (stacktop_ != NULL && stacktop_->down_ == next) |
1252 | 0 | return; |
1253 | | |
1254 | | // Construct op (alternation or concatenation), flattening op of op. |
1255 | 0 | PODArray<Regexp*> subs(n); |
1256 | 0 | next = NULL; |
1257 | 0 | int i = n; |
1258 | 0 | for (sub = stacktop_; sub != NULL && !IsMarker(sub->op()); sub = next) { |
1259 | 0 | next = sub->down_; |
1260 | 0 | if (sub->op_ == op) { |
1261 | 0 | Regexp** sub_subs = sub->sub(); |
1262 | 0 | for (int k = sub->nsub_ - 1; k >= 0; k--) |
1263 | 0 | subs[--i] = sub_subs[k]->Incref(); |
1264 | 0 | sub->Decref(); |
1265 | 0 | } else { |
1266 | 0 | subs[--i] = FinishRegexp(sub); |
1267 | 0 | } |
1268 | 0 | } |
1269 | |
|
1270 | 0 | Regexp* re = ConcatOrAlternate(op, subs.data(), n, flags_, true); |
1271 | 0 | re->simple_ = re->ComputeSimple(); |
1272 | 0 | re->down_ = next; |
1273 | 0 | stacktop_ = re; |
1274 | 0 | } |
1275 | | |
1276 | | // Finishes the current concatenation, |
1277 | | // collapsing it into a single regexp on the stack. |
1278 | 0 | void Regexp::ParseState::DoConcatenation() { |
1279 | 0 | Regexp* r1 = stacktop_; |
1280 | 0 | if (r1 == NULL || IsMarker(r1->op())) { |
1281 | | // empty concatenation is special case |
1282 | 0 | Regexp* re = new Regexp(kRegexpEmptyMatch, flags_); |
1283 | 0 | PushRegexp(re); |
1284 | 0 | } |
1285 | 0 | DoCollapse(kRegexpConcat); |
1286 | 0 | } |
1287 | | |
1288 | | // Finishes the current alternation, |
1289 | | // collapsing it to a single regexp on the stack. |
1290 | 0 | void Regexp::ParseState::DoAlternation() { |
1291 | 0 | DoVerticalBar(); |
1292 | | // Now stack top is kVerticalBar. |
1293 | 0 | Regexp* r1 = stacktop_; |
1294 | 0 | stacktop_ = r1->down_; |
1295 | 0 | r1->Decref(); |
1296 | 0 | DoCollapse(kRegexpAlternate); |
1297 | 0 | } |
1298 | | |
1299 | | // Incremental conversion of concatenated literals into strings. |
1300 | | // If top two elements on stack are both literal or string, |
1301 | | // collapse into single string. |
1302 | | // Don't walk down the stack -- the parser calls this frequently |
1303 | | // enough that below the bottom two is known to be collapsed. |
1304 | | // Only called when another regexp is about to be pushed |
1305 | | // on the stack, so that the topmost literal is not being considered. |
1306 | | // (Otherwise ab* would turn into (ab)*.) |
1307 | | // If r >= 0, consider pushing a literal r on the stack. |
1308 | | // Return whether that happened. |
1309 | 0 | bool Regexp::ParseState::MaybeConcatString(int r, ParseFlags flags) { |
1310 | 0 | Regexp* re1; |
1311 | 0 | Regexp* re2; |
1312 | 0 | if ((re1 = stacktop_) == NULL || (re2 = re1->down_) == NULL) |
1313 | 0 | return false; |
1314 | | |
1315 | 0 | if (re1->op_ != kRegexpLiteral && re1->op_ != kRegexpLiteralString) |
1316 | 0 | return false; |
1317 | 0 | if (re2->op_ != kRegexpLiteral && re2->op_ != kRegexpLiteralString) |
1318 | 0 | return false; |
1319 | 0 | if ((re1->parse_flags_ & FoldCase) != (re2->parse_flags_ & FoldCase)) |
1320 | 0 | return false; |
1321 | | |
1322 | 0 | if (re2->op_ == kRegexpLiteral) { |
1323 | | // convert into string |
1324 | 0 | Rune rune = re2->rune_; |
1325 | 0 | re2->op_ = kRegexpLiteralString; |
1326 | 0 | re2->nrunes_ = 0; |
1327 | 0 | re2->runes_ = NULL; |
1328 | 0 | re2->AddRuneToString(rune); |
1329 | 0 | } |
1330 | | |
1331 | | // push re1 into re2. |
1332 | 0 | if (re1->op_ == kRegexpLiteral) { |
1333 | 0 | re2->AddRuneToString(re1->rune_); |
1334 | 0 | } else { |
1335 | 0 | for (int i = 0; i < re1->nrunes_; i++) |
1336 | 0 | re2->AddRuneToString(re1->runes_[i]); |
1337 | 0 | re1->nrunes_ = 0; |
1338 | 0 | delete[] re1->runes_; |
1339 | 0 | re1->runes_ = NULL; |
1340 | 0 | } |
1341 | | |
1342 | | // reuse re1 if possible |
1343 | 0 | if (r >= 0) { |
1344 | 0 | re1->op_ = kRegexpLiteral; |
1345 | 0 | re1->rune_ = r; |
1346 | 0 | re1->parse_flags_ = static_cast<uint16_t>(flags); |
1347 | 0 | return true; |
1348 | 0 | } |
1349 | | |
1350 | 0 | stacktop_ = re2; |
1351 | 0 | re1->Decref(); |
1352 | 0 | return false; |
1353 | 0 | } |
1354 | | |
1355 | | // Lexing routines. |
1356 | | |
1357 | | // Parses a decimal integer, storing it in *np. |
1358 | | // Sets *s to span the remainder of the string. |
1359 | 0 | static bool ParseInteger(absl::string_view* s, int* np) { |
1360 | 0 | if (s->empty() || !absl::ascii_isdigit((*s)[0] & 0xFF)) |
1361 | 0 | return false; |
1362 | | // Disallow leading zeros. |
1363 | 0 | if (s->size() >= 2 && (*s)[0] == '0' && absl::ascii_isdigit((*s)[1] & 0xFF)) |
1364 | 0 | return false; |
1365 | 0 | int n = 0; |
1366 | 0 | int c; |
1367 | 0 | while (!s->empty() && absl::ascii_isdigit(c = (*s)[0] & 0xFF)) { |
1368 | | // Avoid overflow. |
1369 | 0 | if (n >= 100000000) |
1370 | 0 | return false; |
1371 | 0 | n = n*10 + c - '0'; |
1372 | 0 | s->remove_prefix(1); // digit |
1373 | 0 | } |
1374 | 0 | *np = n; |
1375 | 0 | return true; |
1376 | 0 | } |
1377 | | |
1378 | | // Parses a repetition suffix like {1,2} or {2} or {2,}. |
1379 | | // Sets *s to span the remainder of the string on success. |
1380 | | // Sets *lo and *hi to the given range. |
1381 | | // In the case of {2,}, the high number is unbounded; |
1382 | | // sets *hi to -1 to signify this. |
1383 | | // {,2} is NOT a valid suffix. |
1384 | | // The Maybe in the name signifies that the regexp parse |
1385 | | // doesn't fail even if ParseRepetition does, so the string_view |
1386 | | // s must NOT be edited unless MaybeParseRepetition returns true. |
1387 | 0 | static bool MaybeParseRepetition(absl::string_view* sp, int* lo, int* hi) { |
1388 | 0 | absl::string_view s = *sp; |
1389 | 0 | if (s.empty() || s[0] != '{') |
1390 | 0 | return false; |
1391 | 0 | s.remove_prefix(1); // '{' |
1392 | 0 | if (!ParseInteger(&s, lo)) |
1393 | 0 | return false; |
1394 | 0 | if (s.empty()) |
1395 | 0 | return false; |
1396 | 0 | if (s[0] == ',') { |
1397 | 0 | s.remove_prefix(1); // ',' |
1398 | 0 | if (s.empty()) |
1399 | 0 | return false; |
1400 | 0 | if (s[0] == '}') { |
1401 | | // {2,} means at least 2 |
1402 | 0 | *hi = -1; |
1403 | 0 | } else { |
1404 | | // {2,4} means 2, 3, or 4. |
1405 | 0 | if (!ParseInteger(&s, hi)) |
1406 | 0 | return false; |
1407 | 0 | } |
1408 | 0 | } else { |
1409 | | // {2} means exactly two |
1410 | 0 | *hi = *lo; |
1411 | 0 | } |
1412 | 0 | if (s.empty() || s[0] != '}') |
1413 | 0 | return false; |
1414 | 0 | s.remove_prefix(1); // '}' |
1415 | 0 | *sp = s; |
1416 | 0 | return true; |
1417 | 0 | } |
1418 | | |
1419 | | // Removes the next Rune from the string_view and stores it in *r. |
1420 | | // Returns number of bytes removed from sp. |
1421 | | // Behaves as though there is a terminating NUL at the end of sp. |
1422 | | // Argument order is backwards from usual Google style |
1423 | | // but consistent with chartorune. |
1424 | | static int StringViewToRune(Rune* r, absl::string_view* sp, |
1425 | 0 | RegexpStatus* status) { |
1426 | | // fullrune() takes int, not size_t. However, it just looks |
1427 | | // at the leading byte and treats any length >= 4 the same. |
1428 | 0 | if (fullrune(sp->data(), static_cast<int>(std::min(size_t{4}, sp->size())))) { |
1429 | 0 | int n = chartorune(r, sp->data()); |
1430 | | // Some copies of chartorune have a bug that accepts |
1431 | | // encodings of values in (10FFFF, 1FFFFF] as valid. |
1432 | | // Those values break the character class algorithm, |
1433 | | // which assumes Runemax is the largest rune. |
1434 | 0 | if (*r > Runemax) { |
1435 | 0 | n = 1; |
1436 | 0 | *r = Runeerror; |
1437 | 0 | } |
1438 | 0 | if (!(n == 1 && *r == Runeerror)) { // no decoding error |
1439 | 0 | sp->remove_prefix(n); |
1440 | 0 | return n; |
1441 | 0 | } |
1442 | 0 | } |
1443 | | |
1444 | 0 | if (status != NULL) { |
1445 | 0 | status->set_code(kRegexpBadUTF8); |
1446 | 0 | status->set_error_arg(absl::string_view()); |
1447 | 0 | } |
1448 | 0 | return -1; |
1449 | 0 | } |
1450 | | |
1451 | | // Returns whether name is valid UTF-8. |
1452 | | // If not, sets status to kRegexpBadUTF8. |
1453 | 0 | static bool IsValidUTF8(absl::string_view s, RegexpStatus* status) { |
1454 | 0 | absl::string_view t = s; |
1455 | 0 | Rune r; |
1456 | 0 | while (!t.empty()) { |
1457 | 0 | if (StringViewToRune(&r, &t, status) < 0) |
1458 | 0 | return false; |
1459 | 0 | } |
1460 | 0 | return true; |
1461 | 0 | } |
1462 | | |
1463 | | // Is c a hex digit? |
1464 | 0 | static int IsHex(int c) { |
1465 | 0 | return ('0' <= c && c <= '9') || |
1466 | 0 | ('A' <= c && c <= 'F') || |
1467 | 0 | ('a' <= c && c <= 'f'); |
1468 | 0 | } |
1469 | | |
1470 | | // Convert hex digit to value. |
1471 | 0 | static int UnHex(int c) { |
1472 | 0 | if ('0' <= c && c <= '9') |
1473 | 0 | return c - '0'; |
1474 | 0 | if ('A' <= c && c <= 'F') |
1475 | 0 | return c - 'A' + 10; |
1476 | 0 | if ('a' <= c && c <= 'f') |
1477 | 0 | return c - 'a' + 10; |
1478 | 0 | ABSL_LOG(DFATAL) << "Bad hex digit " << c; |
1479 | 0 | return 0; |
1480 | 0 | } |
1481 | | |
1482 | | // Parse an escape sequence (e.g., \n, \{). |
1483 | | // Sets *s to span the remainder of the string. |
1484 | | // Sets *rp to the named character. |
1485 | | static bool ParseEscape(absl::string_view* s, Rune* rp, |
1486 | 0 | RegexpStatus* status, int rune_max) { |
1487 | 0 | const char* begin = s->data(); |
1488 | 0 | if (s->empty() || (*s)[0] != '\\') { |
1489 | | // Should not happen - caller always checks. |
1490 | 0 | status->set_code(kRegexpInternalError); |
1491 | 0 | status->set_error_arg(absl::string_view()); |
1492 | 0 | return false; |
1493 | 0 | } |
1494 | 0 | if (s->size() == 1) { |
1495 | 0 | status->set_code(kRegexpTrailingBackslash); |
1496 | 0 | status->set_error_arg(absl::string_view()); |
1497 | 0 | return false; |
1498 | 0 | } |
1499 | 0 | Rune c, c1; |
1500 | 0 | s->remove_prefix(1); // backslash |
1501 | 0 | if (StringViewToRune(&c, s, status) < 0) |
1502 | 0 | return false; |
1503 | 0 | int code; |
1504 | 0 | switch (c) { |
1505 | 0 | default: |
1506 | 0 | if (c < Runeself && !absl::ascii_isalnum(c)) { |
1507 | | // Escaped non-word characters are always themselves. |
1508 | | // PCRE is not quite so rigorous: it accepts things like |
1509 | | // \q, but we don't. We once rejected \_, but too many |
1510 | | // programs and people insist on using it, so allow \_. |
1511 | 0 | *rp = c; |
1512 | 0 | return true; |
1513 | 0 | } |
1514 | 0 | goto BadEscape; |
1515 | | |
1516 | | // Octal escapes. |
1517 | 0 | case '1': |
1518 | 0 | case '2': |
1519 | 0 | case '3': |
1520 | 0 | case '4': |
1521 | 0 | case '5': |
1522 | 0 | case '6': |
1523 | 0 | case '7': |
1524 | | // Single non-zero octal digit is a backreference; not supported. |
1525 | 0 | if (s->empty() || (*s)[0] < '0' || (*s)[0] > '7') |
1526 | 0 | goto BadEscape; |
1527 | 0 | [[fallthrough]]; |
1528 | 0 | case '0': |
1529 | | // consume up to three octal digits; already have one. |
1530 | 0 | code = c - '0'; |
1531 | 0 | if (!s->empty() && '0' <= (c = (*s)[0]) && c <= '7') { |
1532 | 0 | code = code * 8 + c - '0'; |
1533 | 0 | s->remove_prefix(1); // digit |
1534 | 0 | if (!s->empty()) { |
1535 | 0 | c = (*s)[0]; |
1536 | 0 | if ('0' <= c && c <= '7') { |
1537 | 0 | code = code * 8 + c - '0'; |
1538 | 0 | s->remove_prefix(1); // digit |
1539 | 0 | } |
1540 | 0 | } |
1541 | 0 | } |
1542 | 0 | if (code > rune_max) |
1543 | 0 | goto BadEscape; |
1544 | 0 | *rp = code; |
1545 | 0 | return true; |
1546 | | |
1547 | | // Hexadecimal escapes |
1548 | 0 | case 'x': |
1549 | 0 | if (s->empty()) |
1550 | 0 | goto BadEscape; |
1551 | 0 | if (StringViewToRune(&c, s, status) < 0) |
1552 | 0 | return false; |
1553 | 0 | if (c == '{') { |
1554 | | // Any number of digits in braces. |
1555 | | // Update n as we consume the string, so that |
1556 | | // the whole thing gets shown in the error message. |
1557 | | // Perl accepts any text at all; it ignores all text |
1558 | | // after the first non-hex digit. We require only hex digits, |
1559 | | // and at least one. |
1560 | 0 | if (StringViewToRune(&c, s, status) < 0) |
1561 | 0 | return false; |
1562 | 0 | int nhex = 0; |
1563 | 0 | code = 0; |
1564 | 0 | while (IsHex(c)) { |
1565 | 0 | nhex++; |
1566 | 0 | code = code * 16 + UnHex(c); |
1567 | 0 | if (code > rune_max) |
1568 | 0 | goto BadEscape; |
1569 | 0 | if (s->empty()) |
1570 | 0 | goto BadEscape; |
1571 | 0 | if (StringViewToRune(&c, s, status) < 0) |
1572 | 0 | return false; |
1573 | 0 | } |
1574 | 0 | if (c != '}' || nhex == 0) |
1575 | 0 | goto BadEscape; |
1576 | 0 | *rp = code; |
1577 | 0 | return true; |
1578 | 0 | } |
1579 | | // Easy case: two hex digits. |
1580 | 0 | if (s->empty()) |
1581 | 0 | goto BadEscape; |
1582 | 0 | if (StringViewToRune(&c1, s, status) < 0) |
1583 | 0 | return false; |
1584 | 0 | if (!IsHex(c) || !IsHex(c1)) |
1585 | 0 | goto BadEscape; |
1586 | 0 | *rp = UnHex(c) * 16 + UnHex(c1); |
1587 | 0 | return true; |
1588 | | |
1589 | | // C escapes. |
1590 | 0 | case 'n': |
1591 | 0 | *rp = '\n'; |
1592 | 0 | return true; |
1593 | 0 | case 'r': |
1594 | 0 | *rp = '\r'; |
1595 | 0 | return true; |
1596 | 0 | case 't': |
1597 | 0 | *rp = '\t'; |
1598 | 0 | return true; |
1599 | | |
1600 | | // Less common C escapes. |
1601 | 0 | case 'a': |
1602 | 0 | *rp = '\a'; |
1603 | 0 | return true; |
1604 | 0 | case 'f': |
1605 | 0 | *rp = '\f'; |
1606 | 0 | return true; |
1607 | 0 | case 'v': |
1608 | 0 | *rp = '\v'; |
1609 | 0 | return true; |
1610 | | |
1611 | | // This code is disabled to avoid misparsing |
1612 | | // the Perl word-boundary \b as a backspace |
1613 | | // when in POSIX regexp mode. Surprisingly, |
1614 | | // in Perl, \b means word-boundary but [\b] |
1615 | | // means backspace. We don't support that: |
1616 | | // if you want a backspace embed a literal |
1617 | | // backspace character or use \x08. |
1618 | | // |
1619 | | // case 'b': |
1620 | | // *rp = '\b'; |
1621 | | // return true; |
1622 | 0 | } |
1623 | | |
1624 | 0 | BadEscape: |
1625 | | // Unrecognized escape sequence. |
1626 | 0 | status->set_code(kRegexpBadEscape); |
1627 | 0 | status->set_error_arg( |
1628 | 0 | absl::string_view(begin, static_cast<size_t>(s->data() - begin))); |
1629 | 0 | return false; |
1630 | 0 | } |
1631 | | |
1632 | | // Add a range to the character class, but exclude newline if asked. |
1633 | | // Also handle case folding. |
1634 | | void CharClassBuilder::AddRangeFlags( |
1635 | 0 | Rune lo, Rune hi, Regexp::ParseFlags parse_flags) { |
1636 | | |
1637 | | // Take out \n if the flags say so. |
1638 | 0 | bool cutnl = !(parse_flags & Regexp::ClassNL) || |
1639 | 0 | (parse_flags & Regexp::NeverNL); |
1640 | 0 | if (cutnl && lo <= '\n' && '\n' <= hi) { |
1641 | 0 | if (lo < '\n') |
1642 | 0 | AddRangeFlags(lo, '\n' - 1, parse_flags); |
1643 | 0 | if (hi > '\n') |
1644 | 0 | AddRangeFlags('\n' + 1, hi, parse_flags); |
1645 | 0 | return; |
1646 | 0 | } |
1647 | | |
1648 | | // If folding case, add fold-equivalent characters too. |
1649 | 0 | if (parse_flags & Regexp::FoldCase) { |
1650 | 0 | if (parse_flags & Regexp::Latin1) { |
1651 | 0 | AddFoldedRangeLatin1(this, lo, hi); |
1652 | 0 | } else { |
1653 | 0 | AddFoldedRange(this, lo, hi, 0); |
1654 | 0 | } |
1655 | 0 | } else { |
1656 | 0 | AddRange(lo, hi); |
1657 | 0 | } |
1658 | 0 | } |
1659 | | |
1660 | | // Look for a group with the given name. |
1661 | | static const UGroup* LookupGroup(absl::string_view name, |
1662 | 0 | const UGroup* groups, int ngroups) { |
1663 | | // Simple name lookup. |
1664 | 0 | for (int i = 0; i < ngroups; i++) |
1665 | 0 | if (absl::string_view(groups[i].name) == name) |
1666 | 0 | return &groups[i]; |
1667 | 0 | return NULL; |
1668 | 0 | } |
1669 | | |
1670 | | // Look for a POSIX group with the given name (e.g., "[:^alpha:]") |
1671 | 0 | static const UGroup* LookupPosixGroup(absl::string_view name) { |
1672 | 0 | return LookupGroup(name, posix_groups, num_posix_groups); |
1673 | 0 | } |
1674 | | |
1675 | 0 | static const UGroup* LookupPerlGroup(absl::string_view name) { |
1676 | 0 | return LookupGroup(name, perl_groups, num_perl_groups); |
1677 | 0 | } |
1678 | | |
1679 | | #if !defined(RE2_USE_ICU) |
1680 | | // Fake UGroup containing all Runes |
1681 | | static URange16 any16[] = { { 0, 65535 } }; |
1682 | | static URange32 any32[] = { { 65536, Runemax } }; |
1683 | | static UGroup anygroup = { "Any", +1, any16, 1, any32, 1 }; |
1684 | | |
1685 | | // Look for a Unicode group with the given name (e.g., "Han") |
1686 | 0 | static const UGroup* LookupUnicodeGroup(absl::string_view name) { |
1687 | | // Special case: "Any" means any. |
1688 | 0 | if (name == absl::string_view("Any")) |
1689 | 0 | return &anygroup; |
1690 | 0 | return LookupGroup(name, unicode_groups, num_unicode_groups); |
1691 | 0 | } |
1692 | | #endif |
1693 | | |
1694 | | // Add a UGroup or its negation to the character class. |
1695 | | static void AddUGroup(CharClassBuilder* cc, const UGroup* g, int sign, |
1696 | 0 | Regexp::ParseFlags parse_flags) { |
1697 | 0 | if (sign == +1) { |
1698 | 0 | for (int i = 0; i < g->nr16; i++) { |
1699 | 0 | cc->AddRangeFlags(g->r16[i].lo, g->r16[i].hi, parse_flags); |
1700 | 0 | } |
1701 | 0 | for (int i = 0; i < g->nr32; i++) { |
1702 | 0 | cc->AddRangeFlags(g->r32[i].lo, g->r32[i].hi, parse_flags); |
1703 | 0 | } |
1704 | 0 | } else { |
1705 | 0 | if (parse_flags & Regexp::FoldCase) { |
1706 | | // Normally adding a case-folded group means |
1707 | | // adding all the extra fold-equivalent runes too. |
1708 | | // But if we're adding the negation of the group, |
1709 | | // we have to exclude all the runes that are fold-equivalent |
1710 | | // to what's already missing. Too hard, so do in two steps. |
1711 | 0 | CharClassBuilder ccb1; |
1712 | 0 | AddUGroup(&ccb1, g, +1, parse_flags); |
1713 | | // If the flags say to take out \n, put it in, so that negating will take it out. |
1714 | | // Normally AddRangeFlags does this, but we're bypassing AddRangeFlags. |
1715 | 0 | bool cutnl = !(parse_flags & Regexp::ClassNL) || |
1716 | 0 | (parse_flags & Regexp::NeverNL); |
1717 | 0 | if (cutnl) { |
1718 | 0 | ccb1.AddRange('\n', '\n'); |
1719 | 0 | } |
1720 | 0 | ccb1.Negate(); |
1721 | 0 | cc->AddCharClass(&ccb1); |
1722 | 0 | return; |
1723 | 0 | } |
1724 | 0 | int next = 0; |
1725 | 0 | for (int i = 0; i < g->nr16; i++) { |
1726 | 0 | if (next < g->r16[i].lo) |
1727 | 0 | cc->AddRangeFlags(next, g->r16[i].lo - 1, parse_flags); |
1728 | 0 | next = g->r16[i].hi + 1; |
1729 | 0 | } |
1730 | 0 | for (int i = 0; i < g->nr32; i++) { |
1731 | 0 | if (next < g->r32[i].lo) |
1732 | 0 | cc->AddRangeFlags(next, g->r32[i].lo - 1, parse_flags); |
1733 | 0 | next = g->r32[i].hi + 1; |
1734 | 0 | } |
1735 | 0 | if (next <= Runemax) |
1736 | 0 | cc->AddRangeFlags(next, Runemax, parse_flags); |
1737 | 0 | } |
1738 | 0 | } |
1739 | | |
1740 | | // Maybe parse a Perl character class escape sequence. |
1741 | | // Only recognizes the Perl character classes (\d \s \w \D \S \W), |
1742 | | // not the Perl empty-string classes (\b \B \A \Z \z). |
1743 | | // On success, sets *s to span the remainder of the string |
1744 | | // and returns the corresponding UGroup. |
1745 | | // The string_view must *NOT* be edited unless the call succeeds. |
1746 | | const UGroup* MaybeParsePerlCCEscape(absl::string_view* s, |
1747 | 0 | Regexp::ParseFlags parse_flags) { |
1748 | 0 | if (!(parse_flags & Regexp::PerlClasses)) |
1749 | 0 | return NULL; |
1750 | 0 | if (s->size() < 2 || (*s)[0] != '\\') |
1751 | 0 | return NULL; |
1752 | | // Could use StringViewToRune, but there aren't |
1753 | | // any non-ASCII Perl group names. |
1754 | 0 | absl::string_view name(s->data(), 2); |
1755 | 0 | const UGroup* g = LookupPerlGroup(name); |
1756 | 0 | if (g == NULL) |
1757 | 0 | return NULL; |
1758 | 0 | s->remove_prefix(name.size()); |
1759 | 0 | return g; |
1760 | 0 | } |
1761 | | |
1762 | | enum ParseStatus { |
1763 | | kParseOk, // Did some parsing. |
1764 | | kParseError, // Found an error. |
1765 | | kParseNothing, // Decided not to parse. |
1766 | | }; |
1767 | | |
1768 | | // Maybe parses a Unicode character group like \p{Han} or \P{Han} |
1769 | | // (the latter is a negated group). |
1770 | | ParseStatus ParseUnicodeGroup(absl::string_view* s, |
1771 | | Regexp::ParseFlags parse_flags, |
1772 | 0 | CharClassBuilder* cc, RegexpStatus* status) { |
1773 | | // Decide whether to parse. |
1774 | 0 | if (!(parse_flags & Regexp::UnicodeGroups)) |
1775 | 0 | return kParseNothing; |
1776 | 0 | if (s->size() < 2 || (*s)[0] != '\\') |
1777 | 0 | return kParseNothing; |
1778 | 0 | Rune c = (*s)[1]; |
1779 | 0 | if (c != 'p' && c != 'P') |
1780 | 0 | return kParseNothing; |
1781 | | |
1782 | | // Committed to parse. Results: |
1783 | 0 | int sign = +1; // -1 = negated char class |
1784 | 0 | if (c == 'P') |
1785 | 0 | sign = -sign; |
1786 | 0 | absl::string_view seq = *s; // \p{Han} or \pL |
1787 | 0 | absl::string_view name; // Han or L |
1788 | 0 | s->remove_prefix(2); // '\\', 'p' |
1789 | |
|
1790 | 0 | if (!StringViewToRune(&c, s, status)) |
1791 | 0 | return kParseError; |
1792 | 0 | if (c != '{') { |
1793 | | // Name is the bit of string we just skipped over for c. |
1794 | 0 | const char* p = seq.data() + 2; |
1795 | 0 | name = absl::string_view(p, static_cast<size_t>(s->data() - p)); |
1796 | 0 | } else { |
1797 | | // Name is in braces. Look for closing } |
1798 | 0 | size_t end = s->find('}', 0); |
1799 | 0 | if (end == absl::string_view::npos) { |
1800 | 0 | if (!IsValidUTF8(seq, status)) |
1801 | 0 | return kParseError; |
1802 | 0 | status->set_code(kRegexpBadCharRange); |
1803 | 0 | status->set_error_arg(seq); |
1804 | 0 | return kParseError; |
1805 | 0 | } |
1806 | 0 | name = absl::string_view(s->data(), end); // without '}' |
1807 | 0 | s->remove_prefix(end + 1); // with '}' |
1808 | 0 | if (!IsValidUTF8(name, status)) |
1809 | 0 | return kParseError; |
1810 | 0 | } |
1811 | | |
1812 | | // Chop seq where s now begins. |
1813 | 0 | seq = absl::string_view(seq.data(), static_cast<size_t>(s->data() - seq.data())); |
1814 | |
|
1815 | 0 | if (!name.empty() && name[0] == '^') { |
1816 | 0 | sign = -sign; |
1817 | 0 | name.remove_prefix(1); // '^' |
1818 | 0 | } |
1819 | |
|
1820 | 0 | #if !defined(RE2_USE_ICU) |
1821 | | // Look up the group in the RE2 Unicode data. |
1822 | 0 | const UGroup* g = LookupUnicodeGroup(name); |
1823 | 0 | if (g == NULL) { |
1824 | 0 | status->set_code(kRegexpBadCharRange); |
1825 | 0 | status->set_error_arg(seq); |
1826 | 0 | return kParseError; |
1827 | 0 | } |
1828 | | |
1829 | 0 | AddUGroup(cc, g, sign, parse_flags); |
1830 | | #else |
1831 | | // Look up the group in the ICU Unicode data. Because ICU provides full |
1832 | | // Unicode properties support, this could be more than a lookup by name. |
1833 | | ::icu::UnicodeString ustr = ::icu::UnicodeString::fromUTF8( |
1834 | | std::string("\\p{") + std::string(name) + std::string("}")); |
1835 | | UErrorCode uerr = U_ZERO_ERROR; |
1836 | | ::icu::UnicodeSet uset(ustr, uerr); |
1837 | | if (U_FAILURE(uerr)) { |
1838 | | status->set_code(kRegexpBadCharRange); |
1839 | | status->set_error_arg(seq); |
1840 | | return kParseError; |
1841 | | } |
1842 | | |
1843 | | // Convert the UnicodeSet to a URange32 and UGroup that we can add. |
1844 | | int nr = uset.getRangeCount(); |
1845 | | PODArray<URange32> r(nr); |
1846 | | for (int i = 0; i < nr; i++) { |
1847 | | r[i].lo = uset.getRangeStart(i); |
1848 | | r[i].hi = uset.getRangeEnd(i); |
1849 | | } |
1850 | | UGroup g = {"", +1, 0, 0, r.data(), nr}; |
1851 | | AddUGroup(cc, &g, sign, parse_flags); |
1852 | | #endif |
1853 | |
|
1854 | 0 | return kParseOk; |
1855 | 0 | } |
1856 | | |
1857 | | // Parses a character class name like [:alnum:]. |
1858 | | // Sets *s to span the remainder of the string. |
1859 | | // Adds the ranges corresponding to the class to ranges. |
1860 | | static ParseStatus ParseCCName(absl::string_view* s, |
1861 | | Regexp::ParseFlags parse_flags, |
1862 | 0 | CharClassBuilder* cc, RegexpStatus* status) { |
1863 | | // Check begins with [: |
1864 | 0 | const char* p = s->data(); |
1865 | 0 | const char* ep = s->data() + s->size(); |
1866 | 0 | if (ep - p < 2 || p[0] != '[' || p[1] != ':') |
1867 | 0 | return kParseNothing; |
1868 | | |
1869 | | // Look for closing :]. |
1870 | 0 | const char* q; |
1871 | 0 | for (q = p+2; q <= ep-2 && (*q != ':' || *(q+1) != ']'); q++) |
1872 | 0 | ; |
1873 | | |
1874 | | // If no closing :], then ignore. |
1875 | 0 | if (q > ep-2) |
1876 | 0 | return kParseNothing; |
1877 | | |
1878 | | // Got it. Check that it's valid. |
1879 | 0 | q += 2; |
1880 | 0 | absl::string_view name(p, static_cast<size_t>(q - p)); |
1881 | |
|
1882 | 0 | const UGroup* g = LookupPosixGroup(name); |
1883 | 0 | if (g == NULL) { |
1884 | 0 | status->set_code(kRegexpBadCharRange); |
1885 | 0 | status->set_error_arg(name); |
1886 | 0 | return kParseError; |
1887 | 0 | } |
1888 | | |
1889 | 0 | s->remove_prefix(name.size()); |
1890 | 0 | AddUGroup(cc, g, g->sign, parse_flags); |
1891 | 0 | return kParseOk; |
1892 | 0 | } |
1893 | | |
1894 | | // Parses a character inside a character class. |
1895 | | // There are fewer special characters here than in the rest of the regexp. |
1896 | | // Sets *s to span the remainder of the string. |
1897 | | // Sets *rp to the character. |
1898 | | bool Regexp::ParseState::ParseCCCharacter(absl::string_view* s, Rune* rp, |
1899 | | absl::string_view whole_class, |
1900 | 0 | RegexpStatus* status) { |
1901 | 0 | if (s->empty()) { |
1902 | 0 | status->set_code(kRegexpMissingBracket); |
1903 | 0 | status->set_error_arg(whole_class); |
1904 | 0 | return false; |
1905 | 0 | } |
1906 | | |
1907 | | // Allow regular escape sequences even though |
1908 | | // many need not be escaped in this context. |
1909 | 0 | if ((*s)[0] == '\\') |
1910 | 0 | return ParseEscape(s, rp, status, rune_max_); |
1911 | | |
1912 | | // Otherwise take the next rune. |
1913 | 0 | return StringViewToRune(rp, s, status) >= 0; |
1914 | 0 | } |
1915 | | |
1916 | | // Parses a character class character, or, if the character |
1917 | | // is followed by a hyphen, parses a character class range. |
1918 | | // For single characters, rr->lo == rr->hi. |
1919 | | // Sets *s to span the remainder of the string. |
1920 | | // Sets *rp to the character. |
1921 | | bool Regexp::ParseState::ParseCCRange(absl::string_view* s, RuneRange* rr, |
1922 | | absl::string_view whole_class, |
1923 | 0 | RegexpStatus* status) { |
1924 | 0 | absl::string_view os = *s; |
1925 | 0 | if (!ParseCCCharacter(s, &rr->lo, whole_class, status)) |
1926 | 0 | return false; |
1927 | | // [a-] means (a|-), so check for final ]. |
1928 | 0 | if (s->size() >= 2 && (*s)[0] == '-' && (*s)[1] != ']') { |
1929 | 0 | s->remove_prefix(1); // '-' |
1930 | 0 | if (!ParseCCCharacter(s, &rr->hi, whole_class, status)) |
1931 | 0 | return false; |
1932 | 0 | if (rr->hi < rr->lo) { |
1933 | 0 | status->set_code(kRegexpBadCharRange); |
1934 | 0 | status->set_error_arg(absl::string_view( |
1935 | 0 | os.data(), static_cast<size_t>(s->data() - os.data()))); |
1936 | 0 | return false; |
1937 | 0 | } |
1938 | 0 | } else { |
1939 | 0 | rr->hi = rr->lo; |
1940 | 0 | } |
1941 | 0 | return true; |
1942 | 0 | } |
1943 | | |
1944 | | // Parses a possibly-negated character class expression like [^abx-z[:digit:]]. |
1945 | | // Sets *s to span the remainder of the string. |
1946 | | // Sets *out_re to the regexp for the class. |
1947 | | bool Regexp::ParseState::ParseCharClass(absl::string_view* s, Regexp** out_re, |
1948 | 0 | RegexpStatus* status) { |
1949 | 0 | absl::string_view whole_class = *s; |
1950 | 0 | if (s->empty() || (*s)[0] != '[') { |
1951 | | // Caller checked this. |
1952 | 0 | status->set_code(kRegexpInternalError); |
1953 | 0 | status->set_error_arg(absl::string_view()); |
1954 | 0 | return false; |
1955 | 0 | } |
1956 | 0 | bool negated = false; |
1957 | 0 | Regexp* re = new Regexp(kRegexpCharClass, flags_ & ~FoldCase); |
1958 | 0 | re->ccb_ = new CharClassBuilder; |
1959 | 0 | s->remove_prefix(1); // '[' |
1960 | 0 | if (!s->empty() && (*s)[0] == '^') { |
1961 | 0 | s->remove_prefix(1); // '^' |
1962 | 0 | negated = true; |
1963 | 0 | if (!(flags_ & ClassNL) || (flags_ & NeverNL)) { |
1964 | | // If NL can't match implicitly, then pretend |
1965 | | // negated classes include a leading \n. |
1966 | 0 | re->ccb_->AddRange('\n', '\n'); |
1967 | 0 | } |
1968 | 0 | } |
1969 | 0 | bool first = true; // ] is okay as first char in class |
1970 | 0 | while (!s->empty() && ((*s)[0] != ']' || first)) { |
1971 | | // - is only okay unescaped as first or last in class. |
1972 | | // Except that Perl allows - anywhere. |
1973 | 0 | if ((*s)[0] == '-' && !first && !(flags_&PerlX) && |
1974 | 0 | (s->size() == 1 || (*s)[1] != ']')) { |
1975 | 0 | absl::string_view t = *s; |
1976 | 0 | t.remove_prefix(1); // '-' |
1977 | 0 | Rune r; |
1978 | 0 | int n = StringViewToRune(&r, &t, status); |
1979 | 0 | if (n < 0) { |
1980 | 0 | re->Decref(); |
1981 | 0 | return false; |
1982 | 0 | } |
1983 | 0 | status->set_code(kRegexpBadCharRange); |
1984 | 0 | status->set_error_arg(absl::string_view(s->data(), 1+n)); |
1985 | 0 | re->Decref(); |
1986 | 0 | return false; |
1987 | 0 | } |
1988 | 0 | first = false; |
1989 | | |
1990 | | // Look for [:alnum:] etc. |
1991 | 0 | if (s->size() > 2 && (*s)[0] == '[' && (*s)[1] == ':') { |
1992 | 0 | switch (ParseCCName(s, flags_, re->ccb_, status)) { |
1993 | 0 | case kParseOk: |
1994 | 0 | continue; |
1995 | 0 | case kParseError: |
1996 | 0 | re->Decref(); |
1997 | 0 | return false; |
1998 | 0 | case kParseNothing: |
1999 | 0 | break; |
2000 | 0 | } |
2001 | 0 | } |
2002 | | |
2003 | | // Look for Unicode character group like \p{Han} |
2004 | 0 | if (s->size() > 2 && |
2005 | 0 | (*s)[0] == '\\' && |
2006 | 0 | ((*s)[1] == 'p' || (*s)[1] == 'P')) { |
2007 | 0 | switch (ParseUnicodeGroup(s, flags_, re->ccb_, status)) { |
2008 | 0 | case kParseOk: |
2009 | 0 | continue; |
2010 | 0 | case kParseError: |
2011 | 0 | re->Decref(); |
2012 | 0 | return false; |
2013 | 0 | case kParseNothing: |
2014 | 0 | break; |
2015 | 0 | } |
2016 | 0 | } |
2017 | | |
2018 | | // Look for Perl character class symbols (extension). |
2019 | 0 | const UGroup* g = MaybeParsePerlCCEscape(s, flags_); |
2020 | 0 | if (g != NULL) { |
2021 | 0 | AddUGroup(re->ccb_, g, g->sign, flags_); |
2022 | 0 | continue; |
2023 | 0 | } |
2024 | | |
2025 | | // Otherwise assume single character or simple range. |
2026 | 0 | RuneRange rr; |
2027 | 0 | if (!ParseCCRange(s, &rr, whole_class, status)) { |
2028 | 0 | re->Decref(); |
2029 | 0 | return false; |
2030 | 0 | } |
2031 | | // AddRangeFlags is usually called in response to a class like |
2032 | | // \p{Foo} or [[:foo:]]; for those, it filters \n out unless |
2033 | | // Regexp::ClassNL is set. In an explicit range or singleton |
2034 | | // like we just parsed, we do not filter \n out, so set ClassNL |
2035 | | // in the flags. |
2036 | 0 | re->ccb_->AddRangeFlags(rr.lo, rr.hi, flags_ | Regexp::ClassNL); |
2037 | 0 | } |
2038 | 0 | if (s->empty()) { |
2039 | 0 | status->set_code(kRegexpMissingBracket); |
2040 | 0 | status->set_error_arg(whole_class); |
2041 | 0 | re->Decref(); |
2042 | 0 | return false; |
2043 | 0 | } |
2044 | 0 | s->remove_prefix(1); // ']' |
2045 | |
|
2046 | 0 | if (negated) |
2047 | 0 | re->ccb_->Negate(); |
2048 | |
|
2049 | 0 | *out_re = re; |
2050 | 0 | return true; |
2051 | 0 | } |
2052 | | |
2053 | | // Returns whether name is a valid capture name. |
2054 | 0 | static bool IsValidCaptureName(absl::string_view name) { |
2055 | 0 | if (name.empty()) |
2056 | 0 | return false; |
2057 | | |
2058 | | // Historically, we effectively used [0-9A-Za-z_]+ to validate; that |
2059 | | // followed Python 2 except for not restricting the first character. |
2060 | | // As of Python 3, Unicode characters beyond ASCII are also allowed; |
2061 | | // accordingly, we permit the Lu, Ll, Lt, Lm, Lo, Nl, Mn, Mc, Nd and |
2062 | | // Pc categories, but again without restricting the first character. |
2063 | | // Also, Unicode normalization (e.g. NFKC) isn't performed: Python 3 |
2064 | | // performs it for identifiers, but seemingly not for capture names; |
2065 | | // if they start doing that for capture names, we won't follow suit. |
2066 | 0 | static const CharClass* const cc = []() { |
2067 | 0 | CharClassBuilder ccb; |
2068 | 0 | for (absl::string_view group : |
2069 | 0 | {"Lu", "Ll", "Lt", "Lm", "Lo", "Nl", "Mn", "Mc", "Nd", "Pc"}) |
2070 | 0 | AddUGroup(&ccb, LookupGroup(group, unicode_groups, num_unicode_groups), |
2071 | 0 | +1, Regexp::NoParseFlags); |
2072 | 0 | return ccb.GetCharClass(); |
2073 | 0 | }(); |
2074 | |
|
2075 | 0 | absl::string_view t = name; |
2076 | 0 | Rune r; |
2077 | 0 | while (!t.empty()) { |
2078 | 0 | if (StringViewToRune(&r, &t, NULL) < 0) |
2079 | 0 | return false; |
2080 | 0 | if (cc->Contains(r)) |
2081 | 0 | continue; |
2082 | 0 | return false; |
2083 | 0 | } |
2084 | 0 | return true; |
2085 | 0 | } |
2086 | | |
2087 | | // Parses a Perl flag setting or non-capturing group or both, |
2088 | | // like (?i) or (?: or (?i:. Removes from s, updates parse state. |
2089 | | // The caller must check that s begins with "(?". |
2090 | | // Returns true on success. If the Perl flag is not |
2091 | | // well-formed or not supported, sets status_ and returns false. |
2092 | 0 | bool Regexp::ParseState::ParsePerlFlags(absl::string_view* s) { |
2093 | 0 | absl::string_view t = *s; |
2094 | | |
2095 | | // Caller is supposed to check this. |
2096 | 0 | if (!(flags_ & PerlX) || t.size() < 2 || t[0] != '(' || t[1] != '?') { |
2097 | 0 | status_->set_code(kRegexpInternalError); |
2098 | 0 | ABSL_LOG(DFATAL) << "Bad call to ParseState::ParsePerlFlags"; |
2099 | 0 | return false; |
2100 | 0 | } |
2101 | | |
2102 | | // Check for look-around assertions. This is NOT because we support them! ;) |
2103 | | // As per https://github.com/google/re2/issues/468, we really want to report |
2104 | | // kRegexpBadPerlOp (not kRegexpBadNamedCapture) for look-behind assertions. |
2105 | | // Additionally, it would be nice to report not "(?<", but "(?<=" or "(?<!". |
2106 | 0 | if ((t.size() > 3 && (t[2] == '=' || t[2] == '!')) || |
2107 | 0 | (t.size() > 4 && t[2] == '<' && (t[3] == '=' || t[3] == '!'))) { |
2108 | 0 | status_->set_code(kRegexpBadPerlOp); |
2109 | 0 | status_->set_error_arg(absl::string_view(t.data(), t[2] == '<' ? 4 : 3)); |
2110 | 0 | return false; |
2111 | 0 | } |
2112 | | |
2113 | | // Check for named captures, first introduced in Python's regexp library. |
2114 | | // As usual, there are three slightly different syntaxes: |
2115 | | // |
2116 | | // (?P<name>expr) the original, introduced by Python |
2117 | | // (?<name>expr) the .NET alteration, adopted by Perl 5.10 |
2118 | | // (?'name'expr) another .NET alteration, adopted by Perl 5.10 |
2119 | | // |
2120 | | // Perl 5.10 gave in and implemented the Python version too, |
2121 | | // but they claim that the last two are the preferred forms. |
2122 | | // PCRE and languages based on it (specifically, PHP and Ruby) |
2123 | | // support all three as well. EcmaScript 4 uses only the Python form. |
2124 | | // |
2125 | | // In both the open source world (via Code Search) and the |
2126 | | // Google source tree, (?P<name>expr) and (?<name>expr) are the |
2127 | | // dominant forms of named captures and both are supported. |
2128 | 0 | if ((t.size() > 4 && t[2] == 'P' && t[3] == '<') || |
2129 | 0 | (t.size() > 3 && t[2] == '<')) { |
2130 | | // Pull out name. |
2131 | 0 | size_t begin = t[2] == 'P' ? 4 : 3; |
2132 | 0 | size_t end = t.find('>', begin); |
2133 | 0 | if (end == absl::string_view::npos) { |
2134 | 0 | if (!IsValidUTF8(t, status_)) |
2135 | 0 | return false; |
2136 | 0 | status_->set_code(kRegexpBadNamedCapture); |
2137 | 0 | status_->set_error_arg(t); |
2138 | 0 | return false; |
2139 | 0 | } |
2140 | | |
2141 | 0 | absl::string_view capture(t.data(), end+1); |
2142 | 0 | absl::string_view name(t.data()+begin, end-begin); |
2143 | 0 | if (!IsValidUTF8(name, status_)) |
2144 | 0 | return false; |
2145 | 0 | if (!IsValidCaptureName(name)) { |
2146 | 0 | status_->set_code(kRegexpBadNamedCapture); |
2147 | 0 | status_->set_error_arg(capture); |
2148 | 0 | return false; |
2149 | 0 | } |
2150 | | |
2151 | 0 | if (!DoLeftParen(name)) { |
2152 | | // DoLeftParen's failure set status_. |
2153 | 0 | return false; |
2154 | 0 | } |
2155 | | |
2156 | 0 | s->remove_prefix(capture.size()); |
2157 | 0 | return true; |
2158 | 0 | } |
2159 | | |
2160 | 0 | t.remove_prefix(2); // "(?" |
2161 | |
|
2162 | 0 | bool negated = false; |
2163 | 0 | bool sawflags = false; |
2164 | 0 | int nflags = flags_; |
2165 | 0 | Rune c; |
2166 | 0 | for (bool done = false; !done; ) { |
2167 | 0 | if (t.empty()) |
2168 | 0 | goto BadPerlOp; |
2169 | 0 | if (StringViewToRune(&c, &t, status_) < 0) |
2170 | 0 | return false; |
2171 | 0 | switch (c) { |
2172 | 0 | default: |
2173 | 0 | goto BadPerlOp; |
2174 | | |
2175 | | // Parse flags. |
2176 | 0 | case 'i': |
2177 | 0 | sawflags = true; |
2178 | 0 | if (negated) |
2179 | 0 | nflags &= ~FoldCase; |
2180 | 0 | else |
2181 | 0 | nflags |= FoldCase; |
2182 | 0 | break; |
2183 | | |
2184 | 0 | case 'm': // opposite of our OneLine |
2185 | 0 | sawflags = true; |
2186 | 0 | if (negated) |
2187 | 0 | nflags |= OneLine; |
2188 | 0 | else |
2189 | 0 | nflags &= ~OneLine; |
2190 | 0 | break; |
2191 | | |
2192 | 0 | case 's': |
2193 | 0 | sawflags = true; |
2194 | 0 | if (negated) |
2195 | 0 | nflags &= ~DotNL; |
2196 | 0 | else |
2197 | 0 | nflags |= DotNL; |
2198 | 0 | break; |
2199 | | |
2200 | 0 | case 'U': |
2201 | 0 | sawflags = true; |
2202 | 0 | if (negated) |
2203 | 0 | nflags &= ~NonGreedy; |
2204 | 0 | else |
2205 | 0 | nflags |= NonGreedy; |
2206 | 0 | break; |
2207 | | |
2208 | | // Negation |
2209 | 0 | case '-': |
2210 | 0 | if (negated) |
2211 | 0 | goto BadPerlOp; |
2212 | 0 | negated = true; |
2213 | 0 | sawflags = false; |
2214 | 0 | break; |
2215 | | |
2216 | | // Open new group. |
2217 | 0 | case ':': |
2218 | 0 | if (!DoLeftParenNoCapture()) { |
2219 | | // DoLeftParenNoCapture's failure set status_. |
2220 | 0 | return false; |
2221 | 0 | } |
2222 | 0 | done = true; |
2223 | 0 | break; |
2224 | | |
2225 | | // Finish flags. |
2226 | 0 | case ')': |
2227 | 0 | done = true; |
2228 | 0 | break; |
2229 | 0 | } |
2230 | 0 | } |
2231 | | |
2232 | 0 | if (negated && !sawflags) |
2233 | 0 | goto BadPerlOp; |
2234 | | |
2235 | 0 | flags_ = static_cast<Regexp::ParseFlags>(nflags); |
2236 | 0 | *s = t; |
2237 | 0 | return true; |
2238 | | |
2239 | 0 | BadPerlOp: |
2240 | 0 | status_->set_code(kRegexpBadPerlOp); |
2241 | 0 | status_->set_error_arg( |
2242 | 0 | absl::string_view(s->data(), static_cast<size_t>(t.data() - s->data()))); |
2243 | 0 | return false; |
2244 | 0 | } |
2245 | | |
2246 | | // Converts latin1 (assumed to be encoded as Latin1 bytes) |
2247 | | // into UTF8 encoding in string. |
2248 | | // Can't use EncodingUtils::EncodeLatin1AsUTF8 because it is |
2249 | | // deprecated and because it rejects code points 0x80-0x9F. |
2250 | 0 | void ConvertLatin1ToUTF8(absl::string_view latin1, std::string* utf) { |
2251 | 0 | char buf[UTFmax]; |
2252 | |
|
2253 | 0 | utf->clear(); |
2254 | 0 | for (size_t i = 0; i < latin1.size(); i++) { |
2255 | 0 | Rune r = latin1[i] & 0xFF; |
2256 | 0 | int n = runetochar(buf, &r); |
2257 | 0 | utf->append(buf, n); |
2258 | 0 | } |
2259 | 0 | } |
2260 | | |
2261 | | // Parses the regular expression given by s, |
2262 | | // returning the corresponding Regexp tree. |
2263 | | // The caller must Decref the return value when done with it. |
2264 | | // Returns NULL on error. |
2265 | | Regexp* Regexp::Parse(absl::string_view s, ParseFlags global_flags, |
2266 | 0 | RegexpStatus* status) { |
2267 | | // Make status non-NULL (easier on everyone else). |
2268 | 0 | RegexpStatus xstatus; |
2269 | 0 | if (status == NULL) |
2270 | 0 | status = &xstatus; |
2271 | |
|
2272 | 0 | ParseState ps(global_flags, s, status); |
2273 | 0 | absl::string_view t = s; |
2274 | | |
2275 | | // Convert regexp to UTF-8 (easier on the rest of the parser). |
2276 | 0 | if (global_flags & Latin1) { |
2277 | 0 | std::string* tmp = new std::string; |
2278 | 0 | ConvertLatin1ToUTF8(t, tmp); |
2279 | 0 | status->set_tmp(tmp); |
2280 | 0 | t = *tmp; |
2281 | 0 | } |
2282 | |
|
2283 | 0 | if (global_flags & Literal) { |
2284 | | // Special parse loop for literal string. |
2285 | 0 | while (!t.empty()) { |
2286 | 0 | Rune r; |
2287 | 0 | if (StringViewToRune(&r, &t, status) < 0) |
2288 | 0 | return NULL; |
2289 | 0 | if (!ps.PushLiteral(r)) |
2290 | 0 | return NULL; |
2291 | 0 | } |
2292 | 0 | return ps.DoFinish(); |
2293 | 0 | } |
2294 | | |
2295 | 0 | absl::string_view lastunary = absl::string_view(); |
2296 | 0 | while (!t.empty()) { |
2297 | 0 | absl::string_view isunary = absl::string_view(); |
2298 | 0 | switch (t[0]) { |
2299 | 0 | default: { |
2300 | 0 | Rune r; |
2301 | 0 | if (StringViewToRune(&r, &t, status) < 0) |
2302 | 0 | return NULL; |
2303 | 0 | if (!ps.PushLiteral(r)) |
2304 | 0 | return NULL; |
2305 | 0 | break; |
2306 | 0 | } |
2307 | | |
2308 | 0 | case '(': |
2309 | | // "(?" introduces Perl escape. |
2310 | 0 | if ((ps.flags() & PerlX) && (t.size() >= 2 && t[1] == '?')) { |
2311 | | // Flag changes and non-capturing groups. |
2312 | 0 | if (!ps.ParsePerlFlags(&t)) |
2313 | 0 | return NULL; |
2314 | 0 | break; |
2315 | 0 | } |
2316 | 0 | if (ps.flags() & NeverCapture) { |
2317 | 0 | if (!ps.DoLeftParenNoCapture()) |
2318 | 0 | return NULL; |
2319 | 0 | } else { |
2320 | 0 | if (!ps.DoLeftParen(absl::string_view())) |
2321 | 0 | return NULL; |
2322 | 0 | } |
2323 | 0 | t.remove_prefix(1); // '(' |
2324 | 0 | break; |
2325 | | |
2326 | 0 | case '|': |
2327 | 0 | if (!ps.DoVerticalBar()) |
2328 | 0 | return NULL; |
2329 | 0 | t.remove_prefix(1); // '|' |
2330 | 0 | break; |
2331 | | |
2332 | 0 | case ')': |
2333 | 0 | if (!ps.DoRightParen()) |
2334 | 0 | return NULL; |
2335 | 0 | t.remove_prefix(1); // ')' |
2336 | 0 | break; |
2337 | | |
2338 | 0 | case '^': // Beginning of line. |
2339 | 0 | if (!ps.PushCaret()) |
2340 | 0 | return NULL; |
2341 | 0 | t.remove_prefix(1); // '^' |
2342 | 0 | break; |
2343 | | |
2344 | 0 | case '$': // End of line. |
2345 | 0 | if (!ps.PushDollar()) |
2346 | 0 | return NULL; |
2347 | 0 | t.remove_prefix(1); // '$' |
2348 | 0 | break; |
2349 | | |
2350 | 0 | case '.': // Any character (possibly except newline). |
2351 | 0 | if (!ps.PushDot()) |
2352 | 0 | return NULL; |
2353 | 0 | t.remove_prefix(1); // '.' |
2354 | 0 | break; |
2355 | | |
2356 | 0 | case '[': { // Character class. |
2357 | 0 | Regexp* re; |
2358 | 0 | if (!ps.ParseCharClass(&t, &re, status)) |
2359 | 0 | return NULL; |
2360 | 0 | if (!ps.PushRegexp(re)) |
2361 | 0 | return NULL; |
2362 | 0 | break; |
2363 | 0 | } |
2364 | | |
2365 | 0 | case '*': { // Zero or more. |
2366 | 0 | RegexpOp op; |
2367 | 0 | op = kRegexpStar; |
2368 | 0 | goto Rep; |
2369 | 0 | case '+': // One or more. |
2370 | 0 | op = kRegexpPlus; |
2371 | 0 | goto Rep; |
2372 | 0 | case '?': // Zero or one. |
2373 | 0 | op = kRegexpQuest; |
2374 | 0 | goto Rep; |
2375 | 0 | Rep: |
2376 | 0 | absl::string_view opstr = t; |
2377 | 0 | bool nongreedy = false; |
2378 | 0 | t.remove_prefix(1); // '*' or '+' or '?' |
2379 | 0 | if (ps.flags() & PerlX) { |
2380 | 0 | if (!t.empty() && t[0] == '?') { |
2381 | 0 | nongreedy = true; |
2382 | 0 | t.remove_prefix(1); // '?' |
2383 | 0 | } |
2384 | 0 | if (!lastunary.empty()) { |
2385 | | // In Perl it is not allowed to stack repetition operators: |
2386 | | // a** is a syntax error, not a double-star. |
2387 | | // (and a++ means something else entirely, which we don't support!) |
2388 | 0 | status->set_code(kRegexpRepeatOp); |
2389 | 0 | status->set_error_arg(absl::string_view( |
2390 | 0 | lastunary.data(), |
2391 | 0 | static_cast<size_t>(t.data() - lastunary.data()))); |
2392 | 0 | return NULL; |
2393 | 0 | } |
2394 | 0 | } |
2395 | 0 | opstr = absl::string_view(opstr.data(), |
2396 | 0 | static_cast<size_t>(t.data() - opstr.data())); |
2397 | 0 | if (!ps.PushRepeatOp(op, opstr, nongreedy)) |
2398 | 0 | return NULL; |
2399 | 0 | isunary = opstr; |
2400 | 0 | break; |
2401 | 0 | } |
2402 | | |
2403 | 0 | case '{': { // Counted repetition. |
2404 | 0 | int lo, hi; |
2405 | 0 | absl::string_view opstr = t; |
2406 | 0 | if (!MaybeParseRepetition(&t, &lo, &hi)) { |
2407 | | // Treat like a literal. |
2408 | 0 | if (!ps.PushLiteral('{')) |
2409 | 0 | return NULL; |
2410 | 0 | t.remove_prefix(1); // '{' |
2411 | 0 | break; |
2412 | 0 | } |
2413 | 0 | bool nongreedy = false; |
2414 | 0 | if (ps.flags() & PerlX) { |
2415 | 0 | if (!t.empty() && t[0] == '?') { |
2416 | 0 | nongreedy = true; |
2417 | 0 | t.remove_prefix(1); // '?' |
2418 | 0 | } |
2419 | 0 | if (!lastunary.empty()) { |
2420 | | // Not allowed to stack repetition operators. |
2421 | 0 | status->set_code(kRegexpRepeatOp); |
2422 | 0 | status->set_error_arg(absl::string_view( |
2423 | 0 | lastunary.data(), |
2424 | 0 | static_cast<size_t>(t.data() - lastunary.data()))); |
2425 | 0 | return NULL; |
2426 | 0 | } |
2427 | 0 | } |
2428 | 0 | opstr = absl::string_view(opstr.data(), |
2429 | 0 | static_cast<size_t>(t.data() - opstr.data())); |
2430 | 0 | if (!ps.PushRepetition(lo, hi, opstr, nongreedy)) |
2431 | 0 | return NULL; |
2432 | 0 | isunary = opstr; |
2433 | 0 | break; |
2434 | 0 | } |
2435 | | |
2436 | 0 | case '\\': { // Escaped character or Perl sequence. |
2437 | | // \b and \B: word boundary or not |
2438 | 0 | if ((ps.flags() & Regexp::PerlB) && |
2439 | 0 | t.size() >= 2 && (t[1] == 'b' || t[1] == 'B')) { |
2440 | 0 | if (!ps.PushWordBoundary(t[1] == 'b')) |
2441 | 0 | return NULL; |
2442 | 0 | t.remove_prefix(2); // '\\', 'b' |
2443 | 0 | break; |
2444 | 0 | } |
2445 | | |
2446 | 0 | if ((ps.flags() & Regexp::PerlX) && t.size() >= 2) { |
2447 | 0 | if (t[1] == 'A') { |
2448 | 0 | if (!ps.PushSimpleOp(kRegexpBeginText)) |
2449 | 0 | return NULL; |
2450 | 0 | t.remove_prefix(2); // '\\', 'A' |
2451 | 0 | break; |
2452 | 0 | } |
2453 | 0 | if (t[1] == 'z') { |
2454 | 0 | if (!ps.PushSimpleOp(kRegexpEndText)) |
2455 | 0 | return NULL; |
2456 | 0 | t.remove_prefix(2); // '\\', 'z' |
2457 | 0 | break; |
2458 | 0 | } |
2459 | | // Do not recognize \Z, because this library can't |
2460 | | // implement the exact Perl/PCRE semantics. |
2461 | | // (This library treats "(?-m)$" as \z, even though |
2462 | | // in Perl and PCRE it is equivalent to \Z.) |
2463 | | |
2464 | 0 | if (t[1] == 'C') { // \C: any byte [sic] |
2465 | 0 | if (!ps.PushSimpleOp(kRegexpAnyByte)) |
2466 | 0 | return NULL; |
2467 | 0 | t.remove_prefix(2); // '\\', 'C' |
2468 | 0 | break; |
2469 | 0 | } |
2470 | | |
2471 | 0 | if (t[1] == 'Q') { // \Q ... \E: the ... is always literals |
2472 | 0 | t.remove_prefix(2); // '\\', 'Q' |
2473 | 0 | while (!t.empty()) { |
2474 | 0 | if (t.size() >= 2 && t[0] == '\\' && t[1] == 'E') { |
2475 | 0 | t.remove_prefix(2); // '\\', 'E' |
2476 | 0 | break; |
2477 | 0 | } |
2478 | 0 | Rune r; |
2479 | 0 | if (StringViewToRune(&r, &t, status) < 0) |
2480 | 0 | return NULL; |
2481 | 0 | if (!ps.PushLiteral(r)) |
2482 | 0 | return NULL; |
2483 | 0 | } |
2484 | 0 | break; |
2485 | 0 | } |
2486 | 0 | } |
2487 | | |
2488 | 0 | if (t.size() >= 2 && (t[1] == 'p' || t[1] == 'P')) { |
2489 | 0 | Regexp* re = new Regexp(kRegexpCharClass, ps.flags() & ~FoldCase); |
2490 | 0 | re->ccb_ = new CharClassBuilder; |
2491 | 0 | switch (ParseUnicodeGroup(&t, ps.flags(), re->ccb_, status)) { |
2492 | 0 | case kParseOk: |
2493 | 0 | if (!ps.PushRegexp(re)) |
2494 | 0 | return NULL; |
2495 | 0 | goto Break2; |
2496 | 0 | case kParseError: |
2497 | 0 | re->Decref(); |
2498 | 0 | return NULL; |
2499 | 0 | case kParseNothing: |
2500 | 0 | re->Decref(); |
2501 | 0 | break; |
2502 | 0 | } |
2503 | 0 | } |
2504 | | |
2505 | 0 | const UGroup* g = MaybeParsePerlCCEscape(&t, ps.flags()); |
2506 | 0 | if (g != NULL) { |
2507 | 0 | Regexp* re = new Regexp(kRegexpCharClass, ps.flags() & ~FoldCase); |
2508 | 0 | re->ccb_ = new CharClassBuilder; |
2509 | 0 | AddUGroup(re->ccb_, g, g->sign, ps.flags()); |
2510 | 0 | if (!ps.PushRegexp(re)) |
2511 | 0 | return NULL; |
2512 | 0 | break; |
2513 | 0 | } |
2514 | | |
2515 | 0 | Rune r; |
2516 | 0 | if (!ParseEscape(&t, &r, status, ps.rune_max())) |
2517 | 0 | return NULL; |
2518 | 0 | if (!ps.PushLiteral(r)) |
2519 | 0 | return NULL; |
2520 | 0 | break; |
2521 | 0 | } |
2522 | 0 | } |
2523 | 0 | Break2: |
2524 | 0 | lastunary = isunary; |
2525 | 0 | } |
2526 | 0 | return ps.DoFinish(); |
2527 | 0 | } |
2528 | | |
2529 | | } // namespace re2 |