source: trunk/yao/share/antlr-2.7.7/lib/csharp/antlr.runtime/antlr/MismatchedCharException.cs @ 1

Last change on this file since 1 was 1, checked in by lnalod, 15 years ago

Initial import of YAO sources

File size: 4.5 KB
Line 
1using System;
2using StringBuilder                     = System.Text.StringBuilder;
3
4using BitSet                            = antlr.collections.impl.BitSet;
5
6namespace antlr
7{
8        /*ANTLR Translator Generator
9        * Project led by Terence Parr at http://www.jGuru.com
10        * Software rights: http://www.antlr.org/license.html
11        *
12        * $Id:$
13        */
14
15        //
16        // ANTLR C# Code Generator by Micheal Jordan
17        //                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
18        //                            Anthony Oguntimehin
19        //
20        // With many thanks to Eric V. Smith from the ANTLR list.
21        //
22       
23        [Serializable]
24        public class MismatchedCharException : RecognitionException
25        {
26                /*
27                * Returns a clean error message (no line number/column information)
28                */
29                override public string Message
30                {
31                        get 
32                        {
33                                StringBuilder sb = new StringBuilder();
34                       
35                                switch (mismatchType)
36                                {
37                                        case CharTypeEnum.CharType: 
38                                                sb.Append("expecting ");   appendCharName(sb, expecting);
39                                                sb.Append(", found ");     appendCharName(sb, foundChar);
40                                                break;
41                               
42                                        case CharTypeEnum.NotCharType: 
43                                                sb.Append("expecting anything but '");
44                                                appendCharName(sb, expecting);
45                                                sb.Append("'; got it anyway");
46                                                break;
47                               
48                                        case CharTypeEnum.RangeType:                           
49                                        case CharTypeEnum.NotRangeType: 
50                                                sb.Append("expecting token ");
51                                                if (mismatchType == CharTypeEnum.NotRangeType)
52                                                        sb.Append("NOT ");
53                                                sb.Append("in range: ");
54                                                appendCharName(sb, expecting);
55                                                sb.Append("..");
56                                                appendCharName(sb, upper);
57                                                sb.Append(", found ");
58                                                appendCharName(sb, foundChar);
59                                                break;
60                               
61                                        case CharTypeEnum.SetType: 
62                                        case CharTypeEnum.NotSetType: 
63                                                sb.Append("expecting " + (mismatchType == CharTypeEnum.NotSetType ? "NOT " : "") + "one of (");
64                                                int[] elems = bset.toArray();
65                                                for (int i = 0; i < elems.Length; i++) 
66                                                {
67                                                        appendCharName(sb, elems[i]);
68                                                }
69                                                sb.Append("), found ");
70                                                appendCharName(sb, foundChar);
71                                                break;
72                               
73                                        default: 
74                                                sb.Append(base.Message);
75                                                break;                         
76                                }                       
77                                return sb.ToString();
78                        }
79                }
80
81                // Types of chars
82
83                public enum CharTypeEnum
84                {
85                        CharType = 1,
86                        NotCharType = 2,
87                        RangeType = 3,
88                        NotRangeType = 4,
89                        SetType = 5,
90                        NotSetType = 6
91                }
92               
93                // One of the above
94                public CharTypeEnum mismatchType;
95               
96                // what was found on the input stream
97                public int foundChar;
98               
99                // For CHAR/NOT_CHAR and RANGE/NOT_RANGE
100                public int expecting;
101               
102                // For RANGE/NOT_RANGE (expecting is lower bound of range)
103                public int upper;
104               
105                // For SET/NOT_SET
106                public BitSet bset;
107               
108                // who knows...they may want to ask scanner questions
109                public CharScanner scanner;
110               
111                /*
112                * MismatchedCharException constructor comment.
113                */
114                public MismatchedCharException() : base("Mismatched char")
115                {
116                }
117               
118                // Expected range / not range
119                public MismatchedCharException(char c, char lower, char upper_, bool matchNot, CharScanner scanner_) : 
120                                        base("Mismatched char", scanner_.getFilename(), scanner_.getLine(), scanner_.getColumn())
121                {
122                        mismatchType = matchNot ? CharTypeEnum.NotRangeType : CharTypeEnum.RangeType;
123                        foundChar = c;
124                        expecting = lower;
125                        upper = upper_;
126                        scanner = scanner_;
127                }
128               
129                // Expected token / not token
130                public MismatchedCharException(char c, char expecting_, bool matchNot, CharScanner scanner_) : 
131                                        base("Mismatched char", scanner_.getFilename(), scanner_.getLine(), scanner_.getColumn())
132                {
133                        mismatchType = matchNot ? CharTypeEnum.NotCharType : CharTypeEnum.CharType;
134                        foundChar = c;
135                        expecting = expecting_;
136                        scanner = scanner_;
137                }
138               
139                // Expected BitSet / not BitSet
140                public MismatchedCharException(char c, BitSet set_, bool matchNot, CharScanner scanner_) :
141                                        base("Mismatched char", scanner_.getFilename(), scanner_.getLine(), scanner_.getColumn())
142                {
143                        mismatchType = matchNot ? CharTypeEnum.NotSetType : CharTypeEnum.SetType;
144                        foundChar = c;
145                        bset = set_;
146                        scanner = scanner_;
147                }               
148
149                /// <summary>
150                /// Append a char to the msg buffer.  If special, then show escaped version
151                /// </summary>
152                /// <param name="sb">Message buffer</param>
153                /// <param name="c">Char to append</param>
154                private void appendCharName(StringBuilder sb, int c) 
155                {
156                        switch (c) 
157                        {
158                                case 65535 :
159                                        // 65535 = (char) -1 = EOF
160                                        sb.Append("'<EOF>'");
161                                        break;
162                                case '\n' :
163                                        sb.Append(@"'\n'");
164                                        break;
165                                case '\r' :
166                                        sb.Append(@"'\r'");
167                                        break;
168                                case '\t' :
169                                        sb.Append(@"'\t'");
170                                        break;
171                                default :
172                                        sb.Append('\'');
173                                        sb.Append((char) c);
174                                        sb.Append('\'');
175                                        break;
176                        }
177                }
178        }
179}
Note: See TracBrowser for help on using the repository browser.