• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CommonErrorNode(TokenStream input, Token start, Token stop, RecognitionException e)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 	@Override
isNil()60 	public boolean isNil() {
61 		return false;
62 	}
63 
64 	@Override
getType()65 	public int getType() {
66 		return Token.INVALID_TOKEN_TYPE;
67 	}
68 
69 	@Override
getText()70 	public String getText() {
71 		String badText;
72 		if ( start instanceof Token ) {
73 			int i = start.getTokenIndex();
74 			int j = stop.getTokenIndex();
75 			if ( stop.getType() == Token.EOF ) {
76 				j = ((TokenStream)input).size();
77 			}
78 			badText = ((TokenStream)input).toString(i, j);
79 		}
80 		else if ( start instanceof Tree ) {
81 			badText = ((TreeNodeStream)input).toString(start, stop);
82 		}
83 		else {
84 			// people should subclass if they alter the tree type so this
85 			// next one is for sure correct.
86 			badText = "<unknown>";
87 		}
88 		return badText;
89 	}
90 
91 	@Override
toString()92 	public String toString() {
93 		if ( trappedException instanceof MissingTokenException ) {
94 			return "<missing type: "+
95 				   ((MissingTokenException)trappedException).getMissingType()+
96 				   ">";
97 		}
98 		else if ( trappedException instanceof UnwantedTokenException ) {
99 			return "<extraneous: "+
100 				   ((UnwantedTokenException)trappedException).getUnexpectedToken()+
101 				   ", resync="+getText()+">";
102 		}
103 		else if ( trappedException instanceof MismatchedTokenException ) {
104 			return "<mismatched token: "+trappedException.token+", resync="+getText()+">";
105 		}
106 		else if ( trappedException instanceof NoViableAltException ) {
107 			return "<unexpected: "+trappedException.token+
108 				   ", resync="+getText()+">";
109 		}
110 		return "<error: "+getText()+">";
111 	}
112 }
113