File: CSSPropertyParserConsumer%2BMetaConsumer.h

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (222 lines) | stat: -rw-r--r-- 9,639 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright (C) 2024-2025 Samuel Weinig <sam@webkit.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#include "CSSParserToken.h"
#include "CSSParserTokenRange.h"
#include "CSSPrimitiveNumericTypes.h"
#include "CSSPropertyParserConsumer+MetaConsumerDefinitions.h"
#include "CSSPropertyParserOptions.h"
#include "StylePrimitiveNumericTypes.h"
#include <optional>
#include <type_traits>
#include <wtf/Brigand.h>
#include <wtf/StdLibExtras.h>

namespace WebCore {

enum CSSParserMode : uint8_t;
enum class ValueRange : uint8_t;

namespace CSSPropertyParserHelpers {

// MARK: - Meta Consumers

/// The result of a meta consume.
/// To be used with a list of `CSS` types (e.g. `ConsumeResult<CSS::Angle<Range>, CSS::Percentage<Range>, CSS::Keyword::None>`), which will yield a
/// result type of either a std::variant of those types (e.g.`std::variant<CSS::Angle<Range>, CSS::Percentage<Range>, CSS::Keyword::None>`) or the type
/// itself if only a single type was specified.
template<typename... Ts>
struct MetaConsumeResult {
    using TypeList = brigand::list<Ts...>;
    using type = VariantOrSingle<TypeList>;
};

template<CSSParserTokenType tokenType, typename Consumer, typename = void>
struct MetaConsumerDispatcher {
    static constexpr bool supported = false;
};

template<typename Consumer>
struct MetaConsumerDispatcher<FunctionToken, Consumer, typename std::void_t<typename Consumer::FunctionToken>> {
    static constexpr bool supported = true;
    template<typename... Args>
    static decltype(auto) consume(Args&&... args)
    {
        return Consumer::FunctionToken::consume(std::forward<Args>(args)...);
    }
};

template<typename Consumer>
struct MetaConsumerDispatcher<NumberToken, Consumer, typename std::void_t<typename Consumer::NumberToken>> {
    static constexpr bool supported = true;
    template<typename... Args>
    static decltype(auto) consume(Args&&... args)
    {
        return Consumer::NumberToken::consume(std::forward<Args>(args)...);
    }
};

template<typename Consumer>
struct MetaConsumerDispatcher<PercentageToken, Consumer, typename std::void_t<typename Consumer::PercentageToken>> {
    static constexpr bool supported = true;
    template<typename... Args>
    static decltype(auto) consume(Args&&... args)
    {
        return Consumer::PercentageToken::consume(std::forward<Args>(args)...);
    }
};

template<typename Consumer>
struct MetaConsumerDispatcher<DimensionToken, Consumer, typename std::void_t<typename Consumer::DimensionToken>> {
    static constexpr bool supported = true;
    template<typename... Args>
    static decltype(auto) consume(Args&&... args)
    {
        return Consumer::DimensionToken::consume(std::forward<Args>(args)...);
    }
};

template<typename Consumer>
struct MetaConsumerDispatcher<IdentToken, Consumer, typename std::void_t<typename Consumer::IdentToken>> {
    static constexpr bool supported = true;
    template<typename... Args>
    static decltype(auto) consume(Args&&... args)
    {
        return Consumer::IdentToken::consume(std::forward<Args>(args)...);
    }
};

// The `MetaConsumerUnroller` gives each type in the consumer list (`Ts...`)
// a chance to consume the token. It recursively peels off types from the
// type list, checks if the consumer supports this token type, and then calls
// to the MetaConsumerDispatcher to actually call right `consume` function.

// Empty case, used to indicate no more types remain to try.
template<typename... Ts>
struct MetaConsumerUnroller {
    template<CSSParserTokenType, typename ResultType>
    static std::nullopt_t consume(CSSParserTokenRange&, const CSSParserContext&, CSSCalcSymbolsAllowed, CSSPropertyParserOptions)
    {
        return std::nullopt;
    }

