Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1813)

Unified Diff: mojom/mojom_parser/lexer/token_stream.go

Issue 1593543004: Update the mojom lexer to emit comment tokens. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojom/mojom_parser/lexer/lexer_test.go ('k') | mojom/mojom_parser/lexer/tokens.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojom/mojom_parser/lexer/token_stream.go
diff --git a/mojom/mojom_parser/lexer/token_stream.go b/mojom/mojom_parser/lexer/token_stream.go
index beb399a164ca443a0f9293736706e7b1e124be01..7a6d53c0b6e3afb42352bcbe79c288abe065c962 100644
--- a/mojom/mojom_parser/lexer/token_stream.go
+++ b/mojom/mojom_parser/lexer/token_stream.go
@@ -49,3 +49,41 @@ func (s *TokenChan) ConsumeNext() {
s.nextToken = eofToken
}
}
+
+// *FilteredTokenStream implements TokenStream
+// This implementation uses an underlying implementation of TokenStream, but
+// it implements a TokenKind blacklist. All tokens of the blacklisted TokenKinds
+// will be silently dropped.
+type FilteredTokenStream struct {
+ // tokenStream is the underlying TokenStream that is the data source.
+ tokenStream TokenStream
+ // blacklist contains the TokenKinds that should be skipped in the source
+ // TokenStream.
+ blacklist map[TokenKind]bool
+}
+
+// See TokenStream.
+func (s *FilteredTokenStream) PeekNext() (token Token) {
+ t := s.tokenStream.PeekNext()
+ for s.isBlacklisted(t) {
+ s.tokenStream.ConsumeNext()
+ t = s.tokenStream.PeekNext()
+ }
+
+ return t
+}
+
+// See TokenStream.
+func (s *FilteredTokenStream) ConsumeNext() {
+ 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.
+}
+
+// isBlacklisted checks if a Token is of a blacklisted TokenKind.
+func (s *FilteredTokenStream) isBlacklisted(token Token) bool {
+ // We don't allow blacklisting EOF since that would cause an infinite loop.
+ 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.
+ return false
+ }
+ _, 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.
+ return ok
+}
« no previous file with comments | « mojom/mojom_parser/lexer/lexer_test.go ('k') | mojom/mojom_parser/lexer/tokens.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698