casketfile

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromJSON

func FromJSON(jsonBytes []byte) ([]byte, error)

FromJSON converts JSON-encoded jsonBytes to Casketfile text

func ToJSON

func ToJSON(casketfile []byte) ([]byte, error)

ToJSON converts casketfile to its JSON representation.

Types

type Dispenser

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

Dispenser is a type that dispenses tokens, similarly to a lexer, except that it can do so with some notion of structure and has some really convenient methods.

func NewDispenser

func NewDispenser(filename string, input io.Reader) Dispenser

NewDispenser returns a Dispenser, ready to use for parsing the given input.

func NewDispenserTokens

func NewDispenserTokens(filename string, tokens []Token) Dispenser

NewDispenserTokens returns a Dispenser filled with the given tokens.

func (*Dispenser) ArgErr

func (d *Dispenser) ArgErr() error

ArgErr returns an argument error, meaning that another argument was expected but not found. In other words, a line break or open curly brace was encountered instead of an argument.

func (*Dispenser) Args

func (d *Dispenser) Args(targets ...*string) bool

Args is a convenience function that loads the next arguments (tokens on the same line) into an arbitrary number of strings pointed to in targets. If there are fewer tokens available than string pointers, the remaining strings will not be changed and false will be returned. If there were enough tokens available to fill the arguments, then true will be returned.

func (*Dispenser) EOFErr

func (d *Dispenser) EOFErr() error

EOFErr returns an error indicating that the dispenser reached the end of the input when searching for the next token.

func (*Dispenser) Err

func (d *Dispenser) Err(msg string) error

Err generates a custom parse-time error with a message of msg.

func (*Dispenser) Errf

func (d *Dispenser) Errf(format string, args ...interface{}) error

Errf is like Err, but for formatted error messages

func (*Dispenser) File

func (d *Dispenser) File() string

File gets the filename of the current token. If there is no token loaded, it returns the filename originally given when parsing started.

func (*Dispenser) Line

func (d *Dispenser) Line() int

Line gets the line number of the current token. If there is no token loaded, it returns 0.

func (*Dispenser) Nesting added in v1.4.0

func (d *Dispenser) Nesting() int

Nesting returns the current nesting level. Necessary if using NextBlock()

func (*Dispenser) Next

func (d *Dispenser) Next() bool

Next loads the next token. Returns true if a token was loaded; false otherwise. If false, all tokens have been consumed.

func (*Dispenser) NextArg

func (d *Dispenser) NextArg() bool

NextArg loads the next token if it is on the same line. Returns true if a token was loaded; false otherwise. If false, all tokens on the line have been consumed. It handles imported tokens correctly.

func (*Dispenser) NextBlock

func (d *Dispenser) NextBlock() bool

NextBlock is equivalent to NextBlockNesting(0).

func (*Dispenser) NextBlockNesting added in v1.4.0

func (d *Dispenser) NextBlockNesting(initialNestingLevel int) bool

NextBlockNesting can be used as the condition of a for loop to load the next token as long as it opens a block or is already in a block nested more than initialNestingLevel. In other words, a loop over NextBlockNesting() will iterate all tokens in the block assuming the next token is an open curly brace, until the matching closing brace. The open and closing brace tokens for the outer-most block will be consumed internally and omitted from the iteration.

Proper use of this method looks like this:

for nesting := d.Nesting(); d.NextBlockNesting(nesting); {
}

However, in simple cases where it is known that the Dispenser is new and has not already traversed state by a loop over NextBlockNesting(), this will do:

for d.NextBlockNesting() {
}

As with other token parsing logic, a loop over NextBlockNesting() should be contained within a loop over Next(), as it is usually prudent to skip the initial token.

func (*Dispenser) NextLine

func (d *Dispenser) NextLine() bool

NextLine loads the next token only if it is not on the same line as the current token, and returns true if a token was loaded; false otherwise. If false, there is not another token or it is on the same line. It handles imported tokens correctly.

func (*Dispenser) RemainingArgs

func (d *Dispenser) RemainingArgs() []string

RemainingArgs loads any more arguments (tokens on the same line) into a slice and returns them. Open curly brace tokens also indicate the end of arguments, and the curly brace is not included in the return value nor is it loaded.

func (*Dispenser) SyntaxErr

func (d *Dispenser) SyntaxErr(expected string) error

SyntaxErr creates a generic syntax error which explains what was found and what was expected.

func (*Dispenser) Val

func (d *Dispenser) Val() string

Val gets the text of the current token. If there is no token loaded, it returns empty string.

type EncodedCasketfile

type EncodedCasketfile []EncodedServerBlock

EncodedCasketfile encapsulates a slice of EncodedServerBlocks.

type EncodedServerBlock

type EncodedServerBlock struct {
	Keys []string        `json:"keys"`
	Body [][]interface{} `json:"body"`
}

EncodedServerBlock represents a server block ripe for encoding.

type ServerBlock

type ServerBlock struct {
	Keys   []string
	Tokens map[string][]Token
}

ServerBlock associates any number of keys (usually addresses of some sort) with tokens (grouped by directive name).

func Parse

func Parse(filename string, input io.Reader, validDirectives []string) ([]ServerBlock, error)

Parse parses the input just enough to group tokens, in order, by server block. No further parsing is performed. Server blocks are returned in the order in which they appear. Directives that do not appear in validDirectives will cause an error. If you do not want to check for valid directives, pass in nil instead.

type Token

type Token struct {
	File string
	Line int
	Text string
}

Token represents a single parsable unit.

func (Token) NumLineBreaks added in v1.4.0

func (t Token) NumLineBreaks() int

NumLineBreaks counts how many line breaks are in the token text.

Jump to

Keyboard shortcuts

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