2017-07-04 17:12:40 +02:00
|
|
|
package expression
|
2017-06-27 21:10:44 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Loop describes the result of parsing and then evaluating a loop statement.
|
|
|
|
type Loop struct {
|
2017-06-29 02:49:38 +02:00
|
|
|
Variable string
|
|
|
|
Expr interface{}
|
|
|
|
loopModifiers
|
2017-06-27 21:10:44 +02:00
|
|
|
}
|
|
|
|
|
2017-06-29 02:49:38 +02:00
|
|
|
type loopModifiers struct {
|
2017-06-28 22:18:32 +02:00
|
|
|
Limit *int
|
|
|
|
Offset int
|
2017-06-27 21:10:44 +02:00
|
|
|
Reversed bool
|
|
|
|
}
|
|
|
|
|
2017-07-02 06:10:54 +02:00
|
|
|
// ParseError represents a parse error.
|
2017-06-27 21:10:44 +02:00
|
|
|
type ParseError string
|
|
|
|
|
|
|
|
func (e ParseError) Error() string { return string(e) }
|
|
|
|
|
|
|
|
// Parse parses an expression string into an Expression.
|
|
|
|
func Parse(source string) (expr Expression, err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
switch e := r.(type) {
|
|
|
|
case ParseError:
|
|
|
|
err = e
|
2017-06-29 13:54:31 +02:00
|
|
|
case UndefinedFilter:
|
2017-06-27 21:10:44 +02:00
|
|
|
err = e
|
|
|
|
default:
|
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
lexer := newLexer([]byte(source + ";"))
|
|
|
|
n := yyParse(lexer)
|
|
|
|
if n != 0 {
|
2017-07-02 13:51:24 +02:00
|
|
|
return nil, fmt.Errorf("parse error in %q", source)
|
2017-06-27 21:10:44 +02:00
|
|
|
}
|
|
|
|
return &expression{lexer.val}, nil
|
|
|
|
}
|
2017-06-29 13:54:31 +02:00
|
|
|
|
|
|
|
// EvaluateString is a wrapper for Parse and Evaluate.
|
|
|
|
func EvaluateString(source string, ctx Context) (interface{}, error) {
|
|
|
|
expr, err := Parse(source)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return expr.Evaluate(ctx)
|
|
|
|
}
|