CSS parsing error types: flatten nested enums some more

This commit is contained in:
Simon Sapin 2017-10-09 17:47:19 +02:00
parent 46ea99d54b
commit 1a041084ce
3 changed files with 47 additions and 32 deletions

View file

@ -176,8 +176,12 @@ pub enum PropertyDeclarationParseErrorKind<'i> {
UnknownVendorProperty,
/// The property declaration was for a disabled experimental property.
ExperimentalProperty,
/// The property declaration contained an invalid color value.
InvalidColor(CowRcStr<'i>, Token<'i>),
/// The property declaration contained an invalid filter value.
InvalidFilter(CowRcStr<'i>, Token<'i>),
/// The property declaration contained an invalid value.
InvalidValue(CowRcStr<'i>, Option<ValueParseErrorKind<'i>>),
OtherInvalidValue(CowRcStr<'i>),
/// The declaration contained an animation property, and we were parsing
/// this as a keyframe block (so that property should be ignored).
///
@ -190,14 +194,21 @@ pub enum PropertyDeclarationParseErrorKind<'i> {
impl<'i> PropertyDeclarationParseErrorKind<'i> {
/// Create an InvalidValue parse error
pub fn new_invalid(name: CowRcStr<'i>, value_error: ParseError<'i>) -> PropertyDeclarationParseError<'i> {
cssparser::ParseError {
kind: cssparser::ParseErrorKind::Custom(PropertyDeclarationParseErrorKind::InvalidValue(
name,
match value_error.kind {
cssparser::ParseErrorKind::Custom(StyleParseErrorKind::ValueError(e)) => Some(e),
_ => None,
let variant = match value_error.kind {
cssparser::ParseErrorKind::Custom(StyleParseErrorKind::ValueError(e)) => {
match e {
ValueParseErrorKind::InvalidColor(token) => {
PropertyDeclarationParseErrorKind::InvalidColor(name, token)
}
ValueParseErrorKind::InvalidFilter(token) => {
PropertyDeclarationParseErrorKind::InvalidFilter(name, token)
}
}
)),
}
_ => PropertyDeclarationParseErrorKind::OtherInvalidValue(name),
};
cssparser::ParseError {
kind: cssparser::ParseErrorKind::Custom(variant),
location: value_error.location,
}
}

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, ValueParseErrorKind};
use style_traits::{StyleParseErrorKind, PropertyDeclarationParseErrorKind};
pub type ErrorKind<'i> = ParseErrorKind<'i, StyleParseErrorKind<'i>>;
@ -92,7 +92,7 @@ fn extract_error_param<'a>(err: ErrorKind<'a>) -> Option<ErrorString<'a>> {
ParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(
PropertyDeclarationParseErrorKind::InvalidValue(property, None)
PropertyDeclarationParseErrorKind::OtherInvalidValue(property)
)
) => {
ErrorString::Snippet(property)
@ -124,13 +124,6 @@ fn extract_error_param<'a>(err: ErrorKind<'a>) -> Option<ErrorString<'a>> {
})
}
fn extract_value_error_param<'a>(err: ValueParseErrorKind<'a>) -> ErrorString<'a> {
match err {
ValueParseErrorKind::InvalidColor(t) |
ValueParseErrorKind::InvalidFilter(t) => ErrorString::UnexpectedToken(t),
}
}
struct ErrorParams<'a> {
prefix_param: Option<ErrorString<'a>>,
main_param: Option<ErrorString<'a>>,
@ -142,10 +135,15 @@ fn extract_error_params<'a>(err: ErrorKind<'a>) -> Option<ErrorParams<'a>> {
let (main, prefix) = match err {
ParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(
PropertyDeclarationParseErrorKind::InvalidValue(property, Some(e))
PropertyDeclarationParseErrorKind::InvalidColor(property, token)
)
) |
ParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(
PropertyDeclarationParseErrorKind::InvalidFilter(property, token)
)
) => {
(Some(ErrorString::Snippet(property.into())), Some(extract_value_error_param(e)))
(Some(ErrorString::Snippet(property.into())), Some(ErrorString::UnexpectedToken(token)))
}
ParseErrorKind::Custom(
@ -250,17 +248,23 @@ impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> {
}
ContextualParseError::UnsupportedPropertyDeclaration(
_, ParseError { kind: ParseErrorKind::Custom(
StyleParseErrorKind::PropertyDeclaration(
PropertyDeclarationParseErrorKind::InvalidValue(_, ref err)
)
StyleParseErrorKind::PropertyDeclaration(ref err)
), .. }
) => {
let prefix = match *err {
Some(ValueParseErrorKind::InvalidColor(_)) => Some(&b"PEColorNotColor\0"[..]),
Some(ValueParseErrorKind::InvalidFilter(_)) => Some(&b"PEExpectedNoneOrURLOrFilterFunction\0"[..]),
_ => None,
};
return (prefix, b"PEValueParsingError\0", Action::Drop);
match *err {
PropertyDeclarationParseErrorKind::InvalidColor(_, _) => {
return (Some(b"PEColorNotColor\0"),
b"PEValueParsingError\0", Action::Drop)
}
PropertyDeclarationParseErrorKind::InvalidFilter(_, _) => {
return (Some(b"PEExpectedNoneOrURLOrFilterFunction\0"),
b"PEValueParsingError\0", Action::Drop)
}
PropertyDeclarationParseErrorKind::OtherInvalidValue(_) => {
(b"PEValueParsingError\0", Action::Drop)
}
_ => (b"PEUnknownProperty\0", Action::Drop)
}
}
ContextualParseError::UnsupportedPropertyDeclaration(..) =>
(b"PEUnknownProperty\0", Action::Drop),

View file

@ -15,11 +15,11 @@ 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, 80);
size_of_test!(test_size_of_style_parse_error_kind, ::style_traits::StyleParseErrorKind, 64);
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, 72);
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, 96);
size_of_test!(test_size_of_style_traits_parse_error, ::style_traits::ParseError, 80);
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, 88);
size_of_test!(test_size_of_declaration_parse_error, ::style_traits::PropertyDeclarationParseError, 72);