KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > lispexpr > ReaderDispatch


1 // Copyright (c) 2001 Per M.A. Bothner
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.kawa.lispexpr;
5 import gnu.kawa.util.RangeTable;
6 import gnu.text.*;
7 import gnu.mapping.Values;
8
9 public class ReaderDispatch extends ReadTableEntry
10 {
11   RangeTable table = new RangeTable();
12   int kind;
13
14   public int getKind()
15   {
16     return kind;
17   }
18
19   public void set(int key, Object JavaDoc value)
20   {
21     table.set(key, key, value);
22   }
23
24   public ReadTableEntry lookup(int key)
25   {
26     return (ReadTableEntry) table.lookup(key, null);
27   }
28
29   public ReaderDispatch()
30   {
31     kind = ReadTable.TERMINATING_MACRO;
32   }
33
34   public ReaderDispatch(boolean nonTerminating)
35   {
36     this.kind = nonTerminating ? ReadTable.NON_TERMINATING_MACRO
37       : ReadTable.TERMINATING_MACRO;
38   }
39
40   /** Create a fresh instance and initialize it appropriately for Common Lisp.
41    */

42   public static ReaderDispatch create()
43   {
44     ReaderDispatch tab = new ReaderDispatch();
45     ReaderDispatchMisc entry = ReaderDispatchMisc.getInstance();
46     tab.set(':', entry);
47     tab.set('B', entry);
48     tab.set('D', entry);
49     tab.set('E', entry);
50     tab.set('F', entry);
51     tab.set('I', entry);
52     tab.set('O', entry);
53     tab.set('R', entry);
54     tab.set('S', entry);
55     tab.set('T', entry);
56     tab.set('U', entry);
57     tab.set('X', entry);
58     tab.set('|', entry);
59     tab.set('!', entry);
60     tab.set('\\', entry);
61     tab.set('\'', new ReaderQuote("function"));
62     tab.set('(', new ReaderVector(')'));
63     return tab;
64   }
65
66   public Object JavaDoc read (Lexer in, int ch, int count)
67     throws java.io.IOException JavaDoc, SyntaxException
68   {
69     count = -1;
70     for (;;)
71       {
72     ch = in.read();
73     if (ch < 0)
74       in.eofError("unexpected EOF after "+ (char) ch);
75     if (ch > 0x10000)
76       break;
77     int digit = Character.digit((char) ch, 10);
78     if (digit < 0)
79       {
80         ch = Character.toUpperCase((char) ch);
81         break;
82       }
83     count = count < 0 ? digit : count * 10 + digit;
84       }
85     ReadTableEntry entry = (ReadTableEntry) table.lookup(ch, null);
86     if (entry == null)
87       {
88     // Effectively subtracting 1 from the column number.
89
in.error('e', in.getName(),
90                  in.getLineNumber() + 1, in.getColumnNumber(),
91                  "invalid dispatch character '"+((char) ch)+'\'');
92     return Values.empty;
93       }
94     return entry.read(in, ch, count);
95   }
96 }
97
Popular Tags