diff --git a/implementations/javascript/packages/schema/src/gen/schema.ts b/implementations/javascript/packages/schema/src/gen/schema.ts index c7968a4..046d378 100644 --- a/implementations/javascript/packages/schema/src/gen/schema.ts +++ b/implementations/javascript/packages/schema/src/gen/schema.ts @@ -84,15 +84,13 @@ export type SimplePattern = ( export type CompoundPattern = ( _.Record<(typeof $rec), [NamedPattern, NamedPattern], _ptr> | _.Record<(typeof $tuple), [Array], _ptr> | - _.Record<(typeof $tuple_STAR_), [Array, NamedPattern], _ptr> | + _.Record<(typeof $tuple_STAR_), [Array, 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]; + if (d.closeCompound()) _tmp8 = [_tmp9, _tmp10] as [Array, NamedSimplePattern]; }; }; - if (_tmp8 !== void 0) result = _.Record<(typeof $tuple_STAR_), [Array, NamedPattern]>(_tmp0 as any, _tmp8 as any); + if (_tmp8 !== void 0) result = _.Record<(typeof $tuple_STAR_), [Array, 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; diff --git a/implementations/javascript/packages/schema/src/reader.ts b/implementations/javascript/packages/schema/src/reader.ts index 02bea29..369419b 100644 --- a/implementations/javascript/packages/schema/src/reader.ts +++ b/implementations/javascript/packages/schema/src/reader.ts @@ -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): Pattern { }); const walk = (b: Input): Pattern => parsePattern(name, [b]); - function _maybeNamed(recur: (b: Input) => R): (b: Input) => NamedSimplePattern | R { + function _maybeNamed(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): Pattern { }; } const maybeNamed = _maybeNamed(walk); + const maybeNamedSimple = _maybeNamed(walkSimple); if (Record.isRecord, never>(item)) { const label = item.label; @@ -222,7 +223,7 @@ function parsePattern(name: symbol, body0: Array): 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): Pattern { const [[kp, vp]] = v.entries(); return Record(M.$dictof, [walkSimple(kp), walkSimple(vp)]); } else { - return Record(M.$dict, [item.mapEntries( - ([k, vp]) => [strip(k), _maybeNamed(walkSimple)(vp)])]); + return Record(M.$dict, [item.mapEntries( + ([k, vp]) => [strip(k), maybeNamedSimple(vp)])]); } } else if (Set.isSet(item)) { if (item.size !== 1) complain(); diff --git a/schema/schema.prs b/schema/schema.prs index 3592e8b..dfddd66 100644 --- a/schema/schema.prs +++ b/schema/schema.prs @@ -51,7 +51,7 @@ CompoundPattern = / ; [a b c ...] ----> ] ]> - / + / ; #{p} ----> > / @@ -63,13 +63,14 @@ CompoundPattern = / . -DictionaryEntries = { any: DictionaryEntryPattern ...:... }. -DictionaryEntryPattern = NamedSimplePattern / SimplePattern . +DictionaryEntries = { any: NamedSimplePattern ...:... }. AtomKind = =Boolean / =Float / =Double / =SignedInteger / =String / =ByteString / =Symbol . -NamedPattern = NamedSimplePattern / Pattern . -NamedSimplePattern = . +NamedSimplePattern = NamedSimplePattern_ / SimplePattern . +NamedPattern = NamedSimplePattern_ / Pattern . + +NamedSimplePattern_ = . Ref = . ModulePath = [symbol ...].