01: package kawa.standard;
02:
03: import kawa.lang.*;
04: import gnu.lists.*;
05: import gnu.expr.*;
06:
07: /** Implements the Kawa extension "%syntax-error".
08: * Prints out its arguments in an error message.
09: * @author Per Bothner
10: */
11:
12: public class syntax_error extends Syntax {
13: public static final syntax_error syntax_error = new syntax_error();
14: static {
15: syntax_error.setName("%syntax-error");
16: }
17:
18: public Expression rewrite(Object obj, Translator tr) {
19: StringBuffer buffer = new StringBuffer();
20: int words = 0;
21: while (obj instanceof Pair) {
22: Pair pair = (Pair) obj;
23: if (words > 0)
24: buffer.append(' ');
25: buffer.append(pair.car);
26: obj = pair.cdr;
27: words++;
28: }
29: if (obj != LList.Empty) {
30: if (words > 0)
31: buffer.append(' ');
32: buffer.append(obj);
33: }
34: return tr.syntaxError(buffer.toString());
35: }
36:
37: public static Expression error(Object form, Object[] message) {
38: StringBuffer buffer = new StringBuffer();
39: int len = message.length;
40: if (message == null || len == 0)
41: buffer.append("invalid syntax");
42: else {
43: for (int i = 0; i < len; i++)
44: buffer.append(message[i]);
45: }
46: Translator tr = (Translator) Compilation.getCurrent();
47: if (tr == null)
48: throw new RuntimeException(buffer.toString());
49: Object savePos = tr.pushPositionOf(form);
50: try {
51: return tr.syntaxError(buffer.toString());
52: } finally {
53: tr.popPositionOf(savePos);
54: }
55: }
56: }
|