OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_PARSER_H_ | 5 #ifndef V8_PARSER_H_ |
6 #define V8_PARSER_H_ | 6 #define V8_PARSER_H_ |
7 | 7 |
8 #include "src/allocation.h" | 8 #include "src/allocation.h" |
9 #include "src/ast.h" | 9 #include "src/ast.h" |
10 #include "src/compiler.h" // TODO(titzer): remove this include dependency | 10 #include "src/compiler.h" // TODO(titzer): remove this include dependency |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 RegExpTree* ReportError(Vector<const char> message); | 443 RegExpTree* ReportError(Vector<const char> message); |
444 void Advance(); | 444 void Advance(); |
445 void Advance(int dist); | 445 void Advance(int dist); |
446 void Reset(int pos); | 446 void Reset(int pos); |
447 | 447 |
448 // Reports whether the pattern might be used as a literal search string. | 448 // Reports whether the pattern might be used as a literal search string. |
449 // Only use if the result of the parse is a single atom node. | 449 // Only use if the result of the parse is a single atom node. |
450 bool simple(); | 450 bool simple(); |
451 bool contains_anchor() { return contains_anchor_; } | 451 bool contains_anchor() { return contains_anchor_; } |
452 void set_contains_anchor() { contains_anchor_ = true; } | 452 void set_contains_anchor() { contains_anchor_ = true; } |
453 int captures_started() { return captures_started_; } | 453 int captures_started() { return captures_ == NULL ? 0 : captures_->length(); } |
454 int position() { return next_pos_ - 1; } | 454 int position() { return next_pos_ - 1; } |
455 bool failed() { return failed_; } | 455 bool failed() { return failed_; } |
456 | 456 |
457 static bool IsSyntaxCharacter(uc32 c); | 457 static bool IsSyntaxCharacter(uc32 c); |
458 | 458 |
459 static const int kMaxCaptures = 1 << 16; | 459 static const int kMaxCaptures = 1 << 16; |
460 static const uc32 kEndMarker = (1 << 21); | 460 static const uc32 kEndMarker = (1 << 21); |
461 | 461 |
462 private: | 462 private: |
463 enum SubexpressionType { | 463 enum SubexpressionType { |
464 INITIAL, | 464 INITIAL, |
465 CAPTURE, // All positive values represent captures. | 465 CAPTURE, // All positive values represent captures. |
466 POSITIVE_LOOKAROUND, | 466 POSITIVE_LOOKAHEAD, |
467 NEGATIVE_LOOKAROUND, | 467 NEGATIVE_LOOKAHEAD, |
468 GROUPING | 468 GROUPING |
469 }; | 469 }; |
470 | 470 |
471 class RegExpParserState : public ZoneObject { | 471 class RegExpParserState : public ZoneObject { |
472 public: | 472 public: |
473 RegExpParserState(RegExpParserState* previous_state, | 473 RegExpParserState(RegExpParserState* previous_state, |
474 SubexpressionType group_type, | 474 SubexpressionType group_type, |
475 RegExpLookaround::Type lookaround_type, | 475 int disjunction_capture_index, |
476 int disjunction_capture_index, Zone* zone) | 476 Zone* zone) |
477 : previous_state_(previous_state), | 477 : previous_state_(previous_state), |
478 builder_(new (zone) RegExpBuilder(zone)), | 478 builder_(new(zone) RegExpBuilder(zone)), |
479 group_type_(group_type), | 479 group_type_(group_type), |
480 lookaround_type_(lookaround_type), | |
481 disjunction_capture_index_(disjunction_capture_index) {} | 480 disjunction_capture_index_(disjunction_capture_index) {} |
482 // Parser state of containing expression, if any. | 481 // Parser state of containing expression, if any. |
483 RegExpParserState* previous_state() { return previous_state_; } | 482 RegExpParserState* previous_state() { return previous_state_; } |
484 bool IsSubexpression() { return previous_state_ != NULL; } | 483 bool IsSubexpression() { return previous_state_ != NULL; } |
485 // RegExpBuilder building this regexp's AST. | 484 // RegExpBuilder building this regexp's AST. |
486 RegExpBuilder* builder() { return builder_; } | 485 RegExpBuilder* builder() { return builder_; } |
487 // Type of regexp being parsed (parenthesized group or entire regexp). | 486 // Type of regexp being parsed (parenthesized group or entire regexp). |
488 SubexpressionType group_type() { return group_type_; } | 487 SubexpressionType group_type() { return group_type_; } |
489 // Lookahead or Lookbehind. | |
490 RegExpLookaround::Type lookaround_type() { return lookaround_type_; } | |
491 // Index in captures array of first capture in this sub-expression, if any. | 488 // Index in captures array of first capture in this sub-expression, if any. |
492 // Also the capture index of this sub-expression itself, if group_type | 489 // Also the capture index of this sub-expression itself, if group_type |
493 // is CAPTURE. | 490 // is CAPTURE. |
494 int capture_index() { return disjunction_capture_index_; } | 491 int capture_index() { return disjunction_capture_index_; } |
495 | 492 |
496 // Check whether the parser is inside a capture group with the given index. | |
497 bool IsInsideCaptureGroup(int index); | |
498 | |
499 private: | 493 private: |
500 // Linked list implementation of stack of states. | 494 // Linked list implementation of stack of states. |
501 RegExpParserState* previous_state_; | 495 RegExpParserState* previous_state_; |
502 // Builder for the stored disjunction. | 496 // Builder for the stored disjunction. |
503 RegExpBuilder* builder_; | 497 RegExpBuilder* builder_; |
504 // Stored disjunction type (capture, look-ahead or grouping), if any. | 498 // Stored disjunction type (capture, look-ahead or grouping), if any. |
505 SubexpressionType group_type_; | 499 SubexpressionType group_type_; |
506 // Stored read direction. | |
507 RegExpLookaround::Type lookaround_type_; | |
508 // Stored disjunction's capture index (if any). | 500 // Stored disjunction's capture index (if any). |
509 int disjunction_capture_index_; | 501 int disjunction_capture_index_; |
510 }; | 502 }; |
511 | 503 |
512 // Return the 1-indexed RegExpCapture object, allocate if necessary. | |
513 RegExpCapture* GetCapture(int index); | |
514 | |
515 Isolate* isolate() { return isolate_; } | 504 Isolate* isolate() { return isolate_; } |
516 Zone* zone() const { return zone_; } | 505 Zone* zone() const { return zone_; } |
517 | 506 |
518 uc32 current() { return current_; } | 507 uc32 current() { return current_; } |
519 bool has_more() { return has_more_; } | 508 bool has_more() { return has_more_; } |
520 bool has_next() { return next_pos_ < in()->length(); } | 509 bool has_next() { return next_pos_ < in()->length(); } |
521 uc32 Next(); | 510 uc32 Next(); |
522 FlatStringReader* in() { return in_; } | 511 FlatStringReader* in() { return in_; } |
523 void ScanForCaptures(); | 512 void ScanForCaptures(); |
524 | 513 |
525 Isolate* isolate_; | 514 Isolate* isolate_; |
526 Zone* zone_; | 515 Zone* zone_; |
527 Handle<String>* error_; | 516 Handle<String>* error_; |
528 ZoneList<RegExpCapture*>* captures_; | 517 ZoneList<RegExpCapture*>* captures_; |
529 FlatStringReader* in_; | 518 FlatStringReader* in_; |
530 uc32 current_; | 519 uc32 current_; |
531 int next_pos_; | 520 int next_pos_; |
532 int captures_started_; | |
533 // The capture count is only valid after we have scanned for captures. | 521 // The capture count is only valid after we have scanned for captures. |
534 int capture_count_; | 522 int capture_count_; |
535 bool has_more_; | 523 bool has_more_; |
536 bool multiline_; | 524 bool multiline_; |
537 bool unicode_; | 525 bool unicode_; |
538 bool simple_; | 526 bool simple_; |
539 bool contains_anchor_; | 527 bool contains_anchor_; |
540 bool is_scanned_for_captures_; | 528 bool is_scanned_for_captures_; |
541 bool failed_; | 529 bool failed_; |
542 }; | 530 }; |
(...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1396 | 1384 |
1397 DoExpression* ParserTraits::ParseDoExpression(bool* ok) { | 1385 DoExpression* ParserTraits::ParseDoExpression(bool* ok) { |
1398 return parser_->ParseDoExpression(ok); | 1386 return parser_->ParseDoExpression(ok); |
1399 } | 1387 } |
1400 | 1388 |
1401 | 1389 |
1402 } // namespace internal | 1390 } // namespace internal |
1403 } // namespace v8 | 1391 } // namespace v8 |
1404 | 1392 |
1405 #endif // V8_PARSER_H_ | 1393 #endif // V8_PARSER_H_ |
OLD | NEW |