source: branches/0.17.x/abcl/src/org/armedbear/lisp/FaslReader.java

Last change on this file was 12254, checked in by ehuelsmann, 16 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.1 KB
Line 
1/*
2 * FaslReader.java
3 *
4 * Copyright (C) 2005 Peter Graves
5 * $Id: FaslReader.java 12254 2009-11-06 20:07:54Z ehuelsmann $
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 * As a special exception, the copyright holders of this library give you
22 * permission to link this library with independent modules to produce an
23 * executable, regardless of the license terms of these independent
24 * modules, and to copy and distribute the resulting executable under
25 * terms of your choice, provided that you also meet, for each linked
26 * independent module, the terms and conditions of the license of that
27 * module.  An independent module is a module which is not derived from
28 * or based on this library.  If you modify this library, you may extend
29 * this exception to your version of the library, but you are not
30 * obligated to do so.  If you do not wish to do so, delete this
31 * exception statement from your version.
32 */
33
34package org.armedbear.lisp;
35
36public final class FaslReader extends Lisp
37{
38    // ### fasl-read-comment
39    public static final ReaderMacroFunction FASL_READ_COMMENT =
40        new ReaderMacroFunction("fasl-read-comment", PACKAGE_SYS, false,
41                                "stream character")
42    {
43        @Override
44        public LispObject execute(Stream stream, char ignored)
45
46        {
47          try 
48            {
49              while (true) {
50                int n = stream._readChar();
51                if (n < 0)
52                  return null;
53                if (n == '\n')
54                  return null;
55              }
56            }
57          catch (java.io.IOException e)
58            {
59              return null;
60            }
61        }
62    };
63
64    // ### fasl-read-string
65    public static final ReaderMacroFunction FASL_READ_STRING =
66        new ReaderMacroFunction("fasl-read-string", PACKAGE_SYS, false,
67                                "stream character")
68    {
69        @Override
70        public LispObject execute(Stream stream, char terminator)
71
72        {
73            final Readtable rt = FaslReadtable.getInstance();
74            FastStringBuffer sb = new FastStringBuffer();
75            try 
76              {
77                while (true) {
78                  int n = stream._readChar();
79                  if (n < 0) {
80                    error(new EndOfFile(stream));
81                    // Not reached.
82                    return null;
83                  }
84                  char c = (char) n;
85                  if (rt.getSyntaxType(c) == Readtable.SYNTAX_TYPE_SINGLE_ESCAPE) {
86                    // Single escape.
87                    n = stream._readChar();
88                    if (n < 0) {
89                      error(new EndOfFile(stream));
90                      // Not reached.
91                      return null;
92                    }
93                    sb.append((char)n);
94                    continue;
95                  }
96                  if (Utilities.isPlatformWindows) {
97                    if (c == '\r') {
98                      n = stream._readChar();
99                      if (n < 0) {
100                        error(new EndOfFile(stream));
101                        // Not reached.
102                        return null;
103                      }
104                      if (n == '\n') {
105                        sb.append('\n');
106                      } else {
107                        // '\r' was not followed by '\n'.
108                        stream._unreadChar(n);
109                        sb.append('\r');
110                      }
111                      continue;
112                    }
113                  }
114                  if (c == terminator)
115                    break;
116                  // Default.
117                  sb.append(c);
118                }
119                return new SimpleString(sb);
120              }
121            catch (java.io.IOException e)
122              {
123                return new SimpleString(sb);
124    //                return null;
125              }
126        }
127    };
128
129    // ### fasl-read-list
130    public static final ReaderMacroFunction FASL_READ_LIST =
131        new ReaderMacroFunction("fasl-read-list", PACKAGE_SYS, false,
132                                "stream character")
133    {
134        @Override
135        public LispObject execute(Stream stream, char ignored)
136
137        {
138            return stream.readList(false, true);
139        }
140    };
141
142    // ### fasl-read-right-paren
143    public static final ReaderMacroFunction FASL_READ_RIGHT_PAREN =
144        new ReaderMacroFunction("fasl-read-right-paren", PACKAGE_SYS, false,
145                                "stream character")
146    {
147        @Override
148        public LispObject execute(Stream stream, char ignored)
149
150        {
151            return error(new ReaderError("Unmatched right parenthesis.", stream));
152        }
153    };
154
155    // ### fasl-read-quote
156    public static final ReaderMacroFunction FASL_READ_QUOTE =
157        new ReaderMacroFunction("fasl-read-quote", PACKAGE_SYS, false,
158                                "stream character")
159    {
160        @Override
161        public LispObject execute(Stream stream, char ignored)
162
163        {
164            return new Cons(Symbol.QUOTE,
165                            new Cons(stream.faslRead(true, NIL, true,
166                                                     LispThread.currentThread())));
167        }
168    };
169
170    // ### fasl-read-dispatch-char
171    public static final ReaderMacroFunction FASL_READ_DISPATCH_CHAR =
172        new ReaderMacroFunction("fasl-read-dispatch-char", PACKAGE_SYS, false,
173                                "stream character")
174    {
175        @Override
176        public LispObject execute(Stream stream, char c)
177
178        {
179            return stream.readDispatchChar(c, true);
180        }
181    };
182
183    // ### fasl-sharp-left-paren
184    public static final DispatchMacroFunction FASL_SHARP_LEFT_PAREN =
185        new DispatchMacroFunction("fasl-sharp-left-paren", PACKAGE_SYS, false,
186                                  "stream sub-char numarg")
187    {
188        @Override
189        public LispObject execute(Stream stream, char c, int n)
190
191        {
192            final LispThread thread = LispThread.currentThread();
193            LispObject list = stream.readList(true, true);
194            if (_BACKQUOTE_COUNT_.symbolValue(thread).zerop()) {
195                if (n >= 0) {
196                    LispObject[] array = new LispObject[n];
197                    for (int i = 0; i < n; i++) {
198                        array[i] = list.car();
199                        if (list.cdr() != NIL)
200                            list = list.cdr();
201                    }
202                    return new SimpleVector(array);
203                } else
204                    return new SimpleVector(list);
205            }
206            return new Cons(_BQ_VECTOR_FLAG_.symbolValue(thread), list);
207        }
208    };
209
210    // ### fasl-sharp-star
211    public static final DispatchMacroFunction FASL_SHARP_STAR =
212        new DispatchMacroFunction("fasl-sharp-star", PACKAGE_SYS, false,
213                                  "stream sub-char numarg")
214    {
215        @Override
216        public LispObject execute(Stream stream, char ignored, int n)
217
218        {
219            final LispThread thread = LispThread.currentThread();
220            final Readtable rt = FaslReadtable.getInstance();
221            final boolean suppress =
222                (Symbol.READ_SUPPRESS.symbolValue(thread) != NIL);
223            FastStringBuffer sb = new FastStringBuffer();
224      try 
225        {
226    while (true) {
227      int ch = stream._readChar();
228      if (ch < 0)
229                    break;
230      char c = (char) ch;
231      if (c == '0' || c == '1')
232                    sb.append(c);
233      else {
234                    int syntaxType = rt.getSyntaxType(c);
235                    if (syntaxType == Readtable.SYNTAX_TYPE_WHITESPACE ||
236                        syntaxType == Readtable.SYNTAX_TYPE_TERMINATING_MACRO) {
237          stream._unreadChar(c);
238          break;
239                    } else if (!suppress) {
240          String name = LispCharacter.charToName(c);
241          if (name == null)
242      name = "#\\" + c;
243          error(new ReaderError("Illegal element for bit-vector: " + name,
244              stream));
245                    }
246      }
247    }
248        }
249      catch (java.io.IOException e)
250        {
251    error(new ReaderError("IO error: ",
252              stream));
253    return NIL;
254        }
255
256            if (suppress)
257                return NIL;
258            if (n >= 0) {
259                // n was supplied.
260                final int length = sb.length();
261                if (length == 0) {
262                    if (n > 0)
263                        return error(new ReaderError("No element specified for bit vector of length " +
264                                                      n + '.',
265                                                      stream));
266                }
267                if (n > length) {
268                    final char c = sb.charAt(length - 1);
269                    for (int i = length; i < n; i++)
270                        sb.append(c);
271                } else if (n < length) {
272                    return error(new ReaderError("Bit vector is longer than specified length: #" +
273                                                  n + '*' + sb.toString(),
274                                                  stream));
275                }
276            }
277            return new SimpleBitVector(sb.toString());
278        }
279    };
280
281    // ### fasl-sharp-dot
282    public static final DispatchMacroFunction FASL_SHARP_DOT =
283        new DispatchMacroFunction("fasl-sharp-dot", PACKAGE_SYS, false,
284                                  "stream sub-char numarg")
285    {
286        @Override
287        public LispObject execute(Stream stream, char c, int n)
288
289        {
290            final LispThread thread = LispThread.currentThread();
291            if (Symbol.READ_EVAL.symbolValue(thread) == NIL)
292                return error(new ReaderError("Can't read #. when *READ-EVAL* is NIL.",
293                                              stream));
294            else
295                return eval(stream.faslRead(true, NIL, true, thread),
296                            new Environment(), thread);
297        }
298    };
299
300    // ### fasl-sharp-colon
301    public static final DispatchMacroFunction FASL_SHARP_COLON =
302        new DispatchMacroFunction("fasl-sharp-colon", PACKAGE_SYS, false,
303                                  "stream sub-char numarg")
304    {
305        @Override
306        public LispObject execute(Stream stream, char c, int n)
307
308        {
309            LispThread thread = LispThread.currentThread();
310            Symbol symbol = (Symbol) stream.readSymbol(FaslReadtable.getInstance());
311            LispObject pkg = Load._FASL_ANONYMOUS_PACKAGE_.symbolValue(thread);
312            Debug.assertTrue(pkg != NIL);
313            symbol = ((Package)pkg).intern(symbol.getName());
314            symbol.setPackage(NIL);
315            return symbol;
316        }
317    };
318
319    // ### fasl-sharp-a
320    public static final DispatchMacroFunction FASL_SHARP_A =
321        new DispatchMacroFunction("fasl-sharp-a", PACKAGE_SYS, false,
322                                  "stream sub-char numarg")
323    {
324        @Override
325        public LispObject execute(Stream stream, char c, int n)
326
327        {
328            return stream.faslReadArray(n);
329        }
330    };
331
332    // ### fasl-sharp-b
333    public static final DispatchMacroFunction FASL_SHARP_B =
334        new DispatchMacroFunction("fasl-sharp-b", PACKAGE_SYS, false,
335                                  "stream sub-char numarg")
336    {
337        @Override
338        public LispObject execute(Stream stream, char c, int n)
339
340        {
341            return stream.faslReadRadix(2);
342        }
343    };
344
345    // ### fasl-sharp-c
346    public static final DispatchMacroFunction FASL_SHARP_C =
347        new DispatchMacroFunction("fasl-sharp-c", PACKAGE_SYS, false,
348                                  "stream sub-char numarg")
349    {
350        @Override
351        public LispObject execute(Stream stream, char c, int n)
352
353        {
354            return stream.faslReadComplex();
355        }
356    };
357
358    // ### fasl-sharp-o
359    public static final DispatchMacroFunction FASL_SHARP_O =
360        new DispatchMacroFunction("fasl-sharp-o", PACKAGE_SYS, false,
361                                  "stream sub-char numarg")
362    {
363        @Override
364        public LispObject execute(Stream stream, char c, int n)
365
366        {
367            return stream.faslReadRadix(8);
368        }
369    };
370
371    // ### fasl-sharp-p
372    public static final DispatchMacroFunction FASL_SHARP_P =
373        new DispatchMacroFunction("fasl-sharp-p", PACKAGE_SYS, false,
374                                  "stream sub-char numarg")
375    {
376        @Override
377        public LispObject execute(Stream stream, char c, int n)
378
379        {
380            return stream.faslReadPathname();
381        }
382    };
383
384    // ### fasl-sharp-r
385    public static final DispatchMacroFunction FASL_SHARP_R =
386        new DispatchMacroFunction("fasl-sharp-r", PACKAGE_SYS, false,
387                                  "stream sub-char numarg")
388    {
389        @Override
390        public LispObject execute(Stream stream, char c, int n)
391
392        {
393            return stream.faslReadRadix(n);
394        }
395    };
396
397    // ### fasl-sharp-s
398    public static final DispatchMacroFunction FASL_SHARP_S =
399        new DispatchMacroFunction("fasl-sharp-s", PACKAGE_SYS, false,
400                                  "stream sub-char numarg")
401    {
402        @Override
403        public LispObject execute(Stream stream, char c, int n)
404
405        {
406            return stream.readStructure();
407        }
408    };
409
410    // ### fasl-sharp-x
411    public static final DispatchMacroFunction FASL_SHARP_X =
412        new DispatchMacroFunction("fasl-sharp-x", PACKAGE_SYS, false,
413                                  "stream sub-char numarg")
414    {
415        @Override
416        public LispObject execute(Stream stream, char c, int n)
417
418        {
419            return stream.faslReadRadix(16);
420        }
421    };
422
423    // ### fasl-sharp-quote
424    public static final DispatchMacroFunction FASL_SHARP_QUOTE =
425        new DispatchMacroFunction("fasl-sharp-quote", PACKAGE_SYS, false,
426                                  "stream sub-char numarg")
427    {
428        @Override
429        public LispObject execute(Stream stream, char c, int n)
430
431        {
432            return new Cons(Symbol.FUNCTION,
433                            new Cons(stream.faslRead(true, NIL, true,
434                                                     LispThread.currentThread())));
435        }
436    };
437
438    // ### fasl-sharp-backslash
439    public static final DispatchMacroFunction FASL_SHARP_BACKSLASH =
440        new DispatchMacroFunction("fasl-sharp-backslash", PACKAGE_SYS, false,
441                                  "stream sub-char numarg")
442    {
443        @Override
444        public LispObject execute(Stream stream, char c, int n)
445
446        {
447            return stream.readCharacterLiteral(FaslReadtable.getInstance(),
448                                               LispThread.currentThread());
449        }
450    };
451
452    // ### fasl-sharp-vertical-bar
453    public static final DispatchMacroFunction FASL_SHARP_VERTICAL_BAR =
454        new DispatchMacroFunction("sharp-vertical-bar", PACKAGE_SYS, false,
455                                  "stream sub-char numarg")
456    {
457        @Override
458        public LispObject execute(Stream stream, char c, int n)
459
460        {
461            stream.skipBalancedComment();
462            return null;
463        }
464    };
465
466    // ### fasl-sharp-illegal
467    public static final DispatchMacroFunction FASL_SHARP_ILLEGAL =
468        new DispatchMacroFunction("fasl-sharp-illegal", PACKAGE_SYS, false,
469                                  "stream sub-char numarg")
470    {
471        @Override
472        public LispObject execute(Stream stream, char c, int n)
473
474        {
475            FastStringBuffer sb =
476                new FastStringBuffer("Illegal # macro character: #\\");
477            String s = LispCharacter.charToName(c);
478            if (s != null)
479                sb.append(s);
480            else
481                sb.append(c);
482            return error(new ReaderError(sb.toString(), stream));
483        }
484    };
485}
Note: See TracBrowser for help on using the repository browser.