github.com/hashicorp/hcl/v2@v2.20.0/hclwrite/ast.go (about)

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