Skip to content

Commit

Permalink
move custom initial-value parsing to Property parser
Browse files Browse the repository at this point in the history
This is the better spot because now it is specific to the
`initial-value` of an `@property` with Universal syntax.

Used `is_exhausted` to know that we are "done" or not, it will skip
whitespace so it will be true in both the `initial-value:;` and
`initial-value: ;` case.

Then we still use `parser.next_including_whitespace()` because we are
still interested in the whitespace if there is some (in case of
`initial-value: ;`). If there is no whitespace, then it results in an
end of input error which we can convert to a `WhiteSpace("")` token.

We do capture the error to default to an empty whitespace token, but we
can capture _all_ errors safely because if _something_ was actually
wrong, the wrapping `parser.is_exhausted()` would not have been ok and
therefore false.
  • Loading branch information
RobinMalfait committed Jan 11, 2024
1 parent 397ee28 commit f61531f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
10 changes: 9 additions & 1 deletion src/rules/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ impl<'i> PropertyRule<'i> {
Some(val) => {
let mut input = ParserInput::new(val);
let mut parser = Parser::new(&mut input);
Some(syntax.parse_value(&mut parser)?)

if parser.is_exhausted() {
Some(ParsedComponent::Token(match parser.next_including_whitespace() {
Ok(t) => t.into(),
Err(_) => crate::properties::custom::Token::WhiteSpace("".into()),
}))
} else {
Some(syntax.parse_value(&mut parser)?)
}
}
},
_ => {
Expand Down
9 changes: 1 addition & 8 deletions src/values/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,7 @@ impl<'i> SyntaxString {
) -> Result<ParsedComponent<'i>, ParseError<'i, ParserError<'i>>> {
match self {
SyntaxString::Universal => Ok(ParsedComponent::Token(crate::properties::custom::Token::from(
match input.next_including_whitespace() {
Ok(token) => token,
Err(BasicParseError {
kind: BasicParseErrorKind::EndOfInput,
..
}) => &Token::WhiteSpace(""),
Err(e) => return Err(e.into()),
},
input.next()?,
))),
SyntaxString::Components(components) => {
// Loop through each component, and return the first one that parses successfully.
Expand Down

0 comments on commit f61531f

Please sign in to comment.