go-toml/parsekit/staterouting.go

53 lines
993 B
Go

package parsekit
func (p *P) RouteRepeat() {
p.nextState = p.state
return
}
type RouteFollowup struct {
p *P
}
func (p *P) RouteTo(state StateFn) *RouteFollowup {
p.nextState = state
return &RouteFollowup{p}
}
func (r *RouteFollowup) ThenTo(state StateFn) *RouteFollowup {
r.p.PushState(state)
return r
}
func (r *RouteFollowup) ThenReturnHere() {
r.p.PushState(r.p.state)
}
func (p *P) RouteReturn() {
p.nextState = p.PopState()
}
func (p *P) ToChildState(state StateFn) StateFn {
p.PushState(p.state)
return state
}
func (p *P) ToParentState() StateFn {
state := p.PopState()
return state
}
// PushState adds the state function to the state stack.
// This is used for implementing nested parsing.
func (p *P) PushState(state StateFn) {
p.stack = append(p.stack, state)
}
// PopState pops the last pushed state from the state stack.
func (p *P) PopState() StateFn {
last := len(p.stack) - 1
head, tail := p.stack[:last], p.stack[last]
p.stack = head
return tail
}