From 6ae79eddb92fa55c4fee6249cf984f756efccb5d Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Wed, 24 Jan 2024 13:26:20 +0800 Subject: [PATCH] fixup! libnixf: use sync tokens for creating unknown nodes (error recovery) --- libnixf/src/Parse/Parser.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/libnixf/src/Parse/Parser.cpp b/libnixf/src/Parse/Parser.cpp index 1cc8f79ac..79e8a8df1 100644 --- a/libnixf/src/Parse/Parser.cpp +++ b/libnixf/src/Parse/Parser.cpp @@ -125,8 +125,7 @@ class Parser { Point Begin = peek().begin(); bool Consumed = false; for (Token Tok = peek(); Tok.kind() != tok_eof; Tok = peek()) { - if (std::find(SyncTokens.begin(), SyncTokens.end(), Tok.kind()) != - SyncTokens.end()) + if (SyncTokens.contains(Tok.kind())) break; Consumed = true; consume(); @@ -160,7 +159,7 @@ class Parser { ExpectResult(Diagnostic *DiagMissing) : Success(false), DiagMissing(DiagMissing) {} - [[nodiscard]] bool success() const { return Success; } + [[nodiscard]] bool ok() const { return Success; } [[nodiscard]] Token tok() const { assert(Tok); return *Tok; @@ -174,7 +173,6 @@ class Parser { ExpectResult expect(TokenKind Kind) { auto Sync = withSync(Kind); if (Token Tok = peek(); Tok.kind() == Kind) { - consume(); return Tok; } // UNKNOWN ? @@ -185,7 +183,6 @@ class Parser { D.fix("remove unexpected text").edit(TextEdit::mkRemoval(*UnknownRange)); if (Token Tok = peek(); Tok.kind() == Kind) { - consume(); return Tok; } // If the next token is not the expected one, then insert it. @@ -233,10 +230,12 @@ class Parser { auto Expr = parseExpr(); if (!Expr) diagNullExpr(Diags, LastToken->end(), "interpolation"); - ExpectResult ER = expect(tok_r_curly); // } - if (!ER.success()) + if (ExpectResult ER = expect(tok_r_curly); ER.ok()) { + consume(); // } + } else { ER.diag().note(Note::NK_ToMachThis, TokDollarCurly.range()) << std::string(tok::spelling(tok_dollar_curly)); + } return Expr; } // with(PS_Expr) return nullptr; @@ -329,7 +328,8 @@ class Parser { auto StringState = withState(IsIndented ? PS_IndString : PS_String); std::shared_ptr Parts = parseStringParts(); ExpectResult ER = expect(QuoteKind); - if (ER.success()) { + if (ER.ok()) { + consume(); return std::make_shared( RangeTy{Quote.begin(), ER.tok().end()}, std::move(Parts)); } @@ -356,7 +356,8 @@ class Parser { if (!Expr) diagNullExpr(Diags, LastToken->end(), "parenthesized"); ExpectResult ER = expect(tok_r_paren); // ) - if (ER.success()) { + if (ER.ok()) { + consume(); auto RParen = std::make_shared(ER.tok().range()); return std::make_shared(RangeTy{L.begin(), ER.tok().end()}, std::move(Expr), std::move(LParen), @@ -434,7 +435,8 @@ class Parser { assert(LastToken && "LastToken should be set after valid attrpath"); auto SyncEq = withSync(tok_eq); auto SyncSemi = withSync(tok_semi_colon); - expect(tok_eq); + if (ExpectResult ER = expect(tok_eq); ER.ok()) + consume(); auto Expr = parseExpr(); if (!Expr) diagNullExpr(Diags, LastToken->end(), "binding"); @@ -504,8 +506,9 @@ class Parser { } assert(LastToken && "LastToken should be set after valid { or rec"); auto Binds = parseBinds(); - ExpectResult ER = expect(tok_r_curly); - if (!ER.success()) + if (ExpectResult ER = expect(tok_r_curly); ER.ok()) + consume(); + else ER.diag().note(Note::NK_ToMachThis, Matcher.range()) << std::string(tok::spelling(Matcher.kind())); return std::make_shared(RangeTy{Begin, LastToken->end()},