More general variant
This commit is contained in:
parent
3463cd4a65
commit
5d2ee85b36
|
@ -43,7 +43,7 @@ class ModuleContext {
|
||||||
return varname;
|
return varname;
|
||||||
}
|
}
|
||||||
|
|
||||||
derefPattern([_name, p]: [symbol, Alternative]): Definition {
|
derefPattern([_name, p]: [string, Alternative]): Definition {
|
||||||
if (p.label === M.$ref) {
|
if (p.label === M.$ref) {
|
||||||
return lookup(refPosition(p), p, this.env,
|
return lookup(refPosition(p), p, this.env,
|
||||||
(p) => p,
|
(p) => p,
|
||||||
|
|
|
@ -68,7 +68,7 @@ export type Definitions = _.KeyedDictionary<symbol, Definition, _ptr>;
|
||||||
|
|
||||||
export type Definition = (_.Record<(typeof $or), [Array<NamedAlternative>], _ptr> | Alternative);
|
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);
|
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) &&
|
_.Array.isArray(v) &&
|
||||||
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
||||||
(v.length === 2) &&
|
(v.length === 2) &&
|
||||||
typeof v[0] === 'symbol' &&
|
typeof v[0] === 'string' &&
|
||||||
isAlternative(v[1])
|
isAlternative(v[1])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -317,10 +317,10 @@ export function decodeNamedAlternative(d: _.TypedDecoder<_ptr>): NamedAlternativ
|
||||||
let _tmp0, _tmp1: any;
|
let _tmp0, _tmp1: any;
|
||||||
let result;
|
let result;
|
||||||
if (d.openSequence()) {
|
if (d.openSequence()) {
|
||||||
_tmp0 = d.nextSymbol();
|
_tmp0 = d.nextString();
|
||||||
if (_tmp0 !== void 0) {
|
if (_tmp0 !== void 0) {
|
||||||
_tmp1 = decodeAlternative(d);
|
_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;
|
return result;
|
||||||
|
|
|
@ -125,22 +125,28 @@ export function parseSchema(toplevelTokens: Array<Input>,
|
||||||
function parseDefinition(name: symbol, body: Array<Input>): Definition {
|
function parseDefinition(name: symbol, body: Array<Input>): Definition {
|
||||||
let nextAnonymousAlternativeNumber = 0;
|
let nextAnonymousAlternativeNumber = 0;
|
||||||
function alternativeName([input, p]: readonly [Array<Input>, Alternative])
|
function alternativeName([input, p]: readonly [Array<Input>, Alternative])
|
||||||
: [symbol, Alternative]
|
: [string, Alternative]
|
||||||
{
|
{
|
||||||
const n = findName(input) || findName(input[0]);
|
const n = findName(input) || findName(input[0]);
|
||||||
if (n !== false) {
|
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') {
|
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) {
|
if (p.label === M.$ref) {
|
||||||
return [p[1], p];
|
return [p[1].description!, p];
|
||||||
}
|
}
|
||||||
if (p.label === M.$lit && typeof p[0] === 'symbol') {
|
if (p.label === M.$lit) {
|
||||||
return [p[0], p];
|
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,
|
return parseOp(body,
|
||||||
|
|
|
@ -19,7 +19,7 @@ Definitions = { symbol: Definition ...:... }.
|
||||||
; and the empty pattern is <or []>
|
; and the empty pattern is <or []>
|
||||||
Definition = <or [@patterns NamedAlternative ...]> / Alternative .
|
Definition = <or [@patterns NamedAlternative ...]> / Alternative .
|
||||||
|
|
||||||
NamedAlternative = [@name symbol @alternative Alternative].
|
NamedAlternative = [@variant string @alternative Alternative].
|
||||||
|
|
||||||
; Pattern & Pattern & ...
|
; Pattern & Pattern & ...
|
||||||
; and the universal pattern, "any", is <and []>
|
; and the universal pattern, "any", is <and []>
|
||||||
|
@ -67,8 +67,8 @@ DictionaryEntries = { any: NamedSimplePattern ...:... }.
|
||||||
|
|
||||||
AtomKind = =Boolean / =Float / =Double / =SignedInteger / =String / =ByteString / =Symbol .
|
AtomKind = =Boolean / =Float / =Double / =SignedInteger / =String / =ByteString / =Symbol .
|
||||||
|
|
||||||
NamedSimplePattern = NamedSimplePattern_ / SimplePattern .
|
NamedSimplePattern = @named NamedSimplePattern_ / @anonymous SimplePattern .
|
||||||
NamedPattern = NamedSimplePattern_ / Pattern .
|
NamedPattern = @named NamedSimplePattern_ / @anonymous Pattern .
|
||||||
|
|
||||||
NamedSimplePattern_ = <named @name symbol @pattern SimplePattern>.
|
NamedSimplePattern_ = <named @name symbol @pattern SimplePattern>.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue