source: trunk/abcl/src/org/armedbear/lisp/peek_char.java @ 12192

Last change on this file since 12192 was 11488, checked in by ehuelsmann, 16 years ago

Add @Override annotations.

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