hil: github.com/hashicorp/hil/ast Index | Files

package ast

import "github.com/hashicorp/hil/ast"

Index

Package Files

arithmetic.go arithmetic_op.go ast.go call.go conditional.go index.go literal.go output.go scope.go stack.go type_string.go unknown.go variable_access.go variables_helper.go

Variables

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).

func IsUnknown Uses

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.

type Arithmetic Uses

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 Uses

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

func (*Arithmetic) GoString Uses

func (n *Arithmetic) GoString() string

func (*Arithmetic) Pos Uses

func (n *Arithmetic) Pos() Pos

func (*Arithmetic) String Uses

func (n *Arithmetic) String() string

func (*Arithmetic) Type Uses

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

type ArithmeticOp Uses

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 Uses

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 Uses

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

func (*BasicScope) LookupVar Uses

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

type Call Uses

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

Call represents a function call.

func (*Call) Accept Uses

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

func (*Call) GoString Uses

func (n *Call) GoString() string

func (*Call) Pos Uses

func (n *Call) Pos() Pos

func (*Call) String Uses

func (n *Call) String() string

func (*Call) Type Uses

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

type Conditional Uses

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

func (*Conditional) Accept Uses

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 Uses

func (n *Conditional) GoString() string

func (*Conditional) Pos Uses

func (n *Conditional) Pos() Pos

func (*Conditional) Type Uses

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

type Function Uses

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 Uses

type Index struct {
    Target Node
    Key    Node
    Posx   Pos
}

Index represents an indexing operation into another data structure

func (*Index) Accept Uses

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

func (*Index) GoString Uses

func (n *Index) GoString() string

func (*Index) Pos Uses

func (n *Index) Pos() Pos

func (*Index) String Uses

func (n *Index) String() string

func (*Index) Type Uses

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

type LiteralNode Uses

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 Uses

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 Uses

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 Uses

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

func (*LiteralNode) GoString Uses

func (n *LiteralNode) GoString() string

func (*LiteralNode) IsUnknown Uses

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 Uses

func (n *LiteralNode) Pos() Pos

func (*LiteralNode) String Uses

func (n *LiteralNode) String() string

func (*LiteralNode) Type Uses

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

type Node Uses

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 Uses

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 Uses

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

func (*Output) GoString Uses

func (n *Output) GoString() string

func (*Output) Pos Uses

func (n *Output) Pos() Pos

func (*Output) String Uses

func (n *Output) String() string

func (*Output) Type Uses

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

type Pos Uses

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 Uses

func (p Pos) String() string

type Scope Uses

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 Uses

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

Stack is a stack of Node.

func (*Stack) Len Uses

func (s *Stack) Len() int

func (*Stack) Pop Uses

func (s *Stack) Pop() Node

func (*Stack) Push Uses

func (s *Stack) Push(n Node)

func (*Stack) Reset Uses

func (s *Stack) Reset()

type Type Uses

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 Uses

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

func VariableMapValueTypesAreHomogenous Uses

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

func (Type) Printable Uses

func (t Type) Printable() string

func (Type) String Uses

func (i Type) String() string

type Variable Uses

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 Uses

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 Uses

func (v Variable) String() string

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

type VariableAccess Uses

type VariableAccess struct {
    Name string
    Posx Pos
}

VariableAccess represents a variable access.

func (*VariableAccess) Accept Uses

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

func (*VariableAccess) GoString Uses

func (n *VariableAccess) GoString() string

func (*VariableAccess) Pos Uses

func (n *VariableAccess) Pos() Pos

func (*VariableAccess) String Uses

func (n *VariableAccess) String() string

func (*VariableAccess) Type Uses

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

type Visitor Uses

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.

Package ast imports 4 packages (graph) and is imported by 350 packages. Updated 2019-09-13. Refresh now. Tools for package owners.