Documentation ¶
Overview ¶
Package compile is responsible for transforming an AST of a template into Go source code.
Index ¶
- Variables
- func AppendError(err, err2 error) error
- func Compile(out io.Writer, node *parse.Tree, pkg *types.Package, funcs ...map[string]Func) error
- func CompileTypeExpr(pkg *types.Package, ident string) (types.Type, error)
- func MakePackageFuncs(pkg *types.Package) map[string]Func
- func MakeParseFuncs(funcs ...map[string]Func) map[string]struct{}
- type Context
- func (ctx *Context) AddVariable(name string, t types.Type)
- func (ctx *Context) CompileActionNode(node *parse.ActionNode) error
- func (ctx *Context) CompileChainNode(node *parse.ChainNode) (types.Type, error)
- func (ctx *Context) CompileCommandNode(node *parse.CommandNode) (types.Type, error)
- func (ctx *Context) CompileCommandNodeArg(node parse.Node, implicit bool) (types.Type, error)
- func (ctx *Context) CompileCommandNodeArgTruthy(node parse.Node) (types.Type, error)
- func (ctx *Context) CompileFieldNode(node *parse.FieldNode) (types.Type, error)
- func (ctx *Context) CompileIfNode(node *parse.IfNode) error
- func (ctx *Context) CompileIfNodePipe(node *parse.PipeNode) (types.Type, error)
- func (ctx *Context) CompileListNode(node *parse.ListNode) error
- func (ctx *Context) CompileNumberNode(node *parse.NumberNode) (types.Type, error)
- func (ctx *Context) CompilePipeNode(node *parse.PipeNode) (types.Type, error)
- func (ctx *Context) CompilePipeNodeDecl(node *parse.PipeNode)
- func (ctx *Context) CompileRangeNode(node *parse.RangeNode) error
- func (ctx *Context) CompileTemplateNode(node *parse.TemplateNode) error
- func (ctx *Context) CompileTextNode(node *parse.TextNode)
- func (ctx *Context) CompileVariableNode(node *parse.VariableNode) (types.Type, error)
- func (ctx *Context) CompileWithNode(node *parse.WithNode) error
- func (ctx *Context) FindFunction(name string) Func
- func (ctx *Context) SetDot(dot types.Type)
- func (ctx *Context) WithNewScope() *Context
- func (ctx *Context) WithWriter(out io.Writer) *Context
- func (ctx *Context) Writer() io.Writer
- type Error
- type Func
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var Funcs = map[string]Func{ "eq": binaryComparison('='<<8 | '='), "ne": binaryComparison('!'<<8 | '='), "gt": binaryComparison('>'), "lt": binaryComparison('<'), "ge": binaryComparison('>'<<8 | '='), "le": binaryComparison('<'<<8 | '='), "not": (*notFn)(nil), "or": logicalFn('|'<<8 | '|'), "and": logicalFn('&'<<8 | '&'), "call": (*callFn)(nil), "len": (*lenFn)(nil), "index": (*indexFn)(nil), "slice": (*sliceFn)(nil), "print": printFn(0), "printf": printFn('f'), "println": printFn('l'<<8 | 'n'), }
Functions ¶
func AppendError ¶
AppendError returns an error that supports appending errors together to create a multi-error. It handles cases where either value is nil. The error formats as the concatenation of the strings obtained by calling the Error method of each element of errs, with a newline between each string.
A non-nil error returned by AppendError may implement the Unwrap() []error method, but only if more than one error was joined together.
func MakeParseFuncs ¶
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
func NewContext ¶
func (*Context) CompileActionNode ¶
func (ctx *Context) CompileActionNode(node *parse.ActionNode) error
func (*Context) CompileChainNode ¶
func (*Context) CompileCommandNode ¶
func (*Context) CompileCommandNodeArg ¶
func (*Context) CompileCommandNodeArgTruthy ¶
func (*Context) CompileFieldNode ¶
func (*Context) CompileIfNodePipe ¶
func (*Context) CompileNumberNode ¶
func (*Context) CompilePipeNode ¶
func (*Context) CompilePipeNodeDecl ¶
func (*Context) CompileRangeNode ¶
func (*Context) CompileTemplateNode ¶
func (ctx *Context) CompileTemplateNode(node *parse.TemplateNode) error
func (*Context) CompileTextNode ¶
func (*Context) CompileVariableNode ¶
func (*Context) FindFunction ¶
func (*Context) WithNewScope ¶
type Error ¶
Error is an error found while trying to compile a node of the AST.
Example ¶
package main import ( "fmt" "strings" "git.sr.ht/~rj/gemplate/compile" "git.sr.ht/~rj/gemplate/parse" ) func main() { tmpl := "{{ slice 1 2 3 4 5}}" // Too many arguments tree, err := parse.Parse("example.tmpl", tmpl, "", "", nil, compile.MakeParseFuncs(compile.Funcs)) if err != nil { // Abbreviate error handling for the example. panic(err) } b := &strings.Builder{} ctx := compile.NewContext(b, nil, compile.Funcs) err = ctx.CompileListNode(tree["example.tmpl"].Root) if err != nil { fmt.Printf("%s\n", err) } }
Output: example.tmpl:1:3: expected between 1 and 4 arguments, found 5
Click to show internal directories.
Click to hide internal directories.