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