nixd
Loading...
Searching...
No Matches
Lambda.h
Go to the documentation of this file.
1#pragma once
2
3#include "Basic.h"
4
5#include <map>
6#include <memory>
7#include <vector>
8
9namespace nixf {
10
11class Formal : public Node {
12 const std::shared_ptr<Misc> Comma;
13 const std::shared_ptr<Identifier> ID;
14 const std::shared_ptr<Expr> Default;
15 const std::shared_ptr<Misc> Ellipsis; // ...
16
17public:
18 Formal(LexerCursorRange Range, std::shared_ptr<Misc> Comma,
19 std::shared_ptr<Identifier> ID, std::shared_ptr<Expr> Default)
20 : Node(NK_Formal, Range), Comma(std::move(Comma)), ID(std::move(ID)),
21 Default(std::move(Default)) {}
22
23 Formal(LexerCursorRange Range, std::shared_ptr<Misc> Comma,
24 std::shared_ptr<Misc> Ellipsis)
25 : Node(NK_Formal, Range), Comma(std::move(Comma)),
26 Ellipsis(std::move(Ellipsis)) {
27 assert(this->Ellipsis && "Ellipsis must not be null");
28 }
29
30 [[nodiscard]] Misc &ellipsis() const {
31 assert(Ellipsis && "Ellipsis must not be null");
32 return *Ellipsis;
33 }
34
35 [[nodiscard]] bool isEllipsis() const { return Ellipsis != nullptr; }
36
37 [[nodiscard]] Identifier *id() const { return ID.get(); }
38
39 [[nodiscard]] Misc *comma() const { return Comma.get(); }
40
41 [[nodiscard]] Expr *defaultExpr() const { return Default.get(); }
42
43 [[nodiscard]] ChildVector children() const override {
44 if (isEllipsis()) {
45 return {Ellipsis.get()};
46 }
47 return {ID.get(), Default.get()};
48 }
49};
50
51/// \brief Lambda formal arguments.
52///
53/// Things to check:
54/// 1. Ellipsis can only occur at the end of the formals.
55/// { ..., pkgs } -> { pkgs, ... }
56/// 2. Ellipsis can only occur once.
57/// { b, ..., a, ... } -> { a, ... }
58class Formals : public Node {
59 const std::vector<std::shared_ptr<Formal>> Members;
60
61 /// Deduplicated formals, useful for encoding
62 const std::map<std::string, const Formal *> Dedup;
63
64public:
65 using FormalVector = std::vector<std::shared_ptr<Formal>>;
67 std::map<std::string, const Formal *> Dedup)
68 : Node(NK_Formals, Range), Members(std::move(Members)),
69 Dedup(std::move(Dedup)) {}
70
71 [[nodiscard]] const FormalVector &members() const { return Members; }
72
73 /// \brief Deduplicated formals.
74 const std::map<std::string, const Formal *> &dedup() { return Dedup; }
75
76 [[nodiscard]] const std::map<std::string, const Formal *> &dedup() const {
77 return Dedup;
78 }
79
80 [[nodiscard]] ChildVector children() const override {
81 ChildVector Children;
82 Children.reserve(Members.size());
83 for (const auto &Member : Members) {
84 Children.emplace_back(Member.get());
85 }
86 return Children;
87 }
88};
89
90class LambdaArg : public Node {
91 const std::shared_ptr<Identifier> ID;
92 const std::shared_ptr<Formals> F;
93
94public:
95 LambdaArg(LexerCursorRange Range, std::shared_ptr<Identifier> ID,
96 std::shared_ptr<Formals> F)
97 : Node(NK_LambdaArg, Range), ID(std::move(ID)), F(std::move(F)) {}
98
99 [[nodiscard]] Identifier *id() const { return ID.get(); }
100
101 [[nodiscard]] Formals *formals() const { return F.get(); }
102
103 [[nodiscard]] ChildVector children() const override {
104 return {ID.get(), F.get()};
105 }
106};
107
108class ExprLambda : public Expr {
109 const std::shared_ptr<LambdaArg> Arg;
110 const std::shared_ptr<Expr> Body;
111
112public:
113 ExprLambda(LexerCursorRange Range, std::shared_ptr<LambdaArg> Arg,
114 std::shared_ptr<Expr> Body)
115 : Expr(NK_ExprLambda, Range), Arg(std::move(Arg)), Body(std::move(Body)) {
116 }
117
118 [[nodiscard]] LambdaArg *arg() const { return Arg.get(); }
119 [[nodiscard]] Expr *body() const { return Body.get(); }
120
121 [[nodiscard]] ChildVector children() const override {
122 return {Arg.get(), Body.get()};
123 }
124};
125
126} // namespace nixf
Expr * body() const
Definition Lambda.h:119
LambdaArg * arg() const
Definition Lambda.h:118
ExprLambda(LexerCursorRange Range, std::shared_ptr< LambdaArg > Arg, std::shared_ptr< Expr > Body)
Definition Lambda.h:113
ChildVector children() const override
Definition Lambda.h:121
Misc & ellipsis() const
Definition Lambda.h:30
Misc * comma() const
Definition Lambda.h:39
ChildVector children() const override
Definition Lambda.h:43
Formal(LexerCursorRange Range, std::shared_ptr< Misc > Comma, std::shared_ptr< Identifier > ID, std::shared_ptr< Expr > Default)
Definition Lambda.h:18
bool isEllipsis() const
Definition Lambda.h:35
Identifier * id() const
Definition Lambda.h:37
Formal(LexerCursorRange Range, std::shared_ptr< Misc > Comma, std::shared_ptr< Misc > Ellipsis)
Definition Lambda.h:23
Expr * defaultExpr() const
Definition Lambda.h:41
Lambda formal arguments.
Definition Lambda.h:58
std::vector< std::shared_ptr< Formal > > FormalVector
Definition Lambda.h:65
Formals(LexerCursorRange Range, FormalVector Members, std::map< std::string, const Formal * > Dedup)
Definition Lambda.h:66
ChildVector children() const override
Definition Lambda.h:80
const std::map< std::string, const Formal * > & dedup() const
Definition Lambda.h:76
const FormalVector & members() const
Definition Lambda.h:71
const std::map< std::string, const Formal * > & dedup()
Deduplicated formals.
Definition Lambda.h:74
Identifier. Variable names, attribute names, etc.
Definition Basic.h:114
Formals * formals() const
Definition Lambda.h:101
LambdaArg(LexerCursorRange Range, std::shared_ptr< Identifier > ID, std::shared_ptr< Formals > F)
Definition Lambda.h:95
Identifier * id() const
Definition Lambda.h:99
ChildVector children() const override
Definition Lambda.h:103
Misc node, used for parentheses, keywords, etc.
Definition Basic.h:106
boost::container::small_vector< Node *, 8 > ChildVector
Definition Basic.h:42