Skip to content

Commit

Permalink
libnixf: support parsing extra dot after attrpath (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Apr 18, 2024
1 parent 384592e commit 30e7a1b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
11 changes: 8 additions & 3 deletions libnixf/include/nixf/Basic/Nodes/Attrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,26 @@ class AttrName : public Node {

class AttrPath : public Node {
const std::vector<std::shared_ptr<AttrName>> Names;
// FIXME: workaround for just recording the trailing dot.
const std::shared_ptr<Misc> TrailingDot;

public:
AttrPath(LexerCursorRange Range, std::vector<std::shared_ptr<AttrName>> Names)
: Node(NK_AttrPath, Range), Names(std::move(Names)) {}
AttrPath(LexerCursorRange Range, std::vector<std::shared_ptr<AttrName>> Names,
std::shared_ptr<Misc> TrailingDot)
: Node(NK_AttrPath, Range), Names(std::move(Names)),
TrailingDot(std::move(TrailingDot)) {}

[[nodiscard]] const std::vector<std::shared_ptr<AttrName>> &names() const {
return Names;
}

[[nodiscard]] ChildVector children() const override {
ChildVector Children;
Children.reserve(Names.size());
Children.reserve(Names.size() + 1);
for (const auto &Name : Names) {
Children.push_back(Name.get());
}
Children.emplace_back(TrailingDot.get());
return Children;
}
};
Expand Down
5 changes: 4 additions & 1 deletion libnixf/src/Parse/ParseAttrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ std::shared_ptr<AttrPath> Parser::parseAttrPath() {
assert(LastToken && "LastToken should be set after valid attrname");
std::vector<std::shared_ptr<AttrName>> AttrNames;
AttrNames.emplace_back(std::move(First));
std::shared_ptr<Misc> TrailingDot;
while (true) {
if (Token Tok = peek(); Tok.kind() == tok_dot) {
consume();
Expand All @@ -47,6 +48,7 @@ std::shared_ptr<AttrPath> Parser::parseAttrPath() {
D.fix("remove extra .").edit(TextEdit::mkRemoval(Tok.range()));
D.fix("insert dummy attrname")
.edit(TextEdit::mkInsertion(Tok.rCur(), R"("dummy")"));
TrailingDot = std::make_shared<Misc>(Tok.range());
continue;
}
AttrNames.emplace_back(std::move(Next));
Expand All @@ -55,7 +57,8 @@ std::shared_ptr<AttrPath> Parser::parseAttrPath() {
break;
}
return std::make_shared<AttrPath>(LexerCursorRange{Begin, LastToken->rCur()},
std::move(AttrNames));
std::move(AttrNames),
std::move(TrailingDot));
}

std::shared_ptr<Binding> Parser::parseBinding() {
Expand Down
3 changes: 2 additions & 1 deletion libnixf/src/Sema/SemaActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ std::shared_ptr<Expr> Sema::desugarInheritExpr(std::shared_ptr<AttrName> Name,
return std::make_shared<ExprVar>(Range, Name->id());

auto Path = std::make_shared<AttrPath>(
Range, std::vector<std::shared_ptr<AttrName>>{std::move(Name)});
Range, std::vector<std::shared_ptr<AttrName>>{std::move(Name)},
/*TrailingDot=*/nullptr);
return std::make_shared<ExprSelect>(Range, std::move(E), std::move(Path),
nullptr);
}
Expand Down

0 comments on commit 30e7a1b

Please sign in to comment.