parse

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package parse handles parsing of various layout file formats

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Format

type Format int

Format represents the supported file formats

const (
	FormatUnknown Format = iota
	FormatPlainText
	FormatYAML
	FormatJSON
)

func DetectFormat

func DetectFormat(filename string) Format

DetectFormat determines the format based on file extension

func (Format) String

func (f Format) String() string

String returns the string representation of the format

type IndentationError

type IndentationError struct {
	ParseError
	Expected int // Expected indentation
	Got      int // Actual indentation
}

IndentationError represents an inconsistent indentation error

func NewIndentationError

func NewIndentationError(line, column, expected, got int) *IndentationError

NewIndentationError creates a new IndentationError

func (*IndentationError) Error

func (e *IndentationError) Error() string

Error implements the error interface

type JSONParser

type JSONParser struct{}

JSONParser parses JSON format layout files

func NewJSONParser

func NewJSONParser() *JSONParser

NewJSONParser creates a new JSON parser

func (*JSONParser) Parse

func (p *JSONParser) Parse(reader io.Reader) ([]*Node, error)

Parse implements the Parser interface

type Node

type Node struct {
	Name     string  // Name of the file or directory
	IsDir    bool    // True if this is a directory
	Children []*Node // Child nodes (only for directories)
	Path     string  // Full path from root (for error reporting)
	Line     int     // Line number in source file (for error reporting)
}

Node represents a single entry in the directory tree

func Parse

func Parse(reader io.Reader, format Format) ([]*Node, error)

Parse reads from the reader and returns the parsed tree structure

func ParseWithIndent

func ParseWithIndent(reader io.Reader, format Format, indentWidth int) ([]*Node, error)

ParseWithIndent reads from the reader with a specific indent width for plain-text

func (*Node) AddChild

func (n *Node) AddChild(name string, isDir bool) *Node

AddChild adds a child node and returns it

func (*Node) CountNodes

func (n *Node) CountNodes() int

CountNodes returns the total number of nodes in the tree

func (*Node) FindChild

func (n *Node) FindChild(name string) *Node

FindChild finds a child by name

func (*Node) Walk

func (n *Node) Walk(fn func(*Node) error) error

Walk recursively walks the tree, calling fn for each node

type ParseError

type ParseError struct {
	Line    int    // Line number where error occurred (1-indexed)
	Column  int    // Column number where error occurred (1-indexed)
	Message string // Error message
	File    string // File being parsed (optional)
}

ParseError represents an error that occurred during parsing

func NewParseError

func NewParseError(line int, message string) *ParseError

NewParseError creates a new ParseError

func (*ParseError) Error

func (e *ParseError) Error() string

Error implements the error interface

type Parser

type Parser interface {
	Parse(reader io.Reader) ([]*Node, error)
}

Parser is the interface that all format parsers must implement

type PlainTextParser

type PlainTextParser struct {
	IndentWidth int  // Expected width of indentation (default 2)
	AutoDetect  bool // Auto-detect indent width from first indented line
}

PlainTextParser parses plain-text tree format

func NewPlainTextParser

func NewPlainTextParser(indentWidth int) *PlainTextParser

NewPlainTextParser creates a new plain-text parser

func (*PlainTextParser) Parse

func (p *PlainTextParser) Parse(reader io.Reader) ([]*Node, error)

Parse implements the Parser interface

func (*PlainTextParser) ValidateIndentation

func (p *PlainTextParser) ValidateIndentation(lines []string) error

ValidateIndentation checks if all lines use consistent indentation

type SyntaxError

type SyntaxError struct {
	ParseError
	Context string // Additional context about what was expected
}

SyntaxError represents a syntax error in the input

func NewSyntaxError

func NewSyntaxError(line int, message, context string) *SyntaxError

NewSyntaxError creates a new SyntaxError

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

Error implements the error interface

type YAMLParser

type YAMLParser struct{}

YAMLParser parses YAML format layout files

func NewYAMLParser

func NewYAMLParser() *YAMLParser

NewYAMLParser creates a new YAML parser

func (*YAMLParser) Parse

func (p *YAMLParser) Parse(reader io.Reader) ([]*Node, error)

Parse implements the Parser interface

Jump to

Keyboard shortcuts

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