1// Copyright (c) Microsoft. All rights reserved. Licensed under the Apache License, Version 2.0. 2// See LICENSE.txt in the project root for complete license information. 3 4///<reference path='typescript.ts' /> 5 6module TypeScript { 7 // TODO: refactor indent logic for use in emit 8 export class PrintContext { 9 public builder = ""; 10 public indent1 = " "; 11 public indentStrings: string[] = []; 12 public indentAmt = 0; 13 14 constructor (public outfile: ITextWriter, public parser: Parser) { 15 } 16 17 public increaseIndent() { 18 this.indentAmt++; 19 } 20 21 public decreaseIndent() { 22 this.indentAmt--; 23 } 24 25 public startLine() { 26 if (this.builder.length > 0) { 27 CompilerDiagnostics.Alert(this.builder); 28 } 29 var indentString = this.indentStrings[this.indentAmt]; 30 if (indentString === undefined) { 31 indentString = ""; 32 for (var i = 0; i < this.indentAmt; i++) { 33 indentString += this.indent1; 34 } 35 this.indentStrings[this.indentAmt] = indentString; 36 } 37 this.builder += indentString; 38 } 39 40 public write(s) { 41 this.builder += s; 42 } 43 44 public writeLine(s) { 45 this.builder += s; 46 this.outfile.WriteLine(this.builder); 47 this.builder = ""; 48 } 49 50 } 51 52 export function prePrintAST(ast: AST, parent: AST, walker: IAstWalker) { 53 var pc: PrintContext = <PrintContext>walker.state; 54 55 ast.print(pc); 56 pc.increaseIndent(); 57 return ast; 58 } 59 60 61 export function postPrintAST(ast: AST, parent: AST, walker: IAstWalker) { 62 var pc: PrintContext = <PrintContext>walker.state; 63 pc.decreaseIndent(); 64 return ast; 65 } 66}