Skip to content

Commit

Permalink
improve T.language()
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Jul 30, 2023
1 parent c351ede commit b6c6863
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,18 +592,31 @@ export const lineEnd = match(alt([
*
* @public
*/
export function language<U>(syntaxes: { [K in keyof U]: (r: Record<string, Parser<any>>) => U[K] }): U {
// TODO: 関数の型宣言をいい感じにしたい
const rules: Record<string, Parser<any>> = {};
for (const key of Object.keys(syntaxes)) {
rules[key] = lazy(() => {
const parser = (syntaxes as any)[key](rules);
export function language<U extends Language<U>>(source: LanguageSource<U>): U {
const lang: Record<string, Parser<any>> = {};
for (const key of Object.keys(source)) {
lang[key] = lazy(() => {
const parser = (source as any)[key](lang);
if (parser == null || !(parser instanceof Parser)) {
throw new Error('syntax must return a Parser.');
}
parser.name = key;
return parser;
});
}
return rules as any;
return lang as any;
}

/**
* A type must be a language object.
*
* @public
*/
export type Language<U> = {[K in keyof U]: U[K] extends Parser<unknown> ? U[K] : never };

/**
* Language source
*
* @public
*/
export type LanguageSource<U extends Language<U>> = { [K in keyof U]: (lang: U) => U[K] };

0 comments on commit b6c6863

Please sign in to comment.