Further schema schema fixes

This commit is contained in:
Tony Garnock-Jones 2021-03-17 20:01:26 +01:00
parent 5f71239130
commit 3463cd4a65
3 changed files with 48 additions and 49 deletions

View File

@ -84,15 +84,13 @@ export type SimplePattern = (
export type CompoundPattern = (
_.Record<(typeof $rec), [NamedPattern, NamedPattern], _ptr> |
_.Record<(typeof $tuple), [Array<NamedPattern>], _ptr> |
_.Record<(typeof $tuple_STAR_), [Array<NamedPattern>, NamedPattern], _ptr> |
_.Record<(typeof $tuple_STAR_), [Array<NamedPattern>, NamedSimplePattern], _ptr> |
_.Record<(typeof $setof), [SimplePattern], _ptr> |
_.Record<(typeof $dictof), [SimplePattern, SimplePattern], _ptr> |
_.Record<(typeof $dict), [DictionaryEntries], _ptr>
);
export type DictionaryEntries = _.KeyedDictionary<_val, DictionaryEntryPattern, _ptr>;
export type DictionaryEntryPattern = (NamedSimplePattern | SimplePattern);
export type DictionaryEntries = _.KeyedDictionary<_val, NamedSimplePattern, _ptr>;
export type AtomKind = (
(typeof $Boolean) |
@ -104,11 +102,13 @@ export type AtomKind = (
(typeof $Symbol)
);
export type NamedPattern = (NamedSimplePattern | Pattern);
export type NamedSimplePattern = (NamedSimplePattern_ | SimplePattern);
export const NamedSimplePattern = _.Record.makeConstructor<{"name": symbol, "pattern": SimplePattern}, _ptr>()($named, ["name","pattern"]);
export type NamedPattern = (NamedSimplePattern_ | Pattern);
export type NamedSimplePattern = _.Record<(typeof $named), [symbol, SimplePattern], _ptr>;
export const NamedSimplePattern_ = _.Record.makeConstructor<{"name": symbol, "pattern": SimplePattern}, _ptr>()($named, ["name","pattern"]);
export type NamedSimplePattern_ = _.Record<(typeof $named), [symbol, SimplePattern], _ptr>;
export const Ref = _.Record.makeConstructor<{"module": ModulePath, "name": symbol}, _ptr>()($ref, ["module","name"]);
@ -482,7 +482,7 @@ export function isCompoundPattern(v: any): v is CompoundPattern {
(v[0].length >= 0) &&
v[0].every(v => (isNamedPattern(v)))
) &&
isNamedPattern(v[1])
isNamedSimplePattern(v[1])
)
) ||
(_.is(v.label, $setof) && ((v.length === 1) && isSimplePattern(v[0]))) ||
@ -551,12 +551,12 @@ export function decodeCompoundPattern(d: _.TypedDecoder<_ptr>): CompoundPattern
};
};
if (_tmp9 !== void 0) {
_tmp10 = decodeNamedPattern(d);
_tmp10 = decodeNamedSimplePattern(d);
if (_tmp10 !== void 0) {
if (d.closeCompound()) _tmp8 = [_tmp9, _tmp10] as [Array<NamedPattern>, NamedPattern];
if (d.closeCompound()) _tmp8 = [_tmp9, _tmp10] as [Array<NamedPattern>, NamedSimplePattern];
};
};
if (_tmp8 !== void 0) result = _.Record<(typeof $tuple_STAR_), [Array<NamedPattern>, NamedPattern]>(_tmp0 as any, _tmp8 as any);
if (_tmp8 !== void 0) result = _.Record<(typeof $tuple_STAR_), [Array<NamedPattern>, NamedSimplePattern]>(_tmp0 as any, _tmp8 as any);
};
if (result === void 0) {
d.restoreMark(_tmp1);
@ -600,10 +600,7 @@ export function isDictionaryEntries(v: any): v is DictionaryEntries {
return (
_.Dictionary.isDictionary<_ptr>(v) &&
((() => {
for (const e of v) {
if (!(true)) return false;
if (!(isDictionaryEntryPattern(e[1]))) return false;
};
for (const e of v) {if (!(true)) return false; if (!(isNamedSimplePattern(e[1]))) return false;};
return true;
})())
);
@ -616,13 +613,13 @@ export function asDictionaryEntries(v: any): DictionaryEntries {
export function decodeDictionaryEntries(d: _.TypedDecoder<_ptr>): DictionaryEntries | undefined {
let result;
if (d.openDictionary()) {
let r: _.KeyedDictionary<_val, DictionaryEntryPattern, _ptr> | undefined = new _.KeyedDictionary();
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 | DictionaryEntryPattern = void 0;
V = decodeDictionaryEntryPattern(d);
let V: undefined | NamedSimplePattern = void 0;
V = decodeNamedSimplePattern(d);
if (V === void 0) { r = void 0; break; };
r.set(K, V);
};
@ -631,21 +628,6 @@ export function decodeDictionaryEntries(d: _.TypedDecoder<_ptr>): DictionaryEntr
return result;
}
export function isDictionaryEntryPattern(v: any): v is DictionaryEntryPattern {return (isNamedSimplePattern(v) || isSimplePattern(v));}
export function asDictionaryEntryPattern(v: any): DictionaryEntryPattern {
if (!isDictionaryEntryPattern(v)) {throw new TypeError(`Invalid DictionaryEntryPattern: ${_.stringify(v)}`);} else {return v;};
}
export function decodeDictionaryEntryPattern(d: _.TypedDecoder<_ptr>): DictionaryEntryPattern | undefined {
let _tmp0: any;
let result;
_tmp0 = d.mark();
result = decodeNamedSimplePattern(d);
if (result === void 0) {d.restoreMark(_tmp0); result = decodeSimplePattern(d);};
return result;
}
export function isAtomKind(v: any): v is AtomKind {
return (
_.is(v, $Boolean) ||
@ -691,7 +673,22 @@ export function decodeAtomKind(d: _.TypedDecoder<_ptr>): AtomKind | undefined {
return result;
}
export function isNamedPattern(v: any): v is NamedPattern {return (isNamedSimplePattern(v) || isPattern(v));}
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);};
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;};
@ -701,12 +698,12 @@ export function decodeNamedPattern(d: _.TypedDecoder<_ptr>): NamedPattern | unde
let _tmp0: any;
let result;
_tmp0 = d.mark();
result = decodeNamedSimplePattern(d);
result = decodeNamedSimplePattern_(d);
if (result === void 0) {d.restoreMark(_tmp0); result = decodePattern(d);};
return result;
}
export function isNamedSimplePattern(v: any): v is NamedSimplePattern {
export function isNamedSimplePattern_(v: any): v is NamedSimplePattern_ {
return (
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $named) &&
@ -714,11 +711,11 @@ export function isNamedSimplePattern(v: any): v is NamedSimplePattern {
);
}
export function asNamedSimplePattern(v: any): NamedSimplePattern {
if (!isNamedSimplePattern(v)) {throw new TypeError(`Invalid NamedSimplePattern: ${_.stringify(v)}`);} else {return 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 {
export function decodeNamedSimplePattern_(d: _.TypedDecoder<_ptr>): NamedSimplePattern_ | undefined {
let result;
if (d.openRecord()) {
let _tmp0: any;

View File

@ -1,5 +1,5 @@
import { Reader, Annotated, Dictionary, is, peel, preserves, Record, strip, Tuple, Position, position, ReaderOptions, stringify, isCompound } from '@preserves/core';
import { Input, Pattern, Schema, Alternative, Definition, CompoundPattern, NamedSimplePattern, SimplePattern } from './meta';
import { Input, Pattern, Schema, Alternative, Definition, CompoundPattern, SimplePattern } from './meta';
import * as M from './meta';
import { SchemaSyntaxError } from './error';
@ -192,7 +192,7 @@ function parsePattern(name: symbol, body0: Array<Input>): Pattern {
});
const walk = (b: Input): Pattern => parsePattern(name, [b]);
function _maybeNamed<R>(recur: (b: Input) => R): (b: Input) => NamedSimplePattern | R {
function _maybeNamed<R>(recur: (b: Input) => R): (b: Input) => M.NamedSimplePattern_ | R {
return (b: Input) => {
const name = findName(b);
if (name === false) return recur(b);
@ -202,6 +202,7 @@ function parsePattern(name: symbol, body0: Array<Input>): Pattern {
};
}
const maybeNamed = _maybeNamed(walk);
const maybeNamedSimple = _maybeNamed(walkSimple);
if (Record.isRecord<Input, Tuple<Input>, never>(item)) {
const label = item.label;
@ -222,7 +223,7 @@ function parsePattern(name: symbol, body0: Array<Input>): Pattern {
if (item.length < 2) complain();
return Record(M.$tuple_STAR_, [
item.slice(0, item.length - 2).map(maybeNamed),
maybeNamed(item[item.length - 2]),
maybeNamedSimple(item[item.length - 2]),
]);
} else {
return Record(M.$tuple, [item.map(maybeNamed)]);
@ -234,8 +235,8 @@ function parsePattern(name: symbol, body0: Array<Input>): Pattern {
const [[kp, vp]] = v.entries();
return Record(M.$dictof, [walkSimple(kp), walkSimple(vp)]);
} else {
return Record(M.$dict, [item.mapEntries<M.DictionaryEntryPattern, Input, never>(
([k, vp]) => [strip(k), _maybeNamed(walkSimple)(vp)])]);
return Record(M.$dict, [item.mapEntries<M.NamedSimplePattern, Input, never>(
([k, vp]) => [strip(k), maybeNamedSimple(vp)])]);
}
} else if (Set.isSet<never>(item)) {
if (item.size !== 1) complain();

View File

@ -51,7 +51,7 @@ CompoundPattern =
/ <tuple [@patterns NamedPattern ...]>
; [a b c ...] ----> <tuple* [<ref a> <ref b>] <ref c>]>
/ <tuple* [@fixed NamedPattern ...] @variable NamedPattern>
/ <tuple* [@fixed NamedPattern ...] @variable NamedSimplePattern>
; #{p} ----> <setof <ref p>>
/ <setof @pattern SimplePattern>
@ -63,13 +63,14 @@ CompoundPattern =
/ <dict @entries DictionaryEntries>
.
DictionaryEntries = { any: DictionaryEntryPattern ...:... }.
DictionaryEntryPattern = NamedSimplePattern / SimplePattern .
DictionaryEntries = { any: NamedSimplePattern ...:... }.
AtomKind = =Boolean / =Float / =Double / =SignedInteger / =String / =ByteString / =Symbol .
NamedPattern = NamedSimplePattern / Pattern .
NamedSimplePattern = <named @name symbol @pattern SimplePattern>.
NamedSimplePattern = NamedSimplePattern_ / SimplePattern .
NamedPattern = NamedSimplePattern_ / Pattern .
NamedSimplePattern_ = <named @name symbol @pattern SimplePattern>.
Ref = <ref @module ModulePath @name symbol>.
ModulePath = [symbol ...].