Home | History | Annotate | Download | only in tool
      1 /*
      2  * [The "BSD license"]
      3  *  Copyright (c) 2010 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.tool;
     29 
     30 import org.stringtemplate.v4.ST;
     31 
     32 /** The ANTLR code calls methods on ErrorManager to report errors etc...
     33  *  Rather than simply pass these arguments to the ANTLRErrorListener directly,
     34  *  create an object that encapsulates everything.  In this way, the error
     35  *  listener interface does not have to change when I add a new kind of
     36  *  error message.  I don't want to break a GUI for example every time
     37  *  I update the error system in ANTLR itself.
     38  *
     39  *  To get a printable error/warning message, call toString().
     40  */
     41 public abstract class Message {
     42 	// msgST is the actual text of the message
     43 	public ST msgST;
     44 	// these are for supporting different output formats
     45 	public ST locationST;
     46 	public ST reportST;
     47 	public ST messageFormatST;
     48 
     49 	public int msgID;
     50 	public Object arg;
     51 	public Object arg2;
     52 	public Throwable e;
     53 	// used for location template
     54 	public String file;
     55 	public int line = -1;
     56 	public int column = -1;
     57 
     58 	public Message() {
     59 	}
     60 
     61 	public Message(int msgID) {
     62 		this(msgID, null, null);
     63 	}
     64 
     65 	public Message(int msgID, Object arg, Object arg2) {
     66 		setMessageID(msgID);
     67 		this.arg = arg;
     68 		this.arg2 = arg2;
     69 	}
     70 
     71 	public void setLine(int line) {
     72 		this.line = line;
     73 	}
     74 
     75 	public void setColumn(int column) {
     76 		this.column = column;
     77 	}
     78 
     79 	public void setMessageID(int msgID) {
     80 		this.msgID = msgID;
     81 		msgST = ErrorManager.getMessage(msgID);
     82 	}
     83 
     84 	/** Return a new template instance every time someone tries to print
     85 	 *  a Message.
     86 	 */
     87 	public ST getMessageTemplate() { return new ST(msgST); }
     88 
     89 	/** Return a new template instance for the location part of a Message.
     90 	 *  TODO: Is this really necessary? -Kay
     91 	 */
     92 	public ST getLocationTemplate() { return new ST(locationST); }
     93 
     94 	public String toString(ST messageST) {
     95 		// setup the location
     96 		locationST = ErrorManager.getLocationFormat();
     97 		reportST = ErrorManager.getReportFormat();
     98 		messageFormatST = ErrorManager.getMessageFormat();
     99 		boolean locationValid = false;
    100 		if (line != -1) {
    101 			locationST.add("line", line);
    102 			locationValid = true;
    103 		}
    104 		if (column != -1) {
    105 			locationST.add("column", column+1);
    106 			locationValid = true;
    107 		}
    108 		if (file != null) {
    109 			locationST.add("file", file);
    110 			locationValid = true;
    111 		}
    112 
    113 		messageFormatST.add("id", msgID);
    114 		messageFormatST.add("text", messageST);
    115 
    116 		if (locationValid) {
    117 			reportST.add("location", locationST);
    118 		}
    119 		reportST.add("message", messageFormatST);
    120 		reportST.add("type", ErrorManager.getMessageType(msgID));
    121 
    122 		return reportST.render();
    123 	}
    124 }
    125