@@ -3,13 +3,14 @@ import { extractVariant } from './extract-variant.js';
33
44import type { ParserOptions } from 'prettier' ;
55import type { AstNode , StrictPolymorphicNode } from '../slang-nodes/types.d.ts' ;
6- import type { CollectedMetadata , SlangAstNode } from '../types.d.ts' ;
6+ import type {
7+ CollectedMetadata ,
8+ SlangAstNode ,
9+ SlangAstNodeConstructors
10+ } from '../types.d.ts' ;
711
812type Constructor < T > = new ( ...args : any ) => T ;
913type ConstructorsFromInstances < U > = U extends any ? Constructor < U > : never ;
10- type GenericFunction < U > = U extends any
11- ? { prototype : unknown ; name : string }
12- : never ;
1314type SlangPolymorphicNode = Extract < SlangAstNode , { variant : unknown } > ;
1415
1516type NonterminalVariantFactory <
@@ -26,16 +27,15 @@ export function createNonterminalVariantSimpleCreator<
2627 T extends StrictPolymorphicNode
2728> (
2829 constructors : [
29- GenericFunction < U [ 'variant' ] > ,
30+ SlangAstNodeConstructors ,
3031 ConstructorsFromInstances < T [ 'variant' ] >
3132 ] [ ]
3233) : NonterminalVariantFactory < U , T > {
33- const variantConstructors = new Map ( constructors ) ;
34-
3534 return ( variant , collected , options ?) => {
36- const constructor = variantConstructors . get ( variant . constructor ) ;
37- if ( constructor !== undefined )
38- return new constructor ( variant , collected , options ) ;
35+ for ( const [ instance , constructor ] of constructors ) {
36+ if ( variant instanceof instance )
37+ return new constructor ( variant , collected , options ) ;
38+ }
3939
4040 throw new Error ( `Unexpected variant: ${ JSON . stringify ( variant ) } ` ) ;
4141 } ;
@@ -46,23 +46,23 @@ export function createNonterminalVariantCreator<
4646 T extends StrictPolymorphicNode
4747> (
4848 constructors : [
49- GenericFunction < U [ 'variant' ] > ,
49+ SlangAstNodeConstructors ,
5050 ConstructorsFromInstances < T [ 'variant' ] >
5151 ] [ ] ,
5252 extractVariantConstructors : [
53- GenericFunction < SlangPolymorphicNode > ,
53+ SlangAstNodeConstructors ,
5454 ConstructorsFromInstances < StrictPolymorphicNode >
5555 ] [ ]
5656) : NonterminalVariantFactory < U , T > {
5757 const simpleCreator = createNonterminalVariantSimpleCreator < U , T > (
5858 constructors
5959 ) ;
60- const extractVariantConstructor = new Map ( extractVariantConstructors ) ;
6160
6261 return ( variant , collected , options ?) => {
63- const constructor = extractVariantConstructor . get ( variant . constructor ) ;
64- if ( constructor !== undefined )
65- return extractVariant ( new constructor ( variant , collected , options ) ) ;
62+ for ( const [ instance , constructor ] of extractVariantConstructors ) {
63+ if ( variant instanceof instance )
64+ return extractVariant ( new constructor ( variant , collected , options ) ) ;
65+ }
6666
6767 return simpleCreator ( variant , collected , options ) ;
6868 } ;
0 commit comments