hcl/ast/ast.go

154 lines
3.6 KiB
Go
Raw Normal View History

2015-10-25 15:14:16 +00:00
// Package ast declares the types used to represent syntax trees for HCL
// (HashiCorp Configuration Language)
2015-10-16 20:12:26 +00:00
package ast
import "github.com/fatih/hcl/token"
// Node is an element in the abstract syntax tree.
type Node interface {
node()
Pos() token.Pos
}
// NewNode returns a non usable Node interface implementer. The position is
// initalizied to zero.
func NewNode() Node {
return &zero{}
}
2015-10-30 19:51:35 +00:00
func (File) node() {}
2015-10-16 20:12:26 +00:00
func (ObjectList) node() {}
func (ObjectKey) node() {}
func (ObjectItem) node() {}
func (Comment) node() {}
func (CommentGroup) node() {}
func (ObjectType) node() {}
func (LiteralType) node() {}
func (ListType) node() {}
2015-10-16 20:12:26 +00:00
type zero struct{}
func (zero) node() {}
func (z *zero) Pos() token.Pos {
return token.Pos{}
}
2015-10-30 19:51:35 +00:00
// File represents a single HCL file
type File struct {
Node Node // usually a *ObjectList
Comments []*CommentGroup // list of all comments in the source
}
func (f *File) Pos() token.Pos {
return f.Node.Pos()
}
2015-10-16 20:12:26 +00:00
// ObjectList represents a list of ObjectItems. An HCL file itself is an
// ObjectList.
type ObjectList struct {
Items []*ObjectItem
}
func (o *ObjectList) Add(item *ObjectItem) {
o.Items = append(o.Items, item)
}
func (o *ObjectList) Pos() token.Pos {
// always returns the uninitiliazed position
return o.Items[0].Pos()
}
// ObjectItem represents a HCL Object Item. An item is represented with a key
// (or keys). It can be an assignment or an object (both normal and nested)
type ObjectItem struct {
// keys is only one length long if it's of type assignment. If it's a
// nested object it can be larger than one. In that case "assign" is
// invalid as there is no assignments for a nested object.
Keys []*ObjectKey
// assign contains the position of "=", if any
Assign token.Pos
// val is the item itself. It can be an object,list, number, bool or a
// string. If key length is larger than one, val can be only of type
// Object.
Val Node
LeadComment *CommentGroup // associated lead comment
LineComment *CommentGroup // associated line comment
2015-10-16 20:12:26 +00:00
}
func (o *ObjectItem) Pos() token.Pos {
return o.Keys[0].Pos()
}
// ObjectKeys are either an identifier or of type string.
type ObjectKey struct {
Token token.Token
}
func (o *ObjectKey) Pos() token.Pos {
return o.Token.Pos
}
// LiteralType represents a literal of basic type. Valid types are:
// token.NUMBER, token.FLOAT, token.BOOL and token.STRING
type LiteralType struct {
Token token.Token
2015-11-01 00:23:33 +00:00
// associated line comment, only when used in a list
LineComment *CommentGroup
2015-10-16 20:12:26 +00:00
}
func (l *LiteralType) Pos() token.Pos {
return l.Token.Pos
}
// ListStatement represents a HCL List type
type ListType struct {
Lbrack token.Pos // position of "["
Rbrack token.Pos // position of "]"
List []Node // the elements in lexical order
}
func (l *ListType) Pos() token.Pos {
return l.Lbrack
}
2015-10-16 21:00:05 +00:00
func (l *ListType) Add(node Node) {
l.List = append(l.List, node)
}
2015-10-16 20:12:26 +00:00
// ObjectType represents a HCL Object Type
type ObjectType struct {
2015-10-16 22:14:40 +00:00
Lbrace token.Pos // position of "{"
Rbrace token.Pos // position of "}"
List *ObjectList // the nodes in lexical order
2015-10-16 20:12:26 +00:00
}
2015-10-16 22:14:40 +00:00
func (o *ObjectType) Pos() token.Pos {
return o.Lbrace
2015-10-16 20:12:26 +00:00
}
2015-10-18 22:30:14 +00:00
// Comment node represents a single //, # style or /*- style commment
type Comment struct {
Start token.Pos // position of / or #
Text string
}
func (c *Comment) Pos() token.Pos {
return c.Start
}
// CommentGroup node represents a sequence of comments with no other tokens and
// no empty lines between.
type CommentGroup struct {
List []*Comment // len(List) > 0
}
func (c *CommentGroup) Pos() token.Pos {
return c.List[0].Pos()
}