KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > LispReader


1 /*
2  * LispReader.java
3  *
4  * Copyright (C) 2004 Peter Graves
5  * $Id: LispReader.java,v 1.24 2004/04/17 10:54:24 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.lisp;
23
24 public final class LispReader extends Lisp
25 {
26     // ### read-comment
27
public static final ReaderMacroFunction READ_COMMENT =
28         new ReaderMacroFunction("read-comment", PACKAGE_SYS, false,
29                                 "stream character")
30     {
31         public LispObject execute(Stream stream, char ignored)
32             throws ConditionThrowable
33         {
34             while (true) {
35                 int n = stream._readChar();
36                 if (n < 0)
37                     return null;
38                 if (n == '\n')
39                     return null;
40             }
41         }
42     };
43
44     // ### read-string
45
public static final ReaderMacroFunction READ_STRING =
46         new ReaderMacroFunction("read-string", PACKAGE_SYS, false,
47                                 "stream character")
48     {
49         public LispObject execute(Stream stream, char terminator)
50             throws ConditionThrowable
51         {
52             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
53             while (true) {
54                 int n = stream._readChar();
55                 if (n < 0) {
56                     signal(new EndOfFile(stream));
57                     // Not reached.
58
return null;
59                 }
60                 char c = (char) n;
61                 if (c == '\\') {
62                     // Single escape.
63
n = stream._readChar();
64                     if (n < 0) {
65                         signal(new EndOfFile(stream));
66                         // Not reached.
67
return null;
68                     }
69                     sb.append((char)n);
70                     continue;
71                 }
72                 if (c == terminator)
73                     break;
74                 // Default.
75
sb.append(c);
76             }
77             return new SimpleString(sb);
78         }
79     };
80
81     // ### read-list
82
public static final ReaderMacroFunction READ_LIST =
83         new ReaderMacroFunction("read-list", PACKAGE_SYS, false,
84                                 "stream character")
85     {
86         public LispObject execute(Stream stream, char ignored)
87             throws ConditionThrowable
88         {
89             return stream.readList();
90         }
91     };
92
93     // ### read-right-paren
94
public static final ReaderMacroFunction READ_RIGHT_PAREN =
95         new ReaderMacroFunction("read-right-paren", PACKAGE_SYS, false,
96                                 "stream character")
97     {
98         public LispObject execute(Stream stream, char ignored)
99             throws ConditionThrowable
100         {
101             return signal(new ReaderError("Unmatched right parenthesis."));
102         }
103     };
104
105     // ### read-quote
106
public static final ReaderMacroFunction READ_QUOTE =
107         new ReaderMacroFunction("read-quote", PACKAGE_SYS, false,
108                                 "stream character")
109     {
110         public LispObject execute(Stream stream, char ignored)
111             throws ConditionThrowable
112         {
113             return new Cons(Symbol.QUOTE,
114                             new Cons(stream.read(true, NIL, true)));
115         }
116     };
117
118     // ### read-dispatch-char
119
public static final ReaderMacroFunction READ_DISPATCH_CHAR =
120         new ReaderMacroFunction("read-dispatch-char", PACKAGE_SYS, false,
121                                 "stream character")
122     {
123         public LispObject execute(Stream stream, char c)
124             throws ConditionThrowable
125         {
126             return stream.readDispatchChar(c);
127         }
128     };
129
130     // ### backquote-macro
131
public static final ReaderMacroFunction BACKQUOTE_MACRO =
132         new ReaderMacroFunction("backquote-macro", PACKAGE_SYS, false,
133                                 "stream character")
134     {
135         public LispObject execute(Stream stream, char ignored)
136             throws ConditionThrowable
137         {
138             return new Cons(Symbol.BACKQUOTE,
139                             new Cons(stream.read(true, NIL, true)));
140         }
141     };
142
143     // ### comma-macro
144
public static final ReaderMacroFunction COMMA_MACRO =
145         new ReaderMacroFunction("comma-macro", PACKAGE_SYS, false,
146                                 "stream character")
147     {
148         public LispObject execute(Stream stream, char ignored)
149             throws ConditionThrowable
150         {
151             return stream.readComma();
152         }
153     };
154
155     // ### sharp-left-paren
156
public static final DispatchMacroFunction SHARP_LEFT_PAREN =
157         new DispatchMacroFunction("sharp-left-paren", PACKAGE_SYS, false,
158                                   "stream sub-char numarg")
159     {
160         public LispObject execute(Stream stream, char c, int n)
161             throws ConditionThrowable
162         {
163             return new SimpleVector(stream.readList());
164         }
165     };
166
167     // ### sharp-star
168
public static final DispatchMacroFunction SHARP_STAR =
169         new DispatchMacroFunction("sharp-star", PACKAGE_SYS, false,
170                                   "stream sub-char numarg")
171     {
172         public LispObject execute(Stream stream, char c, int n)
173             throws ConditionThrowable
174         {
175             return stream.readBitVector();
176         }
177     };
178
179     // ### sharp-dot
180
public static final DispatchMacroFunction SHARP_DOT =
181         new DispatchMacroFunction("sharp-dot", PACKAGE_SYS, false,
182                                   "stream sub-char numarg")
183     {
184         public LispObject execute(Stream stream, char c, int n)
185             throws ConditionThrowable
186         {
187             if (_READ_EVAL_.symbolValueNoThrow() == NIL)
188                 return signal(new ReaderError("Can't read #. when *READ-EVAL* is NIL."));
189             else
190                 return eval(stream.read(true, NIL, true),
191                             new Environment(),
192                             LispThread.currentThread());
193         }
194     };
195
196     // ### sharp-colon
197
public static final DispatchMacroFunction SHARP_COLON =
198         new DispatchMacroFunction("sharp-colon", PACKAGE_SYS, false,
199                                   "stream sub-char numarg")
200     {
201         public LispObject execute(Stream stream, char c, int n)
202             throws ConditionThrowable
203         {
204             return stream.readSymbol();
205         }
206     };
207
208     // ### sharp-a
209
public static final DispatchMacroFunction SHARP_A =
210         new DispatchMacroFunction("sharp-a", PACKAGE_SYS, false,
211                                   "stream sub-char numarg")
212     {
213         public LispObject execute(Stream stream, char c, int n)
214             throws ConditionThrowable
215         {
216             return stream.readArray(n);
217         }
218     };
219
220     // ### sharp-b
221
public static final DispatchMacroFunction SHARP_B =
222         new DispatchMacroFunction("sharp-b", PACKAGE_SYS, false,
223                                   "stream sub-char numarg")
224     {
225         public LispObject execute(Stream stream, char c, int n)
226             throws ConditionThrowable
227         {
228             return stream.readRadix(2);
229         }
230     };
231
232     // ### sharp-c
233
public static final DispatchMacroFunction SHARP_C =
234         new DispatchMacroFunction("sharp-c", PACKAGE_SYS, false,
235                                   "stream sub-char numarg")
236     {
237         public LispObject execute(Stream stream, char c, int n)
238             throws ConditionThrowable
239         {
240             return stream.readComplex();
241         }
242     };
243
244     // ### sharp-o
245
public static final DispatchMacroFunction SHARP_O =
246         new DispatchMacroFunction("sharp-o", PACKAGE_SYS, false,
247                                   "stream sub-char numarg")
248     {
249         public LispObject execute(Stream stream, char c, int n)
250             throws ConditionThrowable
251         {
252             return stream.readRadix(8);
253         }
254     };
255
256     // ### sharp-p
257
public static final DispatchMacroFunction SHARP_P =
258         new DispatchMacroFunction("sharp-p", PACKAGE_SYS, false,
259                                   "stream sub-char numarg")
260     {
261         public LispObject execute(Stream stream, char c, int n)
262             throws ConditionThrowable
263         {
264             return stream.readPathname();
265         }
266     };
267
268     // ### sharp-r
269
public static final DispatchMacroFunction SHARP_R =
270         new DispatchMacroFunction("sharp-r", PACKAGE_SYS, false,
271                                   "stream sub-char numarg")
272     {
273         public LispObject execute(Stream stream, char c, int n)
274             throws ConditionThrowable
275         {
276             return stream.readRadix(n);
277         }
278     };
279
280     // ### sharp-s
281
public static final DispatchMacroFunction SHARP_S =
282         new DispatchMacroFunction("sharp-s", PACKAGE_SYS, false,
283                                   "stream sub-char numarg")
284     {
285         public LispObject execute(Stream stream, char c, int n)
286             throws ConditionThrowable
287         {
288             return stream.readStructure();
289         }
290     };
291
292     // ### sharp-x
293
public static final DispatchMacroFunction SHARP_X =
294         new DispatchMacroFunction("sharp-x", PACKAGE_SYS, false,
295                                   "stream sub-char numarg")
296     {
297         public LispObject execute(Stream stream, char c, int n)
298             throws ConditionThrowable
299         {
300             return stream.readRadix(16);
301         }
302     };
303
304     // ### sharp-quote
305
public static final DispatchMacroFunction SHARP_QUOTE =
306         new DispatchMacroFunction("sharp-quote", PACKAGE_SYS, false,
307                                   "stream sub-char numarg")
308     {
309         public LispObject execute(Stream stream, char c, int n)
310             throws ConditionThrowable
311         {
312             return new Cons(Symbol.FUNCTION,
313                             new Cons(stream.read(true, NIL, true)));
314         }
315     };
316
317     // ### sharp-backslash
318
public static final DispatchMacroFunction SHARP_BACKSLASH =
319         new DispatchMacroFunction("sharp-backslash", PACKAGE_SYS, false,
320                                   "stream sub-char numarg")
321     {
322         public LispObject execute(Stream stream, char c, int n)
323             throws ConditionThrowable
324         {
325             return stream.readCharacterLiteral();
326         }
327     };
328
329     // ### sharp-vertical-bar
330
public static final DispatchMacroFunction SHARP_VERTICAL_BAR =
331         new DispatchMacroFunction("sharp-vertical-bar", PACKAGE_SYS, false,
332                                   "stream sub-char numarg")
333     {
334         public LispObject execute(Stream stream, char c, int n)
335             throws ConditionThrowable
336         {
337             stream.skipBalancedComment();
338             return null;
339         }
340     };
341
342     // ### sharp-illegal
343
public static final DispatchMacroFunction SHARP_ILLEGAL =
344         new DispatchMacroFunction("sharp-illegal", PACKAGE_SYS, false,
345                                   "stream sub-char numarg")
346     {
347         public LispObject execute(Stream stream, char c, int n)
348             throws ConditionThrowable
349         {
350             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Illegal # macro character: #\\");
351             String JavaDoc s = LispCharacter.charToName(c);
352             if (s != null)
353                 sb.append(s);
354             else
355                 sb.append(c);
356             return signal(new ReaderError(sb.toString()));
357         }
358     };
359 }
360
Popular Tags