aontu 0.30.1 → 0.31.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 (184) hide show
  1. package/dist/ctx.d.ts +51 -0
  2. package/dist/ctx.js +105 -0
  3. package/dist/ctx.js.map +1 -0
  4. package/dist/err.d.ts +12 -0
  5. package/dist/err.js +123 -0
  6. package/dist/err.js.map +1 -0
  7. package/dist/hints.d.ts +8 -0
  8. package/dist/hints.js +90 -0
  9. package/dist/hints.js.map +1 -0
  10. package/dist/lang.d.ts +11 -0
  11. package/dist/lang.js +569 -0
  12. package/dist/lang.js.map +1 -0
  13. package/dist/site.d.ts +12 -0
  14. package/dist/site.js +27 -0
  15. package/dist/site.js.map +1 -0
  16. package/dist/tsconfig.tsbuildinfo +1 -0
  17. package/dist/type.d.ts +32 -0
  18. package/dist/type.js +16 -0
  19. package/dist/type.js.map +1 -0
  20. package/dist/unify.d.ts +14 -0
  21. package/dist/unify.js +162 -0
  22. package/dist/unify.js.map +1 -0
  23. package/dist/utility.d.ts +14 -0
  24. package/dist/utility.js +128 -0
  25. package/dist/utility.js.map +1 -0
  26. package/dist/val/BagVal.d.ts +10 -0
  27. package/dist/val/BagVal.js +15 -0
  28. package/dist/val/BagVal.js.map +1 -0
  29. package/dist/val/BooleanVal.d.ts +9 -0
  30. package/dist/val/BooleanVal.js +16 -0
  31. package/dist/val/BooleanVal.js.map +1 -0
  32. package/dist/val/CloseFuncVal.d.ts +11 -0
  33. package/dist/val/CloseFuncVal.js +32 -0
  34. package/dist/val/CloseFuncVal.js.map +1 -0
  35. package/dist/val/ConjunctVal.d.ts +14 -0
  36. package/dist/val/ConjunctVal.js +187 -0
  37. package/dist/val/ConjunctVal.js.map +1 -0
  38. package/dist/val/CopyFuncVal.d.ts +14 -0
  39. package/dist/val/CopyFuncVal.js +43 -0
  40. package/dist/val/CopyFuncVal.js.map +1 -0
  41. package/dist/val/DisjunctVal.d.ts +19 -0
  42. package/dist/val/DisjunctVal.js +194 -0
  43. package/dist/val/DisjunctVal.js.map +1 -0
  44. package/dist/val/FeatureVal.d.ts +10 -0
  45. package/dist/val/FeatureVal.js +29 -0
  46. package/dist/val/FeatureVal.js.map +1 -0
  47. package/dist/val/FuncBaseVal.d.ts +15 -0
  48. package/dist/val/FuncBaseVal.js +131 -0
  49. package/dist/val/FuncBaseVal.js.map +1 -0
  50. package/dist/val/HideFuncVal.d.ts +12 -0
  51. package/dist/val/HideFuncVal.js +48 -0
  52. package/dist/val/HideFuncVal.js.map +1 -0
  53. package/dist/val/IntegerVal.d.ts +9 -0
  54. package/dist/val/IntegerVal.js +46 -0
  55. package/dist/val/IntegerVal.js.map +1 -0
  56. package/dist/val/JunctionVal.d.ts +12 -0
  57. package/dist/val/JunctionVal.js +31 -0
  58. package/dist/val/JunctionVal.js.map +1 -0
  59. package/dist/val/KeyFuncVal.d.ts +13 -0
  60. package/dist/val/KeyFuncVal.js +64 -0
  61. package/dist/val/KeyFuncVal.js.map +1 -0
  62. package/dist/val/ListVal.d.ts +17 -0
  63. package/dist/val/ListVal.js +186 -0
  64. package/dist/val/ListVal.js.map +1 -0
  65. package/dist/val/LowerFuncVal.d.ts +12 -0
  66. package/dist/val/LowerFuncVal.js +40 -0
  67. package/dist/val/LowerFuncVal.js.map +1 -0
  68. package/dist/val/MapVal.d.ts +16 -0
  69. package/dist/val/MapVal.js +233 -0
  70. package/dist/val/MapVal.js.map +1 -0
  71. package/dist/val/MoveFuncVal.d.ts +12 -0
  72. package/dist/val/MoveFuncVal.js +44 -0
  73. package/dist/val/MoveFuncVal.js.map +1 -0
  74. package/dist/val/NilVal.d.ts +25 -0
  75. package/dist/val/NilVal.js +92 -0
  76. package/dist/val/NilVal.js.map +1 -0
  77. package/dist/val/NullVal.d.ts +9 -0
  78. package/dist/val/NullVal.js +18 -0
  79. package/dist/val/NullVal.js.map +1 -0
  80. package/dist/val/NumberVal.d.ts +9 -0
  81. package/dist/val/NumberVal.js +44 -0
  82. package/dist/val/NumberVal.js.map +1 -0
  83. package/dist/val/OpBaseVal.d.ts +18 -0
  84. package/dist/val/OpBaseVal.js +152 -0
  85. package/dist/val/OpBaseVal.js.map +1 -0
  86. package/dist/val/OpenFuncVal.d.ts +11 -0
  87. package/dist/val/OpenFuncVal.js +30 -0
  88. package/dist/val/OpenFuncVal.js.map +1 -0
  89. package/dist/val/PathFuncVal.d.ts +13 -0
  90. package/dist/val/PathFuncVal.js +42 -0
  91. package/dist/val/PathFuncVal.js.map +1 -0
  92. package/dist/val/PlusOpVal.d.ts +16 -0
  93. package/dist/val/PlusOpVal.js +59 -0
  94. package/dist/val/PlusOpVal.js.map +1 -0
  95. package/dist/val/PrefFuncVal.d.ts +11 -0
  96. package/dist/val/PrefFuncVal.js +39 -0
  97. package/dist/val/PrefFuncVal.js.map +1 -0
  98. package/dist/val/PrefVal.d.ts +15 -0
  99. package/dist/val/PrefVal.js +112 -0
  100. package/dist/val/PrefVal.js.map +1 -0
  101. package/dist/val/RefVal.d.ts +22 -0
  102. package/dist/val/RefVal.js +310 -0
  103. package/dist/val/RefVal.js.map +1 -0
  104. package/dist/val/ScalarKindVal.d.ts +16 -0
  105. package/dist/val/ScalarKindVal.js +76 -0
  106. package/dist/val/ScalarKindVal.js.map +1 -0
  107. package/dist/val/ScalarVal.d.ts +16 -0
  108. package/dist/val/ScalarVal.js +62 -0
  109. package/dist/val/ScalarVal.js.map +1 -0
  110. package/dist/val/StringVal.d.ts +10 -0
  111. package/dist/val/StringVal.js +19 -0
  112. package/dist/val/StringVal.js.map +1 -0
  113. package/dist/val/SuperFuncVal.d.ts +11 -0
  114. package/dist/val/SuperFuncVal.js +22 -0
  115. package/dist/val/SuperFuncVal.js.map +1 -0
  116. package/dist/val/TopVal.d.ts +16 -0
  117. package/dist/val/TopVal.js +38 -0
  118. package/dist/val/TopVal.js.map +1 -0
  119. package/dist/val/TypeFuncVal.d.ts +12 -0
  120. package/dist/val/TypeFuncVal.js +35 -0
  121. package/dist/val/TypeFuncVal.js.map +1 -0
  122. package/dist/val/UpperFuncVal.d.ts +12 -0
  123. package/dist/val/UpperFuncVal.js +40 -0
  124. package/dist/val/UpperFuncVal.js.map +1 -0
  125. package/dist/val/Val.d.ts +90 -0
  126. package/dist/val/Val.js +205 -0
  127. package/dist/val/Val.js.map +1 -0
  128. package/dist/val/VarVal.d.ts +13 -0
  129. package/dist/val/VarVal.js +109 -0
  130. package/dist/val/VarVal.js.map +1 -0
  131. package/dist/val/top.d.ts +2 -0
  132. package/dist/val/top.js +10 -0
  133. package/dist/val/top.js.map +1 -0
  134. package/dist/val/valutil.d.ts +2 -0
  135. package/dist/val/valutil.js +30 -0
  136. package/dist/val/valutil.js.map +1 -0
  137. package/package.json +3 -5
  138. package/src/aontu.ts +218 -0
  139. package/src/ctx.ts +201 -0
  140. package/src/err.ts +172 -0
  141. package/src/hints.ts +129 -0
  142. package/src/lang.ts +782 -0
  143. package/src/site.ts +34 -0
  144. package/src/tsconfig.json +16 -0
  145. package/src/type.ts +72 -0
  146. package/src/unify.ts +233 -0
  147. package/src/utility.ts +185 -0
  148. package/src/val/BagVal.ts +33 -0
  149. package/src/val/BooleanVal.ts +32 -0
  150. package/src/val/CloseFuncVal.ts +61 -0
  151. package/src/val/ConjunctVal.ts +285 -0
  152. package/src/val/CopyFuncVal.ts +82 -0
  153. package/src/val/DisjunctVal.ts +288 -0
  154. package/src/val/FeatureVal.ts +56 -0
  155. package/src/val/FuncBaseVal.ts +204 -0
  156. package/src/val/HideFuncVal.ts +85 -0
  157. package/src/val/IntegerVal.ts +75 -0
  158. package/src/val/JunctionVal.ts +54 -0
  159. package/src/val/KeyFuncVal.ts +103 -0
  160. package/src/val/ListVal.ts +286 -0
  161. package/src/val/LowerFuncVal.ts +73 -0
  162. package/src/val/MapVal.ts +349 -0
  163. package/src/val/MoveFuncVal.ts +83 -0
  164. package/src/val/NilVal.ts +156 -0
  165. package/src/val/NullVal.ts +38 -0
  166. package/src/val/NumberVal.ts +75 -0
  167. package/src/val/OpBaseVal.ts +231 -0
  168. package/src/val/OpenFuncVal.ts +60 -0
  169. package/src/val/PathFuncVal.ts +75 -0
  170. package/src/val/PlusOpVal.ts +92 -0
  171. package/src/val/PrefFuncVal.ts +73 -0
  172. package/src/val/PrefVal.ts +170 -0
  173. package/src/val/RefVal.ts +436 -0
  174. package/src/val/ScalarKindVal.ts +125 -0
  175. package/src/val/ScalarVal.ts +107 -0
  176. package/src/val/StringVal.ts +37 -0
  177. package/src/val/SuperFuncVal.ts +46 -0
  178. package/src/val/TopVal.ts +69 -0
  179. package/src/val/TypeFuncVal.ts +69 -0
  180. package/src/val/UpperFuncVal.ts +72 -0
  181. package/src/val/Val.ts +318 -0
  182. package/src/val/VarVal.ts +171 -0
  183. package/src/val/top.ts +12 -0
  184. package/src/val/valutil.ts +31 -0
