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
- Variables
- func ParseEof(str string, first State) (err error)
- func Read(in io.RuneReader, first State) (err error)
- type EndpointError
- type InvalidRune
- type State
- func AtleastOne(filter func(r rune) bool) State
- func Error(e error) State
- func Finished() State
- func MakeState(next func() State) State
- func OnExit(name string, onExit func()) State
- func Optional(filter func(r rune) bool) State
- func Parallel(name string, rs ...State) State
- func Parse(str string, first State) (ret State, err error)
- func Require(filter func(r rune) bool) State
- func RunState(r rune, state State) (ret State)
- func RunStep(r rune, child, parent State) State
- func Self(name string, closure func(State, rune) State) State
- func Statement(name string, closure func(rune) State) State
- func Step(child, parent State) State
- func UnhandledNext() State
- type Terminal
Constants ¶
const Eof = rune(-1)
Variables ¶
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 ¶
Types ¶
type EndpointError ¶
type EndpointError struct {
// contains filtered or unexported fields
}
ended before the whole input was parsed.
func (EndpointError) Error ¶
func (e EndpointError) Error() (ret string)
type InvalidRune ¶
type InvalidRune rune
implements error
func (InvalidRune) Error ¶
func (e InvalidRune) Error() string
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 ¶
one or more of the runes must pass the filter
func Finished ¶ added in v0.8.1
func Finished() State
a next state indicating an expected termination. ( for example, to absorb runes.Eof and end a state gracefully. | whereas returning nil from a state would consider the Eof unhandled | and trigger attempts by chained states to handle the Eof themselves. )
func Parallel ¶
Parallel region; run all of the passed states until they all return nil. if any return error, this returns error.
func Parse ¶
Parse sends each rune of string to the passed state chart, Returns the error underlying error states, or the last returned state if there was no error.
func RunStep ¶
RunStep - run a sequence of of two states sending the current rune to the first state immediately see also: Step()
func Self ¶
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 ¶
Statement functions behave as a State. ( Self works the same, except that passes the state to its function as "self" and this does not )