1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 12:35:16 +01:00
liquid/expressions/parser.go

53 lines
994 B
Go
Raw Normal View History

2017-06-27 21:10:44 +02:00
package expressions
import (
"fmt"
"github.com/osteele/liquid/errors"
)
// Loop describes the result of parsing and then evaluating a loop statement.
type Loop struct {
Name string
Expr interface{}
LoopModifiers
}
type LoopModifiers struct {
Reversed bool
}
type ParseError string
func (e ParseError) Error() string { return string(e) }
2017-06-27 22:11:32 +02:00
type UnimplementedError string
func (e UnimplementedError) Error() string {
return fmt.Sprintf("unimplemented %s", 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) {
defer func() {
if r := recover(); r != nil {
switch e := r.(type) {
case ParseError:
err = e
2017-06-27 22:11:32 +02:00
case UnimplementedError:
err = e
2017-06-27 21:10:44 +02:00
case errors.UndefinedFilter:
err = e
default:
panic(r)
}
}
}()
lexer := newLexer([]byte(source + ";"))
n := yyParse(lexer)
if n != 0 {
return nil, fmt.Errorf("parse error in %s", source)
}
return &expression{lexer.val}, nil
}