eliscript 0.0.1

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 (195) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +135 -0
  3. package/bin/eliscript +16 -0
  4. package/bin/eliscript-bootstrap +15 -0
  5. package/bin/eliscript-build +15 -0
  6. package/bin/eliscript-check +15 -0
  7. package/bin/eliscript-eval +15 -0
  8. package/bin/eliscript-format +15 -0
  9. package/bin/eliscript-org +10 -0
  10. package/bin/eliscript-portable +17 -0
  11. package/bin/eliscript-seed +10 -0
  12. package/bin/eliscript-seed-build +10 -0
  13. package/bin/eliscript-watch +15 -0
  14. package/bootstrap/README.md +100 -0
  15. package/bootstrap/compiler/analyzer.eli +1426 -0
  16. package/bootstrap/compiler/compiler.eli +472 -0
  17. package/bootstrap/compiler/emitter.eli +1745 -0
  18. package/bootstrap/compiler/expander.eli +2522 -0
  19. package/bootstrap/compiler/formatter.eli +359 -0
  20. package/bootstrap/compiler/ir.eli +297 -0
  21. package/bootstrap/compiler/lower.eli +661 -0
  22. package/bootstrap/compiler/project.eli +1120 -0
  23. package/bootstrap/compiler/reader.eli +321 -0
  24. package/bootstrap/compiler/source-map.eli +165 -0
  25. package/bootstrap/compiler/symbol.eli +92 -0
  26. package/bootstrap/compiler/syntax.eli +54 -0
  27. package/bootstrap/compiler/transient-analysis.eli +825 -0
  28. package/bootstrap/host/build.mjs +28 -0
  29. package/bootstrap/host/bun.mjs +282 -0
  30. package/bootstrap/host/check-cli.mjs +163 -0
  31. package/bootstrap/host/eliscript-cli.mjs +36 -0
  32. package/bootstrap/host/evaluation-cli.mjs +401 -0
  33. package/bootstrap/host/evaluation.mjs +560 -0
  34. package/bootstrap/host/format-cli.mjs +139 -0
  35. package/bootstrap/host/project-cli.mjs +295 -0
  36. package/bootstrap/host/project.mjs +1080 -0
  37. package/bootstrap/host/watch-cli.mjs +129 -0
  38. package/bootstrap/host/watch.mjs +267 -0
  39. package/browser/host.mjs +357 -0
  40. package/browser/worker.mjs +60 -0
  41. package/compiler/README.md +118 -0
  42. package/compiler/eliscript-analyzer.el +716 -0
  43. package/compiler/eliscript-cli.el +108 -0
  44. package/compiler/eliscript-diagnostic.el +158 -0
  45. package/compiler/eliscript-emitter.el +1351 -0
  46. package/compiler/eliscript-evaluation.el +259 -0
  47. package/compiler/eliscript-expander.el +1630 -0
  48. package/compiler/eliscript-form.el +79 -0
  49. package/compiler/eliscript-ir-emitter.el +1460 -0
  50. package/compiler/eliscript-ir.el +356 -0
  51. package/compiler/eliscript-lower.el +463 -0
  52. package/compiler/eliscript-macro-eval.el +811 -0
  53. package/compiler/eliscript-parameters.el +324 -0
  54. package/compiler/eliscript-portable.el +492 -0
  55. package/compiler/eliscript-project-cli.el +156 -0
  56. package/compiler/eliscript-project.el +1676 -0
  57. package/compiler/eliscript-reader.el +421 -0
  58. package/compiler/eliscript-source-map.el +192 -0
  59. package/compiler/eliscript-symbol.el +101 -0
  60. package/compiler/eliscript-transient-analysis.el +817 -0
  61. package/compiler/eliscript.el +239 -0
  62. package/dist/bootstrap/analyzer.mjs +968 -0
  63. package/dist/bootstrap/analyzer.mjs.map +1 -0
  64. package/dist/bootstrap/compiler.mjs +228 -0
  65. package/dist/bootstrap/compiler.mjs.map +1 -0
  66. package/dist/bootstrap/emitter.mjs +1173 -0
  67. package/dist/bootstrap/emitter.mjs.map +1 -0
  68. package/dist/bootstrap/expander.mjs +1469 -0
  69. package/dist/bootstrap/expander.mjs.map +1 -0
  70. package/dist/bootstrap/formatter.mjs +304 -0
  71. package/dist/bootstrap/formatter.mjs.map +1 -0
  72. package/dist/bootstrap/ir.mjs +193 -0
  73. package/dist/bootstrap/ir.mjs.map +1 -0
  74. package/dist/bootstrap/lower.mjs +344 -0
  75. package/dist/bootstrap/lower.mjs.map +1 -0
  76. package/dist/bootstrap/project.mjs +585 -0
  77. package/dist/bootstrap/project.mjs.map +1 -0
  78. package/dist/bootstrap/reader.mjs +259 -0
  79. package/dist/bootstrap/reader.mjs.map +1 -0
  80. package/dist/bootstrap/source-map.mjs +154 -0
  81. package/dist/bootstrap/source-map.mjs.map +1 -0
  82. package/dist/bootstrap/symbol.mjs +74 -0
  83. package/dist/bootstrap/symbol.mjs.map +1 -0
  84. package/dist/bootstrap/syntax.mjs +38 -0
  85. package/dist/bootstrap/syntax.mjs.map +1 -0
  86. package/dist/bootstrap/transient-analysis.mjs +660 -0
  87. package/dist/bootstrap/transient-analysis.mjs.map +1 -0
  88. package/dist/browser/compiler.js +6491 -0
  89. package/editor/README.md +83 -0
  90. package/editor/eliscript-mode.el +878 -0
  91. package/editor/eliscript-repl.el +501 -0
  92. package/package.json +163 -0
  93. package/platform/README.md +48 -0
  94. package/platform/browser.mjs +204 -0
  95. package/platform/worker.mjs +166 -0
  96. package/runtime/README.md +296 -0
  97. package/runtime/core/collection-internals.mjs +512 -0
  98. package/runtime/core/collection.mjs +645 -0
  99. package/runtime/core/data-text.mjs +532 -0
  100. package/runtime/core/data.mjs +268 -0
  101. package/runtime/core/identifier.mjs +314 -0
  102. package/runtime/core/lazy-sequence.mjs +244 -0
  103. package/runtime/core/list-internals.mjs +18 -0
  104. package/runtime/core/list.mjs +273 -0
  105. package/runtime/core/map-internals.mjs +793 -0
  106. package/runtime/core/map.mjs +394 -0
  107. package/runtime/core/metadata-internals.mjs +14 -0
  108. package/runtime/core/metadata.mjs +44 -0
  109. package/runtime/core/object-impl.mjs +65 -0
  110. package/runtime/core/object-impl.mjs.map +1 -0
  111. package/runtime/core/object.mjs +13 -0
  112. package/runtime/core/order.mjs +234 -0
  113. package/runtime/core/persistent-kind.mjs +43 -0
  114. package/runtime/core/protocol-error.mjs +36 -0
  115. package/runtime/core/protocol-impl.mjs +326 -0
  116. package/runtime/core/protocol-impl.mjs.map +1 -0
  117. package/runtime/core/protocol.mjs +14 -0
  118. package/runtime/core/queue-internals.mjs +27 -0
  119. package/runtime/core/queue.mjs +238 -0
  120. package/runtime/core/record.mjs +346 -0
  121. package/runtime/core/regex.mjs +184 -0
  122. package/runtime/core/sequence.mjs +632 -0
  123. package/runtime/core/set-algebra.mjs +287 -0
  124. package/runtime/core/set-internals.mjs +33 -0
  125. package/runtime/core/set.mjs +378 -0
  126. package/runtime/core/sorted-internals.mjs +215 -0
  127. package/runtime/core/sorted.mjs +598 -0
  128. package/runtime/core/text-impl.mjs +95 -0
  129. package/runtime/core/text-impl.mjs.map +1 -0
  130. package/runtime/core/text.mjs +15 -0
  131. package/runtime/core/transducer.mjs +668 -0
  132. package/runtime/core/transient-internals.mjs +38 -0
  133. package/runtime/core/transient.mjs +53 -0
  134. package/runtime/core/truth.mjs +3 -0
  135. package/runtime/core/type.mjs +262 -0
  136. package/runtime/core/value-internals.mjs +188 -0
  137. package/runtime/core/value.mjs +93 -0
  138. package/runtime/core/vector-internals.mjs +102 -0
  139. package/runtime/core/vector.mjs +951 -0
  140. package/runtime/literals.mjs +34 -0
  141. package/runtime/source-mapping.mjs +220 -0
  142. package/runtime/testing/list.mjs +52 -0
  143. package/runtime/testing/map.mjs +145 -0
  144. package/runtime/testing/queue.mjs +49 -0
  145. package/runtime/testing/set.mjs +61 -0
  146. package/runtime/testing/sorted.mjs +105 -0
  147. package/runtime/testing/value.mjs +17 -0
  148. package/runtime/testing/vector.mjs +102 -0
  149. package/runtime/worker-value-codec.mjs +565 -0
  150. package/runtime/worker-value-stream.mjs +786 -0
  151. package/runtime/worker.mjs +975 -0
  152. package/stdlib/README.md +774 -0
  153. package/stdlib/bit.eli +36 -0
  154. package/stdlib/core/collection.eli +134 -0
  155. package/stdlib/core/data-text.eli +16 -0
  156. package/stdlib/core/data.eli +306 -0
  157. package/stdlib/core/identifier.eli +32 -0
  158. package/stdlib/core/lazy-sequence.eli +63 -0
  159. package/stdlib/core/metadata.eli +20 -0
  160. package/stdlib/core/object.eli +89 -0
  161. package/stdlib/core/order.eli +133 -0
  162. package/stdlib/core/protocol.eli +387 -0
  163. package/stdlib/core/record.eli +16 -0
  164. package/stdlib/core/regex.eli +17 -0
  165. package/stdlib/core/seq.eli +701 -0
  166. package/stdlib/core/set.eli +312 -0
  167. package/stdlib/core/sorted.eli +53 -0
  168. package/stdlib/core/text.eli +103 -0
  169. package/stdlib/core/transducer.eli +59 -0
  170. package/stdlib/core/transient.eli +24 -0
  171. package/stdlib/core/type.eli +19 -0
  172. package/stdlib/core/walk.eli +195 -0
  173. package/stdlib/core/zipper.eli +508 -0
  174. package/stdlib/data-text.eli +1092 -0
  175. package/stdlib/data.eli +42 -0
  176. package/stdlib/deferred.eli +70 -0
  177. package/stdlib/function.eli +120 -0
  178. package/stdlib/hierarchy.eli +245 -0
  179. package/stdlib/identifier.eli +147 -0
  180. package/stdlib/interop/js.eli +616 -0
  181. package/stdlib/json.eli +870 -0
  182. package/stdlib/metadata.eli +56 -0
  183. package/stdlib/multimethod.eli +464 -0
  184. package/stdlib/numeric.eli +193 -0
  185. package/stdlib/object.eli +96 -0
  186. package/stdlib/persistent-list.eli +145 -0
  187. package/stdlib/persistent-map.eli +724 -0
  188. package/stdlib/persistent-queue.eli +32 -0
  189. package/stdlib/persistent-set.eli +236 -0
  190. package/stdlib/persistent-vector.eli +342 -0
  191. package/stdlib/result.eli +141 -0
  192. package/stdlib/sequence.eli +106 -0
  193. package/stdlib/state/atom.eli +265 -0
  194. package/stdlib/text.eli +88 -0
  195. package/stdlib/value.eli +325 -0
