encoder

package
v0.0.0-...-e00a6a3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NodeOffsetKind = iota * 4
	NodeOffsetPos
	NodeOffsetEnd
	NodeOffsetNext
	NodeOffsetParent
	NodeOffsetData
	NodeOffsetFlags
	// NodeSize is the number of bytes that represents a single node in the encoded format.
	NodeSize
)
View Source
const (
	NodeDataTypeChildren uint32 = iota << 30
	NodeDataTypeString
	NodeDataTypeExtendedData
)
View Source
const (
	NodeDataTypeMask        uint32 = 0xc0_00_00_00
	NodeDataChildMask       uint32 = 0x00_00_00_ff
	NodeDataStringIndexMask uint32 = 0x00_ff_ff_ff
)
View Source
const (
	HeaderOffsetMetadata = iota * 4
	HeaderOffsetHashLo0
	HeaderOffsetHashLo1
	HeaderOffsetHashHi0
	HeaderOffsetHashHi1
	HeaderOffsetParseOptions
	HeaderOffsetStringOffsets
	HeaderOffsetStringData
	HeaderOffsetExtendedData
	HeaderOffsetStructuredData
	HeaderOffsetNodes
	HeaderSize
)
View Source
const (
	ProtocolVersion uint8 = 8
)
View Source
const (
	SyntaxKindNodeList uint32 = 1<<32 - 1
)

Variables

This section is empty.

Functions

func DecodeNodes

func DecodeNodes(data []byte) (*ast.Node, error)

DecodeNodes decodes binary-encoded AST data into a tree of *ast.Node objects.

func DecodeSourceFile

func DecodeSourceFile(data []byte) (*ast.SourceFile, error)

DecodeSourceFile decodes binary-encoded data into an *ast.SourceFile.

func SourceFileHash

func SourceFileHash(sourceFile *ast.SourceFile) string

SourceFileHash returns the 128-bit content hash for a source file as a hex string.

Types

type NodeIndexTable

type NodeIndexTable struct {
	Nodes []*ast.Node // index → node (for resolution)
	// contains filtered or unexported fields
}

NodeIndexTable maps between AST nodes and their encoder indices for O(1) node handle resolution.

func BuildNodeIndexTable

func BuildNodeIndexTable(sourceFile *ast.SourceFile) *NodeIndexTable

BuildNodeIndexTable walks the AST in the same order as encodeTree and builds a NodeIndexTable without performing the full binary encoding. This is used to eagerly create index tables for files that need node handles before getSourceFile is called. The indices produced are guaranteed to match those from EncodeSourceFile.

func EncodeNode

func EncodeNode(node *ast.Node, sourceFile *ast.SourceFile) ([]byte, *NodeIndexTable, error)

EncodeNode encodes an arbitrary AST node and its descendants into the binary format. The sourceFile is needed to provide the source text for efficient string encoding. When encoding a non-SourceFile node, the header hash and parse options fields will be zero. Returns the encoded bytes and a NodeIndexTable mapping encoder indices to AST nodes.

func EncodeSourceFile

func EncodeSourceFile(sourceFile *ast.SourceFile) ([]byte, *NodeIndexTable, error)

EncodeSourceFile encodes an entire source file AST into the binary format. Returns the encoded bytes and a NodeIndexTable mapping encoder indices to AST nodes.

func GetNodeIndexTable

func GetNodeIndexTable(sourceFile *ast.SourceFile) *NodeIndexTable

func (*NodeIndexTable) GetIndex

func (t *NodeIndexTable) GetIndex(node *ast.Node) uint32

GetIndex returns the encoder index for the given node. On the first call the sortedIdx array is built (O(n log n) sort on a flat []uint32), then subsequent calls use binary search (O(log n)). This turns out to be much faster than building a map[*ast.Node]uint32 and not significantly slower for lookups.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL