[csswg-drafts] [Meta] Modular/distributed grammar definitions (#6744)

LeaVerou has just created a new issue for https://github.com/w3c/csswg-drafts:

== [Meta] Modular/distributed grammar definitions ==
There are a lot of cases where a certain value type / token consists of a disjunction of potential tokens, each defined in a separate section of the spec. However, there are still sections with "main" grammar definitions, that need to be updated every time new tokens are added or removed.

E.g.: Images:

```
<image> = <url> | <image()> | <image-set()> | <cross-fade()> | <element()> | <gradient>
```

Color 4:
```
<color> = <absolute-color-base> | currentcolor | <system-color> | <device-cmyk()>

<absolute-color-base> = <hex-color> | <named-color> | transparent |
    <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hwb()> |
    <lab()> | <lch()> |
 <oklab()> | <oklch()> |
    <color()>
```

[Color 5](https://drafts.csswg.org/css-color-5/#typedef-color-space):
```
<color-space> = srgb | hsl | hwb | xyz | lab | lch | oklab | oklch
```

This is a maintenance overhead for editors, and makes it more likely for these definitions to get out of sync as tokens are added or removed (case in point: I just added `oklab()` and `oklch()` to the `<color>` grammar because they had not been added there too).

It should be possible to define a subtype entirely by adding a section, without *also* having to update centralized grammars. This also makes it possible to extend types in separate specs or levels. 

Perhaps in addition to `=`, we could define a `|=` operator for grammars that means "whatever this token can be from the rest of the spec(s), plus these tokens". E.g., right now, [adding OKLab and OKLCH](https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-942473702) needed edits not just to add [their section](https://drafts.csswg.org/css-color-4/#specifying-oklab-oklch) but also to extend [`<color>`](https://drafts.csswg.org/css-color-4/#color-syntax) and in Color 5, to extend [`<color-space>`](https://drafts.csswg.org/css-color-5/#typedef-color-space).

If `|=` existed, the entirety of the grammar changes that these introduce could be self-contained in their section:

```
<absolute-color-base> |= <oklab()> | <oklch()>
<color-space> |= oklab | oklch

oklab() = oklab( [<percentage> | none] [<number> | none] [<number> | none] [ / [<alpha-value> | none] ]? )
oklch() = oklch( [<percentage> | none] [<number> | none] [<hue> | none] [ / [<alpha-value> | none] ]? )
```

One could make the point that these summary grammars are useful (though not sure how useful they can be if out of sync). However, Bikeshed could generate them, similarly to how we generated indices, and that way they are guaranteed to be in sync.

Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/6744 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Tuesday, 19 October 2021 12:21:30 UTC