macroonz-compiler 0.1.0

Deterministic Rust code generation for procedural macros: plan, render, close, explain, and bind one sealed expansion from declared input.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! The two declaration roads, exercised from outside: a lawful body in, the builder or exploration module out, and every malformed clause refused at capture.
//!
//! The positive lanes hold the emitted text to the shapes the grammars promise; the refusal lanes reverse one clause each — an undeclared key, a doubled name, a foreign endpoint, an unreadable phrase, a missing fact, an empty declaration, a separator separating nothing.

mod codec_generated_behavior;

use macroonz_compiler::descriptor::Grammar;
use macroonz_compiler::descriptor::concurrency::ConcurrencyModule;
use macroonz_compiler::descriptor::door;
use macroonz_compiler::descriptor::network::NetworkModule;
use macroonz_compiler::diagnostic::Door as DiagnosticDoor;
use macroonz_compiler::kind::Kind;
use macroonz_compiler::request::Door as RequestDoor;
use macroonz_compiler::{CrateBinding, Diagnostic, Door, Expansion, Phase, Producer, TextCapture};

/// The one value that says who is asking.
const DOOR: Door = Door::declared(
    "lane",
    "lane.declared.grammar",
    "lane::declared",
    CrateBinding::declared("demo"),
    Producer {
        namespace: "lane",
        name: "declared",
    },
);

/// Every approved public path names the one request-owned type.
const _: RequestDoor = DOOR;
const _: DiagnosticDoor = DOOR;
const _: Door = DOOR;

/// The network grammar this lane registers.
const NETWORK: Grammar = Grammar {
    attribute: "network",
};

/// The concurrency grammar this lane registers.
const CONCURRENCY: Grammar = Grammar {
    attribute: "concurrency",
};

/// One lawful network declaration body.
const NETWORK_BODY: &str = r#"
    module = net,
    namespace = "lane",
    nodes = [client, server],
    link forward = client to server,
    link back = server to client,
    schedule quiet = [],
    schedule outage = [
        drop forward at 0,
        delay forward at 1 by 2,
        duplicate back at 0,
        partition forward from 0 until 3,
    ],
"#;

/// One lawful concurrency declaration body.
const CONCURRENCY_BODY: &str = r#"
    module = explorations,
    namespace = "lane",
    transfers_hold {
        population = "transfer-orders",
        interleavings = 16,
        samples = 32,
        seed = 11,
    },
"#;

/// The network road walked over one source, or nothing where the lane's own source did not capture.
fn networked(source: &str) -> Option<Result<Expansion<NetworkModule>, Diagnostic>> {
    let bound = format!("harness = renamed_facade::harness, {source}");
    networked_raw(&bound)
}

/// The network road walked over an already-bound source.
fn networked_raw(source: &str) -> Option<Result<Expansion<NetworkModule>, Diagnostic>> {
    let read = TextCapture::read(source).ok()?;
    Some(door::network(read.input().clone(), NETWORK, &DOOR))
}

/// The concurrency road walked over one source, on the same terms.
fn concurrent(source: &str) -> Option<Result<Expansion<ConcurrencyModule>, Diagnostic>> {
    let bound = format!("harness = renamed_facade::harness, {source}");
    concurrent_raw(&bound)
}

/// The concurrency road walked over an already-bound source.
fn concurrent_raw(source: &str) -> Option<Result<Expansion<ConcurrencyModule>, Diagnostic>> {
    let read = TextCapture::read(source).ok()?;
    Some(door::concurrency(read.input().clone(), CONCURRENCY, &DOOR))
}

/// The declaration-site text one expansion emits.
fn emitted<K: Kind>(expansion: &Expansion<K>) -> Option<String> {
    expansion
        .emit()
        .tokens()
        .map(macroonz_compiler::GeneratedTree::inspected)
}

