// Loaded from https://deno.land/x/dnit@dnit-v1.11.0/adl-gen/runtime/adl.ts import type * as AST from "./sys/adlast.ts"; import type * as utils from "./utils.ts"; export type ScopedName = AST.ScopedName; export type ScopedDecl = AST.ScopedDecl; export type ATypeRef<_T> = {value: AST.TypeRef}; export type ATypeExpr<_T> = {value : AST.TypeExpr}; /** * A function to obtain details on a declared type. */ export interface DeclResolver { (decl : AST.ScopedName): AST.ScopedDecl; }; export function declResolver(...astMaps : ({[key:string] : AST.ScopedDecl})[]) { const astMap : {[key:string] : AST.ScopedDecl} = {}; for (let map of astMaps) { for (let scopedName in map) { astMap[scopedName] = map[scopedName]; } } function resolver(scopedName : AST.ScopedName) : AST.ScopedDecl { const scopedNameStr = scopedName.moduleName + "." + scopedName.name; const result = astMap[scopedNameStr]; if (result === undefined) { throw new Error("Unable to resolve ADL type " + scopedNameStr); } return result; } return resolver; } type Unknown = {} | null; type Json = {} | null; /* Type expressions for primitive types */ function texprPrimitive(ptype: string): ATypeExpr { return { value: { typeRef: { kind: "primitive", value: ptype }, parameters: [] } }; }; function texprPrimitive1(ptype: string, etype: ATypeExpr): ATypeExpr { return { value: { typeRef: { kind: "primitive", value: ptype }, parameters: [etype.value] } }; }; export function texprVoid() : ATypeExpr {return texprPrimitive("Void");} export function texprBool() : ATypeExpr {return texprPrimitive("Bool");} export function texprInt8() : ATypeExpr {return texprPrimitive("Int8");} export function texprInt16() : ATypeExpr {return texprPrimitive("Int16");} export function texprInt32() : ATypeExpr {return texprPrimitive("Int32");} export function texprInt64() : ATypeExpr {return texprPrimitive("Int64");} export function texprWord8() : ATypeExpr {return texprPrimitive("Word8");} export function texprWord16() : ATypeExpr {return texprPrimitive("Word16");} export function texprWord32() : ATypeExpr {return texprPrimitive("Word32");} export function texprWord64() : ATypeExpr {return texprPrimitive("Word64");} export function texprFloat() : ATypeExpr {return texprPrimitive("Float");} export function texprDouble() : ATypeExpr {return texprPrimitive("Double");} export function texprJson() : ATypeExpr {return texprPrimitive("Json");} export function texprByteVector() : ATypeExpr {return texprPrimitive("ByteVector");} export function texprString() : ATypeExpr {return texprPrimitive("String");} export function texprVector(etype: ATypeExpr) : ATypeExpr { return texprPrimitive1("Vector", etype); } export function texprStringMap(etype: ATypeExpr) : ATypeExpr<{[key:string]:T}> { return texprPrimitive1("StringMap", etype); } export function texprNullable(etype: ATypeExpr) : ATypeExpr { return texprPrimitive1("Nullable", etype); } // "Flavoured" nominal typing. // https://spin.atomicobject.com/2018/01/15/typescript-flexible-nominal-typing/ export type Flavored0 = utils.Flavored0; export type Flavored1 = utils.Flavored1; export type Flavored2 = utils.Flavored2; export type Flavored3 = utils.Flavored3;