syntax_error.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package kawa.standard;
  2. import kawa.lang.*;
  3. import gnu.lists.*;
  4. import gnu.expr.*;
  5. import gnu.kawa.functions.DisplayFormat;
  6. import gnu.kawa.io.CharArrayOutPort;
  7. /** Implements the "syntax-error" form.
  8. * Prints out its arguments in an error message.
  9. * @author Per Bothner
  10. */
  11. public class syntax_error extends Syntax
  12. {
  13. public static final syntax_error syntax_error = new syntax_error();
  14. static { syntax_error.setName("syntax-error"); }
  15. public Expression rewrite (Object obj, Translator tr)
  16. {
  17. CharArrayOutPort buf = new CharArrayOutPort();
  18. int words = 0;
  19. DisplayFormat format = DisplayFormat.schemeDisplayFormat;
  20. while (obj instanceof Pair)
  21. {
  22. Pair pair = (Pair) obj;
  23. if (words > 0)
  24. buf.append(' ');
  25. format.format(Translator.stripSyntax(pair.getCar()), buf);
  26. format = DisplayFormat.schemeWriteFormat;
  27. obj = pair.getCdr();
  28. words++;
  29. }
  30. if (obj != LList.Empty)
  31. {
  32. if (words > 0)
  33. buf.append(' ');
  34. format.format(obj, buf);
  35. }
  36. return tr.syntaxError(buf.toString());
  37. }
  38. public static Expression error (Object form, Object[] message)
  39. {
  40. StringBuffer buffer = new StringBuffer();
  41. int len = message.length;
  42. if (message == null || len == 0)
  43. buffer.append("invalid syntax");
  44. else
  45. {
  46. for (int i = 0; i < len; i++)
  47. buffer.append(message[i]);
  48. }
  49. Translator tr = (Translator) Compilation.getCurrent();
  50. if (tr == null)
  51. throw new RuntimeException(buffer.toString());
  52. Object savePos = tr.pushPositionOf(form);
  53. try
  54. {
  55. return tr.syntaxError(buffer.toString());
  56. }
  57. finally
  58. {
  59. tr.popPositionOf(savePos);
  60. }
  61. }
  62. }