salix/salix.go

569 lines
15 KiB
Go
Raw Normal View History

2023-10-29 01:10:17 +00:00
package salix
import (
2023-11-01 18:31:51 +00:00
"bufio"
2023-12-28 15:19:58 +00:00
"bytes"
2023-10-29 01:10:17 +00:00
"errors"
"fmt"
"html"
"io"
"reflect"
2023-12-18 00:55:13 +00:00
"strconv"
2023-10-29 01:10:17 +00:00
2023-10-31 01:38:53 +00:00
"go.elara.ws/salix/ast"
2023-10-29 01:10:17 +00:00
)
// HTML represents unescaped HTML strings
2023-10-29 01:10:17 +00:00
type HTML string
// Template represents a Salix template
type Template struct {
ns *Namespace
name string
2023-10-29 01:10:17 +00:00
ast []ast.Node
escapeHTML *bool
2023-12-28 15:19:58 +00:00
// WriteOnSuccess indicates whether the output should only be written if generation fully succeeds.
// This option buffers the output of the template, so it will use more memory. (default: false)
WriteOnSuccess bool
2023-10-29 01:10:17 +00:00
tags map[string]Tag
2023-11-01 19:00:44 +00:00
vars map[string]any
macros map[string][]ast.Node
2023-10-29 01:10:17 +00:00
}
// WithVarMap returns a copy of the template with its variable map set to m.
2023-10-31 04:18:35 +00:00
func (t Template) WithVarMap(m map[string]any) Template {
2023-11-01 19:04:54 +00:00
if m == nil {
t.vars = map[string]any{}
} else {
t.vars = m
}
2023-10-31 04:18:35 +00:00
return t
2023-10-29 01:10:17 +00:00
}
// WithTagMap returns a copy of the template with its tag map set to m.
2023-10-31 04:18:35 +00:00
func (t Template) WithTagMap(m map[string]Tag) Template {
if m == nil {
2023-11-01 19:04:54 +00:00
t.tags = map[string]Tag{}
} else {
t.tags = m
}
2023-10-31 04:18:35 +00:00
return t
2023-10-29 01:10:17 +00:00
}
// WithEscapeHTML returns a copy of the template with HTML escaping enabled or disabled.
2023-10-29 01:10:17 +00:00
// The HTML escaping functionality is NOT context-aware.
// Using the HTML type allows you to get around the escaping if needed.
2023-10-31 04:18:35 +00:00
func (t Template) WithEscapeHTML(b bool) Template {
t.escapeHTML = &b
2023-10-31 04:18:35 +00:00
return t
2023-10-29 01:10:17 +00:00
}
2023-12-28 15:19:58 +00:00
// WithWriteOnSuccess enables or disables only writing if generation fully succeeds.
func (t Template) WithWriteOnSuccess(b bool) Template {
t.WriteOnSuccess = true
return t
}
2023-10-29 01:10:17 +00:00
// Execute executes a parsed template and writes
// the result to w.
2023-10-31 04:18:35 +00:00
func (t Template) Execute(w io.Writer) error {
t.macros = map[string][]ast.Node{}
2023-12-28 15:19:58 +00:00
if t.WriteOnSuccess {
buf := &bytes.Buffer{}
err := t.execute(buf, t.ast, nil)
if err != nil {
return err
}
_, err = io.Copy(w, buf)
return err
} else {
bw := bufio.NewWriterSize(w, 16384)
defer bw.Flush()
return t.execute(bw, t.ast, nil)
}
2023-10-29 01:10:17 +00:00
}
func (t *Template) execute(w io.Writer, nodes []ast.Node, local map[string]any) error {
2023-11-04 06:36:41 +00:00
if local == nil {
local = map[string]any{}
}
2023-10-29 01:10:17 +00:00
for i := 0; i < len(nodes); i++ {
switch node := nodes[i].(type) {
case ast.Text:
_, err := w.Write(node.Data)
if err != nil {
2023-12-18 00:55:13 +00:00
return ast.PosError(node, "%w", err)
2023-10-29 01:10:17 +00:00
}
case ast.Tag:
newOffset, err := t.execTag(node, w, nodes, i, local)
if err != nil {
return err
}
i = newOffset
case ast.EndTag:
// We should never see an end tag here because it
// should be taken care of by execTag, so if we do,
// return an error because execTag was never called,
// which means there was no start tag.
2023-12-18 00:55:13 +00:00
return ast.PosError(node, "end tag without a matching start tag: %s", node.Name.Value)
2023-10-29 01:10:17 +00:00
case ast.ExprTag:
v, err := t.getValue(node.Value, local)
if err != nil {
return err
}
2023-10-31 20:45:00 +00:00
if _, ok := v.(ast.Assignment); ok {
continue
}
2023-10-29 01:10:17 +00:00
_, err = io.WriteString(w, t.toString(v))
if err != nil {
return err
}
}
}
return nil
}
func (t *Template) getEscapeHTML() bool {
if t.escapeHTML != nil {
return *t.escapeHTML
} else if t.ns.escapeHTML != nil {
return *t.ns.getEscapeHTML()
} else {
return false
}
}
2023-10-29 01:10:17 +00:00
func (t *Template) toString(v any) string {
if h, ok := v.(HTML); ok {
return string(h)
} else if t.getEscapeHTML() {
2023-10-29 01:10:17 +00:00
return html.EscapeString(fmt.Sprint(v))
}
return fmt.Sprint(v)
}
// getBlock gets all the nodes in the input, up to the end tag with the given name
func (t *Template) getBlock(nodes []ast.Node, offset, startLine int, name string) []ast.Node {
var out []ast.Node
tagAmount := 1
for i := offset; i < len(nodes); i++ {
switch node := nodes[i].(type) {
case ast.Tag:
// If we encounter another tag with the same name,
// increment tagAmount so that we know that the next
// end tag isn't the end of this tag.
if node.Name.Value == name {
tagAmount++
}
out = append(out, node)
case ast.EndTag:
if node.Name.Value == name {
tagAmount--
}
// Once tagAmount is zero (all the tags of the same name
// have been closed with an end tag), we can return
// the nodes we've accumulated.
2023-10-29 01:10:17 +00:00
if tagAmount == 0 {
return out
} else {
out = append(out, node)
}
default:
out = append(out, node)
}
}
return out
}
// getValue gets a Go value from an AST node
func (t *Template) getValue(node ast.Node, local map[string]any) (any, error) {
switch node := node.(type) {
case ast.Value:
return t.unwrapASTValue(node, local)
case ast.Ident:
val, err := t.getVar(node, local)
if err != nil {
return nil, err
}
2023-11-01 19:00:44 +00:00
return val, nil
2023-10-29 01:10:17 +00:00
case ast.String:
return node.Value, nil
case ast.Float:
return node.Value, nil
case ast.Integer:
return node.Value, nil
case ast.Bool:
return node.Value, nil
case ast.Expr:
return t.evalExpr(node, local)
case ast.FuncCall:
return t.execFuncCall(node, local)
case ast.Index:
return t.getIndex(node, local)
case ast.FieldAccess:
return t.getField(node, local)
case ast.MethodCall:
return t.execMethodCall(node, local)
2023-10-30 15:37:59 +00:00
case ast.Ternary:
return t.evalTernary(node, local)
2023-10-30 15:52:02 +00:00
case ast.VariableOr:
return t.evalVariableOr(node, local)
2023-10-31 20:45:00 +00:00
case ast.Assignment:
return node, t.handleAssignment(node, local)
2023-10-29 01:10:17 +00:00
default:
return nil, nil
}
}
2023-12-18 00:55:13 +00:00
// valueToString converts an AST node to a textual representation
// for the user to see, such as in error messages. This does not
// directly correlate to Salix source code.
func valueToString(node ast.Node) string {
if node == nil {
return "<nil>"
}
switch node := node.(type) {
case ast.Ident:
return node.Value
case ast.String:
return strconv.Quote(node.Value)
case ast.Integer:
return strconv.FormatInt(node.Value, 10)
case ast.Float:
return strconv.FormatFloat(node.Value, 'g', -1, 64)
case ast.Bool:
return strconv.FormatBool(node.Value)
case ast.Assignment:
return node.Name.Value + " = " + valueToString(node.Value)
case ast.Index:
return valueToString(node.Value) + "[" + valueToString(node.Index) + "]"
case ast.Ternary:
return valueToString(node.Condition) + " ? " + valueToString(node.IfTrue) + " : " + valueToString(node.Else)
case ast.FieldAccess:
return valueToString(node.Value) + "." + node.Name.Value
case ast.Value:
if node.Not {
return "!" + valueToString(node.Node)
}
return valueToString(node.Node)
case ast.FuncCall:
if len(node.Params) > 1 {
return node.Name.Value + "(" + valueToString(node.Params[0]) + ", ...)"
} else if len(node.Params) == 1 {
return node.Name.Value + "(" + valueToString(node.Params[0]) + ")"
} else {
return node.Name.Value + "()"
}
case ast.MethodCall:
if len(node.Params) > 1 {
return valueToString(node.Value) + "." + node.Name.Value + "(" + valueToString(node.Params[0]) + ", ...)"
} else if len(node.Params) == 1 {
return valueToString(node.Value) + "." + node.Name.Value + "(" + valueToString(node.Params[0]) + ")"
} else {
return valueToString(node.Value) + "." + node.Name.Value + "()"
}
case ast.Expr:
if len(node.Rest) == 0 {
return valueToString(node.First)
}
return valueToString(node.First) + " " + node.Rest[0].Operator.Value + " " + valueToString(node.Rest[0])
2023-12-18 00:55:13 +00:00
case ast.Tag:
if len(node.Params) > 1 {
return "#" + node.Name.Value + "(" + valueToString(node.Params[0]) + ", ...)"
} else if len(node.Params) == 1 {
return "#" + node.Name.Value + "(" + valueToString(node.Params[0]) + ")"
} else {
return "#" + node.Name.Value + "()"
}
case ast.EndTag:
return "#!" + node.Name.Value
2023-12-18 00:55:13 +00:00
case ast.ExprTag:
return "#(" + valueToString(node.Value) + ")"
default:
return "..."
}
}
2023-10-29 01:10:17 +00:00
// unwrapASTValue unwraps an ast.Value node into its underlying value
func (t *Template) unwrapASTValue(node ast.Value, local map[string]any) (any, error) {
v, err := t.getValue(node.Node, local)
if err != nil {
return nil, err
}
if node.Not {
rval := reflect.ValueOf(v)
if rval.Kind() != reflect.Bool {
2023-12-18 00:55:13 +00:00
return nil, ast.PosError(node, "%s: the ! operator can only be used on boolean values", valueToString(node))
2023-10-29 01:10:17 +00:00
}
return !rval.Bool(), nil
}
return v, err
}
// getVar tries to get a variable from the local map. If it's not found,
// it'll try the global variable map. If it doesn't exist in either map,
// it will return an error.
2023-11-01 19:00:44 +00:00
func (t *Template) getVar(id ast.Ident, local map[string]any) (any, error) {
2023-10-29 01:10:17 +00:00
if local != nil {
v, ok := local[id.Value]
if ok {
2023-11-01 19:00:44 +00:00
return v, nil
2023-10-29 01:10:17 +00:00
}
}
v, ok := t.vars[id.Value]
if ok {
return v, nil
}
v, ok = t.ns.getVar(id.Value)
if ok {
return v, nil
}
v, ok = globalVars[id.Value]
if ok {
return v, nil
2023-10-29 01:10:17 +00:00
}
2023-12-18 00:55:13 +00:00
return reflect.Value{}, ast.PosError(id, "no such variable: %s", id.Value)
2023-10-29 01:10:17 +00:00
}
func (t *Template) getTag(name string) (Tag, bool) {
tag, ok := t.tags[name]
if ok {
return tag, true
2023-10-29 01:10:17 +00:00
}
tag, ok = t.ns.getTag(name)
if ok {
return tag, true
}
tag, ok = globalTags[name]
if ok {
return tag, true
2023-10-29 01:10:17 +00:00
}
return nil, false
2023-10-29 01:10:17 +00:00
}
// execTag executes a tag
func (t *Template) execTag(node ast.Tag, w io.Writer, nodes []ast.Node, i int, local map[string]any) (newOffset int, err error) {
tag, ok := t.getTag(node.Name.Value)
2023-10-29 01:10:17 +00:00
if !ok {
2023-12-18 00:55:13 +00:00
return 0, ast.PosError(node, "no such tag: %s", node.Name.Value)
2023-10-29 01:10:17 +00:00
}
var block []ast.Node
if node.HasBody {
block = t.getBlock(nodes, i+1, node.Position.Line, node.Name.Value)
i += len(block) + 1
}
2023-12-18 00:55:13 +00:00
tc := &TagContext{node, w, t, local}
2023-10-29 01:10:17 +00:00
err = tag.Run(tc, block, node.Params)
if err != nil {
2023-12-18 00:55:13 +00:00
return 0, errors.Join(ast.PosError(node, "%s ->", valueToString(node)), err)
2023-10-29 01:10:17 +00:00
}
return i, nil
}
// execFuncCall executes a function call
func (t *Template) execFuncCall(fc ast.FuncCall, local map[string]any) (any, error) {
fn, err := t.getVar(fc.Name, local)
if err != nil {
2023-12-18 00:55:13 +00:00
return nil, ast.PosError(fc, "no such function: %s", fc.Name.Value)
2023-10-29 01:10:17 +00:00
}
2023-11-01 19:00:44 +00:00
return t.execFunc(reflect.ValueOf(fn), fc, fc.Params, local)
2023-10-29 01:10:17 +00:00
}
// getIndex tries to evaluate an ast.Index node by indexing the underlying value.
func (t *Template) getIndex(i ast.Index, local map[string]any) (any, error) {
val, err := t.getValue(i.Value, local)
if err != nil {
return nil, err
}
index, err := t.getValue(i.Index, local)
if err != nil {
return nil, err
}
rval := reflect.ValueOf(val)
rindex := reflect.ValueOf(index)
switch rval.Kind() {
2023-12-18 00:55:13 +00:00
case reflect.Slice, reflect.Array, reflect.String:
2023-10-29 01:10:17 +00:00
intType := reflect.TypeOf(0)
if rindex.CanConvert(intType) {
rindex = rindex.Convert(intType)
} else {
2023-12-18 00:55:13 +00:00
return nil, ast.PosError(i, "%s: invalid index type: %T", valueToString(i), index)
2023-10-29 01:10:17 +00:00
}
intIndex := rindex.Interface().(int)
if intIndex < rval.Len() {
return rval.Index(intIndex).Interface(), nil
} else {
2023-12-18 00:55:13 +00:00
return nil, ast.PosError(i, "%s: index out of range: %d", valueToString(i), intIndex)
2023-10-29 01:10:17 +00:00
}
case reflect.Map:
if rindex.CanConvert(rval.Type().Key()) {
rindex = rindex.Convert(rval.Type().Key())
} else {
2023-12-18 00:55:13 +00:00
return nil, ast.PosError(i, "%s: invalid map index type: %T (expected %s)", valueToString(i), index, rval.Type().Key())
2023-10-29 01:10:17 +00:00
}
if out := rval.MapIndex(rindex); out.IsValid() {
return out.Interface(), nil
} else {
2023-12-18 00:55:13 +00:00
return nil, ast.PosError(i, "%s: map index not found: %q", valueToString(i), index)
2023-10-29 01:10:17 +00:00
}
2023-12-18 00:55:13 +00:00
default:
return nil, ast.PosError(i, "%s: cannot index type: %T", valueToString(i), val)
2023-10-29 01:10:17 +00:00
}
}
// getField tries to get a struct field from the underlying value
func (t *Template) getField(fa ast.FieldAccess, local map[string]any) (any, error) {
val, err := t.getValue(fa.Value, local)
if err != nil {
return nil, err
}
rval := reflect.ValueOf(val)
2023-10-31 21:05:21 +00:00
for rval.Kind() == reflect.Pointer {
rval = rval.Elem()
}
2023-10-29 01:10:17 +00:00
field := rval.FieldByName(fa.Name.Value)
if !field.IsValid() {
2023-12-18 00:55:13 +00:00
return nil, ast.PosError(fa, "%s: no such field: %s", valueToString(fa), fa.Name.Value)
2023-10-29 01:10:17 +00:00
}
return field.Interface(), nil
}
// execMethodCall executes a method call on the underlying value
func (t *Template) execMethodCall(mc ast.MethodCall, local map[string]any) (any, error) {
val, err := t.getValue(mc.Value, local)
if err != nil {
return nil, err
}
rval := reflect.ValueOf(val)
2023-10-31 21:05:21 +00:00
for rval.Kind() == reflect.Pointer {
rval = rval.Elem()
}
// First, check for a method with the given name
2023-10-29 01:10:17 +00:00
mtd := rval.MethodByName(mc.Name.Value)
if mtd.IsValid() {
return t.execFunc(mtd, mc, mc.Params, local)
2023-10-29 01:10:17 +00:00
}
// If the method doesn't exist, also check for a field storing a function.
field := rval.FieldByName(mc.Name.Value)
if field.IsValid() && field.Kind() == reflect.Func {
return t.execFunc(field, mc, mc.Params, local)
}
// If neither of those exist, return an error
return nil, ast.PosError(mc, "no such method: %s", mc.Name.Value)
2023-10-29 01:10:17 +00:00
}
// execFunc executes a function call
func (t *Template) execFunc(fn reflect.Value, node ast.Node, args []ast.Node, local map[string]any) (any, error) {
fnType := fn.Type()
if fnType.NumIn() != len(args) {
2023-12-18 00:55:13 +00:00
return nil, ast.PosError(node, "%s: invalid parameter amount: %d (expected %d)", valueToString(node), len(args), fnType.NumIn())
2023-10-29 01:10:17 +00:00
}
2023-12-18 00:55:13 +00:00
if err := validateFunc(fnType, node); err != nil {
return nil, err
2023-10-29 01:10:17 +00:00
}
params := make([]reflect.Value, fnType.NumIn())
for i, arg := range args {
2023-10-31 20:45:00 +00:00
if _, ok := arg.(ast.Assignment); ok {
2023-12-18 00:55:13 +00:00
return nil, ast.PosError(arg, "%s: an assignment cannot be used as a function argument", valueToString(node))
2023-10-31 20:45:00 +00:00
}
2023-10-29 01:10:17 +00:00
paramVal, err := t.getValue(arg, local)
if err != nil {
return nil, err
}
params[i] = reflect.ValueOf(paramVal)
if params[i].CanConvert(fnType.In(i)) {
params[i] = params[i].Convert(fnType.In(i))
} else {
2023-12-18 00:55:13 +00:00
return nil, ast.PosError(node, "%s: invalid parameter type: %T (expected %s)", valueToString(node), paramVal, fnType.In(i))
2023-10-29 01:10:17 +00:00
}
}
ret := fn.Call(params)
if len(ret) == 1 {
retv := ret[0].Interface()
if err, ok := retv.(error); ok {
2023-12-18 00:55:13 +00:00
return nil, ast.PosError(node, "%s: %w", valueToString(node), err)
2023-10-29 01:10:17 +00:00
}
return ret[0].Interface(), nil
} else {
2023-12-18 00:55:13 +00:00
if ret[1].IsNil() {
return ret[0].Interface(), nil
}
return ret[0].Interface(), ast.PosError(node, "%s: %w", valueToString(node), ret[1].Interface().(error))
2023-10-29 01:10:17 +00:00
}
}
2023-10-30 15:37:59 +00:00
func (t *Template) evalTernary(tr ast.Ternary, local map[string]any) (any, error) {
condVal, err := t.getValue(tr.Condition, local)
if err != nil {
2023-12-18 00:55:13 +00:00
return nil, err
2023-10-30 15:37:59 +00:00
}
cond, ok := condVal.(bool)
if !ok {
2023-12-18 00:55:13 +00:00
return nil, ast.PosError(tr.Condition, "%s: ternary condition must be a boolean value", valueToString(tr.Condition))
2023-10-30 15:37:59 +00:00
}
if cond {
return t.getValue(tr.IfTrue, local)
} else {
return t.getValue(tr.Else, local)
}
}
2023-10-30 15:52:02 +00:00
func (t *Template) evalVariableOr(vo ast.VariableOr, local map[string]any) (any, error) {
val, err := t.getVar(vo.Variable, local)
if err != nil {
return t.getValue(vo.Or, local)
}
2023-11-01 19:00:44 +00:00
return val, nil
2023-10-30 15:52:02 +00:00
}
2023-10-31 20:45:00 +00:00
func (t *Template) handleAssignment(a ast.Assignment, local map[string]any) error {
val, err := t.getValue(a.Value, local)
if err != nil {
return err
}
local[a.Name.Value] = val
return nil
}
2023-12-18 00:55:13 +00:00
func validateFunc(t reflect.Type, node ast.Node) error {
2023-10-29 01:10:17 +00:00
numOut := t.NumOut()
if numOut > 2 {
2023-12-18 00:55:13 +00:00
return ast.PosError(node, "template functions cannot have more than two return values")
2023-10-29 01:10:17 +00:00
} else if numOut == 0 {
2023-12-18 00:55:13 +00:00
return ast.PosError(node, "template functions must have at least one return value")
2023-10-29 01:10:17 +00:00
}
if numOut == 2 {
2023-12-18 00:55:13 +00:00
errType := reflect.TypeOf((*error)(nil)).Elem()
if !t.Out(1).Implements(errType) {
return ast.PosError(node, "the second return value of a template function must be an error")
2023-10-29 01:10:17 +00:00
}
}
return nil
}