1import { Node, objectAllocator, SyntaxKind } from "../_namespaces/ts"; 2 3/** 4 * A `BaseNodeFactory` is an abstraction over an `ObjectAllocator` that handles caching `Node` constructors 5 * and allocating `Node` instances based on a set of predefined types. 6 * 7 * @internal 8 */ 9export interface BaseNodeFactory { 10 createBaseSourceFileNode(kind: SyntaxKind): Node; 11 createBaseIdentifierNode(kind: SyntaxKind): Node; 12 createBasePrivateIdentifierNode(kind: SyntaxKind): Node; 13 createBaseTokenNode(kind: SyntaxKind): Node; 14 createBaseNode(kind: SyntaxKind): Node; 15} 16 17/** 18 * Creates a `BaseNodeFactory` which can be used to create `Node` instances from the constructors provided by the object allocator. 19 * 20 * @internal 21 */ 22export function createBaseNodeFactory(): BaseNodeFactory { 23 let NodeConstructor: new (kind: SyntaxKind, pos?: number, end?: number) => Node; 24 let TokenConstructor: new (kind: SyntaxKind, pos?: number, end?: number) => Node; 25 let IdentifierConstructor: new (kind: SyntaxKind, pos?: number, end?: number) => Node; 26 let PrivateIdentifierConstructor: new (kind: SyntaxKind, pos?: number, end?: number) => Node; 27 let SourceFileConstructor: new (kind: SyntaxKind, pos?: number, end?: number) => Node; 28 29 return { 30 createBaseSourceFileNode, 31 createBaseIdentifierNode, 32 createBasePrivateIdentifierNode, 33 createBaseTokenNode, 34 createBaseNode 35 }; 36 37 function createBaseSourceFileNode(kind: SyntaxKind): Node { 38 return new (SourceFileConstructor || (SourceFileConstructor = objectAllocator.getSourceFileConstructor()))(kind, /*pos*/ -1, /*end*/ -1); 39 } 40 41 function createBaseIdentifierNode(kind: SyntaxKind): Node { 42 return new (IdentifierConstructor || (IdentifierConstructor = objectAllocator.getIdentifierConstructor()))(kind, /*pos*/ -1, /*end*/ -1); 43 } 44 45 function createBasePrivateIdentifierNode(kind: SyntaxKind): Node { 46 return new (PrivateIdentifierConstructor || (PrivateIdentifierConstructor = objectAllocator.getPrivateIdentifierConstructor()))(kind, /*pos*/ -1, /*end*/ -1); 47 } 48 49 function createBaseTokenNode(kind: SyntaxKind): Node { 50 return new (TokenConstructor || (TokenConstructor = objectAllocator.getTokenConstructor()))(kind, /*pos*/ -1, /*end*/ -1); 51 } 52 53 function createBaseNode(kind: SyntaxKind): Node { 54 return new (NodeConstructor || (NodeConstructor = objectAllocator.getNodeConstructor()))(kind, /*pos*/ -1, /*end*/ -1); 55 } 56} 57