enso/app/gui2/parser-codegen/util.ts
Kaz Wesley 9fd1ab9092
Parser TS bindings (#7881)
Generate TS bindings and lazy deserialization for the parser types.

# Important Notes
- The new API is imported into `ffi.ts`, but not yet used.
- I have tested the generated code in isolation, but cannot commit tests as we are not currently able to load WASM modules when running in `vitest`.
2023-10-11 13:04:38 +00:00

92 lines
2.7 KiB
TypeScript

import * as changeCase from 'change-case'
import ts from 'typescript'
const tsf = ts.factory
// === Identifier utilities ===
export function toPascal(ident: string): string {
if (ident.includes('.')) throw new Error('toPascal cannot be applied to a namespaced name.')
return changeCase.pascalCase(ident)
}
export function toCamel(ident: string): string {
if (ident.includes('.')) throw new Error('toCamel cannot be applied to a namespaced name.')
return changeCase.camelCase(ident)
}
const RENAME = new Map([
// TS reserved words.
['constructor', 'ident'],
['type', 'typeNode'],
// Rename source references to reflect our usage:
// - In `Tree`s:
['spanLeftOffsetCodeOffsetUtf16', 'whitespaceStartInCodeParsed'],
['spanLeftOffsetCodeUtf16', 'whitespaceLengthInCodeParsed'],
['spanCodeLengthUtf16', 'childrenLengthInCodeParsed'],
// - In `Tokens`s:
['leftOffsetCodeOffsetUtf16', 'whitespaceStartInCodeBuffer'],
['leftOffsetCodeUtf16', 'whitespaceLengthInCodeBuffer'],
['codeUtf16', 'lengthInCodeBuffer'],
['codeOffsetUtf16', 'startInCodeBuffer'],
])
export function mapIdent(ident: string): string {
return RENAME.get(ident) ?? ident
}
export function namespacedName(name: string, namespace?: string): string {
if (namespace == null) {
return toPascal(name)
} else {
return toPascal(namespace) + '.' + toPascal(name)
}
}
// === AST utilities ===
export const modifiers = {
export: tsf.createModifier(ts.SyntaxKind.ExportKeyword),
const: tsf.createModifier(ts.SyntaxKind.ConstKeyword),
readonly: tsf.createModifier(ts.SyntaxKind.ReadonlyKeyword),
abstract: tsf.createModifier(ts.SyntaxKind.AbstractKeyword),
static: tsf.createModifier(ts.SyntaxKind.StaticKeyword),
protected: tsf.createModifier(ts.SyntaxKind.ProtectedKeyword),
} as const
export function assignmentStatement(left: ts.Expression, right: ts.Expression): ts.Statement {
return tsf.createExpressionStatement(
tsf.createBinaryExpression(left, ts.SyntaxKind.EqualsToken, right),
)
}
export function forwardToSuper(
ident: ts.Identifier,
type: ts.TypeNode,
modifiers?: ts.ModifierLike[],
) {
return tsf.createConstructorDeclaration(
modifiers,
[tsf.createParameterDeclaration([], undefined, ident, undefined, type, undefined)],
tsf.createBlock([
tsf.createExpressionStatement(
tsf.createCallExpression(tsf.createIdentifier('super'), [], [ident]),
),
]),
)
}
export function casesOrThrow(cases: ts.CaseClause[], error: string): ts.CaseBlock {
return tsf.createCaseBlock([
...cases,
tsf.createDefaultClause([
tsf.createThrowStatement(
tsf.createNewExpression(
tsf.createIdentifier('Error'),
[],
[tsf.createStringLiteral(error)],
),
),
]),
])
}