PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Dependencies/boo/lib/antlr-2.7.5/lib/csharp/src/antlr/MismatchedTokenException.cs

https://github.com/w4x/boolangstudio
C# | 214 lines | 159 code | 24 blank | 31 comment | 14 complexity | ee66383e785318622b46addf86678eba MD5 | raw file
Possible License(s): GPL-2.0
  1. using System;
  2. using StringBuilder = System.Text.StringBuilder;
  3. using BitSet = antlr.collections.impl.BitSet;
  4. using AST = antlr.collections.AST;
  5. namespace antlr
  6. {
  7. /*ANTLR Translator Generator
  8. * Project led by Terence Parr at http://www.jGuru.com
  9. * Software rights: http://www.antlr.org/license.html
  10. *
  11. * $Id:$
  12. */
  13. //
  14. // ANTLR C# Code Generator by Micheal Jordan
  15. // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
  16. // Anthony Oguntimehin
  17. //
  18. // With many thanks to Eric V. Smith from the ANTLR list.
  19. //
  20. [Serializable]
  21. public class MismatchedTokenException : RecognitionException
  22. {
  23. // Token names array for formatting
  24. internal string[] tokenNames;
  25. // The token that was encountered
  26. public IToken token;
  27. // The offending AST node if tree walking
  28. public AST node;
  29. internal string tokenText = null; // taken from node or token object
  30. // Types of tokens
  31. public enum TokenTypeEnum
  32. {
  33. TokenType = 1,
  34. NotTokenType = 2,
  35. RangeType = 3,
  36. NotRangeType = 4,
  37. SetType = 5,
  38. NotSetType = 6
  39. }
  40. // One of the above
  41. public TokenTypeEnum mismatchType;
  42. // For TOKEN/NOT_TOKEN and RANGE/NOT_RANGE
  43. public int expecting;
  44. // For RANGE/NOT_RANGE (expecting is lower bound of range)
  45. public int upper;
  46. // For SET/NOT_SET
  47. public BitSet bset;
  48. /*Looking for AST wildcard, didn't find it */
  49. public MismatchedTokenException() : base("Mismatched Token: expecting any AST node", "<AST>", - 1, - 1)
  50. {
  51. }
  52. // Expected range / not range
  53. public MismatchedTokenException(string[] tokenNames_, AST node_, int lower, int upper_, bool matchNot) :
  54. base("Mismatched Token", "<AST>", - 1, - 1)
  55. {
  56. tokenNames = tokenNames_;
  57. node = node_;
  58. if (node_ == null)
  59. {
  60. tokenText = "<empty tree>";
  61. }
  62. else
  63. {
  64. tokenText = node_.ToString();
  65. }
  66. mismatchType = matchNot ? TokenTypeEnum.NotRangeType : TokenTypeEnum.RangeType;
  67. expecting = lower;
  68. upper = upper_;
  69. }
  70. // Expected token / not token
  71. public MismatchedTokenException(string[] tokenNames_, AST node_, int expecting_, bool matchNot) :
  72. base("Mismatched Token", "<AST>", - 1, - 1)
  73. {
  74. tokenNames = tokenNames_;
  75. node = node_;
  76. if (node_ == null)
  77. {
  78. tokenText = "<empty tree>";
  79. }
  80. else
  81. {
  82. tokenText = node_.ToString();
  83. }
  84. mismatchType = matchNot ? TokenTypeEnum.NotTokenType : TokenTypeEnum.TokenType;
  85. expecting = expecting_;
  86. }
  87. // Expected BitSet / not BitSet
  88. public MismatchedTokenException(string[] tokenNames_, AST node_, BitSet set_, bool matchNot) :
  89. base("Mismatched Token", "<AST>", - 1, - 1)
  90. {
  91. tokenNames = tokenNames_;
  92. node = node_;
  93. if (node_ == null)
  94. {
  95. tokenText = "<empty tree>";
  96. }
  97. else
  98. {
  99. tokenText = node_.ToString();
  100. }
  101. mismatchType = matchNot ? TokenTypeEnum.NotSetType : TokenTypeEnum.SetType;
  102. bset = set_;
  103. }
  104. // Expected range / not range
  105. public MismatchedTokenException(string[] tokenNames_, IToken token_, int lower, int upper_, bool matchNot, string fileName_) :
  106. base("Mismatched Token", fileName_, token_.getLine(), token_.getColumn())
  107. {
  108. tokenNames = tokenNames_;
  109. token = token_;
  110. tokenText = token_.getText();
  111. mismatchType = matchNot ? TokenTypeEnum.NotRangeType : TokenTypeEnum.RangeType;
  112. expecting = lower;
  113. upper = upper_;
  114. }
  115. // Expected token / not token
  116. public MismatchedTokenException(string[] tokenNames_, IToken token_, int expecting_, bool matchNot, string fileName_) :
  117. base("Mismatched Token", fileName_, token_.getLine(), token_.getColumn())
  118. {
  119. tokenNames = tokenNames_;
  120. token = token_;
  121. tokenText = token_.getText();
  122. mismatchType = matchNot ? TokenTypeEnum.NotTokenType : TokenTypeEnum.TokenType;
  123. expecting = expecting_;
  124. }
  125. // Expected BitSet / not BitSet
  126. public MismatchedTokenException(string[] tokenNames_, IToken token_, BitSet set_, bool matchNot, string fileName_) :
  127. base("Mismatched Token", fileName_, token_.getLine(), token_.getColumn())
  128. {
  129. tokenNames = tokenNames_;
  130. token = token_;
  131. tokenText = token_.getText();
  132. mismatchType = matchNot ? TokenTypeEnum.NotSetType : TokenTypeEnum.SetType;
  133. bset = set_;
  134. }
  135. /*
  136. * Returns a clean error message (no line number/column information)
  137. */
  138. override public string Message
  139. {
  140. get
  141. {
  142. StringBuilder sb = new StringBuilder();
  143. switch (mismatchType)
  144. {
  145. case TokenTypeEnum.TokenType:
  146. sb.Append("expecting " + tokenName(expecting) + ", found '" + tokenText + "'");
  147. break;
  148. case TokenTypeEnum.NotTokenType:
  149. sb.Append("expecting anything but " + tokenName(expecting) + "; got it anyway");
  150. break;
  151. case TokenTypeEnum.RangeType:
  152. sb.Append("expecting token in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'");
  153. break;
  154. case TokenTypeEnum.NotRangeType:
  155. sb.Append("expecting token NOT in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'");
  156. break;
  157. case TokenTypeEnum.SetType: case TokenTypeEnum.NotSetType:
  158. sb.Append("expecting " + (mismatchType == TokenTypeEnum.NotSetType ? "NOT " : "") + "one of (");
  159. int[] elems = bset.toArray();
  160. for (int i = 0; i < elems.Length; i++)
  161. {
  162. sb.Append(" ");
  163. sb.Append(tokenName(elems[i]));
  164. }
  165. sb.Append("), found '" + tokenText + "'");
  166. break;
  167. default:
  168. sb.Append(base.Message);
  169. break;
  170. }
  171. return sb.ToString();
  172. }
  173. }
  174. private string tokenName(int tokenType)
  175. {
  176. if (tokenType == Token.INVALID_TYPE)
  177. {
  178. return "<Set of tokens>";
  179. }
  180. else if (tokenType < 0 || tokenType >= tokenNames.Length)
  181. {
  182. return "<" + tokenType.ToString() + ">";
  183. }
  184. else
  185. {
  186. return tokenNames[tokenType];
  187. }
  188. }
  189. }
  190. }