77c0b55a59
The original prototype of hclwrite tried to track both the tokens and the AST as two parallel data structures. This quickly exploded in complexity, leading to lots of messy code to manage keeping those two structures in sync. This new approach melds the two structures together, creating first a physical token tree (made of "node" objects, and hidden from the caller) and then attaching the AST nodes to that token tree as additional sidecar data. The result is much easier to work with, leading to less code in the parser and considerably less complex data structures in the parser's tests. This commit is enough to reach feature parity with the previous prototype, but it remains a prototype. With a more usable foundation, we'll evolve this into a more complete implementation in subsequent commits.
107 lines
1.8 KiB
Go
107 lines
1.8 KiB
Go
package hclwrite
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
)
|
|
|
|
type File struct {
|
|
inTree
|
|
|
|
srcBytes []byte
|
|
body *node
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// WriteTo writes the tokens underlying the receiving file to the given writer.
|
|
func (f *File) WriteTo(wr io.Writer) (int, error) {
|
|
tokens := f.inTree.children.BuildTokens(nil)
|
|
return tokens.WriteTo(wr)
|
|
}
|
|
|
|
// 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()
|
|
}
|
|
|
|
type comments struct {
|
|
leafNode
|
|
|
|
parent *node
|
|
tokens Tokens
|
|
}
|
|
|
|
func newComments(tokens Tokens) *comments {
|
|
return &comments{
|
|
tokens: tokens,
|
|
}
|
|
}
|
|
|
|
func (c *comments) BuildTokens(to Tokens) Tokens {
|
|
return c.tokens.BuildTokens(to)
|
|
}
|
|
|
|
type identifier struct {
|
|
leafNode
|
|
|
|
parent *node
|
|
token *Token
|
|
}
|
|
|
|
func newIdentifier(token *Token) *identifier {
|
|
return &identifier{
|
|
token: token,
|
|
}
|
|
}
|
|
|
|
func (i *identifier) BuildTokens(to Tokens) Tokens {
|
|
return append(to, i.token)
|
|
}
|
|
|
|
func (i *identifier) hasName(name string) bool {
|
|
return name == string(i.token.Bytes)
|
|
}
|
|
|
|
type number struct {
|
|
leafNode
|
|
|
|
parent *node
|
|
token *Token
|
|
}
|
|
|
|
func newNumber(token *Token) *number {
|
|
return &number{
|
|
token: token,
|
|
}
|
|
}
|
|
|
|
func (n *number) BuildTokens(to Tokens) Tokens {
|
|
return append(to, n.token)
|
|
}
|
|
|
|
type quoted struct {
|
|
leafNode
|
|
|
|
parent *node
|
|
tokens Tokens
|
|
}
|
|
|
|
func newQuoted(tokens Tokens) *quoted {
|
|
return "ed{
|
|
tokens: tokens,
|
|
}
|
|
}
|
|
|
|
func (q *quoted) BuildTokens(to Tokens) Tokens {
|
|
return q.tokens.BuildTokens(to)
|
|
}
|