PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Dependencies/boo/src/Boo.Lang.Compiler/Ast/Visitors/TextEmitter.cs

https://github.com/w4x/boolangstudio
C# | 203 lines | 150 code | 25 blank | 28 comment | 19 complexity | b5cd8abe22e79a42fba9f4d19b0c167f MD5 | raw file
Possible License(s): GPL-2.0
  1. #region license
  2. // Copyright (c) 2004, Rodrigo B. de Oliveira (rbo@acm.org)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Rodrigo B. de Oliveira nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. namespace Boo.Lang.Compiler.Ast.Visitors
  32. {
  33. /// <summary>
  34. /// </summary>
  35. public class TextEmitter : Boo.Lang.Compiler.Ast.DepthFirstVisitor
  36. {
  37. protected TextWriter _writer;
  38. protected int _indent = 0;
  39. protected string _indentText = "\t";
  40. protected bool _needsIndenting = true;
  41. protected int _disableNewLine = 0;
  42. public TextEmitter(TextWriter writer)
  43. {
  44. if (null == writer)
  45. {
  46. throw new ArgumentNullException("writer");
  47. }
  48. _writer = writer;
  49. }
  50. public string IndentText
  51. {
  52. get
  53. {
  54. return _indentText;
  55. }
  56. set
  57. {
  58. _indentText = value;
  59. }
  60. }
  61. public TextWriter Writer
  62. {
  63. get
  64. {
  65. return _writer;
  66. }
  67. }
  68. public void Indent()
  69. {
  70. _indent += 1;
  71. }
  72. public void Dedent()
  73. {
  74. _indent -= 1;
  75. }
  76. public virtual void WriteIndented()
  77. {
  78. if (_needsIndenting)
  79. {
  80. for (int i=0; i<_indent; ++i)
  81. {
  82. _writer.Write(_indentText);
  83. }
  84. _needsIndenting = false;
  85. }
  86. }
  87. public virtual void WriteLine()
  88. {
  89. if (0 == _disableNewLine)
  90. {
  91. _writer.WriteLine();
  92. _needsIndenting = true;
  93. }
  94. }
  95. protected void DisableNewLine()
  96. {
  97. ++_disableNewLine;
  98. }
  99. protected void EnableNewLine()
  100. {
  101. if (0 == _disableNewLine)
  102. {
  103. throw new InvalidOperationException();
  104. }
  105. --_disableNewLine;
  106. }
  107. public virtual void Write(string s)
  108. {
  109. _writer.Write(s);
  110. }
  111. public void WriteIndented(string format, params object[] args)
  112. {
  113. WriteIndented();
  114. Write(format, args);
  115. }
  116. public void Write(string format, params object[] args)
  117. {
  118. Write(string.Format(format, args));
  119. }
  120. public void WriteLine(string s)
  121. {
  122. WriteIndented(s);
  123. WriteLine();
  124. }
  125. public void WriteLine(string format, params object[] args)
  126. {
  127. WriteIndented(format, args);
  128. WriteLine();
  129. }
  130. protected void WriteCommaSeparatedList<T>(IEnumerable<T> items) where T : Node
  131. {
  132. int i = 0;
  133. foreach (T node in items)
  134. {
  135. if (i++ > 0) Write(", ");
  136. Visit(node);
  137. }
  138. }
  139. protected void WriteArray<T>(NodeCollection<T> items) where T : Node
  140. {
  141. WriteArray(items, null);
  142. }
  143. protected void WriteArray<T>(NodeCollection<T> items, ArrayTypeReference type) where T : Node
  144. {
  145. Write("(");
  146. if (null != type)
  147. {
  148. Write("of ");
  149. type.ElementType.Accept(this);
  150. Write(": ");
  151. }
  152. if (items.Count > 1)
  153. {
  154. for (int i=0; i<items.Count; ++i)
  155. {
  156. if (i>0)
  157. {
  158. Write(", ");
  159. }
  160. Visit(items[i]);
  161. }
  162. }
  163. else
  164. {
  165. if (items.Count > 0)
  166. {
  167. Visit(items[0]);
  168. }
  169. //don't write trailing comma for "of" arrays with 1 item
  170. if (items.Count == 0 || null == type)
  171. {
  172. Write(",");
  173. }
  174. }
  175. Write(")");
  176. }
  177. }
  178. }