libparser

package module
v0.2.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2025 License: ISC Imports: 9 Imported by: 2

README

Tomefile Parser

Library to parse Tomefile code and output a node tree for use by another program.

Usage

Parsing a file:

parser := libparser.New("example.tome", bufio.NewReader(file))
tree, err := parser.Parse()
if err != nil {
    err.BeautyPrint(os.Stderr)
    os.Exit(1)
}

// [tree] is [*libparser.NodeTree]

Formatting a variable (i.e. $name ${name:mod} etc.)

formatter := libparser.NewStringFormatter(
    bufio.NewReader(strings.NewReader(
        "this is an example $string with ${string:trim_suffix 123}",
    )),
)

segments, err := formatter.Format()
if err != nil {
    err.BeautyPrint(os.Stderr)
    os.Exit(1)
}

// [segments] is [[]libparser.Segment]

Documentation

Index

Constants

View Source
const (
	ERROR_READING    = "Reading Error"
	ERROR_SYNTAX     = "Syntax Error"
	ERROR_INTERNAL   = "Internal Error"
	ERROR_FORMATTING = "Formatting Error"
)

Variables

View Source
var EOF = &ParsingError{Name: "EOF"}

Functions

This section is empty.

Types

type CallNode

type CallNode struct {
	Macro string

	NodeArgs
}

An executable external program

func (*CallNode) Node

func (node *CallNode) Node() string

type CommentNode

type CommentNode struct {
	Contents string
}

Can be used for documentation

func (*CommentNode) Node

func (node *CommentNode) Node() string

type DirectiveNode

type DirectiveNode struct {
	Name string

	NodeArgs
	NodeChildren
}

func (*DirectiveNode) Node

func (node *DirectiveNode) Node() string

type ExecNode

type ExecNode struct {
	Binary string

	NodeArgs
}

An executable external program

func (*ExecNode) Node

func (node *ExecNode) Node() string

type LiteralNode

type LiteralNode struct {
	Contents string
}

A literal string does not get modified in any way.

func (*LiteralNode) Eval

func (node *LiteralNode) Eval(_ Locals) (string, error)

func (*LiteralNode) Node

func (node *LiteralNode) Node() string

type Locals

type Locals map[string]string

type Node

type Node interface {
	Node() string
}

type NodeArgs

type NodeArgs []Node

type NodeChildren

type NodeChildren []Node

type NodeTree

type NodeTree struct {
	Tomes map[string]Node

	NodeChildren
}

type Parser

type Parser struct {
	Name string
	// contains filtered or unexported fields
}

func New

func New(name string, reader *bufio.Reader) *Parser

func (*Parser) Parse

func (parser *Parser) Parse() (*NodeTree, *ParsingError)

type ParsingError

type ParsingError struct {
	Name    string
	Details string

	File     string
	Col, Row uint
	Context  string
}

func (*ParsingError) BeautyPrint

func (err *ParsingError) BeautyPrint(writer io.Writer)

func (*ParsingError) Error

func (err *ParsingError) Error() string

func (*ParsingError) GetBeautyPrinted

func (err *ParsingError) GetBeautyPrinted() string

type Segment

type Segment interface {
	Eval(Locals) (string, error)
}

type StringFormatter

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

func NewStringFormatter

func NewStringFormatter(reader *bufio.Reader) *StringFormatter

func (*StringFormatter) Format

func (formatter *StringFormatter) Format() ([]Segment, *ParsingError)

type StringModifier

type StringModifier func(string) string

func GetModifier

func GetModifier(name string, args []string) StringModifier

type StringNode

type StringNode struct {
	Contents string
}

A string that can have variable expansions inside. Uses backticks (`) instead of quotes.

func (*StringNode) Node

func (node *StringNode) Node() string

type VariableSegment

type VariableSegment struct {
	Name       string
	Modifier   StringModifier
	IsOptional bool
}

func (*VariableSegment) Eval

func (segment *VariableSegment) Eval(locals Locals) (string, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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