diff --git a/implementations/javascript/packages/schema/src/compiler.ts b/implementations/javascript/packages/schema/src/compiler.ts index 44c6200..d61f040 100644 --- a/implementations/javascript/packages/schema/src/compiler.ts +++ b/implementations/javascript/packages/schema/src/compiler.ts @@ -11,17 +11,17 @@ import { genConstructor } from "./compiler/genctor"; export function compile(env: M.Environment, schema: M.Schema, options: CompilerOptions = {}): string { const mod = new ModuleContext(env, schema, options); - const pointerName = M.Schema._._field0(schema).get(M.$pointer); + const pointerName = schema.pointer; mod.defineType(seq(`export type _ptr = `, - renderType(pointerName === false + renderType(pointerName._variant === 'false' ? EMPTY_TYPE - : typeForDefinition(mod, pointerName)), + : typeForDefinition(mod, M.Definition.Alternative(M.Alternative.Pattern(M.Pattern.SimplePattern(M.SimplePattern.Ref(pointerName.value)))))), `;`)); mod.defineType(`export type _val = _.Value<_ptr>;`); mod.defineFunction(ctx => seq(`export const _decodePtr = `, - (pointerName === false + (pointerName._variant === 'false' ? '() => { throw new _.DecodeError("Pointers forbidden"); }' : seq(`(d: _.TypedDecoder<_ptr>) => `, ctx.block(() => [ seq(`let result`), @@ -30,7 +30,7 @@ export function compile(env: M.Environment, schema: M.Schema, options: CompilerO seq(`return result`)]))), `;`)); - for (const [name, def] of M.Schema._._field0(schema).get(M.$definitions)) { + for (const [name, def] of schema.definitions) { const t = typeForDefinition(mod, def); const nameStr = stringify(name); @@ -48,7 +48,7 @@ export function compile(env: M.Environment, schema: M.Schema, options: CompilerO } } - for (const [name0, def] of M.Schema._._field0(schema).get(M.$definitions)) { + for (const [name0, def] of schema.definitions) { const name = name0 as symbol; mod.defineFunction(ctx => diff --git a/implementations/javascript/packages/schema/src/compiler/context.ts b/implementations/javascript/packages/schema/src/compiler/context.ts index cee2c7b..f8bc812 100644 --- a/implementations/javascript/packages/schema/src/compiler/context.ts +++ b/implementations/javascript/packages/schema/src/compiler/context.ts @@ -41,12 +41,16 @@ export class ModuleContext { } derefPattern([_name, p]: [string, M.Alternative]): M.Definition { - if (p.label === M.$ref) { - return M.lookup(refPosition(p), p, this.env, + if (p._variant === 'Pattern' && + p.value._variant === 'SimplePattern' && + p.value.value._variant === 'Ref') + { + return M.lookup(refPosition(p.value.value.value), p.value.value.value, this.env, (p) => p, - (_modId, _modPath, pp) => pp ?? p); + (_modId, _modPath, pp) => pp ?? M.Definition.Alternative( + M.Alternative.Pattern(p.value))); } else { - return p; + return M.Definition.Alternative(p); } } diff --git a/implementations/javascript/packages/schema/src/compiler/genconverter.ts b/implementations/javascript/packages/schema/src/compiler/genconverter.ts index 175e5d3..4f2ac53 100644 --- a/implementations/javascript/packages/schema/src/compiler/genconverter.ts +++ b/implementations/javascript/packages/schema/src/compiler/genconverter.ts @@ -11,11 +11,11 @@ export function converterForDefinition( src: string, dest: string): Item[] { - if (p.label === M.$or) { - const alts = p[0]; + if (p._variant === 'or') { + const alts = p.patterns; function loop(i: number): Item[] { - ctx.variantName = alts[i][0]; - return [... converterForAlternative(ctx, alts[i][1], src, dest), + ctx.variantName = alts[i].variantLabel; + return [... converterForAlternative(ctx, alts[i].alternative, src, dest), ... ((i < alts.length - 1) ? [seq(`if (${dest} === void 0) `, ctx.block(() => loop(i + 1)))] : [])]; @@ -23,7 +23,7 @@ export function converterForDefinition( return alts.length === 0 ? [] : loop(0); } else { ctx.variantName = void 0; - return converterForAlternative(ctx, p, src, dest); + return converterForAlternative(ctx, p.value, src, dest); } } @@ -33,8 +33,8 @@ function converterForAlternative( src: string, dest: string): Item[] { - if (p.label === M.$and) { - const alts = p[0]; + if (p._variant === 'and') { + const alts = p.patterns; function loop(i: number): Item[] { return (i < alts.length) ? converterFor(ctx, alts[i], src, () => loop(i + 1)) @@ -42,11 +42,11 @@ function converterForAlternative( } return alts.length === 0 ? [seq(`${dest} = ${src}`)] : loop(0); } else { - return converterFor(ctx, p, src, simpleValue => { + return converterFor(ctx, M.NamedPattern.anonymous(p.value), src, simpleValue => { if (simpleValue === void 0) { return [ctx.buildCapturedCompound(dest)]; } else if (ctx.variantName !== void 0) { - if (typeFor(ctx.mod, p).kind === 'unit') { + if (typeFor(ctx.mod, p.value).kind === 'unit') { return [ctx.buildCapturedCompound(dest)]; } else { return [ctx.withCapture('value', @@ -97,12 +97,13 @@ function converterForArray(ctx: FunctionContext, k: (dest: string) => Item[]): Item { const postCheck = () => { - const r = ctx.gentemp(Type.array(simpleType(ctx.mod, M.unname(arrayType)))); + const r = ctx.gentemp(Type.array(simpleType(ctx.mod, M.unnameSimplePattern(arrayType)))); const v = ctx.gentempname(); return [ seq(`${r} = []`), seq(`for (const ${v} of ${src}) `, ctx.block(() => [ - ... converterFor(ctx, arrayType, v, vv => [`${r}.push(${vv})`, `continue`]), + ... converterFor(ctx, M.promoteNamedSimplePattern(arrayType), v, vv => + [`${r}.push(${vv})`, `continue`]), seq(`${r} = void 0`), seq(`break`)])), ctx.convertCapture(M.nameFor(arrayType), r, k)]; @@ -119,32 +120,32 @@ function converterFor( ks: (dest: string | undefined) => Item[], recordFields = false): Item[] { - let p = M.unname(np); + let p = M.unnamePattern(np); let maybeName = M.nameFor(np); - if (M.isSimplePattern(p)) { - const dest = ctx.gentemp(simpleType(ctx.mod, p)); - return [... converterForSimple(ctx, p, src, dest), + if (p._variant === 'SimplePattern') { + const dest = ctx.gentemp(simpleType(ctx.mod, p.value)); + return [... converterForSimple(ctx, p.value, src, dest), ctx.convertCapture(maybeName, dest, ks)]; } else { - switch (p.label) { - case M.$setof: { - const setPattern = p[0]; + switch (p.value._variant) { + case 'setof': { + const setPattern = p.value.pattern; const r = ctx.gentemp(setType(ctx.mod, setPattern)); const v = ctx.gentempname(); return [ seq(`if (_.Set.isSet<_ptr>(${src})) `, ctx.block(() => [ seq(`${r} = new _.KeyedSet()`), seq(`for (const ${v} of ${src}) `, ctx.block(() => [ - ... converterFor(ctx, setPattern, v, vv => + ... converterFor(ctx, M.anonymousSimplePattern(setPattern), v, vv => [`${r}.add(${vv})`, `continue`]), seq(`${r} = void 0`), seq(`break`)])), ctx.convertCapture(maybeName, r, ks)]))]; } - case M.$dictof: { - const keyPattern = p[0]; - const valPattern = p[1]; + case 'dictof': { + const keyPattern = p.value.key; + const valPattern = p.value.value; const r = ctx.gentemp(dictionaryType(ctx.mod, keyPattern, valPattern)); const v = ctx.gentempname(); const k = ctx.gentempname(); @@ -152,19 +153,20 @@ function converterFor( seq(`if (_.Dictionary.isDictionary<_ptr>(${src})) `, ctx.block(() => [ seq(`${r} = new _.KeyedDictionary()`), seq(`for (const [${k}, ${v}] of ${src}) `, ctx.block(() => [ - ... converterFor(ctx, keyPattern, k, kk => - converterFor(ctx, valPattern, v, vv => + ... converterFor(ctx, M.anonymousSimplePattern(keyPattern), k, kk => + converterFor(ctx, M.anonymousSimplePattern(valPattern), v, vv => [`${r}.set(${kk}, ${vv})`, `continue`])), seq(`${r} = void 0`), seq(`break`)])), ctx.convertCapture(maybeName, r, ks)]))]; } default: { - const arrayType = M.simpleArray(p); + const arrayType = M.simpleArray(p.value); if (arrayType === void 0) { - return converterForCompound(ctx, p, src, recordFields, () => ks(void 0)); + return converterForCompound(ctx, p.value, src, recordFields, () => ks(void 0)); } else { - return [converterForArray(ctx, arrayType, src, !recordFields, ks)]; + return [converterForArray( + ctx, M.NamedSimplePattern.anonymous(arrayType), src, !recordFields, ks)]; } } } @@ -177,32 +179,32 @@ function converterForSimple( src: string, dest: string): Item[] { - switch (p.label) { - case M.$any: + switch (p._variant) { + case 'any': return [`${dest} = ${src}`]; - case M.$atom: { + case 'atom': { let test: Item; - switch (p[0]) { - case M.$Boolean: test = `typeof ${src} === 'boolean'`; break; - case M.$Float: test = `_.Float.isSingle(${src})`; break; - case M.$Double: test =`_.Float.isDouble(${src})`; break; - case M.$SignedInteger: test = `typeof ${src} === 'number'`; break; - case M.$String: test = `typeof ${src} === 'string'`; break; - case M.$ByteString: test = `_.Bytes.isBytes(${src})`; break; - case M.$Symbol: test = `typeof ${src} === 'symbol'`; break; + switch (p.atomKind._variant) { + case 'Boolean': test = `typeof ${src} === 'boolean'`; break; + case 'Float': test = `_.Float.isSingle(${src})`; break; + case 'Double': test =`_.Float.isDouble(${src})`; break; + case 'SignedInteger': test = `typeof ${src} === 'number'`; break; + case 'String': test = `typeof ${src} === 'string'`; break; + case 'ByteString': test = `_.Bytes.isBytes(${src})`; break; + case 'Symbol': test = `typeof ${src} === 'symbol'`; break; } return [seq(`${dest} = `, test, ` ? ${src} : void 0`)]; } - case M.$lit: - return [`${dest} = _.is(${src}, ${ctx.mod.literal(p[0])}) ? null : void 0`]; - case M.$ref: - return M.lookup(refPosition(p), p, ctx.mod.env, - (_p) => [`${dest} = to${p[1].description!}(${src})`], + case 'lit': + return [`${dest} = _.is(${src}, ${ctx.mod.literal(p.value)}) ? null : void 0`]; + case 'Ref': + return M.lookup(refPosition(p.value), p.value, ctx.mod.env, + (_p) => [`${dest} = to${p.value.name.description!}(${src})`], (modId, modPath,_p) => { ctx.mod.imports.add([modId, modPath]); - return [`${dest} = ${modId}.decode${p[1].description!}(${src})`]; + return [`${dest} = ${modId}.decode${p.value.name.description!}(${src})`]; }); - case M.$pointer: + case 'pointer': return [`${dest} = _toPtr(${src})`]; default: ((_p: never) => {})(p); @@ -217,28 +219,31 @@ function converterForCompound( recordFields: boolean, ks: () => Item[]): Item[] { - switch (p.label) { - case M.$rec: + switch (p._variant) { + case 'rec': return [seq(`if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(${src})) `, ctx.block(() => - converterFor(ctx, p[0], `${src}.label`, () => - converterFor(ctx, p[1], src, ks, true))))]; - case M.$tuple: - return converterForTuple(ctx, p[0], src, recordFields, void 0, ks); - case M.$tuple_STAR_: - return converterForTuple(ctx, p[0], src, recordFields, p[1], ks); - case M.$setof: - case M.$dictof: + converterFor(ctx, p.label, `${src}.label`, () => + converterFor(ctx, p.fields, src, ks, true))))]; + case 'tuple': + return converterForTuple(ctx, p.patterns, src, recordFields, void 0, ks); + case 'tuple*': + return converterForTuple(ctx, p.fixed, src, recordFields, p.variable, ks); + case 'setof': + case 'dictof': throw new Error('Internal error: setof and dictof are handled in converterFor()'); - case M.$dict: { - const entries = Array.from(p[0]); + case 'dict': { + const entries = Array.from(p.entries); function loop(i: number): Item[] { if (i < entries.length) { const [k, n] = entries[i]; const tmpSrc = ctx.gentemp(); return [seq(`if ((${tmpSrc} = ${src}.get(${ctx.mod.literal(k)})) !== void 0) `, ctx.block(() => - converterFor(ctx, M.addNameIfAbsent(n, k), tmpSrc, () => - loop(i + 1))))]; + converterFor( + ctx, + M.promoteNamedSimplePattern(M.addNameIfAbsent(n, k)), + tmpSrc, + () => loop(i + 1))))]; } else { return ks(); } diff --git a/implementations/javascript/packages/schema/src/compiler/gentype.ts b/implementations/javascript/packages/schema/src/compiler/gentype.ts index 03e1cc5..2626902 100644 --- a/implementations/javascript/packages/schema/src/compiler/gentype.ts +++ b/implementations/javascript/packages/schema/src/compiler/gentype.ts @@ -4,21 +4,21 @@ import { ModuleContext } from "./context"; import { ANY_TYPE, AtomicType, CollectionType, FieldMap, SimpleType, Type } from "./type"; export function typeForDefinition(mod: ModuleContext, d: M.Definition): Type { - if (d.label === M.$or) { + if (d._variant === 'or') { return Type.union( - new Map(d[0].map(a => [a[0], typeForAlternative(mod, a[1])]))); + new Map(d.patterns.map(a => [a.variantLabel, typeForAlternative(mod, a.alternative)]))); } else { - return typeForAlternative(mod, d); + return typeForAlternative(mod, d.value); } } function typeForAlternative(mod: ModuleContext, a: M.Alternative): SimpleType { - if (a.label === M.$and) { + if (a._variant === 'and') { const fs = new Map(); - a[0].forEach(n => gatherFields(fs, mod, n)); + a.patterns.forEach(n => gatherFields(fs, mod, n)); return Type.record(fs); } else { - return typeFor(mod, a); + return typeFor(mod, a.value); } } @@ -34,19 +34,19 @@ export function dictionaryType(mod: ModuleContext, } export function typeFor(mod: ModuleContext, p: M.Pattern): SimpleType { - if (M.isSimplePattern(p)) { - return simpleType(mod, p); + if (p._variant === 'SimplePattern') { + return simpleType(mod, p.value); } else { - switch (p.label) { - case M.$setof: - return setType(mod, p[0]); - case M.$dictof: - return dictionaryType(mod, p[0], p[1]); + switch (p.value._variant) { + case 'setof': + return setType(mod, p.value.pattern); + case 'dictof': + return dictionaryType(mod, p.value.key, p.value.value); default: { - const arrayType = M.simpleArray(p); + const arrayType = M.simpleArray(p.value); if (arrayType === void 0) { const fs = new Map(); - compoundFields(fs, mod, p); + compoundFields(fs, mod, p.value); return Type.record(fs); } else { return Type.array(simpleType(mod, arrayType)); @@ -57,29 +57,29 @@ export function typeFor(mod: ModuleContext, p: M.Pattern): SimpleType { } export function simpleType(mod: ModuleContext, p: M.SimplePattern): AtomicType { - switch (p.label) { - case M.$any: + switch (p._variant) { + case 'any': return ANY_TYPE; - case M.$atom: - switch (p[0]) { - case M.$Boolean: return Type.ref(`boolean`); - case M.$Float: return Type.ref(`_.SingleFloat`); - case M.$Double: return Type.ref(`_.DoubleFloat`); - case M.$SignedInteger: return Type.ref(`number`); - case M.$String: return Type.ref(`string`); - case M.$ByteString: return Type.ref(`_.Bytes`); - case M.$Symbol: return Type.ref(`symbol`); + case 'atom': + switch (p.atomKind._variant) { + case 'Boolean': return Type.ref(`boolean`); + case 'Float': return Type.ref(`_.SingleFloat`); + case 'Double': return Type.ref(`_.DoubleFloat`); + case 'SignedInteger': return Type.ref(`number`); + case 'String': return Type.ref(`string`); + case 'ByteString': return Type.ref(`_.Bytes`); + case 'Symbol': return Type.ref(`symbol`); } - case M.$pointer: + case 'pointer': return Type.ref(`_ptr`); - case M.$lit: + case 'lit': return Type.unit(); - case M.$ref: - return M.lookup(refPosition(p), p, mod.env, - (_p) => Type.ref(p[1].description!), + case 'Ref': + return M.lookup(refPosition(p.value), p.value, mod.env, + (_p) => Type.ref(p.value.name.description!), (modId, modPath,_p) => { mod.imports.add([modId, modPath]); - return Type.ref(`${modId}.${p[1].description!}`); + return Type.ref(`${modId}.${p.value.name.description!}`); }); default: ((_p: never) => {})(p); @@ -88,27 +88,28 @@ export function simpleType(mod: ModuleContext, p: M.SimplePattern): AtomicType { } function compoundFields(fs: FieldMap, mod: ModuleContext, p: M.CompoundPattern): void { - switch (p.label) { - case M.$rec: - gatherFields(fs, mod, p[0]); - gatherFields(fs, mod, p[1]); + switch (p._variant) { + case 'rec': + gatherFields(fs, mod, p.label); + gatherFields(fs, mod, p.fields); break; - case M.$tuple: - p[0].forEach(pp => gatherFields(fs, mod, pp)); + case 'tuple': + p.patterns.forEach(pp => gatherFields(fs, mod, pp)); break; - case M.$tuple_STAR_: { - p[0].forEach(pp => gatherFields(fs, mod, pp)); - const n = p[1]; - if (n.label === M.$named) { - fs.set(n[0].description!, Type.array(simpleType(mod, n[1]))); + case 'tuple*': { + p.fixed.forEach(pp => gatherFields(fs, mod, pp)); + const n = p.variable; + if (n._variant === 'named') { + fs.set(n.value.name.description!, Type.array(simpleType(mod, n.value.pattern))); } break; } - case M.$setof: - case M.$dictof: + case 'setof': + case 'dictof': break; - case M.$dict: - p[0].forEach((n, k) => gatherFields(fs, mod, M.addNameIfAbsent(n, k))); + case 'dict': + p.entries.forEach((n, k) => + gatherFields(fs, mod, M.promoteNamedSimplePattern(M.addNameIfAbsent(n, k)))); break; default: ((_p: never) => {})(p); @@ -117,9 +118,9 @@ function compoundFields(fs: FieldMap, mod: ModuleContext, p: M.CompoundPattern): } function gatherFields(fs: FieldMap, mod: ModuleContext, n: M.NamedPattern): void { - if (n.label === M.$named) { - fs.set(n[0].description!, simpleType(mod, n[1])); - } else if (M.isCompoundPattern(n)) { - compoundFields(fs, mod, n); + if (n._variant === 'named') { + fs.set(n.value.name.description!, simpleType(mod, n.value.pattern)); + } else if (n.value._variant === 'CompoundPattern') { + compoundFields(fs, mod, n.value.value); } } diff --git a/implementations/javascript/packages/schema/src/gen/schema.ts b/implementations/javascript/packages/schema/src/gen/schema.ts index b443f1d..0dff790 100644 --- a/implementations/javascript/packages/schema/src/gen/schema.ts +++ b/implementations/javascript/packages/schema/src/gen/schema.ts @@ -9,7 +9,7 @@ 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 $any = Symbol.for('any'); +export const $any = Symbol.for("any"); export const $atom = Symbol.for("atom"); export const $definitions = Symbol.for("definitions"); export const $dict = Symbol.for("dict"); @@ -31,570 +31,521 @@ export type _ptr = never; export type _val = _.Value<_ptr>; -export const Schema = _.Record.makeConstructor<{ - "_field0": ( - { - get(k: typeof $version): Version; - get(k: typeof $pointer): PointerName; - get(k: typeof $definitions): Definitions; - has(k: typeof $version): true; - has(k: typeof $pointer): true; - has(k: typeof $definitions): true; - } & _.Dictionary<_ptr> - ) -}, _ptr>()($schema, ["_field0"]); +export type Schema = {"version": Version, "pointer": PointerName, "definitions": Definitions}; -export type Schema = _.Record< - (typeof $schema), - [ - ( - { - get(k: typeof $version): Version; - get(k: typeof $pointer): PointerName; - get(k: typeof $definitions): Definitions; - has(k: typeof $version): true; - has(k: typeof $pointer): true; - has(k: typeof $definitions): true; - } & _.Dictionary<_ptr> - ) - ], - _ptr ->; +export type Version = null; -export type Version = (typeof $1); - -export type PointerName = (Ref | (typeof __lit5)); +export type PointerName = ({"_variant": "Ref", "value": Ref} | {"_variant": "false"}); export type Definitions = _.KeyedDictionary; -export type Definition = (_.Record<(typeof $or), [Array], _ptr> | Alternative); +export type Definition = ( + {"_variant": "or", "patterns": Array} | + {"_variant": "Alternative", "value": Alternative} +); -export type NamedAlternative = [string, Alternative]; +export type NamedAlternative = {"variantLabel": string, "alternative": Alternative}; -export type Alternative = (_.Record<(typeof $and), [Array], _ptr> | Pattern); +export type Alternative = ( + {"_variant": "and", "patterns": Array} | + {"_variant": "Pattern", "value": Pattern} +); -export type Pattern = (SimplePattern | CompoundPattern); +export type Pattern = ( + {"_variant": "SimplePattern", "value": SimplePattern} | + {"_variant": "CompoundPattern", "value": CompoundPattern} +); export type SimplePattern = ( - _.Record<(typeof $any), [], _ptr> | - _.Record<(typeof $atom), [AtomKind], _ptr> | - _.Record<(typeof $pointer), [], _ptr> | - _.Record<(typeof $lit), [_val], _ptr> | - Ref + {"_variant": "any"} | + {"_variant": "atom", "atomKind": AtomKind} | + {"_variant": "pointer"} | + {"_variant": "lit", "value": _val} | + {"_variant": "Ref", "value": Ref} ); export type CompoundPattern = ( - _.Record<(typeof $rec), [NamedPattern, NamedPattern], _ptr> | - _.Record<(typeof $tuple), [Array], _ptr> | - _.Record<(typeof $tuple_STAR_), [Array, NamedSimplePattern], _ptr> | - _.Record<(typeof $setof), [SimplePattern], _ptr> | - _.Record<(typeof $dictof), [SimplePattern, SimplePattern], _ptr> | - _.Record<(typeof $dict), [DictionaryEntries], _ptr> + {"_variant": "rec", "label": NamedPattern, "fields": NamedPattern} | + {"_variant": "tuple", "patterns": Array} | + { + "_variant": "tuple*", + "fixed": Array, + "variable": NamedSimplePattern + } | + {"_variant": "setof", "pattern": SimplePattern} | + {"_variant": "dictof", "key": SimplePattern, "value": SimplePattern} | + {"_variant": "dict", "entries": DictionaryEntries} ); export type DictionaryEntries = _.KeyedDictionary<_val, NamedSimplePattern, _ptr>; export type AtomKind = ( - (typeof $Boolean) | - (typeof $Float) | - (typeof $Double) | - (typeof $SignedInteger) | - (typeof $String) | - (typeof $ByteString) | - (typeof $Symbol) + {"_variant": "Boolean"} | + {"_variant": "Float"} | + {"_variant": "Double"} | + {"_variant": "SignedInteger"} | + {"_variant": "String"} | + {"_variant": "ByteString"} | + {"_variant": "Symbol"} ); -export type NamedSimplePattern = (NamedSimplePattern_ | SimplePattern); +export type NamedSimplePattern = ( + {"_variant": "named", "value": NamedSimplePattern_} | + {"_variant": "anonymous", "value": SimplePattern} +); -export type NamedPattern = (NamedSimplePattern_ | Pattern); +export type NamedPattern = ( + {"_variant": "named", "value": NamedSimplePattern_} | + {"_variant": "anonymous", "value": Pattern} +); -export const NamedSimplePattern_ = _.Record.makeConstructor<{"name": symbol, "pattern": SimplePattern}, _ptr>()($named, ["name","pattern"]); +export type NamedSimplePattern_ = {"name": symbol, "pattern": SimplePattern}; -export type NamedSimplePattern_ = _.Record<(typeof $named), [symbol, SimplePattern], _ptr>; - -export const Ref = _.Record.makeConstructor<{"module": ModulePath, "name": symbol}, _ptr>()($ref, ["module","name"]); - -export type Ref = _.Record<(typeof $ref), [ModulePath, symbol], _ptr>; +export type Ref = {"module": ModulePath, "name": symbol}; export type ModulePath = Array; export const _decodePtr = () => { throw new _.DecodeError("Pointers forbidden"); }; -export function isSchema(v: any): v is Schema { - let _tmp0, _tmp1, _tmp2: any; - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $schema) && - ( - (v.length === 1) && - ( - _.Dictionary.isDictionary<_ptr>(v[0]) && - ((_tmp0 = v[0].get($version)) !== void 0 && isVersion(_tmp0)) && - ((_tmp1 = v[0].get($pointer)) !== void 0 && isPointerName(_tmp1)) && - ((_tmp2 = v[0].get($definitions)) !== void 0 && isDefinitions(_tmp2)) - ) - ) - ); +export function Schema(version: Version, pointer: PointerName, definitions: Definitions): Schema {return {version, pointer, definitions};} + +export function Version(): Version {return null;} + +export namespace PointerName { + export function Ref(value: Ref): PointerName {return {"_variant": "Ref", value};}; + export function $false(): PointerName {return {"_variant": "false"};}; } -export function asSchema(v: any): Schema { - if (!isSchema(v)) {throw new TypeError(`Invalid Schema: ${_.stringify(v)}`);} else {return v;}; +export function Definitions(value: _.KeyedDictionary): Definitions {return value;} + +export namespace Definition { + export function or(patterns: Array): Definition {return {"_variant": "or", patterns};}; + export function Alternative(value: Alternative): Definition {return {"_variant": "Alternative", value};}; } -export function decodeSchema(d: _.TypedDecoder<_ptr>): Schema | undefined { - let result; - if (d.openRecord()) { - let _tmp0: any; - _tmp0 = _.asLiteral(d.nextSymbol(), $schema); +export function NamedAlternative(variantLabel: string, alternative: Alternative): NamedAlternative {return {variantLabel, alternative};} + +export namespace Alternative { + export function and(patterns: Array): Alternative {return {"_variant": "and", patterns};}; + export function Pattern(value: Pattern): Alternative {return {"_variant": "Pattern", value};}; +} + +export namespace Pattern { + export function SimplePattern(value: SimplePattern): Pattern {return {"_variant": "SimplePattern", value};}; + export function CompoundPattern(value: CompoundPattern): Pattern {return {"_variant": "CompoundPattern", value};}; +} + +export namespace SimplePattern { + export function any(): SimplePattern {return {"_variant": "any"};}; + export function atom(atomKind: AtomKind): SimplePattern {return {"_variant": "atom", atomKind};}; + export function pointer(): SimplePattern {return {"_variant": "pointer"};}; + export function lit(value: _val): SimplePattern {return {"_variant": "lit", value};}; + export function Ref(value: Ref): SimplePattern {return {"_variant": "Ref", value};}; +} + +export namespace CompoundPattern { + export function rec(label: NamedPattern, fields: NamedPattern): CompoundPattern {return {"_variant": "rec", label, fields};}; + export function tuple(patterns: Array): CompoundPattern {return {"_variant": "tuple", patterns};}; + export function tuple_STAR_(fixed: Array, variable: NamedSimplePattern): CompoundPattern {return {"_variant": "tuple*", fixed, variable};}; + export function setof(pattern: SimplePattern): CompoundPattern {return {"_variant": "setof", pattern};}; + export function dictof(key: SimplePattern, value: SimplePattern): CompoundPattern {return {"_variant": "dictof", key, value};}; + export function dict(entries: DictionaryEntries): CompoundPattern {return {"_variant": "dict", entries};}; +} + +export function DictionaryEntries(value: _.KeyedDictionary<_val, NamedSimplePattern, _ptr>): DictionaryEntries {return value;} + +export namespace AtomKind { + export function Boolean(): AtomKind {return {"_variant": "Boolean"};}; + export function Float(): AtomKind {return {"_variant": "Float"};}; + export function Double(): AtomKind {return {"_variant": "Double"};}; + export function SignedInteger(): AtomKind {return {"_variant": "SignedInteger"};}; + export function String(): AtomKind {return {"_variant": "String"};}; + export function ByteString(): AtomKind {return {"_variant": "ByteString"};}; + export function Symbol(): AtomKind {return {"_variant": "Symbol"};}; +} + +export namespace NamedSimplePattern { + export function named(value: NamedSimplePattern_): NamedSimplePattern {return {"_variant": "named", value};}; + export function anonymous(value: SimplePattern): NamedSimplePattern {return {"_variant": "anonymous", value};}; +} + +export namespace NamedPattern { + export function named(value: NamedSimplePattern_): NamedPattern {return {"_variant": "named", value};}; + export function anonymous(value: Pattern): NamedPattern {return {"_variant": "anonymous", value};}; +} + +export function NamedSimplePattern__(name: symbol, pattern: SimplePattern): NamedSimplePattern_ {return {name, pattern};} + +export function Ref(module: ModulePath, name: symbol): Ref {return {module, name};} + +export function ModulePath(value: Array): ModulePath {return value;} + +export function asSchema(v: _val): Schema { + let result = toSchema(v); + if (result === void 0) throw new TypeError(`Invalid Schema: ${_.stringify(v)}`); + return result; +} + +export function toSchema(v: _val): undefined | Schema { + let result: undefined | Schema; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $schema) ? null : void 0; if (_tmp0 !== void 0) { - let _tmp1, _tmp2, _tmp3, _tmp4, _tmp5: any; - _tmp2 = d.next(); - if (_tmp2 !== void 0 && !(( - _.Dictionary.isDictionary<_ptr>(_tmp2) && - ((_tmp3 = _tmp2.get($version)) !== void 0 && isVersion(_tmp3)) && - ((_tmp4 = _tmp2.get($pointer)) !== void 0 && isPointerName(_tmp4)) && - ((_tmp5 = _tmp2.get($definitions)) !== void 0 && isDefinitions(_tmp5)) - ))) _tmp2 = void 0; - if (_tmp2 !== void 0) { - if (d.closeCompound()) _tmp1 = [_tmp2] as [ - ( - { - get(k: typeof $version): Version; - get(k: typeof $pointer): PointerName; - get(k: typeof $definitions): Definitions; - has(k: typeof $version): true; - has(k: typeof $pointer): true; - has(k: typeof $definitions): true; - } & _.Dictionary<_ptr> - ) - ]; - }; - if (_tmp1 !== void 0) result = _.Record< - (typeof $schema), - [ - ( - { - get(k: typeof $version): Version; - get(k: typeof $pointer): PointerName; - get(k: typeof $definitions): Definitions; - has(k: typeof $version): true; - has(k: typeof $pointer): true; - has(k: typeof $definitions): true; - } & _.Dictionary<_ptr> - ) - ] - >(_tmp0 as any, _tmp1 as any); - }; - }; - return result; -} - -export function isVersion(v: any): v is Version {return _.is(v, $1);} - -export function asVersion(v: any): Version { - if (!isVersion(v)) {throw new TypeError(`Invalid Version: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeVersion(d: _.TypedDecoder<_ptr>): Version | undefined {let result; result = _.asLiteral(d.nextSignedInteger(), $1); return result;} - -export function isPointerName(v: any): v is PointerName {return (isRef(v) || _.is(v, __lit5));} - -export function asPointerName(v: any): PointerName { - if (!isPointerName(v)) {throw new TypeError(`Invalid PointerName: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodePointerName(d: _.TypedDecoder<_ptr>): PointerName | undefined { - let _tmp0: any; - let result; - _tmp0 = d.mark(); - result = decodeRef(d); - if (result === void 0) {d.restoreMark(_tmp0); result = _.asLiteral(d.nextBoolean(), __lit5);}; - return result; -} - -export function isDefinitions(v: any): v is Definitions { - return ( - _.Dictionary.isDictionary<_ptr>(v) && - ((() => { - for (const e of v) { - if (!(typeof e[0] === 'symbol')) return false; - if (!(isDefinition(e[1]))) return false; - }; - return true; - })()) - ); -} - -export function asDefinitions(v: any): Definitions { - if (!isDefinitions(v)) {throw new TypeError(`Invalid Definitions: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeDefinitions(d: _.TypedDecoder<_ptr>): Definitions | undefined { - let result; - if (d.openDictionary()) { - let r: _.KeyedDictionary | undefined = new _.KeyedDictionary(); - while (!d.closeCompound()) { - let K: undefined | symbol = void 0; - K = d.nextSymbol(); - if (K === void 0) { r = void 0; break; }; - let V: undefined | Definition = void 0; - V = decodeDefinition(d); - if (V === void 0) { r = void 0; break; }; - r.set(K, V); - }; - result = r; - }; - return result; -} - -export function isDefinition(v: any): v is Definition { - 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].every(v => (isNamedAlternative(v))) - ) - ) - ) || - isAlternative(v) - ); -} - -export function asDefinition(v: any): Definition { - if (!isDefinition(v)) {throw new TypeError(`Invalid Definition: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeDefinition(d: _.TypedDecoder<_ptr>): Definition | undefined { - let _tmp0: any; - let result; - _tmp0 = d.mark(); - if (d.openRecord()) { - let _tmp1: any; - _tmp1 = _.asLiteral(d.nextSymbol(), $or); - if (_tmp1 !== void 0) { - let _tmp2, _tmp3: any; - if (d.openSequence()) { - let _tmp4: any; - { - let vN: Array | undefined = []; - while (!d.closeCompound()) { - _tmp4 = void 0; - _tmp4 = decodeNamedAlternative(d); - if (_tmp4 === void 0) {vN = void 0; break;}; - vN.push(_tmp4); - }; - _tmp3 = vN; - }; - }; - if (_tmp3 !== void 0) {if (d.closeCompound()) _tmp2 = [_tmp3] as [Array];}; - if (_tmp2 !== void 0) result = _.Record<(typeof $or), [Array]>(_tmp1 as any, _tmp2 as any); - }; - }; - if (result === void 0) {d.restoreMark(_tmp0); result = decodeAlternative(d);}; - return result; -} - -export function isNamedAlternative(v: any): v is NamedAlternative { - return ( - _.Array.isArray(v) && - !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - (v.length === 2) && - typeof v[0] === 'string' && - isAlternative(v[1]) - ); -} - -export function asNamedAlternative(v: any): NamedAlternative { - if (!isNamedAlternative(v)) {throw new TypeError(`Invalid NamedAlternative: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeNamedAlternative(d: _.TypedDecoder<_ptr>): NamedAlternative | undefined { - let _tmp0, _tmp1: any; - let result; - if (d.openSequence()) { - _tmp0 = d.nextString(); - if (_tmp0 !== void 0) { - _tmp1 = decodeAlternative(d); - if (_tmp1 !== void 0) {if (d.closeCompound()) result = [_tmp0, _tmp1] as [string, Alternative];}; - }; - }; - return result; -} - -export function isAlternative(v: any): v is Alternative { - 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].every(v => (isNamedPattern(v))) - ) - ) - ) || - isPattern(v) - ); -} - -export function asAlternative(v: any): Alternative { - if (!isAlternative(v)) {throw new TypeError(`Invalid Alternative: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeAlternative(d: _.TypedDecoder<_ptr>): Alternative | undefined { - let _tmp0: any; - let result; - _tmp0 = d.mark(); - if (d.openRecord()) { - let _tmp1: any; - _tmp1 = _.asLiteral(d.nextSymbol(), $and); - if (_tmp1 !== void 0) { - let _tmp2, _tmp3: any; - if (d.openSequence()) { - let _tmp4: any; - { - let vN: Array | undefined = []; - while (!d.closeCompound()) { - _tmp4 = void 0; - _tmp4 = decodeNamedPattern(d); - if (_tmp4 === void 0) {vN = void 0; break;}; - vN.push(_tmp4); - }; - _tmp3 = vN; - }; - }; - if (_tmp3 !== void 0) {if (d.closeCompound()) _tmp2 = [_tmp3] as [Array];}; - if (_tmp2 !== void 0) result = _.Record<(typeof $and), [Array]>(_tmp1 as any, _tmp2 as any); - }; - }; - if (result === void 0) {d.restoreMark(_tmp0); result = decodePattern(d);}; - return result; -} - -export function isPattern(v: any): v is Pattern {return (isSimplePattern(v) || isCompoundPattern(v));} - -export function asPattern(v: any): Pattern { - if (!isPattern(v)) {throw new TypeError(`Invalid Pattern: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodePattern(d: _.TypedDecoder<_ptr>): Pattern | undefined { - let _tmp0: any; - let result; - _tmp0 = d.mark(); - result = decodeSimplePattern(d); - if (result === void 0) {d.restoreMark(_tmp0); result = decodeCompoundPattern(d);}; - return result; -} - -export function isSimplePattern(v: any): v is SimplePattern { - return _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && ( - ( - (_.is(v.label, $any) && ((v.length === 0))) || - (_.is(v.label, $atom) && ((v.length === 1) && isAtomKind(v[0]))) || - (_.is(v.label, $pointer) && ((v.length === 0))) || - (_.is(v.label, $lit) && ((v.length === 1) && true)) || - ( - _.is(v.label, $ref) && ((v.length === 2) && isModulePath(v[0]) && typeof v[1] === 'symbol') - ) - ) - ); -} - -export function asSimplePattern(v: any): SimplePattern { - if (!isSimplePattern(v)) {throw new TypeError(`Invalid SimplePattern: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeSimplePattern(d: _.TypedDecoder<_ptr>): SimplePattern | undefined { - let result; - if (d.openRecord()) { - let _tmp0, _tmp1: any; - _tmp0 = d.next(); - _tmp1 = d.mark(); - { - if (_.is(_tmp0, $any)) { - if (d.closeCompound()) return _.Record<(typeof $any), []>($any, []); - d.restoreMark(_tmp1); - } - } - if (_.is(_tmp0, $atom)) { - let _tmp2, _tmp3: any; - _tmp3 = decodeAtomKind(d); - if (_tmp3 !== void 0) {if (d.closeCompound()) _tmp2 = [_tmp3] as [AtomKind];}; - if (_tmp2 !== void 0) result = _.Record<(typeof $atom), [AtomKind]>(_tmp0 as any, _tmp2 as any); - }; - if (result === void 0) { - d.restoreMark(_tmp1); - if (_.is(_tmp0, $pointer)) { - let _tmp4: any; - if (d.closeCompound()) _tmp4 = [] as []; - if (_tmp4 !== void 0) result = _.Record<(typeof $pointer), []>(_tmp0 as any, _tmp4 as any); - }; - if (result === void 0) { - d.restoreMark(_tmp1); - if (_.is(_tmp0, $lit)) { - let _tmp5, _tmp6: any; - _tmp6 = d.next(); - if (_tmp6 !== void 0) {if (d.closeCompound()) _tmp5 = [_tmp6] as [_val];}; - if (_tmp5 !== void 0) result = _.Record<(typeof $lit), [_val]>(_tmp0 as any, _tmp5 as any); - }; - if (result === void 0) { - d.restoreMark(_tmp1); - if (_.is(_tmp0, $ref)) { - let _tmp7, _tmp8, _tmp9: any; - _tmp8 = decodeModulePath(d); - if (_tmp8 !== void 0) { - _tmp9 = d.nextSymbol(); - if (_tmp9 !== void 0) {if (d.closeCompound()) _tmp7 = [_tmp8, _tmp9] as [ModulePath, symbol];}; - }; - if (_tmp7 !== void 0) result = _.Record<(typeof $ref), [ModulePath, symbol]>(_tmp0 as any, _tmp7 as any); - }; - }; - }; - }; - }; - return result; -} - -export function isCompoundPattern(v: any): v is CompoundPattern { - return _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && ( - ( - ( - _.is(v.label, $rec) && ((v.length === 2) && isNamedPattern(v[0]) && isNamedPattern(v[1])) - ) || - ( - _.is(v.label, $tuple) && ( - (v.length === 1) && - ( - _.Array.isArray(v[0]) && - !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) && - (v[0].length >= 0) && - v[0].every(v => (isNamedPattern(v))) - ) - ) - ) || - ( - _.is(v.label, $tuple_STAR_) && ( - (v.length === 2) && - ( - _.Array.isArray(v[0]) && - !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) && - (v[0].length >= 0) && - v[0].every(v => (isNamedPattern(v))) - ) && - isNamedSimplePattern(v[1]) - ) - ) || - (_.is(v.label, $setof) && ((v.length === 1) && isSimplePattern(v[0]))) || - ( - _.is(v.label, $dictof) && ((v.length === 2) && isSimplePattern(v[0]) && isSimplePattern(v[1])) - ) || - (_.is(v.label, $dict) && ((v.length === 1) && isDictionaryEntries(v[0]))) - ) - ); -} - -export function asCompoundPattern(v: any): CompoundPattern { - if (!isCompoundPattern(v)) {throw new TypeError(`Invalid CompoundPattern: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeCompoundPattern(d: _.TypedDecoder<_ptr>): CompoundPattern | undefined { - let result; - if (d.openRecord()) { - let _tmp0, _tmp1: any; - _tmp0 = d.next(); - _tmp1 = d.mark(); - if (_.is(_tmp0, $rec)) { - let _tmp2, _tmp3, _tmp4: any; - _tmp3 = decodeNamedPattern(d); - if (_tmp3 !== void 0) { - _tmp4 = decodeNamedPattern(d); - if (_tmp4 !== void 0) {if (d.closeCompound()) _tmp2 = [_tmp3, _tmp4] as [NamedPattern, NamedPattern];}; - }; - if (_tmp2 !== void 0) result = _.Record<(typeof $rec), [NamedPattern, NamedPattern]>(_tmp0 as any, _tmp2 as any); - }; - if (result === void 0) { - d.restoreMark(_tmp1); - if (_.is(_tmp0, $tuple)) { - let _tmp5, _tmp6: any; - if (d.openSequence()) { - let _tmp7: any; - { - let vN: Array | undefined = []; - while (!d.closeCompound()) { - _tmp7 = void 0; - _tmp7 = decodeNamedPattern(d); - if (_tmp7 === void 0) {vN = void 0; break;}; - vN.push(_tmp7); - }; - _tmp6 = vN; - }; - }; - if (_tmp6 !== void 0) {if (d.closeCompound()) _tmp5 = [_tmp6] as [Array];}; - if (_tmp5 !== void 0) result = _.Record<(typeof $tuple), [Array]>(_tmp0 as any, _tmp5 as any); - }; - if (result === void 0) { - d.restoreMark(_tmp1); - if (_.is(_tmp0, $tuple_STAR_)) { - let _tmp8, _tmp9, _tmp10: any; - if (d.openSequence()) { - let _tmp11: any; - { - let vN: Array | undefined = []; - while (!d.closeCompound()) { - _tmp11 = void 0; - _tmp11 = decodeNamedPattern(d); - if (_tmp11 === void 0) {vN = void 0; break;}; - vN.push(_tmp11); - }; - _tmp9 = vN; - }; - }; - if (_tmp9 !== void 0) { - _tmp10 = decodeNamedSimplePattern(d); - if (_tmp10 !== void 0) { - if (d.closeCompound()) _tmp8 = [_tmp9, _tmp10] as [Array, NamedSimplePattern]; - }; - }; - if (_tmp8 !== void 0) result = _.Record<(typeof $tuple_STAR_), [Array, NamedSimplePattern]>(_tmp0 as any, _tmp8 as any); - }; - if (result === void 0) { - d.restoreMark(_tmp1); - if (_.is(_tmp0, $setof)) { - let _tmp12, _tmp13: any; - _tmp13 = decodeSimplePattern(d); - if (_tmp13 !== void 0) {if (d.closeCompound()) _tmp12 = [_tmp13] as [SimplePattern];}; - if (_tmp12 !== void 0) result = _.Record<(typeof $setof), [SimplePattern]>(_tmp0 as any, _tmp12 as any); - }; - if (result === void 0) { - d.restoreMark(_tmp1); - if (_.is(_tmp0, $dictof)) { - let _tmp14, _tmp15, _tmp16: any; - _tmp15 = decodeSimplePattern(d); - if (_tmp15 !== void 0) { - _tmp16 = decodeSimplePattern(d); - if (_tmp16 !== void 0) { - if (d.closeCompound()) _tmp14 = [_tmp15, _tmp16] as [SimplePattern, SimplePattern]; + if (_.Dictionary.isDictionary<_ptr>(v[0])) { + let _tmp1: (_val) | undefined; + if ((_tmp1 = v[0].get($version)) !== void 0) { + let _tmp2: (Version) | undefined; + _tmp2 = toVersion(_tmp1); + if (_tmp2 !== void 0) { + let _tmp3: (_val) | undefined; + if ((_tmp3 = v[0].get($pointer)) !== void 0) { + let _tmp4: (PointerName) | undefined; + _tmp4 = toPointerName(_tmp3); + if (_tmp4 !== void 0) { + let _tmp5: (_val) | undefined; + if ((_tmp5 = v[0].get($definitions)) !== void 0) { + let _tmp6: (Definitions) | undefined; + _tmp6 = toDefinitions(_tmp5); + if (_tmp6 !== void 0) {result = {"version": _tmp2, "pointer": _tmp4, "definitions": _tmp6};}; }; }; - if (_tmp14 !== void 0) result = _.Record<(typeof $dictof), [SimplePattern, SimplePattern]>(_tmp0 as any, _tmp14 as any); }; - if (result === void 0) { - d.restoreMark(_tmp1); - if (_.is(_tmp0, $dict)) { - let _tmp17, _tmp18: any; - _tmp18 = decodeDictionaryEntries(d); - if (_tmp18 !== void 0) {if (d.closeCompound()) _tmp17 = [_tmp18] as [DictionaryEntries];}; - if (_tmp17 !== void 0) result = _.Record<(typeof $dict), [DictionaryEntries]>(_tmp0 as any, _tmp17 as any); + }; + }; + }; + }; + }; + return result; +} + +export function asVersion(v: _val): Version { + let result = toVersion(v); + if (result === void 0) throw new TypeError(`Invalid Version: ${_.stringify(v)}`); + return result; +} + +export function toVersion(v: _val): undefined | Version { + let _tmp0: (null) | undefined; + let result: undefined | Version; + _tmp0 = _.is(v, $1) ? null : void 0; + if (_tmp0 !== void 0) {result = _tmp0;}; + return result; +} + +export function asPointerName(v: _val): PointerName { + let result = toPointerName(v); + if (result === void 0) throw new TypeError(`Invalid PointerName: ${_.stringify(v)}`); + return result; +} + +export function toPointerName(v: _val): undefined | PointerName { + let _tmp0: (Ref) | undefined; + let result: undefined | PointerName; + _tmp0 = toRef(v); + if (_tmp0 !== void 0) {result = {"_variant": "Ref", "value": _tmp0};}; + if (result === void 0) { + let _tmp1: (null) | undefined; + _tmp1 = _.is(v, __lit5) ? null : void 0; + if (_tmp1 !== void 0) {result = {"_variant": "false"};}; + }; + return result; +} + +export function asDefinitions(v: _val): Definitions { + let result = toDefinitions(v); + if (result === void 0) throw new TypeError(`Invalid Definitions: ${_.stringify(v)}`); + return result; +} + +export function toDefinitions(v: _val): undefined | Definitions { + let _tmp0: (_.KeyedDictionary) | undefined; + let result: undefined | Definitions; + if (_.Dictionary.isDictionary<_ptr>(v)) { + _tmp0 = new _.KeyedDictionary(); + for (const [_tmp2, _tmp1] of v) { + let _tmp3: (symbol) | undefined; + _tmp3 = typeof _tmp2 === 'symbol' ? _tmp2 : void 0; + if (_tmp3 !== void 0) { + let _tmp4: (Definition) | undefined; + _tmp4 = toDefinition(_tmp1); + if (_tmp4 !== void 0) {_tmp0.set(_tmp3, _tmp4); continue;}; + }; + _tmp0 = void 0; + break; + }; + if (_tmp0 !== void 0) {result = _tmp0;}; + }; + return result; +} + +export function asDefinition(v: _val): Definition { + let result = toDefinition(v); + if (result === void 0) throw new TypeError(`Invalid Definition: ${_.stringify(v)}`); + return result; +} + +export function toDefinition(v: _val): undefined | Definition { + let result: undefined | Definition; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $or) ? null : void 0; + if (_tmp0 !== void 0) { + if (_.Array.isArray(v[0])) { + let _tmp1: (Array<_val>) | undefined; + let _tmp2: (Array) | undefined; + _tmp1 = v[0]; + { + _tmp2 = []; + for (const _tmp3 of _tmp1) { + let _tmp4: (NamedAlternative) | undefined; + _tmp4 = toNamedAlternative(_tmp3); + if (_tmp4 !== void 0) {_tmp2.push(_tmp4); continue;}; + _tmp2 = void 0; + break; + }; + if (_tmp2 !== void 0) {result = {"_variant": "or", "patterns": _tmp2};}; + }; + }; + }; + }; + if (result === void 0) { + let _tmp5: (Alternative) | undefined; + _tmp5 = toAlternative(v); + if (_tmp5 !== void 0) {result = {"_variant": "Alternative", "value": _tmp5};}; + }; + return result; +} + +export function asNamedAlternative(v: _val): NamedAlternative { + let result = toNamedAlternative(v); + if (result === void 0) throw new TypeError(`Invalid NamedAlternative: ${_.stringify(v)}`); + return result; +} + +export function toNamedAlternative(v: _val): undefined | NamedAlternative { + let result: undefined | NamedAlternative; + if (_.Array.isArray(v) && v.length === 2) { + let _tmp0: (string) | undefined; + _tmp0 = typeof v[0] === 'string' ? v[0] : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (Alternative) | undefined; + _tmp1 = toAlternative(v[1]); + if (_tmp1 !== void 0) {result = {"variantLabel": _tmp0, "alternative": _tmp1};}; + }; + }; + return result; +} + +export function asAlternative(v: _val): Alternative { + let result = toAlternative(v); + if (result === void 0) throw new TypeError(`Invalid Alternative: ${_.stringify(v)}`); + return result; +} + +export function toAlternative(v: _val): undefined | Alternative { + let result: undefined | Alternative; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $and) ? null : void 0; + if (_tmp0 !== void 0) { + if (_.Array.isArray(v[0])) { + let _tmp1: (Array<_val>) | undefined; + let _tmp2: (Array) | undefined; + _tmp1 = v[0]; + { + _tmp2 = []; + for (const _tmp3 of _tmp1) { + let _tmp4: (NamedPattern) | undefined; + _tmp4 = toNamedPattern(_tmp3); + if (_tmp4 !== void 0) {_tmp2.push(_tmp4); continue;}; + _tmp2 = void 0; + break; + }; + if (_tmp2 !== void 0) {result = {"_variant": "and", "patterns": _tmp2};}; + }; + }; + }; + }; + if (result === void 0) { + let _tmp5: (Pattern) | undefined; + _tmp5 = toPattern(v); + if (_tmp5 !== void 0) {result = {"_variant": "Pattern", "value": _tmp5};}; + }; + return result; +} + +export function asPattern(v: _val): Pattern { + let result = toPattern(v); + if (result === void 0) throw new TypeError(`Invalid Pattern: ${_.stringify(v)}`); + return result; +} + +export function toPattern(v: _val): undefined | Pattern { + let _tmp0: (SimplePattern) | undefined; + let result: undefined | Pattern; + _tmp0 = toSimplePattern(v); + if (_tmp0 !== void 0) {result = {"_variant": "SimplePattern", "value": _tmp0};}; + if (result === void 0) { + let _tmp1: (CompoundPattern) | undefined; + _tmp1 = toCompoundPattern(v); + if (_tmp1 !== void 0) {result = {"_variant": "CompoundPattern", "value": _tmp1};}; + }; + return result; +} + +export function asSimplePattern(v: _val): SimplePattern { + let result = toSimplePattern(v); + if (result === void 0) throw new TypeError(`Invalid SimplePattern: ${_.stringify(v)}`); + return result; +} + +export function toSimplePattern(v: _val): undefined | SimplePattern { + let _tmp0: (null) | undefined; + let result: undefined | SimplePattern; + _tmp0 = _.is(v, $any) ? null : void 0; + if (_tmp0 !== void 0) {result = {"_variant": "any"};}; + if (result === void 0) { + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp1: (null) | undefined; + _tmp1 = _.is(v.label, $atom) ? null : void 0; + if (_tmp1 !== void 0) { + let _tmp2: (AtomKind) | undefined; + _tmp2 = toAtomKind(v[0]); + if (_tmp2 !== void 0) {result = {"_variant": "atom", "atomKind": _tmp2};}; + }; + }; + if (result === void 0) { + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp3: (null) | undefined; + _tmp3 = _.is(v.label, $pointer) ? null : void 0; + if (_tmp3 !== void 0) {result = {"_variant": "pointer"};}; + }; + if (result === void 0) { + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp4: (null) | undefined; + _tmp4 = _.is(v.label, $lit) ? null : void 0; + if (_tmp4 !== void 0) { + let _tmp5: (_val) | undefined; + _tmp5 = v[0]; + if (_tmp5 !== void 0) {result = {"_variant": "lit", "value": _tmp5};}; + }; + }; + if (result === void 0) { + let _tmp6: (Ref) | undefined; + _tmp6 = toRef(v); + if (_tmp6 !== void 0) {result = {"_variant": "Ref", "value": _tmp6};}; + }; + }; + }; + }; + return result; +} + +export function asCompoundPattern(v: _val): CompoundPattern { + let result = toCompoundPattern(v); + if (result === void 0) throw new TypeError(`Invalid CompoundPattern: ${_.stringify(v)}`); + return result; +} + +export function toCompoundPattern(v: _val): undefined | CompoundPattern { + let result: undefined | CompoundPattern; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $rec) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (NamedPattern) | undefined; + _tmp1 = toNamedPattern(v[0]); + if (_tmp1 !== void 0) { + let _tmp2: (NamedPattern) | undefined; + _tmp2 = toNamedPattern(v[1]); + if (_tmp2 !== void 0) {result = {"_variant": "rec", "label": _tmp1, "fields": _tmp2};}; + }; + }; + }; + if (result === void 0) { + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp3: (null) | undefined; + _tmp3 = _.is(v.label, $tuple) ? null : void 0; + if (_tmp3 !== void 0) { + if (_.Array.isArray(v[0])) { + let _tmp4: (Array<_val>) | undefined; + let _tmp5: (Array) | undefined; + _tmp4 = v[0]; + { + _tmp5 = []; + for (const _tmp6 of _tmp4) { + let _tmp7: (NamedPattern) | undefined; + _tmp7 = toNamedPattern(_tmp6); + if (_tmp7 !== void 0) {_tmp5.push(_tmp7); continue;}; + _tmp5 = void 0; + break; + }; + if (_tmp5 !== void 0) {result = {"_variant": "tuple", "patterns": _tmp5};}; + }; + }; + }; + }; + if (result === void 0) { + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp8: (null) | undefined; + _tmp8 = _.is(v.label, $tuple_STAR_) ? null : void 0; + if (_tmp8 !== void 0) { + if (_.Array.isArray(v[0])) { + let _tmp9: (Array<_val>) | undefined; + let _tmp10: (Array) | undefined; + _tmp9 = v[0]; + { + _tmp10 = []; + for (const _tmp11 of _tmp9) { + let _tmp12: (NamedPattern) | undefined; + _tmp12 = toNamedPattern(_tmp11); + if (_tmp12 !== void 0) {_tmp10.push(_tmp12); continue;}; + _tmp10 = void 0; + break; + }; + if (_tmp10 !== void 0) { + let _tmp13: (NamedSimplePattern) | undefined; + _tmp13 = toNamedSimplePattern(v[1]); + if (_tmp13 !== void 0) {result = {"_variant": "tuple*", "fixed": _tmp10, "variable": _tmp13};}; + }; + }; + }; + }; + }; + if (result === void 0) { + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp14: (null) | undefined; + _tmp14 = _.is(v.label, $setof) ? null : void 0; + if (_tmp14 !== void 0) { + let _tmp15: (SimplePattern) | undefined; + _tmp15 = toSimplePattern(v[0]); + if (_tmp15 !== void 0) {result = {"_variant": "setof", "pattern": _tmp15};}; + }; + }; + if (result === void 0) { + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp16: (null) | undefined; + _tmp16 = _.is(v.label, $dictof) ? null : void 0; + if (_tmp16 !== void 0) { + let _tmp17: (SimplePattern) | undefined; + _tmp17 = toSimplePattern(v[0]); + if (_tmp17 !== void 0) { + let _tmp18: (SimplePattern) | undefined; + _tmp18 = toSimplePattern(v[1]); + if (_tmp18 !== void 0) {result = {"_variant": "dictof", "key": _tmp17, "value": _tmp18};}; + }; + }; + }; + if (result === void 0) { + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp19: (null) | undefined; + _tmp19 = _.is(v.label, $dict) ? null : void 0; + if (_tmp19 !== void 0) { + let _tmp20: (DictionaryEntries) | undefined; + _tmp20 = toDictionaryEntries(v[0]); + if (_tmp20 !== void 0) {result = {"_variant": "dict", "entries": _tmp20};}; }; }; }; @@ -605,75 +556,69 @@ export function decodeCompoundPattern(d: _.TypedDecoder<_ptr>): CompoundPattern return result; } -export function isDictionaryEntries(v: any): v is DictionaryEntries { - return ( - _.Dictionary.isDictionary<_ptr>(v) && - ((() => { - for (const e of v) {if (!(true)) return false; if (!(isNamedSimplePattern(e[1]))) return false;}; - return true; - })()) - ); +export function asDictionaryEntries(v: _val): DictionaryEntries { + let result = toDictionaryEntries(v); + if (result === void 0) throw new TypeError(`Invalid DictionaryEntries: ${_.stringify(v)}`); + return result; } -export function asDictionaryEntries(v: any): DictionaryEntries { - if (!isDictionaryEntries(v)) {throw new TypeError(`Invalid DictionaryEntries: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeDictionaryEntries(d: _.TypedDecoder<_ptr>): DictionaryEntries | undefined { - let result; - if (d.openDictionary()) { - let r: _.KeyedDictionary<_val, NamedSimplePattern, _ptr> | undefined = new _.KeyedDictionary(); - while (!d.closeCompound()) { - let K: undefined | _val = void 0; - K = d.next(); - if (K === void 0) { r = void 0; break; }; - let V: undefined | NamedSimplePattern = void 0; - V = decodeNamedSimplePattern(d); - if (V === void 0) { r = void 0; break; }; - r.set(K, V); +export function toDictionaryEntries(v: _val): undefined | DictionaryEntries { + let _tmp0: (_.KeyedDictionary<_val, NamedSimplePattern, _ptr>) | undefined; + let result: undefined | DictionaryEntries; + if (_.Dictionary.isDictionary<_ptr>(v)) { + _tmp0 = new _.KeyedDictionary(); + for (const [_tmp2, _tmp1] of v) { + let _tmp3: (_val) | undefined; + _tmp3 = _tmp2; + if (_tmp3 !== void 0) { + let _tmp4: (NamedSimplePattern) | undefined; + _tmp4 = toNamedSimplePattern(_tmp1); + if (_tmp4 !== void 0) {_tmp0.set(_tmp3, _tmp4); continue;}; + }; + _tmp0 = void 0; + break; }; - result = r; + if (_tmp0 !== void 0) {result = _tmp0;}; }; return result; } -export function isAtomKind(v: any): v is AtomKind { - return ( - _.is(v, $Boolean) || - _.is(v, $Float) || - _.is(v, $Double) || - _.is(v, $SignedInteger) || - _.is(v, $String) || - _.is(v, $ByteString) || - _.is(v, $Symbol) - ); +export function asAtomKind(v: _val): AtomKind { + let result = toAtomKind(v); + if (result === void 0) throw new TypeError(`Invalid AtomKind: ${_.stringify(v)}`); + return result; } -export function asAtomKind(v: any): AtomKind { - if (!isAtomKind(v)) {throw new TypeError(`Invalid AtomKind: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeAtomKind(d: _.TypedDecoder<_ptr>): AtomKind | undefined { - let _tmp0: any; - let result; - _tmp0 = d.mark(); - result = _.asLiteral(d.nextSymbol(), $Boolean); +export function toAtomKind(v: _val): undefined | AtomKind { + let _tmp0: (null) | undefined; + let result: undefined | AtomKind; + _tmp0 = _.is(v, $Boolean) ? null : void 0; + if (_tmp0 !== void 0) {result = {"_variant": "Boolean"};}; if (result === void 0) { - d.restoreMark(_tmp0); - result = _.asLiteral(d.nextSymbol(), $Float); + let _tmp1: (null) | undefined; + _tmp1 = _.is(v, $Float) ? null : void 0; + if (_tmp1 !== void 0) {result = {"_variant": "Float"};}; if (result === void 0) { - d.restoreMark(_tmp0); - result = _.asLiteral(d.nextSymbol(), $Double); + let _tmp2: (null) | undefined; + _tmp2 = _.is(v, $Double) ? null : void 0; + if (_tmp2 !== void 0) {result = {"_variant": "Double"};}; if (result === void 0) { - d.restoreMark(_tmp0); - result = _.asLiteral(d.nextSymbol(), $SignedInteger); + let _tmp3: (null) | undefined; + _tmp3 = _.is(v, $SignedInteger) ? null : void 0; + if (_tmp3 !== void 0) {result = {"_variant": "SignedInteger"};}; if (result === void 0) { - d.restoreMark(_tmp0); - result = _.asLiteral(d.nextSymbol(), $String); + let _tmp4: (null) | undefined; + _tmp4 = _.is(v, $String) ? null : void 0; + if (_tmp4 !== void 0) {result = {"_variant": "String"};}; if (result === void 0) { - d.restoreMark(_tmp0); - result = _.asLiteral(d.nextSymbol(), $ByteString); - if (result === void 0) {d.restoreMark(_tmp0); result = _.asLiteral(d.nextSymbol(), $Symbol);}; + let _tmp5: (null) | undefined; + _tmp5 = _.is(v, $ByteString) ? null : void 0; + if (_tmp5 !== void 0) {result = {"_variant": "ByteString"};}; + if (result === void 0) { + let _tmp6: (null) | undefined; + _tmp6 = _.is(v, $Symbol) ? null : void 0; + if (_tmp6 !== void 0) {result = {"_variant": "Symbol"};}; + }; }; }; }; @@ -682,123 +627,111 @@ export function decodeAtomKind(d: _.TypedDecoder<_ptr>): AtomKind | undefined { return result; } -export function isNamedSimplePattern(v: any): v is NamedSimplePattern {return (isNamedSimplePattern_(v) || isSimplePattern(v));} - -export function asNamedSimplePattern(v: any): NamedSimplePattern { - if (!isNamedSimplePattern(v)) {throw new TypeError(`Invalid NamedSimplePattern: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeNamedSimplePattern(d: _.TypedDecoder<_ptr>): NamedSimplePattern | undefined { - let _tmp0: any; - let result; - _tmp0 = d.mark(); - result = decodeNamedSimplePattern_(d); - if (result === void 0) {d.restoreMark(_tmp0); result = decodeSimplePattern(d);}; +export function asNamedSimplePattern(v: _val): NamedSimplePattern { + let result = toNamedSimplePattern(v); + if (result === void 0) throw new TypeError(`Invalid NamedSimplePattern: ${_.stringify(v)}`); return result; } -export function isNamedPattern(v: any): v is NamedPattern {return (isNamedSimplePattern_(v) || isPattern(v));} - -export function asNamedPattern(v: any): NamedPattern { - if (!isNamedPattern(v)) {throw new TypeError(`Invalid NamedPattern: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeNamedPattern(d: _.TypedDecoder<_ptr>): NamedPattern | undefined { - let _tmp0: any; - let result; - _tmp0 = d.mark(); - result = decodeNamedSimplePattern_(d); - if (result === void 0) {d.restoreMark(_tmp0); result = decodePattern(d);}; +export function toNamedSimplePattern(v: _val): undefined | NamedSimplePattern { + let _tmp0: (NamedSimplePattern_) | undefined; + let result: undefined | NamedSimplePattern; + _tmp0 = toNamedSimplePattern_(v); + if (_tmp0 !== void 0) {result = {"_variant": "named", "value": _tmp0};}; + if (result === void 0) { + let _tmp1: (SimplePattern) | undefined; + _tmp1 = toSimplePattern(v); + if (_tmp1 !== void 0) {result = {"_variant": "anonymous", "value": _tmp1};}; + }; return result; } -export function isNamedSimplePattern_(v: any): v is NamedSimplePattern_ { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $named) && - ((v.length === 2) && typeof v[0] === 'symbol' && isSimplePattern(v[1])) - ); +export function asNamedPattern(v: _val): NamedPattern { + let result = toNamedPattern(v); + if (result === void 0) throw new TypeError(`Invalid NamedPattern: ${_.stringify(v)}`); + return result; } -export function asNamedSimplePattern_(v: any): NamedSimplePattern_ { - if (!isNamedSimplePattern_(v)) {throw new TypeError(`Invalid NamedSimplePattern_: ${_.stringify(v)}`);} else {return v;}; +export function toNamedPattern(v: _val): undefined | NamedPattern { + let _tmp0: (NamedSimplePattern_) | undefined; + let result: undefined | NamedPattern; + _tmp0 = toNamedSimplePattern_(v); + if (_tmp0 !== void 0) {result = {"_variant": "named", "value": _tmp0};}; + if (result === void 0) { + let _tmp1: (Pattern) | undefined; + _tmp1 = toPattern(v); + if (_tmp1 !== void 0) {result = {"_variant": "anonymous", "value": _tmp1};}; + }; + return result; } -export function decodeNamedSimplePattern_(d: _.TypedDecoder<_ptr>): NamedSimplePattern_ | undefined { - let result; - if (d.openRecord()) { - let _tmp0: any; - _tmp0 = _.asLiteral(d.nextSymbol(), $named); +export function asNamedSimplePattern_(v: _val): NamedSimplePattern_ { + let result = toNamedSimplePattern_(v); + if (result === void 0) throw new TypeError(`Invalid NamedSimplePattern_: ${_.stringify(v)}`); + return result; +} + +export function toNamedSimplePattern_(v: _val): undefined | NamedSimplePattern_ { + let result: undefined | NamedSimplePattern_; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $named) ? null : void 0; if (_tmp0 !== void 0) { - let _tmp1, _tmp2, _tmp3: any; - _tmp2 = d.nextSymbol(); - if (_tmp2 !== void 0) { - _tmp3 = decodeSimplePattern(d); - if (_tmp3 !== void 0) {if (d.closeCompound()) _tmp1 = [_tmp2, _tmp3] as [symbol, SimplePattern];}; + let _tmp1: (symbol) | undefined; + _tmp1 = typeof v[0] === 'symbol' ? v[0] : void 0; + if (_tmp1 !== void 0) { + let _tmp2: (SimplePattern) | undefined; + _tmp2 = toSimplePattern(v[1]); + if (_tmp2 !== void 0) {result = {"name": _tmp1, "pattern": _tmp2};}; }; - if (_tmp1 !== void 0) result = _.Record<(typeof $named), [symbol, SimplePattern]>(_tmp0 as any, _tmp1 as any); }; }; return result; } -export function isRef(v: any): v is Ref { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $ref) && - ((v.length === 2) && isModulePath(v[0]) && typeof v[1] === 'symbol') - ); +export function asRef(v: _val): Ref { + let result = toRef(v); + if (result === void 0) throw new TypeError(`Invalid Ref: ${_.stringify(v)}`); + return result; } -export function asRef(v: any): Ref { - if (!isRef(v)) {throw new TypeError(`Invalid Ref: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeRef(d: _.TypedDecoder<_ptr>): Ref | undefined { - let result; - if (d.openRecord()) { - let _tmp0: any; - _tmp0 = _.asLiteral(d.nextSymbol(), $ref); +export function toRef(v: _val): undefined | Ref { + let result: undefined | Ref; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $ref) ? null : void 0; if (_tmp0 !== void 0) { - let _tmp1, _tmp2, _tmp3: any; - _tmp2 = decodeModulePath(d); - if (_tmp2 !== void 0) { - _tmp3 = d.nextSymbol(); - if (_tmp3 !== void 0) {if (d.closeCompound()) _tmp1 = [_tmp2, _tmp3] as [ModulePath, symbol];}; + let _tmp1: (ModulePath) | undefined; + _tmp1 = toModulePath(v[0]); + if (_tmp1 !== void 0) { + let _tmp2: (symbol) | undefined; + _tmp2 = typeof v[1] === 'symbol' ? v[1] : void 0; + if (_tmp2 !== void 0) {result = {"module": _tmp1, "name": _tmp2};}; }; - if (_tmp1 !== void 0) result = _.Record<(typeof $ref), [ModulePath, symbol]>(_tmp0 as any, _tmp1 as any); }; }; return result; } -export function isModulePath(v: any): v is ModulePath { - return ( - _.Array.isArray(v) && - !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - (v.length >= 0) && - v.every(v => (typeof v === 'symbol')) - ); +export function asModulePath(v: _val): ModulePath { + let result = toModulePath(v); + if (result === void 0) throw new TypeError(`Invalid ModulePath: ${_.stringify(v)}`); + return result; } -export function asModulePath(v: any): ModulePath { - if (!isModulePath(v)) {throw new TypeError(`Invalid ModulePath: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeModulePath(d: _.TypedDecoder<_ptr>): ModulePath | undefined { - let result; - if (d.openSequence()) { - let _tmp0: any; - { - let vN: Array | undefined = []; - while (!d.closeCompound()) { - _tmp0 = void 0; - _tmp0 = d.nextSymbol(); - if (_tmp0 === void 0) {vN = void 0; break;}; - vN.push(_tmp0); - }; - result = vN; +export function toModulePath(v: _val): undefined | ModulePath { + let result: undefined | ModulePath; + if (_.Array.isArray(v)) { + let _tmp0: (Array) | undefined; + _tmp0 = []; + for (const _tmp1 of v) { + let _tmp2: (symbol) | undefined; + _tmp2 = typeof _tmp1 === 'symbol' ? _tmp1 : void 0; + if (_tmp2 !== void 0) {_tmp0.push(_tmp2); continue;}; + _tmp0 = void 0; + break; }; + if (_tmp0 !== void 0) {result = _tmp0;}; }; return result; } diff --git a/implementations/javascript/packages/schema/src/meta.ts b/implementations/javascript/packages/schema/src/meta.ts index 7b90ad2..59f9982 100644 --- a/implementations/javascript/packages/schema/src/meta.ts +++ b/implementations/javascript/packages/schema/src/meta.ts @@ -58,14 +58,14 @@ export function lookup(namePos: Position | null, kOther: (modId: string, modPath: string, p: M.Definition | null) => R): R { for (const e of env) { - if (is(e.schemaModulePath, M.Ref._.module(name)) || - (e.typescriptModulePath === null && M.Ref._.module(name).length === 0)) + if (is(e.schemaModulePath, name.module) || + (e.typescriptModulePath === null && name.module.length === 0)) { if (e.schema === null) { // It's an artificial module, not from a schema. Assume the identifier is present. return kOther(modsymFor(e), e.typescriptModulePath, null); } else { - const p = M.Schema._._field0(e.schema).get(M.$definitions).get(M.Ref._.name(name)); + const p = e.schema.definitions.get(name.name); if (p !== void 0) { if (e.typescriptModulePath === null) { return kLocal(p); @@ -81,28 +81,36 @@ export function lookup(namePos: Position | null, } export function formatRef(r: M.Ref): string { - return [... r[0], r[1]].map(s => s.description!).join('.'); + return [... r.module, r.name].map(s => s.description!).join('.'); } -export function unname( - p: M.NamedSimplePattern_ | R): M.SimplePattern | R -{ - return (p.label === M.$named) ? p[1] : p; +export function unnamePattern(p: M.NamedPattern): M.Pattern { + return (p._variant === 'named') ? M.Pattern.SimplePattern(p.value.pattern) : p.value; } -export function nameFor( - p: M.NamedSimplePattern_ | R): string | undefined -{ - return (p.label === M.$named) ? p[0].description! : void 0; +export function unnameSimplePattern(p: M.NamedSimplePattern): M.SimplePattern { + return (p._variant === 'named') ? p.value.pattern : p.value; } -export function addNameIfAbsent(p: M.NamedSimplePattern, k: Input): M.NamedSimplePattern { - if (p.label === M.$named) { +export function promoteNamedSimplePattern(p: M.NamedSimplePattern): M.NamedPattern { + return (p._variant === 'named') ? p : M.NamedPattern.anonymous(M.Pattern.SimplePattern(p.value)); +} + +export function nameFor(p: M.NamedSimplePattern | M.NamedPattern) : string | undefined { + return (p._variant === 'named') ? p.value.name.description! : void 0; +} + +export function anonymousSimplePattern(p: M.SimplePattern): M.NamedPattern { + return M.NamedPattern.anonymous(M.Pattern.SimplePattern(p)); +} + +export function addNameIfAbsent(p: M.NamedSimplePattern, k: M._val): M.NamedSimplePattern { + if (p._variant === 'named') { return p; } else { const s = namelike(k); if (s !== void 0) { - return M.NamedSimplePattern_(Symbol.for(s), p); + return M.NamedSimplePattern.named(M.NamedSimplePattern__(Symbol.for(s), p.value)); } else { return p; } @@ -112,8 +120,8 @@ export function addNameIfAbsent(p: M.NamedSimplePattern, k: Input): M.NamedSimpl // Simple arrays at toplevel for convenience // export function simpleArray(p: M.CompoundPattern): M.SimplePattern | undefined { - if (p.label === M.$tuple_STAR_ && p[0].length === 0 && p[1].label !== M.$named) { - return p[1]; + if (p._variant === 'tuple*' && p.fixed.length === 0 && p.variable._variant !== 'named') { + return p.variable.value; } else { return void 0; } diff --git a/implementations/javascript/packages/schema/src/reader.ts b/implementations/javascript/packages/schema/src/reader.ts index cc5ed36..766d82a 100644 --- a/implementations/javascript/packages/schema/src/reader.ts +++ b/implementations/javascript/packages/schema/src/reader.ts @@ -1,17 +1,17 @@ -import { Reader, Annotated, Dictionary, is, peel, preserves, Record, strip, Tuple, Position, position, ReaderOptions, stringify, isCompound } from '@preserves/core'; +import { Reader, Annotated, Dictionary, is, peel, preserves, Record, strip, Tuple, Position, position, ReaderOptions, stringify, isCompound, KeyedDictionary } from '@preserves/core'; import { Input, Pattern, Schema, Alternative, Definition, CompoundPattern, SimplePattern } from './meta'; import * as M from './meta'; import { SchemaSyntaxError } from './error'; -const positionTable = new WeakMap(); +const positionTable = new WeakMap(); -export function recordPosition(v: X, pos: Position | null): X { +export function recordPosition(v: X, pos: Position | null): X { if (pos === null) { console.error('Internal error in Schema reader: null source position for', v); } if (pos !== null) positionTable.set(v, pos); return v; } -export function refPosition(v: Input & object): Position | null { +export function refPosition(v: object): Position | null { return positionTable.get(v) ?? null; } @@ -65,8 +65,8 @@ export function parseSchema(toplevelTokens: Array, options: ReaderOptions & SchemaReaderOptions): Schema { let version: M.Version | undefined = void 0; - let pointer: M.PointerName = false; - let definitions = new Dictionary(); + let pointer: M.PointerName = M.PointerName.$false(); + let definitions = new KeyedDictionary(); function process(toplevelTokens: Array): void { const toplevelClauses = splitBy(peel(toplevelTokens) as Array, M.DOT); @@ -89,10 +89,13 @@ export function parseSchema(toplevelTokens: Array, } else if (clause.length === 2 && is(clause[0], M.$pointer)) { const pos = position(clause[1]); const stx = peel(clause[1]); - const quasiName = 'pointer name specification'; - pointer = M.asPointerName((stx === false) ? stx - : (typeof stx === 'symbol') ? parseRef(quasiName, pos, stx) - : invalidPattern(quasiName, stx, pos)); + if (stx === false) { + pointer = M.PointerName.$false(); + } else if (typeof stx === 'symbol') { + pointer = M.PointerName.Ref(parseRef(stx.description!, pos)); + } else { + invalidPattern('pointer name specification', stx, pos); + } } else if (clause.length === 2 && is(clause[0], M.INCLUDE)) { const pos = position(clause[1]); const path = peel(clause[1]); @@ -115,11 +118,7 @@ export function parseSchema(toplevelTokens: Array, throw new SchemaSyntaxError("Schema: missing version declaration.", null); } - return M.asSchema(Record(M.$schema, [new Dictionary([ - [M.$version, version], - [M.$pointer, pointer], - [M.$definitions, definitions], - ])])); + return M.Schema(M.Version(), pointer, definitions); } function namedMustBeSimple(p: Position | null): never { @@ -129,32 +128,45 @@ function namedMustBeSimple(p: Position | null): never { function parseDefinition(name: symbol, body: Array): Definition { let nextAnonymousAlternativeNumber = 0; function alternativeName([input, p]: readonly [Array, Alternative]) - : [string, Alternative] + : M.NamedAlternative { const n = findName(input) || findName(input[0]); if (n !== false) { - return [n.description!, p]; + return M.NamedAlternative(n.description!, p); } - if (p.label === M.$rec && p[0].label === M.$lit && typeof p[0][0] === 'symbol') { - return [p[0][0].description!, p]; + if (p._variant === 'Pattern' && + p.value._variant === 'CompoundPattern' && + p.value.value._variant === 'rec' && + p.value.value.label._variant === 'anonymous' && + p.value.value.label.value._variant === 'SimplePattern' && + p.value.value.label.value.value._variant === 'lit' && + typeof p.value.value.label.value.value.value === 'symbol') + { + return M.NamedAlternative(p.value.value.label.value.value.value.description!, p); } - if (p.label === M.$ref) { - return [p[1].description!, p]; + if (p._variant === 'Pattern' && + p.value._variant === 'SimplePattern' && + p.value.value._variant === 'Ref') + { + return M.NamedAlternative(p.value.value.value.name.description!, p); } - if (p.label === M.$lit) { - const s = M.namelike(p[0]); - if (s !== void 0) return [s, p]; + if (p._variant === 'Pattern' && + p.value._variant === 'SimplePattern' && + p.value.value._variant === 'lit') + { + const s = M.namelike(p.value.value.value); + if (s !== void 0) return M.NamedAlternative(s, p); } - return ['_anonymous' + nextAnonymousAlternativeNumber++, p]; + return M.NamedAlternative('_anonymous' + nextAnonymousAlternativeNumber++, p); } function patternName([input, p]: readonly [Array, Pattern]) : M.NamedPattern { const n = findName(input) || findName(input[0]); if (n !== false) { - if (!M.isSimplePattern(p)) namedMustBeSimple(position(input[0])); - return Record(M.$named, [n, p]); + if (p._variant !== 'SimplePattern') namedMustBeSimple(position(input[0])); + return M.NamedPattern.named(M.NamedSimplePattern__(n, p.value)); } - return p; + return M.NamedPattern.anonymous(p); } // TODO: deal with situation where there's an or of ands, where @@ -167,29 +179,32 @@ function parseDefinition(name: symbol, body: Array): Definition { p => [p, parseOp(p, M.ANDSYM, p => [p, parsePattern(name, p)] as const, - ps => Record(M.$and, [ps.map(patternName)]), - p => p[1] as Alternative)] as const, - ps => Record(M.$or, [ps.map(alternativeName)]), - p => p[1] as Definition); + ps => M.Alternative.and(ps.map(patternName)), + p => M.Alternative.Pattern(p[1]))] as const, + ps => M.Definition.or(ps.map(alternativeName)), + p => M.Definition.Alternative(p[1])); } function parsePattern(name: symbol, body0: Array): Pattern { - function parseSimple(item0: Input, kf: () => A): SimplePattern | A { + function parseSimple(item0: Input, ks: (p: SimplePattern) => A, kf: () => A): A { const pos = position(item0); const item = peel(item0); function complain(): never { invalidPattern(stringify(name), item, pos); } if (typeof item === 'symbol') { - switch (item) { - case Symbol.for('any'): return Record(M.$any, []); - case Symbol.for('bool'): return Record(M.$atom, [M.$Boolean]); - case Symbol.for('float'): return Record(M.$atom, [M.$Float]); - case Symbol.for('double'): return Record(M.$atom, [M.$Double]); - case Symbol.for('int'): return Record(M.$atom, [M.$SignedInteger]); - case Symbol.for('string'): return Record(M.$atom, [M.$String]); - case Symbol.for('bytes'): return Record(M.$atom, [M.$ByteString]); - case Symbol.for('symbol'): return Record(M.$atom, [M.$Symbol]); - case Symbol.for('ref'): return Record(M.$pointer, []); - default: return parseRef(stringify(name), pos, item); + const str = item.description!; + switch (str) { + case 'any': return ks(M.SimplePattern.any()); + case 'bool': return ks(M.SimplePattern.atom(M.AtomKind.Boolean())); + case 'float': return ks(M.SimplePattern.atom(M.AtomKind.Float())); + case 'double': return ks(M.SimplePattern.atom(M.AtomKind.Double())); + case 'int': return ks(M.SimplePattern.atom(M.AtomKind.SignedInteger())); + case 'string': return ks(M.SimplePattern.atom(M.AtomKind.String())); + case 'bytes': return ks(M.SimplePattern.atom(M.AtomKind.ByteString())); + case 'symbol': return ks(M.SimplePattern.atom(M.AtomKind.Symbol())); + case 'ref': return ks(M.SimplePattern.pointer()); + default: return ks((str[0] === '=') + ? M.SimplePattern.lit(Symbol.for(str.slice(1))) + : M.SimplePattern.Ref(parseRef(str, pos))); } } else if (Record.isRecord, never>(item)) { const label = item.label; @@ -198,7 +213,7 @@ function parsePattern(name: symbol, body0: Array): Pattern { switch (label.label) { case M.$lit: if (item.length !== 1) complain(); - return Record(M.$lit, [item[0]]); + return ks(M.SimplePattern.lit(item[0])); default: return kf(); } @@ -208,7 +223,7 @@ function parsePattern(name: symbol, body0: Array): Pattern { } else if (isCompound(item)) { return kf(); } else { - return Record(M.$lit, [strip(item)]); + return ks(M.SimplePattern.lit(strip(item))); } } @@ -217,14 +232,16 @@ function parsePattern(name: symbol, body0: Array): Pattern { const item = peel(item0); function complain(): never { invalidPattern(stringify(name), item, pos); } - const walkSimple = (b: Input): SimplePattern => parseSimple(b, () => { + const walkSimple = (b: Input): SimplePattern => parseSimple(b, p => p, () => { throw new SchemaSyntaxError(`Compound patterns not accepted here`, position(b)); }); const walk = (b: Input): Pattern => parsePattern(name, [b]); - function _maybeNamed( - recur: (b: Input) => R, - literalName?: Input): (b: Input) => M.NamedSimplePattern_ | R + function _maybeNamed( + named: (p: M.NamedSimplePattern_) => R, + anonymous: (p: P) => R, + recur: (b: Input) => P, + literalName?: Input): (b: Input) => R { return (b: Input) => { let name = findName(b); @@ -234,14 +251,15 @@ function parsePattern(name: symbol, body0: Array): Pattern { } } if (name === false) { - return recur(b); + return anonymous(recur(b)); } - return Record(M.$named, [name, parseSimple(b, () => - namedMustBeSimple(position(b)))]); + return named(M.NamedSimplePattern__(name, parseSimple(b, p => p, () => + namedMustBeSimple(position(b))))); }; } - const maybeNamed = _maybeNamed(walk); - const maybeNamedSimple = _maybeNamed(walkSimple); + const maybeNamed = _maybeNamed(M.NamedPattern.named, M.NamedPattern.anonymous, walk); + const maybeNamedSimple = + _maybeNamed(M.NamedSimplePattern.named, M.NamedSimplePattern.anonymous, walkSimple); if (Record.isRecord, never>(item)) { const label = item.label; @@ -250,37 +268,47 @@ function parsePattern(name: symbol, body0: Array): Pattern { switch (label.label) { case M.$rec: if (item.length !== 2) complain(); - return Record(M.$rec, [walk(item[0]), walk(item[1])]); + return M.CompoundPattern.rec(maybeNamed(item[0]), maybeNamed(item[1])); default: complain(); } } else { - return Record(M.$rec, [Record(M.$lit, [label]), Record(M.$tuple, [item.map(maybeNamed)])]); + return M.CompoundPattern.rec( + M.NamedPattern.anonymous(M.Pattern.SimplePattern(M.SimplePattern.lit(label))), + M.NamedPattern.anonymous(M.Pattern.CompoundPattern( + M.CompoundPattern.tuple(item.map(maybeNamed))))); } } else if (Array.isArray(item)) { if (is(item[item.length - 1], M.DOTDOTDOT)) { if (item.length < 2) complain(); - return Record(M.$tuple_STAR_, [ + return M.CompoundPattern.tuple_STAR_( item.slice(0, item.length - 2).map(maybeNamed), - maybeNamedSimple(item[item.length - 2]), - ]); + maybeNamedSimple(item[item.length - 2])); } else { - return Record(M.$tuple, [item.map(maybeNamed)]); + return M.CompoundPattern.tuple(item.map(maybeNamed)); } } else if (Dictionary.isDictionary(item)) { if (item.size === 2 && item.has(M.DOTDOTDOT)) { const v = item.clone(); v.delete(M.DOTDOTDOT); const [[kp, vp]] = v.entries(); - return Record(M.$dictof, [walkSimple(kp), walkSimple(vp)]); + return M.CompoundPattern.dictof(walkSimple(kp), walkSimple(vp)); } else { - return Record(M.$dict, [item.mapEntries( - ([k, vp]) => [strip(k), _maybeNamed(walkSimple, k)(vp)])]); + return M.CompoundPattern.dict( + M.DictionaryEntries(item.mapEntries( + ([k, vp]) => [ + strip(k), + _maybeNamed( + M.NamedSimplePattern.named, + M.NamedSimplePattern.anonymous, + walkSimple, + k)(vp) + ]))); } } else if (Set.isSet(item)) { if (item.size !== 1) complain(); const [vp] = item.entries(); - return Record(M.$setof, [walkSimple(vp)]); + return M.CompoundPattern.setof(walkSimple(vp)); } else { complain(); } @@ -290,7 +318,9 @@ function parsePattern(name: symbol, body0: Array): Pattern { if (body.length !== 1) { invalidPattern(stringify(name), body, body.length > 0 ? position(body[0]) : position(body)); } - return parseSimple(body[0], () => parseCompound(body[0])); + return parseSimple(body[0], + M.Pattern.SimplePattern, + () => M.Pattern.CompoundPattern(parseCompound(body[0]))); } function parseOp(body: Array, @@ -312,13 +342,9 @@ function findName(x: Input): symbol | false { return false; } -function parseRef(name: string, pos: Position | null, item: symbol): SimplePattern { - const s = item.description; - if (s === void 0) invalidPattern(name, item, pos); - if (s[0] === '=') return Record(M.$lit, [Symbol.for(s.slice(1))]); +function parseRef(s: string, pos: Position | null): M.Ref { const pieces = s.split('.'); - return recordPosition(Record(M.$ref, [ - pieces.slice(0, pieces.length - 1).map(Symbol.for), - Symbol.for(pieces[pieces.length - 1]) - ]), pos); + return recordPosition(M.Ref( + M.ModulePath(pieces.slice(0, pieces.length - 1).map(Symbol.for)), + Symbol.for(pieces[pieces.length - 1])), pos); }