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.
264 lines
9.1 KiB
Ziran
264 lines
9.1 KiB
Ziran
// 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
|
|
}
|