More preparation for alternate compilation approach

This commit is contained in:
Tony Garnock-Jones 2021-03-17 14:36:44 +01:00
parent ca42ffe832
commit 98346c61d5
5 changed files with 415 additions and 300 deletions

View File

@ -23,7 +23,7 @@ export function compile(env: Environment, schema: Schema, options: CompilerOptio
let temps: Array<string> = [];
let body: Array<Item> = [];
let tempCounter = 0;
const pointerName = Schema._.details(schema).get(M.$pointer);
const pointerName = Schema._._field0(schema).get(M.$pointer);
function gentemp(): string {
const varname = '_tmp' + tempCounter++;
@ -101,7 +101,8 @@ export function compile(env: Environment, schema: Schema, options: CompilerOptio
collectBody(() => loop(i + 1))));
} else {
if (variablePattern === void 0) {
emit(seq(`if (d.closeCompound()) ${dest} = `, brackets(... temps)));
emit(seq(`if (d.closeCompound()) ${dest} = `, brackets(... temps),
` as `, typeFor(tuplePattern)));
} else {
emit(block(
seq(`let vN: `, typeFor(tuplePattern),
@ -332,7 +333,7 @@ export function compile(env: Environment, schema: Schema, options: CompilerOptio
return parens(seq(
block(
... Array.from(p[0]).map(([k, vp]) =>
seq(`get(k: typeof ${literal(k)}): `, typeFor(vp))),
seq(`get(k: typeof ${literal(k)}): `, typeFor(unname(vp)))),
... Array.from(p[0]).map(([k, _vp]) =>
seq(`has(k: typeof ${literal(k)}): true`))),
' & _.Dictionary<_ptr>'));
@ -342,7 +343,7 @@ export function compile(env: Environment, schema: Schema, options: CompilerOptio
}
}
function typeForDefinition(name: symbol, d: Definition): Item {
function typeForDefinition(_name: symbol, d: Definition): Item {
if (d.label === M.$or) {
return opseq('never', ' | ', ... d[0].map(a => typeForAlternative(a[1])));
} else {
@ -443,7 +444,7 @@ export function compile(env: Environment, schema: Schema, options: CompilerOptio
const tmp = gentemp();
return parens(seq(
`(${tmp} = ${v}.get(${literal(k)})) !== void 0 && `,
predicateFor(tmp, vp)));
predicateFor(tmp, unname(vp))));
}));
default:
((_p: never) => {})(p);
@ -463,7 +464,7 @@ export function compile(env: Environment, schema: Schema, options: CompilerOptio
return seq(JSON.stringify(fieldName(np, index)), ': ', typeFor(unname(np)));
}
for (const [name0, def] of Schema._.details(schema).get(M.$definitions)) {
for (const [name0, def] of Schema._._field0(schema).get(M.$definitions)) {
const name = name0 as symbol;
temps = [];
const recognizer = predicateFor('v', def);

View File

@ -27,7 +27,7 @@ export const $version = Symbol.for("version");
export const __lit5 = false;
export const Schema = _.Record.makeConstructor<{
"details": (
"_field0": (
{
get(k: typeof $version): Version;
get(k: typeof $pointer): PointerName;
@ -37,7 +37,7 @@ export const Schema = _.Record.makeConstructor<{
has(k: typeof $definitions): true;
} & _.Dictionary<_ptr>
)
}, _ptr>()($schema, ["details"]);
}, _ptr>()($schema, ["_field0"]);
export type Schema = _.Record<
(typeof $schema),
@ -60,23 +60,32 @@ export type Version = (typeof $1);
export type PointerName = (Ref | (typeof __lit5));
export type Definition = (_.Record<(typeof $or), [Array<[symbol, Alternative]>], _ptr> | Alternative);
export type Definition = (_.Record<(typeof $or), [Array<NamedAlternative>], _ptr> | Alternative);
export type NamedAlternative = [symbol, Alternative];
export type Alternative = (_.Record<(typeof $and), [Array<Pattern>], _ptr> | Pattern);
export type Pattern = (
export type Pattern = (SimplePattern | CompoundPattern);
export type SimplePattern = (
_.Record<(typeof $atom), [AtomKind], _ptr> |
_.Record<(typeof $pointer), [], _ptr> |
_.Record<(typeof $lit), [_val], _ptr> |
Ref |
Ref
);
export type CompoundPattern = (
_.Record<(typeof $rec), [Pattern, Pattern], _ptr> |
_.Record<(typeof $tuple), [Array<NamedPattern>], _ptr> |
_.Record<(typeof $tuple_STAR_), [Array<NamedPattern>, NamedPattern], _ptr> |
_.Record<(typeof $setof), [Pattern], _ptr> |
_.Record<(typeof $dictof), [Pattern, Pattern], _ptr> |
_.Record<(typeof $dict), [_.KeyedDictionary<_val, Pattern, _ptr>], _ptr>
_.Record<(typeof $setof), [SimplePattern], _ptr> |
_.Record<(typeof $dictof), [SimplePattern, SimplePattern], _ptr> |
_.Record<(typeof $dict), [DictionaryEntries], _ptr>
);
export type DictionaryEntries = _.KeyedDictionary<_val, NamedPattern, _ptr>;
export type AtomKind = (
(typeof $Boolean) |
(typeof $Float) |
@ -87,7 +96,7 @@ export type AtomKind = (
(typeof $Symbol)
);
export type NamedPattern = (_.Record<(typeof $named), [symbol, Pattern], _ptr> | Pattern);
export type NamedPattern = (_.Record<(typeof $named), [symbol, SimplePattern], _ptr> | Pattern);
export const Ref = _.Record.makeConstructor<{"module": ModulePath, "name": symbol}, _ptr>()($ref, ["module","name"]);
@ -157,7 +166,20 @@ export function decodeSchema(d: _.TypedDecoder<_ptr>): Schema | undefined {
)
)
))) _tmp5 = void 0;
if (_tmp5 !== void 0) {if (d.closeCompound()) _tmp4 = [_tmp5];};
if (_tmp5 !== void 0) {
if (d.closeCompound()) _tmp4 = [_tmp5] as [
(
{
get(k: typeof $version): Version;
get(k: typeof $pointer): PointerName;
get(k: typeof $definitions): _.KeyedDictionary<symbol, Definition, _ptr>;
has(k: typeof $version): true;
has(k: typeof $pointer): true;
has(k: typeof $definitions): true;
} & _.Dictionary<_ptr>
)
];
};
if (_tmp4 !== void 0) result = _.Record<
(typeof $schema),
[
@ -212,15 +234,7 @@ export function isDefinition(v: any): v is Definition {
_.Array.isArray(v[0]) &&
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) &&
(v[0].length >= 0) &&
v[0].every(v => (
(
_.Array.isArray(v) &&
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
(v.length === 2) &&
typeof v[0] === 'symbol' &&
isAlternative(v[1])
)
))
v[0].every(v => (isNamedAlternative(v)))
)
)
) ||
@ -244,31 +258,51 @@ export function decodeDefinition(d: _.TypedDecoder<_ptr>): Definition | undefine
if (d.openSequence()) {
let _tmp14: any;
{
let vN: Array<[symbol, Alternative]> | undefined = [];
let vN: Array<NamedAlternative> | undefined = [];
while (!d.closeCompound()) {
let _tmp15, _tmp16: any;
_tmp14 = void 0;
if (d.openSequence()) {
_tmp15 = d.nextSymbol();
if (_tmp15 !== void 0) {
_tmp16 = decodeAlternative(d);
if (_tmp16 !== void 0) {if (d.closeCompound()) _tmp14 = [_tmp15, _tmp16];};
};
};
_tmp14 = decodeNamedAlternative(d);
if (_tmp14 === void 0) {vN = void 0; break;};
vN.push(_tmp14);
};
_tmp13 = vN;
};
};
if (_tmp13 !== void 0) {if (d.closeCompound()) _tmp12 = [_tmp13];};
if (_tmp12 !== void 0) result = _.Record<(typeof $or), [Array<[symbol, Alternative]>]>(_tmp11 as any, _tmp12 as any);
if (_tmp13 !== void 0) {if (d.closeCompound()) _tmp12 = [_tmp13] as [Array<NamedAlternative>];};
if (_tmp12 !== void 0) result = _.Record<(typeof $or), [Array<NamedAlternative>]>(_tmp11 as any, _tmp12 as any);
};
};
if (result === void 0) {d.restoreMark(_tmp10); 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] === 'symbol' &&
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 _tmp15, _tmp16: any;
let result;
if (d.openSequence()) {
_tmp15 = d.nextSymbol();
if (_tmp15 !== void 0) {
_tmp16 = decodeAlternative(d);
if (_tmp16 !== void 0) {if (d.closeCompound()) result = [_tmp15, _tmp16] as [symbol, Alternative];};
};
};
return result;
}
export function isAlternative(v: any): v is Alternative {
return (
(
@ -314,7 +348,7 @@ export function decodeAlternative(d: _.TypedDecoder<_ptr>): Alternative | undefi
_tmp20 = vN;
};
};
if (_tmp20 !== void 0) {if (d.closeCompound()) _tmp19 = [_tmp20];};
if (_tmp20 !== void 0) {if (d.closeCompound()) _tmp19 = [_tmp20] as [Array<Pattern>];};
if (_tmp19 !== void 0) result = _.Record<(typeof $and), [Array<Pattern>]>(_tmp18 as any, _tmp19 as any);
};
};
@ -322,7 +356,22 @@ export function decodeAlternative(d: _.TypedDecoder<_ptr>): Alternative | undefi
return result;
}
export function isPattern(v: any): v is Pattern {
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 _tmp22: any;
let result;
_tmp22 = d.mark();
result = decodeSimplePattern(d);
if (result === void 0) {d.restoreMark(_tmp22); result = decodeCompoundPattern(d);};
return result;
}
export function isSimplePattern(v: any): v is SimplePattern {
return _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && (
(
(_.is(v.label, $atom) && ((v.length === 1) && isAtomKind(v[0]))) ||
@ -330,7 +379,63 @@ export function isPattern(v: any): v is Pattern {
(_.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 _tmp23, _tmp24: any;
_tmp23 = d.next();
_tmp24 = d.mark();
if (_.is(_tmp23, $atom)) {
let _tmp25, _tmp26: any;
_tmp26 = decodeAtomKind(d);
if (_tmp26 !== void 0) {if (d.closeCompound()) _tmp25 = [_tmp26] as [AtomKind];};
if (_tmp25 !== void 0) result = _.Record<(typeof $atom), [AtomKind]>(_tmp23 as any, _tmp25 as any);
};
if (result === void 0) {
d.restoreMark(_tmp24);
if (_.is(_tmp23, $pointer)) {
let _tmp27: any;
if (d.closeCompound()) _tmp27 = [] as [];
if (_tmp27 !== void 0) result = _.Record<(typeof $pointer), []>(_tmp23 as any, _tmp27 as any);
};
if (result === void 0) {
d.restoreMark(_tmp24);
if (_.is(_tmp23, $lit)) {
let _tmp28, _tmp29: any;
_tmp29 = d.next();
if (_tmp29 !== void 0) {if (d.closeCompound()) _tmp28 = [_tmp29] as [_val];};
if (_tmp28 !== void 0) result = _.Record<(typeof $lit), [_val]>(_tmp23 as any, _tmp28 as any);
};
if (result === void 0) {
d.restoreMark(_tmp24);
if (_.is(_tmp23, $ref)) {
let _tmp30, _tmp31, _tmp32: any;
_tmp31 = decodeModulePath(d);
if (_tmp31 !== void 0) {
_tmp32 = d.nextSymbol();
if (_tmp32 !== void 0) {if (d.closeCompound()) _tmp30 = [_tmp31, _tmp32] as [ModulePath, symbol];};
};
if (_tmp30 !== void 0) result = _.Record<(typeof $ref), [ModulePath, symbol]>(_tmp23 as any, _tmp30 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) && isPattern(v[0]) && isPattern(v[1]))
) ||
@ -357,164 +462,107 @@ export function isPattern(v: any): v is Pattern {
isNamedPattern(v[1])
)
) ||
(_.is(v.label, $setof) && ((v.length === 1) && isPattern(v[0]))) ||
(_.is(v.label, $setof) && ((v.length === 1) && isSimplePattern(v[0]))) ||
(
_.is(v.label, $dictof) && ((v.length === 2) && isPattern(v[0]) && isPattern(v[1]))
_.is(v.label, $dictof) && ((v.length === 2) && isSimplePattern(v[0]) && isSimplePattern(v[1]))
) ||
(
_.is(v.label, $dict) && (
(v.length === 1) &&
(
_.Dictionary.isDictionary<_ptr>(v[0]) &&
((() => {
for (const e of v[0]) {if (!(true)) return false; if (!(isPattern(e[1]))) return false;};
return true;
})())
)
)
)
(_.is(v.label, $dict) && ((v.length === 1) && isDictionaryEntries(v[0])))
)
);
}
export function asPattern(v: any): Pattern {
if (!isPattern(v)) {throw new TypeError(`Invalid Pattern: ${_.stringify(v)}`);} else {return v;};
export function asCompoundPattern(v: any): CompoundPattern {
if (!isCompoundPattern(v)) {throw new TypeError(`Invalid CompoundPattern: ${_.stringify(v)}`);} else {return v;};
}
export function decodePattern(d: _.TypedDecoder<_ptr>): Pattern | undefined {
export function decodeCompoundPattern(d: _.TypedDecoder<_ptr>): CompoundPattern | undefined {
let result;
if (d.openRecord()) {
let _tmp22, _tmp23: any;
_tmp22 = d.next();
_tmp23 = d.mark();
if (_.is(_tmp22, $atom)) {
let _tmp24, _tmp25: any;
_tmp25 = decodeAtomKind(d);
if (_tmp25 !== void 0) {if (d.closeCompound()) _tmp24 = [_tmp25];};
if (_tmp24 !== void 0) result = _.Record<(typeof $atom), [AtomKind]>(_tmp22 as any, _tmp24 as any);
let _tmp33, _tmp34: any;
_tmp33 = d.next();
_tmp34 = d.mark();
if (_.is(_tmp33, $rec)) {
let _tmp35, _tmp36, _tmp37: any;
_tmp36 = decodePattern(d);
if (_tmp36 !== void 0) {
_tmp37 = decodePattern(d);
if (_tmp37 !== void 0) {if (d.closeCompound()) _tmp35 = [_tmp36, _tmp37] as [Pattern, Pattern];};
};
if (_tmp35 !== void 0) result = _.Record<(typeof $rec), [Pattern, Pattern]>(_tmp33 as any, _tmp35 as any);
};
if (result === void 0) {
d.restoreMark(_tmp23);
if (_.is(_tmp22, $pointer)) {
let _tmp26: any;
if (d.closeCompound()) _tmp26 = [];
if (_tmp26 !== void 0) result = _.Record<(typeof $pointer), []>(_tmp22 as any, _tmp26 as any);
d.restoreMark(_tmp34);
if (_.is(_tmp33, $tuple)) {
let _tmp38, _tmp39: any;
if (d.openSequence()) {
let _tmp40: any;
{
let vN: Array<NamedPattern> | undefined = [];
while (!d.closeCompound()) {
_tmp40 = void 0;
_tmp40 = decodeNamedPattern(d);
if (_tmp40 === void 0) {vN = void 0; break;};
vN.push(_tmp40);
};
_tmp39 = vN;
};
};
if (_tmp39 !== void 0) {if (d.closeCompound()) _tmp38 = [_tmp39] as [Array<NamedPattern>];};
if (_tmp38 !== void 0) result = _.Record<(typeof $tuple), [Array<NamedPattern>]>(_tmp33 as any, _tmp38 as any);
};
if (result === void 0) {
d.restoreMark(_tmp23);
if (_.is(_tmp22, $lit)) {
let _tmp27, _tmp28: any;
_tmp28 = d.next();
if (_tmp28 !== void 0) {if (d.closeCompound()) _tmp27 = [_tmp28];};
if (_tmp27 !== void 0) result = _.Record<(typeof $lit), [_val]>(_tmp22 as any, _tmp27 as any);
d.restoreMark(_tmp34);
if (_.is(_tmp33, $tuple_STAR_)) {
let _tmp41, _tmp42, _tmp43: any;
if (d.openSequence()) {
let _tmp44: any;
{
let vN: Array<NamedPattern> | undefined = [];
while (!d.closeCompound()) {
_tmp44 = void 0;
_tmp44 = decodeNamedPattern(d);
if (_tmp44 === void 0) {vN = void 0; break;};
vN.push(_tmp44);
};
_tmp42 = vN;
};
};
if (_tmp42 !== void 0) {
_tmp43 = decodeNamedPattern(d);
if (_tmp43 !== void 0) {
if (d.closeCompound()) _tmp41 = [_tmp42, _tmp43] as [Array<NamedPattern>, NamedPattern];
};
};
if (_tmp41 !== void 0) result = _.Record<(typeof $tuple_STAR_), [Array<NamedPattern>, NamedPattern]>(_tmp33 as any, _tmp41 as any);
};
if (result === void 0) {
d.restoreMark(_tmp23);
if (_.is(_tmp22, $ref)) {
let _tmp29, _tmp30, _tmp31: any;
_tmp30 = decodeModulePath(d);
if (_tmp30 !== void 0) {
_tmp31 = d.nextSymbol();
if (_tmp31 !== void 0) {if (d.closeCompound()) _tmp29 = [_tmp30, _tmp31];};
};
if (_tmp29 !== void 0) result = _.Record<(typeof $ref), [ModulePath, symbol]>(_tmp22 as any, _tmp29 as any);
d.restoreMark(_tmp34);
if (_.is(_tmp33, $setof)) {
let _tmp45, _tmp46: any;
_tmp46 = decodeSimplePattern(d);
if (_tmp46 !== void 0) {if (d.closeCompound()) _tmp45 = [_tmp46] as [SimplePattern];};
if (_tmp45 !== void 0) result = _.Record<(typeof $setof), [SimplePattern]>(_tmp33 as any, _tmp45 as any);
};
if (result === void 0) {
d.restoreMark(_tmp23);
if (_.is(_tmp22, $rec)) {
let _tmp32, _tmp33, _tmp34: any;
_tmp33 = decodePattern(d);
if (_tmp33 !== void 0) {
_tmp34 = decodePattern(d);
if (_tmp34 !== void 0) {if (d.closeCompound()) _tmp32 = [_tmp33, _tmp34];};
d.restoreMark(_tmp34);
if (_.is(_tmp33, $dictof)) {
let _tmp47, _tmp48, _tmp49: any;
_tmp48 = decodeSimplePattern(d);
if (_tmp48 !== void 0) {
_tmp49 = decodeSimplePattern(d);
if (_tmp49 !== void 0) {
if (d.closeCompound()) _tmp47 = [_tmp48, _tmp49] as [SimplePattern, SimplePattern];
};
};
if (_tmp32 !== void 0) result = _.Record<(typeof $rec), [Pattern, Pattern]>(_tmp22 as any, _tmp32 as any);
if (_tmp47 !== void 0) result = _.Record<(typeof $dictof), [SimplePattern, SimplePattern]>(_tmp33 as any, _tmp47 as any);
};
if (result === void 0) {
d.restoreMark(_tmp23);
if (_.is(_tmp22, $tuple)) {
let _tmp35, _tmp36: any;
if (d.openSequence()) {
let _tmp37: any;
{
let vN: Array<NamedPattern> | undefined = [];
while (!d.closeCompound()) {
_tmp37 = void 0;
_tmp37 = decodeNamedPattern(d);
if (_tmp37 === void 0) {vN = void 0; break;};
vN.push(_tmp37);
};
_tmp36 = vN;
};
};
if (_tmp36 !== void 0) {if (d.closeCompound()) _tmp35 = [_tmp36];};
if (_tmp35 !== void 0) result = _.Record<(typeof $tuple), [Array<NamedPattern>]>(_tmp22 as any, _tmp35 as any);
};
if (result === void 0) {
d.restoreMark(_tmp23);
if (_.is(_tmp22, $tuple_STAR_)) {
let _tmp38, _tmp39, _tmp40: any;
if (d.openSequence()) {
let _tmp41: any;
{
let vN: Array<NamedPattern> | undefined = [];
while (!d.closeCompound()) {
_tmp41 = void 0;
_tmp41 = decodeNamedPattern(d);
if (_tmp41 === void 0) {vN = void 0; break;};
vN.push(_tmp41);
};
_tmp39 = vN;
};
};
if (_tmp39 !== void 0) {
_tmp40 = decodeNamedPattern(d);
if (_tmp40 !== void 0) {if (d.closeCompound()) _tmp38 = [_tmp39, _tmp40];};
};
if (_tmp38 !== void 0) result = _.Record<(typeof $tuple_STAR_), [Array<NamedPattern>, NamedPattern]>(_tmp22 as any, _tmp38 as any);
};
if (result === void 0) {
d.restoreMark(_tmp23);
if (_.is(_tmp22, $setof)) {
let _tmp42, _tmp43: any;
_tmp43 = decodePattern(d);
if (_tmp43 !== void 0) {if (d.closeCompound()) _tmp42 = [_tmp43];};
if (_tmp42 !== void 0) result = _.Record<(typeof $setof), [Pattern]>(_tmp22 as any, _tmp42 as any);
};
if (result === void 0) {
d.restoreMark(_tmp23);
if (_.is(_tmp22, $dictof)) {
let _tmp44, _tmp45, _tmp46: any;
_tmp45 = decodePattern(d);
if (_tmp45 !== void 0) {
_tmp46 = decodePattern(d);
if (_tmp46 !== void 0) {if (d.closeCompound()) _tmp44 = [_tmp45, _tmp46];};
};
if (_tmp44 !== void 0) result = _.Record<(typeof $dictof), [Pattern, Pattern]>(_tmp22 as any, _tmp44 as any);
};
if (result === void 0) {
d.restoreMark(_tmp23);
if (_.is(_tmp22, $dict)) {
let _tmp47, _tmp48: any;
if (d.openDictionary()) {
let r: _.KeyedDictionary<_val, Pattern, _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 | Pattern = void 0;
V = decodePattern(d);
if (V === void 0) { r = void 0; break; };
r.set(K, V);
};
_tmp48 = r;
};
if (_tmp48 !== void 0) {if (d.closeCompound()) _tmp47 = [_tmp48];};
if (_tmp47 !== void 0) result = _.Record<(typeof $dict), [_.KeyedDictionary<_val, Pattern, _ptr>]>(_tmp22 as any, _tmp47 as any);
};
};
};
};
d.restoreMark(_tmp34);
if (_.is(_tmp33, $dict)) {
let _tmp50, _tmp51: any;
_tmp51 = decodeDictionaryEntries(d);
if (_tmp51 !== void 0) {if (d.closeCompound()) _tmp50 = [_tmp51] as [DictionaryEntries];};
if (_tmp50 !== void 0) result = _.Record<(typeof $dict), [DictionaryEntries]>(_tmp33 as any, _tmp50 as any);
};
};
};
@ -525,6 +573,38 @@ export function decodePattern(d: _.TypedDecoder<_ptr>): Pattern | undefined {
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 (!(isNamedPattern(e[1]))) return false;};
return true;
})())
);
}
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, NamedPattern, _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 | NamedPattern = void 0;
V = decodeNamedPattern(d);
if (V === void 0) { r = void 0; break; };
r.set(K, V);
};
result = r;
};
return result;
}
export function isAtomKind(v: any): v is AtomKind {
return (
_.is(v, $Boolean) ||
@ -542,26 +622,26 @@ export function asAtomKind(v: any): AtomKind {
}
export function decodeAtomKind(d: _.TypedDecoder<_ptr>): AtomKind | undefined {
let _tmp49: any;
let _tmp52: any;
let result;
_tmp49 = d.mark();
_tmp52 = d.mark();
result = _.asLiteral(d.nextSymbol(), $Boolean);
if (result === void 0) {
d.restoreMark(_tmp49);
d.restoreMark(_tmp52);
result = _.asLiteral(d.nextSymbol(), $Float);
if (result === void 0) {
d.restoreMark(_tmp49);
d.restoreMark(_tmp52);
result = _.asLiteral(d.nextSymbol(), $Double);
if (result === void 0) {
d.restoreMark(_tmp49);
d.restoreMark(_tmp52);
result = _.asLiteral(d.nextSymbol(), $SignedInteger);
if (result === void 0) {
d.restoreMark(_tmp49);
d.restoreMark(_tmp52);
result = _.asLiteral(d.nextSymbol(), $String);
if (result === void 0) {
d.restoreMark(_tmp49);
d.restoreMark(_tmp52);
result = _.asLiteral(d.nextSymbol(), $ByteString);
if (result === void 0) {d.restoreMark(_tmp49); result = _.asLiteral(d.nextSymbol(), $Symbol);};
if (result === void 0) {d.restoreMark(_tmp52); result = _.asLiteral(d.nextSymbol(), $Symbol);};
};
};
};
@ -575,7 +655,7 @@ export function isNamedPattern(v: any): v is NamedPattern {
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $named) &&
((v.length === 2) && typeof v[0] === 'symbol' && isPattern(v[1]))
((v.length === 2) && typeof v[0] === 'symbol' && isSimplePattern(v[1]))
) ||
isPattern(v)
);
@ -586,23 +666,23 @@ export function asNamedPattern(v: any): NamedPattern {
}
export function decodeNamedPattern(d: _.TypedDecoder<_ptr>): NamedPattern | undefined {
let _tmp50: any;
let _tmp53: any;
let result;
_tmp50 = d.mark();
_tmp53 = d.mark();
if (d.openRecord()) {
let _tmp51: any;
_tmp51 = _.asLiteral(d.nextSymbol(), $named);
if (_tmp51 !== void 0) {
let _tmp52, _tmp53, _tmp54: any;
_tmp53 = d.nextSymbol();
if (_tmp53 !== void 0) {
_tmp54 = decodePattern(d);
if (_tmp54 !== void 0) {if (d.closeCompound()) _tmp52 = [_tmp53, _tmp54];};
let _tmp54: any;
_tmp54 = _.asLiteral(d.nextSymbol(), $named);
if (_tmp54 !== void 0) {
let _tmp55, _tmp56, _tmp57: any;
_tmp56 = d.nextSymbol();
if (_tmp56 !== void 0) {
_tmp57 = decodeSimplePattern(d);
if (_tmp57 !== void 0) {if (d.closeCompound()) _tmp55 = [_tmp56, _tmp57] as [symbol, SimplePattern];};
};
if (_tmp52 !== void 0) result = _.Record<(typeof $named), [symbol, Pattern]>(_tmp51 as any, _tmp52 as any);
if (_tmp55 !== void 0) result = _.Record<(typeof $named), [symbol, SimplePattern]>(_tmp54 as any, _tmp55 as any);
};
};
if (result === void 0) {d.restoreMark(_tmp50); result = decodePattern(d);};
if (result === void 0) {d.restoreMark(_tmp53); result = decodePattern(d);};
return result;
}
@ -621,16 +701,16 @@ export function asRef(v: any): Ref {
export function decodeRef(d: _.TypedDecoder<_ptr>): Ref | undefined {
let result;
if (d.openRecord()) {
let _tmp55: any;
_tmp55 = _.asLiteral(d.nextSymbol(), $ref);
if (_tmp55 !== void 0) {
let _tmp56, _tmp57, _tmp58: any;
_tmp57 = decodeModulePath(d);
if (_tmp57 !== void 0) {
_tmp58 = d.nextSymbol();
if (_tmp58 !== void 0) {if (d.closeCompound()) _tmp56 = [_tmp57, _tmp58];};
let _tmp58: any;
_tmp58 = _.asLiteral(d.nextSymbol(), $ref);
if (_tmp58 !== void 0) {
let _tmp59, _tmp60, _tmp61: any;
_tmp60 = decodeModulePath(d);
if (_tmp60 !== void 0) {
_tmp61 = d.nextSymbol();
if (_tmp61 !== void 0) {if (d.closeCompound()) _tmp59 = [_tmp60, _tmp61] as [ModulePath, symbol];};
};
if (_tmp56 !== void 0) result = _.Record<(typeof $ref), [ModulePath, symbol]>(_tmp55 as any, _tmp56 as any);
if (_tmp59 !== void 0) result = _.Record<(typeof $ref), [ModulePath, symbol]>(_tmp58 as any, _tmp59 as any);
};
};
return result;
@ -652,14 +732,14 @@ export function asModulePath(v: any): ModulePath {
export function decodeModulePath(d: _.TypedDecoder<_ptr>): ModulePath | undefined {
let result;
if (d.openSequence()) {
let _tmp59: any;
let _tmp62: any;
{
let vN: Array<symbol> | undefined = [];
while (!d.closeCompound()) {
_tmp59 = void 0;
_tmp59 = d.nextSymbol();
if (_tmp59 === void 0) {vN = void 0; break;};
vN.push(_tmp59);
_tmp62 = void 0;
_tmp62 = d.nextSymbol();
if (_tmp62 === void 0) {vN = void 0; break;};
vN.push(_tmp62);
};
result = vN;
};

View File

@ -53,7 +53,7 @@ export function lookup<R>(namePos: Position | 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 = Schema._.details(e.schema).get($definitions).get(Ref._.name(name));
const p = Schema._._field0(e.schema).get($definitions).get(Ref._.name(name));
if (p !== void 0) {
if (e.typescriptModulePath === null) {
return kLocal(p);
@ -66,7 +66,7 @@ export function lookup<R>(namePos: Position | null,
}
if (Ref._.module(name).length === 0) {
const p = Schema._.details(BASE).get($definitions).get(Ref._.name(name));
const p = Schema._._field0(BASE).get($definitions).get(Ref._.name(name));
if (p !== void 0) return kBase(p as Alternative);
}

View File

@ -1,7 +1,8 @@
import { Reader, Annotated, Dictionary, is, peel, preserves, Record, strip, Tuple, Position, position, ReaderOptions, stringify } from '@preserves/core';
import { Reader, Annotated, Dictionary, is, peel, preserves, Record, strip, Tuple, Position, position, ReaderOptions, stringify, isCompound } from '@preserves/core';
import { Input, NamedPattern, Pattern, Schema, Alternative, Definition } from './meta';
import * as M from './meta';
import { SchemaSyntaxError } from './error';
import { CompoundPattern, SimplePattern } from 'gen/schema';
const positionTable = new WeakMap<Input & object, Position>();
@ -123,7 +124,7 @@ export function parseSchema(toplevelTokens: Array<Input>,
}
function parseDefinition(name: symbol, body: Array<Input>): Definition {
let nextAnonymousFieldNumber = 0;
let nextAnonymousAlternativeNumber = 0;
function alternativeName([input, p]: readonly [Array<Input>, Alternative])
: [symbol, Alternative]
{
@ -140,20 +141,116 @@ function parseDefinition(name: symbol, body: Array<Input>): Definition {
if (p.label === M.$lit && typeof p[0] === 'symbol') {
return [p[0], p];
}
return [Symbol.for('_anonymous' + nextAnonymousFieldNumber++), p];
return [Symbol.for('_anonymous' + nextAnonymousAlternativeNumber++), p];
}
return parseOp(body,
M.ORSYM,
p => [p, parseOp(p,
M.ANDSYM,
p => parseBase(name, p),
p => parsePattern(name, p),
ps => Record(M.$and, [ps]),
p => p as Alternative)] as const,
ps => Record(M.$or, [ps.map(alternativeName)]),
p => p[1] as Definition);
}
function parsePattern(name: symbol, body0: Array<Input>): Pattern {
function parseSimple<A>(item0: Input, kf: () => A): SimplePattern | A {
const pos = position(item0);
const item = peel(item0);
function complain(): never { invalidPattern(stringify(name), item, pos); }
if (typeof item === 'symbol') {
return parseRef(stringify(name), pos, item);
} else if (Record.isRecord<Input, Tuple<Input>, never>(item)) {
const label = item.label;
if (Record.isRecord<Input, [], never>(label)) {
if (label.length !== 0) complain();
switch (label.label) {
case M.$lit:
if (item.length !== 1) complain();
return Record(M.$lit, [item[0]]);
default:
return kf();
}
} else {
return kf();
}
} else if (isCompound(item)) {
return kf();
} else {
return Record(M.$lit, [strip(item)]);
}
}
function parseCompound(item0: Input): CompoundPattern {
const pos = position(item0);
const item = peel(item0);
function complain(): never { invalidPattern(stringify(name), item, pos); }
const walkSimple = (b: Input): SimplePattern => parseSimple(b, () => {
throw new SchemaSyntaxError(`Compound patterns not accepted here`, position(b));
});
const walk = (b: Input): Pattern => parsePattern(name, [b]);
function maybeNamed(b: Input): NamedPattern {
const name = findName(b);
if (name === false) return walk(b);
return Record(M.$named, [name, parseSimple(b, () => {
throw new SchemaSyntaxError(`Named patterns must be Simple patterns`, position(b));
})]);
}
if (Record.isRecord<Input, Tuple<Input>, never>(item)) {
const label = item.label;
if (Record.isRecord<Input, [], never>(label)) {
if (label.length !== 0) complain();
switch (label.label) {
case M.$rec:
if (item.length !== 2) complain();
return Record(M.$rec, [walk(item[0]), walk(item[1])]);
default:
complain();
}
} else {
return Record(M.$rec, [Record(M.$lit, [label]), Record(M.$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_, [
item.slice(0, item.length - 2).map(maybeNamed),
maybeNamed(item[item.length - 2]),
]);
} else {
return Record(M.$tuple, [item.map(maybeNamed)]);
}
} else if (Dictionary.isDictionary<never, Input>(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)]);
} else {
return Record(M.$dict, [item.mapEntries<Pattern, Input, never>(
([k, vp]) => [strip(k), walk(vp)])]);
}
} else if (Set.isSet<never>(item)) {
if (item.size !== 1) complain();
const [vp] = item.entries();
return Record(M.$setof, [walkSimple(vp)]);
} else {
complain();
}
}
const body = peel(body0) as Array<Input>;
if (body.length !== 1) {
invalidPattern(stringify(name), body, body.length > 0 ? position(body[0]) : position(body));
}
return parseSimple(body[0], () => parseCompound(body[0]));
}
function parseOp<Each, Combined>(body: Array<Input>,
op: Input,
each: (p: Array<Input>) => Each,
@ -173,15 +270,7 @@ function findName(x: Input): symbol | false {
return false;
}
function namedwrap(f: (b: Input) => Pattern): (b: Input) => NamedPattern {
return (b: Input) => {
const name = findName(b);
if (name === false) return f(b);
return Record(M.$named, [name, f(b)]);
}
}
function parseRef(name: string, pos: Position | null, item: symbol): Pattern {
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))]);
@ -191,66 +280,3 @@ function parseRef(name: string, pos: Position | null, item: symbol): Pattern {
Symbol.for(pieces[pieces.length - 1])
]), pos);
}
function parseBase(name: symbol, body: Array<Input>): Pattern {
body = peel(body) as Array<Input>;
if (body.length !== 1) {
invalidPattern(stringify(name), body, body.length > 0 ? position(body[0]) : position(body));
}
const pos = position(body[0]);
const item = peel(body[0]);
const walk = (b: Input): Pattern => parseBase(name, [b]);
function complain(): never {
invalidPattern(stringify(name), item, pos);
}
if (typeof item === 'symbol') {
return parseRef(stringify(name), position(body[0]), item);
} else if (Record.isRecord<Input, Tuple<Input>, never>(item)) {
const label = item.label;
if (Record.isRecord<Input, [], never>(label)) {
if (label.length !== 0) complain();
switch (label.label) {
case M.$lit:
if (item.length !== 1) complain();
return Record(M.$lit, [item[0]]);
case M.$rec:
if (item.length !== 2) complain();
return Record(M.$rec, [walk(item[0]), walk(item[1])]);
default:
complain();
}
} else {
return Record(M.$rec, [Record(M.$lit, [label]), Record(M.$tuple, [item.map(namedwrap(walk))])]);
}
} else if (Array.isArray(item)) {
if (is(item[item.length - 1], M.DOTDOTDOT)) {
if (item.length < 2) complain();
return Record(M.$tuple_STAR_, [
item.slice(0, item.length - 2).map(namedwrap(walk)),
namedwrap(walk)(item[item.length - 2]),
]);
} else {
return Record(M.$tuple, [item.map(namedwrap(walk))]);
}
} else if (Dictionary.isDictionary<never, Input>(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, [walk(kp), walk(vp)]);
} else {
return Record(M.$dict, [item.mapEntries<Pattern, Input, never>(([k, vp]) =>
[strip(k), walk(vp)])]);
}
} else if (Set.isSet<never>(item)) {
if (item.size !== 1) complain();
const [vp] = item.entries();
return Record(M.$setof, [walk(vp)]);
} else {
return Record(M.$lit, [strip(item)]);
}
}

View File

@ -2,7 +2,7 @@
version 1 .
Schema = <schema @details {
Schema = <schema {
version: Version
pointer: PointerName
definitions: { symbol: Definition ...:... }
@ -15,14 +15,18 @@ PointerName = Ref / #f.
; Pattern / Pattern / ...
; and the empty pattern is <or []>
Definition = <or @patterns [[@name symbol Alternative] ...]> / Alternative .
Definition = <or [@patterns NamedAlternative ...]> / Alternative .
NamedAlternative = [@name symbol @alternative Alternative].
; Pattern & Pattern & ...
; and the universal pattern, "any", is <and []>
Alternative = <and @patterns [Pattern ...]> / Pattern .
Alternative = <and [@patterns Pattern ...]> / Pattern .
Pattern =
; special builtins or <<atom> AtomKind>
Pattern = SimplePattern / CompoundPattern .
SimplePattern =
; special builtins
/ <atom @atomKind AtomKind>
; matches a pointer in the input
@ -33,31 +37,35 @@ Pattern =
; symbol, symbol.symbol, symbol.symbol.symbol, ...
/ Ref
.
CompoundPattern =
; <label a b c> ----> <rec <lit label> <tuple [<ref a> <ref b> <ref c>]>>
; except for record labels
; <<rec> x y> ---> <rec <ref x> <ref y>>
/ <rec @label Pattern @fields Pattern>
; [a b c] ----> <tuple [<ref a> <ref b> <ref c>]>
/ <tuple @patterns [NamedPattern ...]>
/ <tuple [@patterns NamedPattern ...]>
; [a b c ...] ----> <tuple* [<ref a> <ref b>] <ref c>]>
/ <tuple* @fixed [NamedPattern ...] @variable NamedPattern>
/ <tuple* [@fixed NamedPattern ...] @variable NamedPattern>
; #{p} ----> <setof <ref p>>
/ <setof @pattern Pattern>
/ <setof @pattern SimplePattern>
; {k: v, ...:...} ----> <dictof <ref k> <ref v>>
/ <dictof @key Pattern @value Pattern>
/ <dictof @key SimplePattern @value SimplePattern>
; {a: b, c: d} ----> <dict {a: <ref b>, c: <ref d>}>
/ <dict @entries { any: Pattern ...:... }>
/ <dict @entries DictionaryEntries>
.
DictionaryEntries = { any: NamedPattern ...:... }.
AtomKind = =Boolean / =Float / =Double / =SignedInteger / =String / =ByteString / =Symbol .
NamedPattern = <named @name symbol @pattern Pattern> / @anonymous Pattern .
NamedPattern = <named @name symbol @pattern SimplePattern> / @anonymous Pattern .
Ref = <ref @module ModulePath @name symbol>.
ModulePath = [symbol ...].