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 = ( export type CompoundPattern = (
_.Record<(typeof $rec), [NamedPattern, NamedPattern], _ptr> | _.Record<(typeof $rec), [NamedPattern, NamedPattern], _ptr> |
_.Record<(typeof $tuple), [Array<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 $setof), [SimplePattern], _ptr> |
_.Record<(typeof $dictof), [SimplePattern, SimplePattern], _ptr> | _.Record<(typeof $dictof), [SimplePattern, SimplePattern], _ptr> |
_.Record<(typeof $dict), [DictionaryEntries], _ptr> _.Record<(typeof $dict), [DictionaryEntries], _ptr>
); );
export type DictionaryEntries = _.KeyedDictionary<_val, DictionaryEntryPattern, _ptr>; export type DictionaryEntries = _.KeyedDictionary<_val, NamedSimplePattern, _ptr>;
export type DictionaryEntryPattern = (NamedSimplePattern | SimplePattern);
export type AtomKind = ( export type AtomKind = (
(typeof $Boolean) | (typeof $Boolean) |
@ -104,11 +102,13 @@ export type AtomKind = (
(typeof $Symbol) (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"]); 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].length >= 0) &&
v[0].every(v => (isNamedPattern(v))) v[0].every(v => (isNamedPattern(v)))
) && ) &&
isNamedPattern(v[1]) isNamedSimplePattern(v[1])
) )
) || ) ||
(_.is(v.label, $setof) && ((v.length === 1) && isSimplePattern(v[0]))) || (_.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) { if (_tmp9 !== void 0) {
_tmp10 = decodeNamedPattern(d); _tmp10 = decodeNamedSimplePattern(d);
if (_tmp10 !== void 0) { 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) { if (result === void 0) {
d.restoreMark(_tmp1); d.restoreMark(_tmp1);
@ -600,10 +600,7 @@ export function isDictionaryEntries(v: any): v is DictionaryEntries {
return ( return (
_.Dictionary.isDictionary<_ptr>(v) && _.Dictionary.isDictionary<_ptr>(v) &&
((() => { ((() => {
for (const e of v) { for (const e of v) {if (!(true)) return false; if (!(isNamedSimplePattern(e[1]))) return false;};
if (!(true)) return false;
if (!(isDictionaryEntryPattern(e[1]))) return false;
};
return true; return true;
})()) })())
); );
@ -616,13 +613,13 @@ export function asDictionaryEntries(v: any): DictionaryEntries {
export function decodeDictionaryEntries(d: _.TypedDecoder<_ptr>): DictionaryEntries | undefined { export function decodeDictionaryEntries(d: _.TypedDecoder<_ptr>): DictionaryEntries | undefined {
let result; let result;
if (d.openDictionary()) { if (d.openDictionary()) {
let r: _.KeyedDictionary<_val, DictionaryEntryPattern, _ptr> | undefined = new _.KeyedDictionary(); let r: _.KeyedDictionary<_val, NamedSimplePattern, _ptr> | undefined = new _.KeyedDictionary();
while (!d.closeCompound()) { while (!d.closeCompound()) {
let K: undefined | _val = void 0; let K: undefined | _val = void 0;
K = d.next(); K = d.next();
if (K === void 0) { r = void 0; break; }; if (K === void 0) { r = void 0; break; };
let V: undefined | DictionaryEntryPattern = void 0; let V: undefined | NamedSimplePattern = void 0;
V = decodeDictionaryEntryPattern(d); V = decodeNamedSimplePattern(d);
if (V === void 0) { r = void 0; break; }; if (V === void 0) { r = void 0; break; };
r.set(K, V); r.set(K, V);
}; };
@ -631,21 +628,6 @@ export function decodeDictionaryEntries(d: _.TypedDecoder<_ptr>): DictionaryEntr
return result; 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 { export function isAtomKind(v: any): v is AtomKind {
return ( return (
_.is(v, $Boolean) || _.is(v, $Boolean) ||
@ -691,7 +673,22 @@ export function decodeAtomKind(d: _.TypedDecoder<_ptr>): AtomKind | undefined {
return result; 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 { export function asNamedPattern(v: any): NamedPattern {
if (!isNamedPattern(v)) {throw new TypeError(`Invalid NamedPattern: ${_.stringify(v)}`);} else {return v;}; 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 _tmp0: any;
let result; let result;
_tmp0 = d.mark(); _tmp0 = d.mark();
result = decodeNamedSimplePattern(d); result = decodeNamedSimplePattern_(d);
if (result === void 0) {d.restoreMark(_tmp0); result = decodePattern(d);}; if (result === void 0) {d.restoreMark(_tmp0); result = decodePattern(d);};
return result; return result;
} }
export function isNamedSimplePattern(v: any): v is NamedSimplePattern { export function isNamedSimplePattern_(v: any): v is NamedSimplePattern_ {
return ( return (
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $named) && _.is(v.label, $named) &&
@ -714,11 +711,11 @@ export function isNamedSimplePattern(v: any): v is NamedSimplePattern {
); );
} }
export function asNamedSimplePattern(v: any): NamedSimplePattern { export function asNamedSimplePattern_(v: any): NamedSimplePattern_ {
if (!isNamedSimplePattern(v)) {throw new TypeError(`Invalid NamedSimplePattern: ${_.stringify(v)}`);} else {return v;}; 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; let result;
if (d.openRecord()) { if (d.openRecord()) {
let _tmp0: any; 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 { 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 * as M from './meta';
import { SchemaSyntaxError } from './error'; import { SchemaSyntaxError } from './error';
@ -192,7 +192,7 @@ function parsePattern(name: symbol, body0: Array<Input>): Pattern {
}); });
const walk = (b: Input): Pattern => parsePattern(name, [b]); 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) => { return (b: Input) => {
const name = findName(b); const name = findName(b);
if (name === false) return recur(b); if (name === false) return recur(b);
@ -202,6 +202,7 @@ function parsePattern(name: symbol, body0: Array<Input>): Pattern {
}; };
} }
const maybeNamed = _maybeNamed(walk); const maybeNamed = _maybeNamed(walk);
const maybeNamedSimple = _maybeNamed(walkSimple);
if (Record.isRecord<Input, Tuple<Input>, never>(item)) { if (Record.isRecord<Input, Tuple<Input>, never>(item)) {
const label = item.label; const label = item.label;
@ -222,7 +223,7 @@ function parsePattern(name: symbol, body0: Array<Input>): Pattern {
if (item.length < 2) complain(); if (item.length < 2) complain();
return Record(M.$tuple_STAR_, [ return Record(M.$tuple_STAR_, [
item.slice(0, item.length - 2).map(maybeNamed), item.slice(0, item.length - 2).map(maybeNamed),
maybeNamed(item[item.length - 2]), maybeNamedSimple(item[item.length - 2]),
]); ]);
} else { } else {
return Record(M.$tuple, [item.map(maybeNamed)]); return Record(M.$tuple, [item.map(maybeNamed)]);
@ -234,8 +235,8 @@ function parsePattern(name: symbol, body0: Array<Input>): Pattern {
const [[kp, vp]] = v.entries(); const [[kp, vp]] = v.entries();
return Record(M.$dictof, [walkSimple(kp), walkSimple(vp)]); return Record(M.$dictof, [walkSimple(kp), walkSimple(vp)]);
} else { } else {
return Record(M.$dict, [item.mapEntries<M.DictionaryEntryPattern, Input, never>( return Record(M.$dict, [item.mapEntries<M.NamedSimplePattern, Input, never>(
([k, vp]) => [strip(k), _maybeNamed(walkSimple)(vp)])]); ([k, vp]) => [strip(k), maybeNamedSimple(vp)])]);
} }
} else if (Set.isSet<never>(item)) { } else if (Set.isSet<never>(item)) {
if (item.size !== 1) complain(); if (item.size !== 1) complain();

View File

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