-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(ast) add color_hdr support #61
Conversation
6946110
to
c6d4b39
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might serve as a good example of how to implement some values, and so I'll leave some commentary regarding this PR:
@@ -1 +1,2 @@ | |||
pub(crate) use crate::traits::StyleValue; | |||
pub(crate) use hdx_proc_macro::*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All impls.rs
need to include at least these two lines so that the macros work.
// https://drafts.csswg.org/css-color-hdr-1/#dynamic-range-limit | ||
#[value(" standard | high | constrained-high | <dynamic-range-limit-mix()> ")] | ||
#[initial("high")] | ||
#[applies_to("all elements")] | ||
#[inherited("no")] | ||
#[percentages("n/a")] | ||
#[canonical_order("per grammar")] | ||
#[animation_type("by dynamic-range-limit-mix()")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is alll autogenerated. It doesn't need changing other than oncommenting.
#[percentages("n/a")] | ||
#[canonical_order("per grammar")] | ||
#[animation_type("by dynamic-range-limit-mix()")] | ||
pub enum DynamicRangeLimit<'a> {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A small change is needed here: the <'a>
lifetime is needed because this enum depends on DynamicRangeLimitMix
which itself has <'a>
(because it depends on a Vec<'a>
).
pub(crate) use crate::css::types::*; | ||
pub(crate) use crate::css::units::*; | ||
use crate::css::units::LengthPercentage; | ||
use bumpalo::collections::Vec; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use use bumpalo::collections::Vec
rather than std::vec::Vec
so we can use bump allocations. They work the same other than bumpalo's vec requires a lifetime, so Vec<'a, T>
rather than Vec<T>
, in addition rather than Vec::new()
we need Vec::new_in(bump)
.
mod func { | ||
use hdx_parser::custom_function; | ||
custom_function!(DynamicRangeLimitMix, atom!("dynamic-range-limit-mix")); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When comparing just one or two functions or idents, it can be more convenient to use custom_function!
or custom_keyword!
macros, which generate a mini parsable struct. They get a little unweildy if using many, so only use sparingly. I may end up removing this, depending on how much it gets used overall.
} | ||
} | ||
|
||
impl<'a> Parse<'a> for DynamicRangeLimitMix<'a> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every AST Node needs the Parse
trait which is given a parser and can repeatedly parse other tokens or trees until it can complet its own node.
} | ||
} | ||
|
||
impl<'a> ToCursors<'a> for DynamicRangeLimitMix<'a> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every AST node must also implement the ToCursors
trait which is given a CursorStream
and must feed its cursors, in order, to the cursor stream. This then lets us write the cursor stream back out to a string.
|
||
impl<'a> ToCursors<'a> for DynamicRangeLimitMix<'a> { | ||
fn to_cursors(&self, s: &mut CursorStream<'a>) { | ||
s.append(self.function.into()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like a T![Function]
is a "newtyped" Cursor, so calling .into()
should convert it into a Cursor. For things made up of multiple cursors you'll need to call ToCursors::to_cursors(&self.the_thing, s)
.
#[test] | ||
fn size_test() { | ||
assert_size!(DynamicRangeLimitMix, 56); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Important to track sizes of all structs so we can figure out places to compress or align or otherwise optimize hot nodes.
@@ -1350,8 +1350,8 @@ impl DefType { | |||
|
|||
pub fn requires_allocator_lifetime(&self) -> bool { | |||
if let Self::Custom(DefIdent(ident), _) = self { | |||
return matches!(ident, &atom!("OutlineColor") | &atom!("BorderTopColor") | &atom!("AnchorName")); | |||
} | |||
return matches!(ident, &atom!("OutlineColor") | &atom!("BorderTopColor") | &atom!("AnchorName") | &atom!("DynamicRangeLimitMix")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because DynamicRangeLimitMix
required the <'a>
lifetime, it needs to be added here. This is... not ideal, but it works for now. The generator needs to be smarter.
This adds supports for the CSS color-hdr, by commenting out the
DynamicRangeLimit
enum and adding a newDynamicRangeLimitMix
struct in thecrates/hdx_ast/src/css/values/color_hdr/types.rs
file.DynamicRangeLimitMix is a struct that represents the
dynamic-range-limit-mix()
function in CSS.