mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Unify more invalid declaration recovery. (#3942)
When we expect a semicolon, we should be able to provide more standard
recovery.
Note this affects a test of `base`, but it looks like a partial
improvement (the prior line was moving too far). Still, it looks like
recovery isn't handling `{}` quite right. I'm not trying to address that
here though -- leaving a TODO.
This commit is contained in:
@@ -402,6 +402,33 @@ auto Context::ConsumeListToken(NodeKind comma_kind, Lex::TokenKind close_kind,
|
||||
}
|
||||
}
|
||||
|
||||
auto Context::AddNodeExpectingDeclSemi(StateStackEntry state,
|
||||
NodeKind node_kind,
|
||||
Lex::TokenKind decl_kind,
|
||||
bool is_def_allowed) -> void {
|
||||
// TODO: This could better handle things like:
|
||||
// base: { }
|
||||
// var n: i32;
|
||||
// ^ Ends up at `n`, instead of `var`.
|
||||
if (state.has_error) {
|
||||
RecoverFromDeclError(state, node_kind,
|
||||
/*skip_past_likely_end=*/true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (auto semi = ConsumeIf(Lex::TokenKind::Semi)) {
|
||||
AddNode(node_kind, *semi, state.subtree_start, /*has_error=*/false);
|
||||
} else {
|
||||
if (is_def_allowed) {
|
||||
DiagnoseExpectedDeclSemiOrDefinition(decl_kind);
|
||||
} else {
|
||||
DiagnoseExpectedDeclSemi(decl_kind);
|
||||
}
|
||||
RecoverFromDeclError(state, node_kind,
|
||||
/*skip_past_likely_end=*/true);
|
||||
}
|
||||
}
|
||||
|
||||
auto Context::RecoverFromDeclError(StateStackEntry state, NodeKind node_kind,
|
||||
bool skip_past_likely_end) -> void {
|
||||
auto token = state.token;
|
||||
|
||||
Reference in New Issue
Block a user