forked from syndicate-lang/preserves
Generate as*() alongside is*()
This commit is contained in:
parent
a0d51fab4c
commit
ba2c7e9978
|
@ -17,7 +17,7 @@ export type CompileEnvEntry = {
|
||||||
export function compile(env: Array<CompileEnvEntry>, schema: Schema, preservesModule = '@preserves/core'): string {
|
export function compile(env: Array<CompileEnvEntry>, schema: Schema, preservesModule = '@preserves/core'): string {
|
||||||
const literals = new Dictionary<string, never>();
|
const literals = new Dictionary<string, never>();
|
||||||
const types: Array<Item> = [];
|
const types: Array<Item> = [];
|
||||||
const predicates: Array<Item> = [];
|
const functions: Array<Item> = [];
|
||||||
let temps: Array<string> = [];
|
let temps: Array<string> = [];
|
||||||
|
|
||||||
function environmentWith<R>(name: symbol,
|
function environmentWith<R>(name: symbol,
|
||||||
|
@ -234,12 +234,20 @@ export function compile(env: Array<CompileEnvEntry>, schema: Schema, preservesMo
|
||||||
}
|
}
|
||||||
types.push(
|
types.push(
|
||||||
seq(`export type ${name.description!} = `, typeFor(pattern), `;`));
|
seq(`export type ${name.description!} = `, typeFor(pattern), `;`));
|
||||||
predicates.push(
|
functions.push(
|
||||||
seq('export function ', `is${name.description!}`,
|
seq('export function ', `is${name.description!}`,
|
||||||
'(v: any): v is ', name.description!, ' ',
|
'(v: any): v is ', name.description!, ' ',
|
||||||
block(
|
block(
|
||||||
... temps.length > 0 ? [seq('let ', commas(... temps), ': any')] : [],
|
... temps.length > 0 ? [seq('let ', commas(... temps), ': any')] : [],
|
||||||
seq('return ', recognizer))));
|
seq('return ', recognizer))));
|
||||||
|
functions.push(
|
||||||
|
seq('export function ', `as${name.description!}`,
|
||||||
|
'(v: any): ', name.description!, ' ',
|
||||||
|
block(
|
||||||
|
seq(`if (!is${name.description!}(v)) `,
|
||||||
|
block(`throw new TypeError("${name.description!}")`),
|
||||||
|
' else ',
|
||||||
|
block(`return v`)))));
|
||||||
}
|
}
|
||||||
|
|
||||||
const f = new Formatter();
|
const f = new Formatter();
|
||||||
|
@ -260,7 +268,7 @@ export function compile(env: Array<CompileEnvEntry>, schema: Schema, preservesMo
|
||||||
});
|
});
|
||||||
f.newline();
|
f.newline();
|
||||||
|
|
||||||
predicates.forEach(p => {
|
functions.forEach(p => {
|
||||||
f.write(p);
|
f.write(p);
|
||||||
f.newline();
|
f.newline();
|
||||||
f.newline();
|
f.newline();
|
||||||
|
|
|
@ -54,9 +54,7 @@ export function parseSchema(toplevelTokens: Array<Input>): Schema {
|
||||||
}
|
}
|
||||||
definitions.set(name, parseDefinition(name, clause.slice(2).map(peel)));
|
definitions.set(name, parseDefinition(name, clause.slice(2).map(peel)));
|
||||||
} else if (clause.length === 2 && is(clause[0], M.$version)) {
|
} else if (clause.length === 2 && is(clause[0], M.$version)) {
|
||||||
// TODO: use asVersion
|
version = M.asVersion(clause[1]);
|
||||||
if (!M.isVersion(clause[1])) invalidClause(clause);
|
|
||||||
version = clause[1];
|
|
||||||
} else {
|
} else {
|
||||||
invalidClause(clause);
|
invalidClause(clause);
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,8 +110,12 @@ export function isSchema(v: any): v is Schema {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function asSchema(v: any): Schema {if (!isSchema(v)) {throw new TypeError("Schema");} else {return v;};}
|
||||||
|
|
||||||
export function isVersion(v: any): v is Version {return v === $1;}
|
export function isVersion(v: any): v is Version {return v === $1;}
|
||||||
|
|
||||||
|
export function asVersion(v: any): Version {if (!isVersion(v)) {throw new TypeError("Version");} else {return v;};}
|
||||||
|
|
||||||
export function isPattern(v: any): v is Pattern {
|
export function isPattern(v: any): v is Pattern {
|
||||||
return (
|
return (
|
||||||
(
|
(
|
||||||
|
@ -222,6 +226,8 @@ export function isPattern(v: any): v is Pattern {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function asPattern(v: any): Pattern {if (!isPattern(v)) {throw new TypeError("Pattern");} else {return v;};}
|
||||||
|
|
||||||
export function isNamedPattern(v: any): v is NamedPattern {
|
export function isNamedPattern(v: any): v is NamedPattern {
|
||||||
return (
|
return (
|
||||||
(
|
(
|
||||||
|
@ -233,4 +239,8 @@ export function isNamedPattern(v: any): v is NamedPattern {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function asNamedPattern(v: any): NamedPattern {
|
||||||
|
if (!isNamedPattern(v)) {throw new TypeError("NamedPattern");} else {return v;};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue