Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Attic / Rook / Castle.Rook.Compiler / Parser / antlr / MismatchedTokenException.cs
blob3acb5539a2fdf5645490575e2f6eefad66cb0143
1 using System;
2 using StringBuilder = System.Text.StringBuilder;
4 using BitSet = antlr.collections.impl.BitSet;
5 using AST = antlr.collections.AST;
7 namespace antlr
9 /*ANTLR Translator Generator
10 * Project led by Terence Parr at http://www.jGuru.com
11 * Software rights: http://www.antlr.org/license.html
13 * $Id:$
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.
24 [Serializable]
25 public class MismatchedTokenException : RecognitionException
27 // Token names array for formatting
28 internal string[] tokenNames;
29 // The token that was encountered
30 public IToken token;
31 // The offending AST node if tree walking
32 public AST node;
34 internal string tokenText = null; // taken from node or token object
36 // Types of tokens
37 public enum TokenTypeEnum
39 TokenType = 1,
40 NotTokenType = 2,
41 RangeType = 3,
42 NotRangeType = 4,
43 SetType = 5,
44 NotSetType = 6
46 // One of the above
47 public TokenTypeEnum mismatchType;
49 // For TOKEN/NOT_TOKEN and RANGE/NOT_RANGE
50 public int expecting;
52 // For RANGE/NOT_RANGE (expecting is lower bound of range)
53 public int upper;
55 // For SET/NOT_SET
56 public BitSet bset;
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_;
68 node = node_;
69 if (node_ == null)
71 tokenText = "<empty tree>";
73 else
75 tokenText = node_.ToString();
77 mismatchType = matchNot ? TokenTypeEnum.NotRangeType : TokenTypeEnum.RangeType;
78 expecting = lower;
79 upper = upper_;
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_;
87 node = node_;
88 if (node_ == null)
90 tokenText = "<empty tree>";
92 else
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_;
105 node = node_;
106 if (node_ == null)
108 tokenText = "<empty tree>";
110 else
112 tokenText = node_.ToString();
114 mismatchType = matchNot ? TokenTypeEnum.NotSetType : TokenTypeEnum.SetType;
115 bset = set_;
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_;
123 token = token_;
124 tokenText = token_.getText();
125 mismatchType = matchNot ? TokenTypeEnum.NotRangeType : TokenTypeEnum.RangeType;
126 expecting = lower;
127 upper = upper_;
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_;
135 token = token_;
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_;
146 token = token_;
147 tokenText = token_.getText();
148 mismatchType = matchNot ? TokenTypeEnum.NotSetType : TokenTypeEnum.SetType;
149 bset = set_;
153 * Returns a clean error message (no line number/column information)
155 override public string Message
157 get
159 StringBuilder sb = new StringBuilder();
161 switch (mismatchType)
163 case TokenTypeEnum.TokenType:
164 sb.Append("expecting " + tokenName(expecting) + ", found '" + tokenText + "'");
165 break;
167 case TokenTypeEnum.NotTokenType:
168 sb.Append("expecting anything but " + tokenName(expecting) + "; got it anyway");
169 break;
171 case TokenTypeEnum.RangeType:
172 sb.Append("expecting token in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'");
173 break;
175 case TokenTypeEnum.NotRangeType:
176 sb.Append("expecting token NOT in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'");
177 break;
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++)
184 sb.Append(" ");
185 sb.Append(tokenName(elems[i]));
187 sb.Append("), found '" + tokenText + "'");
188 break;
190 default:
191 sb.Append(base.Message);
192 break;
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() + ">";
208 else
210 return tokenNames[tokenType];