aontu 0.53.0 → 0.55.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 (197) hide show
  1. package/dist/aontu.d.ts +3 -2
  2. package/dist/aontu.js +36 -7
  3. package/dist/aontu.js.map +1 -1
  4. package/dist/cli.d.ts +2 -1
  5. package/dist/cli.js +500 -8
  6. package/dist/cli.js.map +1 -1
  7. package/dist/ctx.d.ts +5 -0
  8. package/dist/ctx.js +1 -0
  9. package/dist/ctx.js.map +1 -1
  10. package/dist/diff.js.map +1 -1
  11. package/dist/err.js +7 -1
  12. package/dist/err.js.map +1 -1
  13. package/dist/graph.d.ts +2 -5
  14. package/dist/graph.js +83 -46
  15. package/dist/graph.js.map +1 -1
  16. package/dist/hcanon.js +9 -10
  17. package/dist/hcanon.js.map +1 -1
  18. package/dist/hints.js +94 -26
  19. package/dist/hints.js.map +1 -1
  20. package/dist/jsonschema.js +34 -0
  21. package/dist/jsonschema.js.map +1 -1
  22. package/dist/lang.js +593 -191
  23. package/dist/lang.js.map +1 -1
  24. package/dist/lsp.d.ts +1 -1
  25. package/dist/lsp.js +86 -6
  26. package/dist/lsp.js.map +1 -1
  27. package/dist/mcp.js +153 -6
  28. package/dist/mcp.js.map +1 -1
  29. package/dist/mod-tool.js +42 -8
  30. package/dist/mod-tool.js.map +1 -1
  31. package/dist/mod.d.ts +4 -0
  32. package/dist/mod.js +97 -2
  33. package/dist/mod.js.map +1 -1
  34. package/dist/patch.d.ts +5 -0
  35. package/dist/patch.js +25 -25
  36. package/dist/patch.js.map +1 -1
  37. package/dist/provenance.d.ts +1 -0
  38. package/dist/provenance.js +2 -1
  39. package/dist/provenance.js.map +1 -1
  40. package/dist/query.js.map +1 -1
  41. package/dist/reach.d.ts +1 -0
  42. package/dist/reach.js +49 -21
  43. package/dist/reach.js.map +1 -1
  44. package/dist/relation.d.ts +4 -0
  45. package/dist/relation.js +125 -200
  46. package/dist/relation.js.map +1 -1
  47. package/dist/sig.d.ts +25 -0
  48. package/dist/sig.js +277 -0
  49. package/dist/sig.js.map +1 -0
  50. package/dist/sigdecl.d.ts +2 -0
  51. package/dist/sigdecl.js +11 -0
  52. package/dist/sigdecl.js.map +1 -0
  53. package/dist/siggate.d.ts +4 -0
  54. package/dist/siggate.js +90 -0
  55. package/dist/siggate.js.map +1 -0
  56. package/dist/std.js +75 -16
  57. package/dist/std.js.map +1 -1
  58. package/dist/subsume.js +57 -10
  59. package/dist/subsume.js.map +1 -1
  60. package/dist/tsconfig.tsbuildinfo +1 -1
  61. package/dist/unify.d.ts +2 -2
  62. package/dist/unify.js +93 -114
  63. package/dist/unify.js.map +1 -1
  64. package/dist/utility.d.ts +1 -2
  65. package/dist/utility.js +7 -61
  66. package/dist/utility.js.map +1 -1
  67. package/dist/val/AggFuncVal.d.ts +12 -1
  68. package/dist/val/AggFuncVal.js +165 -3
  69. package/dist/val/AggFuncVal.js.map +1 -1
  70. package/dist/val/BagVal.d.ts +2 -0
  71. package/dist/val/BagVal.js +56 -8
  72. package/dist/val/BagVal.js.map +1 -1
  73. package/dist/val/ConstraintVal.js +33 -5
  74. package/dist/val/ConstraintVal.js.map +1 -1
  75. package/dist/val/ContainerKindVal.d.ts +35 -0
  76. package/dist/val/ContainerKindVal.js +99 -0
  77. package/dist/val/ContainerKindVal.js.map +1 -0
  78. package/dist/val/CopyFuncVal.js +0 -7
  79. package/dist/val/CopyFuncVal.js.map +1 -1
  80. package/dist/val/DisjunctVal.d.ts +1 -2
  81. package/dist/val/DisjunctVal.js +146 -39
  82. package/dist/val/DisjunctVal.js.map +1 -1
  83. package/dist/val/ExpectVal.js +37 -2
  84. package/dist/val/ExpectVal.js.map +1 -1
  85. package/dist/val/FuncBaseVal.d.ts +1 -0
  86. package/dist/val/FuncBaseVal.js +19 -8
  87. package/dist/val/FuncBaseVal.js.map +1 -1
  88. package/dist/val/GraphAtomVal.d.ts +39 -0
  89. package/dist/val/GraphAtomVal.js +184 -0
  90. package/dist/val/GraphAtomVal.js.map +1 -0
  91. package/dist/val/JunctionVal.js +22 -5
  92. package/dist/val/JunctionVal.js.map +1 -1
  93. package/dist/val/ListVal.js +27 -34
  94. package/dist/val/ListVal.js.map +1 -1
  95. package/dist/val/MapVal.d.ts +1 -0
  96. package/dist/val/MapVal.js +82 -35
  97. package/dist/val/MapVal.js.map +1 -1
  98. package/dist/val/PathFuncVal.d.ts +2 -2
  99. package/dist/val/PathFuncVal.js +75 -16
  100. package/dist/val/PathFuncVal.js.map +1 -1
  101. package/dist/val/PathVal.d.ts +25 -0
  102. package/dist/val/PathVal.js +150 -0
  103. package/dist/val/PathVal.js.map +1 -0
  104. package/dist/val/PlusOpVal.d.ts +2 -1
  105. package/dist/val/PlusOpVal.js +50 -35
  106. package/dist/val/PlusOpVal.js.map +1 -1
  107. package/dist/val/PrefVal.d.ts +2 -0
  108. package/dist/val/PrefVal.js +159 -32
  109. package/dist/val/PrefVal.js.map +1 -1
  110. package/dist/val/RecurseVal.d.ts +19 -0
  111. package/dist/val/RecurseVal.js +217 -0
  112. package/dist/val/RecurseVal.js.map +1 -0
  113. package/dist/val/RefVal.d.ts +2 -1
  114. package/dist/val/RefVal.js +105 -72
  115. package/dist/val/RefVal.js.map +1 -1
  116. package/dist/val/ReferFuncVal.d.ts +30 -7
  117. package/dist/val/ReferFuncVal.js +395 -94
  118. package/dist/val/ReferFuncVal.js.map +1 -1
  119. package/dist/val/ScalarKindVal.d.ts +4 -2
  120. package/dist/val/ScalarKindVal.js +12 -1
  121. package/dist/val/ScalarKindVal.js.map +1 -1
  122. package/dist/val/SuperFuncVal.d.ts +4 -2
  123. package/dist/val/SuperFuncVal.js +118 -14
  124. package/dist/val/SuperFuncVal.js.map +1 -1
  125. package/dist/val/TopVal.d.ts +1 -1
  126. package/dist/val/Val.d.ts +2 -3
  127. package/dist/val/Val.js +39 -20
  128. package/dist/val/Val.js.map +1 -1
  129. package/dist/val/arith.js +4 -1
  130. package/dist/val/arith.js.map +1 -1
  131. package/dist/vet.d.ts +1 -0
  132. package/dist/vet.js +32 -1
  133. package/dist/vet.js.map +1 -1
  134. package/dist/view.d.ts +90 -0
  135. package/dist/view.js +2168 -0
  136. package/dist/view.js.map +1 -0
  137. package/grammar/aontu.gbnf +18 -10
  138. package/grammar/aontu.lark +15 -10
  139. package/grammar/aontu.tmLanguage.json +184 -0
  140. package/package.json +10 -3
  141. package/skill/grammar-card.md +1 -2
  142. package/src/aontu.ts +37 -7
  143. package/src/cli.ts +553 -8
  144. package/src/ctx.ts +20 -0
  145. package/src/diff.ts +4 -2
  146. package/src/err.ts +8 -1
  147. package/src/graph.ts +125 -75
  148. package/src/hcanon.ts +9 -11
  149. package/src/hints.ts +112 -29
  150. package/src/jsonschema.ts +41 -0
  151. package/src/lang.ts +642 -203
  152. package/src/lsp.ts +75 -6
  153. package/src/mcp.ts +164 -6
  154. package/src/mod-tool.ts +48 -9
  155. package/src/mod.ts +110 -1
  156. package/src/patch.ts +31 -27
  157. package/src/provenance.ts +8 -1
  158. package/src/query.ts +4 -2
  159. package/src/reach.ts +52 -23
  160. package/src/relation.ts +139 -236
  161. package/src/sig.ts +345 -0
  162. package/src/sigdecl.ts +11 -0
  163. package/src/siggate.ts +144 -0
  164. package/src/std.ts +77 -16
  165. package/src/subsume.ts +59 -10
  166. package/src/unify.ts +102 -125
  167. package/src/utility.ts +7 -67
  168. package/src/val/AggFuncVal.ts +231 -4
  169. package/src/val/BagVal.ts +58 -9
  170. package/src/val/ConstraintVal.ts +34 -5
  171. package/src/val/ContainerKindVal.ts +158 -0
  172. package/src/val/CopyFuncVal.ts +0 -7
  173. package/src/val/DisjunctVal.ts +152 -40
  174. package/src/val/ExpectVal.ts +39 -4
  175. package/src/val/FuncBaseVal.ts +21 -8
  176. package/src/val/GraphAtomVal.ts +264 -0
  177. package/src/val/JunctionVal.ts +23 -6
  178. package/src/val/ListVal.ts +30 -37
  179. package/src/val/MapVal.ts +87 -39
  180. package/src/val/PathFuncVal.ts +107 -19
  181. package/src/val/PathVal.ts +221 -0
  182. package/src/val/PlusOpVal.ts +56 -37
  183. package/src/val/PrefVal.ts +186 -38
  184. package/src/val/RecurseVal.ts +285 -0
  185. package/src/val/RefVal.ts +105 -83
  186. package/src/val/ReferFuncVal.ts +445 -100
  187. package/src/val/ScalarKindVal.ts +12 -0
  188. package/src/val/SuperFuncVal.ts +137 -13
  189. package/src/val/TopVal.ts +1 -1
  190. package/src/val/Val.ts +44 -35
  191. package/src/val/arith.ts +4 -1
  192. package/src/vet.ts +41 -4
  193. package/src/view.ts +2882 -0
  194. package/dist/val/IdFuncVal.d.ts +0 -13
  195. package/dist/val/IdFuncVal.js +0 -54
  196. package/dist/val/IdFuncVal.js.map +0 -1
  197. package/src/val/IdFuncVal.ts +0 -91
