Loading...
Searching...
No Matches
pathPatternParser.h
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
13#include "pxr/usd/sdf/predicateExpressionParser.h"
14#include "pxr/base/pegtl/pegtl.hpp"
15
16#include <memory>
17
18PXR_NAMESPACE_OPEN_SCOPE
19
20namespace SdfPathPatternParser {
21
22using namespace PXR_PEGTL_NAMESPACE;
23
24template <class Rule, class Sep>
25using LookaheadList = seq<Rule, star<at<Sep, Rule>, Sep, Rule>>;
26
27template <class Rule> using OptSpaced = pad<Rule, blank>;
28
30// Path patterns with predicates.
31struct PathPatStretch : two<'/'> {};
32struct PathPatSep : sor<PathPatStretch, one<'/'>> {};
33
34struct BracedPredExpr
35 : if_must<one<'{'>,
36 OptSpaced<SdfPredicateExpressionParser::PredExpr>,
37 one<'}'>> {};
38
39struct PrimPathWildCard :
40 seq<
41 plus<sor<identifier_other, one<'?','*'>>>,
42 opt<one<'['>,plus<sor<identifier_other, one<'[',']','!','-','?','*'>>>>
43 > {};
44
45struct PropPathWildCard :
46 seq<
47 plus<sor<identifier_other, one<':','?','*'>>>,
48 opt<one<'['>,plus<sor<identifier_other, one<':','[',']','!','-','?','*'>>>>
49 > {};
50
51struct PrimPathPatternElemText : PrimPathWildCard {};
52struct PropPathPatternElemText : PropPathWildCard {};
53
54struct PrimPathPatternElem
55 : if_then_else<PrimPathPatternElemText, opt<BracedPredExpr>,
56 BracedPredExpr> {};
57
58struct PropPathPatternElem
59 : if_then_else<PropPathPatternElemText, opt<BracedPredExpr>,
60 BracedPredExpr> {};
61
62struct PathPatternElems
63 : seq<LookaheadList<PrimPathPatternElem, PathPatSep>,
64 if_must_else<one<'.'>, PropPathPatternElem, opt<PathPatStretch>>> {};
65
66struct AbsPathPattern : seq<PathPatSep, opt<PathPatternElems>> {};
67
68struct DotDot : two<'.'> {};
69struct DotDots : list<DotDot, one<'/'>> {};
70
71struct ReflexiveRelative : one<'.'> {};
72
73struct AbsoluteStart : at<one<'/'>> {};
74
75struct 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
87namespace SdfPathPatternActions {
88
89using namespace PXR_PEGTL_NAMESPACE;
90
91using namespace SdfPathPatternParser;
92
93// Actions /////////////////////////////////////////////////////////////
94
95struct PatternBuilder
96{
97 // The final resulting pattern winds up here.
98 SdfPathPattern pattern;
99
100 // These are used during parsing.
101 std::string curElemText;
102 SdfPredicateExpression curPredExpr;
103};
104
105
106template <class Rule>
107struct PathPatternAction : nothing<Rule> {};
108
109template <>
110struct PathPatternAction<AbsoluteStart>
111{
112 template <class Input>
113 static void apply(Input const &in, PatternBuilder &builder) {
114 builder.pattern.SetPrefix(SdfPath::AbsoluteRootPath());
115 }
116};
117
118template <>
119struct PathPatternAction<PathPatStretch>
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.
130template <>
131struct 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
143template <>
144struct PathPatternAction<PrimPathPatternElemText>
145{
146 template <class Input>
147 static void apply(Input const &in, PatternBuilder &builder) {
148 builder.curElemText = in.string();
149 }
150};
151
152template <>
153struct PathPatternAction<PropPathPatternElemText>
154{
155 template <class Input>
156 static void apply(Input const &in, PatternBuilder &builder) {
157 builder.curElemText = in.string();
158 }
159};
160
161template <>
162struct PathPatternAction<PrimPathPatternElem>
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();
168 builder.curPredExpr = SdfPredicateExpression();
169 }
170};
171
172template <>
173struct PathPatternAction<PropPathPatternElem>
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();
180 builder.curPredExpr = SdfPredicateExpression();
181 }
182};
183
184template <>
185struct PathPatternAction<ReflexiveRelative>
186{
187 template <class Input>
188 static void apply(Input const &in, PatternBuilder &builder) {
189 builder.pattern.SetPrefix(SdfPath::ReflexiveRelativePath());
190 }
191};
192
193template <>
194struct PathPatternAction<DotDot>
195{
196 template <class Input>
197 static void apply(Input const &in, PatternBuilder &builder) {
198 builder.pattern.AppendChild("..");
199 }
200};
201
202} // SdfPathPatternActions
203
204PXR_NAMESPACE_CLOSE_SCOPE
205
206#endif // PXR_USD_SDF_PATH_PATTERN_PARSER_H
207
static SDF_API const SdfPath & AbsoluteRootPath()
The absolute path representing the top of the namespace hierarchy.
static SDF_API const SdfPath & ReflexiveRelativePath()
The relative path representing "self".
Objects of this class represent SdfPath matching patterns, consisting of an SdfPath prefix followed b...
Definition: pathPattern.h:31
Represents a logical expression syntax tree consisting of predicate function calls joined by the logi...
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:266