CSS-wide keywords parsing in longhands: use a single code path with IDs

… rather than generating similar code for every longhand property.
This commit is contained in:
Simon Sapin 2017-07-12 21:50:45 +02:00
parent a5b80e2d75
commit 8a8614eccd
2 changed files with 42 additions and 38 deletions

View file

@ -452,31 +452,26 @@
} }
pub fn parse_declared<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) pub fn parse_declared<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<PropertyDeclaration, ParseError<'i>> { -> Result<PropertyDeclaration, ParseError<'i>> {
match input.try(|i| CSSWideKeyword::parse(context, i)) { input.look_for_var_functions();
Ok(keyword) => Ok(PropertyDeclaration::CSSWideKeyword(LonghandId::${property.camel_case}, keyword)), let start = input.position();
Err(_) => { let specified = parse_specified(context, input);
input.look_for_var_functions(); if specified.is_err() {
let start = input.position(); while let Ok(_) = input.next() {} // Look for var() after the error.
let specified = parse_specified(context, input);
if specified.is_err() {
while let Ok(_) = input.next() {} // Look for var() after the error.
}
let var = input.seen_var_functions();
if specified.is_err() && var {
input.reset(start);
let (first_token_type, css) =
::custom_properties::parse_non_custom_with_var(input)?;
return Ok(PropertyDeclaration::WithVariables(LonghandId::${property.camel_case},
Arc::new(UnparsedValue {
css: css.into_owned(),
first_token_type: first_token_type,
url_data: context.url_data.clone(),
from_shorthand: None,
})))
}
specified.map(|s| PropertyDeclaration::${property.camel_case}(s))
}
} }
let var = input.seen_var_functions();
if specified.is_err() && var {
input.reset(start);
let (first_token_type, css) =
::custom_properties::parse_non_custom_with_var(input)?;
return Ok(PropertyDeclaration::WithVariables(LonghandId::${property.camel_case},
Arc::new(UnparsedValue {
css: css.into_owned(),
first_token_type: first_token_type,
url_data: context.url_data.clone(),
from_shorthand: None,
})))
}
specified.map(|s| PropertyDeclaration::${property.camel_case}(s))
} }
% endif % endif
} }

View file

@ -1521,22 +1521,31 @@ impl PropertyDeclaration {
Ok(()) Ok(())
} }
PropertyId::Longhand(id) => { PropertyId::Longhand(id) => {
match id { if let Ok(keyword) = input.try(|i| CSSWideKeyword::parse(context, i)) {
% for property in data.longhands: declarations.push(PropertyDeclaration::CSSWideKeyword(id, keyword))
LonghandId::${property.camel_case} => { Ok(())
% if not property.derived_from: } else {
match longhands::${property.ident}::parse_declared(context, input) { match id {
Ok(value) => { % for property in data.longhands:
declarations.push(value); LonghandId::${property.camel_case} => {
Ok(()) % if not property.derived_from:
}, match longhands::${property.ident}::parse_declared(context, input) {
Err(_) => Err(PropertyDeclarationParseError::InvalidValue("${property.ident}".into())), Ok(value) => {
declarations.push(value);
Ok(())
},
Err(_) => {
Err(PropertyDeclarationParseError::InvalidValue(
"${property.ident}".into()
))
}
}
% else:
Err(PropertyDeclarationParseError::UnknownProperty)
% endif
} }
% else: % endfor
Err(PropertyDeclarationParseError::UnknownProperty)
% endif
} }
% endfor
} }
} }
PropertyId::Shorthand(id) => { PropertyId::Shorthand(id) => {