3 // Peeker is a utility that wraps a token channel returned by Scan and
4 // provides an interface that allows a caller (e.g. the parser) to
5 // work with the token stream in a mode that allows one token of lookahead,
6 // and provides utilities for more convenient processing of the stream.
12 func NewPeeker(ch <-chan *Token) *Peeker {
18 // Peek returns the next token in the stream without consuming it. A
19 // subsequent call to Read will return the same token.
20 func (p *Peeker) Peek() *Token {
27 // Read consumes the next token in the stream and returns it.
28 func (p *Peeker) Read() *Token {
31 // As a special case, we will produce the EOF token forever once
33 if token.Type != EOF {
40 // Close ensures that the token stream has been exhausted, to prevent
41 // the goroutine in the underlying scanner from leaking.
43 // It's not necessary to call this if the caller reads the token stream
44 // to EOF, since that implicitly closes the scanner.
45 func (p *Peeker) Close() {
49 // Install a synthetic EOF token in 'peeked' in case someone
50 // erroneously calls Peek() or Read() after we've closed.