ast

package
v0.0.0-...-43d11d3 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: MPL-2.0 Imports: 4 Imported by: 519

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InitPos = Pos{Column: 1, Line: 1}

InitPos is an initiaial position value. This should be used as the starting position (presets the column and line to 1).

Functions

func IsUnknown

func IsUnknown(v Variable) bool

IsUnknown reports whether a variable is unknown or contains any value that is unknown. This will recurse into lists and maps and so on.

Types

type Arithmetic

type Arithmetic struct {
	Op    ArithmeticOp
	Exprs []Node
	Posx  Pos
}

Arithmetic represents a node where the result is arithmetic of two or more operands in the order given.

func (*Arithmetic) Accept

func (n *Arithmetic) Accept(v Visitor) Node

func (*Arithmetic) GoString

func (n *Arithmetic) GoString() string

func (*Arithmetic) Pos

func (n *Arithmetic) Pos() Pos

func (*Arithmetic) String

func (n *Arithmetic) String() string

func (*Arithmetic) Type

func (n *Arithmetic) Type(Scope) (Type, error)

type ArithmeticOp

type ArithmeticOp int

ArithmeticOp is the operation to use for the math.

const (
	ArithmeticOpInvalid ArithmeticOp = 0

	ArithmeticOpAdd ArithmeticOp = iota
	ArithmeticOpSub
	ArithmeticOpMul
	ArithmeticOpDiv
	ArithmeticOpMod

	ArithmeticOpLogicalAnd
	ArithmeticOpLogicalOr

	ArithmeticOpEqual
	ArithmeticOpNotEqual
	ArithmeticOpLessThan
	ArithmeticOpLessThanOrEqual
	ArithmeticOpGreaterThan
	ArithmeticOpGreaterThanOrEqual
)

type BasicScope

type BasicScope struct {
	FuncMap map[string]Function
	VarMap  map[string]Variable
}

BasicScope is a simple scope that looks up variables and functions using a map.

func (*BasicScope) LookupFunc

func (s *BasicScope) LookupFunc(n string) (Function, bool)

func (*BasicScope) LookupVar

func (s *BasicScope) LookupVar(n string) (Variable, bool)

type Call

type Call struct {
	Func string
	Args []Node
	Posx Pos
}

Call represents a function call.

func (*Call) Accept

func (n *Call) Accept(v Visitor) Node

func (*Call) GoString

func (n *Call) GoString() string

func (*Call) Pos

func (n *Call) Pos() Pos

func (*Call) String

func (n *Call) String() string

func (*Call) Type

func (n *Call) Type(s Scope) (Type, error)

type Conditional

type Conditional struct {
	CondExpr  Node
	TrueExpr  Node
	FalseExpr Node
	Posx      Pos
}

func (*Conditional) Accept

func (n *Conditional) Accept(v Visitor) Node

Accept passes the given visitor to the child nodes in this order: CondExpr, TrueExpr, FalseExpr. It then finally passes itself to the visitor.

func (*Conditional) GoString

func (n *Conditional) GoString() string

func (*Conditional) Pos

func (n *Conditional) Pos() Pos

func (*Conditional) Type

func (n *Conditional) Type(Scope) (Type, error)

type Function

type Function struct {
	// ArgTypes is the list of types in argument order. These are the
	// required arguments.
	//
	// ReturnType is the type of the returned value. The Callback MUST
	// return this type.
	ArgTypes   []Type
	ReturnType Type

	// Variadic, if true, says that this function is variadic, meaning
	// it takes a variable number of arguments. In this case, the
	// VariadicType must be set.
	Variadic     bool
	VariadicType Type

	// Callback is the function called for a function. The argument
	// types are guaranteed to match the spec above by the type checker.
	// The length of the args is strictly == len(ArgTypes) unless Varidiac
	// is true, in which case its >= len(ArgTypes).
	Callback func([]interface{}) (interface{}, error)
}

Function defines a function that can be executed by the engine. The type checker will validate that the proper types will be called to the callback.

type Index

type Index struct {
	Target Node
	Key    Node
	Posx   Pos
}

Index represents an indexing operation into another data structure

func (*Index) Accept

func (n *Index) Accept(v Visitor) Node

func (*Index) GoString

func (n *Index) GoString() string

func (*Index) Pos

func (n *Index) Pos() Pos

func (*Index) String

func (n *Index) String() string

func (*Index) Type

func (n *Index) Type(s Scope) (Type, error)

type LiteralNode

type LiteralNode struct {
	Value interface{}
	Typex Type
	Posx  Pos
}

LiteralNode represents a single literal value, such as "foo" or 42 or 3.14159. Based on the Type, the Value can be safely cast.

func MustNewLiteralNode

func MustNewLiteralNode(value interface{}, pos Pos) *LiteralNode

MustNewLiteralNode wraps NewLiteralNode and panics if an error is returned, thus allowing valid literal nodes to be easily assigned to global variables.

func NewLiteralNode

func NewLiteralNode(value interface{}, pos Pos) (*LiteralNode, error)

NewLiteralNode returns a new literal node representing the given literal Go value, which must correspond to one of the primitive types supported by HIL. Lists and maps cannot currently be constructed via this function.

