1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 14:38:12 +00:00

LibWeb: Propagate errors from CSS Tokenizer

This commit is contained in:
Sam Atkins 2023-03-06 14:30:10 +00:00 committed by Andreas Kling
parent 17618989a3
commit 84af8dd9ed
2 changed files with 22 additions and 22 deletions

View file

@ -252,15 +252,15 @@ Tokenizer::Tokenizer(String decoded_input)
{ {
} }
Vector<Token> Tokenizer::tokenize() ErrorOr<Vector<Token>> Tokenizer::tokenize()
{ {
Vector<Token> tokens; Vector<Token> tokens;
for (;;) { for (;;) {
auto token_start = m_position; auto token_start = m_position;
auto token = consume_a_token(); auto token = TRY(consume_a_token());
token.m_start_position = token_start; token.m_start_position = token_start;
token.m_end_position = m_position; token.m_end_position = m_position;
tokens.append(token); TRY(tokens.try_append(token));
if (token.is(Token::Type::EndOfFile)) { if (token.is(Token::Type::EndOfFile)) {
return tokens; return tokens;
@ -428,13 +428,13 @@ u32 Tokenizer::consume_escaped_code_point()
} }
// https://www.w3.org/TR/css-syntax-3/#consume-ident-like-token // https://www.w3.org/TR/css-syntax-3/#consume-ident-like-token
Token Tokenizer::consume_an_ident_like_token() ErrorOr<Token> Tokenizer::consume_an_ident_like_token()
{ {
// This section describes how to consume an ident-like token from a stream of code points. // This section describes how to consume an ident-like token from a stream of code points.
// It returns an <ident-token>, <function-token>, <url-token>, or <bad-url-token>. // It returns an <ident-token>, <function-token>, <url-token>, or <bad-url-token>.
// Consume an ident sequence, and let string be the result. // Consume an ident sequence, and let string be the result.
auto string = consume_an_ident_sequence().release_value_but_fixme_should_propagate_errors(); auto string = TRY(consume_an_ident_sequence());
// If strings value is an ASCII case-insensitive match for "url", and the next input code // If strings value is an ASCII case-insensitive match for "url", and the next input code
// point is U+0028 LEFT PARENTHESIS ((), consume it. // point is U+0028 LEFT PARENTHESIS ((), consume it.
@ -625,7 +625,7 @@ ErrorOr<FlyString> Tokenizer::consume_an_ident_sequence()
} }
// https://www.w3.org/TR/css-syntax-3/#consume-url-token // https://www.w3.org/TR/css-syntax-3/#consume-url-token
Token Tokenizer::consume_a_url_token() ErrorOr<Token> Tokenizer::consume_a_url_token()
{ {
// This section describes how to consume a url token from a stream of code points. // This section describes how to consume a url token from a stream of code points.
// It returns either a <url-token> or a <bad-url-token>. // It returns either a <url-token> or a <bad-url-token>.
@ -643,8 +643,8 @@ Token Tokenizer::consume_a_url_token()
// 2. Consume as much whitespace as possible. // 2. Consume as much whitespace as possible.
consume_as_much_whitespace_as_possible(); consume_as_much_whitespace_as_possible();
auto make_token = [&]() { auto make_token = [&]() -> ErrorOr<Token> {
token.m_value = FlyString::from_utf8(builder.string_view()).release_value_but_fixme_should_propagate_errors(); token.m_value = TRY(FlyString::from_utf8(builder.string_view()));
return token; return token;
}; };
@ -769,7 +769,7 @@ void Tokenizer::reconsume_current_input_code_point()
} }
// https://www.w3.org/TR/css-syntax-3/#consume-numeric-token // https://www.w3.org/TR/css-syntax-3/#consume-numeric-token
Token Tokenizer::consume_a_numeric_token() ErrorOr<Token> Tokenizer::consume_a_numeric_token()
{ {
// This section describes how to consume a numeric token from a stream of code points. // This section describes how to consume a numeric token from a stream of code points.
// It returns either a <number-token>, <percentage-token>, or <dimension-token>. // It returns either a <number-token>, <percentage-token>, or <dimension-token>.
@ -785,7 +785,7 @@ Token Tokenizer::consume_a_numeric_token()
token.m_number_value = number; token.m_number_value = number;
// 2. Consume an ident sequence. Set the <dimension-token>s unit to the returned value. // 2. Consume an ident sequence. Set the <dimension-token>s unit to the returned value.
auto unit = consume_an_ident_sequence().release_value_but_fixme_should_propagate_errors(); auto unit = TRY(consume_an_ident_sequence());
VERIFY(!unit.is_empty()); VERIFY(!unit.is_empty());
// NOTE: We intentionally store this in the `value`, to save space. // NOTE: We intentionally store this in the `value`, to save space.
token.m_value = move(unit); token.m_value = move(unit);
@ -921,7 +921,7 @@ bool Tokenizer::would_start_an_ident_sequence(U32Triplet values)
} }
// https://www.w3.org/TR/css-syntax-3/#consume-string-token // https://www.w3.org/TR/css-syntax-3/#consume-string-token
Token Tokenizer::consume_string_token(u32 ending_code_point) ErrorOr<Token> Tokenizer::consume_string_token(u32 ending_code_point)
{ {
// This section describes how to consume a string token from a stream of code points. // This section describes how to consume a string token from a stream of code points.
// It returns either a <string-token> or <bad-string-token>. // It returns either a <string-token> or <bad-string-token>.
@ -934,8 +934,8 @@ Token Tokenizer::consume_string_token(u32 ending_code_point)
auto token = create_new_token(Token::Type::String); auto token = create_new_token(Token::Type::String);
StringBuilder builder; StringBuilder builder;
auto make_token = [&]() { auto make_token = [&]() -> ErrorOr<Token> {
token.m_value = FlyString::from_utf8(builder.string_view()).release_value_but_fixme_should_propagate_errors(); token.m_value = TRY(FlyString::from_utf8(builder.string_view()));
return token; return token;
}; };
@ -1027,7 +1027,7 @@ start:
} }
// https://www.w3.org/TR/css-syntax-3/#consume-token // https://www.w3.org/TR/css-syntax-3/#consume-token
Token Tokenizer::consume_a_token() ErrorOr<Token> Tokenizer::consume_a_token()
{ {
// This section describes how to consume a token from a stream of code points. // This section describes how to consume a token from a stream of code points.
// It will return a single token of any type. // It will return a single token of any type.
@ -1072,7 +1072,7 @@ Token Tokenizer::consume_a_token()
token.m_hash_type = Token::HashType::Id; token.m_hash_type = Token::HashType::Id;
// 3. Consume an ident sequence, and set the <hash-token>s value to the returned string. // 3. Consume an ident sequence, and set the <hash-token>s value to the returned string.
auto name = consume_an_ident_sequence().release_value_but_fixme_should_propagate_errors(); auto name = TRY(consume_an_ident_sequence());
token.m_value = move(name); token.m_value = move(name);
// 4. Return the <hash-token>. // 4. Return the <hash-token>.
@ -1208,7 +1208,7 @@ Token Tokenizer::consume_a_token()
// If the next 3 input code points would start an ident sequence, consume an ident sequence, create // If the next 3 input code points would start an ident sequence, consume an ident sequence, create
// an <at-keyword-token> with its value set to the returned value, and return it. // an <at-keyword-token> with its value set to the returned value, and return it.
if (would_start_an_ident_sequence(peek_triplet())) { if (would_start_an_ident_sequence(peek_triplet())) {
auto name = consume_an_ident_sequence().release_value_but_fixme_should_propagate_errors(); auto name = TRY(consume_an_ident_sequence());
return create_value_token(Token::Type::AtKeyword, move(name)); return create_value_token(Token::Type::AtKeyword, move(name));
} }

View file

@ -66,7 +66,7 @@ public:
private: private:
explicit Tokenizer(String decoded_input); explicit Tokenizer(String decoded_input);
[[nodiscard]] Vector<Token> tokenize(); [[nodiscard]] ErrorOr<Vector<Token>> tokenize();
[[nodiscard]] u32 next_code_point(); [[nodiscard]] u32 next_code_point();
[[nodiscard]] u32 peek_code_point(size_t offset = 0) const; [[nodiscard]] u32 peek_code_point(size_t offset = 0) const;
@ -79,15 +79,15 @@ private:
[[nodiscard]] static Token create_new_token(Token::Type); [[nodiscard]] static Token create_new_token(Token::Type);
[[nodiscard]] static Token create_value_token(Token::Type, FlyString&& value); [[nodiscard]] static Token create_value_token(Token::Type, FlyString&& value);
[[nodiscard]] static Token create_value_token(Token::Type, u32 value); [[nodiscard]] static Token create_value_token(Token::Type, u32 value);
[[nodiscard]] Token consume_a_token(); [[nodiscard]] ErrorOr<Token> consume_a_token();
[[nodiscard]] Token consume_string_token(u32 ending_code_point); [[nodiscard]] ErrorOr<Token> consume_string_token(u32 ending_code_point);
[[nodiscard]] Token consume_a_numeric_token(); [[nodiscard]] ErrorOr<Token> consume_a_numeric_token();
[[nodiscard]] Token consume_an_ident_like_token(); [[nodiscard]] ErrorOr<Token> consume_an_ident_like_token();
[[nodiscard]] Number consume_a_number(); [[nodiscard]] Number consume_a_number();
[[nodiscard]] float convert_a_string_to_a_number(StringView); [[nodiscard]] float convert_a_string_to_a_number(StringView);
[[nodiscard]] ErrorOr<FlyString> consume_an_ident_sequence(); [[nodiscard]] ErrorOr<FlyString> consume_an_ident_sequence();
[[nodiscard]] u32 consume_escaped_code_point(); [[nodiscard]] u32 consume_escaped_code_point();
[[nodiscard]] Token consume_a_url_token(); [[nodiscard]] ErrorOr<Token> consume_a_url_token();
void consume_the_remnants_of_a_bad_url(); void consume_the_remnants_of_a_bad_url();
void consume_comments(); void consume_comments();
void consume_as_much_whitespace_as_possible(); void consume_as_much_whitespace_as_possible();