]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/hashicorp/hcl2/hclwrite/ast.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / hcl2 / hclwrite / ast.go
1 package hclwrite
2
3 import (
4 "bytes"
5 "io"
6 )
7
8 type File struct {
9 inTree
10
11 srcBytes []byte
12 body *node
13 }
14
15 // NewEmptyFile constructs a new file with no content, ready to be mutated
16 // by other calls that append to its body.
17 func NewEmptyFile() *File {
18 f := &File{
19 inTree: newInTree(),
20 }
21 body := newBody()
22 f.body = f.children.Append(body)
23 return f
24 }
25
26 // Body returns the root body of the file, which contains the top-level
27 // attributes and blocks.
28 func (f *File) Body() *Body {
29 return f.body.content.(*Body)
30 }
31
32 // WriteTo writes the tokens underlying the receiving file to the given writer.
33 //
34 // The tokens first have a simple formatting pass applied that adjusts only
35 // the spaces between them.
36 func (f *File) WriteTo(wr io.Writer) (int64, error) {
37 tokens := f.inTree.children.BuildTokens(nil)
38 format(tokens)
39 return tokens.WriteTo(wr)
40 }
41
42 // Bytes returns a buffer containing the source code resulting from the
43 // tokens underlying the receiving file. If any updates have been made via
44 // the AST API, these will be reflected in the result.
45 func (f *File) Bytes() []byte {
46 buf := &bytes.Buffer{}
47 f.WriteTo(buf)
48 return buf.Bytes()
49 }
50
51 type comments struct {
52 leafNode
53
54 parent *node
55 tokens Tokens
56 }
57
58 func newComments(tokens Tokens) *comments {
59 return &comments{
60 tokens: tokens,
61 }
62 }
63
64 func (c *comments) BuildTokens(to Tokens) Tokens {
65 return c.tokens.BuildTokens(to)
66 }
67
68 type identifier struct {
69 leafNode
70
71 parent *node
72 token *Token
73 }
74
75 func newIdentifier(token *Token) *identifier {
76 return &identifier{
77 token: token,
78 }
79 }
80
81 func (i *identifier) BuildTokens(to Tokens) Tokens {
82 return append(to, i.token)
83 }
84
85 func (i *identifier) hasName(name string) bool {
86 return name == string(i.token.Bytes)
87 }
88
89 type number struct {
90 leafNode
91
92 parent *node
93 token *Token
94 }
95
96 func newNumber(token *Token) *number {
97 return &number{
98 token: token,
99 }
100 }
101
102 func (n *number) BuildTokens(to Tokens) Tokens {
103 return append(to, n.token)
104 }
105
106 type quoted struct {
107 leafNode
108
109 parent *node
110 tokens Tokens
111 }
112
113 func newQuoted(tokens Tokens) *quoted {
114 return &quoted{
115 tokens: tokens,
116 }
117 }
118
119 func (q *quoted) BuildTokens(to Tokens) Tokens {
120 return q.tokens.BuildTokens(to)
121 }