1package org.antlr.runtime.tree 2{ 3 4 import org.antlr.runtime.*; 5 6 public class CommonErrorNode extends CommonTree { 7 8 public var input:IntStream; 9 public var start:Token; 10 public var stop:Token; 11 public var trappedException:RecognitionException; 12 13 public function CommonErrorNode(input:TokenStream, start:Token, stop:Token, 14 e:RecognitionException) 15 { 16 //System.out.println("start: "+start+", stop: "+stop); 17 if ( stop==null || 18 (stop.tokenIndex < start.tokenIndex && 19 stop.type!=TokenConstants.EOF) ) 20 { 21 // sometimes resync does not consume a token (when LT(1) is 22 // in follow set. So, stop will be 1 to left to start. adjust. 23 // Also handle case where start is the first token and no token 24 // is consumed during recovery; LT(-1) will return null. 25 stop = start; 26 } 27 this.input = input; 28 this.start = start; 29 this.stop = stop; 30 this.trappedException = e; 31 } 32 33 public override function get isNil():Boolean { 34 return false; 35 } 36 37 public function getType():int { 38 return TokenConstants.INVALID_TOKEN_TYPE; 39 } 40 41 public function getText():String { 42 var badText:String = null; 43 if ( start is Token ) { 44 var i:int = Token(start).tokenIndex; 45 var j:int = Token(stop).tokenIndex; 46 if ( Token(stop).type == TokenConstants.EOF ) { 47 j = TokenStream(input).size; 48 } 49 badText = TokenStream(input).toStringWithRange(i, j); 50 } 51 else if ( start is Tree ) { 52 badText = TreeNodeStream(input).toStringWithRange(start, stop); 53 } 54 else { 55 // people should subclass if they alter the tree type so this 56 // next one is for sure correct. 57 badText = "<unknown>"; 58 } 59 return badText; 60 } 61 62 public override function toString():String { 63 if ( trappedException is MissingTokenException ) { 64 return "<missing type: "+ 65 MissingTokenException(trappedException).missingType+ 66 ">"; 67 } 68 else if ( trappedException is UnwantedTokenException ) { 69 return "<extraneous: "+ 70 UnwantedTokenException(trappedException).unexpectedToken+ 71 ", resync="+getText()+">"; 72 } 73 else if ( trappedException is MismatchedTokenException ) { 74 return "<mismatched token: "+trappedException.token+", resync="+getText()+">"; 75 } 76 else if ( trappedException is NoViableAltException ) { 77 return "<unexpected: "+trappedException.token+ 78 ", resync="+getText()+">"; 79 } 80 return "<error: "+getText()+">"; 81 } 82 83 } 84}