hcl/zclwrite/native_node_sorter.go
Martin Atkins 3c0dde2ae5 zclwrite: foundations of the writer parser
The "writer parser" is a parser that produces a writer AST rather than
a zclsyntax AST. This can be used to produce a writer AST from existing
source in order to modify it before writing it out again.

It's implemented with the somewhat-unintuitive approach of running the
main zclsyntax parser and then mapping the source ranges it finds back
onto the token sequence to pull out the raw tokens for each object.
This allows us to avoid maintaining two parsers but also keeps all of
this raw-token-wrangling complexity out of the main parser.
2017-06-06 08:53:13 -07:00

24 lines
444 B
Go

package zclwrite
import (
"github.com/zclconf/go-zcl/zcl/zclsyntax"
)
type nativeNodeSorter struct {
Nodes []zclsyntax.Node
}
func (s nativeNodeSorter) Len() int {
return len(s.Nodes)
}
func (s nativeNodeSorter) Less(i, j int) bool {
rangeI := s.Nodes[i].Range()
rangeJ := s.Nodes[j].Range()
return rangeI.Start.Byte < rangeJ.Start.Byte
}
func (s nativeNodeSorter) Swap(i, j int) {
s.Nodes[i], s.Nodes[j] = s.Nodes[j], s.Nodes[i]
}