237 lines
7.3 KiB
TypeScript
237 lines
7.3 KiB
TypeScript
|
import * as _ from "@preserves/core";
|
||
|
|
||
|
export const $1 = 1;
|
||
|
export const $Boolean = Symbol.for("Boolean");
|
||
|
export const $ByteString = Symbol.for("ByteString");
|
||
|
export const $Double = Symbol.for("Double");
|
||
|
export const $Float = Symbol.for("Float");
|
||
|
export const $SignedInteger = Symbol.for("SignedInteger");
|
||
|
export const $String = Symbol.for("String");
|
||
|
export const $Symbol = Symbol.for("Symbol");
|
||
|
export const $and = Symbol.for("and");
|
||
|
export const $atom = Symbol.for("atom");
|
||
|
export const $definitions = Symbol.for("definitions");
|
||
|
export const $dict = Symbol.for("dict");
|
||
|
export const $dictof = Symbol.for("dictof");
|
||
|
export const $lit = Symbol.for("lit");
|
||
|
export const $named = Symbol.for("named");
|
||
|
export const $or = Symbol.for("or");
|
||
|
export const $pointer = Symbol.for("pointer");
|
||
|
export const $rec = Symbol.for("rec");
|
||
|
export const $ref = Symbol.for("ref");
|
||
|
export const $schema = Symbol.for("schema");
|
||
|
export const $setof = Symbol.for("setof");
|
||
|
export const $tuple = Symbol.for("tuple");
|
||
|
export const $tuple_STAR_ = Symbol.for("tuple*");
|
||
|
export const $version = Symbol.for("version");
|
||
|
|
||
|
export const Schema = _.Record.makeConstructor<{
|
||
|
"details": (
|
||
|
{
|
||
|
get(k: typeof $version): Version;
|
||
|
get(k: typeof $definitions): _.KeyedDictionary<symbol, Pattern>;
|
||
|
has(k: typeof $version): true;
|
||
|
has(k: typeof $definitions): true;
|
||
|
} & _.Dictionary<_.Value>
|
||
|
)
|
||
|
}>()($schema, ["details"]);
|
||
|
|
||
|
export type Schema = _.Record<
|
||
|
(typeof $schema),
|
||
|
[
|
||
|
(
|
||
|
{
|
||
|
get(k: typeof $version): Version;
|
||
|
get(k: typeof $definitions): _.KeyedDictionary<symbol, Pattern>;
|
||
|
has(k: typeof $version): true;
|
||
|
has(k: typeof $definitions): true;
|
||
|
} & _.Dictionary<_.Value>
|
||
|
)
|
||
|
]
|
||
|
>;
|
||
|
|
||
|
export type Version = (typeof $1);
|
||
|
|
||
|
export type Pattern = (
|
||
|
_.Record<
|
||
|
(typeof $atom),
|
||
|
[
|
||
|
(
|
||
|
(typeof $Boolean) |
|
||
|
(typeof $Float) |
|
||
|
(typeof $Double) |
|
||
|
(typeof $SignedInteger) |
|
||
|
(typeof $String) |
|
||
|
(typeof $ByteString) |
|
||
|
(typeof $Symbol)
|
||
|
)
|
||
|
]
|
||
|
> |
|
||
|
_.Record<(typeof $pointer), []> |
|
||
|
_.Record<(typeof $lit), [_.Value]> |
|
||
|
_.Record<(typeof $ref), [symbol]> |
|
||
|
_.Record<(typeof $or), [Array<Pattern>]> |
|
||
|
_.Record<(typeof $and), [Array<Pattern>]> |
|
||
|
_.Record<(typeof $rec), [Pattern, Pattern]> |
|
||
|
_.Record<(typeof $tuple), [Array<NamedPattern>]> |
|
||
|
_.Record<(typeof $tuple_STAR_), [Array<NamedPattern>, NamedPattern]> |
|
||
|
_.Record<(typeof $setof), [Pattern]> |
|
||
|
_.Record<(typeof $dictof), [Pattern, Pattern]> |
|
||
|
_.Record<(typeof $dict), [_.KeyedDictionary<_.Value, Pattern>]>
|
||
|
);
|
||
|
|
||
|
export type NamedPattern = (_.Record<(typeof $named), [symbol, Pattern]> | Pattern);
|
||
|
|
||
|
|
||
|
export function isSchema(v: any): v is Schema {
|
||
|
let _tmp0, _tmp1: any;
|
||
|
return (
|
||
|
_.Record.isRecord(v) &&
|
||
|
v.label === $schema &&
|
||
|
(
|
||
|
(v.length === 1) &&
|
||
|
(
|
||
|
_.Dictionary.isDictionary(v[0]) &&
|
||
|
((_tmp0 = v[0].get($version)) !== void 0 && isVersion(_tmp0)) &&
|
||
|
(
|
||
|
(_tmp1 = v[0].get($definitions)) !== void 0 && (
|
||
|
_.Dictionary.isDictionary(_tmp1) &&
|
||
|
((() => {
|
||
|
for (const e of _tmp1) {
|
||
|
if (!(typeof e[0] === 'symbol')) return false;
|
||
|
if (!(isPattern(e[1]))) return false;
|
||
|
};
|
||
|
return true;
|
||
|
})())
|
||
|
)
|
||
|
)
|
||
|
)
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
export function isVersion(v: any): v is Version {return v === $1;}
|
||
|
|
||
|
export function isPattern(v: any): v is Pattern {
|
||
|
return (
|
||
|
(
|
||
|
_.Record.isRecord(v) &&
|
||
|
v.label === $atom &&
|
||
|
(
|
||
|
(v.length === 1) &&
|
||
|
(
|
||
|
v[0] === $Boolean ||
|
||
|
v[0] === $Float ||
|
||
|
v[0] === $Double ||
|
||
|
v[0] === $SignedInteger ||
|
||
|
v[0] === $String ||
|
||
|
v[0] === $ByteString ||
|
||
|
v[0] === $Symbol
|
||
|
)
|
||
|
)
|
||
|
) ||
|
||
|
(_.Record.isRecord(v) && v.label === $pointer && ((v.length === 0))) ||
|
||
|
(_.Record.isRecord(v) && v.label === $lit && ((v.length === 1) && true)) ||
|
||
|
(
|
||
|
_.Record.isRecord(v) &&
|
||
|
v.label === $ref &&
|
||
|
((v.length === 1) && typeof v[0] === 'symbol')
|
||
|
) ||
|
||
|
(
|
||
|
_.Record.isRecord(v) &&
|
||
|
v.label === $or &&
|
||
|
(
|
||
|
(v.length === 1) &&
|
||
|
(
|
||
|
_.Array.isArray(v[0]) &&
|
||
|
!_.Record.isRecord(v[0]) &&
|
||
|
(v[0].length >= 0) &&
|
||
|
v[0].slice(0).every(v => (isPattern(v)))
|
||
|
)
|
||
|
)
|
||
|
) ||
|
||
|
(
|
||
|
_.Record.isRecord(v) &&
|
||
|
v.label === $and &&
|
||
|
(
|
||
|
(v.length === 1) &&
|
||
|
(
|
||
|
_.Array.isArray(v[0]) &&
|
||
|
!_.Record.isRecord(v[0]) &&
|
||
|
(v[0].length >= 0) &&
|
||
|
v[0].slice(0).every(v => (isPattern(v)))
|
||
|
)
|
||
|
)
|
||
|
) ||
|
||
|
(
|
||
|
_.Record.isRecord(v) &&
|
||
|
v.label === $rec &&
|
||
|
((v.length === 2) && isPattern(v[0]) && isPattern(v[1]))
|
||
|
) ||
|
||
|
(
|
||
|
_.Record.isRecord(v) &&
|
||
|
v.label === $tuple &&
|
||
|
(
|
||
|
(v.length === 1) &&
|
||
|
(
|
||
|
_.Array.isArray(v[0]) &&
|
||
|
!_.Record.isRecord(v[0]) &&
|
||
|
(v[0].length >= 0) &&
|
||
|
v[0].slice(0).every(v => (isNamedPattern(v)))
|
||
|
)
|
||
|
)
|
||
|
) ||
|
||
|
(
|
||
|
_.Record.isRecord(v) &&
|
||
|
v.label === $tuple_STAR_ &&
|
||
|
(
|
||
|
(v.length === 2) &&
|
||
|
(
|
||
|
_.Array.isArray(v[0]) &&
|
||
|
!_.Record.isRecord(v[0]) &&
|
||
|
(v[0].length >= 0) &&
|
||
|
v[0].slice(0).every(v => (isNamedPattern(v)))
|
||
|
) &&
|
||
|
isNamedPattern(v[1])
|
||
|
)
|
||
|
) ||
|
||
|
(
|
||
|
_.Record.isRecord(v) &&
|
||
|
v.label === $setof &&
|
||
|
((v.length === 1) && isPattern(v[0]))
|
||
|
) ||
|
||
|
(
|
||
|
_.Record.isRecord(v) &&
|
||
|
v.label === $dictof &&
|
||
|
((v.length === 2) && isPattern(v[0]) && isPattern(v[1]))
|
||
|
) ||
|
||
|
(
|
||
|
_.Record.isRecord(v) &&
|
||
|
v.label === $dict &&
|
||
|
(
|
||
|
(v.length === 1) &&
|
||
|
(
|
||
|
_.Dictionary.isDictionary(v[0]) &&
|
||
|
((() => {
|
||
|
for (const e of v[0]) {if (!(true)) return false; if (!(isPattern(e[1]))) return false;};
|
||
|
return true;
|
||
|
})())
|
||
|
)
|
||
|
)
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
export function isNamedPattern(v: any): v is NamedPattern {
|
||
|
return (
|
||
|
(
|
||
|
_.Record.isRecord(v) &&
|
||
|
v.label === $named &&
|
||
|
((v.length === 2) && typeof v[0] === 'symbol' && isPattern(v[1]))
|
||
|
) ||
|
||
|
isPattern(v)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|