github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/schemadsl/lexer/peekable_lex.go (about) 1 package lexer 2 3 import ( 4 "container/list" 5 ) 6 7 // PeekableLexer wraps a lexer and provides the ability to peek forward without 8 // losing state. 9 type PeekableLexer struct { 10 lex *Lexer // a reference to the lexer used for tokenization 11 readTokens *list.List // tokens already read from the lexer during a lookahead. 12 } 13 14 // NewPeekableLexer returns a new PeekableLexer for the given lexer. 15 func NewPeekableLexer(lex *Lexer) *PeekableLexer { 16 return &PeekableLexer{ 17 lex: lex, 18 readTokens: list.New(), 19 } 20 } 21 22 // Close stops the lexer from running. 23 func (l *PeekableLexer) Close() { 24 l.lex.Close() 25 } 26 27 // NextToken returns the next token found in the lexer. 28 func (l *PeekableLexer) NextToken() Lexeme { 29 frontElement := l.readTokens.Front() 30 if frontElement != nil { 31 return l.readTokens.Remove(frontElement).(Lexeme) 32 } 33 34 return l.lex.nextToken() 35 }