Chromium Code Reviews| 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 blacklist. All tokens of the blacklisted 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 // blacklist contains the TokenKinds that should be skipped in the sourc e | |
| 61 // TokenStream. | |
| 62 blacklist map[TokenKind]bool | |
| 63 } | |
| 64 | |
| 65 // See TokenStream. | |
| 66 func (s *FilteredTokenStream) PeekNext() (token Token) { | |
| 67 t := s.tokenStream.PeekNext() | |
| 68 for s.isBlacklisted(t) { | |
| 69 s.tokenStream.ConsumeNext() | |
| 70 t = s.tokenStream.PeekNext() | |
| 71 } | |
| 72 | |
| 73 return t | |
| 74 } | |
| 75 | |
| 76 // See TokenStream. | |
| 77 func (s *FilteredTokenStream) ConsumeNext() { | |
| 78 s.tokenStream.ConsumeNext() | |
|
rudominer
2016/01/20 00:40:31
I wonder if there should be a ConsumeNextInternal(
azani
2016/01/25 20:37:06
Done.
| |
| 79 } | |
| 80 | |
| 81 // isBlacklisted checks if a Token is of a blacklisted TokenKind. | |
| 82 func (s *FilteredTokenStream) isBlacklisted(token Token) bool { | |
| 83 // We don't allow blacklisting EOF since that would cause an infinite lo op. | |
| 84 if token.Kind == EOF { | |
|
rudominer
2016/01/20 00:40:31
Instead of this, make a constructor for FilteredTo
azani
2016/01/25 20:37:06
Done.
| |
| 85 return false | |
| 86 } | |
| 87 _, ok := s.blacklist[token.Kind] | |
|
rudominer
2016/01/20 00:40:31
After the above change you can make this function
azani
2016/01/25 20:37:06
Done.
| |
| 88 return ok | |
| 89 } | |
| OLD | NEW |