parse

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package parse provides tony parsing support.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrParse  = errors.New("parse error")
	ErrKeyTag = fmt.Errorf("%w: key cannot be tagged", ErrParse)
)

Functions

func GetPositions added in v0.0.6

func GetPositions(opts ...ParseOption) map[*ir.Node]*token.Pos

GetPositions extracts the positions map from the provided options. This allows consumers (like FromTony methods) to access position information.

func Parse

func Parse(d []byte, opts ...ParseOption) (*ir.Node, error)

func ParseMulti added in v0.0.6

func ParseMulti(d []byte, opts ...ParseOption) ([]*ir.Node, error)

ParseMulti parses multiple Tony documents separated by '---' from a single byte slice. It preserves global positions for all documents.

Types

type NodeParser added in v0.0.10

type NodeParser struct {
	// contains filtered or unexported fields
}

NodeParser provides incremental parsing of ir.Node from a token source. It uses the existing TokenSource (from token package) for streaming tokenization.

Note: Currently only supports bracketed structures ({...} or [...]). Non-bracketed structures will return an error.

Example usage:

reader := bytes.NewReader([]byte("{key: value}{other: node}"))
source := token.NewTokenSource(reader)
parser := NewNodeParser(source)

for {
    node, err := parser.ParseNext()
    if err == io.EOF {
        break
    }
    if err != nil {
        log.Fatal(err)
    }
    // Process node incrementally
    processNode(node)
}

func NewNodeParser added in v0.0.10

func NewNodeParser(source *token.TokenSource, opts ...ParseOption) *NodeParser

NewNodeParser creates a new NodeParser reading from the given TokenSource.

func (*NodeParser) ParseNext added in v0.0.10

func (p *NodeParser) ParseNext() (*ir.Node, error)

ParseNext parses the next complete ir.Node from the token source. Returns:

  • node: The parsed node, or nil if no more nodes available
  • err: Error if parsing fails, or io.EOF if source is exhausted

Supports both bracketed structures ({...} or [...]) and simple values (strings, numbers, booleans, null).

type ParseOption

type ParseOption func(*parseOpts)

func NoBrackets

func NoBrackets() ParseOption

func ParseComments

func ParseComments(v bool) ParseOption

func ParseFormat

func ParseFormat(f format.Format) ParseOption

func ParseJSON

func ParseJSON() ParseOption

func ParsePositions

func ParsePositions(m map[*ir.Node]*token.Pos) ParseOption

func ParseTony

func ParseTony() ParseOption

func ParseYAML

func ParseYAML() ParseOption

Jump to

Keyboard shortcuts

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