From 4d6abe9ac71c792b9c5468ee1f87ea5126697dd2 Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Tue, 23 Jan 2024 23:27:26 +0800 Subject: [PATCH] libnixf: assert & use LastToken for attrs error recovery --- libnixf/src/Parse/Parser.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/libnixf/src/Parse/Parser.cpp b/libnixf/src/Parse/Parser.cpp index 6bfa7e416..8c15cd805 100644 --- a/libnixf/src/Parse/Parser.cpp +++ b/libnixf/src/Parse/Parser.cpp @@ -445,11 +445,11 @@ class Parser { consume(); } else { // expected "{" for attrset - Point InsertPoint = LastToken ? LastToken->end() : peek().begin(); - Diagnostic &D = - Diags.emplace_back(Diagnostic::DK_Expected, RangeTy(InsertPoint)); + assert(LastToken && "LastToken should be set after valid rec"); + Diagnostic &D = Diags.emplace_back(Diagnostic::DK_Expected, + RangeTy(LastToken->range())); D << std::string(tok::spelling(tok_l_curly)); - D.fix("insert {").edit(TextEdit::mkInsertion(InsertPoint, "{")); + D.fix("insert {").edit(TextEdit::mkInsertion(LastToken->end(), "{")); } assert(LastToken && "LastToken should be set after valid { or rec"); auto Binds = parseBinds(); @@ -457,13 +457,12 @@ class Parser { consume(); } else { // expected "}" for attrset - Point InsertPoint = LastToken ? LastToken->end() : peek().begin(); - Diagnostic &D = - Diags.emplace_back(Diagnostic::DK_Expected, RangeTy(InsertPoint)); + Diagnostic &D = Diags.emplace_back(Diagnostic::DK_Expected, + RangeTy(LastToken->range())); D << std::string(tok::spelling(tok_r_curly)); D.note(Note::NK_ToMachThis, Matcher.range()) << std::string(tok::spelling(Matcher.kind())); - D.fix("insert }").edit(TextEdit::mkInsertion(InsertPoint, "}")); + D.fix("insert }").edit(TextEdit::mkInsertion(LastToken->end(), "}")); } return std::make_shared(RangeTy{Begin, LastToken->end()}, std::move(Binds), std::move(Rec));