1 4 package gnu.kawa.lispexpr; 5 import gnu.kawa.util.RangeTable; 6 import gnu.mapping.*; 7 import gnu.expr.Language; 8 import gnu.kawa.reflect.StaticFieldLocation; 9 10 public class ReadTable extends RangeTable 11 { 12 13 public static final int ILLEGAL = 0; 14 public static final int WHITESPACE = 1; 15 public static final int CONSTITUENT = 2; 16 public static final int SINGLE_ESCAPE = 3; 17 public static final int MULTIPLE_ESCAPE = 4; 18 public static final int TERMINATING_MACRO = 5; 19 public static final int NON_TERMINATING_MACRO = 6; 20 21 22 public static int defaultBracketMode = -1; 23 24 25 public char postfixLookupOperator = (char) (-1); 26 27 static final ThreadLocation current = new ThreadLocation("read-table"); 28 29 public ReadTable() 30 { 31 } 32 33 public void initialize () 34 { 35 ReadTableEntry entry; 36 entry = ReadTableEntry.getWhitespaceInstance(); 37 set(' ', entry); 38 set('\t', entry); 39 set('\n', entry); 40 set('\r', entry); 41 set('\f', entry); 42 set('|', ReadTableEntry.getMultipleEscapeInstance()); 44 set('\\', ReadTableEntry.getSingleEscapeInstance()); 45 set('0', '9', ReadTableEntry.getDigitInstance()); 46 entry = ReadTableEntry.getConstituentInstance(); 47 set('a', 'z', entry); 48 set('A', 'Z', entry); 49 set('!', entry); 50 set('$', entry); 51 set('%', entry); 52 set('&', entry); 53 set('*', entry); 54 set('+', entry); 55 set('-', entry); 56 set('.', entry); 57 set('/', entry); 58 set(':', entry); 59 set('=', entry); 60 set('>', entry); 61 set('?', entry); 62 set('@', entry); 63 set('^', entry); 64 set('_', entry); 65 set('{', entry); 66 set('}', entry); 67 set('~', entry); 68 set('\177',entry); 69 set('\b', entry); 70 set('\"', new ReaderString()); 71 set('#', ReaderDispatch.create()); 72 set(';', ReaderIgnoreRestOfLine.getInstance()); 73 set('(', ReaderParens.getInstance('(', ')')); 74 75 set('\'', new ReaderQuote(LispLanguage.quote_sym)); 76 set('`', new ReaderQuote(LispLanguage.quasiquote_sym)); 77 set(',', new ReaderQuote(LispLanguage.unquote_sym, 78 '@', LispLanguage.unquotesplicing_sym)); 79 80 setBracketMode(); 82 } 83 84 85 public static ReadTable createInitial () 86 { 87 ReadTable tab = new ReadTable(); 88 tab.initialize(); 89 return tab; 90 } 91 92 99 public void setBracketMode(int mode) 100 { 101 if (mode <= 0) 102 { 103 ReadTableEntry token = ReadTableEntry.getConstituentInstance(); 104 set('<', token); 105 if (mode < 0) 106 { 107 set('[', token); 108 set(']', token); 109 } 110 } 111 else 112 set('<', new ReaderTypespec()); 113 if (mode >= 0) 114 { 115 set('[', ReaderParens.getInstance('[', ']')); 116 remove(']'); 117 } 118 } 119 120 122 public void setBracketMode() 123 { 124 setBracketMode(defaultBracketMode); 125 } 126 127 128 Environment ctorTable = null; 129 130 void initCtorTable () 131 { 132 if (ctorTable == null) 133 ctorTable = Environment.make(); 134 } 135 136 137 public synchronized void putReaderCtor (String key, Procedure proc) 138 { 139 initCtorTable(); 140 ctorTable.put(key, proc); 141 } 142 143 144 public synchronized void putReaderCtorFld (String key, 145 String cname, String fname) 146 { 147 initCtorTable(); 148 Symbol symbol = ctorTable.getSymbol(key); 149 StaticFieldLocation.define(ctorTable, symbol, null, cname, fname); 150 } 151 152 153 public synchronized Procedure getReaderCtor (String key) 154 { 155 initCtorTable(); 156 return (Procedure) ctorTable.get(key, null); 157 } 158 159 public static ReadTable getCurrent() 160 { 161 ReadTable table = (ReadTable) current.get(null); 162 if (table == null) 163 { 164 Language language = Language.getDefaultLanguage(); 165 if (language instanceof LispLanguage) 166 table = ((LispLanguage) language).defaultReadTable; 167 else 168 table = ReadTable.createInitial(); 169 current.set(table); 170 } 171 return table; 172 } 173 174 public static void setCurrent(ReadTable rt) 175 { 176 current.set(rt); 177 } 178 179 public ReadTableEntry lookup (int ch) 180 { 181 ReadTableEntry entry = (ReadTableEntry) lookup(ch, null); 182 if (entry == null && ch >= 0 && ch < 0x10000) 183 { 184 if (Character.isDigit((char) ch)) 185 entry = (ReadTableEntry) lookup('0', null); 186 else if (Character.isLowerCase((char) ch)) 187 entry = (ReadTableEntry) lookup('a', null); 188 else if (Character.isLetter((char) ch)) 189 entry = (ReadTableEntry) lookup('A', null); 190 else if (Character.isWhitespace((char) ch)) 191 entry = (ReadTableEntry) lookup(' ', null); 192 if (entry == null && ch >= 128) 194 entry = ReadTableEntry.getConstituentInstance(); 195 } 196 return entry; 197 } 198 199 protected Object makeSymbol (String name) 200 { 201 return name.intern(); 202 } 203 } 204 | Popular Tags |