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.
- package/LICENSE +674 -0
- package/README.md +135 -0
- package/bin/eliscript +16 -0
- package/bin/eliscript-bootstrap +15 -0
- package/bin/eliscript-build +15 -0
- package/bin/eliscript-check +15 -0
- package/bin/eliscript-eval +15 -0
- package/bin/eliscript-format +15 -0
- package/bin/eliscript-org +10 -0
- package/bin/eliscript-portable +17 -0
- package/bin/eliscript-seed +10 -0
- package/bin/eliscript-seed-build +10 -0
- package/bin/eliscript-watch +15 -0
- package/bootstrap/README.md +100 -0
- package/bootstrap/compiler/analyzer.eli +1426 -0
- package/bootstrap/compiler/compiler.eli +472 -0
- package/bootstrap/compiler/emitter.eli +1745 -0
- package/bootstrap/compiler/expander.eli +2522 -0
- package/bootstrap/compiler/formatter.eli +359 -0
- package/bootstrap/compiler/ir.eli +297 -0
- package/bootstrap/compiler/lower.eli +661 -0
- package/bootstrap/compiler/project.eli +1120 -0
- package/bootstrap/compiler/reader.eli +321 -0
- package/bootstrap/compiler/source-map.eli +165 -0
- package/bootstrap/compiler/symbol.eli +92 -0
- package/bootstrap/compiler/syntax.eli +54 -0
- package/bootstrap/compiler/transient-analysis.eli +825 -0
- package/bootstrap/host/build.mjs +28 -0
- package/bootstrap/host/bun.mjs +282 -0
- package/bootstrap/host/check-cli.mjs +163 -0
- package/bootstrap/host/eliscript-cli.mjs +36 -0
- package/bootstrap/host/evaluation-cli.mjs +401 -0
- package/bootstrap/host/evaluation.mjs +560 -0
- package/bootstrap/host/format-cli.mjs +139 -0
- package/bootstrap/host/project-cli.mjs +295 -0
- package/bootstrap/host/project.mjs +1080 -0
- package/bootstrap/host/watch-cli.mjs +129 -0
- package/bootstrap/host/watch.mjs +267 -0
- package/browser/host.mjs +357 -0
- package/browser/worker.mjs +60 -0
- package/compiler/README.md +118 -0
- package/compiler/eliscript-analyzer.el +716 -0
- package/compiler/eliscript-cli.el +108 -0
- package/compiler/eliscript-diagnostic.el +158 -0
- package/compiler/eliscript-emitter.el +1351 -0
- package/compiler/eliscript-evaluation.el +259 -0
- package/compiler/eliscript-expander.el +1630 -0
- package/compiler/eliscript-form.el +79 -0
- package/compiler/eliscript-ir-emitter.el +1460 -0
- package/compiler/eliscript-ir.el +356 -0
- package/compiler/eliscript-lower.el +463 -0
- package/compiler/eliscript-macro-eval.el +811 -0
- package/compiler/eliscript-parameters.el +324 -0
- package/compiler/eliscript-portable.el +492 -0
- package/compiler/eliscript-project-cli.el +156 -0
- package/compiler/eliscript-project.el +1676 -0
- package/compiler/eliscript-reader.el +421 -0
- package/compiler/eliscript-source-map.el +192 -0
- package/compiler/eliscript-symbol.el +101 -0
- package/compiler/eliscript-transient-analysis.el +817 -0
- package/compiler/eliscript.el +239 -0
- package/dist/bootstrap/analyzer.mjs +968 -0
- package/dist/bootstrap/analyzer.mjs.map +1 -0
- package/dist/bootstrap/compiler.mjs +228 -0
- package/dist/bootstrap/compiler.mjs.map +1 -0
- package/dist/bootstrap/emitter.mjs +1173 -0
- package/dist/bootstrap/emitter.mjs.map +1 -0
- package/dist/bootstrap/expander.mjs +1469 -0
- package/dist/bootstrap/expander.mjs.map +1 -0
- package/dist/bootstrap/formatter.mjs +304 -0
- package/dist/bootstrap/formatter.mjs.map +1 -0
- package/dist/bootstrap/ir.mjs +193 -0
- package/dist/bootstrap/ir.mjs.map +1 -0
- package/dist/bootstrap/lower.mjs +344 -0
- package/dist/bootstrap/lower.mjs.map +1 -0
- package/dist/bootstrap/project.mjs +585 -0
- package/dist/bootstrap/project.mjs.map +1 -0
- package/dist/bootstrap/reader.mjs +259 -0
- package/dist/bootstrap/reader.mjs.map +1 -0
- package/dist/bootstrap/source-map.mjs +154 -0
- package/dist/bootstrap/source-map.mjs.map +1 -0
- package/dist/bootstrap/symbol.mjs +74 -0
- package/dist/bootstrap/symbol.mjs.map +1 -0
- package/dist/bootstrap/syntax.mjs +38 -0
- package/dist/bootstrap/syntax.mjs.map +1 -0
- package/dist/bootstrap/transient-analysis.mjs +660 -0
- package/dist/bootstrap/transient-analysis.mjs.map +1 -0
- package/dist/browser/compiler.js +6491 -0
- package/editor/README.md +83 -0
- package/editor/eliscript-mode.el +878 -0
- package/editor/eliscript-repl.el +501 -0
- package/package.json +163 -0
- package/platform/README.md +48 -0
- package/platform/browser.mjs +204 -0
- package/platform/worker.mjs +166 -0
- package/runtime/README.md +296 -0
- package/runtime/core/collection-internals.mjs +512 -0
- package/runtime/core/collection.mjs +645 -0
- package/runtime/core/data-text.mjs +532 -0
- package/runtime/core/data.mjs +268 -0
- package/runtime/core/identifier.mjs +314 -0
- package/runtime/core/lazy-sequence.mjs +244 -0
- package/runtime/core/list-internals.mjs +18 -0
- package/runtime/core/list.mjs +273 -0
- package/runtime/core/map-internals.mjs +793 -0
- package/runtime/core/map.mjs +394 -0
- package/runtime/core/metadata-internals.mjs +14 -0
- package/runtime/core/metadata.mjs +44 -0
- package/runtime/core/object-impl.mjs +65 -0
- package/runtime/core/object-impl.mjs.map +1 -0
- package/runtime/core/object.mjs +13 -0
- package/runtime/core/order.mjs +234 -0
- package/runtime/core/persistent-kind.mjs +43 -0
- package/runtime/core/protocol-error.mjs +36 -0
- package/runtime/core/protocol-impl.mjs +326 -0
- package/runtime/core/protocol-impl.mjs.map +1 -0
- package/runtime/core/protocol.mjs +14 -0
- package/runtime/core/queue-internals.mjs +27 -0
- package/runtime/core/queue.mjs +238 -0
- package/runtime/core/record.mjs +346 -0
- package/runtime/core/regex.mjs +184 -0
- package/runtime/core/sequence.mjs +632 -0
- package/runtime/core/set-algebra.mjs +287 -0
- package/runtime/core/set-internals.mjs +33 -0
- package/runtime/core/set.mjs +378 -0
- package/runtime/core/sorted-internals.mjs +215 -0
- package/runtime/core/sorted.mjs +598 -0
- package/runtime/core/text-impl.mjs +95 -0
- package/runtime/core/text-impl.mjs.map +1 -0
- package/runtime/core/text.mjs +15 -0
- package/runtime/core/transducer.mjs +668 -0
- package/runtime/core/transient-internals.mjs +38 -0
- package/runtime/core/transient.mjs +53 -0
- package/runtime/core/truth.mjs +3 -0
- package/runtime/core/type.mjs +262 -0
- package/runtime/core/value-internals.mjs +188 -0
- package/runtime/core/value.mjs +93 -0
- package/runtime/core/vector-internals.mjs +102 -0
- package/runtime/core/vector.mjs +951 -0
- package/runtime/literals.mjs +34 -0
- package/runtime/source-mapping.mjs +220 -0
- package/runtime/testing/list.mjs +52 -0
- package/runtime/testing/map.mjs +145 -0
- package/runtime/testing/queue.mjs +49 -0
- package/runtime/testing/set.mjs +61 -0
- package/runtime/testing/sorted.mjs +105 -0
- package/runtime/testing/value.mjs +17 -0
- package/runtime/testing/vector.mjs +102 -0
- package/runtime/worker-value-codec.mjs +565 -0
- package/runtime/worker-value-stream.mjs +786 -0
- package/runtime/worker.mjs +975 -0
- package/stdlib/README.md +774 -0
- package/stdlib/bit.eli +36 -0
- package/stdlib/core/collection.eli +134 -0
- package/stdlib/core/data-text.eli +16 -0
- package/stdlib/core/data.eli +306 -0
- package/stdlib/core/identifier.eli +32 -0
- package/stdlib/core/lazy-sequence.eli +63 -0
- package/stdlib/core/metadata.eli +20 -0
- package/stdlib/core/object.eli +89 -0
- package/stdlib/core/order.eli +133 -0
- package/stdlib/core/protocol.eli +387 -0
- package/stdlib/core/record.eli +16 -0
- package/stdlib/core/regex.eli +17 -0
- package/stdlib/core/seq.eli +701 -0
- package/stdlib/core/set.eli +312 -0
- package/stdlib/core/sorted.eli +53 -0
- package/stdlib/core/text.eli +103 -0
- package/stdlib/core/transducer.eli +59 -0
- package/stdlib/core/transient.eli +24 -0
- package/stdlib/core/type.eli +19 -0
- package/stdlib/core/walk.eli +195 -0
- package/stdlib/core/zipper.eli +508 -0
- package/stdlib/data-text.eli +1092 -0
- package/stdlib/data.eli +42 -0
- package/stdlib/deferred.eli +70 -0
- package/stdlib/function.eli +120 -0
- package/stdlib/hierarchy.eli +245 -0
- package/stdlib/identifier.eli +147 -0
- package/stdlib/interop/js.eli +616 -0
- package/stdlib/json.eli +870 -0
- package/stdlib/metadata.eli +56 -0
- package/stdlib/multimethod.eli +464 -0
- package/stdlib/numeric.eli +193 -0
- package/stdlib/object.eli +96 -0
- package/stdlib/persistent-list.eli +145 -0
- package/stdlib/persistent-map.eli +724 -0
- package/stdlib/persistent-queue.eli +32 -0
- package/stdlib/persistent-set.eli +236 -0
- package/stdlib/persistent-vector.eli +342 -0
- package/stdlib/result.eli +141 -0
- package/stdlib/sequence.eli +106 -0
- package/stdlib/state/atom.eli +265 -0
- package/stdlib/text.eli +88 -0
- 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
|