ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
StyleParser.hpp
Go to the documentation of this file.
1#pragma once
2
56
57#include "Common.hpp"
58#include "Style.hpp"
59#include "Theme.hpp"
60#include "Media.hpp"
61#include "Diagnostics.hpp"
62
63#include <filesystem>
64#include <optional>
65#include <string>
66#include <string_view>
67#include <unordered_map>
68#include <vector>
69
70namespace ZenitUI::ZMarkup
71{
75
76 class Registry;
77 struct Element;
78
80 std::string_view trim(std::string_view s);
81
88 std::string stripComments(std::string_view src);
89
91 std::vector<std::string_view> splitBy(std::string_view s, char sep);
92
94 std::vector<std::string_view> splitWs(std::string_view s);
95
102 float parseDurationSec(std::string_view s);
103
127 std::string_view keyRaw,
128 std::string_view valRaw,
129 const SourceSpan &loc,
130 DiagnosticSink *sink = nullptr,
131 bool strict = false);
132
135 {
136 std::string prop;
137 std::string value;
138 SourceSpan loc{};
139 };
140
150 {
152 struct Rule
153 {
154 std::vector<SimpleSelector> chain;
155 std::string part;
156 std::vector<Declaration> decls;
157 SourceSpan loc{};
158 std::optional<MediaQuery> media;
159 };
160 std::vector<Rule> rules;
161
163 struct KfFrame
164 {
165 std::string ts;
166 std::vector<Declaration> decls;
167 SourceSpan loc{};
168 };
169
171 struct KfRule
172 {
173 std::string name;
174 std::vector<KfFrame> frames;
175 SourceSpan loc{};
176 };
177 std::vector<KfRule> keyframes;
178
179 std::unordered_map<std::string, std::string> vars;
180 std::vector<Declaration> rootDecls;
181 };
182
191 {
192 public:
194 StyleParser(std::string_view src, std::string fileName);
195
197 StyleParser(std::string_view src, std::string fileName, DiagnosticSink &sink);
198
200 void setStrict(bool s) noexcept { strict_ = s; }
201
203 void setRegistry(const Registry *reg) noexcept { registry_ = reg; }
204
207
208 private:
209 std::string src;
210 std::string fileName;
211 DiagnosticSink *sink_ = nullptr;
212 bool strict_ = false;
213
214 const Registry *registry_ = nullptr;
215
216 size_t p{0};
217 int line{1};
218 int col{1};
219 StyleSheet sheet;
220
221 SourceSpan spanFrom(int l, int c, std::uint32_t length = 0) const;
222 DiagnosticSeverity propSeverity() const noexcept;
223
224 bool atEnd() const { return p >= src.size(); }
225 char peek() const { return atEnd() ? '\0' : src[p]; }
226 char get();
227 bool match(char c);
228 bool matchStr(std::string_view s);
229 void skipWs();
230 std::string readIdent();
231
232 void parseRoot();
233 void parseKeyframes();
234 void parseMedia();
235 MediaQuery parseMediaQuery(const std::string &s);
236 static float parseAspectRatio(const std::string &s);
237 void parseRuleWithMedia(const MediaQuery &media);
238 void parseRule(const MediaQuery *media = nullptr);
239 };
240
252
268 void applyStyleSheet(const StyleSheet &sheet,
269 Theme &theme,
270 DiagnosticSink *sink = nullptr,
271 bool strict = false);
272
275 {
276 const Registry *registry{nullptr};
277 const Element *markupRoot{nullptr};
278 bool checkVariables{true};
279 bool checkSelectorTags{true};
281 bool strict{false};
282 };
283
290 void lintStyleSheet(const StyleSheet &sheet,
291 DiagnosticSink &sink,
292 const StyleLintOptions &opts = {});
293
298 bool loadStyleString(std::string_view src,
299 Theme &theme,
300 const std::string &fileName = "<string>",
301 DiagnosticSink *sink = nullptr,
302 bool strict = false,
303 const Registry *registry = nullptr);
304
309 bool loadStyleFile(const std::filesystem::path &path,
310 Theme &theme,
311 DiagnosticSink *sink = nullptr,
312 bool strict = false,
313 const Registry *registry = nullptr);
315} // namespace ZenitUI::ZMarkup
Centralized standard library includes used throughout ZenitUI.
Structured diagnostics for the CSS parser and style resolver.
@media query representation and evaluation.
The Style / ComputedStyle pair and all style-related value types.
Global stylesheet storage and rule matching.
Abstract destination for diagnostics.
Definition Diagnostics.hpp:127
Per-context stylesheet: rules, keyframes, and root style.
Definition Theme.hpp:164
Tag → factory map plus the prefab table (either per-UIContext or standalone/global).
Definition ZMarkup.hpp:215
StyleParser(std::string_view src, std::string fileName, DiagnosticSink &sink)
Construct a parser reporting diagnostics into sink.
void setRegistry(const Registry *reg) noexcept
Use a specific Registry for part validation (defaults to global registry).
Definition StyleParser.hpp:203
StyleSheet parse()
Parse the whole source into a StyleSheet.
StyleParser(std::string_view src, std::string fileName)
Construct a parser logging warnings through Logger.
void setStrict(bool s) noexcept
Enable strict mode: unknown properties and pseudo-classes become errors.
Definition StyleParser.hpp:200
bool loadStyleString(std::string_view src, Theme &theme, const std::string &fileName="<string>", DiagnosticSink *sink=nullptr, bool strict=false, const Registry *registry=nullptr)
Parse src and apply it to theme.
void applyStyleSheet(const StyleSheet &sheet, Theme &theme, DiagnosticSink *sink=nullptr, bool strict=false)
Apply a parsed StyleSheet to a Theme.
DiagnosticSeverity
Diagnostic severity level.
Definition Diagnostics.hpp:52
bool loadStyleFile(const std::filesystem::path &path, Theme &theme, DiagnosticSink *sink=nullptr, bool strict=false, const Registry *registry=nullptr)
Load a stylesheet from a file and apply it to theme.
void lintStyleSheet(const StyleSheet &sheet, DiagnosticSink &sink, const StyleLintOptions &opts={})
Run static semantic checks on a parsed StyleSheet.
std::vector< std::string_view > splitWs(std::string_view s)
Split s on any ASCII whitespace.
void normalizeKeyframes(KeyframeAnimation &anim, const SourceSpan &loc)
Fill missing properties in every keyframe from neighbouring frames.
std::vector< std::string_view > splitBy(std::string_view s, char sep)
Split s on every occurrence of sep.
std::string stripComments(std::string_view src)
Strip // line and block comments from src.
float parseDurationSec(std::string_view s)
Parse a CSS duration ("200ms" → 0.2, "1.5s" → 1.5).
void applyStyleDeclaration(Style &st, std::string_view keyRaw, std::string_view valRaw, const SourceSpan &loc, DiagnosticSink *sink=nullptr, bool strict=false)
Apply one declaration to st.
std::string_view trim(std::string_view s)
Trim leading and trailing ASCII whitespace.
A named list of keyframes, keyed by name in Theme::keyframes.
Definition AnimPrimitives.hpp:115
Source location for a diagnostic.
Definition Diagnostics.hpp:41
Sparse style: every property is Opt<T>, unset by default.
Definition Style.hpp:932
One parsed key: value; declaration, with source location.
Definition StyleParser.hpp:135
Parsed representation of one markup node.
Definition ZMarkup.hpp:35
Options controlling static semantic linting in lintStyleSheet().
Definition StyleParser.hpp:275
bool checkSelectorTags
Emit UnknownSelectorTag with suggestions.
Definition StyleParser.hpp:279
bool checkVariables
Emit UnresolvedVariable and UnusedVariable.
Definition StyleParser.hpp:278
const Element * markupRoot
Optional parsed ZMarkup AST for SelectorMatchesNothing.
Definition StyleParser.hpp:277
bool strict
Report findings as Error instead of Warning.
Definition StyleParser.hpp:281
bool checkSelectorMatches
Emit SelectorMatchesNothing when markupRoot != nullptr.
Definition StyleParser.hpp:280
const Registry * registry
Registry for UnknownSelectorTag (defaults to global registry).
Definition StyleParser.hpp:276
One from/to/N% frame inside @keyframes.
Definition StyleParser.hpp:164
One @keyframes name { ... } block.
Definition StyleParser.hpp:172
One selector-plus-declarations entry.
Definition StyleParser.hpp:153
Intermediate representation: parsed rules, keyframes and root vars.
Definition StyleParser.hpp:150