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