1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 10:44:38 +01:00
liquid/expressions/evaluator.go
2017-06-26 10:52:23 -04:00

45 lines
1.0 KiB
Go

package expressions
import (
"fmt"
)
// Context is the expression evaluation context. It maps variables names to values.
// It is currently a concrete type.
type Context struct {
Variables map[string]interface{}
}
// Expression is a parsed expression.
type Expression interface {
// Evaluate evaluates an expression in a context.
Evaluate(ctx Context) (interface{}, error)
}
type expression struct {
evaluator func(Context) interface{}
}
// Parse parses an expression string into an Expression.
func Parse(source string) (Expression, error) {
lexer := newLexer([]byte(source + ";"))
n := yyParse(lexer)
if n != 0 {
return nil, fmt.Errorf("parse error in %s", source)
}
return &expression{lexer.val}, nil
}
func (e expression) Evaluate(ctx Context) (interface{}, error) {
return e.evaluator(ctx), nil
}
// 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
}
return expr.Evaluate(ctx)
}