2017-07-07 17:55:41 +02:00
|
|
|
//go:generate ragel -Z scanner.rl
|
2017-07-09 22:24:17 +02:00
|
|
|
//go:generate gofmt -w scanner.go
|
2017-07-07 17:55:41 +02:00
|
|
|
//go:generate goyacc expressions.y
|
|
|
|
|
2017-07-14 02:18:23 +02:00
|
|
|
package expressions
|
2017-06-27 21:10:44 +02:00
|
|
|
|
2017-07-05 20:24:15 +02:00
|
|
|
import "fmt"
|
2017-06-27 21:10:44 +02:00
|
|
|
|
2017-07-13 15:55:36 +02:00
|
|
|
type parseValue struct {
|
2017-07-13 16:08:17 +02:00
|
|
|
Assignment
|
2017-07-13 17:07:45 +02:00
|
|
|
Cycle
|
2017-07-13 16:08:17 +02:00
|
|
|
Loop
|
2017-07-13 17:59:11 +02:00
|
|
|
When
|
|
|
|
val func(Context) interface{}
|
2017-06-27 21:10:44 +02:00
|
|
|
}
|
|
|
|
|
2017-07-19 00:39:38 +02:00
|
|
|
// SyntaxError represents a syntax error. The yacc-generated compiler
|
2017-07-13 15:55:36 +02:00
|
|
|
// doesn't use error returns; this lets us recognize them.
|
2017-07-19 00:39:38 +02:00
|
|
|
type SyntaxError string
|
2017-06-27 21:10:44 +02:00
|
|
|
|
2017-07-19 00:39:38 +02:00
|
|
|
func (e SyntaxError) Error() string { return string(e) }
|
2017-06-27 21:10:44 +02:00
|
|
|
|
|
|
|
// Parse parses an expression string into an Expression.
|
|
|
|
func Parse(source string) (expr Expression, err error) {
|
2017-07-13 15:55:36 +02:00
|
|
|
p, err := parse(source)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &expression{p.val}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parse(source string) (p *parseValue, err error) {
|
2017-06-27 21:10:44 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
switch e := r.(type) {
|
2017-07-19 00:39:38 +02:00
|
|
|
case SyntaxError:
|
2017-06-27 21:10:44 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2017-07-13 15:55:36 +02:00
|
|
|
// FIXME hack to recognize EOF
|
|
|
|
lex := newLexer([]byte(source + ";"))
|
|
|
|
n := yyParse(lex)
|
2017-06-27 21:10:44 +02:00
|
|
|
if n != 0 {
|
2017-07-19 00:39:38 +02:00
|
|
|
return nil, SyntaxError(fmt.Errorf("syntax error in %q", source).Error())
|
2017-06-27 21:10:44 +02:00
|
|
|
}
|
2017-07-13 15:55:36 +02:00
|
|
|
return &lex.parseValue, nil
|
2017-07-13 15:12:02 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|