Skip to content

Commit

Permalink
libnixf: parse variable (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc authored Jan 23, 2024
1 parent b0d28d2 commit fbdbeed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions libnixf/include/nixf/Parse/Nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Node {
NK_BeginExpr,
NK_ExprInt,
NK_ExprFloat,
NK_ExprVar,
NK_ExprString,
NK_ExprPath,
NK_ExprParen,
Expand Down Expand Up @@ -188,6 +189,15 @@ class Identifier : public Node {
[[nodiscard]] const std::string &name() const { return Name; }
};

class ExprVar : public Expr {
std::shared_ptr<Identifier> ID;

public:
ExprVar(RangeTy Range, std::shared_ptr<Identifier> ID)
: Expr(NK_ExprVar, Range), ID(std::move(ID)) {}
[[nodiscard]] const std::shared_ptr<Identifier> &id() const { return ID; }
};

class AttrName : public Node {
public:
enum AttrNameKind { ANK_ID, ANK_String, ANK_Interpolation };
Expand Down
7 changes: 7 additions & 0 deletions libnixf/src/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ class Parser {
}

/// expr_simple : INT
/// | ID
/// | FLOAT
/// | string
/// | indented_string
Expand All @@ -481,6 +482,12 @@ class Parser {
std::shared_ptr<Expr> parseExprSimple() {
Token Tok = peek();
switch (Tok.kind()) {
case tok_id: {
consume();
auto ID =
std::make_shared<Identifier>(Tok.range(), std::string(Tok.view()));
return std::make_shared<ExprVar>(Tok.range(), std::move(ID));
}
case tok_int: {
consume();
NixInt N;
Expand Down
14 changes: 14 additions & 0 deletions libnixf/test/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,18 @@ TEST(Parser, AttrsExtraDot) {
ASSERT_EQ(F2.newText(), "\"dummy\"");
}

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

std::vector<Diagnostic> Diags;
auto AST = nixf::parse(Src, Diags);

ASSERT_TRUE(AST);
ASSERT_EQ(AST->kind(), Node::NK_ExprVar);
ASSERT_TRUE(AST->range().begin().isAt(0, 0, 0));
ASSERT_TRUE(AST->range().end().isAt(0, 1, 1));

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

} // namespace

0 comments on commit fbdbeed

Please sign in to comment.