Skip to main content

zerodds_lang_rules/rule/
lua.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 ZeroDDS Contributors
3//! The Lua rule catalogue. Spec §2.
4//!
5//! The same constructs the other catalogues cover, mapped onto Lua's
6//! tree. Reading this against `rule/go.rs` is the point, because the two
7//! answer four of the same questions differently:
8//!
9//! - A module becomes a **nested table** where Go's becomes a name
10//!   prefix. A Go package is a directory and cannot nest inside one file;
11//!   a Lua table nests, so `::common::util::Location` stays
12//!   `M.common.util.Location` and the IDL scope survives the mapping.
13//! - A struct becomes a **descriptor and a constructor**, not a type.
14//!   Lua declares nothing, so there is no node for the shape itself —
15//!   what the rule creates is the table that describes it and the
16//!   function that builds one.
17//! - An enumeration becomes **two maps and an ordered list**, where Go
18//!   gets a defined integer type and one constant per enumerator. Lua
19//!   has no constant and no defined type; a name-to-value map, its
20//!   inverse and the declaration order are the three facts an enumerator
21//!   carries, and each is a table entry.
22//! - A **typedef has a rule here and declares nothing**. Lua has no alias
23//!   to write, so what the rule creates is an entry in the descriptor
24//!   table naming the target — which is the only place a `typedef` is
25//!   recoverable from the generated file at all.
26
27use crate::rule::shape::{one_to_one, part_of};
28use seesaw_tgg::rules::format::RuleFile;
29
30/// The catalogue's own name, which a serialised rule file carries.
31const CATALOGUE_NAME: &str = "lua";
32
33/// The name of the rule that maps a struct to a descriptor table.
34pub const STRUCT_TO_TABLE: &str = "struct-to-table";
35
36/// The name of the rule that maps a member to a descriptor entry.
37pub const MEMBER_TO_ENTRY: &str = "member-to-entry";
38
39/// The name of the rule that maps an enumeration to a value table.
40pub const ENUM_TO_TABLE: &str = "enum-to-table";
41
42/// The name of the rule that maps an enumerator to a value entry.
43pub const ENUMERATOR_TO_ENTRY: &str = "enumerator-to-entry";
44
45/// The name of the rule that maps a typedef to a descriptor entry
46/// naming its target.
47pub const ALIAS_TO_ENTRY: &str = "alias-to-entry";
48
49/// The name of the rule that maps a union to a descriptor table.
50pub const UNION_TO_TABLE: &str = "union-to-table";
51
52/// The name of the rule that maps a union case to a branch entry.
53pub const CASE_TO_ENTRY: &str = "case-to-entry";
54
55/// The name of the rule that maps a bitmask to a value table.
56pub const BITMASK_TO_TABLE: &str = "bitmask-to-table";
57
58/// The name of the rule that maps a bit to a value entry.
59pub const BIT_TO_ENTRY: &str = "bit-to-entry";
60
61/// The name of the rule that maps a bitset to a descriptor table.
62pub const BITSET_TO_TABLE: &str = "bitset-to-table";
63
64/// The name of the rule that maps a valuetype to a descriptor table.
65pub const VALUETYPE_TO_TABLE: &str = "valuetype-to-table";
66
67/// The name of the rule that maps a module to a nested table.
68pub const MODULE_TO_TABLE: &str = "module-to-table";
69
70/// The name of the rule that maps a constant to a module field.
71pub const CONST_TO_FIELD: &str = "const-to-field";
72
73/// The name of the rule that maps an exception to a descriptor table.
74pub const EXCEPTION_TO_TABLE: &str = "exception-to-table";
75
76/// The name of the rule that maps a native declaration to an opaque
77/// descriptor.
78pub const NATIVE_TO_TABLE: &str = "native-to-table";
79
80/// The name of the rule that maps an interface to a table of operation
81/// descriptors.
82pub const INTERFACE_TO_TABLE: &str = "interface-to-table";
83
84/// Every Lua rule.
85#[must_use]
86pub fn catalogue() -> RuleFile {
87    RuleFile {
88        // The format version the engine expects; it reports a mismatch
89        // rather than reading an older file as if it were this one.
90        format: 3,
91        name: CATALOGUE_NAME.to_string(),
92        rules: vec![
93            one_to_one(STRUCT_TO_TABLE, "Structure", "LuaStruct", "LuaStructCorr"),
94            part_of(
95                MEMBER_TO_ENTRY,
96                ("Member", "Structure", "LuaStructCorr"),
97                ("LuaMember", "LuaStruct", "LuaMemberCorr"),
98            ),
99            one_to_one(ENUM_TO_TABLE, "Enumeration", "LuaEnum", "LuaEnumCorr"),
100            part_of(
101                ENUMERATOR_TO_ENTRY,
102                ("Enumerator", "Enumeration", "LuaEnumCorr"),
103                ("LuaEnumerator", "LuaEnum", "LuaEnumeratorCorr"),
104            ),
105            one_to_one(ALIAS_TO_ENTRY, "Alias", "LuaAlias", "LuaAliasCorr"),
106            one_to_one(UNION_TO_TABLE, "Union", "LuaUnion", "LuaUnionCorr"),
107            part_of(
108                CASE_TO_ENTRY,
109                ("Case", "Union", "LuaUnionCorr"),
110                ("LuaCase", "LuaUnion", "LuaCaseCorr"),
111            ),
112            one_to_one(BITMASK_TO_TABLE, "Bitmask", "LuaBitmask", "LuaBitmaskCorr"),
113            part_of(
114                BIT_TO_ENTRY,
115                ("BitValue", "Bitmask", "LuaBitmaskCorr"),
116                ("LuaBitEntry", "LuaBitmask", "LuaBitCorr"),
117            ),
118            one_to_one(BITSET_TO_TABLE, "Bitset", "LuaBitset", "LuaBitsetCorr"),
119            one_to_one(VALUETYPE_TO_TABLE, "Valuetype", "LuaValue", "LuaValueCorr"),
120            one_to_one(MODULE_TO_TABLE, "Module", "LuaModule", "LuaModuleCorr"),
121            one_to_one(CONST_TO_FIELD, "Const", "LuaConst", "LuaConstCorr"),
122            one_to_one(EXCEPTION_TO_TABLE, "Exception", "LuaError", "LuaErrorCorr"),
123            one_to_one(NATIVE_TO_TABLE, "Native", "LuaNative", "LuaNativeCorr"),
124            one_to_one(
125                INTERFACE_TO_TABLE,
126                "Interface",
127                "LuaInterface",
128                "LuaInterfaceCorr",
129            ),
130        ],
131    }
132}