[webkit-changes] [WebKit/WebKit] 02a34c: Add utilities to process-css-properties to make ge...

Sam Weinig noreply at github.com
Sat Nov 26 17:36:18 PST 2022


  Branch: refs/heads/eng/Add-support-for-unbounded-repetition-terms-in-the-new-property-parser-generator
  Home:   https://github.com/WebKit/WebKit
  Commit: 02a34cfc0d94f36198572c21cfe8b1507c12ac36
      https://github.com/WebKit/WebKit/commit/02a34cfc0d94f36198572c21cfe8b1507c12ac36
  Author: Sam Weinig <weinig at apple.com>
  Date:   2022-11-26 (Sat, 26 Nov 2022)

  Changed paths:
    M Source/WebCore/css/process-css-properties.py

  Log Message:
  -----------
  Add utilities to process-css-properties to make generation simpler
https://bugs.webkit.org/show_bug.cgi?id=248362
rdar://102682125

Reviewed by NOBODY (OOPS!).

Adds a few utilities to make code generation a bit more streamlined.

- `Writer` class replaces direct use of file output and now automatically
  handles newlines and indentation for callers. Includes a `context manager`,
  a python object that can be used with a `with` statement to scope some
  activity, call `Indent` to support easy scoped indentation.

  As an example. this snippet shows off both automatic newlines and the
  indentation context manager:

  ```
    writer.write("void foo(int x)")
    writer.write("{")
    with writer.indent():
        writer.write("use(x);")
    writer.write("}")
  ```

  and results in output of:

  ```
    void foo(int x)
    {
        use(x);
    }
  ```

- Additionally, the GenerationContext got some new utilities for creating common
  constructs like includes, forward declarations and namespaces. For namespaces,
  another context manager was added to allow automatic closing of the namespace
  when the `with` block ends.

The new utilities and Writer class still allow for use of custom whitespace where
it is more practical or creates more readable code, so some parts, notably parts
of StyleBuilderGenerated.cpp don't fully adopt the indentation mechanism (though
they do make use of the newline generation).

* Source/WebCore/css/process-css-properties.py:
(Writer):
(Writer.__init__):
(Writer._current_indent):
(Writer.write):
(Writer.write_block):
(Writer.write_lines):
(Writer.newline):
(Writer.Indent):
(Writer.Indent.__init__):
(Writer.Indent.__enter__):
(Writer.Indent.__exit__):
(Writer.indent):
(KeywordTerm.perform_fixups):
(MatchOneTerm.__init__):
(MatchOneTerm):
(MatchOneTerm.terms):
(MatchOneTerm.from_json):
(GenerationContext.generate_heading):
(GenerationContext):
(GenerationContext.generate_required_header_pragma):
(GenerationContext.generate_open_namespaces):
(GenerationContext.generate_close_namespaces):
(GenerationContext.generate_open_namespace):
(GenerationContext.generate_close_namespace):
(GenerationContext.Namespaces):
(GenerationContext.Namespaces.__init__):
(GenerationContext.Namespaces.__enter__):
(GenerationContext.Namespaces.__exit__):
(GenerationContext.namespace):
(GenerationContext.namespaces):
(GenerationContext.generate_using_namespace_declarations):
(GenerationContext.generate_includes):
(GenerationContext.generate_cpp_required_includes):
(GenerationContext.generate_forward_declarations):
(GenerationContext.generate_property_id_switch_function):
(GenerationContext.generate_property_id_switch_function_bool):
(GenerateCSSPropertyNames._generate_css_property_names_gperf_prelude):
(GenerateCSSPropertyNames):
(GenerateCSSPropertyNames._generate_css_property_names_gperf_heading): Deleted.


  Commit: 4eefce8617e9d525435c34a73d0d7d18dd8bbfe3
      https://github.com/WebKit/WebKit/commit/4eefce8617e9d525435c34a73d0d7d18dd8bbfe3
  Author: Sam Weinig <weinig at apple.com>
  Date:   2022-11-26 (Sat, 26 Nov 2022)

  Changed paths:
    M Source/WebCore/css/CSSProperties.json
    M Source/WebCore/css/parser/CSSPropertyParser.cpp
    M Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp
    M Source/WebCore/css/parser/CSSPropertyParserHelpers.h
    M Source/WebCore/css/process-css-properties.py
    M Tools/Scripts/webkitpy/style/checkers/jsonchecker.py

  Log Message:
  -----------
  Add support for unbounded repetition terms in the new property parser generator
