1/* 2 [The "BSD licence"] 3 Copyright (c) 2005-2006 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*/ 28package org.antlr.runtime { 29 import org.antlr.runtime.tree.Tree; 30 import org.antlr.runtime.tree.TreeNodeStream; 31 import org.antlr.runtime.tree.TreeAdaptor; 32 import org.antlr.runtime.tree.CommonTree; 33 /** The root of the ANTLR exception hierarchy. 34 * 35 * To avoid English-only error messages and to generally make things 36 * as flexible as possible, these exceptions are not created with strings, 37 * but rather the information necessary to generate an error. Then 38 * the various reporting methods in Parser and Lexer can be overridden 39 * to generate a localized error message. For example, MismatchedToken 40 * exceptions are built with the expected token type. 41 * So, don't expect getMessage() to return anything. 42 * 43 * Note that as of Java 1.4, you can access the stack trace, which means 44 * that you can compute the complete trace of rules from the start symbol. 45 * This gives you considerable context information with which to generate 46 * useful error messages. 47 * 48 * ANTLR generates code that throws exceptions upon recognition error and 49 * also generates code to catch these exceptions in each rule. If you 50 * want to quit upon first error, you can turn off the automatic error 51 * handling mechanism using rulecatch action, but you still need to 52 * override methods mismatch and recoverFromMismatchSet. 53 * 54 * In general, the recognition exceptions can track where in a grammar a 55 * problem occurred and/or what was the expected input. While the parser 56 * knows its state (such as current input symbol and line info) that 57 * state can change before the exception is reported so current token index 58 * is computed and stored at exception time. From this info, you can 59 * perhaps print an entire line of input not just a single token, for example. 60 * Better to just say the recognizer had a problem and then let the parser 61 * figure out a fancy report. 62 */ 63 public class RecognitionException extends Error { 64 /** What input stream did the error occur in? */ 65 public var input:IntStream; 66 67 /** What is index of token/char were we looking at when the error occurred? */ 68 public var index:int; 69 70 /** The current Token when an error occurred. Since not all streams 71 * can retrieve the ith Token, we have to track the Token object. 72 * For parsers. Even when it's a tree parser, token might be set. 73 */ 74 public var token:Token; 75 76 /** If this is a tree parser exception, node is set to the node with 77 * the problem. 78 */ 79 public var node:Object; 80 81 /** The current char when an error occurred. For lexers. */ 82 public var c:int; 83 84 /** Track the line at which the error occurred in case this is 85 * generated from a lexer. We need to track this since the 86 * unexpected char doesn't carry the line info. 87 */ 88 public var line:int; 89 90 public var charPositionInLine:int; 91 92 /** If you are parsing a tree node stream, you will encounter som 93 * imaginary nodes w/o line/col info. We now search backwards looking 94 * for most recent token with line/col info, but notify getErrorHeader() 95 * that info is approximate. 96 */ 97 public var approximateLineInfo:Boolean; 98 99 public function RecognitionException(input:IntStream = null) { 100 if (input == null) { 101 return; 102 } 103 this.input = input; 104 this.index = input.index; 105 if ( input is TokenStream ) { 106 this.token = TokenStream(input).LT(1); 107 this.line = token.line; 108 this.charPositionInLine = token.charPositionInLine; 109 } 110 if ( input is TreeNodeStream ) { 111 extractInformationFromTreeNodeStream(input); 112 } 113 else if ( input is CharStream ) { 114 this.c = input.LA(1); 115 this.line = CharStream(input).line; 116 this.charPositionInLine = CharStream(input).charPositionInLine; 117 } 118 else { 119 this.c = input.LA(1); 120 } 121 } 122 123 protected function extractInformationFromTreeNodeStream(input:IntStream):void { 124 var nodes:TreeNodeStream = TreeNodeStream(input); 125 this.node = nodes.LT(1); 126 var adaptor:TreeAdaptor = nodes.treeAdaptor; 127 var payload:Token = adaptor.getToken(node); 128 if ( payload!=null ) { 129 this.token = payload; 130 if ( payload.line<= 0 ) { 131 // imaginary node; no line/pos info; scan backwards 132 var i:int = -1; 133 var priorNode:Object = nodes.LT(i); 134 while ( priorNode!=null ) { 135 var priorPayload:Token = adaptor.getToken(priorNode); 136 if ( priorPayload!=null && priorPayload.line > 0 ) { 137 // we found the most recent real line / pos info 138 this.line = priorPayload.line; 139 this.charPositionInLine = priorPayload.charPositionInLine; 140 this.approximateLineInfo = true; 141 break; 142 } 143 --i; 144 priorNode = nodes.LT(i); 145 } 146 } 147 else { // node created from real token 148 this.line = payload.line; 149 this.charPositionInLine = payload.charPositionInLine; 150 } 151 } 152 else if ( this.node is Tree) { 153 this.line = this.node.line; 154 this.charPositionInLine = this.node.charPositionInLine; 155 if ( this.node is CommonTree) { 156 this.token = this.node.token; 157 } 158 } 159 else { 160 var type:int = adaptor.getType(this.node); 161 var text:String = adaptor.getText(this.node); 162 this.token = new CommonToken(type, text); 163 } 164 } 165 166 /** Return the token type or char of the unexpected input element */ 167 public function get unexpectedType():int { 168 if ( input is TokenStream ) { 169 return token.type; 170 } 171 else if ( input is TreeNodeStream ) { 172 var nodes:TreeNodeStream = TreeNodeStream(input); 173 var adaptor:TreeAdaptor = nodes.treeAdaptor; 174 return adaptor.getType(node); 175 } 176 else { 177 return c; 178 } 179 180 } 181 } 182 183}