373 lines
12 KiB
TypeScript
373 lines
12 KiB
TypeScript
import * as _ from "@preserves/core";
|
|
import * as _i_Actor from "../actor";
|
|
|
|
export const $__ = Symbol.for("_");
|
|
export const $and = Symbol.for("and");
|
|
export const $arr = Symbol.for("arr");
|
|
export const $bind = Symbol.for("bind");
|
|
export const $compound = Symbol.for("compound");
|
|
export const $dict = Symbol.for("dict");
|
|
export const $lit = Symbol.for("lit");
|
|
export const $not = Symbol.for("not");
|
|
export const $or = Symbol.for("or");
|
|
export const $rec = Symbol.for("rec");
|
|
export const $ref = Symbol.for("ref");
|
|
export const $resolve = Symbol.for("resolve");
|
|
export const $rewrite = Symbol.for("rewrite");
|
|
|
|
export const SturdyRef = _.Record.makeConstructor<{"oid": _val, "caveatChain": Array<Attenuation>, "sig": _.Bytes}, _ptr>()($ref, ["oid","caveatChain","sig"]);
|
|
|
|
export type SturdyRef = _.Record<(typeof $ref), [_val, Array<Attenuation>, _.Bytes], _ptr>;
|
|
|
|
export type Attenuation = Array<Caveat>;
|
|
|
|
export type Caveat = (Rewrite | Alts);
|
|
|
|
export const Rewrite = _.Record.makeConstructor<{"pattern": Pattern, "template": Template}, _ptr>()($rewrite, ["pattern","template"]);
|
|
|
|
export type Rewrite = _.Record<(typeof $rewrite), [Pattern, Template], _ptr>;
|
|
|
|
export const Alts = _.Record.makeConstructor<{"alternatives": Array<Rewrite>}, _ptr>()($or, ["alternatives"]);
|
|
|
|
export type Alts = _.Record<(typeof $or), [Array<Rewrite>], _ptr>;
|
|
|
|
export const Resolve = _.Record.makeConstructor<{"sturdyref": SturdyRef, "observer": _ptr}, _ptr>()($resolve, ["sturdyref","observer"]);
|
|
|
|
export type Resolve = _.Record<(typeof $resolve), [SturdyRef, _ptr], _ptr>;
|
|
|
|
export type ConstructorSpec = (CRec | CArr | CDict);
|
|
|
|
export const CRec = _.Record.makeConstructor<{"label": _val, "arity": number}, _ptr>()($rec, ["label","arity"]);
|
|
|
|
export type CRec = _.Record<(typeof $rec), [_val, number], _ptr>;
|
|
|
|
export const CArr = _.Record.makeConstructor<{"arity": number}, _ptr>()($arr, ["arity"]);
|
|
|
|
export type CArr = _.Record<(typeof $arr), [number], _ptr>;
|
|
|
|
export const CDict = _.Record.makeConstructor<{}, _ptr>()($dict, []);
|
|
|
|
export type CDict = _.Record<(typeof $dict), [], _ptr>;
|
|
|
|
export const Lit = _.Record.makeConstructor<{"value": _val}, _ptr>()($lit, ["value"]);
|
|
|
|
export type Lit = _.Record<(typeof $lit), [_val], _ptr>;
|
|
|
|
export type Pattern = (PDiscard | PBind | PAnd | PNot | Lit | PCompound);
|
|
|
|
export const PDiscard = _.Record.makeConstructor<{}, _ptr>()($__, []);
|
|
|
|
export type PDiscard = _.Record<(typeof $__), [], _ptr>;
|
|
|
|
export const PBind = _.Record.makeConstructor<{"name": symbol, "pattern": Pattern}, _ptr>()($bind, ["name","pattern"]);
|
|
|
|
export type PBind = _.Record<(typeof $bind), [symbol, Pattern], _ptr>;
|
|
|
|
export const PAnd = _.Record.makeConstructor<{"patterns": Array<Pattern>}, _ptr>()($and, ["patterns"]);
|
|
|
|
export type PAnd = _.Record<(typeof $and), [Array<Pattern>], _ptr>;
|
|
|
|
export const PNot = _.Record.makeConstructor<{"pattern": Pattern}, _ptr>()($not, ["pattern"]);
|
|
|
|
export type PNot = _.Record<(typeof $not), [Pattern], _ptr>;
|
|
|
|
export const PCompound = _.Record.makeConstructor<{"ctor": ConstructorSpec, "members": _.KeyedDictionary<_val, Pattern, _ptr>}, _ptr>()($compound, ["ctor","members"]);
|
|
|
|
export type PCompound = _.Record<
|
|
(typeof $compound),
|
|
[ConstructorSpec, _.KeyedDictionary<_val, Pattern, _ptr>],
|
|
_ptr
|
|
>;
|
|
|
|
export type Template = (TRef | Lit | TCompound);
|
|
|
|
export const TRef = _.Record.makeConstructor<{"name": symbol}, _ptr>()($ref, ["name"]);
|
|
|
|
export type TRef = _.Record<(typeof $ref), [symbol], _ptr>;
|
|
|
|
export const TCompound = _.Record.makeConstructor<{"ctor": ConstructorSpec, "members": _.KeyedDictionary<_val, Template, _ptr>}, _ptr>()($compound, ["ctor","members"]);
|
|
|
|
export type TCompound = _.Record<
|
|
(typeof $compound),
|
|
[ConstructorSpec, _.KeyedDictionary<_val, Template, _ptr>],
|
|
_ptr
|
|
>;
|
|
|
|
export type _ptr = _i_Actor.Ref;
|
|
|
|
export type _val = _.Value<_ptr>;
|
|
|
|
|
|
export function isSturdyRef(v: any): v is SturdyRef {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $ref) &&
|
|
(
|
|
(v.length === 3) &&
|
|
true &&
|
|
(
|
|
_.Array.isArray(v[1]) &&
|
|
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[1]) &&
|
|
(v[1].length >= 0) &&
|
|
v[1].slice(0).every(v => (isAttenuation(v)))
|
|
) &&
|
|
_.Bytes.isBytes(v[2])
|
|
)
|
|
);
|
|
}
|
|
|
|
export function asSturdyRef(v: any): SturdyRef {
|
|
if (!isSturdyRef(v)) {throw new TypeError(`Invalid SturdyRef: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isAttenuation(v: any): v is Attenuation {
|
|
return (
|
|
_.Array.isArray(v) &&
|
|
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
(v.length >= 0) &&
|
|
v.slice(0).every(v => (isCaveat(v)))
|
|
);
|
|
}
|
|
|
|
export function asAttenuation(v: any): Attenuation {
|
|
if (!isAttenuation(v)) {throw new TypeError(`Invalid Attenuation: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isCaveat(v: any): v is Caveat {return (isRewrite(v) || isAlts(v));}
|
|
|
|
export function asCaveat(v: any): Caveat {
|
|
if (!isCaveat(v)) {throw new TypeError(`Invalid Caveat: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isRewrite(v: any): v is Rewrite {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $rewrite) &&
|
|
((v.length === 2) && isPattern(v[0]) && isTemplate(v[1]))
|
|
);
|
|
}
|
|
|
|
export function asRewrite(v: any): Rewrite {
|
|
if (!isRewrite(v)) {throw new TypeError(`Invalid Rewrite: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isAlts(v: any): v is Alts {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $or) &&
|
|
(
|
|
(v.length === 1) &&
|
|
(
|
|
_.Array.isArray(v[0]) &&
|
|
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) &&
|
|
(v[0].length >= 0) &&
|
|
v[0].slice(0).every(v => (isRewrite(v)))
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
export function asAlts(v: any): Alts {
|
|
if (!isAlts(v)) {throw new TypeError(`Invalid Alts: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isResolve(v: any): v is Resolve {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $resolve) &&
|
|
((v.length === 2) && isSturdyRef(v[0]) && _.isPointer(v[1]))
|
|
);
|
|
}
|
|
|
|
export function asResolve(v: any): Resolve {
|
|
if (!isResolve(v)) {throw new TypeError(`Invalid Resolve: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isConstructorSpec(v: any): v is ConstructorSpec {return (isCRec(v) || isCArr(v) || isCDict(v));}
|
|
|
|
export function asConstructorSpec(v: any): ConstructorSpec {
|
|
if (!isConstructorSpec(v)) {throw new TypeError(`Invalid ConstructorSpec: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isCRec(v: any): v is CRec {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $rec) &&
|
|
((v.length === 2) && true && typeof v[1] === 'number')
|
|
);
|
|
}
|
|
|
|
export function asCRec(v: any): CRec {
|
|
if (!isCRec(v)) {throw new TypeError(`Invalid CRec: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isCArr(v: any): v is CArr {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $arr) &&
|
|
((v.length === 1) && typeof v[0] === 'number')
|
|
);
|
|
}
|
|
|
|
export function asCArr(v: any): CArr {
|
|
if (!isCArr(v)) {throw new TypeError(`Invalid CArr: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isCDict(v: any): v is CDict {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $dict) &&
|
|
((v.length === 0))
|
|
);
|
|
}
|
|
|
|
export function asCDict(v: any): CDict {
|
|
if (!isCDict(v)) {throw new TypeError(`Invalid CDict: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isLit(v: any): v is Lit {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $lit) &&
|
|
((v.length === 1) && true)
|
|
);
|
|
}
|
|
|
|
export function asLit(v: any): Lit {
|
|
if (!isLit(v)) {throw new TypeError(`Invalid Lit: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isPattern(v: any): v is Pattern {
|
|
return (
|
|
isPDiscard(v) ||
|
|
isPBind(v) ||
|
|
isPAnd(v) ||
|
|
isPNot(v) ||
|
|
isLit(v) ||
|
|
isPCompound(v)
|
|
);
|
|
}
|
|
|
|
export function asPattern(v: any): Pattern {
|
|
if (!isPattern(v)) {throw new TypeError(`Invalid Pattern: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isPDiscard(v: any): v is PDiscard {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $__) &&
|
|
((v.length === 0))
|
|
);
|
|
}
|
|
|
|
export function asPDiscard(v: any): PDiscard {
|
|
if (!isPDiscard(v)) {throw new TypeError(`Invalid PDiscard: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isPBind(v: any): v is PBind {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $bind) &&
|
|
((v.length === 2) && typeof v[0] === 'symbol' && isPattern(v[1]))
|
|
);
|
|
}
|
|
|
|
export function asPBind(v: any): PBind {
|
|
if (!isPBind(v)) {throw new TypeError(`Invalid PBind: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isPAnd(v: any): v is PAnd {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $and) &&
|
|
(
|
|
(v.length === 1) &&
|
|
(
|
|
_.Array.isArray(v[0]) &&
|
|
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) &&
|
|
(v[0].length >= 0) &&
|
|
v[0].slice(0).every(v => (isPattern(v)))
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
export function asPAnd(v: any): PAnd {
|
|
if (!isPAnd(v)) {throw new TypeError(`Invalid PAnd: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isPNot(v: any): v is PNot {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $not) &&
|
|
((v.length === 1) && isPattern(v[0]))
|
|
);
|
|
}
|
|
|
|
export function asPNot(v: any): PNot {
|
|
if (!isPNot(v)) {throw new TypeError(`Invalid PNot: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isPCompound(v: any): v is PCompound {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $compound) &&
|
|
(
|
|
(v.length === 2) &&
|
|
isConstructorSpec(v[0]) &&
|
|
(
|
|
_.Dictionary.isDictionary<_val, _ptr>(v[1]) &&
|
|
((() => {
|
|
for (const e of v[1]) {if (!(true)) return false; if (!(isPattern(e[1]))) return false;};
|
|
return true;
|
|
})())
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
export function asPCompound(v: any): PCompound {
|
|
if (!isPCompound(v)) {throw new TypeError(`Invalid PCompound: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isTemplate(v: any): v is Template {return (isTRef(v) || isLit(v) || isTCompound(v));}
|
|
|
|
export function asTemplate(v: any): Template {
|
|
if (!isTemplate(v)) {throw new TypeError(`Invalid Template: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isTRef(v: any): v is TRef {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $ref) &&
|
|
((v.length === 1) && typeof v[0] === 'symbol')
|
|
);
|
|
}
|
|
|
|
export function asTRef(v: any): TRef {
|
|
if (!isTRef(v)) {throw new TypeError(`Invalid TRef: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isTCompound(v: any): v is TCompound {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $compound) &&
|
|
(
|
|
(v.length === 2) &&
|
|
isConstructorSpec(v[0]) &&
|
|
(
|
|
_.Dictionary.isDictionary<_val, _ptr>(v[1]) &&
|
|
((() => {
|
|
for (const e of v[1]) {if (!(true)) return false; if (!(isTemplate(e[1]))) return false;};
|
|
return true;
|
|
})())
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
export function asTCompound(v: any): TCompound {
|
|
if (!isTCompound(v)) {throw new TypeError(`Invalid TCompound: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|