diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2021-09-30 17:17:21 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-10-01 20:03:03 +0200 |
commit | 2ed0f880eee6f28958907b5da26590c106bb5fbb (patch) | |
tree | 5f0578eb9e95bf6a87eaf2e58d94827433efc577 /Userland | |
parent | c7cd4896890a92d4b4746abb9fb1660d0950b915 (diff) | |
download | serenity-2ed0f880eee6f28958907b5da26590c106bb5fbb.zip |
LibWeb: Add TokenStream::rewind_to_position()
Parsing media queries sometimes requires significant back-tracking, so
`reconsume_current_input_token()` was not good enough.
`rewind_to_position()` lets you reconsume an erbitrary number of tokens
to return to an earlier point in the stream, which you previously saved
from `TokenStream::position()`.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 7 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 3 |
2 files changed, 10 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 39be9f7d04..5599f2d5bd 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -107,6 +107,13 @@ void TokenStream<T>::reconsume_current_input_token() } template<typename T> +void TokenStream<T>::rewind_to_position(int position) +{ + VERIFY(position <= m_iterator_offset); + m_iterator_offset = position; +} + +template<typename T> void TokenStream<T>::skip_whitespace() { while (peek_token().is(Token::Type::Whitespace)) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index fabe517920..0eace6cfc2 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -61,6 +61,9 @@ public: T const& current_token(); void reconsume_current_input_token(); + int position() const { return m_iterator_offset; } + void rewind_to_position(int); + void skip_whitespace(); void dump_all_tokens(); |