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

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..4e345970614b11f3c69c941ee56dcd5b38df49da 100644
--- a/mojom/mojom_parser/lexer/token_stream.go
+++ b/mojom/mojom_parser/lexer/token_stream.go
@@ -49,3 +49,54 @@ func (s *TokenChan) ConsumeNext() {
s.nextToken = eofToken
}
}
+
+// *FilteredTokenStream implements TokenStream
+// This implementation uses an underlying implementation of TokenStream, but
+// it implements a TokenKind filter. All tokens of the filtered TokenKinds
+// will be silently dropped.
+type FilteredTokenStream struct {
+ // tokenStream is the underlying TokenStream that is the data source.
+ tokenStream TokenStream
+ // filter contains the TokenKinds that should be skipped in the source
+ // TokenStream.
+ filter map[TokenKind]bool
+}
+
+// See TokenStream.
+func (s *FilteredTokenStream) PeekNext() (token Token) {
+ s.skipBlacklisted()
+ return s.tokenStream.PeekNext()
+}
+
+// See TokenStream.
+func (s *FilteredTokenStream) ConsumeNext() {
+ s.skipBlacklisted()
+ s.tokenStream.ConsumeNext()
+ s.skipBlacklisted()
+}
+
+func (s *FilteredTokenStream) skipBlacklisted() {
+ t := s.tokenStream.PeekNext()
+ for s.isBlacklisted(t) {
+ s.tokenStream.ConsumeNext()
+ t = s.tokenStream.PeekNext()
+ }
+}
+
+// isBlacklisted checks if a Token is of a filtered TokenKind.
+func (s *FilteredTokenStream) isBlacklisted(token Token) (ok bool) {
+ _, ok = s.filter[token.Kind]
+ return
+}
+
+func NewFilteredTokenStream(ts TokenStream, filter []TokenKind) (filteredTS *FilteredTokenStream) {
+ filteredTS = &FilteredTokenStream{ts, map[TokenKind]bool{}}
+ for _, tk := range filter {
+ if tk == EOF {
+ // We don't allow filtering EOF since that would cause an infinite loop.
+ panic("If the EOF token is filtered, there is no way to find the end of the stream.")
+ }
+ filteredTS.filter[tk] = true
+ }
+ return
+}
« 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