Skip to content

Commit

Permalink
fixup! libnixf: use sync tokens for creating unknown nodes (error rec…
Browse files Browse the repository at this point in the history
…overy)
  • Loading branch information
inclyc committed Jan 24, 2024
1 parent a6dd59c commit 6ae79ed
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions libnixf/src/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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 ?
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -329,7 +328,8 @@ class Parser {
auto StringState = withState(IsIndented ? PS_IndString : PS_String);
std::shared_ptr<InterpolatedParts> Parts = parseStringParts();
ExpectResult ER = expect(QuoteKind);
if (ER.success()) {
if (ER.ok()) {
consume();
return std::make_shared<ExprString>(
RangeTy{Quote.begin(), ER.tok().end()}, std::move(Parts));
}
Expand All @@ -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<Misc>(ER.tok().range());
return std::make_shared<ExprParen>(RangeTy{L.begin(), ER.tok().end()},
std::move(Expr), std::move(LParen),
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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<ExprAttrs>(RangeTy{Begin, LastToken->end()},
Expand Down

0 comments on commit 6ae79ed

Please sign in to comment.