mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
style: Move some parsing-only attributes to use #[parse(..)] instead of #[css(..)].
I need to admit I'm ambivalent about this one :). Bug: 1466609 Reviewed-by: xidorn MozReview-Commit-ID: F1jlfnQKXwo
This commit is contained in:
parent
8821ad72f4
commit
2c0a19e517
8 changed files with 39 additions and 29 deletions
|
@ -157,12 +157,12 @@ impl<'a> ParserContext<'a> {
|
||||||
///
|
///
|
||||||
/// The derive code understands the following attributes on each of the variants:
|
/// The derive code understands the following attributes on each of the variants:
|
||||||
///
|
///
|
||||||
/// * `#[css(aliases = "foo,bar")]` can be used to alias a value with another
|
/// * `#[parse(aliases = "foo,bar")]` can be used to alias a value with another
|
||||||
/// at parse-time.
|
/// at parse-time.
|
||||||
///
|
///
|
||||||
/// * `#[css(parse_condition = "function")]` can be used to make the parsing of
|
/// * `#[parse(condition = "function")]` can be used to make the parsing of the
|
||||||
/// the value conditional on `function`, which will be invoked with a
|
/// value conditional on `function`, which needs to fulfill
|
||||||
/// `&ParserContext` reference.
|
/// `fn(&ParserContext) -> bool`.
|
||||||
pub trait Parse: Sized {
|
pub trait Parse: Sized {
|
||||||
/// Parse a value of this type.
|
/// Parse a value of this type.
|
||||||
///
|
///
|
||||||
|
|
|
@ -594,7 +594,7 @@
|
||||||
aliases.append(alias)
|
aliases.append(alias)
|
||||||
%>
|
%>
|
||||||
% if aliases:
|
% if aliases:
|
||||||
#[css(aliases = "${','.join(aliases)}")]
|
#[parse(aliases = "${','.join(aliases)}")]
|
||||||
% endif
|
% endif
|
||||||
% endif
|
% endif
|
||||||
${to_camel_case(variant)},
|
${to_camel_case(variant)},
|
||||||
|
|
|
@ -53,9 +53,9 @@ pub enum Display {
|
||||||
TableCaption,
|
TableCaption,
|
||||||
ListItem,
|
ListItem,
|
||||||
None,
|
None,
|
||||||
#[css(aliases = "-webkit-flex")]
|
#[parse(aliases = "-webkit-flex")]
|
||||||
Flex,
|
Flex,
|
||||||
#[css(aliases = "-webkit-inline-flex")]
|
#[parse(aliases = "-webkit-inline-flex")]
|
||||||
InlineFlex,
|
InlineFlex,
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
Grid,
|
Grid,
|
||||||
|
@ -84,31 +84,31 @@ pub enum Display {
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
MozInlineBox,
|
MozInlineBox,
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
#[css(parse_condition = "moz_display_values_enabled")]
|
#[parse(condition = "moz_display_values_enabled")]
|
||||||
MozGrid,
|
MozGrid,
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
#[css(parse_condition = "moz_display_values_enabled")]
|
#[parse(condition = "moz_display_values_enabled")]
|
||||||
MozInlineGrid,
|
MozInlineGrid,
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
#[css(parse_condition = "moz_display_values_enabled")]
|
#[parse(condition = "moz_display_values_enabled")]
|
||||||
MozGridGroup,
|
MozGridGroup,
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
#[css(parse_condition = "moz_display_values_enabled")]
|
#[parse(condition = "moz_display_values_enabled")]
|
||||||
MozGridLine,
|
MozGridLine,
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
#[css(parse_condition = "moz_display_values_enabled")]
|
#[parse(condition = "moz_display_values_enabled")]
|
||||||
MozStack,
|
MozStack,
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
#[css(parse_condition = "moz_display_values_enabled")]
|
#[parse(condition = "moz_display_values_enabled")]
|
||||||
MozInlineStack,
|
MozInlineStack,
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
#[css(parse_condition = "moz_display_values_enabled")]
|
#[parse(condition = "moz_display_values_enabled")]
|
||||||
MozDeck,
|
MozDeck,
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
#[css(parse_condition = "moz_display_values_enabled")]
|
#[parse(condition = "moz_display_values_enabled")]
|
||||||
MozPopup,
|
MozPopup,
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
#[css(parse_condition = "moz_display_values_enabled")]
|
#[parse(condition = "moz_display_values_enabled")]
|
||||||
MozGroupbox,
|
MozGroupbox,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ pub fn derive_to_animated_value(stream: TokenStream) -> TokenStream {
|
||||||
to_animated_value::derive(input).into()
|
to_animated_value::derive(input).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_derive(Parse, attributes(css))]
|
#[proc_macro_derive(Parse, attributes(css, parse))]
|
||||||
pub fn derive_parse(stream: TokenStream) -> TokenStream {
|
pub fn derive_parse(stream: TokenStream) -> TokenStream {
|
||||||
let input = syn::parse(stream).unwrap();
|
let input = syn::parse(stream).unwrap();
|
||||||
parse::derive(input).into()
|
parse::derive(input).into()
|
||||||
|
@ -64,7 +64,7 @@ pub fn derive_to_css(stream: TokenStream) -> TokenStream {
|
||||||
to_css::derive(input).into()
|
to_css::derive(input).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_derive(SpecifiedValueInfo, attributes(css, value_info))]
|
#[proc_macro_derive(SpecifiedValueInfo, attributes(css, parse, value_info))]
|
||||||
pub fn derive_specified_value_info(stream: TokenStream) -> TokenStream {
|
pub fn derive_specified_value_info(stream: TokenStream) -> TokenStream {
|
||||||
let input = syn::parse(stream).unwrap();
|
let input = syn::parse(stream).unwrap();
|
||||||
specified_value_info::derive(input).into()
|
specified_value_info::derive(input).into()
|
||||||
|
|
|
@ -4,10 +4,17 @@
|
||||||
|
|
||||||
use cg;
|
use cg;
|
||||||
use quote::Tokens;
|
use quote::Tokens;
|
||||||
use syn::DeriveInput;
|
use syn::{DeriveInput, Path};
|
||||||
use synstructure;
|
use synstructure;
|
||||||
use to_css::CssVariantAttrs;
|
use to_css::CssVariantAttrs;
|
||||||
|
|
||||||
|
#[darling(attributes(parse), default)]
|
||||||
|
#[derive(Default, FromVariant)]
|
||||||
|
pub struct ParseVariantAttrs {
|
||||||
|
pub aliases: Option<String>,
|
||||||
|
pub condition: Option<Path>,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn derive(input: DeriveInput) -> Tokens {
|
pub fn derive(input: DeriveInput) -> Tokens {
|
||||||
let name = &input.ident;
|
let name = &input.ident;
|
||||||
let s = synstructure::Structure::new(&input);
|
let s = synstructure::Structure::new(&input);
|
||||||
|
@ -20,18 +27,21 @@ pub fn derive(input: DeriveInput) -> Tokens {
|
||||||
"Parse is only supported for single-variant enums for now"
|
"Parse is only supported for single-variant enums for now"
|
||||||
);
|
);
|
||||||
|
|
||||||
let variant_attrs = cg::parse_variant_attrs_from_ast::<CssVariantAttrs>(&variant.ast());
|
let css_variant_attrs =
|
||||||
if variant_attrs.skip {
|
cg::parse_variant_attrs_from_ast::<CssVariantAttrs>(&variant.ast());
|
||||||
|
let parse_attrs =
|
||||||
|
cg::parse_variant_attrs_from_ast::<ParseVariantAttrs>(&variant.ast());
|
||||||
|
if css_variant_attrs.skip {
|
||||||
return match_body;
|
return match_body;
|
||||||
}
|
}
|
||||||
|
|
||||||
let identifier = cg::to_css_identifier(
|
let identifier = cg::to_css_identifier(
|
||||||
&variant_attrs.keyword.unwrap_or(variant.ast().ident.as_ref().into()),
|
&css_variant_attrs.keyword.unwrap_or(variant.ast().ident.as_ref().into()),
|
||||||
);
|
);
|
||||||
let ident = &variant.ast().ident;
|
let ident = &variant.ast().ident;
|
||||||
|
|
||||||
saw_condition |= variant_attrs.parse_condition.is_some();
|
saw_condition |= parse_attrs.condition.is_some();
|
||||||
let condition = match variant_attrs.parse_condition {
|
let condition = match parse_attrs.condition {
|
||||||
Some(ref p) => quote! { if #p(context) },
|
Some(ref p) => quote! { if #p(context) },
|
||||||
None => quote! { },
|
None => quote! { },
|
||||||
};
|
};
|
||||||
|
@ -41,7 +51,7 @@ pub fn derive(input: DeriveInput) -> Tokens {
|
||||||
#identifier #condition => Ok(#name::#ident),
|
#identifier #condition => Ok(#name::#ident),
|
||||||
};
|
};
|
||||||
|
|
||||||
let aliases = match variant_attrs.aliases {
|
let aliases = match parse_attrs.aliases {
|
||||||
Some(aliases) => aliases,
|
Some(aliases) => aliases,
|
||||||
None => return body,
|
None => return body,
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,6 +6,7 @@ use cg;
|
||||||
use quote::Tokens;
|
use quote::Tokens;
|
||||||
use syn::{Data, DeriveInput, Fields, Ident, Type};
|
use syn::{Data, DeriveInput, Fields, Ident, Type};
|
||||||
use to_css::{CssFieldAttrs, CssInputAttrs, CssVariantAttrs};
|
use to_css::{CssFieldAttrs, CssInputAttrs, CssVariantAttrs};
|
||||||
|
use parse::ParseVariantAttrs;
|
||||||
|
|
||||||
pub fn derive(mut input: DeriveInput) -> Tokens {
|
pub fn derive(mut input: DeriveInput) -> Tokens {
|
||||||
let css_attrs = cg::parse_input_attrs::<CssInputAttrs>(&input);
|
let css_attrs = cg::parse_input_attrs::<CssInputAttrs>(&input);
|
||||||
|
@ -33,10 +34,11 @@ pub fn derive(mut input: DeriveInput) -> Tokens {
|
||||||
for v in e.variants.iter() {
|
for v in e.variants.iter() {
|
||||||
let css_attrs = cg::parse_variant_attrs::<CssVariantAttrs>(&v);
|
let css_attrs = cg::parse_variant_attrs::<CssVariantAttrs>(&v);
|
||||||
let info_attrs = cg::parse_variant_attrs::<ValueInfoVariantAttrs>(&v);
|
let info_attrs = cg::parse_variant_attrs::<ValueInfoVariantAttrs>(&v);
|
||||||
|
let parse_attrs = cg::parse_variant_attrs::<ParseVariantAttrs>(&v);
|
||||||
if css_attrs.skip {
|
if css_attrs.skip {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let Some(aliases) = css_attrs.aliases {
|
if let Some(aliases) = parse_attrs.aliases {
|
||||||
for alias in aliases.split(",") {
|
for alias in aliases.split(",") {
|
||||||
values.push(alias.to_string());
|
values.push(alias.to_string());
|
||||||
}
|
}
|
||||||
|
|
|
@ -234,8 +234,6 @@ pub struct CssVariantAttrs {
|
||||||
pub comma: bool,
|
pub comma: bool,
|
||||||
pub dimension: bool,
|
pub dimension: bool,
|
||||||
pub keyword: Option<String>,
|
pub keyword: Option<String>,
|
||||||
pub aliases: Option<String>,
|
|
||||||
pub parse_condition: Option<Path>,
|
|
||||||
pub skip: bool,
|
pub skip: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ pub type KeywordsCollectFn<'a> = &'a mut FnMut(&[&'static str]);
|
||||||
/// name is listed in `collect_completion_keywords`.
|
/// name is listed in `collect_completion_keywords`.
|
||||||
/// * If `#[css(skip)]` is found, the content inside the variant or
|
/// * If `#[css(skip)]` is found, the content inside the variant or
|
||||||
/// field is ignored.
|
/// field is ignored.
|
||||||
/// * Values listed in `#[css(if_empty)]`, `#[css(aliases)]`, and
|
/// * Values listed in `#[css(if_empty)]`, `#[parse(aliases)]`, and
|
||||||
/// `#[css(keyword)]` are added into `collect_completion_keywords`.
|
/// `#[css(keyword)]` are added into `collect_completion_keywords`.
|
||||||
///
|
///
|
||||||
/// In addition to `css` attributes, it also has `value_info` helper
|
/// In addition to `css` attributes, it also has `value_info` helper
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue