go-toml/parsekit/staterouting.go

43 lines
821 B
Go

package parsekit
func (p *P) Repeat() {
p.nextState = p.state
return
}
func (p *P) RouteTo(state StateFn) *routeFollowup {
p.nextState = state
return &routeFollowup{p}
}
type routeFollowup struct {
p *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()
}
// 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
}