diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2021-10-21 21:25:14 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-10-23 19:07:44 +0200 |
commit | ecf53685352e2dfc534a96be07b1c3727319389f (patch) | |
tree | c349bf491a5ae3db13dc81e8bdfa5d45d76a4b66 /Userland/Libraries/LibWeb | |
parent | 9a2eecaca4829aa623d33931a47872888541608e (diff) | |
download | serenity-ecf53685352e2dfc534a96be07b1c3727319389f.zip |
LibWeb: Record position information in CSS Tokens
This is a requirement to be able to use the Tokens for syntax
highlighting.
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Token.h | 12 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp | 18 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.h | 2 |
3 files changed, 30 insertions, 2 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Token.h b/Userland/Libraries/LibWeb/CSS/Parser/Token.h index 9c1d945b68..3c906aa166 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Token.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Token.h @@ -56,6 +56,12 @@ public: Number, }; + struct Position { + size_t line { 0 }; + size_t column { 0 }; + }; + + Type type() const { return m_type; } bool is(Type type) const { return m_type == type; } StringView ident() const @@ -136,6 +142,9 @@ public: String to_debug_string() const; + Position const& start_position() const { return m_start_position; } + Position const& end_position() const { return m_end_position; } + private: Type m_type { Type::Invalid }; @@ -143,6 +152,9 @@ private: StringBuilder m_unit; HashType m_hash_type { HashType::Unrestricted }; NumberType m_number_type { NumberType::Integer }; + + Position m_start_position; + Position m_end_position; }; } diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp index ad6a52bc29..9c4602606a 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp @@ -241,7 +241,10 @@ Vector<Token> Tokenizer::parse() { Vector<Token> tokens; for (;;) { + auto token_start = m_position; auto token = consume_a_token(); + token.m_start_position = token_start; + token.m_end_position = m_position; tokens.append(token); if (token.is(Token::Type::EndOfFile)) { @@ -256,8 +259,18 @@ u32 Tokenizer::next_code_point() return TOKENIZER_EOF; m_prev_utf8_iterator = m_utf8_iterator; ++m_utf8_iterator; - dbgln_if(CSS_TOKENIZER_DEBUG, "(Tokenizer) Next code_point: {:d}", *m_prev_utf8_iterator); - return *m_prev_utf8_iterator; + auto code_point = *m_prev_utf8_iterator; + + m_prev_position = m_position; + if (is_newline(code_point)) { + m_position.line++; + m_position.column = 0; + } else { + m_position.column++; + } + + dbgln_if(CSS_TOKENIZER_DEBUG, "(Tokenizer) Next code_point: {:d}", code_point); + return code_point; } u32 Tokenizer::peek_code_point(size_t offset) const @@ -579,6 +592,7 @@ void Tokenizer::consume_as_much_whitespace_as_possible() void Tokenizer::reconsume_current_input_code_point() { m_utf8_iterator = m_prev_utf8_iterator; + m_position = m_prev_position; } // https://www.w3.org/TR/css-syntax-3/#consume-numeric-token diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.h b/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.h index a5198bbd4f..d557bee3db 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.h @@ -103,5 +103,7 @@ private: Utf8View m_utf8_view; AK::Utf8CodePointIterator m_utf8_iterator; AK::Utf8CodePointIterator m_prev_utf8_iterator; + Token::Position m_position; + Token::Position m_prev_position; }; } |