    template<CSSParserTokenType, typename ResultType, typename F>
    static std::nullopt_t consume(CSSParserTokenRange&, const CSSParserContext&, CSSCalcSymbolsAllowed, CSSPropertyParserOptions, NOESCAPE F&&)
    {
        return std::nullopt;
    }
};

// Actionable case, checks if the `Consumer` defined for type `T` supports the
// current token, trying to consume if it does, and in either case, falling
// back to recursively trying the same on the remainder of the type list `Ts...`.
template<typename T, typename... Ts>
struct MetaConsumerUnroller<T, Ts...> {
    template<CSSParserTokenType tokenType, typename ResultType>
    static std::optional<ResultType> consume(CSSParserTokenRange& range, const CSSParserContext& context, CSSCalcSymbolsAllowed symbolsAllowed, CSSPropertyParserOptions options)
    {
        using Consumer = MetaConsumerDispatcher<tokenType, ConsumerDefinition<T>>;
        if constexpr (Consumer::supported) {
            if (auto result = Consumer::consume(range, context, symbolsAllowed, options))
                return { T { *result } };
        }
        return MetaConsumerUnroller<Ts...>::template consume<tokenType, ResultType>(range, context, symbolsAllowed, options);
    }

    template<CSSParserTokenType tokenType, typename ResultType, typename F>
    static std::optional<ResultType> consume(CSSParserTokenRange& range, const CSSParserContext& context, CSSCalcSymbolsAllowed symbolsAllowed, CSSPropertyParserOptions options, NOESCAPE F&& functor)
    {
        using Consumer = MetaConsumerDispatcher<tokenType, ConsumerDefinition<T>>;
        if constexpr (Consumer::supported) {
            if (auto result = Consumer::consume(range, context, symbolsAllowed, options))
                return std::make_optional(functor(T { *result }));
        }
        return MetaConsumerUnroller<Ts...>::template consume<tokenType, ResultType>(range, context, symbolsAllowed, options, std::forward<F>(functor));
    }
};

// The `MetaConsumer` is the main driver of token consumption, dispatching
// to a `MetaConsumerUnroller` based on token type. Caller use this directly.
// An example use that attempts to consumer either a <number> or <percentage>
// looks like (argument list elided for brevity):
//
//    auto result = MetaConsumer<CSS::Percentage<R>, CSS::Number<R>>::consume(range, ...);
//
// If a caller wants to avoid the overhead of switching on the returned variant
// result, an alternative overload of `consume` is provided which takes an additional
// `functor` argument which gets called with the result:
//
//    auto result = MetaConsumer<CSS::Percentage<R>, CSS::Number<R>>::consume(range, ...,
//        [](CSS::Percentage<R> percentage) { ... },
//        [](CSS::Number<R> number) { ... }
//    );
template<typename T, typename... Ts>
struct MetaConsumer {
    static_assert(WTF::all<HasConsumerDefinition::check<T>(), HasConsumerDefinition::check<Ts>()...>);

    using Unroller = MetaConsumerUnroller<T, Ts...>;

    template<typename... F>
    static decltype(auto) consume(CSSParserTokenRange& range, const CSSParserContext& context, CSSCalcSymbolsAllowed symbolsAllowed, CSSPropertyParserOptions options, F&&... f)
    {
        auto visitor = WTF::makeVisitor(std::forward<F>(f)...);
        using ResultType = decltype(visitor(std::declval<T>()));

        switch (range.peek().type()) {
        case FunctionToken:
            return Unroller::template consume<FunctionToken, ResultType>(range, context, WTFMove(symbolsAllowed), options, visitor);

        case NumberToken:
            return Unroller::template consume<NumberToken, ResultType>(range, context, WTFMove(symbolsAllowed), options, visitor);

        case PercentageToken:
            return Unroller::template consume<PercentageToken, ResultType>(range, context, WTFMove(symbolsAllowed), options, visitor);

        case DimensionToken:
            return Unroller::template consume<DimensionToken, ResultType>(range, context, WTFMove(symbolsAllowed), options, visitor);

        case IdentToken:
            return Unroller::template consume<IdentToken, ResultType>(range, context, WTFMove(symbolsAllowed), options, visitor);

        default:
            return std::optional<ResultType> { };
        }
    }

    static decltype(auto) consume(CSSParserTokenRange& range, const CSSParserContext& context, CSSCalcSymbolsAllowed symbolsAllowed, CSSPropertyParserOptions options)
    {
        using ResultType = typename MetaConsumeResult<T, Ts...>::type;

        return consume(range, context, WTFMove(symbolsAllowed), options,
            [](auto&& value) {
                return ResultType { WTFMove(value) };
            }
        );
    }
};

} // namespace CSSPropertyParserHelpers
} // namespace WebCore