preserves/implementations/javascript/packages/schema/src/meta.ts

128 lines
4.2 KiB
TypeScript

import { is, Position } from '@preserves/core';
import * as M from './gen/schema';
import { SchemaSyntaxError } from './error';
import { isJsKeyword } from './compiler/jskw';
export * from './gen/schema';
export type Input = M._val;
export function isValidToken(s: string): boolean {
return /^[a-zA-Z][a-zA-Z_0-9]*$/.test(s);
}
export function isValidJsId(s: string): boolean {
return /^[$_a-zA-Z][$_a-zA-Z0-9]*$/.test(s) && !isJsKeyword(s);
}
export function jsId(v: string, kf?: () => string): string {
return jsToken(v.replace('$', '$$').replace('*', '$STAR$'), kf);
}
export function jsToken(s: string, kf?: () => string): string {
if (isValidJsId(s)) return s;
if (isValidJsId('$' + s)) return '$' + s;
if (kf !== void 0) return kf();
throw new Error(`Internal error: jsToken needs to be completed (${s})`);
}
export const ANDSYM = Symbol.for('&');
export const DOT = Symbol.for('.');
export const DOTDOTDOT = Symbol.for('...');
export const EQUALS = Symbol.for('=');
export const INCLUDE = Symbol.for('include');
export const ORSYM = Symbol.for('/');
export type SchemaEnvEntry = { schemaModulePath: M.ModulePath } & (
({
typescriptModulePath: string | null, // null means it's "this module" in disguise
schema: M.Schema,
}) | ({
typescriptModulePath: string,
schema: null,
})
);
export type Environment = Array<SchemaEnvEntry>;
function modsymFor(e: SchemaEnvEntry): string {
return '_i_' + e.schemaModulePath.map(s => s.description!).join('$');
}
export function lookup<R>(namePos: Position | null,
name: M.Ref,
env: Environment,
kLocal: (p: M.Definition) => R,
kOther: (modId: string, modPath: string, p: M.Definition | null) => R): R
{
for (const e of env) {
if (is(e.schemaModulePath, name.module) ||
(e.typescriptModulePath === null && name.module.length === 0))
{
if (e.schema === null) {
// It's an artificial module, not from a schema. Assume the identifier is present.
return kOther(modsymFor(e), e.typescriptModulePath, null);
} else {
const p = e.schema.definitions.get(name.name);
if (p !== void 0) {
if (e.typescriptModulePath === null) {
return kLocal(p);
} else {
return kOther(modsymFor(e), e.typescriptModulePath, p);
}
}
}
}
}
throw new SchemaSyntaxError(`Undefined reference: ${formatRef(name)}`, namePos);
}
export function formatRef(r: M.Ref): string {
return [... r.module, r.name].map(s => s.description!).join('.');
}
export function unnamePattern(p: M.NamedPattern): M.Pattern {
return (p._variant === 'named') ? M.Pattern.SimplePattern(p.value.pattern) : p.value;
}
export function unnameSimplePattern(p: M.NamedSimplePattern): M.SimplePattern {
return (p._variant === 'named') ? p.value.pattern : p.value;
}
export function promoteNamedSimplePattern(p: M.NamedSimplePattern): M.NamedPattern {
return (p._variant === 'named') ? p : M.NamedPattern.anonymous(M.Pattern.SimplePattern(p.value));
}
export function nameFor(p: M.NamedSimplePattern | M.NamedPattern) : string | undefined {
return (p._variant === 'named') ? p.value.name.description! : void 0;
}
export function anonymousSimplePattern(p: M.SimplePattern): M.NamedPattern {
return M.NamedPattern.anonymous(M.Pattern.SimplePattern(p));
}
export function addNameIfAbsent(p: M.NamedSimplePattern, k: M._val): M.NamedSimplePattern {
if (p._variant === 'named') {
return p;
} else {
const s = namelike(k);
if (s !== void 0) {
return M.NamedSimplePattern.named(M.NamedSimplePattern_({
name: Symbol.for(s),
pattern: p.value
}));
} else {
return p;
}
}
}
export function namelike(x: Input): string | undefined {
if (typeof x === 'string') return x;
if (typeof x === 'symbol') return x.description!;
if (typeof x === 'number') return '' + x;
if (typeof x === 'boolean') return '' + x;
return void 0;
}