Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
pss_compact.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/detail/pss_parser.hpp>
6
10inline constexpr std::size_t MAX_CT_PSS_BYTES = 16 * 1024;
12inline constexpr std::size_t MAX_CT_PSS_RULES = 256;
14inline constexpr std::size_t MAX_CT_PSS_CLASSES = 1024;
16inline constexpr std::size_t MAX_CT_PSS_DECLARATIONS = 1536;
18inline constexpr std::size_t MAX_CT_PSS_TEXT_BYTES = 16 * 1024;
20inline constexpr std::size_t MAX_CT_PSS_SOURCE_NAME_BYTES = 512;
22inline constexpr std::size_t MAX_CT_PSS_DESCRIPTOR_BYTES = 192 * 1024;
23
24namespace detail {
25using portals::detail::pssError;
27using portals::detail::PssPolicy;
30
32struct PssText {
33 std::uint32_t offset{};
34 std::uint32_t size{};
35};
36
39 std::uint32_t offset{};
40 std::uint32_t line{1};
41 std::uint32_t column{1};
42};
43
45inline constexpr auto NO_COMPACT_DECLARATION = std::numeric_limits<std::uint16_t>::max();
46static_assert(MAX_CT_PSS_DECLARATIONS < NO_COMPACT_DECLARATION);
47
52 std::uint32_t classBegin{};
53 std::uint32_t classCount{};
55 bool readOnly{};
57 std::uint32_t declarationBegin{};
58 std::uint32_t declarationCount{};
59 std::array<std::uint16_t, PSS_SCALAR_PROPERTY_COUNT> effectiveDeclaration = [] {
60 std::array<std::uint16_t, PSS_SCALAR_PROPERTY_COUNT> result{};
61 result.fill(NO_COMPACT_DECLARATION);
62
63 return result;
64 }();
65};
66
74
78constexpr PssPosition compactPosition(PssLocation location) {
79 return {static_cast<std::uint32_t>(location.offset), static_cast<std::uint32_t>(location.line),
80 static_cast<std::uint32_t>(location.column)};
81}
82
86constexpr PssLocation expandPosition(PssPosition location) {
87 return {location.offset, location.line, location.column};
88}
89
91struct PssCounts {
92 std::size_t rules{};
93 std::size_t classes{};
94 std::size_t declarations{};
95 std::size_t bytes{};
96};
97
101
105 constexpr std::expected<void, PssError> beginRule(PssLocation location) {
107 return std::unexpected(pssError(PssErrorCode::RULE_LIMIT, location, "Too many compile-time style rules"));
108 }
109
110 ++counts.rules;
111
112 return {};
113 }
114
119 constexpr std::expected<void, PssError> type(PssSpan span, PssLocation location) {
120 return text(span.size, location);
121 }
122
127 constexpr std::expected<void, PssError> id(PssSpan span, PssLocation location) {
128 return text(span.size, location);
129 }
130
135 constexpr std::expected<void, PssError> className(PssSpan span, PssLocation location) {
137 return std::unexpected(pssError(PssErrorCode::CLASS_LIMIT, location, "Too many compile-time selector classes"));
138 }
139
140 ++counts.classes;
141
142 return text(span.size, location);
143 }
144
146 constexpr std::expected<void, PssError> readOnly() {
147 return {};
148 }
149
153 constexpr std::expected<void, PssError> state(StyleState state) {
154 (void)state;
155
156 return {};
157 }
158
164 constexpr std::expected<void, PssError> declaration(PssProperty property, PssValue value, PssLocation location) {
165 if (static_cast<std::size_t>(property) >= PSS_SCALAR_PROPERTY_COUNT) {
166 return std::unexpected(pssError(PssErrorCode::INVALID_VALUE, location, "Property requires runtime PSS"));
167 }
168
169 (void)property;
170 (void)value;
171
173 return std::unexpected(
174 pssError(PssErrorCode::DECLARATION_LIMIT, location, "Too many compile-time declaration occurrences"));
175 }
176
178
179 return {};
180 }
181
183 constexpr std::expected<void, PssError> endRule() {
184 return {};
185 }
186
191 constexpr std::expected<void, PssError> text(std::size_t bytes, PssLocation location) {
192 if (bytes > MAX_CT_PSS_TEXT_BYTES - counts.bytes) {
193 return std::unexpected(
194 pssError(PssErrorCode::TEXT_LIMIT, location, "Compile-time PSS exceeds owning text budget"));
195 }
196
197 counts.bytes += bytes;
198
199 return {};
200 }
201};
202
208template <std::size_t R, std::size_t C, std::size_t D, std::size_t B> class PssCompactSink;
209} // namespace detail
210
218template <std::size_t RuleCapacity, std::size_t ClassCapacity, std::size_t DeclarationCapacity,
219 std::size_t ByteCapacity>
221 static_assert(RuleCapacity <= MAX_CT_PSS_RULES && ClassCapacity <= MAX_CT_PSS_CLASSES &&
222 DeclarationCapacity <= MAX_CT_PSS_DECLARATIONS && ByteCapacity <= MAX_CT_PSS_TEXT_BYTES,
223 "Compact stylesheet capacities exceed CT PSS budgets");
224
225public:
227 constexpr CompactSheet() = default;
228
230 constexpr std::span<const detail::PssCompactRule> rules() const noexcept {
231 return {rules_.data(), ruleCount_};
232 }
233
235 constexpr std::span<const detail::PssText> classes() const noexcept {
236 return {classes_.data(), classCount_};
237 }
238
240 constexpr std::span<const detail::PssCompactDeclaration> declarations() const noexcept {
241 return {declarations_.data(), declarationCount_};
242 }
243
247 constexpr std::string_view text(detail::PssText range) const noexcept {
248 if (range.size == 0) {
249 return {};
250 }
251
252 return {bytes_.data() + range.offset, range.size};
253 }
254
256 constexpr std::string_view sourceName() const noexcept {
257 return text(sourceName_);
258 }
259
261 constexpr PssOriginKind originKind() const noexcept {
262 return originKind_;
263 }
264
266 constexpr std::size_t ownedTextBytes() const noexcept {
267 return byteCount_;
268 }
269
270private:
271 template <std::size_t R, std::size_t C, std::size_t D, std::size_t B> friend class detail::PssCompactSink;
272
273 std::array<detail::PssCompactRule, RuleCapacity> rules_{};
274 std::array<detail::PssText, ClassCapacity> classes_{};
275 std::array<detail::PssCompactDeclaration, DeclarationCapacity> declarations_{};
276 std::array<char, ByteCapacity> bytes_{};
277 detail::PssText sourceName_;
279 std::size_t ruleCount_{};
280 std::size_t classCount_{};
281 std::size_t declarationCount_{};
282 std::size_t byteCount_{};
283};
284
285namespace detail {
292template <std::size_t R, std::size_t C, std::size_t D, std::size_t B> class PssCompactSink {
293public:
297 constexpr PssCompactSink(std::string_view source, CompactSheet<R, C, D, B> &sheet) : source_(source), sheet_(sheet) {
299 "Compact stylesheet object exceeds descriptor budget");
300 }
301
305 constexpr std::expected<void, PssError> sourceName(std::string_view name) {
306 return copyText(name, {}, sheet_.sourceName_);
307 }
308
310 constexpr void originKind(PssOriginKind kind) {
311 sheet_.originKind_ = kind;
312 }
313
317 constexpr std::expected<void, PssError> beginRule(PssLocation location) {
318 if (sheet_.ruleCount_ == R) {
319 return std::unexpected(pssError(PssErrorCode::RULE_LIMIT, location, "Too many compile-time style rules"));
320 }
321
322 auto &rule = current();
323
324 rule.selector = compactPosition(location);
325 rule.classBegin = static_cast<std::uint32_t>(sheet_.classCount_);
326 rule.declarationBegin = static_cast<std::uint32_t>(sheet_.declarationCount_);
327
328 return {};
329 }
330
335 constexpr std::expected<void, PssError> type(PssSpan span, PssLocation location) {
336 return typeName(source_.substr(span.offset, span.size), location);
337 }
338
343 constexpr std::expected<void, PssError> id(PssSpan span, PssLocation location) {
344 return idName(source_.substr(span.offset, span.size), location);
345 }
346
351 constexpr std::expected<void, PssError> className(PssSpan span, PssLocation location) {
352 return classBytes(source_.substr(span.offset, span.size), location);
353 }
354
359 constexpr std::expected<void, PssError> typeName(std::string_view bytes, PssLocation location) {
360 return copyText(bytes, location, current().type);
361 }
362
367 constexpr std::expected<void, PssError> idName(std::string_view bytes, PssLocation location) {
368 return copyText(bytes, location, current().id);
369 }
370
375 constexpr std::expected<void, PssError> classBytes(std::string_view bytes, PssLocation location) {
376 if (sheet_.classCount_ == C) {
377 return std::unexpected(pssError(PssErrorCode::CLASS_LIMIT, location, "Too many compile-time selector classes"));
378 }
379
380 auto admitted = copyText(bytes, location, sheet_.classes_[sheet_.classCount_]);
381
382 if (!admitted) {
383 return admitted;
384 }
385
386 ++sheet_.classCount_;
387 ++current().classCount;
388
389 return {};
390 }
391
393 constexpr std::expected<void, PssError> readOnly() {
394 current().readOnly = true;
395 return {};
396 }
397
401 constexpr std::expected<void, PssError> state(StyleState value) {
402 current().state = value;
403
404 return {};
405 }
406
412 constexpr std::expected<void, PssError> declaration(PssProperty property, PssValue value, PssLocation location) {
413 if (static_cast<std::size_t>(property) >= PSS_SCALAR_PROPERTY_COUNT) {
414 return std::unexpected(pssError(PssErrorCode::INVALID_VALUE, location, "Property requires runtime PSS"));
415 }
416
417 if (sheet_.declarationCount_ == D) {
418 return std::unexpected(
419 pssError(PssErrorCode::DECLARATION_LIMIT, location, "Too many compile-time declaration occurrences"));
420 }
421
422 auto &rule = current();
423
424 rule.effectiveDeclaration[static_cast<std::size_t>(property)] =
425 static_cast<std::uint16_t>(sheet_.declarationCount_);
426 ++rule.declarationCount;
427 sheet_.declarations_[sheet_.declarationCount_++] = {static_cast<std::uint32_t>(sheet_.ruleCount_), property,
428 compactPosition(location), value};
429
430 return {};
431 }
432
434 constexpr std::expected<void, PssError> endRule() {
435 ++sheet_.ruleCount_;
436
437 return {};
438 }
439
445 constexpr std::expected<void, PssError> copyText(std::string_view bytes, PssLocation location, PssText &destination) {
446 if (bytes.size() > B - sheet_.byteCount_) {
447 return std::unexpected(
448 pssError(PssErrorCode::TEXT_LIMIT, location, "Compile-time PSS exceeds owning text budget"));
449 }
450
451 destination = {static_cast<std::uint32_t>(sheet_.byteCount_), static_cast<std::uint32_t>(bytes.size())};
452
453 for (const auto value : bytes) {
454 sheet_.bytes_[sheet_.byteCount_++] = value;
455 }
456
457 return {};
458 }
459
467 template <std::size_t SourceR, std::size_t SourceC, std::size_t SourceD, std::size_t SourceB>
468 constexpr std::expected<void, PssError> copyFrom(const CompactSheet<SourceR, SourceC, SourceD, SourceB> &source) {
469 if (source.ruleCount_ > R || source.classCount_ > C || source.declarationCount_ > D || source.byteCount_ > B) {
470 return std::unexpected(pssError(PssErrorCode::DOCUMENT_LIMIT, {}, "Compact PSS copy exceeds admitted capacity"));
471 }
472
473 sheet_.ruleCount_ = source.ruleCount_;
474 sheet_.classCount_ = source.classCount_;
475 sheet_.declarationCount_ = source.declarationCount_;
476 sheet_.byteCount_ = source.byteCount_;
477 sheet_.sourceName_ = source.sourceName_;
478 sheet_.originKind_ = source.originKind_;
479
480 for (std::size_t index = 0; index < source.ruleCount_; ++index) {
481 sheet_.rules_[index] = source.rules_[index];
482 }
483
484 for (std::size_t index = 0; index < source.classCount_; ++index) {
485 sheet_.classes_[index] = source.classes_[index];
486 }
487
488 for (std::size_t index = 0; index < source.declarationCount_; ++index) {
489 sheet_.declarations_[index] = source.declarations_[index];
490 }
491
492 for (std::size_t index = 0; index < source.byteCount_; ++index) {
493 sheet_.bytes_[index] = source.bytes_[index];
494 }
495
496 return {};
497 }
498
499private:
500 constexpr PssCompactRule &current() {
501 return sheet_.rules_[sheet_.ruleCount_];
502 }
503
504 std::string_view source_;
506};
507} // namespace detail
508} // namespace portals::style::ct
Shared allocation-free grammar kernel; sink calls are synchronous and may throw.
Definition pss_parser.hpp:368
Immutable owning scalar stylesheet; its public type contains only numeric capacities.
Definition pss_compact.hpp:220
constexpr std::span< const detail::PssText > classes() const noexcept
Definition pss_compact.hpp:235
constexpr std::string_view sourceName() const noexcept
Definition pss_compact.hpp:256
constexpr CompactSheet()=default
Construct a valid empty owning description.
constexpr std::span< const detail::PssCompactDeclaration > declarations() const noexcept
Definition pss_compact.hpp:240
constexpr std::size_t ownedTextBytes() const noexcept
Definition pss_compact.hpp:266
constexpr std::span< const detail::PssCompactRule > rules() const noexcept
Definition pss_compact.hpp:230
constexpr std::string_view text(detail::PssText range) const noexcept
Definition pss_compact.hpp:247
constexpr PssOriginKind originKind() const noexcept
Definition pss_compact.hpp:261
Allocation-free storing sink writing one bounded owning descriptor.
Definition pss_compact.hpp:292
constexpr void originKind(PssOriginKind kind)
Definition pss_compact.hpp:310
constexpr std::expected< void, PssError > idName(std::string_view bytes, PssLocation location)
Definition pss_compact.hpp:367
constexpr std::expected< void, PssError > beginRule(PssLocation location)
Definition pss_compact.hpp:317
constexpr std::expected< void, PssError > readOnly()
Definition pss_compact.hpp:393
constexpr std::expected< void, PssError > classBytes(std::string_view bytes, PssLocation location)
Definition pss_compact.hpp:375
constexpr std::expected< void, PssError > type(PssSpan span, PssLocation location)
Definition pss_compact.hpp:335
constexpr std::expected< void, PssError > className(PssSpan span, PssLocation location)
Definition pss_compact.hpp:351
constexpr std::expected< void, PssError > id(PssSpan span, PssLocation location)
Definition pss_compact.hpp:343
constexpr std::expected< void, PssError > declaration(PssProperty property, PssValue value, PssLocation location)
Definition pss_compact.hpp:412
constexpr std::expected< void, PssError > typeName(std::string_view bytes, PssLocation location)
Definition pss_compact.hpp:359
constexpr std::expected< void, PssError > sourceName(std::string_view name)
Definition pss_compact.hpp:305
constexpr std::expected< void, PssError > copyText(std::string_view bytes, PssLocation location, PssText &destination)
Definition pss_compact.hpp:445
constexpr std::expected< void, PssError > copyFrom(const CompactSheet< SourceR, SourceC, SourceD, SourceB > &source)
Definition pss_compact.hpp:468
constexpr std::expected< void, PssError > endRule()
Definition pss_compact.hpp:434
constexpr PssCompactSink(std::string_view source, CompactSheet< R, C, D, B > &sheet)
Definition pss_compact.hpp:297
constexpr std::expected< void, PssError > state(StyleState value)
Definition pss_compact.hpp:401
Owning compile-time PSS v0 snapshots using the existing runtime cascade.
Definition pss_compact.hpp:8
constexpr std::size_t MAX_CT_PSS_DESCRIPTOR_BYTES
Maximum complete compact descriptor object size.
Definition pss_compact.hpp:22
consteval auto sheet(P values, R... rules)
Definition style_static.hpp:1380
consteval auto rule(S target, D... declarations)
Definition style_static.hpp:1366
constexpr std::size_t MAX_CT_PSS_SOURCE_NAME_BYTES
Maximum caller-supplied compile-time UTF-8 source identity bytes.
Definition pss_compact.hpp:20
constexpr std::size_t MAX_CT_PSS_BYTES
Maximum compile-time source bytes, independently of the larger runtime budget.
Definition pss_compact.hpp:10
constexpr std::size_t MAX_CT_PSS_TEXT_BYTES
Maximum shared owning selector and source-identity text bytes.
Definition pss_compact.hpp:18
constexpr std::size_t MAX_CT_PSS_CLASSES
Maximum compile-time class predicates across the whole sheet.
Definition pss_compact.hpp:14
constexpr std::size_t MAX_CT_PSS_DECLARATIONS
Maximum compile-time declaration occurrences, including overwritten duplicates.
Definition pss_compact.hpp:16
constexpr std::size_t MAX_CT_PSS_RULES
Maximum compile-time source-order rules.
Definition pss_compact.hpp:12
PssProperty
PSS v0 declaration identities in metadata and compact descriptions.
Definition pss_data.hpp:13
constexpr std::size_t PSS_SCALAR_PROPERTY_COUNT
Scalar-only prefix retained by compile-time PSS descriptors.
Definition pss_data.hpp:136
StyleState
Presentation states; ANY matches every state without changing behavior.
Definition style.hpp:26
@ ANY
Match all presentation variants.
Definition style.hpp:27
PssOriginKind
Coordinates describe either original PSS bytes or explicit typed ordinals.
Definition pss_data.hpp:147
@ PSS_BYTES
Actual offsets, LF lines, and byte columns in original PSS input.
Definition pss_data.hpp:148
@ RULE_LIMIT
Source-order rule count exceeds the selected budget.
Definition pss_data.hpp:156
@ DOCUMENT_LIMIT
Runtime owning payload exceeds its logical budget.
Definition pss_data.hpp:160
@ CLASS_LIMIT
Compact class predicates exceed their budget.
Definition pss_data.hpp:157
@ DECLARATION_LIMIT
Compact declaration occurrences exceed their budget.
Definition pss_data.hpp:158
@ INVALID_VALUE
Value does not have the property's exact scalar syntax.
Definition pss_data.hpp:172
@ TEXT_LIMIT
Compact owning text exceeds its shared arena.
Definition pss_data.hpp:159
Position in original PSS bytes, including an accepted leading BOM.
Definition pss_data.hpp:178
std::size_t offset
Zero-based byte offset.
Definition pss_data.hpp:179
std::size_t line
One-based line, advanced only by LF.
Definition pss_data.hpp:180
std::size_t column
One-based byte column.
Definition pss_data.hpp:181
Allocation-free declaration scalar; its owning occurrence property identifies the active field.
Definition pss_data.hpp:190
Borrowed byte range used only within a synchronous parser invocation.
Definition pss_parser.hpp:16
std::size_t offset
Start in the original source.
Definition pss_parser.hpp:17
std::size_t size
Number of bytes.
Definition pss_parser.hpp:18
Small validated scalar payload; avoids constructing six optional fields per parsed occurrence.
Definition pss_parser.hpp:22
Compact declaration occurrence with source coordinates and containment.
Definition pss_compact.hpp:68
std::uint32_t ruleIndex
Source-order containing rule.
Definition pss_compact.hpp:69
PssPosition location
Original property start.
Definition pss_compact.hpp:71
PssScalarValue value
Original owning scalar, including overwritten duplicate values.
Definition pss_compact.hpp:72
Compact owning source-order rule data, without per-rule text arenas.
Definition pss_compact.hpp:49
std::uint32_t classCount
Number of conjunction classes.
Definition pss_compact.hpp:53
std::uint32_t declarationCount
All occurrences, including duplicates.
Definition pss_compact.hpp:58
PssText type
Optional type identity in the shared arena.
Definition pss_compact.hpp:50
PssText id
Optional target identity in the shared arena.
Definition pss_compact.hpp:51
PssPosition selector
Original selector start.
Definition pss_compact.hpp:56
StyleState state
Presentation-state predicate.
Definition pss_compact.hpp:54
bool readOnly
Orthogonal read-only predicate.
Definition pss_compact.hpp:55
std::uint32_t classBegin
First class-reference index.
Definition pss_compact.hpp:52
std::uint32_t declarationBegin
First declaration occurrence index.
Definition pss_compact.hpp:57
std::array< std::uint16_t, PSS_SCALAR_PROPERTY_COUNT > effectiveDeclaration
Last winning occurrence for each property.
Definition pss_compact.hpp:59
Allocation-free counting sink with the same limits as the storing sink.
Definition pss_compact.hpp:99
constexpr std::expected< void, PssError > state(StyleState state)
Definition pss_compact.hpp:153
constexpr std::expected< void, PssError > className(PssSpan span, PssLocation location)
Definition pss_compact.hpp:135
constexpr std::expected< void, PssError > type(PssSpan span, PssLocation location)
Definition pss_compact.hpp:119
constexpr std::expected< void, PssError > id(PssSpan span, PssLocation location)
Definition pss_compact.hpp:127
PssCounts counts
Current admitted counts.
Definition pss_compact.hpp:100
constexpr std::expected< void, PssError > endRule()
Definition pss_compact.hpp:183
constexpr std::expected< void, PssError > declaration(PssProperty property, PssValue value, PssLocation location)
Definition pss_compact.hpp:164
constexpr std::expected< void, PssError > readOnly()
Definition pss_compact.hpp:146
constexpr std::expected< void, PssError > beginRule(PssLocation location)
Definition pss_compact.hpp:105
constexpr std::expected< void, PssError > text(std::size_t bytes, PssLocation location)
Definition pss_compact.hpp:191
Shared admission counts computed before exact-sized CT construction.
Definition pss_compact.hpp:91
std::size_t declarations
Total declaration occurrences.
Definition pss_compact.hpp:94
std::size_t rules
Rules in source order.
Definition pss_compact.hpp:92
std::size_t bytes
All copied selector and source-name text.
Definition pss_compact.hpp:95
std::size_t classes
Total conjunction classes.
Definition pss_compact.hpp:93
Compact original-source coordinates within the named CT byte limit.
Definition pss_compact.hpp:38
std::uint32_t line
One-based LF line.
Definition pss_compact.hpp:40
std::uint32_t column
One-based byte column.
Definition pss_compact.hpp:41
std::uint32_t offset
Zero-based byte offset.
Definition pss_compact.hpp:39
Compact owning-arena reference; all admitted CT text fits uint32_t.
Definition pss_compact.hpp:32
std::uint32_t size
Byte count.
Definition pss_compact.hpp:34
std::uint32_t offset
Start within the descriptor's shared arena.
Definition pss_compact.hpp:33