Loading...
Searching...
No Matches
variableExpressionAST.h
1//
2// Copyright 2025 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_SDF_VARIABLE_EXPRESSION_AST_H
8#define PXR_USD_SDF_VARIABLE_EXPRESSION_AST_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/sdf/api.h"
13
14#include <memory>
15#include <string>
16#include <variant>
17#include <vector>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
21namespace SdfVariableExpressionASTNodes
22{
23
26class Node
27{
28public:
29 SDF_API virtual ~Node();
30 SDF_API std::unique_ptr<Node> Clone() const;
31
36
42
54 template <class T>
55 T* As()
56 {
57 static_assert(std::is_base_of_v<Node, T>);
58 return dynamic_cast<T*>(this);
59 }
60
61 template <class T>
62 const T* As() const
63 {
64 static_assert(std::is_base_of_v<Node, T>);
65 return dynamic_cast<const T*>(this);
66 }
68
69protected:
70 Node() = default;
71
72 virtual Node* _Clone() const = 0;
73 virtual SdfVariableExpression::Builder _GetExpressionBuilder() const = 0;
74};
75
79{
80public:
81 NodeList() = default;
82 NodeList(std::vector<std::unique_ptr<Node>>&& nodes);
83
85 SDF_API std::vector<Node*> GetNodes();
86
88 SDF_API std::vector<const Node*> GetNodes() const;
89
91 SDF_API void Append(const Node& node);
92
97 SDF_API void Set(size_t i, const Node& node);
98
104 SDF_API void Insert(size_t i, const Node& node);
105
110 SDF_API void Remove(size_t i);
111
113 SDF_API void Clear();
114
116 SDF_API NodeList Clone() const;
117
118private:
119 std::vector<std::unique_ptr<Node>> _nodes;
120};
121
125 : public Node
126{
127public:
130
133 using LiteralValue = std::variant<
134 std::monostate,
135 bool, int64_t, std::string
136 >;
137
138 const LiteralValue& GetValue() const { return _value; }
139 void SetValue(const LiteralValue& value) { _value = value; }
140
142
143private:
144 friend class _NodeCreator;
145
146 LiteralNode() = default;
147
148 template <class T>
149 LiteralNode(T&& value)
150 : _value(std::forward<T>(value))
151 { }
152
153 Node* _Clone() const final;
154 SdfVariableExpression::Builder _GetExpressionBuilder() const final;
155
156 LiteralValue _value;
157};
158
168 : public Node
169{
170public:
173 const std::string& GetName() const { return _name; }
174 void SetName(const std::string& name) { _name = name; }
176
179
182 Node* GetFallbackValue() { return _fallbackValue.get(); }
183
185 const Node* GetFallbackValue() const { return _fallbackValue.get(); }
186
190 SDF_API void SetFallbackValue(const Node& node);
191
193 void ClearFallbackValue() { _fallbackValue = nullptr; }
195
196private:
197 friend class _NodeCreator;
198
199 VariableNode(std::string&& varName,
200 std::unique_ptr<Node>&& fallbackValue)
201 : _name(std::move(varName)),
202 _fallbackValue(std::move(fallbackValue))
203 { }
204
205 Node* _Clone() const final;
206 SdfVariableExpression::Builder _GetExpressionBuilder() const final;
207
208 std::string _name;
209 std::unique_ptr<Node> _fallbackValue;
210};
211
215 : public Node
216{
217public:
219
222 NodeList& GetElements() { return _elems; }
223 const NodeList& GetElements() const { return _elems; }
225
226private:
227 friend class _NodeCreator;
228
229 ListNode(NodeList&& elems)
230 : _elems(std::move(elems))
231 { }
232
233 Node* _Clone() const final;
234 SdfVariableExpression::Builder _GetExpressionBuilder() const final;
235
236 NodeList _elems;
237};
238
242 : public Node
243{
244public:
245 SDF_API SdfVariableExpression::FunctionBuilder GetExpressionBuilder() const;
246
249 const std::string& GetName() const { return _functionName; }
250 void SetName(const std::string& name) { _functionName = name; }
252
255 const NodeList& GetArguments() const { return _functionArgs; }
256 NodeList& GetArguments() { return _functionArgs; }
258
259private:
260 friend class _NodeCreator;
261
262 FunctionNode(std::string&& functionName, NodeList&& functionArgs)
263 : _functionName(std::move(functionName))
264 , _functionArgs(std::move(functionArgs))
265 { }
266
267 Node* _Clone() const final;
268 SdfVariableExpression::Builder _GetExpressionBuilder() const final;
269
270 std::string _functionName;
271 NodeList _functionArgs;
272};
273
274} // end namespace SdfVariableExpressionASTNodes
275
280{
281public:
284
287 SDF_API explicit SdfVariableExpressionAST(const std::string& expr);
288
291 SDF_API
293
296
298 SDF_API
300
303 operator=(SdfVariableExpressionAST&& rhs) = default;
304
306 SDF_API explicit operator bool() const;
307
310 SDF_API const std::vector<std::string>& GetErrors() const;
311
317
320
333
334private:
335 std::unique_ptr<SdfVariableExpressionASTNodes::Node> _node;
336 std::vector<std::string> _errors;
337};
338
339PXR_NAMESPACE_CLOSE_SCOPE
340
341#endif
Helper class for storing intermediate results when building a variable expression.
Helper class for storing intermediate results when building a function variable expression.
Helper class for storing intermediate results when building a list variable expression.
Abstract syntax tree for an SdfVariableExpression.
SDF_API const SdfVariableExpressionASTNodes::Node * GetRoot() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
SDF_API SdfVariableExpressionASTNodes::Node * GetRoot()
Return the root node of the syntax tree corresponding to the outermost expression if this is a valid ...
SDF_API SdfVariableExpression GetExpression() const
Return the expression represented by this AST if this is a valid AST or a default constructed SdfVari...
SDF_API SdfVariableExpressionAST & operator=(const SdfVariableExpressionAST &rhs)
Assignment. Performs a deep copy of the AST in rhs.
SDF_API const std::vector< std::string > & GetErrors() const
Return list of errors encountered when parsing the expression passed to the constructor.
SDF_API SdfVariableExpressionAST()
Construct an invalid AST.
SDF_API SdfVariableExpressionAST(const std::string &expr)
Construct the AST for the variable expression expr.
SDF_API SdfVariableExpressionAST(const SdfVariableExpression &expr)
This is an overloaded member function, provided for convenience. It differs from the above function o...
SDF_API SdfVariableExpressionAST(const SdfVariableExpressionAST &rhs)
Copy constructor. Performs a deep copy of the AST in rhs.
Abstract syntax tree node representing a function call.
Abstract syntax tree node representing a list.
Abstract syntax tree node representing a literal value.
std::variant< std::monostate, bool, int64_t, std::string > LiteralValue
Variant representing possible literal value types.
Base class for nodes in the SdfVariableExpression abstract syntax tree.
SDF_API SdfVariableExpression GetExpression() const
Return SdfVariableExpression for this node.
SDF_API SdfVariableExpression::Builder GetExpressionBuilder() const
Return SdfVariableExpression::Builder for this node.
Ordered list of abstract syntax tree nodes.
SDF_API void Remove(size_t i)
Remove the node at index i.
SDF_API void Append(const Node &node)
Append a copy of node to the end of this collection.
SDF_API void Clear()
Clear all nodes.
SDF_API std::vector< Node * > GetNodes()
Return list of nodes in this collection.
SDF_API void Set(size_t i, const Node &node)
Set the node at index i to a copy of node.
SDF_API std::vector< const Node * > GetNodes() const
Return list of nodes in this collection.
SDF_API void Insert(size_t i, const Node &node)
Insert a copy of node at index i.
SDF_API NodeList Clone() const
Copy all nodes in this collection to a new collection.
Abstract syntax tree node representing a raw variable reference, i.e.
void ClearFallbackValue()
Clears any set fallback value.
SDF_API void SetFallbackValue(const Node &node)
Sets the fallback value for this node.
const Node * GetFallbackValue() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Node * GetFallbackValue()
Returns the fallback value for this variable.
Class responsible for parsing and evaluating variable expressions.
STL namespace.