dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / tools / gen_aria_roles / text.lucb
#==============================================================================================
#
# text - names, lists and output for the emitters (copied from tools/gen_css_values)
#
# DESCRIPTION:
# The Luce spellings the skeleton generator gives C++ names (DESIGN.md ยง4.2: an enumerator `BackgroundColor` is the case
# `background_color`, reserved and core words take a trailing `_`), a growable list of
# strings, and the `Out` sink the emitters write Luce into. Everything is allocated with
# the current allocator and lives for the whole run.
#
#==============================================================================================
# mark: Strings =================================================================================
## The concatenation of `parts`.
func cat(parts: const str[]) -> str!:
var builder: collections.Buffer
for part in parts:
try builder.put_text(part)
return builder.text()
## The concatenation of `parts` as the C string the files module takes.
func c_cat(parts: const str[]) -> c.str!:
var builder: collections.Buffer
for part in parts:
try builder.put_text(part)
return try builder.terminated()
## ASCII lowercase of one byte.
func lower_byte(byte: u8) -> u8:
if byte >= 'A' and byte <= 'Z':
return byte + 32
return byte
func is_upper(byte: u8) -> bool:
return byte >= 'A' and byte <= 'Z'
func is_lower(byte: u8) -> bool:
return byte >= 'a' and byte <= 'z'
func is_digit(byte: u8) -> bool:
return byte >= '0' and byte <= '9'
## `text[start:end]` by bytes.
func slice(text: str, start: usize, end: usize) -> str:
return (str)text.bytes[start..<end]
## ASCII lowercase of `text` (Python's `str.lower()` on the ASCII inputs).
func lower(text: str) -> str!:
var builder: collections.Buffer
for byte in text.bytes:
let lowered: u8[1] = [lower_byte(byte)]
_ = try builder.write(lowered)
return builder.text()
# mark: Meta/Utils/utils.py =====================================================================
# mark: Luce names (the skeleton generator's names.py) ==========================================
## The words of a C++ identifier, as names.py's `words`: each `_`-separated part split by the
## pattern `[A-Z]+(?=[A-Z][a-z])|[A-Z]?[a-z]+|[A-Z]+|\d+`.
func identifier_words(identifier: str) -> StrList!:
var result = try StrList.create()
let bytes = identifier.bytes
var index: usize = 0
while index < bytes.length:
let byte = bytes[index]
if is_upper(byte):
var end = index
while end < bytes.length and is_upper(bytes[end]):
end += 1
if end - index >= 2 and end < bytes.length and is_lower(bytes[end]):
# [A-Z]+(?=[A-Z][a-z]): the uppercase run without its last letter.
try result.append(slice(identifier, index, end - 1))
index = end - 1
elif end - index == 1 and end < bytes.length and is_lower(bytes[end]):
# [A-Z]?[a-z]+
var word_end = end
while word_end < bytes.length and is_lower(bytes[word_end]):
word_end += 1
try result.append(slice(identifier, index, word_end))
index = word_end
else:
# [A-Z]+
try result.append(slice(identifier, index, end))
index = end
elif is_lower(byte):
var end = index
while end < bytes.length and is_lower(bytes[end]):
end += 1
try result.append(slice(identifier, index, end))
index = end
elif is_digit(byte):
var end = index
while end < bytes.length and is_digit(bytes[end]):
end += 1
try result.append(slice(identifier, index, end))
index = end
else:
index += 1
return result
## names.py's `snake`: `BackgroundColor` is `background_color`; digits stay with the word
## before them (`Matrix3d` is `matrix3_d`).
func luce_snake(identifier: str) -> str!:
let words = try identifier_words(identifier)
var builder: collections.Buffer
var first = true
for index in 0..<words.count:
let word = words.at(index)
if is_digit(word.bytes[0]) and not first:
try builder.put_text(word)
continue
if not first:
try builder.put_text("_")
try builder.put_text(try lower(word))
first = false
return builder.text()
## The reserved and core words of luce-base and the standard module names (names.py).
let reserved_words: str[53] = ["alloc", "and", "as", "asm", "break", "catch", "const", "continue",
"defer", "elif", "else", "enum", "errdefer", "export", "extern", "false", "for", "free", "from",
"func", "goto", "if", "import", "in", "interface", "let", "local", "match", "mutating", "new",
"none", "not", "or", "pub", "recover", "return", "self", "static", "struct", "test", "true",
"try", "type", "union", "var", "volatile", "while", "with", "class", "spawn", "assert",
"discard", "error"]
let core_words: str[32] = ["trap", "hash", "print", "format", "sizeof", "alignof", "offsetof", "hex",
"bin", "pad", "bool", "i8", "i16", "i32", "i64", "isize", "u8", "u16", "u32", "u64", "usize",
"f16", "f32", "f64", "char", "str", "unit", "never", "void", "fmt", "Error", "ErrorCode"]
let std_module_words: str[10] = ["memory", "io", "os", "thread", "sync", "atomic", "c", "testing",
"runtime", "luce"]
## names.py's `safe`: a reserved or core word takes a trailing `_`.
func luce_safe(name: str) -> str!:
for word in reserved_words:
if word == name:
return try cat([name, "_"])
for word in core_words:
if word == name:
return try cat([name, "_"])
for word in std_module_words:
if word == name:
return try cat([name, "_"])
if name == "_":
return "__"
return name
## The Luce case of the C++ enumerator `title` (emit.py's emit_enum: `none` is `none_`).
func luce_case(title: str) -> str!:
return try luce_safe(try luce_snake(title))
# mark: Lists ===================================================================================
## A growable list of strings.
struct StrList:
var items: str[]
var count: usize
static func create() -> StrList!:
return StrList(items = try new str[16], count = 0)
func append(value: str) -> !:
if self.count == self.items.length:
let grown = try new str[self.items.length * 2]
for index in 0..<self.count:
grown[index] = self.items[index]
self.items = grown
self.items[self.count] = value
self.count += 1
func at(index: usize) -> str:
assert(index < self.count)
return self.items[index]
func contains(value: str) -> bool:
for index in 0..<self.count:
if self.items[index] == value:
return true
return false
func view() -> const str[]:
return self.items[..<self.count]
# mark: Output ==================================================================================
## The text of a generated fragment.
struct Out:
var builder: collections.Buffer
static func create() -> Out!:
return Out()
## Append `text`.
func put(text: fmt) -> !:
_ = try self.builder.write(text)
## Append `text` and a line feed.
func line(text: fmt) -> !:
_ = try self.builder.write(text)
try self.builder.put_text("\n")
## Append an empty line.
func blank() -> !:
try self.builder.put_text("\n")
func text() -> str:
return self.builder.text()
## The standard header box of a generated fragment.
func put_header(out: Out*, name: str, summary: str, description: const str[]) -> !:
let rule = "#=============================================================================================="
try out.line(rule)
try out.line("#")
try out.line(f"# {name} - {summary}")
try out.line("#")
try out.line("# DESCRIPTION:")
for line in description:
if line.length == 0:
try out.line("#")
else:
try out.line(f"# {line}")
try out.line("#")
try out.line(rule)
## A `# mark:` line padded to the house width.
func put_mark(out: Out*, title: str) -> !:
try out.blank()
var text: collections.Buffer
try text.put_text("# mark: ")
try text.put_text(title)
try text.put_text(" ")
while text.length() < 97:
try text.put_text("=")
try out.line(f"{text.text()}")
try out.blank()