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
166 : public Node
167{
168public:
171 const std::string& GetName() const { return _name; }
172 void SetName(const std::string& name) { _name = name; }
174
175private:
176 friend class _NodeCreator;
177
178 VariableNode(std::string&& varName) :
179 _name(std::move(varName))
180 { }
181
182 Node* _Clone() const final;
183 SdfVariableExpression::Builder _GetExpressionBuilder() const final;
184
185 std::string _name;
186};
187
191 : public Node
192{
193public:
194 SDF_API SdfVariableExpression::ListBuilder GetExpressionBuilder() const;
195
198 NodeList& GetElements() { return _elems; }
199 const NodeList& GetElements() const { return _elems; }
201
202private:
203 friend class _NodeCreator;
204
205 ListNode(NodeList&& elems)
206 : _elems(std::move(elems))
207 { }
208
209 Node* _Clone() const final;
210 SdfVariableExpression::Builder _GetExpressionBuilder() const final;
211
212 NodeList _elems;
213};
214
218 : public Node
219{
220public:
221 SDF_API SdfVariableExpression::FunctionBuilder GetExpressionBuilder() const;
222
225 const std::string& GetName() const { return _functionName; }
226 void SetName(const std::string& name) { _functionName = name; }
228
231 const NodeList& GetArguments() const { return _functionArgs; }
232 NodeList& GetArguments() { return _functionArgs; }
234
235private:
236 friend class _NodeCreator;
237
238 FunctionNode(std::string&& functionName, NodeList&& functionArgs)
239 : _functionName(std::move(functionName))
240 , _functionArgs(std::move(functionArgs))
241 { }
242
243 Node* _Clone() const final;
244 SdfVariableExpression::Builder _GetExpressionBuilder() const final;
245
246 std::string _functionName;
247 NodeList _functionArgs;
248};
249
250} // end namespace SdfVariableExpressionASTNodes
251
256{
257public:
260
263 SDF_API explicit SdfVariableExpressionAST(const std::string& expr);
264
267 SDF_API
269
272
274 SDF_API
276
279 operator=(SdfVariableExpressionAST&& rhs) = default;
280
282 SDF_API explicit operator bool() const;
283
286 SDF_API const std::vector<std::string>& GetErrors() const;
287
293
296
309
310private:
311 std::unique_ptr<SdfVariableExpressionASTNodes::Node> _node;
312 std::vector<std::string> _errors;
313};
314
315PXR_NAMESPACE_CLOSE_SCOPE
316
317#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.
Class responsible for parsing and evaluating variable expressions.
STL namespace.