https://bugs.webkit.org/show_bug.cgi?id=248355
rdar://102675501

Reviewed by NOBODY (OOPS!).

Adds support for parsing unbounded comma separated lists via CSSProperties.json.
Specifically, this adds support for the `#` operator without range refinement
(e.g. no #{1,4}):

  """
   A hash mark (#) indicates that the preceding type, word, or group occurs
   one or more times, separated by comma tokens (which may optionally be
   surrounded by white space and/or comments).
  """
    -  https://www.w3.org/TR/css-values-4/#mult-comma

This brings with it support for a large collection (29) of longhands in the
animation, transition, background and mask families of properties.

To generate the matching for one of these lists, a new term type can be
specified in CSSProperties.json with 'kind' set to 'repetition' and 'value'
set to whatever should be repeatedly matched. For instance, 'animation-duration'.
which has a specified grammar of:

  `<'animation-duration'> = <time [0s,∞]>`

uses the following:

  "parser-grammar": {
      "kind": "repetition",
      "value": "<time [0,inf]>",
      "single-value-optimization": true
  }

The single-value-optimization bit indicates that if only a single value is
present, it should be returned directly, rather than a CSSValueList with
one element. In all other valid parses, a CSSValueList is returned.

* Source/WebCore/css/CSSProperties.json:
Replaces use of custom-parser/parser-function with parser-grammar for the
newly supported properties and adds a set of new shared grammar rules for
implementing them.

* Source/WebCore/css/parser/CSSPropertyParser.cpp:
(WebCore::consumeCounterStyleRange): Adopt new consumeCommaSeparatedList* helper.
(WebCore::consumeCounterStyleAdditiveSymbols): Ditto.
(WebCore::consumeOverrideColorsDescriptor): Ditto.
(WebCore::isValidAnimationPropertyList): Moved from CSSPropertyParserHelpers as
now the only caller is in this file.
(WebCore::consumeAnimationValueForShorthand): Ditto. Also now implemented using
more exported shared grammar rules.
(WebCore::CSSPropertyParser::consumeAnimationShorthand): Adopt new
consumeCommaSeparatedList* helper
(WebCore::addBackgroundValue): Moved from CSSPropertyParserHelpers as now the only
caller is in this file.
(WebCore::CSSPropertyParser::consumeBackgroundShorthand): Split background-size and
mask size as they now have separate consume functions (though at the moment identical).

* Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp:
(WebCore::CSSPropertyParserHelpers::consumeFontVariationSettings): Adopt new
consumeCommaSeparatedList* helper.
(WebCore::CSSPropertyParserHelpers::consumeFontFamily): Ditto.
(WebCore::CSSPropertyParserHelpers::consumeKeyframesName): Added to allow "animation-name"
to be mostly generated. Unused CSSParserContext parameteter is a by product consumers called
from generated consumers are expected to take a CSSParserContext for the moment. This should
be refined to not be required in the future.
(WebCore::CSSPropertyParserHelpers::consumeSingleTransitionPropertyIdent):
(WebCore::CSSPropertyParserHelpers::consumeSingleTransitionPropertyOrNone):
(WebCore::CSSPropertyParserHelpers::consumeSingleTransitionProperty):
Split out shorthand and longhand behaviors into separate functions (with a shared
helper between them). Previously, the shorthand version (allowing "none") was used
in both places, and then a post consume check if any values were "none" was done.
Now that we are generating the repetition, doing the post consume check is non-trivial
so just doing the parse right makes more sense.
(WebCore::CSSPropertyParserHelpers::consumeSteps): Make static (and remove forward
declaration from header. Nothing outside this file uses this function.
(WebCore::CSSPropertyParserHelpers::consumeCubicBezier): Ditto.
(WebCore::CSSPropertyParserHelpers::consumeSpringFunction): Ditto.
(WebCore::CSSPropertyParserHelpers::consumeTimingFunction): Renamed from
consumeAnimationTimingFunction to better reflect the spec naming and to indicate it used
for more than just animation as it transtions also use it.
(WebCore::CSSPropertyParserHelpers::consumeShadow): Make static (and remove forward
declaration from header. Nothing outside this file uses this function.
(WebCore::CSSPropertyParserHelpers::consumeScrollSnapType): Use auto.
(WebCore::CSSPropertyParserHelpers::consumeBasicShapeOrBox): Use a generated helper and
add a note about the spec ambiguity along with link to spec github issue filed:
https://github.com/w3c/fxtf-drafts/issues/481
(WebCore::CSSPropertyParserHelpers::consumeBaselineKeyword): Make static (and remove forward
declaration from header. Nothing outside this file uses this function
(WebCore::CSSPropertyParserHelpers::consumeBackgroundSize): Make this a template function
generic on CSSPropertyID as the callers all statically know which variant they want.
(WebCore::CSSPropertyParserHelpers::consumeRepeatStyle): Make static (and remove forward
declaration from header. Nothing outside this file uses this function
(WebCore::CSSPropertyParserHelpers::consumeSingleBackgroundRepeat): Added to make it a bit
more clear in consumeBackgroundComponent() what the behavior for each property is.
(WebCore::CSSPropertyParserHelpers::consumeSingleBackgroundPositionX): Ditto.
(WebCore::CSSPropertyParserHelpers::consumeSingleBackgroundPositionY): Ditto.
(WebCore::CSSPropertyParserHelpers::consumeSingleBackgroundSize): Ditto.
(WebCore::CSSPropertyParserHelpers::consumeSingleMaskRepeat): Ditto.
(WebCore::CSSPropertyParserHelpers::consumeSingleMaskSize): Ditto.
(WebCore::CSSPropertyParserHelpers::consumeSingleWebkitBackgroundSize): Ditto.
(WebCore::CSSPropertyParserHelpers::consumeSingleWebkitMaskPositionX): Ditto.
(WebCore::CSSPropertyParserHelpers::consumeSingleWebkitMaskPositionY): Ditto.
(WebCore::CSSPropertyParserHelpers::consumeBackgroundComponent): Reordered to group families
of properties together and updated to use generated consumers where possible or the new
named consumers above.
(WebCore::CSSPropertyParserHelpers::consumeCommaSeparatedBackgroundComponent): Adopt new
consumeCommaSeparatedList* helper

(WebCore::CSSPropertyParserHelpers::consumeAutoOrLengthOrPercent): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeMarginSide): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeSide): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeAnimationIterationCount): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeAnimationName): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeTransitionProperty): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeAnimationTimingFunction): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeAnimationValue): Deleted.
(WebCore::CSSPropertyParserHelpers::isValidAnimationPropertyList): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeAnimationPropertyList): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeBackgroundBlendMode): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeBackgroundAttachment): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeBackgroundBox): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeMaskClip): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeBackgroundClip): Deleted.
(WebCore::CSSPropertyParserHelpers::consumePrefixedMaskComposite): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeMaskComposite): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeWebkitMaskSourceType): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeWebkitMaskMode): Deleted.
(WebCore::CSSPropertyParserHelpers::consumePrefixedBackgroundBox): Deleted.
(WebCore::CSSPropertyParserHelpers::addBackgroundValue): Deleted.
Replaced by generated shared grammar rules or generated properties..

