Skip to content

Commit

Permalink
libnixf/Parse: parse expr ? attrpath (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc authored Feb 12, 2024
1 parent db3792e commit 8b596ef
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions libnixf/include/nixf/Basic/NodeKinds.inc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ EXPR(ExprList)
EXPR(ExprLambda)
EXPR(ExprBinOp)
EXPR(ExprUnaryOp)
EXPR(ExprOpHasAttr)
EXPR(ExprIf)
EXPR(ExprAssert)
EXPR(ExprLet)
Expand Down
19 changes: 19 additions & 0 deletions libnixf/include/nixf/Basic/Nodes/Op.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Basic.h"

#include "nixf/Basic/Nodes/Attrs.h"
#include "nixf/Basic/TokenKinds.h"

#include <memory>
Expand Down Expand Up @@ -53,6 +54,24 @@ class ExprBinOp : public ExprOp {
}
};

class ExprOpHasAttr : public ExprOp {
std::unique_ptr<Expr> E;
std::unique_ptr<AttrPath> Path;

public:
ExprOpHasAttr(LexerCursorRange Range, std::unique_ptr<Op> O,
std::unique_ptr<Expr> E, std::unique_ptr<AttrPath> Path)
: ExprOp(NK_ExprOpHasAttr, Range, std::move(O)), E(std::move(E)),
Path(std::move(Path)) {}

[[nodiscard]] Expr *expr() const { return E.get(); }
[[nodiscard]] AttrPath *attrpath() const { return Path.get(); }

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

class ExprUnaryOp : public ExprOp {
std::unique_ptr<Expr> E;

Expand Down
12 changes: 12 additions & 0 deletions libnixf/src/Parse/ParseOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ std::unique_ptr<Expr> Parser::parseExprOpBP(unsigned LeftRBP) {
std::move(Prefix), std::move(RHS));
break;
}
case tok_question: {
// expr_op '?' attrpath
consume();
assert(LastToken && "consume() should have set LastToken");
auto O = std::make_unique<Op>(Tok.range(), Tok.kind());

std::unique_ptr<AttrPath> Path = parseAttrPath();
LexerCursorRange Range{Prefix->lCur(), LastToken->rCur()};
Prefix = std::make_unique<ExprOpHasAttr>(
Range, std::move(O), std::move(Prefix), std::move(Path));
break;
}
default:
return Prefix;
}
Expand Down
29 changes: 29 additions & 0 deletions libnixf/test/Parse/ParseOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,33 @@ TEST(Parser, UnaryOp) {
ASSERT_EQ(UnaryOp.expr()->kind(), Node::NK_ExprSelect);
}

TEST(Parser, OpHasAttr) {
auto Src = R"(a ? b.c.d)"sv;

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

ASSERT_TRUE(AST);

ASSERT_EQ(AST->kind(), Node::NK_ExprOpHasAttr);

ASSERT_EQ(Diags.size(), 0);
}

TEST(Parser, OpHasAttr_empty) {
auto Src = R"(a ?)"sv;

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

ASSERT_TRUE(AST);

ASSERT_EQ(AST->kind(), Node::NK_ExprOpHasAttr);

// libnixf accepts emtpy attrpath while parsing.
ASSERT_EQ(Diags.size(), 0);
}

} // namespace

0 comments on commit 8b596ef

Please sign in to comment.