More general variant

This commit is contained in:
Tony Garnock-Jones 2021-03-17 20:23:55 +01:00
parent 3463cd4a65
commit 5d2ee85b36
4 changed files with 21 additions and 15 deletions

View File

@ -43,7 +43,7 @@ class ModuleContext {
return varname;
}
derefPattern([_name, p]: [symbol, Alternative]): Definition {
derefPattern([_name, p]: [string, Alternative]): Definition {
if (p.label === M.$ref) {
return lookup(refPosition(p), p, this.env,
(p) => p,

View File

@ -68,7 +68,7 @@ export type Definitions = _.KeyedDictionary<symbol, Definition, _ptr>;
export type Definition = (_.Record<(typeof $or), [Array<NamedAlternative>], _ptr> | Alternative);
export type NamedAlternative = [symbol, Alternative];
export type NamedAlternative = [string, Alternative];
export type Alternative = (_.Record<(typeof $and), [Array<NamedPattern>], _ptr> | Pattern);
@ -304,7 +304,7 @@ export function isNamedAlternative(v: any): v is NamedAlternative {
_.Array.isArray(v) &&
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
(v.length === 2) &&
typeof v[0] === 'symbol' &&
typeof v[0] === 'string' &&
isAlternative(v[1])
);
}
@ -317,10 +317,10 @@ export function decodeNamedAlternative(d: _.TypedDecoder<_ptr>): NamedAlternativ
let _tmp0, _tmp1: any;
let result;
if (d.openSequence()) {
_tmp0 = d.nextSymbol();
_tmp0 = d.nextString();
if (_tmp0 !== void 0) {
_tmp1 = decodeAlternative(d);
if (_tmp1 !== void 0) {if (d.closeCompound()) result = [_tmp0, _tmp1] as [symbol, Alternative];};
if (_tmp1 !== void 0) {if (d.closeCompound()) result = [_tmp0, _tmp1] as [string, Alternative];};
};
};
return result;

View File

@ -125,22 +125,28 @@ export function parseSchema(toplevelTokens: Array<Input>,
function parseDefinition(name: symbol, body: Array<Input>): Definition {
let nextAnonymousAlternativeNumber = 0;
function alternativeName([input, p]: readonly [Array<Input>, Alternative])
: [symbol, Alternative]
: [string, Alternative]
{
const n = findName(input) || findName(input[0]);
if (n !== false) {
return [n, p];
return [n.description!, p];
}
if (p.label === M.$rec && p[0].label === M.$lit && typeof p[0][0] === 'symbol') {
return [p[0][0], p];
return [p[0][0].description!, p];
}
if (p.label === M.$ref) {
return [p[1], p];
return [p[1].description!, p];
}
if (p.label === M.$lit && typeof p[0] === 'symbol') {
return [p[0], p];
if (p.label === M.$lit) {
switch (typeof p[0]) {
case 'symbol': return [p[0].description!, p];
case 'string': return [p[0], p];
case 'number': return ['' + p[0], p];
default:
break;
}
}
return [Symbol.for('_anonymous' + nextAnonymousAlternativeNumber++), p];
return ['_anonymous' + nextAnonymousAlternativeNumber++, p];
}
return parseOp(body,

View File

@ -19,7 +19,7 @@ Definitions = { symbol: Definition ...:... }.
; and the empty pattern is <or []>
Definition = <or [@patterns NamedAlternative ...]> / Alternative .
NamedAlternative = [@name symbol @alternative Alternative].
NamedAlternative = [@variant string @alternative Alternative].
; Pattern & Pattern & ...
; and the universal pattern, "any", is <and []>
@ -67,8 +67,8 @@ DictionaryEntries = { any: NamedSimplePattern ...:... }.
AtomKind = =Boolean / =Float / =Double / =SignedInteger / =String / =ByteString / =Symbol .
NamedSimplePattern = NamedSimplePattern_ / SimplePattern .
NamedPattern = NamedSimplePattern_ / Pattern .
NamedSimplePattern = @named NamedSimplePattern_ / @anonymous SimplePattern .
NamedPattern = @named NamedSimplePattern_ / @anonymous Pattern .
NamedSimplePattern_ = <named @name symbol @pattern SimplePattern>.