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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 }
OLDNEW
« 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