Files
css/src/kss_parser.zi
T
waozi 95387b23f5 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.
2026-09-29 20:56:55 -03:00

358 lines
8.8 KiB
Ziran

#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";