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 <exception>
11
12namespace nixd {
13
14namespace idioms {
15
16/// \brief Hardcoded name for "pkgs.xxx", or "with pkgs;"
17///
18/// Assume that the value of this variable have the same structure with `import
19/// nixpkgs {}
20constexpr inline std::string_view Pkgs = "pkgs";
21
22/// \brief Hardcoded name for nixpkgs "lib"
23///
24/// Assume that the value of this variable is "nixpkgs lib".
25/// e.g. lib.genAttrs.
26constexpr inline std::string_view Lib = "lib";
27
28struct IdiomException : std::exception {};
29
30/// \brief Exceptions scoped in nixd::mkIdiomSelector
31struct IdiomSelectorException : std::exception {};
32
33/// \brief The pattern of this variable cannot be recognized by known idioms.
35 [[nodiscard]] const char *what() const noexcept override {
36 return "not an idiom";
37 }
38};
39
40struct VLAException : std::exception {};
41
42struct NoLocationForBuiltinVariable : std::exception {
43 [[nodiscard]] const char *what() const noexcept override {
44 return "builtins are defined in the interpreter, not in the Nix files";
45 }
46};
47
48/// \brief No such variable.
50 [[nodiscard]] const char *what() const noexcept override {
51 return "no such variable";
52 }
53};
54
56 [[nodiscard]] const char *what() const noexcept override {
57 return "undefined variable";
58 }
59};
60
61/// \brief Construct a nixd::Selector from \p Var.
62///
63/// Try to heuristically find a selector of a variable, based on some known
64/// idioms.
67 const nixf::ParentMapAnalysis &PM);
68
69/// \brief The attrpath has a dynamic name, thus it cannot be trivially
70/// transformed to "static" selector.
72 [[nodiscard]] const char *what() const noexcept override {
73 return "dynamic attribute path encountered";
74 }
75};
76
78 [[nodiscard]] const char *what() const noexcept override {
79 return "the base expression of the select is not a variable";
80 }
81};
82
83/// \brief Construct a nixd::Selector from \p AP.
84Selector mkSelector(const nixf::AttrPath &AP, Selector BaseSelector);
85
86/// \brief Construct a nixd::Selector from \p Select.
87Selector mkSelector(const nixf::ExprSelect &Select, Selector BaseSelector);
88
89/// \brief Construct a nixd::Selector from \p Select.
92 const nixf::ParentMapAnalysis &PM);
93
94} // namespace idioms
95
96/// \brief Search up until there are some node associated with "EnvNode".
97[[nodiscard]] const nixf::EnvNode *
98upEnv(const nixf::Node &Desc, const nixf::VariableLookupAnalysis &VLA,
99 const nixf::ParentMapAnalysis &PM);
100
101/// \brief Determine whether or not some node has enclosed "with pkgs; [ ]"
102///
103/// Yes, this evaluation isn't flawless. What if the identifier isn't "pkgs"? We
104/// can't dynamically evaluate everything each time and invalidate them
105/// immediately after document updates. Therefore, this heuristic method
106/// represents a trade-off between performance considerations.
107[[nodiscard]] bool havePackageScope(const nixf::Node &N,
109 const nixf::ParentMapAnalysis &PM);
110
111/// \brief get variable scope, and it's prefix name.
112///
113/// Nixpkgs has some packages scoped in "nested" attrs.
114/// e.g. llvmPackages, pythonPackages.
115/// Try to find these name as a pre-selected scope, the last value is "prefix".
116std::pair<std::vector<std::string>, std::string>
118
120 OK,
121 Inherit,
124};
125
126/// \brief Heuristically find attrpath suitable for "attrpath" completion.
127/// \param[out] Path the attrpath.
129 const nixf::ParentMapAnalysis &PM,
130 std::vector<std::string> &Path);
131
132} // namespace nixd
Types used in nixpkgs provider.
Lookup variable names, from it's parent scope.
A set of variable definitions, which may inherit parent environment.
ParentMap analysis.
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 Pkgs
Hardcoded name for "pkgs.xxx", or "with pkgs;".
Definition AST.h:20
constexpr std::string_view Lib
Hardcoded name for nixpkgs "lib".
Definition AST.h:26
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
std::vector< std::string > Selector
A list of strings that "select"s into a attribute set.
Definition AttrSet.h:42
FindAttrPathResult
Definition AST.h:119
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
The attrpath has a dynamic name, thus it cannot be trivially transformed to "static" selector.
Definition AST.h:71
const char * what() const noexcept override
Definition AST.h:72
Exceptions scoped in nixd::mkIdiomSelector.
Definition AST.h:31
const char * what() const noexcept override
Definition AST.h:43
No such variable.
Definition AST.h:49
const char * what() const noexcept override
Definition AST.h:50
The pattern of this variable cannot be recognized by known idioms.
Definition AST.h:34
const char * what() const noexcept override
Definition AST.h:35
const char * what() const noexcept override
Definition AST.h:78
const char * what() const noexcept override
Definition AST.h:56