2021-05-21 13:49:06 +00:00
|
|
|
import { typeFor } from './gentype';
|
|
|
|
import { ANY_TYPE } from './type';
|
|
|
|
import * as M from './meta';
|
|
|
|
|
|
|
|
export function checkSchema(schema: M.Schema): M.Schema {
|
|
|
|
const checker = new Checker();
|
|
|
|
schema.definitions.forEach(checker.checkDefinition.bind(checker));
|
|
|
|
if (checker.problems.length > 0) {
|
|
|
|
throw new Error(`Cannot produce unconverter: insufficient information in:\n` +
|
|
|
|
checker.problems.map(c => ' - ' + c).join('\n'));
|
|
|
|
}
|
|
|
|
return schema;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Checker {
|
|
|
|
problems: Array<string> = [];
|
|
|
|
|
|
|
|
recordProblem(context: string, detail: string): void {
|
|
|
|
this.problems.push(`${detail} in ${context}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
checkBinding(scope: Set<string>, sym: symbol, context: string): void {
|
|
|
|
const name = sym.description!;
|
|
|
|
if (scope.has(name)) {
|
|
|
|
this.recordProblem(context, `duplicate binding named ${JSON.stringify(name)}`);
|
|
|
|
} else {
|
|
|
|
scope.add(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkDefinition(def: M.Definition, name: symbol): void {
|
|
|
|
switch (def._variant) {
|
|
|
|
case 'or':
|
|
|
|
[def.pattern0, def.pattern1, ... def.patternN].forEach(({ variantLabel, pattern }) =>
|
|
|
|
this.checkPattern(new Set(), pattern, `variant ${variantLabel} of ${name.description!}`));
|
|
|
|
break;
|
|
|
|
case 'and': {
|
|
|
|
const scope = new Set<string>();
|
|
|
|
[def.pattern0, def.pattern1, ... def.patternN].forEach((p) =>
|
|
|
|
this.checkNamedPattern(scope, p, name.description!));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Pattern':
|
|
|
|
this.checkPattern(new Set(), def.value, name.description!);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkNamedPattern(scope: Set<string>, p: M.NamedPattern, context: string): void {
|
|
|
|
switch (p._variant) {
|
|
|
|
case 'named':
|
2021-05-21 14:14:58 +00:00
|
|
|
this.checkBinding(scope, p.value.name, context);
|
2021-05-21 13:49:06 +00:00
|
|
|
this.checkPattern(scope,
|
|
|
|
M.Pattern.SimplePattern(p.value.pattern),
|
|
|
|
`${p.value.name.description!} of ${context}`);
|
|
|
|
break;
|
|
|
|
case 'anonymous':
|
|
|
|
this.checkPattern(scope, p.value, context);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkPattern(scope: Set<string>, p: M.Pattern, context: string): void {
|
|
|
|
const t = typeFor((_ref) => ANY_TYPE, p);
|
|
|
|
switch (p._variant) {
|
|
|
|
case 'SimplePattern':
|
|
|
|
if (p.value._variant !== 'lit' && (t.kind === 'record' || t.kind === 'unit')) {
|
|
|
|
this.recordProblem(context, 'cannot recover serialization of non-literal pattern');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'CompoundPattern':
|
|
|
|
((p: M.CompoundPattern): void => {
|
|
|
|
switch (p._variant) {
|
|
|
|
case 'rec':
|
|
|
|
this.checkNamedPattern(scope, p.label, `label of ${context}`);
|
|
|
|
this.checkNamedPattern(scope, p.fields, `fields of ${context}`);
|
|
|
|
break;
|
|
|
|
case 'tuple':
|
|
|
|
p.patterns.forEach((pp, i) =>
|
|
|
|
this.checkNamedPattern(scope, pp, `item ${i} of ${context}`));
|
|
|
|
break;
|
|
|
|
case 'tuple*':
|
|
|
|
if (p.variable._variant === 'named') {
|
|
|
|
this.checkBinding(scope, p.variable.value.name, context);
|
|
|
|
this.checkPattern(scope,
|
|
|
|
M.Pattern.SimplePattern(p.variable.value.pattern),
|
|
|
|
`${JSON.stringify(p.variable.value.name.description!)} of ${context}`);
|
|
|
|
} else {
|
|
|
|
if (t.kind !== 'array') {
|
|
|
|
this.recordProblem(context, 'unable to reconstruct tail of tuple* pattern');
|
|
|
|
}
|
|
|
|
this.checkPattern(scope,
|
|
|
|
M.Pattern.SimplePattern(p.variable.value),
|
|
|
|
`variable-length portion of ${context}`);
|
|
|
|
}
|
|
|
|
p.fixed.forEach((pp, i) =>
|
|
|
|
this.checkNamedPattern(scope, pp, `item ${i} of ${context}`));
|
|
|
|
break;
|
|
|
|
case 'setof':
|
|
|
|
if (t.kind !== 'set') {
|
|
|
|
this.recordProblem(context, 'unable to reconstruct set');
|
|
|
|
}
|
|
|
|
this.checkPattern(scope,
|
|
|
|
M.Pattern.SimplePattern(p.pattern),
|
|
|
|
`set in ${context}`);
|
|
|
|
break;
|
|
|
|
case 'dictof':
|
|
|
|
if (t.kind !== 'dictionary') {
|
|
|
|
this.recordProblem(context, 'unable to reconstruct dictionary');
|
|
|
|
}
|
|
|
|
this.checkPattern(scope,
|
|
|
|
M.Pattern.SimplePattern(p.key),
|
|
|
|
`key in dictionary in ${context}`);
|
|
|
|
this.checkPattern(scope,
|
|
|
|
M.Pattern.SimplePattern(p.value),
|
|
|
|
`value in dictionary in ${context}`);
|
|
|
|
break;
|
|
|
|
case 'dict':
|
|
|
|
p.entries.forEach((np, key) =>
|
|
|
|
this.checkNamedPattern(
|
|
|
|
scope,
|
|
|
|
M.promoteNamedSimplePattern(M.addNameIfAbsent(np, key)),
|
|
|
|
`entry ${key.asPreservesText()} in dictionary in ${context}`));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
})(p.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|