1 /* 2 * Note to JL: Removed LINQ usages from original 3 * 4 * [The "BSD licence"] 5 * Copyright (c) 2005-2008 Terence Parr 6 * All rights reserved. 7 * 8 * Conversion to C#: 9 * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 namespace Antlr.Runtime { 36 using System; 37 using System.Collections.Generic; 38 using System.Collections.ObjectModel; 39 using ArgumentNullException = System.ArgumentNullException; 40 using Exception = System.Exception; 41 using SerializationInfo = System.Runtime.Serialization.SerializationInfo; 42 using StreamingContext = System.Runtime.Serialization.StreamingContext; 43 44 /** <summary>A mismatched char or Token or tree node</summary> */ 45 [System.Serializable] 46 public class MismatchedTokenException : RecognitionException { 47 private readonly int _expecting = TokenTypes.Invalid; 48 private readonly ReadOnlyCollection<string> _tokenNames; 49 MismatchedTokenException()50 public MismatchedTokenException() { 51 } 52 MismatchedTokenException(string message)53 public MismatchedTokenException(string message) 54 : base(message) { 55 } 56 MismatchedTokenException(string message, Exception innerException)57 public MismatchedTokenException(string message, Exception innerException) 58 : base(message, innerException) { 59 } 60 MismatchedTokenException(int expecting, IIntStream input)61 public MismatchedTokenException(int expecting, IIntStream input) 62 : this(expecting, input, null) { 63 } 64 MismatchedTokenException(int expecting, IIntStream input, IList<string> tokenNames)65 public MismatchedTokenException(int expecting, IIntStream input, IList<string> tokenNames) 66 : base(input) { 67 this._expecting = expecting; 68 69 if (tokenNames != null) 70 this._tokenNames = new List<string>(tokenNames).AsReadOnly(); 71 } MismatchedTokenException(string message, int expecting, IIntStream input, IList<string> tokenNames)72 public MismatchedTokenException(string message, int expecting, IIntStream input, IList<string> tokenNames) 73 : base(message, input) { 74 this._expecting = expecting; 75 76 if (tokenNames != null) { 77 this._tokenNames = new ReadOnlyCollection<string>(new List<string>(tokenNames)); 78 } 79 } 80 MismatchedTokenException(string message, int expecting, IIntStream input, IList<string> tokenNames, Exception innerException)81 public MismatchedTokenException(string message, int expecting, IIntStream input, IList<string> tokenNames, Exception innerException) 82 : base(message, input, innerException) { 83 this._expecting = expecting; 84 85 if (tokenNames != null) 86 this._tokenNames = new ReadOnlyCollection<string>(new List<string>(tokenNames)); 87 } 88 MismatchedTokenException(SerializationInfo info, StreamingContext context)89 protected MismatchedTokenException(SerializationInfo info, StreamingContext context) 90 : base(info, context) { 91 if (info == null) 92 throw new ArgumentNullException("info"); 93 94 this._expecting = info.GetInt32("Expecting"); 95 this._tokenNames = new ReadOnlyCollection<string>((string[])info.GetValue("TokenNames", typeof(string[]))); 96 } 97 98 public int Expecting { 99 get { 100 return _expecting; 101 } 102 } 103 104 public ReadOnlyCollection<string> TokenNames { 105 get { 106 return _tokenNames; 107 } 108 } 109 GetObjectData(SerializationInfo info, StreamingContext context)110 public override void GetObjectData(SerializationInfo info, StreamingContext context) { 111 if (info == null) 112 throw new ArgumentNullException("info"); 113 114 base.GetObjectData(info, context); 115 info.AddValue("Expecting", _expecting); 116 var tokenArray = default(string[]); 117 if (_tokenNames != null) { 118 tokenArray = new string[_tokenNames.Count]; 119 int i = 0; 120 foreach (var token in _tokenNames) { 121 tokenArray[i++] = token; 122 } 123 } 124 info.AddValue("TokenNames", tokenArray); 125 } 126 ToString()127 public override string ToString() { 128 int unexpectedType = UnexpectedType; 129 string unexpected = (TokenNames != null && unexpectedType >= 0 && unexpectedType < TokenNames.Count) ? TokenNames[unexpectedType] : unexpectedType.ToString(); 130 string expected = (TokenNames != null && Expecting >= 0 && Expecting < TokenNames.Count) ? TokenNames[Expecting] : Expecting.ToString(); 131 return "MismatchedTokenException(" + unexpected + "!=" + expected + ")"; 132 } 133 } 134 } 135