* time.scm (time): Reimplemented as a procedure call.
[bpt/guile.git] / libguile / read.c
CommitLineData
f2c9fcb0 1/* Copyright (C) 1995,1996,1997, 1999, 2000 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd
JB
45\f
46
0f2d19dd 47#include <stdio.h>
a0599745
MD
48#include "libguile/_scm.h"
49#include "libguile/chars.h"
50#include "libguile/eval.h"
51#include "libguile/unif.h"
52#include "libguile/keywords.h"
53#include "libguile/alist.h"
54#include "libguile/srcprop.h"
55#include "libguile/hashtab.h"
56#include "libguile/hash.h"
57#include "libguile/ports.h"
58#include "libguile/root.h"
59#include "libguile/strings.h"
60#include "libguile/vectors.h"
61
62#include "libguile/validate.h"
63#include "libguile/read.h"
0f2d19dd
JB
64
65\f
66
c7733771
GH
67SCM_SYMBOL (scm_keyword_prefix, "prefix");
68
a16f6fe7 69scm_option scm_read_opts[] = {
b7ff98dd
MD
70 { SCM_OPTION_BOOLEAN, "copy", 0,
71 "Copy source code expressions." },
ac74fc22 72 { SCM_OPTION_BOOLEAN, "positions", 0,
deca31e1
GH
73 "Record positions of source code expressions." },
74 { SCM_OPTION_BOOLEAN, "case-insensitive", 0,
c7733771 75 "Convert symbols to lower case."},
f1267706 76 { SCM_OPTION_SCM, "keywords", SCM_UNPACK (SCM_BOOL_F),
c7733771 77 "Style of keyword recognition: #f or 'prefix"}
a16f6fe7
MD
78};
79
a1ec6916 80SCM_DEFINE (scm_read_options, "read-options-interface", 0, 1, 0,
1bbd0b84 81 (SCM setting),
dc7fa443
MG
82 "Option interface for the read options. Instead of using\n"
83 "this procedure directly, use the procedures @code{read-enable},\n"
84 "@code{read-disable}, @code{read-set!} and @var{read-options}.")
1bbd0b84 85#define FUNC_NAME s_scm_read_options
a16f6fe7 86{
b7ff98dd
MD
87 SCM ans = scm_options (setting,
88 scm_read_opts,
89 SCM_N_READ_OPTIONS,
1bbd0b84 90 FUNC_NAME);
b7ff98dd
MD
91 if (SCM_COPY_SOURCE_P)
92 SCM_RECORD_POSITIONS_P = 1;
a16f6fe7
MD
93 return ans;
94}
1bbd0b84 95#undef FUNC_NAME
a16f6fe7 96
14de3b42
GH
97/* An association list mapping extra hash characters to procedures. */
98static SCM *scm_read_hash_procedures;
deca31e1 99
a1ec6916 100SCM_DEFINE (scm_read, "read", 0, 1, 0,
1bbd0b84 101 (SCM port),
dc7fa443
MG
102 "Read an s-expression from the input port @var{port}, or from\n"
103 "the current input port if @var{port} is not specified.\n"
104 "Any whitespace before the next token is discarded.")
1bbd0b84 105#define FUNC_NAME s_scm_read
0f2d19dd
JB
106{
107 int c;
09a4f039 108 SCM tok_buf, copy;
0f2d19dd
JB
109
110 if (SCM_UNBNDP (port))
111 port = scm_cur_inp;
3b3b36dd 112 SCM_VALIDATE_OPINPORT (1,port);
0f2d19dd 113
0f2d19dd
JB
114 c = scm_flush_ws (port, (char *) NULL);
115 if (EOF == c)
116 return SCM_EOF_VAL;
b7f3516f 117 scm_ungetc (c, port);
0f2d19dd
JB
118
119 tok_buf = scm_makstr (30L, 0);
deca31e1 120 return scm_lreadr (&tok_buf, port, &copy);
0f2d19dd 121}
1bbd0b84 122#undef FUNC_NAME
0f2d19dd
JB
123
124
1cc91f1b 125
0f2d19dd 126char *
6e8d25a6 127scm_grow_tok_buf (SCM *tok_buf)
0f2d19dd 128{
94115ae3
DH
129 unsigned long int oldlen = SCM_STRING_LENGTH (*tok_buf);
130 SCM newstr = scm_makstr (2 * oldlen, 0);
131 unsigned long int i;
132
133 for (i = 0; i != oldlen; ++i)
134 SCM_STRING_CHARS (newstr) [i] = SCM_STRING_CHARS (*tok_buf) [i];
135
136 *tok_buf = newstr;
137 return SCM_STRING_CHARS (newstr);
0f2d19dd
JB
138}
139
140
1cc91f1b 141
0f2d19dd 142int
6e8d25a6 143scm_flush_ws (SCM port, const char *eoferr)
0f2d19dd
JB
144{
145 register int c;
146 while (1)
b7f3516f 147 switch (c = scm_getc (port))
0f2d19dd
JB
148 {
149 case EOF:
150 goteof:
151 if (eoferr)
d156d3b7 152 {
d1ca2c64 153 if (!SCM_FALSEP (SCM_FILENAME (port)))
d156d3b7
MV
154 scm_misc_error (eoferr,
155 "end of file in ~A",
156 SCM_LIST1 (SCM_FILENAME (port)));
157 else
158 scm_misc_error (eoferr, "end of file", SCM_EOL);
159 }
0f2d19dd
JB
160 return c;
161 case ';':
162 lp:
b7f3516f 163 switch (c = scm_getc (port))
0f2d19dd
JB
164 {
165 case EOF:
166 goto goteof;
167 default:
168 goto lp;
169 case SCM_LINE_INCREMENTORS:
170 break;
171 }
172 break;
173 case SCM_LINE_INCREMENTORS:
0f2d19dd 174 case SCM_SINGLE_SPACES:
0f2d19dd 175 case '\t':
0f2d19dd
JB
176 break;
177 default:
178 return c;
179 }
180}
181
182
1cc91f1b 183
0f2d19dd 184int
6e8d25a6 185scm_casei_streq (char *s1, char *s2)
0f2d19dd
JB
186{
187 while (*s1 && *s2)
188 if (scm_downcase((int)*s1) != scm_downcase((int)*s2))
189 return 0;
190 else
191 {
192 ++s1;
193 ++s2;
194 }
195 return !(*s1 || *s2);
196}
197
198
09a4f039
MD
199/* recsexpr is used when recording expressions
200 * constructed by read:sharp.
201 */
604d4dd9
JB
202#ifndef DEBUG_EXTENSIONS
203#define recsexpr(obj, line, column, filename) (obj)
204#else
09a4f039 205static SCM
1bbd0b84 206recsexpr (SCM obj,int line,int column,SCM filename)
09a4f039 207{
fee7ef83 208 if (!SCM_CONSP(obj)) {
09a4f039 209 return obj;
fee7ef83 210 } else {
09a4f039
MD
211 SCM tmp = obj, copy;
212 /* If this sexpr is visible in the read:sharp source, we want to
213 keep that information, so only record non-constant cons cells
214 which haven't previously been read by the reader. */
215 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash, obj)))
216 {
217 if (SCM_COPY_SOURCE_P)
218 {
219 copy = scm_cons (recsexpr (SCM_CAR (obj), line, column, filename),
220 SCM_UNDEFINED);
0c95b57d 221 while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
a6c64c3c
MD
222 {
223 SCM_SETCDR (copy, scm_cons (recsexpr (SCM_CAR (tmp),
224 line,
225 column,
226 filename),
227 SCM_UNDEFINED));
228 copy = SCM_CDR (copy);
229 }
230 SCM_SETCDR (copy, tmp);
09a4f039
MD
231 }
232 else
233 {
234 recsexpr (SCM_CAR (obj), line, column, filename);
0c95b57d 235 while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
09a4f039
MD
236 recsexpr (SCM_CAR (tmp), line, column, filename);
237 copy = SCM_UNDEFINED;
238 }
239 scm_whash_insert (scm_source_whash,
240 obj,
241 scm_make_srcprops (line,
242 column,
243 filename,
244 copy,
245 SCM_EOL));
246 }
247 return obj;
248 }
249}
604d4dd9 250#endif
f9c68a47
JB
251
252/* Consume an SCSH-style block comment. Assume that we've already
f9731264
JB
253 read the initial `#!', and eat characters until we get a
254 newline/exclamation-point/sharp-sign/newline sequence. */
f9c68a47
JB
255
256static void
6e8d25a6 257skip_scsh_block_comment (SCM port)
db4b4ca6 258#define FUNC_NAME "skip_scsh_block_comment"
f9c68a47 259{
f9731264
JB
260 /* Is this portable? Dear God, spare me from the non-eight-bit
261 characters. But is it tasteful? */
262 long history = 0;
f9c68a47
JB
263
264 for (;;)
265 {
b7f3516f 266 int c = scm_getc (port);
f9c68a47
JB
267
268 if (c == EOF)
db4b4ca6 269 SCM_MISC_ERROR ("unterminated `#! ... !#' comment", SCM_EOL);
f9731264 270 history = ((history << 8) | (c & 0xff)) & 0xffffffff;
f9c68a47 271
f9731264
JB
272 /* Were the last four characters read "\n!#\n"? */
273 if (history == (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
274 return;
f9c68a47
JB
275 }
276}
db4b4ca6
DH
277#undef FUNC_NAME
278
f9c68a47 279
1bbd0b84 280static SCM scm_get_hash_procedure(int c);
f9c68a47 281
09a4f039 282static char s_list[]="list";
1cc91f1b 283
0f2d19dd 284SCM
1bbd0b84 285scm_lreadr (SCM *tok_buf,SCM port,SCM *copy)
db4b4ca6 286#define FUNC_NAME "scm_lreadr"
0f2d19dd
JB
287{
288 int c;
289 scm_sizet j;
290 SCM p;
deca31e1 291
0f2d19dd 292tryagain:
1bbd0b84 293 c = scm_flush_ws (port, s_scm_read);
b6356af7 294tryagain_no_flush_ws:
0f2d19dd
JB
295 switch (c)
296 {
297 case EOF:
298 return SCM_EOF_VAL;
299
300 case '(':
09a4f039 301 return SCM_RECORD_POSITIONS_P
deca31e1
GH
302 ? scm_lreadrecparen (tok_buf, port, s_list, copy)
303 : scm_lreadparen (tok_buf, port, s_list, copy);
0f2d19dd 304 case ')':
db4b4ca6 305 SCM_MISC_ERROR ("unexpected \")\"", SCM_EOL);
0f2d19dd
JB
306 goto tryagain;
307
308 case '\'':
92e5aa0e 309 p = scm_sym_quote;
09a4f039 310 goto recquote;
0f2d19dd 311 case '`':
92e5aa0e 312 p = scm_sym_quasiquote;
09a4f039 313 goto recquote;
0f2d19dd 314 case ',':
b7f3516f 315 c = scm_getc (port);
0f2d19dd 316 if ('@' == c)
92e5aa0e 317 p = scm_sym_uq_splicing;
0f2d19dd
JB
318 else
319 {
b7f3516f 320 scm_ungetc (c, port);
92e5aa0e 321 p = scm_sym_unquote;
0f2d19dd 322 }
09a4f039
MD
323 recquote:
324 p = scm_cons2 (p,
deca31e1 325 scm_lreadr (tok_buf, port, copy),
09a4f039
MD
326 SCM_EOL);
327 if (SCM_RECORD_POSITIONS_P)
328 scm_whash_insert (scm_source_whash,
329 p,
330 scm_make_srcprops (SCM_LINUM (port),
331 SCM_COL (port) - 1,
332 SCM_FILENAME (port),
333 SCM_COPY_SOURCE_P
334 ? (*copy = scm_cons2 (SCM_CAR (p),
335 SCM_CAR (SCM_CDR (p)),
336 SCM_EOL))
337 : SCM_UNDEFINED,
338 SCM_EOL));
339 return p;
0f2d19dd 340 case '#':
b7f3516f 341 c = scm_getc (port);
0f2d19dd
JB
342 switch (c)
343 {
344 case '(':
deca31e1 345 p = scm_lreadparen (tok_buf, port, "vector", copy);
0f2d19dd
JB
346 return SCM_NULLP (p) ? scm_nullvect : scm_vector (p);
347
348 case 't':
349 case 'T':
350 return SCM_BOOL_T;
351 case 'f':
352 case 'F':
353 return SCM_BOOL_F;
354
355 case 'b':
356 case 'B':
357 case 'o':
358 case 'O':
359 case 'd':
360 case 'D':
361 case 'x':
362 case 'X':
363 case 'i':
364 case 'I':
365 case 'e':
366 case 'E':
b7f3516f 367 scm_ungetc (c, port);
0f2d19dd
JB
368 c = '#';
369 goto num;
370
f9c68a47
JB
371 case '!':
372 /* start of a shell script. Parse as a block comment,
373 terminated by !#, just like SCSH. */
374 skip_scsh_block_comment (port);
b6356af7
MV
375 /* EOF is not an error here */
376 c = scm_flush_ws (port, (char *)NULL);
377 goto tryagain_no_flush_ws;
f9c68a47 378
afe5177e 379#ifdef HAVE_ARRAYS
0f2d19dd 380 case '*':
deca31e1 381 j = scm_read_token (c, tok_buf, port, 0);
405aaef9 382 p = scm_istr2bve (SCM_STRING_CHARS (*tok_buf) + 1, (long) (j - 1));
0f2d19dd
JB
383 if (SCM_NFALSEP (p))
384 return p;
385 else
386 goto unkshrp;
afe5177e 387#endif
0f2d19dd
JB
388
389 case '{':
deca31e1 390 j = scm_read_token (c, tok_buf, port, 1);
38ae064c 391 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
0f2d19dd
JB
392
393 case '\\':
b7f3516f 394 c = scm_getc (port);
deca31e1 395 j = scm_read_token (c, tok_buf, port, 0);
0f2d19dd 396 if (j == 1)
7866a09b 397 return SCM_MAKE_CHAR (c);
0f2d19dd
JB
398 if (c >= '0' && c < '8')
399 {
405aaef9 400 p = scm_istr2int (SCM_STRING_CHARS (*tok_buf), (long) j, 8);
0f2d19dd 401 if (SCM_NFALSEP (p))
7866a09b 402 return SCM_MAKE_CHAR (SCM_INUM (p));
0f2d19dd
JB
403 }
404 for (c = 0; c < scm_n_charnames; c++)
405 if (scm_charnames[c]
405aaef9 406 && (scm_casei_streq (scm_charnames[c], SCM_STRING_CHARS (*tok_buf))))
7866a09b 407 return SCM_MAKE_CHAR (scm_charnums[c]);
db4b4ca6 408 SCM_MISC_ERROR ("unknown # object", SCM_EOL);
0f2d19dd 409
50a095f1
JB
410 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
411 case ':':
412 j = scm_read_token ('-', tok_buf, port, 0);
38ae064c
DH
413 p = scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
414 return scm_make_keyword_from_dash_symbol (p);
0f2d19dd
JB
415
416 default:
417 callshrp:
deca31e1
GH
418 {
419 SCM sharp = scm_get_hash_procedure (c);
420
421 if (SCM_NIMP (sharp))
422 {
423 int line = SCM_LINUM (port);
424 int column = SCM_COL (port) - 2;
425 SCM got;
426
427 got = scm_apply (sharp,
7866a09b 428 SCM_MAKE_CHAR (c),
deca31e1 429 scm_acons (port, SCM_EOL, SCM_EOL));
54778cd3 430 if (SCM_EQ_P (got, SCM_UNSPECIFIED))
deca31e1
GH
431 goto unkshrp;
432 if (SCM_RECORD_POSITIONS_P)
433 return *copy = recsexpr (got, line, column,
434 SCM_FILENAME (port));
435 else
436 return got;
437 }
438 }
03bc4386 439 unkshrp:
70d63753 440 scm_misc_error (s_scm_read, "Unknown # object: ~S",
e0c08f17 441 SCM_LIST1 (SCM_MAKE_CHAR (c)));
0f2d19dd
JB
442 }
443
444 case '"':
445 j = 0;
b7f3516f 446 while ('"' != (c = scm_getc (port)))
0f2d19dd 447 {
b3fcac34
DH
448 if (c == EOF)
449 SCM_MISC_ERROR ("end of file in string constant", SCM_EOL);
0f2d19dd 450
94115ae3 451 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
0f2d19dd
JB
452 scm_grow_tok_buf (tok_buf);
453
454 if (c == '\\')
b7f3516f 455 switch (c = scm_getc (port))
0f2d19dd
JB
456 {
457 case '\n':
458 continue;
459 case '0':
460 c = '\0';
461 break;
462 case 'f':
463 c = '\f';
464 break;
465 case 'n':
466 c = '\n';
467 break;
468 case 'r':
469 c = '\r';
470 break;
471 case 't':
472 c = '\t';
473 break;
474 case 'a':
475 c = '\007';
476 break;
477 case 'v':
478 c = '\v';
479 break;
480 }
405aaef9 481 SCM_STRING_CHARS (*tok_buf)[j] = c;
b7f3516f 482 ++j;
0f2d19dd
JB
483 }
484 if (j == 0)
485 return scm_nullstr;
405aaef9 486 SCM_STRING_CHARS (*tok_buf)[j] = 0;
0f2d19dd
JB
487 {
488 SCM str;
405aaef9 489 str = scm_makfromstr (SCM_STRING_CHARS (*tok_buf), j, 0);
0f2d19dd
JB
490 return str;
491 }
492
493 case'0':case '1':case '2':case '3':case '4':
494 case '5':case '6':case '7':case '8':case '9':
495 case '.':
496 case '-':
497 case '+':
498 num:
deca31e1 499 j = scm_read_token (c, tok_buf, port, 0);
405aaef9 500 p = scm_istring2number (SCM_STRING_CHARS (*tok_buf), (long) j, 10L);
0f2d19dd
JB
501 if (SCM_NFALSEP (p))
502 return p;
503 if (c == '#')
504 {
b7f3516f 505 if ((j == 2) && (scm_getc (port) == '('))
0f2d19dd 506 {
b7f3516f 507 scm_ungetc ('(', port);
405aaef9 508 c = SCM_STRING_CHARS (*tok_buf)[1];
0f2d19dd
JB
509 goto callshrp;
510 }
db4b4ca6 511 SCM_MISC_ERROR ("unknown # object", SCM_EOL);
0f2d19dd
JB
512 }
513 goto tok;
514
515 case ':':
fee7ef83 516 if (SCM_EQ_P (SCM_PACK (SCM_KEYWORD_STYLE), scm_keyword_prefix))
c7733771
GH
517 {
518 j = scm_read_token ('-', tok_buf, port, 0);
38ae064c
DH
519 p = scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
520 return scm_make_keyword_from_dash_symbol (p);
c7733771
GH
521 }
522 /* fallthrough */
0f2d19dd 523 default:
deca31e1 524 j = scm_read_token (c, tok_buf, port, 0);
0f2d19dd
JB
525 /* fallthrough */
526
527 tok:
38ae064c 528 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
0f2d19dd
JB
529 }
530}
db4b4ca6
DH
531#undef FUNC_NAME
532
0f2d19dd
JB
533
534#ifdef _UNICOS
535_Pragma ("noopt"); /* # pragma _CRI noopt */
536#endif
1cc91f1b 537
0f2d19dd 538scm_sizet
6e8d25a6 539scm_read_token (int ic, SCM *tok_buf, SCM port, int weird)
0f2d19dd
JB
540{
541 register scm_sizet j;
542 register int c;
543 register char *p;
544
deca31e1 545 c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(ic) : ic);
405aaef9 546 p = SCM_STRING_CHARS (*tok_buf);
0f2d19dd
JB
547
548 if (weird)
549 j = 0;
550 else
551 {
552 j = 0;
94115ae3 553 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
0f2d19dd 554 p = scm_grow_tok_buf (tok_buf);
b7f3516f
TT
555 p[j] = c;
556 ++j;
0f2d19dd
JB
557 }
558
559 while (1)
560 {
94115ae3 561 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
0f2d19dd 562 p = scm_grow_tok_buf (tok_buf);
b7f3516f 563 c = scm_getc (port);
0f2d19dd
JB
564 switch (c)
565 {
566 case '(':
567 case ')':
568 case '"':
569 case ';':
570 case SCM_WHITE_SPACES:
571 case SCM_LINE_INCREMENTORS:
572 if (weird)
573 goto default_case;
574
b7f3516f 575 scm_ungetc (c, port);
0f2d19dd
JB
576 case EOF:
577 eof_case:
578 p[j] = 0;
579 return j;
580 case '\\':
581 if (!weird)
582 goto default_case;
583 else
584 {
b7f3516f 585 c = scm_getc (port);
0f2d19dd
JB
586 if (c == EOF)
587 goto eof_case;
588 else
589 goto default_case;
590 }
591 case '}':
592 if (!weird)
593 goto default_case;
594
b7f3516f 595 c = scm_getc (port);
0f2d19dd
JB
596 if (c == '#')
597 {
598 p[j] = 0;
599 return j;
600 }
601 else
602 {
b7f3516f 603 scm_ungetc (c, port);
0f2d19dd
JB
604 c = '}';
605 goto default_case;
606 }
607
608 default:
609 default_case:
610 {
deca31e1 611 c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(c) : c);
b7f3516f
TT
612 p[j] = c;
613 ++j;
0f2d19dd
JB
614 }
615
616 }
617 }
618}
1cc91f1b 619
0f2d19dd
JB
620#ifdef _UNICOS
621_Pragma ("opt"); /* # pragma _CRI opt */
622#endif
623
0f2d19dd 624SCM
6e8d25a6 625scm_lreadparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
db4b4ca6 626#define FUNC_NAME "scm_lreadparen"
0f2d19dd
JB
627{
628 SCM tmp;
629 SCM tl;
630 SCM ans;
631 int c;
632
633 c = scm_flush_ws (port, name);
634 if (')' == c)
635 return SCM_EOL;
b7f3516f 636 scm_ungetc (c, port);
54778cd3 637 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
0f2d19dd 638 {
deca31e1 639 ans = scm_lreadr (tok_buf, port, copy);
0f2d19dd
JB
640 closeit:
641 if (')' != (c = scm_flush_ws (port, name)))
db4b4ca6 642 SCM_MISC_ERROR ("missing close paren", SCM_EOL);
0f2d19dd
JB
643 return ans;
644 }
645 ans = tl = scm_cons (tmp, SCM_EOL);
646 while (')' != (c = scm_flush_ws (port, name)))
647 {
b7f3516f 648 scm_ungetc (c, port);
54778cd3 649 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
0f2d19dd 650 {
deca31e1 651 SCM_SETCDR (tl, scm_lreadr (tok_buf, port, copy));
0f2d19dd
JB
652 goto closeit;
653 }
a6c64c3c
MD
654 SCM_SETCDR (tl, scm_cons (tmp, SCM_EOL));
655 tl = SCM_CDR (tl);
0f2d19dd
JB
656 }
657 return ans;
658}
db4b4ca6 659#undef FUNC_NAME
0f2d19dd 660
1cc91f1b 661
09a4f039 662SCM
6e8d25a6 663scm_lreadrecparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
db4b4ca6 664#define FUNC_NAME "scm_lreadrecparen"
09a4f039
MD
665{
666 register int c;
667 register SCM tmp;
4dc2435a
JB
668 register SCM tl, tl2 = SCM_EOL;
669 SCM ans, ans2 = SCM_EOL;
09a4f039
MD
670 /* Need to capture line and column numbers here. */
671 int line = SCM_LINUM (port);
672 int column = SCM_COL (port) - 1;
673
674 c = scm_flush_ws (port, name);
675 if (')' == c)
676 return SCM_EOL;
b7f3516f 677 scm_ungetc (c, port);
54778cd3 678 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
09a4f039 679 {
deca31e1 680 ans = scm_lreadr (tok_buf, port, copy);
09a4f039 681 if (')' != (c = scm_flush_ws (port, name)))
db4b4ca6 682 SCM_MISC_ERROR ("missing close paren", SCM_EOL);
09a4f039
MD
683 return ans;
684 }
685 /* Build the head of the list structure. */
686 ans = tl = scm_cons (tmp, SCM_EOL);
687 if (SCM_COPY_SOURCE_P)
0c95b57d 688 ans2 = tl2 = scm_cons (SCM_CONSP (tmp)
09a4f039
MD
689 ? *copy
690 : tmp,
691 SCM_EOL);
692 while (')' != (c = scm_flush_ws (port, name)))
693 {
62850ef3
DH
694 SCM new_tail;
695
b7f3516f 696 scm_ungetc (c, port);
54778cd3 697 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
09a4f039 698 {
deca31e1 699 SCM_SETCDR (tl, tmp = scm_lreadr (tok_buf, port, copy));
09a4f039 700 if (SCM_COPY_SOURCE_P)
0c95b57d 701 SCM_SETCDR (tl2, scm_cons (SCM_CONSP (tmp)
09a4f039
MD
702 ? *copy
703 : tmp,
704 SCM_EOL));
705 if (')' != (c = scm_flush_ws (port, name)))
db4b4ca6 706 SCM_MISC_ERROR ("missing close paren", SCM_EOL);
09a4f039
MD
707 goto exit;
708 }
62850ef3
DH
709
710 new_tail = scm_cons (tmp, SCM_EOL);
711 SCM_SETCDR (tl, new_tail);
712 tl = new_tail;
713
09a4f039 714 if (SCM_COPY_SOURCE_P)
62850ef3
DH
715 {
716 SCM new_tail2 = scm_cons (SCM_CONSP (tmp) ? *copy : tmp, SCM_EOL);
717 SCM_SETCDR (tl2, new_tail2);
718 tl2 = new_tail2;
719 }
09a4f039
MD
720 }
721exit:
722 scm_whash_insert (scm_source_whash,
723 ans,
724 scm_make_srcprops (line,
725 column,
726 SCM_FILENAME (port),
727 SCM_COPY_SOURCE_P
728 ? *copy = ans2
729 : SCM_UNDEFINED,
730 SCM_EOL));
731 return ans;
732}
db4b4ca6 733#undef FUNC_NAME
09a4f039 734
0f2d19dd
JB
735
736\f
737
14de3b42
GH
738/* Manipulate the read-hash-procedures alist. This could be written in
739 Scheme, but maybe it will also be used by C code during initialisation. */
a1ec6916 740SCM_DEFINE (scm_read_hash_extend, "read-hash-extend", 2, 0, 0,
1bbd0b84 741 (SCM chr, SCM proc),
dc7fa443
MG
742 "Install the procedure @var{proc} for reading expressions\n"
743 "starting with the character sequence @code{#} and @var{chr}.\n"
744 "@var{proc} will be called with two arguments: the character\n"
745 "@var{chr} and the port to read further data from. The object\n"
746 "returned will be the return value of @code{read}.")
1bbd0b84 747#define FUNC_NAME s_scm_read_hash_extend
deca31e1 748{
fed9c9a2
GH
749 SCM this;
750 SCM prev;
751
7866a09b 752 SCM_VALIDATE_CHAR (1,chr);
fed9c9a2 753 SCM_ASSERT (SCM_FALSEP (proc) || SCM_NIMP(proc), proc, SCM_ARG2,
1bbd0b84 754 FUNC_NAME);
fed9c9a2 755
14de3b42
GH
756 /* Check if chr is already in the alist. */
757 this = *scm_read_hash_procedures;
758 prev = SCM_BOOL_F;
fed9c9a2
GH
759 while (1)
760 {
761 if (SCM_NULLP (this))
762 {
763 /* not found, so add it to the beginning. */
764 if (SCM_NFALSEP (proc))
765 {
14de3b42
GH
766 *scm_read_hash_procedures =
767 scm_cons (scm_cons (chr, proc), *scm_read_hash_procedures);
fed9c9a2
GH
768 }
769 break;
770 }
54778cd3 771 if (SCM_EQ_P (chr, SCM_CAAR (this)))
fed9c9a2
GH
772 {
773 /* already in the alist. */
774 if (SCM_FALSEP (proc))
14de3b42
GH
775 {
776 /* remove it. */
54778cd3 777 if (SCM_FALSEP (prev))
14de3b42
GH
778 {
779 *scm_read_hash_procedures =
780 SCM_CDR (*scm_read_hash_procedures);
781 }
782 else
783 scm_set_cdr_x (prev, SCM_CDR (this));
784 }
fed9c9a2 785 else
14de3b42
GH
786 {
787 /* replace it. */
788 scm_set_cdr_x (SCM_CAR (this), proc);
789 }
fed9c9a2
GH
790 break;
791 }
792 prev = this;
793 this = SCM_CDR (this);
794 }
deca31e1 795
deca31e1
GH
796 return SCM_UNSPECIFIED;
797}
1bbd0b84 798#undef FUNC_NAME
0f2d19dd 799
deca31e1
GH
800/* Recover the read-hash procedure corresponding to char c. */
801static SCM
6e8d25a6 802scm_get_hash_procedure (int c)
deca31e1 803{
14de3b42 804 SCM rest = *scm_read_hash_procedures;
fed9c9a2 805
deca31e1
GH
806 while (1)
807 {
808 if (SCM_NULLP (rest))
809 return SCM_BOOL_F;
810
7866a09b 811 if (SCM_CHAR (SCM_CAAR (rest)) == c)
deca31e1
GH
812 return SCM_CDAR (rest);
813
814 rest = SCM_CDR (rest);
815 }
816}
1cc91f1b 817
0f2d19dd
JB
818void
819scm_init_read ()
0f2d19dd 820{
14de3b42
GH
821 scm_read_hash_procedures =
822 SCM_CDRLOC (scm_sysintern ("read-hash-procedures", SCM_EOL));
fed9c9a2 823
b7ff98dd 824 scm_init_opts (scm_read_options, scm_read_opts, SCM_N_READ_OPTIONS);
8dc9439f 825#ifndef SCM_MAGIC_SNARFER
a0599745 826#include "libguile/read.x"
8dc9439f 827#endif
0f2d19dd 828}
89e00824
ML
829
830/*
831 Local Variables:
832 c-file-style: "gnu"
833 End:
834*/