Corrected "Brat" to "Brad". Sorry.
[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
GB
81 (SCM setting),
82"")
83#define FUNC_NAME s_scm_read_options
a16f6fe7 84{
b7ff98dd
MD
85 SCM ans = scm_options (setting,
86 scm_read_opts,
87 SCM_N_READ_OPTIONS,
1bbd0b84 88 FUNC_NAME);
b7ff98dd
MD
89 if (SCM_COPY_SOURCE_P)
90 SCM_RECORD_POSITIONS_P = 1;
a16f6fe7
MD
91 return ans;
92}
1bbd0b84 93#undef FUNC_NAME
a16f6fe7 94
14de3b42
GH
95/* An association list mapping extra hash characters to procedures. */
96static SCM *scm_read_hash_procedures;
deca31e1 97
a1ec6916 98SCM_DEFINE (scm_read, "read", 0, 1, 0,
1bbd0b84
GB
99 (SCM port),
100"")
101#define FUNC_NAME s_scm_read
0f2d19dd
JB
102{
103 int c;
09a4f039 104 SCM tok_buf, copy;
0f2d19dd
JB
105
106 if (SCM_UNBNDP (port))
107 port = scm_cur_inp;
3b3b36dd 108 SCM_VALIDATE_OPINPORT (1,port);
0f2d19dd 109
0f2d19dd
JB
110 c = scm_flush_ws (port, (char *) NULL);
111 if (EOF == c)
112 return SCM_EOF_VAL;
b7f3516f 113 scm_ungetc (c, port);
0f2d19dd
JB
114
115 tok_buf = scm_makstr (30L, 0);
deca31e1 116 return scm_lreadr (&tok_buf, port, &copy);
0f2d19dd 117}
1bbd0b84 118#undef FUNC_NAME
0f2d19dd
JB
119
120
1cc91f1b 121
0f2d19dd 122char *
6e8d25a6 123scm_grow_tok_buf (SCM *tok_buf)
0f2d19dd 124{
94115ae3
DH
125 unsigned long int oldlen = SCM_STRING_LENGTH (*tok_buf);
126 SCM newstr = scm_makstr (2 * oldlen, 0);
127 unsigned long int i;
128
129 for (i = 0; i != oldlen; ++i)
130 SCM_STRING_CHARS (newstr) [i] = SCM_STRING_CHARS (*tok_buf) [i];
131
132 *tok_buf = newstr;
133 return SCM_STRING_CHARS (newstr);
0f2d19dd
JB
134}
135
136
1cc91f1b 137
0f2d19dd 138int
6e8d25a6 139scm_flush_ws (SCM port, const char *eoferr)
0f2d19dd
JB
140{
141 register int c;
142 while (1)
b7f3516f 143 switch (c = scm_getc (port))
0f2d19dd
JB
144 {
145 case EOF:
146 goteof:
147 if (eoferr)
d156d3b7
MV
148 {
149 if (SCM_FILENAME (port) != SCM_BOOL_F)
150 scm_misc_error (eoferr,
151 "end of file in ~A",
152 SCM_LIST1 (SCM_FILENAME (port)));
153 else
154 scm_misc_error (eoferr, "end of file", SCM_EOL);
155 }
0f2d19dd
JB
156 return c;
157 case ';':
158 lp:
b7f3516f 159 switch (c = scm_getc (port))
0f2d19dd
JB
160 {
161 case EOF:
162 goto goteof;
163 default:
164 goto lp;
165 case SCM_LINE_INCREMENTORS:
166 break;
167 }
168 break;
169 case SCM_LINE_INCREMENTORS:
0f2d19dd 170 case SCM_SINGLE_SPACES:
0f2d19dd 171 case '\t':
0f2d19dd
JB
172 break;
173 default:
174 return c;
175 }
176}
177
178
1cc91f1b 179
0f2d19dd 180int
6e8d25a6 181scm_casei_streq (char *s1, char *s2)
0f2d19dd
JB
182{
183 while (*s1 && *s2)
184 if (scm_downcase((int)*s1) != scm_downcase((int)*s2))
185 return 0;
186 else
187 {
188 ++s1;
189 ++s2;
190 }
191 return !(*s1 || *s2);
192}
193
194
09a4f039
MD
195/* recsexpr is used when recording expressions
196 * constructed by read:sharp.
197 */
604d4dd9
JB
198#ifndef DEBUG_EXTENSIONS
199#define recsexpr(obj, line, column, filename) (obj)
200#else
09a4f039 201static SCM
1bbd0b84 202recsexpr (SCM obj,int line,int column,SCM filename)
09a4f039 203{
fee7ef83 204 if (!SCM_CONSP(obj)) {
09a4f039 205 return obj;
fee7ef83 206 } else {
09a4f039
MD
207 SCM tmp = obj, copy;
208 /* If this sexpr is visible in the read:sharp source, we want to
209 keep that information, so only record non-constant cons cells
210 which haven't previously been read by the reader. */
211 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash, obj)))
212 {
213 if (SCM_COPY_SOURCE_P)
214 {
215 copy = scm_cons (recsexpr (SCM_CAR (obj), line, column, filename),
216 SCM_UNDEFINED);
0c95b57d 217 while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
a6c64c3c
MD
218 {
219 SCM_SETCDR (copy, scm_cons (recsexpr (SCM_CAR (tmp),
220 line,
221 column,
222 filename),
223 SCM_UNDEFINED));
224 copy = SCM_CDR (copy);
225 }
226 SCM_SETCDR (copy, tmp);
09a4f039
MD
227 }
228 else
229 {
230 recsexpr (SCM_CAR (obj), line, column, filename);
0c95b57d 231 while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
09a4f039
MD
232 recsexpr (SCM_CAR (tmp), line, column, filename);
233 copy = SCM_UNDEFINED;
234 }
235 scm_whash_insert (scm_source_whash,
236 obj,
237 scm_make_srcprops (line,
238 column,
239 filename,
240 copy,
241 SCM_EOL));
242 }
243 return obj;
244 }
245}
604d4dd9 246#endif
f9c68a47
JB
247
248/* Consume an SCSH-style block comment. Assume that we've already
f9731264
JB
249 read the initial `#!', and eat characters until we get a
250 newline/exclamation-point/sharp-sign/newline sequence. */
f9c68a47
JB
251
252static void
6e8d25a6 253skip_scsh_block_comment (SCM port)
f9c68a47 254{
f9731264
JB
255 /* Is this portable? Dear God, spare me from the non-eight-bit
256 characters. But is it tasteful? */
257 long history = 0;
f9c68a47
JB
258
259 for (;;)
260 {
b7f3516f 261 int c = scm_getc (port);
f9c68a47
JB
262
263 if (c == EOF)
264 scm_wta (SCM_UNDEFINED,
265 "unterminated `#! ... !#' comment", "read");
f9731264 266 history = ((history << 8) | (c & 0xff)) & 0xffffffff;
f9c68a47 267
f9731264
JB
268 /* Were the last four characters read "\n!#\n"? */
269 if (history == (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
270 return;
f9c68a47
JB
271 }
272}
273
1bbd0b84 274static SCM scm_get_hash_procedure(int c);
f9c68a47 275
09a4f039 276static char s_list[]="list";
1cc91f1b 277
0f2d19dd 278SCM
1bbd0b84 279scm_lreadr (SCM *tok_buf,SCM port,SCM *copy)
0f2d19dd
JB
280{
281 int c;
282 scm_sizet j;
283 SCM p;
deca31e1 284
0f2d19dd 285tryagain:
1bbd0b84 286 c = scm_flush_ws (port, s_scm_read);
b6356af7 287tryagain_no_flush_ws:
0f2d19dd
JB
288 switch (c)
289 {
290 case EOF:
291 return SCM_EOF_VAL;
292
293 case '(':
09a4f039 294 return SCM_RECORD_POSITIONS_P
deca31e1
GH
295 ? scm_lreadrecparen (tok_buf, port, s_list, copy)
296 : scm_lreadparen (tok_buf, port, s_list, copy);
0f2d19dd
JB
297 case ')':
298 scm_wta (SCM_UNDEFINED, "unexpected \")\"", "read");
299 goto tryagain;
300
301 case '\'':
92e5aa0e 302 p = scm_sym_quote;
09a4f039 303 goto recquote;
0f2d19dd 304 case '`':
92e5aa0e 305 p = scm_sym_quasiquote;
09a4f039 306 goto recquote;
0f2d19dd 307 case ',':
b7f3516f 308 c = scm_getc (port);
0f2d19dd 309 if ('@' == c)
92e5aa0e 310 p = scm_sym_uq_splicing;
0f2d19dd
JB
311 else
312 {
b7f3516f 313 scm_ungetc (c, port);
92e5aa0e 314 p = scm_sym_unquote;
0f2d19dd 315 }
09a4f039
MD
316 recquote:
317 p = scm_cons2 (p,
deca31e1 318 scm_lreadr (tok_buf, port, copy),
09a4f039
MD
319 SCM_EOL);
320 if (SCM_RECORD_POSITIONS_P)
321 scm_whash_insert (scm_source_whash,
322 p,
323 scm_make_srcprops (SCM_LINUM (port),
324 SCM_COL (port) - 1,
325 SCM_FILENAME (port),
326 SCM_COPY_SOURCE_P
327 ? (*copy = scm_cons2 (SCM_CAR (p),
328 SCM_CAR (SCM_CDR (p)),
329 SCM_EOL))
330 : SCM_UNDEFINED,
331 SCM_EOL));
332 return p;
0f2d19dd 333 case '#':
b7f3516f 334 c = scm_getc (port);
0f2d19dd
JB
335 switch (c)
336 {
337 case '(':
deca31e1 338 p = scm_lreadparen (tok_buf, port, "vector", copy);
0f2d19dd
JB
339 return SCM_NULLP (p) ? scm_nullvect : scm_vector (p);
340
341 case 't':
342 case 'T':
343 return SCM_BOOL_T;
344 case 'f':
345 case 'F':
346 return SCM_BOOL_F;
347
348 case 'b':
349 case 'B':
350 case 'o':
351 case 'O':
352 case 'd':
353 case 'D':
354 case 'x':
355 case 'X':
356 case 'i':
357 case 'I':
358 case 'e':
359 case 'E':
b7f3516f 360 scm_ungetc (c, port);
0f2d19dd
JB
361 c = '#';
362 goto num;
363
f9c68a47
JB
364 case '!':
365 /* start of a shell script. Parse as a block comment,
366 terminated by !#, just like SCSH. */
367 skip_scsh_block_comment (port);
b6356af7
MV
368 /* EOF is not an error here */
369 c = scm_flush_ws (port, (char *)NULL);
370 goto tryagain_no_flush_ws;
f9c68a47 371
afe5177e 372#ifdef HAVE_ARRAYS
0f2d19dd 373 case '*':
deca31e1 374 j = scm_read_token (c, tok_buf, port, 0);
405aaef9 375 p = scm_istr2bve (SCM_STRING_CHARS (*tok_buf) + 1, (long) (j - 1));
0f2d19dd
JB
376 if (SCM_NFALSEP (p))
377 return p;
378 else
379 goto unkshrp;
afe5177e 380#endif
0f2d19dd
JB
381
382 case '{':
deca31e1 383 j = scm_read_token (c, tok_buf, port, 1);
405aaef9 384 p = scm_intern (SCM_STRING_CHARS (*tok_buf), j);
0f2d19dd
JB
385 return SCM_CAR (p);
386
387 case '\\':
b7f3516f 388 c = scm_getc (port);
deca31e1 389 j = scm_read_token (c, tok_buf, port, 0);
0f2d19dd 390 if (j == 1)
7866a09b 391 return SCM_MAKE_CHAR (c);
0f2d19dd
JB
392 if (c >= '0' && c < '8')
393 {
405aaef9 394 p = scm_istr2int (SCM_STRING_CHARS (*tok_buf), (long) j, 8);
0f2d19dd 395 if (SCM_NFALSEP (p))
7866a09b 396 return SCM_MAKE_CHAR (SCM_INUM (p));
0f2d19dd
JB
397 }
398 for (c = 0; c < scm_n_charnames; c++)
399 if (scm_charnames[c]
405aaef9 400 && (scm_casei_streq (scm_charnames[c], SCM_STRING_CHARS (*tok_buf))))
7866a09b 401 return SCM_MAKE_CHAR (scm_charnums[c]);
405aaef9 402 scm_wta (SCM_UNDEFINED, "unknown # object: #\\", SCM_STRING_CHARS (*tok_buf));
0f2d19dd 403
50a095f1
JB
404 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
405 case ':':
406 j = scm_read_token ('-', tok_buf, port, 0);
405aaef9 407 p = scm_intern (SCM_STRING_CHARS (*tok_buf), j);
50a095f1 408 return scm_make_keyword_from_dash_symbol (SCM_CAR (p));
0f2d19dd
JB
409
410 default:
411 callshrp:
deca31e1
GH
412 {
413 SCM sharp = scm_get_hash_procedure (c);
414
415 if (SCM_NIMP (sharp))
416 {
417 int line = SCM_LINUM (port);
418 int column = SCM_COL (port) - 2;
419 SCM got;
420
421 got = scm_apply (sharp,
7866a09b 422 SCM_MAKE_CHAR (c),
deca31e1 423 scm_acons (port, SCM_EOL, SCM_EOL));
54778cd3 424 if (SCM_EQ_P (got, SCM_UNSPECIFIED))
deca31e1
GH
425 goto unkshrp;
426 if (SCM_RECORD_POSITIONS_P)
427 return *copy = recsexpr (got, line, column,
428 SCM_FILENAME (port));
429 else
430 return got;
431 }
432 }
03bc4386 433 unkshrp:
70d63753 434 scm_misc_error (s_scm_read, "Unknown # object: ~S",
7866a09b 435 scm_listify (SCM_MAKE_CHAR (c), SCM_UNDEFINED));
0f2d19dd
JB
436 }
437
438 case '"':
439 j = 0;
b7f3516f 440 while ('"' != (c = scm_getc (port)))
0f2d19dd
JB
441 {
442 SCM_ASSERT (EOF != c, SCM_UNDEFINED, "end of file in ", "string");
443
94115ae3 444 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
0f2d19dd
JB
445 scm_grow_tok_buf (tok_buf);
446
447 if (c == '\\')
b7f3516f 448 switch (c = scm_getc (port))
0f2d19dd
JB
449 {
450 case '\n':
451 continue;
452 case '0':
453 c = '\0';
454 break;
455 case 'f':
456 c = '\f';
457 break;
458 case 'n':
459 c = '\n';
460 break;
461 case 'r':
462 c = '\r';
463 break;
464 case 't':
465 c = '\t';
466 break;
467 case 'a':
468 c = '\007';
469 break;
470 case 'v':
471 c = '\v';
472 break;
473 }
405aaef9 474 SCM_STRING_CHARS (*tok_buf)[j] = c;
b7f3516f 475 ++j;
0f2d19dd
JB
476 }
477 if (j == 0)
478 return scm_nullstr;
405aaef9 479 SCM_STRING_CHARS (*tok_buf)[j] = 0;
0f2d19dd
JB
480 {
481 SCM str;
405aaef9 482 str = scm_makfromstr (SCM_STRING_CHARS (*tok_buf), j, 0);
0f2d19dd
JB
483 return str;
484 }
485
486 case'0':case '1':case '2':case '3':case '4':
487 case '5':case '6':case '7':case '8':case '9':
488 case '.':
489 case '-':
490 case '+':
491 num:
deca31e1 492 j = scm_read_token (c, tok_buf, port, 0);
405aaef9 493 p = scm_istring2number (SCM_STRING_CHARS (*tok_buf), (long) j, 10L);
0f2d19dd
JB
494 if (SCM_NFALSEP (p))
495 return p;
496 if (c == '#')
497 {
b7f3516f 498 if ((j == 2) && (scm_getc (port) == '('))
0f2d19dd 499 {
b7f3516f 500 scm_ungetc ('(', port);
405aaef9 501 c = SCM_STRING_CHARS (*tok_buf)[1];
0f2d19dd
JB
502 goto callshrp;
503 }
405aaef9 504 scm_wta (SCM_UNDEFINED, "unknown # object", SCM_STRING_CHARS (*tok_buf));
0f2d19dd
JB
505 }
506 goto tok;
507
508 case ':':
fee7ef83 509 if (SCM_EQ_P (SCM_PACK (SCM_KEYWORD_STYLE), scm_keyword_prefix))
c7733771
GH
510 {
511 j = scm_read_token ('-', tok_buf, port, 0);
405aaef9 512 p = scm_intern (SCM_STRING_CHARS (*tok_buf), j);
c7733771
GH
513 return scm_make_keyword_from_dash_symbol (SCM_CAR (p));
514 }
515 /* fallthrough */
0f2d19dd 516 default:
deca31e1 517 j = scm_read_token (c, tok_buf, port, 0);
0f2d19dd
JB
518 /* fallthrough */
519
520 tok:
405aaef9 521 p = scm_intern (SCM_STRING_CHARS (*tok_buf), j);
0f2d19dd
JB
522 return SCM_CAR (p);
523 }
524}
525
526#ifdef _UNICOS
527_Pragma ("noopt"); /* # pragma _CRI noopt */
528#endif
1cc91f1b 529
0f2d19dd 530scm_sizet
6e8d25a6 531scm_read_token (int ic, SCM *tok_buf, SCM port, int weird)
0f2d19dd
JB
532{
533 register scm_sizet j;
534 register int c;
535 register char *p;
536
deca31e1 537 c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(ic) : ic);
405aaef9 538 p = SCM_STRING_CHARS (*tok_buf);
0f2d19dd
JB
539
540 if (weird)
541 j = 0;
542 else
543 {
544 j = 0;
94115ae3 545 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
0f2d19dd 546 p = scm_grow_tok_buf (tok_buf);
b7f3516f
TT
547 p[j] = c;
548 ++j;
0f2d19dd
JB
549 }
550
551 while (1)
552 {
94115ae3 553 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
0f2d19dd 554 p = scm_grow_tok_buf (tok_buf);
b7f3516f 555 c = scm_getc (port);
0f2d19dd
JB
556 switch (c)
557 {
558 case '(':
559 case ')':
560 case '"':
561 case ';':
562 case SCM_WHITE_SPACES:
563 case SCM_LINE_INCREMENTORS:
564 if (weird)
565 goto default_case;
566
b7f3516f 567 scm_ungetc (c, port);
0f2d19dd
JB
568 case EOF:
569 eof_case:
570 p[j] = 0;
571 return j;
572 case '\\':
573 if (!weird)
574 goto default_case;
575 else
576 {
b7f3516f 577 c = scm_getc (port);
0f2d19dd
JB
578 if (c == EOF)
579 goto eof_case;
580 else
581 goto default_case;
582 }
583 case '}':
584 if (!weird)
585 goto default_case;
586
b7f3516f 587 c = scm_getc (port);
0f2d19dd
JB
588 if (c == '#')
589 {
590 p[j] = 0;
591 return j;
592 }
593 else
594 {
b7f3516f 595 scm_ungetc (c, port);
0f2d19dd
JB
596 c = '}';
597 goto default_case;
598 }
599
600 default:
601 default_case:
602 {
deca31e1 603 c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(c) : c);
b7f3516f
TT
604 p[j] = c;
605 ++j;
0f2d19dd
JB
606 }
607
608 }
609 }
610}
1cc91f1b 611
0f2d19dd
JB
612#ifdef _UNICOS
613_Pragma ("opt"); /* # pragma _CRI opt */
614#endif
615
0f2d19dd 616SCM
6e8d25a6 617scm_lreadparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
0f2d19dd
JB
618{
619 SCM tmp;
620 SCM tl;
621 SCM ans;
622 int c;
623
624 c = scm_flush_ws (port, name);
625 if (')' == c)
626 return SCM_EOL;
b7f3516f 627 scm_ungetc (c, port);
54778cd3 628 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
0f2d19dd 629 {
deca31e1 630 ans = scm_lreadr (tok_buf, port, copy);
0f2d19dd
JB
631 closeit:
632 if (')' != (c = scm_flush_ws (port, name)))
633 scm_wta (SCM_UNDEFINED, "missing close paren", "");
634 return ans;
635 }
636 ans = tl = scm_cons (tmp, SCM_EOL);
637 while (')' != (c = scm_flush_ws (port, name)))
638 {
b7f3516f 639 scm_ungetc (c, port);
54778cd3 640 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
0f2d19dd 641 {
deca31e1 642 SCM_SETCDR (tl, scm_lreadr (tok_buf, port, copy));
0f2d19dd
JB
643 goto closeit;
644 }
a6c64c3c
MD
645 SCM_SETCDR (tl, scm_cons (tmp, SCM_EOL));
646 tl = SCM_CDR (tl);
0f2d19dd
JB
647 }
648 return ans;
649}
650
1cc91f1b 651
09a4f039 652SCM
6e8d25a6 653scm_lreadrecparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
09a4f039
MD
654{
655 register int c;
656 register SCM tmp;
4dc2435a
JB
657 register SCM tl, tl2 = SCM_EOL;
658 SCM ans, ans2 = SCM_EOL;
09a4f039
MD
659 /* Need to capture line and column numbers here. */
660 int line = SCM_LINUM (port);
661 int column = SCM_COL (port) - 1;
662
663 c = scm_flush_ws (port, name);
664 if (')' == c)
665 return SCM_EOL;
b7f3516f 666 scm_ungetc (c, port);
54778cd3 667 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
09a4f039 668 {
deca31e1 669 ans = scm_lreadr (tok_buf, port, copy);
09a4f039
MD
670 if (')' != (c = scm_flush_ws (port, name)))
671 scm_wta (SCM_UNDEFINED, "missing close paren", "");
672 return ans;
673 }
674 /* Build the head of the list structure. */
675 ans = tl = scm_cons (tmp, SCM_EOL);
676 if (SCM_COPY_SOURCE_P)
0c95b57d 677 ans2 = tl2 = scm_cons (SCM_CONSP (tmp)
09a4f039
MD
678 ? *copy
679 : tmp,
680 SCM_EOL);
681 while (')' != (c = scm_flush_ws (port, name)))
682 {
62850ef3
DH
683 SCM new_tail;
684
b7f3516f 685 scm_ungetc (c, port);
54778cd3 686 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
09a4f039 687 {
deca31e1 688 SCM_SETCDR (tl, tmp = scm_lreadr (tok_buf, port, copy));
09a4f039 689 if (SCM_COPY_SOURCE_P)
0c95b57d 690 SCM_SETCDR (tl2, scm_cons (SCM_CONSP (tmp)
09a4f039
MD
691 ? *copy
692 : tmp,
693 SCM_EOL));
694 if (')' != (c = scm_flush_ws (port, name)))
695 scm_wta (SCM_UNDEFINED, "missing close paren", "");
696 goto exit;
697 }
62850ef3
DH
698
699 new_tail = scm_cons (tmp, SCM_EOL);
700 SCM_SETCDR (tl, new_tail);
701 tl = new_tail;
702
09a4f039 703 if (SCM_COPY_SOURCE_P)
62850ef3
DH
704 {
705 SCM new_tail2 = scm_cons (SCM_CONSP (tmp) ? *copy : tmp, SCM_EOL);
706 SCM_SETCDR (tl2, new_tail2);
707 tl2 = new_tail2;
708 }
09a4f039
MD
709 }
710exit:
711 scm_whash_insert (scm_source_whash,
712 ans,
713 scm_make_srcprops (line,
714 column,
715 SCM_FILENAME (port),
716 SCM_COPY_SOURCE_P
717 ? *copy = ans2
718 : SCM_UNDEFINED,
719 SCM_EOL));
720 return ans;
721}
722
0f2d19dd
JB
723
724\f
725
14de3b42
GH
726/* Manipulate the read-hash-procedures alist. This could be written in
727 Scheme, but maybe it will also be used by C code during initialisation. */
a1ec6916 728SCM_DEFINE (scm_read_hash_extend, "read-hash-extend", 2, 0, 0,
1bbd0b84
GB
729 (SCM chr, SCM proc),
730"")
731#define FUNC_NAME s_scm_read_hash_extend
deca31e1 732{
fed9c9a2
GH
733 SCM this;
734 SCM prev;
735
7866a09b 736 SCM_VALIDATE_CHAR (1,chr);
fed9c9a2 737 SCM_ASSERT (SCM_FALSEP (proc) || SCM_NIMP(proc), proc, SCM_ARG2,
1bbd0b84 738 FUNC_NAME);
fed9c9a2 739
14de3b42
GH
740 /* Check if chr is already in the alist. */
741 this = *scm_read_hash_procedures;
742 prev = SCM_BOOL_F;
fed9c9a2
GH
743 while (1)
744 {
745 if (SCM_NULLP (this))
746 {
747 /* not found, so add it to the beginning. */
748 if (SCM_NFALSEP (proc))
749 {
14de3b42
GH
750 *scm_read_hash_procedures =
751 scm_cons (scm_cons (chr, proc), *scm_read_hash_procedures);
fed9c9a2
GH
752 }
753 break;
754 }
54778cd3 755 if (SCM_EQ_P (chr, SCM_CAAR (this)))
fed9c9a2
GH
756 {
757 /* already in the alist. */
758 if (SCM_FALSEP (proc))
14de3b42
GH
759 {
760 /* remove it. */
54778cd3 761 if (SCM_FALSEP (prev))
14de3b42
GH
762 {
763 *scm_read_hash_procedures =
764 SCM_CDR (*scm_read_hash_procedures);
765 }
766 else
767 scm_set_cdr_x (prev, SCM_CDR (this));
768 }
fed9c9a2 769 else
14de3b42
GH
770 {
771 /* replace it. */
772 scm_set_cdr_x (SCM_CAR (this), proc);
773 }
fed9c9a2
GH
774 break;
775 }
776 prev = this;
777 this = SCM_CDR (this);
778 }
deca31e1 779
deca31e1
GH
780 return SCM_UNSPECIFIED;
781}
1bbd0b84 782#undef FUNC_NAME
0f2d19dd 783
deca31e1
GH
784/* Recover the read-hash procedure corresponding to char c. */
785static SCM
6e8d25a6 786scm_get_hash_procedure (int c)
deca31e1 787{
14de3b42 788 SCM rest = *scm_read_hash_procedures;
fed9c9a2 789
deca31e1
GH
790 while (1)
791 {
792 if (SCM_NULLP (rest))
793 return SCM_BOOL_F;
794
7866a09b 795 if (SCM_CHAR (SCM_CAAR (rest)) == c)
deca31e1
GH
796 return SCM_CDAR (rest);
797
798 rest = SCM_CDR (rest);
799 }
800}
1cc91f1b 801
0f2d19dd
JB
802void
803scm_init_read ()
0f2d19dd 804{
14de3b42
GH
805 scm_read_hash_procedures =
806 SCM_CDRLOC (scm_sysintern ("read-hash-procedures", SCM_EOL));
fed9c9a2 807
b7ff98dd 808 scm_init_opts (scm_read_options, scm_read_opts, SCM_N_READ_OPTIONS);
a0599745 809#include "libguile/read.x"
0f2d19dd 810}
89e00824
ML
811
812/*
813 Local Variables:
814 c-file-style: "gnu"
815 End:
816*/