hcl: add *ast.File with comments
This commit is contained in:
parent
877d63151c
commit
407cd650d1
11
ast/ast.go
11
ast/ast.go
@ -10,6 +10,7 @@ type Node interface {
|
|||||||
Pos() token.Pos
|
Pos() token.Pos
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (File) node() {}
|
||||||
func (ObjectList) node() {}
|
func (ObjectList) node() {}
|
||||||
func (ObjectKey) node() {}
|
func (ObjectKey) node() {}
|
||||||
func (ObjectItem) node() {}
|
func (ObjectItem) node() {}
|
||||||
@ -20,6 +21,16 @@ func (ObjectType) node() {}
|
|||||||
func (LiteralType) node() {}
|
func (LiteralType) node() {}
|
||||||
func (ListType) node() {}
|
func (ListType) node() {}
|
||||||
|
|
||||||
|
// 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()
|
||||||
|
}
|
||||||
|
|
||||||
// ObjectList represents a list of ObjectItems. An HCL file itself is an
|
// ObjectList represents a list of ObjectItems. An HCL file itself is an
|
||||||
// ObjectList.
|
// ObjectList.
|
||||||
type ObjectList struct {
|
type ObjectList struct {
|
||||||
|
@ -34,7 +34,7 @@ func newParser(src []byte) *Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse returns the fully parsed source and returns the abstract syntax tree.
|
// Parse returns the fully parsed source and returns the abstract syntax tree.
|
||||||
func Parse(src []byte) (ast.Node, error) {
|
func Parse(src []byte) (*ast.File, error) {
|
||||||
p := newParser(src)
|
p := newParser(src)
|
||||||
return p.Parse()
|
return p.Parse()
|
||||||
}
|
}
|
||||||
@ -42,8 +42,16 @@ func Parse(src []byte) (ast.Node, error) {
|
|||||||
var errEofToken = errors.New("EOF token found")
|
var errEofToken = errors.New("EOF token found")
|
||||||
|
|
||||||
// Parse returns the fully parsed source and returns the abstract syntax tree.
|
// Parse returns the fully parsed source and returns the abstract syntax tree.
|
||||||
func (p *Parser) Parse() (ast.Node, error) {
|
func (p *Parser) Parse() (*ast.File, error) {
|
||||||
return p.objectList()
|
f := &ast.File{}
|
||||||
|
var err error
|
||||||
|
f.Node, err = p.objectList()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
f.Comments = p.comments
|
||||||
|
return f, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) objectList() (*ast.ObjectList, error) {
|
func (p *Parser) objectList() (*ast.ObjectList, error) {
|
||||||
@ -64,7 +72,6 @@ func (p *Parser) objectList() (*ast.ObjectList, error) {
|
|||||||
|
|
||||||
node.Add(n)
|
node.Add(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
return node, nil
|
return node, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,13 @@ func (p *printer) output(n interface{}) []byte {
|
|||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
switch t := n.(type) {
|
switch t := n.(type) {
|
||||||
|
case *ast.File:
|
||||||
|
// for i, group := range t.Comments {
|
||||||
|
// for _, comment := range group.List {
|
||||||
|
// fmt.Printf("[%d] comment = %+v\n", i, comment)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
return p.output(t.Node)
|
||||||
case *ast.ObjectList:
|
case *ast.ObjectList:
|
||||||
for i, item := range t.Items {
|
for i, item := range t.Items {
|
||||||
buf.Write(p.objectItem(item))
|
buf.Write(p.objectItem(item))
|
||||||
|
Loading…
Reference in New Issue
Block a user