7 "github.com/apparentlymart/go-textseg/textseg"
8 "github.com/hashicorp/hcl2/hcl"
9 "github.com/hashicorp/hcl2/hcl/hclsyntax"
12 // Token is a single sequence of bytes annotated with a type. It is similar
13 // in purpose to hclsyntax.Token, but discards the source position information
14 // since that is not useful in code generation.
16 Type hclsyntax.TokenType
19 // We record the number of spaces before each token so that we can
20 // reproduce the exact layout of the original file when we're making
21 // surgical changes in-place. When _new_ code is created it will always
22 // be in the canonical style, but we preserve layout of existing code.
26 // asHCLSyntax returns the receiver expressed as an incomplete hclsyntax.Token.
27 // A complete token is not possible since we don't have source location
28 // information here, and so this method is unexported so we can be sure it will
29 // only be used for internal purposes where we know the range isn't important.
31 // This is primarily intended to allow us to re-use certain functionality from
32 // hclsyntax rather than re-implementing it against our own token type here.
33 func (t *Token) asHCLSyntax() hclsyntax.Token {
34 return hclsyntax.Token{
38 Filename: "<invalid>",
43 // Tokens is a flat list of tokens.
46 func (ts Tokens) Bytes() []byte {
47 buf := &bytes.Buffer{}
52 func (ts Tokens) testValue() string {
53 return string(ts.Bytes())
56 // Columns returns the number of columns (grapheme clusters) the token sequence
57 // occupies. The result is not meaningful if there are newline or single-line
58 // comment tokens in the sequence.
59 func (ts Tokens) Columns() int {
61 for _, token := range ts {
62 ret += token.SpacesBefore // spaces are always worth one column each
63 ct, _ := textseg.TokenCount(token.Bytes, textseg.ScanGraphemeClusters)
69 // WriteTo takes an io.Writer and writes the bytes for each token to it,
70 // along with the spacing that separates each token. In other words, this
71 // allows serializing the tokens to a file or other such byte stream.
72 func (ts Tokens) WriteTo(wr io.Writer) (int64, error) {
73 // We know we're going to be writing a lot of small chunks of repeated
74 // space characters, so we'll prepare a buffer of these that we can
75 // easily pass to wr.Write without any further allocation.
76 spaces := make([]byte, 40)
77 for i := range spaces {
83 for _, token := range ts {
88 for spacesBefore := token.SpacesBefore; spacesBefore > 0; spacesBefore -= len(spaces) {
89 thisChunk := spacesBefore
90 if thisChunk > len(spaces) {
91 thisChunk = len(spaces)
94 thisN, err = wr.Write(spaces[:thisChunk])
102 thisN, err = wr.Write(token.Bytes)
109 func (ts Tokens) walkChildNodes(w internalWalkFunc) {
110 // Unstructured tokens have no child nodes
113 func (ts Tokens) BuildTokens(to Tokens) Tokens {
114 return append(to, ts...)
117 func newIdentToken(name string) *Token {
119 Type: hclsyntax.TokenIdent,