00001 /* 00002 [The "BSD licence"] 00003 Copyright (c) 2005-2008 Terence Parr 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions 00008 are met: 00009 1. Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 2. Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in the 00013 documentation and/or other materials provided with the distribution. 00014 3. The name of the author may not be used to endorse or promote products 00015 derived from this software without specific prior written permission. 00016 00017 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 package org.antlr.runtime; 00029 00030 import org.antlr.runtime.tree.*; 00031 00062 public class RecognitionException extends Exception { 00064 public transient IntStream input; 00065 00067 public int index; 00068 00073 public Token token; 00074 00078 public Object node; 00079 00081 public int c; 00082 00087 public int line; 00088 00089 public int charPositionInLine; 00090 00096 public boolean approximateLineInfo; 00097 00099 public RecognitionException() { 00100 } 00101 00102 public RecognitionException(IntStream input) { 00103 this.input = input; 00104 this.index = input.index(); 00105 if ( input instanceof TokenStream ) { 00106 this.token = ((TokenStream)input).LT(1); 00107 this.line = token.getLine(); 00108 this.charPositionInLine = token.getCharPositionInLine(); 00109 } 00110 if ( input instanceof TreeNodeStream ) { 00111 extractInformationFromTreeNodeStream(input); 00112 } 00113 else if ( input instanceof CharStream ) { 00114 this.c = input.LA(1); 00115 this.line = ((CharStream)input).getLine(); 00116 this.charPositionInLine = ((CharStream)input).getCharPositionInLine(); 00117 } 00118 else { 00119 this.c = input.LA(1); 00120 } 00121 } 00122 00123 protected void extractInformationFromTreeNodeStream(IntStream input) { 00124 TreeNodeStream nodes = (TreeNodeStream)input; 00125 this.node = nodes.LT(1); 00126 TreeAdaptor adaptor = nodes.getTreeAdaptor(); 00127 Token payload = adaptor.getToken(node); 00128 if ( payload!=null ) { 00129 this.token = payload; 00130 if ( payload.getLine()<= 0 ) { 00131 // imaginary node; no line/pos info; scan backwards 00132 int i = -1; 00133 Object priorNode = nodes.LT(i); 00134 while ( priorNode!=null ) { 00135 Token priorPayload = adaptor.getToken(priorNode); 00136 if ( priorPayload!=null && priorPayload.getLine()>0 ) { 00137 // we found the most recent real line / pos info 00138 this.line = priorPayload.getLine(); 00139 this.charPositionInLine = priorPayload.getCharPositionInLine(); 00140 this.approximateLineInfo = true; 00141 break; 00142 } 00143 --i; 00144 priorNode = nodes.LT(i); 00145 } 00146 } 00147 else { // node created from real token 00148 this.line = payload.getLine(); 00149 this.charPositionInLine = payload.getCharPositionInLine(); 00150 } 00151 } 00152 else if ( this.node instanceof Tree) { 00153 this.line = ((Tree)this.node).getLine(); 00154 this.charPositionInLine = ((Tree)this.node).getCharPositionInLine(); 00155 if ( this.node instanceof CommonTree) { 00156 this.token = ((CommonTree)this.node).token; 00157 } 00158 } 00159 else { 00160 int type = adaptor.getType(this.node); 00161 String text = adaptor.getText(this.node); 00162 this.token = new CommonToken(type, text); 00163 } 00164 } 00165 00167 public int getUnexpectedType() { 00168 if ( input instanceof TokenStream ) { 00169 return token.getType(); 00170 } 00171 else if ( input instanceof TreeNodeStream ) { 00172 TreeNodeStream nodes = (TreeNodeStream)input; 00173 TreeAdaptor adaptor = nodes.getTreeAdaptor(); 00174 return adaptor.getType(node); 00175 } 00176 else { 00177 return c; 00178 } 00179 } 00180 }
1.5.5