aontu 0.57.0 → 0.58.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/README.md +2 -2
  2. package/dist/agentsmd.js +1 -1
  3. package/dist/alias.d.ts +3 -0
  4. package/dist/alias.js +59 -0
  5. package/dist/alias.js.map +1 -0
  6. package/dist/aontu.d.ts +4 -2
  7. package/dist/aontu.js +11 -3
  8. package/dist/aontu.js.map +1 -1
  9. package/dist/cli.d.ts +3 -1
  10. package/dist/cli.js +475 -3
  11. package/dist/cli.js.map +1 -1
  12. package/dist/ctx.d.ts +2 -0
  13. package/dist/ctx.js +1 -0
  14. package/dist/ctx.js.map +1 -1
  15. package/dist/format.js +8 -2
  16. package/dist/format.js.map +1 -1
  17. package/dist/hints.js +40 -9
  18. package/dist/hints.js.map +1 -1
  19. package/dist/lang.js +354 -57
  20. package/dist/lang.js.map +1 -1
  21. package/dist/lower.d.ts +20 -0
  22. package/dist/lower.js +575 -0
  23. package/dist/lower.js.map +1 -0
  24. package/dist/lsp.d.ts +1 -1
  25. package/dist/lsp.js +1 -1
  26. package/dist/lsp.js.map +1 -1
  27. package/dist/mcp-server.js +1 -1
  28. package/dist/mcp.d.ts +1 -0
  29. package/dist/mcp.js +40 -3
  30. package/dist/mcp.js.map +1 -1
  31. package/dist/render.d.ts +53 -0
  32. package/dist/render.js +542 -0
  33. package/dist/render.js.map +1 -0
  34. package/dist/sigdecl.js +1 -1
  35. package/dist/sigdecl.js.map +1 -1
  36. package/dist/std.d.ts +2 -0
  37. package/dist/std.js +498 -2
  38. package/dist/std.js.map +1 -1
  39. package/dist/template.d.ts +5 -0
  40. package/dist/template.js +257 -0
  41. package/dist/template.js.map +1 -0
  42. package/dist/tsconfig.tsbuildinfo +1 -1
  43. package/dist/unify.js +43 -0
  44. package/dist/unify.js.map +1 -1
  45. package/dist/val/AggFuncVal.d.ts +1 -1
  46. package/dist/val/AggFuncVal.js +10 -21
  47. package/dist/val/AggFuncVal.js.map +1 -1
  48. package/dist/val/BagVal.js +1 -1
  49. package/dist/val/BagVal.js.map +1 -1
  50. package/dist/val/ConstraintVal.js +1 -1
  51. package/dist/val/EachFuncVal.d.ts +1 -1
  52. package/dist/val/EachFuncVal.js +8 -13
  53. package/dist/val/EachFuncVal.js.map +1 -1
  54. package/dist/val/EmitFuncVal.d.ts +23 -4
  55. package/dist/val/EmitFuncVal.js +298 -28
  56. package/dist/val/EmitFuncVal.js.map +1 -1
  57. package/dist/val/FilterFuncVal.js +8 -4
  58. package/dist/val/FilterFuncVal.js.map +1 -1
  59. package/dist/val/FormFuncVal.d.ts +14 -0
  60. package/dist/val/FormFuncVal.js +55 -0
  61. package/dist/val/FormFuncVal.js.map +1 -0
  62. package/dist/val/MapVal.d.ts +2 -1
  63. package/dist/val/MapVal.js +2 -1
  64. package/dist/val/MapVal.js.map +1 -1
  65. package/dist/val/PackFuncVal.d.ts +1 -1
  66. package/dist/val/PackFuncVal.js +22 -18
  67. package/dist/val/PackFuncVal.js.map +1 -1
  68. package/dist/val/PlaceVal.js +2 -1
  69. package/dist/val/PlaceVal.js.map +1 -1
  70. package/dist/val/RefVal.d.ts +3 -0
  71. package/dist/val/RefVal.js +156 -17
  72. package/dist/val/RefVal.js.map +1 -1
  73. package/dist/val/Val.d.ts +7 -1
  74. package/dist/val/Val.js +12 -1
  75. package/dist/val/Val.js.map +1 -1
  76. package/dist/val/members.d.ts +9 -0
  77. package/dist/val/members.js +52 -0
  78. package/dist/val/members.js.map +1 -0
  79. package/grammar/aontu.abnf +1 -1
  80. package/grammar/aontu.gbnf +1 -1
  81. package/grammar/aontu.lark +1 -1
  82. package/grammar/aontu.tmLanguage.json +1 -1
  83. package/package.json +1 -1
  84. package/skill/SKILL.md +4 -4
  85. package/skill/error-codes.md +1 -1
  86. package/skill/examples.md +1 -1
  87. package/skill/grammar-card.md +1 -1
  88. package/src/agentsmd.ts +1 -1
  89. package/src/alias.ts +112 -0
  90. package/src/aontu.ts +19 -2
  91. package/src/cli.ts +517 -3
  92. package/src/ctx.ts +13 -0
  93. package/src/format.ts +12 -2
  94. package/src/hints.ts +47 -9
  95. package/src/lang.ts +406 -62
  96. package/src/lower.ts +636 -0
  97. package/src/lsp.ts +1 -1
  98. package/src/mcp-server.ts +1 -1
  99. package/src/mcp.ts +43 -4
  100. package/src/render.ts +727 -0
  101. package/src/sigdecl.ts +1 -1
  102. package/src/std.ts +506 -1
  103. package/src/template.ts +291 -0
  104. package/src/unify.ts +47 -0
  105. package/src/val/AggFuncVal.ts +10 -21
  106. package/src/val/BagVal.ts +1 -1
  107. package/src/val/ConstraintVal.ts +1 -1
  108. package/src/val/EachFuncVal.ts +8 -16
  109. package/src/val/EmitFuncVal.ts +376 -39
  110. package/src/val/FilterFuncVal.ts +11 -4
  111. package/src/val/FormFuncVal.ts +119 -0
  112. package/src/val/MapVal.ts +3 -2
  113. package/src/val/PackFuncVal.ts +23 -19
  114. package/src/val/PlaceVal.ts +2 -1
  115. package/src/val/RefVal.ts +167 -18
  116. package/src/val/Val.ts +42 -1
  117. package/src/val/members.ts +86 -0
