1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
package fexpr
import (
"errors"
"fmt"
)
var ErrEmpty = errors.New("empty filter expression")
var ErrIncomplete = errors.New("invalid or incomplete filter expression")
var ErrInvalidComment = errors.New("invalid comment")
// Expr represents an individual tokenized expression consisting
// of left operand, operator and a right operand.
type Expr struct {
Left Token
Op SignOp
Right Token
}
// IsZero checks if the current Expr has zero-valued props.
func (e Expr) IsZero() bool {
return e.Op == "" && e.Left.Literal == "" && e.Left.Type == "" && e.Right.Literal == "" && e.Right.Type == ""
}
// ExprGroup represents a wrapped expression and its join type.
//
// The group's Item could be either an `Expr` instance or `[]ExprGroup` slice (for nested expressions).
type ExprGroup struct {
Item interface{}
Join JoinOp
}
// parser's state machine steps
const (
stepBeforeSign = iota
stepSign
stepAfterSign
StepJoin
)
// Parse parses the provided text and returns its processed AST
// in the form of `ExprGroup` slice(s).
//
// Comments and whitespaces are ignored.
func Parse(text string) ([]ExprGroup, error) {
result := []ExprGroup{}
scanner := NewScanner([]byte(text))
step := stepBeforeSign
join := JoinAnd
var expr Expr
for {
t, err := scanner.Scan()
if err != nil {
return nil, err
}
if t.Type == TokenEOF {
break
}
if t.Type == TokenWS || t.Type == TokenComment {
continue
}
if t.Type == TokenGroup {
groupResult, err := Parse(t.Literal)
if err != nil {
return nil, err
}
// append only if non-empty group
if len(groupResult) > 0 {
result = append(result, ExprGroup{Join: join, Item: groupResult})
}
step = StepJoin
continue
}
switch step {
case stepBeforeSign:
if t.Type != TokenIdentifier && t.Type != TokenText && t.Type != TokenNumber && t.Type != TokenFunction {
return nil, fmt.Errorf("expected left operand (identifier, function, text or number), got %q (%s)", t.Literal, t.Type)
}
expr = Expr{Left: t}
step = stepSign
case stepSign:
if t.Type != TokenSign {
return nil, fmt.Errorf("expected a sign operator, got %q (%s)", t.Literal, t.Type)
}
expr.Op = SignOp(t.Literal)
step = stepAfterSign
case stepAfterSign:
if t.Type != TokenIdentifier && t.Type != TokenText && t.Type != TokenNumber && t.Type != TokenFunction {
return nil, fmt.Errorf("expected right operand (identifier, function text or number), got %q (%s)", t.Literal, t.Type)
}
expr.Right = t
result = append(result, ExprGroup{Join: join, Item: expr})
step = StepJoin
case StepJoin:
if t.Type != TokenJoin {
return nil, fmt.Errorf("expected && or ||, got %q (%s)", t.Literal, t.Type)
}
join = JoinAnd
if t.Literal == "||" {
join = JoinOr
}
step = stepBeforeSign
}
}
if step != StepJoin {
if len(result) == 0 && expr.IsZero() {
return nil, ErrEmpty
}
return nil, ErrIncomplete
}
return result, nil
}
|