001: /* Copyright 2004-2006 David N. Welton
002:
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014: */
015:
016: package org.hecl;
017:
018: import java.util.Stack;
019: import java.util.Vector;
020:
021: /**
022: * The <code>HeclException</code> class implements exceptions for Hecl.
023: *
024: * @author <a href="mailto:davidw@dedasys.com">David N. Welton </a>
025: * @version 1.0
026: */
027:
028: public class HeclException extends Exception {
029: public String code = null;
030:
031: Stack stack;
032: Thing value = null;
033: Thing message = null;
034:
035: static final String BREAK = "BREAK";
036:
037: static final String CONTINUE = "CONT";
038:
039: static final String RETURN = "RETURN";
040:
041: static final String ERROR = "ERROR";
042:
043: /**
044: * Creates a new <code>HeclException</code> instance.
045: *
046: * @param s a <code>String</code> value
047: */
048:
049: public HeclException(String s) {
050: this (s, ERROR, null);
051: }
052:
053: public HeclException(String s, int lineno) {
054: this (s, ERROR, null);
055: try {
056: setLine(lineno);
057: } catch (HeclException ignore) {
058: }
059: }
060:
061: /**
062: * Creates a new <code>HeclException</code> instance.
063: *
064: * @param s a <code>String</code> value describing the error.
065: * @param exception_code an <code>int</code> value
066: */
067: public HeclException(String s, String exception_code) {
068: this (s, exception_code, null);
069: }
070:
071: HeclException(String s, String exception_code, Thing value) {
072: super (s);
073: this .message = new Thing(s);
074: this .value = value;
075: this .code = exception_code;
076: pushException(s);
077: }
078:
079: /**
080: * <code>pushException</code> adds to the exception stack.
081: */
082: private void pushException(String s) {
083: stack = new Stack();
084: Vector lst = new Vector();
085: lst.addElement(new Thing(code));
086: lst.addElement(message);
087: stack.push(new Thing(new ListThing(lst)));
088: }
089:
090: /**
091: * The <code>where</code> method tells the exception what command it
092: * occurred in.
093: *
094: * @param cmd
095: * a <code>String</code> containing the command name.
096: */
097: public void where(String cmd, int lineno) {
098: Vector lst = new Vector();
099: lst.addElement(new Thing(cmd));
100: lst.addElement(IntThing.create(lineno));
101: stack.push(new Thing(new ListThing(lst)));
102: }
103:
104: /**
105: * The <code>toString</code> method turns the exception stack into a
106: * string.
107: *
108: * @return a <code>String</code> value
109: */
110:
111: public String toString() {
112: return getStack().toString();
113: }
114:
115: /**
116: * The <code>getStack</code> method returns the exception as a Thing.
117: *
118: * @return a <code>Thing</code> value
119: */
120:
121: public Thing getStack() {
122: return ListThing.create((Vector) stack);
123: }
124:
125: public static HeclException createWrongNumArgsException(
126: Thing argv[], int count, String message)
127: throws HeclException {
128: StringBuffer str = new StringBuffer();
129: for (int i = 0; i < count && i < argv.length; i++) {
130: str.append(argv[i].toString());
131: str.append(" ");
132: }
133: return new HeclException("wrong # args: should be \"" + str
134: + message + "\"");
135: }
136:
137: /**
138: *
139: * @param param
140: * <code>Thing</code> specifying the actual parameter.
141: * @param type
142: * String saying the type - ie <i>option </i>, <i>command </i>.
143: * @param options
144: * A comma-separated list of options that can be supplied.
145: * @return a new HeclException
146: * @throws HeclException
147: */
148: public static HeclException createInvalidParameter(Thing param,
149: String type, String options) throws HeclException {
150: return new HeclException("invalid " + type + " specified \""
151: + param.toString() + "\"; should be: " + options + ".");
152: }
153:
154: /**
155: * The <code>setLine</code> method sets the line number of an
156: * error. FIXME - this could probably be done in a cleaner way...
157: *
158: * @param lineno an <code>int</code> value
159: * @exception HeclException if an error occurs
160: */
161: public void setLine(int lineno) throws HeclException {
162: Vector ex = (Vector) stack;
163: Vector err;
164: err = ListThing.get((Thing) ex.elementAt(0));
165: Thing l = IntThing.create(lineno);
166: if (err.size() == 2) {
167: err.addElement(l);
168: } else {
169: err.setElementAt(l, 2);
170: }
171: }
172: }
|