mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 00:47:34 +00:00
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:
parent
9a2eecaca4
commit
ecf5368535
3 changed files with 30 additions and 2 deletions
|
@ -56,6 +56,12 @@ public:
|
||||||
Number,
|
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; }
|
bool is(Type type) const { return m_type == type; }
|
||||||
|
|
||||||
StringView ident() const
|
StringView ident() const
|
||||||
|
@ -136,6 +142,9 @@ public:
|
||||||
|
|
||||||
String to_debug_string() const;
|
String to_debug_string() const;
|
||||||
|
|
||||||
|
Position const& start_position() const { return m_start_position; }
|
||||||
|
Position const& end_position() const { return m_end_position; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Type m_type { Type::Invalid };
|
Type m_type { Type::Invalid };
|
||||||
|
|
||||||
|
@ -143,6 +152,9 @@ private:
|
||||||
StringBuilder m_unit;
|
StringBuilder m_unit;
|
||||||
HashType m_hash_type { HashType::Unrestricted };
|
HashType m_hash_type { HashType::Unrestricted };
|
||||||
NumberType m_number_type { NumberType::Integer };
|
NumberType m_number_type { NumberType::Integer };
|
||||||
|
|
||||||
|
Position m_start_position;
|
||||||
|
Position m_end_position;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -241,7 +241,10 @@ Vector<Token> Tokenizer::parse()
|
||||||
{
|
{
|
||||||
Vector<Token> tokens;
|
Vector<Token> tokens;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
auto token_start = m_position;
|
||||||
auto token = consume_a_token();
|
auto token = consume_a_token();
|
||||||
|
token.m_start_position = token_start;
|
||||||
|
token.m_end_position = m_position;
|
||||||
tokens.append(token);
|
tokens.append(token);
|
||||||
|
|
||||||
if (token.is(Token::Type::EndOfFile)) {
|
if (token.is(Token::Type::EndOfFile)) {
|
||||||
|
@ -256,8 +259,18 @@ u32 Tokenizer::next_code_point()
|
||||||
return TOKENIZER_EOF;
|
return TOKENIZER_EOF;
|
||||||
m_prev_utf8_iterator = m_utf8_iterator;
|
m_prev_utf8_iterator = m_utf8_iterator;
|
||||||
++m_utf8_iterator;
|
++m_utf8_iterator;
|
||||||
dbgln_if(CSS_TOKENIZER_DEBUG, "(Tokenizer) Next code_point: {:d}", *m_prev_utf8_iterator);
|
auto code_point = *m_prev_utf8_iterator;
|
||||||
return *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
|
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()
|
void Tokenizer::reconsume_current_input_code_point()
|
||||||
{
|
{
|
||||||
m_utf8_iterator = m_prev_utf8_iterator;
|
m_utf8_iterator = m_prev_utf8_iterator;
|
||||||
|
m_position = m_prev_position;
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://www.w3.org/TR/css-syntax-3/#consume-numeric-token
|
// https://www.w3.org/TR/css-syntax-3/#consume-numeric-token
|
||||||
|
|
|
@ -103,5 +103,7 @@ private:
|
||||||
Utf8View m_utf8_view;
|
Utf8View m_utf8_view;
|
||||||
AK::Utf8CodePointIterator m_utf8_iterator;
|
AK::Utf8CodePointIterator m_utf8_iterator;
|
||||||
AK::Utf8CodePointIterator m_prev_utf8_iterator;
|
AK::Utf8CodePointIterator m_prev_utf8_iterator;
|
||||||
|
Token::Position m_position;
|
||||||
|
Token::Position m_prev_position;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue