Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-css

Source of release 0.5.1

0.5.1 / tools / gen_css / emit_common.lucb

#==============================================================================================
#
#   emit_common - declarations every emitter writes the same way
#
#   DESCRIPTION:
#       An `enum class` of a generated header becomes an integer-backed Luce enum with the
#       enumerators numbered from zero in order and named as the skeleton generator names
#       them (DESIGN.md ยง4.2), so that the names the other packages use keep working. Every
#       generated declaration is documented with the C++ name and the generated file it
#       stands for.
#
#==============================================================================================

## `## <what> (Build/Libraries/LibWeb/CSS/<file>).`: the doc line of a generated declaration.
func put_doc(out: Out*, what: str, file: str) -> !:
    try out.line(f"## {what} (Build/Libraries/LibWeb/CSS/{file}).")

## An integer-backed enum `name` of `underlying` whose cases are the C++ enumerators `titles`
## (TitleCase), numbered from zero.
func put_enum(out: Out*, cpp_name: str, header: str, name: str, underlying: str, titles: const StrList*) -> !:
    try put_doc(out, cpp_name, header)
    try out.line(f"pub enum {name} as {underlying}:")
    var seen = try StrList.create()
    for index in 0..<titles.count:
        let case_name = try luce_case(titles.at(index))
        if seen.contains(case_name):
            trap(f"two enumerators of {cpp_name} are both named {case_name}")
        try seen.append(case_name)
        try out.line(f"    {case_name} = {index}")

## `Name.case`: the Luce spelling of the C++ `Name::Title`.
func enum_value(name: str, title: str) -> str!:
    return try cat([name, ".", try luce_case(title)])

## A `str` literal wrapped as an `ak.StringView` (`"text"sv`).
func string_view_literal(text: str) -> str!:
    return try cat(["ak.sv(", try literal(text), ")"])

## `"text"_fly_string`.
func fly_string_literal(text: str) -> str!:
    return try cat(["ak.fly_string_from_str(", try literal(text), ")"])

## `string.equals_ignoring_ascii_case("text"sv)` for the StringView parameter `variable`.
func equals_ignoring_ascii_case(variable: str, text: str) -> str!:
    return try cat(["ak.string_view_equals_ignoring_ascii_case(&", variable, ", ", try string_view_literal(text), ")"])

## The standard description of a generated fragment: which C++ files it stands for and which
## Python generator of Ladybird it follows.
func put_generated_header(out: Out*, name: str, summary: str, cpp_files: const str[], generator: str, input: str, notes: const str[]) -> !:
    var lines = try StrList.create()
    try lines.append("Generated by tools/gen_css from data/css; do not edit. The C++ it stands for, at")
    try lines.append(try cat(["Ladybird 47c82b38d0, is written by Meta/Generators/", generator]))
    try lines.append(try cat(["from ", input, ":"]))
    try lines.append("")
    for file in cpp_files:
        try lines.append(try cat(["    Build/Libraries/LibWeb/CSS/", file]))
    try lines.append("")
    if notes.length > 0:
        for note in notes:
            try lines.append(note)
        try lines.append("")
    try lines.append("Copyright (c) 2018-2026, the Ladybird developers.")
    try lines.append("Copyright (c) 2026 the luce-browser authors.")
    try lines.append("SPDX-License-Identifier: BSD-2-Clause (see LICENSE)")
    try put_header(out, name, summary, lines.view())

## `## Ported from Build/Libraries/LibWeb/CSS/<file> <cpp_name>.`: the doc line of a generated
## function.
func put_ported(out: Out*, file: str, cpp_name: str) -> !:
    try out.line(f"## Ported from Build/Libraries/LibWeb/CSS/{file} {cpp_name}.")

## No notes for put_generated_header.
func no_notes() -> const str[]:
    var empty: str[]
    return empty