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

129 lines
4.1 KiB
TypeScript
Raw Normal View History

2021-03-19 22:42:43 +00:00
import { Value, is, Position } from '@preserves/core';
2021-03-18 10:15:10 +00:00
import * as M from './gen/schema';
import { SchemaSyntaxError } from './error';
2021-03-19 22:42:43 +00:00
import type { AtomicType } from './compiler/type';
2021-03-21 21:02:34 +00:00
import { JS_KEYWORDS } from './compiler/jskw';
2021-03-11 09:56:49 +00:00
export * from './gen/schema';
2021-03-09 14:59:40 +00:00
2021-03-19 22:42:43 +00:00
export type Builtin = { type: AtomicType, pattern: M.Alternative };
2021-03-09 14:59:40 +00:00
export type Input = Value<never>;
2021-03-21 21:02:34 +00:00
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) && !JS_KEYWORDS.has(s);
}
export function jsId(v: string, kf?: () => string): string {
const s = v
.replace('_', '__')
.replace('*', '_STAR_');
if (isValidJsId(s)) return s;
if (isValidJsId('$' + s)) return '$' + s;
if (kf !== void 0) return kf();
throw new Error(`Internal error: idForLiteral needs to be completed (${v})`);
2021-03-09 15:45:57 +00:00
}
2021-03-09 14:59:40 +00:00
export const ANDSYM = Symbol.for('&');
export const DOT = Symbol.for('.');
export const DOTDOTDOT = Symbol.for('...');
export const EQUALS = Symbol.for('=');
2021-03-11 22:02:18 +00:00
export const INCLUDE = Symbol.for('include');
2021-03-09 14:59:40 +00:00
export const ORSYM = Symbol.for('/');
2021-03-18 10:15:10 +00:00
export type SchemaEnvEntry = { schemaModulePath: M.ModulePath } & (
2021-03-11 16:59:40 +00:00
({
2021-03-14 21:59:53 +00:00
typescriptModulePath: string | null, // null means it's "this module" in disguise
2021-03-18 10:15:10 +00:00
schema: M.Schema,
2021-03-11 16:59:40 +00:00
}) | ({
typescriptModulePath: string,
schema: null,
})
);
2021-03-11 09:56:49 +00:00
export type Environment = Array<SchemaEnvEntry>;
2021-03-09 14:59:40 +00:00
2021-03-11 16:59:40 +00:00
function modsymFor(e: SchemaEnvEntry): string {
return '_i_' + e.schemaModulePath.map(s => s.description!).join('$');
}
export function lookup<R>(namePos: Position | null,
2021-03-18 10:15:10 +00:00
name: M.Ref,
2021-03-11 09:56:49 +00:00
env: Environment,
2021-03-18 10:15:10 +00:00
kLocal: (p: M.Definition) => R,
kOther: (modId: string, modPath: string, p: M.Definition | null) => R): R
2021-03-11 09:56:49 +00:00
{
for (const e of env) {
2021-03-18 10:15:10 +00:00
if (is(e.schemaModulePath, M.Ref._.module(name)) ||
(e.typescriptModulePath === null && M.Ref._.module(name).length === 0))
2021-03-11 09:56:49 +00:00
{
2021-03-11 16:59:40 +00:00
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 {
2021-03-18 10:15:10 +00:00
const p = M.Schema._._field0(e.schema).get(M.$definitions).get(M.Ref._.name(name));
2021-03-11 16:59:40 +00:00
if (p !== void 0) {
if (e.typescriptModulePath === null) {
return kLocal(p);
} else {
return kOther(modsymFor(e), e.typescriptModulePath, p);
}
2021-03-11 09:56:49 +00:00
}
}
}
}
2021-03-09 14:59:40 +00:00
throw new SchemaSyntaxError(`Undefined reference: ${formatRef(name)}`, namePos);
}
2021-03-18 10:15:10 +00:00
export function formatRef(r: M.Ref): string {
2021-03-14 21:59:53 +00:00
return [... r[0], r[1]].map(s => s.description!).join('.');
2021-03-09 14:59:40 +00:00
}
2021-03-18 10:15:10 +00:00
export function unname<R extends M.Pattern | M.SimplePattern>(
p: M.NamedSimplePattern_ | R): M.SimplePattern | R
{
return (p.label === M.$named) ? p[1] : p;
}
export function nameFor<R extends M.Pattern | M.SimplePattern>(
p: M.NamedSimplePattern_ | R): string | undefined
{
2021-03-19 22:42:43 +00:00
return (p.label === M.$named) ? p[0].description! : void 0;
}
export function addNameIfAbsent(p: M.NamedSimplePattern, k: Input): M.NamedSimplePattern {
if (p.label === M.$named) {
return p;
} else {
const s = namelike(k);
if (s !== void 0) {
return M.NamedSimplePattern_(Symbol.for(s), p);
} else {
return p;
}
}
}
2021-03-18 10:15:10 +00:00
// Simple arrays at toplevel for convenience
//
export function simpleArray(p: M.CompoundPattern): M.SimplePattern | undefined {
if (p.label === M.$tuple_STAR_ && p[0].length === 0 && p[1].label !== M.$named) {
return p[1];
} else {
return void 0;
}
}
export function namelike(x: Input): string | undefined {
if (typeof x === 'string') return x;
2021-03-19 22:42:43 +00:00
if (typeof x === 'symbol') return x.description!;
2021-03-18 10:15:10 +00:00
if (typeof x === 'number') return '' + x;
2021-03-19 22:42:43 +00:00
if (typeof x === 'boolean') return '' + x;
2021-03-18 10:15:10 +00:00
return void 0;
}