package/src/site.ts ADDED
@@ -0,0 +1,34 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+
4
+ import type {
5
+ Val,
6
+ } from './type'
7
+
8
+
9
+ class Site {
10
+ row: number = -1
11
+ col: number = -1
12
+ url: string = ''
13
+
14
+ constructor(val?: Val | { row?: number, col?: number, url?: string }) {
15
+ if (val) {
16
+ if ('site' in val) {
17
+ // Val with site property
18
+ this.row = val.site?.row ?? -1
19
+ this.col = val.site?.col ?? -1
20
+ this.url = val.site?.url ?? ''
21
+ } else {
22
+ // Plain object with row, col, url
23
+ this.row = val.row ?? -1
24
+ this.col = val.col ?? -1
25
+ this.url = val.url ?? ''
26
+ }
27
+ }
28
+ }
29
+ }
30
+
31
+
32
+ export {
33
+ Site,
34
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "esModuleInterop": true,
4
+ "module": "nodenext",
5
+ "noEmitOnError": true,
6
+ "outDir":"../dist",
7
+ "rootDir":".",
8
+ "resolveJsonModule": true,
9
+ "sourceMap": true,
10
+ "strict": true,
11
+ "target": "es2021",
12
+ "declaration": true,
13
+ "declarationDir": "../dist"
14
+ }
15
+ }
16
+
package/src/type.ts ADDED
@@ -0,0 +1,72 @@
1
+ /* Copyright (c) 2022-2025 Richard Rodger, MIT License */
2
+
3
+ import * as Fs from 'node:fs'
4
+
5
+ // TODO: refactor these out
6
+
7
+ import { Resolver } from '@jsonic/multisource'
8
+
9
+ import { AontuContext } from './ctx'
10
+ import { Val, DONE, SPREAD } from './val/Val'
11
+ import type { ValMark, ValSpec } from './val/Val'
12
+
13
+
14
+ type FST = typeof Fs
15
+
16
+
17
+
18
+ type AontuOptions = {
19
+ src?: string // Source text.
20
+ print?: number // Print debug verbosity
21
+ resolver?: Resolver // Source resolver
22
+ base?: string // Base path for resolver
23
+ path?: string // Path of entry file
24
+ debug?: boolean
25
+ trace?: boolean
26
+ fs?: FST
27
+ deps?: any
28
+ log?: any
29
+ idcount?: number
30
+ collect?: boolean // Collect errors into an errs property, rather than throw them.
31
+ err?: any[]
32
+ explain?: any[]
33
+ }
34
+
35
+
36
+ const DEFAULT_OPTS: () => AontuOptions = () => {
37
+ return {
38
+ print: -1,
39
+ debug: false,
40
+ trace: false,
41
+ }
42
+ }
43
+
44
+
45
+ type ValMap = { [key: string]: Val }
46
+ type ValList = Val[]
47
+
48
+
49
+
50
+ type ErrContext = {
51
+ src?: string,
52
+ fs?: FST
53
+ }
54
+
55
+ export type {
56
+ Val,
57
+ ValMark,
58
+ ValSpec,
59
+ ValMap,
60
+ ValList,
61
+ AontuOptions,
62
+ ErrContext,
63
+ FST,
64
+ }
65
+
66
+ export {
67
+ DONE,
68
+ SPREAD,
69
+ DEFAULT_OPTS,
70
+ Resolver,
71
+ }
72
+
package/src/unify.ts ADDED
@@ -0,0 +1,233 @@
1
+ /* Copyright (c) 2021-2023 Richard Rodger, MIT License */
2
+
3
+
4
+ import type { Val } from './type'
5
+
6
+ import { AontuContext } from './ctx'
7
+
8
+ import { DONE } from './type'
9
+
10
+ import { makeNilErr } from './err'
11
+
12
+ import { NilVal } from './val/NilVal'
13
+
14
+ import {
15
+ Lang
16
+ } from './lang'
17
+
18
+
19
+ import {
20
+ explainOpen, ec, explainClose,
21
+ } from './utility'
22
+
23
+
24
+ import {
25
+ top
26
+ } from './val/top'
27
+
28
+
29
+ // TODO: relation to unify loops?
30
+ const MAXCYCLE = 9
31
+
32
+ let uc = 0
33
+
34
+ // Vals should only have to unify downwards (in .unify) over Vals they understand.
35
+ // and for complex Vals, TOP, which means self unify if not yet done
36
+ const unite = (ctx: AontuContext, a: any, b: any, whence: string) => {
37
+ const te = ctx.explain && explainOpen(ctx, ctx.explain, 'unite', a, b)
38
+
39
+ let out = a
40
+ let why = 'u'
41
+
42
+ const saw =
43
+ (a ? a.id + (a.done ? '' : '*') : '') + '~' + (b ? b.id + (b.done ? '' : '*') : '') +
44
+ '@' + ctx.pathstr
45
+
46
+
47
+ /*
48
+ if (1 < ctx.seen[saw]) {
49
+ console.log('UNITE-SAW', ctx.cc, saw, ctx.seen[saw], 1 < ctx.seen[saw] ? (a?.canon + ' ~ ' + b?.canon) : '')
50
+ // console.trace()
51
+ // process.exit()
52
+ }
53
+ */
54
+
55
+ // NOTE: if this error occurs "unreasonably", attemp to avoid unnecesary unification
56
+ // See for example PrefVal peg.id equality inspection.
57
+ if (MAXCYCLE < ctx.seen[saw]) {
58
+ // console.log('SAW', ctx.seen[saw], saw, a?.id, a?.canon, b?.id, b?.canon, ctx.cc)
59
+ out = makeNilErr(ctx, 'unify_cycle', a, b)
60
+ }
61
+ else {
62
+ ctx.seen[saw] = 1 + (ctx.seen[saw] ?? 0)
63
+
64
+ try {
65
+
66
+ let unified = false
67
+
68
+ if (b && (!a || a.isTop)) {
69
+ out = b
70
+ why = 'b'
71
+ }
72
+
73
+ else if (a && (!b || b.isTop)) {
74
+ out = a
75
+ why = 'a'
76
+ }
77
+
78
+ else if (a && b && !b.isTop) {
79
+ if (a.isNil) {
80
+ out = update(a, b)
81
+ why = 'an'
82
+ }
83
+ else if (b.isNil) {
84
+ out = update(b, a)
85
+ why = 'bn'
86
+ }
87
+ else if (a.isConjunct) {
88
+ out = a.unify(b, ctx.clone({ explain: ec(te, 'CJ') }))
89
+ unified = true
90
+ why = 'acj'
91
+ }
92
+ else if (
93
+ b.isConjunct
94
+ || b.isDisjunct
95
+ || b.isRef
96
+ || b.isPref
97
+ || b.isFunc
98
+ ) {
99
+
100
+ out = b.unify(a, ctx.clone({ explain: ec(te, 'BW') }))
101
+ unified = true
102
+ why = 'bv'
103
+ }
104
+
105
+ // Exactly equal scalars.
106
+ else if (a.constructor === b.constructor && a.peg === b.peg) {
107
+ out = update(a, b)
108
+ why = 'up'
109
+ }
110
+
111
+ else {
112
+ out = a.unify(b, ctx.clone({ explain: ec(te, 'GN') }))
113
+ unified = true
114
+ why = 'ab'
115
+ }
116
+ }
117
+
118
+ if (!out || !out.unify) {
119
+ out = makeNilErr(ctx, 'unite', a, b, whence + '/nil')
120
+ why += 'N'
121
+ }
122
+
123
+ // console.log('UNITE-DONE', out.id, out.canon, out.done)
124
+
125
+ // if (DONE !== out.dc && !unified) {
126
+ if (!out.done && !unified) {
127
+ let nout = out.unify(top(), ctx.clone({ explain: ec(te, 'ND') }))
128
+ out = nout
129
+ why += 'T'
130
+ }
131
+
132
+ // console.log('UNITE', why, a?.id, a?.canon, a?.done, b?.id, b?.canon, b?.done, '->', out?.id, out?.canon, out?.done)
133
+
134
+ uc++
135
+ }
136
+ catch (err: any) {
137
+ // console.log(err)
138
+ // TODO: handle unexpected
139
+ out = makeNilErr(ctx, 'internal', a, b)
140
+ }
141
+ }
142
+
143
+ ctx.explain && explainClose(te, out)
144
+
145
+ return out
146
+ }
147
+
148
+
149
+ function update(x: Val, _y: Val) {
150
+ // TODO: update x with y.site
151
+ return x
152
+ }
153
+
154
+
155
+ class Unify {
156
+ root: Val
157
+ res: Val
158
+ // err: Omit<NilVal[], "push">
159
+ err: any[]
160
+ explain: any[] | null
161
+ cc: number
162
+ lang: Lang
163
+
164
+ constructor(root: Val | string, lang?: Lang, ctx?: AontuContext | any, src?: any) {
165
+ this.lang = lang || new Lang()
166
+ if ('string' === typeof root) {
167
+ root = this.lang.parse(root)
168
+ }
169
+
170
+ if ('string' !== typeof src) {
171
+ src = ''
172
+ }
173
+
174
+ this.cc = 0
175
+ this.root = root
176
+ this.res = root
177
+ this.err = ctx?.err ?? root.err ?? []
178
+ this.explain = ctx?.explain ?? root.explain ?? null
179
+
180
+ let res = root
181
+ let uctx: AontuContext
182
+
183
+ // Only unify if no syntax errors
184
+ if (!(root as NilVal).isNil) {
185
+ if (ctx instanceof AontuContext) {
186
+ uctx = ctx
187
+ }
188
+ else {
189
+ uctx = new AontuContext({
190
+ ...(ctx || {}),
191
+ root: res,
192
+ err: this.err,
193
+ explain: this.explain,
194
+ src,
195
+ })
196
+ }
197
+
198
+ // TODO: messy
199
+ // uctx.seterr(this.err)
200
+ uctx.err = this.err
201
+ uctx.explain = this.explain
202
+
203
+ const explain = null == ctx?.explain ? undefined : ctx?.explain
204
+ const te = explain && explainOpen(uctx, explain, 'root', res)
205
+
206
+ // NOTE: if true === res.done already, then this loop never needs to run.
207
+ let maxcc = 9 // 99
208
+ for (; this.cc < maxcc && DONE !== res.dc; this.cc++) {
209
+ // console.log('CC', this.cc, res.canon)
210
+ uctx.cc = this.cc
211
+ res = unite(uctx.clone({ explain: ec(te, 'run') }), res, top(), 'unify')
212
+
213
+ if (0 < uctx.err.length) {
214
+ break
215
+ }
216
+
217
+ uctx = uctx.clone({ root: res })
218
+ }
219
+
220
+ uctx.explain && explainClose(te, res)
221
+ }
222
+
223
+ this.res = res
224
+ }
225
+ }
226
+
227
+
228
+
229
+
230
+ export {
231
+ Unify,
232
+ unite,
233
+ }
package/src/utility.ts ADDED
@@ -0,0 +1,185 @@
1
+ /* Copyright (c) 2023-2025 Richard Rodger, MIT License */
2
+
3
+
4
+
5
+ import type { Val } from './type'
6
+
7
+
8
+ // Mark value in source is propagated to target (true ratchets).
9
+ function propagateMarks(source: Val, target: Val): void {
10
+ for (let name in source.mark) {
11
+ (target.mark as any)[name] = (target.mark as any)[name] || (source.mark as any)[name]
12
+ }
13
+ }
14
+
15
+
16
+ function formatPath(path: Val | string[], absolute?: boolean) {
17
+ let parts: string[]
18
+ if (Array.isArray(path)) {
19
+ parts = path
20
+ }
21
+ else {
22
+ parts = path.path
23
+ }
24
+
25
+ let pathstr = (0 < parts.length && false !== absolute ? '$.' : '') + parts.join('.')
26
+
27
+ return pathstr
28
+ }
29
+
30
+
31
+ type WalkApply = (
32
+ key: string | number | undefined,
33
+ val: Val,
34
+ parent: Val | undefined,
35
+ path: (string | number)[]
36
+ ) => Val
37
+
38
+ /**
39
+ * Walk a Val structure depth first, applying functions before and after descending.
40
+ * Only traverses Val instances - stops at non-Val children.
41
+ */
42
+ function walk(
43
+ // These arguments are the public interface.
44
+ val: Val,
45
+
46
+ // Before descending into a node.
47
+ before?: WalkApply,
48
+
49
+ // After descending into a node.
50
+ after?: WalkApply,
51
+
52
+ // Maximum recursive depth, default: 32. Use null for infinite depth.
53
+ maxdepth?: number | null,
54
+
55
+ // These arguments are used for recursive state.
56
+ key?: string | number,
57
+ parent?: Val,
58
+ path?: (string | number)[]
59
+ ): Val {
60
+ let out = null == before ? val : before(key, val, parent, path || [])
61
+
62
+ maxdepth = null != maxdepth && 0 <= maxdepth ? maxdepth : 32
63
+ if (null != maxdepth && 0 === maxdepth) {
64
+ return out
65
+ }
66
+ if (null != path && null != maxdepth && 0 < maxdepth && maxdepth <= path.length) {
67
+ return out
68
+ }
69
+
70
+ const child: any = out.peg
71
+
72
+ // Container Vals (Map etc) have peg = plain {} or []
73
+ if (null != child && !child.isVal) {
74
+ if ('object' === typeof child) {
75
+ for (let ckey in child) {
76
+ if (child[ckey] && child[ckey].isVal) {
77
+ child[ckey] = walk(
78
+ child[ckey], before, after, maxdepth, ckey, out, [...(path || []), ckey])
79
+ }
80
+ }
81
+ }
82
+ else if (Array.isArray(child)) {
83
+ for (let i = 0; i < child.length; i++) {
84
+ if (child[i] && child[i].isVal) {
85
+ child[i] = walk(
86
+ child[i], before, after, maxdepth, i, out, [...(path || []), '' + i])
87
+ }
88
+ }
89
+ }
90
+ }
91
+
92
+ out = null == after ? out : after(key, out, parent, path || [])
93
+
94
+ return out
95
+ }
96
+
97
+
98
+ const T_NOTE = 0
99
+ const T_WHY = 1
100
+ const T_PATH = 2
101
+ const T_AVAL = 3
102
+ const T_BVAL = 4
103
+ const T_OVAL = 5
104
+ const T_CHILDREN = 6
105
+
106
+
107
+ function explainOpen(
108
+ ctx: any,
109
+ t: any[] | undefined | null | false,
110
+ note: string,
111
+ ac?: Val,
112
+ bc?: Val
113
+ ): any[] | null {
114
+ if (false === t) return null;
115
+
116
+ t = t ?? [null, 'root', null, null, null, null]
117
+ t[T_WHY] = t[T_WHY] ?? ''
118
+ t[T_NOTE] = (0 <= ctx.cc ? ctx.cc + '~' : '') + note
119
+ t[T_PATH] = ['$', ctx.path.join('.')].filter(p => '' != p).join('.') + ' '
120
+ if (ac) {
121
+ t[T_AVAL] = ac.id + (ac.done ? '' : '!') + '=' + ac.canon
122
+ }
123
+ if (bc) {
124
+ t[T_BVAL] = bc.id + (bc.done ? '' : '!') + '=' + bc.canon
125
+ }
126
+
127
+ return t
128
+ }
129
+
130
+
131
+ function ec(t: any[] | undefined | null, why: string) {
132
+ if (null == t) return;
133
+
134
+ const child = [null, why, null, null, null, null]
135
+ t[T_CHILDREN] = t[T_CHILDREN] ?? []
136
+ t[T_CHILDREN].push(child)
137
+ return child
138
+ }
139
+
140
+
141
+ function explainClose(t: any[] | undefined | null, out?: Val) {
142
+ if (null == t) return;
143
+
144
+ if (out) {
145
+ t[T_OVAL] = '-> ' + out.id + (out.done ? '' : '!') + '=' + out.canon
146
+ }
147
+ }
148
+
149
+
150
+ function formatExplain(t: any[], d?: number) {
151
+ d = null == d ? 0 : d
152
+ const indent = (' '.repeat(d))
153
+
154
+ if (Array.isArray(t)) {
155
+ const b = [
156
+ indent + t.slice(0, t.length - 1).join(' ')
157
+ ]
158
+
159
+ const children = t[t.length - 1]
160
+ if (Array.isArray(children)) {
161
+ for (let ce of children) {
162
+ b.push(formatExplain(ce, d + 1))
163
+ }
164
+ }
165
+
166
+ return b.join('\n')
167
+ }
168
+ else {
169
+ return indent + t
170
+ }
171
+ }
172
+
173
+
174
+
175
+ export {
176
+ propagateMarks,
177
+ formatPath,
178
+ walk,
179
+ WalkApply,
180
+ explainOpen,
181
+ ec,
182
+ explainClose,
183
+ formatExplain,
184
+ }
185
+
@@ -0,0 +1,33 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+
4
+ import type {
5
+ ValSpec,
6
+ } from '../type'
7
+
8
+ import {
9
+ AontuContext,
10
+ } from '../ctx'
11
+
12
+
13
+ import { FeatureVal } from './FeatureVal'
14
+
15
+
16
+ abstract class BagVal extends FeatureVal {
17
+ isBag = true
18
+
19
+ closed: boolean = false
20
+ optionalKeys: string[] = []
21
+
22
+ constructor(
23
+ spec: ValSpec,
24
+ ctx?: AontuContext
25
+ ) {
26
+ super(spec, ctx)
27
+ }
28
+ }
29
+
30
+
31
+ export {
32
+ BagVal,
33
+ }
@@ -0,0 +1,32 @@
1
+ /* Copyright (c) 2021-2023 Richard Rodger, MIT License */
2
+
3
+ import type {
4
+ Val,
5
+ ValSpec,
6
+ } from '../type'
7
+
8
+ import {
9
+ AontuContext,
10
+ } from '../ctx'
11
+
12
+ import { ScalarVal } from './ScalarVal'
13
+
14
+ class BooleanVal extends ScalarVal {
15
+ isBoolean = true
16
+
17
+ constructor(
18
+ spec: ValSpec,
19
+ ctx?: AontuContext
20
+ ) {
21
+ super({ peg: spec.peg, kind: Boolean }, ctx)
22
+ }
23
+
24
+ unify(peer: Val, ctx: AontuContext): Val {
25
+ return super.unify(peer, ctx)
26
+ }
27
+ }
28
+
29
+
30
+ export {
31
+ BooleanVal,
32
+ }
@@ -0,0 +1,61 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+
4
+ import type {
5
+ Val,
6
+ ValSpec,
7
+ } from '../type'
8
+
9
+ import {
10
+ AontuContext,
11
+ } from '../ctx'
12
+
13
+ import { makeNilErr } from '../err'
14
+
15
+
16
+ import { FuncBaseVal } from './FuncBaseVal'
17
+ import { BagVal } from '../val/BagVal'
18
+
19
+
20
+ class CloseFuncVal extends FuncBaseVal {
21
+ isCloseFunc = true
22
+
23
+ constructor(
24
+ spec: ValSpec,
25
+ ctx?: AontuContext
26
+ ) {
27
+ super(spec, ctx)
28
+ this.validateArgs(spec.peg, 1)
29
+ }
30
+
31
+
32
+ make(_ctx: AontuContext, spec: ValSpec): Val {
33
+ return new CloseFuncVal(spec)
34
+ }
35
+
36
+ funcname() {
37
+ return 'close'
38
+ }
39
+
40
+
41
+ resolve(ctx: AontuContext, args: Val[]) {
42
+ const argval: any = args[0]
43
+
44
+ if (null == argval) {
45
+ return makeNilErr(ctx, 'no_first_arg', this, undefined, 'close')
46
+ }
47
+
48
+ if (argval.isMap || argval.isList) {
49
+ (argval as BagVal).closed = true
50
+ // console.log('CLOSED', argval.canon)
51
+ }
52
+
53
+ return argval
54
+ }
55
+
56
+ }
57
+
58
+
59
+ export {
60
+ CloseFuncVal,
61
+ }