| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // TokenStream is the interface between the lexer and the parser. The lexer | 5 // TokenStream is the interface between the lexer and the parser. The lexer |
| 6 // creates a TokenStream which the parser consumes. | 6 // creates a TokenStream which the parser consumes. |
| 7 | 7 |
| 8 package lexer | 8 package lexer |
| 9 | 9 |
| 10 type TokenStream interface { | 10 type TokenStream interface { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 } | 42 } |
| 43 | 43 |
| 44 // See TokenStream. | 44 // See TokenStream. |
| 45 func (s *TokenChan) ConsumeNext() { | 45 func (s *TokenChan) ConsumeNext() { |
| 46 if t, open := <-s.tokenChan; open { | 46 if t, open := <-s.tokenChan; open { |
| 47 s.nextToken = t | 47 s.nextToken = t |
| 48 } else { | 48 } else { |
| 49 s.nextToken = eofToken | 49 s.nextToken = eofToken |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 |
| 53 // *FilteredTokenStream implements TokenStream |
| 54 // This implementation uses an underlying implementation of TokenStream, but |
| 55 // it implements a TokenKind filter. All tokens of the filtered TokenKinds |
| 56 // will be silently dropped. |
| 57 type FilteredTokenStream struct { |
| 58 // tokenStream is the underlying TokenStream that is the data source. |
| 59 tokenStream TokenStream |
| 60 // filter contains the TokenKinds that should be skipped in the source |
| 61 // TokenStream. |
| 62 filter map[TokenKind]bool |
| 63 } |
| 64 |
| 65 // See TokenStream. |
| 66 func (s *FilteredTokenStream) PeekNext() (token Token) { |
| 67 s.skipBlacklisted() |
| 68 return s.tokenStream.PeekNext() |
| 69 } |
| 70 |
| 71 // See TokenStream. |
| 72 func (s *FilteredTokenStream) ConsumeNext() { |
| 73 s.skipBlacklisted() |
| 74 s.tokenStream.ConsumeNext() |
| 75 s.skipBlacklisted() |
| 76 } |
| 77 |
| 78 func (s *FilteredTokenStream) skipBlacklisted() { |
| 79 t := s.tokenStream.PeekNext() |
| 80 for s.isBlacklisted(t) { |
| 81 s.tokenStream.ConsumeNext() |
| 82 t = s.tokenStream.PeekNext() |
| 83 } |
| 84 } |
| 85 |
| 86 // isBlacklisted checks if a Token is of a filtered TokenKind. |
| 87 func (s *FilteredTokenStream) isBlacklisted(token Token) (ok bool) { |
| 88 _, ok = s.filter[token.Kind] |
| 89 return |
| 90 } |
| 91 |
| 92 func NewFilteredTokenStream(ts TokenStream, filter []TokenKind) (filteredTS *Fil
teredTokenStream) { |
| 93 filteredTS = &FilteredTokenStream{ts, map[TokenKind]bool{}} |
| 94 for _, tk := range filter { |
| 95 if tk == EOF { |
| 96 // We don't allow filtering EOF since that would cause a
n infinite loop. |
| 97 panic("If the EOF token is filtered, there is no way to
find the end of the stream.") |
| 98 } |
| 99 filteredTS.filter[tk] = true |
| 100 } |
| 101 return |
| 102 } |
| OLD | NEW |