Documentation ¶
Overview ¶
Package expr provides support for evaluating boolean expressions.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Expr ¶
type Expr struct {
// contains filtered or unexported fields
}
Expr holds a parsed boolean expression that matches some combination of attributes.
Expressions are supplied as strings consisting of the following tokens:
- Attributes, either as bare identifiers (only if compliant with https://golang.org/ref/spec#Identifiers) or as double-quoted strings (in which '*' characters are interpreted as wildcards)
- Binary operators: && (and), || (or)
- Unary operator: ! (not)
- Grouping: (, )
The expression syntax is conveniently a subset of Go's syntax, so Go's parser and ast packages are used to convert the initial expression into a binary expression tree.
After an Expr object is created from a string expression, it can be asked if it is satisfied by a supplied set of attributes.
Example ¶
package main import ( "fmt" "go.chromium.org/tast/core/internal/expr" ) func main() { e, _ := expr.New("a && (b || c) && !d") for _, attrs := range [][]string{ {"a"}, {"a", "b"}, {"a", "c"}, {"a", "c", "d"}, } { if e.Matches(attrs) { fmt.Println(attrs, "matched") } else { fmt.Println(attrs, "not matched") } } }
Output: [a] not matched [a b] matched [a c] matched [a c d] not matched
Example (Quoted) ¶
package main import ( "fmt" "go.chromium.org/tast/core/internal/expr" ) func main() { e, _ := expr.New("\"attr-hyphen\" && \"attr space\" && attr_under") if e.Matches([]string{"attr-hyphen", "attr space", "attr_under"}) { fmt.Println("matched") } }
Output: matched
Example (Wildcard) ¶
package main import ( "fmt" "go.chromium.org/tast/core/internal/expr" ) func main() { e, _ := expr.New("\"foo:*\" && !\"*bar\"") for _, attrs := range [][]string{ {"foo:"}, {"foo:a"}, {"foo:a", "bar"}, {"foo:a", "foo:bar"}, } { if e.Matches(attrs) { fmt.Println(attrs, "matched") } else { fmt.Println(attrs, "not matched") } } }
Output: [foo:] matched [foo:a] matched [foo:a bar] not matched [foo:a foo:bar] not matched
Click to show internal directories.
Click to hide internal directories.