package/dist/cli.js CHANGED
@@ -13,6 +13,8 @@ exports.runRelations = runRelations;
13
13
  exports.runReaches = runReaches;
14
14
  exports.runView = runView;
15
15
  exports.runJsonSchema = runJsonSchema;
16
+ exports.runRender = runRender;
17
+ exports.runTemplate = runTemplate;
16
18
  exports.runMod = runMod;
17
19
  exports.runHash = runHash;
18
20
  exports.runGet = runGet;
@@ -39,6 +41,8 @@ const node_path_1 = require("node:path");
39
41
  const node_os_1 = require("node:os");
40
42
  const node_readline_1 = require("node:readline");
41
43
  const aontu_1 = require("./aontu");
44
+ const template_1 = require("./template");
45
+ const mcp_1 = require("./mcp");
42
46
  const report_sarif_1 = require("./report-sarif");
43
47
  const lsp_server_1 = require("./lsp-server");
44
48
  const mcp_server_1 = require("./mcp-server");
@@ -61,6 +65,10 @@ const HELP = `Usage: aontu [options] [file]
61
65
  aontu view <kind> [options] <file>...
62
66
  aontu view --views <path> [--check] [options] <file>
63
67
  aontu jsonschema [--at <path>] [--strict] [options] <file>
68
+ aontu render [--at <path>] [--profile <file>]... [--unit <path>]
69
+ [--stdout | --out <dir> | --check <dir> | --coverage]
70
+ [--coverage-at <path>] [--strict] <file>
71
+ aontu template [--resugar] [--check] [--marker <token>] <file>
64
72
  aontu hash [options] <file>
65
73
  aontu mod tidy|verify|vendor|manifest [options] [dir]
66
74
  aontu get <path> [options] <file>
@@ -71,7 +79,7 @@ const HELP = `Usage: aontu [options] [file]
71
79
  aontu lsp
72
80
  aontu mcp [--root <dir>]
73
81
 
