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.kawa.util.RangeTable;
07: import gnu.text.*;
08: import gnu.mapping.Values;
09:
10: public class ReaderDispatch extends ReadTableEntry {
11: RangeTable table = new RangeTable();
12: int kind;
13:
14: public int getKind() {
15: return kind;
16: }
17:
18: public void set(int key, Object value) {
19: table.set(key, key, value);
20: }
21:
22: public ReadTableEntry lookup(int key) {
23: return (ReadTableEntry) table.lookup(key, null);
24: }
25:
26: public ReaderDispatch() {
27: kind = ReadTable.TERMINATING_MACRO;
28: }
29:
30: public ReaderDispatch(boolean nonTerminating) {
31: this .kind = nonTerminating ? ReadTable.NON_TERMINATING_MACRO
32: : ReadTable.TERMINATING_MACRO;
33: }
34:
35: /** Create a fresh instance and initialize it appropriately for Common Lisp.
36: */
37: public static ReaderDispatch create() {
38: ReaderDispatch tab = new ReaderDispatch();
39: ReaderDispatchMisc entry = ReaderDispatchMisc.getInstance();
40: tab.set(':', entry);
41: tab.set('B', entry);
42: tab.set('D', entry);
43: tab.set('E', entry);
44: tab.set('F', entry);
45: tab.set('I', entry);
46: tab.set('O', entry);
47: tab.set('R', entry);
48: tab.set('S', entry);
49: tab.set('T', entry);
50: tab.set('U', entry);
51: tab.set('X', entry);
52: tab.set('|', entry);
53: tab.set('!', entry);
54: tab.set('\\', entry);
55: tab.set('\'', new ReaderQuote("function"));
56: tab.set('(', new ReaderVector(')'));
57: return tab;
58: }
59:
60: public Object read(Lexer in, int ch, int count)
61: throws java.io.IOException, SyntaxException {
62: count = -1;
63: for (;;) {
64: ch = in.read();
65: if (ch < 0)
66: in.eofError("unexpected EOF after " + (char) ch);
67: if (ch > 0x10000)
68: break;
69: int digit = Character.digit((char) ch, 10);
70: if (digit < 0) {
71: ch = Character.toUpperCase((char) ch);
72: break;
73: }
74: count = count < 0 ? digit : count * 10 + digit;
75: }
76: ReadTableEntry entry = (ReadTableEntry) table.lookup(ch, null);
77: if (entry == null) {
78: // Effectively subtracting 1 from the column number.
79: in.error('e', in.getName(), in.getLineNumber() + 1, in
80: .getColumnNumber(), "invalid dispatch character '"
81: + ((char) ch) + '\'');
82: return Values.empty;
83: }
84: return entry.read(in, ch, count);
85: }
86: }
|