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