1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:27:36 +00:00

LIbWeb: Spec-comment consume_a_list_of_declarations()

This commit is contained in:
Sam Atkins 2022-03-30 14:08:27 +01:00 committed by Andreas Kling
parent e72f42bea1
commit be86d19529

View file

@ -1869,60 +1869,85 @@ Optional<StyleDeclarationRule> Parser::consume_a_declaration(TokenStream<T>& tok
return declaration; return declaration;
} }
// 5.4.5. Consume a list of declarations
// https://www.w3.org/TR/css-syntax-3/#consume-list-of-declarations
template<typename T> template<typename T>
Vector<DeclarationOrAtRule> Parser::consume_a_list_of_declarations(TokenStream<T>& tokens) Vector<DeclarationOrAtRule> Parser::consume_a_list_of_declarations(TokenStream<T>& tokens)
{ {
Vector<DeclarationOrAtRule> list; // To consume a list of declarations:
// Create an initially empty list of declarations.
Vector<DeclarationOrAtRule> list_of_declarations;
// Repeatedly consume the next input token:
for (;;) { for (;;) {
auto& token = tokens.next_token(); auto& token = tokens.next_token();
// <whitespace-token>
// <semicolon-token>
if (token.is(Token::Type::Whitespace) || token.is(Token::Type::Semicolon)) { if (token.is(Token::Type::Whitespace) || token.is(Token::Type::Semicolon)) {
// Do nothing.
continue; continue;
} }
// <EOF-token>
if (token.is(Token::Type::EndOfFile)) { if (token.is(Token::Type::EndOfFile)) {
return list; // Return the list of declarations.
return list_of_declarations;
} }
// <at-keyword-token>
if (token.is(Token::Type::AtKeyword)) { if (token.is(Token::Type::AtKeyword)) {
// Reconsume the current input token.
tokens.reconsume_current_input_token(); tokens.reconsume_current_input_token();
list.append(DeclarationOrAtRule(consume_an_at_rule(tokens)));
// Consume an at-rule. Append the returned rule to the list of declarations.
list_of_declarations.append(DeclarationOrAtRule(consume_an_at_rule(tokens)));
continue; continue;
} }
// <ident-token>
if (token.is(Token::Type::Ident)) { if (token.is(Token::Type::Ident)) {
Vector<StyleComponentValueRule> temp; // Initialize a temporary list initially filled with the current input token.
temp.append(token); Vector<StyleComponentValueRule> temporary_list;
temporary_list.append(token);
// As long as the next input token is anything other than a <semicolon-token> or <EOF-token>,
// consume a component value and append it to the temporary list.
for (;;) { for (;;) {
auto& peek = tokens.peek_token(); auto& peek = tokens.peek_token();
if (peek.is(Token::Type::Semicolon) || peek.is(Token::Type::EndOfFile)) { if (peek.is(Token::Type::Semicolon) || peek.is(Token::Type::EndOfFile))
break; break;
} temporary_list.append(consume_a_component_value(tokens));
temp.append(consume_a_component_value(tokens));
} }
auto token_stream = TokenStream(temp); // Consume a declaration from the temporary list. If anything was returned, append it to the list of declarations.
auto maybe_declaration = consume_a_declaration(token_stream); auto token_stream = TokenStream(temporary_list);
if (maybe_declaration.has_value()) { if (auto maybe_declaration = consume_a_declaration(token_stream); maybe_declaration.has_value())
list.append(DeclarationOrAtRule(maybe_declaration.value())); list_of_declarations.append(DeclarationOrAtRule(maybe_declaration.value()));
}
continue; continue;
} }
log_parse_error(); // anything else
tokens.reconsume_current_input_token(); {
// This is a parse error.
log_parse_error();
for (;;) { // Reconsume the current input token.
auto& peek = tokens.peek_token(); tokens.reconsume_current_input_token();
if (peek.is(Token::Type::Semicolon) || peek.is(Token::Type::EndOfFile))
break; // As long as the next input token is anything other than a <semicolon-token> or <EOF-token>,
dbgln_if(CSS_PARSER_DEBUG, "Discarding token: '{}'", peek.to_debug_string()); // consume a component value and throw away the returned value.
(void)consume_a_component_value(tokens); for (;;) {
auto& peek = tokens.peek_token();
if (peek.is(Token::Type::Semicolon) || peek.is(Token::Type::EndOfFile))
break;
dbgln_if(CSS_PARSER_DEBUG, "Discarding token: '{}'", peek.to_debug_string());
(void)consume_a_component_value(tokens);
}
} }
} }
return list;
} }
RefPtr<CSSRule> Parser::parse_as_css_rule() RefPtr<CSSRule> Parser::parse_as_css_rule()