If an inappropriately-typed value is provided, this function will return an error. The main intended use of this function is to produce "synthetic" literals from constants in code, where the value type is well known at compile time. To easily store these in global variables, see also MustNewLiteralNode.

func (*LiteralNode) Accept

func (n *LiteralNode) Accept(v Visitor) Node

func (*LiteralNode) GoString

func (n *LiteralNode) GoString() string

func (*LiteralNode) IsUnknown

func (n *LiteralNode) IsUnknown() bool

IsUnknown returns true either if the node's value is itself unknown of if it is a collection containing any unknown elements, deeply.

func (*LiteralNode) Pos

func (n *LiteralNode) Pos() Pos

func (*LiteralNode) String

func (n *LiteralNode) String() string

func (*LiteralNode) Type

func (n *LiteralNode) Type(Scope) (Type, error)

type Node

type Node interface {
	// Accept is called to dispatch to the visitors. It must return the
	// resulting Node (which might be different in an AST transform).
	Accept(Visitor) Node

	// Pos returns the position of this node in some source.
	Pos() Pos

	// Type returns the type of this node for the given context.
	Type(Scope) (Type, error)
}

Node is the interface that all AST nodes must implement.

type Output

type Output struct {
	Exprs []Node
	Posx  Pos
}

Output represents the root node of all interpolation evaluations. If the output only has one expression which is either a TypeList or TypeMap, the Output can be type-asserted to []interface{} or map[string]interface{} respectively. Otherwise the Output evaluates as a string, and concatenates the evaluation of each expression.

func (*Output) Accept

func (n *Output) Accept(v Visitor) Node

func (*Output) GoString

func (n *Output) GoString() string

func (*Output) Pos

func (n *Output) Pos() Pos

func (*Output) String

func (n *Output) String() string

func (*Output) Type

func (n *Output) Type(s Scope) (Type, error)

type Pos

type Pos struct {
	Column, Line int    // Column/Line number, starting at 1
	Filename     string // Optional source filename, if known
}

Pos is the starting position of an AST node

func (Pos) String

func (p Pos) String() string

type Scope

type Scope interface {
	LookupFunc(string) (Function, bool)
	LookupVar(string) (Variable, bool)
}

Scope is the interface used to look up variables and functions while evaluating. How these functions/variables are defined are up to the caller.

type Stack

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

Stack is a stack of Node.

func (*Stack) Len

func (s *Stack) Len() int

func (*Stack) Pop

func (s *Stack) Pop() Node

func (*Stack) Push

func (s *Stack) Push(n Node)

func (*Stack) Reset

func (s *Stack) Reset()

type Type

type Type uint32

Type is the type of any value.

const (
	TypeInvalid Type = 0
	TypeAny     Type = 1 << iota
	TypeBool
	TypeString
	TypeInt
	TypeFloat
	TypeList
	TypeMap

	// This is a special type used by Terraform to mark "unknown" values.
	// It is impossible for this type to be introduced into your HIL programs
	// unless you explicitly set a variable to this value. In that case,
	// any operation including the variable will return "TypeUnknown" as the
	// type.
	TypeUnknown
)

func VariableListElementTypesAreHomogenous

func VariableListElementTypesAreHomogenous(variableName string, list []Variable) (Type, error)

func VariableMapValueTypesAreHomogenous

func VariableMapValueTypesAreHomogenous(variableName string, vmap map[string]Variable) (Type, error)

func (Type) Printable

func (t Type) Printable() string

func (Type) String

func (i Type) String() string

type Variable

type Variable struct {
	Value interface{}
	Type  Type
}

Variable is a variable value for execution given as input to the engine. It records the value of a variables along with their type.

func NewVariable

func NewVariable(v interface{}) (result Variable, err error)

NewVariable creates a new Variable for the given value. This will attempt to infer the correct type. If it can't, an error will be returned.

func (Variable) String

func (v Variable) String() string

String implements Stringer on Variable, displaying the type and value of the Variable.

type VariableAccess

type VariableAccess struct {
	Name string
	Posx Pos
}

VariableAccess represents a variable access.

func (*VariableAccess) Accept

func (n *VariableAccess) Accept(v Visitor) Node

func (*VariableAccess) GoString

func (n *VariableAccess) GoString() string

func (*VariableAccess) Pos

func (n *VariableAccess) Pos() Pos

func (*VariableAccess) String

func (n *VariableAccess) String() string

func (*VariableAccess) Type

func (n *VariableAccess) Type(s Scope) (Type, error)

type Visitor

type Visitor func(Node) Node

Visitors are just implementations of this function.

The function must return the Node to replace this node with. "nil" is _not_ a valid return value. If there is no replacement, the original node should be returned. We build this replacement directly into the visitor pattern since AST transformations are a common and useful tool and building it into the AST itself makes it required for future Node implementations and very easy to do.

Note that this isn't a true implementation of the visitor pattern, which generally requires proper type dispatch on the function. However, implementing this basic visitor pattern style is still very useful even if you have to type switch.

Jump to

Keyboard shortcuts

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