evaluator

package
v0.0.0-...-4998ed7 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package evaluator is used to evaluate (execute) an abstract syntax tree.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorLocation

func ErrorLocation(e error) (int, int, bool)

ErrorLocation returns the location in the template where the evaluation error e occurred. ok will be true if e actually was an evaluation error.

func IsEvaluationError

func IsEvaluationError(e error) bool

IsEvaluationError returns whether e is an evaluation error that occurred in the evaluator.

Types

type ArgumentResolver

type ArgumentResolver interface {
	// Resolve inspects the type t and returns a value for it. If no actual value can be produced, nil may be returned
	// as the value. The returned value must be convertible to the type t.
	Resolve(t reflect.Type) (interface{}, error)
}

An ArgumentResolver resolves additional arguments of methods or functions that should be called. For example, a method could expect the arguments "x int, y *FooBar", but the method call only specifies the first argument: "a.b(123)". In that case, the second *FooBar argument can be automatically resolved by the argument resolver.

type ArgumentResolverFunc

type ArgumentResolverFunc func(t reflect.Type) (interface{}, error)

An ArgumentResolverFunc is an adapter type that allows ordinary functions to be used as argument resolvers. If f is a function with the appropriate signature, ArgumentResolverFunc(f) is an argument resolver that calls f.

func (ArgumentResolverFunc) Resolve

func (r ArgumentResolverFunc) Resolve(t reflect.Type) (interface{}, error)

type Evaluator

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

Evaluator evaluates an abstract syntax tree node and returns its result.

func New

func New(opts ...Opt) *Evaluator

New returns a new evaluator, configured with opts.

func (*Evaluator) Eval

func (ev *Evaluator) Eval(n ast.Node, s *scope.Scope) (interface{}, error)

Eval evaluates the abstract syntax tree node n and returns its result. The scope s is used to look up and store variable state using identifiers. The scope may be pre-filled with identifiers which can be used during evaluation of expressions.

type LiteralStringer

type LiteralStringer interface {
	String(s string) (interface{}, error)
}

A LiteralStringer converts a literal string in a template to a value suitable for output. For example, it can wrap the string in a renderer.SafeString so that all literal strings are output as-is. It may also escape strings before wrapping them.

type LiteralStringerFunc

type LiteralStringerFunc func(s string) (interface{}, error)

A LiteralStringerFunc is an adapter type that allows ordinary functions to be used as literal stringers. If f is a function with the appropriate signature, LiteralStringerFunc(f) is a literal stringer that calls f.

func (LiteralStringerFunc) String

func (f LiteralStringerFunc) String(s string) (interface{}, error)

type Opt

type Opt func(ev *Evaluator)

Opt is the type of a function that configures an option of ev.

func WithArgumentResolver

func WithArgumentResolver(r ArgumentResolver) Opt

WithArgumentResolver configures an evaluator to use r to automatically resolve additional arguments of method or function calls in a template. The default is to not resolve any arguments.

WithArgumentResolver may be used multiple times to configure additional resolvers. The first resolver to return a value other than nil wins.

func WithLiteralStringer

func WithLiteralStringer(l LiteralStringer) Opt

WithLiteralStringer configures an evaluator to use l to convert literal strings in a template to values suitable for output. The default is to return strings as-is, without escaping.

Jump to

Keyboard shortcuts

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