|
1 | 1 | // SPDX-FileCopyrightText: 2026 MesTTo |
2 | 2 | // SPDX-License-Identifier: MIT |
| 3 | +import fc from "fast-check"; |
3 | 4 | import { describe, it, expect } from "vitest"; |
4 | | -import { parseAllSpanned } from "./cst"; |
| 5 | +import { parseAllSpanned, parseCst } from "./cst"; |
5 | 6 | import { standardTokenizer } from "./runner"; |
6 | | -import { format } from "./parser"; |
| 7 | +import { format, parseAll } from "./parser"; |
7 | 8 |
|
8 | 9 | describe("parseAllSpanned", () => { |
9 | 10 | it("records the span of a nested symbol precisely", () => { |
@@ -36,3 +37,98 @@ describe("parseAllSpanned", () => { |
36 | 37 | expect(src.slice(strNode.span.start, strNode.span.end)).toBe('"hi"'); |
37 | 38 | }); |
38 | 39 | }); |
| 40 | + |
| 41 | +describe("parseCst — editor CST", () => { |
| 42 | + it("collects line comments with their spans, out of the atom tree", () => { |
| 43 | + const src = "; hi\n(f 1) ; trailing\n(g 2)"; |
| 44 | + const cst = parseCst(src, standardTokenizer()); |
| 45 | + expect(cst.comments.map((c) => src.slice(c.span.start, c.span.end))).toEqual([ |
| 46 | + "; hi", |
| 47 | + "; trailing", |
| 48 | + ]); |
| 49 | + expect(cst.nodes.map((n) => format(n.atom))).toEqual(["(f 1)", "(g 2)"]); |
| 50 | + expect(cst.diagnostics).toEqual([]); |
| 51 | + }); |
| 52 | + |
| 53 | + it("marks a top-level bang and records the ! span separately from the form", () => { |
| 54 | + const src = "!(f 2)"; |
| 55 | + const [top] = parseCst(src, standardTokenizer()).nodes; |
| 56 | + expect(top!.bang).toBe(true); |
| 57 | + expect(src.slice(top!.bangSpan!.start, top!.bangSpan!.end)).toBe("!"); |
| 58 | + expect(src.slice(top!.span.start, top!.span.end)).toBe("(f 2)"); |
| 59 | + expect(format(top!.atom)).toBe("(f 2)"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("records open and close paren spans on an expression", () => { |
| 63 | + const src = "(a b)"; |
| 64 | + const [top] = parseCst(src, standardTokenizer()).nodes; |
| 65 | + expect(src.slice(top!.open!.start, top!.open!.end)).toBe("("); |
| 66 | + expect(src.slice(top!.close!.start, top!.close!.end)).toBe(")"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("classifies leaf kinds from the atom", () => { |
| 70 | + const [top] = parseCst('(f $x 42 "s" True)', standardTokenizer()).nodes; |
| 71 | + expect(top!.children!.map((c) => c.kind)).toEqual([ |
| 72 | + "symbol", |
| 73 | + "variable", |
| 74 | + "number", |
| 75 | + "string", |
| 76 | + "symbol", |
| 77 | + ]); |
| 78 | + }); |
| 79 | + |
| 80 | + it("recovers from an unclosed paren without throwing and closes at end of input", () => { |
| 81 | + const cst = parseCst("(f (g 1)", standardTokenizer()); |
| 82 | + expect(cst.diagnostics.map((d) => d.code)).toContain("syntax.unclosedDelimiter"); |
| 83 | + expect(format(cst.nodes[0]!.atom)).toBe("(f (g 1))"); |
| 84 | + expect(cst.nodes[0]!.close).toBeUndefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("recovers from an unexpected closing paren", () => { |
| 88 | + const cst = parseCst("(f 1)) (g 2)", standardTokenizer()); |
| 89 | + expect(cst.diagnostics.map((d) => d.code)).toContain("syntax.unexpectedClose"); |
| 90 | + expect(cst.nodes.map((n) => format(n.atom))).toEqual(["(f 1)", "(g 2)"]); |
| 91 | + }); |
| 92 | + |
| 93 | + it("recovers from an unterminated string", () => { |
| 94 | + const cst = parseCst('(f "oops)', standardTokenizer()); |
| 95 | + expect(cst.diagnostics.map((d) => d.code)).toContain("syntax.unterminatedString"); |
| 96 | + }); |
| 97 | + |
| 98 | + it("never throws on arbitrary input", () => { |
| 99 | + fc.assert( |
| 100 | + fc.property(fc.string(), (src) => { |
| 101 | + parseCst(src, standardTokenizer()); |
| 102 | + }), |
| 103 | + { numRuns: 2000 }, |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it("produces the same atoms and bang flags as parseAll on valid programs", () => { |
| 108 | + const leaf = fc.oneof( |
| 109 | + fc.constantFrom("foo", "bar", "f", "g", "map", "+", "-", "=", ":", "cons"), |
| 110 | + fc.constantFrom("$x", "$y", "$acc", "$xs"), |
| 111 | + fc.integer({ min: -999, max: 999 }).map(String), |
| 112 | + fc.constantFrom('"hi"', '"a b"', '"x"'), |
| 113 | + ); |
| 114 | + const { atomSrc } = fc.letrec((tie) => ({ |
| 115 | + atomSrc: fc.oneof({ maxDepth: 4 }, leaf, tie("exprSrc")), |
| 116 | + exprSrc: fc |
| 117 | + .array(tie("atomSrc"), { minLength: 0, maxLength: 4 }) |
| 118 | + .map((items) => `(${items.join(" ")})`), |
| 119 | + })); |
| 120 | + const programArb = fc |
| 121 | + .array(fc.tuple(fc.boolean(), atomSrc), { minLength: 1, maxLength: 6 }) |
| 122 | + .map((tops) => tops.map(([bang, s]) => (bang ? `!${s}` : s)).join("\n")); |
| 123 | + fc.assert( |
| 124 | + fc.property(programArb, (src) => { |
| 125 | + const cst = parseCst(src, standardTokenizer()); |
| 126 | + const plain = parseAll(src, standardTokenizer()); |
| 127 | + expect(cst.diagnostics).toEqual([]); |
| 128 | + expect(cst.nodes.map((n) => format(n.atom))).toEqual(plain.map((t) => format(t.atom))); |
| 129 | + expect(cst.nodes.map((n) => n.bang === true)).toEqual(plain.map((t) => t.bang)); |
| 130 | + }), |
| 131 | + { numRuns: 1000 }, |
| 132 | + ); |
| 133 | + }); |
| 134 | +}); |
0 commit comments