HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SdfPredicateExpression Class Reference

#include <predicateExpression.h>

Classes

class  FnArg
 
class  FnCall
 

Public Types

enum  Op {
  Call, Not, ImpliedAnd, And,
  Or
}
 Enumerant describing a subexpression operation. More...
 

Public Member Functions

 SdfPredicateExpression ()=default
 Construct the empty expression whose bool-operator returns false. More...
 
 SdfPredicateExpression (SdfPredicateExpression const &)=default
 Copy construct from another expression. More...
 
 SdfPredicateExpression (SdfPredicateExpression &&)=default
 Move construct from another expression. More...
 
SDF_API SdfPredicateExpression (std::string const &expr, std::string const &context={})
 
SdfPredicateExpressionoperator= (SdfPredicateExpression const &)=default
 Copy assign from another expression. More...
 
SdfPredicateExpressionoperator= (SdfPredicateExpression &&)=default
 Move assign from another expression. More...
 
SDF_API void Walk (TfFunctionRef< void(Op, int)> logic, TfFunctionRef< void(FnCall const &)> call) const
 
SDF_API void WalkWithOpStack (TfFunctionRef< void(std::vector< std::pair< Op, int >> const &)> logic, TfFunctionRef< void(FnCall const &)> call) const
 
SDF_API std::string GetText () const
 
bool IsEmpty () const
 
 operator bool () const
 Return true if this expression contains any operations, false otherwise. More...
 
std::string constGetParseError () const &
 
std::string GetParseError () const &&
 

Static Public Member Functions

static SDF_API
SdfPredicateExpression 
MakeNot (SdfPredicateExpression &&right)
 Produce a new expression by prepending the 'not' operator onto right. More...
 
static SDF_API
SdfPredicateExpression 
MakeOp (Op op, SdfPredicateExpression &&left, SdfPredicateExpression &&right)
 
static SDF_API
SdfPredicateExpression 
MakeCall (FnCall &&call)
 Produce a new expression containing just a the function call call. More...
 

Detailed Description

Represents a logical expression syntax tree consisting of predicate function calls joined by the logical operators 'and', 'or', 'not', and an implied-and operator that represents two subexpressions joined by only whitespace.

An SdfPredicateExpression can be constructed with a string, which will parse an expression. The syntax for an expression is as follows:

The fundamental building blocks are calls to predicate functions. There are three syntaxes for function calls.

  • Bare call: just a function name: isDefined
  • Colon call: name, colon, positional arguments: isa:mammal,bird
  • Paren call: name and parenthesized positional and keyword args: isClose(1.23, tolerance=0.01)

Colon call arguments are all positional and must be separated by commas with no spaces between arguments. In paren calls, positional arguments must precede keyword arguments, and whitespace is allowed between arguments.

The string parser supports argument values of the following types: double-quoted "strings", unquoted strings, integers, floating-point numbers, and boolean values 'true' and 'false'.

The unary operator 'not' may appear preceding a function call, or a subexpresion enclosed in parentheses. The binary operators 'and' and 'or' may appear between subexpressions. If subexpressions appear adjacent to each other (other than possible whitespace), this is considered an implied 'and' operator.

Operator precedence in order from highest to lowest is: 'not', <implied-and>, 'and', 'or'.

Here are some examples of valid predicate expression syntax:

  • foo (call "foo" with no arguments)
  • foo bar (implicit 'and' of "foo" and "bar")
  • foo not bar (implicit 'and' of "foo" and "not bar")
  • color:red (shiny or matte)
  • animal or mineral or vegetable
  • (mammal or bird) and (tame or small)
  • isClose(100, tolerance=3.0) or negative

Definition at line 80 of file predicateExpression.h.

Member Enumeration Documentation

Enumerant describing a subexpression operation.

Enumerator
Call 
Not 
ImpliedAnd 
And 
Or 

Definition at line 147 of file predicateExpression.h.

Constructor & Destructor Documentation

