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