compile

package
v0.0.0-...-a325e41 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package compile is responsible for transforming an AST of a template into Go source code.

Index

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

func AppendError(err, err2 error) error

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 Compile

func Compile(out io.Writer, node *parse.Tree, pkg *types.Package, funcs ...map[string]Func) error

func CompileTypeExpr

func CompileTypeExpr(pkg *types.Package, ident string) (types.Type, error)

func MakePackageFuncs

func MakePackageFuncs(pkg *types.Package) map[string]Func

func MakeParseFuncs

func MakeParseFuncs(funcs ...map[string]Func) map[string]struct{}

Types

type Context

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

func NewContext

func NewContext(out io.Writer, pkg *types.Package, funcs ...map[string]Func) *Context

func (*Context) AddVariable

func (ctx *Context) AddVariable(name string, t types.Type)

func (*Context) CompileActionNode

func (ctx *Context) CompileActionNode(node *parse.ActionNode) error

func (*Context) CompileChainNode

func (ctx *Context) CompileChainNode(node *parse.ChainNode) (types.Type, error)

func (*Context) CompileCommandNode

func (ctx *Context) CompileCommandNode(node *parse.CommandNode) (types.Type, error)

func (*Context) CompileCommandNodeArg

func (ctx *Context) CompileCommandNodeArg(node parse.Node, implicit bool) (types.Type, error)

func (*Context) CompileCommandNodeArgTruthy

func (ctx *Context) CompileCommandNodeArgTruthy(node parse.Node) (types.Type, error)

func (*Context) CompileFieldNode

func (ctx *Context) CompileFieldNode(node *parse.FieldNode) (types.Type, error)

func (*Context) CompileIfNode

func (ctx *Context) CompileIfNode(node *parse.IfNode) error

func (*Context) CompileIfNodePipe

func (ctx *Context) CompileIfNodePipe(node *parse.PipeNode) (types.Type, error)

func (*Context) CompileListNode

func (ctx *Context) CompileListNode(node *parse.ListNode) error

func (*Context) CompileNumberNode

func (ctx *Context) CompileNumberNode(node *parse.NumberNode) (types.Type, error)

func (*Context) CompilePipeNode

func (ctx *Context) CompilePipeNode(node *parse.PipeNode) (types.Type, error)

func (*Context) CompilePipeNodeDecl

func (ctx *Context) CompilePipeNodeDecl(node *parse.PipeNode)

func (*Context) CompileRangeNode

func (ctx *Context) CompileRangeNode(node *parse.RangeNode) error

func (*Context) CompileTemplateNode

func (ctx *Context) CompileTemplateNode(node *parse.TemplateNode) error

func (*Context) CompileTextNode

func (ctx *Context) CompileTextNode(node *parse.TextNode)

func (*Context) CompileVariableNode

func (ctx *Context) CompileVariableNode(node *parse.VariableNode) (types.Type, error)

func (*Context) CompileWithNode

func (ctx *Context) CompileWithNode(node *parse.WithNode) error

func (*Context) FindFunction

func (ctx *Context) FindFunction(name string) Func

func (*Context) SetDot

func (ctx *Context) SetDot(dot types.Type)

func (*Context) WithNewScope

func (ctx *Context) WithNewScope() *Context

func (*Context) WithWriter

func (ctx *Context) WithWriter(out io.Writer) *Context

func (*Context) Writer

func (ctx *Context) Writer() io.Writer

type Error

type Error struct {
	Node    parse.Node
	Message string
}

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

func (*Error) Error

func (err *Error) Error() string

type Func

type Func interface {
	Compile(*Context, *parse.CommandNode) (types.Type, error)
}

Func compiles the command node to a Go expression.

Jump to

Keyboard shortcuts

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