go-toml/parsekit/statehandler_on_route.go

60 lines
1.6 KiB
Go

package parsekit
// RouteAction is a struct that is used for building On() method chains.
type RouteAction struct {
ChainAction
}
// RouteRepeat indicates that on the next parsing cycle,
// the current StateHandler must be reinvoked.
func (a *RouteAction) RouteRepeat() *ChainAction {
if a.ok {
return a.p.RouteRepeat()
}
return &ChainAction{nil, false}
}
// RouteTo tells the parser what StateHandler function to invoke
// in the next parsing cycle.
func (a *RouteAction) RouteTo(state StateHandler) *RouteFollowupAction {
if a.ok {
return a.p.RouteTo(state)
}
return &RouteFollowupAction{ChainAction: ChainAction{nil, false}}
}
// RouteReturn tells the parser that on the next cycle the next scheduled
// route must be invoked.
func (a *RouteAction) RouteReturn() *ChainAction {
if a.ok {
return a.p.RouteReturn()
}
return &ChainAction{nil, false}
}
// RouteFollowupAction chains parsing routes.
// It allows for routing code like p.RouteTo(handlerA).ThenTo(handlerB).
type RouteFollowupAction struct {
ChainAction
}
// ThenTo schedules a StateHandler that must be invoked after the RouteTo
// StateHandler has been completed.
// For example: p.RouteTo(handlerA).ThenTo(handlerB)
func (a *RouteFollowupAction) ThenTo(state StateHandler) *ChainAction {
if a.ok {
a.p.pushRoute(state)
}
return &ChainAction{nil, a.ok}
}
// ThenReturnHere schedules the current StateHandler to be invoked after
// the RouteTo StateHandler has been completed.
// For example: p.RouteTo(handlerA).ThenReturnHere()
func (a *RouteFollowupAction) ThenReturnHere() *ChainAction {
if a.ok {
a.p.pushRoute(a.p.state)
}
return &ChainAction{nil, a.ok}
}