/// A lawful network declaration becomes one builder module: the fault enum, the topology, and one function per schedule.
#[test]
fn a_network_declaration_becomes_its_builder_module() -> Result<(), ()> {
    let expansion = networked(NETWORK_BODY).ok_or(())?.ok().ok_or(())?;
    let text = emitted(&expansion).ok_or(())?;
    for spelled in [
        ":: renamed_facade :: harness",
        "pub mod net",
        "pub enum Fault",
        "pub fn topology",
        "pub fn quiet",
        "pub fn outage",
        "Topology",
        "NetworkSchedule",
        "LinkDiscipline",
        "DropAt",
        "DelayAt",
        "DuplicateAt",
        "Partition",
        "TickSpan",
    ] {
        assert!(
            text.contains(spelled),
            "the module does not spell {spelled}"
        );
    }
    assert_eq!(text.matches("pub fn").count(), 3usize);
    Ok(())
}

/// A lawful concurrency declaration becomes one exploration module: the fault enum and one generic function per row.
#[test]
fn a_concurrency_declaration_becomes_its_exploration_module() -> Result<(), ()> {
    let expansion = concurrent(CONCURRENCY_BODY).ok_or(())?.ok().ok_or(())?;
    let text = emitted(&expansion).ok_or(())?;
    for spelled in [
        ":: renamed_facade :: harness",
        "pub mod explorations",
        "pub enum Fault",
        "pub fn transfers_hold",
        "StrandSet",
        "TransitionContract",
        "ExplorationBound",
        "PopulationRef",
        "RootSeed",
        "concluded",
    ] {
        assert!(
            text.contains(spelled),
            "the module does not spell {spelled}"
        );
    }
    assert_eq!(text.matches("pub fn").count(), 1usize);
    Ok(())
}

/// A direct harness binding is required, singular, bounded, and a fully consumed Rust path.
#[test]
fn a_direct_harness_binding_refuses_every_unwritable_shape() -> Result<(), ()> {
    let malformed = [
        NETWORK_BODY.to_owned(),
        format!("harness = , {NETWORK_BODY}"),
        format!("harness = type, {NETWORK_BODY}"),
        format!("harness = renamed:harness, {NETWORK_BODY}"),
        format!("harness = renamed::, {NETWORK_BODY}"),
        format!("harness = one, harness = two, {NETWORK_BODY}"),
        format!("harness = a::b::c::d::e::f::g::h::i, {NETWORK_BODY}"),
    ];
    for source in &malformed {
        let refusal = networked_raw(source).ok_or(())?.err().ok_or(())?;
        assert_eq!(refusal.phase(), Phase::Capture, "{source} did not refuse");
        assert!(
            refusal.summary().contains("(at "),
            "{source} carries no coordinate"
        );
    }

    let concurrency_without_binding = CONCURRENCY_BODY;
    let refusal = concurrent_raw(concurrency_without_binding)
        .ok_or(())?
        .err()
        .ok_or(())?;
    assert_eq!(refusal.phase(), Phase::Capture);
    assert!(refusal.summary().contains("(at "));
    Ok(())
}

/// The direct binding moves the declaration's own content commitment and the exact generated path.
#[test]
fn a_direct_harness_binding_is_committed_content() -> Result<(), ()> {
    let one_source = format!("harness = mh, {NETWORK_BODY}");
    let facade_source = format!("harness = renamed_facade::harness, {NETWORK_BODY}");
    let one = networked_raw(&one_source).ok_or(())?.ok().ok_or(())?;
    let facade = networked_raw(&facade_source).ok_or(())?.ok().ok_or(())?;

    assert_ne!(
        one.plan().account().content_commitment(),
        facade.plan().account().content_commitment()
    );
    assert!(emitted(&one).ok_or(())?.contains(":: mh :: network"));
    assert!(
        emitted(&facade)
            .ok_or(())?
            .contains(":: renamed_facade :: harness :: network")
    );
    Ok(())
}

/// A malformed network declaration refuses at capture, clause by clause.
#[test]
fn a_malformed_network_declaration_refuses_at_capture() -> Result<(), ()> {
    let missing_module = r#"
        namespace = "lane",
        nodes = [client, server],
        link forward = client to server,
    "#;
    let doubled_node = r#"
        module = net,
        namespace = "lane",
        nodes = [client, client],
        link forward = client to client,
    "#;
    let foreign_endpoint = r#"
        module = net,
        namespace = "lane",
        nodes = [client, server],
        link forward = client to stranger,
    "#;
    let undrawn_link = r#"
        module = net,
        namespace = "lane",
        nodes = [client, server],
        link forward = client to server,
        schedule outage = [drop sideways at 0],
    "#;
    let unread_phrase = r#"
        module = net,
        namespace = "lane",
        nodes = [client, server],
        link forward = client to server,
        schedule outage = [scramble forward at 0],
    "#;
    let undeclared_key = r#"
        module = net,
        namespace = "lane",
        nodes = [client, server],
        link forward = client to server,
        latency = 3,
    "#;
    let empty_then_stated_nodes = r#"
        module = net,
        namespace = "lane",
        nodes = [],
        nodes = [client, server],
        link forward = client to server,
    "#;
    let unseparated_nodes = r#"
        module = net,
        namespace = "lane",
        nodes = [client server],
        link forward = client to server,
    "#;
    let reserved_schedule = r#"
        module = net,
        namespace = "lane",
        nodes = [client, server],
        link forward = client to server,
        schedule topology = [],
    "#;
    let keyword_module = r#"
        module = type,
        namespace = "lane",
        nodes = [client, server],
        link forward = client to server,
    "#;
    let oversized_position = r#"
        module = net,
        namespace = "lane",
        nodes = [client, server],
        link forward = client to server,
        schedule outage = [drop forward at 4294967296],
    "#;
    for source in [
        missing_module,
        doubled_node,
        foreign_endpoint,
        undrawn_link,
        unread_phrase,
        undeclared_key,
        empty_then_stated_nodes,
        unseparated_nodes,
        reserved_schedule,
        keyword_module,
        oversized_position,
    ] {
        let refusal = networked(source).ok_or(())?.err().ok_or(())?;
        assert_eq!(refusal.phase(), Phase::Capture, "{source} did not refuse");
    }
    Ok(())
}

/// A separator separating nothing refuses at its own comma — leading or doubled — while a trailing comma stays ordinary Rust.
#[test]
fn a_network_separator_separating_nothing_refuses() -> Result<(), ()> {
    let leading_separator = r#"
        , module = net,
        namespace = "lane",
        nodes = [client, server],
        link forward = client to server,
    "#;
    let doubled_separator = r#"
        module = net,,
        namespace = "lane",
        nodes = [client, server],
        link forward = client to server,
    "#;
    for source in [leading_separator, doubled_separator] {
        let refusal = networked(source).ok_or(())?.err().ok_or(())?;
        assert_eq!(refusal.phase(), Phase::Capture, "{source} did not refuse");
        assert!(
            refusal
                .summary()
                .contains("a separator stands where no clause does"),
            "{source} does not name the dangling separator"
        );
        assert!(
            refusal.summary().contains("(at "),
            "{source} carries no coordinate"
        );
    }
    Ok(())
}

/// A concurrency separator separating nothing refuses at its own comma, named as such — at the declaration level and inside a row body.
#[test]
fn a_concurrency_separator_separating_nothing_refuses() -> Result<(), ()> {
    let doubled_separator = r#"
        module = explorations,,
        namespace = "lane",
        transfers_hold { population = "a", interleavings = 1, samples = 1, seed = 1 },
    "#;
    let dangling_row_separator = r#"
        module = explorations,
        namespace = "lane",
        transfers_hold { population = "a", interleavings = 1,, samples = 1, seed = 1 },
    "#;
    for source in [doubled_separator, dangling_row_separator] {
        let refusal = concurrent(source).ok_or(())?.err().ok_or(())?;
        assert_eq!(refusal.phase(), Phase::Capture, "{source} did not refuse");
        assert!(
            refusal
                .summary()
                .contains("a separator stands where no clause does"),
            "{source} does not name the dangling separator"
        );
        assert!(
            refusal.summary().contains("(at "),
            "{source} carries no coordinate"
        );
    }
    Ok(())
}

/// A keyword cannot name a rendered item: every programmatic name constructor reads the composed law, not the alphabet alone.
///
/// Paths read position-aware the way the language does: the stamp site root admits `crate`, `self`, or a leading run of `super` at the root, the codec path types its qualifier as the rooting — so every later segment is an item name the keyword roster refuses.
#[test]
fn a_keyword_cannot_name_a_rendered_item() {
    assert!(macroonz_compiler::descriptor::ModuleName::declared("type").is_err());
    assert!(macroonz_compiler::descriptor::SupportName::declared("mod").is_err());
    assert!(macroonz_compiler::descriptor::TypeName::declared("gen").is_err());
    assert!(macroonz_compiler::descriptor::FunctionName::declared("fn").is_err());
    assert!(macroonz_compiler::descriptor::ModuleName::declared("lawful_name").is_ok());
    assert!(macroonz_compiler::stamp::StampName::declared("type").is_err());
    assert!(macroonz_compiler::support::SupportName::declared("loop").is_err());
    assert!(macroonz_compiler::codec::ModuleSpelling::spelled("type").is_err());
    assert!(macroonz_compiler::stamp::SiteRoot::spelled(vec!["crate".to_owned()]).is_ok());
    assert!(
        macroonz_compiler::stamp::SiteRoot::spelled(vec!["crate".to_owned(), "type".to_owned()])
            .is_err()
    );
    assert!(
        macroonz_compiler::stamp::SiteRoot::spelled(vec![
            "super".to_owned(),
            "super".to_owned(),
            "stamps".to_owned(),
        ])
        .is_ok()
    );
    assert!(
        macroonz_compiler::stamp::SiteRoot::spelled(vec!["stamps".to_owned(), "self".to_owned()])
            .is_err()
    );
    assert!(
        macroonz_compiler::codec::CodecTypePath::spelled(
            macroonz_compiler::codec::PathRooting::ParentScoped,
            vec!["Thing".to_owned()],
        )
        .is_ok()
    );
    assert!(
        macroonz_compiler::codec::CodecTypePath::spelled(
            macroonz_compiler::codec::PathRooting::InScope,
            vec!["self".to_owned(), "Thing".to_owned()],
        )
        .is_err()
    );
}

/// A malformed concurrency declaration refuses at capture, clause by clause.
#[test]
fn a_malformed_concurrency_declaration_refuses_at_capture() -> Result<(), ()> {
    let missing_fact = r#"
        module = explorations,
        namespace = "lane",
        transfers_hold {
            population = "transfer-orders",
            interleavings = 16,
            samples = 32,
        },
    "#;
    let doubled_row = r#"
        module = explorations,
        namespace = "lane",
        transfers_hold { population = "a", interleavings = 1, samples = 1, seed = 1 },
        transfers_hold { population = "b", interleavings = 1, samples = 1, seed = 1 },
    "#;
    let no_rows = r#"
        module = explorations,
        namespace = "lane",
    "#;
    let unread_number = r#"
        module = explorations,
        namespace = "lane",
        transfers_hold { population = "a", interleavings = many, samples = 1, seed = 1 },
    "#;
    let oversized_number = r#"
        module = explorations,
        namespace = "lane",
        transfers_hold { population = "a", interleavings = 4294967296, samples = 1, seed = 1 },
    "#;
    let keyword_module = r#"
        module = type,
        namespace = "lane",
        transfers_hold { population = "a", interleavings = 1, samples = 1, seed = 1 },
    "#;
    let keyword_row = r#"
        module = explorations,
        namespace = "lane",
        type { population = "a", interleavings = 1, samples = 1, seed = 1 },
    "#;
    for source in [
        missing_fact,
        doubled_row,
        no_rows,
        unread_number,
        oversized_number,
        keyword_module,
        keyword_row,
    ] {
        let refusal = concurrent(source).ok_or(())?.err().ok_or(())?;
        assert_eq!(refusal.phase(), Phase::Capture, "{source} did not refuse");
    }
    Ok(())
}