13447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/* 23447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein [The "BSD license"] 33447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Copyright (c) 2005-2009 Terence Parr 43447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein All rights reserved. 53447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 63447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Redistribution and use in source and binary forms, with or without 73447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein modification, are permitted provided that the following conditions 83447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein are met: 93447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1. Redistributions of source code must retain the above copyright 103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein notice, this list of conditions and the following disclaimer. 113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2. Redistributions in binary form must reproduce the above copyright 123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein notice, this list of conditions and the following disclaimer in the 133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein documentation and/or other materials provided with the distribution. 143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 3. The name of the author may not be used to endorse or promote products 153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein derived from this software without specific prior written permission. 163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpackage org.antlr.runtime; 293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.tree.*; 313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/** The root of the ANTLR exception hierarchy. 333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * To avoid English-only error messages and to generally make things 353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * as flexible as possible, these exceptions are not created with strings, 363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * but rather the information necessary to generate an error. Then 373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * the various reporting methods in Parser and Lexer can be overridden 383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * to generate a localized error message. For example, MismatchedToken 393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * exceptions are built with the expected token type. 403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * So, don't expect getMessage() to return anything. 413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * Note that as of Java 1.4, you can access the stack trace, which means 433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * that you can compute the complete trace of rules from the start symbol. 443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * This gives you considerable context information with which to generate 453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * useful error messages. 463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * ANTLR generates code that throws exceptions upon recognition error and 483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * also generates code to catch these exceptions in each rule. If you 493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * want to quit upon first error, you can turn off the automatic error 503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * handling mechanism using rulecatch action, but you still need to 513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * override methods mismatch and recoverFromMismatchSet. 523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * In general, the recognition exceptions can track where in a grammar a 543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * problem occurred and/or what was the expected input. While the parser 553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * knows its state (such as current input symbol and line info) that 563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * state can change before the exception is reported so current token index 573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * is computed and stored at exception time. From this info, you can 583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * perhaps print an entire line of input not just a single token, for example. 593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * Better to just say the recognizer had a problem and then let the parser 603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * figure out a fancy report. 613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic class RecognitionException extends Exception { 633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** What input stream did the error occur in? */ 643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public transient IntStream input; 653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** What is index of token/char were we looking at when the error occurred? */ 673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int index; 683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** The current Token when an error occurred. Since not all streams 703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * can retrieve the ith Token, we have to track the Token object. 713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * For parsers. Even when it's a tree parser, token might be set. 723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Token token; 743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** If this is a tree parser exception, node is set to the node with 763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * the problem. 773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object node; 793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** The current char when an error occurred. For lexers. */ 813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int c; 823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** Track the line at which the error occurred in case this is 843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * generated from a lexer. We need to track this since the 853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * unexpected char doesn't carry the line info. 863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int line; 883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int charPositionInLine; 903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** If you are parsing a tree node stream, you will encounter som 923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * imaginary nodes w/o line/col info. We now search backwards looking 933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * for most recent token with line/col info, but notify getErrorHeader() 943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * that info is approximate. 953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public boolean approximateLineInfo; 973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** Used for remote debugger deserialization */ 993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public RecognitionException() { 1003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public RecognitionException(IntStream input) { 1033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.input = input; 1043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.index = input.index(); 1053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( input instanceof TokenStream ) { 1063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.token = ((TokenStream)input).LT(1); 1073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.line = token.getLine(); 1083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.charPositionInLine = token.getCharPositionInLine(); 1093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( input instanceof TreeNodeStream ) { 1113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein extractInformationFromTreeNodeStream(input); 1123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else if ( input instanceof CharStream ) { 1143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.c = input.LA(1); 1153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.line = ((CharStream)input).getLine(); 1163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.charPositionInLine = ((CharStream)input).getCharPositionInLine(); 1173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else { 1193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.c = input.LA(1); 1203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein protected void extractInformationFromTreeNodeStream(IntStream input) { 1243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein TreeNodeStream nodes = (TreeNodeStream)input; 1253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.node = nodes.LT(1); 1263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein TreeAdaptor adaptor = nodes.getTreeAdaptor(); 1273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Token payload = adaptor.getToken(node); 1283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( payload!=null ) { 1293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.token = payload; 1303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( payload.getLine()<= 0 ) { 1313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // imaginary node; no line/pos info; scan backwards 1323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein int i = -1; 1333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Object priorNode = nodes.LT(i); 1343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein while ( priorNode!=null ) { 1353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Token priorPayload = adaptor.getToken(priorNode); 1363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( priorPayload!=null && priorPayload.getLine()>0 ) { 1373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // we found the most recent real line / pos info 1383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.line = priorPayload.getLine(); 1393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.charPositionInLine = priorPayload.getCharPositionInLine(); 1403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.approximateLineInfo = true; 1413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein break; 1423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein --i; 1443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein priorNode = nodes.LT(i); 1453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else { // node created from real token 1483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.line = payload.getLine(); 1493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.charPositionInLine = payload.getCharPositionInLine(); 1503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else if ( this.node instanceof Tree) { 1533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.line = ((Tree)this.node).getLine(); 1543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.charPositionInLine = ((Tree)this.node).getCharPositionInLine(); 1553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( this.node instanceof CommonTree) { 1563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.token = ((CommonTree)this.node).token; 1573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else { 1603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein int type = adaptor.getType(this.node); 1613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein String text = adaptor.getText(this.node); 1623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.token = new CommonToken(type, text); 1633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** Return the token type or char of the unexpected input element */ 1673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int getUnexpectedType() { 1683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( input instanceof TokenStream ) { 1693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return token.getType(); 1703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else if ( input instanceof TreeNodeStream ) { 1723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein TreeNodeStream nodes = (TreeNodeStream)input; 1733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein TreeAdaptor adaptor = nodes.getTreeAdaptor(); 1743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return adaptor.getType(node); 1753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else { 1773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return c; 1783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein} 181