2017-07-13 15:55:36 +02:00
|
|
|
package expression
|
|
|
|
|
|
|
|
// These strings match lexer tokens.
|
|
|
|
const (
|
|
|
|
AssignStatementSelector = "%assign "
|
|
|
|
CycleStatementSelector = "{%cycle "
|
|
|
|
LoopStatementSelector = "%loop "
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Statement is the result of parsing a string.
|
|
|
|
type Statement struct{ parseValue }
|
|
|
|
|
2017-07-13 16:08:17 +02:00
|
|
|
// Expression returns a statement's expression function.
|
2017-07-13 15:55:36 +02:00
|
|
|
func (s *Statement) Expression() Expression { return &expression{s.val} }
|
|
|
|
|
|
|
|
// Assignment captures the parse of an {% assign %} statement
|
|
|
|
type Assignment struct {
|
2017-07-13 16:08:17 +02:00
|
|
|
Variable string
|
|
|
|
ValueFn Expression
|
2017-07-13 15:55:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop captures the parse of a {% loop %} statement
|
|
|
|
type Loop struct {
|
|
|
|
Variable string
|
2017-07-13 16:08:17 +02:00
|
|
|
Expr Expression
|
2017-07-13 15:55:36 +02:00
|
|
|
loopModifiers
|
|
|
|
}
|
|
|
|
|
|
|
|
type loopModifiers struct {
|
|
|
|
Limit *int
|
|
|
|
Offset int
|
|
|
|
Reversed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseStatement parses an statement into an Expression that can evaluated to return a
|
|
|
|
// structure specific to the statement.
|
|
|
|
func ParseStatement(sel, source string) (*Statement, error) {
|
|
|
|
p, err := parse(sel + source)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Statement{*p}, nil
|
|
|
|
}
|