PropertyDeclarationParseResult -> Result<(), PropertyDeclarationParseError>

This commit is contained in:
Simon Sapin 2017-03-07 16:19:06 +01:00
parent da6316fbe3
commit 7455ad5eb4
5 changed files with 30 additions and 45 deletions

View file

@ -13,7 +13,6 @@ use parser::{ParserContext, ParserContextExtraData, log_css_error};
use properties::{Importance, PropertyDeclaration, PropertyDeclarationBlock, PropertyId};
use properties::{PropertyDeclarationId, LonghandId, DeclaredValue};
use properties::LonghandIdSet;
use properties::PropertyDeclarationParseResult;
use properties::animated_properties::TransitionProperty;
use properties::longhands::transition_timing_function::single_value::SpecifiedValue as SpecifiedTimingFunction;
use std::fmt;
@ -407,12 +406,9 @@ impl<'a, 'b> DeclarationParser for KeyframeDeclarationParser<'a, 'b> {
fn parse_value(&mut self, name: &str, input: &mut Parser) -> Result<(), ()> {
let id = try!(PropertyId::parse(name.into()));
let old_len = self.declarations.len();
match PropertyDeclaration::parse(id, self.context, input, &mut self.declarations, true) {
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration => {}
_ => {
self.declarations.truncate(old_len);
return Err(());
}
if PropertyDeclaration::parse(id, self.context, input, &mut self.declarations, true).is_err() {
self.declarations.truncate(old_len);
return Err(())
}
// In case there is still unparsed text in the declaration, we should roll back.
if !input.is_exhausted() {

View file

@ -561,8 +561,8 @@ pub fn parse_one_declaration(id: PropertyId,
Parser::new(input).parse_entirely(|parser| {
let mut results = vec![];
match PropertyDeclaration::parse(id, &context, parser, &mut results, false) {
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration => Ok(results),
_ => Err(())
Ok(()) => Ok(results),
Err(_) => Err(())
}
})
}
@ -592,10 +592,8 @@ impl<'a, 'b> DeclarationParser for PropertyDeclarationParser<'a, 'b> {
let id = try!(PropertyId::parse(name.into()));
let old_len = self.declarations.len();
let parse_result = input.parse_until_before(Delimiter::Bang, |input| {
match PropertyDeclaration::parse(id, self.context, input, &mut self.declarations, false) {
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration => Ok(()),
_ => Err(())
}
PropertyDeclaration::parse(id, self.context, input, &mut self.declarations, false)
.map_err(|_| ())
});
if let Err(_) = parse_result {
// rollback

View file

@ -886,7 +886,7 @@ impl HasViewportPercentage for PropertyDeclaration {
/// The result of parsing a property declaration.
#[derive(Eq, PartialEq, Copy, Clone)]
pub enum PropertyDeclarationParseResult {
pub enum PropertyDeclarationParseError {
/// The property declaration was for an unknown property.
UnknownProperty,
/// The property declaration was for a disabled experimental property.
@ -898,8 +898,6 @@ pub enum PropertyDeclarationParseResult {
///
/// See: https://drafts.csswg.org/css-animations/#keyframes
AnimationPropertyInKeyframeBlock,
/// The declaration was either valid or ignored.
ValidOrIgnoredDeclaration,
}
impl fmt::Debug for PropertyDeclaration {
@ -933,7 +931,7 @@ impl ToCss for PropertyDeclaration {
% if property.experimental and product == "servo":
if !PREFS.get("${property.experimental}")
.as_boolean().unwrap_or(false) {
return PropertyDeclarationParseResult::ExperimentalProperty
return Err(PropertyDeclarationParseError::ExperimentalProperty)
}
% endif
% if product == "gecko":
@ -950,7 +948,7 @@ impl ToCss for PropertyDeclaration {
let id = structs::${helpers.to_nscsspropertyid(property.ident)};
let enabled = unsafe { bindings::Gecko_PropertyId_IsPrefEnabled(id) };
if !enabled {
return PropertyDeclarationParseResult::ExperimentalProperty
return Err(PropertyDeclarationParseError::ExperimentalProperty)
}
}
% endif
@ -1060,19 +1058,19 @@ impl PropertyDeclaration {
pub fn parse(id: PropertyId, context: &ParserContext, input: &mut Parser,
result_list: &mut Vec<(PropertyDeclaration, Importance)>,
in_keyframe_block: bool)
-> PropertyDeclarationParseResult {
-> Result<(), PropertyDeclarationParseError> {
match id {
PropertyId::Custom(name) => {
let value = match input.try(|i| CSSWideKeyword::parse(context, i)) {
Ok(keyword) => DeclaredValue::CSSWideKeyword(keyword),
Err(()) => match ::custom_properties::SpecifiedValue::parse(context, input) {
Ok(value) => DeclaredValue::Value(value),
Err(()) => return PropertyDeclarationParseResult::InvalidValue,
Err(()) => return Err(PropertyDeclarationParseError::InvalidValue),
}
};
result_list.push((PropertyDeclaration::Custom(name, value),
Importance::Normal));
return PropertyDeclarationParseResult::ValidOrIgnoredDeclaration;
return Ok(());
}
PropertyId::Longhand(id) => match id {
% for property in data.longhands:
@ -1080,12 +1078,12 @@ impl PropertyDeclaration {
% if not property.derived_from:
% if not property.allowed_in_keyframe_block:
if in_keyframe_block {
return PropertyDeclarationParseResult::AnimationPropertyInKeyframeBlock
return Err(PropertyDeclarationParseError::AnimationPropertyInKeyframeBlock)
}
% endif
% if property.internal:
if context.stylesheet_origin != Origin::UserAgent {
return PropertyDeclarationParseResult::UnknownProperty
return Err(PropertyDeclarationParseError::UnknownProperty)
}
% endif
@ -1095,12 +1093,12 @@ impl PropertyDeclaration {
Ok(value) => {
result_list.push((PropertyDeclaration::${property.camel_case}(value),
Importance::Normal));
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration
Ok(())
},
Err(()) => PropertyDeclarationParseResult::InvalidValue,
Err(()) => Err(PropertyDeclarationParseError::InvalidValue),
}
% else:
PropertyDeclarationParseResult::UnknownProperty
Err(PropertyDeclarationParseError::UnknownProperty)
% endif
}
% endfor
@ -1110,12 +1108,12 @@ impl PropertyDeclaration {
ShorthandId::${shorthand.camel_case} => {
% if not shorthand.allowed_in_keyframe_block:
if in_keyframe_block {
return PropertyDeclarationParseResult::AnimationPropertyInKeyframeBlock
return Err(PropertyDeclarationParseError::AnimationPropertyInKeyframeBlock)
}
% endif
% if shorthand.internal:
if context.stylesheet_origin != Origin::UserAgent {
return PropertyDeclarationParseResult::UnknownProperty
return Err(PropertyDeclarationParseError::UnknownProperty)
}
% endif
@ -1128,16 +1126,16 @@ impl PropertyDeclaration {
PropertyDeclaration::${sub_property.camel_case}(
DeclaredValue::CSSWideKeyword(keyword)), Importance::Normal));
% endfor
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration
Ok(())
},
Err(()) => match shorthands::${shorthand.ident}::parse(context, input) {
Ok(parsed) => {
parsed.expand(|declaration| {
result_list.push((declaration, Importance::Normal))
});
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration
Ok(())
}
Err(()) => PropertyDeclarationParseResult::InvalidValue,
Err(()) => Err(PropertyDeclarationParseError::InvalidValue),
}
}
}

View file

@ -205,7 +205,6 @@ impl Declaration {
///
/// https://drafts.csswg.org/css-conditional-3/#support-definition
pub fn eval(&self, cx: &ParserContext) -> bool {
use properties::PropertyDeclarationParseResult::*;
let id = if let Ok(id) = PropertyId::parse((&*self.prop).into()) {
id
} else {
@ -219,13 +218,6 @@ impl Declaration {
if !input.is_exhausted() {
return false;
}
match res {
UnknownProperty => false,
ExperimentalProperty => false, // only happens for experimental props
// that haven't been enabled
InvalidValue => false,
AnimationPropertyInKeyframeBlock => unreachable!(),
ValidOrIgnoredDeclaration => true,
}
res.is_ok()
}
}