kdl

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: MIT Imports: 19 Imported by: 0

README

KDLGo

GoDoc

WIP Go parser for the KDL Document Language, version 1.0.0.

Current status

  • parsing to a kdl.Document model
  • serializing a kdl.Document model to a string
  • marshalling from a struct
  • unmarshalling to a struct
  • improve performance?

Usage

import (
	kdl "github.com/frixuu/kdlgo"
)
Parse (to a Document model)
// or any of: ParseBytes, ParseFile, ParseReader
document, err := kdl.ParseString(`foo bar="baz"`)
Modify the Document
if document.Nodes[0].HasProp("bar") {
	n := kdl.NewNode("person")
	n.AddArg("known")
	// or: n.AddArgValue(kdl.NewStringValue("known", kdl.NoHint()))
	n.SetProp("name", "Joe")
	// or: n.SetPropValue("name", kdl.NewStringValue("Joe", kdl.NoHint()))
	document.AddChild(n)
}
Serialize the Document
// or Write() to an io.Writer
s, err := document.WriteString()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidSyntax is a base error for when
	// a parser comes across a document that is not spec-compliant.
	ErrInvalidSyntax = errors.New("invalid syntax")
	// ErrInvalidEncoding is a base error for when
	// an invalid UTF8 byte sequence is encountered.
	ErrInvalidEncoding = errors.New("document is not UTF-8 encoded")
	// ErrUnexpectedEOF is a base error for when
	// the data abruptly ends e.g. inside a string.
	ErrUnexpectedEOF = io.ErrUnexpectedEOF
	// ErrInvalidValueType happens when a raw value cannot be cast to a kdl.Value.
	ErrInvalidValueType = errors.New("cannot transform to a valid kdl.Value type")
)

Functions

This section is empty.

Types

type Document

type Document struct {
	Nodes []Node
}

Document is a top-level unit of the KDL format.

func NewDocument

func NewDocument() Document

NewDocument creates a new Document.

func ParseBytes

func ParseBytes(b []byte) (Document, error)

func ParseFile

func ParseFile(path string) (Document, error)

func ParseReader

func ParseReader(r io.Reader) (Document, error)

func ParseString

func ParseString(s string) (Document, error)

func (*Document) AddChild added in v0.2.0

func (d *Document) AddChild(n Node)

AddChild adds a node to this Document.

func (*Document) Write added in v0.2.0

func (d *Document) Write(w io.Writer) error

Write writes the Document to an io.Writer.

func (*Document) WriteString

func (d *Document) WriteString() (string, error)

WriteString marshals the Document to a new string.

type ErrWithPosition

type ErrWithPosition struct {
	Err    error // The original error.
	Line   int   // Line where the error occurred, 1-indexed.
	Column int   // Column where the error occurred, 0-indexed.
}

ErrWithPosition wraps an error, adding information where in the document did it occur.

func (*ErrWithPosition) Error

func (e *ErrWithPosition) Error() string

Error formats an error message.

func (*ErrWithPosition) Unwrap

func (e *ErrWithPosition) Unwrap() error

Unwrap returns the original error.

type Identifier

type Identifier string

Identifier is a fancy name for a string in place of a node's name, type hint or a property key.

type Node

type Node struct {
	TypeHint TypeHint             // Optional hint about the type of this node.
	Name     Identifier           // Name of the node.
	Args     []Value              // Ordered arguments of the node.
	Props    map[Identifier]Value // Unordered properties of the node. CAN BE NIL.
	Children []Node               // Ordered children of the node.
}

Node is an object in a KDL Document.

func NewNode

func NewNode(name string) Node

NewNode creates a new KDL node.

func (*Node) AddArg

func (n *Node) AddArg(arg interface{}) error

AddArg adds an element as an order-sensitive argument of this Node.

func (*Node) AddArgValue added in v0.2.0

func (n *Node) AddArgValue(arg Value)

AddArgValue adds a Value as an order-sensitive argument of this Node.

func (*Node) AddChild

func (n *Node) AddChild(child Node)

AddChild adds another Node as an order-sensitive child of this Node.

func (*Node) GetProp added in v0.2.0

func (n *Node) GetProp(key Identifier) Value

GetProp returns a property of this Node.

func (*Node) HasProp added in v0.2.0

func (n *Node) HasProp(key Identifier) bool

HasProp returns true if this Node has a property of that name.

func (*Node) RemoveProp added in v0.2.0

func (n *Node) RemoveProp(key Identifier)

RemoveProp removes a property from this Node.

func (*Node) SetProp

func (n *Node) SetProp(key Identifier, value interface{}) error

SetProp sets or replaces a property of this Node.

func (*Node) SetPropValue added in v0.2.0

func (n *Node) SetPropValue(key Identifier, value Value)

SetPropValue sets or replaces a property of this Node.

type TypeHint

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

TypeHint is an optional Identifier associated with a Value.

func Hint

func Hint(name string) TypeHint

Hint constructs a present TypeHint.

func NoHint

func NoHint() TypeHint

NoHint constructs a missing TypeHint.

func (TypeHint) Get

func (h TypeHint) Get() (Identifier, bool)

Get returns the inner type hint, if it exists.

func (TypeHint) IsAbsent

func (h TypeHint) IsAbsent() bool

IsAbsent returns true if the hint does not exist.

func (TypeHint) IsPresent

func (h TypeHint) IsPresent() bool

IsPresent returns true if the hint exists.

func (TypeHint) MustGet

func (h TypeHint) MustGet() Identifier

MustGet returns the inner type hint or panics, if it does not exist.

type TypeTag

type TypeTag byte

TypeTag discriminates between Value types.

const (
	TypeInvalid TypeTag = iota // The described Value is in an invalid state.

	TypeNull    // The described Value holds a null.
	TypeBool    // The described Value holds a boolean.
	TypeString  // The described Value holds a string.
	TypeInteger // The described Value holds an integer.
	TypeFloat   // The described Value holds a floating point number.
)

type Value

type Value struct {
	RawValue interface{}
	TypeHint TypeHint
	Type     TypeTag
}

Value can be used either as an argument or a property to a Node.

func NewBoolValue

func NewBoolValue(v bool, hint TypeHint) Value

NewBoolValue constructs a Value that holds a boolean.

func NewFloatValue

func NewFloatValue(v *big.Float, hint TypeHint) Value

NewFloatValue constructs a Value that holds a float.

func NewIntegerValue

func NewIntegerValue(v *big.Int, hint TypeHint) Value

NewIntegerValue constructs a Value that holds an integer.

func NewNullValue

func NewNullValue(hint TypeHint) Value

NewNullValue constructs a Value that holds a null.

func NewStringValue

func NewStringValue(v string, hint TypeHint) Value

NewStringValue constructs a Value that holds a string.

func ValueOf added in v0.2.0

func ValueOf(v interface{}) (Value, error)

ValueOf tries to construct a Value from a provided object. The resulting Value, if valid, will not have a type hint.

func (Value) BoolValue added in v0.2.0

func (v Value) BoolValue() bool

BoolValue returns the inner bool value or panics, if the Value is not a boolean.

func (Value) FloatValue added in v0.2.0

func (v Value) FloatValue() *big.Float

FloatValue returns the inner float value or panics, if the Value is not a floating point number.

func (Value) IntegerValue added in v0.2.0

func (v Value) IntegerValue() *big.Int

IntegerValue returns the inner int value or panics, if the Value is not an integer.

func (Value) StringValue added in v0.2.0

func (v Value) StringValue() string

StringValue returns the inner string value or panics, if the Value is not a string.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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