parser

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2024 License: MIT Imports: 5 Imported by: 16

Documentation

Overview

Package parser implements a parser for TOML, as defined by the TOML v1 specification https://toml.io/en/v1.0.0.

Unlike other TOML libraries, this parser does not unmarshal into Go values, but only constructs an abstract syntax tree for its input. This is meant to support manipulating the structure of a TOML document.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanTrailer added in v0.0.11

func CleanTrailer(s string) string

CleanTrailer returns a copy of s that is suitable for use as a line-ending comment. It removes leading and trailing whitespace and prepends a "#" marker if necessary. If s contains newlines, they are converted to spaces.

Types

type Array

type Array []ArrayItem

An Array represents a (possibly empty) array value.

func (Array) String

func (a Array) String() string

type ArrayItem

type ArrayItem interface {
	// contains filtered or unexported methods
}

An ArrayItem is an element in a TOML array value. The concrete type of an ArrayItem is one of Comments or Value.

type Comments

type Comments []string

Comments is an Item that represents a block of comments. Each entry represents a single comment line.

Comment lines parsed from a source file include the comment marker at the beginning. When constructing comments programmatically, the comment marker is optional; one will be prepended if it does not exist.

func (Comments) Clean added in v0.0.11

func (c Comments) Clean() []string

Clean returns a copy of c in which each line has been "cleaned", removing leading and trailing whitespace and prepending a "#" marker to each line that does not already have one. If a line includes embedded newlines, it is split into multiple lines, each of which is cleaned.

func (Comments) String

func (c Comments) String() string

type Datum

type Datum interface {
	String() string
	// contains filtered or unexported methods
}

A Datum is the representation of a data value. The concrete type of a Datum is one of Token, Array, or Inline.

type Heading

type Heading struct {
	Block   Comments // a block comment before the heading (empty if none)
	Trailer string   // a trailing line comment after the heading (empty if none)
	IsArray bool     // whether this table is part of a table array
	Name    Key      // the name of the table
	Line    int      // the input line where the heading was defined (1-based)
}

Heading is an Item that represents a table section heading.

func (*Heading) String

func (h *Heading) String() string

type Inline

type Inline []*KeyValue

An Inline represents a (possibly empty) inline table value.

func (Inline) String

func (t Inline) String() string

type Item

type Item interface {
	// contains filtered or unexported methods
}

An Item is an element in a TOML document. The concrete type of an Item is one of Comments, Heading, or KeyValue.

type Key

type Key []string

A Key represents a dotted compound name.

func ParseKey

func ParseKey(s string) (Key, error)

ParseKey parses s as a TOML key.

func (Key) Before added in v0.0.8

func (k Key) Before(k2 Key) bool

Before reports whether k is lexicographically prior to k2.

func (Key) Equals

func (k Key) Equals(k2 Key) bool

Equals reports whether k and k2 are equal.

func (Key) IsPrefixOf

func (k Key) IsPrefixOf(k2 Key) bool

IsPrefixOf reports whether k is a prefix of k2.

func (Key) String

func (k Key) String() string

type KeyValue

type KeyValue struct {
	Block Comments // a block comment before the key-value pair (empty if none)
	Name  Key
	Value Value
	Line  int // the input line where the key-value was defined (1-based)
}

KeyValue is an Item that represents a key-value definition.

func (*KeyValue) String

func (kv *KeyValue) String() string

type Parser

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

A Parser is a parser for TOML syntax.

func New

func New(r io.Reader) *Parser

New constructs a new parser that consumes input from r.

func (*Parser) Items

func (p *Parser) Items() ([]Item, error)

Items reads the top-level items from the input.

type Token

type Token struct {
	Type scanner.Token // the lexical type of the token
	// contains filtered or unexported fields
}

A Token represents a lexical data element such as a string, integer, floating point literal, Boolean, or date/time literal.

func (Token) String

func (t Token) String() string

type Value

type Value struct {
	Trailer string // a trailing line-comment after the value (empty if none)
	X       Datum  // the concrete value
	Line    int    // the input line where the value is defined (1-based)
}

A Value represents a value in an array or a key-value assignment.

func MustValue

func MustValue(s string) Value

MustValue parses s as a TOML value. It panics if parsing fails. This is intended for use at program initialization time, or for static string constants that are expected to be always valid. For all other cases, use ParseValue to check the error.

func ParseValue

func ParseValue(s string) (Value, error)

ParseValue parses s as a TOML value.

func (Value) String

func (v Value) String() string

func (Value) WithComment added in v0.0.4

func (v Value) WithComment(text string) Value

WithComment returns a copy of v with its trailer set to text.

Jump to

Keyboard shortcuts

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