HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pathPatternParser.h
Go to the documentation of this file.
1 //
2 // Copyright 2024 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_USD_SDF_PATH_PATTERN_PARSER_H
9 #define PXR_USD_SDF_PATH_PATTERN_PARSER_H
10 
11 #include "pxr/pxr.h"
12 
14 #include "pxr/base/pegtl/pegtl.hpp"
15 
16 #include <memory>
17 
19 
20 namespace SdfPathPatternParser {
21 
22 using namespace PXR_PEGTL_NAMESPACE;
23 
24 template <class Rule, class Sep>
25 using LookaheadList = seq<Rule, star<at<Sep, Rule>, Sep, Rule>>;
26 
27 template <class Rule> using OptSpaced = pad<Rule, blank>;
28 
29 ////////////////////////////////////////////////////////////////////////
30 // Path patterns with predicates.
31 struct PathPatStretch : two<'/'> {};
32 struct PathPatSep : sor<PathPatStretch, one<'/'>> {};
33 
35  : if_must<one<'{'>,
36  OptSpaced<SdfPredicateExpressionParser::PredExpr>,
37  one<'}'>> {};
38 
40  seq<
41  plus<sor<identifier_other, one<'?','*'>>>,
42  opt<one<'['>,plus<sor<identifier_other, one<'[',']','!','-','?','*'>>>>
43  > {};
44 
46  seq<
47  plus<sor<identifier_other, one<':','?','*'>>>,
48  opt<one<'['>,plus<sor<identifier_other, one<':','[',']','!','-','?','*'>>>>
49  > {};
50 
53 
55  : if_then_else<PrimPathPatternElemText, opt<BracedPredExpr>,
56  BracedPredExpr> {};
57 
59  : if_then_else<PropPathPatternElemText, opt<BracedPredExpr>,
60  BracedPredExpr> {};
61 
63  : seq<LookaheadList<PrimPathPatternElem, PathPatSep>,
64  if_must_else<one<'.'>, PropPathPatternElem, opt<PathPatStretch>>> {};
65 
66 struct AbsPathPattern : seq<PathPatSep, opt<PathPatternElems>> {};
67 
68 struct DotDot : two<'.'> {};
69 struct DotDots : list<DotDot, one<'/'>> {};
70 
71 struct ReflexiveRelative : one<'.'> {};
72 
73 struct AbsoluteStart : at<one<'/'>> {};
74 
75 struct PathPattern :
76  sor<
77  if_must<AbsoluteStart, AbsPathPattern>,
78  seq<DotDots, if_then_else<PathPatSep, opt<PathPatternElems>, success>>,
79  PathPatternElems,
80  seq<ReflexiveRelative, opt<PathPatStretch, opt<PathPatternElems>>>
81  >
82 {};
83 
84 } // SdfPathPatternParser
85 
86 
87 namespace SdfPathPatternActions {
88 
89 using namespace PXR_PEGTL_NAMESPACE;
90 
91 using namespace SdfPathPatternParser;
92 
93 // Actions /////////////////////////////////////////////////////////////
94 
96 {
97  // The final resulting pattern winds up here.
99 
100  // These are used during parsing.
101  std::string curElemText;
103 };
104 
105 
106 template <class Rule>
107 struct PathPatternAction : nothing<Rule> {};
108 
109 template <>
111 {
112  template <class Input>
113  static void apply(Input const &in, PatternBuilder &builder) {
115  }
116 };
117 
118 template <>
120 {
121  template <class Input>
122  static void apply(Input const &in, PatternBuilder &builder) {
123  // '//' appends a component representing arbitrary hierarchy.
124  TF_VERIFY(builder.pattern.AppendStretchIfPossible());
125  }
126 };
127 
128 // Change action & state to the PredicateExpressionParser so it can parse &
129 // build a predicate expression for us.
130 template <>
131 struct PathPatternAction<SdfPredicateExpressionParser::PredExpr>
132  : change_action_and_states<SdfPredicateExpressionParser::PredAction,
133  SdfPredicateExprBuilder>
134 {
135  template <class Input>
136  static void success(Input const &in,
137  SdfPredicateExprBuilder &predExprBuilder,
138  PatternBuilder &builder) {
139  builder.curPredExpr = predExprBuilder.Finish();
140  }
141 };
142 
143 template <>
145 {
146  template <class Input>
147  static void apply(Input const &in, PatternBuilder &builder) {
148  builder.curElemText = in.string();
149  }
150 };
151 
152 template <>
154 {
155  template <class Input>
156  static void apply(Input const &in, PatternBuilder &builder) {
157  builder.curElemText = in.string();
158  }
159 };
160 
161 template <>
163 {
164  template <class Input>
165  static void apply(Input const &in, PatternBuilder &builder) {
166  builder.pattern.AppendChild(builder.curElemText, builder.curPredExpr);
167  builder.curElemText.clear();
169  }
170 };
171 
172 template <>
174 {
175  template <class Input>
176  static void apply(Input const &in, PatternBuilder &builder) {
177  builder.pattern.AppendProperty(builder.curElemText,
178  builder.curPredExpr);
179  builder.curElemText.clear();
181  }
182 };
183 
184 template <>
186 {
187  template <class Input>
188  static void apply(Input const &in, PatternBuilder &builder) {
190  }
191 };
192 
193 template <>
195 {
196  template <class Input>
197  static void apply(Input const &in, PatternBuilder &builder) {
198  builder.pattern.AppendChild("..");
199  }
200 };
201 
202 } // SdfPathPatternActions
203 
205 
206 #endif // PXR_USD_SDF_PATH_PATTERN_PARSER_H
207 
static SDF_API const SdfPath & AbsoluteRootPath()
SDF_API SdfPathPattern & AppendChild(std::string const &text, SdfPredicateExpression &&predExpr)
static void apply(Input const &in, PatternBuilder &builder)
static void apply(Input const &in, PatternBuilder &builder)
SDF_API SdfPathPattern & SetPrefix(SdfPath &&p)
constexpr auto in(type t, int set) -> bool
Definition: core.h:611
static void apply(Input const &in, PatternBuilder &builder)
pad< Rule, blank > OptSpaced
static void apply(Input const &in, PatternBuilder &builder)
static void apply(Input const &in, PatternBuilder &builder)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
SdfPredicateExpression Finish()
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
static void apply(Input const &in, PatternBuilder &builder)
static void success(Input const &in, SdfPredicateExprBuilder &predExprBuilder, PatternBuilder &builder)
SDF_API SdfPathPattern & AppendStretchIfPossible()
SDF_API SdfPathPattern & AppendProperty(std::string const &text, SdfPredicateExpression &&predExpr)
static void apply(Input const &in, PatternBuilder &builder)
static SDF_API const SdfPath & ReflexiveRelativePath()
The relative path representing "self".
static void apply(Input const &in, PatternBuilder &builder)
seq< Rule, star< at< Sep, Rule >, Sep, Rule >> LookaheadList