style: Reduce some code duplication and ugliness when parsing identifiers.

This commit is contained in:
Emilio Cobos Álvarez 2017-06-10 15:07:33 +02:00
parent ddfe8b0468
commit e3c4d03bde
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
12 changed files with 104 additions and 134 deletions

View file

@ -115,15 +115,13 @@
/// Parse a display value.
pub fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<SpecifiedValue, ParseError<'i>> {
let ident = try!(input.expect_ident());
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
% for value in values:
"${value}" => {
Ok(computed_value::T::${to_rust_ident(value)})
},
% endfor
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
impl ComputedValueAsSpecified for SpecifiedValue {}
@ -295,17 +293,15 @@ ${helpers.single_keyword("position", "static absolute relative fixed",
/// | <percentage> | <length>
pub fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<SpecifiedValue, ParseError<'i>> {
input.try(|i| specified::LengthOrPercentage::parse_quirky(context, i, AllowQuirks::Yes))
.map(SpecifiedValue::LengthOrPercentage)
.or_else(|_| {
let ident = try!(input.expect_ident());
(match_ignore_ascii_case! { &ident,
% for keyword in vertical_align_keywords:
"${keyword}" => Ok(SpecifiedValue::${to_rust_ident(keyword)}),
% endfor
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
})
if let Ok(lop) = input.try(|i| specified::LengthOrPercentage::parse_quirky(context, i, AllowQuirks::Yes)) {
return Ok(SpecifiedValue::LengthOrPercentage(lop));
}
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
% for keyword in vertical_align_keywords:
"${keyword}" => Ok(SpecifiedValue::${to_rust_ident(keyword)}),
% endfor
}
}
/// The computed value for `vertical-align`.
@ -1997,8 +1993,7 @@ ${helpers.predefined_type("shape-outside", "basic_shape::FloatAreaShape",
pub fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<SpecifiedValue, ParseError<'i>> {
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"auto" => Ok(TOUCH_ACTION_AUTO),
"none" => Ok(TOUCH_ACTION_NONE),
"manipulation" => Ok(TOUCH_ACTION_MANIPULATION),
@ -2016,8 +2011,7 @@ ${helpers.predefined_type("shape-outside", "basic_shape::FloatAreaShape",
Ok(TOUCH_ACTION_PAN_Y)
}
},
_ => Err(()),
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
#[cfg(feature = "gecko")]

View file

@ -5,8 +5,7 @@
<%namespace name="helpers" file="/helpers.mako.rs" />
<% from data import Method, to_camel_case, to_rust_ident, to_camel_case_lower, SYSTEM_FONT_LONGHANDS %>
<% data.new_style_struct("Font",
inherited=True) %>
<% data.new_style_struct("Font", inherited=True) %>
<%def name="nongecko_unreachable()">
%if product == "gecko":
@ -447,15 +446,15 @@ ${helpers.single_keyword_system("font-variant-caps",
/// normal | bold | bolder | lighter | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
pub fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<SpecifiedValue, ParseError<'i>> {
let result: Result<_, ParseError> = input.try(|input| {
let ident = try!(input.expect_ident());
(match_ignore_ascii_case! { &ident,
let result = input.try(|input| {
let ident = input.expect_ident().map_err(|_| ())?;
match_ignore_ascii_case! { &ident,
"normal" => Ok(SpecifiedValue::Normal),
"bold" => Ok(SpecifiedValue::Bold),
"bolder" => Ok(SpecifiedValue::Bolder),
"lighter" => Ok(SpecifiedValue::Lighter),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
});
result.or_else(|_| {
SpecifiedValue::from_int(input.expect_integer()?)
@ -689,8 +688,7 @@ ${helpers.single_keyword_system("font-variant-caps",
impl KeywordSize {
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
let ident = input.expect_ident()?;
(match_ignore_ascii_case! {&*ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"xx-small" => Ok(XXSmall),
"x-small" => Ok(XSmall),
"small" => Ok(Small),
@ -698,8 +696,7 @@ ${helpers.single_keyword_system("font-variant-caps",
"large" => Ok(Large),
"x-large" => Ok(XLarge),
"xx-large" => Ok(XXLarge),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
}
@ -931,12 +928,10 @@ ${helpers.single_keyword_system("font-variant-caps",
return Ok(SpecifiedValue::Keyword(kw, 1.))
}
let ident = input.expect_ident()?;
(match_ignore_ascii_case! {&*ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"smaller" => Ok(SpecifiedValue::Smaller),
"larger" => Ok(SpecifiedValue::Larger),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
impl SpecifiedValue {
@ -1212,8 +1207,7 @@ ${helpers.single_keyword_system("font-variant-caps",
pub fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<SpecifiedValue, ParseError<'i>> {
let mut result = SpecifiedValue { weight: false, style: false };
let ident = try!(input.expect_ident());
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"none" => Ok(result),
"weight" => {
result.weight = true;
@ -1229,8 +1223,7 @@ ${helpers.single_keyword_system("font-variant-caps",
}
Ok(result)
},
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
#[cfg(feature = "gecko")]
@ -2343,7 +2336,6 @@ ${helpers.single_keyword("-moz-math-variant",
use app_units::Au;
use cssparser::Parser;
use properties::longhands;
use selectors::parser::SelectorParseError;
use std::hash::{Hash, Hasher};
use style_traits::ParseError;
use values::computed::{ToComputedValue, Context};
@ -2465,13 +2457,11 @@ ${helpers.single_keyword("-moz-math-variant",
impl SystemFont {
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &*ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
% for font in system_fonts:
"${font}" => Ok(SystemFont::${to_camel_case(font)}),
% endfor
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
}
}

View file

@ -195,15 +195,12 @@ ${helpers.predefined_type("marker-end", "UrlOrNone", "Either::Second(None_)",
let mut pos = 0;
loop {
let result: Result<_, ParseError> = input.try(|i| {
let ident = i.expect_ident()?;
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { i.expect_ident()?,
"fill" => Ok(FILL),
"stroke" => Ok(STROKE),
"markers" => Ok(MARKERS),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident.into()).into())
}
});
match result {

View file

@ -106,7 +106,7 @@
}
pub fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<SpecifiedValue, ParseError<'i>> {
let first = try!(Side::parse(context, input));
let first = Side::parse(context, input)?;
let second = input.try(|input| Side::parse(context, input)).ok();
Ok(SpecifiedValue {
first: first,
@ -116,14 +116,17 @@
impl Parse for Side {
fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Side, ParseError<'i>> {
if let Ok(ident) = input.try(|input| input.expect_ident()) {
(match_ignore_ascii_case! { &ident,
"clip" => Ok(Side::Clip),
"ellipsis" => Ok(Side::Ellipsis),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
} else {
Ok(Side::String(try!(input.expect_string()).into_owned().into_boxed_str()))
match input.next()? {
Token::Ident(ident) => {
try_match_ident_ignore_ascii_case! { ident,
"clip" => Ok(Side::Clip),
"ellipsis" => Ok(Side::Ellipsis),
}
}
Token::QuotedString(v) => {
Ok(Side::String(v.into_owned().into_boxed_str()))
}
other => Err(BasicParseError::UnexpectedToken(other).into()),
}
}
}

View file

@ -15,9 +15,7 @@
-> Result<Longhands, ParseError<'i>> {
% if product == "gecko":
let moz_kw_found = input.try(|i| {
let ident = i.expect_ident()?;
(match_ignore_ascii_case! {
&ident,
try_match_ident_ignore_ascii_case! { i.expect_ident()?,
"-moz-scrollbars-horizontal" => {
Ok(expanded! {
overflow_x: SpecifiedValue::scroll,
@ -36,14 +34,13 @@
overflow_y: SpecifiedValue::hidden,
})
}
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
});
if moz_kw_found.is_ok() {
return moz_kw_found
}
% endif
let overflow = try!(parse_overflow(context, input));
let overflow = parse_overflow(context, input)?;
Ok(expanded! {
overflow_x: overflow,
overflow_y: overflow,