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.Tree 34 { 35 36 /** <summary>A node representing erroneous token range in token stream</summary> */ 37 [System.Serializable] 38 public class CommonErrorNode : CommonTree 39 { 40 public IIntStream input; 41 public IToken start; 42 public IToken stop; 43 public RecognitionException trappedException; 44 CommonErrorNode( ITokenStream input, IToken start, IToken stop, RecognitionException e )45 public CommonErrorNode( ITokenStream input, IToken start, IToken stop, 46 RecognitionException e ) 47 { 48 //System.out.println("start: "+start+", stop: "+stop); 49 if ( stop == null || 50 ( stop.TokenIndex < start.TokenIndex && 51 stop.Type != TokenTypes.EndOfFile ) ) 52 { 53 // sometimes resync does not consume a token (when LT(1) is 54 // in follow set. So, stop will be 1 to left to start. adjust. 55 // Also handle case where start is the first token and no token 56 // is consumed during recovery; LT(-1) will return null. 57 stop = start; 58 } 59 this.input = input; 60 this.start = start; 61 this.stop = stop; 62 this.trappedException = e; 63 } 64 65 #region Properties 66 public override bool IsNil 67 { 68 get 69 { 70 return false; 71 } 72 } 73 public override string Text 74 { 75 get 76 { 77 string badText = null; 78 if ( start is IToken ) 79 { 80 int i = ( (IToken)start ).TokenIndex; 81 int j = ( (IToken)stop ).TokenIndex; 82 if ( ( (IToken)stop ).Type == TokenTypes.EndOfFile ) 83 { 84 j = ( (ITokenStream)input ).Count; 85 } 86 badText = ( (ITokenStream)input ).ToString( i, j ); 87 } 88 else if ( start is ITree ) 89 { 90 badText = ( (ITreeNodeStream)input ).ToString( start, stop ); 91 } 92 else 93 { 94 // people should subclass if they alter the tree type so this 95 // next one is for sure correct. 96 badText = "<unknown>"; 97 } 98 return badText; 99 } 100 set 101 { 102 } 103 } 104 public override int Type 105 { 106 get 107 { 108 return TokenTypes.Invalid; 109 } 110 set 111 { 112 } 113 } 114 #endregion 115 ToString()116 public override string ToString() 117 { 118 if ( trappedException is MissingTokenException ) 119 { 120 return "<missing type: " + 121 ( (MissingTokenException)trappedException ).MissingType + 122 ">"; 123 } 124 else if ( trappedException is UnwantedTokenException ) 125 { 126 return "<extraneous: " + 127 ( (UnwantedTokenException)trappedException ).UnexpectedToken + 128 ", resync=" + Text + ">"; 129 } 130 else if ( trappedException is MismatchedTokenException ) 131 { 132 return "<mismatched token: " + trappedException.Token + ", resync=" + Text + ">"; 133 } 134 else if ( trappedException is NoViableAltException ) 135 { 136 return "<unexpected: " + trappedException.Token + 137 ", resync=" + Text + ">"; 138 } 139 return "<error: " + Text + ">"; 140 } 141 } 142 } 143