@@ -0,0 +1,870 @@
1
+ (module eliscript.json
2
+ (import-portable "./persistent-map.eli"
3
+ empty-persistent-map
4
+ persistent-map-assoc
5
+ persistent-map-count
6
+ persistent-map-get
7
+ persistent-map-has?
8
+ persistent-map-to-entries
9
+ persistent-map?)
10
+ (import-portable "./persistent-vector.eli"
11
+ empty-persistent-vector
12
+ persistent-vector-conj
13
+ persistent-vector-count
14
+ persistent-vector-from-array
15
+ persistent-vector-nth
16
+ persistent-vector?)
17
+ (import-portable "./result.eli" err ok)
18
+ (import-portable "./value.eli" empty-value-map value-map-from-entries)
19
+
20
+ (defconst json-default-max-depth 256)
21
+ (defconst json-default-max-length 16777216)
22
+ (defconst json-default-max-values 1000000)
23
+ (defconst json-hard-max-depth 512)
24
+ (defconst json-hard-max-length 67108864)
25
+ (defconst json-hard-max-values 10000000)
26
+ (defconst json-error-kind "eliscript/json-error")
27
+
28
+ (defportable json-finite-number? (value)
29
+ (and (= (value-type value) "number")
30
+ (= value value)
31
+ (= (- value value) 0)))
32
+
33
+ (defportable json-safe-integer? (value)
34
+ (and (json-finite-number? value)
35
+ (= (mod value 1) 0)
36
+ (<= (- 9007199254740991) value)
37
+ (<= value 9007199254740991)))
38
+
39
+ (defportable json-option-integer?
40
+ (value minimum maximum)
41
+ (and (json-safe-integer? value)
42
+ (<= minimum value)
43
+ (<= value maximum)))
44
+
45
+ (defportable json-option-value (options key fallback)
46
+ (if (object-has? options key)
47
+ (get options key nil)
48
+ fallback))
49
+
50
+ (defportable json-options (options)
51
+ (if (nullish? options)
52
+ (js-object :maxDepth json-default-max-depth
53
+ :maxLength json-default-max-length
54
+ :maxValues json-default-max-values)
55
+ (if (not (= (value-type options) "object"))
56
+ nil
57
+ (let ((keys (object-keys options))
58
+ (valid t)
59
+ (index 0))
60
+ (while (and valid (< index (js-length keys)))
61
+ (let ((key (js-nth index keys)))
62
+ (unless (or (= key "maxDepth")
63
+ (= key "maxLength")
64
+ (= key "maxValues"))
65
+ (setq valid false)))
66
+ (setq index (1+ index)))
67
+ (if (not valid)
68
+ nil
69
+ (let ((max-depth
70
+ (json-option-value
71
+ options "maxDepth" json-default-max-depth))
72
+ (max-length
73
+ (json-option-value
74
+ options "maxLength" json-default-max-length))
75
+ (max-values
76
+ (json-option-value
77
+ options "maxValues" json-default-max-values)))
78
+ (if (and
79
+ (json-option-integer?
80
+ max-depth 0 json-hard-max-depth)
81
+ (json-option-integer?
82
+ max-length 1 json-hard-max-length)
83
+ (json-option-integer?
84
+ max-values 1 json-hard-max-values))
85
+ (js-object :maxDepth max-depth
86
+ :maxLength max-length
87
+ :maxValues max-values)
88
+ nil)))))))
89
+
90
+ (defportable json-error
91
+ (code message offset path)
92
+ (value-map-from-entries
93
+ (js-array (js-array "kind" json-error-kind)
94
+ (js-array "code" code)
95
+ (js-array "message" message)
96
+ (js-array "offset" offset)
97
+ (js-array "path" path))))
98
+
99
+ (defportable json-error? (value)
100
+ (and (persistent-map? value)
101
+ (= (persistent-map-count value) 5)
102
+ (= (persistent-map-get value "kind" nil) json-error-kind)
103
+ (= (value-type (persistent-map-get value "code" nil)) "string")
104
+ (= (value-type (persistent-map-get value "message" nil)) "string")
105
+ (= (value-type (persistent-map-get value "path" nil)) "string")
106
+ (persistent-map-has? value "offset")
107
+ (let ((offset (persistent-map-get value "offset" nil)))
108
+ (or (nil? offset)
109
+ (and (json-safe-integer? offset) (>= offset 0))))))
110
+
111
+ (defportable json-error-code (error)
112
+ (if (json-error? error)
113
+ (persistent-map-get error "code" nil)
114
+ nil))
115
+
116
+ (defportable json-error-message (error)
117
+ (if (json-error? error)
118
+ (persistent-map-get error "message" nil)
119
+ nil))
120
+
121
+ (defportable json-error-offset (error)
122
+ (if (json-error? error)
123
+ (persistent-map-get error "offset" nil)
124
+ nil))
125
+
126
+ (defportable json-error-path (error)
127
+ (if (json-error? error)
128
+ (persistent-map-get error "path" nil)
129
+ nil))
130
+
131
+ (defportable json-step-success (value index count)
132
+ (js-object :ok t :value value :index index :count count))
133
+
134
+ (defportable json-step-failure
135
+ (code message offset path count)
136
+ (js-object :ok false
137
+ :error (json-error code message offset path)
138
+ :index offset
139
+ :count count))
140
+
141
+ (defportable json-step-ok? (result)
142
+ (get result "ok" false))
143
+
144
+ (defportable json-step-error (result)
145
+ (get result "error" nil))
146
+
147
+ (defportable json-min (left right)
148
+ (if (< left right) left right))
149
+
150
+ (defportable json-slice (start end text)
151
+ (let ((result "")
152
+ (index start))
153
+ (while (< index end)
154
+ (setq result (str result (js-nth index text))
155
+ index (1+ index)))
156
+ result))
157
+
158
+ (defportable json-whitespace? (character)
159
+ (or (= character " ")
160
+ (= character "\t")
161
+ (= character "\n")
162
+ (= character "\r")))
163
+
164
+ (defportable json-skip-whitespace (source start)
165
+ (let ((index start))
166
+ (while (and (< index (js-length source))
167
+ (json-whitespace? (js-nth index source)))
168
+ (setq index (1+ index)))
169
+ index))
170
+
171
+ (defportable json-digit? (character)
172
+ (and (not (nil? character))
173
+ (>= character "0")
174
+ (<= character "9")))
175
+
176
+ (defportable json-nonzero-digit? (character)
177
+ (and (json-digit? character) (/= character "0")))
178
+
179
+ (defportable json-hex-value (character)
180
+ (cond
181
+ ((and (>= character "0") (<= character "9"))
182
+ (- (string-code-unit-at character 0) 48))
183
+ ((and (>= character "a") (<= character "f"))
184
+ (- (string-code-unit-at character 0) 87))
185
+ ((and (>= character "A") (<= character "F"))
186
+ (- (string-code-unit-at character 0) 55))
187
+ (t -1)))
188
+
189
+ (defportable json-hex-digit (value)
190
+ (js-nth value "0123456789abcdef"))
191
+
192
+ (defportable json-hex4 (value)
193
+ (str (json-hex-digit (bit-and
194
+ (unsigned-bit-shift-right value 12) 15))
195
+ (json-hex-digit (bit-and
196
+ (unsigned-bit-shift-right value 8) 15))
197
+ (json-hex-digit (bit-and
198
+ (unsigned-bit-shift-right value 4) 15))
199
+ (json-hex-digit (bit-and value 15))))
200
+
201
+ (defportable json-encode-string (value)
202
+ (let ((result "\"")
203
+ (index 0))
204
+ (while (< index (js-length value))
205
+ (let ((character (js-nth index value))
206
+ (code (string-code-unit-at value index)))
207
+ (cond
208
+ ((= character "\"") (setq result (str result "\\\"")))
209
+ ((= character "\\") (setq result (str result "\\\\")))
210
+ ((= code 8) (setq result (str result "\\b")))
211
+ ((= code 9) (setq result (str result "\\t")))
212
+ ((= code 10) (setq result (str result "\\n")))
213
+ ((= code 12) (setq result (str result "\\f")))
214
+ ((= code 13) (setq result (str result "\\r")))
215
+ ((or (< code 32)
216
+ (and (>= code 55296) (<= code 57343)))
217
+ (setq result (str result "\\u" (json-hex4 code))))
218
+ (t (setq result (str result character)))))
219
+ (setq index (1+ index)))
220
+ (str result "\"")))
221
+
222
+ (defportable json-path-index (path index)
223
+ (str path "[" index "]"))
224
+
225
+ (defportable json-path-key (path key)
226
+ (str path "[" (json-encode-string key) "]"))
227
+
228
+ (defportable json-read-string (source start count path)
229
+ (let ((index (1+ start))
230
+ (result "")
231
+ (done false)
232
+ (failure nil))
233
+ (while (and (not done) (nil? failure) (< index (js-length source)))
234
+ (let ((character (js-nth index source))
235
+ (code (string-code-unit-at source index)))
236
+ (cond
237
+ ((= character "\"")
238
+ (setq done t index (1+ index)))
239
+ ((< code 32)
240
+ (setq failure
241
+ (json-step-failure
242
+ "ELI-JSON-SYNTAX"
243
+ "unescaped control character in string"
244
+ index path count)))
245
+ ((= character "\\")
246
+ (if (>= (1+ index) (js-length source))
247
+ (setq failure
248
+ (json-step-failure
249
+ "ELI-JSON-SYNTAX"
250
+ "unexpected end of string"
251
+ start path count))
252
+ (let ((escape (js-nth (1+ index) source)))
253
+ (cond
254
+ ((= escape "\"")
255
+ (setq result (str result "\"") index (+ index 2)))
256
+ ((= escape "\\")
257
+ (setq result (str result "\\") index (+ index 2)))
258
+ ((= escape "/")
259
+ (setq result (str result "/") index (+ index 2)))
260
+ ((= escape "b")
261
+ (setq result (str result (string-from-code-unit 8))
262
+ index (+ index 2)))
263
+ ((= escape "f")
264
+ (setq result (str result (string-from-code-unit 12))
265
+ index (+ index 2)))
266
+ ((= escape "n")
267
+ (setq result (str result "\n") index (+ index 2)))
268
+ ((= escape "r")
269
+ (setq result (str result "\r") index (+ index 2)))
270
+ ((= escape "t")
271
+ (setq result (str result "\t") index (+ index 2)))
272
+ ((= escape "u")
273
+ (if (> (+ index 6) (js-length source))
274
+ (setq failure
275
+ (json-step-failure
276
+ "ELI-JSON-SYNTAX"
277
+ "incomplete Unicode escape"
278
+ index path count))
279
+ (let ((digit-index (+ index 2))
280
+ (limit (+ index 6))
281
+ (value 0)
282
+ (valid t))
283
+ (while (and valid (< digit-index limit))
284
+ (let ((digit
285
+ (json-hex-value
286
+ (js-nth digit-index source))))
287
+ (if (< digit 0)
288
+ (setq valid false)
289
+ (setq value (+ (* value 16) digit)
290
+ digit-index (1+ digit-index)))))
291
+ (if (not valid)
292
+ (setq failure
293
+ (json-step-failure
294
+ "ELI-JSON-SYNTAX"
295
+ "invalid Unicode escape"
296
+ index path count))
297
+ (setq result
298
+ (str result (string-from-code-unit value))
299
+ index limit)))))
300
+ (t
301
+ (setq failure
302
+ (json-step-failure
303
+ "ELI-JSON-SYNTAX"
304
+ "invalid string escape"
305
+ index path count)))))))
306
+ (t
307
+ (setq result (str result character)
308
+ index (1+ index))))))
309
+ (cond
310
+ ((not (nil? failure)) failure)
311
+ ((not done)
312
+ (json-step-failure
313
+ "ELI-JSON-SYNTAX" "unexpected end of string"
314
+ start path count))
315
+ (t (json-step-success result index count)))))
316
+
317
+ (defportable json-read-number (source start count path)
318
+ (let ((index start)
319
+ (failure nil))
320
+ (when (= (js-nth index source) "-")
321
+ (setq index (1+ index)))
322
+ (cond
323
+ ((>= index (js-length source))
324
+ (setq failure
325
+ (json-step-failure
326
+ "ELI-JSON-SYNTAX" "incomplete number"
327
+ start path count)))
328
+ ((= (js-nth index source) "0")
329
+ (setq index (1+ index))
330
+ (when (json-digit? (js-nth index source))
331
+ (setq failure
332
+ (json-step-failure
333
+ "ELI-JSON-SYNTAX" "number has a leading zero"
334
+ index path count))))
335
+ ((json-nonzero-digit? (js-nth index source))
336
+ (while (json-digit? (js-nth index source))
337
+ (setq index (1+ index))))
338
+ (t
339
+ (setq failure
340
+ (json-step-failure
341
+ "ELI-JSON-SYNTAX" "invalid number"
342
+ index path count))))
343
+ (when (and (nil? failure) (= (js-nth index source) "."))
344
+ (setq index (1+ index))
345
+ (if (not (json-digit? (js-nth index source)))
346
+ (setq failure
347
+ (json-step-failure
348
+ "ELI-JSON-SYNTAX"
349
+ "fraction requires at least one digit"
350
+ index path count))
351
+ (while (json-digit? (js-nth index source))
352
+ (setq index (1+ index)))))
353
+ (when (and (nil? failure)
354
+ (or (= (js-nth index source) "e")
355
+ (= (js-nth index source) "E")))
356
+ (setq index (1+ index))
357
+ (when (or (= (js-nth index source) "+")
358
+ (= (js-nth index source) "-"))
359
+ (setq index (1+ index)))
360
+ (if (not (json-digit? (js-nth index source)))
361
+ (setq failure
362
+ (json-step-failure
363
+ "ELI-JSON-SYNTAX"
364
+ "exponent requires at least one digit"
365
+ index path count))
366
+ (while (json-digit? (js-nth index source))
367
+ (setq index (1+ index)))))
368
+ (if (not (nil? failure))
369
+ failure
370
+ (let ((value
371
+ (string-to-number (json-slice start index source))))
372
+ (if (not (json-finite-number? value))
373
+ (json-step-failure
374
+ "ELI-JSON-NUMBER"
375
+ "number is outside the finite IEEE-754 range"
376
+ start path count)
377
+ (json-step-success value index count))))))
378
+
379
+ (defportable json-literal-matches? (source start literal)
380
+ (let ((index 0)
381
+ (matches t))
382
+ (while (and matches (< index (js-length literal)))
383
+ (unless (= (js-nth (+ start index) source)
384
+ (js-nth index literal))
385
+ (setq matches false))
386
+ (setq index (1+ index)))
387
+ matches))
388
+
389
+ (defportable json-read-literal
390
+ (source start count path literal value)
391
+ (if (json-literal-matches? source start literal)
392
+ (json-step-success value (+ start (js-length literal)) count)
393
+ (json-step-failure
394
+ "ELI-JSON-SYNTAX" (str "expected " literal)
395
+ start path count)))
396
+
397
+ (defportable json-read-array
398
+ (source options depth count start path)
399
+ (let ((index (json-skip-whitespace source (1+ start)))
400
+ (result (empty-persistent-vector))
401
+ (current-count count)
402
+ (item-index 0)
403
+ (done false)
404
+ (failure nil))
405
+ (if (= (js-nth index source) "]")
406
+ (json-step-success result (1+ index) current-count)
407
+ (progn
408
+ (while (and (not done) (nil? failure))
409
+ (let ((item
410
+ (json-read-node
411
+ source options (1+ depth) current-count index
412
+ (json-path-index path item-index))))
413
+ (if (not (json-step-ok? item))
414
+ (setq failure item)
415
+ (progn
416
+ (setq result
417
+ (persistent-vector-conj
418
+ result (get item "value" nil))
419
+ current-count (get item "count" current-count)
420
+ index
421
+ (json-skip-whitespace
422
+ source (get item "index" index)))
423
+ (cond
424
+ ((= (js-nth index source) "]")
425
+ (setq done t index (1+ index)))
426
+ ((= (js-nth index source) ",")
427
+ (setq index
428
+ (json-skip-whitespace source (1+ index))
429
+ item-index (1+ item-index))
430
+ (when (= (js-nth index source) "]")
431
+ (setq failure
432
+ (json-step-failure
433
+ "ELI-JSON-SYNTAX"
434
+ "array has a trailing comma"
435
+ index path current-count))))
436
+ (t
437
+ (setq failure
438
+ (json-step-failure
439
+ "ELI-JSON-SYNTAX"
440
+ "expected ',' or ']' after array item"
441
+ index path current-count))))))))
442
+ (if (nil? failure)
443
+ (json-step-success result index current-count)
444
+ failure)))))
445
+
446
+ (defportable json-read-object
447
+ (source options depth count start path)
448
+ (let ((index (json-skip-whitespace source (1+ start)))
449
+ (result (empty-value-map))
450
+ (current-count count)
451
+ (done false)
452
+ (failure nil))
453
+ (if (= (js-nth index source) "}")
454
+ (json-step-success result (1+ index) current-count)
455
+ (progn
456
+ (while (and (not done) (nil? failure))
457
+ (if (/= (js-nth index source) "\"")
458
+ (setq failure
459
+ (json-step-failure
460
+ "ELI-JSON-SYNTAX"
461
+ "object key must be a string"
462
+ index path current-count))
463
+ (let ((key-result
464
+ (json-read-string
465
+ source index current-count path)))
466
+ (if (not (json-step-ok? key-result))
467
+ (setq failure key-result)
468
+ (let* ((key (get key-result "value" nil))
469
+ (key-path (json-path-key path key))
470
+ (colon-index
471
+ (json-skip-whitespace
472
+ source (get key-result "index" index))))
473
+ (if (/= (js-nth colon-index source) ":")
474
+ (setq failure
475
+ (json-step-failure
476
+ "ELI-JSON-SYNTAX"
477
+ "object key requires ':'"
478
+ colon-index key-path current-count))
479
+ (if (persistent-map-has? result key)
480
+ (setq failure
481
+ (json-step-failure
482
+ "ELI-JSON-DUPLICATE"
483
+ "duplicate object key"
484
+ index key-path current-count))
485
+ (let* ((value-index
486
+ (json-skip-whitespace
487
+ source (1+ colon-index)))
488
+ (value-result
489
+ (json-read-node
490
+ source options (1+ depth)
491
+ current-count value-index key-path)))
492
+ (if (not (json-step-ok? value-result))
493
+ (setq failure value-result)
494
+ (progn
495
+ (setq result
496
+ (persistent-map-assoc
497
+ result key
498
+ (get value-result "value" nil))
499
+ current-count
500
+ (get value-result "count" current-count)
501
+ index
502
+ (json-skip-whitespace
503
+ source
504
+ (get value-result "index" value-index)))
505
+ (cond
506
+ ((= (js-nth index source) "}")
507
+ (setq done t index (1+ index)))
508
+ ((= (js-nth index source) ",")
509
+ (setq index
510
+ (json-skip-whitespace
511
+ source (1+ index)))
512
+ (when (= (js-nth index source) "}")
513
+ (setq failure
514
+ (json-step-failure
515
+ "ELI-JSON-SYNTAX"
516
+ "object has a trailing comma"
517
+ index path current-count))))
518
+ (t
519
+ (setq failure
520
+ (json-step-failure
521
+ "ELI-JSON-SYNTAX"
522
+ "expected ',' or '}' after object value"
523
+ index path current-count))))))))))))))
524
+ (if (nil? failure)
525
+ (json-step-success result index current-count)
526
+ failure)))))
527
+
528
+ (defportable json-read-node
529
+ (source options depth count start path)
530
+ (let ((index (json-skip-whitespace source start)))
531
+ (cond
532
+ ((> depth (get options "maxDepth" json-default-max-depth))
533
+ (json-step-failure
534
+ "ELI-JSON-DEPTH"
535
+ (str "JSON exceeds maxDepth "
536
+ (get options "maxDepth" json-default-max-depth))
537
+ index path count))
538
+ ((>= count (get options "maxValues" json-default-max-values))
539
+ (json-step-failure
540
+ "ELI-JSON-VALUES"
541
+ (str "JSON exceeds maxValues "
542
+ (get options "maxValues" json-default-max-values))
543
+ index path count))
544
+ ((>= index (js-length source))
545
+ (json-step-failure
546
+ "ELI-JSON-SYNTAX" "unexpected end of input"
547
+ index path count))
548
+ (t
549
+ (let ((next-count (1+ count))
550
+ (character (js-nth index source)))
551
+ (cond
552
+ ((= character "\"")
553
+ (json-read-string source index next-count path))
554
+ ((= character "[")
555
+ (json-read-array
556
+ source options depth next-count index path))
557
+ ((= character "{")
558
+ (json-read-object
559
+ source options depth next-count index path))
560
+ ((or (= character "-") (json-digit? character))
561
+ (json-read-number source index next-count path))
562
+ ((= character "t")
563
+ (json-read-literal
564
+ source index next-count path "true" t))
565
+ ((= character "f")
566
+ (json-read-literal
567
+ source index next-count path "false" false))
568
+ ((= character "n")
569
+ (json-read-literal
570
+ source index next-count path "null" nil))
571
+ (t
572
+ (json-step-failure
573
+ "ELI-JSON-SYNTAX" "unexpected JSON token"
574
+ index path next-count))))))))
575
+
576
+ (defportable parse-json (source &optional options)
577
+ (let ((normalized (json-options options)))
578
+ (cond
579
+ ((not (= (value-type source) "string"))
580
+ (err
581
+ (json-error
582
+ "ELI-JSON-TYPE" "JSON source must be a string" 0 "$")))
583
+ ((nil? normalized)
584
+ (err
585
+ (json-error
586
+ "ELI-JSON-OPTIONS" "invalid JSON options" 0 "$")))
587
+ ((> (js-length source)
588
+ (get normalized "maxLength" json-default-max-length))
589
+ (err
590
+ (json-error
591
+ "ELI-JSON-LENGTH"
592
+ (str "JSON source exceeds maxLength "
593
+ (get normalized "maxLength" json-default-max-length))
594
+ 0 "$")))
595
+ (t
596
+ (let ((result (json-read-node source normalized 0 0 0 "$")))
597
+ (if (not (json-step-ok? result))
598
+ (err (json-step-error result))
599
+ (let ((index
600
+ (json-skip-whitespace
601
+ source (get result "index" 0))))
602
+ (if (/= index (js-length source))
603
+ (err
604
+ (json-error
605
+ "ELI-JSON-SYNTAX"
606
+ "trailing data after JSON value"
607
+ index "$"))
608
+ (ok (get result "value" nil))))))))))
609
+
610
+ (defportable json-identity-hash (value)
611
+ (uint32 (host-identity-token value)))
612
+
613
+ (defportable json-identical? (left right)
614
+ (eq left right))
615
+
616
+ (defportable json-empty-active-map ()
617
+ (empty-persistent-map
618
+ json-identity-hash json-identical? json-identical?))
619
+
620
+ (defportable json-encode-success (text count)
621
+ (js-object :ok t :text text :count count))
622
+
623
+ (defportable json-encode-failure
624
+ (code message path count)
625
+ (js-object :ok false
626
+ :error (json-error code message nil path)
627
+ :count count))
628
+
629
+ (defportable json-encode-text (text options path count)
630
+ (if (> (js-length text)
631
+ (get options "maxLength" json-default-max-length))
632
+ (json-encode-failure
633
+ "ELI-JSON-LENGTH"
634
+ (str "JSON output exceeds maxLength "
635
+ (get options "maxLength" json-default-max-length))
636
+ path count)
637
+ (json-encode-success text count)))
638
+
639
+ (defportable json-entry-key (entry)
640
+ (js-nth 0 entry))
641
+
642
+ (defportable json-sort-entry-pass (entries width)
643
+ (let ((result (empty-persistent-vector))
644
+ (count (persistent-vector-count entries))
645
+ (start 0))
646
+ (while (< start count)
647
+ (let ((left start)
648
+ (middle (json-min (+ start width) count))
649
+ (right (json-min (+ start (* width 2)) count))
650
+ (left-index start)
651
+ (right-index (json-min (+ start width) count)))
652
+ (while (or (< left-index middle) (< right-index right))
653
+ (cond
654
+ ((>= left-index middle)
655
+ (setq result
656
+ (persistent-vector-conj
657
+ result
658
+ (persistent-vector-nth
659
+ entries right-index nil))
660
+ right-index (1+ right-index)))
661
+ ((>= right-index right)
662
+ (setq result
663
+ (persistent-vector-conj
664
+ result
665
+ (persistent-vector-nth
666
+ entries left-index nil))
667
+ left-index (1+ left-index)))
668
+ (t
669
+ (let ((left-entry
670
+ (persistent-vector-nth entries left-index nil))
671
+ (right-entry
672
+ (persistent-vector-nth entries right-index nil)))
673
+ (if (<= (json-entry-key left-entry)
674
+ (json-entry-key right-entry))
675
+ (setq result
676
+ (persistent-vector-conj result left-entry)
677
+ left-index (1+ left-index))
678
+ (setq result
679
+ (persistent-vector-conj result right-entry)
680
+ right-index (1+ right-index)))))))
681
+ (setq start right)))
682
+ result))
683
+
684
+ (defportable json-sort-entries (entries)
685
+ (let ((result entries)
686
+ (width 1)
687
+ (count (persistent-vector-count entries)))
688
+ (while (< width count)
689
+ (setq result (json-sort-entry-pass result width)
690
+ width (* width 2)))
691
+ result))
692
+
693
+ (defportable json-encode-vector
694
+ (value options depth count path active)
695
+ (if (persistent-map-has? active value)
696
+ (json-encode-failure
697
+ "ELI-JSON-CYCLE"
698
+ (str "cyclic JSON value refers to "
699
+ (persistent-map-get active value "$"))
700
+ path count)
701
+ (let ((next-active (persistent-map-assoc active value path))
702
+ (text "[")
703
+ (current-count count)
704
+ (index 0)
705
+ (failure nil))
706
+ (while (and (nil? failure)
707
+ (< index (persistent-vector-count value)))
708
+ (let ((encoded
709
+ (json-encode-node
710
+ (persistent-vector-nth value index nil)
711
+ options (1+ depth) current-count
712
+ (json-path-index path index) next-active)))
713
+ (if (not (get encoded "ok" false))
714
+ (setq failure encoded)
715
+ (progn
716
+ (setq text
717
+ (str text
718
+ (if (> index 0) "," "")
719
+ (get encoded "text" ""))
720
+ current-count
721
+ (get encoded "count" current-count))
722
+ (when (> (js-length text)
723
+ (get options "maxLength"
724
+ json-default-max-length))
725
+ (setq failure
726
+ (json-encode-failure
727
+ "ELI-JSON-LENGTH"
728
+ (str "JSON output exceeds maxLength "
729
+ (get options "maxLength"
730
+ json-default-max-length))
731
+ path current-count))))))
732
+ (setq index (1+ index)))
733
+ (if (nil? failure)
734
+ (json-encode-text
735
+ (str text "]") options path current-count)
736
+ failure))))
737
+
738
+ (defportable json-encode-map
739
+ (value options depth count path active)
740
+ (if (persistent-map-has? active value)
741
+ (json-encode-failure
742
+ "ELI-JSON-CYCLE"
743
+ (str "cyclic JSON value refers to "
744
+ (persistent-map-get active value "$"))
745
+ path count)
746
+ (let* ((next-active (persistent-map-assoc active value path))
747
+ (raw-entries
748
+ (persistent-vector-from-array
749
+ (persistent-map-to-entries value)))
750
+ (entry-count (persistent-vector-count raw-entries))
751
+ (valid t)
752
+ (validation-index 0)
753
+ (failure nil))
754
+ (while (and valid (< validation-index entry-count))
755
+ (let ((entry
756
+ (persistent-vector-nth
757
+ raw-entries validation-index nil)))
758
+ (when (/= (value-type (json-entry-key entry)) "string")
759
+ (setq valid false
760
+ failure
761
+ (json-encode-failure
762
+ "ELI-JSON-KEY"
763
+ "JSON object keys must be strings"
764
+ (str path "<key:" validation-index ">")
765
+ count)))
766
+ (setq validation-index (1+ validation-index))))
767
+ (if (not valid)
768
+ failure
769
+ (let ((entries (json-sort-entries raw-entries))
770
+ (text "{")
771
+ (current-count count)
772
+ (index 0))
773
+ (while (and (nil? failure) (< index entry-count))
774
+ (let* ((entry
775
+ (persistent-vector-nth entries index nil))
776
+ (key (js-nth 0 entry))
777
+ (key-path (json-path-key path key))
778
+ (encoded
779
+ (json-encode-node
780
+ (js-nth 1 entry) options (1+ depth)
781
+ current-count key-path next-active)))
782
+ (if (not (get encoded "ok" false))
783
+ (setq failure encoded)
784
+ (progn
785
+ (setq text
786
+ (str text
787
+ (if (> index 0) "," "")
788
+ (json-encode-string key)
789
+ ":"
790
+ (get encoded "text" ""))
791
+ current-count
792
+ (get encoded "count" current-count))
793
+ (when (> (js-length text)
794
+ (get options "maxLength"
795
+ json-default-max-length))
796
+ (setq failure
797
+ (json-encode-failure
798
+ "ELI-JSON-LENGTH"
799
+ (str "JSON output exceeds maxLength "
800
+ (get options "maxLength"
801
+ json-default-max-length))
802
+ path current-count))))))
803
+ (setq index (1+ index)))
804
+ (if (nil? failure)
805
+ (json-encode-text
806
+ (str text "}") options path current-count)
807
+ failure))))))
808
+
809
+ (defportable json-encode-node
810
+ (value options depth count path active)
811
+ (cond
812
+ ((> depth (get options "maxDepth" json-default-max-depth))
813
+ (json-encode-failure
814
+ "ELI-JSON-DEPTH"
815
+ (str "JSON exceeds maxDepth "
816
+ (get options "maxDepth" json-default-max-depth))
817
+ path count))
818
+ ((>= count (get options "maxValues" json-default-max-values))
819
+ (json-encode-failure
820
+ "ELI-JSON-VALUES"
821
+ (str "JSON exceeds maxValues "
822
+ (get options "maxValues" json-default-max-values))
823
+ path count))
824
+ (t
825
+ (let ((next-count (1+ count))
826
+ (type (value-type value)))
827
+ (cond
828
+ ((= type "null")
829
+ (json-encode-text "null" options path next-count))
830
+ ((= type "boolean")
831
+ (json-encode-text
832
+ (if value "true" "false") options path next-count))
833
+ ((= type "number")
834
+ (if (json-finite-number? value)
835
+ (json-encode-text
836
+ (str value) options path next-count)
837
+ (json-encode-failure
838
+ "ELI-JSON-NUMBER"
839
+ "JSON numbers must be finite"
840
+ path next-count)))
841
+ ((= type "string")
842
+ (json-encode-text
843
+ (json-encode-string value) options path next-count))
844
+ ((persistent-vector? value)
845
+ (json-encode-vector
846
+ value options depth next-count path active))
847
+ ((persistent-map? value)
848
+ (json-encode-map
849
+ value options depth next-count path active))
850
+ (t
851
+ (json-encode-failure
852
+ "ELI-JSON-UNSUPPORTED"
853
+ (str "unsupported JSON value type: " type)
854
+ path next-count)))))))
855
+
856
+ (defportable stringify-json (value &optional options)
857
+ (let ((normalized (json-options options)))
858
+ (if (nil? normalized)
859
+ (err
860
+ (json-error
861
+ "ELI-JSON-OPTIONS" "invalid JSON options" nil "$"))
862
+ (let ((result
863
+ (json-encode-node
864
+ value normalized 0 0 "$" (json-empty-active-map))))
865
+ (if (get result "ok" false)
866
+ (ok (get result "text" ""))
867
+ (err (json-step-error result)))))))
868
+
869
+ (export json-error-code json-error-message json-error-offset
870
+ json-error-path json-error? parse-json stringify-json))