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

@ -295,8 +295,7 @@ pub enum System {
impl Parse for System {
fn parse<'i, 't>(_context: &ParserContext, 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()?,
"cyclic" => Ok(System::Cyclic),
"numeric" => Ok(System::Numeric),
"alphabetic" => Ok(System::Alphabetic),
@ -310,8 +309,7 @@ impl Parse for System {
let other = parse_counter_style_name(input)?;
Ok(System::Extends(other))
}
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
}
@ -617,9 +615,9 @@ pub enum SpeakAs {
impl Parse for SpeakAs {
fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
let mut is_spell_out = false;
let result: Result<_, ParseError> = input.try(|input| {
let ident = 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,
"auto" => Ok(SpeakAs::Auto),
"bullets" => Ok(SpeakAs::Bullets),
"numbers" => Ok(SpeakAs::Numbers),
@ -628,8 +626,8 @@ impl Parse for SpeakAs {
is_spell_out = true;
Err(())
}
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
_ => Err(()),
}
});
if is_spell_out {
// spell-out is not supported, but dont parse it as a <counter-style-name>.

View file

@ -4,6 +4,24 @@
//! Various macro helpers.
/// A macro to parse an identifier, or return an `UnexpectedIndent` error
/// otherwise.
///
/// FIXME(emilio): The fact that `UnexpectedIdent` is a `SelectorParseError`
/// doesn't make a lot of sense to me.
macro_rules! try_match_ident_ignore_ascii_case {
($ident:expr, $( $match_body:tt )*) => {
let __ident = $ident;
(match_ignore_ascii_case! { &*__ident,
$( $match_body )*
_ => Err(()),
})
.map_err(|()| {
::selectors::parser::SelectorParseError::UnexpectedIdent(__ident).into()
})
}
}
macro_rules! define_numbered_css_keyword_enum {
($name: ident: $( $css: expr => $variant: ident = $value: expr ),+,) => {
define_numbered_css_keyword_enum!($name: $( $css => $variant = $value ),+);
@ -21,11 +39,9 @@ macro_rules! define_numbered_css_keyword_enum {
fn parse<'i, 't>(_context: &$crate::parser::ParserContext,
input: &mut ::cssparser::Parser<'i, 't>)
-> Result<$name, ::style_traits::ParseError<'i>> {
let ident = try!(input.expect_ident());
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
$( $css => Ok($name::$variant), )+
_ => Err(())
}).map_err(|()| ::selectors::parser::SelectorParseError::UnexpectedIdent(ident).into())
}
}
}

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,
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
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
})
}
}
/// 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,
match input.next()? {
Token::Ident(ident) => {
try_match_ident_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()))
}
}
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,

View file

@ -7,7 +7,6 @@
use cssparser::{Parser, serialize_identifier};
use parser::{Parse, ParserContext};
use selectors::parser::SelectorParseError;
use std::{fmt, mem, usize};
use style_traits::{ToCss, ParseError, StyleParseError};
use values::{CSSFloat, CustomIdent};
@ -350,12 +349,10 @@ impl Parse for RepeatCount {
Err(StyleParseError::UnspecifiedError.into())
}
} else {
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"auto-fill" => Ok(RepeatCount::AutoFill),
"auto-fit" => Ok(RepeatCount::AutoFit),
_ => Err(()),
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
}
}

View file

