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

31 lines
930 B
TypeScript
Raw Normal View History

2021-03-10 22:15:53 +00:00
import { Value, preserves } from '@preserves/core';
2021-03-09 14:59:40 +00:00
export type Input = Value<never>;
2021-03-09 15:45:57 +00:00
export function isValidToken(s: string, allowLeadingUnderscore = false): boolean {
if (allowLeadingUnderscore) {
return /^[a-zA-Z_][a-zA-Z_0-9]*$/.test(s);
} else {
return /^[a-zA-Z][a-zA-Z_0-9]*$/.test(s);
}
}
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('=');
export const ORSYM = Symbol.for('/');
2021-03-10 22:15:53 +00:00
export * from './schemaschema';
import { Schema, Pattern, $definitions } from './schemaschema';
2021-03-09 14:59:40 +00:00
2021-03-10 22:15:53 +00:00
export type Environment = Array<Schema>;
2021-03-09 14:59:40 +00:00
export function lookup(env: Environment, name: symbol): Pattern {
for (const s of env) {
2021-03-10 22:15:53 +00:00
const p = Schema._.details(s).get($definitions).get(name);
2021-03-09 14:59:40 +00:00
if (p !== void 0) return p;
}
throw new Error(preserves`Schema: unbound name ${name}`);
}