json: token peeker
This will help our hand-written recursive-descent parser walk the tokens with one token of lookahead.
This commit is contained in:
parent
0c1a99df48
commit
838a0332e6
24
zcl/json/peeker.go
Normal file
24
zcl/json/peeker.go
Normal file
@ -0,0 +1,24 @@
|
||||
package json
|
||||
|
||||
type peeker struct {
|
||||
tokens []token
|
||||
pos int
|
||||
}
|
||||
|
||||
func newPeeker(tokens []token) *peeker {
|
||||
return &peeker{
|
||||
tokens: tokens,
|
||||
pos: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *peeker) Peek() token {
|
||||
return p.tokens[p.pos]
|
||||
}
|
||||
|
||||
func (p *peeker) Read() token {
|
||||
if p.tokens[p.pos].Type != tokenEOF {
|
||||
p.pos++
|
||||
}
|
||||
return p.tokens[p.pos]
|
||||
}
|
Loading…
Reference in New Issue
Block a user