2017-09-11 16:00:31 -07:00
|
|
|
package hclwrite
|
2017-05-29 16:05:34 -07:00
|
|
|
|
2017-06-07 07:24:10 -07:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
)
|
2017-06-07 07:06:23 -07:00
|
|
|
|
2017-05-29 16:05:34 -07:00
|
|
|
type File struct {
|
2018-08-01 08:45:22 -07:00
|
|
|
inTree
|
2017-05-29 16:05:34 -07:00
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
srcBytes []byte
|
|
|
|
body *node
|
|
|
|
}
|
|
|
|
|
2018-11-03 20:45:25 +00:00
|
|
|
// NewEmptyFile constructs a new file with no content, ready to be mutated
|
|
|
|
// by other calls that append to its body.
|
|
|
|
func NewEmptyFile() *File {
|
|
|
|
f := &File{
|
|
|
|
inTree: newInTree(),
|
|
|
|
}
|
|
|
|
body := newBody()
|
|
|
|
f.body = f.children.Append(body)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
// Body returns the root body of the file, which contains the top-level
|
|
|
|
// attributes and blocks.
|
|
|
|
func (f *File) Body() *Body {
|
|
|
|
return f.body.content.(*Body)
|
2017-05-29 16:05:34 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 07:24:10 -07:00
|
|
|
// WriteTo writes the tokens underlying the receiving file to the given writer.
|
2018-08-14 07:55:47 -07:00
|
|
|
//
|
|
|
|
// The tokens first have a simple formatting pass applied that adjusts only
|
|
|
|
// the spaces between them.
|
2018-11-03 20:10:05 +00:00
|
|
|
func (f *File) WriteTo(wr io.Writer) (int64, error) {
|
2018-08-01 08:45:22 -07:00
|
|
|
tokens := f.inTree.children.BuildTokens(nil)
|
2018-08-14 07:55:47 -07:00
|
|
|
format(tokens)
|
2018-08-01 08:45:22 -07:00
|
|
|
return tokens.WriteTo(wr)
|
2017-06-07 07:06:23 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 07:24:10 -07:00
|
|
|
// Bytes returns a buffer containing the source code resulting from the
|
|
|
|
// tokens underlying the receiving file. If any updates have been made via
|
|
|
|
// the AST API, these will be reflected in the result.
|
|
|
|
func (f *File) Bytes() []byte {
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
f.WriteTo(buf)
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
type comments struct {
|
|
|
|
leafNode
|
2017-05-29 16:05:34 -07:00
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
parent *node
|
|
|
|
tokens Tokens
|
2017-06-08 09:04:27 -07:00
|
|
|
}
|
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
func newComments(tokens Tokens) *comments {
|
|
|
|
return &comments{
|
|
|
|
tokens: tokens,
|
2017-06-06 08:53:13 -07:00
|
|
|
}
|
2017-06-09 08:31:14 -07:00
|
|
|
}
|
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
func (c *comments) BuildTokens(to Tokens) Tokens {
|
|
|
|
return c.tokens.BuildTokens(to)
|
2017-06-09 08:31:14 -07:00
|
|
|
}
|
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
type identifier struct {
|
|
|
|
leafNode
|
2017-05-29 16:05:34 -07:00
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
parent *node
|
|
|
|
token *Token
|
2017-06-07 08:24:33 -07:00
|
|
|
}
|
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
func newIdentifier(token *Token) *identifier {
|
|
|
|
return &identifier{
|
|
|
|
token: token,
|
|
|
|
}
|
2017-05-29 16:05:34 -07:00
|
|
|
}
|
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
func (i *identifier) BuildTokens(to Tokens) Tokens {
|
|
|
|
return append(to, i.token)
|
2017-05-29 16:05:34 -07:00
|
|
|
}
|
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
func (i *identifier) hasName(name string) bool {
|
|
|
|
return name == string(i.token.Bytes)
|
2017-05-29 16:05:34 -07:00
|
|
|
}
|
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
type number struct {
|
|
|
|
leafNode
|
2017-05-29 16:05:34 -07:00
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
parent *node
|
|
|
|
token *Token
|
2017-05-29 16:05:34 -07:00
|
|
|
}
|
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
func newNumber(token *Token) *number {
|
|
|
|
return &number{
|
|
|
|
token: token,
|
2017-05-29 16:05:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
func (n *number) BuildTokens(to Tokens) Tokens {
|
|
|
|
return append(to, n.token)
|
2017-05-29 16:05:34 -07:00
|
|
|
}
|
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
type quoted struct {
|
|
|
|
leafNode
|
2018-07-14 13:07:39 -07:00
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
parent *node
|
|
|
|
tokens Tokens
|
2017-05-29 16:05:34 -07:00
|
|
|
}
|
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
func newQuoted(tokens Tokens) *quoted {
|
|
|
|
return "ed{
|
|
|
|
tokens: tokens,
|
|
|
|
}
|
2017-05-29 16:05:34 -07:00
|
|
|
}
|
2018-07-14 13:07:39 -07:00
|
|
|
|
2018-08-01 08:45:22 -07:00
|
|
|
func (q *quoted) BuildTokens(to Tokens) Tokens {
|
|
|
|
return q.tokens.BuildTokens(to)
|
2018-07-14 13:07:39 -07:00
|
|
|
}
|