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_assert #350

Merged
merged 3 commits into from
Feb 11, 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 @@ -40,5 +40,6 @@ EXPR(ExprLambda)
EXPR(ExprBinOp)
EXPR(ExprUnaryOp)
EXPR(ExprIf)
EXPR(ExprAssert)

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

class ExprAssert : public Expr {
std::unique_ptr<Expr> Cond;
std::unique_ptr<Expr> Value; // If "cond" is true, then "value" is returned.

public:
ExprAssert(LexerCursorRange Range, std::unique_ptr<Expr> Cond,
std::unique_ptr<Expr> Value)
: Expr(NK_ExprAssert, Range), Cond(std::move(Cond)),
Value(std::move(Value)) {}

[[nodiscard]] Expr *cond() const { return Cond.get(); }
[[nodiscard]] Expr *value() const { return Value.get(); }

[[nodiscard]] ChildVector children() const override {
return {Cond.get(), Value.get()};
}
};

} // namespace nixf
45 changes: 45 additions & 0 deletions libnixf/src/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ std::unique_ptr<Expr> Parser::parseExpr() {
} // case tok_l_curly
case tok_kw_if:
return parseExprIf();
case tok_kw_assert:
return parseExprAssert();
default:
break;
}
Expand Down Expand Up @@ -184,4 +186,47 @@ std::unique_ptr<ExprIf> Parser::parseExprIf() {
std::move(Else));
}

std::unique_ptr<ExprAssert> Parser::parseExprAssert() {
LexerCursor LCur = lCur();
Token TokAssert = peek();
assert(TokAssert.kind() == tok_kw_assert && "should be tok_kw_assert");
consume(); // assert
assert(LastToken && "LastToken should be set after consume()");

auto SyncSemi = withSync(tok_semi_colon);

auto Cond = parseExpr();
if (!Cond) {
Diagnostic &D = diagNullExpr(Diags, LastToken->rCur(), "condition");
D.fix("remove `assert` keyword")
.edit(TextEdit::mkRemoval(TokAssert.range()));

if (peek().kind() != tok_colon)
return std::make_unique<ExprAssert>(
LexerCursorRange{LCur, LastToken->rCur()}, std::move(Cond),
/*Value=*/nullptr);
}

ExpectResult ExpSemi = expect(tok_semi_colon);
if (!ExpSemi.ok()) {
// missing ';'
Diagnostic &D = ExpSemi.diag();
Note &N = D.note(Note::NK_ToMachThis, TokAssert.range());
N << std::string(tok::spelling(tok_kw_assert));
return std::make_unique<ExprAssert>(
LexerCursorRange{LCur, LastToken->rCur()}, std::move(Cond),
/*Value=*/nullptr);
}

consume(); // ;

auto Value = parseExpr();

if (!Value)
diagNullExpr(Diags, LastToken->rCur(), "assert value");

return std::make_unique<ExprAssert>(LexerCursorRange{LCur, LastToken->rCur()},
std::move(Cond), std::move(Value));
}

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

namespace detail {

Expand Down Expand Up @@ -329,6 +330,12 @@ class Parser {
/// expr_if : 'if' expr 'then' expr 'else' expr
/// \endcode
std::unique_ptr<ExprIf> parseExprIf();

/// \code
/// expr_assert : 'assert' expr ';' expr
/// \endcode
std::unique_ptr<ExprAssert> parseExprAssert();

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

Expand Down
20 changes: 20 additions & 0 deletions libnixf/test/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,24 @@ TEST(Parser, ExprIf) {
ASSERT_EQ(If.elseExpr()->kind(), Node::NK_ExprString);
}

TEST(Parser, ExprAssert) {
// assert ... ; ...
auto Src = R"(assert 1; "d")"sv;

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

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

ASSERT_TRUE(AST->lCur().isAt(0, 0, 0));
ASSERT_TRUE(AST->rCur().isAt(0, 13, 13));

ExprAssert &Assert = *static_cast<ExprAssert *>(AST.get());

ASSERT_EQ(Assert.cond()->kind(), Node::NK_ExprInt);
ASSERT_EQ(Assert.value()->kind(), Node::NK_ExprString);
}

} // namespace
Loading