Simpler schema-schema

This commit is contained in:
Tony Garnock-Jones 2021-03-14 22:59:53 +01:00
parent 14be044092
commit adfb1822ac
4 changed files with 23 additions and 46 deletions

View File

@ -21,7 +21,6 @@ export const $rec = Symbol.for("rec");
export const $ref = Symbol.for("ref");
export const $schema = Symbol.for("schema");
export const $setof = Symbol.for("setof");
export const $thisModule = Symbol.for("thisModule");
export const $tuple = Symbol.for("tuple");
export const $tuple_STAR_ = Symbol.for("tuple*");
export const $version = Symbol.for("version");
@ -92,11 +91,9 @@ export type Pattern = (
export type NamedPattern = (_.Record<(typeof $named), [symbol, Pattern], _ptr> | Pattern);
export const Ref = _.Record.makeConstructor<{"module": ModuleRef, "name": symbol}, _ptr>()($ref, ["module","name"]);
export const Ref = _.Record.makeConstructor<{"module": ModulePath, "name": symbol}, _ptr>()($ref, ["module","name"]);
export type Ref = _.Record<(typeof $ref), [ModuleRef, symbol], _ptr>;
export type ModuleRef = ((typeof $thisModule) | ModulePath);
export type Ref = _.Record<(typeof $ref), [ModulePath, symbol], _ptr>;
export type ModulePath = Array<symbol>;
@ -226,7 +223,7 @@ export function isPattern(v: any): v is Pattern {
(_.is(v.label, $pointer) && ((v.length === 0))) ||
(_.is(v.label, $lit) && ((v.length === 1) && true)) ||
(
_.is(v.label, $ref) && ((v.length === 2) && isModuleRef(v[0]) && typeof v[1] === 'symbol')
_.is(v.label, $ref) && ((v.length === 2) && isModulePath(v[0]) && typeof v[1] === 'symbol')
) ||
(
_.is(v.label, $or) && (
@ -366,12 +363,12 @@ export function decodePattern(d: _.TypedDecoder<_ptr>): Pattern | undefined {
d.restoreMark(_tmp11);
if (_.is(_tmp10, $ref)) {
let _tmp18, _tmp19, _tmp20: any;
_tmp19 = decodeModuleRef(d);
_tmp19 = decodeModulePath(d);
if (_tmp19 !== void 0) {
_tmp20 = d.nextSymbol();
if (_tmp20 !== void 0) {if (d.closeCompound()) _tmp18 = [_tmp19, _tmp20];};
};
if (_tmp18 !== void 0) result = _.Record<(typeof $ref), [ModuleRef, symbol]>(_tmp10 as any, _tmp18 as any);
if (_tmp18 !== void 0) result = _.Record<(typeof $ref), [ModulePath, symbol]>(_tmp10 as any, _tmp18 as any);
};
if (result === void 0) {
d.restoreMark(_tmp11);
@ -561,7 +558,7 @@ export function isRef(v: any): v is Ref {
return (
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $ref) &&
((v.length === 2) && isModuleRef(v[0]) && typeof v[1] === 'symbol')
((v.length === 2) && isModulePath(v[0]) && typeof v[1] === 'symbol')
);
}
@ -576,32 +573,17 @@ export function decodeRef(d: _.TypedDecoder<_ptr>): Ref | undefined {
_tmp49 = _.asLiteral(d.nextSymbol(), $ref);
if (_tmp49 !== void 0) {
let _tmp50, _tmp51, _tmp52: any;
_tmp51 = decodeModuleRef(d);
_tmp51 = decodeModulePath(d);
if (_tmp51 !== void 0) {
_tmp52 = d.nextSymbol();
if (_tmp52 !== void 0) {if (d.closeCompound()) _tmp50 = [_tmp51, _tmp52];};
};
if (_tmp50 !== void 0) result = _.Record<(typeof $ref), [ModuleRef, symbol]>(_tmp49 as any, _tmp50 as any);
if (_tmp50 !== void 0) result = _.Record<(typeof $ref), [ModulePath, symbol]>(_tmp49 as any, _tmp50 as any);
};
};
return result;
}
export function isModuleRef(v: any): v is ModuleRef {return (_.is(v, $thisModule) || isModulePath(v));}
export function asModuleRef(v: any): ModuleRef {
if (!isModuleRef(v)) {throw new TypeError(`Invalid ModuleRef: ${_.stringify(v)}`);} else {return v;};
}
export function decodeModuleRef(d: _.TypedDecoder<_ptr>): ModuleRef | undefined {
let _tmp53: any;
let result;
_tmp53 = d.mark();
result = _.asLiteral(d.nextSymbol(), $thisModule);
if (result === void 0) {d.restoreMark(_tmp53); result = decodeModulePath(d);};
return result;
}
export function isModulePath(v: any): v is ModulePath {
return (
_.Array.isArray(v) &&
@ -618,14 +600,14 @@ export function asModulePath(v: any): ModulePath {
export function decodeModulePath(d: _.TypedDecoder<_ptr>): ModulePath | undefined {
let result;
if (d.openSequence()) {
let _tmp54: any;
let _tmp53: any;
{
let vN: Array<symbol> | undefined = [];
while (!d.closeCompound()) {
_tmp54 = void 0;
_tmp54 = d.nextSymbol();
if (_tmp54 === void 0) {vN = void 0; break;};
vN.push(_tmp54);
_tmp53 = void 0;
_tmp53 = d.nextSymbol();
if (_tmp53 === void 0) {vN = void 0; break;};
vN.push(_tmp53);
};
result = vN;
};

View File

@ -1,5 +1,5 @@
import { Value, is, Position } from '@preserves/core';
import { ModulePath, Ref, Schema, Pattern, $thisModule, $definitions } from './gen/schema';
import { ModulePath, Ref, Schema, Pattern, $definitions } from './gen/schema';
import { BASE } from './base';
import { SchemaSyntaxError } from './error';
@ -24,7 +24,7 @@ export const ORSYM = Symbol.for('/');
export type SchemaEnvEntry = { schemaModulePath: ModulePath } & (
({
typescriptModulePath: string | null, // null means it's $thisModule in disguise
typescriptModulePath: string | null, // null means it's "this module" in disguise
schema: Schema,
}) | ({
typescriptModulePath: string,
@ -47,7 +47,7 @@ export function lookup<R>(namePos: Position | null,
{
for (const e of env) {
if (is(e.schemaModulePath, Ref._.module(name)) ||
(e.typescriptModulePath === null && Ref._.module(name) === $thisModule))
(e.typescriptModulePath === null && Ref._.module(name).length === 0))
{
if (e.schema === null) {
// It's an artificial module, not from a schema. Assume the identifier is present.
@ -65,7 +65,7 @@ export function lookup<R>(namePos: Position | null,
}
}
if (Ref._.module(name) === $thisModule) {
if (Ref._.module(name).length === 0) {
const p = Schema._.details(BASE).get($definitions).get(Ref._.name(name));
if (p !== void 0) return kBase(p);
}
@ -74,5 +74,5 @@ export function lookup<R>(namePos: Position | null,
}
export function formatRef(r: Ref): string {
return [... r[0] === $thisModule ? [] : r[0], r[1]].map(s => s.description!).join('.');
return [... r[0], r[1]].map(s => s.description!).join('.');
}

View File

@ -150,14 +150,10 @@ function parseRef(name: string, pos: Position | null, item: symbol): Pattern {
if (s === void 0) invalidPattern(name, item, pos);
if (s[0] === '=') return Record(M.$lit, [Symbol.for(s.slice(1))]);
const pieces = s.split('.');
if (pieces.length === 1) {
return recordPosition(Record(M.$ref, [M.$thisModule, item]), pos);
} else {
return recordPosition(Record(M.$ref, [
pieces.slice(0, pieces.length - 1).map(Symbol.for),
Symbol.for(pieces[pieces.length - 1])
]), pos);
}
return recordPosition(Record(M.$ref, [
pieces.slice(0, pieces.length - 1).map(Symbol.for),
Symbol.for(pieces[pieces.length - 1])
]), pos);
}
function parseBase(name: symbol, body: Array<Input>): Pattern {

View File

@ -59,6 +59,5 @@ Pattern = <<or> [
NamedPattern = <named @name symbol @pattern Pattern> / Pattern .
Ref = <ref @module ModuleRef @name symbol>.
ModuleRef = =thisModule / ModulePath .
Ref = <ref @module ModulePath @name symbol>.
ModulePath = [symbol ...].