package/src/lang.ts CHANGED
@@ -27,6 +27,20 @@ import {
27
27
  } from '@tabnas/jsonic'
28
28
 
29
29
 
30
+ // THE CONFIG-FORMAT READERS (ADR-012). Each is a jsonic plugin for one
31
+ // format, so an included `.toml` or `.yaml` is parsed by a real parser
32
+ // for that format rather than guessed at by this one. `@tabnas/json` is
33
+ // the strict RFC 8259 reader, used for `.json` and `.jsonld`.
34
+ import { funcSig } from './sig'
35
+ import type { FuncSig } from './sig'
36
+
37
+ import { make as makeJsonParser } from '@tabnas/json'
38
+ import { Toml } from '@tabnas/toml'
39
+ import { Jsonc } from '@tabnas/jsonc'
40
+ import { Json5 } from '@tabnas/json5'
41
+ import { Yaml } from '@tabnas/yaml'
42
+ import { Ini } from '@tabnas/ini'
43
+
30
44
  import { Debug } from '@tabnas/debug'
31
45
 
32
46
  import {
@@ -47,8 +61,20 @@ import {
47
61
  makeMemResolver
48
62
  } from '@tabnas/multisource/resolver/mem'
49
63
 
64
+ // The Aontu-source processor, TAKEN RATHER THAN ALIASED. The obvious
65
+ // spelling is the alias `aon: 'jsonic'`, which multisource resolves
66
+ // through its own processor map -- but `jsonic` is a FORMAT NAME in
67
+ // the include table now, so that alias resolved to the plain-jsonic
68
+ // DATA reader and every `.aon` include was suddenly parsed without the
69
+ // language in it. Naming the function leaves nothing to collide with.
70
+ import {
71
+ makeJsonicProcessor,
72
+ } from '@tabnas/multisource/processor/jsonic'
73
+
50
74
  import { STD_SOURCES } from './std'
51
- import { parseModuleRef, resolveModule, modCacheDir } from './mod'
75
+ import {
76
+ parseModuleRef, resolveModule, modCacheDir, MODULE_REFUSAL_CODES,
77
+ } from './mod'
52
78
 
53
79
  import {
54
80
  Expr,
@@ -113,8 +139,8 @@ import { KeyFuncVal } from './val/KeyFuncVal'
113
139
  import { TypeFuncVal } from './val/TypeFuncVal'
114
140
  import { HideFuncVal } from './val/HideFuncVal'
115
141
  import { DeprecateFuncVal } from './val/DeprecateFuncVal'
116
- import { IdFuncVal } from './val/IdFuncVal'
117
- import { ReferFuncVal } from './val/ReferFuncVal'
142
+ import { ReferFuncVal, RelFuncVal } from './val/ReferFuncVal'
143
+ import { AcyclicFuncVal, InverseFuncVal } from './val/GraphAtomVal'
118
144
  import { PackFuncVal } from './val/PackFuncVal'
119
145
  import { EachFuncVal } from './val/EachFuncVal'
120
146
  import { FilterFuncVal } from './val/FilterFuncVal'
@@ -123,11 +149,12 @@ import {
123
149
  AddFuncVal, SubFuncVal, MulFuncVal, DivFuncVal, ModFuncVal, RemFuncVal,
124
150
  } from './val/ArithFuncVal'
125
151
  import {
126
- SumFuncVal, LeastFuncVal, GreatestFuncVal, PickFuncVal,
152
+ SumFuncVal, LeastFuncVal, GreatestFuncVal, PickFuncVal, JoinFuncVal,
127
153
  } from './val/AggFuncVal'
128
154
  import { PlaceVal } from './val/PlaceVal'
129
155
  import { MoveFuncVal } from './val/MoveFuncVal'
130
156
  import { PathFuncVal } from './val/PathFuncVal'
157
+ import { MapFuncVal, ListFuncVal } from './val/ContainerKindVal'
131
158
  import { PrefFuncVal } from './val/PrefFuncVal'
132
159
  import { CloseFuncVal } from './val/CloseFuncVal'
133
160
  import { OpenFuncVal } from './val/OpenFuncVal'
@@ -188,6 +215,13 @@ const CC_0 = 48
188
215
  const CC_d = 100
189
216
  const CC_D = 68
190
217
 
218
+ // THE ALIAS SIGIL. `%` is part of an alias's name, so the name is one
219
+ // lexeme wherever it appears and its meaning is decided by position:
220
+ // a BINDING in key position (`%uint8: …` declares), a USE in value
221
+ // position (`listen: %uint8` refers). docs/design/ALIASES.0.md §4.
222
+ const CC_PCT = 37
223
+ const ALIAS_RE = /^%[A-Za-z_][A-Za-z0-9_]*/
224
+
191
225
  let AontuJsonic: Plugin = function AontuLang(jsonic: Jsonic) {
192
226
 
193
227
  jsonic.use(asPlugin(Path))
@@ -199,6 +233,38 @@ let AontuJsonic: Plugin = function AontuLang(jsonic: Jsonic) {
199
233
  let dotRef = (r: Rule, ctx: JsonicContext, terms: any, prefix: boolean) => {
200
234
  terms = dropUnfilled(terms)
201
235
  if (0 === terms.length) return incompleteNil(r, ctx)
236
+
237
+ // AN ALIAS IS NOT A PATH SEGMENT. `$.%foo` is refused: the alias
238
+ // namespace and the path namespace are disjoint, and an alias is
239
+ // reached by writing `%foo` and only that.
240
+ //
241
+ // The engine spells an alias reference AS a root reference to the
242
+ // declaration -- which is what gives it order independence and a
243
+ // cycle check shared with paths -- but that is an implementation of
244
+ // the name, not a second way to write it. Left writable, the two
245
+ // spellings would drift apart the moment aliases stop being
246
+ // file-shaped, and `$.%b` inside an included file would reach the
247
+ // INCLUDER's `%b` rather than its own, which is exactly the
248
+ // cross-file capture the sigil exists to prevent.
249
+ // `%foo` lexes to the reference itself, so in `$.%foo` it arrives
250
+ // as a TERM rather than as a string segment -- both shapes are
251
+ // checked, since a quoted `$."%foo"` would arrive as the string.
252
+ // Terms here are always Vals -- dropUnfilled has removed the
253
+ // nulls, and the dot rules never hand over a raw string -- so the
254
+ // shapes are exactly three: a RefVal (peg is the segment array), a
255
+ // StringVal (peg is the segment), and anything else (a numeric or
256
+ // exact segment, which cannot be an alias name).
257
+ for (const t of terms) {
258
+ const segs: any[] =
259
+ Array.isArray(t.peg) ? t.peg :
260
+ ('string' === typeof t.peg ? [t.peg] : [])
261
+ for (const seg of segs) {
262
+ if ('string' === typeof seg && ALIAS_RE.test(seg)) {
263
+ return addsite(new NilVal({ why: 'alias_in_path' }), r, ctx)
264
+ }
265
+ }
266
+ }
267
+
202
268
  return addsite(new RefVal({ peg: terms, prefix }), r, ctx)
203
269
  }
204
270
 
@@ -273,9 +339,47 @@ let AontuJsonic: Plugin = function AontuLang(jsonic: Jsonic) {
273
339
  check: (lex: any) => {
274
340
  // Guard first, on char codes: this hook runs at every text
275
341
  // position, and the common case (any run that cannot be a `0d`
276
- // literal) must cost two char reads and no allocation.
342
+ // literal or an alias) must cost two char reads and no
343
+ // allocation.
277
344
  const pnt = lex.pnt
278
345
  const src = lex.src
346
+
347
+ // AN ALIAS NAME IS CLAIMED WHOLE, for the same reason the `0d`
348
+ // run below is: the text matcher's ender regexp would otherwise
349
+ // carve `%uint8` at the `%` and emit the sigil as its own token,
350
+ // leaving a bare `uint8` behind -- which is exactly the capture
351
+ // the sigil exists to prevent. Claiming it here, before that
352
+ // ender runs, keeps the name one lexeme.
353
+ //
354
+ // The token's SOURCE is the whole `%name`, which is what makes
355
+ // the same lexeme work in both positions: jsonic keys a pair by
356
+ // the token's source text (`0d1: 5` yields the key `0d1`), so a
357
+ // declaration reads as the key `%uint8`, while a value position
358
+ // calls the function below and gets the reference.
359
+ if (CC_PCT === src.charCodeAt(pnt.sI)) {
360
+ const ares = ALIAS_RE.exec(lex.refwd())
361
+ if (null == ares) {
362
+ return undefined
363
+ }
364
+ const asrc = ares[0]
365
+ const atkn = lex.token(
366
+ '#VL',
367
+ // AN ALIAS REFERENCE IS A PATH REFERENCE. `%uint8` is
368
+ // `$.%uint8`: root-absolute, one segment, spelled with the
369
+ // sigil the declaration is spelled with. Everything the
370
+ // design asks of it -- order independence, alias-of-alias,
371
+ // redeclaration unifying, cycle refusal spanning both
372
+ // namespaces -- is then the reference machinery already in
373
+ // the language, not a second resolver beside it.
374
+ (r: Rule, ctx: JsonicContext) =>
375
+ addsite(new RefVal({ peg: [asrc], absolute: true }), r, ctx),
376
+ asrc,
377
+ pnt)
378
+ pnt.sI += asrc.length
379
+ pnt.cI += asrc.length
380
+ return { done: true, token: atkn }
381
+ }
382
+
279
383
  if (CC_0 !== src.charCodeAt(pnt.sI)) {
280
384
  return undefined
281
385
  }
@@ -459,6 +563,14 @@ help isolate the syntax error.`,
459
563
  move: MoveFuncVal,
460
564
  path: PathFuncVal,
461
565
  pref: PrefFuncVal,
566
+
567
+ // First-class paths and the container kinds
568
+ // (docs/design/PATHS.0.md). `path(p)` CAPTURES a path as a value;
569
+ // `path()`, `map()` and `list()` are kinds -- the vacuous
570
+ // constructor call admits its values and defaults to nothing,
571
+ // where the container LITERALS `{}`/`[]` default to empty.
572
+ map: MapFuncVal,
573
+ list: ListFuncVal,
462
574
  close: CloseFuncVal,
463
575
  open: OpenFuncVal,
464
576
  super: SuperFuncVal,
@@ -495,17 +607,18 @@ help isolate the syntax error.`,
495
607
  // call back (canonRiders).
496
608
  deprecate: DeprecateFuncVal,
497
609
 
498
- // G4 phase 1: the identity mark. Written as a conjunct
499
- // (`id(svc/auth) & {…}`), it resolves to the unit carrying the
500
- // name, and every node in one evaluation with that name is
501
- // unified with every other.
502
- id: IdFuncVal,
503
-
504
610
  // G4 phase 2: the checked, typed, LINK-shaped reference. A
505
- // constraint on a string field: the string must be an entity
506
- // address, the address must resolve, and the optional argument
507
- // flows INTO the target. The field keeps the string.
611
+ // constraint on a string field: the string must be a TREE ADDRESS
612
+ // (`$.a.b` or `.b`), the address must resolve, and the optional
613
+ // argument flows INTO the target. The field keeps the string.
508
614
  refer: ReferFuncVal,
615
+ rel: RelFuncVal,
616
+
617
+ // RELATIONS P2 (docs/design/RELATIONS.0.md §3.3): the graph
618
+ // atoms, conjoined at the field whose key is the predicate they
619
+ // govern. Lattice-inert; the verdict lands at generation.
620
+ acyclic: AcyclicFuncVal,
621
+ inverse: InverseFuncVal,
509
622
 
510
623
  // G8 phase 1: the generation combinators. `pack` makes one keyed
511
624
  // child per child of its data, `each` one list element; both clone
@@ -548,6 +661,12 @@ help isolate the syntax error.`,
548
661
  // RECORDS: `sum(pick($.lines, amountCents))`. Not a clever `each`
549
662
  // template -- `each` MEETS each child, and a meet cannot select.
550
663
  pick: PickFuncVal,
664
+
665
+ // G9 phase 2: the fold to a STRING. `sum` folds with `add`; this
666
+ // folds with `+`, so it inherits the one number-to-text rule and
667
+ // the language does not grow a second. It is the primitive that
668
+ // turns a bag of computed lines into a file.
669
+ join: JoinFuncVal,
551
670
  }
552
671
 
553
672
 
@@ -562,10 +681,8 @@ help isolate the syntax error.`,
562
681
  addsite(new NilVal({ why: 'incomplete_expression' }), r, ctx)
563
682
 
564
683
  // Build a call from a NAME and the argument terms as the author
565
- // wrote them. Shared by the `func(...)` handler and by the pipe,
566
- // which is the same call with one more argument on the front — so
567
- // the arity check, the comma-group rule and the raw-value conversion
568
- // are stated once and both spellings get all three.
684
+ // wrote them: the arity check, the comma-group rule and the
685
+ // raw-value conversion, stated once.
569
686
  const buildCall = (r: Rule, ctx: JsonicContext,
570
687
  fname: string, argterms: any[]): any => {
571
688
  const funcval = funcMap[fname]
@@ -597,12 +714,6 @@ help isolate the syntax error.`,
597
714
  want: arityText(arity[0], arity[1]),
598
715
  got: '' + got,
599
716
  }
600
- // The CALL AS WRITTEN rides the refusal (G8 phase 4): a pipe
601
- // rebuilds `x |> upper()` as `upper(x)`, and the arity it fails
602
- // on here is the arity of a call one argument short of the one
603
- // the author actually wrote.
604
- nil._callname = fname
605
- nil._callterms = argterms
606
717
  return addsite(nil, r, ctx)
607
718
  }
608
719
  }
@@ -633,45 +744,10 @@ help isolate the syntax error.`,
633
744
  new NilVal({ why: 'unknown_function' }) :
634
745
  new funcval({ peg: args })
635
746
 
636
- // The call as written, for the pipe to rebuild from. Parse-time
637
- // only: nothing downstream reads it, and a clone does not carry it.
638
- //
639
- // NOT on a constraint atom that BUILT: an atom with its argument
640
- // list complete is a residual, not a call waiting for a subject,
641
- // and `1 |> neq(2,3)` is asking for `1 & neq(2,3)` -- which is
642
- // what `&` is for. (An atom the arity check REFUSED still carries
643
- // it, on the nil above: `1 |> min()` is `min(1)`, and that is a
644
- // call waiting for a subject.) The Go port cannot rebuild a built
645
- // atom at all -- its residual keeps no atom name -- so this is
646
- // also what keeps the two ports answering the same thing.
647
- if (true !== val.isConstraint) {
648
- val._callname = fname
649
- val._callterms = argterms
650
- }
651
-
652
747
  return val
653
748
  }
654
749
 
655
750
 
656
- // The argument terms `f(...)` would have been written with, had the
657
- // piped value been written into it. A comma group is one raw-array
658
- // term: for a POSITIONAL function the group is separate arguments,
659
- // so the piped value joins them; for a constraint atom the group IS
660
- // the argument list, so the piped value joins the list instead.
661
- const pipeTerms = (call: any, val: any): any[] => {
662
- const written: any[] = call._callterms
663
- const group = 1 === written.length && Array.isArray(written[0]) ?
664
- written[0] : written
665
-
666
- if (0 === group.length) {
667
- return [val]
668
- }
669
-
670
- return true === POSITIONAL_ARG_FUNCS[call._callname] ?
671
- [val, ...group] : [[val, ...group]]
672
- }
673
-
674
-
675
751
  let opmap: any = {
676
752
  'conjunct-infix': (r: Rule, ctx: JsonicContext, _op: Op, terms: any) =>
677
753
  addsite(new ConjunctVal({ peg: dropUnfilled(terms) }), r, ctx),
@@ -690,11 +766,50 @@ help isolate the syntax error.`,
690
766
 
691
767
  'star-prefix': (r: Rule, ctx: JsonicContext, _op: Op, terms: any) => {
692
768
  if (null == terms[0]) return incompleteNil(r, ctx)
769
+
770
+ // A PREFERENCE MARKS A VALUE, AND A BARE KEY IS NOT ONE.
771
+ // `*a: 1` has no braces, so the prefix took the whole IMPLICIT
772
+ // map as its operand and the document silently became
773
+ // `*{"a":1}` -- `*a: 1, b: 2` became a one-element LIST, losing
774
+ // `b` outright. Neither is anything the author wrote.
775
+ //
776
+ // The accident is confined to the first position of the implicit
777
+ // top-level map, which is the only place no brace has yet
778
+ // committed the rule to a map: `{*a: 1}` and `a: 1, *b: 2` are
779
+ // ALREADY parse errors. This makes the third spelling agree with
780
+ // them rather than inventing a meaning for it.
781
+ //
782
+ // A BRACED operand is untouched, and that is the whole of the
783
+ // distinction: `*{x:1}` and `*[1]` are the real spelling, they
784
+ // are what `*{x:1} | *{y:2}` needs, and the shared spec pins them
785
+ // (11 rows). The open token's own source text is what separates
786
+ // the two -- `{` or `[` for a braced bag, the first key or
787
+ // element for an implicit one.
788
+ const bag: any = terms[0]
789
+ if ((bag.isMap && '{' !== bag.site.src) ||
790
+ (bag.isList && '[' !== bag.site.src)) {
791
+ return addsite(new NilVal({ why: 'pref_implicit_bag' }), r, ctx)
792
+ }
793
+
693
794
  return addsite(new PrefVal({ peg: terms[0] }), r, ctx)
694
795
  },
695
796
 
696
797
  'dollar-prefix': (r: Rule, ctx: JsonicContext, _op: Op, terms: any) => {
697
798
  if (null == terms[0]) return incompleteNil(r, ctx)
799
+ // A refusal from the dot rule below (an alias used as a path
800
+ // segment) rides straight through: wrapping it in a VarVal would
801
+ // replace `alias_in_path` with a var whose peg is a nil.
802
+ if (terms[0]?.isNil) {
803
+ return terms[0]
804
+ }
805
+ // `$%foo` -- the sigil directly after the root -- reaches here
806
+ // as the alias reference rather than through the dot rule, and
807
+ // is refused for the same reason.
808
+ if (terms[0] instanceof RefVal &&
809
+ terms[0].peg.some((seg: any) =>
810
+ 'string' === typeof seg && ALIAS_RE.test(seg))) {
811
+ return addsite(new NilVal({ why: 'alias_in_path' }), r, ctx)
812
+ }
698
813
  // $.a.b absolute path
699
814
  if (terms[0] instanceof RefVal) {
700
815
  terms[0].absolute = true
@@ -751,41 +866,6 @@ help isolate the syntax error.`,
751
866
  return addsite(val, r, ctx)
752
867
  },
753
868
 
754
- // THE PIPE `|>` (G8 phase 4): parse-time sugar and nothing else.
755
- // `x |> f(a)` IS `f(x, a)` -- the piped value goes in as the FIRST
756
- // argument, Elixir-style, because every Aontu call is data-first
757
- // already (`close(x)`, `pack(data, tmpl)`) and a pipe must read the
758
- // way the calls it replaces read. It never reaches a Val: by the
759
- // time the tree exists the call is an ordinary call, which is why
760
- // canon can never emit the token and the two ports' canon stay
761
- // byte-identical without either knowing about it.
762
- 'pipe-infix': (r: Rule, ctx: JsonicContext, _op: Op, terms: any) => {
763
- const val = terms[0]
764
- const call: any = terms[1]
765
-
766
- if (null == val || null == call) return incompleteNil(r, ctx)
767
-
768
- // The right-hand side is a CALL: either one the func handler
769
- // already built, or one it refused for an arity the pipe is about
770
- // to satisfy. Both carry what they were written as.
771
- if (null != call._callname) {
772
- return buildCall(r, ctx, call._callname, pipeTerms(call, val))
773
- }
774
-
775
- // ... or a bare NAME, which is the whole point of the short
776
- // spelling: `x |> upper` is `upper(x)`. A bare word has already
777
- // become a string VALUE by the time an infix operator sees it, so
778
- // this is where a string becomes a call.
779
- if (true === call?.isScalar && 'string' === typeof call.peg &&
780
- null != funcMap[call.peg]) {
781
- return buildCall(r, ctx, call.peg, [val])
782
- }
783
-
784
- // Anything else is not a call, and a pipe into a non-call is a
785
- // mistake in the source rather than a value.
786
- return addsite(new NilVal({ why: 'pipe_target' }), r, ctx)
787
- },
788
-
789
869
  'func-paren': (r: Rule, ctx: JsonicContext, _op: Op, terms: any) => {
790
870
  let val = terms[1]
791
871
  const fname = terms[0]
@@ -817,14 +897,6 @@ help isolate the syntax error.`,
817
897
  infix: true, src: '|', left: 14_000_000, right: 15_000_000
818
898
  },
819
899
 
820
- // G8 phase 4: the pipe. LOOSEST of all the infix operators, so
821
- // `a & b |> f` pipes the whole meet and not just `b` -- a pipe
822
- // reads as "and then", which is a statement about everything to
823
- // its left. Kept in lock-step with the op table in go/lang.go.
824
- 'pipe-infix': {
825
- infix: true, src: '|>', left: 12_000_000, right: 13_000_000
826
- },
827
-
828
900
  'plus-infix': {
829
901
  src: '+',
830
902
  infix: true,
@@ -926,6 +998,8 @@ help isolate the syntax error.`,
926
998
 
927
999
  const QM = jsonic.token.QM
928
1000
 
1001
+ const VL = jsonic.token.VL
1002
+
929
1003
  const OPTKEY = [TX, ST, NR]
930
1004
 
931
1005
 
@@ -1067,6 +1141,7 @@ help isolate the syntax error.`,
1067
1141
 
1068
1142
  .bc((r: Rule, ctx: JsonicContext) => {
1069
1143
  const optionalKeys = r.u.aontu_optional_keys ?? []
1144
+ const aliasKeys = r.u.aontu_alias_keys ?? []
1070
1145
 
1071
1146
  let mo = r.node
1072
1147
 
@@ -1133,6 +1208,7 @@ help isolate the syntax error.`,
1133
1208
  // TODO: needs addpath?
1134
1209
  let mopv = new MapVal({ peg: mop })
1135
1210
  mopv.optionalKeys = optionalKeys
1211
+ mopv.aliasKeys = aliasKeys
1136
1212
 
1137
1213
  r.node =
1138
1214
  addsite(new ConjunctVal({ peg: [mopv, ...mo.___merge] }), r, ctx)
@@ -1140,6 +1216,7 @@ help isolate the syntax error.`,
1140
1216
  else {
1141
1217
  r.node = addsite(new MapVal({ peg: mo }), r, ctx)
1142
1218
  r.node.optionalKeys = optionalKeys
1219
+ r.node.aliasKeys = aliasKeys
1143
1220
  }
1144
1221
 
1145
1222
  return undefined
@@ -1290,6 +1367,33 @@ help isolate the syntax error.`,
1290
1367
  .bc((rule: Rule) => {
1291
1368
  // TRAVERSE PARENTS TO GET PATH
1292
1369
 
1370
+ // A DECLARATION IS A PAIR WHOSE KEY IS AN ALIAS NAME. The lexer
1371
+ // claims `%name` whole and hands it over as a #VL token whose
1372
+ // SOURCE is the name, so the key TEXT alone cannot be the test:
1373
+ // a quoted `"%a": 1` is an ordinary key that merely starts with
1374
+ // the sigil, and erasing that would be wrong. The token is what
1375
+ // separates them.
1376
+ //
1377
+ // Recorded on the enclosing map, never on the value, and that is
1378
+ // the point: a reference COPIES the value it resolves to, so a
1379
+ // mark riding the value would erase the referring field too.
1380
+ // Being a property of the map is also what carries it through a
1381
+ // meet, the way optional keys are carried.
1382
+ const ktkn: any = rule.o0
1383
+ if (null != ktkn && VL === ktkn.tin && ALIAS_RE.test('' + ktkn.src)) {
1384
+ const holder: any = rule.parent
1385
+ const aname = '' + ktkn.src
1386
+
1387
+ // Always recorded here; whether the map is ALLOWED to carry
1388
+ // declarations is decided on the VALUE (MapVal.unify), not at
1389
+ // the parse. The parse cannot see it: an INCLUDED file's
1390
+ // declarations are at the root of their own text, and only
1391
+ // once the loaded map is placed does it become apparent that
1392
+ // root is not the document's.
1393
+ holder.u.aontu_alias_keys = (holder.u.aontu_alias_keys || [])
1394
+ holder.u.aontu_alias_keys.push(aname)
1395
+ }
1396
+
1293
1397
  if (rule.u.spread) {
1294
1398
  rule.node[SPREAD] =
1295
1399
  (rule.node[SPREAD] || { o: rule.o0.src, v: [] })
@@ -1335,7 +1439,10 @@ help isolate the syntax error.`,
1335
1439
  s: [QM, CL],
1336
1440
  c: (r) => r.prev.u.aontu_optional,
1337
1441
  p: 'val',
1338
- u: { spread: true, done: true, list: true, pair: true },
1442
+ u: {
1443
+ spread: true, done: true, list: true, pair: true,
1444
+ aontu_optional_elem: true,
1445
+ },
1339
1446
  a: (r) => {
1340
1447
  pairkey(r.prev)
1341
1448
  r.u.key = r.prev.u.key
@@ -1344,18 +1451,17 @@ help isolate the syntax error.`,
1344
1451
  g: 'aontu-optional-elem'
1345
1452
  },
1346
1453
 
1347
- // A PLAIN pair in list position, `[k:v]`. It contributes no
1348
- // element either -- a key:value pair is simply not a list element,
1349
- // which is the rule the optional form above already followed, and
1350
- // the two spellings must not disagree (issue #40).
1351
- //
1352
- // It needed an alt of its own because only a NON-NUMERIC key was
1353
- // already inert: jsonic writes the pair at `node[key]`, and the
1354
- // node is an array, so `[x:1]` set a property that never showed up
1355
- // (`length` stays 0) while `[0:1]` set an INDEX and became an
1356
- // element -- `[1:2]` even filling the gap with a null. That is the
1357
- // shape of a JavaScript array, not a decision about the language,
1358
- // and it made the two ports disagree on generate as well as canon.
1454
+ // A PLAIN pair in list position IS A SINGLE-KEY MAP ELEMENT:
1455
+ // `[a:1, b:2]` is `[{a:1}, {b:2}]` (the rule @tabnas/jsonic
1456
+ // spells as `list.pair`). This REVERSES issue #40's "a pair is
1457
+ // not an element": that rule was chosen because jsonic wrote
1458
+ // the pair at `node[key]` -- an array PROPERTY that never
1459
+ // showed up for a text key and an INDEX for a numeric one --
1460
+ // and inert beat that incoherence. But inert was itself a
1461
+ // silent drop: `x: [a:1, b:2]` evaluated to `x: []`, the
1462
+ // author's data gone at exit 0. The element is built in the
1463
+ // bc below, where the value is already a Val; the snapshot
1464
+ // still neutralises jsonic's raw slot write first.
1359
1465
  {
1360
1466
  s: [OPTKEY, CL], p: 'val',
1361
1467
  u: { spread: true, done: true, list: true, pair: true },
@@ -1368,17 +1474,109 @@ help isolate the syntax error.`,
1368
1474
  ])
1369
1475
 
1370
1476
 
1371
- .bc((rule: Rule) => {
1477
+ // NOTE: manually adjust path - the twin of the `pair` rule's hook
1478
+ // above, and for the same reason, one layer down.
1479
+ //
1480
+ // Every alt above contributes NO element: a `&:` spread is a
1481
+ // constraint on the elements, and a `k:v` pair in list position is
1482
+ // simply not one (the `aontu-plain-pair-elem` note above). The array
1483
+ // slot they briefly occupy is already given back by
1484
+ // restorePairSlot. The PATH index was not: @tabnas/path's
1485
+ // `@elem-ao` increments `r.k.index` for every elem rule it sees, so
1486
+ // each of these stole an index and every later element's path was
1487
+ // one too high — `[&: integer, 10, 20, "bad"]` reported the bad
1488
+ // value at `$.l.3` while `aontu get $.l.2` returned it, and on a
1489
+ // one-element list the path pointed off the end. Generation was
1490
+ // never wrong, which is why nothing caught it: the array is right
1491
+ // and only the labels on it were shifted (BUGS.md 44).
1492
+ //
1493
+ // Rewinding here rather than in the plugin keeps the plugin's rule
1494
+ // ("in an array, the path property is the element index") true —
1495
+ // these alts are the aontu-specific exceptions to what counts as an
1496
+ // element, so the correction belongs with the grammar that
1497
+ // introduces them. The child is re-pathed because the plugin has
1498
+ // already stamped it with the index being given back: a spread
1499
+ // takes the `'&'` segment its map twin takes, and a pair takes its
1500
+ // key, as a map entry would.
1501
+ .ao((r) => {
1502
+ // A pair IS an element now, so it keeps the index @tabnas/path
1503
+ // gave it, and its VALUE is pathed through both the index and
1504
+ // the key (`[a: $.nope]` fails at $.l.0.a). Only the `&:`
1505
+ // spread still contributes no element and gives its index back
1506
+ // (BUGS.md 44).
1507
+ if (0 < r.d && r.u.spread && !r.u.pair) {
1508
+ r.k.index = r.k.index - 1
1509
+
1510
+ const seg = '&'
1511
+ r.child.k.path = [...r.k.path, seg]
1512
+ r.child.k.key = seg
1513
+ }
1514
+ else if (0 < r.d && r.u.pair) {
1515
+ // The element's index is the array length: everything before
1516
+ // it is already pushed, and the pair's own map is pushed at
1517
+ // close. `r.k.index` is not usable here -- the path plugin
1518
+ // counts only the elements it pushes itself, and this one is
1519
+ // aontu's.
1520
+ const seg = '' + r.u.key
1521
+ r.child.k.path =
1522
+ [...r.k.path, '' + (r.node?.length ?? 0), seg]
1523
+ r.child.k.key = seg
1524
+ }
1525
+ })
1526
+
1527
+ .bc((rule: Rule, ctx: JsonicContext) => {
1372
1528
  // TRAVERSE PARENTS TO GET PATH
1373
1529
 
1374
- if (rule.u.spread) {
1530
+ // Only the `&:` alternative is a SPREAD. All four alts above set
1531
+ // `spread: true` -- it is what marks them as contributing no
1532
+ // element -- so this guard needs the narrower test, and `pair`
1533
+ // is what distinguishes a `k:v` in list position from a spread.
1534
+ //
1535
+ // Without it a pair BUILT the spread record, with `o` taken from
1536
+ // its own key rather than '&': `[x:1, &:integer, "bad"]` left
1537
+ // `{o:'x', v:[1, integer]}`, and ListVal's `'&' === spread.o`
1538
+ // then discarded the real constraint -- so the element spread
1539
+ // was silently dropped and the bad value generated (BUGS.md 46).
1540
+ // A pair alone did it too: `[x:1, 10]` produced a spread record
1541
+ // out of nothing.
1542
+ if (rule.u.spread && !rule.u.pair) {
1375
1543
  rule.node[SPREAD] =
1376
1544
  (rule.node[SPREAD] || { o: rule.o0.src, v: [] })
1377
1545
  rule.node[SPREAD].v.push(rule.child.node)
1378
1546
  }
1379
1547
 
1548
+ // The slot is given back BEFORE the element is added: the
1549
+ // restore undoes jsonic's raw write (a property for a text
1550
+ // key, an INDEX for a numeric one -- restoring length is what
1551
+ // keeps `[1:2]` from padding with a null), and the push then
1552
+ // appends cleanly after it.
1380
1553
  restorePairSlot(rule)
1381
1554
 
1555
+ // THE SINGLE-KEY MAP ELEMENT, for both pair spellings. The
1556
+ // value is a Val already (`p: 'val'`), so the map is built
1557
+ // exactly as the map rule builds one -- and an elided value
1558
+ // (`[a:]`) is refused exactly as the map rule refuses one
1559
+ // (issue #48): a key with nothing after the colon is a
1560
+ // mistake, not an empty value.
1561
+ if (true === rule.u.pair) {
1562
+ const key = '' + rule.u.key
1563
+ let v: any = rule.child.node
1564
+ if (null == v) {
1565
+ v = addsite(new NilVal({ why: 'elided_value' }), rule, ctx)
1566
+ v.path = [...(rule.k?.path ?? []),
1567
+ '' + rule.node.length, key]
1568
+ }
1569
+ const mv: any = addsite(
1570
+ new MapVal({ peg: { [key]: v } }), rule, ctx)
1571
+ // `[a?: 1]` is `[{a?: 1}]`: the key is optional IN the
1572
+ // element, so the two spellings stay one rule apart rather
1573
+ // than two behaviours apart.
1574
+ if (true === rule.u.aontu_optional_elem) {
1575
+ mv.optionalKeys = [key]
1576
+ }
1577
+ rule.node.push(mv)
1578
+ }
1579
+
1382
1580
  return undefined
1383
1581
  })
1384
1582
 
@@ -1391,12 +1589,202 @@ help isolate the syntax error.`,
1391
1589
 
1392
1590
 
1393
1591
 
1592
+ // INCLUDE_KINDS IS THE RULE FOR WHAT AN INCLUDE MEANS (ADR-012,
1593
+ // use-cases/BUGS.md §49). An extension is on this list or it is not
1594
+ // read at all, and its entry says WHICH OF TWO THINGS the file is.
1595
+ //
1596
+ // `source` — Aontu, with everything the language has: types, defaults,
1597
+ // references, constraints, its own includes. Two extensions, and they
1598
+ // are the ones this project owns.
1599
+ //
1600
+ // A FORMAT NAME — configuration DATA, parsed by that format's own
1601
+ // parser into the JSON value it denotes, which then becomes Aontu
1602
+ // values like any other data. Every one of these formats maps onto
1603
+ // JSON, which is why one word covers them: a `.toml` file is a map of
1604
+ // scalars, lists and maps, and so is the `.aon` file that unifies with
1605
+ // it. What the format does NOT get is the language — a `&` in a YAML
1606
+ // file is a YAML anchor, not a spread key, because the YAML parser
1607
+ // reads it, not this one.
1608
+ //
1609
+ // The parsers are @tabnas's, one per format, and the Go port uses the
1610
+ // same ones (ADR-001): the two implementations agree because they are
1611
+ // running the same grammar, not because two hand-written readers were
1612
+ // kept in step.
1613
+ //
1614
+ // This table and go/source.go's includeKinds are the same table.
1615
+ const INCLUDE_KINDS: { [kind: string]: string } = {
1616
+ aon: 'source',
1617
+ aontu: 'source',
1618
+
1619
+ json: 'json',
1620
+ // JSON-LD is JSON: a `@context` is a key like any other here, and
1621
+ // what it MEANS is the vocabulary's business, not the reader's.
1622
+ jsonld: 'json',
1623
+ jsonc: 'jsonc',
1624
+ json5: 'json5',
1625
+ jsonic: 'jsonic',
1626
+ jsc: 'jsonic',
1627
+ toml: 'toml',
1628
+ yaml: 'yaml',
1629
+ yml: 'yaml',
1630
+ ini: 'ini',
1631
+ }
1632
+
1633
+ // `.csv` IS DELIBERATELY ABSENT, and the reason is ADR-001 rather than
1634
+ // taste. The two ports' CSV parsers disagree about what a CSV file IS:
1635
+ // one answers header-keyed records with string fields, the other raw
1636
+ // rows including the header, with numbers parsed. Admitting it would
1637
+ // admit a divergence into the one thing this project refuses to have
1638
+ // one in. Recorded in ADR-012 and pinned by file.tsv's load-ext-csv.
1639
+
1640
+ // The multisource kind of a path: the LAST segment's extension, without
1641
+ // its dot, lowercased -- `''` for a name that has none. The rule is
1642
+ // @tabnas/multisource's own extKind (and Go's filepath.Ext), copied
1643
+ // rather than imported because it decides what a source IS: a dot in a
1644
+ // parent folder (`/my.app/conf`) must not read as an extension.
1645
+ function extKindOf(full: string): string {
1646
+ const seg = (full.match(/[^\\/]*$/) as string[])[0]
1647
+ return (seg.match(/\.([^.]*)$/) || ['', ''])[1].toLowerCase()
1648
+ }
1649
+
1650
+ // The refusal message, naming the extension -- because the extension is
1651
+ // the whole reason, and a reader told only "not readable" has to guess
1652
+ // which part of the path the engine objected to. Byte-identical to Go's
1653
+ // extensionMsg.
1654
+ function extensionMsg(path: string, ext: string): string {
1655
+ const which = '' === ext ? 'no extension' : 'extension: .' + ext
1656
+ return 'include not readable: ' + path + ' (' + which + ')'
1657
+ }
1658
+
1659
+ // THE RULE ALSO HOLDS FOR A RESOLVER THIS ENGINE DID NOT WRITE.
1660
+ // gateExtension refuses an unlisted extension inside makeModelResolver,
1661
+ // which is the default; a HOST may supply its own through
1662
+ // `AontuOptions.resolver`, and that one has never heard of
1663
+ // INCLUDE_KINDS. Without this the host's resolution would fall to
1664
+ // multisource's own default for an unnamed kind, which hands the file
1665
+ // back as TEXT — or, for `.js`, EXECUTES it. So the two roads end in
1666
+ // one place: whatever chose the source, an extension off the list is
1667
+ // refused with the same code and the same message.
1668
+ const refuseProcessor = (res: any) => {
1669
+ // `full` is the one part a host resolution may leave out -- it is the
1670
+ // path the resolver CHOSE, and a resolver that answers from something
1671
+ // other than a filesystem need not have one. The written path always
1672
+ // reaches here, so it is the fallback.
1673
+ const err: any = new Error(extensionMsg(res.path, extKindOf(res.full ?? res.path)))
1674
+ err.code = 'include_extension'
1675
+ throw err
1676
+ }
1677
+
1678
+
1679
+ // ONE READER PER FORMAT, BUILT ONCE. These are stateless parsers and
1680
+ // building a jsonic instance is not free, so they are made at module
1681
+ // load rather than per include. The file name is passed through so a
1682
+ // syntax error inside an included `.toml` names the `.toml`.
1683
+ const DATA_READERS: { [format: string]: (src: string, fileName: string) => any } =
1684
+ (() => {
1685
+ const viaPlugin = (plugin: any) => {
1686
+ const jsonic = Jsonic.make().use(plugin)
1687
+ return (src: string, fileName: string) => jsonic(src, { fileName })
1688
+ }
1689
+ const toml = viaPlugin(Toml)
1690
+ // The strict RFC 8259 reader is its own parser rather than a
1691
+ // plugin, and it is `make().parse` rather than the module's bare
1692
+ // `parse`: only the instance carries the meta bag, and without it
1693
+ // a syntax error in an included `.json` says `<no-file>`.
1694
+ const json = makeJsonParser()
1695
+ return {
1696
+ json: (src: string, fileName: string) => json.parse(src, { fileName }),
1697
+ jsonc: viaPlugin(Jsonc),
1698
+ json5: viaPlugin(Json5),
1699
+ // Plain jsonic needs no plugin: it IS the base parser.
1700
+ jsonic: (src: string, fileName: string) => Jsonic(src, { fileName }),
1701
+ toml: (src: string, fileName: string) => tomlDates(toml(src, fileName)),
1702
+ yaml: viaPlugin(Yaml),
1703
+ ini: viaPlugin(Ini),
1704
+ }
1705
+ })()
1706
+
1707
+ /**
1708
+ * A TOML document with its dates as the TEXT they were written as.
1709
+ *
1710
+ * TOML HAS DATES AND JSON DOES NOT, so the reader cannot hand one over
1711
+ * as itself: it answers with a marker object carrying the kind and the
1712
+ * source text. The value that reaches a document is that TEXT, which is
1713
+ * what a JSON document carries for a date anyway — and it is what the
1714
+ * Go port produces too, from a `*TomlTime` holding those same two
1715
+ * fields (`dataToValDepth`, go/source.go). Without this the same file
1716
+ * is a nested map in one port and a string in the other, which is the
1717
+ * class of divergence ADR-012 exists to stop.
1718
+ *
1719
+ * The guard is exact — one key, `__toml__`, holding a `kind` and a
1720
+ * `src` string — so a document whose own data happens to use the name
1721
+ * passes through untouched.
1722
+ */
1723
+ function tomlDates(node: any): any {
1724
+ if (Array.isArray(node)) {
1725
+ return node.map(tomlDates)
1726
+ }
1727
+ if (null === node || 'object' !== typeof node) {
1728
+ return node
1729
+ }
1730
+ const keys = Object.keys(node)
1731
+ const mark = node.__toml__
1732
+ if (1 === keys.length && '__toml__' === keys[0] && null != mark &&
1733
+ 'string' === typeof mark.kind && 'string' === typeof mark.src) {
1734
+ return mark.src
1735
+ }
1736
+ const out: Record<string, any> = {}
1737
+ for (const k of keys) {
1738
+ out[k] = tomlDates(node[k])
1739
+ }
1740
+ return out
1741
+ }
1742
+
1743
+ /**
1744
+ * Read one included file as DATA in the named format.
1745
+ *
1746
+ * The parser hands back the JSON value the file denotes — plain maps,
1747
+ * lists and scalars — and rawToVal turns that into Vals. THE
1748
+ * CONVERSION HAPPENS HERE, not at the top level, because an include is
1749
+ * usually not at the top level: `a: @"conf.toml"` puts the value under
1750
+ * a key, where a raw JavaScript object is something the tree cannot
1751
+ * unify with (the crash that was BUGS §49b).
1752
+ */
1753
+ const dataProcessor = (format: string) => (res: any) => {
1754
+ res.val = rawToVal(DATA_READERS[format](res.src, res.path))
1755
+ }
1756
+
1757
+ /**
1758
+ * The multisource processor map, built FROM the include table so the
1759
+ * two cannot drift: every extension the table names gets the reader
1760
+ * the table names for it, and the two kinds that are not in the table
1761
+ * refuse.
1762
+ */
1763
+ function includeProcessors(): { [kind: string]: any } {
1764
+ const map: { [kind: string]: any } = {
1765
+ // multisource's fallback for an extension no entry names, so it is
1766
+ // the one that catches whatever the resolver's gate did not.
1767
+ '': refuseProcessor,
1768
+ // ... and the one upstream default that would EXECUTE the file.
1769
+ js: refuseProcessor,
1770
+ }
1771
+ const source = makeJsonicProcessor()
1772
+ for (const kind of Object.keys(INCLUDE_KINDS)) {
1773
+ const format = INCLUDE_KINDS[kind]
1774
+ map[kind] = 'source' === format ? source : dataProcessor(format)
1775
+ }
1776
+ return map
1777
+ }
1778
+
1779
+
1394
1780
  // SECURITY: under the DEFAULT ('system') include capability this
1395
- // resolver reads any file/package the process can reach — @"path"
1396
- // follows relative paths (`@"../../etc/passwd"`) and symlinks, and
1397
- // @"pkg" can require() arbitrary installed modules — so treat opening
1398
- // an untrusted source as running it. The trust profile (G5,
1399
- // docs/trust.md) is the confinement surface: `trust.include` of
1781
+ // resolver reads any file the process can reach — @"path" follows
1782
+ // relative paths (`@"../../etc/passwd.aon"`) and symlinks — so treat
1783
+ // opening an untrusted source as reading your disk. It no longer RUNS
1784
+ // one: @"pkg" could require() an arbitrary installed module until
1785
+ // ADR-012, which refuses a `.js` entry point by the same rule that
1786
+ // refuses `.txt`. The trust profile (G5, docs/trust.md) is the
1787
+ // confinement surface: `trust.include` of
1400
1788
  // 'none', `{ mem }` or `{ root }` restricts what `@"..."` may resolve,
1401
1789
  // and a denied resolution is a deterministic parse-stage
1402
1790
  // `include_denied` error.
@@ -1489,6 +1877,27 @@ function makeModelResolver(options: any) {
1489
1877
  throw err
1490
1878
  }
1491
1879
 
1880
+ // AN UNREADABLE EXTENSION THROWS, exactly as a denial does, and for
1881
+ // the same reason: a bare-member include (`@"notes.txt"` at the top
1882
+ // of a file) MERGES into the enclosing map, and a nil contributes no
1883
+ // keys, so an injected refusal would vanish and leave a plausible,
1884
+ // silently-partial document. Lang.parse turns the throw into the
1885
+ // parse-stage `include_extension` nil.
1886
+ const refuseExtension = (path: string, full: string): never => {
1887
+ const err: any = new Error(extensionMsg(path, extKindOf(full)))
1888
+ err.code = 'include_extension'
1889
+ throw err
1890
+ }
1891
+
1892
+ // The gate every leg that RESOLVES A NAME passes through. The std and
1893
+ // module legs do not: both state `kind: 'aon'` because what they
1894
+ // serve is Aontu source by construction, not by its spelling.
1895
+ const gateExtension = (path: string, full: string): void => {
1896
+ if (undefined === INCLUDE_KINDS[extKindOf(full)]) {
1897
+ refuseExtension(path, full)
1898
+ }
1899
+ }
1900
+
1492
1901
  // The user cache: whatever the host named, else the platform rule
1493
1902
  // (`modCacheDir`, ts/src/mod.ts) the tooling writes by.
1494
1903
  const modCache = (opts: any): string | undefined => {
@@ -1571,6 +1980,11 @@ function makeModelResolver(options: any) {
1571
1980
  let res = memResolver(path, popts, rule, ctx, jsonic)
1572
1981
  res.path = path
1573
1982
  if (res.found) {
1983
+ // THE EXTENSION DECIDES HERE TOO. A virtual file set is still a
1984
+ // file set: its keys carry extensions, and the same rule has to
1985
+ // read them, or the mem capability becomes a way to include what
1986
+ // the filesystem would refuse.
1987
+ gateExtension(path, res.full ?? path)
1574
1988
  record(ctx, res.full ?? path, 'mem')
1575
1989
  return res
1576
1990
  }
@@ -1623,6 +2037,10 @@ function makeModelResolver(options: any) {
1623
2037
  if (null != rootDir && outsideRoot(rootDir, full)) {
1624
2038
  deny(path)
1625
2039
  }
2040
+ // After the trust check, not before: a file outside the
2041
+ // confinement root is denied whatever it is called, and answering
2042
+ // "extension" there would say the file exists.
2043
+ gateExtension(path, full)
1626
2044
  // The warning window for the staged default flip (G5 phase 6):
1627
2045
  // under 'system', the CLI supplies trustWarn and the entry root,
1628
2046
  // and every resolution escaping that root names the flag a future
@@ -1648,6 +2066,7 @@ function makeModelResolver(options: any) {
1648
2066
  res = pkgResolver(path, popts, rule, ctx, jsonic)
1649
2067
  res.path = path
1650
2068
  if (res.found) {
2069
+ gateExtension(path, res.full as string)
1651
2070
  if (null != options.trustWarn) {
1652
2071
  options.trustWarn('pkg', res.full as string)
1653
2072
  }
@@ -1661,63 +2080,62 @@ function makeModelResolver(options: any) {
1661
2080
  }
1662
2081
 
1663
2082
 
1664
- // funcArity is the permitted WRITTEN argument count of each built-in, as
2083
+ // THE SIGNATURE REGISTRY (docs/design/SIGNATURES.0.md). The call
2084
+ // surface is DECLARED in test/spec/signature.tsv and parsed by the
2085
+ // signature grammar (ts/src/sig.ts) from the build-time-inlined copy;
2086
+ // the arity table and the positional set below are DERIVED from the
2087
+ // parsed registry (funcSig, ts/src/sig.ts), so the declaration is the
2088
+ // one source. go/func.go derives the same two tables from the same
2089
+ // text.
1665
2090
  // The functions whose comma-separated arguments are distinct POSITIONS
1666
2091
  // rather than one argument list. See the func-paren handler above: this
1667
2092
  // is the set whose comma group is expanded back into separate `peg`
1668
- // entries.
1669
- const POSITIONAL_ARG_FUNCS: Record<string, boolean> = {
1670
- deprecate: true, pack: true, each: true, filter: true, match: true,
1671
- // Arithmetic takes two OPERANDS, and an operand is a position: `sub`
1672
- // is not commutative, so `sub(a, b)` reaching the engine as one
1673
- // two-element list would lose which is which.
1674
- add: true, sub: true, mul: true, div: true, mod: true, rem: true,
1675
- // The bag and the key are distinct positions.
1676
- pick: true,
2093
+ // entries. Derived: two or more declared argument slots, excluding the
2094
+ // residual producers (`constraint` results) -- the constraint atoms
2095
+ // make the same expansion in their own constructor (`atomArgs`,
2096
+ // ConstraintVal.ts, deliberately before the settled check), which is
2097
+ // why they are not in this set; `must` is the load-bearing example.
2098
+ // Arithmetic is here because `sub` is not commutative: `sub(a, b)`
2099
+ // reaching the engine as one two-element list would lose which is
2100
+ // which.
2101
+ const POSITIONAL_ARG_FUNCS: Record<string, boolean> = {}
2102
+ for (const name in funcSig) {
2103
+ if (2 <= funcSig[name].args.length && 'constraint' !== funcSig[name].out) {
2104
+ POSITIONAL_ARG_FUNCS[name] = true
2105
+ }
1677
2106
  }
1678
2107
 
1679
2108
 
1680
2109
  // [min, max]; a max of -1 is unbounded. Every name in funcMap has an
1681
2110
  // entry, and the arity is a property of the language rather than of
1682
- // either port -- go/func.go carries the same table.
1683
- //
1684
- // Nearly everything takes exactly one. The exceptions earn their place:
1685
- // key() names how many levels UP the path to read, defaulting to the
1686
- // parent when omitted, neq takes a whole set of exclusions, unique()
1687
- // takes none (a property of the container) or a PROJECTOR key, must()
1688
- // takes a check AND the author's message for when it fails, and the
1689
- // arithmetic family takes two operands.
1690
- const funcArity: Record<string, [number, number]> = {
1691
- upper: [1, 1], lower: [1, 1], copy: [1, 1], pref: [1, 1],
1692
- super: [1, 1], type: [1, 1], hide: [1, 1], close: [1, 1],
1693
- open: [1, 1], move: [1, 1], path: [1, 1],
1694
- min: [1, 1], max: [1, 1], above: [1, 1], below: [1, 1], re: [1, 1],
1695
- length: [1, 1],
1696
- key: [0, 1],
1697
- // THE ONE ARGUMENT IS A PROJECTOR: `unique(port)` says no two
1698
- // members share a `port`. The arity was reserved for it (finding I).
1699
- unique: [0, 1],
1700
- neq: [1, -1],
1701
- must: [2, 2],
1702
- deprecate: [1, 2],
1703
- id: [1, 1],
1704
- refer: [0, 1],
1705
- pack: [2, 2],
1706
- each: [1, 2],
1707
- filter: [2, 2],
1708
- // The scrutinee, then pattern/result pairs, then an optional
1709
- // default: three arguments at least, and any number above that.
1710
- match: [3, -1],
1711
- // Two operands, always. Arithmetic has no variadic reading that is
1712
- // not a fold, and a fold is the recursion this language refuses.
1713
- add: [2, 2], sub: [2, 2], mul: [2, 2],
1714
- div: [2, 2], mod: [2, 2], rem: [2, 2],
1715
- // One bag. The operation is fixed, so there is nothing else to pass:
1716
- // a fold's second argument is a FUNCTION, and this language has none
1717
- // to give it.
1718
- sum: [1, 1], least: [1, 1], greatest: [1, 1],
1719
- // The bag, and the key to take from each of its children.
1720
- pick: [2, 2],
2111
+ // either port -- go/func.go derives the same table. A required slot
2112
+ // counts toward the minimum; a rest slot makes the maximum unbounded
2113
+ // and counts its group size (one, for a plain rest type) toward the
2114
+ // minimum, which is what gives `match` its floor of three and `neq`
2115
+ // its floor of one.
2116
+ function sigArity(sig: FuncSig): [number, number] {
2117
+ let min = 0
2118
+ let max = 0
2119
+ for (const a of sig.args) {
2120
+ if (true === a.rest) {
2121
+ min += undefined === a.group ? 1 : a.group.length
2122
+ max = -1
2123
+ }
2124
+ else {
2125
+ if (true !== a.opt) {
2126
+ min++
2127
+ }
2128
+ if (-1 !== max) {
2129
+ max++
2130
+ }
2131
+ }
2132
+ }
2133
+ return [min, max]
2134
+ }
2135
+
2136
+ const funcArity: Record<string, [number, number]> = {}
2137
+ for (const name in funcSig) {
2138
+ funcArity[name] = sigArity(funcSig[name])
1721
2139
  }
1722
2140
 
1723
2141
 
@@ -1754,11 +2172,11 @@ function arityText(lo: number, hi: number): string {
1754
2172
  if (lo !== hi) {
1755
2173
  return 0 === lo ? 'no arguments or one' : 'one argument or two'
1756
2174
  }
1757
- // NO {0,0} ARM. `unique` was the only built-in taking none, and its
1758
- // one argument is now the projector the arity was reserved for
1759
- // (finding I), so a phrasing for a count no entry carries would be
1760
- // untested prose pretending to be tested -- the rule this function's
1761
- // header states. The arm returns with the table, if one ever does.
2175
+ // The {0,0} arm returned with the container kinds and acyclic()
2176
+ // (ADR-015): `map(1)` must not claim map takes exactly one.
2177
+ if (0 === hi) {
2178
+ return 'no arguments'
2179
+ }
1762
2180
  if (2 === hi) {
1763
2181
  return 'exactly two arguments'
1764
2182
  }
@@ -1800,15 +2218,22 @@ function opCharHint(src: string): string {
1800
2218
 
1801
2219
 
1802
2220
  function rawToVal(n: any): Val {
1803
- if (null == n) {
1804
- return new NullVal({ peg: null })
1805
- }
1806
- if (true === n.isVal) {
2221
+ if (true === n?.isVal) {
1807
2222
  return n
1808
2223
  }
1809
2224
  if (Array.isArray(n)) {
1810
2225
  return new ListVal({ peg: n.map(rawToVal) })
1811
2226
  }
2227
+
2228
+ // THE SCALAR ARMS ARE WHERE A CONFIG FILE BECOMES VALUES. Every
2229
+ // format on the include table is read by its own parser into plain
2230
+ // JavaScript -- a string, a number, a map -- and this is the walk
2231
+ // that turns that into Vals (dataProcessor, ADR-012). The two arms
2232
+ // above are the other caller: a raw expression TERM, which the
2233
+ // expression grammar hands over already built.
2234
+ if (null == n) {
2235
+ return new NullVal({ peg: null })
2236
+ }
1812
2237
  const t = typeof n
1813
2238
  if ('string' === t) {
1814
2239
  return new StringVal({ peg: n })
@@ -1823,14 +2248,20 @@ function rawToVal(n: any): Val {
1823
2248
  if ('boolean' === t) {
1824
2249
  return new BooleanVal({ peg: n })
1825
2250
  }
1826
- if ('object' === t) {
1827
- const peg: Record<string, Val> = {}
1828
- for (const k in n) {
1829
- peg[k] = rawToVal(n[k])
1830
- }
1831
- return new MapVal({ peg })
2251
+ // AND EVERYTHING ELSE IS A MAP, with no arm after it because there is
2252
+ // nothing after it. Every reader on the include table answers with
2253
+ // the JSON kinds and no others -- probed, including the two that
2254
+ // could plausibly escape them: a big integer comes back a `number`,
2255
+ // and a TOML date is normalised to its text before it gets here. The
2256
+ // one include that could hand over a function was `.js`, which
2257
+ // ADR-012 refuses. `parse_unknown` lived here for that case and has
2258
+ // no producer left in this port; the Go twin keeps its own, where the
2259
+ // type switch really can be handed something unaccounted for.
2260
+ const peg: Record<string, Val> = {}
2261
+ for (const k in n) {
2262
+ peg[k] = rawToVal(n[k])
1832
2263
  }
1833
- return new NilVal({ why: 'parse_unknown' })
2264
+ return new MapVal({ peg })
1834
2265
  }
1835
2266
 
1836
2267
 
@@ -1862,11 +2293,22 @@ class Lang {
1862
2293
  // works. `.jsonic` is retired (no longer auto-resolved); the
1863
2294
  // default `['jsonic','jsc','json','js']` is overridden here.
1864
2295
  // (Upstream option name is the misspelled `implictExt`.)
2296
+ //
2297
+ // Only these two are SEARCHED for a bare `@"name"`; `.json` and
2298
+ // `.jsonld` are read when NAMED, which is how a vendored
2299
+ // vocabulary is always written.
1865
2300
  implictExt: ['aon', 'aontu'],
1866
- processor: {
1867
- aontu: 'jsonic',
1868
- aon: 'jsonic',
1869
- }
2301
+ // ONE ENTRY PER EXTENSION THE TABLE NAMES, built from it (see
2302
+ // includeProcessors) so the rule and its wiring cannot drift.
2303
+ //
2304
+ // The upstream defaults are REPLACED, not extended. Its `json`
2305
+ // entry is what made that extension the one that crashed: it
2306
+ // hands back a raw JS object where the aontu grammar produces
2307
+ // Vals, and the tree then met a value it could not convert
2308
+ // (BUGS §49b). Its `js` entry EXECUTES the file, which is not
2309
+ // something an extension should be able to ask for. And its
2310
+ // fallback hands any other file back as TEXT.
2311
+ processor: includeProcessors()
1870
2312
  })
1871
2313
  .use(AontuJsonic)
1872
2314
  }
@@ -1916,18 +2358,15 @@ class Lang {
1916
2358
  }
1917
2359
  }
1918
2360
  catch (e: any) {
1919
- if ('include_denied' === e?.code ||
1920
- 'module_missing' === e?.code || 'module_integrity' === e?.code ||
1921
- 'module_depth' === e?.code) {
1922
- // A denied include (trust profile, G5): the resolver throws so
1923
- // a bare-member include cannot vanish in the merge, and the
1924
- // code survives here as the parse-stage nil the registry
1925
- // pins (errcodes.tsv: include_denied, class parse).
1926
- // A denied include (G5) and a module that is missing or fails
1927
- // its pin (G6 phase 2) are refused the same way, for the same
1928
- // reason: the resolver THROWS so a bare-member include cannot
1929
- // vanish in the merge, and the code survives here as the
1930
- // parse-stage nil the registry pins (errcodes.tsv).
2361
+ if ('include_denied' === e?.code || 'include_extension' === e?.code ||
2362
+ MODULE_REFUSAL_CODES.has(e?.code)) {
2363
+ // A denied include (G5), an include whose extension is not read
2364
+ // as Aontu source (ADR-012, INCLUDE_KINDS), and a module that is
2365
+ // missing, fails its pin, or names a path that escapes its store
2366
+ // (G6 phase 2) are refused the same way, for the same reason: the
2367
+ // resolver THROWS so a bare-member include cannot vanish in the
2368
+ // merge, and the code survives here as the parse-stage nil the
2369
+ // registry pins (errcodes.tsv).
1931
2370
  val = new NilVal({
1932
2371
  why: 'parse',
1933
2372
  err: new NilVal({