Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Hsiwe committed Nov 5, 2023
1 parent f6a0bdf commit 3874f6c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
24 changes: 13 additions & 11 deletions dev/test/astUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { describe, it} from "mocha";
import type { astToString } from "../../src/parse/ast/utils.js";
import { attest } from "../attest/main.js";
import { describe, it } from "mocha"
import type { astToString } from "../../src/parse/ast/utils.js"
import { attest } from "../attest/main.js"

describe('astToString', () => {
it('no parentheses if nested ast is an array', () => {
const t = {} as unknown as astToString<[["number", "[]"], "|", "number"]>
describe("astToString", () => {
it("no parentheses if nested ast is an array", () => {
const t = {} as unknown as astToString<
[["number", "[]"], "|", "number"]
>
attest(t).typed as "'number[]|number'"
});
it('parentheses if nested ast is an infix expression', () => {
})
it("parentheses if nested ast is an infix expression", () => {
const t = {} as unknown as astToString<[["0", "|", "1"], "|", "string"]>
attest(t).typed as "'(0|1)|string'"
});
})
it('defaults to "..." if input is bad', () => {
const t = {} as unknown as astToString<["0", "///", "1"]>
attest(t).typed as "'...'"
});
});
})
})
37 changes: 19 additions & 18 deletions src/parse/ast/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ import type { List, Literalable } from "../../utils/generics.js"
import type { Scanner } from "../string/shift/scanner.js"
import type { InfixExpression, PostfixExpression } from "./ast.js"

export type astToString<ast> = `'${astToStringRecurse<ast>}'`;

type astToStringRecurse<ast> =
ast extends PostfixExpression<infer operator, infer operand>
? operator extends "[]"
? `${groupAst<operand>}[]`
: never
export type astToString<ast> = `'${astToStringRecurse<ast>}'`

type astToStringRecurse<ast> = ast extends PostfixExpression<
infer operator,
infer operand
>
? operator extends "[]"
? `${groupAst<operand>}[]`
: never
: ast extends InfixExpression<infer operator, infer l, infer r>
? operator extends "&" | "|" | "%" | Scanner.Comparator
? `${groupAst<l>}${operator}${groupAst<r>}`
: never
? operator extends "&" | "|" | "%" | Scanner.Comparator
? `${groupAst<l>}${operator}${groupAst<r>}`
: never
: ast extends Literalable
? `${ast extends bigint ? `${ast}n` : ast}`
: "..."

? `${ast extends bigint ? `${ast}n` : ast}`
: "..."

type groupAst<ast> = ast extends List
? ast[1] extends "[]"
? astToStringRecurse<ast>
: `(${astToStringRecurse<ast>})`
: astToStringRecurse<ast>
type groupAst<ast> = ast extends List
? ast[1] extends "[]"
? astToStringRecurse<ast>
: `(${astToStringRecurse<ast>})`
: astToStringRecurse<ast>

0 comments on commit 3874f6c

Please sign in to comment.