From 95387b23f55f7ca272d48264c8a8a6ec3cf1e09b Mon Sep 17 00:00:00 2001 From: Waozi Date: Tue, 29 Sep 2026 20:56:26 -0300 Subject: [PATCH] Parse, format, and install KSS style sheets as a package over Kryon KSS moves here from Kryon, with the classic, lightfield, and material style packs, the pack module generator, and the parser tests and sanitized fuzzing. Kryon keeps the rule table and style resolution; apps that read .kss text add this package and import kss/Kss or kss/SystemStyle. --- .github/workflows/ci.yml | 40 + .gitignore | 2 + LICENSE | 28 + Makefile | 61 + README.md | 44 + src/Kss/module.zi | 3 + src/kss_formatter.zi | 600 ++++++ src/kss_parser.zi | 357 ++++ src/parser/blocks.zi | 742 +++++++ src/parser/css_values.zi | 953 +++++++++ src/parser/directives.zi | 293 +++ src/parser/lexer.zi | 577 +++++ src/parser/rules.zi | 862 ++++++++ src/parser/selectors.zi | 1110 ++++++++++ src/parser/tokens.zi | 124 ++ src/parser/vocabulary.zi | 405 ++++ src/style_pack_classic.zi | 2349 +++++++++++++++++++++ src/style_parse.zi | 97 + src/system_style.zi | 108 + styles/classic.kss | 2244 ++++++++++++++++++++ styles/lightfield.kss | 2230 +++++++++++++++++++ styles/material.kss | 2210 +++++++++++++++++++ tests/kss_corpus/block-directives.kss | 8 + tests/kss_corpus/comment-in-string.kss | 3 + tests/kss_corpus/import-kept.ok.kss | 3 + tests/kss_corpus/import-target-braces.kss | 2 + tests/kss_corpus/import-target-line.kss | 6 + tests/kss_corpus/many-blocks.ok.kss | 42 + tests/kss_corpus/nested-braces.kss | 2 + tests/kss_corpus/quote-line-break.kss | 3 + tests/kss_corpus/unclosed-comment.kss | 2 + tests/kss_corpus/variant-labels.ok.kss | 5 + tests/kss_fuzz.sh | 69 + tests/kss_fuzz.zi | 1413 +++++++++++++ tests/style_parse_behavior.zi | 108 + tests/system_style_behavior.zi | 31 + tools/style_pack_module.zi | 263 +++ ziran.lock | 31 + ziran.toml | 15 + 39 files changed, 17445 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 src/Kss/module.zi create mode 100644 src/kss_formatter.zi create mode 100644 src/kss_parser.zi create mode 100644 src/parser/blocks.zi create mode 100644 src/parser/css_values.zi create mode 100644 src/parser/directives.zi create mode 100644 src/parser/lexer.zi create mode 100644 src/parser/rules.zi create mode 100644 src/parser/selectors.zi create mode 100644 src/parser/tokens.zi create mode 100644 src/parser/vocabulary.zi create mode 100644 src/style_pack_classic.zi create mode 100644 src/style_parse.zi create mode 100644 src/system_style.zi create mode 100644 styles/classic.kss create mode 100644 styles/lightfield.kss create mode 100644 styles/material.kss create mode 100644 tests/kss_corpus/block-directives.kss create mode 100644 tests/kss_corpus/comment-in-string.kss create mode 100644 tests/kss_corpus/import-kept.ok.kss create mode 100644 tests/kss_corpus/import-target-braces.kss create mode 100644 tests/kss_corpus/import-target-line.kss create mode 100644 tests/kss_corpus/many-blocks.ok.kss create mode 100644 tests/kss_corpus/nested-braces.kss create mode 100644 tests/kss_corpus/quote-line-break.kss create mode 100644 tests/kss_corpus/unclosed-comment.kss create mode 100644 tests/kss_corpus/variant-labels.ok.kss create mode 100644 tests/kss_fuzz.sh create mode 100644 tests/kss_fuzz.zi create mode 100644 tests/style_parse_behavior.zi create mode 100644 tests/system_style_behavior.zi create mode 100644 tools/style_pack_module.zi create mode 100644 ziran.lock create mode 100644 ziran.toml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..48327fd --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,40 @@ +name: CI + +on: + push: + branches: [master] + pull_request: + workflow_dispatch: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + with: + path: kss + + - name: Build the ziran launcher + run: | + git clone --depth 1 https://github.com/ziranlang/ziran.git ziran + make -C ziran + echo "$PWD/ziran/build/bin" >> "$GITHUB_PATH" + + - name: Test + working-directory: kss + run: | + ziran fetch --locked + make test style-pack-check + + # A failure names the seed; rerun it with make fuzz FUZZ_SEED=N. + - name: Fuzz with this run's seed + working-directory: kss + run: env -u DISPLAY -u WAYLAND_DISPLAY make fuzz FUZZ_SEED=${{ github.run_number }} FUZZ_COUNT=10000 + + - name: Keep a failing sheet + if: failure() + uses: actions/upload-artifact@v7 + with: + name: kss-fuzz-failure + path: kss/build/kss-fuzz/failure-*.kss + if-no-files-found: ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d086e16 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +ziran.local.toml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9eeb030 --- /dev/null +++ b/LICENSE @@ -0,0 +1,28 @@ +BSD 3-Clause License + +Copyright (c) 2026, Waozi + +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. + +3. Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR 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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ec6009b --- /dev/null +++ b/Makefile @@ -0,0 +1,61 @@ +.DEFAULT_GOAL := check + +ZIRAN ?= ziran +CC ?= cc +BUILD := build +FUZZ_SEED ?= 1 +FUZZ_COUNT ?= 2000 +STYLE_PACKS := classic +STYLE_PACK_TOOL := $(BUILD)/tools/style_pack_module + +.PHONY: check test style-packs style-pack-check fuzz clean + +check: test style-pack-check fuzz + +test: + $(ZIRAN) check --project + $(ZIRAN) bundle --project --entry style_parse_behavior:Answer \ + -o $(BUILD)/style-parse.zib tests/style_parse_behavior.zi + test "$$($(ZIRAN) run --project $(BUILD)/style-parse.zib)" = 42 + rm -rf $(BUILD)/style-parse-c + $(ZIRAN) build --project --target=c --entry style_parse_behavior:Answer \ + -o $(BUILD)/style-parse-c tests/style_parse_behavior.zi + printf '#include "style_parse_behavior.h"\nint main(void) { return Answer() == 42 ? 0 : 1; }\n' \ + > $(BUILD)/style-parse-c/main.c + $(CC) -std=c11 -I$$($(ZIRAN) pkg path ziran)/include -I$(BUILD)/style-parse-c \ + $(BUILD)/style-parse-c/*.c -o $(BUILD)/style-parse + $(BUILD)/style-parse + # The whole classic pack is more than the portable runner's step + # budget, so the system style test runs as native code. + rm -rf $(BUILD)/system-style-c + $(ZIRAN) build --project --target=c --entry system_style_behavior:main \ + -o $(BUILD)/system-style-c tests/system_style_behavior.zi + $(CC) -std=c11 -I$$($(ZIRAN) pkg path ziran)/include -I$(BUILD)/system-style-c \ + $(BUILD)/system-style-c/*.c -o $(BUILD)/system-style + $(BUILD)/system-style + +# Style packs apps can install without the .kss file. Their modules are +# committed, because apps import KSS as a package without running this +# Makefile; style-pack-check fails when a module no longer matches its pack. +$(STYLE_PACK_TOOL): tools/style_pack_module.zi + rm -rf $@-c + mkdir -p $(dir $@) + $(ZIRAN) build --project --target=c --entry style_pack_module:main \ + -o $@-c tools/style_pack_module.zi + $(CC) -std=c11 -O2 -I$$($(ZIRAN) pkg path ziran)/include -I$@-c $@-c/*.c -o $@ + +style-packs: $(STYLE_PACK_TOOL) + @for pack in $(STYLE_PACKS); do $(STYLE_PACK_TOOL) write $$pack || exit 1; done + +style-pack-check: $(STYLE_PACK_TOOL) + @for pack in $(STYLE_PACKS); do $(STYLE_PACK_TOOL) check $$pack || exit 1; done + @echo "Style pack modules match: $(STYLE_PACKS)" + +# Generated and damaged style sheets through the parser, the rule table, +# and the formatter, built with AddressSanitizer and UndefinedBehaviorSanitizer. +# The same seed makes the same sheets; see tests/kss_fuzz.zi. +fuzz: + @ZIRAN=$(ZIRAN) sh tests/kss_fuzz.sh $(FUZZ_SEED) $(FUZZ_COUNT) + +clean: + rm -rf $(BUILD) diff --git a/README.md b/README.md new file mode 100644 index 0000000..2436e24 --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +# KSS + +KSS is the style sheet language of [Kryon](https://github.com/kryonlabs/kryon) +applications. This package parses and formats `.kss` sheets, resolves their +`@import`s and color tokens, installs the rules into Kryon's style table, and +ships the classic, lightfield, and material style packs in `styles/`. Kryon +itself only resolves installed rules; apps that read `.kss` text add this +package. + +## Use it + +```sh +ziran add https://github.com/kryonlabs/kss.git +``` + +```zi +using Styles :: #import "kss/Kss"; +#import "kss/SystemStyle" +``` + +`BeginStyleRules`, `ParseStyleRules`, and `InstallParsedStyleRules` parse a +sheet in steps and install it; `ProvideStyleRulesImport` answers an +`@import`. `SystemStyle` picks a pack that matches the desktop theme. The +classic pack is also compiled in as `style_pack_classic`, so an app can +install it without shipping the `.kss` file. + +## Develop + +`make check` checks the package, runs the parser and style tests as portable +bundles and native code, verifies that `src/style_pack_classic.zi` matches +`styles/classic.kss`, and fuzzes generated and damaged sheets under +AddressSanitizer and UndefinedBehaviorSanitizer. After editing +`styles/classic.kss`, run `make style-packs` and commit the regenerated +module with it. `make fuzz FUZZ_SEED=N FUZZ_COUNT=M` runs other sheets; a +failing one is kept in `build/kss-fuzz/` with a command that shrinks it. + +To test against local checkouts, keep them next to this one and put this in +an ignored `ziran.local.toml`: + +```toml +[overrides] +ziran = "../ziran" +kryon = "../kryon" +``` diff --git a/src/Kss/module.zi b/src/Kss/module.zi new file mode 100644 index 0000000..9176836 --- /dev/null +++ b/src/Kss/module.zi @@ -0,0 +1,3 @@ +using ParserModule :: #import "kss_parser"; +using FormatterModule :: #import "kss_formatter"; +using StyleParseModule :: #import "style_parse"; diff --git a/src/kss_formatter.zi b/src/kss_formatter.zi new file mode 100644 index 0000000..9704704 --- /dev/null +++ b/src/kss_formatter.zi @@ -0,0 +1,600 @@ +#import "kss_parser" + +using KssStatus; + +// The KSS formatter lives here once and lowers to C, Go, and JavaScript with +// the shared transpilers. Formatting is structural re-layout: the shared +// parser supplies every construct's span, and construct text is copied +// verbatim so the output is semantically identical to the input - only +// indentation, line breaks, and blank-line placement change. Comments inside +// constructs travel with them; comments between constructs are preserved as +// their own constructs. + +// Output is segment-based so callers need no allocator: hosts stitch kind 0 +// (copy source[start..start+length]), kind 1 (emit the single atom byte), and +// kind 2 (emit a newline followed by `length` spaces). + +KssFormatSegment :: struct { + kind: s32 + start: s32 + length: s32 + atom: u8 +} + +KssFormatResult :: struct { + ok: bool + segments: [1024]KssFormatSegment + count: s32 + diagnostic: [128]u8 + diagnostic_length: s32 +} + +// Construct kinds: 1 tokens, 2 theme, 3 env, 4 variant, 5 directive, 6 rule, +// 7 foreign block, 8 comment. Mirrors the parser's block kinds. + +KssFormatConstruct :: struct { + kind: s32 + start: s32 + end: s32 +} + +KssFormatState :: struct { + source: string + result: KssFormatResult + constructs: [192]KssFormatConstruct + construct_count: s32 +} + +KssFormatFail :: (state: KssFormatState, text: string) -> KssFormatState { + index: s32 = 0 + state.result.ok = false + state.result.diagnostic_length = 0 + index = 0 + while index < 128 && index < (cast(s32)text.count) { + state.result.diagnostic[index] = text[index] + state.result.diagnostic_length = index + 1 + index = index + 1 + } + return state +} + +KssFormatEmitCopy :: (state: KssFormatState, start: s32, length: s32) -> KssFormatState { + if length <= 0 { + return state + } + if state.result.count >= 1024 { + return KssFormatFail(state, "format segment capacity exceeded") + } + state.result.segments[state.result.count].kind = 0 + state.result.segments[state.result.count].start = start + state.result.segments[state.result.count].length = length + state.result.segments[state.result.count].atom = 0 + state.result.count = state.result.count + 1 + return state +} + +KssFormatEmitAtom :: (state: KssFormatState, atom: u8) -> KssFormatState { + if state.result.count >= 1024 { + return KssFormatFail(state, "format segment capacity exceeded") + } + state.result.segments[state.result.count].kind = 1 + state.result.segments[state.result.count].start = 0 + state.result.segments[state.result.count].length = 0 + state.result.segments[state.result.count].atom = atom + state.result.count = state.result.count + 1 + return state +} + +KssFormatEmitIndent :: (state: KssFormatState, spaces: s32) -> KssFormatState { + if state.result.count >= 1024 { + return KssFormatFail(state, "format segment capacity exceeded") + } + if spaces > 128 { + spaces = 128 + } + state.result.segments[state.result.count].kind = 2 + state.result.segments[state.result.count].start = 0 + state.result.segments[state.result.count].length = spaces + state.result.segments[state.result.count].atom = 0 + state.result.count = state.result.count + 1 + return state +} + +KssFormatAddConstruct :: (state: KssFormatState, kind: s32, start: s32, end: s32) -> KssFormatState { + if end <= start { + return state + } + if state.construct_count >= 192 { + return KssFormatFail(state, "format construct capacity exceeded") + } + state.constructs[state.construct_count].kind = kind + state.constructs[state.construct_count].start = start + state.constructs[state.construct_count].end = end + state.construct_count = state.construct_count + 1 + return state +} + +// The '@' of a foreign or group block: its query follows the directive name +// and white space. The parser keeps only the query's position, so that its +// by-value state stays small. -1 when a comment sits between the two. +KssFormatDirectiveStart :: (source: string, query_start: s32, name_length: s32) -> s32 { + pos: s32 = query_start + while pos > 0 { + if !KssIsSpace(source[pos - 1]) { + break + } + pos = pos - 1 + } + pos = pos - name_length - 1 + if pos < 0 || source[pos] != 64 { + return -1 + } + return pos +} + +// Collect every construct by running the shared parser in declarative mode. +// Imports resolve to empty modules: formatting re-layouts one file's text and +// never follows imports. + +KssFormatCollect :: (state: KssFormatState) -> KssFormatState { + env: KssEnvironment = KssDefaultEnvironment() + p: KssParser = KssBeginDeclarative(state.source, "", env) + running: bool = true + while running { + if p.status == cast(s32)KssStatusRule { + end: s32 = p.rule_span.end + if end <= p.rule_span.selector_start { + end = p.rule_span.body_start + p.rule_span.body_length + } + state = KssFormatAddConstruct(state, 6, p.rule_span.selector_start, end) + p.status = cast(s32)KssStatusContinue + continue + } + if p.status != cast(s32)KssStatusContinue { + break + } + p = KssStep(p) + if p.status == cast(s32)KssStatusNeedImport { + // Formatting re-layouts one file; imports resolve to empty modules + // and the resolved name is irrelevant to the layout. + p = KssProvideImport(p, "", "import") + } + } + // One failure call: each call copies the whole state onto the stack. + failure: string = "" + if p.status != cast(s32)KssStatusDone { + failure = "style sheet did not parse" + } else if p.blocks_overflow { + failure = "format construct capacity exceeded" + } + index: s32 = 0 + while failure.count == 0 && index < p.foreign_count { + if p.foreign[index].file == 0 && KssFormatDirectiveStart(state.source, + p.foreign[index].query_start, p.foreign[index].name.length) < 0 { + failure = "cannot place a block directive" + } + index = index + 1 + } + if failure.count > 0 { + return KssFormatFail(state, failure) + } + index = 0 + while index < p.block_count { + state = KssFormatAddConstruct(state, p.blocks[index].kind, + p.blocks[index].start, + p.blocks[index].start + p.blocks[index].length) + index = index + 1 + } + // A foreign or group block runs from its '@' through its closing brace. + // Blocks read from imports hold positions in another source. + index = 0 + while index < p.foreign_count { + if p.foreign[index].file == 0 { + start: s32 = KssFormatDirectiveStart(state.source, + p.foreign[index].query_start, p.foreign[index].name.length) + end: s32 = p.foreign[index].body_start + p.foreign[index].body_length + 1 + state = KssFormatAddConstruct(state, 7, start, end) + } + index = index + 1 + } + return state +} + +// Comments between constructs are preserved as standalone constructs. + +KssFormatScanGaps :: (state: KssFormatState) -> KssFormatState { + // Comments found here are appended; scan only the gaps between the + // sorted constructs that existed before. + count: s32 = state.construct_count + cursor: s32 = 0 + index: s32 = 0 + while index < count { + start: s32 = state.constructs[index].start + state = KssFormatScanRange(state, cursor, start) + cursor = state.constructs[index].end + index = index + 1 + } + state = KssFormatScanRange(state, cursor, (cast(s32)state.source.count)) + return state +} + +KssFormatScanRange :: (state: KssFormatState, from: s32, to: s32) -> KssFormatState { + cursor: s32 = from + while cursor < to { + byte: u8 = 0 + if cursor < (cast(s32)state.source.count) { + byte = state.source[cursor] + } + if byte == 47 && cursor + 1 < to && cursor + 1 < (cast(s32)state.source.count) { + next: u8 = state.source[cursor + 1] + if next == 47 { + end: s32 = cursor + while end < (cast(s32)state.source.count) && state.source[end] != 10 { + end = end + 1 + } + state = KssFormatAddConstruct(state, 8, cursor, end) + cursor = end + continue + } + if next == 42 { + // An unclosed comment runs to the end of the source. + end: s32 = cast(s32)state.source.count + scan: s32 = cursor + 2 + while scan + 1 < (cast(s32)state.source.count) { + if state.source[scan] == 42 && state.source[scan + 1] == 47 { + end = scan + 2 + break + } + scan = scan + 1 + } + state = KssFormatAddConstruct(state, 8, cursor, end) + cursor = end + continue + } + } + cursor = cursor + 1 + } + return state +} + +// Sort constructs by start (insertion sort; sheets are small). + +KssFormatSortConstructs :: (state: KssFormatState) -> KssFormatState { + index: s32 = 1 + while index < state.construct_count { + candidate: KssFormatConstruct = state.constructs[index] + scan: s32 = index - 1 + while scan >= 0 && state.constructs[scan].start > candidate.start { + state.constructs[scan + 1] = state.constructs[scan] + scan = scan - 1 + } + state.constructs[scan + 1] = candidate + index = index + 1 + } + return state +} + +// Rules inside container blocks (env, variant) are covered by the block's own +// span; drop them so their text is not emitted twice. + +KssFormatDropNested :: (state: KssFormatState) -> KssFormatState { + kept: s32 = 0 + index: s32 = 0 + while index < state.construct_count { + candidate: KssFormatConstruct = state.constructs[index] + nested: bool = false + scan: s32 = 0 + while scan < state.construct_count { + other: KssFormatConstruct = state.constructs[scan] + if scan != index && other.kind != 6 && other.kind != 8 { + if candidate.start >= other.start && candidate.end <= other.end { + nested = true + break + } + } + scan = scan + 1 + } + if !nested { + state.constructs[kept] = candidate + kept = kept + 1 + } + index = index + 1 + } + state.construct_count = kept + return state +} + +// Emit one construct with its lines re-indented by brace depth. Construct +// text is copied verbatim per line; only leading whitespace is dropped unless a +// single-line declaration block can be reflowed structurally. + +KssFormatTrimStart :: (state: KssFormatState, start: s32, end: s32) -> s32 { + pos: s32 = start + while pos < end { + byte: u8 = state.source[pos] + if byte == 32 || byte == 9 || byte == 13 { + pos = pos + 1 + } else { + break + } + } + return pos +} + +KssFormatTrimEnd :: (state: KssFormatState, start: s32, end: s32) -> s32 { + pos: s32 = end + while pos > start { + byte: u8 = state.source[pos - 1] + if byte == 32 || byte == 9 || byte == 13 { + pos = pos - 1 + } else { + break + } + } + return pos +} + +// Structure bytes count only outside quoted text and comments. A scan +// stands in code (mode 0), inside a quote (34 or 39) that ends at its line as +// in CSS, inside a block comment (42), or inside a line comment (47). + +KssFormatScan :: struct { + pos: s32 + mode: s32 + code: bool +} + +// Steps over one unit: an escaped pair, a comment opener or closer, or one +// byte. `code` says whether the unit was structure. +KssFormatScanNext :: (source: string, scan: KssFormatScan, end: s32) -> KssFormatScan { + at: s32 = scan.pos + byte: u8 = source[at] + next: u8 = 0 + if at + 1 < end { + next = source[at + 1] + } + scan.code = false + scan.pos = at + 1 + if scan.mode == 0 { + if byte == 34 || byte == 39 { + scan.mode = cast(s32)byte + } else if byte == 47 && next == 42 { + scan.mode = 42 + scan.pos = at + 2 + } else if byte == 47 && next == 47 { + scan.mode = 47 + scan.pos = at + 2 + } else { + scan.code = true + } + } else if scan.mode == 42 { + if byte == 42 && next == 47 { + scan.mode = 0 + scan.pos = at + 2 + } + } else if scan.mode == 47 { + if byte == 10 { + scan.mode = 0 + } + } else if byte == 10 || byte == 13 || cast(s32)byte == scan.mode { + scan.mode = 0 + } else if byte == 92 && at + 1 < end && next != 10 && next != 13 { + scan.pos = at + 2 + } + return scan +} + +KssFormatLineHasComments :: (source: string, start: s32, end: s32) -> bool { + scan: KssFormatScan + scan.pos = start + scan.mode = 0 + while scan.pos < end { + scan = KssFormatScanNext(source, scan, end) + if scan.mode == 42 || scan.mode == 47 { + return true + } + } + return false +} + +KssFormatLineCanReflow :: (source: string, start: s32, end: s32) -> bool { + if KssFormatLineHasComments(source, start, end) { + return false + } + open_seen: bool = false + semicolon_seen: bool = false + close_seen: bool = false + scan: KssFormatScan + scan.pos = start + scan.mode = 0 + while scan.pos < end { + at: s32 = scan.pos + scan = KssFormatScanNext(source, scan, end) + if scan.code { + byte: u8 = source[at] + if byte == 123 { + open_seen = true + } else if byte == 59 && open_seen { + semicolon_seen = true + } else if byte == 125 && semicolon_seen { + close_seen = true + } + } + } + return open_seen && semicolon_seen && close_seen +} + +KssFormatEmitLineStart :: (state: KssFormatState, leading_break: bool, + first_line: bool, spaces: s32) -> KssFormatState { + if leading_break && first_line { + state = KssFormatEmitIndent(state, 0) + state = KssFormatEmitIndent(state, 0) + } else if !first_line { + state = KssFormatEmitIndent(state, spaces) + } + return state +} + +KssFormatEmitTrimmedRange :: (state: KssFormatState, start: s32, end: s32) -> KssFormatState { + trimmed_start: s32 = KssFormatTrimStart(state, start, end) + trimmed_end: s32 = KssFormatTrimEnd(state, trimmed_start, end) + if trimmed_start < trimmed_end { + state = KssFormatEmitCopy(state, trimmed_start, trimmed_end - trimmed_start) + } + return state +} + +KssFormatEmitReflowLine :: (state: KssFormatState, start: s32, end: s32, + base_depth: s32, leading_break: bool, first_line: bool) -> KssFormatState { + token_start: s32 = start + depth: s32 = base_depth + emitted: bool = false + scan: KssFormatScan + scan.pos = start + scan.mode = 0 + while scan.pos < end { + cursor: s32 = scan.pos + scan = KssFormatScanNext(state.source, scan, end) + byte: u8 = 0 + if scan.code { + byte = state.source[cursor] + } + if byte == 123 { + state = KssFormatEmitLineStart(state, leading_break, first_line && !emitted, depth * 4) + // A brace with nothing before it opens its own line, unspaced. + if KssFormatTrimStart(state, token_start, cursor) < KssFormatTrimEnd(state, token_start, cursor) { + state = KssFormatEmitTrimmedRange(state, token_start, cursor) + state = KssFormatEmitAtom(state, 32) + } + state = KssFormatEmitAtom(state, 123) + emitted = true + depth = depth + 1 + token_start = cursor + 1 + } else if byte == 59 { + state = KssFormatEmitLineStart(state, leading_break, first_line && !emitted, depth * 4) + state = KssFormatEmitTrimmedRange(state, token_start, cursor + 1) + emitted = true + token_start = cursor + 1 + } else if byte == 125 { + if KssFormatTrimStart(state, token_start, cursor) < KssFormatTrimEnd(state, token_start, cursor) { + state = KssFormatEmitLineStart(state, leading_break, first_line && !emitted, depth * 4) + state = KssFormatEmitTrimmedRange(state, token_start, cursor) + emitted = true + } + if depth > 0 { + depth = depth - 1 + } + state = KssFormatEmitLineStart(state, leading_break, first_line && !emitted, depth * 4) + state = KssFormatEmitAtom(state, 125) + emitted = true + token_start = cursor + 1 + } + } + if KssFormatTrimStart(state, token_start, end) < KssFormatTrimEnd(state, token_start, end) { + state = KssFormatEmitLineStart(state, leading_break, first_line && !emitted, depth * 4) + state = KssFormatEmitTrimmedRange(state, token_start, end) + } + return state +} + +KssFormatEmitConstruct :: (state: KssFormatState, construct: KssFormatConstruct, leading_break: bool) -> KssFormatState { + depth: s32 = 0 + mode: s32 = 0 + pos: s32 = construct.start + first: bool = true + while pos < construct.end { + line_end: s32 = pos + while line_end < construct.end && line_end < (cast(s32)state.source.count) { + if state.source[line_end] == 10 { + break + } + line_end = line_end + 1 + } + trimmed: s32 = pos + while trimmed < line_end { + byte: u8 = state.source[trimmed] + if byte == 32 || byte == 9 || byte == 13 { + trimmed = trimmed + 1 + } else { + break + } + } + if trimmed < line_end { + copy_start: s32 = trimmed + indent: s32 = depth + if mode != 0 { + // Inside a block comment the line is text; keep it whole. + copy_start = pos + indent = 0 + } else if state.source[trimmed] == 125 && depth > 0 { + indent = depth - 1 + } + if mode == 0 && KssFormatLineCanReflow(state.source, trimmed, line_end) { + state = KssFormatEmitReflowLine(state, trimmed, line_end, + indent, leading_break, first) + } else { + state = KssFormatEmitLineStart(state, leading_break, first, + indent * 4) + state = KssFormatEmitCopy(state, copy_start, line_end - copy_start) + } + scan: KssFormatScan + scan.pos = trimmed + scan.mode = mode + while scan.pos < line_end { + at: s32 = scan.pos + scan = KssFormatScanNext(state.source, scan, line_end) + if scan.code { + byte: u8 = state.source[at] + if byte == 123 { + depth = depth + 1 + } else if byte == 125 && depth > 0 { + depth = depth - 1 + } + } + } + // Quotes and line comments end with their line. + mode = 0 + if scan.mode == 42 { + mode = 42 + } + first = false + } + pos = line_end + 1 + } + return state +} + +// Format a style sheet. Output assembles from segments; hosts emit kind 0 as +// source.slice(start, start + length), kind 1 as the atom byte, and kind 2 as +// a newline plus `length` spaces. + +#program_export +KssFormat :: (source: string) -> KssFormatResult { + state: KssFormatState + state.source = source + state.result.ok = true + state.result.count = 0 + state.result.diagnostic_length = 0 + state.construct_count = 0 + state = KssFormatCollect(state) + if !state.result.ok { + return state.result + } + state = KssFormatDropNested(state) + state = KssFormatSortConstructs(state) + state = KssFormatScanGaps(state) + state = KssFormatSortConstructs(state) + index: s32 = 0 + while index < state.construct_count { + if !state.result.ok { + return state.result + } + leading: bool = index > 0 + state = KssFormatEmitConstruct(state, state.constructs[index], leading) + index = index + 1 + } + if state.result.count > 0 { + state = KssFormatEmitAtom(state, 10) + } + return state.result +} diff --git a/src/kss_parser.zi b/src/kss_parser.zi new file mode 100644 index 0000000..f025686 --- /dev/null +++ b/src/kss_parser.zi @@ -0,0 +1,357 @@ +#import "kryon/style" +#import "kryon/style_sheet" +#import "kryon/control_props" + +using KssTokenKind; +using KssOriginKind; +using KssStatus; +using KssContrast; +using KssDensity; +using KssPointer; +using KssPlatform; +using KssTheme; +using KssCSSValueKind; +using KssSelectorChainAction; +using KssSelectorAtomKind; +using ButtonTone; +using ButtonEmphasis; +using ControlSize; +using ButtonState; +using MaterialKind; +using StyleField; + +// KSS is the text style format; this module is the one shared implementation. +// Backends lower it to C, Go, and JavaScript, so the grammar, environment +// axes, theme overlays, imports, and provenance rules exist exactly once. +// Hosts own I/O: they feed source text in and resolve import requests. + +KssTokenKind :: enum { + KssTokenColor :: 0 + KssTokenLength :: 1 + KssTokenDuration :: 2 + KssTokenMaterial :: 3 +} + +KssOriginKind :: enum { + KssOriginPack :: 0 + KssOriginImport :: 1 + KssOriginTheme :: 2 + KssOriginEnvironment :: 3 + KssOriginVariant :: 4 +} + +KssStatus :: enum { + KssStatusContinue :: 0 + KssStatusRule :: 1 + KssStatusNeedImport :: 2 + KssStatusDone :: 3 + KssStatusError :: 4 +} + +KssContrast :: enum { + KssContrastNormal :: 0 + KssContrastHigh :: 1 +} + +KssDensity :: enum { + KssDensityComfortable :: 0 + KssDensityCompact :: 1 + KssDensityTouch :: 2 +} + +KssPointer :: enum { + KssPointerMouse :: 0 + KssPointerTouch :: 1 + KssPointerMixed :: 2 +} + +KssPlatform :: enum { + KssPlatformDesktop :: 0 + KssPlatformAndroid :: 1 + KssPlatformWeb :: 2 + KssPlatformPlan9 :: 3 + KssPlatformTerminal :: 4 +} + +KssTheme :: enum { + KssThemeNone :: 0 + KssThemeLight :: 1 + KssThemeDark :: 2 +} + + +#program_export +KssDefaultEnvironment :: () -> KssEnvironment { + env: KssEnvironment + env.theme = cast(s32)KssThemeNone + env.theme_name = KssMakeName("") + env.contrast = cast(s32)KssContrastNormal + env.density = cast(s32)KssDensityComfortable + env.pointer = cast(s32)KssPointerMouse + env.platform = cast(s32)KssPlatformDesktop + return env +} + +// Hosts supply platform defaults; interpretation of KSS names stays here. +#program_export +KssEnvironmentWithNames :: (env: KssEnvironment, theme: string, + contrast: string, density: string, pointer: string, platform: string, + variant: string) -> KssEnvironment { + env.theme = cast(s32)KssThemeNone + env.theme_name = KssMakeName(theme) + if KssNameEquals(KssMakeName(theme), "light") { + env.theme = cast(s32)KssThemeLight + } else if KssNameEquals(KssMakeName(theme), "dark") { + env.theme = cast(s32)KssThemeDark + } + env.contrast = cast(s32)KssContrastNormal + if KssNameEquals(KssMakeName(contrast), "high") { + env.contrast = cast(s32)KssContrastHigh + } + env.density = cast(s32)KssDensityComfortable + if KssNameEquals(KssMakeName(density), "compact") { + env.density = cast(s32)KssDensityCompact + } else if KssNameEquals(KssMakeName(density), "touch") { + env.density = cast(s32)KssDensityTouch + } + env.pointer = cast(s32)KssPointerMouse + if KssNameEquals(KssMakeName(pointer), "touch") { + env.pointer = cast(s32)KssPointerTouch + } else if KssNameEquals(KssMakeName(pointer), "mixed") { + env.pointer = cast(s32)KssPointerMixed + } + if KssNameEquals(KssMakeName(platform), "desktop") { + env.platform = cast(s32)KssPlatformDesktop + } else if KssNameEquals(KssMakeName(platform), "android") { + env.platform = cast(s32)KssPlatformAndroid + } else if KssNameEquals(KssMakeName(platform), "web") { + env.platform = cast(s32)KssPlatformWeb + } else if KssNameEquals(KssMakeName(platform), "plan9") { + env.platform = cast(s32)KssPlatformPlan9 + } else if KssNameEquals(KssMakeName(platform), "terminal") { + env.platform = cast(s32)KssPlatformTerminal + } + env.variant = KssMakeName(variant) + return env +} + +KssCursor :: struct { + source: string + pos: s32 + line: s32 + column: s32 + file: s32 +} + +KssName :: struct { + bytes: [64]u8 + length: s32 +} + +KssEnvironment :: struct { + theme: s32 + theme_name: KssName + contrast: s32 + density: s32 + pointer: s32 + platform: s32 + variant: KssName +} + +KssSourceFile :: struct { + name: [96]u8 + length: s32 +} + +KssToken :: struct { + name: KssName + kind: s32 + color: u32 + number: float32 + material: s32 + origin: s32 + file: s32 + line: s32 + column: s32 +} + +KssColorOverride :: struct { + name: string + color: u32 +} + +KssOrigin :: struct { + file: s32 + line: s32 + column: s32 +} + +// Source spans let hosts map typed rules back to their original text (the web +// CSS layer builds selectors and declarations from these byte ranges). + +KssRuleSpan :: struct { + file: s32 + selector_start: s32 + selector_length: s32 + body_start: s32 + body_length: s32 + group: s32 + end: s32 +} + +// Declarative hosts (the web CSS mapping) consume raw declaration text and +// foreign blocks instead of the closed property vocabulary. Structure lexing +// stays here; only value interpretation lives in the host. Entries are spans +// into the owning rule's source unit. + +KssDeclaration :: struct { + rule: s32 + name_start: s32 + name_length: s32 + value_start: s32 + value_length: s32 +} + +KssForeignBlock :: struct { + name: KssName + query_start: s32 + query_length: s32 + body_start: s32 + body_length: s32 + file: s32 +} + +KssImportFrame :: struct { + name: KssName + outer_source: string + outer_pos: s32 + outer_line: s32 + outer_column: s32 + outer_file: s32 +} + +KssVariantDecl :: struct { + name: KssName + label: KssName +} + +// Structural block spans: every top-level construct records the source range +// it occupies so tooling (the KSS formatter) can re-layout without re-parsing +// in the host. Kinds: 1 tokens, 2 theme, 3 env, 4 variant, 5 directive. + +KssBlockSpan :: struct { + kind: s32 + start: s32 + length: s32 +} + +KssParser :: struct { + cursor: KssCursor + files: [8]KssSourceFile + file_count: s32 + imports: [8]KssImportFrame + import_depth: s32 + tokens: [128]KssToken + token_count: s32 + overrides: [64]KssColorOverride + override_count: s32 + layer: s32 + layer_names: [12]KssName + layer_count: s32 + layer_declared: bool + pack: KssName + env: KssEnvironment + themes: [16]KssName + theme_count: s32 + version_seen: bool + status: s32 + rule: StyleRule + origin: KssOrigin + rule_span: KssRuleSpan + pending_import: KssName + in_env: bool + variants: [8]KssVariantDecl + variant_count: s32 + in_variant: bool + variant_label: KssName + blocks: [32]KssBlockSpan + block_count: s32 + resume_group: s32 + declarative: bool + // Beside the other flag, in its padding: the parser travels by value, + // and a larger one overflows small thread stacks (Android render thread). + blocks_overflow: bool + declarations: [1024]KssDeclaration + declaration_count: s32 + foreign: [16]KssForeignBlock + foreign_count: s32 + diagnostic: [256]u8 + diagnostic_length: s32 + rule_total: s32 +} + +#program_export +KssBegin :: (source: string, path: string, env: KssEnvironment) -> KssParser { + p: KssParser + p.cursor.source = source + p.cursor.pos = 0 + p.cursor.line = 1 + p.cursor.column = 1 + p.cursor.file = 0 + p.files[0] = KssCopyFileName(path) + p.file_count = 1 + p.import_depth = 0 + p.token_count = 0 + p.override_count = 0 + p.layer = 0 + p.layer_count = 4 + p.layer_names[0] = KssMakeName("reset") + p.layer_names[1] = KssMakeName("components") + p.layer_names[2] = KssMakeName("app") + p.layer_names[3] = KssMakeName("overrides") + p.layer_declared = false + p.env = env + p.theme_count = 0 + p.version_seen = false + p.status = KssStatusContinue + p.rule_total = 0 + p.declarative = false + p.declaration_count = 0 + p.foreign_count = 0 + p.resume_group = -1 + p.variant_count = 0 + p.block_count = 0 + p.blocks_overflow = false + p.diagnostic_length = 0 + return p +} + +// Declarative hosts consume raw declarations and foreign blocks instead of the +// closed property vocabulary; grammar and structure stay identical. + +#program_export +KssBeginDeclarative :: (source: string, path: string, env: KssEnvironment) -> KssParser { + p: KssParser = KssBegin(source, path, env) + p.declarative = true + return p +} + +#program_export +KssAddColorOverride :: (p: KssParser, name: string, color: u32) -> KssParser { + if p.override_count < 64 { + p.overrides[p.override_count].name = name + p.overrides[p.override_count].color = color + p.override_count = p.override_count + 1 + } + return p +} + +// The rest of the module, in this order. +#load "parser/lexer.zi"; +#load "parser/tokens.zi"; +#load "parser/vocabulary.zi"; +#load "parser/rules.zi"; +#load "parser/blocks.zi"; +#load "parser/directives.zi"; +#load "parser/css_values.zi"; +#load "parser/selectors.zi"; diff --git a/src/parser/blocks.zi b/src/parser/blocks.zi new file mode 100644 index 0000000..b35fd47 --- /dev/null +++ b/src/parser/blocks.zi @@ -0,0 +1,742 @@ +// Part of the kss_parser module, loaded by src/kss/kss_parser.zi. +// Environment, theme, and variant blocks; token blocks; layers; import targets. + +// Environment axes are closed and typed. Blocks match the active environment +// only; values are validated regardless of the match. + +KssEnvAxisValid :: (axis: KssName) -> bool { + return KssNameEquals(axis, "theme") || KssNameEquals(axis, "contrast") || + KssNameEquals(axis, "density") || KssNameEquals(axis, "pointer") || + KssNameEquals(axis, "platform") +} + +KssEnvValueValid :: (axis: KssName, value: KssName) -> bool { + if KssNameEquals(axis, "theme") { + return KssNameEquals(value, "light") || KssNameEquals(value, "dark") + } + if KssNameEquals(axis, "contrast") { + return KssNameEquals(value, "normal") || KssNameEquals(value, "high") + } + if KssNameEquals(axis, "density") { + return KssNameEquals(value, "compact") || + KssNameEquals(value, "comfortable") || KssNameEquals(value, "touch") + } + if KssNameEquals(axis, "pointer") { + return KssNameEquals(value, "mouse") || KssNameEquals(value, "touch") || + KssNameEquals(value, "mixed") + } + return KssNameEquals(value, "desktop") || KssNameEquals(value, "android") || + KssNameEquals(value, "web") || KssNameEquals(value, "plan9") || + KssNameEquals(value, "terminal") +} + +KssEnvMatches :: (p: KssParser, axis: KssName, value: KssName) -> bool { + if KssNameEquals(axis, "theme") { + if KssNameEquals(value, "light") { + return p.env.theme == cast(s32)KssThemeLight + } + return p.env.theme == cast(s32)KssThemeDark + } + if KssNameEquals(axis, "contrast") { + if KssNameEquals(value, "high") { + return p.env.contrast == cast(s32)KssContrastHigh + } + return p.env.contrast == cast(s32)KssContrastNormal + } + if KssNameEquals(axis, "density") { + if KssNameEquals(value, "compact") { + return p.env.density == cast(s32)KssDensityCompact + } + if KssNameEquals(value, "touch") { + return p.env.density == cast(s32)KssDensityTouch + } + return p.env.density == cast(s32)KssDensityComfortable + } + if KssNameEquals(axis, "pointer") { + if KssNameEquals(value, "touch") { + return p.env.pointer == cast(s32)KssPointerTouch + } + if KssNameEquals(value, "mixed") { + return p.env.pointer == cast(s32)KssPointerMixed + } + return p.env.pointer == cast(s32)KssPointerMouse + } + if KssNameEquals(value, "android") { + return p.env.platform == cast(s32)KssPlatformAndroid + } + if KssNameEquals(value, "web") { + return p.env.platform == cast(s32)KssPlatformWeb + } + if KssNameEquals(value, "plan9") { + return p.env.platform == cast(s32)KssPlatformPlan9 + } + if KssNameEquals(value, "terminal") { + return p.env.platform == cast(s32)KssPlatformTerminal + } + return p.env.platform == cast(s32)KssPlatformDesktop +} + +KssThemeActive :: (p: KssParser, name: KssName) -> bool { + if p.env.theme_name.length > 0 && + KssNameEqualsName(p.env.theme_name, name) { + return true + } + if KssNameEquals(name, "light") { + return p.env.theme == cast(s32)KssThemeLight + } + if KssNameEquals(name, "dark") { + return p.env.theme == cast(s32)KssThemeDark + } + return false +} + +// Skip the remainder of a block whose '{' is already consumed. Used for +// non-matching environment overlays. + +KssSkipBlockRemainder :: (p: KssParser) -> KssParser { + c: KssCursor = p.cursor + depth: s32 = 1 + while !KssAtEnd(c) && depth > 0 { + if c.source[c.pos] == 123 { + depth = depth + 1 + } else if c.source[c.pos] == 125 { + depth = depth - 1 + } + c = KssAdvance(c, 1) + } + p.cursor = c + if depth != 0 { + return KssFail(p, "unterminated block") + } + return p +} + +// One 'name: value;' overlay entry. The existing token fixes the value type; +// unknown names and value/type mismatches are errors. + +KssOverlayOutcome :: struct { + parser: KssParser + matched: bool +} + +KssTryOverlayEntry :: (p: KssParser, origin_kind: s32) -> KssOverlayOutcome { + c: KssCursor = p.cursor + outcome: KssOverlayOutcome + name: KssNameResult = KssReadName(c) + if !name.ok { + p.cursor = name.parser + outcome.parser = p + return outcome + } + colon: KssExpectResult = KssExpect(name.parser, 58) + if !colon.ok { + p.cursor = colon.parser + outcome.parser = p + return outcome + } + p.cursor = name.parser + token_index: s32 = -1 + scan: s32 = p.token_count - 1 + while scan >= 0 { + if KssNameEqualsName(p.tokens[scan].name, name.name) { + token_index = scan + } + scan = scan - 1 + } + if token_index < 0 { + p.cursor = colon.parser + outcome.parser = p + return outcome + } + kind: s32 = p.tokens[token_index].kind + if kind == cast(s32)KssTokenColor { + value: KssColorResult = KssReadHexColor(colon.parser) + if !value.ok { + p.cursor = colon.parser + outcome.parser = p + return outcome + } + semicolon: KssExpectResult = KssExpect(value.parser, 59) + if !semicolon.ok { + p.cursor = semicolon.parser + outcome.parser = p + return outcome + } + p.cursor = semicolon.parser + p = KssOverlayToken(p, name.name, kind, value.value, + 0.0, 0, origin_kind) + if p.status == cast(s32)KssStatusError { + outcome.parser = p + return outcome + } + outcome.parser = p + outcome.matched = true + return outcome + } + if kind == cast(s32)KssTokenMaterial { + p.cursor = colon.parser + value: KssIntResult = KssReadMaterialValue(p) + if !value.ok { + outcome.parser = value.parser + return outcome + } + semicolon: KssExpectResult = KssExpect(value.parser.cursor, 59) + if !semicolon.ok { + p.cursor = semicolon.parser + outcome.parser = p + return outcome + } + p.cursor = semicolon.parser + p = KssOverlayToken(p, name.name, kind, 0, 0.0, + value.value, origin_kind) + if p.status == cast(s32)KssStatusError { + outcome.parser = p + return outcome + } + outcome.parser = p + outcome.matched = true + return outcome + } + value: KssNumberResult = KssReadDuration(colon.parser) + if !value.ok { + value = KssReadNumber(colon.parser) + } + if !value.ok { + p.cursor = colon.parser + outcome.parser = p + return outcome + } + semicolon: KssExpectResult = KssExpect(value.parser, 59) + if !semicolon.ok { + p.cursor = semicolon.parser + outcome.parser = p + return outcome + } + p.cursor = semicolon.parser + p = KssOverlayToken(p, name.name, kind, 0, value.value, + 0, origin_kind) + if p.status == cast(s32)KssStatusError { + outcome.parser = p + return outcome + } + outcome.parser = p + outcome.matched = true + return outcome +} + +// '@theme name { entry... }': always validated, applied only when active. + +KssParseThemeBlock :: (p: KssParser, name: KssName) -> KssParser { + c: KssCursor = p.cursor + if p.theme_count >= 16 { + return KssFail(p, "style theme capacity exceeded") + } + p.themes[p.theme_count] = name + p.theme_count = p.theme_count + 1 + open: KssExpectResult = KssExpect(c, 123) + if !open.ok { + p.cursor = open.parser + return KssFail(p, "expected '{' after @theme name") + } + c = open.parser + p.cursor = c + active: bool = KssThemeActive(p, name) + running: bool = true + while running { + c = KssSkipSpace(c) + p.cursor = c + if KssAtEnd(c) { + return KssFail(p, "unterminated theme block") + } + if p.cursor.source[p.cursor.pos] == 125 { + c = KssAdvance(c, 1) + p.cursor = c + running = false + continue + } + if !active { + return KssSkipBlockRemainder(p) + } + entry: KssOverlayOutcome = KssTryOverlayEntry(p, cast(s32)KssOriginTheme) + if entry.parser.status == cast(s32)KssStatusError { + return entry.parser + } + if !entry.matched { + return KssFail(entry.parser, "expected theme overlay entry") + } + p = entry.parser + c = p.cursor + } + return p +} + +// '@env axis(value) { rules and token overlays }'. Matching blocks yield each +// rule to the host and resume inside the block on the next step; non-matching +// blocks are validated by shape and skipped. + +KssParseEnvBlock :: (p: KssParser, axis: KssName, value: KssName) -> KssParser { + c: KssCursor = p.cursor + if !KssEnvValueValid(axis, value) { + return KssFailName(p, "unknown environment value '", value) + } + open: KssExpectResult = KssExpect(c, 123) + if !open.ok { + p.cursor = open.parser + return KssFail(p, "expected '{' after @env query") + } + c = open.parser + p.cursor = c + if !KssEnvMatches(p, axis, value) { + return KssSkipBlockRemainder(p) + } + p.in_env = true + return KssContinueEnvBlock(p) +} + +KssContinueEnvBlock :: (p: KssParser) -> KssParser { + c: KssCursor = p.cursor + running: bool = true + while running { + c = KssSkipSpace(c) + p.cursor = c + if KssAtEnd(c) { + return KssFail(p, "unterminated environment block") + } + if c.source[c.pos] == 125 { + c = KssAdvance(c, 1) + p.cursor = c + p.in_env = false + running = false + continue + } + saved: KssParser = p + entry: KssOverlayOutcome = KssTryOverlayEntry(p, + cast(s32)KssOriginEnvironment) + if entry.parser.status == cast(s32)KssStatusError { + return entry.parser + } + if entry.matched { + p = entry.parser + c = p.cursor + continue + } + p = KssParseRule(saved) + if p.status == cast(s32)KssStatusError { + return p + } + return p + } + return p +} + + +// '@variant name "Label" { token overlays and rules }'. Variants are pack +// options declared in the sheet itself: every declaration is recorded (name +// and label) for host enumeration, and only the variant named by the parse +// environment contributes rules and token overlays. Overlay entries apply +// with the variant origin, above theme and environment. + +KssReadVariantLabel :: (c: KssCursor) -> KssNameResult { + result: KssNameResult + c = KssSkipSpace(c) + if KssAtEnd(c) || c.source[c.pos] != 34 { + result.parser = c + return result + } + c = KssAdvance(c, 1) + while !KssAtEnd(c) && c.source[c.pos] != 34 && !KssIsLineBreak(c.source[c.pos]) { + result.name = KssNameAppend(result.name, c.source[c.pos]) + c = KssAdvance(c, 1) + } + if KssAtEnd(c) || c.source[c.pos] != 34 { + result.parser = c + return result + } + c = KssAdvance(c, 1) + result.parser = c + result.ok = true + return result +} + +// Spans are positions in the top-level source, so constructs read from an +// import are not recorded. They serve tooling only: a full table marks the +// overflow for the formatter and never stops a parse. +KssRecordBlock :: (p: KssParser, kind: s32, start: s32, end_pos: s32) -> KssParser { + if p.import_depth > 0 { + return p + } + if p.block_count >= 32 { + p.blocks_overflow = true + return p + } + p.blocks[p.block_count].kind = kind + p.blocks[p.block_count].start = start + p.blocks[p.block_count].length = end_pos - start + p.block_count = p.block_count + 1 + return p +} + +#program_export +KssSetVariant :: (p: KssParser, name: KssName) -> KssParser { + p.env.variant = name + return p +} + +#program_export +KssSetTheme :: (p: KssParser, theme: s32) -> KssParser { + p.env.theme = theme + return p +} + +KssVariantActive :: (p: KssParser, name: KssName) -> bool { + if p.env.variant.length <= 0 { + return false + } + return KssNameEqualsName(p.env.variant, name) +} + +KssParseVariantBlock :: (p: KssParser, name: KssName) -> KssParser { + if p.variant_count >= 8 { + return KssFail(p, "style variant capacity exceeded") + } + p.variants[p.variant_count].name = name + p.variants[p.variant_count].label = p.variant_label + p.variant_count = p.variant_count + 1 + open: KssExpectResult = KssExpect(p.cursor, 123) + if !open.ok { + p.cursor = open.parser + return KssFail(p, "expected '{' after @variant name") + } + p.cursor = open.parser + if !KssVariantActive(p, name) { + return KssSkipBlockRemainder(p) + } + p.in_variant = true + return KssContinueVariantBlock(p) +} + +KssContinueVariantBlock :: (p: KssParser) -> KssParser { + c: KssCursor = p.cursor + running: bool = true + while running { + c = KssSkipSpace(c) + p.cursor = c + if KssAtEnd(c) { + return KssFail(p, "unterminated variant block") + } + if c.source[c.pos] == 125 { + c = KssAdvance(c, 1) + p.cursor = c + p.in_variant = false + running = false + continue + } + saved: KssParser = p + entry: KssOverlayOutcome = KssTryOverlayEntry(p, + cast(s32)KssOriginVariant) + if entry.parser.status == cast(s32)KssStatusError { + return entry.parser + } + if entry.matched { + p = entry.parser + c = p.cursor + continue + } + p = KssParseRule(saved) + if p.status == cast(s32)KssStatusError { + return p + } + return p + } + return p +} + +// 'tokens { group { name: value; ... } ... }' + +KssParseTokenGroup :: (p: KssParser, origin_kind: s32) -> KssParser { + c: KssCursor = p.cursor + group: KssNameResult = KssReadName(c) + if !group.ok { + p.cursor = group.parser + return KssFail(p, "expected token group") + } + kind: s32 = -1 + if KssNameEquals(group.name, "color") { + kind = cast(s32)KssTokenColor + } else if KssNameEquals(group.name, "length") || + KssNameEquals(group.name, "number") { + kind = cast(s32)KssTokenLength + } else if KssNameEquals(group.name, "duration") { + kind = cast(s32)KssTokenDuration + } else if KssNameEquals(group.name, "material") { + kind = cast(s32)KssTokenMaterial + } else { + p.cursor = group.parser + return KssFailName(p, "unknown token group '", group.name) + } + open: KssExpectResult = KssExpect(group.parser, 123) + if !open.ok { + p.cursor = open.parser + return KssFail(p, "expected '{' after token group") + } + c = open.parser + p.cursor = c + running: bool = true + while running { + token: KssToken + token.kind = kind + token.origin = origin_kind + token.file = c.file + c = KssSkipSpace(c) + p.cursor = c + if KssAtEnd(c) { + return KssFail(p, "unterminated token group") + } + if p.cursor.source[p.cursor.pos] == 125 { + c = KssAdvance(c, 1) + p.cursor = c + running = false + continue + } + token.line = p.cursor.line + token.column = p.cursor.column + name: KssNameResult = KssReadName(c) + if !name.ok { + p.cursor = name.parser + return KssFail(p, "expected token name") + } + token.name = name.name + colon: KssExpectResult = KssExpect(name.parser, 58) + if !colon.ok { + p.cursor = colon.parser + return KssFail(p, "expected ':' after token name") + } + if kind == cast(s32)KssTokenColor { + value: KssColorResult = KssReadHexColor(colon.parser) + if !value.ok { + p.cursor = value.parser + return KssFail(p, "expected token color") + } + token.color = value.value + c = value.parser + p.cursor = c + } else if kind == cast(s32)KssTokenMaterial { + p.cursor = colon.parser + value: KssIntResult = KssReadMaterialValue(p) + if !value.ok { + return value.parser + } + token.material = value.value + c = value.parser.cursor + } else if kind == cast(s32)KssTokenDuration { + value: KssNumberResult = KssReadDuration(colon.parser) + if !value.ok { + p.cursor = value.parser + return KssFail(p, "expected token duration") + } + token.number = value.value + c = value.parser + p.cursor = c + } else { + value: KssNumberResult = KssReadNumber(colon.parser) + if !value.ok { + p.cursor = value.parser + return KssFail(p, "expected token number") + } + token.number = value.value + c = value.parser + p.cursor = c + } + semicolon: KssExpectResult = KssExpect(c, 59) + if !semicolon.ok { + p.cursor = semicolon.parser + return KssFail(p, "expected ';'") + } + c = semicolon.parser + p.cursor = c + p = KssAddToken(p, token) + if p.status == cast(s32)KssStatusError { + return p + } + } + return p +} + +KssParseTokensBlock :: (p: KssParser, origin_kind: s32) -> KssParser { + c: KssCursor = p.cursor + open: KssExpectResult = KssExpect(c, 123) + if !open.ok { + p.cursor = open.parser + return KssFail(p, "expected '{' after tokens") + } + c = open.parser + p.cursor = c + running: bool = true + while running { + c = KssSkipSpace(c) + p.cursor = c + if KssAtEnd(c) { + return KssFail(p, "unterminated tokens block") + } + if c.source[c.pos] == 125 { + c = KssAdvance(c, 1) + p.cursor = c + running = false + continue + } + p = KssParseTokenGroup(p, origin_kind) + if p.status == cast(s32)KssStatusError { + return p + } + c = p.cursor + } + return p +} + +// Directives: @pack, @version, @layer, @import, @theme, @env. + +KssParseLayerName :: (p: KssParser, name: KssName) -> KssParser { + index: s32 = 0 + while index < p.layer_count { + if KssNameEqualsName(p.layer_names[index], name) { + p.layer = index + return p + } + index = index + 1 + } + if !p.layer_declared { + if KssNameEquals(name, "reset") || KssNameEquals(name, "base") || + KssNameEquals(name, "defaults") { + p.layer = 0 + return p + } + if KssNameEquals(name, "components") || KssNameEquals(name, "widgets") { + p.layer = 1 + return p + } + if KssNameEquals(name, "app") { + p.layer = 2 + return p + } + if KssNameEquals(name, "overrides") { + p.layer = 3 + return p + } + } + return KssFailName(p, "unknown layer '", name) +} + +KssParseLayerDeclaration :: (p: KssParser) -> KssParser { + c: KssCursor = p.cursor + if p.layer_declared { + return KssFail(p, "layer order declared twice") + } + p.layer_count = 0 + p.layer = 0 + running: bool = true + while running { + name: KssNameResult = KssReadName(c) + if !name.ok { + p.cursor = name.parser + return KssFail(p, "expected layer name") + } + if p.layer_count >= 12 { + return KssFail(p, "layer capacity exceeded") + } + p.layer_names[p.layer_count] = name.name + p.layer_count = p.layer_count + 1 + c = name.parser + p.cursor = c + c = KssSkipSpace(c) + p.cursor = c + if !KssAtEnd(c) && p.cursor.source[p.cursor.pos] == 44 { + c = KssAdvance(c, 1) + p.cursor = c + continue + } + running = false + } + p.layer_declared = true + return p +} + +KssReadImportTarget :: (p: KssParser) -> KssNameResult { + result: KssNameResult + c: KssCursor = KssSkipSpace(p.cursor) + if KssAtEnd(c) { + result.parser = c + return result + } + if c.source[c.pos] == 60 { + c = KssAdvance(c, 1) + while !KssAtEnd(c) && KssImportTargetByte(c.source[c.pos]) { + result.name = KssNameAppend(result.name, c.source[c.pos]) + c = KssAdvance(c, 1) + } + if KssAtEnd(c) || c.source[c.pos] != 62 { + result.parser = c + return result + } + c = KssAdvance(c, 1) + result.parser = c + result.ok = result.name.length > 0 + return result + } + if c.source[c.pos] == 34 { + c = KssAdvance(c, 1) + while !KssAtEnd(c) && KssImportTargetByte(c.source[c.pos]) { + result.name = KssNameAppend(result.name, c.source[c.pos]) + c = KssAdvance(c, 1) + } + if KssAtEnd(c) || c.source[c.pos] != 34 { + result.parser = c + return result + } + c = KssAdvance(c, 1) + result.parser = c + result.ok = result.name.length > 0 + return result + } + result.parser = c + return result +} + +KssParseLayerList :: (p: KssParser, block_start: s32) -> KssParser { + layer_start: s32 = block_start + c: KssCursor = KssSkipSpace(p.cursor) + p.cursor = c + if KssAtEnd(c) { + return KssFail(p, "expected layer name") + } + first: KssNameResult = KssReadName(c) + if !first.ok { + p.cursor = first.parser + return KssFail(p, "expected layer name") + } + c = first.parser + p.cursor = c + c = KssSkipSpace(c) + p.cursor = c + if !KssAtEnd(c) && p.cursor.source[p.cursor.pos] == 44 { + c = KssAdvance(c, 1) + p.cursor = c + p = KssParseLayerDeclaration(p) + if p.status == cast(s32)KssStatusError { + return p + } + c = p.cursor + } else { + p = KssParseLayerName(p, first.name) + if p.status == cast(s32)KssStatusError { + return p + } + c = p.cursor + } + semicolon: KssExpectResult = KssExpect(c, 59) + if !semicolon.ok { + p.cursor = semicolon.parser + return KssFail(p, "expected ';'") + } + p.cursor = semicolon.parser + return KssRecordBlock(p, 5, layer_start, p.cursor.pos) +} diff --git a/src/parser/css_values.zi b/src/parser/css_values.zi new file mode 100644 index 0000000..2b2808c --- /dev/null +++ b/src/parser/css_values.zi @@ -0,0 +1,953 @@ +// Part of the kss_parser module, loaded by src/kss/kss_parser.zi. +// CSS export values for declarative hosts. + +// CSS export values use the shared KSS vocabulary and numeric/token grammar. +// Native-only StyleData consumers retain their closed set of fields; CSS-only +// properties stay opaque after classification instead of becoming native fields. + +KssCSSValueKind :: enum { + KssCSSLiteral :: 0 + KssCSSNumber :: 1 + KssCSSColor :: 2 + KssCSSMaterial :: 3 +} + +KssCSSValue :: struct { + valid: bool + kind: s32 + number: float64 + color: u32 + material: s32 + text: string + has_token: bool + token_name: KssName + token_origin: s32 +} + +#program_export +KssCSSPropertyKind :: (name: KssName) -> s32 { + if name.length > 2 && name.bytes[0] == 45 && name.bytes[1] == 45 { + return -2 + } + if KssNameEquals(name, "background") || + KssNameEquals(name, "foreground") || + KssNameEquals(name, "border") || + KssNameEquals(name, "focus") || + KssNameEquals(name, "background-end") || + KssNameEquals(name, "color") || + KssNameEquals(name, "background-color") || + KssNameEquals(name, "accent-color") { + return 0 + } + if KssNameEquals(name, "caret-color") || + KssNameEquals(name, "border-color") || + KssNameEquals(name, "border-top-color") || + KssNameEquals(name, "border-right-color") || + KssNameEquals(name, "border-bottom-color") || + KssNameEquals(name, "border-left-color") || + KssNameEquals(name, "border-inline-color") || + KssNameEquals(name, "border-block-color") { + return 0 + } + if KssNameEquals(name, "border-inline-start-color") || + KssNameEquals(name, "border-inline-end-color") || + KssNameEquals(name, "border-block-start-color") || + KssNameEquals(name, "border-block-end-color") || + KssNameEquals(name, "outline-color") || + KssNameEquals(name, "text-decoration-color") || + KssNameEquals(name, "text-emphasis-color") || + KssNameEquals(name, "column-rule-color") { + return 0 + } + if KssNameEquals(name, "radius") || + KssNameEquals(name, "border-radius") || + KssNameEquals(name, "border-width") || + KssNameEquals(name, "opacity") || + KssNameEquals(name, "border-top-width") || + KssNameEquals(name, "border-right-width") || + KssNameEquals(name, "border-bottom-width") || + KssNameEquals(name, "border-left-width") { + return 1 + } + if KssNameEquals(name, "border-inline-width") || + KssNameEquals(name, "border-block-width") || + KssNameEquals(name, "border-inline-start-width") || + KssNameEquals(name, "border-inline-end-width") || + KssNameEquals(name, "border-block-start-width") || + KssNameEquals(name, "border-block-end-width") || + KssNameEquals(name, "border-top-left-radius") || + KssNameEquals(name, "border-top-right-radius") { + return 1 + } + if KssNameEquals(name, "border-bottom-right-radius") || + KssNameEquals(name, "border-bottom-left-radius") || + KssNameEquals(name, "border-start-start-radius") || + KssNameEquals(name, "border-start-end-radius") || + KssNameEquals(name, "border-end-start-radius") || + KssNameEquals(name, "border-end-end-radius") || + KssNameEquals(name, "padding") || + KssNameEquals(name, "padding-x") { + return 1 + } + if KssNameEquals(name, "padding-y") || + KssNameEquals(name, "padding-left") || + KssNameEquals(name, "padding-right") || + KssNameEquals(name, "padding-top") || + KssNameEquals(name, "padding-bottom") || + KssNameEquals(name, "padding-inline") || + KssNameEquals(name, "padding-block") || + KssNameEquals(name, "padding-inline-start") { + return 1 + } + if KssNameEquals(name, "padding-inline-end") || + KssNameEquals(name, "padding-block-start") || + KssNameEquals(name, "padding-block-end") || + KssNameEquals(name, "margin") || + KssNameEquals(name, "margin-x") || + KssNameEquals(name, "margin-y") || + KssNameEquals(name, "margin-left") || + KssNameEquals(name, "margin-right") { + return 1 + } + if KssNameEquals(name, "margin-top") || + KssNameEquals(name, "margin-bottom") || + KssNameEquals(name, "margin-inline") || + KssNameEquals(name, "margin-block") || + KssNameEquals(name, "margin-inline-start") || + KssNameEquals(name, "margin-inline-end") || + KssNameEquals(name, "margin-block-start") || + KssNameEquals(name, "margin-block-end") { + return 1 + } + if KssNameEquals(name, "width") || + KssNameEquals(name, "height") || + KssNameEquals(name, "min-width") || + KssNameEquals(name, "max-width") || + KssNameEquals(name, "min-height") || + KssNameEquals(name, "max-height") || + KssNameEquals(name, "inline-size") || + KssNameEquals(name, "block-size") { + return 1 + } + if KssNameEquals(name, "min-inline-size") || + KssNameEquals(name, "max-inline-size") || + KssNameEquals(name, "min-block-size") || + KssNameEquals(name, "max-block-size") || + KssNameEquals(name, "inset") || + KssNameEquals(name, "top") || + KssNameEquals(name, "right") || + KssNameEquals(name, "bottom") { + return 1 + } + if KssNameEquals(name, "left") || + KssNameEquals(name, "inset-inline") || + KssNameEquals(name, "inset-block") || + KssNameEquals(name, "inset-inline-start") || + KssNameEquals(name, "inset-inline-end") || + KssNameEquals(name, "inset-block-start") || + KssNameEquals(name, "inset-block-end") || + KssNameEquals(name, "gap") { + return 1 + } + if KssNameEquals(name, "row-gap") || + KssNameEquals(name, "column-gap") || + KssNameEquals(name, "font-size") || + KssNameEquals(name, "letter-spacing") || + KssNameEquals(name, "line-height") || + KssNameEquals(name, "flex-basis") || + KssNameEquals(name, "text-indent") || + KssNameEquals(name, "text-decoration-thickness") { + return 1 + } + if KssNameEquals(name, "text-underline-offset") || + KssNameEquals(name, "vertical-align") || + KssNameEquals(name, "perspective") || + KssNameEquals(name, "offset-distance") || + KssNameEquals(name, "outline-width") || + KssNameEquals(name, "outline-offset") || + KssNameEquals(name, "tab-size") || + KssNameEquals(name, "column-count") { + return 1 + } + if KssNameEquals(name, "column-width") || + KssNameEquals(name, "column-rule-width") || + KssNameEquals(name, "border-spacing") || + KssNameEquals(name, "contain-intrinsic-size") || + KssNameEquals(name, "contain-intrinsic-width") || + KssNameEquals(name, "contain-intrinsic-height") || + KssNameEquals(name, "contain-intrinsic-inline-size") || + KssNameEquals(name, "contain-intrinsic-block-size") { + return 1 + } + if KssNameEquals(name, "overflow-clip-margin") || + KssNameEquals(name, "shape-margin") || + KssNameEquals(name, "scroll-margin") || + KssNameEquals(name, "scroll-margin-top") || + KssNameEquals(name, "scroll-margin-right") || + KssNameEquals(name, "scroll-margin-bottom") || + KssNameEquals(name, "scroll-margin-left") || + KssNameEquals(name, "scroll-margin-inline") { + return 1 + } + if KssNameEquals(name, "scroll-margin-block") || + KssNameEquals(name, "scroll-margin-inline-start") || + KssNameEquals(name, "scroll-margin-inline-end") || + KssNameEquals(name, "scroll-margin-block-start") || + KssNameEquals(name, "scroll-margin-block-end") || + KssNameEquals(name, "scroll-padding") || + KssNameEquals(name, "scroll-padding-top") || + KssNameEquals(name, "scroll-padding-right") { + return 1 + } + if KssNameEquals(name, "scroll-padding-bottom") || + KssNameEquals(name, "scroll-padding-left") || + KssNameEquals(name, "scroll-padding-inline") || + KssNameEquals(name, "scroll-padding-block") || + KssNameEquals(name, "scroll-padding-inline-start") || + KssNameEquals(name, "scroll-padding-inline-end") || + KssNameEquals(name, "scroll-padding-block-start") || + KssNameEquals(name, "scroll-padding-block-end") { + return 1 + } + if KssNameEquals(name, "icon-size") || + KssNameEquals(name, "offset-x") || + KssNameEquals(name, "offset-y") || + KssNameEquals(name, "content-offset-x") || + KssNameEquals(name, "content-offset-y") { + return 1 + } + if KssNameEquals(name, "material") { + return 3 + } + if KssNameEquals(name, "font") || + KssNameEquals(name, "typeface") || + KssNameEquals(name, "font-family") || + KssNameEquals(name, "font-weight") || + KssNameEquals(name, "font-style") || + KssNameEquals(name, "font-variant") || + KssNameEquals(name, "font-stretch") || + KssNameEquals(name, "font-kerning") { + return -2 + } + if KssNameEquals(name, "font-optical-sizing") || + KssNameEquals(name, "font-feature-settings") || + KssNameEquals(name, "font-variation-settings") || + KssNameEquals(name, "font-size-adjust") || + KssNameEquals(name, "font-synthesis") || + KssNameEquals(name, "font-synthesis-weight") || + KssNameEquals(name, "font-synthesis-style") || + KssNameEquals(name, "font-synthesis-small-caps") { + return -2 + } + if KssNameEquals(name, "font-synthesis-position") || + KssNameEquals(name, "font-variant-alternates") || + KssNameEquals(name, "font-variant-caps") || + KssNameEquals(name, "font-variant-east-asian") || + KssNameEquals(name, "font-variant-ligatures") || + KssNameEquals(name, "font-variant-numeric") || + KssNameEquals(name, "font-variant-position") || + KssNameEquals(name, "font-language-override") { + return -2 + } + if KssNameEquals(name, "font-palette") || + KssNameEquals(name, "text-align") || + KssNameEquals(name, "text-align-last") || + KssNameEquals(name, "text-rendering") || + KssNameEquals(name, "text-decoration") || + KssNameEquals(name, "text-decoration-line") || + KssNameEquals(name, "text-decoration-style") || + KssNameEquals(name, "text-decoration-skip") { + return -2 + } + if KssNameEquals(name, "text-decoration-skip-ink") || + KssNameEquals(name, "text-underline-position") || + KssNameEquals(name, "text-shadow") || + KssNameEquals(name, "text-emphasis") || + KssNameEquals(name, "text-emphasis-style") || + KssNameEquals(name, "text-emphasis-position") || + KssNameEquals(name, "text-transform") || + KssNameEquals(name, "text-overflow") { + return -2 + } + if KssNameEquals(name, "white-space") || + KssNameEquals(name, "text-size-adjust") || + KssNameEquals(name, "text-orientation") || + KssNameEquals(name, "text-wrap") || + KssNameEquals(name, "text-wrap-mode") || + KssNameEquals(name, "text-wrap-style") || + KssNameEquals(name, "text-justify") || + KssNameEquals(name, "line-break") { + return -2 + } + if KssNameEquals(name, "hanging-punctuation") || + KssNameEquals(name, "text-combine-upright") || + KssNameEquals(name, "ruby-align") || + KssNameEquals(name, "ruby-position") || + KssNameEquals(name, "text-spacing-trim") || + KssNameEquals(name, "text-autospace") || + KssNameEquals(name, "text-box-trim") || + KssNameEquals(name, "text-box-edge") { + return -2 + } + if KssNameEquals(name, "word-break") || + KssNameEquals(name, "overflow-wrap") || + KssNameEquals(name, "word-wrap") || + KssNameEquals(name, "display") || + KssNameEquals(name, "position") || + KssNameEquals(name, "z-index") || + KssNameEquals(name, "overflow") || + KssNameEquals(name, "overflow-inline") { + return -2 + } + if KssNameEquals(name, "overflow-block") || + KssNameEquals(name, "border-top") || + KssNameEquals(name, "border-right") || + KssNameEquals(name, "border-bottom") || + KssNameEquals(name, "border-left") || + KssNameEquals(name, "border-inline") || + KssNameEquals(name, "border-block") || + KssNameEquals(name, "border-inline-start") { + return -2 + } + if KssNameEquals(name, "border-inline-end") || + KssNameEquals(name, "border-block-start") || + KssNameEquals(name, "border-block-end") || + KssNameEquals(name, "border-style") || + KssNameEquals(name, "border-top-style") || + KssNameEquals(name, "border-right-style") || + KssNameEquals(name, "border-bottom-style") || + KssNameEquals(name, "border-left-style") { + return -2 + } + if KssNameEquals(name, "border-inline-style") || + KssNameEquals(name, "border-block-style") || + KssNameEquals(name, "border-inline-start-style") || + KssNameEquals(name, "border-inline-end-style") || + KssNameEquals(name, "border-block-start-style") || + KssNameEquals(name, "border-block-end-style") || + KssNameEquals(name, "border-image") || + KssNameEquals(name, "border-image-source") { + return -2 + } + if KssNameEquals(name, "border-image-slice") || + KssNameEquals(name, "border-image-width") || + KssNameEquals(name, "border-image-outset") || + KssNameEquals(name, "border-image-repeat") || + KssNameEquals(name, "overflow-x") || + KssNameEquals(name, "overflow-y") || + KssNameEquals(name, "box-sizing") || + KssNameEquals(name, "direction") { + return -2 + } + if KssNameEquals(name, "writing-mode") || + KssNameEquals(name, "hyphens") || + KssNameEquals(name, "line-clamp") || + KssNameEquals(name, "list-style") || + KssNameEquals(name, "list-style-type") || + KssNameEquals(name, "list-style-position") || + KssNameEquals(name, "list-style-image") || + KssNameEquals(name, "counter-reset") { + return -2 + } + if KssNameEquals(name, "counter-increment") || + KssNameEquals(name, "counter-set") || + KssNameEquals(name, "quotes") || + KssNameEquals(name, "marker-side") || + KssNameEquals(name, "marker-start") || + KssNameEquals(name, "marker-end") || + KssNameEquals(name, "orphans") || + KssNameEquals(name, "widows") { + return -2 + } + if KssNameEquals(name, "box-decoration-break") || + KssNameEquals(name, "border-collapse") || + KssNameEquals(name, "table-layout") || + KssNameEquals(name, "caption-side") || + KssNameEquals(name, "empty-cells") || + KssNameEquals(name, "scroll-behavior") || + KssNameEquals(name, "overscroll-behavior") || + KssNameEquals(name, "overscroll-behavior-x") { + return -2 + } + if KssNameEquals(name, "overscroll-behavior-y") || + KssNameEquals(name, "overscroll-behavior-inline") || + KssNameEquals(name, "overscroll-behavior-block") || + KssNameEquals(name, "scroll-snap-type") || + KssNameEquals(name, "scroll-snap-align") || + KssNameEquals(name, "scroll-snap-stop") || + KssNameEquals(name, "scrollbar-color") || + KssNameEquals(name, "scrollbar-width") { + return -2 + } + if KssNameEquals(name, "scrollbar-gutter") || + KssNameEquals(name, "touch-action") || + KssNameEquals(name, "align-items") || + KssNameEquals(name, "justify-content") || + KssNameEquals(name, "align-self") || + KssNameEquals(name, "justify-self") || + KssNameEquals(name, "flex-direction") || + KssNameEquals(name, "flex-wrap") { + return -2 + } + if KssNameEquals(name, "flex-flow") || + KssNameEquals(name, "flex") || + KssNameEquals(name, "flex-grow") || + KssNameEquals(name, "flex-shrink") || + KssNameEquals(name, "grid") || + KssNameEquals(name, "grid-template") || + KssNameEquals(name, "grid-template-columns") || + KssNameEquals(name, "grid-template-rows") { + return -2 + } + if KssNameEquals(name, "grid-template-areas") || + KssNameEquals(name, "grid-auto-columns") || + KssNameEquals(name, "grid-auto-rows") || + KssNameEquals(name, "grid-auto-flow") || + KssNameEquals(name, "grid-column") || + KssNameEquals(name, "grid-column-start") || + KssNameEquals(name, "grid-column-end") || + KssNameEquals(name, "grid-area") { + return -2 + } + if KssNameEquals(name, "grid-row") || + KssNameEquals(name, "grid-row-start") || + KssNameEquals(name, "grid-row-end") || + KssNameEquals(name, "align-content") || + KssNameEquals(name, "justify-items") || + KssNameEquals(name, "place-items") || + KssNameEquals(name, "place-content") || + KssNameEquals(name, "place-self") { + return -2 + } + if KssNameEquals(name, "align-tracks") || + KssNameEquals(name, "justify-tracks") || + KssNameEquals(name, "object-fit") || + KssNameEquals(name, "object-position") || + KssNameEquals(name, "object-view-box") || + KssNameEquals(name, "aspect-ratio") || + KssNameEquals(name, "image-rendering") || + KssNameEquals(name, "image-orientation") { + return -2 + } + if KssNameEquals(name, "image-resolution") || + KssNameEquals(name, "background-image") || + KssNameEquals(name, "background-size") || + KssNameEquals(name, "background-position") || + KssNameEquals(name, "background-position-x") || + KssNameEquals(name, "background-position-y") || + KssNameEquals(name, "background-repeat") || + KssNameEquals(name, "background-repeat-x") { + return -2 + } + if KssNameEquals(name, "background-repeat-y") || + KssNameEquals(name, "background-clip") || + KssNameEquals(name, "background-origin") || + KssNameEquals(name, "background-attachment") || + KssNameEquals(name, "background-blend-mode") || + KssNameEquals(name, "visibility") || + KssNameEquals(name, "transition") || + KssNameEquals(name, "transition-property") { + return -2 + } + if KssNameEquals(name, "transition-duration") || + KssNameEquals(name, "transition-timing-function") || + KssNameEquals(name, "transition-delay") || + KssNameEquals(name, "transition-behavior") || + KssNameEquals(name, "animation") || + KssNameEquals(name, "animation-name") || + KssNameEquals(name, "animation-duration") || + KssNameEquals(name, "animation-timing-function") { + return -2 + } + if KssNameEquals(name, "animation-delay") || + KssNameEquals(name, "animation-iteration-count") || + KssNameEquals(name, "animation-direction") || + KssNameEquals(name, "animation-fill-mode") || + KssNameEquals(name, "animation-play-state") || + KssNameEquals(name, "animation-composition") || + KssNameEquals(name, "animation-timeline") || + KssNameEquals(name, "animation-range") { + return -2 + } + if KssNameEquals(name, "animation-range-start") || + KssNameEquals(name, "animation-range-end") || + KssNameEquals(name, "scroll-timeline") || + KssNameEquals(name, "scroll-timeline-name") || + KssNameEquals(name, "scroll-timeline-axis") || + KssNameEquals(name, "view-timeline") || + KssNameEquals(name, "view-timeline-name") || + KssNameEquals(name, "view-timeline-axis") { + return -2 + } + if KssNameEquals(name, "view-timeline-inset") || + KssNameEquals(name, "timeline-scope") || + KssNameEquals(name, "transform") || + KssNameEquals(name, "transform-origin") || + KssNameEquals(name, "transform-box") || + KssNameEquals(name, "transform-style") || + KssNameEquals(name, "translate") || + KssNameEquals(name, "rotate") { + return -2 + } + if KssNameEquals(name, "scale") || + KssNameEquals(name, "perspective-origin") || + KssNameEquals(name, "backface-visibility") || + KssNameEquals(name, "offset-path") || + KssNameEquals(name, "offset-rotate") || + KssNameEquals(name, "offset-anchor") || + KssNameEquals(name, "offset-position") || + KssNameEquals(name, "filter") { + return -2 + } + if KssNameEquals(name, "backdrop-filter") || + KssNameEquals(name, "clip-path") || + KssNameEquals(name, "mask") || + KssNameEquals(name, "mask-image") || + KssNameEquals(name, "mask-size") || + KssNameEquals(name, "mask-position") || + KssNameEquals(name, "mask-repeat") || + KssNameEquals(name, "mask-origin") { + return -2 + } + if KssNameEquals(name, "mask-clip") || + KssNameEquals(name, "mask-composite") || + KssNameEquals(name, "mask-mode") || + KssNameEquals(name, "cursor") || + KssNameEquals(name, "pointer-events") || + KssNameEquals(name, "appearance") || + KssNameEquals(name, "user-select") || + KssNameEquals(name, "resize") { + return -2 + } + if KssNameEquals(name, "outline") || + KssNameEquals(name, "outline-style") || + KssNameEquals(name, "box-shadow") || + KssNameEquals(name, "color-scheme") || + KssNameEquals(name, "field-sizing") || + KssNameEquals(name, "interpolate-size") || + KssNameEquals(name, "overlay") || + KssNameEquals(name, "forced-color-adjust") { + return -2 + } + if KssNameEquals(name, "print-color-adjust") || + KssNameEquals(name, "color-interpolation") || + KssNameEquals(name, "color-interpolation-filters") || + KssNameEquals(name, "paint-order") || + KssNameEquals(name, "shape-outside") || + KssNameEquals(name, "shape-image-threshold") || + KssNameEquals(name, "contain") || + KssNameEquals(name, "content-visibility") { + return -2 + } + if KssNameEquals(name, "contain-intrinsic-size") || + KssNameEquals(name, "contain-intrinsic-width") || + KssNameEquals(name, "contain-intrinsic-height") || + KssNameEquals(name, "contain-intrinsic-inline-size") || + KssNameEquals(name, "contain-intrinsic-block-size") || + KssNameEquals(name, "container") || + KssNameEquals(name, "container-type") || + KssNameEquals(name, "container-name") { + return -2 + } + if KssNameEquals(name, "will-change") || + KssNameEquals(name, "anchor-name") || + KssNameEquals(name, "position-anchor") || + KssNameEquals(name, "position-area") || + KssNameEquals(name, "position-try") || + KssNameEquals(name, "position-try-fallbacks") || + KssNameEquals(name, "position-try-order") || + KssNameEquals(name, "position-visibility") { + return -2 + } + if KssNameEquals(name, "view-transition-name") || + KssNameEquals(name, "isolation") || + KssNameEquals(name, "mix-blend-mode") || + KssNameEquals(name, "columns") || + KssNameEquals(name, "column-fill") || + KssNameEquals(name, "column-span") || + KssNameEquals(name, "column-rule") || + KssNameEquals(name, "column-rule-style") { + return -2 + } + if KssNameEquals(name, "break-before") || + KssNameEquals(name, "break-after") || + KssNameEquals(name, "break-inside") || + KssNameEquals(name, "float") || + KssNameEquals(name, "clear") || + KssNameEquals(name, "order") { + return -2 + } + return -1 +} + +// CSS property vocabulary reuses declaration classification. These names are +// canonical KSS spellings; DOM camel-case names are only accepted by unit policy. +#program_export +KssCSSPropertyName :: (name: string) -> string { + if name.count >= 2 && name[0] == 45 && name[1] == 45 { + return name + } + if name.count > 64 { + return "" + } + index: s32 = 0 + while index < cast(s32)name.count { + if name[index] != KssLower(name[index]) { + return "" + } + index = index + 1 + } + if name == "foreground" { + return "color" + } + if name == "border" { + return "border-color" + } + if name == "radius" { + return "border-radius" + } + if name == "padding-x" { + return "padding-left" + } + if name == "padding-y" { + return "padding-top" + } + if name == "margin-x" { + return "margin-left" + } + if name == "margin-y" { + return "margin-top" + } + if name == "typeface" { + return "font-family" + } + if name == "focus" { + return "outline-color" + } + if name == "material" || name == "content-offset-x" || + name == "content-offset-y" || name == "icon-size" || + name == "background-end" || name == "offset-x" || name == "offset-y" { + return "" + } + if KssCSSPropertyKind(KssMakeName(name)) == -1 { + return "" + } + return name +} + +// Whether a numeric host value receives px. String values keep their units. +#program_export +KssCSSNeedsPixels :: (name: string) -> bool { + if name == "opacity" || + name == "font-weight" || + name == "fontWeight" || + name == "line-height" || + name == "lineHeight" || + name == "z-index" { + return false + } + if name == "zIndex" || + name == "tab-size" || + name == "tabSize" || + name == "font-size-adjust" || + name == "fontSizeAdjust" || + name == "column-count" { + return false + } + if name == "columnCount" || + name == "order" || + name == "flex-grow" || + name == "flexGrow" || + name == "flex-shrink" || + name == "flexShrink" { + return false + } + if name == "border-image-slice" || + name == "borderImageSlice" || + name == "border-image-width" || + name == "borderImageWidth" || + name == "border-image-outset" || + name == "borderImageOutset" { + return false + } + if name == "grid-column-start" || + name == "gridColumnStart" || + name == "grid-column-end" || + name == "gridColumnEnd" || + name == "grid-row-start" || + name == "gridRowStart" { + return false + } + if name == "grid-row-end" || + name == "gridRowEnd" || + name == "line-clamp" || + name == "lineClamp" || + name == "webkitLineClamp" || name == "-webkit-line-clamp" || + name == "orphans" { + return false + } + if name == "widows" || + name == "animation-iteration-count" || + name == "animationIterationCount" || + name == "shape-image-threshold" || + name == "shapeImageThreshold" || + name == "scale" { + return false + } + return true +} + +// UTF-8 whitespace accepted by the existing CSS text adapter. +KssCSSSpaceLength :: (value: string, index: s32) -> s32 { + if KssIsSpace(value[index]) { + return 1 + } + if index + 1 < (cast(s32)value.count) && value[index] == 194 && value[index + 1] == 160 { + return 2 + } + if index + 2 >= (cast(s32)value.count) { + return 0 + } + first: u8 = value[index] + second: u8 = value[index + 1] + third: u8 = value[index + 2] + if first == 225 && second == 154 && third == 128 { + return 3 + } + if first == 226 && second == 128 && + ((third >= 128 && third <= 138) || third == 168 || third == 169 || third == 175) { + return 3 + } + if first == 226 && second == 129 && third == 159 { + return 3 + } + if first == 227 && second == 128 && third == 128 { + return 3 + } + if first == 239 && second == 187 && third == 191 { + return 3 + } + return 0 +} + +// Preserve KSS border's color alias unless its trimmed value has whitespace. +#program_export +KssCSSBorderShorthand :: (value: string) -> bool { + index: s32 = 0 + content: bool = false + space_after_content: bool = false + while index < (cast(s32)value.count) { + space: s32 = KssCSSSpaceLength(value, index) + if space > 0 { + if content { + space_after_content = true + } + index = index + space + } else { + if space_after_content { + return true + } + content = true + index = index + 1 + } + } + return false +} + +// Declaration expansion returns borrowed property names. Hosts emit the value +// for each nonempty name, using the shared numeric-unit policy. +KssCSSExpansion :: struct { + first: string + second: string +} + +#program_export +KssCSSExpandDeclaration :: (name: string, value: string, has_offsets: bool) -> KssCSSExpansion { + result: KssCSSExpansion + if value == "" || name == "offset-x" || name == "offset-y" { + return result + } + if name == "transform" && has_offsets { + return result + } + if name == "content-offset-x" { + result.first = "--kryon-content-offset-x" + } else if name == "content-offset-y" { + result.first = "--kryon-content-offset-y" + } else if name == "icon-size" { + result.first = "--kryon-icon-size" + } else if name == "background-end" { + result.first = "--kryon-background-end" + } else if name == "border" && KssCSSBorderShorthand(value) { + result.first = "border" + } else { + result.first = KssCSSPropertyName(name) + } + if name == "padding-x" { + result.second = "padding-right" + } else if name == "padding-y" { + result.second = "padding-bottom" + } else if name == "margin-x" { + result.second = "margin-right" + } else if name == "margin-y" { + result.second = "margin-bottom" + } else if name == "line-clamp" { + result.second = "-webkit-line-clamp" + } else if name == "box-decoration-break" { + result.second = "-webkit-box-decoration-break" + } + return result +} + +// An authored nonzero border width or border paint requests a visible border. +// Emit this before authored declarations so explicit styles and sides win. +#program_export +KssCSSBorderDefault :: (border: string, width: string) -> string { + paint: KssCSSValue = KssCSSValueFromText(border) + size: KssCSSValue = KssCSSValueFromText(width) + if (paint.text != "" && (paint.kind != cast(s32)KssCSSNumber || paint.number != 0)) || + (size.text != "" && (size.kind != cast(s32)KssCSSNumber || size.number != 0)) { + return "solid" + } + return "" +} + +// Values already have their CSS units. Empty text means absent, not zero. +KssCSSEffectFacts :: struct { + background: string + background_end: string + background_image: string + offset_x: string + offset_y: string + transform: string +} + +// A borrowed-fragment output recipe avoids a fixed output buffer or allocation. +// Emit name: prefix + first + separator + second + suffix when name is nonempty. +KssCSSOutput :: struct { + name: string + prefix: string + first: string + separator: string + second: string + suffix: string +} + +#program_export +KssCSSHasOffsets :: (facts: KssCSSEffectFacts) -> bool { + return facts.offset_x != "" || facts.offset_y != "" +} + +// Slots 0..3 follow ordinary declarations: gradient, x, y, then transform. +// An absent or out-of-range slot produces no output. +#program_export +KssCSSEffectAt :: (facts: KssCSSEffectFacts, index: s32) -> KssCSSOutput { + result: KssCSSOutput + if index == 0 && facts.background != "" && facts.background_end != "" && facts.background_image == "" { + result.name = "background-image" + result.prefix = "linear-gradient(" + result.first = facts.background + result.separator = ", " + result.second = facts.background_end + result.suffix = ")" + } else if index == 1 && facts.offset_x != "" { + result.name = "--kryon-offset-x" + result.first = facts.offset_x + } else if index == 2 && facts.offset_y != "" { + result.name = "--kryon-offset-y" + result.first = facts.offset_y + } else if index == 3 && KssCSSHasOffsets(facts) { + result.name = "transform" + result.prefix = "translate(var(--kryon-offset-x, 0px), var(--kryon-offset-y, 0px))" + if facts.transform != "" { + result.separator = " " + result.second = facts.transform + } + } + return result +} + +#program_export +KssCSSValueFromText :: (source: string) -> KssCSSValue { + result: KssCSSValue + result.valid = true + start: s32 = 0 + end: s32 = (cast(s32)source.count) + while start < end && KssIsSpace(source[start]) { + start = start + 1 + } + while end > start && KssIsSpace(source[end - 1]) { + end = end - 1 + } + result.text = source[start:end] + c: KssCursor + c.source = result.text + scalar: KssScalarResult = KssReadScalar(c) + if scalar.ok { + c = scalar.parser + if KssPeek(c, 0) == 112 && KssPeek(c, 1) == 120 { + c = KssAdvance(c, 2) + } + c = KssSkipSpace(c) + if KssAtEnd(c) { + result.kind = cast(s32)KssCSSNumber + result.number = scalar.value + } + } + return result +} + +#program_export +KssResolveCSSValue :: (p: KssParser, property: string, source: string) -> KssCSSValue { + result: KssCSSValue = KssCSSValueFromText(source) + property_kind: s32 = KssCSSPropertyKind(KssMakeName(property)) + if property_kind == -1 { + result.valid = false + return result + } + if result.kind == cast(s32)KssCSSNumber || property_kind == -2 { + return result + } + c: KssCursor + c.source = result.text + reference: KssNameResult = KssReadName(c) + c = KssSkipSpace(reference.parser) + if !reference.ok || !KssAtEnd(c) { + return result + } + index: s32 = -1 + if property_kind == cast(s32)KssTokenLength { + index = KssFindNumberToken(p, reference.name) + } else { + index = KssFindToken(p, reference.name, property_kind) + } + if index < 0 { + return result + } + result.has_token = true + result.token_name = p.tokens[index].name + result.token_origin = p.tokens[index].origin + if property_kind == cast(s32)KssTokenColor { + result.kind = cast(s32)KssCSSColor + result.color = p.tokens[index].color + } else if property_kind == cast(s32)KssTokenMaterial { + result.kind = cast(s32)KssCSSMaterial + result.material = p.tokens[index].material + } else { + result.kind = cast(s32)KssCSSNumber + result.number = cast(float64)p.tokens[index].number + } + return result +} + +#program_export +KssOverrideCSSValue :: (value: KssCSSValue, replacement: string, provided: bool) -> KssCSSValue { + if !provided || !value.has_token || value.kind != cast(s32)KssCSSColor { + return value + } + parsed: KssCSSValue = KssCSSValueFromText(replacement) + value.kind = parsed.kind + value.number = parsed.number + value.text = parsed.text + return value +} diff --git a/src/parser/directives.zi b/src/parser/directives.zi new file mode 100644 index 0000000..ff7cbc5 --- /dev/null +++ b/src/parser/directives.zi @@ -0,0 +1,293 @@ +// Part of the kss_parser module, loaded by src/kss/kss_parser.zi. +// Directives, imports, and the step that drives a parse. + +KssParseDirective :: (p: KssParser) -> KssParser { + c: KssCursor = p.cursor + directive_start: s32 = p.cursor.pos + c = KssAdvance(c, 1) + p.cursor = c + keyword: KssNameResult = KssReadName(c) + if !keyword.ok { + p.cursor = keyword.parser + return KssFail(p, "expected directive name") + } + + if KssNameEquals(keyword.name, "pack") { + value: KssNameResult = KssReadName(keyword.parser) + if !value.ok { + p.cursor = value.parser + return KssFail(p, "expected pack id") + } + c = value.parser + p.cursor = c + if p.import_depth > 0 { + c = KssSkipSpace(c) + p.cursor = c + if !KssAtEnd(c) && p.cursor.source[p.cursor.pos] == 59 { + c = KssAdvance(c, 1) + p.cursor = c + } + return p + } + p.pack = value.name + semicolon: KssExpectResult = KssExpect(c, 59) + if !semicolon.ok { + p.cursor = semicolon.parser + return KssFail(p, "expected ';'") + } + p.cursor = semicolon.parser + return KssRecordBlock(p, 5, directive_start, p.cursor.pos) + } + if KssNameEquals(keyword.name, "version") { + value: KssNumberResult = KssReadNumber(keyword.parser) + if !value.ok { + p.cursor = value.parser + return KssFail(p, "expected version number") + } + if cast(s32)value.value != 1 { + p.cursor = value.parser + return KssFail(p, "unsupported style sheet version") + } + if p.version_seen { + p.cursor = value.parser + return KssFail(p, "version declared twice") + } + p.version_seen = true + semicolon: KssExpectResult = KssExpect(value.parser, 59) + if !semicolon.ok { + p.cursor = semicolon.parser + return KssFail(p, "expected ';'") + } + p.cursor = semicolon.parser + return KssRecordBlock(p, 5, directive_start, p.cursor.pos) + } + if KssNameEquals(keyword.name, "layer") { + p.cursor = keyword.parser + return KssParseLayerList(p, directive_start) + } + if KssNameEquals(keyword.name, "import") { + p.cursor = keyword.parser + target: KssNameResult = KssReadImportTarget(p) + if !target.ok { + p.cursor = target.parser + return KssFail(p, "expected import target") + } + c = target.parser + p.cursor = c + semicolon: KssExpectResult = KssExpect(c, 59) + if !semicolon.ok { + p.cursor = semicolon.parser + return KssFail(p, "expected ';'") + } + c = semicolon.parser + p.cursor = c + p = KssRecordBlock(p, 5, directive_start, p.cursor.pos) + p.pending_import = target.name + p.status = cast(s32)KssStatusNeedImport + return p + } + if KssNameEquals(keyword.name, "variant") { + name: KssNameResult = KssReadName(keyword.parser) + if !name.ok { + p.cursor = name.parser + return KssFail(p, "expected variant name") + } + label: KssNameResult = KssReadVariantLabel(name.parser) + if !label.ok { + p.cursor = label.parser + return KssFail(p, "expected variant label string") + } + p.variant_label = label.name + p.cursor = label.parser + p = KssParseVariantBlock(p, name.name) + if p.status == cast(s32)KssStatusError { + return p + } + return KssRecordBlock(p, 4, directive_start, p.cursor.pos) + } + if KssNameEquals(keyword.name, "theme") { + value: KssNameResult = KssReadName(keyword.parser) + if !value.ok { + p.cursor = value.parser + return KssFail(p, "expected theme name") + } + p.cursor = value.parser + p = KssParseThemeBlock(p, value.name) + if p.status == cast(s32)KssStatusError { + return p + } + return KssRecordBlock(p, 2, directive_start, p.cursor.pos) + } + if KssNameEquals(keyword.name, "env") { + axis: KssNameResult = KssReadName(keyword.parser) + if !axis.ok { + p.cursor = axis.parser + return KssFail(p, "expected environment axis") + } + if !KssEnvAxisValid(axis.name) { + p.cursor = axis.parser + return KssFailName(p, "unknown environment axis '", + axis.name) + } + open: KssExpectResult = KssExpect(axis.parser, 40) + if !open.ok { + p.cursor = open.parser + return KssFail(p, "expected '(' after @env axis") + } + value: KssNameResult = KssReadName(open.parser) + if !value.ok { + p.cursor = value.parser + return KssFail(p, "expected environment value") + } + close: KssExpectResult = KssExpect(value.parser, 41) + if !close.ok { + p.cursor = close.parser + return KssFail(p, "expected ')'") + } + p.cursor = close.parser + p = KssParseEnvBlock(p, axis.name, value.name) + if p.status == cast(s32)KssStatusError { + return p + } + return KssRecordBlock(p, 3, directive_start, p.cursor.pos) + } + if p.declarative { + if KssIsGroupName(keyword.name) { + p.cursor = keyword.parser + return KssParseGroupBlock(p, keyword.name) + } + p.cursor = keyword.parser + return KssCaptureForeign(p, keyword.name) + } + p.cursor = keyword.parser + return KssFailName(p, "unknown directive '@", keyword.name) +} + +// Import resolution. The host supplies sources; the parser tracks ordering, +// cycles, and per-file provenance. + +KssImportNameOnStack :: (p: KssParser, name: KssName) -> bool { + index: s32 = 0 + while index < p.import_depth { + if KssNameEqualsName(p.imports[index].name, name) { + return true + } + index = index + 1 + } + return false +} + +#program_export +KssProvideImport :: (p: KssParser, source: string, name: string) -> KssParser { + import_name: KssName = KssMakeName(name) + if KssImportNameOnStack(p, import_name) { + return KssFailName(p, "@import cycle: ", import_name) + } + if p.import_depth >= 8 || p.file_count >= 8 { + return KssFail(p, "style import depth exceeded") + } + p.imports[p.import_depth].name = import_name + p.imports[p.import_depth].outer_source = p.cursor.source + p.imports[p.import_depth].outer_pos = p.cursor.pos + p.imports[p.import_depth].outer_line = p.cursor.line + p.imports[p.import_depth].outer_column = p.cursor.column + p.imports[p.import_depth].outer_file = p.cursor.file + p.import_depth = p.import_depth + 1 + p.files[p.file_count] = KssCopyFileName(name) + p.cursor.file = p.file_count + p.file_count = p.file_count + 1 + p.cursor.source = source + p.cursor.pos = 0 + p.cursor.line = 1 + p.cursor.column = 1 + p.status = cast(s32)KssStatusContinue + return p +} + +#program_export +KssFailImport :: (p: KssParser) -> KssParser { + return KssFailName(p, "missing import: ", p.pending_import) +} + +KssPopImport :: (p: KssParser) -> KssParser { + if p.import_depth <= 0 { + return p + } + p.import_depth = p.import_depth - 1 + p.cursor.source = p.imports[p.import_depth].outer_source + p.cursor.pos = p.imports[p.import_depth].outer_pos + p.cursor.line = p.imports[p.import_depth].outer_line + p.cursor.column = p.imports[p.import_depth].outer_column + p.cursor.file = p.imports[p.import_depth].outer_file + return p +} + +// One parse step. Statuses: Rule (collect p.rule), NeedImport (feed via +// KssProvideImport/KssFailImport), Done, Error (read p.diagnostic). + +#program_export +KssStep :: (p: KssParser) -> KssParser { + c: KssCursor = p.cursor + if p.status == cast(s32)KssStatusError || p.status == cast(s32)KssStatusDone { + return p + } + if p.resume_group >= 0 { + p.status = cast(s32)KssStatusContinue + p = KssContinueGroupBlock(p) + if p.status == cast(s32)KssStatusRule || + p.status == cast(s32)KssStatusError { + return p + } + c = p.cursor + } + if p.in_env { + p.status = cast(s32)KssStatusContinue + p = KssContinueEnvBlock(p) + if p.status == cast(s32)KssStatusRule || + p.status == cast(s32)KssStatusError { + return p + } + c = p.cursor + } + if p.in_variant { + p.status = cast(s32)KssStatusContinue + p = KssContinueVariantBlock(p) + if p.status == cast(s32)KssStatusRule || + p.status == cast(s32)KssStatusError { + return p + } + c = p.cursor + } + c = KssSkipSpace(c) + p.cursor = c + if KssAtEnd(c) { + if p.import_depth > 0 { + p = KssPopImport(p) + p.status = cast(s32)KssStatusContinue + return p + } + p.status = cast(s32)KssStatusDone + return p + } + if p.cursor.source[p.cursor.pos] == 64 { + p = KssParseDirective(p) + if p.status == cast(s32)KssStatusNeedImport || + p.status == cast(s32)KssStatusError || + p.status == cast(s32)KssStatusRule { + return p + } + p.status = cast(s32)KssStatusContinue + return p + } + tokens_start: s32 = c.pos + probe: KssNameResult = KssReadName(c) + if probe.ok && KssNameEquals(probe.name, "tokens") { + p.cursor = probe.parser + p = KssParseTokensBlock(p, cast(s32)KssOriginPack) + if p.status == cast(s32)KssStatusError { + return p + } + return KssRecordBlock(p, 1, tokens_start, p.cursor.pos) + } + return KssParseRule(p) +} diff --git a/src/parser/lexer.zi b/src/parser/lexer.zi new file mode 100644 index 0000000..a9ca6c1 --- /dev/null +++ b/src/parser/lexer.zi @@ -0,0 +1,577 @@ +// Part of the kss_parser module, loaded by src/kss/kss_parser.zi. +// Source bytes, names, white space and comments, literals, and diagnostics. + +// Byte helpers over the borrowed source view. Every advance keeps the +// line/column provenance current so diagnostics and rule origins stay exact. + +KssAtEnd :: (c: KssCursor) -> bool { + return c.pos >= (cast(s32)c.source.count) +} + +KssPeek :: (c: KssCursor, ahead: s32) -> u8 { + index: s32 = c.pos + ahead + if index < 0 || index >= (cast(s32)c.source.count) { + return 0 + } + return c.source[index] +} + +KssAdvance :: (c: KssCursor, count: s32) -> KssCursor { + step: s32 = 0 + while step < count { + if c.pos < (cast(s32)c.source.count) { + if c.source[c.pos] == 10 { + c.line = c.line + 1 + c.column = 1 + } else { + c.column = c.column + 1 + } + c.pos = c.pos + 1 + } + step = step + 1 + } + return c +} + +KssIsSpace :: (byte: u8) -> bool { + return byte == 32 || byte == 9 || byte == 13 || byte == 10 || byte == 11 || byte == 12 +} + +// Quoted text ends on its line, as CSS strings do, and an escape does not +// carry it over; re-indenting a later line must not change what it says. +KssIsLineBreak :: (byte: u8) -> bool { + return byte == 10 || byte == 13 +} + +// An import target names a pack or a path: no spaces, control bytes, quotes, +// angle brackets, or the braces and semicolons that give a sheet structure. +KssImportTargetByte :: (byte: u8) -> bool { + return byte > 32 && byte != 127 && byte != 34 && byte != 39 && + byte != 59 && byte != 60 && byte != 62 && byte != 123 && byte != 125 +} + +KssIsDigit :: (byte: u8) -> bool { + return byte >= 48 && byte <= 57 +} + +KssIsHexDigit :: (byte: u8) -> bool { + return (byte >= 48 && byte <= 57) || (byte >= 97 && byte <= 102) || + (byte >= 65 && byte <= 70) +} + +KssIsAlpha :: (byte: u8) -> bool { + return (byte >= 97 && byte <= 122) || (byte >= 65 && byte <= 90) +} + +KssIdentStart :: (byte: u8) -> bool { + return KssIsAlpha(byte) || byte == 95 || byte == 45 +} + +KssIdentChar :: (byte: u8) -> bool { + return KssIsAlpha(byte) || KssIsDigit(byte) || byte == 95 || byte == 45 || + byte == 46 +} + +KssSelectorIdentChar :: (byte: u8) -> bool { + return KssIsAlpha(byte) || KssIsDigit(byte) || byte == 95 || byte == 45 +} + +KssLower :: (byte: u8) -> u8 { + if byte >= 65 && byte <= 90 { + return byte + 32 + } + return byte +} + +KssNameEquals :: (name: KssName, text: string) -> bool { + if name.length != (cast(s32)text.count) { + return false + } + index: s32 = 0 + while index < name.length { + if KssLower(name.bytes[index]) != KssLower(text[index]) { + return false + } + index = index + 1 + } + return true +} + +KssNameEqualsName :: (a: KssName, b: KssName) -> bool { + if a.length != b.length { + return false + } + index: s32 = 0 + while index < a.length { + if KssLower(a.bytes[index]) != KssLower(b.bytes[index]) { + return false + } + index = index + 1 + } + return true +} + +KssMakeName :: (text: string) -> KssName { + name: KssName + index: s32 = 0 + while index < (cast(s32)text.count) && index < 64 { + name.bytes[index] = text[index] + index = index + 1 + } + name.length = index + return name +} + +KssCopyFileName :: (text: string) -> KssSourceFile { + file: KssSourceFile + index: s32 = 0 + while index < (cast(s32)text.count) && index < 96 { + file.name[index] = text[index] + index = index + 1 + } + file.length = index + return file +} + +KssNameAppend :: (destination: KssName, byte: u8) -> KssName { + if destination.length < 64 { + destination.bytes[destination.length] = byte + destination.length = destination.length + 1 + } + return destination +} + +// Skip whitespace and // plus /* */ comments, keeping provenance current. + +KssSkipSpace :: (c: KssCursor) -> KssCursor { + while !KssAtEnd(c) { + byte: u8 = c.source[c.pos] + if KssIsSpace(byte) { + c = KssAdvance(c, 1) + continue + } + if byte == 47 && KssPeek(c, 1) == 47 { + while !KssAtEnd(c) && c.source[c.pos] != 10 { + c = KssAdvance(c, 1) + } + continue + } + if byte == 47 && KssPeek(c, 1) == 42 { + c = KssAdvance(c, 2) + while !KssAtEnd(c) { + if c.source[c.pos] == 42 && KssPeek(c, 1) == 47 { + c = KssAdvance(c, 2) + break + } + c = KssAdvance(c, 1) + } + continue + } + break + } + return c +} + +KssNameResult :: struct { + parser: KssCursor + name: KssName + ok: bool +} + +KssExpectResult :: struct { + parser: KssCursor + ok: bool +} + +KssNumberResult :: struct { + parser: KssCursor + value: float32 + ok: bool +} + +KssScalarResult :: struct { + parser: KssCursor + value: float64 + ok: bool +} + +KssColorResult :: struct { + parser: KssCursor + value: u32 + ok: bool +} + +KssReadName :: (c: KssCursor) -> KssNameResult { + result: KssNameResult + c = KssSkipSpace(c) + if KssAtEnd(c) || !KssIdentStart(c.source[c.pos]) { + result.parser = c + result.ok = false + return result + } + while !KssAtEnd(c) && KssIdentChar(c.source[c.pos]) { + result.name = KssNameAppend(result.name, c.source[c.pos]) + c = KssAdvance(c, 1) + } + result.parser = c + result.ok = result.name.length > 0 + return result +} + +KssReadSelectorName :: (c: KssCursor) -> KssNameResult { + result: KssNameResult + c = KssSkipSpace(c) + if !KssAtEnd(c) && c.source[c.pos] == 42 { + result.name = KssNameAppend(result.name, 42) + c = KssAdvance(c, 1) + result.parser = c + result.ok = true + return result + } + if KssAtEnd(c) || !KssIdentStart(c.source[c.pos]) { + result.parser = c + result.ok = false + return result + } + while !KssAtEnd(c) && KssSelectorIdentChar(c.source[c.pos]) { + result.name = KssNameAppend(result.name, c.source[c.pos]) + c = KssAdvance(c, 1) + } + result.parser = c + result.ok = result.name.length > 0 + return result +} + +KssExpect :: (c: KssCursor, byte: u8) -> KssExpectResult { + result: KssExpectResult + c = KssSkipSpace(c) + if KssAtEnd(c) || c.source[c.pos] != byte { + result.parser = c + result.ok = false + return result + } + c = KssAdvance(c, 1) + result.parser = c + result.ok = true + return result +} + +// Diagnostics carry the message plus the exact file, line, and column. + +KssDiagnosticAppend :: (p: KssParser, text: string) -> KssParser { + index: s32 = 0 + while index < (cast(s32)text.count) && p.diagnostic_length < 255 { + p.diagnostic[p.diagnostic_length] = text[index] + p.diagnostic_length = p.diagnostic_length + 1 + index = index + 1 + } + return p +} + +KssDiagnosticAppendName :: (p: KssParser, name: KssName) -> KssParser { + index: s32 = 0 + while index < name.length && p.diagnostic_length < 255 { + p.diagnostic[p.diagnostic_length] = name.bytes[index] + p.diagnostic_length = p.diagnostic_length + 1 + index = index + 1 + } + return p +} + +KssDiagnosticAppendInt :: (p: KssParser, value: s32) -> KssParser { + remaining: s32 = value + digits: s32 = 1 + if remaining < 0 { + p = KssDiagnosticAppend(p, "-") + remaining = -remaining + } + probe: s32 = 10 + while probe <= remaining && digits < 10 { + probe = probe * 10 + digits = digits + 1 + } + while digits > 0 { + power: s32 = 1 + step: s32 = 1 + while step < digits { + power = power * 10 + step = step + 1 + } + digit: s32 = 0 + while remaining >= power { + remaining = remaining - power + digit = digit + 1 + } + if p.diagnostic_length < 255 { + p.diagnostic[p.diagnostic_length] = cast(u8)(48 + digit) + p.diagnostic_length = p.diagnostic_length + 1 + } + digits = digits - 1 + } + return p +} + +KssDiagnosticAt :: (p: KssParser) -> KssParser { + p = KssDiagnosticAppend(p, " at ") + if p.cursor.file >= 0 && p.cursor.file < p.file_count && p.files[p.cursor.file].length > 0 { + index: s32 = 0 + while index < p.files[p.cursor.file].length && p.diagnostic_length < 255 { + p.diagnostic[p.diagnostic_length] = p.files[p.cursor.file].name[index] + p.diagnostic_length = p.diagnostic_length + 1 + index = index + 1 + } + p = KssDiagnosticAppend(p, ":") + } + p = KssDiagnosticAppendInt(p, p.cursor.line) + p = KssDiagnosticAppend(p, ":") + p = KssDiagnosticAppendInt(p, p.cursor.column) + return p +} + +KssFail :: (p: KssParser, message: string) -> KssParser { + p.diagnostic_length = 0 + p = KssDiagnosticAppend(p, message) + p = KssDiagnosticAt(p) + p.status = cast(s32)KssStatusError + return p +} + +KssFailName :: (p: KssParser, message: string, name: KssName) -> KssParser { + p.diagnostic_length = 0 + p = KssDiagnosticAppend(p, message) + p = KssDiagnosticAppendName(p, name) + p = KssDiagnosticAt(p) + p.status = cast(s32)KssStatusError + return p +} + +KssReadHexColor :: (c: KssCursor) -> KssColorResult { + result: KssColorResult + value: u32 = 0 + digits: s32 = 0 + c = KssSkipSpace(c) + if KssAtEnd(c) || c.source[c.pos] != 35 { + result.parser = c + return result + } + c = KssAdvance(c, 1) + while !KssAtEnd(c) && KssIsHexDigit(c.source[c.pos]) { + byte: u8 = c.source[c.pos] + digit: u32 = 0 + if byte >= 48 && byte <= 57 { + digit = cast(u32)(byte - 48) + } else if byte >= 97 && byte <= 102 { + digit = cast(u32)(byte - 87) + } else { + digit = cast(u32)(byte - 55) + } + value = (value << cast(u32)4) | digit + digits = digits + 1 + c = KssAdvance(c, 1) + } + if digits == 6 { + value = (value << cast(u32)8) | cast(u32)255 + } else if digits != 8 { + result.parser = c + return result + } + result.parser = c + result.value = value + result.ok = true + return result +} + +KssReadNumber :: (c: KssCursor) -> KssNumberResult { + scalar: KssScalarResult = KssReadScalar(c) + result: KssNumberResult + result.parser = scalar.parser + result.value = cast(float32)scalar.value + result.ok = scalar.ok + return result +} + +KssReadScalar :: (c: KssCursor) -> KssScalarResult { + result: KssScalarResult + negative: bool = false + whole: float64 = 0.0 + fraction: float64 = 0.0 + fraction_digits: s32 = 0 + exponent: s32 = 0 + exponent_negative: bool = false + seen_digits: bool = false + power: float64 = 1.0 + value64: float64 = 0.0 + c = KssSkipSpace(c) + if KssAtEnd(c) { + result.parser = c + return result + } + if c.source[c.pos] == 45 { + negative = true + c = KssAdvance(c, 1) + } else if c.source[c.pos] == 43 { + c = KssAdvance(c, 1) + } + while !KssAtEnd(c) && KssIsDigit(c.source[c.pos]) { + whole = whole * 10.0 + cast(float64)(c.source[c.pos] - 48) + seen_digits = true + c = KssAdvance(c, 1) + } + if !KssAtEnd(c) && c.source[c.pos] == 46 && !seen_digits { + result.parser = c + return result + } + if !KssAtEnd(c) && c.source[c.pos] == 46 { + c = KssAdvance(c, 1) + while !KssAtEnd(c) && KssIsDigit(c.source[c.pos]) { + if fraction_digits < 17 { + fraction = fraction * 10.0 + cast(float64)(c.source[c.pos] - 48) + fraction_digits = fraction_digits + 1 + } + seen_digits = true + c = KssAdvance(c, 1) + } + } + if !seen_digits { + result.parser = c + return result + } + if !KssAtEnd(c) && (c.source[c.pos] == 101 || c.source[c.pos] == 69) { + c = KssAdvance(c, 1) + if !KssAtEnd(c) && c.source[c.pos] == 45 { + exponent_negative = true + c = KssAdvance(c, 1) + } else if !KssAtEnd(c) && c.source[c.pos] == 43 { + c = KssAdvance(c, 1) + } + exponent_start: s32 = c.pos + while !KssAtEnd(c) && KssIsDigit(c.source[c.pos]) { + if exponent <= 400 { + exponent = exponent * 10 + cast(s32)(c.source[c.pos] - 48) + } + c = KssAdvance(c, 1) + } + if c.pos == exponent_start || exponent > 400 { + result.parser = c + return result + } + } + step: s32 = 0 + while step < fraction_digits { + power = power * 10.0 + step = step + 1 + } + value64 = cast(float64)whole + cast(float64)fraction / power + step = 0 + while step < exponent { + if exponent_negative { + value64 = value64 / 10.0 + } else { + value64 = value64 * 10.0 + } + step = step + 1 + } + if negative { + value64 = -value64 + } + result.parser = c + if value64 > 1.7976931348623157e308 || value64 < -1.7976931348623157e308 { + result.ok = false + return result + } + result.value = value64 + result.ok = true + return result +} + +// Durations convert to milliseconds with exact decimal shifting so '0.14s' +// yields exactly 140 instead of a float rounding artifact. + +KssReadDuration :: (c: KssCursor) -> KssNumberResult { + result: KssNumberResult + negative: bool = false + whole: u32 = 0 + fraction: u32 = 0 + fraction_digits: s32 = 0 + seen_digits: bool = false + seconds: bool = false + c = KssSkipSpace(c) + if KssAtEnd(c) { + result.parser = c + return result + } + if c.source[c.pos] == 45 { + negative = true + c = KssAdvance(c, 1) + } else if c.source[c.pos] == 43 { + c = KssAdvance(c, 1) + } + while !KssAtEnd(c) && KssIsDigit(c.source[c.pos]) { + whole = whole * 10 + cast(u32)(c.source[c.pos] - 48) + seen_digits = true + c = KssAdvance(c, 1) + } + if !KssAtEnd(c) && c.source[c.pos] == 46 { + c = KssAdvance(c, 1) + while !KssAtEnd(c) && KssIsDigit(c.source[c.pos]) { + if fraction_digits < 9 { + fraction = fraction * 10 + cast(u32)(c.source[c.pos] - 48) + fraction_digits = fraction_digits + 1 + } + seen_digits = true + c = KssAdvance(c, 1) + } + } + if !seen_digits { + result.parser = c + return result + } + unit: KssNameResult = KssReadName(c) + if unit.ok && KssNameEquals(unit.name, "ms") { + seconds = false + } else if unit.ok && KssNameEquals(unit.name, "s") { + seconds = true + } else if unit.ok { + result.parser = unit.parser + return result + } else { + result.parser = c + return result + } + + value: float64 = 0.0 + scaled: u32 = whole + shift: s32 = fraction_digits + while shift > 0 { + scaled = scaled * 10 + shift = shift - 1 + } + scaled = scaled + fraction + if seconds { + value = cast(float64)scaled + step: s32 = 0 + while step < 3 - fraction_digits { + value = value * 10.0 + step = step + 1 + } + step = 0 + while step < fraction_digits - 3 { + value = value / 10.0 + step = step + 1 + } + } else { + value = cast(float64)scaled + step: s32 = 0 + while step < fraction_digits { + value = value / 10.0 + step = step + 1 + } + } + if negative { + value = -value + } + result.parser = unit.parser + result.value = cast(float32)value + result.ok = true + return result +} diff --git a/src/parser/rules.zi b/src/parser/rules.zi new file mode 100644 index 0000000..b72f6b5 --- /dev/null +++ b/src/parser/rules.zi @@ -0,0 +1,862 @@ +// Part of the kss_parser module, loaded by src/kss/kss_parser.zi. +// Rules: selectors, declarations, foreign and group blocks, and properties. + +// Selector: Kind[attr=value].class:state chains filling a StyleRule selector. + +KssRuleResult :: struct { + parser: KssParser + rule: StyleRule +} + +KssApplyAttr :: (p: KssParser, rule: StyleRule) -> KssRuleResult { + c: KssCursor = p.cursor + result: KssRuleResult + attribute: KssNameResult = KssReadName(c) + if !attribute.ok { + p.cursor = attribute.parser + result.parser = KssFail(p, "expected selector attribute") + return result + } + equals: KssExpectResult = KssExpect(attribute.parser, 61) + if !equals.ok { + p.cursor = equals.parser + result.parser = KssFail(p, "expected '=' in selector attribute") + return result + } + value: KssNameResult = KssReadName(equals.parser) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected selector attribute value") + return result + } + close: KssExpectResult = KssExpect(value.parser, 93) + if !close.ok { + p.cursor = close.parser + result.parser = KssFail(p, "expected ']'") + return result + } + if KssNameEquals(attribute.name, "tone") { + mapped: s32 = KssToneValue(value.name) + if mapped == -999999 { + p.cursor = close.parser + result.parser = KssFailName(p, "unknown tone '", value.name) + return result + } + rule.selector.tone = mapped + } else if KssNameEquals(attribute.name, "emphasis") { + mapped: s32 = KssEmphasisValue(value.name) + if mapped == -999999 { + p.cursor = close.parser + result.parser = KssFailName(p, "unknown emphasis '", value.name) + return result + } + rule.selector.emphasis = mapped + } else if KssNameEquals(attribute.name, "size") { + mapped: s32 = KssSizeValue(value.name) + if mapped == -999999 { + p.cursor = close.parser + result.parser = KssFailName(p, "unknown size '", value.name) + return result + } + rule.selector.size = mapped + } else if KssNameEquals(attribute.name, "state") { + mapped: s32 = KssStateValue(value.name) + if mapped == -999999 { + p.cursor = close.parser + result.parser = KssFailName(p, "unknown state '", value.name) + return result + } + rule.selector.state = mapped + } else if KssNameEquals(attribute.name, "role") { + mapped: s32 = KssRoleValue(value.name) + if mapped == -999999 { + p.cursor = close.parser + result.parser = KssFailName(p, "unknown role '", value.name) + return result + } + rule.selector.role = mapped + } else if KssNameEquals(attribute.name, "class") { + rule.selector.class_name = KssClassId(value.name) + } else { + p.cursor = close.parser + result.parser = KssFailName(p, "unknown selector attribute '", + attribute.name) + return result + } + p.cursor = close.parser + result.parser = p + result.rule = rule + return result +} + +KssParseSelector :: (p: KssParser) -> KssRuleResult { + c: KssCursor = p.cursor + result: KssRuleResult + rule: StyleRule + rule.selector = StyleDefaultSelector() + rule.state = StyleStateAny() + kind: KssNameResult = KssReadSelectorName(c) + if !kind.ok { + p.cursor = kind.parser + result.parser = KssFail(p, "expected style selector") + return result + } + mapped: s32 = KssKindValue(kind.name) + if mapped == -999999 { + p.cursor = kind.parser + result.parser = KssFailName(p, "unknown style selector '", + kind.name) + return result + } + rule.selector.kind = mapped + c = kind.parser + p.cursor = c + running: bool = true + while running { + c = KssSkipSpace(c) + p.cursor = c + if KssAtEnd(c) { + result.parser = KssFail(p, "unterminated style rule") + return result + } + if p.cursor.source[p.cursor.pos] == 91 { + c = KssAdvance(c, 1) + p.cursor = c + attribute: KssRuleResult = KssApplyAttr(p, rule) + if attribute.parser.status == cast(s32)KssStatusError { + return attribute + } + p = attribute.parser + c = p.cursor + rule = attribute.rule + continue + } + if p.cursor.source[p.cursor.pos] == 46 { + c = KssAdvance(c, 1) + p.cursor = c + class_name: KssNameResult = KssReadSelectorName(c) + if !class_name.ok { + p.cursor = class_name.parser + result.parser = KssFail(p, + "expected class name after '.'") + return result + } + rule.selector.class_name = KssClassId(class_name.name) + c = class_name.parser + p.cursor = c + continue + } + if p.cursor.source[p.cursor.pos] == 58 { + c = KssAdvance(c, 1) + p.cursor = c + state: KssNameResult = KssReadName(c) + if !state.ok { + p.cursor = state.parser + result.parser = KssFail(p, "expected state after ':'") + return result + } + state_mapped: s32 = KssStateValue(state.name) + if state_mapped == -999999 { + p.cursor = state.parser + result.parser = KssFailName(p, "unknown state '", + state.name) + return result + } + rule.state = state_mapped + c = state.parser + p.cursor = c + continue + } + running = false + } + result.parser = p + result.rule = rule + return result +} + +// Property declarations. Token references resolve immediately against the +// current token table, so overlays applied earlier win as intended. Declarative +// hosts capture source spans and resolve them with KssResolveCSSValue when the +// rule is yielded, before subsequent overlays change the table. + +KssCaptureDeclaration :: (p: KssParser, rule: StyleRule, name: KssName, + name_start: s32, name_end: s32) -> KssRuleResult { + c: KssCursor = p.cursor + result: KssRuleResult + if p.declaration_count >= 1024 { + result.parser = KssFail(p, "declaration capacity exceeded") + return result + } + entry: KssDeclaration + entry.rule = p.rule_total + entry.name_start = name_start + entry.name_length = name_end - name_start + c = KssSkipSpace(c) + p.cursor = c + entry.value_start = p.cursor.pos + quote: u8 = 0 + depth: s32 = 0 + running: bool = true + while running { + if KssAtEnd(c) { + result.parser = KssFail(p, "expected ';'") + return result + } + byte: u8 = c.source[c.pos] + if quote != 0 { + if byte == 92 && KssPeek(c, 1) != 0 && !KssIsLineBreak(KssPeek(c, 1)) { + c = KssAdvance(c, 2) + p.cursor = c + continue + } + if byte == quote || KssIsLineBreak(byte) { + quote = 0 + } + } else if byte == 34 || byte == 39 { + quote = byte + } else if byte == 47 && (KssPeek(c, 1) == 42 || (depth == 0 && KssPeek(c, 1) == 47)) { + c = KssSkipSpace(c) + p.cursor = c + continue + } else if byte == 40 || byte == 91 { + depth = depth + 1 + } else if byte == 41 || byte == 93 { + if depth <= 0 { + result.parser = KssFail(p, "unmatched declaration delimiter") + return result + } + depth = depth - 1 + } else if depth == 0 && (byte == 59 || byte == 125) { + running = false + continue + } + c = KssAdvance(c, 1) + p.cursor = c + } + entry.value_length = p.cursor.pos - entry.value_start + while entry.value_length > 0 { + byte: u8 = p.cursor.source[entry.value_start + entry.value_length - 1] + if !KssIsSpace(byte) { + break + } + entry.value_length = entry.value_length - 1 + } + if entry.value_length <= 0 { + result.parser = KssFailName(p, "expected declaration value after '", name) + return result + } + if p.cursor.source[p.cursor.pos] == 59 { + c = KssAdvance(c, 1) + p.cursor = c + } + p.declarations[p.declaration_count] = entry + p.declaration_count = p.declaration_count + 1 + result.parser = p + result.rule = rule + return result +} + +// Unknown foreign blocks are structure-lexed here. Recognized conditional +// groups and keyframes use the shared rule/declaration stream instead. + +KssCaptureForeign :: (p: KssParser, name: KssName) -> KssParser { + c: KssCursor = p.cursor + if p.foreign_count >= 16 { + return KssFail(p, "foreign block capacity exceeded") + } + c = KssSkipSpace(c) + p.cursor = c + query_start: s32 = p.cursor.pos + while !KssAtEnd(c) && p.cursor.source[p.cursor.pos] != 123 { + if p.cursor.source[p.cursor.pos] == 59 || p.cursor.source[p.cursor.pos] == 125 { + return KssFailName(p, "unknown directive '@", name) + } + c = KssAdvance(c, 1) + p.cursor = c + } + if KssAtEnd(c) { + return KssFail(p, "expected '{' after block name") + } + entry: KssForeignBlock + entry.name = name + entry.query_start = query_start + entry.query_length = p.cursor.pos - query_start + while entry.query_length > 0 { + byte: u8 = p.cursor.source[entry.query_start + entry.query_length - 1] + if !KssIsSpace(byte) { + break + } + entry.query_length = entry.query_length - 1 + } + entry.body_start = 0 + entry.body_length = 0 + entry.file = p.cursor.file + c = KssAdvance(c, 1) + p.cursor = c + entry.body_start = p.cursor.pos + p.foreign[p.foreign_count] = entry + p.foreign_count = p.foreign_count + 1 + p = KssSkipBlockRemainder(p) + if p.status == cast(s32)KssStatusError { + return p + } + p.foreign[p.foreign_count - 1].body_length = p.cursor.pos - 1 - p.foreign[p.foreign_count - 1].body_start + return p +} + +// Conditional groups ('@media (query) { rules }' and friends) contain rules; +// declarative hosts get them lexed here with per-rule group tags. The query +// text stays opaque to the grammar. + +KssIsGroupName :: (name: KssName) -> bool { + return KssNameEquals(name, "media") || KssNameEquals(name, "supports") || + KssNameEquals(name, "container") || KssNameEquals(name, "keyframes") +} + +KssParseGroupBlock :: (p: KssParser, name: KssName) -> KssParser { + c: KssCursor = p.cursor + if p.foreign_count >= 16 { + return KssFail(p, "foreign block capacity exceeded") + } + c = KssSkipSpace(c) + p.cursor = c + query_start: s32 = p.cursor.pos + while !KssAtEnd(c) && p.cursor.source[p.cursor.pos] != 123 { + c = KssAdvance(c, 1) + p.cursor = c + } + if KssAtEnd(c) { + return KssFail(p, "expected '{' after group query") + } + entry: KssForeignBlock + entry.name = name + entry.query_start = query_start + entry.query_length = p.cursor.pos - query_start + while entry.query_length > 0 { + byte: u8 = p.cursor.source[entry.query_start + entry.query_length - 1] + if !KssIsSpace(byte) { + break + } + entry.query_length = entry.query_length - 1 + } + entry.body_start = 0 + entry.body_length = 0 + entry.file = p.cursor.file + group: s32 = p.foreign_count + p.foreign[p.foreign_count] = entry + p.foreign_count = p.foreign_count + 1 + c = KssAdvance(c, 1) + p.cursor = c + p.foreign[group].body_start = p.cursor.pos + p.resume_group = group + return KssContinueGroupBlock(p) +} + +KssContinueGroupBlock :: (p: KssParser) -> KssParser { + c: KssCursor = p.cursor + running: bool = true + while running { + c = KssSkipSpace(c) + p.cursor = c + if KssAtEnd(c) { + return KssFail(p, "unterminated group block") + } + if p.cursor.source[p.cursor.pos] == 125 { + p.foreign[p.resume_group].body_length = p.cursor.pos - p.foreign[p.resume_group].body_start + c = KssAdvance(c, 1) + p.cursor = c + p.resume_group = -1 + running = false + continue + } + p = KssParseRule(p) + if p.status == cast(s32)KssStatusError { + return p + } + p.rule_span.group = p.resume_group + return p + } + return p +} + +KssReadColorValue :: (p: KssParser) -> KssColorResult { + c: KssCursor = p.cursor + hex: KssColorResult = KssReadHexColor(c) + if hex.ok { + return hex + } + reference: KssNameResult = KssReadName(hex.parser) + if reference.ok { + p.cursor = reference.parser + index: s32 = KssFindColorToken(p, reference.name) + if index >= 0 { + hex.value = p.tokens[index].color + hex.ok = true + hex.parser = reference.parser + return hex + } + // Closed set of named colors from the value-set proposal. Tokens win + // over builtins so packs keep full control of their palette. + if KssNameEquals(reference.name, "transparent") { + hex.value = 0x00000000 + hex.ok = true + hex.parser = reference.parser + return hex + } + if KssNameEquals(reference.name, "black") { + hex.value = 0x000000ff + hex.ok = true + hex.parser = reference.parser + return hex + } + if KssNameEquals(reference.name, "white") { + hex.value = 0xffffffff + hex.ok = true + hex.parser = reference.parser + return hex + } + } + hex.ok = false + hex.parser = hex.parser + return hex +} + +KssReadNumberValue :: (p: KssParser) -> KssNumberResult { + c: KssCursor = p.cursor + literal: KssNumberResult = KssReadNumber(c) + if literal.ok { + return literal + } + reference: KssNameResult = KssReadName(literal.parser) + if reference.ok { + p.cursor = reference.parser + index: s32 = KssFindNumberToken(p, reference.name) + if index >= 0 { + literal.value = p.tokens[index].number + literal.ok = true + literal.parser = reference.parser + return literal + } + } + literal.ok = false + return literal +} + +KssIntResult :: struct { + parser: KssParser + value: s32 + ok: bool +} + +KssReadMaterialValue :: (p: KssParser) -> KssIntResult { + c: KssCursor = p.cursor + result: KssIntResult + reference: KssNameResult = KssReadName(c) + if !reference.ok { + p.cursor = reference.parser + result.parser = KssFail(p, "expected material") + return result + } + mapped: s32 = KssMaterialValue(reference.name) + if mapped == -999999 { + p.cursor = reference.parser + index: s32 = KssFindMaterialToken(p, reference.name) + if index >= 0 { + mapped = p.tokens[index].material + } + } + if mapped == -999999 { + result.parser = KssFailName(p, "unknown material '", + reference.name) + return result + } + p.cursor = reference.parser + result.parser = p + result.value = mapped + result.ok = true + return result +} + +// One declaration into the rule's StyleData. Field flags come from +// control_props; every property stores presence explicitly. + +KssParseProperty :: (p: KssParser, rule: StyleRule) -> KssRuleResult { + c: KssCursor = p.cursor + result: KssRuleResult + c = KssSkipSpace(c) + p.cursor = c + name_start: s32 = p.cursor.pos + name: KssNameResult = KssReadName(c) + if !name.ok { + p.cursor = name.parser + result.parser = KssFail(p, "expected property name") + return result + } + colon: KssExpectResult = KssExpect(name.parser, 58) + if !colon.ok { + p.cursor = colon.parser + result.parser = KssFail(p, "expected ':' after property") + return result + } + c = colon.parser + p.cursor = c + + if p.declarative { + return KssCaptureDeclaration(p, rule, name.name, name_start, + name_start + name.name.length) + } + + if KssNameEquals(name.name, "background") { + value: KssColorResult = KssReadColorValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected hex color") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleBackground + rule.style.background = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name.name, "foreground") { + value: KssColorResult = KssReadColorValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected hex color") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleForeground + rule.style.foreground = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name.name, "border") { + value: KssColorResult = KssReadColorValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected hex color") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleBorder + rule.style.border = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name.name, "focus") { + value: KssColorResult = KssReadColorValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected hex color") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleFocus + rule.style.focus = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name.name, "background-end") { + value: KssColorResult = KssReadColorValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected hex color") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleBackgroundEnd + rule.style.background_end = value.value + c = value.parser + p.cursor = c + } else { + return KssParseNumberProperty(p, rule, name.name) + } + + semicolon: KssExpectResult = KssExpect(c, 59) + if !semicolon.ok { + p.cursor = semicolon.parser + result.parser = KssFail(p, "expected ';'") + return result + } + p.cursor = semicolon.parser + result.parser = p + result.rule = rule + return result +} + +KssParseNumberProperty :: (p: KssParser, rule: StyleRule, name: KssName) -> KssRuleResult { + c: KssCursor = p.cursor + result: KssRuleResult + if KssNameEquals(name, "radius") { + value: KssNumberResult = KssReadNumberValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected number") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleRadius + rule.style.radius = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name, "border-width") { + value: KssNumberResult = KssReadNumberValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected number") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleBorderWidth + rule.style.border_width = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name, "opacity") { + value: KssNumberResult = KssReadNumberValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected number") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleOpacity + rule.style.opacity = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name, "padding-x") { + value: KssNumberResult = KssReadNumberValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected number") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StylePaddingX + rule.style.padding_x = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name, "padding-y") { + value: KssNumberResult = KssReadNumberValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected number") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StylePaddingY + rule.style.padding_y = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name, "gap") { + value: KssNumberResult = KssReadNumberValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected number") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleGap + rule.style.gap = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name, "font-size") { + value: KssNumberResult = KssReadNumberValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected number") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleFontSize + rule.style.font_size = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name, "letter-spacing") { + value: KssNumberResult = KssReadNumberValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected number") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleLetterSpacing + rule.style.letter_spacing = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name, "icon-size") { + value: KssNumberResult = KssReadNumberValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected number") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleIconSize + rule.style.icon_size = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name, "offset-x") { + value: KssNumberResult = KssReadNumberValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected number") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleContentOffset + rule.style.offset_x = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name, "offset-y") { + value: KssNumberResult = KssReadNumberValue(p) + if !value.ok { + p.cursor = value.parser + result.parser = KssFail(p, "expected number") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleContentOffset + rule.style.offset_y = value.value + c = value.parser + p.cursor = c + } else if KssNameEquals(name, "material") { + value: KssIntResult = KssReadMaterialValue(p) + if !value.ok { + result.parser = value.parser + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleMaterial + rule.style.material = cast(MaterialKind)value.value + p.cursor = value.parser.cursor + c = value.parser.cursor + } else if KssNameEquals(name, "typeface") { + start: s32 = -1 + length: s32 = 0 + scan: KssCursor = KssSkipSpace(c) + if !KssAtEnd(scan) && KssIdentStart(scan.source[scan.pos]) { + start = scan.pos + while !KssAtEnd(scan) && KssIdentChar(scan.source[scan.pos]) { + length = length + 1 + scan = KssAdvance(scan, 1) + } + } + if start < 0 { + p.cursor = scan + result.parser = KssFail(p, "expected typeface name") + return result + } + rule.style.fields = rule.style.fields | cast(u32)StyleTypeface + rule.style.typeface = scan.source[start:start + length] + c = scan + } else { + result.parser = KssFailName(p, "unknown property '", name) + return result + } + + semicolon: KssExpectResult = KssExpect(c, 59) + if !semicolon.ok { + p.cursor = semicolon.parser + result.parser = KssFail(p, "expected ';'") + return result + } + p.cursor = semicolon.parser + result.parser = p + result.rule = rule + return result +} + +// Rules: selector, declaration block, layer and order bookkeeping. Strict +// mode parses the typed selector grammar; declarative mode records the +// selector as a raw span consumed by the shared declarative selector lexer. + +KssParseRule :: (p: KssParser) -> KssParser { + c: KssCursor = p.cursor + origin: KssOrigin + origin.file = p.cursor.file + origin.line = p.cursor.line + origin.column = p.cursor.column + span: KssRuleSpan + span.file = p.cursor.file + span.group = -1 + selector: KssRuleResult + c = KssSkipSpace(c) + p.cursor = c + selector_start: s32 = p.cursor.pos + if p.declarative { + bracket: s32 = 0 + paren: s32 = 0 + quote: u8 = 0 + running: bool = true + while running { + if KssAtEnd(c) { + return KssFail(p, "expected '{'") + } + byte: u8 = p.cursor.source[p.cursor.pos] + if quote != 0 { + if byte == 92 && !KssIsLineBreak(KssPeek(c, 1)) { + c = KssAdvance(c, 2) + p.cursor = c + continue + } + if byte == quote || KssIsLineBreak(byte) { + quote = 0 + } + } else if byte == 47 && (KssPeek(c, 1) == 42 || + (bracket == 0 && paren == 0 && KssPeek(c, 1) == 47)) { + comment: KssExpectResult = KssSelectorComment(c) + if !comment.ok { + return KssFail(p, "unterminated selector comment") + } + c = comment.parser + p.cursor = c + continue + } else if byte == 34 || byte == 39 { + quote = byte + } else if byte == 91 { + bracket = bracket + 1 + } else if byte == 93 && bracket > 0 { + bracket = bracket - 1 + } else if byte == 40 { + paren = paren + 1 + } else if byte == 41 && paren > 0 { + paren = paren - 1 + } else if byte == 123 && bracket == 0 && paren == 0 { + running = false + continue + } + c = KssAdvance(c, 1) + p.cursor = c + } + } else { + selector = KssParseSelector(p) + if selector.parser.status == cast(s32)KssStatusError { + return selector.parser + } + p = selector.parser + c = p.cursor + c = KssSkipSpace(c) + p.cursor = c + if KssAtEnd(c) || p.cursor.source[p.cursor.pos] != 123 { + return KssFail(p, "expected '{'") + } + } + span.selector_start = selector_start + span.selector_length = p.cursor.pos - selector_start + c = KssAdvance(c, 1) + p.cursor = c + span.body_start = p.cursor.pos + rule: StyleRule = selector.rule + rule.layer = p.layer + rule.order = p.rule_total + running: bool = true + while running { + c = KssSkipSpace(c) + p.cursor = c + if KssAtEnd(c) { + return KssFail(p, "unterminated style rule") + } + if p.cursor.source[p.cursor.pos] == 125 { + span.body_length = p.cursor.pos - span.body_start + c = KssAdvance(c, 1) + p.cursor = c + span.end = c.pos + running = false + continue + } + property: KssRuleResult = KssParseProperty(p, rule) + if property.parser.status == cast(s32)KssStatusError { + return property.parser + } + p = property.parser + c = p.cursor + rule = property.rule + } + p.rule = rule + p.origin = origin + p.rule_span = span + p.rule_total = p.rule_total + 1 + p.status = cast(s32)KssStatusRule + return p +} diff --git a/src/parser/selectors.zi b/src/parser/selectors.zi new file mode 100644 index 0000000..602c6f7 --- /dev/null +++ b/src/parser/selectors.zi @@ -0,0 +1,1110 @@ +// Part of the kss_parser module, loaded by src/kss/kss_parser.zi. +// CSS selector matching for declarative hosts. + +// Selector predicates operate on borrowed UTF-8 bytes. Hosts supply attribute +// values and sibling positions; matching semantics stay shared. +KssTextAt :: (value: string, needle: string, start: s32) -> bool { + if start < 0 || start > (cast(s32)value.count) || (cast(s32)needle.count) > (cast(s32)value.count) - start { + return false + } + index: s32 = 0 + while index < (cast(s32)needle.count) { + if value[start + index] != needle[index] { + return false + } + index = index + 1 + } + return true +} + +KssIdentityFacts :: struct { + id: string + name: string + key: string +} + +#program_export +KssIdentityMatches :: (expected: string, facts: KssIdentityFacts) -> bool { + if (cast(s32)expected.count) == 0 { + return false + } + return expected == facts.id || expected == facts.name || expected == facts.key +} + +#program_export +KssAttributeMatches :: (value: string, needle: string, operation: string, present: bool) -> bool { + if !present { + return false + } + if operation == "=" || operation == "" { + return value == needle + } + if operation == "|=" { + return value == needle || (KssTextAt(value, needle, 0) && + (cast(s32)value.count) > (cast(s32)needle.count) && value[(cast(s32)needle.count)] == 45) + } + if (cast(s32)needle.count) == 0 { + return false + } + if operation == "^=" { + return KssTextAt(value, needle, 0) + } + if operation == "$=" { + return KssTextAt(value, needle, (cast(s32)value.count) - (cast(s32)needle.count)) + } + if operation == "*=" { + start: s32 = 0 + while start <= (cast(s32)value.count) - (cast(s32)needle.count) { + if KssTextAt(value, needle, start) { + return true + } + start = start + 1 + } + return false + } + if operation == "~=" { + start: s32 = 0 + while start < (cast(s32)value.count) { + while start < (cast(s32)value.count) && KssIsSpace(value[start]) { + start = start + 1 + } + end: s32 = start + while end < (cast(s32)value.count) && !KssIsSpace(value[end]) { + end = end + 1 + } + if end - start == (cast(s32)needle.count) && KssTextAt(value, needle, start) { + return true + } + start = end + } + return false + } + return false +} + +KssNthFormula :: struct { + step: s64 + offset: s64 + ok: bool +} + +#program_export +KssParseNth :: (source: string) -> KssNthFormula { + result: KssNthFormula + c: KssCursor + c.source = source + space: KssExpectResult = KssSelectorSpace(c) + if !space.ok { + return result + } + c = space.parser + word: KssNameResult = KssReadName(c) + tail: KssExpectResult = KssSelectorSpace(word.parser) + if word.ok && tail.ok && KssAtEnd(tail.parser) { + if KssNameEquals(word.name, "odd") { + result.step = 2 + result.offset = 1 + result.ok = true + return result + } + if KssNameEquals(word.name, "even") { + result.step = 2 + result.ok = true + return result + } + } + sign: s64 = 1 + if KssPeek(c, 0) == 45 { + sign = -1 + c = KssAdvance(c, 1) + } else if KssPeek(c, 0) == 43 { + c = KssAdvance(c, 1) + } + number: s64 = 0 + digits: bool = false + while KssIsDigit(KssPeek(c, 0)) { + number = number * 10 + cast(s64)(KssPeek(c, 0) - 48) + if number > 2147483647 { + return result + } + digits = true + c = KssAdvance(c, 1) + } + if KssLower(KssPeek(c, 0)) != 110 { + tail = KssSelectorSpace(c) + result.ok = digits && tail.ok && KssAtEnd(tail.parser) + if result.ok { + result.offset = sign * number + } + return result + } + if !digits { + number = 1 + } + step: s64 = sign * number + space = KssSelectorSpace(KssAdvance(c, 1)) + if !space.ok { + return result + } + c = space.parser + offset: s64 = 0 + if !KssAtEnd(c) { + sign = 1 + if KssPeek(c, 0) == 45 { + sign = -1 + } else if KssPeek(c, 0) != 43 { + return result + } + space = KssSelectorSpace(KssAdvance(c, 1)) + if !space.ok { + return result + } + c = space.parser + digits = false + while KssIsDigit(KssPeek(c, 0)) { + offset = offset * 10 + cast(s64)(KssPeek(c, 0) - 48) + if offset > 2147483647 { + return result + } + digits = true + c = KssAdvance(c, 1) + } + tail = KssSelectorSpace(c) + if !digits || !tail.ok || !KssAtEnd(tail.parser) { + return result + } + offset = sign * offset + } + result.step = step + result.offset = offset + result.ok = true + return result +} + +KssNthAppendInteger :: (text: KssName, value: s64) -> KssName { + if value < 0 { + text = KssNameAppend(text, 45) + value = -value + } + divisor: s64 = 1 + while value / divisor >= 10 { + divisor = divisor * 10 + } + while divisor > 0 { + text = KssNameAppend(text, cast(u8)(48 + value / divisor)) + value = value % divisor + divisor = divisor / 10 + } + return text +} + +// A bounded canonical CSS An+B spelling; invalid formulas produce empty text. +#program_export +KssNthText :: (source: string) -> KssName { + text: KssName + formula: KssNthFormula = KssParseNth(source) + if !formula.ok { + return text + } + if formula.step == 0 { + return KssNthAppendInteger(text, formula.offset) + } + if formula.step == -1 { + text = KssNameAppend(text, 45) + } else if formula.step != 1 { + text = KssNthAppendInteger(text, formula.step) + } + text = KssNameAppend(text, 110) + if formula.offset > 0 { + text = KssNameAppend(text, 43) + } + if formula.offset != 0 { + text = KssNthAppendInteger(text, formula.offset) + } + return text +} + +#program_export +KssNthMatches :: (source: string, position: s32) -> bool { + if position <= 0 { + return false + } + formula: KssNthFormula = KssParseNth(source) + if !formula.ok { + return false + } + if formula.step == 0 { + return cast(s64)position == formula.offset + } + delta: s64 = cast(s64)position - formula.offset + return delta % formula.step == 0 && delta / formula.step >= 0 +} + +#program_export +KssNthSiblingMatches :: (source: string, index: s32, count: s32, from_end: bool) -> bool { + if index < 0 || index >= count { + return false + } + position: s32 = index + 1 + if from_end { + position = count - index + } + return KssNthMatches(source, position) +} + +KssStateFacts :: struct { + kind: string + tag: string + any_active: bool + requested: bool + hover: bool + pressed: bool + focus: bool + disabled: bool + node_disabled: bool + readonly: bool + readonly_camel: bool + node_readonly: bool + required: bool + node_required: bool + valid: bool + invalid: bool + aria_invalid: bool + extra_invalid: bool + placeholder_shown: bool + placeholder_shown_camel: bool + placeholder_present: bool + value_present: bool +} + +#program_export +KssStateName :: (source: string) -> KssName { + name: KssName + if (cast(s32)source.count) > 64 { + return name + } + while name.length < (cast(s32)source.count) { + name.bytes[name.length] = KssLower(source[name.length]) + name.length = name.length + 1 + } + return name +} + +KssIsFormControl :: (kind: string, tag: string) -> bool { + if tag == "input" || tag == "select" || tag == "textarea" { + return true + } + if kind == "TextField" || kind == "Input" || kind == "TextArea" || + kind == "ColorPicker" || kind == "Slider" || kind == "Spinbox" { + return true + } + return kind == "Dropdown" || kind == "ListBox" || kind == "Checkbox" || + kind == "Toggle" || kind == "Radio" +} + +#program_export +KssStateMatches :: (source: string, facts: KssStateFacts) -> bool { + if (cast(s32)source.count) > 64 { + return false + } + name: KssName = KssStateName(source) + if name.length == 0 || KssNameEquals(name, "any") { + return true + } + if KssNameEquals(name, "normal") { + return !facts.any_active && !facts.node_readonly && !facts.node_required + } + if KssNameEquals(name, "hover") { + return facts.requested || facts.hover + } + if KssNameEquals(name, "pressed") || KssNameEquals(name, "active") { + return facts.requested || facts.pressed + } + if KssNameEquals(name, "focus") || KssNameEquals(name, "focused") || + KssNameEquals(name, "focus-visible") { + return facts.requested || facts.focus + } + if KssNameEquals(name, "enabled") { + return !facts.disabled && !facts.node_disabled + } + if KssNameEquals(name, "readonly") || KssNameEquals(name, "read-only") { + return facts.node_readonly || facts.readonly || facts.readonly_camel + } + if KssNameEquals(name, "required") { + return facts.node_required || facts.required + } + if KssNameEquals(name, "optional") { + return !facts.node_required && !facts.required + } + if KssNameEquals(name, "valid") { + if facts.valid { + return true + } + return KssIsFormControl(facts.kind, facts.tag) && !facts.invalid && + !facts.aria_invalid && !facts.extra_invalid + } + if KssNameEquals(name, "placeholder-shown") || KssNameEquals(name, "placeholder_shown") { + if facts.placeholder_shown || facts.placeholder_shown_camel { + return true + } + return KssIsFormControl(facts.kind, facts.tag) && facts.placeholder_present && + !facts.value_present + } + return facts.requested +} + +KssStructuralFacts :: struct { + has_parent: bool + has_scope: bool + is_scope: bool + sibling_index: s32 + sibling_count: s32 + type_index: s32 + type_count: s32 + has_children: bool + has_text: bool + focus_within: bool + target: bool +} + +// -1 means the name is not a basic structural pseudo. KssPseudoMatch handles +// further dispatch; zero and one are complete decisions from these facts. +#program_export +KssStructuralMatch :: (name: string, facts: KssStructuralFacts) -> s32 { + matched: bool = false + if name == "root" { + matched = !facts.has_parent + } else if name == "scope" { + if facts.has_scope { + matched = facts.is_scope + } else { + matched = !facts.has_parent + } + } else if name == "first-child" { + matched = facts.sibling_index == 0 && facts.sibling_count > 0 + } else if name == "last-child" { + matched = facts.sibling_index >= 0 && facts.sibling_index == facts.sibling_count - 1 + } else if name == "only-child" { + matched = facts.sibling_index == 0 && facts.sibling_count == 1 + } else if name == "first-of-type" { + matched = facts.type_index == 0 && facts.type_count > 0 + } else if name == "last-of-type" { + matched = facts.type_index >= 0 && facts.type_index == facts.type_count - 1 + } else if name == "only-of-type" { + matched = facts.type_index == 0 && facts.type_count == 1 + } else if name == "empty" { + matched = !facts.has_children && !facts.has_text + } else if name == "focus-within" { + matched = facts.focus_within + } else if name == "target" { + matched = facts.target + } else { + return -1 + } + if matched { + return 1 + } + return 0 +} + +KssPseudoFunction :: struct { + relative: bool + positional: bool + same_type: bool + from_end: bool +} + +#program_export +KssPseudoFunctionInfo :: (name: string) -> KssPseudoFunction { + result: KssPseudoFunction + result.relative = name == "has" + result.positional = name == "nth-child" || name == "nth-last-child" || + name == "nth-of-type" || name == "nth-last-of-type" + result.same_type = name == "nth-of-type" || name == "nth-last-of-type" + result.from_end = name == "nth-last-child" || name == "nth-last-of-type" + return result +} + +// Names and arguments come from selector atoms. -1 requests relative :has +// traversal; all other supported/unknown cases resolve to zero or one here. +#program_export +KssPseudoMatch :: (name: string, argument: string, functional: bool, + facts: KssStructuralFacts) -> s32 { + if !functional { + decision: s32 = KssStructuralMatch(name, facts) + if decision < 0 { + return 0 + } + return decision + } + kind: KssPseudoFunction = KssPseudoFunctionInfo(name) + if kind.relative { + return -1 + } + if !kind.positional { + return 0 + } + index: s32 = facts.sibling_index + count: s32 = facts.sibling_count + if kind.same_type { + index = facts.type_index + count = facts.type_count + } + matched: bool = KssNthSiblingMatches(argument, index, count, kind.from_end) + if matched { + return 1 + } + return 0 +} + +// Declarative selector lexing. Results borrow spans from the input; the host +// stores the resulting selector objects but does not parse their syntax. +KssSelectorStack :: struct { + bytes: [64]u8 +} + +KssSelectorSpan :: struct { + parser: KssCursor + start: s32 + length: s32 + combinator: s32 + ok: bool + done: bool +} + +KssSelectorComment :: (c: KssCursor) -> KssExpectResult { + result: KssExpectResult + line: bool = KssPeek(c, 1) == 47 + c = KssAdvance(c, 2) + while !KssAtEnd(c) { + if line && KssPeek(c, 0) == 10 { + result.parser = c + result.ok = true + return result + } + if !line && KssPeek(c, 0) == 42 && KssPeek(c, 1) == 47 { + result.parser = KssAdvance(c, 2) + result.ok = true + return result + } + c = KssAdvance(c, 1) + } + result.parser = c + result.ok = line + return result +} + +KssSelectorSpace :: (c: KssCursor) -> KssExpectResult { + result: KssExpectResult + while !KssAtEnd(c) { + if KssIsSpace(KssPeek(c, 0)) { + c = KssAdvance(c, 1) + } else if KssPeek(c, 0) == 47 && (KssPeek(c, 1) == 42 || KssPeek(c, 1) == 47) { + comment: KssExpectResult = KssSelectorComment(c) + c = comment.parser + if !comment.ok { + return comment + } + } else { + break + } + } + result.parser = c + result.ok = true + return result +} + +KssSelectorPartMode :: (c: KssCursor, sequence: bool, relative: bool) -> KssSelectorSpan { + result: KssSelectorSpan + initial: s32 = c.pos + skipped: KssExpectResult = KssSelectorSpace(c) + c = skipped.parser + result.parser = c + if !skipped.ok { + return result + } + if KssAtEnd(c) { + result.ok = !(initial > 0 && c.source[initial - 1] == 44) + result.done = true + return result + } + if sequence { + if initial > 0 { + result.combinator = 32 + } + byte: u8 = KssPeek(c, 0) + if byte == 62 || byte == 43 || byte == 126 { + if initial == 0 && !relative { + return result + } + result.combinator = cast(s32)byte + skipped = KssSelectorSpace(KssAdvance(c, 1)) + c = skipped.parser + result.parser = c + if !skipped.ok || KssAtEnd(c) { + return result + } + } + } + result.start = c.pos + end: s32 = c.pos + stack: KssSelectorStack + depth: s32 = 0 + quote: u8 = 0 + while !KssAtEnd(c) { + byte: u8 = KssPeek(c, 0) + if quote != 0 { + if byte == 92 && !KssIsLineBreak(KssPeek(c, 1)) { + c = KssAdvance(c, 2) + end = c.pos + continue + } + if byte == quote || KssIsLineBreak(byte) { + quote = 0 + } + } else if byte == 34 || byte == 39 { + quote = byte + } else if byte == 47 && (KssPeek(c, 1) == 42 || (depth == 0 && KssPeek(c, 1) == 47)) { + comment: KssExpectResult = KssSelectorComment(c) + c = comment.parser + if !comment.ok { + result.parser = c + return result + } + continue + } else if byte == 40 || byte == 91 { + if depth >= 64 { + result.parser = c + return result + } + stack.bytes[depth] = byte + depth = depth + 1 + } else if byte == 41 || byte == 93 { + if depth == 0 { + result.parser = c + return result + } + opening: u8 = stack.bytes[depth - 1] + if (byte == 41 && opening != 40) || (byte == 93 && opening != 91) { + result.parser = c + return result + } + depth = depth - 1 + } else if depth == 0 { + if !sequence && byte == 44 { + break + } + if sequence && (KssIsSpace(byte) || byte == 62 || byte == 43 || byte == 126) { + break + } + } + c = KssAdvance(c, 1) + if !KssIsSpace(byte) || depth > 0 || quote != 0 { + end = c.pos + } + } + result.parser = c + if depth != 0 || quote != 0 || end <= result.start { + return result + } + result.length = end - result.start + if !sequence && KssPeek(c, 0) == 44 { + result.parser = KssAdvance(c, 1) + } + result.ok = true + return result +} + +#program_export +KssSelectorPart :: (c: KssCursor, sequence: bool) -> KssSelectorSpan { + return KssSelectorPartMode(c, sequence, false) +} + +#program_export +KssRelativeSelectorPart :: (c: KssCursor) -> KssSelectorSpan { + result: KssSelectorSpan = KssSelectorPartMode(c, true, true) + if c.pos == 0 && result.ok && !result.done && result.combinator == 0 { + result.combinator = 32 + } + return result +} + +KssSelectorGroup :: (c: KssCursor) -> KssSelectorSpan { + result: KssSelectorSpan + opening: u8 = KssPeek(c, 0) + stack: KssSelectorStack + stack.bytes[0] = opening + depth: s32 = 1 + quote: u8 = 0 + c = KssAdvance(c, 1) + result.start = c.pos + while !KssAtEnd(c) { + byte: u8 = KssPeek(c, 0) + if quote != 0 { + if byte == 92 && !KssIsLineBreak(KssPeek(c, 1)) { + c = KssAdvance(c, 2) + continue + } + if byte == quote || KssIsLineBreak(byte) { + quote = 0 + } + } else if byte == 34 || byte == 39 { + quote = byte + } else if byte == 47 && KssPeek(c, 1) == 42 { + comment: KssExpectResult = KssSelectorComment(c) + c = comment.parser + if !comment.ok { + result.parser = c + return result + } + continue + } else if byte == 40 || byte == 91 { + if depth >= 64 { + result.parser = c + return result + } + stack.bytes[depth] = byte + depth = depth + 1 + } else if byte == 41 || byte == 93 { + opening = stack.bytes[depth - 1] + if (byte == 41 && opening != 40) || (byte == 93 && opening != 91) { + result.parser = c + return result + } + depth = depth - 1 + if depth == 0 { + result.length = c.pos - result.start + result.parser = KssAdvance(c, 1) + result.ok = true + return result + } + } + c = KssAdvance(c, 1) + } + result.parser = c + return result +} + +// The host retains a stack of frames and supplies relationships for cursor. +// A popped child means that candidate failed, so the resumed frame tries the +// next ancestor/sibling only for descendant and general-sibling combinators. +KssSelectorChainAction :: enum { + KssSelectorPop :: 0 + KssSelectorPush :: 1 + KssSelectorAccept :: 2 +} + +KssSelectorChainFrame :: struct { + part: s32 + cursor: s32 + entered: bool + anchored: bool + anchor: s32 +} + +KssSelectorChainResult :: struct { + frame: KssSelectorChainFrame + next: KssSelectorChainFrame + action: s32 +} + +#program_export +KssSelectorChainBegin :: (count: s32, node: s32) -> KssSelectorChainFrame { + frame: KssSelectorChainFrame + frame.part = count - 1 + frame.cursor = node + return frame +} + +#program_export +KssRelativeSelectorBegin :: (count: s32, node: s32, anchor: s32) -> KssSelectorChainFrame { + frame: KssSelectorChainFrame = KssSelectorChainBegin(count, node) + frame.anchored = count > 0 && anchor >= 0 + frame.anchor = anchor + if !frame.anchored { + frame.cursor = -1 + } + return frame +} + +#program_export +KssSelectorChainStep :: (frame: KssSelectorChainFrame, matched: bool, + relation: s32, parent: s32, previous: s32) -> KssSelectorChainResult { + result: KssSelectorChainResult + result.frame = frame + if frame.cursor < 0 { + return result + } + if frame.part < 0 { + if frame.part == -1 && frame.anchored && frame.cursor == frame.anchor { + result.action = cast(s32)KssSelectorAccept + } + return result + } + repeated: bool = relation == 0 || relation == 32 || relation == 126 + if frame.entered { + if !repeated { + return result + } + } else { + if !matched { + return result + } + if frame.part == 0 && !frame.anchored { + result.action = cast(s32)KssSelectorAccept + return result + } + } + candidate: s32 = -1 + if relation == 0 || relation == 32 || relation == 62 { + candidate = parent + } else if relation == 43 || relation == 126 { + candidate = previous + } + if candidate < 0 { + return result + } + result.frame.cursor = candidate + result.frame.entered = true + result.next.part = frame.part - 1 + result.next.cursor = candidate + result.next.anchored = frame.anchored + result.next.anchor = frame.anchor + result.action = cast(s32)KssSelectorPush + return result +} + +// Hosts count matching alternatives; this policy owns selector-list negation. +#program_export +KssSelectorGroupMatches :: (name: string, count: s32, matching: s32) -> bool { + if count <= 0 || matching < 0 || matching > count { + return false + } + if name == "not" { + return matching == 0 + } + if name == "is" || name == "where" { + return matching > 0 + } + return false +} + +KssSelectorAtomKind :: enum { + KssSelectorKind :: 0 + KssSelectorId :: 1 + KssSelectorClass :: 2 + KssSelectorAttribute :: 3 + KssSelectorPseudo :: 4 + KssSelectorState :: 5 + KssSelectorNot :: 6 + KssSelectorMatches :: 7 +} + +KssSelectorCursor :: struct { + cursor: KssCursor + specificity: s32 + has_kind: bool +} + +KssSelectorAtom :: struct { + parser: KssSelectorCursor + kind: s32 + name: string + value: string + operation: string + argument: string + canonical: KssName + has_value: bool + has_argument: bool + ok: bool + done: bool +} + +#program_export +KssBeginSelector :: (source: string) -> KssSelectorCursor { + parser: KssSelectorCursor + parser.cursor.source = source + return parser +} + +KssPseudoName :: (source: string) -> KssName { + name: KssName = KssStateName(source) + index: s32 = 0 + while index < name.length { + if name.bytes[index] == 95 { + name.bytes[index] = 45 + } + index = index + 1 + } + return name +} + +KssPseudoState :: (name: KssName) -> KssName { + if KssNameEquals(name, "active") { + return KssMakeName("pressed") + } + if KssNameEquals(name, "focused") || KssNameEquals(name, "focus-visible") { + return KssMakeName("focus") + } + if KssNameEquals(name, "read-only") { + return KssMakeName("readonly") + } + if KssNameEquals(name, "hover") || KssNameEquals(name, "pressed") || + KssNameEquals(name, "focus") || KssNameEquals(name, "normal") || + KssNameEquals(name, "disabled") || KssNameEquals(name, "loading") || + KssNameEquals(name, "selected") || KssNameEquals(name, "checked") { + return name + } + if KssNameEquals(name, "invalid") || KssNameEquals(name, "valid") || + KssNameEquals(name, "indeterminate") || KssNameEquals(name, "default") || + KssNameEquals(name, "autofill") || KssNameEquals(name, "placeholder-shown") || + KssNameEquals(name, "expanded") || KssNameEquals(name, "open") { + return name + } + if KssNameEquals(name, "readonly") || KssNameEquals(name, "required") || + KssNameEquals(name, "enabled") || KssNameEquals(name, "optional") { + return name + } + empty: KssName + return empty +} + +KssSelectorAttributeAtom :: (result: KssSelectorAtom, group: KssSelectorSpan) -> KssSelectorAtom { + c: KssCursor + c.source = group.parser.source[group.start:group.start + group.length] + skipped: KssExpectResult = KssSelectorSpace(c) + c = skipped.parser + start: s32 = c.pos + key: KssNameResult = KssReadName(c) + if !skipped.ok || !key.ok { + return result + } + result.name = c.source[start:key.parser.pos] + skipped = KssSelectorSpace(key.parser) + c = skipped.parser + if !skipped.ok { + return result + } + if KssAtEnd(c) { + result.ok = true + return result + } + start = c.pos + byte: u8 = KssPeek(c, 0) + if byte == 126 || byte == 124 || byte == 94 || byte == 36 || byte == 42 { + c = KssAdvance(c, 1) + } + if KssPeek(c, 0) != 61 { + return result + } + c = KssAdvance(c, 1) + result.operation = c.source[start:c.pos] + skipped = KssSelectorSpace(c) + c = skipped.parser + if !skipped.ok || KssAtEnd(c) { + return result + } + start = c.pos + end: s32 = (cast(s32)c.source.count) + quote: u8 = KssPeek(c, 0) + if quote == 34 || quote == 39 { + c = KssAdvance(c, 1) + start = c.pos + while !KssAtEnd(c) && KssPeek(c, 0) != quote && !KssIsLineBreak(KssPeek(c, 0)) { + if KssPeek(c, 0) == 92 && !KssIsLineBreak(KssPeek(c, 1)) { + c = KssAdvance(c, 2) + } else { + c = KssAdvance(c, 1) + } + } + if KssAtEnd(c) || KssPeek(c, 0) != quote { + return result + } + end = c.pos + skipped = KssSelectorSpace(KssAdvance(c, 1)) + if !skipped.ok || !KssAtEnd(skipped.parser) { + return result + } + } else { + while !KssAtEnd(c) && !KssIsSpace(KssPeek(c, 0)) { + if KssPeek(c, 0) == 47 && KssPeek(c, 1) == 42 { + break + } + c = KssAdvance(c, 1) + } + end = c.pos + skipped = KssSelectorSpace(c) + if !skipped.ok || !KssAtEnd(skipped.parser) { + return result + } + } + result.value = c.source[start:end] + result.has_value = true + result.ok = true + return result +} + +// Nested :has is outside the supported selector language. Inspect tokens, +// excluding quoted values, attributes, and comments, rather than searching text. +KssHasArgumentAllowed :: (source: string) -> bool { + c: KssCursor + c.source = source + while !KssAtEnd(c) { + skipped: KssExpectResult = KssSelectorSpace(c) + c = skipped.parser + if !skipped.ok { + return false + } + byte: u8 = KssPeek(c, 0) + if byte == 91 { + group: KssSelectorSpan = KssSelectorGroup(c) + if !group.ok { + return false + } + c = group.parser + } else if byte == 34 || byte == 39 { + c = KssAdvance(c, 1) + while !KssAtEnd(c) && KssPeek(c, 0) != byte { + if KssPeek(c, 0) == 92 { + c = KssAdvance(c, 2) + } else { + c = KssAdvance(c, 1) + } + } + if KssAtEnd(c) { + return false + } + c = KssAdvance(c, 1) + } else if byte == 58 { + c = KssAdvance(c, 1) + start: s32 = c.pos + name: KssNameResult = KssReadSelectorName(c) + if !name.ok { + return false + } + c = name.parser + canonical: KssName = KssPseudoName(source[start:c.pos]) + if KssNameEquals(canonical, "has") && KssPeek(c, 0) == 40 { + return false + } + } else { + c = KssAdvance(c, 1) + } + } + return true +} + +#program_export +KssSelectorNext :: (parser: KssSelectorCursor) -> KssSelectorAtom { + result: KssSelectorAtom + result.parser = parser + skipped: KssExpectResult = KssSelectorSpace(parser.cursor) + c: KssCursor = skipped.parser + result.parser.cursor = c + if !skipped.ok { + return result + } + if KssAtEnd(c) { + result.done = true + result.ok = true + return result + } + byte: u8 = KssPeek(c, 0) + start: s32 = c.pos + if byte == 91 { + group: KssSelectorSpan = KssSelectorGroup(c) + result.parser.cursor = group.parser + if !group.ok { + return result + } + result.kind = cast(s32)KssSelectorAttribute + result = KssSelectorAttributeAtom(result, group) + if result.ok { + result.parser.specificity = result.parser.specificity + StyleSpecificity(0, 1, 0, 0) + } + return result + } + if byte == 35 || byte == 46 || byte == 58 { + c = KssAdvance(c, 1) + start = c.pos + name: KssNameResult = KssReadSelectorName(c) + if !name.ok || !KssIdentStart(KssPeek(c, 0)) { + return result + } + c = name.parser + result.name = c.source[start:c.pos] + if byte == 35 { + result.kind = cast(s32)KssSelectorId + result.parser.specificity = result.parser.specificity + StyleSpecificity(0, 0, 0, 1) + } else if byte == 46 { + result.kind = cast(s32)KssSelectorClass + result.parser.specificity = result.parser.specificity + StyleSpecificity(0, 0, 1, 0) + } else { + result.kind = cast(s32)KssSelectorPseudo + result.canonical = KssPseudoName(result.name) + if result.canonical.length == 0 { + return result + } + result.parser.specificity = result.parser.specificity + StyleSpecificity(0, 1, 0, 0) + if KssPeek(c, 0) == 40 { + group: KssSelectorSpan = KssSelectorGroup(c) + result.parser.cursor = group.parser + if !group.ok { + return result + } + result.has_argument = true + result.argument = c.source[group.start:group.start + group.length] + c = group.parser + if KssNameEquals(result.canonical, "not") || KssNameEquals(result.canonical, "is") || + KssNameEquals(result.canonical, "where") || KssNameEquals(result.canonical, "has") { + argument_cursor: KssCursor + argument_cursor.source = result.argument + argument_space: KssExpectResult = KssSelectorSpace(argument_cursor) + if !argument_space.ok || KssAtEnd(argument_space.parser) { + return result + } + } + if KssNameEquals(result.canonical, "has") && !KssHasArgumentAllowed(result.argument) { + return result + } + if KssNameEquals(result.canonical, "not") { + result.kind = cast(s32)KssSelectorNot + } else if KssNameEquals(result.canonical, "is") || KssNameEquals(result.canonical, "where") { + result.kind = cast(s32)KssSelectorMatches + } + } else { + state: KssName = KssPseudoState(result.canonical) + if state.length > 0 { + result.kind = cast(s32)KssSelectorState + result.canonical = state + } + } + } + } else { + if parser.has_kind || (!KssIdentStart(byte) && byte != 42) { + return result + } + while !KssAtEnd(c) { + byte = KssPeek(c, 0) + if KssIsSpace(byte) || byte == 46 || byte == 35 || byte == 91 || byte == 58 { + break + } + if byte == 47 && (KssPeek(c, 1) == 42 || KssPeek(c, 1) == 47) { + break + } + if byte == 44 || byte == 40 || byte == 41 || byte == 93 || byte == 62 || byte == 43 || byte == 126 { + return result + } + c = KssAdvance(c, 1) + } + result.name = c.source[start:c.pos] + result.kind = cast(s32)KssSelectorKind + result.parser.has_kind = true + if result.name != "*" { + result.parser.specificity = result.parser.specificity + StyleSpecificity(1, 0, 0, 0) + } + } + result.parser.cursor = c + result.ok = true + return result +} diff --git a/src/parser/tokens.zi b/src/parser/tokens.zi new file mode 100644 index 0000000..c8e6c90 --- /dev/null +++ b/src/parser/tokens.zi @@ -0,0 +1,124 @@ +// Part of the kss_parser module, loaded by src/kss/kss_parser.zi. +// The token table: lookups, caller overrides, and overlays. + +// Token table. Later entries shadow earlier ones; overlays update in place so +// provenance always reflects the winning value. Variant color overrides are +// applied on add, matching the highest precedence layer. + +KssFindToken :: (p: KssParser, name: KssName, kind: s32) -> s32 { + index: s32 = p.token_count - 1 + while index >= 0 { + if p.tokens[index].kind == kind && KssNameEqualsName(p.tokens[index].name, name) { + return index + } + index = index - 1 + } + return -1 +} + +KssApplyOverride :: (p: KssParser, index: s32) -> KssParser { + override: s32 = 0 + while override < p.override_count { + if p.tokens[index].kind == cast(s32)KssTokenColor && + cast(s32)p.overrides[override].name.count == p.tokens[index].name.length { + same: bool = true + byte_index: s32 = 0 + while byte_index < p.tokens[index].name.length { + if KssLower(cast(u8)p.overrides[override].name[byte_index]) != + KssLower(p.tokens[index].name.bytes[byte_index]) { + same = false + } + byte_index = byte_index + 1 + } + if same { + p.tokens[index].color = p.overrides[override].color + p.tokens[index].origin = cast(s32)KssOriginVariant + return p + } + } + override = override + 1 + } + return p +} + +KssAddToken :: (p: KssParser, token: KssToken) -> KssParser { + if p.token_count >= 128 { + return KssFail(p, "style token capacity exceeded") + } + p.tokens[p.token_count] = token + p.token_count = p.token_count + 1 + p = KssApplyOverride(p, p.token_count - 1) + return p +} + +// Update an existing token in place: theme overlays and matching environment +// overlays win over base and imported values but keep a single entry. + +KssOverlayToken :: (p: KssParser, name: KssName, kind: s32, color: u32, + number: float32, material: s32, origin: s32) -> KssParser { + index: s32 = KssFindToken(p, name, kind) + if index < 0 { + return KssFailName(p, "unknown overlay token '", name) + } + if kind == cast(s32)KssTokenColor { + p.tokens[index].color = color + } else if kind == cast(s32)KssTokenMaterial { + p.tokens[index].material = material + } else { + p.tokens[index].number = number + } + p.tokens[index].origin = origin + p.tokens[index].file = p.cursor.file + p.tokens[index].line = p.cursor.line + p.tokens[index].column = p.cursor.column + p = KssApplyOverride(p, index) + return p +} + +KssFindColorToken :: (p: KssParser, name: KssName) -> s32 { + return KssFindToken(p, name, cast(s32)KssTokenColor) +} + +KssColorTokenResult :: struct { + found: bool + color: u32 +} + +#program_export +KssColorTokenValue :: (parser: KssParser, +name: string) -> KssColorTokenResult { + result: KssColorTokenResult + result.found = false + result.color = 0 + index: s32 = KssFindColorToken(parser, KssMakeName(name)) + if index >= 0 { + result.found = true + result.color = parser.tokens[index].color + } + return result +} + +KssFindNumberToken :: (p: KssParser, name: KssName) -> s32 { + index: s32 = KssFindToken(p, name, cast(s32)KssTokenDuration) + if index >= 0 { + return index + } + return KssFindToken(p, name, cast(s32)KssTokenLength) +} + +KssFindMaterialToken :: (p: KssParser, name: KssName) -> s32 { + return KssFindToken(p, name, cast(s32)KssTokenMaterial) +} + +KssMaterialValue :: (name: KssName) -> s32 { + if KssNameEquals(name, "lightfield") { + return cast(s32)MaterialLightfield + } + if KssNameEquals(name, "flat") { + return cast(s32)MaterialFlat + } + if KssNameEquals(name, "glass") { + return cast(s32)MaterialGlass + } + return -999999 +} diff --git a/src/parser/vocabulary.zi b/src/parser/vocabulary.zi new file mode 100644 index 0000000..28d7c91 --- /dev/null +++ b/src/parser/vocabulary.zi @@ -0,0 +1,405 @@ +// Part of the kss_parser module, loaded by src/kss/kss_parser.zi. +// Widget kinds, classes, states, tones, emphases, sizes, and roles by name. + +// Stable class ids match StyleClassId in the C and Go pack registries. + +KssClassId :: (name: KssName) -> s32 { + hash: u32 = 2166136261 + if name.length == 0 { + return 0 + } + index: s32 = 0 + while index < name.length { + hash = hash ^ cast(u32)name.bytes[index] + hash = hash * cast(u32)16777619 + index = index + 1 + } + hash = hash & cast(u32)2147483647 + if hash == 0 { + return 1 + } + return cast(s32)hash +} + +KssKindValue :: (name: KssName) -> s32 { + if KssNameEquals(name, "*") || KssNameEquals(name, "Any") { + return StyleKindAny() + } + if KssNameEquals(name, "App") { + return StyleKindApp() + } + if KssNameEquals(name, "Button") { + return StyleKindButton() + } + if KssNameEquals(name, "Text") { + return StyleKindText() + } + if KssNameEquals(name, "TextField") { + return StyleKindTextField() + } + if KssNameEquals(name, "TextArea") { + return StyleKindTextArea() + } + if KssNameEquals(name, "Surface") { + return StyleKindSurface() + } + if KssNameEquals(name, "Dropdown") { + return StyleKindDropdown() + } + if KssNameEquals(name, "Card") { + return StyleKindCard() + } + if KssNameEquals(name, "Slider") { + return StyleKindSlider() + } + if KssNameEquals(name, "SliderThumb") { + return StyleKindSliderThumb() + } + if KssNameEquals(name, "Toggle") { + return StyleKindToggle() + } + if KssNameEquals(name, "ToggleThumb") { + return StyleKindToggleThumb() + } + if KssNameEquals(name, "Scroll") { + return StyleKindScroll() + } + if KssNameEquals(name, "ScrollThumb") { + return StyleKindScrollThumb() + } + if KssNameEquals(name, "Checkbox") { + return StyleKindCheckbox() + } + if KssNameEquals(name, "Radio") { + return StyleKindRadio() + } + if KssNameEquals(name, "Progress") { + return StyleKindProgress() + } + if KssNameEquals(name, "Separator") { + return StyleKindSeparator() + } + if KssNameEquals(name, "NavigationBar") { + return StyleKindNavigationBar() + } + if KssNameEquals(name, "NavigationBarItem") { + return StyleKindNavigationBarItem() + } + if KssNameEquals(name, "Selectable") { + return StyleKindSelectable() + } + if KssNameEquals(name, "Fieldset") { + return StyleKindFieldset() + } + if KssNameEquals(name, "Plot") { + return StyleKindPlot() + } + if KssNameEquals(name, "PlotMark") { + return StyleKindPlotMark() + } + if KssNameEquals(name, "Link") { + return StyleKindLink() + } + if KssNameEquals(name, "TabBar") { + return StyleKindTabBar() + } + if KssNameEquals(name, "Tab") { + return StyleKindTab() + } + if KssNameEquals(name, "TabClose") { + return StyleKindTabClose() + } + if KssNameEquals(name, "SegmentedControl") { + return StyleKindSegmentedControl() + } + if KssNameEquals(name, "Segment") { + return StyleKindSegment() + } + if KssNameEquals(name, "Menu") { + return StyleKindMenu() + } + if KssNameEquals(name, "MenuItem") { + return StyleKindMenuItem() + } + if KssNameEquals(name, "MenuSeparator") { + return StyleKindMenuSeparator() + } + if KssNameEquals(name, "ListBox") { + return StyleKindListBox() + } + if KssNameEquals(name, "ListBoxItem") { + return StyleKindListBoxItem() + } + if KssNameEquals(name, "TreeView") { + return StyleKindTreeView() + } + if KssNameEquals(name, "TreeViewItem") { + return StyleKindTreeViewItem() + } + return KssKindValueMore(name) +} + +KssKindValueMore :: (name: KssName) -> s32 { + if KssNameEquals(name, "ListBoxMulti") { + return StyleKindListBoxMulti() + } + if KssNameEquals(name, "ListBoxMultiItem") { + return StyleKindListBoxMultiItem() + } + if KssNameEquals(name, "DragDropTarget") { + return StyleKindDragDropTarget() + } + if KssNameEquals(name, "Spinbox") { + return StyleKindSpinbox() + } + if KssNameEquals(name, "SpinboxValue") { + return StyleKindSpinboxValue() + } + if KssNameEquals(name, "ColorPicker") { + return StyleKindColorPicker() + } + if KssNameEquals(name, "ColorPickerSwatch") { + return StyleKindColorPickerSwatch() + } + if KssNameEquals(name, "PanedView") { + return StyleKindPanedView() + } + if KssNameEquals(name, "Toast") { + return StyleKindToast() + } + if KssNameEquals(name, "Collapsible") { + return StyleKindCollapsible() + } + if KssNameEquals(name, "TitleBar") { + return StyleKindTitleBar() + } + if KssNameEquals(name, "Toolbar") { + return StyleKindToolbar() + } + if KssNameEquals(name, "Modal") { + return StyleKindModal() + } + if KssNameEquals(name, "TableView") { + return StyleKindTableView() + } + if KssNameEquals(name, "Guide") { + return StyleKindGuide() + } + if KssNameEquals(name, "Image") { + return StyleKindImage() + } + if KssNameEquals(name, "Focus") { + return StyleKindFocus() + } + if KssNameEquals(name, "Popup") { + return StyleKindPopup() + } + if KssNameEquals(name, "Canvas") { + return StyleKindCanvas() + } + if KssNameEquals(name, "Drag") { + return StyleKindDrag() + } + if KssNameEquals(name, "DragValue") { + return StyleKindDragValue() + } + if KssNameEquals(name, "Heading") { + return StyleKindHeading() + } + if KssNameEquals(name, "ParagraphText") { + return StyleKindParagraphText() + } + if KssNameEquals(name, "Page") { + return StyleKindPage() + } + if KssNameEquals(name, "Section") { + return StyleKindSection() + } + if KssNameEquals(name, "Reorder") { + return StyleKindReorder() + } + return -999999 +} + +KssStateValue :: (name: KssName) -> s32 { + if KssNameEquals(name, "any") { + return StyleStateAny() + } + if KssNameEquals(name, "normal") { + return cast(s32)ButtonStateNormal + } + if KssNameEquals(name, "hover") { + return cast(s32)ButtonStateHover + } + if KssNameEquals(name, "pressed") || KssNameEquals(name, "press") { + return cast(s32)ButtonStatePressed + } + if KssNameEquals(name, "focus") || KssNameEquals(name, "focused") { + return cast(s32)ButtonStateFocus + } + if KssNameEquals(name, "disabled") { + return cast(s32)ButtonStateDisabled + } + if KssNameEquals(name, "loading") { + return cast(s32)ButtonStateLoading + } + if KssNameEquals(name, "selected") { + return cast(s32)ButtonStateSelected + } + return -999999 +} + +KssToneValue :: (name: KssName) -> s32 { + if KssNameEquals(name, "any") { + return StyleAny() + } + if KssNameEquals(name, "Neutral") { + return cast(s32)ButtonToneNeutral + } + if KssNameEquals(name, "Accent") { + return cast(s32)ButtonToneAccent + } + if KssNameEquals(name, "Danger") { + return cast(s32)ButtonToneDanger + } + if KssNameEquals(name, "Success") { + return cast(s32)ButtonToneSuccess + } + if KssNameEquals(name, "Warning") { + return cast(s32)ButtonToneWarning + } + return -999999 +} + +KssEmphasisValue :: (name: KssName) -> s32 { + if KssNameEquals(name, "any") { + return StyleAny() + } + if KssNameEquals(name, "Filled") { + return cast(s32)ButtonEmphasisFilled + } + if KssNameEquals(name, "Soft") { + return cast(s32)ButtonEmphasisSoft + } + if KssNameEquals(name, "Outline") { + return cast(s32)ButtonEmphasisOutline + } + if KssNameEquals(name, "Ghost") { + return cast(s32)ButtonEmphasisGhost + } + if KssNameEquals(name, "Link") { + return cast(s32)ButtonEmphasisLink + } + return -999999 +} + +KssSizeValue :: (name: KssName) -> s32 { + if KssNameEquals(name, "any") { + return StyleAny() + } + if KssNameEquals(name, "Medium") { + return cast(s32)ControlSizeMedium + } + if KssNameEquals(name, "Small") { + return cast(s32)ControlSizeSmall + } + if KssNameEquals(name, "Large") { + return cast(s32)ControlSizeLarge + } + return -999999 +} + +KssRoleValue :: (name: KssName) -> s32 { + if KssNameEquals(name, "Any") { + return StyleAny() + } + if KssNameEquals(name, "Bar") || KssNameEquals(name, "MenuBar") { + return 1 + } + if KssNameEquals(name, "Popup") || KssNameEquals(name, "Panel") || + KssNameEquals(name, "MenuPopup") { + return 2 + } + if KssNameEquals(name, "Context") || KssNameEquals(name, "MenuContext") { + return 3 + } + if KssNameEquals(name, "Track") { + return 4 + } + if KssNameEquals(name, "Fill") { + return 5 + } + if KssNameEquals(name, "Label") || KssNameEquals(name, "Text") { + return 6 + } + if KssNameEquals(name, "Line") { + return 7 + } + if KssNameEquals(name, "Bullet") { + return 8 + } + if KssNameEquals(name, "Box") { + return 9 + } + if KssNameEquals(name, "Mark") || KssNameEquals(name, "Check") { + return 10 + } + if KssNameEquals(name, "Ring") { + return 11 + } + if KssNameEquals(name, "Handle") { + return 12 + } + if KssNameEquals(name, "Header") { + return 13 + } + if KssNameEquals(name, "TreeHeader") { + return 14 + } + if KssNameEquals(name, "Close") { + return 15 + } + if KssNameEquals(name, "Title") { + return 16 + } + if KssNameEquals(name, "Action") { + return 17 + } + if KssNameEquals(name, "Divider") { + return 18 + } + if KssNameEquals(name, "Scrim") { + return 19 + } + if KssNameEquals(name, "Message") { + return 20 + } + if KssNameEquals(name, "Row") { + return 21 + } + if KssNameEquals(name, "Cell") { + return 22 + } + if KssNameEquals(name, "Selection") || KssNameEquals(name, "Selected") { + return 23 + } + if KssNameEquals(name, "Anchor") { + return 24 + } + if KssNameEquals(name, "Placeholder") { + return 25 + } + if KssNameEquals(name, "Option") { + return 26 + } + if KssNameEquals(name, "Scrollbar") { + return 27 + } + if KssNameEquals(name, "BottomBar") { + return 28 + } + if KssNameEquals(name, "BottomAction") { + return 29 + } + return -999999 +} diff --git a/src/style_pack_classic.zi b/src/style_pack_classic.zi new file mode 100644 index 0000000..c27fd5b --- /dev/null +++ b/src/style_pack_classic.zi @@ -0,0 +1,2349 @@ +// Generated from styles/classic.kss by tools/style_pack_module.zi. +// Edit the .kss and run `make style-packs`; do not edit this file. +style_pack_classic_bytes: [37511]u8; +style_pack_classic_length: s32 = 0; + +StylePackClassicChunk0 :: #string KSSEND +@pack classic; + +tokens { + color { + canvas: #d4d0c8; + text: #171717; + muted: #666666; + surface: #e6e3dc; + card: #f0eee8; + button: #d4d0c8; + button-hover: #e4e1d9; + button-pressed: #bab7b0; + button-disabled: #d4d0c8; + border: #66645f; + border-soft: #9b9993; + border-hover: #383838; + border-disabled: #aaa8a2; + focus-ring: #242424; + accent: #3b609b; + accent-hover: #4c70a8; + accent-pressed: #2c5088; + accent-border: #254779; + accent-ink: #ffffff; + accent-soft: #d6dfed; + accent-soft-hover: #c9d5e7; + accent-soft-pressed: #b8c8de; + danger: #a44848; + danger-hover: #b25757; + danger-pressed: #913b3b; + danger-ink: #ffffff; + danger-soft: #eddddd; + danger-soft-hover: #e8d1d1; + danger-soft-pressed: #dec0c0; + success: #2c7654; + success-hover: #398561; + success-pressed: #206547; + success-ink: #ffffff; + success-soft: #daeadf; + success-soft-hover: #cce2d3; + success-soft-pressed: #b9d5c3; + warning: #956f10; + warning-hover: #a58020; + warning-pressed: #805e08; + warning-ink: #ffffff; + warning-soft: #f0e7ca; + warning-soft-hover: #eaddb7; + warning-soft-pressed: #dfcea2; + outline-border: #575650; + row: #e2dfd8; + row-hover: #eceae5; + row-pressed: #d3d0c9; + accent-end: #3b609b; + thumb-ink: #ffffff; + transparent: #00000000; + } + length { + radius: 0; + border: 1; + border.none: 0; + gap: 6; + space.1: 5; + space.2: 8; + space.3: 10; + field-x: 7; + font: 14; + icon: 16; + } + material { + default: Flat; + } +} + +@layer base; + +App { + background: canvas; + foreground: text; + material: default; +} + +Surface { + background: surface; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; +KSSEND; + +StylePackClassicChunk1 :: #string KSSEND + padding-x: space.2; + padding-y: space.2; + gap: gap; + material: default; +} + +Popup { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Popup[role=Panel] { + background: card; + foreground: text; + border: border; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +Canvas { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +Card { + background: card; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + padding-x: space.3; + padding-y: space.2; + gap: gap; + material: default; +} + +Image { + font-size: font; + icon-size: icon; + background: #00000000; + foreground: #ffffff; + border: #00000000; + focus: accent; + radius: 0; + border-width: 0; + opacity: 1; + material: Flat; +} + +Image[role=Label] { + foreground: muted; + font-size: font; + opacity: 1; + material: default; +} + +Focus { + foreground: accent; + border: accent-border; + focus: accent; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +Focus:focus { + foreground: accent; + border: accent-border; +} + +Focus[role=Box] { + foreground: muted; + border: outline-border; + border-width: 2; + padding-x: 3; + opacity: 1; + material: default; +} + +Focus[role=Box]:focus { + foreground: accent; + border: accent-border; +} + +Focus[role=Label] { + foreground: muted; + font-size: font; + opacity: 1; + material: default; +} + +Focus[role=Label]:focus { + foreground: accent; +} + +NavigationBar { + background: surface; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + padding-x: space.2; + padding-y: space.2; + gap: gap; +KSSEND; + +StylePackClassicChunk2 :: #string KSSEND + icon-size: 86; + offset-x: 96; + offset-y: 56; + opacity: 1; + material: default; +} + +NavigationBar[role=Panel] { + offset-x: 340; + icon-size: 128; + padding-y: 58; +} + +NavigationBar[role=Row] { + icon-size: 58; + offset-y: 22; + padding-x: 36; + padding-y: 36; +} + +NavigationBar[role=Action] { + icon-size: 34; + offset-x: 180; + offset-y: 16; + padding-x: 92; + padding-y: 36; + gap: 8; +} + +NavigationBar[role=Divider] { + icon-size: 48; + offset-y: 8; + padding-y: 12; + gap: 8; +} + +NavigationBarItem { + background: transparent; + foreground: muted; + border: transparent; + focus: accent; + radius: radius; + border-width: border.none; + padding-x: 3; + padding-y: 2; + gap: 4; + font-size: font; + icon-size: 32; + offset-x: 16; + opacity: 1; + material: default; +} + +NavigationBarItem:hover { + background: button-hover; + foreground: text; +} + +NavigationBarItem:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +NavigationBarItem:disabled { + foreground: muted; + opacity: 0.45; +} + +TabBar { + font-size: font; + background: surface; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + padding-x: 120; + icon-size: 36; + offset-x: 120; + gap: gap; + opacity: 1; + material: default; +} + +Tab { + font-size: font; + background: button; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.2; + padding-y: space.1; + icon-size: 44; + opacity: 1; + material: default; +} + +Tab:hover { + background: button-hover; + foreground: text; + border: border-hover; +} + +Tab:pressed { + background: button-pressed; + foreground: text; +} + +Tab:selected { + background: card; + foreground: accent; + border: accent-border; +} + +Tab:disabled { + background: button-disabled; +KSSEND; + +StylePackClassicChunk3 :: #string KSSEND + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +TabClose { + background: transparent; + foreground: muted; + border: transparent; + radius: radius; + border-width: border.none; + icon-size: 24; + opacity: 1; + material: default; +} + +TabClose:hover { + background: button-hover; + foreground: accent; +} + +SegmentedControl { + font-size: font; + background: surface; + foreground: text; + border: border; + radius: radius; + border-width: border; + padding-x: 2; + padding-y: 2; + icon-size: 30; + offset-x: 72; + offset-y: 180; + gap: 0; + opacity: 1; + material: default; +} + +Segment { + background: button; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.2; + padding-y: 5; + offset-x: 20; + font-size: font; + opacity: 1; + material: default; +} + +Segment:hover { + background: button-hover; + foreground: text; +} + +Segment:pressed { + background: button-pressed; + foreground: text; +} + +Segment:selected { + background: card; + foreground: accent; + border: accent-border; +} + +Segment:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +Menu { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border; + radius: radius; + border-width: border; + padding-x: space.1; + padding-y: space.1; + gap: 4; + opacity: 1; + material: default; +} + +Menu[role=Bar] { + background: surface; + foreground: text; + border: border-soft; + padding-x: 12; + padding-y: 3; + gap: 2; +} + +Menu[role=Popup] { + background: card; + foreground: text; + border: outline-border; + padding-x: 12; + gap: 4; + offset-x: 180; +} + +Menu[role=Context] { + background: card; + foreground: text; + border: outline-border; +} + +MenuItem { + icon-size: icon; +KSSEND; + +StylePackClassicChunk4 :: #string KSSEND + background: transparent; + foreground: text; + border: transparent; + focus: accent; + radius: radius; + border-width: border.none; + padding-x: space.2; + padding-y: 8; + offset-x: 88; + offset-y: 30; + font-size: font; + opacity: 1; + material: default; +} + +MenuItem:hover { + background: button-hover; + foreground: text; + border: border-hover; +} + +MenuItem:pressed { + background: button-pressed; + foreground: text; +} + +MenuItem:selected { + background: card; + foreground: accent; + border: accent-border; +} + +MenuItem:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +MenuSeparator { + background: transparent; + foreground: border; + border: border; + radius: 0; + border-width: border; + padding-x: 12; + gap: 4; + icon-size: 32; + offset-x: 60; + opacity: 1; + material: default; +} + +Spinbox { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +SpinboxValue { + background: card; + foreground: text; + border: border-soft; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.2; + font-size: font; + opacity: 1; + material: default; +} + +Drag { + icon-size: icon; + background: surface; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + font-size: font; + padding-x: 6; + padding-y: 4; + opacity: 1; + material: default; +} + +DragValue { + icon-size: icon; + background: card; + foreground: text; + border: border-soft; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.2; + font-size: font; + opacity: 1; + material: default; +} + +DragValue:disabled { + background: button-disabled; +KSSEND; + +StylePackClassicChunk5 :: #string KSSEND + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +ColorPicker { + font-size: font; + opacity: 1; + material: default; + gap: space.1; + icon-size: 36; + padding-y: 2; + offset-x: 20; + offset-y: 28; +} + +ColorPickerSwatch { + background: transparent; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.1; + font-size: font; + opacity: 1; + material: default; +} + +ListBox { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.1; + padding-y: space.1; + offset-y: 8; + gap: space.1; + opacity: 1; + material: default; +} + +ListBoxItem { + background: transparent; + foreground: text; + border: transparent; + focus: accent; + radius: radius; + border-width: border.none; + padding-x: space.2; + padding-y: space.1; + icon-size: 30; + offset-y: 30; + font-size: font; + opacity: 1; + material: default; +} + +ListBoxItem:hover { + background: button-hover; + foreground: text; + border: border-hover; +} + +ListBoxItem:selected { + background: card; + foreground: accent; + border: accent-border; +} + +ListBoxItem:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +TreeView { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.1; + padding-y: space.1; + gap: space.1; + opacity: 1; + material: default; +} + +TreeViewItem { + background: transparent; + foreground: text; + border: transparent; + focus: accent; + radius: radius; + border-width: border.none; + padding-x: space.2; + padding-y: 7; + gap: 2; + icon-size: 16; +KSSEND; + +StylePackClassicChunk6 :: #string KSSEND + offset-x: 18; + offset-y: 28; + font-size: font; + opacity: 1; + material: default; +} + +TreeViewItem:hover { + background: button-hover; + foreground: text; + border: border-hover; +} + +TreeViewItem:selected { + background: card; + foreground: accent; + border: accent-border; +} + +TreeViewItem:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +ListBoxMulti { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.1; + padding-y: space.1; + gap: space.1; + opacity: 1; + material: default; +} + +ListBoxMultiItem { + background: transparent; + foreground: text; + border: transparent; + focus: accent; + radius: radius; + border-width: border.none; + padding-x: space.2; + padding-y: space.1; + icon-size: 28; + offset-y: 28; + font-size: font; + opacity: 1; + material: default; +} + +ListBoxMultiItem:hover { + background: button-hover; + foreground: text; + border: border-hover; +} + +ListBoxMultiItem:selected { + background: card; + foreground: accent; + border: accent-border; +} + +ListBoxMultiItem:focus { + border: accent; +} + +ListBoxMultiItem:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +DragDropTarget { + background: transparent; + border: border-hover; + focus: accent; + radius: radius; + border-width: border; + material: default; +} + +DragDropTarget:hover { + border: accent-border; + border-width: 2; +} + +Reorder { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Reorder[role=Handle] { + background: transparent; + foreground: muted; + border: transparent; + border-width: border.none; +KSSEND; + +StylePackClassicChunk7 :: #string KSSEND + padding-x: 8; + gap: 4; + icon-size: 3; + offset-x: 36; + offset-y: 5; + opacity: 1; + material: default; +} + +Reorder[role=Handle]:selected { + foreground: accent; +} + +Reorder[role=Placeholder] { + background: accent; + foreground: accent; + border: accent-border; + radius: radius; + border-width: 2; + padding-x: 3; + icon-size: 32; + offset-x: 34; + offset-y: 12; + opacity: 0.10; + material: default; +} + +Selectable { + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + focus: accent; + radius: radius; + border-width: border.none; + padding-x: space.2; + padding-y: space.1; + font-size: font; + opacity: 1; + material: default; +} + +Selectable:hover { + background: button-hover; + foreground: text; +} + +Selectable:pressed { + background: button-pressed; + foreground: text; +} + +Selectable:selected { + background: accent; + foreground: accent-ink; +} + +Selectable:disabled { + foreground: muted; + opacity: 0.45; +} + +Fieldset { + icon-size: icon; + background: canvas; + foreground: muted; + border: border; + radius: radius; + border-width: border; + padding-x: 6; + padding-y: 6; + gap: 7; + font-size: font; + opacity: 1; + material: default; +} + +PanedView { + background: transparent; + foreground: muted; + border: transparent; + opacity: 1; + material: default; +} + +PanedView[role=Handle] { + background: border; + foreground: muted; + border: transparent; + radius: 0; + border-width: border.none; + icon-size: 8; + opacity: 1; + material: default; +} + +PanedView[role=Handle]:hover { + background: border-hover; +} + +PanedView[role=Handle]:pressed { + background: accent; +} + +Toast { + background: surface; + foreground: text; + border: border; + radius: radius; + border-width: border; + padding-x: 14; + padding-y: 10; + gap: 18; + opacity: 1; +KSSEND; + +StylePackClassicChunk8 :: #string KSSEND + material: default; +} + +Toast[role=Label] { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Collapsible { + font-size: font; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Collapsible[role=Header] { + background: button; + foreground: text; + border: border; + radius: radius; + border-width: border; + font-size: font; + icon-size: 20; + padding-x: 8; + padding-y: 8; + opacity: 1; + material: default; +} + +Collapsible[role=Header]:hover { + background: button-hover; +} + +Collapsible[role=Header]:pressed { + background: button-pressed; +} + +Collapsible[role=Header]:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Collapsible[role=Header]:disabled { + foreground: muted; + opacity: 0.45; +} + +Collapsible[role=TreeHeader] { + background: transparent; + foreground: text; + border: transparent; + radius: 0; + border-width: border.none; + font-size: font; + icon-size: 20; + padding-x: 20; + padding-y: 8; + opacity: 1; + material: default; +} + +Collapsible[role=TreeHeader]:hover { + background: button-hover; +} + +Collapsible[role=TreeHeader]:selected { + background: transparent; + foreground: accent; +} + +Collapsible[role=TreeHeader]:disabled { + foreground: muted; + opacity: 0.45; +} + +Collapsible[role=Close] { + background: transparent; + foreground: muted; + border: transparent; + font-size: font; + icon-size: 28; + opacity: 1; + material: default; +} + +Collapsible[role=Close]:hover { + foreground: text; +} + +Collapsible[role=Close]:disabled { + opacity: 0.45; +} + +TitleBar { + font-size: font; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +KSSEND; + +StylePackClassicChunk9 :: #string KSSEND +TitleBar[role=Bar] { + background: surface; + foreground: text; + border: border; + radius: 0; + border-width: border; + padding-x: 12; + gap: 4; + icon-size: 32; + offset-x: 60; + opacity: 1; + material: default; +} + +TitleBar[role=Title] { + background: transparent; + foreground: text; + border: transparent; + font-size: font; + icon-size: 48; + padding-x: 16; + opacity: 1; + material: default; +} + +TitleBar[role=Action] { + background: transparent; + foreground: text; + border: transparent; + radius: radius; + border-width: border.none; + icon-size: 20; + padding-x: 10; + opacity: 1; + material: default; +} + +TitleBar[role=Action]:hover { + background: button-hover; +} + +TitleBar[role=Action]:pressed { + background: button-pressed; +} + +Toolbar { + font-size: font; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Toolbar[role=Bar] { + background: surface; + foreground: text; + border: border; + radius: 0; + border-width: border; + padding-x: 12; + opacity: 1; + material: default; +} + +Toolbar[role=Divider] { + background: transparent; + foreground: muted; + border: border; + border-width: border; + opacity: 1; + material: default; +} + +Toolbar[role=Action] { + background: transparent; + foreground: text; + border: transparent; + radius: radius; + border-width: border.none; + padding-x: 8; + gap: 6; + icon-size: 20; + opacity: 1; + material: default; +} + +Toolbar[role=Action]:hover { + background: button-hover; +} + +Toolbar[role=Action]:pressed { + background: button-pressed; +} + +Toolbar[role=BottomBar] { + padding-x: 24; + padding-y: 6; + gap: 8; + offset-x: 120; +} + +Toolbar[role=BottomAction] { + padding-x: 10; + gap: 12; + icon-size: 24; + offset-x: 16; + offset-y: 6; +} + +Modal { + font-size: font; +KSSEND; + +StylePackClassicChunk10 :: #string KSSEND + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Modal[role=Scrim] { + background: #000000; + foreground: text; + border: transparent; + opacity: 0.32; + material: default; +} + +Modal[role=Panel] { + background: card; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: 18; + padding-y: 18; + gap: 24; + icon-size: 280; + offset-x: 420; + offset-y: 58; + opacity: 1; + material: default; +} + +Modal[role=Title] { + foreground: text; + font-size: font; + padding-x: 24; + padding-y: 18; + icon-size: 48; + offset-y: 14; + opacity: 1; + material: default; +} + +Modal[role=Message] { + foreground: text; + font-size: font; + padding-x: 120; + padding-y: 160; + gap: 18; + icon-size: 38; + offset-y: 18; + opacity: 1; + material: default; +} + +Modal[role=Action] { + background: button; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: 24; + gap: 8; + icon-size: 44; + offset-x: 88; + offset-y: 150; + opacity: 1; + material: default; +} + +Modal[role=Action]:hover { + background: button-hover; + border: border-hover; +} + +Modal[role=Action]:pressed { + background: button-pressed; +} + +Modal[role=Action][tone=Accent] { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Modal[role=Close] { + background: transparent; + foreground: text; + border: transparent; + radius: radius; + border-width: border.none; + padding-x: 8; + padding-y: 16; + gap: 6; + icon-size: 20; + offset-x: 120; + offset-y: 96; + opacity: 1; + material: default; +} + +Modal[role=Close]:hover { + background: button-hover; + border: border; +} + +Modal[role=Close]:pressed { + background: button-pressed; +} + +Guide { +KSSEND; + +StylePackClassicChunk11 :: #string KSSEND + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Guide[role=Scrim] { + background: #000000; + foreground: text; + border: transparent; + opacity: 0.22; + material: default; +} + +Guide[role=Bar] { + background: card; + foreground: text; + border: border; + radius: 0; + border-width: border.none; + padding-x: 12; + gap: 12; + icon-size: 48; + offset-x: 48; + opacity: 1; + material: default; +} + +Guide[role=Divider] { + background: transparent; + foreground: text; + border: border; + border-width: border; + opacity: 1; + material: default; +} + +Guide[role=Panel] { + background: card; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: 12; + padding-y: 12; + gap: 20; + offset-x: 300; + offset-y: 112; + opacity: 1; + material: default; +} + +Guide[role=Anchor] { + background: transparent; + foreground: accent; + border: accent-border; + border-width: border; + padding-x: 4; + opacity: 1; + material: default; +} + +Guide[role=Label] { + foreground: text; + font-size: font; + padding-x: 8; + padding-y: 6; + gap: 8; + opacity: 1; + material: default; +} + +Guide[role=Action] { + background: button; + foreground: text; + border: border; + radius: radius; + border-width: border; + padding-x: 7; + gap: 8; + icon-size: 19; + offset-y: 34; + opacity: 1; + material: default; +} + +Guide[role=Action]:hover { + background: button-hover; + border: border-hover; +} + +Guide[role=Close] { + background: transparent; + foreground: text; + border: transparent; + radius: radius; + border-width: border.none; + padding-x: 6; + gap: 12; + icon-size: 16; + offset-y: 28; + opacity: 1; + material: default; +} + +Guide[role=Close]:hover { + background: button-hover; + border: border; +} + +TableView { +KSSEND; + +StylePackClassicChunk12 :: #string KSSEND + font-size: font; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + offset-y: 28; + opacity: 1; + material: default; +} + +TableView[role=Panel] { + background: card; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +TableView[role=Header] { + background: surface; + foreground: text; + border: border; + radius: radius; + border-width: border; + padding-x: 6; + offset-y: 30; + font-size: font; + opacity: 1; + material: default; +} + +TableView[role=Header]:hover { + background: button-hover; + border: border-hover; +} + +TableView[role=Header]:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +TableView[role=Row] { + background: row; + foreground: text; + border: transparent; + radius: 0; + border-width: border.none; + opacity: 1; + material: default; +} + +TableView[role=Cell] { + background: transparent; + foreground: text; + border: transparent; + offset-x: 32; + font-size: font; + opacity: 1; + material: default; +} + +TableView[role=Selection] { + background: accent; + foreground: accent-ink; + border: accent-border; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +TableView[role=Divider] { + background: transparent; + foreground: muted; + border: border; + border-width: border; + padding-x: 5; + offset-y: 8; + opacity: 1; + material: default; +} + +Plot { + icon-size: icon; + background: card; + foreground: muted; + border: border; + radius: radius; + border-width: border; + padding-x: 6; + padding-y: 4; + font-size: font; + opacity: 1; + material: default; +} + +PlotMark { + background: accent; + foreground: accent-ink; + border: accent-border; + opacity: 1; + material: default; +} + +Text { +KSSEND; + +StylePackClassicChunk13 :: #string KSSEND + foreground: text; + font-size: font; +} + +Page { + gap: 0; + padding-x: 0; +} + +Section { + gap: 0; + padding-x: 0; +} + +Heading { + foreground: text; + font-size: 24; +} + +ParagraphText { + foreground: text; + font-size: font; + gap: 4; +} + +Link { + icon-size: icon; + foreground: accent; + font-size: font; + opacity: 1; + material: default; +} + +Link:hover { + foreground: accent-border; +} + +Link:disabled { + foreground: muted; + opacity: 0.45; +} + +Separator { + opacity: 1; + material: default; + background: border-soft; + foreground: muted; + gap: gap; +} + +Separator[role=Line] { + background: border-soft; + foreground: muted; + gap: gap; +} + +Separator[role=Label] { + background: border-soft; + foreground: muted; + font-size: font; + gap: gap; +} + +Separator[role=Bullet] { + background: border-soft; + foreground: muted; + icon-size: 6; + gap: gap; +} + +Progress { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +Progress[role=Track] { + background: surface; + foreground: text; + border: border; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +Progress[role=Fill] { + background: accent; + foreground: accent-ink; + border: accent-border; + opacity: 1; + material: default; +} + +Progress[role=Label] { + background: transparent; + foreground: text; + border: transparent; + padding-x: 4; + font-size: font; + opacity: 1; + material: default; +} + +@layer components; + +Button { + background: button; + background-end: button; + foreground: text; + border: border; + focus: focus-ring; + radius: radius; + border-width: border; + padding-x: space.3; + padding-y: space.1; + gap: gap; + font-size: font; + icon-size: icon; + opacity: 1; +KSSEND; + +StylePackClassicChunk14 :: #string KSSEND + material: default; +} + +Button:hover { + background: button-hover; + background-end: button-hover; + border: border-hover; +} + +Button:pressed { + background: button-pressed; + background-end: button-pressed; +} + +Button:selected { + border: focus-ring; + border-width: 2; +} + +Button:focus { + border: focus-ring; +} + +Button:disabled { + foreground: muted; + background: button-disabled; + background-end: button-disabled; + border: border-disabled; + opacity: 1; +} + +Button:loading { + foreground: accent; +} + +Button[tone=Accent] { + background: accent; + background-end: accent; + foreground: accent-ink; + border: accent-border; +} + +Button[tone=Accent]:loading { + foreground: accent; +} + +Button[tone=Danger] { + background: danger; + background-end: danger; + foreground: danger-ink; + border: danger; +} + +Button[tone=Success] { + background: success; + background-end: success; + foreground: success-ink; + border: success; +} + +Button[tone=Warning] { + background: warning; + background-end: warning; + foreground: warning-ink; + border: warning; +} + +Button[emphasis=Soft] { + background: row; + background-end: row; + foreground: text; + border: border-soft; +} + +Button[emphasis=Soft]:hover { + background: row-hover; + background-end: row-hover; +} + +Button[emphasis=Soft]:pressed { + background: row-pressed; + background-end: row-pressed; +} + +Button[tone=Accent][emphasis=Soft] { + background: accent-soft; + background-end: accent-soft; + foreground: accent; + border: accent-soft; +} + +Button[tone=Danger][emphasis=Soft] { + background: danger-soft; + background-end: danger-soft; + foreground: danger; + border: danger-soft; +} + +Button[tone=Success][emphasis=Soft] { + background: success-soft; + background-end: success-soft; + foreground: success; + border: success-soft; +} + +KSSEND; + +StylePackClassicChunk15 :: #string KSSEND +Button[tone=Warning][emphasis=Soft] { + background: warning-soft; + background-end: warning-soft; + foreground: warning; + border: warning-soft; +} + +Button[emphasis=Outline] { + background: canvas; + background-end: canvas; + foreground: text; + border: outline-border; +} + +Button[emphasis=Outline]:hover { + background: button-hover; + background-end: button-hover; +} + +Button[emphasis=Outline]:pressed { + background: button-pressed; + background-end: button-pressed; +} + +Button[emphasis=Ghost] { + background: transparent; + background-end: transparent; + border: transparent; + border-width: border.none; +} + +Button[tone=Accent][emphasis=Outline] { foreground: accent; } +Button[tone=Danger][emphasis=Outline] { foreground: danger; } +Button[tone=Success][emphasis=Outline] { foreground: success; } +Button[tone=Warning][emphasis=Outline] { foreground: warning; } + +Button[tone=Accent][emphasis=Ghost] { foreground: accent; } +Button[tone=Danger][emphasis=Ghost] { foreground: danger; } +Button[tone=Success][emphasis=Ghost] { foreground: success; } +Button[tone=Warning][emphasis=Ghost] { foreground: warning; } + +Button[emphasis=Ghost]:hover { + background: row-hover; + background-end: row-hover; +} +Button[emphasis=Ghost]:pressed { + background: button-pressed; + background-end: button-pressed; +} + +Button[tone=Accent][emphasis=Filled]:loading { + foreground: accent-ink; +} + +Button[emphasis=Link] { + background: transparent; + background-end: transparent; + foreground: accent; + border: transparent; + border-width: border.none; +} + +Button[tone=Danger][emphasis=Link] { foreground: danger; } +Button[tone=Success][emphasis=Link] { foreground: success; } +Button[tone=Warning][emphasis=Link] { foreground: warning; } + +Button[emphasis=Link]:hover { foreground: accent-border; } + +KSSEND; + +StylePackClassicChunk16 :: #string KSSEND +Button[tone=Accent][emphasis=Filled]:hover { + background: accent-hover; + background-end: accent-hover; +} +Button[tone=Accent][emphasis=Filled]:pressed { + background: accent-pressed; + background-end: accent-pressed; +} +Button[tone=Danger][emphasis=Filled]:hover { + background: danger-hover; + background-end: danger-hover; +} +Button[tone=Danger][emphasis=Filled]:pressed { + background: danger-pressed; + background-end: danger-pressed; +} +Button[tone=Success][emphasis=Filled]:hover { + background: success-hover; + background-end: success-hover; +} +Button[tone=Success][emphasis=Filled]:pressed { + background: success-pressed; + background-end: success-pressed; +} +Button[tone=Warning][emphasis=Filled]:hover { + background: warning-hover; + background-end: warning-hover; +} +Button[tone=Warning][emphasis=Filled]:pressed { + background: warning-pressed; + background-end: warning-pressed; +} + +Button[tone=Accent][emphasis=Soft]:hover { + background: accent-soft-hover; + background-end: accent-soft-hover; +} +Button[tone=Accent][emphasis=Soft]:pressed { + background: accent-soft-pressed; + background-end: accent-soft-pressed; +} +Button[tone=Danger][emphasis=Soft]:hover { + background: danger-soft-hover; + background-end: danger-soft-hover; +} +Button[tone=Danger][emphasis=Soft]:pressed { + background: danger-soft-pressed; + background-end: danger-soft-pressed; +} +Button[tone=Success][emphasis=Soft]:hover { + background: success-soft-hover; + background-end: success-soft-hover; +} +Button[tone=Success][emphasis=Soft]:pressed { + background: success-soft-pressed; + background-end: success-soft-pressed; +} +Button[tone=Warning][emphasis=Soft]:hover { + background: warning-soft-hover; + background-end: warning-soft-hover; +} +Button[tone=Warning][emphasis=Soft]:pressed { +KSSEND; + +StylePackClassicChunk17 :: #string KSSEND + background: warning-soft-pressed; + background-end: warning-soft-pressed; +} + +Button[tone=Accent][emphasis=Link]:hover { foreground: accent-hover; } +Button[tone=Danger][emphasis=Link]:hover { foreground: danger-hover; } +Button[tone=Success][emphasis=Link]:hover { foreground: success-hover; } +Button[tone=Warning][emphasis=Link]:hover { foreground: warning-hover; } + +Dropdown { + icon-size: icon; + opacity: 1; + background: card; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.2; + padding-y: space.1; + gap: gap; + offset-y: 16; + font-size: font; + material: default; + offset-x: 24; +} + +Dropdown:hover { + background: button-hover; + border: border-hover; +} + +Dropdown:pressed { + background: button-pressed; +} + +Dropdown:focus { + border: accent; +} + +Dropdown:disabled { + foreground: muted; + background: button-disabled; + border: border-disabled; + opacity: 0.7; +} + +Dropdown[tone=Accent] { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Dropdown:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Dropdown[role=Panel] { + background: card; + border: border; + padding-y: 4; + gap: 4; + offset-y: 16; +} + +Dropdown[role=Option] { + padding-x: 4; + padding-y: 2; +} + +Dropdown[role=Scrollbar] { + icon-size: 8; + padding-y: 2; + gap: 2; + offset-x: 8; +} + +Dropdown[role=Option]:hover { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Dropdown[role=Option]:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Slider { + font-size: font; + icon-size: icon; + background: button-pressed; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + opacity: 1; +KSSEND; + +StylePackClassicChunk18 :: #string KSSEND + material: default; +} + +Slider[role=Track] { + padding-x: 28; + padding-y: 4; + gap: 10; + icon-size: 6; + offset-x: 28; + offset-y: 24; + opacity: 1; +} + +Slider[role=Label] { + foreground: text; + font-size: font; +} + +Slider:hover { + background: button-hover; + border: border-hover; +} + +Slider[role=Fill] { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Slider:disabled { + foreground: muted; + background: button-disabled; + border: border-disabled; + opacity: 0.7; +} + +SliderThumb { + background: accent; + foreground: thumb-ink; + border: accent-border; + focus: accent; + radius: radius; + border-width: border; + gap: 6; + icon-size: 18; + opacity: 1; + material: default; +} + +SliderThumb:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + focus: border-disabled; + opacity: 0.7; +} + +Scroll { + background: button-pressed; + foreground: text; + border: border-soft; + focus: accent; + radius: radius; + border-width: border; + icon-size: 10; + padding-x: 16; + padding-y: 8; + gap: 20; + offset-x: 5; + offset-y: 42; + opacity: 1; + material: default; +} + +ScrollThumb { + background: button; + foreground: text; + border: outline-border; + focus: accent; + radius: radius; + border-width: border; + icon-size: 16; + padding-x: 2; + opacity: 1; + material: default; +} + +ScrollThumb:hover { + background: button-hover; + border: border-hover; +} + +ScrollThumb:pressed { + background: button-pressed; + border: accent-border; +} + +Toggle { + font-size: font; + icon-size: icon; + background: button; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +Toggle[role=Track] { + padding-x: 46; + padding-y: 24; + gap: 2; + radius: 999; + opacity: 1; +} + +KSSEND; + +StylePackClassicChunk19 :: #string KSSEND +Toggle[role=Label] { + foreground: text; + padding-x: 10; + font-size: font; + opacity: 1; +} + +Toggle:hover { + background: button-hover; + border: border-hover; +} + +Toggle:pressed { + background: button-pressed; +} + +Toggle[role=Fill] { + background: accent; + foreground: accent-ink; + border: accent-border; + radius: 999; + gap: 2; +} + +Toggle:disabled { + foreground: muted; + background: button-disabled; + border: border-disabled; + opacity: 0.7; +} + +ToggleThumb { + background: card; + foreground: thumb-ink; + border: border-hover; + focus: accent; + radius: radius; + border-width: border; + gap: 4; + icon-size: 16; + opacity: 1; + material: default; +} + +ToggleThumb[tone=Accent] { + background: thumb-ink; + foreground: thumb-ink; + border: accent-border; + focus: accent; + icon-size: 18; +} + +ToggleThumb:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + focus: border-disabled; + opacity: 0.7; +} + +Checkbox { + font-size: font; + background: card; + foreground: text; + border: border; + focus: accent; + border-width: border; + padding-x: 22; + padding-y: 3; + gap: 4; + icon-size: 20; + opacity: 1; + material: default; +} + +Checkbox:hover { + background: button-hover; + border: border-hover; +} + +Checkbox:pressed { + background: button-pressed; +} + +Checkbox:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Checkbox:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.7; +} + +Checkbox[role=Box] { + background: card; + foreground: text; + border: border; + focus: accent; + border-width: border; + padding-x: 22; + padding-y: 3; + gap: 4; + icon-size: 20; + opacity: 1; + material: default; +} + +Checkbox[role=Mark] { + background: accent; + foreground: accent-ink; +KSSEND; + +StylePackClassicChunk20 :: #string KSSEND + border: accent-border; + focus: accent; + border-width: border; + padding-x: 5; + icon-size: 2.4; + opacity: 1; + material: default; +} + +Checkbox[role=Label] { + foreground: text; + gap: 10; + font-size: font; + opacity: 1; + material: default; +} + +Checkbox[role=Label]:disabled { + foreground: muted; + opacity: 0.7; +} + +Radio { + font-size: font; + icon-size: icon; + background: card; + foreground: text; + border: border; + focus: accent; + border-width: border; + opacity: 1; + material: default; +} + +Radio:hover { + border: border-hover; +} + +Radio:selected { + background: accent; + foreground: text; + border: accent-border; +} + +Radio:disabled { + foreground: muted; + border: border-disabled; + opacity: 0.7; +} + +Radio[role=Ring] { + background: card; + foreground: text; + border: border; + focus: accent; + border-width: border; + icon-size: 20; + padding-x: 20; + gap: gap; + opacity: 1; + material: default; +} + +Radio[role=Mark] { + background: accent; + foreground: text; + border: accent-border; + focus: accent; + border-width: border; + font-size: font; + opacity: 1; + material: default; +} + +Radio[role=Label] { + foreground: text; + font-size: font; + opacity: 1; + material: default; +} + +Radio[role=Label]:disabled { + foreground: muted; + opacity: 0.7; +} + +TextField { + opacity: 1; + background: card; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: field-x; + padding-y: space.1; + font-size: font; + material: default; +} + +TextArea { + opacity: 1; + background: card; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: field-x; + padding-y: gap; + font-size: font; + gap: space.1; + material: default; +} +KSSEND; + +StylePackClassicAppend :: (chunk: string) { + index: s32 = 0 + while index < cast(s32)chunk.count { + style_pack_classic_bytes[style_pack_classic_length + index] = chunk[index] + index += 1 + } + style_pack_classic_length += cast(s32)chunk.count +} + +// The classic style pack's KSS source. +#program_export +StylePackClassic :: () -> string { + if style_pack_classic_length == 0 { + StylePackClassicAppend(StylePackClassicChunk0) + StylePackClassicAppend(StylePackClassicChunk1) + StylePackClassicAppend(StylePackClassicChunk2) + StylePackClassicAppend(StylePackClassicChunk3) + StylePackClassicAppend(StylePackClassicChunk4) + StylePackClassicAppend(StylePackClassicChunk5) + StylePackClassicAppend(StylePackClassicChunk6) + StylePackClassicAppend(StylePackClassicChunk7) + StylePackClassicAppend(StylePackClassicChunk8) + StylePackClassicAppend(StylePackClassicChunk9) + StylePackClassicAppend(StylePackClassicChunk10) + StylePackClassicAppend(StylePackClassicChunk11) + StylePackClassicAppend(StylePackClassicChunk12) + StylePackClassicAppend(StylePackClassicChunk13) + StylePackClassicAppend(StylePackClassicChunk14) + StylePackClassicAppend(StylePackClassicChunk15) + StylePackClassicAppend(StylePackClassicChunk16) + StylePackClassicAppend(StylePackClassicChunk17) + StylePackClassicAppend(StylePackClassicChunk18) + StylePackClassicAppend(StylePackClassicChunk19) + StylePackClassicAppend(StylePackClassicChunk20) + } + return TextView(style_pack_classic_bytes[0:style_pack_classic_length]) +} diff --git a/src/style_parse.zi b/src/style_parse.zi new file mode 100644 index 0000000..4986b48 --- /dev/null +++ b/src/style_parse.zi @@ -0,0 +1,97 @@ +#import "kss_parser" +#import "kryon/style_sheet" +#import "kryon/drawing_props" + +using KssStatus; +using KssTokenKind; + +// The parser and its rule table are owned values. An application may pause at +// NeedImport, provide the requested source, then keep parsing the same table. +StyleRulesParse :: struct { + parser: KssParser + rules: StyleRules + overflow: bool +} + +#program_export +BeginStyleRules :: (source: string, path: string, + environment: KssEnvironment) -> StyleRulesParse { + result: StyleRulesParse + result.parser = KssBegin(source, path, environment) + return result +} + +#program_export +StepStyleRules :: (result: StyleRulesParse) -> StyleRulesParse { + if result.overflow { return result } + result.parser = KssStep(result.parser) + if result.parser.status == cast(s32)KssStatusRule { + if result.rules.count < 0 || result.rules.count >= 320 { + result.overflow = true + } else { + result.rules.items[result.rules.count] = result.parser.rule + result.rules.count += 1 + } + } + return result +} + +#program_export +ParseStyleRules :: (result: StyleRulesParse) -> StyleRulesParse { + while !result.overflow && + result.parser.status != cast(s32)KssStatusNeedImport && + result.parser.status != cast(s32)KssStatusDone && + result.parser.status != cast(s32)KssStatusError { + result = StepStyleRules(result) + } + return result +} + +#program_export +ProvideStyleRulesImport :: (result: StyleRulesParse, + source: string, name: string) -> StyleRulesParse { + if !result.overflow && + result.parser.status == cast(s32)KssStatusNeedImport { + result.parser = KssProvideImport(result.parser, source, name) + } + return result +} + +#program_export +FailStyleRulesImport :: (result: StyleRulesParse) -> StyleRulesParse { + if !result.overflow && + result.parser.status == cast(s32)KssStatusNeedImport { + result.parser = KssFailImport(result.parser) + } + return result +} + +// Commit rules and their resolved color tokens together. The parser has +// already applied imports, theme overlays, and caller overrides to its table. +#program_export +InstallParsedStyleRules :: (result: StyleRulesParse) -> bool { + if result.overflow || result.parser.status != cast(s32)KssStatusDone { + return false + } + index: s32 = 0 + while index < result.parser.token_count { + token: KssToken = result.parser.tokens[index] + if token.kind == cast(s32)KssTokenColor && + (token.name.length <= 0 || token.name.length > 64) { + return false + } + index += 1 + } + ClearStyleTokenColors() + index = 0 + while index < result.parser.token_count { + token: KssToken = result.parser.tokens[index] + if token.kind == cast(s32)KssTokenColor && + !SetStyleTokenColorBytes( + token.name.bytes[0:token.name.length], token.color) { + return false + } + index += 1 + } + return InstallStyleRules(result.rules) +} diff --git a/src/system_style.zi b/src/system_style.zi new file mode 100644 index 0000000..2f13c1a --- /dev/null +++ b/src/system_style.zi @@ -0,0 +1,108 @@ +// Installs the classic style pack in the desktop's colors, so an application +// looks like the system theme (light or dark) without carrying its own. +#import "kryon/drawing_props" +#import "kss_parser" +#import "style_pack_classic" +#import "style_parse" +#import "kryon/SystemTheme" +#import "kryon/Theme" + +using KssStatus; + +// A color between two others; 0 is first and 1 is second. +StyleBlend :: (first: u32, second: u32, amount: float32) -> u32 { + return CssMix(first, second, amount) +} + +// The pack's color tokens, derived from the palette's few roles. +SystemStyleOverrides :: (parser: KssParser, palette: SystemPalette) -> KssParser { + background: u32 = palette.background + text: u32 = palette.text + parser = KssAddColorOverride(parser, "canvas", background) + parser = KssAddColorOverride(parser, "text", text) + parser = KssAddColorOverride(parser, "muted", StyleBlend(text, background, 0.4)) + parser = KssAddColorOverride(parser, "surface", StyleBlend(background, text, 0.05)) + parser = KssAddColorOverride(parser, "card", palette.base) + parser = KssAddColorOverride(parser, "button", StyleBlend(background, text, 0.10)) + parser = KssAddColorOverride(parser, "button-hover", StyleBlend(background, text, 0.16)) + parser = KssAddColorOverride(parser, "button-pressed", StyleBlend(background, text, 0.04)) + parser = KssAddColorOverride(parser, "button-disabled", background) + parser = KssAddColorOverride(parser, "border", StyleBlend(background, text, 0.30)) + parser = KssAddColorOverride(parser, "border-soft", StyleBlend(background, text, 0.16)) + parser = KssAddColorOverride(parser, "border-hover", StyleBlend(background, text, 0.55)) + parser = KssAddColorOverride(parser, "border-disabled", StyleBlend(background, text, 0.12)) + parser = KssAddColorOverride(parser, "focus-ring", palette.selected) + parser = KssAddColorOverride(parser, "outline-border", StyleBlend(background, text, 0.5)) + parser = KssAddColorOverride(parser, "row", StyleBlend(background, text, 0.03)) + parser = KssAddColorOverride(parser, "row-hover", StyleBlend(background, text, 0.07)) + parser = KssAddColorOverride(parser, "row-pressed", StyleBlend(background, text, 0.10)) + parser = KssAddColorOverride(parser, "thumb-ink", cast(u32)0xFFFFFFFF) + selected: u32 = palette.selected + parser = KssAddColorOverride(parser, "accent", selected) + parser = KssAddColorOverride(parser, "accent-end", selected) + parser = KssAddColorOverride(parser, "accent-hover", StyleBlend(selected, text, 0.15)) + parser = KssAddColorOverride(parser, "accent-pressed", StyleBlend(selected, background, 0.2)) + parser = KssAddColorOverride(parser, "accent-border", StyleBlend(selected, background, 0.35)) + parser = KssAddColorOverride(parser, "accent-ink", palette.selected_text) + parser = KssAddColorOverride(parser, "accent-soft", StyleBlend(background, selected, 0.22)) + parser = KssAddColorOverride(parser, "accent-soft-hover", StyleBlend(background, selected, 0.30)) + parser = KssAddColorOverride(parser, "accent-soft-pressed", StyleBlend(background, selected, 0.38)) + parser = SystemStyleStatus(parser, "danger", palette.danger, background, text) + parser = SystemStyleStatus(parser, "success", palette.success, background, text) + parser = SystemStyleStatus(parser, "warning", palette.warning, background, text) + return parser +} + +// One status color and the variants the pack derives from it. +SystemStyleStatus :: (parser: KssParser, name: string, color: u32, background: u32, + text: u32) -> KssParser { + if name == "danger" { + parser = KssAddColorOverride(parser, "danger", color) + parser = KssAddColorOverride(parser, "danger-hover", StyleBlend(color, text, 0.15)) + parser = KssAddColorOverride(parser, "danger-pressed", StyleBlend(color, background, 0.2)) + parser = KssAddColorOverride(parser, "danger-ink", OnColor(color)) + parser = KssAddColorOverride(parser, "danger-soft", StyleBlend(background, color, 0.2)) + parser = KssAddColorOverride(parser, "danger-soft-hover", StyleBlend(background, color, 0.28)) + parser = KssAddColorOverride(parser, "danger-soft-pressed", StyleBlend(background, color, 0.36)) + } else if name == "success" { + parser = KssAddColorOverride(parser, "success", color) + parser = KssAddColorOverride(parser, "success-hover", StyleBlend(color, text, 0.15)) + parser = KssAddColorOverride(parser, "success-pressed", StyleBlend(color, background, 0.2)) + parser = KssAddColorOverride(parser, "success-ink", OnColor(color)) + parser = KssAddColorOverride(parser, "success-soft", StyleBlend(background, color, 0.2)) + parser = KssAddColorOverride(parser, "success-soft-hover", StyleBlend(background, color, 0.28)) + parser = KssAddColorOverride(parser, "success-soft-pressed", StyleBlend(background, color, 0.36)) + } else { + parser = KssAddColorOverride(parser, "warning", color) + parser = KssAddColorOverride(parser, "warning-hover", StyleBlend(color, text, 0.15)) + parser = KssAddColorOverride(parser, "warning-pressed", StyleBlend(color, background, 0.2)) + parser = KssAddColorOverride(parser, "warning-ink", OnColor(color)) + parser = KssAddColorOverride(parser, "warning-soft", StyleBlend(background, color, 0.2)) + parser = KssAddColorOverride(parser, "warning-soft-hover", StyleBlend(background, color, 0.28)) + parser = KssAddColorOverride(parser, "warning-soft-pressed", StyleBlend(background, color, 0.36)) + } + return parser +} + +// Installs the classic pack with the palette's colors. Returns false when the +// pack does not parse; the previous style then stays. +#program_export +StyleInstallSystem :: (palette: SystemPalette) -> bool { + environment: KssEnvironment = KssEnvironmentWithNames(KssDefaultEnvironment(), + ifx palette.dark then "dark" else "light", "", "", "", "", "") + parsed: StyleRulesParse = BeginStyleRules("@import ;", "system-theme", + environment) + parsed.parser = SystemStyleOverrides(parsed.parser, palette) + imports: s32 = 0 + while imports < 2 { + parsed = ParseStyleRules(parsed) + if parsed.parser.status != cast(s32)KssStatusNeedImport { break } + if KssNameEquals(parsed.parser.pending_import, "classic") { + parsed = ProvideStyleRulesImport(parsed, StylePackClassic(), "classic") + } else { + parsed = FailStyleRulesImport(parsed) + } + imports += 1 + } + return InstallParsedStyleRules(parsed) +} diff --git a/styles/classic.kss b/styles/classic.kss new file mode 100644 index 0000000..457019b --- /dev/null +++ b/styles/classic.kss @@ -0,0 +1,2244 @@ +@pack classic; + +tokens { + color { + canvas: #d4d0c8; + text: #171717; + muted: #666666; + surface: #e6e3dc; + card: #f0eee8; + button: #d4d0c8; + button-hover: #e4e1d9; + button-pressed: #bab7b0; + button-disabled: #d4d0c8; + border: #66645f; + border-soft: #9b9993; + border-hover: #383838; + border-disabled: #aaa8a2; + focus-ring: #242424; + accent: #3b609b; + accent-hover: #4c70a8; + accent-pressed: #2c5088; + accent-border: #254779; + accent-ink: #ffffff; + accent-soft: #d6dfed; + accent-soft-hover: #c9d5e7; + accent-soft-pressed: #b8c8de; + danger: #a44848; + danger-hover: #b25757; + danger-pressed: #913b3b; + danger-ink: #ffffff; + danger-soft: #eddddd; + danger-soft-hover: #e8d1d1; + danger-soft-pressed: #dec0c0; + success: #2c7654; + success-hover: #398561; + success-pressed: #206547; + success-ink: #ffffff; + success-soft: #daeadf; + success-soft-hover: #cce2d3; + success-soft-pressed: #b9d5c3; + warning: #956f10; + warning-hover: #a58020; + warning-pressed: #805e08; + warning-ink: #ffffff; + warning-soft: #f0e7ca; + warning-soft-hover: #eaddb7; + warning-soft-pressed: #dfcea2; + outline-border: #575650; + row: #e2dfd8; + row-hover: #eceae5; + row-pressed: #d3d0c9; + accent-end: #3b609b; + thumb-ink: #ffffff; + transparent: #00000000; + } + length { + radius: 0; + border: 1; + border.none: 0; + gap: 6; + space.1: 5; + space.2: 8; + space.3: 10; + field-x: 7; + font: 14; + icon: 16; + } + material { + default: Flat; + } +} + +@layer base; + +App { + background: canvas; + foreground: text; + material: default; +} + +Surface { + background: surface; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + padding-x: space.2; + padding-y: space.2; + gap: gap; + material: default; +} + +Popup { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Popup[role=Panel] { + background: card; + foreground: text; + border: border; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +Canvas { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +Card { + background: card; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + padding-x: space.3; + padding-y: space.2; + gap: gap; + material: default; +} + +Image { + font-size: font; + icon-size: icon; + background: #00000000; + foreground: #ffffff; + border: #00000000; + focus: accent; + radius: 0; + border-width: 0; + opacity: 1; + material: Flat; +} + +Image[role=Label] { + foreground: muted; + font-size: font; + opacity: 1; + material: default; +} + +Focus { + foreground: accent; + border: accent-border; + focus: accent; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +Focus:focus { + foreground: accent; + border: accent-border; +} + +Focus[role=Box] { + foreground: muted; + border: outline-border; + border-width: 2; + padding-x: 3; + opacity: 1; + material: default; +} + +Focus[role=Box]:focus { + foreground: accent; + border: accent-border; +} + +Focus[role=Label] { + foreground: muted; + font-size: font; + opacity: 1; + material: default; +} + +Focus[role=Label]:focus { + foreground: accent; +} + +NavigationBar { + background: surface; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + padding-x: space.2; + padding-y: space.2; + gap: gap; + icon-size: 86; + offset-x: 96; + offset-y: 56; + opacity: 1; + material: default; +} + +NavigationBar[role=Panel] { + offset-x: 340; + icon-size: 128; + padding-y: 58; +} + +NavigationBar[role=Row] { + icon-size: 58; + offset-y: 22; + padding-x: 36; + padding-y: 36; +} + +NavigationBar[role=Action] { + icon-size: 34; + offset-x: 180; + offset-y: 16; + padding-x: 92; + padding-y: 36; + gap: 8; +} + +NavigationBar[role=Divider] { + icon-size: 48; + offset-y: 8; + padding-y: 12; + gap: 8; +} + +NavigationBarItem { + background: transparent; + foreground: muted; + border: transparent; + focus: accent; + radius: radius; + border-width: border.none; + padding-x: 3; + padding-y: 2; + gap: 4; + font-size: font; + icon-size: 32; + offset-x: 16; + opacity: 1; + material: default; +} + +NavigationBarItem:hover { + background: button-hover; + foreground: text; +} + +NavigationBarItem:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +NavigationBarItem:disabled { + foreground: muted; + opacity: 0.45; +} + +TabBar { + font-size: font; + background: surface; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + padding-x: 120; + icon-size: 36; + offset-x: 120; + gap: gap; + opacity: 1; + material: default; +} + +Tab { + font-size: font; + background: button; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.2; + padding-y: space.1; + icon-size: 44; + opacity: 1; + material: default; +} + +Tab:hover { + background: button-hover; + foreground: text; + border: border-hover; +} + +Tab:pressed { + background: button-pressed; + foreground: text; +} + +Tab:selected { + background: card; + foreground: accent; + border: accent-border; +} + +Tab:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +TabClose { + background: transparent; + foreground: muted; + border: transparent; + radius: radius; + border-width: border.none; + icon-size: 24; + opacity: 1; + material: default; +} + +TabClose:hover { + background: button-hover; + foreground: accent; +} + +SegmentedControl { + font-size: font; + background: surface; + foreground: text; + border: border; + radius: radius; + border-width: border; + padding-x: 2; + padding-y: 2; + icon-size: 30; + offset-x: 72; + offset-y: 180; + gap: 0; + opacity: 1; + material: default; +} + +Segment { + background: button; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.2; + padding-y: 5; + offset-x: 20; + font-size: font; + opacity: 1; + material: default; +} + +Segment:hover { + background: button-hover; + foreground: text; +} + +Segment:pressed { + background: button-pressed; + foreground: text; +} + +Segment:selected { + background: card; + foreground: accent; + border: accent-border; +} + +Segment:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +Menu { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border; + radius: radius; + border-width: border; + padding-x: space.1; + padding-y: space.1; + gap: 4; + opacity: 1; + material: default; +} + +Menu[role=Bar] { + background: surface; + foreground: text; + border: border-soft; + padding-x: 12; + padding-y: 3; + gap: 2; +} + +Menu[role=Popup] { + background: card; + foreground: text; + border: outline-border; + padding-x: 12; + gap: 4; + offset-x: 180; +} + +Menu[role=Context] { + background: card; + foreground: text; + border: outline-border; +} + +MenuItem { + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + focus: accent; + radius: radius; + border-width: border.none; + padding-x: space.2; + padding-y: 8; + offset-x: 88; + offset-y: 30; + font-size: font; + opacity: 1; + material: default; +} + +MenuItem:hover { + background: button-hover; + foreground: text; + border: border-hover; +} + +MenuItem:pressed { + background: button-pressed; + foreground: text; +} + +MenuItem:selected { + background: card; + foreground: accent; + border: accent-border; +} + +MenuItem:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +MenuSeparator { + background: transparent; + foreground: border; + border: border; + radius: 0; + border-width: border; + padding-x: 12; + gap: 4; + icon-size: 32; + offset-x: 60; + opacity: 1; + material: default; +} + +Spinbox { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +SpinboxValue { + background: card; + foreground: text; + border: border-soft; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.2; + font-size: font; + opacity: 1; + material: default; +} + +Drag { + icon-size: icon; + background: surface; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + font-size: font; + padding-x: 6; + padding-y: 4; + opacity: 1; + material: default; +} + +DragValue { + icon-size: icon; + background: card; + foreground: text; + border: border-soft; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.2; + font-size: font; + opacity: 1; + material: default; +} + +DragValue:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +ColorPicker { + font-size: font; + opacity: 1; + material: default; + gap: space.1; + icon-size: 36; + padding-y: 2; + offset-x: 20; + offset-y: 28; +} + +ColorPickerSwatch { + background: transparent; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.1; + font-size: font; + opacity: 1; + material: default; +} + +ListBox { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.1; + padding-y: space.1; + offset-y: 8; + gap: space.1; + opacity: 1; + material: default; +} + +ListBoxItem { + background: transparent; + foreground: text; + border: transparent; + focus: accent; + radius: radius; + border-width: border.none; + padding-x: space.2; + padding-y: space.1; + icon-size: 30; + offset-y: 30; + font-size: font; + opacity: 1; + material: default; +} + +ListBoxItem:hover { + background: button-hover; + foreground: text; + border: border-hover; +} + +ListBoxItem:selected { + background: card; + foreground: accent; + border: accent-border; +} + +ListBoxItem:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +TreeView { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.1; + padding-y: space.1; + gap: space.1; + opacity: 1; + material: default; +} + +TreeViewItem { + background: transparent; + foreground: text; + border: transparent; + focus: accent; + radius: radius; + border-width: border.none; + padding-x: space.2; + padding-y: 7; + gap: 2; + icon-size: 16; + offset-x: 18; + offset-y: 28; + font-size: font; + opacity: 1; + material: default; +} + +TreeViewItem:hover { + background: button-hover; + foreground: text; + border: border-hover; +} + +TreeViewItem:selected { + background: card; + foreground: accent; + border: accent-border; +} + +TreeViewItem:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +ListBoxMulti { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.1; + padding-y: space.1; + gap: space.1; + opacity: 1; + material: default; +} + +ListBoxMultiItem { + background: transparent; + foreground: text; + border: transparent; + focus: accent; + radius: radius; + border-width: border.none; + padding-x: space.2; + padding-y: space.1; + icon-size: 28; + offset-y: 28; + font-size: font; + opacity: 1; + material: default; +} + +ListBoxMultiItem:hover { + background: button-hover; + foreground: text; + border: border-hover; +} + +ListBoxMultiItem:selected { + background: card; + foreground: accent; + border: accent-border; +} + +ListBoxMultiItem:focus { + border: accent; +} + +ListBoxMultiItem:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.55; +} + +DragDropTarget { + background: transparent; + border: border-hover; + focus: accent; + radius: radius; + border-width: border; + material: default; +} + +DragDropTarget:hover { + border: accent-border; + border-width: 2; +} + +Reorder { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Reorder[role=Handle] { + background: transparent; + foreground: muted; + border: transparent; + border-width: border.none; + padding-x: 8; + gap: 4; + icon-size: 3; + offset-x: 36; + offset-y: 5; + opacity: 1; + material: default; +} + +Reorder[role=Handle]:selected { + foreground: accent; +} + +Reorder[role=Placeholder] { + background: accent; + foreground: accent; + border: accent-border; + radius: radius; + border-width: 2; + padding-x: 3; + icon-size: 32; + offset-x: 34; + offset-y: 12; + opacity: 0.10; + material: default; +} + +Selectable { + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + focus: accent; + radius: radius; + border-width: border.none; + padding-x: space.2; + padding-y: space.1; + font-size: font; + opacity: 1; + material: default; +} + +Selectable:hover { + background: button-hover; + foreground: text; +} + +Selectable:pressed { + background: button-pressed; + foreground: text; +} + +Selectable:selected { + background: accent; + foreground: accent-ink; +} + +Selectable:disabled { + foreground: muted; + opacity: 0.45; +} + +Fieldset { + icon-size: icon; + background: canvas; + foreground: muted; + border: border; + radius: radius; + border-width: border; + padding-x: 6; + padding-y: 6; + gap: 7; + font-size: font; + opacity: 1; + material: default; +} + +PanedView { + background: transparent; + foreground: muted; + border: transparent; + opacity: 1; + material: default; +} + +PanedView[role=Handle] { + background: border; + foreground: muted; + border: transparent; + radius: 0; + border-width: border.none; + icon-size: 8; + opacity: 1; + material: default; +} + +PanedView[role=Handle]:hover { + background: border-hover; +} + +PanedView[role=Handle]:pressed { + background: accent; +} + +Toast { + background: surface; + foreground: text; + border: border; + radius: radius; + border-width: border; + padding-x: 14; + padding-y: 10; + gap: 18; + opacity: 1; + material: default; +} + +Toast[role=Label] { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Collapsible { + font-size: font; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Collapsible[role=Header] { + background: button; + foreground: text; + border: border; + radius: radius; + border-width: border; + font-size: font; + icon-size: 20; + padding-x: 8; + padding-y: 8; + opacity: 1; + material: default; +} + +Collapsible[role=Header]:hover { + background: button-hover; +} + +Collapsible[role=Header]:pressed { + background: button-pressed; +} + +Collapsible[role=Header]:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Collapsible[role=Header]:disabled { + foreground: muted; + opacity: 0.45; +} + +Collapsible[role=TreeHeader] { + background: transparent; + foreground: text; + border: transparent; + radius: 0; + border-width: border.none; + font-size: font; + icon-size: 20; + padding-x: 20; + padding-y: 8; + opacity: 1; + material: default; +} + +Collapsible[role=TreeHeader]:hover { + background: button-hover; +} + +Collapsible[role=TreeHeader]:selected { + background: transparent; + foreground: accent; +} + +Collapsible[role=TreeHeader]:disabled { + foreground: muted; + opacity: 0.45; +} + +Collapsible[role=Close] { + background: transparent; + foreground: muted; + border: transparent; + font-size: font; + icon-size: 28; + opacity: 1; + material: default; +} + +Collapsible[role=Close]:hover { + foreground: text; +} + +Collapsible[role=Close]:disabled { + opacity: 0.45; +} + +TitleBar { + font-size: font; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +TitleBar[role=Bar] { + background: surface; + foreground: text; + border: border; + radius: 0; + border-width: border; + padding-x: 12; + gap: 4; + icon-size: 32; + offset-x: 60; + opacity: 1; + material: default; +} + +TitleBar[role=Title] { + background: transparent; + foreground: text; + border: transparent; + font-size: font; + icon-size: 48; + padding-x: 16; + opacity: 1; + material: default; +} + +TitleBar[role=Action] { + background: transparent; + foreground: text; + border: transparent; + radius: radius; + border-width: border.none; + icon-size: 20; + padding-x: 10; + opacity: 1; + material: default; +} + +TitleBar[role=Action]:hover { + background: button-hover; +} + +TitleBar[role=Action]:pressed { + background: button-pressed; +} + +Toolbar { + font-size: font; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Toolbar[role=Bar] { + background: surface; + foreground: text; + border: border; + radius: 0; + border-width: border; + padding-x: 12; + opacity: 1; + material: default; +} + +Toolbar[role=Divider] { + background: transparent; + foreground: muted; + border: border; + border-width: border; + opacity: 1; + material: default; +} + +Toolbar[role=Action] { + background: transparent; + foreground: text; + border: transparent; + radius: radius; + border-width: border.none; + padding-x: 8; + gap: 6; + icon-size: 20; + opacity: 1; + material: default; +} + +Toolbar[role=Action]:hover { + background: button-hover; +} + +Toolbar[role=Action]:pressed { + background: button-pressed; +} + +Toolbar[role=BottomBar] { + padding-x: 24; + padding-y: 6; + gap: 8; + offset-x: 120; +} + +Toolbar[role=BottomAction] { + padding-x: 10; + gap: 12; + icon-size: 24; + offset-x: 16; + offset-y: 6; +} + +Modal { + font-size: font; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Modal[role=Scrim] { + background: #000000; + foreground: text; + border: transparent; + opacity: 0.32; + material: default; +} + +Modal[role=Panel] { + background: card; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: 18; + padding-y: 18; + gap: 24; + icon-size: 280; + offset-x: 420; + offset-y: 58; + opacity: 1; + material: default; +} + +Modal[role=Title] { + foreground: text; + font-size: font; + padding-x: 24; + padding-y: 18; + icon-size: 48; + offset-y: 14; + opacity: 1; + material: default; +} + +Modal[role=Message] { + foreground: text; + font-size: font; + padding-x: 120; + padding-y: 160; + gap: 18; + icon-size: 38; + offset-y: 18; + opacity: 1; + material: default; +} + +Modal[role=Action] { + background: button; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: 24; + gap: 8; + icon-size: 44; + offset-x: 88; + offset-y: 150; + opacity: 1; + material: default; +} + +Modal[role=Action]:hover { + background: button-hover; + border: border-hover; +} + +Modal[role=Action]:pressed { + background: button-pressed; +} + +Modal[role=Action][tone=Accent] { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Modal[role=Close] { + background: transparent; + foreground: text; + border: transparent; + radius: radius; + border-width: border.none; + padding-x: 8; + padding-y: 16; + gap: 6; + icon-size: 20; + offset-x: 120; + offset-y: 96; + opacity: 1; + material: default; +} + +Modal[role=Close]:hover { + background: button-hover; + border: border; +} + +Modal[role=Close]:pressed { + background: button-pressed; +} + +Guide { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Guide[role=Scrim] { + background: #000000; + foreground: text; + border: transparent; + opacity: 0.22; + material: default; +} + +Guide[role=Bar] { + background: card; + foreground: text; + border: border; + radius: 0; + border-width: border.none; + padding-x: 12; + gap: 12; + icon-size: 48; + offset-x: 48; + opacity: 1; + material: default; +} + +Guide[role=Divider] { + background: transparent; + foreground: text; + border: border; + border-width: border; + opacity: 1; + material: default; +} + +Guide[role=Panel] { + background: card; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: 12; + padding-y: 12; + gap: 20; + offset-x: 300; + offset-y: 112; + opacity: 1; + material: default; +} + +Guide[role=Anchor] { + background: transparent; + foreground: accent; + border: accent-border; + border-width: border; + padding-x: 4; + opacity: 1; + material: default; +} + +Guide[role=Label] { + foreground: text; + font-size: font; + padding-x: 8; + padding-y: 6; + gap: 8; + opacity: 1; + material: default; +} + +Guide[role=Action] { + background: button; + foreground: text; + border: border; + radius: radius; + border-width: border; + padding-x: 7; + gap: 8; + icon-size: 19; + offset-y: 34; + opacity: 1; + material: default; +} + +Guide[role=Action]:hover { + background: button-hover; + border: border-hover; +} + +Guide[role=Close] { + background: transparent; + foreground: text; + border: transparent; + radius: radius; + border-width: border.none; + padding-x: 6; + gap: 12; + icon-size: 16; + offset-y: 28; + opacity: 1; + material: default; +} + +Guide[role=Close]:hover { + background: button-hover; + border: border; +} + +TableView { + font-size: font; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + offset-y: 28; + opacity: 1; + material: default; +} + +TableView[role=Panel] { + background: card; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +TableView[role=Header] { + background: surface; + foreground: text; + border: border; + radius: radius; + border-width: border; + padding-x: 6; + offset-y: 30; + font-size: font; + opacity: 1; + material: default; +} + +TableView[role=Header]:hover { + background: button-hover; + border: border-hover; +} + +TableView[role=Header]:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +TableView[role=Row] { + background: row; + foreground: text; + border: transparent; + radius: 0; + border-width: border.none; + opacity: 1; + material: default; +} + +TableView[role=Cell] { + background: transparent; + foreground: text; + border: transparent; + offset-x: 32; + font-size: font; + opacity: 1; + material: default; +} + +TableView[role=Selection] { + background: accent; + foreground: accent-ink; + border: accent-border; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +TableView[role=Divider] { + background: transparent; + foreground: muted; + border: border; + border-width: border; + padding-x: 5; + offset-y: 8; + opacity: 1; + material: default; +} + +Plot { + icon-size: icon; + background: card; + foreground: muted; + border: border; + radius: radius; + border-width: border; + padding-x: 6; + padding-y: 4; + font-size: font; + opacity: 1; + material: default; +} + +PlotMark { + background: accent; + foreground: accent-ink; + border: accent-border; + opacity: 1; + material: default; +} + +Text { + foreground: text; + font-size: font; +} + +Page { + gap: 0; + padding-x: 0; +} + +Section { + gap: 0; + padding-x: 0; +} + +Heading { + foreground: text; + font-size: 24; +} + +ParagraphText { + foreground: text; + font-size: font; + gap: 4; +} + +Link { + icon-size: icon; + foreground: accent; + font-size: font; + opacity: 1; + material: default; +} + +Link:hover { + foreground: accent-border; +} + +Link:disabled { + foreground: muted; + opacity: 0.45; +} + +Separator { + opacity: 1; + material: default; + background: border-soft; + foreground: muted; + gap: gap; +} + +Separator[role=Line] { + background: border-soft; + foreground: muted; + gap: gap; +} + +Separator[role=Label] { + background: border-soft; + foreground: muted; + font-size: font; + gap: gap; +} + +Separator[role=Bullet] { + background: border-soft; + foreground: muted; + icon-size: 6; + gap: gap; +} + +Progress { + font-size: font; + icon-size: icon; + background: surface; + foreground: text; + border: border; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +Progress[role=Track] { + background: surface; + foreground: text; + border: border; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +Progress[role=Fill] { + background: accent; + foreground: accent-ink; + border: accent-border; + opacity: 1; + material: default; +} + +Progress[role=Label] { + background: transparent; + foreground: text; + border: transparent; + padding-x: 4; + font-size: font; + opacity: 1; + material: default; +} + +@layer components; + +Button { + background: button; + background-end: button; + foreground: text; + border: border; + focus: focus-ring; + radius: radius; + border-width: border; + padding-x: space.3; + padding-y: space.1; + gap: gap; + font-size: font; + icon-size: icon; + opacity: 1; + material: default; +} + +Button:hover { + background: button-hover; + background-end: button-hover; + border: border-hover; +} + +Button:pressed { + background: button-pressed; + background-end: button-pressed; +} + +Button:selected { + border: focus-ring; + border-width: 2; +} + +Button:focus { + border: focus-ring; +} + +Button:disabled { + foreground: muted; + background: button-disabled; + background-end: button-disabled; + border: border-disabled; + opacity: 1; +} + +Button:loading { + foreground: accent; +} + +Button[tone=Accent] { + background: accent; + background-end: accent; + foreground: accent-ink; + border: accent-border; +} + +Button[tone=Accent]:loading { + foreground: accent; +} + +Button[tone=Danger] { + background: danger; + background-end: danger; + foreground: danger-ink; + border: danger; +} + +Button[tone=Success] { + background: success; + background-end: success; + foreground: success-ink; + border: success; +} + +Button[tone=Warning] { + background: warning; + background-end: warning; + foreground: warning-ink; + border: warning; +} + +Button[emphasis=Soft] { + background: row; + background-end: row; + foreground: text; + border: border-soft; +} + +Button[emphasis=Soft]:hover { + background: row-hover; + background-end: row-hover; +} + +Button[emphasis=Soft]:pressed { + background: row-pressed; + background-end: row-pressed; +} + +Button[tone=Accent][emphasis=Soft] { + background: accent-soft; + background-end: accent-soft; + foreground: accent; + border: accent-soft; +} + +Button[tone=Danger][emphasis=Soft] { + background: danger-soft; + background-end: danger-soft; + foreground: danger; + border: danger-soft; +} + +Button[tone=Success][emphasis=Soft] { + background: success-soft; + background-end: success-soft; + foreground: success; + border: success-soft; +} + +Button[tone=Warning][emphasis=Soft] { + background: warning-soft; + background-end: warning-soft; + foreground: warning; + border: warning-soft; +} + +Button[emphasis=Outline] { + background: canvas; + background-end: canvas; + foreground: text; + border: outline-border; +} + +Button[emphasis=Outline]:hover { + background: button-hover; + background-end: button-hover; +} + +Button[emphasis=Outline]:pressed { + background: button-pressed; + background-end: button-pressed; +} + +Button[emphasis=Ghost] { + background: transparent; + background-end: transparent; + border: transparent; + border-width: border.none; +} + +Button[tone=Accent][emphasis=Outline] { foreground: accent; } +Button[tone=Danger][emphasis=Outline] { foreground: danger; } +Button[tone=Success][emphasis=Outline] { foreground: success; } +Button[tone=Warning][emphasis=Outline] { foreground: warning; } + +Button[tone=Accent][emphasis=Ghost] { foreground: accent; } +Button[tone=Danger][emphasis=Ghost] { foreground: danger; } +Button[tone=Success][emphasis=Ghost] { foreground: success; } +Button[tone=Warning][emphasis=Ghost] { foreground: warning; } + +Button[emphasis=Ghost]:hover { + background: row-hover; + background-end: row-hover; +} +Button[emphasis=Ghost]:pressed { + background: button-pressed; + background-end: button-pressed; +} + +Button[tone=Accent][emphasis=Filled]:loading { + foreground: accent-ink; +} + +Button[emphasis=Link] { + background: transparent; + background-end: transparent; + foreground: accent; + border: transparent; + border-width: border.none; +} + +Button[tone=Danger][emphasis=Link] { foreground: danger; } +Button[tone=Success][emphasis=Link] { foreground: success; } +Button[tone=Warning][emphasis=Link] { foreground: warning; } + +Button[emphasis=Link]:hover { foreground: accent-border; } + +Button[tone=Accent][emphasis=Filled]:hover { + background: accent-hover; + background-end: accent-hover; +} +Button[tone=Accent][emphasis=Filled]:pressed { + background: accent-pressed; + background-end: accent-pressed; +} +Button[tone=Danger][emphasis=Filled]:hover { + background: danger-hover; + background-end: danger-hover; +} +Button[tone=Danger][emphasis=Filled]:pressed { + background: danger-pressed; + background-end: danger-pressed; +} +Button[tone=Success][emphasis=Filled]:hover { + background: success-hover; + background-end: success-hover; +} +Button[tone=Success][emphasis=Filled]:pressed { + background: success-pressed; + background-end: success-pressed; +} +Button[tone=Warning][emphasis=Filled]:hover { + background: warning-hover; + background-end: warning-hover; +} +Button[tone=Warning][emphasis=Filled]:pressed { + background: warning-pressed; + background-end: warning-pressed; +} + +Button[tone=Accent][emphasis=Soft]:hover { + background: accent-soft-hover; + background-end: accent-soft-hover; +} +Button[tone=Accent][emphasis=Soft]:pressed { + background: accent-soft-pressed; + background-end: accent-soft-pressed; +} +Button[tone=Danger][emphasis=Soft]:hover { + background: danger-soft-hover; + background-end: danger-soft-hover; +} +Button[tone=Danger][emphasis=Soft]:pressed { + background: danger-soft-pressed; + background-end: danger-soft-pressed; +} +Button[tone=Success][emphasis=Soft]:hover { + background: success-soft-hover; + background-end: success-soft-hover; +} +Button[tone=Success][emphasis=Soft]:pressed { + background: success-soft-pressed; + background-end: success-soft-pressed; +} +Button[tone=Warning][emphasis=Soft]:hover { + background: warning-soft-hover; + background-end: warning-soft-hover; +} +Button[tone=Warning][emphasis=Soft]:pressed { + background: warning-soft-pressed; + background-end: warning-soft-pressed; +} + +Button[tone=Accent][emphasis=Link]:hover { foreground: accent-hover; } +Button[tone=Danger][emphasis=Link]:hover { foreground: danger-hover; } +Button[tone=Success][emphasis=Link]:hover { foreground: success-hover; } +Button[tone=Warning][emphasis=Link]:hover { foreground: warning-hover; } + +Dropdown { + icon-size: icon; + opacity: 1; + background: card; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: space.2; + padding-y: space.1; + gap: gap; + offset-y: 16; + font-size: font; + material: default; + offset-x: 24; +} + +Dropdown:hover { + background: button-hover; + border: border-hover; +} + +Dropdown:pressed { + background: button-pressed; +} + +Dropdown:focus { + border: accent; +} + +Dropdown:disabled { + foreground: muted; + background: button-disabled; + border: border-disabled; + opacity: 0.7; +} + +Dropdown[tone=Accent] { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Dropdown:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Dropdown[role=Panel] { + background: card; + border: border; + padding-y: 4; + gap: 4; + offset-y: 16; +} + +Dropdown[role=Option] { + padding-x: 4; + padding-y: 2; +} + +Dropdown[role=Scrollbar] { + icon-size: 8; + padding-y: 2; + gap: 2; + offset-x: 8; +} + +Dropdown[role=Option]:hover { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Dropdown[role=Option]:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Slider { + font-size: font; + icon-size: icon; + background: button-pressed; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +Slider[role=Track] { + padding-x: 28; + padding-y: 4; + gap: 10; + icon-size: 6; + offset-x: 28; + offset-y: 24; + opacity: 1; +} + +Slider[role=Label] { + foreground: text; + font-size: font; +} + +Slider:hover { + background: button-hover; + border: border-hover; +} + +Slider[role=Fill] { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Slider:disabled { + foreground: muted; + background: button-disabled; + border: border-disabled; + opacity: 0.7; +} + +SliderThumb { + background: accent; + foreground: thumb-ink; + border: accent-border; + focus: accent; + radius: radius; + border-width: border; + gap: 6; + icon-size: 18; + opacity: 1; + material: default; +} + +SliderThumb:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + focus: border-disabled; + opacity: 0.7; +} + +Scroll { + background: button-pressed; + foreground: text; + border: border-soft; + focus: accent; + radius: radius; + border-width: border; + icon-size: 10; + padding-x: 16; + padding-y: 8; + gap: 20; + offset-x: 5; + offset-y: 42; + opacity: 1; + material: default; +} + +ScrollThumb { + background: button; + foreground: text; + border: outline-border; + focus: accent; + radius: radius; + border-width: border; + icon-size: 16; + padding-x: 2; + opacity: 1; + material: default; +} + +ScrollThumb:hover { + background: button-hover; + border: border-hover; +} + +ScrollThumb:pressed { + background: button-pressed; + border: accent-border; +} + +Toggle { + font-size: font; + icon-size: icon; + background: button; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + opacity: 1; + material: default; +} + +Toggle[role=Track] { + padding-x: 46; + padding-y: 24; + gap: 2; + radius: 999; + opacity: 1; +} + +Toggle[role=Label] { + foreground: text; + padding-x: 10; + font-size: font; + opacity: 1; +} + +Toggle:hover { + background: button-hover; + border: border-hover; +} + +Toggle:pressed { + background: button-pressed; +} + +Toggle[role=Fill] { + background: accent; + foreground: accent-ink; + border: accent-border; + radius: 999; + gap: 2; +} + +Toggle:disabled { + foreground: muted; + background: button-disabled; + border: border-disabled; + opacity: 0.7; +} + +ToggleThumb { + background: card; + foreground: thumb-ink; + border: border-hover; + focus: accent; + radius: radius; + border-width: border; + gap: 4; + icon-size: 16; + opacity: 1; + material: default; +} + +ToggleThumb[tone=Accent] { + background: thumb-ink; + foreground: thumb-ink; + border: accent-border; + focus: accent; + icon-size: 18; +} + +ToggleThumb:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + focus: border-disabled; + opacity: 0.7; +} + +Checkbox { + font-size: font; + background: card; + foreground: text; + border: border; + focus: accent; + border-width: border; + padding-x: 22; + padding-y: 3; + gap: 4; + icon-size: 20; + opacity: 1; + material: default; +} + +Checkbox:hover { + background: button-hover; + border: border-hover; +} + +Checkbox:pressed { + background: button-pressed; +} + +Checkbox:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Checkbox:disabled { + background: button-disabled; + foreground: muted; + border: border-disabled; + opacity: 0.7; +} + +Checkbox[role=Box] { + background: card; + foreground: text; + border: border; + focus: accent; + border-width: border; + padding-x: 22; + padding-y: 3; + gap: 4; + icon-size: 20; + opacity: 1; + material: default; +} + +Checkbox[role=Mark] { + background: accent; + foreground: accent-ink; + border: accent-border; + focus: accent; + border-width: border; + padding-x: 5; + icon-size: 2.4; + opacity: 1; + material: default; +} + +Checkbox[role=Label] { + foreground: text; + gap: 10; + font-size: font; + opacity: 1; + material: default; +} + +Checkbox[role=Label]:disabled { + foreground: muted; + opacity: 0.7; +} + +Radio { + font-size: font; + icon-size: icon; + background: card; + foreground: text; + border: border; + focus: accent; + border-width: border; + opacity: 1; + material: default; +} + +Radio:hover { + border: border-hover; +} + +Radio:selected { + background: accent; + foreground: text; + border: accent-border; +} + +Radio:disabled { + foreground: muted; + border: border-disabled; + opacity: 0.7; +} + +Radio[role=Ring] { + background: card; + foreground: text; + border: border; + focus: accent; + border-width: border; + icon-size: 20; + padding-x: 20; + gap: gap; + opacity: 1; + material: default; +} + +Radio[role=Mark] { + background: accent; + foreground: text; + border: accent-border; + focus: accent; + border-width: border; + font-size: font; + opacity: 1; + material: default; +} + +Radio[role=Label] { + foreground: text; + font-size: font; + opacity: 1; + material: default; +} + +Radio[role=Label]:disabled { + foreground: muted; + opacity: 0.7; +} + +TextField { + opacity: 1; + background: card; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: field-x; + padding-y: space.1; + font-size: font; + material: default; +} + +TextArea { + opacity: 1; + background: card; + foreground: text; + border: border; + focus: accent; + radius: radius; + border-width: border; + padding-x: field-x; + padding-y: gap; + font-size: font; + gap: space.1; + material: default; +} diff --git a/styles/lightfield.kss b/styles/lightfield.kss new file mode 100644 index 0000000..8cc34bc --- /dev/null +++ b/styles/lightfield.kss @@ -0,0 +1,2230 @@ +@pack lightfield; + +tokens { + color { + canvas: #0d1118; + text: #f4f0ff; + muted: #969aa5; + surface: #171c25dd; + surface-end: #222936ee; + card: #1a202bdd; + card-end: #252c39ee; + button: #202633dd; + button-end: #2b3342ee; + button-hover: #283142ee; + button-hover-end: #333c4fee; + button-pressed: #171c25ee; + button-pressed-end: #202734ee; + dropdown-end: #242b38ee; + popup: #1c2230; + border: #41495a; + border-soft: #343c4c; + card-border: #384252; + focus: #caa8ff; + accent: #caa8ff; + accent-end: #b990ff; + accent-hover: #d5bcff; + accent-hover-end: #c49fff; + accent-ink: #171022; + accent-border: #dbc4ff; + accent-soft: #352948dd; + accent-soft-end: #44365aee; + danger: #f0819a; + danger-ink: #260d16; + danger-soft: #432933dd; + success: #70d7b3; + success-ink: #10241c; + success-soft: #244038dd; + warning: #f4d282; + warning-ink: #2b210d; + warning-soft: #443a29dd; + row: #171d27aa; + thumb-ink: #ffffff; + transparent: #00000000; + } + length { + radius: 10; + border: 1; + gap: 8; + space.3: 12; + space.4: 16; + space.5: 18; + pad.control-y: 10; + pad.dropdown-y: 9; + pad.field-y: 9; + font: 16; + font.md: 16; + icon: 20; + } + material { + app: Flat; + premium: Lightfield; + } +} + +@layer base; + +App { + background: canvas; + foreground: text; + material: app; +} + +Surface { + background: surface; + background-end: surface-end; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + padding-x: space.5; + padding-y: space.4; + gap: space.3; + material: premium; +} + +Popup { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: premium; +} + +Popup[role=Panel] { + background: button; + background-end: dropdown-end; + foreground: text; + border: border-soft; + focus: focus; + radius: radius; + border-width: border; + opacity: 1; + material: premium; +} + +Canvas { + font-size: font.md; + icon-size: icon; + background: surface; + background-end: surface-end; + foreground: text; + border: border-soft; + focus: focus; + radius: radius; + border-width: border; + opacity: 1; + material: premium; +} + +Card { + background: card; + background-end: card-end; + foreground: text; + border: card-border; + radius: radius; + border-width: border; + padding-x: space.5; + padding-y: space.4; + gap: space.3; + material: premium; +} + +Image { + font-size: font.md; + icon-size: icon; + background: #00000000; + foreground: #ffffff; + border: #00000000; + focus: focus; + radius: 0; + border-width: 0; + opacity: 1; + material: Flat; +} + +Image[role=Label] { + foreground: muted; + font-size: font; + opacity: 1; + material: app; +} + +Focus { + foreground: focus; + border: focus; + focus: focus; + radius: radius; + border-width: border; + opacity: 1; + material: app; +} + +Focus:focus { + foreground: focus; + border: focus; +} + +Focus[role=Box] { + foreground: muted; + border: border; + border-width: 2; + padding-x: 3; + opacity: 1; + material: app; +} + +Focus[role=Box]:focus { + foreground: focus; + border: focus; +} + +Focus[role=Label] { + foreground: muted; + font-size: font; + opacity: 1; + material: app; +} + +Focus[role=Label]:focus { + foreground: focus; +} + +NavigationBar { + background: surface; + background-end: surface-end; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + padding-x: space.5; + padding-y: space.3; + gap: gap; + icon-size: 86; + offset-x: 96; + offset-y: 56; + opacity: 1; + material: premium; +} + +NavigationBar[role=Panel] { + offset-x: 340; + icon-size: 128; + padding-y: 58; +} + +NavigationBar[role=Row] { + icon-size: 58; + offset-y: 22; + padding-x: 36; + padding-y: 36; +} + +NavigationBar[role=Action] { + icon-size: 34; + offset-x: 180; + offset-y: 16; + padding-x: 92; + padding-y: 36; + gap: 8; +} + +NavigationBar[role=Divider] { + icon-size: 48; + offset-y: 8; + padding-y: 12; + gap: 8; +} + +NavigationBarItem { + background: transparent; + foreground: muted; + border: transparent; + focus: focus; + radius: radius; + border-width: 0; + padding-x: 3; + padding-y: 2; + gap: 4; + font-size: font; + icon-size: 32; + offset-x: 16; + opacity: 1; + material: premium; +} + +NavigationBarItem:hover { + background: button-hover; + background-end: button-hover-end; + foreground: text; +} + +NavigationBarItem:selected { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +NavigationBarItem:disabled { + foreground: muted; + opacity: 0.45; +} + +TabBar { + font-size: font.md; + background: surface; + background-end: surface-end; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + padding-x: 120; + icon-size: 36; + offset-x: 120; + gap: gap; + opacity: 1; + material: premium; +} + +Tab { + font-size: font.md; + background: button; + background-end: button-end; + foreground: text; + border: border-soft; + focus: focus; + radius: radius; + border-width: border; + padding-x: space.3; + padding-y: 8; + icon-size: 44; + opacity: 1; + material: premium; +} + +Tab:hover { + background: button-hover; + background-end: button-hover-end; +} + +Tab:pressed { + background: button-pressed; + background-end: button-pressed-end; +} + +Tab:selected { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +Tab:disabled { + foreground: muted; + opacity: 0.45; +} + +TabClose { + background: transparent; + foreground: muted; + border: transparent; + radius: radius; + border-width: 0; + icon-size: 24; + opacity: 1; + material: premium; +} + +TabClose:hover { + background: button-hover; + background-end: button-hover-end; + foreground: accent; +} + +SegmentedControl { + font-size: font.md; + background: surface; + background-end: surface-end; + foreground: text; + border: border; + radius: radius; + border-width: 1; + padding-x: 2; + padding-y: 2; + icon-size: 30; + offset-x: 72; + offset-y: 180; + gap: gap; + opacity: 1; + material: premium; +} + +Segment { + background: transparent; + foreground: muted; + border: transparent; + focus: focus; + radius: radius; + border-width: 0; + padding-x: space.3; + padding-y: 8; + offset-x: 20; + font-size: font; + opacity: 1; + material: premium; +} + +Segment:hover { + background: button-hover; + background-end: button-hover-end; + foreground: text; +} + +Segment:pressed { + background: button-pressed; + background-end: button-pressed-end; + foreground: text; +} + +Segment:selected { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +Segment:disabled { + foreground: muted; + opacity: 0.45; +} + +Menu { + font-size: font.md; + icon-size: icon; + background: surface; + background-end: surface-end; + foreground: text; + border: border; + radius: radius; + border-width: 1; + padding-x: space.3; + padding-y: gap; + gap: gap; + opacity: 1; + material: premium; +} + +Menu[role=Bar] { + background: surface; + background-end: surface-end; + foreground: text; + border: border-soft; + padding-x: 12; + padding-y: 3; + gap: 2; +} + +Menu[role=Popup] { + background: card; + background-end: card-end; + foreground: text; + border: card-border; + padding-x: 12; + gap: 4; + offset-x: 180; +} + +Menu[role=Context] { + background: card; + background-end: card-end; + foreground: text; + border: card-border; +} + +MenuItem { + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + focus: focus; + radius: radius; + border-width: 0; + padding-x: space.3; + padding-y: 7; + offset-x: 88; + offset-y: 30; + font-size: font; + opacity: 1; + material: premium; +} + +MenuItem:hover { + background: button-hover; + background-end: button-hover-end; + foreground: text; +} + +MenuItem:pressed { + background: button-pressed; + background-end: button-pressed-end; + foreground: text; +} + +MenuItem:selected { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +MenuItem:disabled { + foreground: muted; + opacity: 0.45; +} + +MenuSeparator { + background: transparent; + foreground: border; + border: border; + radius: 0; + border-width: 1; + opacity: 1; + material: premium; +} + +Spinbox { + font-size: font.md; + icon-size: icon; + background: surface; + background-end: surface-end; + foreground: text; + border: border-soft; + focus: focus; + radius: radius; + border-width: 1; + opacity: 1; + material: premium; +} + +SpinboxValue { + background: button; + background-end: button-end; + foreground: text; + border: border-soft; + focus: focus; + radius: radius; + border-width: 1; + padding-x: space.3; + font-size: font; + opacity: 1; + material: premium; +} + +Drag { + icon-size: icon; + background: surface; + background-end: surface-end; + foreground: text; + border: border-soft; + focus: focus; + radius: radius; + border-width: 1; + font-size: font; + opacity: 1; + material: premium; +} + +DragValue { + icon-size: icon; + background: button; + background-end: button-end; + foreground: text; + border: border-soft; + focus: focus; + radius: radius; + border-width: 1; + padding-x: space.3; + font-size: font; + opacity: 1; + material: premium; +} + +DragValue:disabled { + background: surface; + background-end: surface-end; + foreground: muted; + border: border-soft; + opacity: 0.55; +} + +ColorPicker { + font-size: font.md; + opacity: 1; + material: app; + gap: 4; + icon-size: 36; + padding-y: 2; + offset-x: 20; + offset-y: 28; +} + +ColorPickerSwatch { + background: transparent; + foreground: text; + border: border; + focus: focus; + radius: radius; + border-width: 1; + padding-x: gap; + font-size: font; + opacity: 1; + material: premium; +} + +ListBox { + font-size: font.md; + icon-size: icon; + background: surface; + background-end: surface-end; + foreground: text; + border: border; + focus: focus; + radius: radius; + border-width: 1; + padding-x: gap; + padding-y: gap; + offset-y: 8; + gap: gap; + opacity: 1; + material: premium; +} + +ListBoxItem { + background: transparent; + foreground: text; + border: transparent; + focus: focus; + radius: radius; + border-width: 0; + padding-x: space.3; + padding-y: gap; + icon-size: 30; + offset-y: 30; + font-size: font; + opacity: 1; + material: premium; +} + +ListBoxItem:hover { + background: button-hover; + background-end: button-hover-end; + foreground: text; +} + +ListBoxItem:selected { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +ListBoxItem:disabled { + foreground: muted; + opacity: 0.45; +} + +TreeView { + font-size: font.md; + icon-size: icon; + background: surface; + background-end: surface-end; + foreground: text; + border: border; + focus: focus; + radius: radius; + border-width: 1; + padding-x: gap; + padding-y: gap; + gap: gap; + opacity: 1; + material: premium; +} + +TreeViewItem { + background: transparent; + foreground: text; + border: transparent; + focus: focus; + radius: radius; + border-width: 0; + padding-x: space.3; + padding-y: 6; + gap: 2; + icon-size: 16; + offset-x: 18; + offset-y: 28; + font-size: font; + opacity: 1; + material: premium; +} + +TreeViewItem:hover { + background: button-hover; + background-end: button-hover-end; + foreground: text; +} + +TreeViewItem:selected { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +TreeViewItem:disabled { + foreground: muted; + opacity: 0.45; +} + +ListBoxMulti { + font-size: font.md; + icon-size: icon; + background: surface; + background-end: surface-end; + foreground: text; + border: border; + focus: focus; + radius: radius; + border-width: 1; + padding-x: gap; + padding-y: gap; + gap: gap; + opacity: 1; + material: premium; +} + +ListBoxMultiItem { + background: transparent; + foreground: text; + border: transparent; + focus: focus; + radius: radius; + border-width: 0; + padding-x: space.3; + padding-y: gap; + icon-size: 28; + offset-y: 28; + font-size: font; + opacity: 1; + material: premium; +} + +ListBoxMultiItem:hover { + background: button-hover; + background-end: button-hover-end; + foreground: text; +} + +ListBoxMultiItem:selected { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +ListBoxMultiItem:focus { + border: focus; +} + +ListBoxMultiItem:disabled { + foreground: muted; + opacity: 0.45; +} + +DragDropTarget { + background: transparent; + border: border; + focus: focus; + radius: radius; + border-width: border; + material: premium; +} + +DragDropTarget:hover { + border: accent-border; + border-width: 2; +} + +Reorder { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: premium; +} + +Reorder[role=Handle] { + background: transparent; + foreground: muted; + border: transparent; + border-width: 0; + padding-x: 8; + gap: 4; + icon-size: 3; + offset-x: 36; + offset-y: 5; + opacity: 1; + material: premium; +} + +Reorder[role=Handle]:selected { + foreground: accent; +} + +Reorder[role=Placeholder] { + background: accent; + foreground: accent; + border: accent-border; + radius: radius; + border-width: 2; + padding-x: 3; + icon-size: 32; + offset-x: 34; + offset-y: 12; + opacity: 0.10; + material: premium; +} + +Selectable { + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + focus: focus; + radius: radius; + border-width: 0; + padding-x: space.3; + padding-y: 8; + font-size: font; + opacity: 1; + material: premium; +} + +Selectable:hover { + background: button-hover; + background-end: button-hover-end; + foreground: text; +} + +Selectable:pressed { + background: button-pressed; + background-end: button-pressed-end; + foreground: text; +} + +Selectable:selected { + background: accent; + background-end: accent-end; + foreground: accent-ink; +} + +Selectable:disabled { + foreground: muted; + opacity: 0.45; +} + +Fieldset { + icon-size: icon; + background: canvas; + foreground: muted; + border: border-soft; + radius: radius; + border-width: border; + padding-x: 8; + padding-y: 8; + gap: 9; + font-size: font; + opacity: 1; + material: app; +} + +PanedView { + background: transparent; + foreground: muted; + border: transparent; + opacity: 1; + material: app; +} + +PanedView[role=Handle] { + background: border-soft; + background-end: border; + foreground: muted; + border: transparent; + radius: radius; + border-width: 0; + icon-size: 8; + opacity: 1; + material: premium; +} + +PanedView[role=Handle]:hover { + background: border; + background-end: focus; +} + +PanedView[role=Handle]:pressed { + background: accent; + background-end: accent-end; +} + +Toast { + background: card; + background-end: card-end; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + padding-x: 14; + padding-y: 10; + gap: 18; + opacity: 0.98; + material: premium; +} + +Toast[role=Label] { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: app; +} + +Collapsible { + font-size: font.md; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: app; +} + +Collapsible[role=Header] { + background: button; + background-end: button-end; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + font-size: font; + padding-x: 6; + padding-y: 8; + icon-size: 20; + opacity: 1; + material: premium; +} + +Collapsible[role=Header]:hover { + background: button-hover; + background-end: button-hover-end; +} + +Collapsible[role=Header]:pressed { + background: button-pressed; + background-end: button-pressed-end; +} + +Collapsible[role=Header]:selected { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +Collapsible[role=Header]:disabled { + foreground: muted; + opacity: 0.45; +} + +Collapsible[role=TreeHeader] { + background: transparent; + foreground: text; + border: transparent; + radius: radius; + border-width: 0; + font-size: font; + icon-size: 20; + padding-x: 20; + padding-y: 8; + opacity: 1; + material: app; +} + +Collapsible[role=TreeHeader]:hover { + background: button; + background-end: button-end; + material: premium; +} + +Collapsible[role=TreeHeader]:selected { + background: transparent; + foreground: accent; +} + +Collapsible[role=TreeHeader]:disabled { + foreground: muted; + opacity: 0.45; +} + +Collapsible[role=Close] { + background: transparent; + foreground: muted; + border: transparent; + font-size: font; + icon-size: 28; + opacity: 1; + material: app; +} + +Collapsible[role=Close]:hover { + foreground: text; +} + +Collapsible[role=Close]:disabled { + opacity: 0.45; +} + +TitleBar { + font-size: font.md; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: app; +} + +TitleBar[role=Bar] { + background: surface; + background-end: surface-end; + foreground: text; + border: border-soft; + radius: 0; + border-width: border; + padding-x: 12; + gap: 4; + icon-size: 32; + offset-x: 60; + opacity: 1; + material: premium; +} + +TitleBar[role=Title] { + background: transparent; + foreground: text; + border: transparent; + font-size: font.md; + icon-size: 48; + padding-x: 16; + opacity: 1; + material: app; +} + +TitleBar[role=Action] { + background: transparent; + foreground: text; + border: transparent; + radius: radius; + border-width: 0; + icon-size: 20; + padding-x: 10; + opacity: 1; + material: app; +} + +TitleBar[role=Action]:hover { + background: button; + background-end: button-end; + material: premium; +} + +TitleBar[role=Action]:pressed { + background: button-pressed; + background-end: button-pressed-end; +} + +Toolbar { + font-size: font.md; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: app; +} + +Toolbar[role=Bar] { + background: surface; + background-end: surface-end; + foreground: text; + border: border-soft; + radius: 0; + border-width: border; + padding-x: 12; + opacity: 1; + material: premium; +} + +Toolbar[role=Divider] { + background: transparent; + foreground: muted; + border: border-soft; + border-width: border; + opacity: 1; + material: app; +} + +Toolbar[role=Action] { + background: transparent; + foreground: text; + border: transparent; + radius: radius; + border-width: 0; + padding-x: 8; + gap: 6; + icon-size: 20; + opacity: 1; + material: app; +} + +Toolbar[role=Action]:hover { + background: button; + background-end: button-end; + material: premium; +} + +Toolbar[role=Action]:pressed { + background: button-pressed; + background-end: button-pressed-end; +} + +Toolbar[role=BottomBar] { + padding-x: 24; + padding-y: 6; + gap: 8; + offset-x: 120; +} + +Toolbar[role=BottomAction] { + padding-x: 10; + gap: 12; + icon-size: 24; + offset-x: 16; + offset-y: 6; +} + +Modal { + font-size: font.md; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: app; +} + +Modal[role=Scrim] { + background: #000000; + foreground: text; + border: transparent; + opacity: 0.62; + material: app; +} + +Modal[role=Panel] { + background: card; + background-end: card-end; + foreground: text; + border: card-border; + focus: focus; + radius: radius; + border-width: border; + padding-x: 18; + padding-y: 18; + gap: 24; + icon-size: 280; + offset-x: 420; + offset-y: 58; + opacity: 1; + material: premium; +} + +Modal[role=Title] { + foreground: text; + font-size: font.md; + padding-x: 24; + padding-y: 18; + icon-size: 48; + offset-y: 14; + opacity: 1; + material: app; +} + +Modal[role=Message] { + foreground: text; + font-size: font.md; + padding-x: 120; + padding-y: 160; + gap: 18; + icon-size: 38; + offset-y: 18; + opacity: 1; + material: app; +} + +Modal[role=Action] { + background: button; + background-end: button-end; + foreground: text; + border: border; + focus: focus; + radius: radius; + border-width: border; + padding-x: 24; + gap: 8; + icon-size: 44; + offset-x: 88; + offset-y: 150; + opacity: 1; + material: premium; +} + +Modal[role=Action]:hover { + background: button-hover; + background-end: button-hover-end; +} + +Modal[role=Action]:pressed { + background: button-pressed; + background-end: button-pressed-end; +} + +Modal[role=Action][tone=Accent] { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +Modal[role=Close] { + background: transparent; + foreground: text; + border: transparent; + radius: radius; + border-width: 0; + padding-x: 8; + padding-y: 16; + gap: 6; + icon-size: 20; + offset-x: 120; + offset-y: 96; + opacity: 1; + material: app; +} + +Modal[role=Close]:hover { + background: button; + background-end: button-end; + material: premium; +} + +Modal[role=Close]:pressed { + background: button-pressed; + background-end: button-pressed-end; +} + +Guide { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: app; +} + +Guide[role=Scrim] { + background: #000000; + foreground: text; + border: transparent; + opacity: 0.36; + material: app; +} + +Guide[role=Bar] { + background: card; + background-end: card-end; + foreground: text; + border: card-border; + radius: 0; + border-width: 0; + padding-x: 12; + gap: 12; + icon-size: 48; + offset-x: 48; + opacity: 1; + material: premium; +} + +Guide[role=Divider] { + background: transparent; + foreground: text; + border: card-border; + border-width: border; + opacity: 1; + material: app; +} + +Guide[role=Panel] { + background: card; + background-end: card-end; + foreground: text; + border: card-border; + focus: focus; + radius: radius; + border-width: border; + padding-x: 12; + padding-y: 12; + gap: 20; + offset-x: 300; + offset-y: 112; + opacity: 1; + material: premium; +} + +Guide[role=Anchor] { + background: transparent; + foreground: accent; + border: accent-border; + border-width: border; + padding-x: 4; + opacity: 1; + material: app; +} + +Guide[role=Label] { + foreground: text; + font-size: font; + padding-x: 8; + padding-y: 6; + gap: 8; + opacity: 1; + material: app; +} + +Guide[role=Action] { + background: button; + background-end: button-end; + foreground: text; + border: border; + radius: radius; + border-width: border; + padding-x: 7; + gap: 8; + icon-size: 19; + offset-y: 34; + opacity: 1; + material: premium; +} + +Guide[role=Action]:hover { + background: button-hover; + background-end: button-hover-end; +} + +Guide[role=Close] { + background: transparent; + foreground: text; + border: transparent; + radius: radius; + border-width: 0; + padding-x: 6; + gap: 12; + icon-size: 16; + offset-y: 28; + opacity: 1; + material: app; +} + +Guide[role=Close]:hover { + background: button; + background-end: button-end; + material: premium; +} + +TableView { + font-size: font.md; + icon-size: icon; + background: transparent; + foreground: text; + border: transparent; + offset-y: 28; + opacity: 1; + material: app; +} + +TableView[role=Panel] { + background: surface; + background-end: surface-end; + foreground: text; + border: border-soft; + focus: focus; + radius: radius; + border-width: border; + opacity: 1; + material: premium; +} + +TableView[role=Header] { + background: button; + background-end: button-end; + foreground: text; + border: border; + radius: radius; + border-width: border; + padding-x: 6; + offset-y: 30; + font-size: font; + opacity: 1; + material: premium; +} + +TableView[role=Header]:hover { + background: button-hover; + background-end: button-hover-end; +} + +TableView[role=Header]:selected { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +TableView[role=Row] { + background: row; + foreground: text; + border: transparent; + radius: 0; + border-width: 0; + opacity: 1; + material: app; +} + +TableView[role=Cell] { + background: transparent; + foreground: text; + border: transparent; + offset-x: 32; + font-size: font; + opacity: 1; + material: app; +} + +TableView[role=Selection] { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; + radius: radius; + border-width: border; + opacity: 1; + material: premium; +} + +TableView[role=Divider] { + background: transparent; + foreground: muted; + border: border-soft; + border-width: border; + padding-x: 5; + offset-y: 8; + opacity: 1; + material: app; +} + +Plot { + icon-size: icon; + background: card; + background-end: card-end; + foreground: muted; + border: card-border; + radius: radius; + border-width: border; + padding-x: 6; + padding-y: 4; + font-size: font; + opacity: 1; + material: premium; +} + +PlotMark { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; + opacity: 1; + material: premium; +} + +Text { + foreground: text; + font-size: font; +} + +Page { + gap: 0; + padding-x: 0; +} + +Section { + gap: 0; + padding-x: 0; +} + +Heading { + foreground: text; + font-size: 24; +} + +ParagraphText { + foreground: text; + font-size: font; + gap: 0; +} + +Link { + icon-size: icon; + foreground: accent; + font-size: font; + opacity: 1; + material: premium; +} + +Link:hover { + foreground: accent-hover; +} + +Link:disabled { + foreground: muted; + opacity: 0.45; +} + +Separator { + opacity: 1; + material: app; + background: border; + foreground: muted; + gap: gap; +} + +Separator[role=Line] { + background: border; + foreground: muted; + gap: gap; +} + +Separator[role=Label] { + background: border; + foreground: muted; + font-size: font; + gap: gap; +} + +Separator[role=Bullet] { + background: border; + foreground: muted; + icon-size: 6; + gap: gap; +} + +Progress { + font-size: font.md; + icon-size: icon; + background: button; + background-end: button-end; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + opacity: 1; + material: premium; +} + +Progress[role=Track] { + background: button; + background-end: button-end; + foreground: text; + border: border-soft; + radius: radius; + border-width: border; + opacity: 1; + material: premium; +} + +Progress[role=Fill] { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; + opacity: 1; + material: premium; +} + +Progress[role=Label] { + background: transparent; + foreground: text; + border: transparent; + padding-x: 6; + font-size: font; + opacity: 1; + material: premium; +} + +@layer components; + +Button { + background: button; + background-end: button-end; + foreground: text; + border: border; + focus: focus; + radius: radius; + border-width: border; + padding-x: space.4; + padding-y: pad.control-y; + gap: gap; + font-size: font; + icon-size: icon; + opacity: 1; + material: premium; +} + +Button:hover { + background: button-hover; + background-end: button-hover-end; +} + +Button:pressed { + background: button-pressed; + background-end: button-pressed-end; +} + +Button:selected { + border: focus; + border-width: 2; +} + +Button:focus { + border: focus; +} + +Button:disabled { + foreground: muted; + opacity: 0.55; +} + +Button:loading { + foreground: accent; +} + +Button[tone=Accent] { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +Button[tone=Accent]:hover { + background: accent-hover; + background-end: accent-hover-end; +} + +Button[tone=Accent]:loading { + foreground: accent; +} + +Button[tone=Danger] { + background: danger; + background-end: danger; + foreground: danger-ink; + border: danger; +} + +Button[tone=Success] { + background: success; + background-end: success; + foreground: success-ink; + border: success; +} + +Button[tone=Warning] { + background: warning; + background-end: warning; + foreground: warning-ink; + border: warning; +} + +Button[emphasis=Soft] { + background: surface; + background-end: surface-end; + foreground: text; + border: border-soft; +} + +Button[tone=Accent][emphasis=Soft] { + background: accent-soft; + background-end: accent-soft-end; + foreground: accent; + border: accent-soft-end; +} + +Button[tone=Danger][emphasis=Soft] { + background: danger-soft; + background-end: danger-soft; + foreground: danger; + border: danger-soft; +} + +Button[tone=Success][emphasis=Soft] { + background: success-soft; + background-end: success-soft; + foreground: success; + border: success-soft; +} + +Button[tone=Warning][emphasis=Soft] { + background: warning-soft; + background-end: warning-soft; + foreground: warning; + border: warning-soft; +} + +Button[emphasis=Outline] { + background: transparent; + background-end: transparent; + foreground: text; + border: border; +} + +Button[tone=Accent][emphasis=Outline] { + foreground: accent; + border: accent; +} +Button[tone=Danger][emphasis=Outline] { + foreground: danger; + border: danger; +} +Button[tone=Success][emphasis=Outline] { + foreground: success; + border: success; +} +Button[tone=Warning][emphasis=Outline] { + foreground: warning; + border: warning; +} + +Button[emphasis=Ghost] { + background: transparent; + background-end: transparent; + foreground: text; + border: transparent; + border-width: 0; +} + +Button[tone=Accent][emphasis=Ghost] { foreground: accent; } +Button[tone=Danger][emphasis=Ghost] { foreground: danger; } +Button[tone=Success][emphasis=Ghost] { foreground: success; } +Button[tone=Warning][emphasis=Ghost] { foreground: warning; } + +Button[emphasis=Ghost]:hover { + background: button-hover; + background-end: button-hover-end; +} + +Button[emphasis=Link] { + background: transparent; + background-end: transparent; + foreground: accent; + border: transparent; + border-width: 0; +} + +Button[tone=Danger][emphasis=Link] { foreground: danger; } +Button[tone=Success][emphasis=Link] { foreground: success; } +Button[tone=Warning][emphasis=Link] { foreground: warning; } + +Button[tone=Accent][emphasis=Filled]:loading { + foreground: accent-ink; +} + +Dropdown { + icon-size: icon; + opacity: 1; + background: surface; + background-end: dropdown-end; + foreground: text; + border: border; + focus: focus; + radius: radius; + border-width: border; + padding-x: 14; + padding-y: pad.dropdown-y; + gap: gap; + offset-y: 16; + font-size: font; + material: premium; + offset-x: 24; +} + +Dropdown:hover { + background: button-hover; + background-end: button-hover-end; + border: focus; +} + +Dropdown:pressed { + background: button-pressed; + background-end: button-pressed-end; +} + +Dropdown:focus { + border: focus; +} + +Dropdown:disabled { + foreground: muted; + opacity: 0.55; +} + +Dropdown[tone=Accent] { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +Dropdown:selected { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +Dropdown[role=Panel] { + background: popup; + background-end: popup; + border: border; + padding-y: 4; + gap: 4; + offset-y: 16; +} + +Dropdown[role=Option] { + padding-x: 4; + padding-y: 2; +} + +Dropdown[role=Scrollbar] { + icon-size: 8; + padding-y: 2; + gap: 2; + offset-x: 8; +} + +Dropdown[role=Option]:hover { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +Dropdown[role=Option]:selected { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +Slider { + font-size: font.md; + icon-size: icon; + background: button-pressed; + background-end: button-pressed-end; + foreground: text; + border: border; + focus: focus; + radius: radius; + border-width: border; + opacity: 1; + material: premium; +} + +Slider[role=Track] { + padding-x: 32; + padding-y: 6; + gap: 14; + icon-size: 8; + offset-x: 36; + offset-y: 28; + opacity: 1; +} + +Slider[role=Label] { + foreground: text; + font-size: font; +} + +Slider:hover { + background: button-hover; + background-end: button-hover-end; +} + +Slider[role=Fill] { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +Slider:disabled { + foreground: muted; + opacity: 0.55; +} + +SliderThumb { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; + focus: accent; + radius: radius; + border-width: border; + gap: 8; + icon-size: 22; + opacity: 1; + material: premium; +} + +SliderThumb:disabled { + background: muted; + foreground: button; + border: border; + focus: border; + opacity: 0.55; +} + +Scroll { + background: button; + background-end: button-end; + foreground: text; + border: border-soft; + focus: focus; + radius: radius; + border-width: border; + icon-size: 10; + padding-x: 16; + padding-y: 8; + gap: 20; + offset-x: 5; + offset-y: 42; + opacity: 1; + material: premium; +} + +ScrollThumb { + background: muted; + foreground: surface; + border: border; + focus: focus; + radius: radius; + border-width: border; + icon-size: 16; + padding-x: 2; + opacity: 1; + material: premium; +} + +ScrollThumb:hover { + background: accent-hover; + background-end: accent-hover-end; + foreground: accent-ink; + border: accent-border; +} + +ScrollThumb:pressed { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +Toggle { + font-size: font.md; + icon-size: icon; + background: button; + background-end: button-end; + foreground: text; + border: border; + focus: focus; + radius: radius; + border-width: border; + opacity: 1; + material: premium; +} + +Toggle[role=Track] { + padding-x: 54; + padding-y: 32; + gap: 4; + radius: 999; + opacity: 1; +} + +Toggle[role=Label] { + foreground: text; + padding-x: 10; + font-size: font; + opacity: 1; +} + +Toggle:hover { + background: button-hover; + background-end: button-hover-end; +} + +Toggle:pressed { + background: button-pressed; + background-end: button-pressed-end; +} + +Toggle[role=Fill] { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; + radius: 999; + gap: 3; +} + +Toggle:disabled { + foreground: muted; + opacity: 0.55; +} + +ToggleThumb { + background: surface; + background-end: surface-end; + foreground: thumb-ink; + border: border; + focus: focus; + radius: radius; + border-width: border; + gap: 4; + icon-size: 20; + opacity: 1; + material: premium; +} + +ToggleThumb[tone=Accent] { + background: thumb-ink; + foreground: thumb-ink; + border: accent-border; + focus: accent; + icon-size: 24; +} + +ToggleThumb:disabled { + background: muted; + foreground: button; + border: border; + focus: border; + opacity: 0.55; +} + +Checkbox { + font-size: font.md; + background: surface; + background-end: surface-end; + foreground: text; + border: border; + focus: focus; + border-width: border; + padding-x: 22; + padding-y: 3; + gap: 4; + icon-size: 20; + opacity: 1; + material: premium; +} + +Checkbox:hover { + background: button-hover; + background-end: button-hover-end; +} + +Checkbox:pressed { + background: button-pressed; + background-end: button-pressed-end; +} + +Checkbox:selected { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; +} + +Checkbox:disabled { + foreground: muted; + opacity: 0.55; +} + +Checkbox[role=Box] { + background: surface; + background-end: surface-end; + foreground: text; + border: border; + focus: focus; + border-width: border; + padding-x: 22; + padding-y: 3; + gap: 4; + icon-size: 20; + opacity: 1; + material: premium; +} + +Checkbox[role=Mark] { + background: accent; + background-end: accent-end; + foreground: accent-ink; + border: accent-border; + focus: focus; + border-width: border; + padding-x: 5; + icon-size: 2.4; + opacity: 1; + material: premium; +} + +Checkbox[role=Label] { + foreground: text; + gap: 10; + font-size: font; + opacity: 1; + material: premium; +} + +Checkbox[role=Label]:disabled { + foreground: muted; + opacity: 0.55; +} + +Radio { + font-size: font.md; + icon-size: icon; + background: surface; + background-end: surface-end; + foreground: text; + border: border; + focus: focus; + border-width: border; + opacity: 1; + material: premium; +} + +Radio:hover { + border: focus; +} + +Radio:selected { + background: accent; + background-end: accent-end; + foreground: text; + border: accent-border; +} + +Radio:disabled { + foreground: muted; + opacity: 0.55; +} + +Radio[role=Ring] { + background: surface; + background-end: surface-end; + foreground: text; + border: border; + focus: focus; + border-width: border; + icon-size: 20; + padding-x: 40; + gap: 4; + opacity: 1; + material: premium; +} + +Radio[role=Mark] { + background: accent; + background-end: accent-end; + foreground: text; + border: accent-border; + focus: focus; + border-width: border; + font-size: font; + opacity: 1; + material: premium; +} + +Radio[role=Label] { + foreground: text; + font-size: font; + opacity: 1; + material: premium; +} + +Radio[role=Label]:disabled { + foreground: muted; + opacity: 0.55; +} + +TextField { + opacity: 1; + background: button; + background-end: button-end; + foreground: text; + border: border; + focus: focus; + radius: radius; + border-width: border; + padding-x: space.3; + padding-y: pad.field-y; + font-size: font.md; + material: premium; +} + +TextArea { + opacity: 1; + background: button; + background-end: button-end; + foreground: text; + border: border; + focus: focus; + radius: radius; + border-width: border; + padding-x: space.3; + padding-y: pad.control-y; + font-size: font.md; + gap: 4; + material: premium; +} diff --git a/styles/material.kss b/styles/material.kss new file mode 100644 index 0000000..0b3e23d --- /dev/null +++ b/styles/material.kss @@ -0,0 +1,2210 @@ +@pack material; + +tokens { + color { + canvas: #101217; + text: #f5f2ff; + muted: #8d919a; + surface: #151922; + card: #1a1f29; + panel: #202631; + panel-hover: #2a3140; + panel-pressed: #181d26; + border: #3a4250; + border-soft: #2a303b; + border-hover: #4a5363; + focus: #c9a8ff; + accent: #c9a8ff; + accent-hover: #d5bbff; + accent-ink: #171022; + danger: #f07178; + danger-ink: #220b0d; + danger-border: #ff9da2; + success: #60d6a4; + success-ink: #0d241a; + warning: #f3cb78; + warning-ink: #2a200c; + accent-soft: #322940; + danger-soft: #3c272d; + success-soft: #24392f; + warning-soft: #3d3527; + divider: #343b48; + row: #171c25; + track: #222832; + field-border: #2b3039; + accent-border: #d8c0ff; + border-hover-strong: #4b5361; + scroll-thumb: #6f7480; + scroll-border: #9ca2ad; + field: #11151d; + thumb-ink: #ffffff; + transparent: #00000000; + } + length { + radius.sm: 6; + radius.md: 8; + radius.lg: 10; + border: 1; + border.none: 0; + space.2: 8; + space.3: 12; + space.4: 16; + space.5: 20; + pad.control-y: 10; + pad.field-y: 9; + font.sm: 15; + font.md: 18; + font.lg: 20; + icon.md: 20; + } + material { + default: Flat; + } +} + +@layer base; + +App { + background: canvas; + foreground: text; + material: default; +} + +Surface { + background: surface; + foreground: text; + border: border-soft; + radius: radius.md; + border-width: border; + padding-x: space.4; + padding-y: 14; + gap: space.3; + material: default; +} + +Popup { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Popup[role=Panel] { + background: panel; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.lg; + border-width: border; + opacity: 1; + material: default; +} + +Canvas { + font-size: font.md; + icon-size: icon.md; + background: surface; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.md; + border-width: border; + opacity: 1; + material: default; +} + +Card { + background: surface; + foreground: text; + border: border; + radius: 16; + border-width: border; + padding-x: 18; + padding-y: space.4; + gap: space.3; + material: default; +} + +Image { + font-size: font.md; + icon-size: icon.md; + background: transparent; + foreground: #ffffff; + border: transparent; + focus: focus; + radius: 0; + border-width: border.none; + opacity: 1; + material: Flat; +} + +Image[role=Label] { + foreground: muted; + font-size: font.md; + opacity: 1; + material: default; +} + +Focus { + foreground: focus; + border: focus; + focus: focus; + radius: radius.md; + border-width: border; + opacity: 1; + material: default; +} + +Focus:focus { + foreground: focus; + border: focus; +} + +Focus[role=Box] { + foreground: muted; + border: border-hover; + border-width: 2; + padding-x: 3; + opacity: 1; + material: default; +} + +Focus[role=Box]:focus { + foreground: focus; + border: focus; +} + +Focus[role=Label] { + foreground: muted; + font-size: font.sm; + opacity: 1; + material: default; +} + +Focus[role=Label]:focus { + foreground: focus; +} + +NavigationBar { + background: surface; + foreground: text; + border: transparent; + radius: 0; + border-width: border.none; + padding-x: 0; + padding-y: 0; + gap: space.3; + icon-size: 80; + offset-x: 96; + offset-y: 56; + opacity: 1; + material: default; +} + +NavigationBar[role=Panel] { + offset-x: 340; + icon-size: 128; + padding-y: 58; +} + +NavigationBar[role=Row] { + icon-size: 58; + offset-y: 22; + padding-x: 36; + padding-y: 36; +} + +NavigationBar[role=Action] { + icon-size: 34; + offset-x: 180; + offset-y: 16; + padding-x: 92; + padding-y: 36; + gap: 8; +} + +NavigationBar[role=Divider] { + icon-size: 48; + offset-y: 8; + padding-y: 12; + gap: 8; +} + +NavigationBarItem { + background: transparent; + foreground: text; + border: transparent; + focus: focus; + radius: 16; + border-width: border.none; + padding-x: 3; + padding-y: 12; + gap: 4; + font-size: font.md; + icon-size: 24; + offset-x: 8; + opacity: 1; + material: default; +} + +NavigationBarItem:hover { + background: panel; + foreground: text; + padding-x: 64; + padding-y: 32; +} + +NavigationBarItem:selected { + background: accent; + foreground: accent-ink; + border: accent; + padding-x: 64; + padding-y: 32; +} + +NavigationBarItem:disabled { + foreground: muted; + opacity: 0.45; +} + +TabBar { + font-size: font.md; + background: surface; + foreground: text; + border: border-soft; + radius: radius.md; + border-width: border; + padding-x: 72; + icon-size: 48; + offset-x: 168; + gap: space.2; + opacity: 1; + material: default; +} + +Tab { + background: transparent; + foreground: muted; + border: transparent; + focus: focus; + radius: radius.lg; + border-width: border.none; + padding-x: space.3; + padding-y: space.2; + icon-size: 44; + font-size: font.md; + opacity: 1; + material: default; +} + +Tab:hover { + background: panel; + foreground: text; +} + +Tab:pressed { + background: panel-pressed; + foreground: text; +} + +Tab:selected { + background: accent; + foreground: accent-ink; + border: accent; +} + +Tab:disabled { + foreground: muted; + opacity: 0.45; +} + +TabClose { + background: transparent; + foreground: muted; + border: transparent; + radius: radius.sm; + border-width: border.none; + icon-size: 24; + opacity: 1; + material: default; +} + +TabClose:hover { + background: panel; + foreground: text; +} + +SegmentedControl { + font-size: font.md; + background: surface; + foreground: text; + border: border; + radius: 21; + border-width: border; + padding-x: 2; + padding-y: 2; + icon-size: 30; + offset-x: 72; + offset-y: 180; + gap: space.2; + opacity: 1; + material: default; +} + +Segment { + background: transparent; + foreground: text; + border: transparent; + focus: focus; + radius: 19; + border-width: border.none; + padding-x: space.3; + padding-y: 7; + offset-x: 20; + font-size: font.sm; + opacity: 1; + material: default; +} + +Segment:hover { + background: panel; + foreground: text; +} + +Segment:pressed { + background: panel-pressed; + foreground: text; +} + +Segment:selected { + background: accent; + foreground: accent-ink; + border: accent; +} + +Segment:disabled { + foreground: muted; + opacity: 0.45; +} + +Menu { + font-size: font.md; + icon-size: icon.md; + background: surface; + foreground: text; + border: border; + radius: radius.md; + border-width: border; + padding-x: space.2; + padding-y: space.2; + gap: space.2; + opacity: 1; + material: default; +} + +Menu[role=Bar] { + background: surface; + foreground: text; + border: border-soft; + padding-x: 12; + padding-y: 3; + gap: 2; +} + +Menu[role=Popup] { + background: card; + foreground: text; + border: border; + padding-x: 12; + gap: 4; + offset-x: 180; +} + +Menu[role=Context] { + background: card; + foreground: text; + border: border; +} + +MenuItem { + icon-size: icon.md; + background: transparent; + foreground: text; + border: transparent; + focus: focus; + radius: radius.sm; + border-width: border.none; + padding-x: space.3; + padding-y: space.2; + offset-x: 88; + offset-y: 30; + font-size: font.sm; + opacity: 1; + material: default; +} + +MenuItem:hover { + background: panel-hover; + foreground: text; +} + +MenuItem:pressed { + background: panel-pressed; + foreground: text; +} + +MenuItem:selected { + background: panel-hover; + foreground: text; +} + +MenuItem:disabled { + foreground: muted; + opacity: 0.45; +} + +MenuSeparator { + background: transparent; + foreground: border-soft; + border: border-soft; + radius: 0; + border-width: border; + padding-x: 12; + gap: 4; + icon-size: 32; + offset-x: 60; + opacity: 1; + material: default; +} + +Spinbox { + font-size: font.md; + icon-size: icon.md; + background: surface; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.md; + border-width: border; + opacity: 1; + material: default; +} + +SpinboxValue { + background: panel; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.sm; + border-width: border; + padding-x: space.3; + font-size: font.md; + opacity: 1; + material: default; +} + +Drag { + icon-size: icon.md; + background: surface; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.md; + border-width: border; + font-size: font.sm; + padding-x: 6; + padding-y: 4; + opacity: 1; + material: default; +} + +DragValue { + icon-size: icon.md; + background: panel; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.sm; + border-width: border; + padding-x: space.3; + font-size: font.sm; + opacity: 1; + material: default; +} + +DragValue:disabled { + background: surface; + foreground: muted; + border: border-soft; + opacity: 0.55; +} + +ColorPicker { + font-size: font.md; + opacity: 1; + material: default; + gap: 4; + icon-size: 36; + padding-y: 2; + offset-x: 20; + offset-y: 28; +} + +ColorPickerSwatch { + background: transparent; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.sm; + border-width: border; + padding-x: space.2; + font-size: font.sm; + opacity: 1; + material: default; +} + +ListBox { + font-size: font.md; + icon-size: icon.md; + background: surface; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.md; + border-width: border; + padding-x: space.2; + padding-y: space.2; + offset-y: 8; + gap: space.2; + opacity: 1; + material: default; +} + +ListBoxItem { + background: transparent; + foreground: text; + border: transparent; + focus: focus; + radius: radius.sm; + border-width: border.none; + padding-x: space.3; + padding-y: space.2; + icon-size: 30; + offset-y: 30; + font-size: font.sm; + opacity: 1; + material: default; +} + +ListBoxItem:hover { + background: panel-hover; + foreground: text; +} + +ListBoxItem:selected { + background: accent; + foreground: accent-ink; +} + +ListBoxItem:disabled { + foreground: muted; + opacity: 0.45; +} + +TreeView { + font-size: font.md; + icon-size: icon.md; + background: surface; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.md; + border-width: border; + padding-x: space.2; + padding-y: space.2; + gap: space.2; + opacity: 1; + material: default; +} + +TreeViewItem { + background: transparent; + foreground: text; + border: transparent; + focus: focus; + radius: radius.sm; + border-width: border.none; + padding-x: space.3; + padding-y: 7; + gap: 2; + icon-size: 16; + offset-x: 18; + offset-y: 28; + font-size: font.sm; + opacity: 1; + material: default; +} + +TreeViewItem:hover { + background: panel-hover; + foreground: text; +} + +TreeViewItem:selected { + background: accent; + foreground: accent-ink; +} + +TreeViewItem:disabled { + foreground: muted; + opacity: 0.45; +} + +ListBoxMulti { + font-size: font.md; + icon-size: icon.md; + background: surface; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.md; + border-width: border; + padding-x: space.2; + padding-y: space.2; + gap: space.2; + opacity: 1; + material: default; +} + +ListBoxMultiItem { + background: transparent; + foreground: text; + border: transparent; + focus: focus; + radius: radius.sm; + border-width: border.none; + padding-x: space.3; + padding-y: space.2; + icon-size: 28; + offset-y: 28; + font-size: font.sm; + opacity: 1; + material: default; +} + +ListBoxMultiItem:hover { + background: panel-hover; + foreground: text; +} + +ListBoxMultiItem:selected { + background: accent; + foreground: accent-ink; +} + +ListBoxMultiItem:focus { + border: focus; +} + +ListBoxMultiItem:disabled { + foreground: muted; + opacity: 0.45; +} + +DragDropTarget { + background: transparent; + border: border-hover; + focus: focus; + radius: radius.md; + border-width: border; + material: default; +} + +DragDropTarget:hover { + border: accent; + border-width: 2; +} + +Reorder { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Reorder[role=Handle] { + background: transparent; + foreground: muted; + border: transparent; + border-width: border.none; + padding-x: 8; + gap: 4; + icon-size: 3; + offset-x: 36; + offset-y: 5; + opacity: 1; + material: default; +} + +Reorder[role=Handle]:selected { + foreground: accent; +} + +Reorder[role=Placeholder] { + background: accent; + foreground: accent; + border: accent; + radius: radius.md; + border-width: 2; + padding-x: 3; + icon-size: 32; + offset-x: 34; + offset-y: 12; + opacity: 0.10; + material: default; +} + +Selectable { + icon-size: icon.md; + background: transparent; + foreground: text; + border: transparent; + focus: focus; + radius: radius.md; + border-width: border.none; + padding-x: space.2; + padding-y: space.2; + font-size: font.md; + opacity: 1; + material: default; +} + +Selectable:hover { + background: panel; + foreground: text; +} + +Selectable:pressed { + background: panel-pressed; + foreground: text; +} + +Selectable:selected { + background: accent; + foreground: accent-ink; +} + +Selectable:disabled { + foreground: muted; + opacity: 0.45; +} + +Fieldset { + icon-size: icon.md; + background: canvas; + foreground: muted; + border: border-soft; + radius: radius.md; + border-width: border; + padding-x: 8; + padding-y: 8; + gap: 9; + font-size: font.sm; + opacity: 1; + material: default; +} + +PanedView { + background: transparent; + foreground: muted; + border: transparent; + opacity: 1; + material: default; +} + +PanedView[role=Handle] { + background: border-soft; + foreground: muted; + border: transparent; + radius: radius.sm; + border-width: border.none; + icon-size: 8; + opacity: 1; + material: default; +} + +PanedView[role=Handle]:hover { + background: border-hover; +} + +PanedView[role=Handle]:pressed { + background: accent; +} + +Toast { + background: panel; + foreground: text; + border: border-soft; + radius: radius.md; + border-width: border; + padding-x: 14; + padding-y: 10; + gap: 18; + opacity: 0.98; + material: default; +} + +Toast[role=Label] { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Collapsible { + font-size: font.md; + icon-size: icon.md; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Collapsible[role=Header] { + background: panel; + foreground: text; + border: border-soft; + radius: radius.md; + border-width: border; + font-size: font.md; + icon-size: 20; + padding-x: 8; + padding-y: 8; + opacity: 1; + material: default; +} + +Collapsible[role=Header]:hover { + background: panel-hover; +} + +Collapsible[role=Header]:pressed { + background: panel-pressed; +} + +Collapsible[role=Header]:selected { + background: accent; + foreground: accent-ink; + border: accent; +} + +Collapsible[role=Header]:disabled { + foreground: muted; + opacity: 0.45; +} + +Collapsible[role=TreeHeader] { + background: transparent; + foreground: text; + border: transparent; + radius: radius.sm; + border-width: border.none; + font-size: font.md; + icon-size: 20; + padding-x: 20; + padding-y: 8; + opacity: 1; + material: default; +} + +Collapsible[role=TreeHeader]:hover { + background: panel; +} + +Collapsible[role=TreeHeader]:selected { + background: transparent; + foreground: accent; +} + +Collapsible[role=TreeHeader]:disabled { + foreground: muted; + opacity: 0.45; +} + +Collapsible[role=Close] { + background: transparent; + foreground: muted; + border: transparent; + font-size: font.md; + icon-size: 28; + opacity: 1; + material: default; +} + +Collapsible[role=Close]:hover { + foreground: text; +} + +Collapsible[role=Close]:disabled { + opacity: 0.45; +} + +TitleBar { + font-size: font.md; + icon-size: icon.md; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +TitleBar[role=Bar] { + background: canvas; + foreground: text; + border: border-soft; + radius: 0; + border-width: border.none; + padding-x: 12; + gap: 4; + icon-size: 32; + offset-x: 60; + opacity: 1; + material: default; +} + +TitleBar[role=Title] { + background: transparent; + foreground: text; + border: transparent; + font-size: font.lg; + icon-size: 48; + padding-x: 16; + opacity: 1; + material: default; +} + +TitleBar[role=Action] { + background: panel; + foreground: text; + border: transparent; + radius: radius.sm; + border-width: border.none; + icon-size: 24; + padding-x: 10; + opacity: 1; + material: default; +} + +TitleBar[role=Action]:hover { + background: panel-hover; +} + +TitleBar[role=Action]:pressed { + background: panel-pressed; +} + +Toolbar { + font-size: font.md; + icon-size: icon.md; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Toolbar[role=Bar] { + background: surface; + foreground: text; + border: border-soft; + radius: 0; + border-width: border; + padding-x: 12; + opacity: 1; + material: default; +} + +Toolbar[role=Divider] { + background: transparent; + foreground: muted; + border: border-soft; + border-width: border; + opacity: 1; + material: default; +} + +Toolbar[role=Action] { + background: transparent; + foreground: text; + border: transparent; + radius: radius.sm; + border-width: border.none; + padding-x: 8; + gap: 0; + icon-size: 20; + opacity: 1; + material: default; +} + +Toolbar[role=Action]:hover { + background: panel; +} + +Toolbar[role=Action]:pressed { + background: panel-pressed; +} + +Toolbar[role=BottomBar] { + padding-x: 24; + padding-y: 6; + gap: 8; + offset-x: 120; +} + +Toolbar[role=BottomAction] { + padding-x: 10; + gap: 12; + icon-size: 24; + offset-x: 16; + offset-y: 6; +} + +Modal { + font-size: font.md; + icon-size: icon.md; + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Modal[role=Scrim] { + background: #000000; + foreground: text; + border: transparent; + opacity: 0.62; + material: default; +} + +Modal[role=Panel] { + background: panel; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.lg; + border-width: border; + padding-x: 18; + padding-y: 18; + gap: 24; + icon-size: 280; + offset-x: 420; + offset-y: 58; + opacity: 1; + material: default; +} + +Modal[role=Title] { + foreground: text; + font-size: font.lg; + padding-x: 24; + padding-y: 18; + icon-size: 48; + offset-y: 14; + opacity: 1; + material: default; +} + +Modal[role=Message] { + foreground: text; + font-size: font.md; + padding-x: 120; + padding-y: 160; + gap: 18; + icon-size: 38; + offset-y: 18; + opacity: 1; + material: default; +} + +Modal[role=Action] { + background: surface; + foreground: text; + border: border-hover; + focus: focus; + radius: radius.md; + border-width: border; + padding-x: 24; + gap: 8; + icon-size: 44; + offset-x: 88; + offset-y: 150; + opacity: 1; + material: default; +} + +Modal[role=Action]:hover { + background: panel-hover; +} + +Modal[role=Action]:pressed { + background: panel-pressed; +} + +Modal[role=Action][tone=Accent] { + background: accent; + foreground: accent-ink; + border: accent; +} + +Modal[role=Action][tone=Danger] { + background: danger; + foreground: danger-ink; + border: danger-border; +} + +Modal[role=Close] { + background: transparent; + foreground: text; + border: transparent; + radius: radius.sm; + border-width: border.none; + padding-x: 8; + padding-y: 16; + gap: 6; + icon-size: 20; + offset-x: 120; + offset-y: 96; + opacity: 1; + material: default; +} + +Modal[role=Close]:hover { + background: panel-hover; +} + +Modal[role=Close]:pressed { + background: panel-pressed; +} + +Guide { + background: transparent; + foreground: text; + border: transparent; + opacity: 1; + material: default; +} + +Guide[role=Scrim] { + background: #000000; + foreground: text; + border: transparent; + opacity: 0.34; + material: default; +} + +Guide[role=Bar] { + background: panel; + foreground: text; + border: border-soft; + radius: 0; + border-width: border.none; + padding-x: 12; + gap: 12; + icon-size: 48; + offset-x: 48; + opacity: 1; + material: default; +} + +Guide[role=Divider] { + background: transparent; + foreground: text; + border: border-soft; + border-width: border; + opacity: 1; + material: default; +} + +Guide[role=Panel] { + background: panel; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.lg; + border-width: border; + padding-x: 12; + padding-y: 12; + gap: 20; + offset-x: 300; + offset-y: 112; + opacity: 1; + material: default; +} + +Guide[role=Anchor] { + background: transparent; + foreground: accent; + border: accent; + border-width: border; + padding-x: 4; + opacity: 1; + material: default; +} + +Guide[role=Label] { + foreground: text; + font-size: font.md; + padding-x: 8; + padding-y: 6; + gap: 8; + opacity: 1; + material: default; +} + +Guide[role=Action] { + background: surface; + foreground: text; + border: border-hover; + radius: radius.md; + border-width: border; + padding-x: 7; + gap: 8; + icon-size: 19; + offset-y: 34; + opacity: 1; + material: default; +} + +Guide[role=Action]:hover { + background: panel-hover; +} + +Guide[role=Close] { + background: transparent; + foreground: text; + border: transparent; + radius: radius.sm; + border-width: border.none; + padding-x: 6; + gap: 12; + icon-size: 16; + offset-y: 28; + opacity: 1; + material: default; +} + +Guide[role=Close]:hover { + background: panel-hover; +} + +TableView { + font-size: font.md; + icon-size: icon.md; + background: transparent; + foreground: text; + border: transparent; + offset-y: 28; + opacity: 1; + material: default; +} + +TableView[role=Panel] { + background: surface; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.md; + border-width: border; + opacity: 1; + material: default; +} + +TableView[role=Header] { + background: panel; + foreground: text; + border: border-soft; + radius: radius.sm; + border-width: border; + padding-x: 6; + offset-y: 30; + font-size: font.sm; + opacity: 1; + material: default; +} + +TableView[role=Header]:hover { + background: panel-hover; +} + +TableView[role=Header]:selected { + background: accent; + foreground: accent-ink; + border: accent; +} + +TableView[role=Row] { + background: row; + foreground: text; + border: transparent; + radius: 0; + border-width: border.none; + opacity: 1; + material: default; +} + +TableView[role=Cell] { + background: transparent; + foreground: text; + border: transparent; + offset-x: 32; + font-size: font.sm; + opacity: 1; + material: default; +} + +TableView[role=Selection] { + background: accent; + foreground: accent-ink; + border: accent; + radius: radius.sm; + border-width: border; + opacity: 1; + material: default; +} + +TableView[role=Selection]:hover { + background: accent-hover; +} + +TableView[role=Divider] { + background: transparent; + foreground: muted; + border: border-soft; + border-width: border; + padding-x: 5; + offset-y: 8; + opacity: 1; + material: default; +} + +Plot { + icon-size: icon.md; + background: card; + foreground: muted; + border: border-soft; + radius: radius.md; + border-width: border; + padding-x: 6; + padding-y: 4; + font-size: font.sm; + opacity: 1; + material: default; +} + +PlotMark { + background: accent; + foreground: accent-ink; + border: accent; + opacity: 1; + material: default; +} + +Text { + foreground: text; + font-size: font.md; + opacity: 1; +} + +Text.muted { + foreground: muted; +} + +Page { + gap: 0; + padding-x: 0; +} + +Section { + gap: 0; + padding-x: 0; +} + +Heading { + foreground: text; + font-size: 24; + opacity: 1; +} + +ParagraphText { + foreground: text; + font-size: font.md; + gap: space.2; + opacity: 1; +} + +Link { + icon-size: icon.md; + foreground: accent; + font-size: font.md; + opacity: 1; + material: default; +} + +Link:hover { + foreground: accent-hover; +} + +Link:disabled { + foreground: muted; + opacity: 0.45; +} + +Separator { + material: default; + background: divider; + foreground: muted; + gap: space.3; + opacity: 1; +} + +Separator[role=Line] { + background: divider; + foreground: muted; + gap: space.3; + opacity: 1; +} + +Separator[role=Label] { + background: divider; + foreground: muted; + font-size: font.sm; + gap: space.3; + opacity: 1; +} + +Separator[role=Bullet] { + background: divider; + foreground: muted; + icon-size: 6; + gap: space.3; + opacity: 1; +} + +Progress { + font-size: font.md; + icon-size: icon.md; + background: track; + foreground: text; + border: border-soft; + radius: radius.md; + border-width: border; + opacity: 1; + material: default; +} + +Progress[role=Track] { + background: track; + foreground: text; + border: border-soft; + radius: radius.md; + border-width: border; + opacity: 1; + material: default; +} + +Progress[role=Fill] { + background: accent; + foreground: accent-ink; + border: accent; + opacity: 1; + material: default; +} + +Progress[role=Label] { + background: transparent; + foreground: text; + border: transparent; + padding-x: 6; + font-size: font.sm; + opacity: 1; + material: default; +} + +@layer components; + +Button { + background: panel; + background-end: panel; + foreground: text; + border: border; + focus: focus; + radius: radius.md; + border-width: border; + padding-x: space.4; + padding-y: pad.control-y; + gap: space.2; + font-size: font.md; + icon-size: icon.md; + opacity: 1; + material: default; +} + +Button:hover { + background: panel-hover; + background-end: panel-hover; + border: border-hover; +} + +Button:pressed { + background: panel-pressed; + background-end: panel-pressed; +} + +Button:selected { + border: focus; + border-width: 2; +} + +Button:focus { + border: focus; +} + +Button:disabled { + foreground: muted; + border: field-border; + opacity: 0.55; +} + +Button:loading { + foreground: accent; +} + +Button[tone=Accent] { + background: accent; + background-end: accent; + foreground: accent-ink; + border: accent-border; +} + +Button[tone=Accent]:hover { + background: accent-hover; + background-end: accent-hover; +} + +Button[tone=Accent]:focus { + border: focus; +} + +Button[tone=Accent]:loading { + foreground: accent; +} + +Button[tone=Danger] { + background: danger; + background-end: danger; + foreground: danger-ink; + border: danger-border; +} + +Button[tone=Success] { + background: success; + background-end: success; + foreground: success-ink; + border: success; +} + +Button[tone=Warning] { + background: warning; + background-end: warning; + foreground: warning-ink; + border: warning; +} + +Button[emphasis=Soft] { + background: panel; + background-end: panel; + foreground: text; + border: border-soft; +} + +Button[emphasis=Soft]:hover { + background: panel-hover; + background-end: panel-hover; +} + +Button[emphasis=Soft]:pressed { + background: panel-pressed; + background-end: panel-pressed; +} + +Button[tone=Accent][emphasis=Soft] { + background: accent-soft; + background-end: accent-soft; + foreground: accent; + border: accent-soft; +} + +Button[tone=Danger][emphasis=Soft] { + background: danger-soft; + background-end: danger-soft; + foreground: danger; + border: danger-soft; +} + +Button[tone=Success][emphasis=Soft] { + background: success-soft; + background-end: success-soft; + foreground: success; + border: success-soft; +} + +Button[tone=Warning][emphasis=Soft] { + background: warning-soft; + background-end: warning-soft; + foreground: warning; + border: warning-soft; +} + +Button[emphasis=Outline] { + background: transparent; + background-end: transparent; + foreground: text; + border: border-hover-strong; +} + +Button[emphasis=Ghost] { + background: transparent; + background-end: transparent; + foreground: text; + border: transparent; + border-width: border.none; +} + +Button[tone=Accent][emphasis=Outline] { + foreground: accent; + border: accent; +} + +Button[tone=Accent][emphasis=Ghost] { + foreground: accent; +} + +Button[tone=Danger][emphasis=Outline] { + foreground: danger; + border: danger; +} + +Button[tone=Danger][emphasis=Ghost] { + foreground: danger; +} + +Button[tone=Success][emphasis=Outline] { + foreground: success; + border: success; +} + +Button[tone=Success][emphasis=Ghost] { foreground: success; } + +Button[tone=Warning][emphasis=Outline] { + foreground: warning; + border: warning; +} + +Button[tone=Warning][emphasis=Ghost] { foreground: warning; } + +Button[emphasis=Link] { + background: transparent; + background-end: transparent; + foreground: accent; + border: transparent; + border-width: border.none; +} + +Button[tone=Danger][emphasis=Link] { foreground: danger; } +Button[tone=Success][emphasis=Link] { foreground: success; } +Button[tone=Warning][emphasis=Link] { foreground: warning; } + +Button[emphasis=Link]:hover { foreground: accent-hover; } +Button[tone=Danger][emphasis=Link]:hover { foreground: danger-border; } + +Button[tone=Accent][emphasis=Filled]:loading { + foreground: accent-ink; +} + +Button[emphasis=Ghost]:hover { + background: panel-hover; + background-end: panel-hover; +} + +Button[emphasis=Ghost]:pressed { + background: panel-pressed; + background-end: panel-pressed; +} + +Button[emphasis=Outline]:hover { + background: panel-hover; + background-end: panel-hover; +} + +Button[emphasis=Outline]:pressed { + background: panel-pressed; + background-end: panel-pressed; +} + +Button[size=Small] { + radius: radius.sm; + padding-x: space.3; + padding-y: 7; + font-size: font.sm; + icon-size: 18; +} + +Button[size=Large] { + radius: radius.lg; + padding-x: space.5; + padding-y: 13; + font-size: font.lg; + icon-size: 22; +} + +Dropdown { + icon-size: icon.md; + opacity: 1; + background: row; + foreground: text; + border: border; + focus: focus; + radius: 8; + border-width: 1; + padding-x: 14; + padding-y: 9; + gap: 8; + offset-y: 16; + font-size: font.md; + material: Flat; + offset-x: 24; +} + +Dropdown:hover { + background: panel-hover; + border: border-hover; +} + +Dropdown:pressed { + background: panel-pressed; +} + +Dropdown:focus { + border: focus; +} + +Dropdown:disabled { + foreground: muted; + opacity: 0.55; +} + +Dropdown[tone=Accent] { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Dropdown:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Dropdown[role=Panel] { + background: panel; + border: border; + padding-y: 4; + gap: 4; + offset-y: 16; +} + +Dropdown[role=Option] { + padding-x: 4; + padding-y: 2; +} + +Dropdown[role=Scrollbar] { + icon-size: 8; + padding-y: 2; + gap: 2; + offset-x: 8; +} + +Dropdown[role=Option]:hover { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Dropdown[role=Option]:selected { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Slider { + font-size: font.md; + icon-size: icon.md; + background: border-soft; + foreground: text; + border: border; + focus: focus; + radius: radius.lg; + border-width: border; + opacity: 1; + material: default; +} + +Slider[role=Track] { + padding-x: 32; + padding-y: 6; + gap: 14; + icon-size: 8; + offset-x: 36; + offset-y: 28; + opacity: 1; +} + +Slider[role=Label] { + foreground: text; + font-size: font.md; +} + +Slider:hover { + background: divider; +} + +Slider:pressed { + background: panel; +} + +Slider[role=Fill] { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Slider:disabled { + foreground: muted; + opacity: 0.5; +} + +SliderThumb { + background: accent; + foreground: accent-ink; + border: accent-border; + focus: accent; + radius: radius.lg; + border-width: border; + gap: 8; + icon-size: 22; + opacity: 1; + material: flat; +} + +SliderThumb:disabled { + background: muted; + foreground: surface; + border: border; + focus: border; + opacity: 0.5; +} + +Scroll { + background: panel; + foreground: text; + border: border-soft; + focus: focus; + radius: radius.lg; + border-width: border; + icon-size: 10; + padding-x: 16; + padding-y: 8; + gap: 20; + offset-x: 5; + offset-y: 42; + opacity: 1; + material: default; +} + +ScrollThumb { + background: scroll-thumb; + foreground: surface; + border: scroll-border; + focus: focus; + radius: radius.lg; + border-width: border; + icon-size: 16; + padding-x: 2; + opacity: 1; + material: default; +} + +ScrollThumb:hover { + background: accent-hover; + foreground: accent-ink; + border: accent-border; +} + +ScrollThumb:pressed { + background: accent; + foreground: accent-ink; + border: accent-border; +} + +Toggle { + font-size: font.md; + icon-size: icon.md; + background: border-soft; + foreground: text; + border: border; + focus: focus; + radius: radius.lg; + border-width: border; + opacity: 1; + material: default; +} + +Toggle[role=Track] { + padding-x: 54; + padding-y: 32; + gap: 4; + radius: 999; + opacity: 1; +} + +Toggle[role=Label] { + foreground: text; + padding-x: 10; + font-size: font.md; + opacity: 1; +} + +Toggle:hover { + background: divider; +} + +Toggle:pressed { + background: panel; +} + +Toggle[role=Fill] { + background: accent; + foreground: accent-ink; + border: accent-border; + radius: 999; + gap: 3; +} + +Toggle:disabled { + foreground: muted; + opacity: 0.5; +} + +ToggleThumb { + background: surface; + foreground: thumb-ink; + border: border-hover; + focus: focus; + radius: radius.lg; + border-width: border; + gap: 4; + icon-size: 20; + opacity: 1; + material: flat; +} + +ToggleThumb[tone=Accent] { + background: thumb-ink; + foreground: thumb-ink; + border: accent-border; + focus: accent; + icon-size: 24; +} + +ToggleThumb:disabled { + background: muted; + foreground: surface; + border: border; + focus: border; + opacity: 0.5; +} + +Checkbox { + font-size: font.md; + background: surface; + foreground: text; + border: border-hover; + focus: focus; + border-width: border; + padding-x: 22; + padding-y: 3; + gap: 4; + icon-size: 20; + opacity: 1; + material: default; +} + +Checkbox:hover { + background: panel; +} + +Checkbox:pressed { + background: panel-pressed; +} + +Checkbox:selected { + background: accent; + foreground: accent-ink; + border: accent; +} + +Checkbox:disabled { + background: surface; + foreground: muted; + border: border-soft; + opacity: 0.55; +} + +Checkbox[role=Box] { + background: surface; + foreground: text; + border: border-hover; + focus: focus; + border-width: border; + padding-x: 22; + padding-y: 3; + gap: 4; + icon-size: 20; + opacity: 1; + material: default; +} + +Checkbox[role=Mark] { + background: accent; + foreground: accent-ink; + border: accent; + focus: focus; + border-width: border; + padding-x: 5; + icon-size: 2.4; + opacity: 1; + material: default; +} + +Checkbox[role=Label] { + foreground: text; + gap: 10; + font-size: font.md; + opacity: 1; + material: default; +} + +Checkbox[role=Label]:disabled { + foreground: muted; + opacity: 0.55; +} + +Radio { + font-size: font.md; + icon-size: icon.md; + background: surface; + foreground: text; + border: border-hover; + focus: focus; + border-width: border; + opacity: 1; + material: default; +} + +Radio:hover { + border: focus; +} + +Radio:selected { + background: accent; + foreground: text; + border: accent; +} + +Radio:disabled { + foreground: muted; + border: border-soft; + opacity: 0.55; +} + +Radio[role=Ring] { + background: surface; + foreground: text; + border: border-hover; + focus: focus; + border-width: border; + icon-size: 20; + padding-x: 40; + gap: 4; + opacity: 1; + material: default; +} + +Radio[role=Mark] { + background: accent; + foreground: text; + border: accent; + focus: focus; + border-width: border; + font-size: font.md; + opacity: 1; + material: default; +} + +Radio[role=Label] { + foreground: text; + font-size: font.md; + opacity: 1; + material: default; +} + +Radio[role=Label]:disabled { + foreground: muted; + opacity: 0.55; +} + +TextField { + opacity: 1; + background: field; + foreground: text; + border: border; + focus: focus; + radius: 8; + border-width: 1; + padding-x: 12; + padding-y: 9; + font-size: 16; + material: Flat; +} + +TextArea { + opacity: 1; + background: field; + foreground: text; + border: border; + focus: focus; + radius: 8; + border-width: 1; + padding-x: 12; + padding-y: 10; + font-size: 16; + gap: 6; + material: Flat; +} diff --git a/tests/kss_corpus/block-directives.kss b/tests/kss_corpus/block-directives.kss new file mode 100644 index 0000000..6732c55 --- /dev/null +++ b/tests/kss_corpus/block-directives.kss @@ -0,0 +1,8 @@ +@pack corpus; +@media (min-width: 10px) { + Button { gap: 1; } +} +@font-face { + src: x; +} +Text { gap: 2; } diff --git a/tests/kss_corpus/comment-in-string.kss b/tests/kss_corpus/comment-in-string.kss new file mode 100644 index 0000000..cd21b76 --- /dev/null +++ b/tests/kss_corpus/comment-in-string.kss @@ -0,0 +1,3 @@ +Button { gap: 1; } +/**/ +Text { typeface: "/*"; } diff --git a/tests/kss_corpus/import-kept.ok.kss b/tests/kss_corpus/import-kept.ok.kss new file mode 100644 index 0000000..99a7d5e --- /dev/null +++ b/tests/kss_corpus/import-kept.ok.kss @@ -0,0 +1,3 @@ +@pack corpus; +@import ; +Button { gap: 1; } diff --git a/tests/kss_corpus/import-target-braces.kss b/tests/kss_corpus/import-target-braces.kss new file mode 100644 index 0000000..43c3204 --- /dev/null +++ b/tests/kss_corpus/import-target-braces.kss @@ -0,0 +1,2 @@ +@pack corpus; +@import<{;}>; diff --git a/tests/kss_corpus/import-target-line.kss b/tests/kss_corpus/import-target-line.kss new file mode 100644 index 0000000..0403538 --- /dev/null +++ b/tests/kss_corpus/import-target-line.kss @@ -0,0 +1,6 @@ +@pack corpus; +Button { + gap: 1; +} +@import< + s>; diff --git a/tests/kss_corpus/many-blocks.ok.kss b/tests/kss_corpus/many-blocks.ok.kss new file mode 100644 index 0000000..6decf6d --- /dev/null +++ b/tests/kss_corpus/many-blocks.ok.kss @@ -0,0 +1,42 @@ +@pack corpus; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +@layer app; +Button { gap: 2; } diff --git a/tests/kss_corpus/nested-braces.kss b/tests/kss_corpus/nested-braces.kss new file mode 100644 index 0000000..b2bcac7 --- /dev/null +++ b/tests/kss_corpus/nested-braces.kss @@ -0,0 +1,2 @@ +@n{{;}} +@m{{{;}}} diff --git a/tests/kss_corpus/quote-line-break.kss b/tests/kss_corpus/quote-line-break.kss new file mode 100644 index 0000000..26670e0 --- /dev/null +++ b/tests/kss_corpus/quote-line-break.kss @@ -0,0 +1,3 @@ +" +"//"{{p:1;} +{x:0} diff --git a/tests/kss_corpus/unclosed-comment.kss b/tests/kss_corpus/unclosed-comment.kss new file mode 100644 index 0000000..138c774 --- /dev/null +++ b/tests/kss_corpus/unclosed-comment.kss @@ -0,0 +1,2 @@ +A{} +/*xy \ No newline at end of file diff --git a/tests/kss_corpus/variant-labels.ok.kss b/tests/kss_corpus/variant-labels.ok.kss new file mode 100644 index 0000000..2018138 --- /dev/null +++ b/tests/kss_corpus/variant-labels.ok.kss @@ -0,0 +1,5 @@ +@pack corpus; +@variant e "{" { Button { gap: 1; } } +@variant f "a; b" { Button { gap: 2; } } +@variant g "close }" { Button { gap: 3; } } +@variant h "/* not a comment" { Button { gap: 4; } } diff --git a/tests/kss_fuzz.sh b/tests/kss_fuzz.sh new file mode 100644 index 0000000..8700c55 --- /dev/null +++ b/tests/kss_fuzz.sh @@ -0,0 +1,69 @@ +#!/bin/sh +# Build tests/kss_fuzz.zi with AddressSanitizer and UndefinedBehaviorSanitizer +# and run it over the shipped style packs. +# +# sh tests/kss_fuzz.sh SEED COUNT COUNT cases from SEED +# sh tests/kss_fuzz.sh check FILE the checks on one saved sheet +# sh tests/kss_fuzz.sh shrink FILE the smallest part of FILE that still +# fails, kept as build/kss-fuzz/shrunk.kss +# +# A failing sheet is kept as build/kss-fuzz/failure-SEED.kss. +set -eu + +repo=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +ziran=${ZIRAN:-ziran} +timeout=${FUZZ_TIMEOUT:-600} +if test "$#" -ne 2; then + echo "usage: $0 SEED COUNT | check FILE | shrink FILE" >&2 + exit 2 +fi + +cd "$repo" +toolchain=$("$ziran" pkg path ziran) +out=$repo/build/kss-fuzz +mkdir -p "$out" +build=$(mktemp -d "$out/build.XXXXXX") +trap 'rm -rf "$build"' EXIT HUP INT TERM +"$ziran" build --project --target=c --entry kss_fuzz:main \ + -o "$build/c" "$repo/tests/kss_fuzz.zi" +"${CC:-cc}" -std=c11 -O1 -g -fno-omit-frame-pointer \ + -fsanitize=address,undefined -fno-sanitize-recover=all \ + -I"$toolchain/include" -I"$build/c" "$build/c"/*.c \ + "$toolchain/build/libziran.a" -o "$build/kss_fuzz" + +# The parser passes its large state by value; give the sanitized stack room. +ulimit -s unlimited 2>/dev/null || ulimit -s 1048576 2>/dev/null || true +current=$build/current.kss +if test "$1" = check; then + env -u DISPLAY -u WAYLAND_DISPLAY ASAN_OPTIONS=detect_leaks=0 \ + "$build/kss_fuzz" "$current" check "$2" + exit +fi +if test "$1" = shrink; then + env -u DISPLAY -u WAYLAND_DISPLAY ASAN_OPTIONS=detect_leaks=0 \ + "$build/kss_fuzz" "$current" shrink "$2" + if test -f "$current"; then cp "$current" "$out/shrunk.kss"; fi + exit +fi +status=0 +# Sheets that once failed come first; see tests/kss_corpus. +env -u DISPLAY -u WAYLAND_DISPLAY ASAN_OPTIONS=detect_leaks=0 \ + timeout "$timeout" "$build/kss_fuzz" "$current" corpus \ + "$repo"/tests/kss_corpus/*.kss || status=$? +if test "$status" -ne 0; then + echo "kss fuzz: a corpus sheet failed; it is kept in build/kss-fuzz/failure-corpus.kss" >&2 + cp "$current" "$out/failure-corpus.kss" + exit 1 +fi +env -u DISPLAY -u WAYLAND_DISPLAY ASAN_OPTIONS=detect_leaks=0 \ + timeout "$timeout" "$build/kss_fuzz" "$current" "$1" "$2" \ + "$repo"/styles/*.kss || status=$? +if test "$status" -ne 0; then + cp "$current" "$out/failure-$1.kss" + if test "$status" -eq 124; then + echo "kss fuzz: seed $1 ran longer than $timeout seconds" >&2 + fi + echo "kss fuzz: failing sheet kept in build/kss-fuzz/failure-$1.kss" >&2 + echo "kss fuzz: rerun it with sh tests/kss_fuzz.sh check build/kss-fuzz/failure-$1.kss" >&2 + exit 1 +fi diff --git a/tests/kss_fuzz.zi b/tests/kss_fuzz.zi new file mode 100644 index 0000000..ef328cd --- /dev/null +++ b/tests/kss_fuzz.zi @@ -0,0 +1,1413 @@ +// KSS fuzz: feed generated and damaged style sheets to the parser, the style +// rule table, and the formatter, and check what they promise. +// +// kss_fuzz CURRENT SEED COUNT PACK... run COUNT cases made from SEED +// kss_fuzz CURRENT check FILE run the checks on one file +// kss_fuzz CURRENT corpus FILE... run the checks on each file; one +// named *.ok.kss must also parse +// kss_fuzz CURRENT shrink FILE cut FILE down to a small sheet that +// still fails, and write it to CURRENT +// +// Each case is either a random sheet assembled from the KSS grammar or a +// shipped PACK with random damage: flipped bytes, inserted directives and +// blocks, deleted, repeated, or spliced ranges, and cut-off endings. Every +// case is checked for: +// - no abort in the typed parser, the declarative parser, or the formatter +// (Ziran stops a program on an out-of-range index or cast); +// - parser state that stays inside its tables after every step; +// - a parse that ends: at most one step per source byte, plus imports; +// - an error that says what went wrong; +// - formatting that keeps meaning: the formatted sheet parses to the same +// status, rules, tokens, and declarations as the original; +// - formatting that only moves whitespace: every other byte stays, in order; +// - formatting that is stable: formatting the output again changes nothing. +// +// Before each case the input is written to CURRENT, so when a check fails or +// the program is killed, CURRENT holds the sheet that did it. Exit status 1 +// means a check failed. +#import "byte_text_linux" +#import "c_string" +#import "file_linux" +#import "kss_formatter" +#import "kss_parser" +#import "style_parse" +#import "kryon/style" +#import "kryon/style_sheet" + +using KssStatus; + +libc :: #system_library "libc"; +RawWrite :: (descriptor: s32, input: *u8, count: u64) -> s64 #foreign libc "write"; +RawExit :: (status: s32) #foreign libc "_exit"; +RawFork :: () -> s32 #foreign libc "fork"; +RawWaitPid :: (pid: s32, status: *s32, options: s32) -> s32 #foreign libc "waitpid"; +RawOpen :: (path: *u8, flags: s32, mode: ..any) -> s32 #foreign libc "open"; +RawDup2 :: (source: s32, destination: s32) -> s32 #foreign libc "dup2"; + +// ------------------------------------------------------------------ output + +WriteText :: (descriptor: s32, text: string) { + chunk: [512]u8 + at: s32 = 0 + while at < cast(s32)text.count { + count: s32 = cast(s32)text.count - at + if count > 512 { count = 512 } + index: s32 = 0 + while index < count { + chunk[index] = text[at + index] + index += 1 + } + if RawWrite(descriptor, *chunk[0], cast(u64)count) <= 0 { return } + at += count + } +} + +NumberText :: (value: s64, output: []u8) -> string { + at: s32 = cast(s32)output.count + magnitude: s64 = value + if magnitude < 0 { magnitude = -magnitude } + while true { + at -= 1 + output[at] = cast(u8)(48 + magnitude % 10) + magnitude = magnitude / 10 + if magnitude == 0 { break } + } + if value < 0 { + at -= 1 + output[at] = 45 + } + return TextView(output[at:cast(s32)output.count]) +} + +WriteNumber :: (descriptor: s32, value: s64) { + digits: [24]u8 + WriteText(descriptor, NumberText(value, digits[:])) +} + +case_seed: s64 = 0; +case_index: s32 = 0; +shrinking: bool = false; + +// The typed parse status of the last case. +last_status: s32 = 0; + +// How far the cases got, so a run shows what it covered. +parsed_count: s64 = 0; +compared_count: s64 = 0; + +// Reports a failed check with the case that failed and stops with status 1. +// CURRENT still holds the input. +Fail :: (message: string) { + WriteText(2, "kss fuzz: seed ") + WriteNumber(2, case_seed) + WriteText(2, " case ") + WriteNumber(2, cast(s64)case_index) + WriteText(2, ": ") + WriteText(2, message) + WriteText(2, "\n") + if shrinking { + // The status names the check, so shrinking keeps the same failure. + hash: u32 = 2166136261 + index: s32 = 0 + while index < cast(s32)message.count { + hash = (hash ^ cast(u32)message[index]) * 16777619 + index += 1 + } + RawExit(cast(s32)(10 + hash % 200)) + } + RawExit(1) +} + +Check :: (condition: bool, message: string) { + if !condition { Fail(message) } +} + +// ------------------------------------------------------------------ random + +random_state: u64 = 1; + +SeedRandom :: (seed: s64) { + random_state = cast(u64)seed +} + +// splitmix64: every seed gives a different, well mixed sequence. +NextRandom :: () -> u64 { + random_state = random_state + 11400714819323198485 + z: u64 = random_state + z = (z ^ (z >> 30)) * 13787848793156543929 + z = (z ^ (z >> 27)) * 10723151780598845931 + return z ^ (z >> 31) +} + +// A number from 0 up to but not including limit. +Below :: (limit: s32) -> s32 { + if limit <= 1 { return 0 } + return cast(s32)((NextRandom() >> 11) % cast(u64)limit) +} + +Chance :: (percent: s32) -> bool { + return Below(100) < percent +} + +// ------------------------------------------------------------------ input + +InputCapacity :: 131072; + +// The case being checked. Damage works on it in place. +input_bytes: [InputCapacity]u8; +input_length: s32 = 0; + +// A scratch copy for moving ranges around. +scratch_bytes: [InputCapacity]u8; + +Input :: () -> string { + return TextView(input_bytes[0:input_length]) +} + +Append :: (text: string) { + index: s32 = 0 + while index < cast(s32)text.count && input_length < InputCapacity { + input_bytes[input_length] = text[index] + input_length += 1 + index += 1 + } +} + +AppendByte :: (byte: u8) { + if input_length < InputCapacity { + input_bytes[input_length] = byte + input_length += 1 + } +} + +AppendNumber :: (value: s64) { + digits: [24]u8 + Append(NumberText(value, digits[:])) +} + +// Replaces input[start:end] with text. +Replace :: (start: s32, end: s32, text: string) { + if start < 0 || end < start || end > input_length { return } + tail: s32 = input_length - end + index: s32 = 0 + while index < tail { + scratch_bytes[index] = input_bytes[end + index] + index += 1 + } + input_length = start + Append(text) + index = 0 + while index < tail && input_length < InputCapacity { + input_bytes[input_length] = scratch_bytes[index] + input_length += 1 + index += 1 + } +} + +// The shipped packs, read once. +PackCapacity :: 4; +PackBytes :: 65536; +pack_bytes: [PackCapacity][PackBytes]u8; +pack_lengths: [PackCapacity]s32; +pack_count: s32 = 0; + +Pack :: (index: s32) -> string { + return TextView(pack_bytes[index][0:pack_lengths[index]]) +} + +LoadPack :: (path: string) -> bool { + if pack_count >= PackCapacity { return false } + file: FileHandle = OpenRead(path) + if !file.valid { return false } + read: s64 = ReadAt(file, pack_bytes[pack_count][:], 0) + CloseFile(file) + if read <= 0 || read >= cast(s64)PackBytes { return false } + pack_lengths[pack_count] = cast(s32)read + pack_count += 1 + return true +} + +LoadInput :: (path: string) -> bool { + file: FileHandle = OpenRead(path) + if !file.valid { return false } + read: s64 = ReadAt(file, input_bytes[:], 0) + CloseFile(file) + if read < 0 || read >= cast(s64)InputCapacity { return false } + input_length = cast(s32)read + return true +} + +SaveInput :: (path: string) { + file: FileHandle = OpenReplace(path) + Check(file.valid, "cannot write CURRENT") + written: s64 = WriteAt(file, input_bytes[0:input_length], 0) + CloseFile(file) + Check(written == cast(s64)input_length, "cannot write CURRENT") +} + +// ------------------------------------------------------------------ grammar + +Kinds :: 24; +KindName :: (index: s32) -> string { + if index == 0 { return "Button" } + if index == 1 { return "Text" } + if index == 2 { return "TextField" } + if index == 3 { return "Surface" } + if index == 4 { return "Card" } + if index == 5 { return "Slider" } + if index == 6 { return "Toggle" } + if index == 7 { return "Progress" } + if index == 8 { return "Popup" } + if index == 9 { return "Menu" } + if index == 10 { return "MenuItem" } + if index == 11 { return "TabBar" } + if index == 12 { return "Tab" } + if index == 13 { return "ListBoxItem" } + if index == 14 { return "TreeView" } + if index == 15 { return "Checkbox" } + if index == 16 { return "Any" } + if index == 17 { return "App" } + if index == 18 { return "Scroll" } + if index == 19 { return "Plot" } + if index == 20 { return "NavigationBarItem" } + if index == 21 { return "Dropdown" } + if index == 22 { return "Nowhere" } + return "button" +} + +Attributes :: 12; +AttributeText :: (index: s32) -> string { + if index == 0 { return "[tone=Accent]" } + if index == 1 { return "[tone=Danger]" } + if index == 2 { return "[emphasis=Outline]" } + if index == 3 { return "[size=Small]" } + if index == 4 { return "[role=Track]" } + if index == 5 { return "[role=Panel]" } + if index == 6 { return "[state=hover]" } + if index == 7 { return ".primary" } + if index == 8 { return ":hover" } + if index == 9 { return ":disabled" } + if index == 10 { return "[role=Nothing]" } + return ":focus" +} + +Properties :: 20; +PropertyName :: (index: s32) -> string { + if index == 0 { return "background" } + if index == 1 { return "foreground" } + if index == 2 { return "border" } + if index == 3 { return "border-width" } + if index == 4 { return "radius" } + if index == 5 { return "padding-x" } + if index == 6 { return "padding-y" } + if index == 7 { return "gap" } + if index == 8 { return "opacity" } + if index == 9 { return "font-size" } + if index == 10 { return "icon-size" } + if index == 11 { return "material" } + if index == 12 { return "typeface" } + if index == 13 { return "letter-spacing" } + if index == 14 { return "offset-x" } + if index == 15 { return "offset-y" } + if index == 16 { return "background-end" } + if index == 17 { return "focus" } + if index == 18 { return "transition" } + return "unknown-property" +} + +Values :: 26; +ValueText :: (index: s32) -> string { + if index == 0 { return "#112233" } + if index == 1 { return "#11223344" } + if index == 2 { return "#abc" } + if index == 3 { return "#zzzzzz" } + if index == 4 { return "accent" } + if index == 5 { return "text" } + if index == 6 { return "space.2" } + if index == 7 { return "0" } + if index == 8 { return "12" } + if index == 9 { return "-3.5" } + if index == 10 { return "99999999999999999999" } + if index == 11 { return "1e40" } + if index == 12 { return "0.000001" } + if index == 13 { return "Flat" } + if index == 14 { return "default" } + if index == 15 { return "transparent" } + if index == 16 { return "\"body\"" } + if index == 17 { return "\"unterminated" } + if index == 18 { return "120ms" } + if index == 19 { return "var(--x)" } + if index == 20 { return "" } + if index == 21 { return "1 2 3" } + if index == 22 { return "missing-token" } + if index == 23 { return "radius" } + if index == 24 { return "." } + return "-" +} + +TokenGroups :: 5; +TokenGroupName :: (index: s32) -> string { + if index == 0 { return "color" } + if index == 1 { return "length" } + if index == 2 { return "duration" } + if index == 3 { return "material" } + return "shadow" +} + +TokenNames :: 10; +TokenName :: (index: s32) -> string { + if index == 0 { return "accent" } + if index == 1 { return "text" } + if index == 2 { return "canvas" } + if index == 3 { return "space.2" } + if index == 4 { return "radius" } + if index == 5 { return "default" } + if index == 6 { return "fast" } + if index == 7 { return "border" } + if index == 8 { return "a-token-name-that-is-longer-than-the-sixty-four-bytes-a-kss-name-holds" } + return "x" +} + +EnvQueries :: 12; +EnvQuery :: (index: s32) -> string { + if index == 0 { return "theme(dark)" } + if index == 1 { return "theme(light)" } + if index == 2 { return "contrast(high)" } + if index == 3 { return "density(compact)" } + if index == 4 { return "density(touch)" } + if index == 5 { return "pointer(touch)" } + if index == 6 { return "platform(android)" } + if index == 7 { return "platform(web)" } + if index == 8 { return "platform(beos)" } + if index == 9 { return "mood(happy)" } + if index == 10 { return "theme(dark" } + return "density()" +} + +// Pieces that damage inserts anywhere: whole constructs, their openers and +// closers, and single bytes that change how the text around them reads. +Fragments :: 40; +Fragment :: (index: s32) -> string { + if index == 0 { return "{" } + if index == 1 { return "}" } + if index == 2 { return ";" } + if index == 3 { return ":" } + if index == 4 { return "\"" } + if index == 5 { return "/*" } + if index == 6 { return "*/" } + if index == 7 { return "//" } + if index == 8 { return "\n" } + if index == 9 { return "@" } + if index == 10 { return "#" } + if index == 11 { return "[" } + if index == 12 { return "]" } + if index == 13 { return "(" } + if index == 14 { return ")" } + if index == 15 { return "@import ;" } + if index == 16 { return "@import \"other\";" } + if index == 17 { return "@layer app;" } + if index == 18 { return "@layer reset, components, app, overrides, extra;" } + if index == 19 { return "@pack again;" } + if index == 20 { return "@version 1;" } + if index == 21 { return "@version 99;" } + if index == 22 { return "@theme dark { accent: #ff0000; }" } + if index == 23 { return "@theme " } + if index == 24 { return "@env density(compact) { Button { gap: 2; } }" } + if index == 25 { return "@env " } + if index == 26 { return "@variant dense \"Dense\" { Button { radius: 0; } }" } + if index == 27 { return "@variant " } + if index == 28 { return "@media (min-width: 10px) { a { b: c; } }" } + if index == 29 { return "@keyframes spin { from { x: 0; } }" } + if index == 30 { return "tokens { color { accent: #123456; } }" } + if index == 31 { return "tokens {" } + if index == 32 { return "Button { background: accent; }" } + if index == 33 { return "Button[tone=Accent]:hover { }" } + if index == 34 { return "Button " } + if index == 35 { return "\\" } + if index == 36 { return "\xc3\xa9" } + if index == 37 { return "\x7f" } + if index == 38 { return "\x00" } + return " \t " +} + +AppendSelector :: () { + Append(KindName(Below(Kinds))) + count: s32 = Below(3) + while count > 0 { + Append(AttributeText(Below(Attributes))) + count -= 1 + } +} + +AppendRule :: () { + AppendSelector() + if Chance(10) { + Append(", ") + AppendSelector() + } + Append(" {\n") + count: s32 = Below(6) + while count > 0 { + Append(" ") + Append(PropertyName(Below(Properties))) + Append(": ") + Append(ValueText(Below(Values))) + if !Chance(3) { Append(";") } + Append("\n") + count -= 1 + } + if !Chance(3) { Append("}\n") } +} + +AppendTokens :: () { + Append("tokens {\n") + groups: s32 = 1 + Below(3) + while groups > 0 { + Append(" ") + Append(TokenGroupName(Below(TokenGroups))) + Append(" {\n") + count: s32 = Below(6) + while count > 0 { + Append(" ") + Append(TokenName(Below(TokenNames))) + Append(": ") + Append(ValueText(Below(Values))) + Append(";\n") + count -= 1 + } + Append(" }\n") + groups -= 1 + } + Append("}\n") +} + +AppendOverlay :: () { + count: s32 = Below(4) + while count > 0 { + Append(" ") + if Chance(60) { + Append(TokenName(Below(TokenNames))) + Append(": ") + Append(ValueText(Below(Values))) + Append(";\n") + } else { + AppendRule() + } + count -= 1 + } +} + +AppendConstruct :: () { + choice: s32 = Below(12) + if choice < 5 { + AppendRule() + } else if choice == 5 { + AppendTokens() + } else if choice == 6 { + Append("@theme ") + if Chance(80) { Append("dark") } else { Append("sepia") } + Append(" {\n") + AppendOverlay() + Append("}\n") + } else if choice == 7 { + Append("@env ") + Append(EnvQuery(Below(EnvQueries))) + Append(" {\n") + AppendOverlay() + Append("}\n") + } else if choice == 8 { + Append("@variant ") + if Chance(50) { Append("dense") } else { Append("roomy") } + Append(" \"Label\" {\n") + AppendOverlay() + Append("}\n") + } else if choice == 9 { + if Chance(50) { + Append("@layer ") + Append(KindName(Below(Kinds))) + Append(";\n") + } else { + Append("@layer reset, app;\n") + } + } else if choice == 10 { + if Chance(50) { Append("@import ;\n") } else { Append("@import ;\n") } + } else { + Append("/* ") + Append(Fragment(Below(Fragments))) + Append(" */\n") + } +} + +// A random sheet assembled from the grammar. Most of it is valid; a few +// choices (unknown names, missing semicolons and braces, odd values) are not. +Generate :: () { + input_length = 0 + if Chance(80) { + Append("@pack fuzz;\n") + } + if Chance(30) { + Append("@version 1;\n") + } + count: s32 = Below(24) + while count > 0 { + AppendConstruct() + if Chance(20) { Append("\n") } + count -= 1 + } +} + +// ------------------------------------------------------------------ valid sheets + +// A sheet made only of valid pieces, so the parser accepts it and the +// formatted text can be compared with it rule by rule. +ValidColor :: () -> string { + choice: s32 = Below(7) + if choice == 0 { return "#112233" } + if choice == 1 { return "#11223344" } + if choice == 2 { return "transparent" } + if choice == 3 { return "accent" } + if choice == 4 { return "ink" } + if choice == 5 { return "#ffffff" } + return "surface" +} + +ValidLength :: () -> string { + choice: s32 = Below(7) + if choice == 0 { return "0" } + if choice == 1 { return "12" } + if choice == 2 { return "2.5" } + if choice == 3 { return "space.2" } + if choice == 4 { return "corner" } + if choice == 5 { return "-4" } + return "1000" +} + +AppendPair :: (name: string, value: string) { + Append(name) + Append(value) +} + +AppendValidProperty :: () { + choice: s32 = Below(17) + if choice == 0 { AppendPair("background: ", ValidColor()) } + else if choice == 1 { AppendPair("foreground: ", ValidColor()) } + else if choice == 2 { AppendPair("border: ", ValidColor()) } + else if choice == 3 { AppendPair("focus: ", ValidColor()) } + else if choice == 4 { AppendPair("background-end: ", ValidColor()) } + else if choice == 5 { AppendPair("border-width: ", ValidLength()) } + else if choice == 6 { AppendPair("radius: ", ValidLength()) } + else if choice == 7 { AppendPair("padding-x: ", ValidLength()) } + else if choice == 8 { AppendPair("padding-y: ", ValidLength()) } + else if choice == 9 { AppendPair("gap: ", ValidLength()) } + else if choice == 10 { AppendPair("font-size: ", ValidLength()) } + else if choice == 11 { AppendPair("icon-size: ", ValidLength()) } + else if choice == 12 { AppendPair("offset-x: ", ValidLength()) } + else if choice == 13 { AppendPair("letter-spacing: ", ValidLength()) } + else if choice == 14 { + Append("opacity: ") + if Chance(50) { Append("0.5") } else { Append("1") } + } else if choice == 15 { + Append("material: ") + if Chance(50) { Append("Flat") } else { Append("default") } + } else { + Append("typeface: ") + if Chance(50) { Append("body") } else { Append("mono") } + } + Append(";") +} + +AppendValidRule :: (indent: string) { + Append(indent) + Append(KindName(Below(22))) + attributes: s32 = Below(3) + while attributes > 0 { + Append(AttributeText(Below(10))) + attributes -= 1 + } + Append(" {") + count: s32 = Below(5) + while count > 0 { + if Chance(50) { + Append("\n ") + Append(indent) + } else { + Append(" ") + } + AppendValidProperty() + count -= 1 + } + if Chance(50) { + Append("\n") + Append(indent) + } else { + Append(" ") + } + Append("}\n") +} + +AppendValidOverlay :: () { + choice: s32 = Below(4) + if choice == 0 { AppendPair(" accent: ", ValidColor()) } + else if choice == 1 { Append(" ink: #000000") } + else if choice == 2 { AppendPair(" space.2: ", ValidLength()) } + else { Append(" corner: 3") } + Append(";\n") +} + +// Labels are quoted text, so structure bytes inside them are only text. +VariantLabel :: () -> string { + choice: s32 = Below(6) + if choice == 0 { return "\"Dense\"" } + if choice == 1 { return "\"a; b\"" } + if choice == 2 { return "\"{ open\"" } + if choice == 3 { return "\"close }\"" } + if choice == 4 { return "\"/* not a comment\"" } + return "\"// nor this\"" +} + +AppendComment :: () { + choice: s32 = Below(4) + if choice == 0 { Append("/* a comment */\n") } + else if choice == 1 { Append("// a line comment\n") } + else if choice == 2 { Append("/* a comment\n over lines */\n") } + else { Append("/**/\n") } +} + +GenerateValid :: () { + input_length = 0 + Append("@pack fuzz;\n") + if Chance(50) { Append("@version 1;\n") } + Append("tokens {\n") + Append(" color {\n accent: #3b609b;\n ink: #171717;\n surface: #e6e3dc;\n }\n") + Append(" length {\n space.2: 8;\n corner: 4;\n }\n") + Append(" material {\n default: Flat;\n }\n") + Append("}\n") + count: s32 = Below(16) + while count > 0 { + choice: s32 = Below(12) + if choice < 6 { + AppendValidRule("") + } else if choice == 6 { + Append("@theme dark {\n") + AppendValidOverlay() + Append("}\n") + } else if choice == 7 { + Append("@env ") + Append(EnvQuery(Below(8))) + Append(" {\n") + if Chance(50) { AppendValidOverlay() } else { AppendValidRule(" ") } + Append("}\n") + } else if choice == 8 { + Append("@variant dense ") + Append(VariantLabel()) + if Chance(50) { + // One line, which the formatter reflows. + Append(" { Button { gap: 1; } }\n") + } else { + Append(" {\n") + AppendValidRule(" ") + Append("}\n") + } + } else if choice == 9 { + if Chance(50) { Append("@layer app;\n") } else { Append("@layer components;\n") } + } else if choice == 10 { + AppendComment() + } else { + Append("@import ;\n") + } + if Chance(15) { Append("\n") } + count -= 1 + } +} + +RandomPosition :: () -> s32 { + return Below(input_length + 1) +} + +// One random change to the input. +Damage :: () { + kind: s32 = Below(9) + if kind == 0 && input_length > 0 { + input_bytes[Below(input_length)] = cast(u8)Below(256) + } else if kind == 1 && input_length > 0 { + // Nearby punctuation reads as structure; swap it in. + at: s32 = Below(input_length) + input_bytes[at] = Fragment(Below(15))[0] + } else if kind == 2 { + at: s32 = RandomPosition() + Replace(at, at, Fragment(Below(Fragments))) + } else if kind == 3 { + start: s32 = RandomPosition() + end: s32 = start + Below(64) + if end > input_length { end = input_length } + Replace(start, end, "") + } else if kind == 4 && input_length > 0 { + // Repeat a range, so tables fill up. + start: s32 = Below(input_length) + length: s32 = 1 + Below(512) + if start + length > input_length { length = input_length - start } + copies: s32 = 1 + Below(40) + copy: [512]u8 + index: s32 = 0 + while index < length { + copy[index] = input_bytes[start + index] + index += 1 + } + at: s32 = start + length + while copies > 0 { + Replace(at, at, TextView(copy[0:length])) + copies -= 1 + } + } else if kind == 5 { + input_length = RandomPosition() + } else if kind == 6 && pack_count > 0 { + // Splice a range of a pack in. + pack: string = Pack(Below(pack_count)) + start: s32 = Below(cast(s32)pack.count) + end: s32 = start + Below(2048) + if end > cast(s32)pack.count { end = cast(s32)pack.count } + at: s32 = RandomPosition() + Replace(at, at, pack[start:end]) + } else if kind == 7 { + // A whole generated construct goes in front of the tail. + at: s32 = RandomPosition() + tail_length: s32 = input_length - at + index: s32 = 0 + while index < tail_length { + scratch_bytes[index] = input_bytes[at + index] + index += 1 + } + input_length = at + AppendConstruct() + index = 0 + while index < tail_length && input_length < InputCapacity { + input_bytes[input_length] = scratch_bytes[index] + input_length += 1 + index += 1 + } + } else { + at: s32 = RandomPosition() + Replace(at, at, "\n") + } +} + +// ------------------------------------------------------------------ imports + +// The text an import name resolves to. It depends only on the name, so the +// original and formatted sheets see the same imports. +ImportSource :: (name: KssName) -> string { + hash: u32 = 2166136261 + index: s32 = 0 + while index < name.length && index < 64 { + hash = (hash ^ cast(u32)name.bytes[index]) * 16777619 + index += 1 + } + choice: u32 = hash % 6 + if choice == 0 { return "" } + if choice == 1 { return "tokens { color { accent: #334455; } }\nButton { background: accent; }\n" } + if choice == 2 { return "@import ;\nText { foreground: #000000; }\n" } + if choice == 3 { return "@import ;\n" } + if choice == 4 { return "Card { radius: 4; gap: space.2; }\n@theme dark { accent: #000000; }\n" } + return "Button { background: #00000" +} + +// Import names live here for the rest of the parse that uses them. +import_names: [64]KssName; + +ImportName :: (slot: s32, name: KssName) -> string { + import_names[slot] = name + length: s32 = name.length + if length < 0 { length = 0 } + if length > 64 { length = 64 } + return TextView(import_names[slot].bytes[0:length]) +} + +ImportFails :: (name: KssName) -> bool { + return name.length > 0 && name.bytes[0] == cast(u8)120 +} + +// ------------------------------------------------------------------ parsing + +RuleCapacity :: 2048; + +// What one parse produced, for comparing two parses. +Outcome :: struct { + status: s32 + steps: s32 + rule_count: s32 + token_count: s32 + declaration_count: s32 + foreign_count: s32 + variant_count: s32 + variants: [8]KssVariantDecl +} + +rules_first: [RuleCapacity]StyleRule; +rules_second: [RuleCapacity]StyleRule; +tokens_first: [128]KssToken; +tokens_second: [128]KssToken; + +// Parser tables stay inside their bounds after every step. +CheckParser :: (p: KssParser) { + Check(p.cursor.pos >= 0 && p.cursor.pos <= cast(s32)p.cursor.source.count, + "cursor left its source") + Check(p.file_count >= 1 && p.file_count <= 8, "file count out of range") + Check(p.cursor.file >= 0 && p.cursor.file < p.file_count, "cursor file out of range") + Check(p.import_depth >= 0 && p.import_depth <= 8, "import depth out of range") + Check(p.token_count >= 0 && p.token_count <= 128, "token count out of range") + Check(p.layer_count >= 0 && p.layer_count <= 12, "layer count out of range") + Check(p.theme_count >= 0 && p.theme_count <= 16, "theme count out of range") + Check(p.variant_count >= 0 && p.variant_count <= 8, "variant count out of range") + Check(p.block_count >= 0 && p.block_count <= 32, "block count out of range") + Check(p.declaration_count >= 0 && p.declaration_count <= 1024, + "declaration count out of range") + Check(p.foreign_count >= 0 && p.foreign_count <= 16, "foreign count out of range") + Check(p.diagnostic_length >= 0 && p.diagnostic_length <= 256, + "diagnostic length out of range") + Check(p.status >= cast(s32)KssStatusContinue && p.status <= cast(s32)KssStatusError, + "unknown parser status") +} + +// Parses source to the end, answering imports by name. The step limit is +// generous: every step either consumes source or finishes an import. +Parse :: (source: string, declarative: bool, rules: []StyleRule, tokens: []KssToken) -> Outcome { + outcome: Outcome + environment: KssEnvironment = KssDefaultEnvironment() + p: KssParser + if declarative { + p = KssBeginDeclarative(source, "fuzz.kss", environment) + } else { + p = KssBegin(source, "fuzz.kss", environment) + } + imports: s32 = 0 + limit: s32 = cast(s32)source.count * 2 + 4096 + while p.status != cast(s32)KssStatusDone && p.status != cast(s32)KssStatusError { + Check(outcome.steps < limit, "the parser does not finish") + if p.status == cast(s32)KssStatusNeedImport { + imports += 1 + Check(imports <= 64, "the parser keeps asking for imports") + if ImportFails(p.pending_import) { + p = KssFailImport(p) + } else { + p = KssProvideImport(p, ImportSource(p.pending_import), + ImportName(imports - 1, p.pending_import)) + } + } else { + p = KssStep(p) + } + outcome.steps += 1 + CheckParser(p) + if p.status == cast(s32)KssStatusRule { + if outcome.rule_count < RuleCapacity { + rules[outcome.rule_count] = p.rule + } + outcome.rule_count += 1 + } + } + if p.status == cast(s32)KssStatusError { + Check(p.diagnostic_length > 0, "an error without a diagnostic") + if verbose { + WriteText(2, "error: ") + WriteText(2, TextView(p.diagnostic[0:p.diagnostic_length])) + WriteText(2, "\n") + } + } + outcome.status = p.status + outcome.token_count = p.token_count + outcome.declaration_count = p.declaration_count + outcome.foreign_count = p.foreign_count + outcome.variant_count = p.variant_count + index: s32 = 0 + while index < p.token_count { + tokens[index] = p.tokens[index] + index += 1 + } + index = 0 + while index < p.variant_count && index < 8 { + outcome.variants[index] = p.variants[index] + index += 1 + } + return outcome +} + +// The rule table path: the same parser behind the table an app installs. +ParseTable :: (source: string) { + table: StyleRulesParse = BeginStyleRules(source, "fuzz.kss", KssDefaultEnvironment()) + imports: s32 = 0 + while true { + table = ParseStyleRules(table) + if table.overflow || table.parser.status != cast(s32)KssStatusNeedImport { break } + imports += 1 + Check(imports <= 64, "the rule table keeps asking for imports") + if ImportFails(table.parser.pending_import) { + table = FailStyleRulesImport(table) + } else { + table = ProvideStyleRulesImport(table, + ImportSource(table.parser.pending_import), + ImportName(imports - 1, table.parser.pending_import)) + } + } + Check(table.rules.count >= 0 && table.rules.count <= 320, "rule table count out of range") +} + +SameFloat :: (a: float32, b: float32) -> bool { + return a == b || (a != a && b != b) +} + +SameSelector :: (a: StyleSelector, b: StyleSelector) -> bool { + return a.kind == b.kind && a.name == b.name && a.class_name == b.class_name && + a.role == b.role && a.tone == b.tone && a.emphasis == b.emphasis && + a.size == b.size && a.state == b.state && a.validation == b.validation && + a.orientation == b.orientation && a.placement == b.placement +} + +SameStyle :: (a: StyleData, b: StyleData) -> bool { + return a.fields == b.fields && a.background == b.background && + a.foreground == b.foreground && a.border == b.border && a.focus == b.focus && + SameFloat(a.radius, b.radius) && SameFloat(a.border_width, b.border_width) && + SameFloat(a.opacity, b.opacity) && SameFloat(a.padding_x, b.padding_x) && + SameFloat(a.padding_y, b.padding_y) && SameFloat(a.gap, b.gap) && + SameFloat(a.font_size, b.font_size) && SameFloat(a.icon_size, b.icon_size) && + SameFloat(a.offset_x, b.offset_x) && SameFloat(a.offset_y, b.offset_y) && + a.background_end == b.background_end && a.material == b.material && + a.typeface == b.typeface && SameFloat(a.letter_spacing, b.letter_spacing) +} + +SameRule :: (a: StyleRule, b: StyleRule) -> bool { + return SameSelector(a.selector, b.selector) && a.state == b.state && + a.layer == b.layer && a.order == b.order && SameStyle(a.style, b.style) +} + +SameToken :: (a: KssToken, b: KssToken) -> bool { + if a.name.length != b.name.length || a.kind != b.kind || a.color != b.color || + !SameFloat(a.number, b.number) || a.material != b.material || + a.origin != b.origin { + return false + } + index: s32 = 0 + while index < a.name.length && index < 64 { + if a.name.bytes[index] != b.name.bytes[index] { return false } + index += 1 + } + return true +} + +// ------------------------------------------------------------------ formatting + +FormattedCapacity :: 262144; +formatted_first: [FormattedCapacity]u8; +formatted_second: [FormattedCapacity]u8; + +// Joins the formatter's segments into text. Returns -1 when it does not fit. +Stitch :: (source: string, result: KssFormatResult, output: []u8) -> s32 { + length: s32 = 0 + capacity: s32 = cast(s32)output.count + Check(result.count >= 0 && result.count <= 1024, "segment count out of range") + index: s32 = 0 + while index < result.count { + segment: KssFormatSegment = result.segments[index] + if segment.kind == 0 { + Check(segment.start >= 0 && segment.length >= 0 && + segment.start + segment.length <= cast(s32)source.count, + "a copy segment leaves the source") + if length + segment.length > capacity { return -1 } + at: s32 = 0 + while at < segment.length { + output[length] = source[segment.start + at] + length += 1 + at += 1 + } + } else if segment.kind == 1 { + if length + 1 > capacity { return -1 } + output[length] = segment.atom + length += 1 + } else { + Check(segment.kind == 2, "unknown segment kind") + Check(segment.length >= 0 && segment.length <= 4096, "indent out of range") + if length + 1 + segment.length > capacity { return -1 } + output[length] = 10 + length += 1 + at: s32 = 0 + while at < segment.length { + output[length] = 32 + length += 1 + at += 1 + } + } + index += 1 + } + return length +} + +// Check mode explains a sheet: the formatted text and both parses. +verbose: bool = false; + +WriteOutcome :: (outcome: Outcome) { + WriteText(2, "status ") + WriteNumber(2, cast(s64)outcome.status) + WriteText(2, ", ") + WriteNumber(2, cast(s64)outcome.rule_count) + WriteText(2, " rules, ") + WriteNumber(2, cast(s64)outcome.token_count) + WriteText(2, " tokens, ") + WriteNumber(2, cast(s64)outcome.declaration_count) + WriteText(2, " declarations, ") + WriteNumber(2, cast(s64)outcome.foreign_count) + WriteText(2, " foreign blocks\n") +} + +IsSpace :: (byte: u8) -> bool { + return byte == 32 || byte == 9 || byte == 10 || byte == 13 +} + +// True when a and b hold the same bytes apart from whitespace. +SameText :: (a: string, b: string) -> bool { + left: s32 = 0 + right: s32 = 0 + while true { + while left < cast(s32)a.count && IsSpace(a[left]) { left += 1 } + while right < cast(s32)b.count && IsSpace(b[right]) { right += 1 } + if left >= cast(s32)a.count || right >= cast(s32)b.count { + return left >= cast(s32)a.count && right >= cast(s32)b.count + } + if a[left] != b[right] { return false } + left += 1 + right += 1 + } + return false +} + +SameName :: (a: KssName, b: KssName) -> bool { + if a.length != b.length { return false } + index: s32 = 0 + while index < a.length && index < 64 { + if a.bytes[index] != b.bytes[index] { return false } + index += 1 + } + return true +} + +SameOutcome :: (a: Outcome, b: Outcome) -> bool { + if a.status != b.status || a.rule_count != b.rule_count || + a.token_count != b.token_count || a.declaration_count != b.declaration_count || + a.foreign_count != b.foreign_count || a.variant_count != b.variant_count { + return false + } + // Variant labels are quoted text the formatter must leave alone. + index: s32 = 0 + while index < a.variant_count && index < 8 { + if !SameName(a.variants[index].name, b.variants[index].name) || + !SameName(a.variants[index].label, b.variants[index].label) { + return false + } + index += 1 + } + return true +} + +// Formatting may refuse a sheet, but what it produces must parse like the +// original and must not change when formatted again. +CheckFormat :: (source: string, typed: Outcome) { + first: KssFormatResult = KssFormat(source) + if !first.ok { + if verbose { + WriteText(2, "the formatter declines: ") + WriteText(2, TextView(first.diagnostic[0:first.diagnostic_length])) + WriteText(2, "\n") + } + Check(first.diagnostic_length > 0 && first.diagnostic_length <= 128, + "a format failure without a diagnostic") + return + } + length: s32 = Stitch(source, first, formatted_first[:]) + if length < 0 { return } + formatted: string = TextView(formatted_first[0:length]) + Check(SameText(source, formatted), "formatting changed more than whitespace") + + again: Outcome = Parse(formatted, false, rules_second[:], tokens_second[:]) + if verbose { + WriteText(2, "formatted sheet:\n") + WriteText(2, formatted) + WriteText(2, "\noriginal: ") + WriteOutcome(typed) + WriteText(2, "formatted: ") + WriteOutcome(again) + } + if typed.status == cast(s32)KssStatusDone || again.status == cast(s32)KssStatusDone { + compared_count += 1 + Check(SameOutcome(typed, again), "formatting changed what the sheet means") + index: s32 = 0 + while index < typed.rule_count && index < RuleCapacity { + Check(SameRule(rules_first[index], rules_second[index]), + "formatting changed a rule") + index += 1 + } + index = 0 + while index < typed.token_count { + Check(SameToken(tokens_first[index], tokens_second[index]), + "formatting changed a token") + index += 1 + } + declarative: Outcome = Parse(source, true, rules_second[:], tokens_second[:]) + formatted_declarative: Outcome = Parse(formatted, true, rules_second[:], tokens_second[:]) + Check(SameOutcome(declarative, formatted_declarative), + "formatting changed the declarations") + } + + second: KssFormatResult = KssFormat(formatted) + Check(second.ok, "the formatter rejects its own output") + second_length: s32 = Stitch(formatted, second, formatted_second[:]) + if second_length < 0 { return } + if verbose { + WriteText(2, "formatted again:\n") + WriteText(2, TextView(formatted_second[0:second_length])) + WriteText(2, "\n") + } + Check(second_length == length, "formatting twice changes the length") + index: s32 = 0 + while index < length { + Check(formatted_first[index] == formatted_second[index], "formatting twice changes the text") + index += 1 + } +} + +// ------------------------------------------------------------------ cases + +current_path: [4096]u8; +current_path_length: s32 = 0; +saving: bool = true; + +RunCase :: () { + if saving { SaveInput(TextView(current_path[0:current_path_length])) } + source: string = Input() + typed: Outcome = Parse(source, false, rules_first[:], tokens_first[:]) + if verbose { + WriteText(2, "parsed: ") + WriteOutcome(typed) + } + if typed.status == cast(s32)KssStatusDone { parsed_count += 1 } + last_status = typed.status + Parse(source, true, rules_second[:], tokens_second[:]) + ParseTable(source) + CheckFormat(source, typed) +} + +MakeCase :: () { + if Chance(40) { + GenerateValid() + // Now and then one change, which may or may not keep it valid. + if Chance(20) { Damage() } + return + } + if pack_count == 0 || Chance(50) { + Generate() + damage: s32 = Below(4) + while damage > 0 { + Damage() + damage -= 1 + } + return + } + pack: string = Pack(Below(pack_count)) + input_length = 0 + Append(pack) + damage: s32 = 1 + Below(8) + while damage > 0 { + Damage() + damage -= 1 + } +} + +// ------------------------------------------------------------------ shrinking + +backup_bytes: [InputCapacity]u8; + +// Runs the checks on the input in a child process, whose output goes nowhere, +// and returns its wait status: 0 when every check passes. +CaseStatus :: () -> s32 { + pid: s32 = RawFork() + Check(pid >= 0, "cannot fork") + if pid == 0 { + nowhere: [16]u8 + descriptor: s32 = -1 + if CopyCString("/dev/null", nowhere[:]) { + descriptor = RawOpen(*nowhere[0], 1) + } + if descriptor >= 0 { + RawDup2(descriptor, 1) + RawDup2(descriptor, 2) + } + saving = false + verbose = false + shrinking = true + RunCase() + RawExit(0) + } + status: s32 = 0 + RawWaitPid(pid, *status, 0) + return status +} + +// Removes ever smaller ranges while the sheet keeps failing the same way. +Shrink :: (failure: s32) { + chunk: s32 = input_length / 2 + if chunk < 1 { chunk = 1 } + while chunk >= 1 { + progress: bool = false + at: s32 = 0 + while at < input_length { + end: s32 = at + chunk + if end > input_length { end = input_length } + kept: s32 = input_length + index: s32 = 0 + while index < kept { + backup_bytes[index] = input_bytes[index] + index += 1 + } + Replace(at, end, "") + if CaseStatus() == failure { + progress = true + } else { + index = 0 + while index < kept { + input_bytes[index] = backup_bytes[index] + index += 1 + } + input_length = kept + at += chunk + } + } + if !progress { chunk = chunk / 2 } + } +} + +ParseNumber :: (text: string) -> s64 { + value: s64 = 0 + index: s32 = 0 + if text.count == 0 { return -1 } + while index < cast(s32)text.count { + byte: u8 = text[index] + if byte < 48 || byte > 57 || value > 100000000000 { return -1 } + value = value * 10 + cast(s64)(byte - 48) + index += 1 + } + return value +} + +Usage :: () { + WriteText(2, "usage: kss_fuzz CURRENT SEED COUNT PACK...\n") + WriteText(2, " kss_fuzz CURRENT check FILE\n") + WriteText(2, " kss_fuzz CURRENT corpus FILE...\n") + WriteText(2, " kss_fuzz CURRENT shrink FILE\n") +} + +#program_export +main :: (argc: s32, argv: **s8) -> s32 { + if argc < 4 { + Usage() + return 2 + } + current: string = TextFromCString(cast(*u8)argv[1]) + if current.count == 0 || current.count >= 4096 { + Usage() + return 2 + } + index: s32 = 0 + while index < cast(s32)current.count { + current_path[index] = current[index] + index += 1 + } + current_path_length = cast(s32)current.count + + mode: string = TextFromCString(cast(*u8)argv[2]) + if mode == "check" { + if argc != 4 { + Usage() + return 2 + } + Check(LoadInput(TextFromCString(cast(*u8)argv[3])), "cannot read FILE") + verbose = true + RunCase() + WriteText(1, "kss fuzz: the sheet passes every check\n") + return 0 + } + if mode == "corpus" { + argument: s32 = 3 + while argument < argc { + path: string = TextFromCString(cast(*u8)argv[argument]) + case_index = argument - 3 + if !LoadInput(path) { + WriteText(2, "kss fuzz: cannot read ") + WriteText(2, path) + WriteText(2, "\n") + return 2 + } + RunCase() + if path.count > 7 && path[path.count - 7:path.count] == ".ok.kss" && + last_status != cast(s32)KssStatusDone { + WriteText(2, path) + WriteText(2, "\n") + Fail("a sheet named .ok.kss does not parse") + } + argument += 1 + } + WriteText(1, "kss fuzz: ") + WriteNumber(1, cast(s64)(argc - 3)) + WriteText(1, " corpus sheets passed\n") + return 0 + } + if mode == "shrink" { + if argc != 4 { + Usage() + return 2 + } + Check(LoadInput(TextFromCString(cast(*u8)argv[3])), "cannot read FILE") + failure: s32 = CaseStatus() + if failure == 0 { + WriteText(1, "kss fuzz: the sheet passes every check; nothing to shrink\n") + return 0 + } + Shrink(failure) + SaveInput(TextView(current_path[0:current_path_length])) + WriteNumber(1, cast(s64)input_length) + WriteText(1, " bytes still fail:\n") + WriteText(1, Input()) + WriteText(1, "\n") + return 0 + } + + seed: s64 = ParseNumber(mode) + count: s64 = ParseNumber(TextFromCString(cast(*u8)argv[3])) + if seed < 0 || count < 0 { + Usage() + return 2 + } + argument: s32 = 4 + while argument < argc { + path: string = TextFromCString(cast(*u8)argv[argument]) + if !LoadPack(path) { + WriteText(2, "kss fuzz: cannot read pack ") + WriteText(2, path) + WriteText(2, "\n") + return 2 + } + argument += 1 + } + case_seed = seed + SeedRandom(seed) + while cast(s64)case_index < count { + MakeCase() + RunCase() + case_index += 1 + } + WriteText(1, "kss fuzz: ") + WriteNumber(1, count) + WriteText(1, " sheets from seed ") + WriteNumber(1, seed) + WriteText(1, " passed; ") + WriteNumber(1, parsed_count) + WriteText(1, " parsed without error, ") + WriteNumber(1, compared_count) + WriteText(1, " compared with their formatted text\n") + return 0 +} diff --git a/tests/style_parse_behavior.zi b/tests/style_parse_behavior.zi new file mode 100644 index 0000000..0b3fca4 --- /dev/null +++ b/tests/style_parse_behavior.zi @@ -0,0 +1,108 @@ +#import "kryon/drawing_props" +#import "kryon/style" +#import "kryon/style_sheet" +#import "kss_parser" +#import "style_parse" + +using KssStatus; + +InstallBodyTypeface :: (bytes: []u8) -> bool { + rules: StyleRules + rules.count = 1 + rules.items[0].style.typeface = TextView(bytes[:]) + return InstallStyleRules(rules) +} + +StyleRulesAtCapacity :: (count: s32) -> StyleRules { + result: StyleRules + if count >= 0 && count <= 320 { result.count = count } + return result +} + +// A rule with this background; border, width, and radius when nonzero. +HasRule :: (rules: StyleRules, background: u32, border: u32, + border_width: float32, radius: float32) -> bool { + index: s32 = 0 + while index < rules.count { + style: StyleData = rules.items[index].style + if style.background == background && + (border == cast(u32)0 || style.border == border) && + (border_width == 0.0 || style.border_width == border_width) && + (radius == 0.0 || style.radius == radius) { return true } + index += 1 + } + return false +} + +// Parses sheets with imports, color overrides, and full rule tables, then +// installs them into Kryon's style rules. +#program_export +Answer :: () -> s32 { + parsed: StyleRulesParse = BeginStyleRules( + "@pack demo; @import ; Progress[role=Track] { background: #112233; border: #99aabb; border-width: 2; radius: 5; }", + "demo.kss", KssDefaultEnvironment()) + parsed = ParseStyleRules(parsed) + if parsed.parser.status != cast(s32)KssStatusNeedImport || + parsed.rules.count != 0 { return 0 } + parsed = ProvideStyleRulesImport(parsed, + "Progress[role=Fill] { background: #556677; }", "base") + parsed = ParseStyleRules(parsed) + if parsed.parser.status != cast(s32)KssStatusDone || + parsed.overflow || parsed.rules.count != 2 { return 0 } + if !InstallParsedStyleRules(parsed) { return 0 } + // The sheet's own rule and the imported one are both installed. + installed_pair: StyleRules = ActiveStyleRules() + if installed_pair.count != 2 || + !HasRule(installed_pair, cast(u32)0x112233ff, cast(u32)0x99aabbff, 2.0, 5.0) || + !HasRule(installed_pair, cast(u32)0x556677ff, cast(u32)0, 0.0, 0.0) { + return 0 + } + if !KssCSSNeedsPixels("width") { return 0 } + parsed = BeginStyleRules( + "@pack colors; tokens { color { canvas: #112233; } }", + "colors.kss", KssDefaultEnvironment()) + parsed.parser = KssAddColorOverride(parsed.parser, "canvas", + cast(u32)0xaabbccdd) + parsed = ParseStyleRules(parsed) + if !InstallParsedStyleRules(parsed) { return 0 } + color: Color = StyleTokenColor("canvas") + if color.r != 0xaa || color.g != 0xbb || color.b != 0xcc || + color.a != 0xdd { return 0 } + color = StyleTokenColorOr("missing", Color.{1, 2, 3, 4}) + if color.r != 1 || color.g != 2 || color.b != 3 || color.a != 4 { + return 0 + } + parsed = BeginStyleRules( + "@pack replacement; tokens { color { text: #445566; } }", + "replacement.kss", KssDefaultEnvironment()) + parsed = ParseStyleRules(parsed) + if !InstallParsedStyleRules(parsed) { return 0 } + color = StyleTokenColor("canvas") + if color.r != 0 || color.g != 0 || color.b != 0 || color.a != 255 { + return 0 + } + full_rules: StyleRules = StyleRulesAtCapacity(320) + parsed = BeginStyleRules("Progress { background: #112233; }", + "full.kss", KssDefaultEnvironment()) + parsed.rules = full_rules + parsed = StepStyleRules(parsed) + if !parsed.overflow || parsed.rules.count != 320 { return 0 } + parsed = BeginStyleRules("@import ;", "missing.kss", + KssDefaultEnvironment()) + parsed = ParseStyleRules(parsed) + if parsed.parser.status != cast(s32)KssStatusNeedImport { return 0 } + parsed = FailStyleRulesImport(parsed) + if parsed.parser.status != cast(s32)KssStatusError { return 0 } + + bytes: [4]u8 = .[98, 111, 100, 121] + if !InstallBodyTypeface(bytes[:]) { return 0 } + installed: StyleRules = ActiveStyleRules() + if installed.items[0].style.typeface != "body" { return 0 } + bytes[0] = cast(u8)120 + if installed.items[0].style.typeface != "body" { return 0 } + owned_rules: StyleRules + owned_rules.count = 321 + if InstallStyleRules(owned_rules) || + ActiveStyleRules().items[0].style.typeface != "body" { return 0 } + return 42 +} diff --git a/tests/system_style_behavior.zi b/tests/system_style_behavior.zi new file mode 100644 index 0000000..d408a6c --- /dev/null +++ b/tests/system_style_behavior.zi @@ -0,0 +1,31 @@ +#import "kryon/drawing_props" +#import "system_style" +#import "kryon/SystemTheme" + +Sweet :: "@define-color theme_fg_color #C3C7D1;\n@define-color theme_bg_color #161925;\n@define-color theme_base_color #181b28;\n@define-color theme_selected_bg_color #c50ed2;\n@define-color theme_selected_fg_color #fefefe;\n@define-color error_color #e6133e;\n@define-color warning_color #cc5500;\n@define-color success_color #0096b1;\n"; + +Same :: (name: string, expected: u32) -> bool { + color: Color = StyleTokenColor(name) + return color.r == cast(u8)((expected >> cast(u32)24) & cast(u32)255) && + color.g == cast(u8)((expected >> cast(u32)16) & cast(u32)255) && + color.b == cast(u8)((expected >> cast(u32)8) & cast(u32)255) +} + +#program_export +main :: () -> s32 { + // The system palette becomes the pack's color tokens. + if !StyleInstallSystem(SystemPaletteFromCss(Sweet, false)) { return 1 } + if !Same("canvas", cast(u32)0x161925FF) || !Same("text", cast(u32)0xC3C7D1FF) { return 2 } + if !Same("accent", cast(u32)0xc50ed2FF) || !Same("accent-ink", cast(u32)0xfefefeFF) { return 3 } + if !Same("danger", cast(u32)0xe6133eFF) { return 4 } + + // Installing a light palette replaces them. + light: string = "@define-color theme_fg_color #2e3436;\n@define-color theme_bg_color #f6f5f4;\n" + if !StyleInstallSystem(SystemPaletteFromCss(light, false)) { return 5 } + if !Same("canvas", cast(u32)0xf6f5f4FF) || !Same("text", cast(u32)0x2e3436FF) { return 6 } + + // Without a theme, the fallback still installs a full style. + if !StyleInstallSystem(SystemPaletteFallback(true)) { return 7 } + if StyleTokenColor("canvas").r == cast(u8)0xf6 { return 8 } + return 0 +} diff --git a/tools/style_pack_module.zi b/tools/style_pack_module.zi new file mode 100644 index 0000000..7c0f98a --- /dev/null +++ b/tools/style_pack_module.zi @@ -0,0 +1,263 @@ +// Embed a style pack as a Ziran module, so an application can install it +// without shipping the .kss file. Run it from the repository root: +// +// style_pack_module write NAME write src/style_pack_NAME.zi from +// styles/NAME.kss +// style_pack_module check NAME fail when src/style_pack_NAME.zi is not +// what write would produce +// +// Apps import Kryon as a Ziran package without running its Makefile, so the +// module is committed; `make style-pack-check` keeps it equal to its pack. +// A Ziran string literal holds at most 4095 characters, so the pack is stored +// as chunks that the module joins into one static buffer on first use. +#import "byte_text_linux" +#import "c_string" +#import "file_linux" + +libc :: #system_library "libc"; +RawWrite :: (descriptor: s32, input: *u8, count: u64) -> s64 #foreign libc "write"; + +// Lines go into a chunk until it would pass this many bytes. +ChunkBytes :: 1800; +// No chunk may pass this many, which leaves room below the literal limit. +ChunkLimit :: 3000; + +source_bytes: [262144]u8; +module_bytes: [524288]u8; +module_length: s32 = 0; +disk_bytes: [524288]u8; + +WriteText :: (descriptor: s32, text: string) { + chunk: [512]u8 + at: s32 = 0 + while at < cast(s32)text.count { + count: s32 = cast(s32)text.count - at + if count > 512 { count = 512 } + index: s32 = 0 + while index < count { + chunk[index] = text[at + index] + index += 1 + } + if RawWrite(descriptor, *chunk[0], cast(u64)count) <= 0 { return } + at += count + } +} + +Fail :: (message: string, name: string) -> s32 { + WriteText(2, "style_pack_module: ") + WriteText(2, name) + WriteText(2, ": ") + WriteText(2, message) + WriteText(2, "\n") + return 1 +} + +Emit :: (text: string) -> bool { + index: s32 = 0 + while index < cast(s32)text.count { + if module_length >= cast(s32)module_bytes.count { return false } + module_bytes[module_length] = text[index] + module_length += 1 + index += 1 + } + return true +} + +EmitNumber :: (value: s32) -> bool { + digits: [16]u8 + at: s32 = 16 + magnitude: s32 = value + while true { + at -= 1 + digits[at] = cast(u8)(48 + magnitude % 10) + magnitude = magnitude / 10 + if magnitude == 0 { break } + } + return Emit(TextView(digits[at:16])) +} + +// The pack name with its first letter upper case, as in StylePackClassic. +title_bytes: [64]u8; + +Title :: (name: string) -> string { + index: s32 = 0 + while index < cast(s32)name.count { + byte: u8 = name[index] + if index == 0 && byte >= 97 && byte <= 122 { + byte = byte - 32 + } else if index > 0 && byte >= 65 && byte <= 90 { + byte = byte + 32 + } + title_bytes[index] = byte + index += 1 + } + return TextView(title_bytes[0:name.count]) +} + +Contains :: (text: string, part: string) -> bool { + at: s32 = 0 + while at + cast(s32)part.count <= cast(s32)text.count { + index: s32 = 0 + while index < cast(s32)part.count && text[at + index] == part[index] { + index += 1 + } + if index == cast(s32)part.count { return true } + at += 1 + } + return false +} + +// Chunk boundaries: chunk i covers source[chunk_starts[i]:chunk_starts[i + 1]]. +chunk_starts: [256]s32; +chunk_count: s32 = 0; + +Chunk :: (source: string) -> bool { + chunk_count = 0 + chunk_starts[0] = 0 + current: s32 = 0 + line: s32 = 0 + while line < cast(s32)source.count { + end: s32 = line + while end < cast(s32)source.count && source[end] != 10 { end += 1 } + if end < cast(s32)source.count { end += 1 } + length: s32 = end - line + if current > 0 && current + length > ChunkBytes { + chunk_count += 1 + if chunk_count >= 255 { return false } + chunk_starts[chunk_count] = line + current = 0 + } + current += length + line = end + } + chunk_count += 1 + chunk_starts[chunk_count] = cast(s32)source.count + index: s32 = 0 + while index < chunk_count { + if chunk_starts[index + 1] - chunk_starts[index] > ChunkLimit { return false } + index += 1 + } + return true +} + +// Builds the module text for the pack in module_bytes. +Generate :: (name: string, source: string) -> bool { + title: string = Title(name) + module_length = 0 + ok: bool = Emit("// Generated from styles/") && Emit(name) && + Emit(".kss by tools/style_pack_module.zi.\n") && + Emit("// Edit the .kss and run `make style-packs`; do not edit this file.\n") && + Emit("style_pack_") && Emit(name) && Emit("_bytes: [") && + EmitNumber(cast(s32)source.count + 1) && Emit("]u8;\n") && + Emit("style_pack_") && Emit(name) && Emit("_length: s32 = 0;\n\n") + index: s32 = 0 + while ok && index < chunk_count { + ok = Emit("StylePack") && Emit(title) && Emit("Chunk") && EmitNumber(index) && + Emit(" :: #string KSSEND\n") && + Emit(source[chunk_starts[index]:chunk_starts[index + 1]]) && + Emit("KSSEND;\n\n") + index += 1 + } + ok = ok && Emit("StylePack") && Emit(title) && Emit("Append :: (chunk: string) {\n") && + Emit(" index: s32 = 0\n while index < cast(s32)chunk.count {\n") && + Emit(" style_pack_") && Emit(name) && Emit("_bytes[style_pack_") && + Emit(name) && Emit("_length + index] = chunk[index]\n") && + Emit(" index += 1\n }\n") && + Emit(" style_pack_") && Emit(name) && Emit("_length += cast(s32)chunk.count\n}\n\n") && + Emit("// The ") && Emit(name) && Emit(" style pack's KSS source.\n#program_export\nStylePack") && + Emit(title) && Emit(" :: () -> string {\n") && + Emit(" if style_pack_") && Emit(name) && Emit("_length == 0 {\n") + index = 0 + while ok && index < chunk_count { + ok = Emit(" StylePack") && Emit(title) && Emit("Append(StylePack") && + Emit(title) && Emit("Chunk") && EmitNumber(index) && Emit(")\n") + index += 1 + } + return ok && Emit(" }\n") && + Emit(" return TextView(style_pack_") && Emit(name) && Emit("_bytes[0:style_pack_") && + Emit(name) && Emit("_length])\n}\n") +} + +path_bytes: [512]u8; + +Path :: (before: string, name: string, after: string) -> string { + length: s32 = 0 + parts: [3]string + parts[0] = before + parts[1] = name + parts[2] = after + part: s32 = 0 + while part < 3 { + index: s32 = 0 + while index < cast(s32)parts[part].count && length < 511 { + path_bytes[length] = parts[part][index] + length += 1 + index += 1 + } + part += 1 + } + return TextView(path_bytes[0:length]) +} + +ValidName :: (name: string) -> bool { + if name.count == 0 || name.count > 32 { return false } + index: s32 = 0 + while index < cast(s32)name.count { + byte: u8 = name[index] + if !((byte >= 97 && byte <= 122) || (byte >= 48 && byte <= 57) || byte == 95) { + return false + } + index += 1 + } + return true +} + +#program_export +main :: (argc: s32, argv: **s8) -> s32 { + if argc != 3 { + WriteText(2, "usage: style_pack_module write|check NAME\n") + return 2 + } + mode: string = TextFromCString(cast(*u8)argv[1]) + name: string = TextFromCString(cast(*u8)argv[2]) + if !(mode == "write" || mode == "check") || !ValidName(name) { + WriteText(2, "usage: style_pack_module write|check NAME\n") + return 2 + } + + file: FileHandle = OpenRead(Path("styles/", name, ".kss")) + if !file.valid { return Fail("cannot read the pack in styles", name) } + read: s64 = ReadAt(file, source_bytes[:], 0) + CloseFile(file) + if read <= 0 || read >= cast(s64)source_bytes.count { + return Fail("the pack is empty or too large", name) + } + source: string = TextView(source_bytes[0:cast(s32)read]) + if Contains(source, "KSSEND") || source[source.count - 1] != 10 { + return Fail("the pack must end in a newline and not contain KSSEND", name) + } + if !Chunk(source) { return Fail("a line is too long to embed", name) } + if !Generate(name, source) { return Fail("the module is too large", name) } + module: string = TextView(module_bytes[0:module_length]) + target: string = Path("src/style_pack_", name, ".zi") + + if mode == "check" { + disk: FileHandle = OpenRead(target) + if !disk.valid { return Fail("its module in src is missing; run `make style-packs`", name) } + count: s64 = ReadAt(disk, disk_bytes[:], 0) + CloseFile(disk) + if count != cast(s64)module_length || + TextView(disk_bytes[0:cast(s32)count]) != module { + return Fail("its module in src differs from the pack; run `make style-packs`", name) + } + return 0 + } + output: FileHandle = OpenReplace(target) + if !output.valid { return Fail("cannot write its module in src", name) } + written: s64 = WriteAt(output, module_bytes[0:module_length], 0) + CloseFile(output) + if written != cast(s64)module_length { return Fail("cannot write its module in src", name) } + WriteText(1, target) + WriteText(1, "\n") + return 0 +} diff --git a/ziran.lock b/ziran.lock new file mode 100644 index 0000000..74f8746 --- /dev/null +++ b/ziran.lock @@ -0,0 +1,31 @@ +{ + "format": 1, + "packages": [ + { + "commit": "fafa2cea97d0cb379d832086ba45a989fa6f9c8e", + "dependencies": {}, + "id": "p5a77c9c35110e925", + "name": "raylib", + "ref": "fafa2cea97d0cb379d832086ba45a989fa6f9c8e", + "source": true, + "url": "https://github.com/raysan5/raylib.git" + }, + { + "commit": "29a8d453158131c25e994c5e628e7ce2d412ef03", + "dependencies": {"raylib": "p5a77c9c35110e925"}, + "id": "p63ea47daa6d3e9c3", + "name": "Kryon", + "ref": "master", + "url": "https://github.com/kryonlabs/kryon.git" + } + ], + "root": { + "dependencies": {"kryon": "p63ea47daa6d3e9c3"}, + "name": "Kss" + }, + "toolchain": { + "commit": "4af62b0c960a3a9bb88b80a618d2a91c4937189c", + "ref": "master", + "url": "https://github.com/ziranlang/ziran.git" + } +} diff --git a/ziran.toml b/ziran.toml new file mode 100644 index 0000000..646e5bd --- /dev/null +++ b/ziran.toml @@ -0,0 +1,15 @@ +[package] +name = "Kss" +module_roots = ["src", "tests", "tools"] + +[toolchain] +git = "https://github.com/ziranlang/ziran.git" +ref = "master" + +[dependencies.kryon] +git = "https://github.com/kryonlabs/kryon.git" +ref = "master" + +[exports] +Kss = "src/Kss/module.zi" +SystemStyle = "src/system_style.zi"