01: // Copyright (c) 2001 Per M.A. Bothner
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.kawa.lispexpr;
05:
06: import gnu.mapping.*;
07: import gnu.text.*;
08:
09: /** Wrapper for user-supplied reader macro procedure. */
10:
11: public class ReaderMacro extends ReaderMisc {
12: Procedure procedure;
13:
14: public ReaderMacro(Procedure procedure, boolean nonTerminating) {
15: super (nonTerminating ? ReadTable.NON_TERMINATING_MACRO
16: : ReadTable.TERMINATING_MACRO);
17: this .procedure = procedure;
18: }
19:
20: public ReaderMacro(Procedure procedure) {
21: super (ReadTable.TERMINATING_MACRO);
22: this .procedure = procedure;
23: }
24:
25: public boolean isNonTerminating() {
26: return kind == ReadTable.NON_TERMINATING_MACRO;
27: }
28:
29: public Procedure getProcedure() {
30: return procedure;
31: }
32:
33: public Object read(Lexer in, int ch, int count)
34: throws java.io.IOException, SyntaxException {
35: // java.io.Reader reader = in;
36: java.io.Reader reader = in.getPort();
37: try {
38: return procedure.apply2(reader, Char.make(ch));
39: } catch (java.io.IOException ex) {
40: throw ex;
41: } catch (gnu.text.SyntaxException ex) {
42: throw ex;
43: } catch (Throwable ex) {
44: in.fatal("reader macro '" + procedure + "' threw: " + ex);
45: return null; // Never executed.
46: }
47: }
48: }
|