2015-10-07 22:38:39 +00:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import "github.com/fatih/hcl/scanner"
|
|
|
|
|
2015-10-11 22:38:59 +00:00
|
|
|
// Node is an element in the abstract syntax tree.
|
2015-10-07 22:38:39 +00:00
|
|
|
type Node interface {
|
2015-10-11 21:20:17 +00:00
|
|
|
node()
|
2015-10-09 09:36:40 +00:00
|
|
|
Pos() scanner.Pos
|
2015-10-07 22:38:39 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 21:57:57 +00:00
|
|
|
func (ObjectList) node() {}
|
|
|
|
func (ObjectItem) node() {}
|
2015-10-12 19:53:40 +00:00
|
|
|
func (ObjectType) node() {}
|
2015-10-15 21:57:57 +00:00
|
|
|
func (LiteralType) node() {}
|
2015-10-12 19:53:40 +00:00
|
|
|
func (ListType) node() {}
|
2015-10-15 21:57:57 +00:00
|
|
|
func (Ident) node() {}
|
2015-10-12 19:53:40 +00:00
|
|
|
|
2015-10-15 21:57:57 +00:00
|
|
|
// ObjectList represents a list of ObjectItems. An HCL file itself is an
|
|
|
|
// ObjectList.
|
|
|
|
type ObjectList struct {
|
|
|
|
items []*ObjectItem
|
2015-10-11 22:38:59 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 21:57:57 +00:00
|
|
|
func (o *ObjectList) add(item *ObjectItem) {
|
|
|
|
o.items = append(o.items, item)
|
2015-10-11 22:38:59 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 21:57:57 +00:00
|
|
|
func (o *ObjectList) Pos() scanner.Pos {
|
2015-10-11 22:38:59 +00:00
|
|
|
// always returns the uninitiliazed position
|
2015-10-15 21:57:57 +00:00
|
|
|
return o.items[0].Pos()
|
2015-10-11 22:38:59 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 21:57:57 +00:00
|
|
|
// 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 {
|
|
|
|
// key is either an Identifier or a String. The slice is only one lenght
|
|
|
|
// long, however if it's a nested object it'll can be larger than one. In
|
|
|
|
// that case "assign" is invalid as there is no assignments for a nested
|
|
|
|
// object.
|
|
|
|
key []Ident
|
2015-10-11 21:20:17 +00:00
|
|
|
|
2015-10-15 21:57:57 +00:00
|
|
|
// assign contains the position of "=", if any
|
|
|
|
assign scanner.Pos
|
2015-10-07 22:38:39 +00:00
|
|
|
|
2015-10-15 21:57:57 +00:00
|
|
|
// val is the item itself. It can be an object,list, number, bool or a
|
|
|
|
// string. If key lenght is larger than one, val can be only of type
|
|
|
|
// Object.
|
|
|
|
val Node
|
2015-10-11 21:20:17 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 21:57:57 +00:00
|
|
|
func (o *ObjectItem) Pos() scanner.Pos {
|
|
|
|
return o.key[0].Pos()
|
2015-10-07 22:38:39 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 21:57:57 +00:00
|
|
|
// IdentStatement represents an identifier.
|
|
|
|
type Ident struct {
|
|
|
|
token scanner.Token
|
2015-10-12 19:53:40 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 21:57:57 +00:00
|
|
|
func (i *Ident) Pos() scanner.Pos {
|
|
|
|
return i.token.Pos
|
2015-10-12 19:53:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LiteralType represents a literal of basic type. Valid types are:
|
|
|
|
// scanner.NUMBER, scanner.FLOAT, scanner.BOOL and scanner.STRING
|
|
|
|
type LiteralType struct {
|
2015-10-14 22:27:35 +00:00
|
|
|
token scanner.Token
|
2015-10-12 19:53:40 +00:00
|
|
|
}
|
|
|
|
|
2015-10-12 20:44:53 +00:00
|
|
|
// isValid() returns true if the underlying identifier satisfies one of the
|
|
|
|
// valid types.
|
|
|
|
func (l *LiteralType) isValid() bool {
|
|
|
|
switch l.token.Type {
|
|
|
|
case scanner.NUMBER, scanner.FLOAT, scanner.BOOL, scanner.STRING:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2015-10-12 19:53:40 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 22:27:35 +00:00
|
|
|
func (l *LiteralType) Pos() scanner.Pos {
|
|
|
|
return l.token.Pos
|
|
|
|
}
|
|
|
|
|
2015-10-12 19:53:40 +00:00
|
|
|
// ListStatement represents a HCL List type
|
|
|
|
type ListType struct {
|
2015-10-11 21:20:17 +00:00
|
|
|
lbrack scanner.Pos // position of "["
|
|
|
|
rbrack scanner.Pos // position of "]"
|
|
|
|
list []Node // the elements in lexical order
|
|
|
|
}
|
|
|
|
|
2015-10-12 19:53:40 +00:00
|
|
|
func (l *ListType) Pos() scanner.Pos {
|
2015-10-11 21:20:17 +00:00
|
|
|
return l.lbrack
|
2015-10-07 22:38:39 +00:00
|
|
|
}
|
|
|
|
|
2015-10-12 19:53:40 +00:00
|
|
|
// ObjectType represents a HCL Object Type
|
|
|
|
type ObjectType struct {
|
|
|
|
lbrace scanner.Pos // position of "{"
|
|
|
|
rbrace scanner.Pos // position of "}"
|
|
|
|
list []Node // the nodes in lexical order
|
2015-10-07 22:38:39 +00:00
|
|
|
}
|
2015-10-11 21:20:17 +00:00
|
|
|
|
2015-10-12 19:53:40 +00:00
|
|
|
func (b *ObjectType) Pos() scanner.Pos {
|
|
|
|
return b.lbrace
|
2015-10-11 21:20:17 +00:00
|
|
|
}
|