Skip to content

Commit 40f4945

Browse files
committed
standardising the factory type
1 parent be561db commit 40f4945

1 file changed

Lines changed: 14 additions & 21 deletions

File tree

src/slang-utils/create-nonterminal-variant-creator.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ type GenericFunction<U> = U extends any
1616
: never;
1717
type SlangPolymorphicNode = Extract<SlangAstNode, { variant: unknown }>;
1818

19+
type NonterminalVariantFactory<
20+
U extends SlangPolymorphicNode,
21+
T extends StrictPolymorphicNode
22+
> = (
23+
variant: U['variant'],
24+
collected: CollectedMetadata,
25+
options?: ParserOptions<AstNode>
26+
) => T['variant'];
27+
1928
export function createNonterminalVariantSimpleCreator<
2029
U extends SlangPolymorphicNode,
2130
T extends StrictPolymorphicNode
@@ -24,18 +33,10 @@ export function createNonterminalVariantSimpleCreator<
2433
GenericFunction<U['variant']>,
2534
ConstructorsFromInstances<T['variant']>
2635
][]
27-
): (
28-
variant: U['variant'],
29-
collected: CollectedMetadata,
30-
options?: ParserOptions<AstNode>
31-
) => T['variant'] {
36+
): NonterminalVariantFactory<U, T> {
3237
const variantConstructors = new Map(constructors);
3338

34-
return (
35-
variant: U['variant'],
36-
collected: CollectedMetadata,
37-
options?: ParserOptions<AstNode>
38-
): T['variant'] => {
39+
return (variant, collected, options?) => {
3940
const constructor = variantConstructors.get(variant.constructor);
4041
if (constructor !== undefined)
4142
return new constructor(variant, collected, options);
@@ -56,25 +57,17 @@ export function createNonterminalVariantCreator<
5657
GenericFunction<SlangPolymorphicNode>,
5758
ConstructorsFromInstances<StrictPolymorphicNode>
5859
][]
59-
): (
60-
variant: U['variant'] | SlangPolymorphicNode,
61-
collected: CollectedMetadata,
62-
options?: ParserOptions<AstNode>
63-
) => T['variant'] {
60+
): NonterminalVariantFactory<U, T> {
6461
const simpleCreator = createNonterminalVariantSimpleCreator<U, T>(
6562
constructors
6663
);
6764
const extractVariantsConstructor = new Map(extractVariantsConstructors);
6865

69-
return (
70-
variant: U['variant'] | SlangPolymorphicNode,
71-
collected: CollectedMetadata,
72-
options?: ParserOptions<AstNode>
73-
): T['variant'] => {
66+
return (variant, collected, options?) => {
7467
const constructor = extractVariantsConstructor.get(variant.constructor);
7568
if (constructor !== undefined)
7669
return extractVariant(new constructor(variant, collected, options));
7770

78-
return simpleCreator(variant as U['variant'], collected, options);
71+
return simpleCreator(variant, collected, options);
7972
};
8073
}

0 commit comments

Comments
 (0)