KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * peek_char.java
3  *
4  * Copyright (C) 2004 Peter Graves
5  * $Id: peek_char.java,v 1.4 2004/03/12 18:48:03 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 // ### peek-char
25
public final class peek_char extends Primitive
26 {
27     private peek_char()
28     {
29         super("peek-char",
30               "&optional peek-type input-stream eof-error-p eof-value recursive-p");
31     }
32
33     public LispObject execute(LispObject[] args) throws ConditionThrowable
34     {
35         int length = args.length;
36         if (length > 5)
37             signal(new WrongNumberOfArgumentsException(this));
38         LispObject peekType = length > 0 ? args[0] : NIL;
39         Stream stream = length > 1 ? inSynonymOf(args[1]) : getStandardInput();
40         boolean eofError = length > 2 ? (args[2] != NIL) : true;
41         LispObject eofValue = length > 3 ? args[3] : NIL;
42         boolean recursive = length > 4 ? (args[4] != NIL) : false;
43         if (peekType == NIL) {
44             LispObject result = stream.readChar(eofError, eofValue);
45             if (result instanceof LispCharacter)
46                 stream.unreadChar((LispCharacter)result);
47             return result;
48         }
49         if (peekType == T) {
50             Readtable rt = currentReadtable();
51             while (true) {
52                 LispObject result = stream.readChar(eofError, eofValue);
53                 if (result instanceof LispCharacter) {
54                     char c = ((LispCharacter)result).value;
55                     if (!rt.isWhitespace(c)) {
56                         stream.unreadChar((LispCharacter)result);
57                         return result;
58                     }
59                 } else
60                     return result;
61             }
62         }
63         if (peekType instanceof LispCharacter) {
64             char c = ((LispCharacter)peekType).value;
65             while (true) {
66                 LispObject result = stream.readChar(eofError, eofValue);
67                 if (result instanceof LispCharacter) {
68                     if (((LispCharacter)result).value == c) {
69                         stream.unreadChar((LispCharacter)result);
70                         return result;
71                     }
72                 } else
73                     return result;
74             }
75         }
76         return signal(new SimpleError(String.valueOf(peekType) +
77                                       " is an illegal peek-type."));
78     }
79
80     private static final Primitive PEEK_CHAR = new peek_char();
81 }
82
Popular Tags