001: /*
002: [The "BSD licence"]
003: Copyright (c) 2005-2006 Terence Parr
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in the
013: documentation and/or other materials provided with the distribution.
014: 3. The name of the author may not be used to endorse or promote products
015: derived from this software without specific prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package org.antlr.tool;
029:
030: import org.antlr.stringtemplate.StringTemplate;
031:
032: /** The ANTLR code calls methods on ErrorManager to report errors etc...
033: * Rather than simply pass these arguments to the ANTLRErrorListener directly,
034: * create an object that encapsulates everything. In this way, the error
035: * listener interface does not have to change when I add a new kind of
036: * error message. I don't want to break a GUI for example every time
037: * I update the error system in ANTLR itself.
038: *
039: * To get a printable error/warning message, call toString().
040: */
041: public abstract class Message {
042: // msgST is the actual text of the message
043: public StringTemplate msgST;
044: // these are for supporting different output formats
045: public StringTemplate locationST;
046: public StringTemplate reportST;
047: public StringTemplate messageFormatST;
048:
049: public int msgID;
050: public Object arg;
051: public Object arg2;
052: public Throwable e;
053: // used for location template
054: public String file;
055: public int line = -1;
056: public int column = -1;
057:
058: public Message() {
059: }
060:
061: public Message(int msgID) {
062: this (msgID, null, null);
063: }
064:
065: public Message(int msgID, Object arg, Object arg2) {
066: setMessageID(msgID);
067: this .arg = arg;
068: this .arg2 = arg2;
069: }
070:
071: public void setLine(int line) {
072: this .line = line;
073: }
074:
075: public void setColumn(int column) {
076: this .column = column;
077: }
078:
079: public void setMessageID(int msgID) {
080: this .msgID = msgID;
081: msgST = ErrorManager.getMessage(msgID);
082: }
083:
084: /** Return a new template instance every time someone tries to print
085: * a Message.
086: */
087: public StringTemplate getMessageTemplate() {
088: return msgST.getInstanceOf();
089: }
090:
091: /** Return a new template instance for the location part of a Message.
092: * TODO: Is this really necessary? -Kay
093: */
094: public StringTemplate getLocationTemplate() {
095: return locationST.getInstanceOf();
096: }
097:
098: public String toString(StringTemplate messageST) {
099: // setup the location
100: locationST = ErrorManager.getLocationFormat();
101: reportST = ErrorManager.getReportFormat();
102: messageFormatST = ErrorManager.getMessageFormat();
103: boolean locationValid = false;
104: if (line != -1) {
105: locationST.setAttribute("line", line);
106: locationValid = true;
107: }
108: if (column != -1) {
109: locationST.setAttribute("column", column);
110: locationValid = true;
111: }
112: if (file != null) {
113: locationST.setAttribute("file", file);
114: locationValid = true;
115: }
116:
117: messageFormatST.setAttribute("id", msgID);
118: messageFormatST.setAttribute("text", messageST);
119:
120: if (locationValid) {
121: reportST.setAttribute("location", locationST);
122: }
123: reportST.setAttribute("message", messageFormatST);
124: reportST.setAttribute("type", ErrorManager
125: .getMessageType(msgID));
126:
127: return reportST.toString();
128: }
129: }
|