Skip to content

Commit

Permalink
style: Simplify some code now that lifetimes are non-lexical.
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio committed Nov 30, 2019
1 parent 1ef7c2f commit d9aa057
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 257 deletions.
35 changes: 17 additions & 18 deletions components/style/custom_properties.rs
Expand Up @@ -306,8 +306,7 @@ fn parse_declaration_value_block<'i, 't>(
) -> Result<(TokenSerializationType, TokenSerializationType), ParseError<'i>> {
let mut token_start = input.position();
let mut token = match input.next_including_whitespace_and_comments() {
// FIXME: remove clone() when borrows are non-lexical
Ok(token) => token.clone(),
Ok(token) => token,
Err(_) => {
return Ok((
TokenSerializationType::nothing(),
Expand Down Expand Up @@ -335,8 +334,9 @@ fn parse_declaration_value_block<'i, 't>(
}
};
}
let last_token_type = match token {
let last_token_type = match *token {
Token::Comment(_) => {
let serialization_type = token.serialization_type();
let token_slice = input.slice_from(token_start);
if !token_slice.ends_with("*/") {
missing_closing_characters.push_str(if token_slice.ends_with('*') {
Expand All @@ -345,14 +345,14 @@ fn parse_declaration_value_block<'i, 't>(
"*/"
})
}
token.serialization_type()
serialization_type
},
Token::BadUrl(u) => {
let e = StyleParseErrorKind::BadUrlInDeclarationValueBlock(u);
Token::BadUrl(ref u) => {
let e = StyleParseErrorKind::BadUrlInDeclarationValueBlock(u.clone());
return Err(input.new_custom_error(e));
},
Token::BadString(s) => {
let e = StyleParseErrorKind::BadStringInDeclarationValueBlock(s);
Token::BadString(ref s) => {
let e = StyleParseErrorKind::BadStringInDeclarationValueBlock(s.clone());
return Err(input.new_custom_error(e));
},
Token::CloseParenthesis => {
Expand Down Expand Up @@ -401,13 +401,14 @@ fn parse_declaration_value_block<'i, 't>(
Token::CloseSquareBracket.serialization_type()
},
Token::QuotedString(_) => {
let serialization_type = token.serialization_type();
let token_slice = input.slice_from(token_start);
let quote = &token_slice[..1];
debug_assert!(matches!(quote, "\"" | "'"));
if !(token_slice.ends_with(quote) && token_slice.len() > 1) {
missing_closing_characters.push_str(quote)
}
token.serialization_type()
serialization_type
},
Token::Ident(ref value) |
Token::AtKeyword(ref value) |
Expand All @@ -417,25 +418,26 @@ fn parse_declaration_value_block<'i, 't>(
Token::Dimension {
unit: ref value, ..
} => {
let serialization_type = token.serialization_type();
let is_unquoted_url = matches!(token, Token::UnquotedUrl(_));
if value.ends_with("�") && input.slice_from(token_start).ends_with("\\") {
// Unescaped backslash at EOF in these contexts is interpreted as U+FFFD
// Check the value in case the final backslash was itself escaped.
// Serialize as escaped U+FFFD, which is also interpreted as U+FFFD.
// (Unescaped U+FFFD would also work, but removing the backslash is annoying.)
missing_closing_characters.push_str("�")
}
if matches!(token, Token::UnquotedUrl(_)) {
if is_unquoted_url {
check_closed!(")");
}
token.serialization_type()
serialization_type
},
_ => token.serialization_type(),
};

token_start = input.position();
token = match input.next_including_whitespace_and_comments() {
// FIXME: remove clone() when borrows are non-lexical
Ok(token) => token.clone(),
Ok(token) => token,
Err(..) => return Ok((first_token_type, last_token_type)),
};
}
Expand Down Expand Up @@ -888,15 +890,12 @@ fn substitute_block<'i>(
let mut set_position_at_next_iteration = false;
loop {
let before_this_token = input.position();
// FIXME: remove clone() when borrows are non-lexical
let next = input
.next_including_whitespace_and_comments()
.map(|t| t.clone());
let next = input.next_including_whitespace_and_comments();
if set_position_at_next_iteration {
*position = (
before_this_token,
match next {
Ok(ref token) => token.serialization_type(),
Ok(token) => token.serialization_type(),
Err(_) => TokenSerializationType::nothing(),
},
);
Expand Down
8 changes: 4 additions & 4 deletions components/style/stylesheets/supports_rule.rs
Expand Up @@ -178,16 +178,16 @@ impl SupportsCondition {
while input.try(Parser::expect_whitespace).is_ok() {}
let pos = input.position();
let location = input.current_source_location();
// FIXME: remove clone() when lifetimes are non-lexical
match input.next()?.clone() {
match *input.next()? {
Token::ParenthesisBlock => {
let nested =
input.try(|input| input.parse_nested_block(parse_condition_or_declaration));
if nested.is_ok() {
return nested;
}
},
Token::Function(ident) => {
Token::Function(ref ident) => {
let ident = ident.clone();
let nested = input.try(|input| {
input.parse_nested_block(|input| {
SupportsCondition::parse_functional(&ident, input)
Expand All @@ -197,7 +197,7 @@ impl SupportsCondition {
return nested;
}
},
t => return Err(location.new_unexpected_token_error(t)),
ref t => return Err(location.new_unexpected_token_error(t.clone())),
}
input.parse_nested_block(consume_any_value)?;
Ok(SupportsCondition::FutureSyntax(
Expand Down
24 changes: 17 additions & 7 deletions components/style/values/specified/angle.rs
Expand Up @@ -208,24 +208,34 @@ impl Angle {
input: &mut Parser<'i, 't>,
allow_unitless_zero: AllowUnitlessZeroAngle,
) -> Result<Self, ParseError<'i>> {
// FIXME: remove clone() when lifetimes are non-lexical
let token = input.next()?.clone();
match token {
let t = input.next()?;
match *t {
Token::Dimension {
value, ref unit, ..
} => {
Angle::parse_dimension(value, unit, /* from_calc = */ false)
match Angle::parse_dimension(value, unit, /* from_calc = */ false) {
Ok(angle) => Ok(angle),
Err(()) => {
let t = t.clone();
Err(input.new_unexpected_token_error(t))
}
}
},
Token::Number { value, .. } if value == 0. => match allow_unitless_zero {
AllowUnitlessZeroAngle::Yes => Ok(Angle::zero()),
AllowUnitlessZeroAngle::No => Err(()),
AllowUnitlessZeroAngle::No => {
let t = t.clone();
Err(input.new_unexpected_token_error(t))
},
},
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
return input.parse_nested_block(|i| CalcNode::parse_angle(context, i));
},
_ => Err(()),
ref t => {
let t = t.clone();
Err(input.new_unexpected_token_error(t))
}
}
.map_err(|()| input.new_unexpected_token_error(token.clone()))
}
}

Expand Down
36 changes: 20 additions & 16 deletions components/style/values/specified/calc.rs
Expand Up @@ -163,9 +163,8 @@ impl CalcNode {
expected_unit: CalcUnit,
) -> Result<Self, ParseError<'i>> {
let location = input.current_source_location();
// FIXME: remove early returns when lifetimes are non-lexical
match (input.next()?, expected_unit) {
(&Token::Number { value, .. }, _) => return Ok(CalcNode::Number(value)),
(&Token::Number { value, .. }, _) => Ok(CalcNode::Number(value)),
(
&Token::Dimension {
value, ref unit, ..
Expand All @@ -178,45 +177,48 @@ impl CalcNode {
},
CalcUnit::LengthPercentage,
) => {
return NoCalcLength::parse_dimension(context, value, unit)
NoCalcLength::parse_dimension(context, value, unit)
.map(CalcNode::Length)
.map_err(|()| {
location.new_custom_error(StyleParseErrorKind::UnspecifiedError)
});
})
},
(
&Token::Dimension {
value, ref unit, ..
},
CalcUnit::Angle,
) => {
return Angle::parse_dimension(value, unit, /* from_calc = */ true)
Angle::parse_dimension(value, unit, /* from_calc = */ true)
.map(CalcNode::Angle)
.map_err(|()| {
location.new_custom_error(StyleParseErrorKind::UnspecifiedError)
});
})
},
(
&Token::Dimension {
value, ref unit, ..
},
CalcUnit::Time,
) => {
return Time::parse_dimension(value, unit, /* from_calc = */ true)
Time::parse_dimension(value, unit, /* from_calc = */ true)
.map(CalcNode::Time)
.map_err(|()| {
location.new_custom_error(StyleParseErrorKind::UnspecifiedError)
});
})
},
(&Token::Percentage { unit_value, .. }, CalcUnit::LengthPercentage) |
(&Token::Percentage { unit_value, .. }, CalcUnit::Percentage) => {
return Ok(CalcNode::Percentage(unit_value));
Ok(CalcNode::Percentage(unit_value))
},
(&Token::ParenthesisBlock, _) => {
input.parse_nested_block(|i| CalcNode::parse(context, i, expected_unit))
},
(&Token::Function(ref name), _) if name.eq_ignore_ascii_case("calc") => {
input.parse_nested_block(|i| CalcNode::parse(context, i, expected_unit))
},
(&Token::ParenthesisBlock, _) => {},
(&Token::Function(ref name), _) if name.eq_ignore_ascii_case("calc") => {},
(t, _) => return Err(location.new_unexpected_token_error(t.clone())),
(t, _) => Err(location.new_unexpected_token_error(t.clone())),
}
input.parse_nested_block(|i| CalcNode::parse(context, i, expected_unit))
}

/// Parse a top-level `calc` expression, with all nested sub-expressions.
Expand All @@ -236,8 +238,7 @@ impl CalcNode {
if input.is_exhausted() {
break; // allow trailing whitespace
}
// FIXME: remove clone() when lifetimes are non-lexical
match input.next()?.clone() {
match *input.next()? {
Token::Delim('+') => {
let rhs = Self::parse_product(context, input, expected_unit)?;
let new_root = CalcNode::Sum(Box::new(root), Box::new(rhs));
Expand All @@ -248,7 +249,10 @@ impl CalcNode {
let new_root = CalcNode::Sub(Box::new(root), Box::new(rhs));
root = new_root;
},
t => return Err(input.new_unexpected_token_error(t)),
ref t => {
let t = t.clone();
return Err(input.new_unexpected_token_error(t));
}
}
},
_ => {
Expand Down
56 changes: 31 additions & 25 deletions components/style/values/specified/counters.rs
Expand Up @@ -63,7 +63,10 @@ fn parse_counters<'i, 't>(
let location = input.current_source_location();
let name = match input.next() {
Ok(&Token::Ident(ref ident)) => CustomIdent::from_ident(location, ident, &["none"])?,
Ok(t) => return Err(location.new_unexpected_token_error(t.clone())),
Ok(t) => {
let t = t.clone();
return Err(location.new_unexpected_token_error(t));
},
Err(_) => break,
};

Expand Down Expand Up @@ -147,57 +150,60 @@ impl Parse for Content {
continue;
}
}
// FIXME: remove clone() when lifetimes are non-lexical
match input.next().map(|t| t.clone()) {
Ok(Token::QuotedString(ref value)) => {
match input.next() {
Ok(&Token::QuotedString(ref value)) => {
content.push(generics::ContentItem::String(
value.as_ref().to_owned().into_boxed_str(),
));
},
Ok(Token::Function(ref name)) => {
Ok(&Token::Function(ref name)) => {
let result = match_ignore_ascii_case! { &name,
"counter" => Some(input.parse_nested_block(|input| {
"counter" => input.parse_nested_block(|input| {
let location = input.current_source_location();
let name = CustomIdent::from_ident(location, input.expect_ident()?, &[])?;
let style = Content::parse_counter_style(context, input);
Ok(generics::ContentItem::Counter(name, style))
})),
"counters" => Some(input.parse_nested_block(|input| {
}),
"counters" => input.parse_nested_block(|input| {
let location = input.current_source_location();
let name = CustomIdent::from_ident(location, input.expect_ident()?, &[])?;
input.expect_comma()?;
let separator = input.expect_string()?.as_ref().to_owned().into_boxed_str();
let style = Content::parse_counter_style(context, input);
Ok(generics::ContentItem::Counters(name, separator, style))
})),
}),
#[cfg(feature = "gecko")]
"attr" => Some(input.parse_nested_block(|input| {
"attr" => input.parse_nested_block(|input| {
Ok(generics::ContentItem::Attr(Attr::parse_function(context, input)?))
})),
_ => None
};
match result {
Some(result) => content.push(result?),
None => {
}),
_ => {
let name = name.clone();
return Err(input.new_custom_error(
StyleParseErrorKind::UnexpectedFunction(name.clone()),
));
},
}
StyleParseErrorKind::UnexpectedFunction(name),
))
}
}?;
content.push(result);
},
Ok(Token::Ident(ref ident)) => {
Ok(&Token::Ident(ref ident)) => {
content.push(match_ignore_ascii_case! { &ident,
"open-quote" => generics::ContentItem::OpenQuote,
"close-quote" => generics::ContentItem::CloseQuote,
"no-open-quote" => generics::ContentItem::NoOpenQuote,
"no-close-quote" => generics::ContentItem::NoCloseQuote,
_ => return Err(input.new_custom_error(
SelectorParseErrorKind::UnexpectedIdent(ident.clone())
))
_ =>{
let ident = ident.clone();
return Err(input.new_custom_error(
SelectorParseErrorKind::UnexpectedIdent(ident)
));
}
});
},
Err(_) => break,
Ok(t) => return Err(input.new_unexpected_token_error(t)),
Ok(t) => {
let t = t.clone();
return Err(input.new_unexpected_token_error(t));
}
}
}
if content.is_empty() {
Expand Down

0 comments on commit d9aa057

Please sign in to comment.