OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CSSParserTokenStream_h | |
6 #define CSSParserTokenStream_h | |
7 | |
8 #include "core/css/parser/CSSParserToken.h" | |
9 #include "core/css/parser/CSSParserTokenRange.h" | |
10 #include "core/css/parser/CSSTokenizer.h" | |
11 | |
12 namespace blink { | |
13 | |
14 class CSSParserObserver; | |
15 | |
16 // This class is a lazily tokenized stream of CSSParserTokens. It provides only | |
17 // a single token of lookahead. When this is not sufficient, the functions | |
18 // index(), consumeUntilAtEndOrPeekedTypeIs() and makeSubRangeFrom() are used | |
19 // to make a CSSParserTokenRange. | |
20 class CSSParserTokenStream { | |
21 public: | |
22 enum MakeSubStreamTag { MakeSubStream }; | |
Charlie Harrison
2017/01/09 21:35:07
optional: Put this right above the constructor tha
| |
23 | |
24 explicit CSSParserTokenStream(CSSTokenizer& tokenizer) | |
25 : m_tokenizer(tokenizer), | |
26 m_startIndex(0), | |
27 m_currentIndex(0), | |
28 m_isSubStream(false) { | |
29 DCHECK(tokenizer.m_tokens.isEmpty()); | |
30 } | |
31 | |
32 // This constructor can be called when the next token is a LeftBraceToken. | |
33 // The new stream will end at the end of the block and clean up by deleting | |
34 // all the tokens in the block. The original stream cannot be used until the | |
35 // sub-stream is destroyed. | |
36 CSSParserTokenStream(CSSParserTokenStream& stream, MakeSubStreamTag) | |
37 : m_tokenizer(stream.m_tokenizer), | |
38 m_startIndex(stream.m_currentIndex), | |
39 m_currentIndex(stream.m_currentIndex + 1), | |
40 m_isSubStream(true) { | |
41 DCHECK_EQ(m_tokenizer.m_tokens.size(), m_currentIndex); | |
42 DCHECK_EQ(m_tokenizer.m_tokens.back().type(), LeftBraceToken); | |
43 } | |
44 | |
45 ~CSSParserTokenStream(); | |
46 | |
47 // Delete all the tokens this stream has consumed. This is used to ensure | |
48 // the tokenizer doesn't take up much memory. | |
49 void clean() { | |
50 m_tokenizer.m_tokens.remove(m_startIndex, m_currentIndex - m_startIndex); | |
Charlie Harrison
2017/01/09 21:35:07
I'll need to look at all usage, but it seems like
| |
51 m_currentIndex = m_startIndex; | |
52 } | |
53 | |
54 const CSSParserToken& peek() { | |
55 const CSSParserToken& result = peekInternal(); | |
56 if (result.getBlockType() == CSSParserToken::BlockEnd) | |
57 return staticEOFToken; | |
58 return result; | |
59 } | |
60 | |
61 bool atEnd() { return peek().type() == EOFToken; } | |
62 | |
63 // Consumes component values until the next token is a listed type or EOF. | |
64 template <CSSParserTokenType... types> | |
65 void consumeUntilAtEndOrPeekedTypeIs(); | |
66 | |
67 // Only for single-length component values. | |
68 void consume() { | |
69 DCHECK(hasLookedAhead()); | |
70 DCHECK(peekInternal().getBlockType() == CSSParserToken::NotBlock); | |
71 DCHECK(!atEnd()); | |
72 m_currentIndex++; | |
73 } | |
74 | |
75 // These do not create tokens | |
76 void skipWhitespaceAndComments() { | |
77 DCHECK_EQ(m_currentIndex, m_tokenizer.m_tokens.size()); | |
78 m_tokenizer.skipWhitespaceAndComments(); | |
79 } | |
80 void skipBlock() { | |
81 DCHECK(hasLookedAhead()); | |
82 DCHECK(peek().type() == LeftBraceToken); | |
83 m_tokenizer.skipToBlockEnd(); | |
84 } | |
85 | |
86 // This is the current character index in the original string. | |
87 // This can only be called if we have not looked ahead. After calling peek(), | |
88 // atEnd(), or consumeUntilAtEndOrPeekedTypeIs(), we can only call offset() | |
89 // if we're at the end of the stream. | |
90 // After calling consume() or making a substream we can call offset(). | |
91 size_t offset() { | |
92 if (hasLookedAhead()) { | |
93 DCHECK(atEnd()); | |
94 return previousOffset(); | |
95 } | |
96 return m_tokenizer.m_input.offset(); | |
97 } | |
98 // This is the character index in the original string of where we have | |
99 // consumed up to, when we have looked ahead. This is only correct when | |
100 // the look-ahead token is a single character or EOF. | |
101 size_t previousOffset() { | |
102 if (m_tokenizer.m_finishedTokenizing) { | |
103 DCHECK_EQ(m_currentIndex, m_tokenizer.m_tokens.size()); | |
104 return m_tokenizer.m_input.offset(); | |
105 } | |
106 DCHECK_EQ(m_currentIndex, m_tokenizer.m_tokens.size() - 1); | |
107 return m_tokenizer.m_input.offset() - 1; | |
108 } | |
109 | |
110 size_t offsetAfterComments() { | |
111 if (!hasLookedAhead()) | |
112 m_tokenizer.skipComments(); | |
113 return offset(); | |
114 } | |
115 | |
116 // CSSParserTokenRange objects are created by calling index(), consuming from | |
117 // the stream, then calling makeSubRangeFrom. | |
118 size_t index() { return m_currentIndex; } | |
119 CSSParserTokenRange makeSubRangeFrom(size_t startIndex) const { | |
120 return CSSParserTokenRange(m_tokenizer.m_tokens) | |
121 .makeSubRange(m_tokenizer.m_tokens.begin() + startIndex, | |
122 m_tokenizer.m_tokens.begin() + m_currentIndex); | |
123 } | |
124 | |
125 void yieldComments(CSSParserObserver& observer) { | |
126 if (hasLookedAhead()) | |
127 DCHECK(atEnd()); | |
128 else | |
129 m_tokenizer.yieldComments(observer); | |
130 } | |
131 | |
132 private: | |
133 bool hasLookedAhead() { | |
Charlie Harrison
2017/01/09 21:35:07
Maybe replace this method with:
DCHECK(m_currentI
| |
134 if (m_currentIndex == m_tokenizer.m_tokens.size()) | |
135 return false; | |
136 DCHECK_EQ(m_currentIndex, m_tokenizer.m_tokens.size() - 1); | |
137 return true; | |
138 } | |
139 | |
140 const CSSParserToken& peekInternal(); | |
141 | |
142 CSSTokenizer& m_tokenizer; | |
143 size_t m_startIndex; | |
144 size_t m_currentIndex; | |
145 bool m_isSubStream; | |
146 }; | |
147 | |
148 template <typename... emptyBaseCase> | |
149 inline bool typeIs(CSSParserTokenType type) { | |
150 return false; | |
151 } | |
152 | |
153 template <CSSParserTokenType head, CSSParserTokenType... tail> | |
154 inline bool typeIs(CSSParserTokenType type) { | |
155 return type == head || typeIs<tail...>(type); | |
156 } | |
157 | |
158 template <CSSParserTokenType... types> | |
159 void CSSParserTokenStream::consumeUntilAtEndOrPeekedTypeIs() { | |
160 unsigned nestingLevel = 0; | |
161 const CSSParserToken* token = &peekInternal(); | |
162 if (token->type() == EOFToken) | |
163 return; | |
164 | |
165 while (nestingLevel || !typeIs<types...>(token->type())) { | |
166 if (token->getBlockType() == CSSParserToken::BlockStart) { | |
167 nestingLevel++; | |
168 } else if (token->getBlockType() == CSSParserToken::BlockEnd) { | |
169 if (nestingLevel == 0) | |
170 return; | |
171 nestingLevel--; | |
172 } | |
173 | |
174 m_currentIndex++; | |
175 m_tokenizer.tokenizeSingle(); | |
176 if (m_tokenizer.m_finishedTokenizing) | |
177 return; | |
178 token = &m_tokenizer.m_tokens[m_currentIndex]; | |
179 } | |
180 } | |
181 | |
182 } // namespace blink | |
183 | |
184 #endif // CSSParserTokenStream_h | |
OLD | NEW |