LibWeb: Record position information in CSS Tokens

This is a requirement to be able to use the Tokens for syntax
highlighting.
This commit is contained in:
Sam Atkins 2021-10-21 21:25:14 +01:00 committed by Andreas Kling
parent 9a2eecaca4
commit ecf5368535
Notes: sideshowbarker 2024-07-18 01:58:56 +09:00
3 changed files with 30 additions and 2 deletions

View file

@ -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;
};
}

View file

@ -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

View file

@ -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;
};
}