aontu 0.29.0 → 0.30.2

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 (270) hide show
  1. package/dist/aontu.d.ts +17 -13
  2. package/dist/aontu.js +145 -60
  3. package/dist/aontu.js.map +1 -1
  4. package/dist/ctx.d.ts +51 -0
  5. package/dist/ctx.js +105 -0
  6. package/dist/ctx.js.map +1 -0
  7. package/dist/err.d.ts +12 -0
  8. package/dist/err.js +123 -0
  9. package/dist/err.js.map +1 -0
  10. package/dist/hints.d.ts +8 -0
  11. package/dist/hints.js +84 -0
  12. package/dist/hints.js.map +1 -0
  13. package/dist/lang.d.ts +11 -0
  14. package/dist/lang.js +569 -0
  15. package/dist/lang.js.map +1 -0
  16. package/dist/site.d.ts +12 -0
  17. package/dist/site.js +27 -0
  18. package/dist/site.js.map +1 -0
  19. package/dist/tsconfig.tsbuildinfo +1 -0
  20. package/dist/type.d.ts +32 -0
  21. package/dist/type.js +16 -0
  22. package/dist/type.js.map +1 -0
  23. package/dist/unify.d.ts +14 -0
  24. package/dist/unify.js +156 -0
  25. package/dist/unify.js.map +1 -0
  26. package/dist/utility.d.ts +14 -0
  27. package/dist/utility.js +122 -0
  28. package/dist/utility.js.map +1 -0
  29. package/dist/val/BagVal.d.ts +10 -0
  30. package/dist/val/BagVal.js +15 -0
  31. package/dist/val/BagVal.js.map +1 -0
  32. package/dist/val/BooleanVal.d.ts +9 -0
  33. package/dist/val/BooleanVal.js +16 -0
  34. package/dist/val/BooleanVal.js.map +1 -0
  35. package/dist/val/CloseFuncVal.d.ts +11 -0
  36. package/dist/val/CloseFuncVal.js +32 -0
  37. package/dist/val/CloseFuncVal.js.map +1 -0
  38. package/dist/val/ConjunctVal.d.ts +14 -0
  39. package/dist/val/ConjunctVal.js +187 -0
  40. package/dist/val/ConjunctVal.js.map +1 -0
  41. package/dist/val/CopyFuncVal.d.ts +13 -0
  42. package/dist/val/CopyFuncVal.js +35 -0
  43. package/dist/val/CopyFuncVal.js.map +1 -0
  44. package/dist/val/DisjunctVal.d.ts +19 -0
  45. package/dist/val/DisjunctVal.js +194 -0
  46. package/dist/val/DisjunctVal.js.map +1 -0
  47. package/dist/val/FeatureVal.d.ts +10 -0
  48. package/dist/val/FeatureVal.js +29 -0
  49. package/dist/val/FeatureVal.js.map +1 -0
  50. package/dist/val/FuncBaseVal.d.ts +15 -0
  51. package/dist/val/FuncBaseVal.js +127 -0
  52. package/dist/val/FuncBaseVal.js.map +1 -0
  53. package/dist/val/HideFuncVal.d.ts +12 -0
  54. package/dist/val/HideFuncVal.js +48 -0
  55. package/dist/val/HideFuncVal.js.map +1 -0
  56. package/dist/val/IntegerVal.d.ts +9 -0
  57. package/dist/val/IntegerVal.js +46 -0
  58. package/dist/val/IntegerVal.js.map +1 -0
  59. package/dist/val/JunctionVal.d.ts +12 -0
  60. package/dist/val/JunctionVal.js +31 -0
  61. package/dist/val/JunctionVal.js.map +1 -0
  62. package/dist/val/KeyFuncVal.d.ts +12 -0
  63. package/dist/val/KeyFuncVal.js +34 -0
  64. package/dist/val/KeyFuncVal.js.map +1 -0
  65. package/dist/val/ListVal.d.ts +17 -0
  66. package/dist/val/ListVal.js +186 -0
  67. package/dist/val/ListVal.js.map +1 -0
  68. package/dist/val/LowerFuncVal.d.ts +12 -0
  69. package/dist/val/LowerFuncVal.js +40 -0
  70. package/dist/val/LowerFuncVal.js.map +1 -0
  71. package/dist/val/MapVal.d.ts +16 -0
  72. package/dist/val/MapVal.js +211 -0
  73. package/dist/val/MapVal.js.map +1 -0
  74. package/dist/val/MoveFuncVal.d.ts +11 -0
  75. package/dist/val/MoveFuncVal.js +41 -0
  76. package/dist/val/MoveFuncVal.js.map +1 -0
  77. package/dist/val/NilVal.d.ts +25 -0
  78. package/dist/{lib/val/Nil.js → val/NilVal.js} +38 -30
  79. package/dist/val/NilVal.js.map +1 -0
  80. package/dist/val/NullVal.d.ts +9 -0
  81. package/dist/val/NullVal.js +18 -0
  82. package/dist/val/NullVal.js.map +1 -0
  83. package/dist/val/NumberVal.d.ts +9 -0
  84. package/dist/val/NumberVal.js +44 -0
  85. package/dist/val/NumberVal.js.map +1 -0
  86. package/dist/val/OpBaseVal.d.ts +18 -0
  87. package/dist/val/OpBaseVal.js +152 -0
  88. package/dist/val/OpBaseVal.js.map +1 -0
  89. package/dist/val/OpenFuncVal.d.ts +11 -0
  90. package/dist/val/OpenFuncVal.js +30 -0
  91. package/dist/val/OpenFuncVal.js.map +1 -0
  92. package/dist/val/PathFuncVal.d.ts +13 -0
  93. package/dist/val/PathFuncVal.js +42 -0
  94. package/dist/val/PathFuncVal.js.map +1 -0
  95. package/dist/val/PlusOpVal.d.ts +16 -0
  96. package/dist/val/PlusOpVal.js +59 -0
  97. package/dist/val/PlusOpVal.js.map +1 -0
  98. package/dist/val/PrefFuncVal.d.ts +11 -0
  99. package/dist/val/PrefFuncVal.js +39 -0
  100. package/dist/val/PrefFuncVal.js.map +1 -0
  101. package/dist/val/PrefVal.d.ts +15 -0
  102. package/dist/val/PrefVal.js +104 -0
  103. package/dist/val/PrefVal.js.map +1 -0
  104. package/dist/val/RefVal.d.ts +22 -0
  105. package/dist/val/RefVal.js +276 -0
  106. package/dist/val/RefVal.js.map +1 -0
  107. package/dist/val/ScalarKindVal.d.ts +16 -0
  108. package/dist/val/ScalarKindVal.js +76 -0
  109. package/dist/val/ScalarKindVal.js.map +1 -0
  110. package/dist/val/ScalarVal.d.ts +16 -0
  111. package/dist/val/ScalarVal.js +61 -0
  112. package/dist/val/ScalarVal.js.map +1 -0
  113. package/dist/val/StringVal.d.ts +10 -0
  114. package/dist/val/StringVal.js +19 -0
  115. package/dist/val/StringVal.js.map +1 -0
  116. package/dist/val/SuperFuncVal.d.ts +11 -0
  117. package/dist/val/SuperFuncVal.js +22 -0
  118. package/dist/val/SuperFuncVal.js.map +1 -0
  119. package/dist/val/TopVal.d.ts +16 -0
  120. package/dist/val/TopVal.js +38 -0
  121. package/dist/val/TopVal.js.map +1 -0
  122. package/dist/val/TypeFuncVal.d.ts +12 -0
  123. package/dist/val/TypeFuncVal.js +35 -0
  124. package/dist/val/TypeFuncVal.js.map +1 -0
  125. package/dist/val/UpperFuncVal.d.ts +12 -0
  126. package/dist/val/UpperFuncVal.js +40 -0
  127. package/dist/val/UpperFuncVal.js.map +1 -0
  128. package/dist/val/Val.d.ts +89 -0
  129. package/dist/val/Val.js +186 -0
  130. package/dist/val/Val.js.map +1 -0
  131. package/dist/val/VarVal.d.ts +13 -0
  132. package/dist/val/VarVal.js +109 -0
  133. package/dist/val/VarVal.js.map +1 -0
  134. package/dist/val/top.d.ts +2 -0
  135. package/dist/val/top.js +10 -0
  136. package/dist/val/top.js.map +1 -0
  137. package/dist/val/valutil.d.ts +2 -0
  138. package/dist/val/valutil.js +30 -0
  139. package/dist/val/valutil.js.map +1 -0
  140. package/package.json +20 -38
  141. package/src/aontu.ts +218 -0
  142. package/src/ctx.ts +201 -0
  143. package/src/err.ts +172 -0
  144. package/src/hints.ts +119 -0
  145. package/src/lang.ts +782 -0
  146. package/src/site.ts +34 -0
  147. package/src/tsconfig.json +16 -0
  148. package/src/type.ts +72 -0
  149. package/src/unify.ts +225 -0
  150. package/src/utility.ts +179 -0
  151. package/src/val/BagVal.ts +33 -0
  152. package/src/val/BooleanVal.ts +32 -0
  153. package/src/val/CloseFuncVal.ts +61 -0
  154. package/src/val/ConjunctVal.ts +285 -0
  155. package/src/val/CopyFuncVal.ts +69 -0
  156. package/src/val/DisjunctVal.ts +288 -0
  157. package/src/val/FeatureVal.ts +56 -0
  158. package/src/val/FuncBaseVal.ts +198 -0
  159. package/src/val/HideFuncVal.ts +85 -0
  160. package/src/val/IntegerVal.ts +75 -0
  161. package/src/val/JunctionVal.ts +54 -0
  162. package/src/val/KeyFuncVal.ts +67 -0
  163. package/src/val/ListVal.ts +286 -0
  164. package/src/val/LowerFuncVal.ts +73 -0
  165. package/src/val/MapVal.ts +316 -0
  166. package/src/val/MoveFuncVal.ts +79 -0
  167. package/{lib/val/Nil.ts → src/val/NilVal.ts} +49 -37
  168. package/src/val/NullVal.ts +38 -0
  169. package/src/val/NumberVal.ts +75 -0
  170. package/src/val/OpBaseVal.ts +231 -0
  171. package/src/val/OpenFuncVal.ts +60 -0
  172. package/src/val/PathFuncVal.ts +75 -0
  173. package/src/val/PlusOpVal.ts +92 -0
  174. package/src/val/PrefFuncVal.ts +73 -0
  175. package/src/val/PrefVal.ts +159 -0
  176. package/src/val/RefVal.ts +384 -0
  177. package/src/val/ScalarKindVal.ts +125 -0
  178. package/src/val/ScalarVal.ts +105 -0
  179. package/src/val/StringVal.ts +37 -0
  180. package/src/val/SuperFuncVal.ts +46 -0
  181. package/src/val/TopVal.ts +69 -0
  182. package/src/val/TypeFuncVal.ts +69 -0
  183. package/src/val/UpperFuncVal.ts +72 -0
  184. package/src/val/Val.ts +294 -0
  185. package/src/val/VarVal.ts +171 -0
  186. package/src/val/top.ts +12 -0
  187. package/src/val/valutil.ts +31 -0
  188. package/aontu.ts +0 -92
  189. package/dist/lib/err.d.ts +0 -4
  190. package/dist/lib/err.js +0 -61
  191. package/dist/lib/err.js.map +0 -1
  192. package/dist/lib/lang.d.ts +0 -15
  193. package/dist/lib/lang.js +0 -409
  194. package/dist/lib/lang.js.map +0 -1
  195. package/dist/lib/op/disjunct.d.ts +0 -3
  196. package/dist/lib/op/disjunct.js +0 -28
  197. package/dist/lib/op/disjunct.js.map +0 -1
  198. package/dist/lib/op/op.d.ts +0 -6
  199. package/dist/lib/op/op.js +0 -9
  200. package/dist/lib/op/op.js.map +0 -1
  201. package/dist/lib/op/unite.d.ts +0 -3
  202. package/dist/lib/op/unite.js +0 -100
  203. package/dist/lib/op/unite.js.map +0 -1
  204. package/dist/lib/type.d.ts +0 -49
  205. package/dist/lib/type.js +0 -7
  206. package/dist/lib/type.js.map +0 -1
  207. package/dist/lib/unify.d.ts +0 -38
  208. package/dist/lib/unify.js +0 -67
  209. package/dist/lib/unify.js.map +0 -1
  210. package/dist/lib/utility.d.ts +0 -3
  211. package/dist/lib/utility.js +0 -16
  212. package/dist/lib/utility.js.map +0 -1
  213. package/dist/lib/val/ConjunctVal.d.ts +0 -17
  214. package/dist/lib/val/ConjunctVal.js +0 -171
  215. package/dist/lib/val/ConjunctVal.js.map +0 -1
  216. package/dist/lib/val/DisjunctVal.d.ts +0 -17
  217. package/dist/lib/val/DisjunctVal.js +0 -95
  218. package/dist/lib/val/DisjunctVal.js.map +0 -1
  219. package/dist/lib/val/ListVal.d.ts +0 -18
  220. package/dist/lib/val/ListVal.js +0 -120
  221. package/dist/lib/val/ListVal.js.map +0 -1
  222. package/dist/lib/val/MapVal.d.ts +0 -18
  223. package/dist/lib/val/MapVal.js +0 -117
  224. package/dist/lib/val/MapVal.js.map +0 -1
  225. package/dist/lib/val/Nil.d.ts +0 -22
  226. package/dist/lib/val/Nil.js.map +0 -1
  227. package/dist/lib/val/NullVal.d.ts +0 -15
  228. package/dist/lib/val/NullVal.js +0 -38
  229. package/dist/lib/val/NullVal.js.map +0 -1
  230. package/dist/lib/val/OpVal.d.ts +0 -17
  231. package/dist/lib/val/OpVal.js +0 -89
  232. package/dist/lib/val/OpVal.js.map +0 -1
  233. package/dist/lib/val/PlusVal.d.ts +0 -12
  234. package/dist/lib/val/PlusVal.js +0 -32
  235. package/dist/lib/val/PlusVal.js.map +0 -1
  236. package/dist/lib/val/PrefVal.d.ts +0 -17
  237. package/dist/lib/val/PrefVal.js +0 -87
  238. package/dist/lib/val/PrefVal.js.map +0 -1
  239. package/dist/lib/val/RefVal.d.ts +0 -22
  240. package/dist/lib/val/RefVal.js +0 -247
  241. package/dist/lib/val/RefVal.js.map +0 -1
  242. package/dist/lib/val/ValBase.d.ts +0 -26
  243. package/dist/lib/val/ValBase.js +0 -77
  244. package/dist/lib/val/ValBase.js.map +0 -1
  245. package/dist/lib/val/VarVal.d.ts +0 -15
  246. package/dist/lib/val/VarVal.js +0 -84
  247. package/dist/lib/val/VarVal.js.map +0 -1
  248. package/dist/lib/val.d.ts +0 -81
  249. package/dist/lib/val.js +0 -191
  250. package/dist/lib/val.js.map +0 -1
  251. package/lib/err.ts +0 -83
  252. package/lib/lang.ts +0 -585
  253. package/lib/op/disjunct.ts +0 -51
  254. package/lib/op/op.ts +0 -18
  255. package/lib/op/unite.ts +0 -143
  256. package/lib/type.ts +0 -95
  257. package/lib/unify.ts +0 -128
  258. package/lib/utility.ts +0 -24
  259. package/lib/val/ConjunctVal.ts +0 -265
  260. package/lib/val/DisjunctVal.ts +0 -160
  261. package/lib/val/ListVal.ts +0 -196
  262. package/lib/val/MapVal.ts +0 -181
  263. package/lib/val/NullVal.ts +0 -72
  264. package/lib/val/OpVal.ts +0 -159
  265. package/lib/val/PlusVal.ts +0 -76
  266. package/lib/val/PrefVal.ts +0 -154
  267. package/lib/val/RefVal.ts +0 -362
  268. package/lib/val/ValBase.ts +0 -109
  269. package/lib/val/VarVal.ts +0 -157
  270. package/lib/val.ts +0 -327
package/src/aontu.ts ADDED
@@ -0,0 +1,218 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+ import type { Val, AontuOptions } from './type'
4
+
5
+ import { Lang } from './lang'
6
+ import { Unify } from './unify'
7
+ import { AontuContext, AontuContextConfig } from './ctx'
8
+ import { MapVal } from './val/MapVal'
9
+ import { formatExplain } from './utility'
10
+ import { makeNilErr, AontuError } from './err'
11
+
12
+
13
+
14
+ class Aontu {
15
+ opts: AontuOptions
16
+ lang: Lang
17
+
18
+
19
+ constructor(popts?: AontuOptions) {
20
+ this.opts = popts ?? {}
21
+ this.lang = new Lang(this.opts)
22
+ }
23
+
24
+
25
+ // Create a new context.
26
+ ctx(cfg?: AontuContextConfig): AontuContext {
27
+ cfg = cfg ?? {}
28
+ const ac = new AontuContext(cfg)
29
+ return ac
30
+ }
31
+
32
+
33
+ // Parse source into a matching Val AST, not yet unified.
34
+ parse(src: string, opts?: AontuOptions, ac?: AontuContext): Val | undefined {
35
+ let out: Val | undefined
36
+ let errs: any[] = []
37
+
38
+ if (null == src) {
39
+ src = ''
40
+ }
41
+
42
+ ac = ac ?? this.ctx()
43
+ ac.addopts({ ...(opts ?? {}), src })
44
+
45
+ if ('string' !== typeof src) {
46
+ out = makeNilErr(ac, 'parse_bad_src')
47
+ errs.push(out)
48
+ }
49
+
50
+ if (0 === errs.length) {
51
+ out = runparse(src, this.lang, ac)
52
+ out.deps = ac.deps
53
+ ac.root = out
54
+ }
55
+
56
+ handleErrors(errs, out, ac)
57
+
58
+ return out
59
+ }
60
+
61
+
62
+ // Unify source or Val, returning a fully unified Val.
63
+ unify(src: string | Val, opts?: AontuOptions, ac?: AontuContext | any): Val {
64
+ let out: Val | undefined
65
+ let errs: any[] = []
66
+
67
+ ac = ac ?? this.ctx()
68
+ ac.addopts({ ...(opts ?? {}), src })
69
+
70
+ let pval: Val | undefined
71
+
72
+ if (null == src) {
73
+ src = ''
74
+ }
75
+
76
+ if ('string' === typeof src) {
77
+ pval = this.parse(src, undefined, ac)
78
+ }
79
+ else if (src && src.isVal) {
80
+ pval = src
81
+ }
82
+ else {
83
+ out = makeNilErr(ac, 'unify_no_src')
84
+ errs.push(out)
85
+ }
86
+
87
+ if (null != pval && 0 === errs.length) {
88
+ let uni = new Unify(pval, this.lang, ac, src)
89
+ errs = uni.err
90
+
91
+ out = uni.res
92
+
93
+ if (null == out) {
94
+ out = makeNilErr(ac, 'unify_no_res')
95
+ if (0 === errs.length) {
96
+ errs = [out]
97
+ }
98
+ }
99
+
100
+ out.deps = pval.deps
101
+ out.err = errs
102
+ ac.root = out
103
+ }
104
+
105
+ handleErrors(errs, out, ac)
106
+
107
+ return out as Val
108
+ }
109
+
110
+
111
+ // Generate output structure from source, which must parse and fully unify.
112
+ generate(src: string, opts?: any, ac?: AontuContext): any {
113
+ try {
114
+ let out = undefined
115
+
116
+ ac = ac ?? this.ctx()
117
+ ac.addopts({ ...(opts ?? {}), src })
118
+
119
+ let pval = this.parse(src, undefined, ac)
120
+
121
+ if (undefined !== pval && 0 === pval.err.length) {
122
+
123
+ let uval = this.unify(pval, undefined, ac)
124
+
125
+ if (undefined !== uval && 0 === uval.err.length) {
126
+
127
+ out = uval.isNil ? (ac.adderr(uval as any), undefined) : uval.gen(ac as any)
128
+
129
+ if (0 < ac.err.length) {
130
+ if (!ac.collect) {
131
+ throw new AontuError(ac.errmsg(), ac.err)
132
+ }
133
+ out = undefined
134
+ }
135
+ }
136
+ }
137
+
138
+ return out
139
+ }
140
+ catch (err: any) {
141
+ if (err instanceof AontuError || true === err.aontu) {
142
+ throw err
143
+ }
144
+ const unex = new AontuError('Aontu: unexpected error: ' + err.message)
145
+ Object.assign(unex, err)
146
+ unex.stack = err.stack
147
+ throw unex
148
+ }
149
+ }
150
+ }
151
+
152
+
153
+ // Either throw an exception or add collected errors to result.
154
+ function handleErrors(errs: any[], out: Val | undefined, ac: AontuContext) {
155
+
156
+ errs.map((err: any) => ac.adderr(err))
157
+
158
+ if (out) {
159
+ out.err.map((err: any) => ac.adderr(err))
160
+ }
161
+
162
+ if (0 < ac.err.length) {
163
+ if (ac.collect) {
164
+ if (out) {
165
+ out.err = ac.err
166
+ }
167
+ }
168
+ else {
169
+ throw new AontuError(ac.errmsg(), ac.err)
170
+ }
171
+ }
172
+ }
173
+
174
+
175
+ // Perform parse of source code (minor customizations over Lang.parse).
176
+ function runparse(src: string, lang: Lang, ctx: AontuContext): Val {
177
+ const popts = {
178
+ // src: ctx.src,
179
+ deps: ctx.deps,
180
+ fs: ctx.fs,
181
+ path: ctx.opts.path
182
+ }
183
+ let val
184
+
185
+ const tsrc = src.trim().replace(/^(\n\s*)+/, '')
186
+
187
+ if ('string' === typeof src && '' !== tsrc) {
188
+ val = lang.parse(src, popts)
189
+ }
190
+
191
+ if (undefined === val) {
192
+ val = new MapVal({ peg: {} })
193
+ }
194
+
195
+ return val
196
+ }
197
+
198
+
199
+ const util = {
200
+ runparse,
201
+ }
202
+
203
+
204
+ export {
205
+ Aontu,
206
+ AontuOptions,
207
+ AontuContext,
208
+ AontuError,
209
+
210
+ Val,
211
+ Lang,
212
+ runparse,
213
+ util,
214
+ formatExplain
215
+ }
216
+
217
+
218
+ export default Aontu
package/src/ctx.ts ADDED
@@ -0,0 +1,201 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+
4
+ import type { Val, FST, AontuOptions } from './type'
5
+
6
+ import { DEFAULT_OPTS } from './type'
7
+
8
+
9
+ import { MapVal } from './val/MapVal'
10
+ import { ListVal } from './val/ListVal'
11
+ import { NilVal } from './val/NilVal'
12
+
13
+
14
+ import {
15
+ descErr
16
+ } from './err'
17
+
18
+
19
+
20
+ type AontuContextConfig = {
21
+ cc?: number
22
+ err?: any[] // Omit<NilVal[], "push">
23
+ explain?: any[]
24
+ fs?: any
25
+ path?: string[]
26
+ root?: Val
27
+ seen?: Record<string, number>
28
+ seenI?: number
29
+ src?: string
30
+ srcpath?: string
31
+ vars?: Record<string, Val>
32
+ vc?: number
33
+ collect?: boolean
34
+ opts?: AontuOptions
35
+ deps?: Record<string, any>
36
+ }
37
+
38
+
39
+ class AontuContext {
40
+ root?: Val // Starting Val, root of paths.
41
+ path: string[] // Path to current Val.
42
+ vc: number // Val counter to create unique val ids.
43
+ cc: number = -1
44
+ vars: Record<string, Val> = {}
45
+ src?: string
46
+ fs?: FST
47
+
48
+ seenI: number
49
+ seen: Record<string, number>
50
+
51
+ collect: boolean
52
+
53
+ // errlist: Omit<NilVal[], "push"> // Nil error log of current unify.
54
+ err: any[]
55
+ explain: any[] | null
56
+
57
+ // TODO: separate options and context!!!
58
+ srcpath?: string
59
+
60
+ deps: Record<string, any>
61
+ opts: AontuOptions
62
+
63
+ _pathstr: string | undefined
64
+
65
+
66
+ constructor(cfg: AontuContextConfig) {
67
+ this.root = cfg.root
68
+ this.path = [...(cfg.path ?? [])]
69
+ this.src = cfg.src
70
+
71
+ this.collect = cfg.collect ?? null != cfg.err
72
+
73
+ this.err = cfg.err ?? []
74
+ this.explain = cfg.explain ?? null
75
+
76
+ this.fs = cfg.fs ?? null
77
+
78
+ // Multiple unify passes will keep incrementing Val counter.
79
+ this.vc = null == cfg.vc ? 1_000_000_000 : cfg.vc
80
+
81
+ this.cc = null == cfg.cc ? this.cc : cfg.cc
82
+
83
+ this.vars = cfg.vars ?? this.vars
84
+ this.seenI = cfg.seenI ?? 0
85
+ this.seen = cfg.seen ?? {}
86
+
87
+ this.srcpath = cfg.srcpath ?? undefined
88
+
89
+ this.deps = cfg.deps ?? {}
90
+
91
+ this.opts = DEFAULT_OPTS()
92
+ this.addopts(cfg.opts)
93
+ }
94
+
95
+
96
+ clone(cfg: {
97
+ root?: Val,
98
+ path?: string[],
99
+ err?: any[],
100
+ explain?: any[]
101
+ }): AontuContext {
102
+ const ctx = Object.create(this)
103
+ ctx.path = cfg.path ?? this.path
104
+ ctx.root = cfg.root ?? this.root
105
+ ctx.var = Object.create(this.vars)
106
+
107
+ ctx.err = cfg.err ?? ctx.err
108
+ ctx.explain = cfg.explain ?? ctx.explain
109
+
110
+ ctx._pathstr = undefined
111
+
112
+ return ctx
113
+ }
114
+
115
+ descend(key: string): AontuContext {
116
+ return this.clone({
117
+ root: this.root,
118
+ path: this.path.concat(key),
119
+ })
120
+ }
121
+
122
+
123
+ addopts(opts?: AontuOptions) {
124
+ if (null != opts) {
125
+ Object.assign(this.opts, opts)
126
+ }
127
+
128
+ this.collect = (this.opts.collect ?? null != this.opts.err) ?? this.collect
129
+ this.err = this.opts.err ?? this.err
130
+ this.fs = this.opts.fs ?? this.fs
131
+ this.explain = this.opts.explain ?? this.explain
132
+
133
+ this.src = ('string' === typeof this.opts.src ? this.opts.src : undefined) ?? this.src
134
+
135
+ // TODO: rename srcpath to file
136
+ this.srcpath = this.opts.path ?? this.srcpath
137
+ }
138
+
139
+
140
+ adderr(err: NilVal) {
141
+ if (null != err && err.isNil) {
142
+ if (null == err.primary) {
143
+ err.primary = err
144
+ }
145
+
146
+ if (!this.err.includes(err)) {
147
+ this.err.push(err)
148
+ }
149
+ if (null == err.msg || '' == err.msg) {
150
+ descErr(err, this)
151
+ }
152
+ }
153
+ }
154
+
155
+
156
+ errmsg() {
157
+ // return this.errlist
158
+ return this.err
159
+ .map((err: any) => err?.msg)
160
+ .filter(msg => null != msg)
161
+ .join('\n------\n')
162
+ }
163
+
164
+
165
+ find(path: string[]): Val | undefined {
166
+ let node: Val | undefined = this.root
167
+ let pI = 0
168
+ for (; pI < path.length; pI++) {
169
+ let part = path[pI]
170
+
171
+ if (node instanceof MapVal) {
172
+ node = node.peg[part]
173
+ }
174
+ else if (node instanceof ListVal) {
175
+ node = node.peg[part]
176
+ }
177
+ else {
178
+ break;
179
+ }
180
+ }
181
+
182
+ if (pI < path.length) {
183
+ node = undefined
184
+ }
185
+
186
+ return node
187
+ }
188
+
189
+
190
+ get pathstr() {
191
+ return this._pathstr ??
192
+ (this._pathstr = this.path.map(p => p.replaceAll('.', '\\.')).join('.'))
193
+ }
194
+ }
195
+
196
+
197
+
198
+ export {
199
+ AontuContext,
200
+ AontuContextConfig
201
+ }
package/src/err.ts ADDED
@@ -0,0 +1,172 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+
4
+ import { util } from 'jsonic'
5
+
6
+ import { Val, ErrContext } from './type'
7
+
8
+ import { AontuContext } from './ctx'
9
+
10
+ import { NilVal } from './val/NilVal'
11
+
12
+ import { hints } from './hints'
13
+
14
+
15
+ const { errmsg } = util
16
+
17
+
18
+ function getHint(why: any): string | undefined {
19
+ if (hints[why]) {
20
+ return hints[why]
21
+ }
22
+
23
+ return undefined
24
+ }
25
+
26
+
27
+ function makeNilErr(
28
+ ctx?: AontuContext,
29
+ why?: any,
30
+ av?: Val,
31
+ bv?: Val,
32
+ attempt?: string
33
+ ): NilVal {
34
+ const nilval = NilVal.make(ctx, why, av, bv, attempt)
35
+ return nilval
36
+ }
37
+
38
+
39
+ // TODO: move to utility?
40
+ function descErr<NILS extends NilVal | NilVal[]>(
41
+ err: NILS | any,
42
+ errctx?: ErrContext,
43
+ ): any {
44
+ if (err?.isNil) {
45
+ if (null == err.msg || '' === err.msg) {
46
+ let v1: any = err.primary
47
+ let v2: any = err.secondary
48
+
49
+ let v1src = resolveSrc(v1, errctx)
50
+ let v2src = resolveSrc(v2, errctx)
51
+
52
+ let path = ['$', ...err.path].filter((p: any) => null != p && '' != p)
53
+
54
+ let valpath = (0 < path.length ? path.join('.') : '')
55
+ let attempt = null != err.attempt ? err.attempt : (null == v2 ? 'resolve' : 'unify')
56
+
57
+ err.msg = [
58
+ errmsg({
59
+ color: { active: true },
60
+ name: 'aontu',
61
+ code: err.why,
62
+ txts: {
63
+ msg: 'Cannot ' +
64
+ attempt +
65
+ ' value' + (null == v2 ? '' : 's') +
66
+ ' at path ' + valpath,
67
+ hint: getHint(err.why)
68
+ }
69
+ }),
70
+
71
+ '\n',
72
+
73
+ (null != v1 && errmsg({
74
+ // TODO: color should come from jsonic config
75
+ color: { active: true, line: '\x1b[34m' },
76
+ txts: {
77
+ msg: 'Cannot ' + attempt + ' value: ' + v1.canon +
78
+ (null == v2 ? '' : ' with value: ' + v2.canon), // + ' #' + err.id,
79
+ site: ''
80
+ },
81
+ smsg: 'value was: ' + v1.canon,
82
+ file: resolveFile(v1.site.url),
83
+ src: v1src,
84
+ row: v1.site.row,
85
+ col: v1.site.col,
86
+ })),
87
+
88
+ (null != v2 && errmsg({
89
+ // TODO: color should come from jsonic config
90
+ color: { active: true, line: '\x1b[34m' },
91
+ txts: {
92
+ msg: 'Cannot ' + attempt + ' value: ' + v2.canon +
93
+ ' with value: ' + v1.canon, // + ' #' + err.id,
94
+ site: ''
95
+ },
96
+ smsg: 'value was: ' + v2.canon,
97
+ file: resolveFile(v2.site.url),
98
+ src: v2src,
99
+ row: v2.site.row,
100
+ col: v2.site.col,
101
+ })),
102
+
103
+
104
+ ]
105
+ .filter((n: any) => null != n && false !== n)
106
+ .join('\n')
107
+
108
+ // TODO: update jsonic errmsg to avoid multiple empty lines
109
+ .replace(/\n\n/g, '\n')
110
+
111
+ }
112
+ return err
113
+ }
114
+ else {
115
+ return err.map((n: any) => descErr(n, errctx))
116
+ }
117
+ }
118
+
119
+ function resolveFile(url: string | undefined) {
120
+ const cwd = process.cwd()
121
+ let out = url?.replace(cwd + '/', '') ?? '<no-file>'
122
+ out = out === cwd || '' === out ? '<no-file>' : out
123
+ return out
124
+ }
125
+
126
+
127
+ function resolveSrc(v: Val, errctx?: ErrContext) {
128
+ let src: string | undefined = undefined
129
+
130
+ if (null != v?.site.url) {
131
+ try {
132
+ const fileExists = errctx?.fs?.existsSync(v.site.url)
133
+ if (fileExists) {
134
+ src = errctx?.fs?.readFileSync(v.site.url, 'utf8') ?? undefined
135
+ }
136
+ }
137
+ catch (fe: any) {
138
+ // ignore as more important to report original error
139
+ }
140
+ }
141
+
142
+ if (errctx && (undefined == src || '' === src)) {
143
+ src = errctx.src ?? ''
144
+ }
145
+
146
+ return src
147
+ }
148
+
149
+
150
+ class AontuError extends Error {
151
+ aontu = true
152
+
153
+ constructor(msg: string, errs?: NilVal[]) {
154
+ super(msg)
155
+ this.name = this.constructor.name
156
+ this.errs = () => errs ?? []
157
+
158
+ this.stack = this.stack?.split('\n')
159
+ .filter(line => !line.match(/aontu\/(src|dist)\//))
160
+ .join('\n')
161
+ }
162
+
163
+ errs: () => NilVal[]
164
+ }
165
+
166
+
167
+ export {
168
+ getHint,
169
+ makeNilErr,
170
+ descErr,
171
+ AontuError,
172
+ }
package/src/hints.ts ADDED
@@ -0,0 +1,119 @@
1
+ /* Copyright (c) 2025 Richard Rodger, MIT License */
2
+
3
+ /**
4
+ * Error code hints for Aontu unification errors.
5
+ *
6
+ * Each key is an error code that can be passed to makeNilErr.
7
+ * Each value is a human-readable explanation of what the error means.
8
+ */
9
+
10
+ const hints: Record<string, string> = {
11
+
12
+ scalar_value:
13
+ 'Literal scalar values of the same kind can only unify if they are\n' +
14
+ 'exactly equal.' +
15
+ '\n \nExamples:\n' +
16
+ ' 1 & 1 -> 1 # Does unify (equal Integers);\n' +
17
+ ' a & a -> a # Does unify (equal Strings);\n' +
18
+ ' 1 & 2 -> nil # Does not unify (unequal Integers);\n' +
19
+ ' 1 & 1.0 -> nil # Does not unify (kinds: Integer & Number).',
20
+
21
+ scalar_kind:
22
+ 'Literal scalar values of different kinds cannot unify.' +
23
+ '\n \nExamples:\n' +
24
+ ' 1 & 1 -> 1 # Does unify (equal Integers);\n' +
25
+ ' 1 & a -> nil # Does not unify (Kinds: Integer & String);\n' +
26
+ ' 1 & 1.0 -> nil # Does not unify (kinds: Integer & Number).',
27
+
28
+ nil_gen:
29
+ 'The nil value was present after unification, and nil cannot be\n' +
30
+ 'generated because nil is not a literal value.',
31
+
32
+ no_gen:
33
+ 'This value was present after unification, and cannot be generated\n' +
34
+ 'because it is not a literal value.',
35
+
36
+
37
+ // TODO: extend errors to have details so we can name the key
38
+ mapval_required: 'This map value is required.',
39
+
40
+ mapval_no_gen:
41
+ 'This value was present after unification, and cannot be generated\n' +
42
+ 'because it is not a literal value.',
43
+
44
+ listval_required: 'This list element is required.',
45
+
46
+ listval_no_gen:
47
+ 'This list element was present after unification, and cannot be generated\n' +
48
+ 'because it is not a literal value.',
49
+
50
+ unknown_function:
51
+ 'This function name is not recognized.',
52
+
53
+ literal_nil:
54
+ 'A literal nil cannot unify with any other value.',
55
+
56
+
57
+ // Parsing errors
58
+ 'parse_bad_src': 'Invalid source provided for parsing. The source must be a non-empty string.',
59
+
60
+ // Unification errors
61
+ 'unify_no_src': 'No source provided for unification. Cannot unify without source values.',
62
+ 'unify_no_res': 'Unification produced no result. The values could not be unified.',
63
+ 'unite': 'Failed to unite two values. The values are incompatible and cannot be unified.',
64
+ 'cycle': 'Circular reference detected during unification. The unification process encountered a cycle.',
65
+ 'internal': 'Internal error during unification. This indicates an unexpected error in the unification process.',
66
+
67
+ // Type mismatch errors
68
+
69
+ 'scalar-type': 'Scalar type mismatch. The scalar types are incompatible.',
70
+ 'no-scalar-unify': 'Cannot unify scalar values. The scalar values have incompatible types.',
71
+ 'not-scalar-type': 'Expected a scalar type but got a non-scalar type.',
72
+ 'map': 'Type mismatch: expected a map value but got a different type.',
73
+ 'list': 'Type mismatch: expected a list value but got a different type.',
74
+
75
+ // Argument errors
76
+ 'arg': 'Missing required argument. A function requires an argument but none was provided.',
77
+ 'invalid-arg': 'Invalid argument provided. The argument does not match the expected type or format.',
78
+ 'no_first_arg': 'Missing first argument. The function requires a first argument but none was provided.',
79
+
80
+ // Variable errors
81
+ 'unknown_var': 'Unknown variable reference. The variable has not been defined.',
82
+ 'invalid_var_kind': 'Invalid variable kind. The variable type does not match the expected kind.',
83
+
84
+ // Path and reference errors
85
+ path_cycle: 'Path cycle detected. The path contains a circular reference.',
86
+ 'ref': 'Reference resolution failed. Unable to resolve the reference to a value.',
87
+
88
+ // Closure and state errors
89
+ 'closed': 'Cannot add to closed structure. The map or list is closed and does not accept new keys/elements.',
90
+ 'required_listelem': 'Required list element is missing. A non-optional list element has no value.',
91
+
92
+ // Junction errors (disjunction/conjection)
93
+ '|:empty': 'Empty disjunction. The disjunction has no valid alternatives.',
94
+ '|:empty-dist': 'Empty disjunction distribution. All alternatives in the disjunction are invalid.',
95
+
96
+ // Function errors (dynamic patterns)
97
+ 'func': 'Function operation failed. See the specific function name for details.',
98
+ 'make': 'Failed to create a new value. The make operation could not construct the value.',
99
+ 'resolve': 'Failed to resolve a value. The resolution process could not find or compute the value.',
100
+ 'operate': 'Operation failed. The operation could not be performed on the given values.',
101
+
102
+ // Operator errors (dynamic patterns)
103
+ 'op': 'Operator operation failed. See the specific operator name for details.',
104
+
105
+ // Close operation
106
+ 'close': 'Failed to close structure. The structure could not be closed.',
107
+
108
+ // Dynamic patterns (these serve as prefixes)
109
+ 'func:': 'Function error: ',
110
+ 'op:': 'Operator error: ',
111
+ 'var[': 'Variable type error: ',
112
+ 'ref[': 'Reference error: ',
113
+ 'op[': 'Operator value error: ',
114
+ }
115
+
116
+
117
+ export {
118
+ hints,
119
+ }