Commit c4e20786 authored by Toon Verwaest's avatar Toon Verwaest Committed by Commit Bot

[parser] Simplify SourceRangeScope

Change-Id: I44262469469565592c7c3f3cf29638ce65686e6f
Reviewed-on: https://chromium-review.googlesource.com/c/1273078Reviewed-by: 's avatarIgor Sheludko <ishell@chromium.org>
Commit-Queue: Toon Verwaest <verwaest@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56518}
parent 9d53e539
...@@ -74,51 +74,22 @@ struct FormalParametersBase { ...@@ -74,51 +74,22 @@ struct FormalParametersBase {
// Stack-allocated scope to collect source ranges from the parser. // Stack-allocated scope to collect source ranges from the parser.
class SourceRangeScope final { class SourceRangeScope final {
public: public:
enum PositionKind { SourceRangeScope(const Scanner* scanner, SourceRange* range)
POSITION_BEG, : scanner_(scanner), range_(range) {
POSITION_END, range_->start = scanner->peek_location().beg_pos;
PEEK_POSITION_BEG,
PEEK_POSITION_END,
};
SourceRangeScope(Scanner* scanner, SourceRange* range,
PositionKind pre_kind = PEEK_POSITION_BEG,
PositionKind post_kind = POSITION_END)
: scanner_(scanner), range_(range), post_kind_(post_kind) {
range_->start = GetPosition(pre_kind);
DCHECK_NE(range_->start, kNoSourcePosition); DCHECK_NE(range_->start, kNoSourcePosition);
DCHECK_EQ(range_->end, kNoSourcePosition);
} }
~SourceRangeScope() { Finalize(); } ~SourceRangeScope() {
DCHECK_EQ(kNoSourcePosition, range_->end);
const SourceRange& Finalize() { range_->end = scanner_->location().end_pos;
if (is_finalized_) return *range_;
is_finalized_ = true;
range_->end = GetPosition(post_kind_);
DCHECK_NE(range_->end, kNoSourcePosition); DCHECK_NE(range_->end, kNoSourcePosition);
return *range_;
} }
private: private:
int32_t GetPosition(PositionKind kind) { const Scanner* scanner_;
switch (kind) {
case POSITION_BEG:
return scanner_->location().beg_pos;
case POSITION_END:
return scanner_->location().end_pos;
case PEEK_POSITION_BEG:
return scanner_->peek_location().beg_pos;
case PEEK_POSITION_END:
return scanner_->peek_location().end_pos;
default:
UNREACHABLE();
}
}
Scanner* scanner_;
SourceRange* range_; SourceRange* range_;
PositionKind post_kind_;
bool is_finalized_ = false;
DISALLOW_IMPLICIT_CONSTRUCTORS(SourceRangeScope); DISALLOW_IMPLICIT_CONSTRUCTORS(SourceRangeScope);
}; };
...@@ -3127,7 +3098,6 @@ template <typename Impl> ...@@ -3127,7 +3098,6 @@ template <typename Impl>
typename ParserBase<Impl>::ExpressionT typename ParserBase<Impl>::ExpressionT
ParserBase<Impl>::ParseBinaryContinuation(ExpressionT x, int prec, int prec1, ParserBase<Impl>::ParseBinaryContinuation(ExpressionT x, int prec, int prec1,
bool accept_IN, bool* ok) { bool accept_IN, bool* ok) {
SourceRange right_range;
do { do {
// prec1 >= 4 // prec1 >= 4
while (Token::Precedence(peek()) == prec1) { while (Token::Precedence(peek()) == prec1) {
...@@ -3136,14 +3106,18 @@ ParserBase<Impl>::ParseBinaryContinuation(ExpressionT x, int prec, int prec1, ...@@ -3136,14 +3106,18 @@ ParserBase<Impl>::ParseBinaryContinuation(ExpressionT x, int prec, int prec1,
BindingPatternUnexpectedToken(); BindingPatternUnexpectedToken();
ArrowFormalParametersUnexpectedToken(); ArrowFormalParametersUnexpectedToken();
SourceRangeScope right_range_scope(scanner(), &right_range); SourceRange right_range;
Token::Value op = Next(); int pos = peek_position();
int pos = position(); ExpressionT y;
Token::Value op;
const bool is_right_associative = op == Token::EXP; {
const int next_prec = is_right_associative ? prec1 : prec1 + 1; SourceRangeScope right_range_scope(scanner(), &right_range);
ExpressionT y = ParseBinaryExpression(next_prec, accept_IN, CHECK_OK); op = Next();
right_range_scope.Finalize();
const bool is_right_associative = op == Token::EXP;
const int next_prec = is_right_associative ? prec1 : prec1 + 1;
y = ParseBinaryExpression(next_prec, accept_IN, CHECK_OK);
}
ValidateExpression(CHECK_OK); ValidateExpression(CHECK_OK);
// For now we distinguish between comparisons and other binary // For now we distinguish between comparisons and other binary
...@@ -5546,28 +5520,30 @@ typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseSwitchStatement( ...@@ -5546,28 +5520,30 @@ typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseSwitchStatement(
while (peek() != Token::RBRACE) { while (peek() != Token::RBRACE) {
// An empty label indicates the default case. // An empty label indicates the default case.
ExpressionT label = impl()->NullExpression(); ExpressionT label = impl()->NullExpression();
StatementListT statements = impl()->NewStatementList(5);
SourceRange clause_range; SourceRange clause_range;
SourceRangeScope range_scope(scanner(), &clause_range); {
if (Check(Token::CASE)) { SourceRangeScope range_scope(scanner(), &clause_range);
label = ParseExpression(CHECK_OK); if (Check(Token::CASE)) {
} else { label = ParseExpression(CHECK_OK);
Expect(Token::DEFAULT, CHECK_OK); } else {
if (default_seen) { Expect(Token::DEFAULT, CHECK_OK);
ReportMessage(MessageTemplate::kMultipleDefaultsInSwitch); if (default_seen) {
*ok = false; ReportMessage(MessageTemplate::kMultipleDefaultsInSwitch);
return impl()->NullStatement(); *ok = false;
return impl()->NullStatement();
}
default_seen = true;
}
Expect(Token::COLON, CHECK_OK);
while (peek() != Token::CASE && peek() != Token::DEFAULT &&
peek() != Token::RBRACE) {
StatementT stat = ParseStatementListItem(CHECK_OK);
statements->Add(stat, zone());
} }
default_seen = true;
}
Expect(Token::COLON, CHECK_OK);
StatementListT statements = impl()->NewStatementList(5);
while (peek() != Token::CASE && peek() != Token::DEFAULT &&
peek() != Token::RBRACE) {
StatementT stat = ParseStatementListItem(CHECK_OK);
statements->Add(stat, zone());
} }
auto clause = factory()->NewCaseClause(label, statements); auto clause = factory()->NewCaseClause(label, statements);
impl()->RecordCaseClauseSourceRange(clause, range_scope.Finalize()); impl()->RecordCaseClauseSourceRange(clause, clause_range);
switch_statement->cases()->Add(clause, zone()); switch_statement->cases()->Add(clause, zone());
} }
Expect(Token::RBRACE, CHECK_OK); Expect(Token::RBRACE, CHECK_OK);
...@@ -5855,10 +5831,12 @@ ParserBase<Impl>::ParseForEachStatementWithDeclarations( ...@@ -5855,10 +5831,12 @@ ParserBase<Impl>::ParseForEachStatementWithDeclarations(
&scope_, inner_block_scope != nullptr ? inner_block_scope : scope_); &scope_, inner_block_scope != nullptr ? inner_block_scope : scope_);
SourceRange body_range; SourceRange body_range;
SourceRangeScope range_scope(scanner(), &body_range); StatementT body = impl()->NullStatement();
{
StatementT body = ParseStatement(nullptr, nullptr, CHECK_OK); SourceRangeScope range_scope(scanner(), &body_range);
impl()->RecordIterationStatementSourceRange(loop, range_scope.Finalize()); body = ParseStatement(nullptr, nullptr, CHECK_OK);
}
impl()->RecordIterationStatementSourceRange(loop, body_range);
impl()->DesugarBindingInForEachStatement(for_info, &body_block, impl()->DesugarBindingInForEachStatement(for_info, &body_block,
&each_variable, CHECK_OK); &each_variable, CHECK_OK);
...@@ -5921,13 +5899,12 @@ ParserBase<Impl>::ParseForEachStatementWithoutDeclarations( ...@@ -5921,13 +5899,12 @@ ParserBase<Impl>::ParseForEachStatementWithoutDeclarations(
Expect(Token::RPAREN, CHECK_OK); Expect(Token::RPAREN, CHECK_OK);
StatementT body = impl()->NullStatement(); StatementT body = impl()->NullStatement();
SourceRange body_range;
{ {
SourceRange body_range;
SourceRangeScope range_scope(scanner(), &body_range); SourceRangeScope range_scope(scanner(), &body_range);
body = ParseStatement(nullptr, nullptr, CHECK_OK); body = ParseStatement(nullptr, nullptr, CHECK_OK);
impl()->RecordIterationStatementSourceRange(loop, range_scope.Finalize());
} }
impl()->RecordIterationStatementSourceRange(loop, body_range);
return impl()->InitializeForEachStatement(loop, expression, enumerable, body); return impl()->InitializeForEachStatement(loop, expression, enumerable, body);
} }
...@@ -6130,11 +6107,12 @@ typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseForAwaitStatement( ...@@ -6130,11 +6107,12 @@ typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseForAwaitStatement(
scope()->set_start_position(scanner()->location().beg_pos); scope()->set_start_position(scanner()->location().beg_pos);
SourceRange body_range; SourceRange body_range;
SourceRangeScope range_scope(scanner(), &body_range); {
SourceRangeScope range_scope(scanner(), &body_range);
body = ParseStatement(nullptr, nullptr, CHECK_OK); body = ParseStatement(nullptr, nullptr, CHECK_OK);
scope()->set_end_position(end_position()); scope()->set_end_position(end_position());
impl()->RecordIterationStatementSourceRange(loop, range_scope.Finalize()); }
impl()->RecordIterationStatementSourceRange(loop, body_range);
if (has_declarations) { if (has_declarations) {
BlockT body_block = impl()->NullStatement(); BlockT body_block = impl()->NullStatement();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment