zclwrite: begin to flesh out public interface
This commit is contained in:
parent
598740b638
commit
fa8a707c7f
@ -1,6 +1,9 @@
|
|||||||
package zclwrite
|
package zclwrite
|
||||||
|
|
||||||
import "io"
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
type Node interface {
|
type Node interface {
|
||||||
walkChildNodes(w internalWalkFunc)
|
walkChildNodes(w internalWalkFunc)
|
||||||
@ -11,17 +14,35 @@ type internalWalkFunc func(Node)
|
|||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
Name string
|
Name string
|
||||||
Bytes []byte
|
SrcBytes []byte
|
||||||
|
|
||||||
Body *Body
|
Body *Body
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteTo writes the tokens underlying the receiving file to the given writer.
|
||||||
func (f *File) WriteTo(wr io.Writer) (int, error) {
|
func (f *File) WriteTo(wr io.Writer) (int, error) {
|
||||||
return f.Body.AllTokens.WriteTo(wr)
|
return f.Body.AllTokens.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()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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())
|
||||||
|
}
|
||||||
|
|
||||||
type Body struct {
|
type Body struct {
|
||||||
// Items may contain Attribute, Block and Unstructured instances.
|
// Items may contain Attribute, Block and Unstructured instances.
|
||||||
|
// Items and AllTokens should be updated only by methods of this type,
|
||||||
|
// since they must be kept synchronized for correct operation.
|
||||||
Items []Node
|
Items []Node
|
||||||
AllTokens *TokenSeq
|
AllTokens *TokenSeq
|
||||||
|
|
||||||
|
7
zclwrite/format.go
Normal file
7
zclwrite/format.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package zclwrite
|
||||||
|
|
||||||
|
// format rewrites tokens within the given sequence, in-place, to adjust the
|
||||||
|
// whitespace around their content to achieve canonical formatting.
|
||||||
|
func format(tokens Tokens) {
|
||||||
|
// currently does nothing
|
||||||
|
}
|
@ -54,7 +54,7 @@ func parse(src []byte, filename string, start zcl.Pos) (*File, zcl.Diagnostics)
|
|||||||
|
|
||||||
return &File{
|
return &File{
|
||||||
Name: filename,
|
Name: filename,
|
||||||
Bytes: src,
|
SrcBytes: src,
|
||||||
|
|
||||||
Body: root,
|
Body: root,
|
||||||
}, nil
|
}, nil
|
||||||
|
30
zclwrite/public.go
Normal file
30
zclwrite/public.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package zclwrite
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
|
||||||
|
"github.com/zclconf/go-zcl/zcl"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Parse interprets the given source bytes into a writer AST. The resulting AST
|
||||||
|
// can be used to perform surgical edits on the source code before turning
|
||||||
|
// it back into bytes again.
|
||||||
|
func Parse(src []byte, filename string, start zcl.Pos) (*File, zcl.Diagnostics) {
|
||||||
|
return parse(src, filename, start)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format takes source code and performs simple whitespace changes to transform
|
||||||
|
// it to a canonical layout style.
|
||||||
|
//
|
||||||
|
// Format skips constructing an AST and works directly with tokens, so it
|
||||||
|
// is less expensive than formatting via the AST for situations where no other
|
||||||
|
// changes will be made. It also ignores syntax errors and can thus be applied
|
||||||
|
// to partial source code, although the result in that case may not be
|
||||||
|
// desirable.
|
||||||
|
func Format(src []byte) []byte {
|
||||||
|
tokens := lexConfig(src)
|
||||||
|
format(tokens)
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
(&TokenSeq{tokens}).WriteTo(buf)
|
||||||
|
return buf.Bytes()
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user