PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Boo.Lang.Compiler/CompilerError.cs

https://github.com/boo/boo-lang
C# | 148 lines | 100 code | 17 blank | 31 comment | 8 complexity | 37132b34cd8c54ae0bbc1369f77253c3 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.Text;
  30. using Boo.Lang.Compiler.Ast;
  31. namespace Boo.Lang.Compiler
  32. {
  33. /// <summary>
  34. /// A compilation error.
  35. /// </summary>
  36. [Serializable]
  37. public class CompilerError : ApplicationException
  38. {
  39. LexicalInfo _lexicalInfo;
  40. string _code;
  41. public CompilerError(string code, LexicalInfo lexicalInfo, Exception cause, params object[] args) : base(ResourceManager.Format(code, args), cause)
  42. {
  43. if (null == lexicalInfo)
  44. {
  45. throw new ArgumentNullException("lexicalInfo");
  46. }
  47. _code = code;
  48. _lexicalInfo = lexicalInfo;
  49. }
  50. public CompilerError(string code, Exception cause, params object[] args) : this(code, LexicalInfo.Empty, cause, args)
  51. {
  52. }
  53. public CompilerError(string code, LexicalInfo lexicalInfo, params object[] args) : base(ResourceManager.Format(code, args))
  54. {
  55. if (null == lexicalInfo)
  56. {
  57. throw new ArgumentNullException("lexicalInfo");
  58. }
  59. _code = code;
  60. _lexicalInfo = lexicalInfo;
  61. }
  62. public CompilerError(string code, LexicalInfo lexicalInfo, string message, Exception cause) : base(message, cause)
  63. {
  64. if (null == lexicalInfo)
  65. {
  66. throw new ArgumentNullException("lexicalInfo");
  67. }
  68. _code = code;
  69. _lexicalInfo = lexicalInfo;
  70. }
  71. public CompilerError(LexicalInfo lexicalInfo, string message, Exception cause) : this("BCE0000", lexicalInfo, message, cause)
  72. {
  73. }
  74. public CompilerError(Node node, string message, Exception cause) : this(node.LexicalInfo, message, cause)
  75. {
  76. }
  77. public CompilerError(Node node, string message) : this(node, message, null)
  78. {
  79. }
  80. public CompilerError(LexicalInfo data, string message) : this(data, message, null)
  81. {
  82. }
  83. public CompilerError(LexicalInfo data, Exception cause) : this(data, cause.Message, cause)
  84. {
  85. }
  86. public CompilerError(string message) : this(LexicalInfo.Empty, message, null)
  87. {
  88. }
  89. /// <summary>
  90. /// Error code.
  91. /// </summary>
  92. public string Code
  93. {
  94. get
  95. {
  96. return _code;
  97. }
  98. }
  99. public LexicalInfo LexicalInfo
  100. {
  101. get
  102. {
  103. return _lexicalInfo;
  104. }
  105. }
  106. override public string ToString()
  107. {
  108. return ToString(false);
  109. }
  110. public string ToString(bool verbose)
  111. {
  112. StringBuilder sb = new StringBuilder();
  113. if (_lexicalInfo.Line > 0)
  114. {
  115. sb.Append(_lexicalInfo);
  116. sb.Append(": ");
  117. }
  118. sb.Append(_code);
  119. sb.Append(": ");
  120. if (verbose)
  121. {
  122. sb.Append(base.ToString());
  123. }
  124. else
  125. {
  126. sb.Append(Message);
  127. }
  128. return sb.ToString();
  129. }
  130. }
  131. }