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,421 @@
1
+ ;;; eliscript-reader.el --- Reader for Eliscript -*- lexical-binding: t; -*-
2
+
3
+ ;;; Commentary:
4
+
5
+ ;; Eliscript intentionally starts with the Emacs Lisp reader. The compiler
6
+ ;; owns the language semantics after reading, so accepting an Emacs Lisp form
7
+ ;; does not imply that the form has Emacs Lisp runtime behavior.
8
+
9
+ ;;; Code:
10
+
11
+ (require 'cl-lib)
12
+ (require 'lisp-mode)
13
+ (require 'eliscript-diagnostic)
14
+ (require 'eliscript-form)
15
+
16
+ (define-error 'eliscript-read-error
17
+ "Eliscript reader error"
18
+ 'eliscript-compile-error)
19
+
20
+ (cl-defstruct (eliscript-reader--node
21
+ (:constructor eliscript-reader--node-create))
22
+ span
23
+ children
24
+ kind)
25
+
26
+ (defun eliscript-reader--queue-opening-index (source index)
27
+ "Return the opening bracket index for a Queue tag at INDEX in SOURCE."
28
+ (let ((tag-end (+ index 6)))
29
+ (when (and (<= tag-end (length source))
30
+ (string= (substring source index tag-end) "#queue"))
31
+ (let ((cursor tag-end)
32
+ done)
33
+ (while (and (< cursor (length source)) (not done))
34
+ (let ((character (aref source cursor)))
35
+ (cond
36
+ ((memq character '(32 9 10 13 12))
37
+ (setq cursor (1+ cursor)))
38
+ ((= character ?\;)
39
+ (while (and (< cursor (length source))
40
+ (/= (aref source cursor) ?\n))
41
+ (setq cursor (1+ cursor))))
42
+ (t (setq done t)))))
43
+ (and (< cursor (length source))
44
+ (= (aref source cursor) ?\[)
45
+ cursor)))))
46
+
47
+ (defun eliscript-reader--queue-closing-index (source opening)
48
+ "Return the bracket closing Queue OPENING in SOURCE, or nil."
49
+ (let ((cursor (1+ opening))
50
+ (stack (list ?\]))
51
+ in-string escaped in-comment result)
52
+ (while (and (< cursor (length source)) stack)
53
+ (let ((character (aref source cursor)))
54
+ (cond
55
+ (in-comment
56
+ (when (= character ?\n) (setq in-comment nil)))
57
+ (in-string
58
+ (cond
59
+ (escaped (setq escaped nil))
60
+ ((= character ?\\) (setq escaped t))
61
+ ((= character ?\") (setq in-string nil))))
62
+ ((= character ?\;) (setq in-comment t))
63
+ ((= character ?\") (setq in-string t))
64
+ ((= character ?\() (push ?\) stack))
65
+ ((= character ?\[) (push ?\] stack))
66
+ ((= character ?\{) (push ?\} stack))
67
+ ((memq character '(?\) ?\] ?\}))
68
+ (if (= character (car stack))
69
+ (progn
70
+ (pop stack)
71
+ (unless stack (setq result cursor)))
72
+ (setq stack nil result nil)))))
73
+ (setq cursor (1+ cursor)))
74
+ result))
75
+
76
+ (defun eliscript-reader--emacs-source (source)
77
+ "Return a length-preserving Emacs-readable copy of SOURCE.
78
+
79
+ Map braces outside strings and comments become parentheses. Set literals
80
+ become a same-length parenthesized form with the dispatch brace replaced by
81
+ whitespace. Eliscript owns both persistent value semantics after the host
82
+ reader has produced a list."
83
+ (let ((result (copy-sequence source))
84
+ (index 0)
85
+ (in-string nil)
86
+ (escaped nil)
87
+ (in-comment nil))
88
+ (while (< index (length result))
89
+ (let ((character (aref result index)))
90
+ (cond
91
+ (in-comment
92
+ (when (= character ?\n)
93
+ (setq in-comment nil)))
94
+ (in-string
95
+ (cond
96
+ (escaped (setq escaped nil))
97
+ ((= character ?\\) (setq escaped t))
98
+ ((= character ?\") (setq in-string nil))))
99
+ ((= character ?\;) (setq in-comment t))
100
+ ((= character ?\") (setq in-string t))
101
+ ((let ((opening
102
+ (eliscript-reader--queue-opening-index source index)))
103
+ (when opening
104
+ (aset result index ?\()
105
+ (aset result opening ?\s)
106
+ (let ((closing
107
+ (eliscript-reader--queue-closing-index source opening)))
108
+ (when closing (aset result closing ?\))))
109
+ t)))
110
+ ((and (= character ?#)
111
+ (< (1+ index) (length result))
112
+ (= (aref result (1+ index)) ?\{))
113
+ (aset result index ?\()
114
+ (aset result (1+ index) ?\s))
115
+ ((= character ?\{) (aset result index ?\())
116
+ ((= character ?\}) (aset result index ?\)))))
117
+ (setq index (1+ index)))
118
+ result))
119
+
120
+ (defun eliscript-reader--span (start end filename)
121
+ "Return a source span from buffer positions START to END in FILENAME."
122
+ (let (line column end-line end-column)
123
+ (save-excursion
124
+ (goto-char start)
125
+ (setq line (line-number-at-pos)
126
+ column (1+ (- start (line-beginning-position))))
127
+ (goto-char end)
128
+ (setq end-line (line-number-at-pos)
129
+ end-column (1+ (- end (line-beginning-position)))))
130
+ (eliscript-source-span-create
131
+ :filename filename
132
+ :start (1- start)
133
+ :end (1- end)
134
+ :line line
135
+ :column column
136
+ :end-line end-line
137
+ :end-column end-column)))
138
+
139
+ (defun eliscript-reader--prefix-length ()
140
+ "Return the reader-prefix length at point, or nil when none is present."
141
+ (cond
142
+ ((looking-at "#'") 2)
143
+ ((memq (char-after) '(?' ?`)) 1)
144
+ ((eq (char-after) ?,)
145
+ (if (eq (char-after (1+ (point))) ?@) 2 1))))
146
+
147
+ (defun eliscript-reader--queue-opening-position ()
148
+ "Return the Queue payload opening position at point, or nil."
149
+ (let ((start (point)))
150
+ (when (and (<= (+ start 6) (point-max))
151
+ (string= (buffer-substring-no-properties start (+ start 6))
152
+ "#queue"))
153
+ (save-excursion
154
+ (goto-char (+ start 6))
155
+ (forward-comment (point-max))
156
+ (and (eq (char-after) ?\[) (point))))))
157
+
158
+ (defun eliscript-reader--scan-node (filename)
159
+ "Scan one source node at point for FILENAME and return its location tree."
160
+ (forward-comment (point-max))
161
+ (let* ((start (point))
162
+ (prefix-length (eliscript-reader--prefix-length))
163
+ (queue-opening (eliscript-reader--queue-opening-position))
164
+ children)
165
+ (cond
166
+ (prefix-length
167
+ (forward-char prefix-length)
168
+ (let* ((prefix-end (point))
169
+ (child (eliscript-reader--scan-node filename))
170
+ (end (point)))
171
+ (setq children
172
+ (list
173
+ (eliscript-reader--node-create
174
+ :span (eliscript-reader--span start prefix-end filename)
175
+ :kind 'prefix-token)
176
+ child))
177
+ (eliscript-reader--node-create
178
+ :span (eliscript-reader--span start end filename)
179
+ :children children
180
+ :kind 'prefix)))
181
+ (queue-opening
182
+ (goto-char queue-opening)
183
+ (forward-char 1)
184
+ (forward-comment (point-max))
185
+ (while (and (char-after) (not (eq (char-after) ?\])))
186
+ (when (memq (char-after) '(?\) ?\}))
187
+ (eliscript-diagnostic-signal
188
+ 'eliscript-read-error "ELI-R0001" "reader"
189
+ filename (eliscript-reader--span start (+ start 6) filename)
190
+ "Invalid read syntax: %S" (char-to-string (char-after))))
191
+ (push (eliscript-reader--scan-node filename) children)
192
+ (forward-comment (point-max)))
193
+ (when (eq (char-after) ?\]) (forward-char 1))
194
+ (eliscript-reader--node-create
195
+ :span (eliscript-reader--span start (point) filename)
196
+ :children (nreverse children)
197
+ :kind 'queue))
198
+ ((and (eq (char-after) ?#)
199
+ (eq (char-after (1+ (point))) ?\{))
200
+ (forward-char 2)
201
+ (forward-comment (point-max))
202
+ (while (and (char-after) (not (eq (char-after) ?\})))
203
+ (when (memq (char-after) '(?\) ?\]))
204
+ (eliscript-diagnostic-signal
205
+ 'eliscript-read-error "ELI-R0001" "reader"
206
+ filename (eliscript-reader--span start (+ start 2) filename)
207
+ "Invalid read syntax: %S" (char-to-string (char-after))))
208
+ (push (eliscript-reader--scan-node filename) children)
209
+ (forward-comment (point-max)))
210
+ (when (eq (char-after) ?\})
211
+ (forward-char 1))
212
+ (eliscript-reader--node-create
213
+ :span (eliscript-reader--span start (point) filename)
214
+ :children (nreverse children)
215
+ :kind 'set))
216
+ ((memq (char-after) '(?\( ?\[ ?\{))
217
+ (let* ((opening (char-after))
218
+ (closing (pcase opening
219
+ (?\( ?\))
220
+ (?\[ ?\])
221
+ (?\{ ?\})))
222
+ (kind (pcase opening
223
+ (?\( 'list)
224
+ (?\[ 'vector)
225
+ (?\{ 'map))))
226
+ (forward-char 1)
227
+ (forward-comment (point-max))
228
+ (while (and (char-after) (not (eq (char-after) closing)))
229
+ (when (memq (char-after) '(?\) ?\] ?\}))
230
+ (eliscript-diagnostic-signal
231
+ 'eliscript-read-error "ELI-R0001" "reader"
232
+ filename (eliscript-reader--span start (1+ start) filename)
233
+ "Invalid read syntax: %S" (char-to-string (char-after))))
234
+ (push (eliscript-reader--scan-node filename) children)
235
+ (forward-comment (point-max)))
236
+ (when (eq (char-after) closing)
237
+ (forward-char 1))
238
+ (eliscript-reader--node-create
239
+ :span (eliscript-reader--span start (point) filename)
240
+ :children (nreverse children)
241
+ :kind kind)))
242
+ (t
243
+ (let ((end (scan-sexps start 1)))
244
+ (unless end
245
+ (setq end (point-max)))
246
+ (goto-char end)
247
+ (eliscript-reader--node-create
248
+ :span (eliscript-reader--span start end filename)
249
+ :kind 'atom))))))
250
+
251
+ (defun eliscript-reader--map-operator-span (span)
252
+ "Return the opening-brace operator span within map SPAN."
253
+ (eliscript-source-span-create
254
+ :filename (eliscript-source-span-filename span)
255
+ :start (eliscript-source-span-start span)
256
+ :end (1+ (eliscript-source-span-start span))
257
+ :line (eliscript-source-span-line span)
258
+ :column (eliscript-source-span-column span)
259
+ :end-line (eliscript-source-span-line span)
260
+ :end-column (1+ (eliscript-source-span-column span))))
261
+
262
+ (defun eliscript-reader--set-operator-span (span)
263
+ "Return the dispatch-brace operator span within Set SPAN."
264
+ (eliscript-source-span-create
265
+ :filename (eliscript-source-span-filename span)
266
+ :start (eliscript-source-span-start span)
267
+ :end (+ 2 (eliscript-source-span-start span))
268
+ :line (eliscript-source-span-line span)
269
+ :column (eliscript-source-span-column span)
270
+ :end-line (eliscript-source-span-line span)
271
+ :end-column (+ 2 (eliscript-source-span-column span))))
272
+
273
+ (defun eliscript-reader--queue-operator-span (span)
274
+ "Return the Queue tag operator span within Queue SPAN."
275
+ (eliscript-source-span-create
276
+ :filename (eliscript-source-span-filename span)
277
+ :start (eliscript-source-span-start span)
278
+ :end (+ 6 (eliscript-source-span-start span))
279
+ :line (eliscript-source-span-line span)
280
+ :column (eliscript-source-span-column span)
281
+ :end-line (eliscript-source-span-line span)
282
+ :end-column (+ 6 (eliscript-source-span-column span))))
283
+
284
+ (defun eliscript-reader--valid-keyword-name-p (name)
285
+ "Return non-nil when NAME is a valid source Keyword name."
286
+ (let ((slash-count (cl-count ?/ name)))
287
+ (and (> (length name) 0)
288
+ (or (= slash-count 0)
289
+ (and (= slash-count 1)
290
+ (not (= (aref name 0) ?/))
291
+ (not (= (aref name (1- (length name))) ?/)))))))
292
+
293
+ (defun eliscript-reader--locate-value (value node)
294
+ "Attach locations from NODE to recursively read VALUE."
295
+ (let ((span (eliscript-reader--node-span node))
296
+ (children (eliscript-reader--node-children node))
297
+ (kind (eliscript-reader--node-kind node)))
298
+ (cond
299
+ ((keywordp value)
300
+ (let ((name (substring (symbol-name value) 1)))
301
+ (unless (eliscript-reader--valid-keyword-name-p name)
302
+ (eliscript-diagnostic-signal
303
+ 'eliscript-read-error "ELI-R0001" "reader"
304
+ (eliscript-source-span-filename span) span
305
+ "invalid keyword literal: %s" value))
306
+ (eliscript-form-locate-generated value span)))
307
+ ((eq kind 'map)
308
+ (when (= (% (length children) 2) 1)
309
+ (eliscript-diagnostic-signal
310
+ 'eliscript-read-error "ELI-R0001" "reader"
311
+ (eliscript-source-span-filename span) span
312
+ "map literal must contain an even number of forms"))
313
+ (eliscript-form-wrap
314
+ (cons
315
+ (eliscript-form-wrap
316
+ 'hash-map (eliscript-reader--map-operator-span span))
317
+ (cl-mapcar #'eliscript-reader--locate-value value children))
318
+ span))
319
+ ((eq kind 'set)
320
+ (eliscript-form-wrap
321
+ (cons
322
+ (eliscript-form-wrap
323
+ 'hash-set (eliscript-reader--set-operator-span span))
324
+ (cl-mapcar #'eliscript-reader--locate-value value children))
325
+ span))
326
+ ((eq kind 'queue)
327
+ (eliscript-form-wrap
328
+ (cons
329
+ (eliscript-form-wrap
330
+ 'queue (eliscript-reader--queue-operator-span span))
331
+ (cl-mapcar #'eliscript-reader--locate-value (cdr value) children))
332
+ span))
333
+ ((and (vectorp value) (= (length value) (length children)))
334
+ (eliscript-form-wrap
335
+ (apply #'vector
336
+ (cl-mapcar #'eliscript-reader--locate-value
337
+ (append value nil) children))
338
+ span))
339
+ ((and (proper-list-p value) (= (length value) (length children)))
340
+ (eliscript-form-wrap
341
+ (cl-mapcar #'eliscript-reader--locate-value value children)
342
+ span))
343
+ (t (eliscript-form-locate-generated value span)))))
344
+
345
+ (defun eliscript-reader--diagnostic-span (filename)
346
+ "Return a one-character diagnostic span at point in FILENAME."
347
+ (let ((start (point)))
348
+ (eliscript-reader--span
349
+ start (min (point-max) (1+ start)) filename)))
350
+
351
+ (defun eliscript-reader--invalid-syntax-message (source start error-data)
352
+ "Return a stable invalid-reader message for SOURCE at START.
353
+
354
+ Emacs releases disagree on whether an unknown dispatch reports `#' or its
355
+ two-character dispatch prefix. Eliscript owns that diagnostic contract."
356
+ (if (and (< start (length source))
357
+ (= (aref source start) ?#))
358
+ (format "Invalid read syntax: %S"
359
+ (substring source start (min (length source) (+ start 2))))
360
+ (if (and (< start (length source))
361
+ (= (aref source start) ?\}))
362
+ "Invalid read syntax: \"}\""
363
+ (error-message-string error-data))))
364
+
365
+ (defun eliscript-read-located-string (source &optional filename)
366
+ "Read every Eliscript form from SOURCE with recursive source locations."
367
+ (with-temp-buffer
368
+ (insert source)
369
+ (set-syntax-table (copy-syntax-table emacs-lisp-mode-syntax-table))
370
+ (modify-syntax-entry ?\{ "(}" (syntax-table))
371
+ (modify-syntax-entry ?\} "){" (syntax-table))
372
+ (goto-char (point-min))
373
+ (let ((emacs-source (eliscript-reader--emacs-source source))
374
+ forms done)
375
+ (while (not done)
376
+ (forward-comment (point-max))
377
+ (if (eobp)
378
+ (setq done t)
379
+ (let ((start (point)))
380
+ (condition-case error-data
381
+ (let* ((result (read-from-string emacs-source (1- start)))
382
+ (value (car result))
383
+ (end (cdr result))
384
+ (node (eliscript-reader--scan-node filename)))
385
+ (push (eliscript-reader--locate-value value node) forms)
386
+ (goto-char (1+ end)))
387
+ (end-of-file
388
+ (eliscript-diagnostic-signal
389
+ 'eliscript-read-error "ELI-R0001" "reader"
390
+ filename (eliscript-reader--diagnostic-span filename)
391
+ "unexpected end of input"))
392
+ (invalid-read-syntax
393
+ (eliscript-diagnostic-signal
394
+ 'eliscript-read-error "ELI-R0001" "reader"
395
+ filename (eliscript-reader--diagnostic-span filename)
396
+ "%s" (eliscript-reader--invalid-syntax-message
397
+ source (1- start) error-data)))))))
398
+ (nreverse forms))))
399
+
400
+ (defun eliscript-read-string (source &optional filename)
401
+ "Read every Eliscript form from SOURCE.
402
+
403
+ FILENAME is used only to improve diagnostics."
404
+ (mapcar #'eliscript-form-strip
405
+ (eliscript-read-located-string source filename)))
406
+
407
+ (defun eliscript-read-file (filename)
408
+ "Read all Eliscript forms from FILENAME."
409
+ (with-temp-buffer
410
+ (insert-file-contents filename)
411
+ (eliscript-read-string (buffer-string) filename)))
412
+
413
+ (defun eliscript-read-located-file (filename)
414
+ "Read located Eliscript forms from FILENAME."
415
+ (with-temp-buffer
416
+ (insert-file-contents filename)
417
+ (eliscript-read-located-string (buffer-string) filename)))
418
+
419
+ (provide 'eliscript-reader)
420
+
421
+ ;;; eliscript-reader.el ends here
@@ -0,0 +1,192 @@
1
+ ;;; eliscript-source-map.el --- Source Map v3 support for Eliscript -*- lexical-binding: t; -*-
2
+
3
+ ;;; Commentary:
4
+
5
+ ;; The direct IR emitter temporarily marks generated string positions with
6
+ ;; source spans. This module turns those marks into a standard Source Map v3
7
+ ;; document and strips the Emacs-only text properties from the final module.
8
+
9
+ ;;; Code:
10
+
11
+ (require 'cl-lib)
12
+ (require 'json)
13
+ (require 'eliscript-form)
14
+
15
+ (cl-defstruct (eliscript-emission
16
+ (:constructor eliscript-emission-create))
17
+ javascript
18
+ source-map)
19
+
20
+ (defconst eliscript-source-map--base64
21
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
22
+ "Base64 alphabet used by Source Map VLQ fields.")
23
+
24
+ (defconst eliscript-source-map--span-property 'eliscript-source-span
25
+ "Text property used to mark generated positions with source spans.")
26
+
27
+ (defun eliscript-source-map-mark (text span)
28
+ "Mark the first character of TEXT with source SPAN.
29
+
30
+ An existing, more deeply nested marker at the same position takes precedence."
31
+ (if (and span (> (length text) 0))
32
+ (let ((marked (copy-sequence text)))
33
+ (when (null (get-text-property
34
+ 0 eliscript-source-map--span-property marked))
35
+ (put-text-property
36
+ 0 1 eliscript-source-map--span-property span marked))
37
+ marked)
38
+ text))
39
+
40
+ (defun eliscript-source-map--utf16-width (character)
41
+ "Return the UTF-16 code-unit width of CHARACTER."
42
+ (if (> character #xffff) 2 1))
43
+
44
+ (defun eliscript-source-map--collect-marks (generated)
45
+ "Collect source-span marks from GENERATED in generated order."
46
+ (let ((position 0)
47
+ (line 0)
48
+ (column 0)
49
+ marks)
50
+ (while (< position (length generated))
51
+ (let ((span (get-text-property
52
+ position eliscript-source-map--span-property generated))
53
+ (character (aref generated position)))
54
+ (when span
55
+ (push (list line column span) marks))
56
+ (if (= character ?\n)
57
+ (setq line (1+ line)
58
+ column 0)
59
+ (setq column
60
+ (+ column
61
+ (eliscript-source-map--utf16-width character))))
62
+ (setq position (1+ position))))
63
+ (nreverse marks)))
64
+
65
+ (defun eliscript-source-map--source-locations (source marks)
66
+ "Return source-map locations for source positions referenced by MARKS.
67
+
68
+ SOURCE is scanned once so tabs and non-BMP characters use Source Map's UTF-16
69
+ column convention rather than display columns."
70
+ (let ((wanted (make-hash-table :test #'eql))
71
+ (locations (make-hash-table :test #'eql)))
72
+ (dolist (mark marks)
73
+ (let ((start (eliscript-source-span-start (nth 2 mark))))
74
+ (when start (puthash start t wanted))))
75
+ (let ((offset 0)
76
+ (line 0)
77
+ (column 0)
78
+ (length (length source)))
79
+ (while (<= offset length)
80
+ (when (gethash offset wanted)
81
+ (puthash offset (cons line column) locations))
82
+ (when (< offset length)
83
+ (let ((character (aref source offset)))
84
+ (if (= character ?\n)
85
+ (setq line (1+ line)
86
+ column 0)
87
+ (setq column
88
+ (+ column
89
+ (eliscript-source-map--utf16-width character))))))
90
+ (setq offset (1+ offset))))
91
+ locations))
92
+
93
+ (defun eliscript-source-map--signed-vlq (value)
94
+ "Convert signed VALUE to its unsigned VLQ representation."
95
+ (if (< value 0)
96
+ (logior (ash (- value) 1) 1)
97
+ (ash value 1)))
98
+
99
+ (defun eliscript-source-map--encode-vlq (value)
100
+ "Encode signed integer VALUE as a Base64 VLQ string."
101
+ (let ((remaining (eliscript-source-map--signed-vlq value))
102
+ characters)
103
+ (while (>= remaining 32)
104
+ (push (aref eliscript-source-map--base64
105
+ (logior (logand remaining 31) 32))
106
+ characters)
107
+ (setq remaining (ash remaining -5)))
108
+ (push (aref eliscript-source-map--base64 remaining) characters)
109
+ (apply #'string (nreverse characters))))
110
+
111
+ (defun eliscript-source-map--original-location (span source-locations)
112
+ "Return zero-based source location for SPAN using SOURCE-LOCATIONS."
113
+ (or (and (eliscript-source-span-start span)
114
+ (gethash (eliscript-source-span-start span) source-locations))
115
+ (cons (1- (eliscript-source-span-line span))
116
+ (1- (eliscript-source-span-column span)))))
117
+
118
+ (defun eliscript-source-map--group-marks (marks source-locations)
119
+ "Group MARKS by generated line using SOURCE-LOCATIONS."
120
+ (let ((current-line 0)
121
+ current-segments
122
+ lines)
123
+ (dolist (mark marks)
124
+ (let ((generated-line (nth 0 mark)))
125
+ (while (< current-line generated-line)
126
+ (push (nreverse current-segments) lines)
127
+ (setq current-segments nil
128
+ current-line (1+ current-line)))
129
+ (pcase-let ((`(,original-line . ,original-column)
130
+ (eliscript-source-map--original-location
131
+ (nth 2 mark) source-locations)))
132
+ (push (list (nth 1 mark) 0 original-line original-column)
133
+ current-segments))))
134
+ (push (nreverse current-segments) lines)
135
+ (nreverse lines)))
136
+
137
+ (defun eliscript-source-map--encode-mappings (lines)
138
+ "Encode source-map segment LINES into a mappings string."
139
+ (let ((previous-source 0)
140
+ (previous-original-line 0)
141
+ (previous-original-column 0))
142
+ (mapconcat
143
+ (lambda (segments)
144
+ (let ((previous-generated-column 0))
145
+ (mapconcat
146
+ (lambda (segment)
147
+ (pcase-let ((`(,generated-column ,source
148
+ ,original-line ,original-column)
149
+ segment))
150
+ (prog1
151
+ (concat
152
+ (eliscript-source-map--encode-vlq
153
+ (- generated-column previous-generated-column))
154
+ (eliscript-source-map--encode-vlq
155
+ (- source previous-source))
156
+ (eliscript-source-map--encode-vlq
157
+ (- original-line previous-original-line))
158
+ (eliscript-source-map--encode-vlq
159
+ (- original-column previous-original-column)))
160
+ (setq previous-generated-column generated-column
161
+ previous-source source
162
+ previous-original-line original-line
163
+ previous-original-column original-column))))
164
+ segments ",")))
165
+ lines ";")))
166
+
167
+ (defun eliscript-source-map-create (generated source source-name generated-name)
168
+ "Create a Source Map v3 JSON document for GENERATED and SOURCE.
169
+
170
+ SOURCE-NAME identifies the original source. GENERATED-NAME identifies the
171
+ generated module and may be nil."
172
+ (let* ((marks (eliscript-source-map--collect-marks generated))
173
+ (locations (eliscript-source-map--source-locations source marks))
174
+ (mappings
175
+ (eliscript-source-map--encode-mappings
176
+ (eliscript-source-map--group-marks marks locations))))
177
+ (concat
178
+ (json-serialize
179
+ (append
180
+ '((version . 3))
181
+ (and generated-name `((file . ,generated-name)))
182
+ `((sources . [,source-name])
183
+ (sourcesContent . [,source])
184
+ (names . [])
185
+ (mappings . ,mappings)))
186
+ :null-object nil
187
+ :false-object :json-false)
188
+ "\n")))
189
+
190
+ (provide 'eliscript-source-map)
191
+
192
+ ;;; eliscript-source-map.el ends here