2014-08-02 18:38:41 +00:00
|
|
|
package ast
|
2014-08-01 01:44:21 +00:00
|
|
|
|
2014-08-01 15:31:17 +00:00
|
|
|
// ValueType is an enum represnting the type of a value in
|
|
|
|
// a LiteralNode.
|
2014-08-01 01:44:21 +00:00
|
|
|
type ValueType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
ValueTypeUnknown ValueType = iota
|
2014-08-03 20:34:08 +00:00
|
|
|
ValueTypeFloat
|
2014-08-01 01:44:21 +00:00
|
|
|
ValueTypeInt
|
|
|
|
ValueTypeString
|
2014-08-02 18:38:41 +00:00
|
|
|
ValueTypeBool
|
|
|
|
ValueTypeNil
|
2014-08-01 01:44:21 +00:00
|
|
|
)
|
|
|
|
|
2014-08-01 15:31:17 +00:00
|
|
|
// Node is implemented by all AST nodes for HCL.
|
|
|
|
type Node interface {
|
|
|
|
Accept(Visitor)
|
|
|
|
}
|
|
|
|
|
2014-08-02 22:44:45 +00:00
|
|
|
// KeyedNode is a node that has a key associated with it.
|
|
|
|
type KeyedNode interface {
|
|
|
|
Node
|
|
|
|
|
|
|
|
Key() string
|
|
|
|
}
|
|
|
|
|
2014-08-01 15:31:17 +00:00
|
|
|
// Visitor is the interface that must be implemented by any
|
|
|
|
// structures who want to be visited as part of the visitor pattern
|
|
|
|
// on the AST.
|
|
|
|
type Visitor interface {
|
|
|
|
Visit(Node)
|
|
|
|
}
|
2014-08-01 01:44:21 +00:00
|
|
|
|
2014-08-01 15:31:17 +00:00
|
|
|
// ObjectNode represents an object that has multiple elements.
|
|
|
|
// An object's elements may repeat (keys). This is expected to
|
|
|
|
// be validated/removed at a semantic check, rather than at a
|
|
|
|
// syntax level.
|
2014-08-01 01:44:21 +00:00
|
|
|
type ObjectNode struct {
|
2014-08-02 22:44:45 +00:00
|
|
|
K string
|
|
|
|
Elem []KeyedNode
|
2014-08-01 01:44:21 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 15:31:17 +00:00
|
|
|
// AssignmentNode represents a direct assignment with an equals
|
|
|
|
// sign.
|
2014-08-01 15:14:57 +00:00
|
|
|
type AssignmentNode struct {
|
2014-08-02 22:44:45 +00:00
|
|
|
K string
|
2014-08-01 15:14:57 +00:00
|
|
|
Value Node
|
|
|
|
}
|
|
|
|
|
2014-08-01 15:31:17 +00:00
|
|
|
// ListNode represents a list or array of items.
|
2014-08-01 15:14:57 +00:00
|
|
|
type ListNode struct {
|
|
|
|
Elem []Node
|
|
|
|
}
|
|
|
|
|
2014-08-01 15:31:17 +00:00
|
|
|
// LiteralNode is a direct value.
|
2014-08-01 15:14:57 +00:00
|
|
|
type LiteralNode struct {
|
2014-08-01 01:44:21 +00:00
|
|
|
Type ValueType
|
|
|
|
Value interface{}
|
|
|
|
}
|
2014-08-01 15:31:17 +00:00
|
|
|
|
|
|
|
func (n ObjectNode) Accept(v Visitor) {
|
2014-08-02 22:44:45 +00:00
|
|
|
v.Visit(n)
|
|
|
|
|
2014-08-01 15:31:17 +00:00
|
|
|
for _, e := range n.Elem {
|
|
|
|
e.Accept(v)
|
|
|
|
}
|
2014-08-02 22:44:45 +00:00
|
|
|
}
|
2014-08-01 15:31:17 +00:00
|
|
|
|
2014-08-02 22:44:45 +00:00
|
|
|
func (n ObjectNode) Key() string {
|
|
|
|
return n.K
|
2014-08-01 15:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n AssignmentNode) Accept(v Visitor) {
|
|
|
|
v.Visit(n)
|
2014-08-02 22:44:45 +00:00
|
|
|
n.Value.Accept(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n AssignmentNode) Key() string {
|
|
|
|
return n.K
|
2014-08-01 15:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n ListNode) Accept(v Visitor) {
|
2014-08-02 22:44:45 +00:00
|
|
|
v.Visit(n)
|
|
|
|
|
2014-08-01 15:31:17 +00:00
|
|
|
for _, e := range n.Elem {
|
|
|
|
e.Accept(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n LiteralNode) Accept(v Visitor) {
|
|
|
|
v.Visit(n)
|
|
|
|
}
|