Skip to content

Commit

Permalink
(ast/stylesheet) fixup error recovery on unknown rules
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Nov 10, 2024
1 parent fa91e15 commit 10841dd
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions crates/hdx_ast/src/css/stylesheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,33 @@ apply_rules!(rule);

impl<'a> Parse<'a> for Rule<'a> {
fn parse(parser: &mut Parser<'a>) -> ParserResult<Self> {
let checkpoint = parser.checkpoint();
if let Some(token) = parser.peek::<Token![AtKeyword]>() {
macro_rules! parse_rule {
( $(
$name: ident$(<$a: lifetime>)?: $atom: pat,
)+ ) => {
match parser.parse_atom_lower(token) {
$($atom => rules::$name::try_parse(parser).map(Self::$name),)+
$($atom => rules::$name::parse(parser).map(Self::$name),)+
_ => {
let rule = AtRule::parse_spanned(parser)?;
Ok(Self::UnknownAt(rule.node))
}
}
}
}
return apply_rules!(parse_rule).or_else(|_| AtRule::parse(parser).map(Self::UnknownAt));
if let Ok(rule) = apply_rules!(parse_rule) {
Ok(rule)
} else {
parser.rewind(checkpoint);
AtRule::parse(parser).map(Self::UnknownAt)
}
} else if let Ok(rule) = StyleRule::parse(parser) {
Ok(Self::Style(rule))
} else {
parser.rewind(checkpoint);
QualifiedRule::parse(parser).map(Self::Unknown)
}
// "Consume a qualified rule from input. If anything is returned, append it to rules."
StyleRule::try_parse(parser).map(Self::Style).or_else(|_| QualifiedRule::parse(parser).map(Self::Unknown))
}
}

Expand Down

0 comments on commit 10841dd

Please sign in to comment.