Skip to content

Combine parsing into legacy and new parsing. #328

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

Merged
merged 2 commits into from
Mar 9, 2023

Conversation

tiaanl
Copy link
Collaborator

@tiaanl tiaanl commented Mar 9, 2023

Combine the rules of legacy and new parsing into single functions. This allows for a much clearer separation of which keywords support which syntax. RGB is still special, because it the components must be of the same type (numbers or percentages). Most of the color functions are using the new syntax, but some special notes:

  • RGB uses it's own parser as mentioned.
  • HSL supports both legacy and new syntax.
  • HWB only supports new suntax.
  • _ the rest supports the new syntax only.

These changes are also in preparation of introducing the none keyword where it is much more important to know which syntax is being used, as legacy syntax does not support the none keyword.

Copy link
Member

@emilio emilio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the parse_components split. If this PR is on its own was the final state, I'd insist on having a simple parse_components with an allow_legacy argument, same for parse_alpha with uses_commas.

But if you somehow require the split later down the road and this makes it easier for you, then this is fine with the nits below.

let r2 = f2(color_parser, input)?;
let r3 = f3(color_parser, input)?;

let alpha = if !input.is_exhausted() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems we could keep having a single parse_alpha, but pass /* uses_commas = */ false here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We know what to expect at compile time here, so I like this for performance. Also, when none is implemented, the legacy syntax will return f32, but for new syntax it will be Option<f32>.

/// Try to parse the components and alpha with the legacy syntax, but also allow
/// the [color-4] syntax if that fails.
/// https://drafts.csswg.org/css-color-4/#color-syntax-legacy
pub fn parse_legacy_components<'i, 't, P, F1, F2, F3, R1, R2, R3>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe parse_components_allow_legacy would be a better name for this?

(But then again we could have a single parse_components with allow_legacy: bool). I assume this makes it easier to handle none in the future somehow tho, so this is fine as an interim step.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, adding none complicates things, because the legacy syntax doesn't allow none. But also, because if the function supports legacy, then the newer syntax for that same function doesn't allow none either. So adding the allow_legacy turns into an if explosion.

@tiaanl
Copy link
Collaborator Author

tiaanl commented Mar 9, 2023

As mentioned most of this will be clearer once none comes. Would be happy to do a final cleanup once everything is merged.

@emilio
Copy link
Member

emilio commented Mar 9, 2023

@bors-servo r+

Okay, let's try this. if we care about the extra branches when we know we can't parse legacy syntax, I think a template argument const ALLOW_LEGACY: bool instead of two functions should make sure the compiler can optimize those out (if it can't already).

But anyways let's do this for now and we can reevaluate as we implement none.

@bors-servo
Copy link
Contributor

📌 Commit 85463cd has been approved by emilio

@bors-servo
Copy link
Contributor

⌛ Testing commit 85463cd with merge caa0076...

@bors-servo
Copy link
Contributor

☀️ Test successful - checks-github
Approved by: emilio
Pushing caa0076 to master...

@bors-servo bors-servo merged commit caa0076 into servo:master Mar 9, 2023
@bors-servo bors-servo mentioned this pull request Mar 9, 2023
@tiaanl tiaanl deleted the new-parsing branch March 9, 2023 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants