2017-05-29 23:05:34 +00:00
|
|
|
package zclwrite
|
|
|
|
|
2017-06-07 14:24:10 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
)
|
2017-06-07 14:06:23 +00:00
|
|
|
|
2017-05-29 23:05:34 +00:00
|
|
|
type Node interface {
|
|
|
|
walkChildNodes(w internalWalkFunc)
|
|
|
|
Tokens() *TokenSeq
|
|
|
|
}
|
|
|
|
|
|
|
|
type internalWalkFunc func(Node)
|
|
|
|
|
|
|
|
type File struct {
|
2017-06-07 14:24:10 +00:00
|
|
|
Name string
|
|
|
|
SrcBytes []byte
|
2017-05-29 23:05:34 +00:00
|
|
|
|
2017-06-07 14:37:56 +00:00
|
|
|
Body *Body
|
|
|
|
AllTokens *TokenSeq
|
2017-05-29 23:05:34 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 14:24:10 +00:00
|
|
|
// WriteTo writes the tokens underlying the receiving file to the given writer.
|
2017-06-07 14:06:23 +00:00
|
|
|
func (f *File) WriteTo(wr io.Writer) (int, error) {
|
2017-06-07 14:37:56 +00:00
|
|
|
return f.AllTokens.WriteTo(wr)
|
2017-06-07 14:06:23 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 14:24:10 +00: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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format makes in-place modifications to the tokens underlying the receiving
|
|
|
|
// file in order to change the whitespace to be in canonical form.
|
|
|
|
func (f *File) Format() {
|
|
|
|
format(f.Body.AllTokens.Tokens())
|
|
|
|
}
|
|
|
|
|
2017-05-29 23:05:34 +00:00
|
|
|
type Body struct {
|
|
|
|
// Items may contain Attribute, Block and Unstructured instances.
|
2017-06-07 14:24:10 +00:00
|
|
|
// Items and AllTokens should be updated only by methods of this type,
|
|
|
|
// since they must be kept synchronized for correct operation.
|
2017-05-29 23:05:34 +00:00
|
|
|
Items []Node
|
|
|
|
AllTokens *TokenSeq
|
|
|
|
|
|
|
|
// IndentLevel is the number of spaces that should appear at the start
|
|
|
|
// of lines added within this body.
|
|
|
|
IndentLevel int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Body) walkChildNodes(w internalWalkFunc) {
|
|
|
|
for _, item := range n.Items {
|
|
|
|
w(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Body) Tokens() *TokenSeq {
|
|
|
|
return n.AllTokens
|
|
|
|
}
|
|
|
|
|
2017-06-06 15:53:13 +00:00
|
|
|
func (n *Body) AppendItem(node Node) {
|
|
|
|
if n.AllTokens == nil {
|
|
|
|
new := make(TokenSeq, 0, 1)
|
|
|
|
n.AllTokens = &new
|
|
|
|
}
|
|
|
|
n.Items = append(n.Items, node)
|
|
|
|
*(n.AllTokens) = append(*(n.AllTokens), node.Tokens())
|
|
|
|
}
|
|
|
|
|
2017-05-29 23:05:34 +00:00
|
|
|
type Attribute struct {
|
|
|
|
AllTokens *TokenSeq
|
|
|
|
|
|
|
|
LeadCommentTokens *TokenSeq
|
|
|
|
NameToken *Token
|
|
|
|
EqualsToken *Token
|
|
|
|
Value *Expression
|
|
|
|
LineCommentTokens *TokenSeq
|
|
|
|
EOLToken *Token
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Attribute) walkChildNodes(w internalWalkFunc) {
|
|
|
|
w(a.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Block struct {
|
|
|
|
AllTokens *TokenSeq
|
|
|
|
|
|
|
|
LeadCommentTokens *TokenSeq
|
|
|
|
TypeToken *Token
|
|
|
|
LabelTokens []*TokenSeq
|
|
|
|
LabelTokensFlat *TokenSeq
|
|
|
|
OBraceToken *Token
|
|
|
|
Body *Body
|
|
|
|
CBraceToken *Token
|
|
|
|
EOLToken *Token
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Block) walkChildNodes(w internalWalkFunc) {
|
|
|
|
w(n.Body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unstructured represents consecutive sets of tokens within a Body that
|
|
|
|
// aren't part of any particular construct. This includes blank lines
|
|
|
|
// and comments that aren't immediately before an attribute or nested block.
|
|
|
|
type Unstructured struct {
|
|
|
|
AllTokens *TokenSeq
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Unstructured) Tokens() *TokenSeq {
|
|
|
|
return n.AllTokens
|
|
|
|
}
|
|
|
|
|
2017-06-06 15:53:13 +00:00
|
|
|
func (n *Unstructured) walkChildNodes(w internalWalkFunc) {
|
|
|
|
// no child nodes
|
|
|
|
}
|
|
|
|
|
2017-05-29 23:05:34 +00:00
|
|
|
type Expression struct {
|
|
|
|
AllTokens *TokenSeq
|
|
|
|
VarRefs []*VarRef
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Expression) walkChildNodes(w internalWalkFunc) {
|
|
|
|
for _, name := range n.VarRefs {
|
|
|
|
w(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Expression) Tokens() *TokenSeq {
|
|
|
|
return n.AllTokens
|
|
|
|
}
|
|
|
|
|
|
|
|
type VarRef struct {
|
|
|
|
// Tokens alternate between TokenIdent and TokenDot, with the first
|
|
|
|
// and last elements always being TokenIdent.
|
|
|
|
AllTokens *TokenSeq
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *VarRef) walkChildNodes(w internalWalkFunc) {
|
|
|
|
// no child nodes of a variable name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *VarRef) Tokens() *TokenSeq {
|
|
|
|
return n.AllTokens
|
|
|
|
}
|