source: branches/0.22.x/abcl/src/org/armedbear/lisp/peek_char.java

Last change on this file was 12288, checked in by vvoutilainen, 15 years ago

Don't extend Lisp in LispObject, static import Lisp wherever
necessary. Patch by Douglas R. Miles.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1/*
2 * peek_char.java
3 *
4 * Copyright (C) 2004-2005 Peter Graves
5 * $Id: peek_char.java 12288 2009-11-29 22:00:12Z vvoutilainen $
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
36import static org.armedbear.lisp.Lisp.*;
37
38// ### peek-char
39public final class peek_char extends Primitive
40{
41    private peek_char()
42    {
43        super("peek-char",
44              "&optional peek-type input-stream eof-error-p eof-value recursive-p");
45    }
46
47    @Override
48    public LispObject execute(LispObject[] args)
49    {
50        int length = args.length;
51        if (length > 5)
52            error(new WrongNumberOfArgumentsException(this));
53        LispObject peekType = length > 0 ? args[0] : NIL;
54        Stream stream = length > 1 ? inSynonymOf(args[1]) : getStandardInput();
55        boolean eofError = length > 2 ? (args[2] != NIL) : true;
56        LispObject eofValue = length > 3 ? args[3] : NIL;
57        // recursive-p is ignored
58        // boolean recursive = length > 4 ? (args[4] != NIL) : false;
59        if (peekType == NIL) {
60            // "If PEEK-TYPE is not supplied or NIL, PEEK-CHAR returns the next
61            // character to be read from INPUT-STREAM, without actually
62            // removing it from INPUT-STREAM."
63            final Stream in;
64            if (stream instanceof EchoStream)
65                // "When INPUT-STREAM is an echo stream, characters that are
66                // only peeked at are not echoed." Read from the echo stream's
67                // input stream to bypass the echo.
68                in = ((EchoStream)stream).getInputStream();
69            else
70                in = stream;
71            final LispObject result = in.readChar(eofError, eofValue);
72            if (result instanceof LispCharacter)
73                in.unreadChar((LispCharacter)result);
74            return result;
75        }
76        if (peekType == T) {
77            // "If PEEK-TYPE is T, then PEEK-CHAR skips over whitespace[2]
78            // characters, but not comments, and then performs the peeking
79            // operation on the next character."
80            Readtable rt = currentReadtable();
81            while (true) {
82                LispObject result = stream.readChar(eofError, eofValue);
83                if (result instanceof LispCharacter) {
84                    char c = ((LispCharacter)result).value;
85                    if (!rt.isWhitespace(c)) {
86                        stream.unreadChar((LispCharacter)result);
87                        return result;
88                    }
89                } else
90                    return result;
91            }
92        }
93        if (peekType instanceof LispCharacter) {
94            // "If PEEK-TYPE is a character, then PEEK-CHAR skips over input
95            // characters until a character that is CHAR= to that character is
96            // found; that character is left in INPUT-STREAM."
97            char c = ((LispCharacter)peekType).value;
98            while (true) {
99                LispObject result = stream.readChar(eofError, eofValue);
100                if (result instanceof LispCharacter) {
101                    if (((LispCharacter)result).value == c) {
102                        stream.unreadChar((LispCharacter)result);
103                        return result;
104                    }
105                } else
106                    return result;
107            }
108        }
109        return error(new SimpleError(String.valueOf(peekType) +
110                                      " is an illegal peek-type."));
111    }
112
113    private static final Primitive PEEK_CHAR = new peek_char();
114}
Note: See TracBrowser for help on using the repository browser.