HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
variableExpressionAST.h
Go to the documentation of this file.
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 
20 
21 namespace SdfVariableExpressionASTNodes
22 {
23 
24 /// \class Node
25 /// Base class for nodes in the SdfVariableExpression abstract syntax tree.
26 class Node
27 {
28 public:
29  SDF_API virtual ~Node();
30  SDF_API std::unique_ptr<Node> Clone() const;
31 
32  /// Return SdfVariableExpression for this node.
33  ///
34  /// \see SdfVariableExpressionAST::GetExpression
36 
37  /// Return SdfVariableExpression::Builder for this node.
38  /// This can be used when creating expressions using the various
39  /// SdfVariableExpression API for building expressions without
40  /// incurring extra overhead from parsing into an SdfVariableExpression.
42 
43  /// \name Node Casts
44  /// Return a pointer to this node as the specified type or nullptr
45  /// if this node does not represent that type.
46  ///
47  /// \code
48  /// using namespace SdfVariableExpressionASTNodes;
49  /// Node* n = ...;
50  /// FunctionNode* fnNode = n->As<FunctionNode>();
51  /// if (fnNode) { /* node represents a function */ }
52  /// \endcode
53  /// @{
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  }
67  /// @}
68 
69 protected:
70  Node() = default;
71 
72  virtual Node* _Clone() const = 0;
74 };
75 
76 /// \class NodeList
77 /// Ordered list of abstract syntax tree nodes.
78 class NodeList
79 {
80 public:
81  NodeList() = default;
82  NodeList(std::vector<std::unique_ptr<Node>>&& nodes);
83 
84  /// Return list of nodes in this collection.
85  SDF_API std::vector<Node*> GetNodes();
86 
87  /// Return list of nodes in this collection.
88  SDF_API std::vector<const Node*> GetNodes() const;
89 
90  /// Append a copy of \p node to the end of this collection.
91  SDF_API void Append(const Node& node);
92 
93  /// Set the node at index \p i to a copy of \p node.
94  ///
95  /// If \p i is not the index of a node in this list, a coding
96  /// error will be emitted and no changes will be made.
97  SDF_API void Set(size_t i, const Node& node);
98 
99  /// Insert a copy of \p node at index \p i.
100  ///
101  /// \p i must be in the range [0, GetNodes().size()]. Otherwise,
102  /// a coding error will be emitted and no changes will be made
103  /// to this list.
104  SDF_API void Insert(size_t i, const Node& node);
105 
106  /// Remove the node at index \p i.
107  ///
108  /// If \p i is not the index of a node in this list, a coding
109  /// error will be emitted and no changes will be made.
110  SDF_API void Remove(size_t i);
111 
112  /// Clear all nodes.
113  SDF_API void Clear();
114 
115  /// Copy all nodes in this collection to a new collection.
116  SDF_API NodeList Clone() const;
117 
118 private:
119  std::vector<std::unique_ptr<Node>> _nodes;
120 };
121 
122 /// \class LiteralNode
123 /// Abstract syntax tree node representing a literal value.
125  : public Node
126 {
127 public:
128  /// \name Value
129  /// @{
130 
131  /// Variant representing possible literal value types.
132  /// std::monostate represents the special "None" value.
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 
141  /// @}
142 
143 private:
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 
159 /// \class VariableNode
160 /// Abstract syntax tree node representing a raw variable reference,
161 /// i.e. ${FOO}.
162 ///
163 /// This does not include variable references within quoted strings,
164 /// i.e. "FOO_${BAR}". See LiteralNode.
166  : public Node
167 {
168 public:
169  /// \name Variable Name
170  /// @{
171  const std::string& GetName() const { return _name; }
172  void SetName(const std::string& name) { _name = name; }
173  /// @}
174 
175 private:
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 
188 /// \class ListNode
189 /// Abstract syntax tree node representing a list.
191  : public Node
192 {
193 public:
195 
196  /// \name List Elements
197  /// @{
198  NodeList& GetElements() { return _elems; }
199  const NodeList& GetElements() const { return _elems; }
200  /// @}
201 
202 private:
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 
215 /// \class FunctionNode
216 /// Abstract syntax tree node representing a function call.
218  : public Node
219 {
220 public:
222 
223  /// \name Function Name
224  /// @{
225  const std::string& GetName() const { return _functionName; }
226  void SetName(const std::string& name) { _functionName = name; }
227  /// @}
228 
229  /// \name Function Arguments
230  /// @{
231  const NodeList& GetArguments() const { return _functionArgs; }
232  NodeList& GetArguments() { return _functionArgs; }
233  /// @}
234 
235 private:
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 
252 /// \class SdfVariableExpressionAST
253 /// Abstract syntax tree for an SdfVariableExpression. Provides read-only
254 /// inspection of the structure of a variable expression.
256 {
257 public:
258  /// Construct an invalid AST.
259  SDF_API SdfVariableExpressionAST();
260 
261  /// Construct the AST for the variable expression \p expr. Creates an
262  /// invalid AST if \p expr cannot be parsed.
263  SDF_API explicit SdfVariableExpressionAST(const std::string& expr);
264 
265  /// \overload
266  /// Equivalent to SdfVariableExpressionAST(expr.GetString());
267  SDF_API
268  explicit SdfVariableExpressionAST(const SdfVariableExpression& expr);
269 
270  /// Copy constructor. Performs a deep copy of the AST in \p rhs.
271  SDF_API SdfVariableExpressionAST(const SdfVariableExpressionAST& rhs);
272 
273  /// Assignment. Performs a deep copy of the AST in \p rhs.
274  SDF_API
275  SdfVariableExpressionAST& operator=(const SdfVariableExpressionAST& rhs);
276 
277  SdfVariableExpressionAST(SdfVariableExpressionAST&& rhs) = default;
278  SdfVariableExpressionAST&
279  operator=(SdfVariableExpressionAST&& rhs) = default;
280 
281  /// Return true if this is a valid AST, false otherwise.
282  SDF_API explicit operator bool() const;
283 
284  /// Return list of errors encountered when parsing the expression
285  /// passed to the constructor.
286  SDF_API const std::vector<std::string>& GetErrors() const;
287 
288  /// Return the root node of the syntax tree corresponding to the
289  /// outermost expression if this is a valid AST or \c nullptr
290  /// otherwise. The returned pointer is valid for the lifetime of
291  /// its associated SdfVariableExpressionAST.
293 
294  /// \overload
295  SDF_API const SdfVariableExpressionASTNodes::Node* GetRoot() const;
296 
297  /// Return the expression represented by this AST if this is a
298  /// valid AST or a default constructed SdfVariableExpression otherwise.
299  ///
300  /// The expression string may be formatted differently than what
301  /// was in the original expression passed to the constructor.
302  ///
303  /// The returned SdfVariableExpression may be an invalid expression
304  /// that cannot be evaluated, e.g. if the expression contains an
305  /// unrecognized function name. However, calling GetString on the
306  /// returned SdfVariableExpression will still return the expression
307  /// string for inspection.
309 
310 private:
311  std::unique_ptr<SdfVariableExpressionASTNodes::Node> _node;
312  std::vector<std::string> _errors;
313 };
314 
316 
317 #endif
SDF_API SdfVariableExpression::Builder GetExpressionBuilder() const
SDF_API std::vector< Node * > GetNodes()
Return list of nodes in this collection.
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
OutGridT const XformOp bool bool
SDF_API std::unique_ptr< Node > Clone() const
SDF_API void Insert(size_t i, const Node &node)
SDF_API void Append(const Node &node)
Append a copy of node to the end of this collection.
SDF_API NodeList Clone() const
Copy all nodes in this collection to a new collection.
GLuint const GLchar * name
Definition: glcorearb.h:786
virtual SdfVariableExpression::Builder _GetExpressionBuilder() const =0
#define SDF_API
Definition: api.h:23
SDF_API void Clear()
Clear all nodes.
std::variant< std::monostate, bool, int64_t, std::string > LiteralValue
LeafData & operator=(const LeafData &)=delete
SDF_API void Set(size_t i, const Node &node)
virtual Node * _Clone() const =0
SDF_API SdfVariableExpression GetExpression() const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74