2 using StringBuilder
= System
.Text
.StringBuilder
;
4 using BitSet
= antlr
.collections
.impl
.BitSet
;
5 using AST
= antlr
.collections
.AST
;
9 /*ANTLR Translator Generator
10 * Project led by Terence Parr at http://www.jGuru.com
11 * Software rights: http://www.antlr.org/license.html
17 // ANTLR C# Code Generator by Micheal Jordan
18 // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
19 // Anthony Oguntimehin
21 // With many thanks to Eric V. Smith from the ANTLR list.
25 public class MismatchedTokenException
: RecognitionException
27 // Token names array for formatting
28 internal string[] tokenNames
;
29 // The token that was encountered
31 // The offending AST node if tree walking
34 internal string tokenText
= null; // taken from node or token object
37 public enum TokenTypeEnum
47 public TokenTypeEnum mismatchType
;
49 // For TOKEN/NOT_TOKEN and RANGE/NOT_RANGE
52 // For RANGE/NOT_RANGE (expecting is lower bound of range)
58 /*Looking for AST wildcard, didn't find it */
59 public MismatchedTokenException() : base("Mismatched Token: expecting any AST node", "<AST>", - 1, - 1)
63 // Expected range / not range
64 public MismatchedTokenException(string[] tokenNames_
, AST node_
, int lower
, int upper_
, bool matchNot
) :
65 base("Mismatched Token", "<AST>", - 1, - 1)
67 tokenNames
= tokenNames_
;
71 tokenText
= "<empty tree>";
75 tokenText
= node_
.ToString();
77 mismatchType
= matchNot
? TokenTypeEnum
.NotRangeType
: TokenTypeEnum
.RangeType
;
82 // Expected token / not token
83 public MismatchedTokenException(string[] tokenNames_
, AST node_
, int expecting_
, bool matchNot
) :
84 base("Mismatched Token", "<AST>", - 1, - 1)
86 tokenNames
= tokenNames_
;
90 tokenText
= "<empty tree>";
94 tokenText
= node_
.ToString();
96 mismatchType
= matchNot
? TokenTypeEnum
.NotTokenType
: TokenTypeEnum
.TokenType
;
97 expecting
= expecting_
;
100 // Expected BitSet / not BitSet
101 public MismatchedTokenException(string[] tokenNames_
, AST node_
, BitSet set_
, bool matchNot
) :
102 base("Mismatched Token", "<AST>", - 1, - 1)
104 tokenNames
= tokenNames_
;
108 tokenText
= "<empty tree>";
112 tokenText
= node_
.ToString();
114 mismatchType
= matchNot
? TokenTypeEnum
.NotSetType
: TokenTypeEnum
.SetType
;
118 // Expected range / not range
119 public MismatchedTokenException(string[] tokenNames_
, IToken token_
, int lower
, int upper_
, bool matchNot
, string fileName_
) :
120 base("Mismatched Token", fileName_
, token_
.getLine(), token_
.getColumn())
122 tokenNames
= tokenNames_
;
124 tokenText
= token_
.getText();
125 mismatchType
= matchNot
? TokenTypeEnum
.NotRangeType
: TokenTypeEnum
.RangeType
;
130 // Expected token / not token
131 public MismatchedTokenException(string[] tokenNames_
, IToken token_
, int expecting_
, bool matchNot
, string fileName_
) :
132 base("Mismatched Token", fileName_
, token_
.getLine(), token_
.getColumn())
134 tokenNames
= tokenNames_
;
136 tokenText
= token_
.getText();
137 mismatchType
= matchNot
? TokenTypeEnum
.NotTokenType
: TokenTypeEnum
.TokenType
;
138 expecting
= expecting_
;
141 // Expected BitSet / not BitSet
142 public MismatchedTokenException(string[] tokenNames_
, IToken token_
, BitSet set_
, bool matchNot
, string fileName_
) :
143 base("Mismatched Token", fileName_
, token_
.getLine(), token_
.getColumn())
145 tokenNames
= tokenNames_
;
147 tokenText
= token_
.getText();
148 mismatchType
= matchNot
? TokenTypeEnum
.NotSetType
: TokenTypeEnum
.SetType
;
153 * Returns a clean error message (no line number/column information)
155 override public string Message
159 StringBuilder sb
= new StringBuilder();
161 switch (mismatchType
)
163 case TokenTypeEnum
.TokenType
:
164 sb
.Append("expecting " + tokenName(expecting
) + ", found '" + tokenText
+ "'");
167 case TokenTypeEnum
.NotTokenType
:
168 sb
.Append("expecting anything but " + tokenName(expecting
) + "; got it anyway");
171 case TokenTypeEnum
.RangeType
:
172 sb
.Append("expecting token in range: " + tokenName(expecting
) + ".." + tokenName(upper
) + ", found '" + tokenText
+ "'");
175 case TokenTypeEnum
.NotRangeType
:
176 sb
.Append("expecting token NOT in range: " + tokenName(expecting
) + ".." + tokenName(upper
) + ", found '" + tokenText
+ "'");
179 case TokenTypeEnum
.SetType
: case TokenTypeEnum
.NotSetType
:
180 sb
.Append("expecting " + (mismatchType
== TokenTypeEnum
.NotSetType
? "NOT " : "") + "one of (");
181 int[] elems
= bset
.toArray();
182 for (int i
= 0; i
< elems
.Length
; i
++)
185 sb
.Append(tokenName(elems
[i
]));
187 sb
.Append("), found '" + tokenText
+ "'");
191 sb
.Append(base.Message
);
194 return sb
.ToString();
198 private string tokenName(int tokenType
)
200 if (tokenType
== Token
.INVALID_TYPE
)
202 return "<Set of tokens>";
204 else if (tokenType
< 0 || tokenType
>= tokenNames
.Length
)
206 return "<" + tokenType
.ToString() + ">";
210 return tokenNames
[tokenType
];