@ -8,7 +8,6 @@
use counter_style::{Symbols, parse_counter_style_name};
use cssparser::Parser;
use parser::{Parse, ParserContext};
use selectors::parser::SelectorParseError;
use std::fmt;
use style_traits::{OneOrMoreCommaSeparated, ToCss, ParseError, StyleParseError};
use super::CustomIdent;
@ -328,13 +327,11 @@ impl<ColorType> SVGPaint<ColorType> {
impl<ColorType> SVGPaintKind<ColorType> {
/// Parse a keyword value only
fn parse_ident<'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()?,
"none" => Ok(SVGPaintKind::None),
"context-fill" => Ok(SVGPaintKind::ContextFill),
"context-stroke" => Ok(SVGPaintKind::ContextStroke),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
}

View file

@ -343,13 +343,11 @@ fn parse_auto_normal_stretch_baseline<'i, 't>(input: &mut Parser<'i, 't>)
return Ok(baseline);
}
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"auto" => Ok(ALIGN_AUTO),
"normal" => Ok(ALIGN_NORMAL),
"stretch" => Ok(ALIGN_STRETCH),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
// normal | stretch | <baseline-position>
@ -358,12 +356,10 @@ fn parse_normal_stretch_baseline<'i, 't>(input: &mut Parser<'i, 't>) -> Result<A
return Ok(baseline);
}
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"normal" => Ok(ALIGN_NORMAL),
"stretch" => Ok(ALIGN_STRETCH),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
// normal | <baseline-position>
@ -372,34 +368,33 @@ fn parse_normal_or_baseline<'i, 't>(input: &mut Parser<'i, 't>) -> Result<AlignF
return Ok(baseline);
}
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &ident,
"normal" => Ok(ALIGN_NORMAL),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
input.expect_ident_matching("normal")?;
Ok(ALIGN_NORMAL)
}
// <baseline-position>
fn parse_baseline<'i, 't>(input: &mut Parser<'i, 't>) -> Result<AlignFlags, ParseError<'i>> {
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"baseline" => Ok(ALIGN_BASELINE),
"first" => return input.expect_ident_matching("baseline").map(|_| ALIGN_BASELINE).map_err(|e| e.into()),
"last" => return input.expect_ident_matching("baseline").map(|_| ALIGN_LAST_BASELINE).map_err(|e| e.into()),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
"first" => {
input.expect_ident_matching("baseline")?;
Ok(ALIGN_BASELINE)
}
"last" => {
input.expect_ident_matching("baseline")?;
Ok(ALIGN_LAST_BASELINE)
}
}
}
// <content-distribution>
fn parse_content_distribution<'i, 't>(input: &mut Parser<'i, 't>) -> Result<AlignFlags, ParseError<'i>> {
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"stretch" => Ok(ALIGN_STRETCH),
"space-between" => Ok(ALIGN_SPACE_BETWEEN),
"space-around" => Ok(ALIGN_SPACE_AROUND),
"space-evenly" => Ok(ALIGN_SPACE_EVENLY),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
// [ <overflow-position>? && <content-position> ]
@ -422,8 +417,7 @@ fn parse_overflow_content_position<'i, 't>(input: &mut Parser<'i, 't>) -> Result
// <content-position>
fn parse_content_position<'i, 't>(input: &mut Parser<'i, 't>) -> Result<AlignFlags, ParseError<'i>> {
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"start" => Ok(ALIGN_START),
"end" => Ok(ALIGN_END),
"flex-start" => Ok(ALIGN_FLEX_START),
@ -431,18 +425,15 @@ fn parse_content_position<'i, 't>(input: &mut Parser<'i, 't>) -> Result<AlignFla
"center" => Ok(ALIGN_CENTER),
"left" => Ok(ALIGN_LEFT),
"right" => Ok(ALIGN_RIGHT),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
// <overflow-position>
fn parse_overflow_position<'i, 't>(input: &mut Parser<'i, 't>) -> Result<AlignFlags, ParseError<'i>> {
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"safe" => Ok(ALIGN_SAFE),
"unsafe" => Ok(ALIGN_UNSAFE),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
// [ <overflow-position>? && <self-position> ]
@ -465,8 +456,7 @@ fn parse_overflow_self_position<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Al
// <self-position>
fn parse_self_position<'i, 't>(input: &mut Parser<'i, 't>) -> Result<AlignFlags, ParseError<'i>> {
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"start" => Ok(ALIGN_START),
"end" => Ok(ALIGN_END),
"flex-start" => Ok(ALIGN_FLEX_START),
@ -476,8 +466,7 @@ fn parse_self_position<'i, 't>(input: &mut Parser<'i, 't>) -> Result<AlignFlags,
"right" => Ok(ALIGN_RIGHT),
"self-start" => Ok(ALIGN_SELF_START),
"self-end" => Ok(ALIGN_SELF_END),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
// [ legacy && [ left | right | center ] ]

View file

@ -9,7 +9,6 @@
use cssparser::Parser;
use parser::{Parse, ParserContext};
use selectors::parser::SelectorParseError;
use std::borrow::Cow;
use std::fmt;
use style_traits::{ToCss, ParseError, StyleParseError};
@ -91,13 +90,11 @@ impl Parse for GeometryBox {
return Ok(GeometryBox::ShapeBox(shape_box))
}
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"fill-box" => Ok(GeometryBox::FillBox),
"stroke-box" => Ok(GeometryBox::StrokeBox),
"view-box" => Ok(GeometryBox::ViewBox),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
}
@ -231,12 +228,10 @@ impl Parse for ShapeRadius {
return Ok(GenericShapeRadius::Length(lop))
}
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"closest-side" => Ok(GenericShapeRadius::ClosestSide),
"farthest-side" => Ok(GenericShapeRadius::FarthestSide),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
}

View file

@ -7,7 +7,6 @@
use app_units::Au;
use cssparser::Parser;
use parser::{Parse, ParserContext};
use selectors::parser::SelectorParseError;
use style_traits::ParseError;
use values::computed::{Context, ToComputedValue};
use values::generics::border::BorderCornerRadius as GenericBorderCornerRadius;
@ -58,13 +57,11 @@ impl BorderSideWidth {
if let Ok(length) = input.try(|i| Length::parse_non_negative_quirky(context, i, allow_quirks)) {
return Ok(BorderSideWidth::Length(length));
}
let ident = input.expect_ident()?;
(match_ignore_ascii_case! { &ident,
try_match_ident_ignore_ascii_case! { input.expect_ident()?,
"thin" => Ok(BorderSideWidth::Thin),
"medium" => Ok(BorderSideWidth::Medium),
"thick" => Ok(BorderSideWidth::Thick),
_ => Err(())
}).map_err(|()| SelectorParseError::UnexpectedIdent(ident).into())
}
}
}