nixd
Toggle main menu visibility
Loading...
Searching...
No Matches
libnixf
include
nixf
Basic
Nodes
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
9
namespace
nixf
{
10
11
class
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
17
public
:
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, ... }
58
class
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
64
public
:
65
using
FormalVector
= std::vector<std::shared_ptr<Formal>>;
66
Formals
(
LexerCursorRange
Range,
FormalVector
Members,
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
90
class
LambdaArg
:
public
Node
{
91
const
std::shared_ptr<Identifier> ID;
92
const
std::shared_ptr<Formals> F;
93
94
public
:
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
108
class
ExprLambda
:
public
Expr
{
109
const
std::shared_ptr<LambdaArg> Arg;
110
const
std::shared_ptr<Expr> Body;
111
112
public
:
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
Basic.h
nixf::ExprLambda::body
Expr * body() const
Definition
Lambda.h:119
nixf::ExprLambda::arg
LambdaArg * arg() const
Definition
Lambda.h:118
nixf::ExprLambda::ExprLambda
ExprLambda(LexerCursorRange Range, std::shared_ptr< LambdaArg > Arg, std::shared_ptr< Expr > Body)
Definition
Lambda.h:113
nixf::ExprLambda::children
ChildVector children() const override
Definition
Lambda.h:121
nixf::Expr
Definition
Basic.h:70
nixf::Expr::Expr
Expr(NodeKind Kind, LexerCursorRange Range)
Definition
Basic.h:72
nixf::Formal::ellipsis
Misc & ellipsis() const
Definition
Lambda.h:30
nixf::Formal::comma
Misc * comma() const
Definition
Lambda.h:39
nixf::Formal::children
ChildVector children() const override
Definition
Lambda.h:43
nixf::Formal::Formal
Formal(LexerCursorRange Range, std::shared_ptr< Misc > Comma, std::shared_ptr< Identifier > ID, std::shared_ptr< Expr > Default)
Definition
Lambda.h:18
nixf::Formal::isEllipsis
bool isEllipsis() const
Definition
Lambda.h:35
nixf::Formal::id
Identifier * id() const
Definition
Lambda.h:37
nixf::Formal::Formal
Formal(LexerCursorRange Range, std::shared_ptr< Misc > Comma, std::shared_ptr< Misc > Ellipsis)
Definition
Lambda.h:23
nixf::Formal::defaultExpr
Expr * defaultExpr() const
Definition
Lambda.h:41
nixf::Formals
Lambda formal arguments.
Definition
Lambda.h:58
nixf::Formals::FormalVector
std::vector< std::shared_ptr< Formal > > FormalVector
Definition
Lambda.h:65
nixf::Formals::Formals
Formals(LexerCursorRange Range, FormalVector Members, std::map< std::string, const Formal * > Dedup)
Definition
Lambda.h:66
nixf::Formals::children
ChildVector children() const override
Definition
Lambda.h:80
nixf::Formals::dedup
const std::map< std::string, const Formal * > & dedup() const
Definition
Lambda.h:76
nixf::Formals::members
const FormalVector & members() const
Definition
Lambda.h:71
nixf::Formals::dedup
const std::map< std::string, const Formal * > & dedup()
Deduplicated formals.
Definition
Lambda.h:74
nixf::Identifier
Identifier. Variable names, attribute names, etc.
Definition
Basic.h:114
nixf::LambdaArg
Definition
Lambda.h:90
nixf::LambdaArg::formals
Formals * formals() const
Definition
Lambda.h:101
nixf::LambdaArg::LambdaArg
LambdaArg(LexerCursorRange Range, std::shared_ptr< Identifier > ID, std::shared_ptr< Formals > F)
Definition
Lambda.h:95
nixf::LambdaArg::id
Identifier * id() const
Definition
Lambda.h:99
nixf::LambdaArg::children
ChildVector children() const override
Definition
Lambda.h:103
nixf::LexerCursorRange
Definition
Range.h:105
nixf::Misc
Misc node, used for parentheses, keywords, etc.
Definition
Basic.h:106
nixf::Node::ChildVector
boost::container::small_vector< Node *, 8 > ChildVector
Definition
Basic.h:42
nixf::Node::Node
Node(NodeKind Kind, LexerCursorRange Range)
Definition
Basic.h:30
nixf
Definition
Diagnostic.h:19
Generated by
1.17.0