start int
pos int
width int
+ state stateFn // Used by non-concurrent 'lexNoConcurrent'
items chan item
}
return nil
}
+func lexNoConcurrent(name, input string) *lexer {
+ l := &lexer{
+ name: name,
+ input: input,
+ state: lexText,
+ items: make(chan item, 2),
+ }
+ return l
+}
+
+func (l *lexer) nextItem() item {
+ for {
+ select {
+ case item := <-l.items:
+ return item
+ default:
+ l.state = l.state(l)
+ }
+ }
+ panic("not reached")
+}
+
func main() {
+ fmt.Println("### Concurrent version:")
_, ch := lex("huy", "text {{ 1.34e3 }} yet another text")
for it := range ch {
fmt.Printf("Received %v\n", it)
}
fmt.Println("End")
+
+ fmt.Println("\n### Not concurrent version:")
+ l := lexNoConcurrent("huy", "text {{ 1.34e3 }} yet another text")
+ for it := l.nextItem(); it.typ != itemEOF; it = l.nextItem() {
+ fmt.Printf("Received %v\n", it)
+ }
+ fmt.Println("End")
}