charm

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2024 License: MIT Imports: 5 Imported by: 4

Documentation

Overview

Package charm provides common utilities for parsing documents using hand-rolled hierarchical state machines. Its So named because what this package lacks in speed or good sense, it makes up in strange charm.

Index

Constants

View Source
const Eof = rune(-1)

Variables

View Source
var ErrFinished = errors.New("finished")

this provided an alternative to the terminal state holding nil. ( to avoid testing its error when dereferencing )

View Source
var StateName = func(n State) (ret string) {
	if s, ok := n.(interface{ String() string }); !ok {
		ret = "unknown state"
	} else {
		ret = s.String()
	}
	return
}

replaceable function for printing the name of a state by default uses Stringer's String(), if not implemented it returns "unknown state" test packages can overwrite with something that uses package reflect if desired.

Functions

func ParseEof

func ParseEof(str string, first State) error

utility function which creates a string reader, a parser, and calls Parser.ParseEof()

Types

type InvalidRune

type InvalidRune rune

implements error

func (InvalidRune) Error

func (e InvalidRune) Error() string

type Parser added in v0.9.1

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

func MakeParser added in v0.9.1

func MakeParser(in io.RuneReader) Parser

func (*Parser) Error added in v0.9.1

func (p *Parser) Error() error

func (*Parser) Offset added in v0.9.1

func (p *Parser) Offset() int

number of runes read from the input

func (*Parser) Parse added in v0.9.1

func (p *Parser) Parse(first State) (ret State, err error)

always returns an error, and the final state. ex. if the last rune was unhandled, then this returns an UnhandledRune error and the state that failed to handle it.

func (*Parser) ParseEof added in v0.9.1

func (p *Parser) ParseEof(first State) (err error)

run Parse() and send the final state an explicit Eof rune. unlike parse, only returns an error if there was an error. this also unwraps Finished and all terminal errors, returning the underlying error ( if any. )

func (*Parser) Remaining added in v0.9.1

func (p *Parser) Remaining() string

consumes ~25 of the remaining runes for error reporting

type State

type State interface {
	// process the next element of the incoming data,
	// return the next state or nil when done.
	NewRune(rune) State
}

definition of states n the state chart if State implements Stringer StateName() will use it.

func AtleastOne

func AtleastOne(filter func(r rune) bool) State

one or more of the runes must pass the filter

func Error

func Error(e error) State

Used by states to wrap an error in a Terminal state. This is the only way for states to return an error. To stop processing, but return no error: see Finished()

func Finished added in v0.8.1

func Finished() State

A next state indicating an expected termination. This is used to absorb runes.Eof and end a state gracefully because returning nil on Eof will trigger attempts by chained states to handle the Eof themselves.

func MakeState

func MakeState(next func() State) State

Creates a state on demand

func OnExit

func OnExit(name string, onExit func()) State

For use in Step() to run an action after the first step completes.

func Optional

func Optional(filter func(r rune) bool) State

zero or more of the runes must pass the filter

func Parallel

func Parallel(name string, rs ...State) State

Parallel region; run all of the passed states until they all return nil. if any return error, this returns error.

func Require

func Require(filter func(r rune) bool) State

ensure the next rune passes the filter

func RunState

func RunState(r rune, state State) (ret State)

calls NewRune on state. sometimes this is a more convenient notation.

func RunStep

func RunStep(r rune, child, parent State) State

RunStep - run a sequence of of two states sending the current rune to the first state immediately see also: Step()

func Self

func Self(name string, closure func(State, rune) State) State

Self are States which pass a function pointer as the first argument to the state callback. This allows closures to return themselves. For example, the following state returns itself forever:

var recursive  Self = func(self State, r rune) State { return self }

func Statement

func Statement(name string, closure func(rune) State) State

Statement functions behave as a State. ( Self works the same, except that passes the state to its function as "self" and this does not )

func Step

func Step(child, parent State) State

Step - construct a sequence of two states. If the next rune is not handled by the first state or any of its returned states, the rune is handed to the second state. this acts similar to a parent-child statechart.

func UnhandledNext

func UnhandledNext() State

the next rune will return unhandled

type Terminal

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

acts as both an error and a state

func (Terminal) Error

func (e Terminal) Error() string

terminal implements error. returns the string of the wrapped error.

func (Terminal) Finished added in v0.8.1

func (e Terminal) Finished() bool

true if this was an expected termination. see also: the package level Finished().

func (Terminal) NewRune

func (e Terminal) NewRune(r rune) (ret State)

returns itself forever.

func (Terminal) String

func (e Terminal) String() string

implements string for printing states.

func (Terminal) Unwrap

func (e Terminal) Unwrap() error

access the underlying error.

type UnhandledRune added in v0.9.1

type UnhandledRune rune

returned from Parser

func (UnhandledRune) Error added in v0.9.1

func (u UnhandledRune) Error() string

Jump to

Keyboard shortcuts

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