@here.build/arrival-syntax 0.0.0-stage → 0.10.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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ # MIT License
2
+
3
+ Copyright (c) 2026 here.build
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,26 @@
1
- # Temporary Holding Version
1
+ # @here.build/arrival-syntax
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Span-preserving Scheme s-expression forest. This is the syntax tree, not the evaluator.
4
+
5
+ `parseSexprs` answers “what did the user write, and where?” Arrival’s `Parser` answers “what does this program mean?” — it expands reader macros into `APair` / `AVector` / interned `ASymbol` and drops comments. Editors, the type-lens, and the sugarcoat round-trip need the first tree.
6
+
7
+ ```ts
8
+ import { parseSexprs } from "@here.build/arrival-syntax";
9
+
10
+ parseSexprs("(map f xs) ; trailing");
11
+ // [{ list: [{ atom: "map" }, { atom: "f" }, { atom: "xs" }], span: [0, 10], trail: ["; trailing"] }]
12
+ ```
13
+
14
+ `[]` and `{}` are containers (`open: "[" | "{"`), not atom glue. Racket `#:limit` mints as `:limit`. A `;` comment on its own line before a datum is that datum’s `lead`; one on the same line after is its `trail`.
15
+
16
+ `@here.build/arrival-sugarcoat` re-exports `parseSexprs` / `Node` and owns the Scheme↔sugarcoat lens on top. `@here.build/arrival`’s type-layer and `@here.build/arrival-types-bridge` import this package directly.
17
+
18
+ ## Install
19
+
20
+ ```bash
21
+ pnpm add @here.build/arrival-syntax
22
+ ```
23
+
24
+ ## License
25
+
26
+ [MIT](./LICENSE.md).
@@ -0,0 +1,2 @@
1
+ export { parseSexprs, type ListOpen, type Node } from "./parse.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,KAAK,QAAQ,EAAE,KAAK,IAAI,EAAE,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ // @here.build/arrival-syntax — span-preserving Scheme s-expression forest.
2
+ // Leaf: parseSexprs + Node. Not the evaluator reader; not the sugarcoat lens.
3
+ export { parseSexprs } from "./parse.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,8EAA8E;AAE9E,OAAO,EAAE,WAAW,EAA4B,MAAM,YAAY,CAAC","sourcesContent":["// @here.build/arrival-syntax — span-preserving Scheme s-expression forest.\n// Leaf: parseSexprs + Node. Not the evaluator reader; not the sugarcoat lens.\n\nexport { parseSexprs, type ListOpen, type Node } from \"./parse.js\";\n"]}
@@ -0,0 +1,19 @@
1
+ /** Opener that minted a list node. Stamped by parseSexprs; sugarcoat's
2
+ * `normalizePolyglot` strips it after recovering free `[]`/`{}` surfaces.
3
+ * Absent/`"("` means a plain paren list. */
4
+ export type ListOpen = "(" | "[" | "{";
5
+ export type Node = {
6
+ atom: string;
7
+ str?: boolean;
8
+ lead?: string[];
9
+ trail?: string[];
10
+ span?: readonly [start: number, end: number];
11
+ } | {
12
+ list: Node[];
13
+ open?: ListOpen;
14
+ lead?: string[];
15
+ trail?: string[];
16
+ span?: readonly [start: number, end: number];
17
+ };
18
+ export declare function parseSexprs(src: string): Node[];
19
+ //# sourceMappingURL=parse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAeA;;6CAE6C;AAC7C,MAAM,MAAM,QAAQ,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;AAEvC,MAAM,MAAM,IAAI,GACZ;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,OAAO,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IAAC,IAAI,CAAC,EAAE,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,CAAA;CAAE,GAChH;IACE,IAAI,EAAE,IAAI,EAAE,CAAC;IACb,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,IAAI,CAAC,EAAE,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;CAC9C,CAAC;AAEN,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,EAAE,CAiL/C"}
package/dist/parse.js ADDED
@@ -0,0 +1,197 @@
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
+ export function parseSexprs(src) {
16
+ let i = 0;
17
+ const n = src.length;
18
+ // `{`/`}` are delimiters so free dict/n-expr surfaces parse as containers (not atom glue).
19
+ const isDelim = (c) => c === undefined ||
20
+ /\s/.test(c) ||
21
+ c === "(" ||
22
+ c === ")" ||
23
+ c === "[" ||
24
+ c === "]" ||
25
+ c === "{" ||
26
+ c === "}" ||
27
+ c === '"' ||
28
+ c === ";";
29
+ let pendingLead = [];
30
+ let lastNode = null;
31
+ let sawNewlineSinceNode = false;
32
+ const skipWs = () => {
33
+ while (i < n) {
34
+ const c = src[i];
35
+ if (c === "\n") {
36
+ sawNewlineSinceNode = true;
37
+ i++;
38
+ continue;
39
+ }
40
+ if (/\s/.test(c)) {
41
+ i++;
42
+ continue;
43
+ }
44
+ if (c === ";") {
45
+ const start = i;
46
+ while (i < n && src[i] !== "\n")
47
+ i++;
48
+ const text = src.slice(start, i).replace(/\s+$/, "");
49
+ // same line as the just-read datum → its trailing comment; else leading.
50
+ if (!sawNewlineSinceNode && lastNode)
51
+ (lastNode.trail ??= []).push(text);
52
+ else
53
+ pendingLead.push(text);
54
+ continue;
55
+ }
56
+ break;
57
+ }
58
+ };
59
+ const readString = () => {
60
+ i++; // opening quote
61
+ let out = "";
62
+ while (i < n) {
63
+ const c = src[i];
64
+ if (c === "\\") {
65
+ out += src[i] + (src[i + 1] ?? "");
66
+ i += 2;
67
+ continue;
68
+ }
69
+ if (c === '"') {
70
+ i++;
71
+ return { atom: out, str: true };
72
+ }
73
+ out += c;
74
+ i++;
75
+ }
76
+ invariant(false, "unterminated string");
77
+ };
78
+ const readDatum = () => {
79
+ skipWs();
80
+ const lead = pendingLead;
81
+ pendingLead = [];
82
+ const start = i; // datum's first char (after lead-comment/whitespace skip)
83
+ const c = src[i];
84
+ invariant(c !== undefined, "unexpected EOF");
85
+ let node;
86
+ if (c === "#" && src[i + 1] === "\\") {
87
+ // `#\<char>` character literal (R7RS 7.1.1): self-delimiting — a single
88
+ // non-alphabetic payload (`#\"`, `#\\`, `#\(`, `#\;`, `#\ `, ...) always
89
+ // consumes EXACTLY one more character regardless of what follows; an
90
+ // alphabetic payload can extend into a named literal (`#\space`,
91
+ // `#\newline`, `#\x41`, ...), so consume the whole run. Without this,
92
+ // the payload char falls through to the generic atom scan below, which
93
+ // stops at `"`/`(`/`)`/`[`/`]`/`;` — e.g. `#\"` reads as the 2-char atom
94
+ // `#\` and then hands the bare `"` to `readString`, which then swallows
95
+ // everything up to the NEXT quote in the source (or to EOF, throwing
96
+ // "unterminated string") instead of the one intended character literal.
97
+ const charStart = i;
98
+ i += 2;
99
+ if (i < n && /[a-z]/i.test(src[i])) {
100
+ while (i < n && /[a-z0-9]/i.test(src[i]))
101
+ i++;
102
+ }
103
+ else if (i < n) {
104
+ i++;
105
+ }
106
+ node = { atom: src.slice(charStart, i) };
107
+ if (lead.length > 0)
108
+ node.lead = lead;
109
+ node.span = [start, i];
110
+ lastNode = node;
111
+ sawNewlineSinceNode = false;
112
+ return node;
113
+ }
114
+ switch (c) {
115
+ case "(":
116
+ case "[":
117
+ case "{": {
118
+ node = readList(c);
119
+ break;
120
+ }
121
+ case ")":
122
+ case "]":
123
+ case "}": {
124
+ invariant(false, () => `unexpected ${c} at ${i}`);
125
+ break;
126
+ }
127
+ case '"': {
128
+ node = readString();
129
+ break;
130
+ }
131
+ case "'": {
132
+ i++;
133
+ node = { list: [{ atom: "quote" }, readDatum()] };
134
+ break;
135
+ }
136
+ case "`": {
137
+ i++;
138
+ node = { list: [{ atom: "quasiquote" }, readDatum()] };
139
+ break;
140
+ }
141
+ case ",": {
142
+ i++;
143
+ if (src[i] === "@") {
144
+ i++;
145
+ node = { list: [{ atom: "unquote-splicing" }, readDatum()] };
146
+ }
147
+ else
148
+ node = { list: [{ atom: "unquote" }, readDatum()] };
149
+ break;
150
+ }
151
+ default: {
152
+ const start = i;
153
+ while (i < n && !isDelim(src[i]))
154
+ i++;
155
+ // Racket `#:limit` ≡ arrival `:limit` (same mint as ASymbol keywords).
156
+ let name = src.slice(start, i);
157
+ if (name.length > 2 && name.startsWith("#:"))
158
+ name = `:${name.slice(2)}`;
159
+ node = { atom: name };
160
+ }
161
+ }
162
+ if (lead.length > 0)
163
+ node.lead = lead;
164
+ // Source span [start, end) in `src` — inert metadata (like lead/trail), consumed
165
+ // by the editor's parameter-hint placement and the type-layer span map.
166
+ node.span = [start, i];
167
+ lastNode = node;
168
+ sawNewlineSinceNode = false;
169
+ return node;
170
+ };
171
+ function readList(open) {
172
+ i++; // open
173
+ const items = [];
174
+ for (;;) {
175
+ skipWs();
176
+ const c = src[i];
177
+ invariant(c !== undefined, "unbalanced list");
178
+ if (c === ")" || c === "]" || c === "}") {
179
+ // Mismatched closer still consumes (the reader was permissive on ) vs ]); keep that.
180
+ i++;
181
+ break;
182
+ }
183
+ items.push(readDatum());
184
+ }
185
+ // Only stamp non-paren openers — paren is the default Scheme list.
186
+ return open === "(" ? { list: items } : { list: items, open };
187
+ }
188
+ const forms = [];
189
+ for (;;) {
190
+ skipWs();
191
+ if (i >= n)
192
+ break;
193
+ forms.push(readDatum());
194
+ }
195
+ return forms;
196
+ }
197
+ //# sourceMappingURL=parse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,SAAS,MAAM,gBAAgB,CA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C;IAChE,CAAC;IAED,MAAM,KAAK,GAAW,EAAE,CAAC;IACzB,SAAS,CAAC;QACR,MAAM,EAAE,CAAC;QACT,IAAI,CAAC,IAAI,CAAC;YAAE,MAAM;QAClB,KAAK,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC;IAC1B,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["/**\n * Scheme s-expression forest: source text → plain trees with comments and spans.\n *\n * This is not Arrival’s evaluator `Parser`. That reader mints `APair` / `AVector` /\n * interned `ASymbol`, expands `[…]`/`{…}` into values, and drops comments. This\n * parser keeps the written surface: `[]`/`{}` as containers (`open`), lead/trail\n * comments, and a byte span on every node — so an editor or type-lens can map\n * back onto the buffer.\n *\n * A `;`-line-comment on its OWN line(s) before a datum becomes that datum's\n * `lead`; one on the SAME line just after a datum is its `trail`. (A dangling\n * comment before a `)` with no following datum is dropped.)\n */\nimport invariant from \"tiny-invariant\";\n\n/** Opener that minted a list node. Stamped by parseSexprs; sugarcoat's\n * `normalizePolyglot` strips it after recovering free `[]`/`{}` surfaces.\n * Absent/`\"(\"` means a plain paren list. */\nexport type ListOpen = \"(\" | \"[\" | \"{\";\n\nexport type Node =\n | { atom: string; str?: boolean; lead?: string[]; trail?: string[]; span?: readonly [start: number, end: number] }\n | {\n list: Node[];\n open?: ListOpen;\n lead?: string[];\n trail?: string[];\n span?: readonly [start: number, end: number];\n };\n\nexport function parseSexprs(src: string): Node[] {\n let i = 0;\n const n = src.length;\n // `{`/`}` are delimiters so free dict/n-expr surfaces parse as containers (not atom glue).\n const isDelim = (c: string | undefined) =>\n c === undefined ||\n /\\s/.test(c) ||\n c === \"(\" ||\n c === \")\" ||\n c === \"[\" ||\n c === \"]\" ||\n c === \"{\" ||\n c === \"}\" ||\n c === '\"' ||\n c === \";\";\n\n let pendingLead: string[] = [];\n let lastNode: Node | null = null;\n let sawNewlineSinceNode = false;\n\n const skipWs = () => {\n while (i < n) {\n const c = src[i];\n if (c === \"\\n\") {\n sawNewlineSinceNode = true;\n i++;\n continue;\n }\n if (/\\s/.test(c)) {\n i++;\n continue;\n }\n if (c === \";\") {\n const start = i;\n while (i < n && src[i] !== \"\\n\") i++;\n const text = src.slice(start, i).replace(/\\s+$/, \"\");\n // same line as the just-read datum → its trailing comment; else leading.\n if (!sawNewlineSinceNode && lastNode) (lastNode.trail ??= []).push(text);\n else pendingLead.push(text);\n continue;\n }\n break;\n }\n };\n\n const readString = (): Node => {\n i++; // opening quote\n let out = \"\";\n while (i < n) {\n const c = src[i];\n if (c === \"\\\\\") {\n out += src[i] + (src[i + 1] ?? \"\");\n i += 2;\n continue;\n }\n if (c === '\"') {\n i++;\n return { atom: out, str: true };\n }\n out += c;\n i++;\n }\n invariant(false, \"unterminated string\");\n };\n\n const readDatum = (): Node => {\n skipWs();\n const lead = pendingLead;\n pendingLead = [];\n const start = i; // datum's first char (after lead-comment/whitespace skip)\n const c = src[i];\n invariant(c !== undefined, \"unexpected EOF\");\n let node: Node;\n if (c === \"#\" && src[i + 1] === \"\\\\\") {\n // `#\\<char>` character literal (R7RS 7.1.1): self-delimiting — a single\n // non-alphabetic payload (`#\\\"`, `#\\\\`, `#\\(`, `#\\;`, `#\\ `, ...) always\n // consumes EXACTLY one more character regardless of what follows; an\n // alphabetic payload can extend into a named literal (`#\\space`,\n // `#\\newline`, `#\\x41`, ...), so consume the whole run. Without this,\n // the payload char falls through to the generic atom scan below, which\n // stops at `\"`/`(`/`)`/`[`/`]`/`;` — e.g. `#\\\"` reads as the 2-char atom\n // `#\\` and then hands the bare `\"` to `readString`, which then swallows\n // everything up to the NEXT quote in the source (or to EOF, throwing\n // \"unterminated string\") instead of the one intended character literal.\n const charStart = i;\n i += 2;\n if (i < n && /[a-z]/i.test(src[i])) {\n while (i < n && /[a-z0-9]/i.test(src[i])) i++;\n } else if (i < n) {\n i++;\n }\n node = { atom: src.slice(charStart, i) };\n if (lead.length > 0) node.lead = lead;\n node.span = [start, i];\n lastNode = node;\n sawNewlineSinceNode = false;\n return node;\n }\n switch (c) {\n case \"(\":\n case \"[\":\n case \"{\": {\n node = readList(c);\n break;\n }\n case \")\":\n case \"]\":\n case \"}\": {\n invariant(false, () => `unexpected ${c} at ${i}`);\n break;\n }\n case '\"': {\n node = readString();\n break;\n }\n case \"'\": {\n i++;\n node = { list: [{ atom: \"quote\" }, readDatum()] };\n break;\n }\n case \"`\": {\n i++;\n node = { list: [{ atom: \"quasiquote\" }, readDatum()] };\n break;\n }\n case \",\": {\n i++;\n if (src[i] === \"@\") {\n i++;\n node = { list: [{ atom: \"unquote-splicing\" }, readDatum()] };\n } else node = { list: [{ atom: \"unquote\" }, readDatum()] };\n\n break;\n }\n default: {\n const start = i;\n while (i < n && !isDelim(src[i])) i++;\n // Racket `#:limit` ≡ arrival `:limit` (same mint as ASymbol keywords).\n let name = src.slice(start, i);\n if (name.length > 2 && name.startsWith(\"#:\")) name = `:${name.slice(2)}`;\n node = { atom: name };\n }\n }\n if (lead.length > 0) node.lead = lead;\n // Source span [start, end) in `src` — inert metadata (like lead/trail), consumed\n // by the editor's parameter-hint placement and the type-layer span map.\n node.span = [start, i];\n lastNode = node;\n sawNewlineSinceNode = false;\n return node;\n };\n\n function readList(open: ListOpen): Node {\n i++; // open\n const items: Node[] = [];\n for (;;) {\n skipWs();\n const c = src[i];\n invariant(c !== undefined, \"unbalanced list\");\n if (c === \")\" || c === \"]\" || c === \"}\") {\n // Mismatched closer still consumes (the reader was permissive on ) vs ]); keep that.\n i++;\n break;\n }\n items.push(readDatum());\n }\n // Only stamp non-paren openers — paren is the default Scheme list.\n return open === \"(\" ? { list: items } : { list: items, open };\n }\n\n const forms: Node[] = [];\n for (;;) {\n skipWs();\n if (i >= n) break;\n forms.push(readDatum());\n }\n return forms;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,67 @@
1
1
  {
2
2
  "name": "@here.build/arrival-syntax",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.10.0",
4
+ "description": "Span-preserving Scheme s-expression forest for Arrival — parseSexprs, not the evaluator reader",
5
+ "keywords": [
6
+ "arrival",
7
+ "s-expressions",
8
+ "scheme",
9
+ "lisp",
10
+ "parser"
11
+ ],
12
+ "homepage": "https://github.com/here-build/arrival#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/here-build/arrival/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/here-build/arrival.git",
19
+ "directory": "packages/arrival-syntax"
20
+ },
21
+ "license": "MIT",
22
+ "author": "here.build",
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "type": "module",
27
+ "sideEffects": false,
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js"
32
+ }
33
+ },
34
+ "main": "./dist/index.js",
35
+ "types": "./dist/index.d.ts",
36
+ "files": [
37
+ "dist",
38
+ "src",
39
+ "!**/__tests__",
40
+ "!**/__research__",
41
+ "!**/__benchmarks__",
42
+ "!**/__custdev__",
43
+ "!**/__visual__",
44
+ "!**/*.test.*",
45
+ "!**/*.stories.*"
46
+ ],
47
+ "dependencies": {
48
+ "tiny-invariant": "1.3.3"
49
+ },
50
+ "devDependencies": {
51
+ "@here.build/eslint-configs": "0.9.0",
52
+ "@here.build/tsconfig": "0.9.0",
53
+ "@types/node": "24.0.0",
54
+ "eslint": "9.39.5",
55
+ "typescript": "6.0.3",
56
+ "vitest": "4.1.11"
57
+ },
58
+ "scripts": {
59
+ "build": "tsc -p tsconfig.build.json",
60
+ "dev": "tsc -p tsconfig.build.json --watch",
61
+ "lint": "eslint .",
62
+ "lint:fix": "eslint . --fix",
63
+ "test": "vitest run",
64
+ "test:watch": "vitest",
65
+ "typecheck": "tsc --noEmit"
66
+ }
6
67
  }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ // @here.build/arrival-syntax — span-preserving Scheme s-expression forest.
2
+ // Leaf: parseSexprs + Node. Not the evaluator reader; not the sugarcoat lens.
3
+
4
+ export { parseSexprs, type ListOpen, type Node } from "./parse.js";
package/src/parse.ts ADDED
@@ -0,0 +1,208 @@
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
+ }