Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libnixf/Parse: parse expr_let #351

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libnixf/include/nixf/Basic/NodeKinds.inc
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ EXPR(ExprBinOp)
EXPR(ExprUnaryOp)
EXPR(ExprIf)
EXPR(ExprAssert)
EXPR(ExprLet)

#endif // EXPR
27 changes: 27 additions & 0 deletions libnixf/include/nixf/Basic/Nodes/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,31 @@ class ExprAssert : public Expr {
}
};

class ExprLet : public Expr {
// 'let' binds 'in' expr

std::unique_ptr<Misc> KwLet; // 'let', not null
std::unique_ptr<Binds> B;
std::unique_ptr<Misc> KwIn;
std::unique_ptr<Expr> E;

public:
ExprLet(LexerCursorRange Range, std::unique_ptr<Misc> KwLet,
std::unique_ptr<Binds> B, std::unique_ptr<Misc> KwIn,
std::unique_ptr<Expr> E)
: Expr(NK_ExprLet, Range), KwLet(std::move(KwLet)), B(std::move(B)),
KwIn(std::move(KwIn)), E(std::move(E)) {
assert(this->KwLet && "KwLet should not be empty!");
}

[[nodiscard]] Binds *binds() const { return B.get(); }
[[nodiscard]] Expr *expr() const { return E.get(); }
[[nodiscard]] Misc &let() const { return *KwLet; }
[[nodiscard]] Misc *in() const { return KwIn.get(); }

[[nodiscard]] ChildVector children() const override {
return {KwLet.get(), B.get(), KwIn.get(), E.get()};
}
};

} // namespace nixf
42 changes: 42 additions & 0 deletions libnixf/src/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ std::unique_ptr<Expr> Parser::parseExpr() {
return parseExprIf();
case tok_kw_assert:
return parseExprAssert();
case tok_kw_let:
if (peek(1).kind() != tok_l_curly)
return parseExprLet();
break;
default:
break;
}
Expand Down Expand Up @@ -229,4 +233,42 @@ std::unique_ptr<ExprAssert> Parser::parseExprAssert() {
std::move(Cond), std::move(Value));
}

std::unique_ptr<ExprLet> Parser::parseExprLet() {
LexerCursor LCur = lCur();
Token TokLet = peek();
assert(TokLet.kind() == tok_kw_let &&
"first token should be tok_kw_let in parseExprLet()");

auto Let = std::make_unique<Misc>(TokLet.range());

consume(); // 'let'

auto SyncIn = withSync(tok_kw_in);

assert(LastToken && "LastToken should be set after consume()");

auto Binds = parseBinds();

ExpectResult ExpKwIn = expect(tok_kw_in);

if (!ExpKwIn.ok())
// missing 'in'
return std::make_unique<ExprLet>(LexerCursorRange{LCur, LastToken->rCur()},
std::move(Let), std::move(Binds),
/*KwIn=*/nullptr,
/*E=*/nullptr);

auto In = std::make_unique<Misc>(ExpKwIn.tok().range());

consume(); // 'in'

auto E = parseExpr();
if (!E)
diagNullExpr(Diags, LastToken->rCur(), "let ... in");

return std::make_unique<ExprLet>(LexerCursorRange{LCur, LastToken->rCur()},
std::move(Let), std::move(Binds),
std::move(In), std::move(E));
}

} // namespace nixf
6 changes: 6 additions & 0 deletions libnixf/src/Parse/ParserImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class LambdaArg;
class ExprLambda;
class ExprIf;
class ExprAssert;
class ExprLet;

namespace detail {

Expand Down Expand Up @@ -336,6 +337,11 @@ class Parser {
/// \endcode
std::unique_ptr<ExprAssert> parseExprAssert();

/// \code
/// epxr_let : 'let' binds 'in' expr
/// \endcode
std::unique_ptr<ExprLet> parseExprLet();

std::unique_ptr<Expr> parse() { return parseExpr(); }
};

Expand Down
27 changes: 27 additions & 0 deletions libnixf/test/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,31 @@ TEST(Parser, ExprAssert) {
ASSERT_EQ(Assert.value()->kind(), Node::NK_ExprString);
}

TEST(Parser, ExprLet) {
auto Src = R"(let x = 1; in 1)"sv;

std::vector<Diagnostic> Diags;
Parser P(Src, Diags);
auto AST = P.parseExpr();

ASSERT_TRUE(AST);
ASSERT_EQ(AST->kind(), Node::NK_ExprLet);

Binds &B = *static_cast<ExprLet *>(AST.get())->binds();
ASSERT_EQ(B.bindings().size(), 1);
}

TEST(Parser, ExprLet_Binds) {
auto Src = R"(let in 1)"sv;

std::vector<Diagnostic> Diags;
Parser P(Src, Diags);
auto AST = P.parseExpr();

ASSERT_TRUE(AST);
ASSERT_EQ(AST->kind(), Node::NK_ExprLet);

ASSERT_FALSE(static_cast<ExprLet *>(AST.get())->binds());
}

} // namespace
Loading