* Source/WebCore/css/parser/CSSPropertyParserHelpers.h:
(WebCore::CSSPropertyParserHelpers::consumeCommaSeparatedListWithSingleValueOptimization):
(WebCore::CSSPropertyParserHelpers::consumeCommaSeparatedListWithoutSingleValueOptimization):
Add two helpers which can consume a comma separated list, one with an optimization to return
the underlying CSSValue if there was only one element in the list (and a CSSValueList otherwise)
and the other which always returns a CSSValueList. This allows callers to maintain their
existing behavior.

* Source/WebCore/css/process-css-properties.py:
(Term.from_json): Create a RepetitionTerm if the 'kind' is 'repetition'.
(RepetitionTerm): RepetitionTerm has a unique 'single-value-optimization' flag
that can be set on it to indicate that it should the variant of consumeCommaSeparatedList*
that does the single value optimizaton.
(TermGeneratorRepetitionTerm:) To generate code for a RepetitionTerm, we utilize one of the
consumeCommaSeparatedList* functions and pass it a lambda which contains the submatch. The
new automatic indentation support really came in handy here to allow an arbitrarty set of
terms to generate inside the lambda without issue.


Compare: https://github.com/WebKit/WebKit/compare/02a34cfc0d94%5E...4eefce8617e9


More information about the webkit-changes mailing list