CSS parsing error types: flatten nested enums more still

This commit is contained in:
Simon Sapin 2017-10-09 18:15:18 +02:00
parent 1a041084ce
commit c36ac69d48
6 changed files with 49 additions and 88 deletions

View file

@ -1079,9 +1079,9 @@ impl<'a, 'b, 'i> DeclarationParser<'i> for PropertyDeclarationParser<'a, 'b> {
Ok(id) => id,
Err(()) => {
return Err(input.new_custom_error(if is_non_mozilla_vendor_identifier(&name) {
PropertyDeclarationParseErrorKind::UnknownVendorProperty
StyleParseErrorKind::UnknownVendorProperty
} else {
PropertyDeclarationParseErrorKind::UnknownProperty(name)
StyleParseErrorKind::UnknownProperty(name)
}));
}
};
@ -1125,9 +1125,7 @@ pub fn parse_property_declaration_list<R>(context: &ParserContext,
// If the unrecognized property looks like a vendor-specific property,
// silently ignore it instead of polluting the error output.
if let ParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(
PropertyDeclarationParseErrorKind::UnknownVendorProperty)) = error.kind {
if let ParseErrorKind::Custom(StyleParseErrorKind::UnknownVendorProperty) = error.kind {
continue;
}

View file

@ -38,8 +38,7 @@ use selector_parser::PseudoElement;
use selectors::parser::SelectorParseErrorKind;
#[cfg(feature = "servo")] use servo_config::prefs::PREFS;
use shared_lock::StylesheetGuards;
use style_traits::{PARSING_MODE_DEFAULT, ToCss, ParseError, PropertyDeclarationParseError};
use style_traits::{PropertyDeclarationParseErrorKind, StyleParseErrorKind};
use style_traits::{PARSING_MODE_DEFAULT, ToCss, ParseError, StyleParseErrorKind};
use stylesheets::{CssRuleType, Origin, UrlExtraData};
#[cfg(feature = "servo")] use values::Either;
use values::generics::text::LineHeight;
@ -554,7 +553,7 @@ impl LonghandId {
longhands::${property.ident}::parse_declared(context, input)
% else:
Err(input.new_custom_error(
PropertyDeclarationParseErrorKind::UnknownProperty("${property.ident}".into())
StyleParseErrorKind::UnknownProperty("${property.ident}".into())
))
% endif
}
@ -1632,7 +1631,7 @@ impl PropertyDeclaration {
pub fn parse_into<'i, 't>(declarations: &mut SourcePropertyDeclaration,
id: PropertyId, name: CowRcStr<'i>,
context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<(), PropertyDeclarationParseError<'i>> {
-> Result<(), ParseError<'i>> {
assert!(declarations.is_empty());
let start = input.state();
match id {
@ -1644,7 +1643,7 @@ impl PropertyDeclaration {
Ok(keyword) => DeclaredValueOwned::CSSWideKeyword(keyword),
Err(()) => match ::custom_properties::SpecifiedValue::parse(input) {
Ok(value) => DeclaredValueOwned::Value(value),
Err(e) => return Err(PropertyDeclarationParseErrorKind::new_invalid(name, e)),
Err(e) => return Err(StyleParseErrorKind::new_invalid(name, e)),
}
};
declarations.push(PropertyDeclaration::Custom(property_name, value));
@ -1663,7 +1662,7 @@ impl PropertyDeclaration {
input.reset(&start);
let (first_token_type, css) =
::custom_properties::parse_non_custom_with_var(input).map_err(|e| {
PropertyDeclarationParseErrorKind::new_invalid(name, e)
StyleParseErrorKind::new_invalid(name, e)
})?;
Ok(PropertyDeclaration::WithVariables(id, Arc::new(UnparsedValue {
css: css.into_owned(),
@ -1672,7 +1671,7 @@ impl PropertyDeclaration {
from_shorthand: None,
})))
} else {
Err(PropertyDeclarationParseErrorKind::new_invalid(name, err))
Err(StyleParseErrorKind::new_invalid(name, err))
}
})
}).map(|declaration| {
@ -1700,7 +1699,7 @@ impl PropertyDeclaration {
input.reset(&start);
let (first_token_type, css) =
::custom_properties::parse_non_custom_with_var(input).map_err(|e| {
PropertyDeclarationParseErrorKind::new_invalid(name, e)
StyleParseErrorKind::new_invalid(name, e)
})?;
let unparsed = Arc::new(UnparsedValue {
css: css.into_owned(),
@ -1719,7 +1718,7 @@ impl PropertyDeclaration {
}
Ok(())
} else {
Err(PropertyDeclarationParseErrorKind::new_invalid(name, err))
Err(StyleParseErrorKind::new_invalid(name, err))
}
})
}

View file

@ -16,7 +16,6 @@ use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, SharedRwLock, SharedRwLockReadGuard, Locked, ToCssWithGuard};
use std::fmt;
use style_traits::{PARSING_MODE_DEFAULT, ToCss, ParseError, StyleParseErrorKind};
use style_traits::PropertyDeclarationParseErrorKind;
use stylesheets::{CssRuleType, StylesheetContents};
use stylesheets::rule_parser::VendorPrefix;
use values::{KeyframesName, serialize_percentage};
@ -591,7 +590,7 @@ impl<'a, 'b, 'i> DeclarationParser<'i> for KeyframeDeclarationParser<'a, 'b> {
let property_context = PropertyParserContext::new(self.context);
let id = PropertyId::parse(&name, Some(&property_context)).map_err(|()| {
input.new_custom_error(PropertyDeclarationParseErrorKind::UnknownProperty(name.clone()))
input.new_custom_error(StyleParseErrorKind::UnknownProperty(name.clone()))
})?;
match PropertyDeclaration::parse_into(self.declarations, id, name, self.context, input) {
Ok(()) => {

View file

@ -90,9 +90,6 @@ pub type ParseError<'i> = cssparser::ParseError<'i, StyleParseErrorKind<'i>>;
/// Error in property value parsing
pub type ValueParseError<'i> = cssparser::ParseError<'i, ValueParseErrorKind<'i>>;
/// Error in property parsing
pub type PropertyDeclarationParseError<'i> = cssparser::ParseError<'i, PropertyDeclarationParseErrorKind<'i>>;
#[derive(Clone, Debug, PartialEq)]
/// Errors that can be encountered while parsing CSS values.
pub enum StyleParseErrorKind<'i> {
@ -106,8 +103,6 @@ pub enum StyleParseErrorKind<'i> {
UnbalancedCloseSquareBracketInDeclarationValueBlock,
/// Unexpected closing curly bracket in a DVB.
UnbalancedCloseCurlyBracketInDeclarationValueBlock,
/// A property declaration parsing error.
PropertyDeclaration(PropertyDeclarationParseErrorKind<'i>),
/// A property declaration value had input remaining after successfully parsing.
PropertyDeclarationValueNotExhausted,
/// An unexpected dimension token was encountered.
@ -138,38 +133,7 @@ pub enum StyleParseErrorKind<'i> {
ValueError(ValueParseErrorKind<'i>),
/// An error was encountered while parsing a selector
SelectorError(SelectorParseErrorKind<'i>),
}
impl<'i> From<ValueParseErrorKind<'i>> for StyleParseErrorKind<'i> {
fn from(this: ValueParseErrorKind<'i>) -> Self {
StyleParseErrorKind::ValueError(this)
}
}
impl<'i> From<SelectorParseErrorKind<'i>> for StyleParseErrorKind<'i> {
fn from(this: SelectorParseErrorKind<'i>) -> Self {
StyleParseErrorKind::SelectorError(this)
}
}
impl<'i> From<PropertyDeclarationParseErrorKind<'i>> for StyleParseErrorKind<'i> {
fn from(this: PropertyDeclarationParseErrorKind<'i>) -> Self {
StyleParseErrorKind::PropertyDeclaration(this)
}
}
/// Specific errors that can be encountered while parsing property values.
#[derive(Clone, Debug, PartialEq)]
pub enum ValueParseErrorKind<'i> {
/// An invalid token was encountered while parsing a color value.
InvalidColor(Token<'i>),
/// An invalid filter value was encountered.
InvalidFilter(Token<'i>),
}
/// The result of parsing a property declaration.
#[derive(Clone, Debug, PartialEq)]
pub enum PropertyDeclarationParseErrorKind<'i> {
/// The property declaration was for an unknown property.
UnknownProperty(CowRcStr<'i>),
/// An unknown vendor-specific identifier was encountered.
@ -191,21 +155,42 @@ pub enum PropertyDeclarationParseErrorKind<'i> {
NotAllowedInPageRule,
}
impl<'i> PropertyDeclarationParseErrorKind<'i> {
impl<'i> From<ValueParseErrorKind<'i>> for StyleParseErrorKind<'i> {
fn from(this: ValueParseErrorKind<'i>) -> Self {
StyleParseErrorKind::ValueError(this)
}
}
impl<'i> From<SelectorParseErrorKind<'i>> for StyleParseErrorKind<'i> {
fn from(this: SelectorParseErrorKind<'i>) -> Self {
StyleParseErrorKind::SelectorError(this)
}
}
/// Specific errors that can be encountered while parsing property values.
#[derive(Clone, Debug, PartialEq)]
pub enum ValueParseErrorKind<'i> {
/// An invalid token was encountered while parsing a color value.
InvalidColor(Token<'i>),
/// An invalid filter value was encountered.
InvalidFilter(Token<'i>),
}
impl<'i> StyleParseErrorKind<'i> {
/// Create an InvalidValue parse error
pub fn new_invalid(name: CowRcStr<'i>, value_error: ParseError<'i>) -> PropertyDeclarationParseError<'i> {
pub fn new_invalid(name: CowRcStr<'i>, value_error: ParseError<'i>) -> ParseError<'i> {
let variant = match value_error.kind {
cssparser::ParseErrorKind::Custom(StyleParseErrorKind::ValueError(e)) => {
match e {
ValueParseErrorKind::InvalidColor(token) => {
PropertyDeclarationParseErrorKind::InvalidColor(name, token)
StyleParseErrorKind::InvalidColor(name, token)
}
ValueParseErrorKind::InvalidFilter(token) => {
PropertyDeclarationParseErrorKind::InvalidFilter(name, token)
StyleParseErrorKind::InvalidFilter(name, token)
}
}
}
_ => PropertyDeclarationParseErrorKind::OtherInvalidValue(name),
_ => StyleParseErrorKind::OtherInvalidValue(name),
};
cssparser::ParseError {
kind: cssparser::ParseErrorKind::Custom(variant),

View file

@ -18,7 +18,7 @@ use style::gecko_bindings::structs::ErrorReporter as GeckoErrorReporter;
use style::gecko_bindings::structs::URLExtraData as RawUrlExtraData;
use style::gecko_bindings::sugar::refptr::RefPtr;
use style::stylesheets::UrlExtraData;
use style_traits::{StyleParseErrorKind, PropertyDeclarationParseErrorKind};
use style_traits::StyleParseErrorKind;
pub type ErrorKind<'i> = ParseErrorKind<'i, StyleParseErrorKind<'i>>;
@ -90,11 +90,7 @@ fn extract_error_param<'a>(err: ErrorKind<'a>) -> Option<ErrorString<'a>> {
ErrorString::Snippet(s.into())
}
ParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(
PropertyDeclarationParseErrorKind::OtherInvalidValue(property)
)
) => {
ParseErrorKind::Custom(StyleParseErrorKind::OtherInvalidValue(property)) => {
ErrorString::Snippet(property)
}
@ -106,11 +102,7 @@ fn extract_error_param<'a>(err: ErrorKind<'a>) -> Option<ErrorString<'a>> {
ErrorString::Ident(ident)
}
ParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(
PropertyDeclarationParseErrorKind::UnknownProperty(property)
)
) => {
ParseErrorKind::Custom(StyleParseErrorKind::UnknownProperty(property)) => {
ErrorString::Ident(property)
}
@ -133,16 +125,8 @@ struct ErrorParams<'a> {
/// a second parameter if it exists, for use in the prefix for the eventual error message.
fn extract_error_params<'a>(err: ErrorKind<'a>) -> Option<ErrorParams<'a>> {
let (main, prefix) = match err {
ParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(
PropertyDeclarationParseErrorKind::InvalidColor(property, token)
)
) |
ParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(
PropertyDeclarationParseErrorKind::InvalidFilter(property, token)
)
) => {
ParseErrorKind::Custom(StyleParseErrorKind::InvalidColor(property, token)) |
ParseErrorKind::Custom(StyleParseErrorKind::InvalidFilter(property, token)) => {
(Some(ErrorString::Snippet(property.into())), Some(ErrorString::UnexpectedToken(token)))
}
@ -247,20 +231,18 @@ impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> {
(b"PEParseDeclarationDeclExpected\0", Action::Skip)
}
ContextualParseError::UnsupportedPropertyDeclaration(
_, ParseError { kind: ParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(ref err)
), .. }
_, ParseError { kind: ParseErrorKind::Custom(ref err), .. }
) => {
match *err {
PropertyDeclarationParseErrorKind::InvalidColor(_, _) => {
StyleParseErrorKind::InvalidColor(_, _) => {
return (Some(b"PEColorNotColor\0"),
b"PEValueParsingError\0", Action::Drop)
}
PropertyDeclarationParseErrorKind::InvalidFilter(_, _) => {
StyleParseErrorKind::InvalidFilter(_, _) => {
return (Some(b"PEExpectedNoneOrURLOrFilterFunction\0"),
b"PEValueParsingError\0", Action::Drop)
}
PropertyDeclarationParseErrorKind::OtherInvalidValue(_) => {
StyleParseErrorKind::OtherInvalidValue(_) => {
(b"PEValueParsingError\0", Action::Drop)
}
_ => (b"PEUnknownProperty\0", Action::Drop)

View file

@ -15,11 +15,9 @@ size_of_test!(test_size_of_property_declaration, properties::PropertyDeclaration
size_of_test!(test_size_of_parsed_declaration, properties::SourcePropertyDeclaration, 576);
size_of_test!(test_size_of_selector_parse_error_kind, SelectorParseErrorKind, 40);
size_of_test!(test_size_of_style_parse_error_kind, ::style_traits::StyleParseErrorKind, 64);
size_of_test!(test_size_of_style_parse_error_kind, ::style_traits::StyleParseErrorKind, 56);
size_of_test!(test_size_of_value_parse_error_kind, ::style_traits::ValueParseErrorKind, 40);
size_of_test!(test_size_of_declaration_parse_error_kind, ::style_traits::PropertyDeclarationParseErrorKind, 56);
size_of_test!(test_size_of_selector_parse_error, SelectorParseError, 56);
size_of_test!(test_size_of_style_traits_parse_error, ::style_traits::ParseError, 80);
size_of_test!(test_size_of_style_traits_parse_error, ::style_traits::ParseError, 72);
size_of_test!(test_size_of_value_parse_error, ::style_traits::ValueParseError, 56);
size_of_test!(test_size_of_declaration_parse_error, ::style_traits::PropertyDeclarationParseError, 72);