74
- Evaluate an Aontu source file and print the result as JSON.
82
+ Evaluate an aontu source file and print the result as JSON.
75
83
  With no file on an interactive terminal, start a REPL.
76
84
  With no file and piped input, read the source from stdin.
77
85
 
@@ -253,6 +261,51 @@ View exit codes: 0 rendered, 1 --check mismatch or lossy under
253
261
  --strict, 2 usage or --max-rows exceeded, 4 the document does not stand
254
262
  up on its own, or a relation, root or path that names nothing.
255
263
 
264
+ Render options:
265
+ --at <path> Render the value at this path ($.a.b); the root by
266
+ default
267
+ --profile <file> A profile document, profile: {lang, ...}, vetted
268
+ against aontu:profile; repeatable, one per language
269
+ --unit <path> Render only the unit with this path
270
+ --stdout One unit's bytes and nothing else (with --unit when
271
+ the instance has several)
272
+ --out <dir> Write every unit below dir, or nothing; never deletes
273
+ --check <dir> Compare every unit with dir/<path>; drift is listed
274
+ --coverage Report what the render read and what it did not:
275
+ model paths no output consumed, and rendered
276
+ declarations no rule produced. Writes nothing
277
+ --coverage-at <p> Measure coverage under this path only, instead of
278
+ the document root
279
+ --strict Refuse the opaque escapes (a text declaration, a raw
280
+ block)
281
+ --format <f> text (default) or json, the whole report; json
282
+ carries the dispatch trace, one entry per emitted
283
+ piece
284
+
285
+ Render exit codes: 0 rendered, 1 lossy under --strict or drift under
286
+ --check, 2 usage or I/O (a refused unit path included), 4 the document
287
+ does not stand up or the instance is not aontu:code.
288
+
289
+ A render entry file whose extension is not .aon is a TEMPLATE: a
290
+ generator in the target's own syntax, whose marker lines carry aontu
291
+ and whose other lines are output. It is desugared before it is
292
+ evaluated, and --marker names the marker for a language the table does
293
+ not know.
294
+
295
+ Template options:
296
+ --resugar The file is the canonical aontu; print the template
297
+ form instead of reading one
298
+ --check Desugar and resugar, and exit 1 if the file is not
299
+ what the round trip answers
300
+ --marker <t> The marker, when the extension does not name it
301
+ (default //-, and #- --- /*- by extension)
302
+
303
+ The template verb prints the canonical aontu form of a generator
304
+ written in the target's own syntax: a marked line is aontu source, and
305
+ every other line is a line of output.
306
+
307
+ Template exit codes: 0 written, 1 --check drift, 2 usage or I/O.
308
+
256
309
  Set options:
257
310
  --entry <file> The document the change is checked against
258
311
  --overlay <file> The file the change is appended to (created if
@@ -609,7 +662,7 @@ function runRepl(initialMode, jsonl, trust) {
609
662
  prompt: jsonl ? '' : 'aontu> ',
610
663
  });
611
664
  if (!jsonl) {
612
- process.stdout.write(`Aontu v${version()} REPL — :help for commands, :quit to exit\n`);
665
+ process.stdout.write(`aontu v${version()} REPL — :help for commands, :quit to exit\n`);
613
666
  }
614
667
  rl.prompt();
615
668
  rl.on('line', (line) => {
@@ -2432,6 +2485,403 @@ function runJsonSchema(argv) {
2432
2485
  strict && 'lossy' === report.verdict ? 1 : 0;
2433
2486
  }
2434
2487
  // ---------------------------------------------------------------------
2488
+ // THE RENDER VERB (docs/design/RENDER.0.md D8): evaluate a document,
2489
+ // vet the value at --at against aontu:code, fold code.units into bytes,
2490
+ // and put them where the flag says -- one unit on stdout, every unit
2491
+ // below --out (all or nothing), or compared against --check. Exit codes
2492
+ // mirror jsonschema's: 0 ok; 1 lossy under --strict or drift under
2493
+ // --check; 2 usage or I/O, a refused unit path included; 4 the
2494
+ // document does not stand up or the instance is not aontu:code.
2495
+ const RENDER_HELP = 'aontu render [--at <path>] [--profile <file>]... [--unit <path>] ' +
2496
+ '[--stdout | --out <dir> | --check <dir> | --coverage] ' +
2497
+ '[--coverage-at <path>] [--strict] [--marker <token>] <file> (try --help)';
2498
+ function runRender(argv) {
2499
+ const trusted = takeTrust(argv);
2500
+ if (null == trusted) {
2501
+ return 2;
2502
+ }
2503
+ argv = trusted.argv;
2504
+ const trust = trusted.trust;
2505
+ const files = [];
2506
+ const profileFiles = [];
2507
+ let format = 'text';
2508
+ let at = undefined;
2509
+ let unit = undefined;
2510
+ let out = undefined;
2511
+ let check = undefined;
2512
+ let toStdout = false;
2513
+ let strict = false;
2514
+ let coverage = false;
2515
+ let coverageAt = undefined;
2516
+ let marker = undefined;
2517
+ for (let i = 0; i < argv.length; i++) {
2518
+ const arg = argv[i];
2519
+ if ('-h' === arg || '--help' === arg) {
2520
+ process.stdout.write(HELP);
2521
+ return 0;
2522
+ }
2523
+ if ('--format' === arg) {
2524
+ const f = argv[++i];
2525
+ if ('text' !== f && 'json' !== f) {
2526
+ process.stderr.write('aontu: --format needs text or json\n');
2527
+ return 2;
2528
+ }
2529
+ format = f;
2530
+ }
2531
+ else if ('--at' === arg) {
2532
+ at = argv[++i];
2533
+ if (null == at) {
2534
+ process.stderr.write('aontu: --at needs a path\n');
2535
+ return 2;
2536
+ }
2537
+ }
2538
+ else if ('--unit' === arg) {
2539
+ unit = argv[++i];
2540
+ if (null == unit) {
2541
+ process.stderr.write('aontu: --unit needs a unit path\n');
2542
+ return 2;
2543
+ }
2544
+ }
2545
+ else if ('--profile' === arg) {
2546
+ const pf = argv[++i];
2547
+ if (null == pf) {
2548
+ process.stderr.write('aontu: --profile needs a file\n');
2549
+ return 2;
2550
+ }
2551
+ profileFiles.push(pf);
2552
+ }
2553
+ else if ('--out' === arg) {
2554
+ out = argv[++i];
2555
+ if (null == out) {
2556
+ process.stderr.write('aontu: --out needs a directory\n');
2557
+ return 2;
2558
+ }
2559
+ }
2560
+ else if ('--check' === arg) {
2561
+ check = argv[++i];
2562
+ if (null == check) {
2563
+ process.stderr.write('aontu: --check needs a directory\n');
2564
+ return 2;
2565
+ }
2566
+ }
2567
+ else if ('--stdout' === arg) {
2568
+ toStdout = true;
2569
+ }
2570
+ else if ('--coverage' === arg) {
2571
+ coverage = true;
2572
+ }
2573
+ else if ('--marker' === arg) {
2574
+ marker = argv[++i];
2575
+ if (null == marker) {
2576
+ process.stderr.write('aontu: --marker needs a token\n');
2577
+ return 2;
2578
+ }
2579
+ }
2580
+ else if ('--coverage-at' === arg) {
2581
+ coverageAt = argv[++i];
2582
+ if (null == coverageAt) {
2583
+ process.stderr.write('aontu: --coverage-at needs a path\n');
2584
+ return 2;
2585
+ }
2586
+ }
2587
+ else if ('--strict' === arg) {
2588
+ strict = true;
2589
+ }
2590
+ else if (arg.startsWith('-')) {
2591
+ process.stderr.write(`aontu: unknown render option ${arg} (try --help)\n`);
2592
+ return 2;
2593
+ }
2594
+ else {
2595
+ files.push(arg);
2596
+ }
2597
+ }
2598
+ if (1 !== files.length) {
2599
+ process.stderr.write(`aontu: render needs one file\n${RENDER_HELP}\n`);
2600
+ return 2;
2601
+ }
2602
+ const modes = [toStdout, undefined !== out, undefined !== check, coverage]
2603
+ .filter((on) => on).length;
2604
+ if (1 < modes) {
2605
+ process.stderr.write('aontu: render takes one of --stdout, --out, --check or --coverage\n');
2606
+ return 2;
2607
+ }
2608
+ // A NARROWER MEASURE NEEDS SOMETHING TO NARROW. `--coverage-at`
2609
+ // without `--coverage` asks for a report the run does not compute,
2610
+ // and answering silently would be the wrong half of the request.
2611
+ if (undefined !== coverageAt && !coverage) {
2612
+ process.stderr.write('aontu: --coverage-at needs --coverage\n');
2613
+ return 2;
2614
+ }
2615
+ let src;
2616
+ try {
2617
+ src = (0, node_fs_1.readFileSync)(files[0], 'utf8');
2618
+ }
2619
+ catch (err) {
2620
+ process.stderr.write(`aontu: cannot read ${err.path}: ${err.message}\n`);
2621
+ return 2;
2622
+ }
2623
+ // THE ENTRY MAY BE A TEMPLATE (TEMPLATE.0.md; P8), and its EXTENSION
2624
+ // decides, as an include's extension decides what the include is
2625
+ // (ADR-012): a generator is a file in the target's own syntax, so it
2626
+ // carries the target's extension and never `.aon`. Desugared here
2627
+ // rather than anywhere deeper, because a template is an entry
2628
+ // spelling and not a value: an include is still aontu.
2629
+ if (!files[0].endsWith('.aon')) {
2630
+ src = (0, template_1.desugarTemplate)(src, marker ?? (0, template_1.markerFor)(files[0]));
2631
+ }
2632
+ // THE PROFILES (D5): each --profile file is a document whose root is
2633
+ // `profile: {lang, ...}`, evaluated under the verb's trust and vetted
2634
+ // against aontu:profile as a settled value before the fold reads it
2635
+ // (renderProfile, which also fills the defaults). Two files claiming
2636
+ // one lang is a usage error: the fold could not choose.
2637
+ const profiles = [];
2638
+ const langs = new Map();
2639
+ for (const pf of profileFiles) {
2640
+ let text;
2641
+ try {
2642
+ text = (0, node_fs_1.readFileSync)(pf, 'utf8');
2643
+ }
2644
+ catch (err) {
2645
+ process.stderr.write(`aontu: cannot read ${err.path}: ${err.message}\n`);
2646
+ return 2;
2647
+ }
2648
+ const loaded = (0, aontu_1.renderProfile)(text, { path: (0, node_path_1.resolve)(pf), ...verbOpts(trust, entryRootOf(pf)) });
2649
+ if (undefined !== loaded.errors) {
2650
+ process.stderr.write(loaded.errors.map(renderFinding).join('\n') + '\n');
2651
+ return 4;
2652
+ }
2653
+ const profile = loaded.profile;
2654
+ const prev = langs.get(profile.lang);
2655
+ if (undefined !== prev) {
2656
+ process.stderr.write(`aontu: two profiles claim ${profile.lang}: ${prev} and ${pf}\n`);
2657
+ return 2;
2658
+ }
2659
+ langs.set(profile.lang, pf);
2660
+ profiles.push(profile);
2661
+ }
2662
+ const report = (0, aontu_1.render)(src, {
2663
+ at, unit, strict, profiles, path: files[0],
2664
+ coverage, coverageAt,
2665
+ // THE JSON REPORT CARRIES THE TRACE (D9), which is what the shape
2666
+ // there has always said; a text run computes it only when the
2667
+ // coverage report needs it.
2668
+ trace: 'json' === format,
2669
+ ...verbOpts(trust, entryRootOf(files[0])),
2670
+ });
2671
+ if ('json' === format) {
2672
+ process.stdout.write((0, aontu_1.exactJSON)({
2673
+ aontu: { version: version(), verb: 'render' },
2674
+ verdict: report.verdict,
2675
+ units: report.units,
2676
+ lossy: report.lossy,
2677
+ ...(null == report.errors ? {} : { errors: report.errors }),
2678
+ ...(null == report.trace ? {} : { trace: report.trace }),
2679
+ ...(null == report.coverage ? {} : { coverage: report.coverage }),
2680
+ }, 2) + '\n');
2681
+ return renderExit(report, 0);
2682
+ }
2683
+ if ('error' === report.verdict) {
2684
+ process.stderr.write(report.errors.map(renderFinding).join('\n') + '\n');
2685
+ return renderExit(report, 0);
2686
+ }
2687
+ let drift = 0;
2688
+ if (toStdout) {
2689
+ // ONE UNIT'S BYTES AND NOTHING ELSE, so the output can be piped
2690
+ // into a formatter or a file.
2691
+ if (1 !== report.units.length) {
2692
+ process.stderr.write('aontu: --stdout needs exactly one unit, and the instance has ' +
2693
+ `${report.units.length}; --unit names one\n`);
2694
+ return 2;
2695
+ }
2696
+ process.stdout.write(report.units[0].text);
2697
+ }
2698
+ else if (undefined !== out) {
2699
+ // EVERY UNIT BELOW <dir>, OR NOTHING: every unit rendered first
2700
+ // (the report above), and no file touched unless all did. The
2701
+ // directory is realpath-confined; a unit path is already a relative
2702
+ // descent (render_path refuses the rest), and the check here is
2703
+ // against the symlink inside it. render never deletes.
2704
+ for (const u of report.units) {
2705
+ if ((0, mcp_1.outsideRoot)(out, (0, node_path_1.resolve)(out, u.path))) {
2706
+ process.stderr.write(`aontu: ${u.path} escapes ${out}\n`);
2707
+ return 2;
2708
+ }
2709
+ }
2710
+ for (const u of report.units) {
2711
+ const full = (0, node_path_1.resolve)(out, u.path);
2712
+ try {
2713
+ (0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(full), { recursive: true });
2714
+ (0, node_fs_1.writeFileSync)(full, u.text, 'utf8');
2715
+ }
2716
+ catch (err) {
2717
+ process.stderr.write(`aontu: cannot write ${u.path}: ${err.message}\n`);
2718
+ return 2;
2719
+ }
2720
+ process.stderr.write(`wrote ${u.path}\n`);
2721
+ }
2722
+ }
2723
+ else if (undefined !== check) {
2724
+ // RENDER AND COMPARE: a unit whose bytes differ from the file at
2725
+ // <dir>/<path>, or whose file is absent, is drift, listed by path.
2726
+ // The CI form.
2727
+ for (const u of report.units) {
2728
+ let have = undefined;
2729
+ try {
2730
+ have = (0, node_fs_1.readFileSync)((0, node_path_1.resolve)(check, u.path), 'utf8');
2731
+ }
2732
+ catch {
2733
+ // Absent is drift, reported below.
2734
+ }
2735
+ if (undefined === have) {
2736
+ drift++;
2737
+ process.stderr.write(`aontu: ${u.path} is missing from ${check}\n`);
2738
+ }
2739
+ else if (have !== u.text) {
2740
+ drift++;
2741
+ process.stderr.write(`aontu: ${u.path} differs from the rendered unit\n`);
2742
+ }
2743
+ }
2744
+ }
2745
+ else if (coverage) {
2746
+ // THE COVERAGE REPORT (P7), one line per finding and a count at
2747
+ // the end: dead model first, then the declarations no rule
2748
+ // produced. A clean report is the count line alone.
2749
+ const cov = report.coverage;
2750
+ for (const d of cov.dead) {
2751
+ process.stdout.write(`dead: ${d}\n`);
2752
+ }
2753
+ for (const u of cov.unruled) {
2754
+ process.stdout.write(`unruled: ${u.unit} ${u.path}\n`);
2755
+ }
2756
+ process.stdout.write(`coverage: ${cov.read.length} path(s) read, ${cov.dead.length} ` +
2757
+ `no output consumed, ${cov.unruled.length} declaration(s) ` +
2758
+ 'no rule produced\n');
2759
+ }
2760
+ else {
2761
+ // THE SUMMARY: one line per unit -- its path, its language and its
2762
+ // size -- since several units have no one text to print.
2763
+ for (const u of report.units) {
2764
+ process.stdout.write(`${u.path}\t${u.lang}\t${u.text.length} bytes\n`);
2765
+ }
2766
+ }
2767
+ for (const l of report.lossy) {
2768
+ process.stderr.write(`lossy: ${l.unit} ${l.path} tier ${l.tier} ${l.construct}: ${l.reason}\n`);
2769
+ }
2770
+ return renderExit(report, drift);
2771
+ }
2772
+ // D8's exit table over a report: a refused unit path is usage (2), a
2773
+ // strict refusal is lossy (1), any other error is the document's (4);
2774
+ // drift under --check is 1.
2775
+ function renderExit(report, drift) {
2776
+ if ('error' === report.verdict) {
2777
+ const errors = report.errors;
2778
+ if (errors.every((f) => 'render_path' === f.code)) {
2779
+ return 2;
2780
+ }
2781
+ if (errors.every((f) => 'render_strict' === f.code)) {
2782
+ return 1;
2783
+ }
2784
+ return 4;
2785
+ }
2786
+ return 0 < drift ? 1 : 0;
2787
+ }
2788
+ // ---------------------------------------------------------------------
2789
+ // THE TEMPLATE SURFACE (docs/design/TEMPLATE.0.md; RENDER.0.md P8): the
2790
+ // two transforms and the round trip between them. `render` reads a
2791
+ // template directly, by its extension; this verb is for seeing the
2792
+ // canonical form, for writing one by hand and sugaring it, and for the
2793
+ // check that keeps a committed template and its meaning in agreement.
2794
+ const TEMPLATE_HELP = 'aontu template [--resugar] [--check] [--marker <token>] <file> (try --help)';
2795
+ function runTemplate(argv) {
2796
+ const files = [];
2797
+ let resugar = false;
2798
+ let check = false;
2799
+ let marker = undefined;
2800
+ for (let i = 0; i < argv.length; i++) {
2801
+ const arg = argv[i];
2802
+ if ('-h' === arg || '--help' === arg) {
2803
+ process.stdout.write(HELP);
2804
+ return 0;
2805
+ }
2806
+ else if ('--resugar' === arg) {
2807
+ resugar = true;
2808
+ }
2809
+ else if ('--check' === arg) {
2810
+ check = true;
2811
+ }
2812
+ else if ('--marker' === arg) {
2813
+ marker = argv[++i];
2814
+ if (null == marker) {
2815
+ process.stderr.write('aontu: --marker needs a token\n');
2816
+ return 2;
2817
+ }
2818
+ }
2819
+ else if (arg.startsWith('-')) {
2820
+ process.stderr.write(`aontu: unknown template option ${arg} (try --help)\n`);
2821
+ return 2;
2822
+ }
2823
+ else {
2824
+ files.push(arg);
2825
+ }
2826
+ }
2827
+ if (1 !== files.length) {
2828
+ process.stderr.write(`aontu: template needs one file\n${TEMPLATE_HELP}\n`);
2829
+ return 2;
2830
+ }
2831
+ // THE TWO ARE DIRECTIONS, NOT MODES THAT COMPOSE: `--check` reads a
2832
+ // template and asks whether the round trip answers it back, and
2833
+ // `--resugar` reads the canonical form instead. A run cannot be both
2834
+ // at once, because the file is one thing or the other.
2835
+ if (resugar && check) {
2836
+ process.stderr.write('aontu: template takes one of --resugar or --check\n');
2837
+ return 2;
2838
+ }
2839
+ let src;
2840
+ try {
2841
+ src = (0, node_fs_1.readFileSync)(files[0], 'utf8');
2842
+ }
2843
+ catch (err) {
2844
+ process.stderr.write(`aontu: cannot read ${err.path}: ${err.message}\n`);
2845
+ return 2;
2846
+ }
2847
+ const mark = marker ?? (0, template_1.markerFor)(files[0]);
2848
+ if (check) {
2849
+ // THE ROUND TRIP IS THE CHECK (D6): the file held to the spelling
2850
+ // the two transforms answer. What that names is a marker line the
2851
+ // transform would not have written -- one without its space, or one
2852
+ // whose aontu is indented after the marker rather than before it,
2853
+ // since the marker keeps its own indentation. It does NOT name a
2854
+ // changed body line: a template's whitespace is output, so a
2855
+ // trimmed trailing space is still a valid template and it is
2856
+ // `render --check` against the committed files that catches it.
2857
+ // The first line that differs is the report, since a whole diff of
2858
+ // a generator is the file again.
2859
+ const back = (0, template_1.resugarTemplate)((0, template_1.desugarTemplate)(src, mark), mark);
2860
+ if (back === src) {
2861
+ return 0;
2862
+ }
2863
+ const want = back.split('\n');
2864
+ const have = src.split('\n');
2865
+ let n = 0;
2866
+ while (n < want.length && n < have.length && want[n] === have[n]) {
2867
+ n++;
2868
+ }
2869
+ // THE TWO ARE THE SAME LENGTH, always: each transform maps one
2870
+ // line to one line and applies the same trailing-newline rule, so
2871
+ // `back` has as many lines as `src`. The loop above therefore stops
2872
+ // at a real difference rather than by running out of either -- an
2873
+ // equal prefix all the way to the end IS `back === src`, which
2874
+ // returned above. So both indexes are in range here.
2875
+ process.stderr.write(`aontu: ${files[0]}:${n + 1} is not what the round trip answers\n` +
2876
+ ` have: ${JSON.stringify(have[n])}\n` +
2877
+ ` want: ${JSON.stringify(want[n])}\n`);
2878
+ return 1;
2879
+ }
2880
+ process.stdout.write(resugar ?
2881
+ (0, template_1.resugarTemplate)(src, mark) : (0, template_1.desugarTemplate)(src, mark));
2882
+ return 0;
2883
+ }
2884
+ // ---------------------------------------------------------------------
2435
2885
  // The canon-hash (G6 phase 1): the pin an agent, a lockfile or a
2436
2886
  // registry stores for "this module, this meaning". The hash covers the
2437
2887
  // module evaluated STANDALONE -- its own include closure resolved and
@@ -3022,6 +3472,22 @@ function runFmt(argv) {
3022
3472
  }
3023
3473
  let worst = 0;
3024
3474
  for (const file of files) {
3475
+ // FMT FORMATS AONTU SOURCE, AND THE EXTENSION SAYS WHAT A FILE IS
3476
+ // (ADR-012's rule, and the one `render` reads a template by).
3477
+ // A TEMPLATE FILE IS NOT AONTU (docs/design/TEMPLATE.0.md; P8):
3478
+ // its marker lines are fragments of a document and its other lines
3479
+ // are the target's, so there is nothing here to format that would
3480
+ // not also rewrite the output. Refused rather than attempted, and
3481
+ // refused BY NAME rather than by a parse failure, because a `#-`
3482
+ // template parses: `#` opens a comment, so every marker line
3483
+ // vanishes and what is left is read as a document that was never
3484
+ // written. The verb answered `0` over one, having understood none
3485
+ // of it.
3486
+ if (!/[.](aon|aontu)$/.test(file)) {
3487
+ process.stderr.write(`aontu: ${file} is not aontu source (.aon, .aontu); a generator ` +
3488
+ 'written in the target\'s own syntax is aontu template\'s\n');
3489
+ return 2;
3490
+ }
3025
3491
  let src;
3026
3492
  try {
3027
3493
  src = (0, node_fs_1.readFileSync)(file, 'utf8');
@@ -3199,6 +3665,12 @@ function main(argv, servers = SERVERS) {
3199
3665
  if ('jsonschema' === argv[2]) {
3200
3666
  return finish(runJsonSchema(argv.slice(3)));
3201
3667
  }
3668
+ if ('render' === argv[2]) {
3669
+ return finish(runRender(argv.slice(3)));
3670
+ }
3671
+ if ('template' === argv[2]) {
3672
+ return finish(runTemplate(argv.slice(3)));
3673
+ }
3202
3674
  if ('reaches' === argv[2]) {
3203
3675
  return finish(runReaches(argv.slice(3)));
3204
3676
  }
@@ -3296,5 +3768,5 @@ function main(argv, servers = SERVERS) {
3296
3768
  else {
3297
3769
  runStdin(mode, trust).then((code) => finish(code));
3298
3770
  }
3299
- } /* node:coverage ignore next 16 */
3771
+ } /* node:coverage ignore next 18 */
3300
3772
  //# sourceMappingURL=cli.js.map