expressions

package
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2024 License: MIT Imports: 5 Imported by: 49

Documentation

Overview

Package expressions is an internal package that parses and evaluates the expression language.

This is the language that is used inside Liquid object and tags; e.g. "a.b[c]" in {{ a.b[c] }}, and "pages = site.pages | reverse" in {% assign pages = site.pages | reverse %}.

Index

Constants

View Source
const (
	AssignStatementSelector = "%assign "
	CycleStatementSelector  = "{%cycle "
	LoopStatementSelector   = "%loop "
	WhenStatementSelector   = "{%when "
)

These strings match lexer tokens.

View Source
const AND = 57359
View Source
const ASSIGN = 57350
View Source
const CONTAINS = 57361
View Source
const CYCLE = 57351
View Source
const DOTDOT = 57362
View Source
const EQ = 57354
View Source
const GE = 57356
View Source
const IDENTIFIER = 57347
View Source
const IN = 57358
View Source
const KEYWORD = 57348
View Source
const LE = 57357
View Source
const LITERAL = 57346
View Source
const LOOP = 57352
View Source
const NEQ = 57355
View Source
const OR = 57360
View Source
const PROPERTY = 57349
View Source
const WHEN = 57353

Variables

This section is empty.

Functions

func EvaluateString

func EvaluateString(source string, ctx Context) (any, error)

EvaluateString is a wrapper for Parse and Evaluate.

Types

type Assignment

type Assignment struct {
	Variable string
	ValueFn  Expression
}

An Assignment is a parse of an {% assign %} statement

type Closure

type Closure interface {
	// Bind creates a new closure with a new binding.
	Bind(name string, value any) Closure
	Evaluate() (any, error)
}

A Closure is an expression within a lexical environment. A closure may refer to variables that are not defined in the environment. (Therefore it's not a technically a closure.)

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config holds configuration information for expression interpretation.

func NewConfig

func NewConfig() Config

NewConfig creates a new Config.

func (*Config) AddFilter

func (c *Config) AddFilter(name string, fn any)

AddFilter adds a filter to the filter dictionary.

type Context

type Context interface {
	ApplyFilter(string, valueFn, []valueFn) (any, error)
	// Clone returns a copy with a new variable binding map
	// (so that copy.Set does effect the source context.)
	Clone() Context
	Get(string) any
	Set(string, any)
}

Context is the expression evaluation context. It maps variables names to values.

func NewContext

func NewContext(vars map[string]any, cfg Config) Context

NewContext makes a new expression evaluation context.

type Cycle

type Cycle struct {
	Group  string
	Values []string
}

A Cycle is a parse of an {% assign %} statement

type Expression

type Expression interface {
	// Evaluate evaluates an expression in a context.
	Evaluate(ctx Context) (any, error)
}

An Expression is a compiled expression.

func Constant

func Constant(k any) Expression

Constant creates an expression that returns a constant value.

func Not

func Not(e Expression) Expression

Not creates an expression that returns ! of the wrapped expression.

func Parse

func Parse(source string) (expr Expression, err error)

Parse parses an expression string into an Expression.

type FilterError added in v1.2.1

type FilterError struct {
	FilterName string
	Err        error
}

FilterError is the error returned by a filter when it is applied

func (FilterError) Error added in v1.2.1

func (e FilterError) Error() string

type InterpreterError

type InterpreterError string

An InterpreterError is an error during expression interpretation. It is used for errors in the input expression, to distinguish them from implementation errors in the interpreter.

func (InterpreterError) Error

func (e InterpreterError) Error() string

type Loop

type Loop struct {
	Variable string
	Expr     Expression
	// contains filtered or unexported fields
}

A Loop is a parse of a {% loop %} statement

type Statement

type Statement struct {
	// contains filtered or unexported fields
}

A Statement is the result of parsing a string.

func ParseStatement

func ParseStatement(sel, source string) (*Statement, error)

ParseStatement parses an statement into an Expression that can evaluated to return a structure specific to the statement.

type SyntaxError added in v1.1.2

type SyntaxError string

SyntaxError represents a syntax error. The yacc-generated compiler doesn't use error returns; this lets us recognize them.

func (SyntaxError) Error added in v1.1.2

func (e SyntaxError) Error() string

type UndefinedFilter

type UndefinedFilter string

UndefinedFilter is an error that the named filter is not defined.

func (UndefinedFilter) Error

func (e UndefinedFilter) Error() string

type When

type When struct {
	Exprs []Expression
}

A When is a parse of a {% when %} clause

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL