syntax

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package syntax creates abstract TunaScript and TunaQuest template language constructs from parse trees passed to it from generated frontends.

Index

Constants

This section is empty.

Variables

View Source
var (
	// BuiltInFunctions has function definitions info for each function that is
	// built-in to TunaScript. It does *not* contain their implementations, only
	// info on them.
	BuiltInFunctions = map[string]Function{
		"ADD":               {Name: "ADD", RequiredArgs: 2},
		"SUB":               {Name: "SUB", RequiredArgs: 2},
		"MULT":              {Name: "MULT", RequiredArgs: 2},
		"DIV":               {Name: "DIV", RequiredArgs: 2},
		"NEG":               {Name: "NEG", RequiredArgs: 1},
		"OR":                {Name: "OR", RequiredArgs: 2},
		"AND":               {Name: "AND", RequiredArgs: 2},
		"NOT":               {Name: "NOT", RequiredArgs: 1},
		"FLAG_ENABLED":      {Name: "FLAG_ENABLED", RequiredArgs: 1},
		"FLAG_DISABLED":     {Name: "FLAG_DISABLED", RequiredArgs: 1},
		"FLAG_IS":           {Name: "FLAG_IS", RequiredArgs: 2},
		"FLAG_LESS_THAN":    {Name: "FLAG_LESS_THAN", RequiredArgs: 2},
		"FLAG_GREATER_THAN": {Name: "FLAG_GREATER_THAN", RequiredArgs: 2},
		"ENABLE":            {Name: "ENABLE", RequiredArgs: 1, SideEffects: true},
		"DISABLE":           {Name: "DISABLE", RequiredArgs: 1, SideEffects: true},
		"TOGGLE":            {Name: "TOGGLE", RequiredArgs: 1, SideEffects: true},
		"INC":               {Name: "INC", RequiredArgs: 1, OptionalArgs: 1, SideEffects: true},
		"DEC":               {Name: "DEC", RequiredArgs: 1, OptionalArgs: 1, SideEffects: true},
		"SET":               {Name: "SET", RequiredArgs: 2, SideEffects: true},
		"IN_INVEN":          {Name: "IN_INVEN", RequiredArgs: 1},
		"MOVE":              {Name: "MOVE", RequiredArgs: 2, SideEffects: true},
		"OUTPUT":            {Name: "OUTPUT", RequiredArgs: 1, SideEffects: true},
	}
)
View Source
var (
	HooksTable = trans.HookMap{
		"ast":           hookAST,
		"bin_or":        makeHookBinaryOp(OpBinaryLogicalOr),
		"bin_and":       makeHookBinaryOp(OpBinaryLogicalAnd),
		"bin_eq":        makeHookBinaryOp(OpBinaryEqual),
		"bin_ne":        makeHookBinaryOp(OpBinaryNotEqual),
		"bin_lt":        makeHookBinaryOp(OpBinaryLessThan),
		"bin_le":        makeHookBinaryOp(OpBinaryLessThanEqual),
		"bin_gt":        makeHookBinaryOp(OpBinaryGreaterThan),
		"bin_ge":        makeHookBinaryOp(OpBinaryGreaterThanEqual),
		"bin_sub":       makeHookBinaryOp(OpBinarySubtract),
		"bin_add":       makeHookBinaryOp(OpBinaryAdd),
		"bin_mult":      makeHookBinaryOp(OpBinaryMultiply),
		"bin_div":       makeHookBinaryOp(OpBinaryDivide),
		"unary_not":     makeHookUnaryOp(OpUnaryLogicalNot),
		"unary_neg":     makeHookUnaryOp(OpUnaryNegate),
		"group":         hookGroup,
		"func":          hookFunc,
		"args_list":     hookArgsList,
		"assign_set":    makeHookAssignBinary(OpAssignSet),
		"assign_incset": makeHookAssignBinary(OpAssignIncrementBy),
		"assign_decset": makeHookAssignBinary(OpAssignDecrementBy),
		"assign_dec":    makeHookAssignUnary(OpAssignDecrement),
		"assign_inc":    makeHookAssignUnary(OpAssignIncrement),
		"flag":          hookFlag,
		"lit_binary":    hookLitBinary,
		"lit_text":      hookLitText,
		"lit_num":       hookLitNum,
		"identity":      func(info trans.SetterInfo, args []interface{}) (interface{}, error) { return args[0], nil },
	}
)
View Source
var (
	TmplHooksTable = trans.HookMap{
		"identity":         func(info trans.SetterInfo, args []interface{}) (interface{}, error) { return args[0], nil },
		"ast":              tmplHookAST,
		"text":             tmplHookText,
		"flag":             tmplHookFlag,
		"branch":           tmplHookBranch,
		"branch_with_else": tmplHookBranchWithElse,
		"cond_list":        tmplHookCondList,
		"node_list":        tmplHookNodeList,
	}
)

Functions

func InterpretEscapes

func InterpretEscapes(s string) string

Types

type AST

type AST struct {
	Nodes []ASTNode
}

func (AST) Equal

func (ast AST) Equal(o any) bool

Equal checks if the AST is equal to another object. If the other object is not another AST, they are not considered equal. If the other object is, Equal returns whether the two trees, if invoked for any type of meaning, would return the same result. Note that this is distinct from whether they were created from the tunascript source code; to check that, use EqualSource().

func (AST) String

func (ast AST) String() string

String returns a prettified representation of the entire AST suitable for use in line-by-line comparisons of tree structure. Two ASTs are considered semantcally identical if they produce identical String() output. Each statement is shown on a new line.

func (AST) Tunascript

func (ast AST) Tunascript() string

Tunascript returns a string that contains tunascript code that if parsed would result in an equivalent ASTNode. It is not necessarily the source that produced this node as non-semantic elements are not included (such as extra whitespace not a part of an unquoted string).

Each node is placed on its own line in the resulting string.

type ASTNode

type ASTNode interface {

	// Type returns the type of the ASTNode. This determines which of the As*()
	// functions may be called.
	Type() NodeType

	// Returns this node as a LiteralNode. Panics if Type() does not return
	// ASTLiteral.
	AsLiteralNode() LiteralNode

	// Returns this node as a FuncNode. Panics if Type() does not return
	// ASTFunc.
	AsFuncNode() FuncNode

	// Returns this node as a FlagNode. Panics if Type() does not return
	// ASTFlag.
	AsFlagNode() FlagNode

	// Returns this node as a GroupNode. Panics if Type() does not return
	// ASTGroup.
	AsGroupNode() GroupNode

	// Returns this node as a BinaryOpNode. Panics if Type() does not return
	// ASTBinaryOp.
	AsBinaryOpNode() BinaryOpNode

	// Returns this node as a UnaryOpNode. Panics if Type() does not return
	// ASTUnaryOp.
	AsUnaryOpNode() UnaryOpNode

	// Returns this node as an AssignmentNode. Panics if Type() does not return
	// ASTAssignment.
	AsAssignmentNode() AssignmentNode

	// Source is the token from source text that had the first token lexed as
	// part of this literal.
	Source() lex.Token

	// String returns a prettified representation of the node suitable for use
	// in line-by-line comparisons of tree structure. Two nodes are considered
	// semantcally identical if they produce identical String() output.
	String() string

	// Tunascript returns a string that contains tunascript code that if parsed
	// would result in an equivalent ASTNode. It is not necessarily the source
	// that produced this node as non-semantic elements are not included (such
	// as extra whitespace not a part of an unquoted string).
	Tunascript() string

	// Equal returns whether a node is equal to another. It will return false
	// if anything besides an ASTNode is passed in. ASTNodes do not consider
	// the result of Source() in their equality; ergo, this returns whether two
	// nodes have the same structure regardless of the exact source that
	// produced them.
	Equal(o any) bool
}

type AssignmentNode

type AssignmentNode struct {

	// Flag is the name of the flag being assigned to, without the leading $.
	Flag string

	// Value will be nil if Op is an operation which does not take an argument
	// (such as Increment or Decrement, which always have an implied argument of
	// 1).
	Value ASTNode

	// Op is the operation performed
	Op AssignmentOperation

	// PostFix can only be true when Expr is nil, otherwise it will always be
	// false.
	PostFix bool
	// contains filtered or unexported fields
}

AssignmentNode represents assignment of a value to a flag. Strictly speaking, this is a type of binary operation or unary operation depedning on the type of assignment being done, but it is kept as a separate AST node to help with analysis.

func (AssignmentNode) AsAssignmentNode

func (n AssignmentNode) AsAssignmentNode() AssignmentNode

func (AssignmentNode) AsBinaryOpNode

func (n AssignmentNode) AsBinaryOpNode() BinaryOpNode

func (AssignmentNode) AsFlagNode

func (n AssignmentNode) AsFlagNode() FlagNode

func (AssignmentNode) AsFuncNode

func (n AssignmentNode) AsFuncNode() FuncNode

func (AssignmentNode) AsGroupNode

func (n AssignmentNode) AsGroupNode() GroupNode

func (AssignmentNode) AsLiteralNode

func (n AssignmentNode) AsLiteralNode() LiteralNode

func (AssignmentNode) AsUnaryOpNode

func (n AssignmentNode) AsUnaryOpNode() UnaryOpNode

func (AssignmentNode) Equal

func (n AssignmentNode) Equal(o any) bool

Does not consider Source.

func (AssignmentNode) Source

func (n AssignmentNode) Source() lex.Token

func (AssignmentNode) String

func (n AssignmentNode) String() string

func (AssignmentNode) Tunascript

func (n AssignmentNode) Tunascript() string

func (AssignmentNode) Type

func (n AssignmentNode) Type() NodeType

type AssignmentOperation

type AssignmentOperation int
const (
	OpAssignSet AssignmentOperation = iota
	OpAssignIncrement
	OpAssignDecrement
	OpAssignIncrementBy
	OpAssignDecrementBy
)

func (AssignmentOperation) BuiltInFunc

func (op AssignmentOperation) BuiltInFunc() string

BuiltInFunc returns the name of the built-in function that provides the same functionality as the operator.

func (AssignmentOperation) String

func (op AssignmentOperation) String() string

func (AssignmentOperation) Symbol

func (op AssignmentOperation) Symbol() string

Symbol returns the Tunascript operator that corresponds to the operation.

type BinaryOpNode

type BinaryOpNode struct {
	Left  ASTNode
	Right ASTNode
	Op    BinaryOperation
	// contains filtered or unexported fields
}

func (BinaryOpNode) AsAssignmentNode

func (n BinaryOpNode) AsAssignmentNode() AssignmentNode

func (BinaryOpNode) AsBinaryOpNode

func (n BinaryOpNode) AsBinaryOpNode() BinaryOpNode

func (BinaryOpNode) AsFlagNode

func (n BinaryOpNode) AsFlagNode() FlagNode

func (BinaryOpNode) AsFuncNode

func (n BinaryOpNode) AsFuncNode() FuncNode

func (BinaryOpNode) AsGroupNode

func (n BinaryOpNode) AsGroupNode() GroupNode

func (BinaryOpNode) AsLiteralNode

func (n BinaryOpNode) AsLiteralNode() LiteralNode

func (BinaryOpNode) AsUnaryOpNode

func (n BinaryOpNode) AsUnaryOpNode() UnaryOpNode

func (BinaryOpNode) Equal

func (n BinaryOpNode) Equal(o any) bool

Does not consider Source.

func (BinaryOpNode) Source

func (n BinaryOpNode) Source() lex.Token

func (BinaryOpNode) String

func (n BinaryOpNode) String() string

func (BinaryOpNode) Tunascript

func (n BinaryOpNode) Tunascript() string

func (BinaryOpNode) Type

func (n BinaryOpNode) Type() NodeType

type BinaryOperation

type BinaryOperation int
const (
	OpBinaryEqual BinaryOperation = iota
	OpBinaryNotEqual
	OpBinaryLessThan
	OpBinaryLessThanEqual
	OpBinaryGreaterThan
	OpBinaryGreaterThanEqual
	OpBinaryAdd
	OpBinarySubtract
	OpBinaryDivide
	OpBinaryMultiply
	OpBinaryLogicalAnd
	OpBinaryLogicalOr
)

func (BinaryOperation) BuiltInFunc

func (op BinaryOperation) BuiltInFunc() string

BuiltInFunc returns the name of the built-in function that provides the same functionality as the operator.

func (BinaryOperation) String

func (op BinaryOperation) String() string

func (BinaryOperation) Symbol

func (op BinaryOperation) Symbol() string

Symbol returns the Tunascript operator that corresponds to the operation.

type Block

type Block interface {

	// Type returns the type of the Block. This determines which of the As*()
	// functions may be called.
	Type() BlockType

	// Returns this node as an ExpTextNode. Panics if Type() does not return
	// ExpText.
	AsText() TextBlock

	// Returns this node as an ExpFlagNode. Panics if Type() does not return
	// ExpFlag.
	AsFlag() FlagBlock

	// Returns this node as an ExpBranchNode. Panics if Type() does not return
	// ExpBranch.
	AsBranch() BranchBlock

	// Returns this node as an ExpCondNode. Panics if Type() does not return
	// ExpCond.
	AsCond() CondBlock

	// String returns a prettified representation of the node suitable for use
	// in line-by-line comparisons of tree structure. Two nodes are considered
	// semantcally identical if they produce identical String() output.
	String() string

	// Equal returns whether a node is equal to another. It will return false
	// if anything besides an ASTNode is passed in. ASTNodes do not consider
	// the result of Source() in their equality; ergo, this returns whether two
	// nodes have the same structure regardless of the exact source that
	// produced them.
	Equal(o any) bool

	// Template returns the string that, if pased, would produce a Block
	// identical to this one. It does *not* return, necessarily, the exact text
	// that was parsed to create it, as some non-semantic elements such as
	// whitespace within control-flow statements may be slightly altered.
	Template() string
}

Block is a block of parsed template code in a Template. It represents the smallest abstract unit that a template can be divided into.

type BlockType

type BlockType int

BlockType is the type of a template Block. Every Block will be one of these types, and it dictates which of its As*() functions can be called.

const (
	// TmplText is the type of a TextBlock, which contains literal text which
	// will not be expanded further.
	TmplText BlockType = iota

	// TmplFlag is the type of a FlagBlock, which contains a flag that will be
	// replaced with its actual value at the time it is expanded.
	TmplFlag

	// TmplBranch is the type of a BranchBlock, which contains flow-control
	// statements that will be replaced with the text in the applicable branch
	// at the time it is expanded.
	TmplBranch

	// TmplCond is the type of a CondBlock, which contains both a TunaScript
	// condition and the content that the block should be expanded to if it is
	// selected as the branch from within a BranchBlock.
	TmplCond
)

type BranchBlock

type BranchBlock struct {
	If CondBlock

	// ElseIf will be empty if there are no else-if blocks.
	ElseIf []CondBlock

	// Else will be nil if there are no else blocks.
	Else []Block

	Source lex.Token
}

BranchBlock is a series of control-flow statements and their contents within a template. This may include the opening IF statement, any ELSE-IFs, and the ELSE attached to the IF, if any is present. This block is expanded to the first applicable branch's contents at the time that it is expanded.

func (BranchBlock) AsBranch

func (n BranchBlock) AsBranch() BranchBlock

func (BranchBlock) AsCond

func (n BranchBlock) AsCond() CondBlock

func (BranchBlock) AsFlag

func (n BranchBlock) AsFlag() FlagBlock

func (BranchBlock) AsText

func (n BranchBlock) AsText() TextBlock

func (BranchBlock) Equal

func (n BranchBlock) Equal(o any) bool

Does not consider Source.

func (BranchBlock) String

func (n BranchBlock) String() string

func (BranchBlock) Template

func (n BranchBlock) Template() string

func (BranchBlock) Type

func (n BranchBlock) Type() BlockType

type CondBlock

type CondBlock struct {
	Cond AST

	// On initial parsing of template trees, only this will be set. The
	// contents of this string can be parsed by passing it to the TS frontend.
	RawCond string

	Content []Block

	Source lex.Token
}

CondBlock is a single condition from a BranchBlock. It holds both the TunaScript code that makes up its condition, which may or may not already be parsed (it will be parsed if this CondBlock was in a Template returned by an Interpreter).

func (CondBlock) AsBranch

func (n CondBlock) AsBranch() BranchBlock

func (CondBlock) AsCond

func (n CondBlock) AsCond() CondBlock

func (CondBlock) AsFlag

func (n CondBlock) AsFlag() FlagBlock

func (CondBlock) AsText

func (n CondBlock) AsText() TextBlock

func (CondBlock) Equal

func (n CondBlock) Equal(o any) bool

Does not consider Source.

func (CondBlock) String

func (n CondBlock) String() string

func (CondBlock) Template

func (n CondBlock) Template() string

func (CondBlock) Type

func (n CondBlock) Type() BlockType

type FlagBlock

type FlagBlock struct {
	Flag   string
	Source lex.Token
}

FlagBlock holds a variable (flag) within a template. During expansion, this will be replaced with the actual value of the flag at that time, converted to a string for display.

func (FlagBlock) AsBranch

func (n FlagBlock) AsBranch() BranchBlock

func (FlagBlock) AsCond

func (n FlagBlock) AsCond() CondBlock

func (FlagBlock) AsFlag

func (n FlagBlock) AsFlag() FlagBlock

func (FlagBlock) AsText

func (n FlagBlock) AsText() TextBlock

func (FlagBlock) Equal

func (n FlagBlock) Equal(o any) bool

Does not consider Source.

func (FlagBlock) String

func (n FlagBlock) String() string

func (FlagBlock) Template

func (n FlagBlock) Template() string

func (FlagBlock) Type

func (n FlagBlock) Type() BlockType

type FlagNode

type FlagNode struct {
	// Flag is the name of the flag, without the leading $.
	Flag string
	// contains filtered or unexported fields
}

func (FlagNode) AsAssignmentNode

func (n FlagNode) AsAssignmentNode() AssignmentNode

func (FlagNode) AsBinaryOpNode

func (n FlagNode) AsBinaryOpNode() BinaryOpNode

func (FlagNode) AsFlagNode

func (n FlagNode) AsFlagNode() FlagNode

func (FlagNode) AsFuncNode

func (n FlagNode) AsFuncNode() FuncNode

func (FlagNode) AsGroupNode

func (n FlagNode) AsGroupNode() GroupNode

func (FlagNode) AsLiteralNode

func (n FlagNode) AsLiteralNode() LiteralNode

func (FlagNode) AsUnaryOpNode

func (n FlagNode) AsUnaryOpNode() UnaryOpNode

func (FlagNode) Equal

func (n FlagNode) Equal(o any) bool

Does not consider Source.

func (FlagNode) Source

func (n FlagNode) Source() lex.Token

func (FlagNode) String

func (n FlagNode) String() string

func (FlagNode) Tunascript

func (n FlagNode) Tunascript() string

func (FlagNode) Type

func (n FlagNode) Type() NodeType

type FuncNode

type FuncNode struct {
	// Func is the name of the function being called, without the leading $.
	Func string

	// Args is all arguments to the function.
	Args []ASTNode
	// contains filtered or unexported fields
}

func (FuncNode) AsAssignmentNode

func (n FuncNode) AsAssignmentNode() AssignmentNode

func (FuncNode) AsBinaryOpNode

func (n FuncNode) AsBinaryOpNode() BinaryOpNode

func (FuncNode) AsFlagNode

func (n FuncNode) AsFlagNode() FlagNode

func (FuncNode) AsFuncNode

func (n FuncNode) AsFuncNode() FuncNode

func (FuncNode) AsGroupNode

func (n FuncNode) AsGroupNode() GroupNode

func (FuncNode) AsLiteralNode

func (n FuncNode) AsLiteralNode() LiteralNode

func (FuncNode) AsUnaryOpNode

func (n FuncNode) AsUnaryOpNode() UnaryOpNode

func (FuncNode) Equal

func (n FuncNode) Equal(o any) bool

Does not consider Source.

func (FuncNode) Source

func (n FuncNode) Source() lex.Token

func (FuncNode) String

func (n FuncNode) String() string

func (FuncNode) Tunascript

func (n FuncNode) Tunascript() string

func (FuncNode) Type

func (n FuncNode) Type() NodeType

type Function

type Function struct {
	// Name is the name of the function. It would be called with $Name(). Name
	// is case-insensitive and must follow identifier naming rules ([A-Z0-9_]+)
	Name string

	// RequiredArgs is the number of required arguments. This many arguments is
	// guaranteed to be passed to the implementation.
	RequiredArgs int

	// OptionalArgs is the number of optional arguments. Up to RequiredArgs +
	// OptionalArgs may be passed to the implementation.
	OptionalArgs int

	// SideEffects tells whether the function has side-effects. Certain contexts
	// such as within an $IF() may restrict the execution of side-effect
	// functions.
	SideEffects bool
}

Function defines a function that can be executed in tunascript. It includes all info required to check usage of the function, but not the actual implementation.

type GroupNode

type GroupNode struct {
	Expr ASTNode
	// contains filtered or unexported fields
}

func (GroupNode) AsAssignmentNode

func (n GroupNode) AsAssignmentNode() AssignmentNode

func (GroupNode) AsBinaryOpNode

func (n GroupNode) AsBinaryOpNode() BinaryOpNode

func (GroupNode) AsFlagNode

func (n GroupNode) AsFlagNode() FlagNode

func (GroupNode) AsFuncNode

func (n GroupNode) AsFuncNode() FuncNode

func (GroupNode) AsGroupNode

func (n GroupNode) AsGroupNode() GroupNode

func (GroupNode) AsLiteralNode

func (n GroupNode) AsLiteralNode() LiteralNode

func (GroupNode) AsUnaryOpNode

func (n GroupNode) AsUnaryOpNode() UnaryOpNode

func (GroupNode) Equal

func (n GroupNode) Equal(o any) bool

Does not consider Source.

func (GroupNode) Source

func (n GroupNode) Source() lex.Token

func (GroupNode) String

func (n GroupNode) String() string

func (GroupNode) Tunascript

func (n GroupNode) Tunascript() string

func (GroupNode) Type

func (n GroupNode) Type() NodeType

type LiteralNode

type LiteralNode struct {
	// Quoted can only be true if Value.Type() == String, and indicates whether
	// the value is wrapped in @-signs.
	Quoted bool

	// Value is the value of the literal.
	Value Value
	// contains filtered or unexported fields
}

LiteralNode is a node of the AST that represents a typed literal in code.

func (LiteralNode) AsAssignmentNode

func (n LiteralNode) AsAssignmentNode() AssignmentNode

func (LiteralNode) AsBinaryOpNode

func (n LiteralNode) AsBinaryOpNode() BinaryOpNode

func (LiteralNode) AsFlagNode

func (n LiteralNode) AsFlagNode() FlagNode

func (LiteralNode) AsFuncNode

func (n LiteralNode) AsFuncNode() FuncNode

func (LiteralNode) AsGroupNode

func (n LiteralNode) AsGroupNode() GroupNode

func (LiteralNode) AsLiteralNode

func (n LiteralNode) AsLiteralNode() LiteralNode

func (LiteralNode) AsUnaryOpNode

func (n LiteralNode) AsUnaryOpNode() UnaryOpNode

func (LiteralNode) Equal

func (n LiteralNode) Equal(o any) bool

Does not consider Source.

func (LiteralNode) Source

func (n LiteralNode) Source() lex.Token

func (LiteralNode) String

func (n LiteralNode) String() string

func (LiteralNode) Tunascript

func (n LiteralNode) Tunascript() string

func (LiteralNode) Type

func (n LiteralNode) Type() NodeType

type NodeType

type NodeType int
const (
	ASTLiteral NodeType = iota
	ASTFunc
	ASTFlag
	ASTGroup
	ASTBinaryOp
	ASTUnaryOp
	ASTAssignment
)

type Template

type Template struct {
	// Blocks is the template blocks that make up the complete template. They
	// are arranged in the order they appear in the finished template.
	Blocks []Block
}

Template is a parsed tunaquest template containing both tunascript template-legal expressions and regular text. The zero-value of an Template is an empty Template. Text can be parsed into a Template by calling Analyze on the template frontend.

Templates cannot be expanded on their own and require the help of an engine that provides a suitable execution environment. Typically, this is done with an Interpreter from the tunascript package.

func (Template) Equal

func (tmpl Template) Equal(o any) bool

Equal returns whether this Template is equal to another value. This will return true only if o is another Template or Template pointer that has the same members as tmpl.

func (Template) String

func (tmpl Template) String() string

String returns a debug-tailored string that represents the Template. Two templates are considered semantically identical and will produce the same text if their String() methods produce the same output.

func (Template) Template

func (tmpl Template) Template() string

Template returns the string that, if pased, would produce a Template identical to this one. It does *not* return, necessarily, the exact text that was parsed to create it, as some non-semantic elements such as whitespace within control-flow statements may be slightly altered.

type TextBlock

type TextBlock struct {

	// Text is the literal text in the block.
	Text              string
	LeftSpaceTrimmed  string
	RightSpaceTrimmed string

	// Source is the lexed token that was used to produce this TextBlock. If
	// this TextBlock was not created by parsing template code, this will be
	// nil.
	Source lex.Token
}

TextBlock is a block of unexpandable text in a template. This text will be returned as-is when expanded.

func (TextBlock) AsBranch

func (n TextBlock) AsBranch() BranchBlock

func (TextBlock) AsCond

func (n TextBlock) AsCond() CondBlock

func (TextBlock) AsFlag

func (n TextBlock) AsFlag() FlagBlock

func (TextBlock) AsText

func (n TextBlock) AsText() TextBlock

func (TextBlock) Equal

func (n TextBlock) Equal(o any) bool

Does not consider Source.

func (TextBlock) HasLeftTrimmed

func (n TextBlock) HasLeftTrimmed() bool

func (TextBlock) HasRightTrimmed

func (n TextBlock) HasRightTrimmed() bool

func (TextBlock) String

func (n TextBlock) String() string

func (TextBlock) Template

func (n TextBlock) Template() string

func (TextBlock) Type

func (n TextBlock) Type() BlockType

type UnaryOpNode

type UnaryOpNode struct {
	Operand ASTNode
	Op      UnaryOperation
	PostFix bool
	// contains filtered or unexported fields
}

func (UnaryOpNode) AsAssignmentNode

func (n UnaryOpNode) AsAssignmentNode() AssignmentNode

func (UnaryOpNode) AsBinaryOpNode

func (n UnaryOpNode) AsBinaryOpNode() BinaryOpNode

func (UnaryOpNode) AsFlagNode

func (n UnaryOpNode) AsFlagNode() FlagNode

func (UnaryOpNode) AsFuncNode

func (n UnaryOpNode) AsFuncNode() FuncNode

func (UnaryOpNode) AsGroupNode

func (n UnaryOpNode) AsGroupNode() GroupNode

func (UnaryOpNode) AsLiteralNode

func (n UnaryOpNode) AsLiteralNode() LiteralNode

func (UnaryOpNode) AsUnaryOpNode

func (n UnaryOpNode) AsUnaryOpNode() UnaryOpNode

func (UnaryOpNode) Equal

func (n UnaryOpNode) Equal(o any) bool

Does not consider Source.

func (UnaryOpNode) Source

func (n UnaryOpNode) Source() lex.Token

func (UnaryOpNode) String

func (n UnaryOpNode) String() string

func (UnaryOpNode) Tunascript

func (n UnaryOpNode) Tunascript() string

func (UnaryOpNode) Type

func (n UnaryOpNode) Type() NodeType

type UnaryOperation

type UnaryOperation int
const (
	OpUnaryNegate UnaryOperation = iota
	OpUnaryLogicalNot
)

func (UnaryOperation) BuiltInFunc

func (op UnaryOperation) BuiltInFunc() string

BuiltInFunc returns the name of the built-in function that provides the same functionality as the operator.

func (UnaryOperation) String

func (op UnaryOperation) String() string

String returns the Tunascript operator that corresponds to the operation.

func (UnaryOperation) Symbol

func (op UnaryOperation) Symbol() string

Symbol returns the Tunascript operator that corresponds to the operation.

type Value

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

Value is a value in Tunascript. It is formally quad-typed, though official documentation does not distinguish between ints and floats (they are called "numbers"). If math with a float is performed, the result is float.

Only two of the values in a Value will be valid: vType and one other, depending on the value of vType.

Some operations may cause coercion to be performed so that all operands are of compatible types. In this case, it is always the type of the Value that the method is being called on that will determine the final result.

func ValueOf

func ValueOf(v any) Value

ValueOf creates a TSValueFrom of the appropriate type based on the arguments. The argument must be an int, float64, bool, or string.

func (Value) Add

func (v Value) Add(v2 Value) Value

Add returns the result of adding v2 to v.

This function performs type coercion on the arguments, in the following order: If v is a String, both are converted to String and concatenated to produce the result. Otherwise, numeric addition is performed, and if one of the arguments is a Bool, the typical Int() value from a Bool is used. If either argument is a Float type, the result will also be of type float.

func (Value) And

func (v Value) And(v2 Value) Value

And returns the result of performing a logical AND on v and v2. Both are coerced to bool and the result of AND-ing them is returned.

func (Value) Bool

func (v Value) Bool() bool

if it is a number, true if non-zero, false if zero. if a string, true if string is not empty.

func (Value) CastToBool

func (v Value) CastToBool() Value

CastToBool returns this value cast to Bool type. If this value is already Bool, it is returned unchanged, otherwise it is converted to Bool type.

func (Value) CastToNumber

func (v Value) CastToNumber() Value

CastToNumber returns this value cast to a numeric type. If this value is already an Int or a Float, it is returned unchanged, otherwise it is converted to Int type.

func (Value) CastToString

func (v Value) CastToString() Value

CastToString returns this value cast to String type. If this value is already String, it is returned unchanged, otherwise it is converted to String type.

func (Value) Divide

func (v Value) Divide(v2 Value) Value

Divide returns the result of dividing v by v2. The result will always be numeric. If either argument is of type Float, the result will be of type Float. If the operation is performed with Ints but the result is fractional, the result will be Float. Otherwise the result will be Int.

func (Value) Equal

func (v Value) Equal(o any) bool

Equal returns whether this value struct is exactly the same as another Go value. No type coercion is performed; any must be a Value or a *Value with all members identical to v for Equal to return true. For TunaScript equality semantics, use EqualTo.

func (Value) EqualTo

func (v Value) EqualTo(v2 Value) Value

EqualTo returns whether v is equal to v2 using TunaScript semantics. The value held within v2 is converted to v's type and the results are compared. This does *not* do a strict struct member comparison; for that, use Equal.

The result will always be of type Bool.

func (Value) Escaped

func (v Value) Escaped() string

Escaped returns the String() value but with all tunascript-sensitive characters escaped, including whitespace at the start or end, as it would appear in tunascript source.

func (Value) Float

func (v Value) Float() float64

If it is a string, attempts to parse it. if unparsable, returns 0.0. Bool true is 1.0, bool false is 0.0.

func (Value) GreaterThan

func (v Value) GreaterThan(v2 Value) Value

GreaterThan returns whether v is greater than v2. Both are interpreted as numeric. The result will always be type Bool.

func (Value) GreaterThanEqualTo

func (v Value) GreaterThanEqualTo(v2 Value) Value

GreaterThanEqualTo returns whether v is greater than or equal to v2. Both are interpreted as numeric. The result will always be type Bool.

func (Value) Int

func (v Value) Int() int

if it is a float, rounding will occur. If it is a string, attempts to parse it. if unparsable, returns 0. Bool true is 1, bool false is 0.

func (Value) IsNumber

func (v Value) IsNumber() bool

func (Value) LessThan

func (v Value) LessThan(v2 Value) Value

LessThan returns whether v is less than v2. Both are interpreted as numeric. The result will always be type Bool.

func (Value) LessThanEqualTo

func (v Value) LessThanEqualTo(v2 Value) Value

LessThanEqualTo returns whether v is less than or equal to v2. Both are interpreted as numeric. The result will always be type Bool.

func (Value) Multiply

func (v Value) Multiply(v2 Value) Value

Multiply returns the result of multiplying v by v2. If v is a String, then it is repeated v2 times and the resulting String is returned (in this case, the greater of v2 or 0 is used as the repetitions). If either argument is a Float, the result will be of type Float, otherwise it will be Int.

func (Value) Negate

func (v Value) Negate() Value

Negate returns the numeric negation of this value. The Value is coerced to a numberic type and a Value holding the negative result is returned.

func (Value) Not

func (v Value) Not() Value

Not returns the logical negation of this value. The Value is coerced to a bool and the negation is returned.

func (Value) Or

func (v Value) Or(v2 Value) Value

Or returns the result of performing a logical OR on v and v2. Both are coerced to bool and the result of OR-ing them is returned.

func (Value) Quoted

func (v Value) Quoted() string

Quoted returns the String() value in between two "@" chars with any "@" chars within it escaped (as well as any existing backslashes)

func (Value) String

func (v Value) String() string

func (Value) Subtract

func (v Value) Subtract(v2 Value) Value

Subtract returns the result of subtracting v2 from v. The result will always be numeric. If either argument is of type Float, the result will be of type Float, otherwise it will be Int.

func (Value) Type

func (v Value) Type() ValueType

type ValueType

type ValueType int
const (
	Int ValueType = iota
	Float
	String
	Bool
)

Jump to

Keyboard shortcuts

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