SdfPredicateExpression::SdfPredicateExpression ( )
default

Construct the empty expression whose bool-operator returns false.

SdfPredicateExpression::SdfPredicateExpression ( SdfPredicateExpression const )
default

Copy construct from another expression.

SdfPredicateExpression::SdfPredicateExpression ( SdfPredicateExpression &&  )
default

Move construct from another expression.

SDF_API SdfPredicateExpression::SdfPredicateExpression ( std::string const expr,
std::string const context = {} 
)
explicit

Construct an expression by parsing expr. If provided, context appears in a parse error, if one is generated. See GetParseError(). See the class documentation for details on expression syntax.

Member Function Documentation

std::string const& SdfPredicateExpression::GetParseError ( ) const
inline

Return parsing errors as a string if this function was constructed from a string and parse errors were encountered.

Definition at line 228 of file predicateExpression.h.

std::string SdfPredicateExpression::GetParseError ( ) const
inline

Return parsing errors as a string if this function was constructed from a string and parse errors were encountered.

Definition at line 234 of file predicateExpression.h.

SDF_API std::string SdfPredicateExpression::GetText ( ) const

Return a text representation of this expression that parses to the same expression.

bool SdfPredicateExpression::IsEmpty ( ) const
inline

Return true if this is the empty expression; i.e. default-constructed or constructed from a string with invalid syntax.

Definition at line 217 of file predicateExpression.h.

static SDF_API SdfPredicateExpression SdfPredicateExpression::MakeCall ( FnCall &&  call)
static

Produce a new expression containing just a the function call call.

static SDF_API SdfPredicateExpression SdfPredicateExpression::MakeNot ( SdfPredicateExpression &&  right)
static

Produce a new expression by prepending the 'not' operator onto right.

static SDF_API SdfPredicateExpression SdfPredicateExpression::MakeOp ( Op  op,
SdfPredicateExpression &&  left,
SdfPredicateExpression &&  right 
)
static

Produce a new expression by combining left and right with the operator op. The op must be one of ImpliedAnd, And, or Or.

SdfPredicateExpression::operator bool ( ) const
inlineexplicit

Return true if this expression contains any operations, false otherwise.

Definition at line 222 of file predicateExpression.h.

SdfPredicateExpression& SdfPredicateExpression::operator= ( SdfPredicateExpression const )
default

Copy assign from another expression.

SdfPredicateExpression& SdfPredicateExpression::operator= ( SdfPredicateExpression &&  )
default

Move assign from another expression.

SDF_API void SdfPredicateExpression::Walk ( TfFunctionRef< void(Op, int)>  logic,
TfFunctionRef< void(FnCall const &)>  call 
) const

Walk this expression's syntax tree in depth-first order, calling call with the current function call when a function call is encountered, and calling logic multiple times for each logical operation encountered. When calling logic, the logical operation is passed as the Op parameter, and an integer indicating "where" we are in the set of operands is passed as the int parameter. For a 'not', call logic(Op=Not, int=0) to start, then after the subexpression that the 'not' applies to is walked, call logic(Op=Not, int=1). For the binary operators like 'and' and 'or', call logic(Op, 0) before the first argument, then logic(Op, 1) after the first subexpression, then logic(Op, 2) after the second subexpression. For a concrete example, consider the following expression:

(foo or bar) and not baz

The sequence of calls from Walk() will be:

logic(And, 0)
logic(Or, 0)
call("foo")
logic(Or, 1)
call("bar")
logic(Or, 2)
logic(And, 1)
logic(Not, 0)
call("baz")
logic(Not, 1)
logic(And, 2)
SDF_API void SdfPredicateExpression::WalkWithOpStack ( )

Equivalent to Walk(), except that the logic function is called with a const reference to the current Op stack instead of just the top of it. The top of the Op stack is the vector's back. This is useful in case the processing code needs to understand the context in which an Op appears.


The documentation for this class was generated from the following file: