| 1 | /* |
|---|
| 2 | * LispReader.java |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2004-2007 Peter Graves |
|---|
| 5 | * $Id: LispReader.java 12646 2010-05-01 21:43:28Z 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 | public final class LispReader |
|---|
| 39 | { |
|---|
| 40 | // ### read-comment |
|---|
| 41 | public static final ReaderMacroFunction READ_COMMENT = |
|---|
| 42 | new ReaderMacroFunction("read-comment", PACKAGE_SYS, false, |
|---|
| 43 | "stream character") |
|---|
| 44 | { |
|---|
| 45 | @Override |
|---|
| 46 | public LispObject execute(Stream stream, char ignored) |
|---|
| 47 | |
|---|
| 48 | { |
|---|
| 49 | try |
|---|
| 50 | { |
|---|
| 51 | while (true) { |
|---|
| 52 | int n = stream._readChar(); |
|---|
| 53 | if (n < 0) |
|---|
| 54 | return LispThread.currentThread().setValues(); |
|---|
| 55 | if (n == '\n') |
|---|
| 56 | return LispThread.currentThread().setValues(); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | catch (java.io.IOException e) |
|---|
| 60 | { |
|---|
| 61 | return LispThread.currentThread().setValues(); |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | // ### read-string |
|---|
| 67 | public static final ReaderMacroFunction READ_STRING = |
|---|
| 68 | new ReaderMacroFunction("read-string", PACKAGE_SYS, false, |
|---|
| 69 | "stream character") |
|---|
| 70 | { |
|---|
| 71 | @Override |
|---|
| 72 | public LispObject execute(Stream stream, char terminator) |
|---|
| 73 | |
|---|
| 74 | { |
|---|
| 75 | return stream.readString(terminator, Stream.currentReadtable); |
|---|
| 76 | } |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | // ### read-list |
|---|
| 80 | public static final ReaderMacroFunction READ_LIST = |
|---|
| 81 | new ReaderMacroFunction("read-list", PACKAGE_SYS, false, |
|---|
| 82 | "stream character") |
|---|
| 83 | { |
|---|
| 84 | @Override |
|---|
| 85 | public LispObject execute(Stream stream, char ignored) |
|---|
| 86 | |
|---|
| 87 | { |
|---|
| 88 | return stream.readList(false, Stream.currentReadtable); |
|---|
| 89 | } |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | // ### read-right-paren |
|---|
| 93 | public static final ReaderMacroFunction READ_RIGHT_PAREN = |
|---|
| 94 | new ReaderMacroFunction("read-right-paren", PACKAGE_SYS, false, |
|---|
| 95 | "stream character") |
|---|
| 96 | { |
|---|
| 97 | @Override |
|---|
| 98 | public LispObject execute(Stream stream, char ignored) |
|---|
| 99 | |
|---|
| 100 | { |
|---|
| 101 | return error(new ReaderError("Unmatched right parenthesis.", stream)); |
|---|
| 102 | } |
|---|
| 103 | }; |
|---|
| 104 | |
|---|
| 105 | // ### read-quote |
|---|
| 106 | public static final ReaderMacroFunction READ_QUOTE = |
|---|
| 107 | new ReaderMacroFunction("read-quote", PACKAGE_SYS, false, |
|---|
| 108 | "stream character") |
|---|
| 109 | { |
|---|
| 110 | @Override |
|---|
| 111 | public LispObject execute(Stream stream, char ignored) |
|---|
| 112 | |
|---|
| 113 | { |
|---|
| 114 | return new Cons(Symbol.QUOTE, |
|---|
| 115 | new Cons(stream.read(true, NIL, true, |
|---|
| 116 | LispThread.currentThread(), |
|---|
| 117 | Stream.currentReadtable))); |
|---|
| 118 | } |
|---|
| 119 | }; |
|---|
| 120 | |
|---|
| 121 | // ### read-dispatch-char |
|---|
| 122 | public static final ReaderMacroFunction READ_DISPATCH_CHAR = |
|---|
| 123 | new ReaderMacroFunction("read-dispatch-char", PACKAGE_SYS, false, |
|---|
| 124 | "stream character") |
|---|
| 125 | { |
|---|
| 126 | @Override |
|---|
| 127 | public LispObject execute(Stream stream, char c) |
|---|
| 128 | |
|---|
| 129 | { |
|---|
| 130 | return stream.readDispatchChar(c, Stream.currentReadtable); |
|---|
| 131 | } |
|---|
| 132 | }; |
|---|
| 133 | |
|---|
| 134 | // ### sharp-left-paren |
|---|
| 135 | public static final DispatchMacroFunction SHARP_LEFT_PAREN = |
|---|
| 136 | new DispatchMacroFunction("sharp-left-paren", PACKAGE_SYS, false, |
|---|
| 137 | "stream sub-char numarg") |
|---|
| 138 | { |
|---|
| 139 | @Override |
|---|
| 140 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 141 | |
|---|
| 142 | { |
|---|
| 143 | return stream.readSharpLeftParen(c, n, Stream.currentReadtable); |
|---|
| 144 | } |
|---|
| 145 | }; |
|---|
| 146 | |
|---|
| 147 | // ### sharp-star |
|---|
| 148 | public static final DispatchMacroFunction SHARP_STAR = |
|---|
| 149 | new DispatchMacroFunction("sharp-star", PACKAGE_SYS, false, |
|---|
| 150 | "stream sub-char numarg") |
|---|
| 151 | { |
|---|
| 152 | @Override |
|---|
| 153 | public LispObject execute(Stream stream, char ignored, int n) |
|---|
| 154 | |
|---|
| 155 | { |
|---|
| 156 | return stream.readSharpStar(ignored, n, Stream.currentReadtable); |
|---|
| 157 | } |
|---|
| 158 | }; |
|---|
| 159 | |
|---|
| 160 | // ### sharp-dot |
|---|
| 161 | public static final DispatchMacroFunction SHARP_DOT = |
|---|
| 162 | new DispatchMacroFunction("sharp-dot", PACKAGE_SYS, false, |
|---|
| 163 | "stream sub-char numarg") |
|---|
| 164 | { |
|---|
| 165 | @Override |
|---|
| 166 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 167 | |
|---|
| 168 | { |
|---|
| 169 | return stream.readSharpDot(c, n, Stream.currentReadtable); |
|---|
| 170 | } |
|---|
| 171 | }; |
|---|
| 172 | |
|---|
| 173 | // ### sharp-colon |
|---|
| 174 | public static final DispatchMacroFunction SHARP_COLON = |
|---|
| 175 | new DispatchMacroFunction("sharp-colon", PACKAGE_SYS, false, |
|---|
| 176 | "stream sub-char numarg") |
|---|
| 177 | { |
|---|
| 178 | @Override |
|---|
| 179 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 180 | |
|---|
| 181 | { |
|---|
| 182 | return stream.readSymbol(); |
|---|
| 183 | } |
|---|
| 184 | }; |
|---|
| 185 | |
|---|
| 186 | // ### sharp-a |
|---|
| 187 | public static final DispatchMacroFunction SHARP_A = |
|---|
| 188 | new DispatchMacroFunction("sharp-a", PACKAGE_SYS, false, |
|---|
| 189 | "stream sub-char numarg") |
|---|
| 190 | { |
|---|
| 191 | @Override |
|---|
| 192 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 193 | |
|---|
| 194 | { |
|---|
| 195 | return stream.readArray(n, Stream.currentReadtable); |
|---|
| 196 | } |
|---|
| 197 | }; |
|---|
| 198 | |
|---|
| 199 | // ### sharp-b |
|---|
| 200 | public static final DispatchMacroFunction SHARP_B = |
|---|
| 201 | new DispatchMacroFunction("sharp-b", PACKAGE_SYS, false, |
|---|
| 202 | "stream sub-char numarg") |
|---|
| 203 | { |
|---|
| 204 | @Override |
|---|
| 205 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 206 | |
|---|
| 207 | { |
|---|
| 208 | return stream.readRadix(2, Stream.currentReadtable); |
|---|
| 209 | } |
|---|
| 210 | }; |
|---|
| 211 | |
|---|
| 212 | // ### sharp-c |
|---|
| 213 | public static final DispatchMacroFunction SHARP_C = |
|---|
| 214 | new DispatchMacroFunction("sharp-c", PACKAGE_SYS, false, |
|---|
| 215 | "stream sub-char numarg") |
|---|
| 216 | { |
|---|
| 217 | @Override |
|---|
| 218 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 219 | |
|---|
| 220 | { |
|---|
| 221 | return stream.readComplex(Stream.currentReadtable); |
|---|
| 222 | } |
|---|
| 223 | }; |
|---|
| 224 | |
|---|
| 225 | // ### sharp-o |
|---|
| 226 | public static final DispatchMacroFunction SHARP_O = |
|---|
| 227 | new DispatchMacroFunction("sharp-o", PACKAGE_SYS, false, |
|---|
| 228 | "stream sub-char numarg") |
|---|
| 229 | { |
|---|
| 230 | @Override |
|---|
| 231 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 232 | |
|---|
| 233 | { |
|---|
| 234 | return stream.readRadix(8, Stream.currentReadtable); |
|---|
| 235 | } |
|---|
| 236 | }; |
|---|
| 237 | |
|---|
| 238 | // ### sharp-p |
|---|
| 239 | public static final DispatchMacroFunction SHARP_P = |
|---|
| 240 | new DispatchMacroFunction("sharp-p", PACKAGE_SYS, false, |
|---|
| 241 | "stream sub-char numarg") |
|---|
| 242 | { |
|---|
| 243 | @Override |
|---|
| 244 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 245 | |
|---|
| 246 | { |
|---|
| 247 | return stream.readPathname(Stream.currentReadtable); |
|---|
| 248 | } |
|---|
| 249 | }; |
|---|
| 250 | |
|---|
| 251 | // ### sharp-r |
|---|
| 252 | public static final DispatchMacroFunction SHARP_R = |
|---|
| 253 | new DispatchMacroFunction("sharp-r", PACKAGE_SYS, false, |
|---|
| 254 | "stream sub-char numarg") |
|---|
| 255 | { |
|---|
| 256 | @Override |
|---|
| 257 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 258 | |
|---|
| 259 | { |
|---|
| 260 | return stream.readRadix(n, Stream.currentReadtable); |
|---|
| 261 | } |
|---|
| 262 | }; |
|---|
| 263 | |
|---|
| 264 | // ### sharp-s |
|---|
| 265 | public static final DispatchMacroFunction SHARP_S = |
|---|
| 266 | new DispatchMacroFunction("sharp-s", PACKAGE_SYS, false, |
|---|
| 267 | "stream sub-char numarg") |
|---|
| 268 | { |
|---|
| 269 | @Override |
|---|
| 270 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 271 | |
|---|
| 272 | { |
|---|
| 273 | return stream.readStructure(Stream.currentReadtable); |
|---|
| 274 | } |
|---|
| 275 | }; |
|---|
| 276 | |
|---|
| 277 | // ### sharp-x |
|---|
| 278 | public static final DispatchMacroFunction SHARP_X = |
|---|
| 279 | new DispatchMacroFunction("sharp-x", PACKAGE_SYS, false, |
|---|
| 280 | "stream sub-char numarg") |
|---|
| 281 | { |
|---|
| 282 | @Override |
|---|
| 283 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 284 | |
|---|
| 285 | { |
|---|
| 286 | return stream.readRadix(16, Stream.currentReadtable); |
|---|
| 287 | } |
|---|
| 288 | }; |
|---|
| 289 | |
|---|
| 290 | // ### sharp-quote |
|---|
| 291 | public static final DispatchMacroFunction SHARP_QUOTE = |
|---|
| 292 | new DispatchMacroFunction("sharp-quote", PACKAGE_SYS, false, |
|---|
| 293 | "stream sub-char numarg") |
|---|
| 294 | { |
|---|
| 295 | @Override |
|---|
| 296 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 297 | |
|---|
| 298 | { |
|---|
| 299 | return new Cons(Symbol.FUNCTION, |
|---|
| 300 | new Cons(stream.read(true, NIL, true, |
|---|
| 301 | LispThread.currentThread(), |
|---|
| 302 | Stream.currentReadtable))); |
|---|
| 303 | } |
|---|
| 304 | }; |
|---|
| 305 | |
|---|
| 306 | // ### sharp-backslash |
|---|
| 307 | public static final DispatchMacroFunction SHARP_BACKSLASH = |
|---|
| 308 | new DispatchMacroFunction("sharp-backslash", PACKAGE_SYS, false, |
|---|
| 309 | "stream sub-char numarg") |
|---|
| 310 | { |
|---|
| 311 | @Override |
|---|
| 312 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 313 | |
|---|
| 314 | { |
|---|
| 315 | final LispThread thread = LispThread.currentThread(); |
|---|
| 316 | final Readtable rt = (Readtable) Symbol.CURRENT_READTABLE.symbolValue(thread); |
|---|
| 317 | return stream.readCharacterLiteral(rt, thread); |
|---|
| 318 | } |
|---|
| 319 | }; |
|---|
| 320 | |
|---|
| 321 | // ### sharp-vertical-bar |
|---|
| 322 | public static final DispatchMacroFunction SHARP_VERTICAL_BAR = |
|---|
| 323 | new DispatchMacroFunction("sharp-vertical-bar", PACKAGE_SYS, false, |
|---|
| 324 | "stream sub-char numarg") |
|---|
| 325 | { |
|---|
| 326 | @Override |
|---|
| 327 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 328 | |
|---|
| 329 | { |
|---|
| 330 | stream.skipBalancedComment(); |
|---|
| 331 | return LispThread.currentThread().setValues(); |
|---|
| 332 | } |
|---|
| 333 | }; |
|---|
| 334 | |
|---|
| 335 | // ### sharp-illegal |
|---|
| 336 | public static final DispatchMacroFunction SHARP_ILLEGAL = |
|---|
| 337 | new DispatchMacroFunction("sharp-illegal", PACKAGE_SYS, false, |
|---|
| 338 | "stream sub-char numarg") |
|---|
| 339 | { |
|---|
| 340 | @Override |
|---|
| 341 | public LispObject execute(Stream stream, char c, int n) |
|---|
| 342 | |
|---|
| 343 | { |
|---|
| 344 | StringBuilder sb = new StringBuilder("Illegal # macro character: #\\"); |
|---|
| 345 | String s = LispCharacter.charToName(c); |
|---|
| 346 | if (s != null) |
|---|
| 347 | sb.append(s); |
|---|
| 348 | else |
|---|
| 349 | sb.append(c); |
|---|
| 350 | return error(new ReaderError(sb.toString(), stream)); |
|---|
| 351 | } |
|---|
| 352 | }; |
|---|
| 353 | } |
|---|