2017-06-26 15:33:07 +02:00
|
|
|
package expressions
|
2017-06-26 14:23:50 +02:00
|
|
|
|
2017-06-26 15:06:55 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-27 14:46:06 +02:00
|
|
|
|
|
|
|
"github.com/osteele/liquid/errors"
|
2017-06-26 15:06:55 +02:00
|
|
|
)
|
2017-06-26 14:23:50 +02:00
|
|
|
|
2017-06-26 16:52:23 +02:00
|
|
|
// Expression is a parsed expression.
|
|
|
|
type Expression interface {
|
|
|
|
// Evaluate evaluates an expression in a context.
|
|
|
|
Evaluate(ctx Context) (interface{}, error)
|
2017-06-26 14:23:50 +02:00
|
|
|
}
|
|
|
|
|
2017-06-26 16:52:23 +02:00
|
|
|
type expression struct {
|
|
|
|
evaluator func(Context) interface{}
|
2017-06-26 14:23:50 +02:00
|
|
|
}
|
2017-06-26 15:06:55 +02:00
|
|
|
|
2017-06-26 16:52:23 +02:00
|
|
|
// Parse parses an expression string into an Expression.
|
2017-06-27 14:46:06 +02:00
|
|
|
func Parse(source string) (expr Expression, err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
switch e := r.(type) {
|
|
|
|
case errors.UndefinedFilter:
|
|
|
|
err = e
|
|
|
|
default:
|
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2017-06-26 16:52:23 +02:00
|
|
|
lexer := newLexer([]byte(source + ";"))
|
|
|
|
n := yyParse(lexer)
|
|
|
|
if n != 0 {
|
|
|
|
return nil, fmt.Errorf("parse error in %s", source)
|
2017-06-26 15:06:55 +02:00
|
|
|
}
|
2017-06-26 16:52:23 +02:00
|
|
|
return &expression{lexer.val}, nil
|
2017-06-26 15:06:55 +02:00
|
|
|
}
|
|
|
|
|
2017-06-26 16:52:23 +02:00
|
|
|
func (e expression) Evaluate(ctx Context) (interface{}, error) {
|
|
|
|
return e.evaluator(ctx), nil
|
2017-06-26 15:06:55 +02:00
|
|
|
}
|
|
|
|
|
2017-06-26 16:52:23 +02:00
|
|
|
// EvaluateExpr is a wrapper for Parse and Evaluate.
|
|
|
|
func EvaluateExpr(source string, ctx Context) (interface{}, error) {
|
|
|
|
expr, err := Parse(source)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-06-26 15:06:55 +02:00
|
|
|
}
|
2017-06-26 16:52:23 +02:00
|
|
|
return expr.Evaluate(ctx)
|
2017-06-26 15:06:55 +02:00
|
|
|
}
|