25 #ifndef PXR_USD_SDF_PREDICATE_EXPRESSION_PARSER_H
26 #define PXR_USD_SDF_PREDICATE_EXPRESSION_PARSER_H
34 #include "pxr/base/tf/pxrPEGTL/pegtl.h"
43 size_t trimBothSides,
unsigned int* numLines=NULL);
52 _stacks.back().PushCall(
53 kind, std::move(_funcName), std::move(_funcArgs));
63 _funcArgs.push_back({ std::move(_funcKwArgName), val });
64 _funcKwArgName.clear();
76 _stacks.back().PushExpr(std::move(innerExpr));
93 return (left <
right) || (left ==
right && left != Op::Not);
96 while (!opStack.empty() && higherPrec(opStack.back(), op)) {
99 opStack.push_back(op);
104 std::vector<SdfPredicateExpression::FnArg> &&
args) {
107 kind, std::move(
name), std::move(
args) }));
111 exprStack.push_back(std::move(expr));
115 while (!opStack.empty()) {
128 exprStack.pop_back();
138 exprStack.pop_back();
141 op, std::move(left), std::move(right))
147 std::vector<SdfPredicateExpression::Op> opStack;
148 std::vector<SdfPredicateExpression> exprStack;
151 std::vector<_Stack> _stacks;
155 std::vector<SdfPredicateExpression::FnArg> _funcArgs;
165 using namespace tao::TAO_PEGTL_NAMESPACE;
167 template <
class Rule,
class Sep>
168 using LookaheadList = seq<Rule, star<at<Sep, Rule>, Sep,
Rule>>;
170 template <
class Rule>
using OptSpaced = pad<Rule, blank>;
172 using OptSpacedComma = OptSpaced<one<','>>;
177 struct NotKW : keyword<'n','o','t'> {};
178 struct AndKW : keyword<'a','n','d'> {};
179 struct OrKW : keyword<'o','r'> {};
180 struct Inf : keyword<'i','n','f'> {};
181 struct True : keyword<'t','r','u','e'> {};
182 struct False : keyword<'f','a','l','s','e'> {};
183 struct ImpliedAnd :
plus<blank> {};
185 struct ReservedWord : sor<
186 NotKW, AndKW, OrKW, Inf, True, False> {};
188 struct Digits :
plus<range<'0','9'>> {};
190 struct Exp : seq<one<'e','E'>, opt<one<'-','+'>>, must<Digits>> {};
191 struct Frac : if_must<one<'.'>, Digits> {};
192 struct PredArgFloat : seq<
193 opt<one<'-'>>, sor<Inf, seq<Digits, if_then_else<Frac, opt<Exp>, Exp>>>
195 struct PredArgInt : seq<opt<one<'-'>>, Digits> {};
197 struct PredArgBool : sor<True, False> {};
199 template <
class Quote>
200 struct Escaped : sor<Quote, one<'\\', 'b', 'f', 'n', 'r', 't'>> {};
201 template <
class Quote>
202 struct Unescaped :
minus<utf8::range<0x20, 0x10FFFF>, Quote> {};
204 template <
class Quote>
205 struct StringChar : if_then_else<
206 one<'\\'>, must<Escaped<Quote>>, Unescaped<Quote>> {};
208 struct QuotedString : sor<
209 if_must<one<'"'>, until<one<'"'>, StringChar<one<'"'>>>>,
210 if_must<one<'\''>, until<one<'\''>, StringChar<one<'\''>>>>
213 struct UnquotedStringChar
214 : sor<identifier_other,
215 one<'~', '!', '@', '#', '$', '%', '^', '&', '*', '-', '+', '=',
216 '|', '\\', '.', '?', '/'>> {};
218 struct UnquotedString : star<UnquotedStringChar> {};
220 struct PredArgString : sor<QuotedString, UnquotedString> {};
222 struct PredArgVal : sor<
223 PredArgFloat, PredArgInt, PredArgBool, PredArgString> {};
225 struct PredKWArgName :
minus<identifier, ReservedWord> {};
227 struct PredKWArgPrefix : seq<PredKWArgName, OptSpaced<one<'='>>> {};
228 struct PredKWArg : if_must<PredKWArgPrefix, PredArgVal> {};
230 struct PredParenPosArg : seq<not_at<PredKWArgPrefix>, PredArgVal> {};
232 struct PredFuncName :
minus<identifier, ReservedWord> {};
235 : if_then_else<list<PredParenPosArg, OptSpacedComma>,
236 opt<OptSpacedComma, list<PredKWArg, OptSpacedComma>>,
237 opt<list<PredKWArg, OptSpacedComma>>>
240 struct PredColonArgs : list<PredArgVal, one<','>> {};
241 struct PredColonCall : if_must<seq<PredFuncName, one<':'>>, PredColonArgs> {};
242 struct PredParenCall : seq<
243 PredFuncName, OptSpaced<one<'('>>,
244 must<PredParenArgs, star<blank>, one<')'>>
248 struct PredBareCall : PredFuncName {};
252 struct PredOpenGroup : one<'('> {};
253 struct PredCloseGroup : one<')'> {};
260 if_must<PredOpenGroup, OptSpaced<PredExpr>, PredCloseGroup>
264 struct PredFactor : seq<opt<OptSpaced<list<NotKW, plus<blank>>>>, PredAtom> {};
265 struct PredOperator : sor<OptSpaced<AndKW>, OptSpaced<OrKW>, ImpliedAnd> {};
266 struct PredExpr : LookaheadList<PredFactor, PredOperator> {};
270 template <
class Rule>
271 struct PredAction : nothing<Rule> {};
273 template <SdfPredicateExpression::Op op>
276 template <
class Input>
282 template <>
struct PredAction<NotKW>
283 : PredOpAction<SdfPredicateExpression::Not> {};
284 template <>
struct PredAction<AndKW>
285 : PredOpAction<SdfPredicateExpression::And> {};
286 template <>
struct PredAction<OrKW>
287 : PredOpAction<SdfPredicateExpression::Or> {};
288 template <>
struct PredAction<ImpliedAnd>
289 : PredOpAction<SdfPredicateExpression::ImpliedAnd> {};
292 struct PredAction<PredOpenGroup>
294 template <
class Input>
301 struct PredAction<PredCloseGroup>
303 template <
class Input>
310 struct PredAction<PredFuncName>
312 template <
class Input>
319 struct PredAction<PredArgInt>
321 template <
class Input>
323 bool outOfRange =
false;
334 struct PredAction<PredArgBool>
336 template <
class Input>
343 struct PredAction<PredArgFloat>
345 template <
class Input>
349 if (instr ==
"inf") {
350 fval = std::numeric_limits<double>::infinity();
352 else if (instr ==
"-inf") {
353 fval = -std::numeric_limits<double>::infinity();
363 struct PredAction<PredArgString>
365 template <
class Input>
368 size_t trimAmount = 0;
369 if (instr.size() >= 2 &&
370 ((instr.front() ==
'"' && instr.back() ==
'"') ||
371 (instr.front() ==
'\'' && instr.back() ==
'\''))) {
376 instr.c_str(), instr.size(), trimAmount)));
381 struct PredAction<PredKWArgName>
383 template <
class Input>
389 template <SdfPredicateExpression::FnCall::Kind callKind>
390 struct PredCallAction
392 template <
class Input>
397 template <>
struct PredAction<PredBareCall>
398 : PredCallAction<SdfPredicateExpression::FnCall::BareCall> {};
399 template <>
struct PredAction<PredParenCall>
400 : PredCallAction<SdfPredicateExpression::FnCall::ParenCall> {};
401 template <>
struct PredAction<PredColonCall>
402 : PredCallAction<SdfPredicateExpression::FnCall::ColonCall> {};
404 template <
class Grammar>
405 static void Analyze() {
406 static const size_t numIssues = analyze<Grammar>();
409 TF_FUNC_NAME().c_str());
417 #endif // PXR_USD_SDF_PREDICATE_EXPRESSION_PARSER_H
GLsizei const GLchar *const * string
static SDF_API SdfPredicateExpression MakeCall(FnCall &&call)
Produce a new expression containing just a the function call call.
TF_API double TfStringToDouble(const std::string &txt)
**But if you need a result
void SetFuncArgKWName(std::string const &kw)
void PushCall(SdfPredicateExpression::FnCall::Kind kind)
void AddFuncArg(VtValue const &val)
PXR_NAMESPACE_OPEN_SCOPE std::string Sdf_EvalQuotedString(const char *x, size_t n, size_t trimBothSides, unsigned int *numLines=NULL)
GLuint const GLchar * name
static SDF_API SdfPredicateExpression MakeOp(Op op, SdfPredicateExpression &&left, SdfPredicateExpression &&right)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
static SDF_API SdfPredicateExpression MakeNot(SdfPredicateExpression &&right)
Produce a new expression by prepending the 'not' operator onto right.
SdfPredicateExpression Finish()
#define PXR_NAMESPACE_CLOSE_SCOPE
**If you just want to fire and args
SdfPredicateExprBuilder()
void PushOp(SdfPredicateExpression::Op op)
Op
Enumerant describing a subexpression operation.
TF_API int64_t TfStringToInt64(const std::string &txt, bool *outOfRange=NULL)
Type Exp(const Type &x)
Return ex.
void SetFuncName(std::string const &name)