Home | History | Annotate | Download | only in tree
      1 /*
      2  [The "BSD license"]
      3  Copyright (c) 2005-2009 Terence Parr
      4  All rights reserved.
      5 
      6  Redistribution and use in source and binary forms, with or without
      7  modification, are permitted provided that the following conditions
      8  are met:
      9  1. Redistributions of source code must retain the above copyright
     10      notice, this list of conditions and the following disclaimer.
     11  2. Redistributions in binary form must reproduce the above copyright
     12      notice, this list of conditions and the following disclaimer in the
     13      documentation and/or other materials provided with the distribution.
     14  3. The name of the author may not be used to endorse or promote products
     15      derived from this software without specific prior written permission.
     16 
     17  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 package org.antlr.runtime.tree;
     29 
     30 import org.antlr.runtime.*;
     31 
     32 /** A node representing erroneous token range in token stream */
     33 public class CommonErrorNode extends CommonTree {
     34 	public IntStream input;
     35 	public Token start;
     36 	public Token stop;
     37 	public RecognitionException trappedException;
     38 
     39 	public CommonErrorNode(TokenStream input, Token start, Token stop,
     40 						   RecognitionException e)
     41 	{
     42 		//System.out.println("start: "+start+", stop: "+stop);
     43 		if ( stop==null ||
     44 			 (stop.getTokenIndex() < start.getTokenIndex() &&
     45 			  stop.getType()!=Token.EOF) )
     46 		{
     47 			// sometimes resync does not consume a token (when LT(1) is
     48 			// in follow set.  So, stop will be 1 to left to start. adjust.
     49 			// Also handle case where start is the first token and no token
     50 			// is consumed during recovery; LT(-1) will return null.
     51 			stop = start;
     52 		}
     53 		this.input = input;
     54 		this.start = start;
     55 		this.stop = stop;
     56 		this.trappedException = e;
     57 	}
     58 
     59 	public boolean isNil() {
     60 		return false;
     61 	}
     62 
     63 	public int getType() {
     64 		return Token.INVALID_TOKEN_TYPE;
     65 	}
     66 
     67 	public String getText() {
     68 		String badText = null;
     69 		if ( start instanceof Token ) {
     70 			int i = ((Token)start).getTokenIndex();
     71 			int j = ((Token)stop).getTokenIndex();
     72 			if ( ((Token)stop).getType() == Token.EOF ) {
     73 				j = ((TokenStream)input).size();
     74 			}
     75 			badText = ((TokenStream)input).toString(i, j);
     76 		}
     77 		else if ( start instanceof Tree ) {
     78 			badText = ((TreeNodeStream)input).toString(start, stop);
     79 		}
     80 		else {
     81 			// people should subclass if they alter the tree type so this
     82 			// next one is for sure correct.
     83 			badText = "<unknown>";
     84 		}
     85 		return badText;
     86 	}
     87 
     88 	public String toString() {
     89 		if ( trappedException instanceof MissingTokenException ) {
     90 			return "<missing type: "+
     91 				   ((MissingTokenException)trappedException).getMissingType()+
     92 				   ">";
     93 		}
     94 		else if ( trappedException instanceof UnwantedTokenException ) {
     95 			return "<extraneous: "+
     96 				   ((UnwantedTokenException)trappedException).getUnexpectedToken()+
     97 				   ", resync="+getText()+">";
     98 		}
     99 		else if ( trappedException instanceof MismatchedTokenException ) {
    100 			return "<mismatched token: "+trappedException.token+", resync="+getText()+">";
    101 		}
    102 		else if ( trappedException instanceof NoViableAltException ) {
    103 			return "<unexpected: "+trappedException.token+
    104 				   ", resync="+getText()+">";
    105 		}
    106 		return "<error: "+getText()+">";
    107 	}
    108 }
    109