diff --git a/toolchain/parse/context.cpp b/toolchain/parse/context.cpp index 3bcdfa0bd134..2e1e201f88e0 100644 --- a/toolchain/parse/context.cpp +++ b/toolchain/parse/context.cpp @@ -68,10 +68,9 @@ auto Context::ConsumeAndAddOpenParen(Lex::TokenIndex default_token, } } -auto Context::ConsumeAndAddCloseSymbol(Lex::TokenIndex expected_open, - State state, NodeKind close_kind) +auto Context::ConsumeAndAddCloseSymbol(State state, NodeKind close_kind) -> void { - Lex::TokenKind open_token_kind = tokens().GetKind(expected_open); + Lex::TokenKind open_token_kind = tokens().GetKind(state.token); if (!open_token_kind.is_opening_symbol()) { AddNode(close_kind, state.token, /*has_error=*/true); @@ -85,7 +84,7 @@ auto Context::ConsumeAndAddCloseSymbol(Lex::TokenIndex expected_open, emitter_.Emit(*position_, ExpectedCloseSymbol, open_token_kind.closing_symbol()); - SkipTo(tokens().GetMatchedClosingToken(expected_open)); + SkipTo(tokens().GetMatchedClosingToken(state.token)); AddNode(close_kind, Consume(), /*has_error=*/true); } } diff --git a/toolchain/parse/context.h b/toolchain/parse/context.h index f9ec61b00d56..e09d36efe87b 100644 --- a/toolchain/parse/context.h +++ b/toolchain/parse/context.h @@ -196,13 +196,12 @@ class Context { NodeKind start_kind) -> std::optional; - // Parses a closing symbol corresponding to the opening symbol - // `expected_open`, possibly skipping forward and diagnosing if necessary. - // Creates a parse node of the specified close kind. If `expected_open` is not - // an opening symbol, the parse node will be associated with `state.token`, - // no input will be consumed, and no diagnostic will be emitted. - auto ConsumeAndAddCloseSymbol(Lex::TokenIndex expected_open, State state, - NodeKind close_kind) -> void; + // Parses a closing symbol corresponding to the opening symbol `state.token`, + // possibly skipping forward and diagnosing if necessary. Creates a parse node + // of the specified close kind. If `state.token` is not an opening symbol, + // no input will be consumed, and no diagnostic will be emitted, but the parse + // node will still be marked as having an error. + auto ConsumeAndAddCloseSymbol(State state, NodeKind close_kind) -> void; // Composes `ConsumeIf` and `AddLeafNode`, returning false when ConsumeIf // fails. diff --git a/toolchain/parse/handle_array_expr.cpp b/toolchain/parse/handle_array_expr.cpp index 345c7f2a2c2b..27905269134e 100644 --- a/toolchain/parse/handle_array_expr.cpp +++ b/toolchain/parse/handle_array_expr.cpp @@ -41,8 +41,7 @@ auto HandleArrayExprComma(Context& context) -> void { auto HandleArrayExprFinish(Context& context) -> void { auto state = context.PopState(); - context.ConsumeAndAddCloseSymbol(*(Lex::TokenIterator(state.token)), state, - NodeKind::ArrayExpr); + context.ConsumeAndAddCloseSymbol(state, NodeKind::ArrayExpr); } } // namespace Carbon::Parse diff --git a/toolchain/parse/handle_form_literal.cpp b/toolchain/parse/handle_form_literal.cpp index e69639720dad..936c1d0056f6 100644 --- a/toolchain/parse/handle_form_literal.cpp +++ b/toolchain/parse/handle_form_literal.cpp @@ -72,7 +72,7 @@ auto HandlePrimitiveFormFinish(Context& context) -> void { auto HandleFormLiteralFinish(Context& context) -> void { auto state = context.PopState(); - context.ConsumeAndAddCloseSymbol(state.token, state, NodeKind::FormLiteral); + context.ConsumeAndAddCloseSymbol(state, NodeKind::FormLiteral); } } // namespace Carbon::Parse diff --git a/toolchain/parse/handle_index_expr.cpp b/toolchain/parse/handle_index_expr.cpp index 856456adf5cd..3fbef2ae3b91 100644 --- a/toolchain/parse/handle_index_expr.cpp +++ b/toolchain/parse/handle_index_expr.cpp @@ -20,7 +20,7 @@ auto HandleIndexExpr(Context& context) -> void { auto HandleIndexExprFinish(Context& context) -> void { auto state = context.PopState(); - context.ConsumeAndAddCloseSymbol(state.token, state, NodeKind::IndexExpr); + context.ConsumeAndAddCloseSymbol(state, NodeKind::IndexExpr); } } // namespace Carbon::Parse diff --git a/toolchain/parse/handle_paren_condition.cpp b/toolchain/parse/handle_paren_condition.cpp index 40f9ab499011..0a9f4896f7b8 100644 --- a/toolchain/parse/handle_paren_condition.cpp +++ b/toolchain/parse/handle_paren_condition.cpp @@ -49,21 +49,19 @@ auto HandleParenConditionAsMatch(Context& context) -> void { auto HandleParenConditionFinishAsIf(Context& context) -> void { auto state = context.PopState(); - context.ConsumeAndAddCloseSymbol(state.token, state, NodeKind::IfCondition); + context.ConsumeAndAddCloseSymbol(state, NodeKind::IfCondition); } auto HandleParenConditionFinishAsWhile(Context& context) -> void { auto state = context.PopState(); - context.ConsumeAndAddCloseSymbol(state.token, state, - NodeKind::WhileCondition); + context.ConsumeAndAddCloseSymbol(state, NodeKind::WhileCondition); } auto HandleParenConditionFinishAsMatch(Context& context) -> void { auto state = context.PopState(); - context.ConsumeAndAddCloseSymbol(state.token, state, - NodeKind::MatchCondition); + context.ConsumeAndAddCloseSymbol(state, NodeKind::MatchCondition); } } // namespace Carbon::Parse diff --git a/toolchain/parse/handle_statement.cpp b/toolchain/parse/handle_statement.cpp index 317bf2884dcf..3a1a4d755319 100644 --- a/toolchain/parse/handle_statement.cpp +++ b/toolchain/parse/handle_statement.cpp @@ -156,7 +156,7 @@ auto HandleStatementForHeaderIn(Context& context) -> void { auto HandleStatementForHeaderFinish(Context& context) -> void { auto state = context.PopState(); - context.ConsumeAndAddCloseSymbol(state.token, state, NodeKind::ForHeader); + context.ConsumeAndAddCloseSymbol(state, NodeKind::ForHeader); context.PushState(StateKind::CodeBlock); }