Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / tools / gen_css_values / module.lucb

#==============================================================================================
#
#   gen_css_values - the generator of the engine's value-dependent CSS fragments
#
#   DESCRIPTION:
#       Ladybird's generate_libweb_css_*.py programs write the CSS data tables and, in the
#       same generated files, functions that build or parse style values. luce-browser-css's
#       tools/gen_css writes the data into css_data; the value-dependent functions belong to
#       the engine, which has the StyleValue types (DESIGN.md §4.1.3, §5). This program writes
#       them into src/web/generated, from the same JSON, copied into
#       data/css:
#
#           math_functions.lucb   MathFunctions.json: Parser::parse_math_function and its
#                                 parse_rounding_strategy (Build/.../CSS/MathFunctions.cpp,
#                                 generate_libweb_css_math_functions.py)
#           generated_css_numeric_factory_methods.lucb
#                                 Units.json: CSS.number(), CSS.px(), ...
#                                 (Build/.../CSS/GeneratedCSSNumericFactoryMethods.cpp,
#                                 generate_libweb_css_numeric_factory_methods.py)
#           generated_value_types_parsing.lucb
#                                 ValueTypes.json: Parser::parse_<type>_value
#                                 (Build/.../CSS/Parser/GeneratedValueTypesParsing.cpp,
#                                 generate_libweb_css_value_types_parsing.py and
#                                 Meta/Utils/CSSGrammar)
#           generated_css_style_properties.lucb
#                                 Properties.json: the GeneratedCSSStyleProperties mixin's
#                                 per-property setters and getters
#                                 (Build/.../CSS/GeneratedCSSStyleProperties.h and .cpp,
#                                 generate_libweb_css_style_properties.py)
#           property_id.lucb      Properties.json, Enums.json, LogicalPropertyGroups.json:
#                                 property_initial_value, property_accepts_type and the other
#                                 value-dependent functions of Build/.../CSS/PropertyID.cpp
#                                 (generate_libweb_css_property_id.py)
#           descriptor_id.lucb    Descriptors.json: descriptor_initial_value
#                                 (Build/.../CSS/DescriptorID.cpp,
#                                 generate_libweb_css_descriptors.py)
#           environment_variable.lucb
#                                 EnvironmentVariables.json: environment_variable_type
#                                 (Build/.../CSS/EnvironmentVariable.cpp,
#                                 generate_libweb_css_environment_variables.py)
#
#       The JSON reader and the text helpers are luce-browser-css's (tools/gen_css/json.lucb,
#       text.lucb), copied.
#
#       Usage: gen_css_values DATA_DIRECTORY OUTPUT_DIRECTORY, e.g. gen_css_values data/css
#       src/web/generated. tests/generated regenerates into build/tests/generated
#       and compares with the committed fragments.
#
#       Copyright (c) 2022-2026, the Ladybird developers.
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-2-Clause (see data/css/LICENSE)
#
#==============================================================================================

import c
from luce_std import collections, files
import strings

## Generate every fragment from the JSON of `arguments[1]` into `arguments[2]`.
pub func main(arguments: c.str[]) -> i32!:
    if arguments.length != 3:
        print("usage: gen_css_values DATA_DIRECTORY OUTPUT_DIRECTORY")
        return 2
    let data_directory = try str(arguments[1])
    let output_directory = try str(arguments[2])

    try write_output(output_directory, "math_functions.lucb", try emit_math_functions(try read_json(data_directory, "MathFunctions.json")))
    try write_output(output_directory, "generated_css_numeric_factory_methods.lucb", try emit_numeric_factory_methods(try read_json(data_directory, "Units.json")))
    try write_output(output_directory, "generated_value_types_parsing.lucb", try emit_value_types_parsing(try read_json(data_directory, "ValueTypes.json")))
    try write_output(output_directory, "generated_css_style_properties.lucb", try emit_generated_css_style_properties(try read_json(data_directory, "Properties.json")))
    try write_output(output_directory, "property_id.lucb", try emit_property_id_values(try read_json(data_directory, "Properties.json"), try read_json(data_directory, "Enums.json"),
                                                                                       try read_json(data_directory, "LogicalPropertyGroups.json")))
    try write_output(output_directory, "descriptor_id.lucb", try emit_descriptor_id_values(try read_json(data_directory, "Descriptors.json")))
    try write_output(output_directory, "environment_variable.lucb", try emit_environment_variable_values(try read_json(data_directory, "EnvironmentVariables.json")))
    return 0

## The JSON file `name` of `directory`, parsed. Its numbers borrow the file's bytes, which the
## whole run keeps, so they are never released.
func read_json(directory: str, name: str) -> Json*!:
    let data = try files.read(try c_cat([directory, "/", name]))
    return try json_parse(data.value)

## Write `text` to the file `name` of `directory`.
func write_output(directory: str, name: str, text: str) -> !:
    try files.write(try c_cat([directory, "/", name]), text.bytes)