Skip to content

Commit

Permalink
Revert "Add a raw mode for CSSParserTokenStream."
Browse files Browse the repository at this point in the history
We discovered that we do not actually need to preserve trailing
comments, so we're only left with the block behavior, which we'll
solve in a different way.

This reverts commit 8b35a97.

Change-Id: Ic0d30ae980ae0ff93571989dc1e08b84a1306f0a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4324039
Commit-Queue: Anders Hartvoll Ruud <andruud@chromium.org>
Auto-Submit: Steinar H Gunderson <sesse@chromium.org>
Reviewed-by: Anders Hartvoll Ruud <andruud@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1115640}
  • Loading branch information
Steinar H. Gunderson authored and Chromium LUCI CQ committed Mar 10, 2023
1 parent 27baf7a commit 7cee5db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,24 @@

namespace blink {

template <bool Raw>
StringView CSSParserTokenStreamImpl<Raw>::StringRangeAt(
wtf_size_t start,
wtf_size_t length) const {
StringView CSSParserTokenStream::StringRangeAt(wtf_size_t start,
wtf_size_t length) const {
return tokenizer_.StringRangeAt(start, length);
}

template <bool Raw>
void CSSParserTokenStreamImpl<Raw>::ConsumeWhitespace() {
void CSSParserTokenStream::ConsumeWhitespace() {
while (Peek().GetType() == kWhitespaceToken) {
UncheckedConsume();
}
}

template <bool Raw>
CSSParserToken CSSParserTokenStreamImpl<Raw>::ConsumeIncludingWhitespace() {
CSSParserToken CSSParserTokenStream::ConsumeIncludingWhitespace() {
CSSParserToken result = Consume();
ConsumeWhitespace();
return result;
}

template <bool Raw>
bool CSSParserTokenStreamImpl<Raw>::ConsumeCommentOrNothing() {
bool CSSParserTokenStream::ConsumeCommentOrNothing() {
DCHECK(!HasLookAhead());
const auto token = tokenizer_.TokenizeSingleWithComments();
if (token.GetType() != kCommentToken) {
Expand All @@ -42,8 +37,7 @@ bool CSSParserTokenStreamImpl<Raw>::ConsumeCommentOrNothing() {
return true;
}

template <bool Raw>
void CSSParserTokenStreamImpl<Raw>::UncheckedConsumeComponentValue() {
void CSSParserTokenStream::UncheckedConsumeComponentValue() {
DCHECK(HasLookAhead());

// Have to use internal consume/peek in here because they can read past
Expand All @@ -59,8 +53,7 @@ void CSSParserTokenStreamImpl<Raw>::UncheckedConsumeComponentValue() {
} while (!PeekInternal().IsEOF() && nesting_level);
}

template <bool Raw>
void CSSParserTokenStreamImpl<Raw>::UncheckedSkipToEndOfBlock() {
void CSSParserTokenStream::UncheckedSkipToEndOfBlock() {
DCHECK(HasLookAhead());

// Process and consume the lookahead token.
Expand All @@ -74,7 +67,7 @@ void CSSParserTokenStreamImpl<Raw>::UncheckedSkipToEndOfBlock() {

// Skip tokens until we see EOF or the closing brace.
while (nesting_level != 0) {
CSSParserToken token = TokenizeSingle();
CSSParserToken token = tokenizer_.TokenizeSingle();
if (token.IsEOF()) {
break;
} else if (token.GetBlockType() == CSSParserToken::kBlockStart) {
Expand All @@ -86,7 +79,4 @@ void CSSParserTokenStreamImpl<Raw>::UncheckedSkipToEndOfBlock() {
offset_ = tokenizer_.Offset();
}

template class CORE_EXPORT CSSParserTokenStreamImpl<false>;
template class CORE_EXPORT CSSParserTokenStreamImpl<true>;

} // namespace blink
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@ bool IsTokenTypeOneOf(CSSParserTokenType t) {
// Methods prefixed with "Unchecked" can only be called after calls to Peek(),
// EnsureLookAhead(), or AtEnd() with no subsequent modifications to the stream
// such as a consume.
//
// Use the aliases CSSParserTokenStream or CSSParserRawTokenStream instead of
// calling the Impl directly. CSSParserRawTokenStream includes comments and
// allows you to read past the end of blocks without recursing, which is
// normally not what you want to do when parsing (it is really only useful
// during variable substitution).
template <bool Raw>
class CORE_EXPORT CSSParserTokenStreamImpl {
class CORE_EXPORT CSSParserTokenStream {
DISALLOW_NEW();

public:
Expand All @@ -53,7 +46,7 @@ class CORE_EXPORT CSSParserTokenStreamImpl {
STACK_ALLOCATED();

public:
explicit BlockGuard(CSSParserTokenStreamImpl& stream) : stream_(stream) {
explicit BlockGuard(CSSParserTokenStream& stream) : stream_(stream) {
const CSSParserToken next = stream.ConsumeInternal();
DCHECK_EQ(next.GetBlockType(), CSSParserToken::kBlockStart);
}
Expand All @@ -72,7 +65,7 @@ class CORE_EXPORT CSSParserTokenStreamImpl {
}

private:
CSSParserTokenStreamImpl& stream_;
CSSParserTokenStream& stream_;
bool skipped_to_end_of_block_ = false;
};

Expand All @@ -86,7 +79,7 @@ class CORE_EXPORT CSSParserTokenStreamImpl {
STACK_ALLOCATED();

public:
Boundary(CSSParserTokenStreamImpl& stream, CSSParserTokenType boundary_type)
Boundary(CSSParserTokenStream& stream, CSSParserTokenType boundary_type)
: auto_reset_(&stream.boundaries_,
stream.boundaries_ | FlagForTokenType(boundary_type)) {}
~Boundary() = default;
Expand All @@ -103,24 +96,24 @@ class CORE_EXPORT CSSParserTokenStreamImpl {
// only needed for declarations which are easier to think about?
static constexpr int kInitialBufferSize = 128;

explicit CSSParserTokenStreamImpl(CSSTokenizer& tokenizer)
explicit CSSParserTokenStream(CSSTokenizer& tokenizer)
: tokenizer_(tokenizer), next_(kEOFToken) {}

CSSParserTokenStreamImpl(CSSParserTokenStreamImpl&&) = default;
CSSParserTokenStreamImpl(const CSSParserTokenStreamImpl&) = delete;
CSSParserTokenStreamImpl& operator=(const CSSParserTokenStreamImpl&) = delete;
CSSParserTokenStream(CSSParserTokenStream&&) = default;
CSSParserTokenStream(const CSSParserTokenStream&) = delete;
CSSParserTokenStream& operator=(const CSSParserTokenStream&) = delete;

inline void EnsureLookAhead() {
if (!HasLookAhead()) {
has_look_ahead_ = true;
next_ = TokenizeSingle();
next_ = tokenizer_.TokenizeSingle();
}
}

// Forcibly read a lookahead token.
inline void LookAhead() {
DCHECK(!HasLookAhead());
next_ = TokenizeSingle();
next_ = tokenizer_.TokenizeSingle();
has_look_ahead_ = true;
}

Expand All @@ -143,10 +136,8 @@ class CORE_EXPORT CSSParserTokenStreamImpl {

const CSSParserToken& UncheckedConsume() {
DCHECK(HasLookAhead());
if constexpr (!Raw) {
DCHECK_NE(next_.GetBlockType(), CSSParserToken::kBlockStart);
DCHECK_NE(next_.GetBlockType(), CSSParserToken::kBlockEnd);
}
DCHECK_NE(next_.GetBlockType(), CSSParserToken::kBlockStart);
DCHECK_NE(next_.GetBlockType(), CSSParserToken::kBlockEnd);
has_look_ahead_ = false;
offset_ = tokenizer_.Offset();
return next_;
Expand Down Expand Up @@ -223,7 +214,7 @@ class CORE_EXPORT CSSParserTokenStreamImpl {
// Add tokens to our return vector until we see either EOF or we meet the
// return condition. (The termination condition is within the loop.)
while (true) {
buffer_.push_back(TokenizeSingle());
buffer_.push_back(tokenizer_.TokenizeSingle());
if (buffer_.back().IsEOF() ||
(nesting_level == 0 && TokenMarksEnd<Types...>(buffer_.back()))) {
// Undo the token we just pushed; it goes into the lookahead slot
Expand Down Expand Up @@ -275,14 +266,6 @@ class CORE_EXPORT CSSParserTokenStreamImpl {
// leave a lookahead token active (for unknown reasons).
void UncheckedSkipToEndOfBlock();

ALWAYS_INLINE CSSParserToken TokenizeSingle() {
if constexpr (Raw) {
return tokenizer_.TokenizeSingleWithComments();
} else {
return tokenizer_.TokenizeSingle();
}
}

Vector<CSSParserToken, kInitialBufferSize> buffer_;
CSSTokenizer& tokenizer_;
CSSParserToken next_;
Expand All @@ -291,18 +274,6 @@ class CORE_EXPORT CSSParserTokenStreamImpl {
uint64_t boundaries_ = FlagForTokenType(kEOFToken);
};

// This allows us to still forward-declare CSSParserTokenStream.
class CORE_EXPORT CSSParserTokenStream
: public CSSParserTokenStreamImpl</*Raw=*/false> {
// Forward the constructors.
using CSSParserTokenStreamImpl<false>::CSSParserTokenStreamImpl;
};
class CORE_EXPORT CSSParserRawTokenStream
: public CSSParserTokenStreamImpl</*Raw=*/true> {
// Forward the constructors.
using CSSParserTokenStreamImpl<true>::CSSParserTokenStreamImpl;
};

} // namespace blink

#endif // THIRD_PARTY_BLINK_RENDERER_CORE_CSS_PARSER_CSS_PARSER_TOKEN_STREAM_H_

0 comments on commit 7cee5db

Please sign in to comment.