import { typeFor, typeForIntersection } from './gentype'; import { ANY_TYPE, SimpleType } from './type'; import * as M from './meta'; export function checkSchema(schema: M.Schema): ( { ok: true, schema: M.Schema } | { ok: false, problems: Array }) { const checker = new Checker(); schema.definitions.forEach(checker.checkDefinition.bind(checker)); if (checker.problems.length > 0) { return { ok: false, problems: checker.problems }; } else { return { ok: true, schema }; } } class Checker { problems: Array = []; recordProblem(context: string, detail: string): void { this.problems.push(`${detail} in ${context}`); } checkBinding(scope: Set, 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': { const labels = new Set(); [def.pattern0, def.pattern1, ... def.patternN].forEach(({ variantLabel, pattern }) => { const context = `variant ${variantLabel} of ${name.description!}`; if (labels.has(variantLabel)) { this.recordProblem(context, `duplicate variant label`); } labels.add(variantLabel); this.checkPattern(new Set(), pattern, context, typeFor(_ref => ANY_TYPE, pattern)); }); break; } case 'and': { const ps = [def.pattern0, def.pattern1, ... def.patternN]; const scope = new Set(); ps.forEach((p) => this.checkNamedPattern( scope, p, name.description!, typeForIntersection(_ref => ANY_TYPE, ps))); break; } case 'Pattern': this.checkPattern( new Set(), def.value, name.description!, typeFor(_ref => ANY_TYPE, def.value)); break; } } checkNamedPattern(scope: Set, p: M.NamedPattern, context: string, t: SimpleType): void { switch (p._variant) { case 'named': { const key = p.value.name.description!; if (t.kind !== 'record' || !t.fields.has(key)) { throw new Error( `Internal error: cannot step ${JSON.stringify(t)} by ${JSON.stringify(key)}`); } this.checkBinding(scope, p.value.name, context); this.checkPattern(scope, M.Pattern.SimplePattern(p.value.pattern), `${JSON.stringify(p.value.name.description!)} of ${context}`, t.fields.get(key)!); break; } case 'anonymous': this.checkPattern(scope, p.value, context, t); break; } } checkPattern(scope: Set, p: M.Pattern, context: string, t: SimpleType): void { 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}`, t); this.checkNamedPattern(scope, p.fields, `fields of ${context}`, t); break; case 'tuple': p.patterns.forEach((pp, i) => this.checkNamedPattern(scope, pp, `item ${i} of ${context}`, t)); break; case 'tuple*': p.fixed.forEach((pp, i) => this.checkNamedPattern(scope, pp, `item ${i} of ${context}`, t)); this.checkNamedPattern( scope, M.promoteNamedSimplePattern(p.variable), `tail of ${context}`, t); break; case 'dict': p.entries.forEach((np, key) => this.checkNamedPattern( scope, M.promoteNamedSimplePattern(M.addNameIfAbsent(np, key)), `entry ${key.asPreservesText()} in dictionary in ${context}`, t)); break; } })(p.value); } } }