nixd
Loading...
Searching...
No Matches
AST.h
Go to the documentation of this file.
1/// \file
2/// \brief This file declares some common analysis (tree walk) on the AST.
3
5
9
10#include <llvm/ADT/StringRef.h>
11
12#include <exception>
13
14namespace nixd {
15
16namespace idioms {
17
18/// \brief Hardcoded name for "pkgs.xxx", or "with pkgs;"
19///
20/// Assume that the value of this variable have the same structure with `import
21/// nixpkgs {}
22constexpr inline std::string_view Pkgs = "pkgs";
23
24/// \brief Hardcoded name for nixpkgs "lib"
25///
26/// Assume that the value of this variable is "nixpkgs lib".
27/// e.g. lib.genAttrs.
28constexpr inline std::string_view Lib = "lib";
29
30struct IdiomException : std::exception {};
31
32/// \brief Exceptions scoped in nixd::mkIdiomSelector
33struct IdiomSelectorException : std::exception {};
34
35/// \brief The pattern of this variable cannot be recognized by known idioms.
37 [[nodiscard]] const char *what() const noexcept override {
38 return "not an idiom";
39 }
40};
41
42struct VLAException : std::exception {};
43
44struct NoLocationForBuiltinVariable : std::exception {
45 [[nodiscard]] const char *what() const noexcept override {
46 return "builtins are defined in the interpreter, not in the Nix files";
47 }
48};
49
50/// \brief No such variable.
52 [[nodiscard]] const char *what() const noexcept override {
53 return "no such variable";
54 }
55};
56
58 [[nodiscard]] const char *what() const noexcept override {
59 return "undefined variable";
60 }
61};
62
63/// \brief Construct a nixd::Selector from \p Var.
64///
65/// Try to heuristically find a selector of a variable, based on some known
66/// idioms.
69 const nixf::ParentMapAnalysis &PM);
70
71/// \brief The attrpath has a dynamic name, thus it cannot be trivially
72/// transformed to "static" selector.
74 [[nodiscard]] const char *what() const noexcept override {
75 return "dynamic attribute path encountered";
76 }
77};
78
80 [[nodiscard]] const char *what() const noexcept override {
81 return "the base expression of the select is not a variable";
82 }
83};
84
85/// \brief Construct a nixd::Selector from \p AP.
86Selector mkSelector(const nixf::AttrPath &AP, Selector BaseSelector);
87
88/// \brief Construct a nixd::Selector from \p Select.
89Selector mkSelector(const nixf::ExprSelect &Select, Selector BaseSelector);
90
91/// \brief Construct a nixd::Selector from \p Select.
94 const nixf::ParentMapAnalysis &PM);
95
96} // namespace idioms
97
98/// \brief Search up until there are some node associated with "EnvNode".
99[[nodiscard]] const nixf::EnvNode *
100upEnv(const nixf::Node &Desc, const nixf::VariableLookupAnalysis &VLA,
101 const nixf::ParentMapAnalysis &PM);
102
103/// \brief Determine whether or not some node has enclosed "with pkgs; [ ]"
104///
105/// Yes, this evaluation isn't flawless. What if the identifier isn't "pkgs"? We
106/// can't dynamically evaluate everything each time and invalidate them
107/// immediately after document updates. Therefore, this heuristic method
108/// represents a trade-off between performance considerations.
109[[nodiscard]] bool havePackageScope(const nixf::Node &N,
111 const nixf::ParentMapAnalysis &PM);
112
113/// \brief get variable scope, and it's prefix name.
114///
115/// Nixpkgs has some packages scoped in "nested" attrs.
116/// e.g. llvmPackages, pythonPackages.
117/// Try to find these name as a pre-selected scope, the last value is "prefix".
118std::pair<std::vector<std::string>, std::string>
120
127
128/// \brief Heuristically find attrpath suitable for "attrpath" completion.
129/// \param[out] Path the attrpath.
131 const nixf::ParentMapAnalysis &PM,
132 std::vector<std::string> &Path);
133
134/// \brief Heuristically find attrpath suitable for "attrpath" completion.
135/// Strips "config." from the start to support config sections in NixOS modules.
136///
137/// \param[out] Path the attrpath.
139 const nixf::ParentMapAnalysis &PM,
140 std::vector<std::string> &Path);
141
142/// Whether \p N mentions `__curPos`, whose value is the position it is
143/// written at.
144bool mentionsCurPos(const nixf::Node &N);
145
146/// Whether copying \p E refers to the value it denotes rather than a new one:
147/// a variable, or attributes selected from one.
148bool isShared(const nixf::Expr &E);
149
150/// \p Name with the spaces and tabs before it on its line: removing that keeps
151/// comments and the other names.
153 llvm::StringRef Src);
154
155/// Whether removing \p R would join the text on either side of it into one
156/// token: `"b"` out of `a"b"c` leaves `ac`.
157bool gluesNeighbours(const nixf::LexerCursorRange &R, llvm::StringRef Src);
158
159} // namespace nixd
Types used in nixpkgs provider.
ParentMap analysis.
Lookup variable names, from it's parent scope.
A set of variable definitions, which may inherit parent environment.
std::string Path
Definition Path.h:24
Selector mkVarSelector(const nixf::ExprVar &Var, const nixf::VariableLookupAnalysis &VLA, const nixf::ParentMapAnalysis &PM)
Construct a nixd::Selector from Var.
Definition AST.cpp:199
Selector mkSelector(const nixf::AttrPath &AP, Selector BaseSelector)
Construct a nixd::Selector from AP.
Definition AST.cpp:249
constexpr std::string_view Lib
Hardcoded name for "pkgs.xxx", or "with pkgs;".
Definition AST.h:28
bool gluesNeighbours(const nixf::LexerCursorRange &R, llvm::StringRef Src)
Definition AST.cpp:385
FindAttrPathResult findAttrPath(const nixf::Node &N, const nixf::ParentMapAnalysis &PM, std::vector< std::string > &Path)
Heuristically find attrpath suitable for "attrpath" completion.
Definition AST.cpp:283
const nixf::EnvNode * upEnv(const nixf::Node &Desc, const nixf::VariableLookupAnalysis &VLA, const nixf::ParentMapAnalysis &PM)
Search up until there are some node associated with "EnvNode".
Definition AST.cpp:98
bool havePackageScope(const nixf::Node &N, const nixf::VariableLookupAnalysis &VLA, const nixf::ParentMapAnalysis &PM)
Determine whether or not some node has enclosed "with pkgs; [ ]".
Definition AST.cpp:108
bool isShared(const nixf::Expr &E)
Definition AST.cpp:357
std::vector< std::string > Selector
A list of strings that "select"s into a attribute set.
Definition AttrSet.h:43
FindAttrPathResult
Definition AST.h:121
FindAttrPathResult findAttrPathForOptions(const nixf::Node &N, const nixf::ParentMapAnalysis &PM, std::vector< std::string > &Path)
Heuristically find attrpath suitable for "attrpath" completion. Strips "config." from the start to su...
Definition AST.cpp:332
nixf::LexerCursorRange withLeadingSpaces(const nixf::Node &Name, llvm::StringRef Src)
Definition AST.cpp:374
std::pair< std::vector< std::string >, std::string > getScopeAndPrefix(const nixf::Node &N, const nixf::ParentMapAnalysis &PM)
get variable scope, and it's prefix name.
Definition AST.cpp:274
bool mentionsCurPos(const nixf::Node &N)
Definition AST.cpp:347
The attrpath has a dynamic name, thus it cannot be trivially transformed to "static" selector.
Definition AST.h:73
const char * what() const noexcept override
Definition AST.h:74
Exceptions scoped in nixd::mkIdiomSelector.
Definition AST.h:33
const char * what() const noexcept override
Definition AST.h:45
No such variable.
Definition AST.h:51
const char * what() const noexcept override
Definition AST.h:52
The pattern of this variable cannot be recognized by known idioms.
Definition AST.h:36
const char * what() const noexcept override
Definition AST.h:37
const char * what() const noexcept override
Definition AST.h:80
const char * what() const noexcept override
Definition AST.h:58