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