import { BlankNode } from "../Tokenization/Nodes/BlankNode" ; import { CommandNode } from "../Tokenization/Nodes/CommandNode" ; import { IBudgieNode } from "../Tokenization/Nodes/IBudgieNode" ; import { TextNode } from "../Tokenization/Nodes/TextNode" ; import { Command } from "./Commands/Command" ; import { CommandsBag } from "./Commands/CommandsBag" ; import { Import } from "./Languages/Imports/Import" ; import { LineResults } from "./LineResults" ; import { ParametersValidator } from "./ParametersValidator" ; /**
 * Renders Budgie nodes into line results.
 */ export class BudgieNodeRenderer { /**
     * Budgie commands indexed by name.
     */ private commandsBag : CommandsBag ; /**
     * Validates whether input parameters match command requirements.
     */ private parametersValidator : ParametersValidator ; param commandsBag public constructor ( commandsBag ) { this . commandsBag = commandsBag ; this . parametersValidator = new ParametersValidator ( ) ; } param node returns public renderNode ( node ) { if ( node instanceof BlankNode ) { return LineResults . newSingleLine ( "" ) ; } if ( node instanceof TextNode ) { return LineResults . newSingleLine ( node . text ) ; } if ( node instanceof CommandNode ) { return this . renderCommandNode ( node ) ; } throw new Error ( `Unknown node type: ${ node . toString ( ) }` ) ; } param node returns private renderCommandNode ( node ) { const command = this . commandsBag . getCommand ( node . command ) ; this . parametersValidator . validate ( node , command . getMetadata ( ) . parameters ) ; const imports = [ ] ; const parameters = [ node . command ] ; for ( const arg of node . args ) { const subResults = this . renderNode ( arg ) ; imports . push ( ... subResults . addedImports ) ; for ( const subResult of subResults . commandResults ) { parameters . push ( subResult . text ) ; } } return command . render ( parameters ) . withImports ( imports ) ; } }