@here.build/arrival-syntax 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/parse.ts DELETED
@@ -1,208 +0,0 @@
1
- /**
2
- * Scheme s-expression forest: source text → plain trees with comments and spans.
3
- *
4
- * This is not Arrival’s evaluator `Parser`. That reader mints `APair` / `AVector` /
5
- * interned `ASymbol`, expands `[…]`/`{…}` into values, and drops comments. This
6
- * parser keeps the written surface: `[]`/`{}` as containers (`open`), lead/trail
7
- * comments, and a byte span on every node — so an editor or type-lens can map
8
- * back onto the buffer.
9
- *
10
- * A `;`-line-comment on its OWN line(s) before a datum becomes that datum's
11
- * `lead`; one on the SAME line just after a datum is its `trail`. (A dangling
12
- * comment before a `)` with no following datum is dropped.)
13
- */
14
- import invariant from "tiny-invariant";
15
-
16
- /** Opener that minted a list node. Stamped by parseSexprs; sugarcoat's
17
- * `normalizePolyglot` strips it after recovering free `[]`/`{}` surfaces.
18
- * Absent/`"("` means a plain paren list. */
19
- export type ListOpen = "(" | "[" | "{";
20
-
21
- export type Node =
22
- | { atom: string; str?: boolean; lead?: string[]; trail?: string[]; span?: readonly [start: number, end: number] }
23
- | {
24
- list: Node[];
25
- open?: ListOpen;
26
- lead?: string[];
27
- trail?: string[];
28
- span?: readonly [start: number, end: number];
29
- };
30
-
31
- export function parseSexprs(src: string): Node[] {
32
- let i = 0;
33
- const n = src.length;
34
- // `{`/`}` are delimiters so free dict/n-expr surfaces parse as containers (not atom glue).
35
- const isDelim = (c: string | undefined) =>
36
- c === undefined ||
37
- /\s/.test(c) ||
38
- c === "(" ||
39
- c === ")" ||
40
- c === "[" ||
41
- c === "]" ||
42
- c === "{" ||
43
- c === "}" ||
44
- c === '"' ||
45
- c === ";";
46
-
47
- let pendingLead: string[] = [];
48
- let lastNode: Node | null = null;
49
- let sawNewlineSinceNode = false;
50
-
51
- const skipWs = () => {
52
- while (i < n) {
53
- const c = src[i];
54
- if (c === "\n") {
55
- sawNewlineSinceNode = true;
56
- i++;
57
- continue;
58
- }
59
- if (/\s/.test(c)) {
60
- i++;
61
- continue;
62
- }
63
- if (c === ";") {
64
- const start = i;
65
- while (i < n && src[i] !== "\n") i++;
66
- const text = src.slice(start, i).replace(/\s+$/, "");
67
- // same line as the just-read datum → its trailing comment; else leading.
68
- if (!sawNewlineSinceNode && lastNode) (lastNode.trail ??= []).push(text);
69
- else pendingLead.push(text);
70
- continue;
71
- }
72
- break;
73
- }
74
- };
75
-
76
- const readString = (): Node => {
77
- i++; // opening quote
78
- let out = "";
79
- while (i < n) {
80
- const c = src[i];
81
- if (c === "\\") {
82
- out += src[i] + (src[i + 1] ?? "");
83
- i += 2;
84
- continue;
85
- }
86
- if (c === '"') {
87
- i++;
88
- return { atom: out, str: true };
89
- }
90
- out += c;
91
- i++;
92
- }
93
- invariant(false, "unterminated string");
94
- };
95
-
96
- const readDatum = (): Node => {
97
- skipWs();
98
- const lead = pendingLead;
99
- pendingLead = [];
100
- const start = i; // datum's first char (after lead-comment/whitespace skip)
101
- const c = src[i];
102
- invariant(c !== undefined, "unexpected EOF");
103
- let node: Node;
104
- if (c === "#" && src[i + 1] === "\\") {
105
- // `#\<char>` character literal (R7RS 7.1.1): self-delimiting — a single
106
- // non-alphabetic payload (`#\"`, `#\\`, `#\(`, `#\;`, `#\ `, ...) always
107
- // consumes EXACTLY one more character regardless of what follows; an
108
- // alphabetic payload can extend into a named literal (`#\space`,
109
- // `#\newline`, `#\x41`, ...), so consume the whole run. Without this,
110
- // the payload char falls through to the generic atom scan below, which
111
- // stops at `"`/`(`/`)`/`[`/`]`/`;` — e.g. `#\"` reads as the 2-char atom
112
- // `#\` and then hands the bare `"` to `readString`, which then swallows
113
- // everything up to the NEXT quote in the source (or to EOF, throwing
114
- // "unterminated string") instead of the one intended character literal.
115
- const charStart = i;
116
- i += 2;
117
- if (i < n && /[a-z]/i.test(src[i])) {
118
- while (i < n && /[a-z0-9]/i.test(src[i])) i++;
119
- } else if (i < n) {
120
- i++;
121
- }
122
- node = { atom: src.slice(charStart, i) };
123
- if (lead.length > 0) node.lead = lead;
124
- node.span = [start, i];
125
- lastNode = node;
126
- sawNewlineSinceNode = false;
127
- return node;
128
- }
129
- switch (c) {
130
- case "(":
131
- case "[":
132
- case "{": {
133
- node = readList(c);
134
- break;
135
- }
136
- case ")":
137
- case "]":
138
- case "}": {
139
- invariant(false, () => `unexpected ${c} at ${i}`);
140
- break;
141
- }
142
- case '"': {
143
- node = readString();
144
- break;
145
- }
146
- case "'": {
147
- i++;
148
- node = { list: [{ atom: "quote" }, readDatum()] };
149
- break;
150
- }
151
- case "`": {
152
- i++;
153
- node = { list: [{ atom: "quasiquote" }, readDatum()] };
154
- break;
155
- }
156
- case ",": {
157
- i++;
158
- if (src[i] === "@") {
159
- i++;
160
- node = { list: [{ atom: "unquote-splicing" }, readDatum()] };
161
- } else node = { list: [{ atom: "unquote" }, readDatum()] };
162
-
163
- break;
164
- }
165
- default: {
166
- const start = i;
167
- while (i < n && !isDelim(src[i])) i++;
168
- // Racket `#:limit` ≡ arrival `:limit` (same mint as ASymbol keywords).
169
- let name = src.slice(start, i);
170
- if (name.length > 2 && name.startsWith("#:")) name = `:${name.slice(2)}`;
171
- node = { atom: name };
172
- }
173
- }
174
- if (lead.length > 0) node.lead = lead;
175
- // Source span [start, end) in `src` — inert metadata (like lead/trail), consumed
176
- // by the editor's parameter-hint placement and the type-layer span map.
177
- node.span = [start, i];
178
- lastNode = node;
179
- sawNewlineSinceNode = false;
180
- return node;
181
- };
182
-
183
- function readList(open: ListOpen): Node {
184
- i++; // open
185
- const items: Node[] = [];
186
- for (;;) {
187
- skipWs();
188
- const c = src[i];
189
- invariant(c !== undefined, "unbalanced list");
190
- if (c === ")" || c === "]" || c === "}") {
191
- // Mismatched closer still consumes (the reader was permissive on ) vs ]); keep that.
192
- i++;
193
- break;
194
- }
195
- items.push(readDatum());
196
- }
197
- // Only stamp non-paren openers — paren is the default Scheme list.
198
- return open === "(" ? { list: items } : { list: items, open };
199
- }
200
-
201
- const forms: Node[] = [];
202
- for (;;) {
203
- skipWs();
204
- if (i >= n) break;
205
- forms.push(readDatum());
206
- }
207
- return forms;
208
- }