(scm_lreadr): Bugfix: include the last bit in the bit vector.
[bpt/guile.git] / libguile / read.c
1 /* Copyright (C) 1995,1996,1997,1999,2000,2001,2003, 2004 Free Software
2 * Foundation, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20 \f
21
22 #include <stdio.h>
23 #include "libguile/_scm.h"
24 #include "libguile/chars.h"
25 #include "libguile/eval.h"
26 #include "libguile/unif.h"
27 #include "libguile/keywords.h"
28 #include "libguile/alist.h"
29 #include "libguile/srcprop.h"
30 #include "libguile/hashtab.h"
31 #include "libguile/hash.h"
32 #include "libguile/ports.h"
33 #include "libguile/root.h"
34 #include "libguile/strings.h"
35 #include "libguile/strports.h"
36 #include "libguile/vectors.h"
37 #include "libguile/validate.h"
38 #include "libguile/srfi-4.h"
39
40 #include "libguile/read.h"
41
42 \f
43
44 SCM_GLOBAL_SYMBOL (scm_sym_dot, ".");
45 SCM_SYMBOL (scm_keyword_prefix, "prefix");
46
47 scm_t_option scm_read_opts[] = {
48 { SCM_OPTION_BOOLEAN, "copy", 0,
49 "Copy source code expressions." },
50 { SCM_OPTION_BOOLEAN, "positions", 0,
51 "Record positions of source code expressions." },
52 { SCM_OPTION_BOOLEAN, "case-insensitive", 0,
53 "Convert symbols to lower case."},
54 { SCM_OPTION_SCM, "keywords", SCM_UNPACK (SCM_BOOL_F),
55 "Style of keyword recognition: #f or 'prefix."}
56 #if SCM_ENABLE_ELISP
57 ,
58 { SCM_OPTION_BOOLEAN, "elisp-vectors", 0,
59 "Support Elisp vector syntax, namely `[...]'."},
60 { SCM_OPTION_BOOLEAN, "elisp-strings", 0,
61 "Support `\\(' and `\\)' in strings."}
62 #endif
63 };
64
65 /*
66 Give meaningful error messages for errors
67
68 We use the format
69
70 FILE:LINE:COL: MESSAGE
71 This happened in ....
72
73 This is not standard GNU format, but the test-suite likes the real
74 message to be in front.
75
76 */
77
78
79 void
80 scm_i_input_error (char const *function,
81 SCM port, const char *message, SCM arg)
82 {
83 SCM fn = (scm_is_string (SCM_FILENAME(port))
84 ? SCM_FILENAME(port)
85 : scm_from_locale_string ("#<unknown port>"));
86
87 SCM string_port = scm_open_output_string ();
88 SCM string = SCM_EOL;
89 scm_simple_format (string_port,
90 scm_from_locale_string ("~A:~S:~S: ~A"),
91 scm_list_4 (fn,
92 scm_from_int (SCM_LINUM (port) + 1),
93 scm_from_int (SCM_COL (port) + 1),
94 scm_from_locale_string (message)));
95
96 string = scm_get_output_string (string_port);
97 scm_close_output_port (string_port);
98 scm_error_scm (scm_from_locale_symbol ("read-error"),
99 function? scm_from_locale_string (function) : SCM_BOOL_F,
100 string,
101 arg,
102 SCM_BOOL_F);
103 }
104
105
106 SCM_DEFINE (scm_read_options, "read-options-interface", 0, 1, 0,
107 (SCM setting),
108 "Option interface for the read options. Instead of using\n"
109 "this procedure directly, use the procedures @code{read-enable},\n"
110 "@code{read-disable}, @code{read-set!} and @code{read-options}.")
111 #define FUNC_NAME s_scm_read_options
112 {
113 SCM ans = scm_options (setting,
114 scm_read_opts,
115 SCM_N_READ_OPTIONS,
116 FUNC_NAME);
117 if (SCM_COPY_SOURCE_P)
118 SCM_RECORD_POSITIONS_P = 1;
119 return ans;
120 }
121 #undef FUNC_NAME
122
123 /* An association list mapping extra hash characters to procedures. */
124 static SCM *scm_read_hash_procedures;
125
126 SCM_DEFINE (scm_read, "read", 0, 1, 0,
127 (SCM port),
128 "Read an s-expression from the input port @var{port}, or from\n"
129 "the current input port if @var{port} is not specified.\n"
130 "Any whitespace before the next token is discarded.")
131 #define FUNC_NAME s_scm_read
132 {
133 int c;
134 SCM tok_buf, copy;
135
136 if (SCM_UNBNDP (port))
137 port = scm_cur_inp;
138 SCM_VALIDATE_OPINPORT (1, port);
139
140 c = scm_flush_ws (port, (char *) NULL);
141 if (EOF == c)
142 return SCM_EOF_VAL;
143 scm_ungetc (c, port);
144
145 tok_buf = scm_c_make_string (30, SCM_UNDEFINED);
146 return scm_lreadr (&tok_buf, port, &copy);
147 }
148 #undef FUNC_NAME
149
150
151
152 char *
153 scm_grow_tok_buf (SCM *tok_buf)
154 {
155 size_t oldlen = scm_i_string_length (*tok_buf);
156 const char *olddata = scm_i_string_chars (*tok_buf);
157 char *newdata;
158 SCM newstr = scm_i_make_string (2 * oldlen, &newdata);
159 size_t i;
160
161 for (i = 0; i != oldlen; ++i)
162 newdata[i] = olddata[i];
163
164 *tok_buf = newstr;
165 return newdata;
166 }
167
168 /* Consume an SCSH-style block comment. Assume that we've already
169 read the initial `#!', and eat characters until we get a
170 exclamation-point/sharp-sign sequence.
171 */
172
173 static void
174 skip_scsh_block_comment (SCM port)
175 {
176 int bang_seen = 0;
177
178 for (;;)
179 {
180 int c = scm_getc (port);
181
182 if (c == EOF)
183 scm_i_input_error ("skip_block_comment", port,
184 "unterminated `#! ... !#' comment", SCM_EOL);
185
186 if (c == '!')
187 bang_seen = 1;
188 else if (c == '#' && bang_seen)
189 return;
190 else
191 bang_seen = 0;
192 }
193 }
194
195 int
196 scm_flush_ws (SCM port, const char *eoferr)
197 {
198 register int c;
199 while (1)
200 switch (c = scm_getc (port))
201 {
202 case EOF:
203 goteof:
204 if (eoferr)
205 {
206 scm_i_input_error (eoferr,
207 port,
208 "end of file",
209 SCM_EOL);
210 }
211 return c;
212 case ';':
213 lp:
214 switch (c = scm_getc (port))
215 {
216 case EOF:
217 goto goteof;
218 default:
219 goto lp;
220 case SCM_LINE_INCREMENTORS:
221 break;
222 }
223 break;
224 case '#':
225 switch (c = scm_getc (port))
226 {
227 case EOF:
228 eoferr = "read_sharp";
229 goto goteof;
230 case '!':
231 skip_scsh_block_comment (port);
232 break;
233 default:
234 scm_ungetc (c, port);
235 return '#';
236 }
237 break;
238 case SCM_LINE_INCREMENTORS:
239 case SCM_SINGLE_SPACES:
240 case '\t':
241 break;
242 default:
243 return c;
244 }
245 }
246
247
248
249 int
250 scm_casei_streq (char *s1, char *s2)
251 {
252 while (*s1 && *s2)
253 if (scm_c_downcase((int)*s1) != scm_c_downcase((int)*s2))
254 return 0;
255 else
256 {
257 ++s1;
258 ++s2;
259 }
260 return !(*s1 || *s2);
261 }
262
263 static int
264 scm_i_casei_streq (const char *s1, const char *s2, size_t len2)
265 {
266 while (*s1 && len2 > 0)
267 if (scm_c_downcase((int)*s1) != scm_c_downcase((int)*s2))
268 return 0;
269 else
270 {
271 ++s1;
272 ++s2;
273 --len2;
274 }
275 return !(*s1 || len2 > 0);
276 }
277
278 /* recsexpr is used when recording expressions
279 * constructed by read:sharp.
280 */
281 static SCM
282 recsexpr (SCM obj, long line, int column, SCM filename)
283 {
284 if (!scm_is_pair(obj)) {
285 return obj;
286 } else {
287 SCM tmp = obj, copy;
288 /* If this sexpr is visible in the read:sharp source, we want to
289 keep that information, so only record non-constant cons cells
290 which haven't previously been read by the reader. */
291 if (scm_is_false (scm_whash_lookup (scm_source_whash, obj)))
292 {
293 if (SCM_COPY_SOURCE_P)
294 {
295 copy = scm_cons (recsexpr (SCM_CAR (obj), line, column, filename),
296 SCM_UNDEFINED);
297 while ((tmp = SCM_CDR (tmp)) && scm_is_pair (tmp))
298 {
299 SCM_SETCDR (copy, scm_cons (recsexpr (SCM_CAR (tmp),
300 line,
301 column,
302 filename),
303 SCM_UNDEFINED));
304 copy = SCM_CDR (copy);
305 }
306 SCM_SETCDR (copy, tmp);
307 }
308 else
309 {
310 recsexpr (SCM_CAR (obj), line, column, filename);
311 while ((tmp = SCM_CDR (tmp)) && scm_is_pair (tmp))
312 recsexpr (SCM_CAR (tmp), line, column, filename);
313 copy = SCM_UNDEFINED;
314 }
315 scm_whash_insert (scm_source_whash,
316 obj,
317 scm_make_srcprops (line,
318 column,
319 filename,
320 copy,
321 SCM_EOL));
322 }
323 return obj;
324 }
325 }
326
327
328 static SCM scm_get_hash_procedure(int c);
329 static SCM scm_i_lreadparen (SCM *, SCM, char *, SCM *, char);
330
331 static char s_list[]="list";
332 static char s_vector[]="vector";
333
334 SCM
335 scm_lreadr (SCM *tok_buf, SCM port, SCM *copy)
336 #define FUNC_NAME "scm_lreadr"
337 {
338 int c;
339 size_t j;
340 SCM p;
341
342 tryagain:
343 c = scm_flush_ws (port, s_scm_read);
344 switch (c)
345 {
346 case EOF:
347 return SCM_EOF_VAL;
348
349 case '(':
350 return SCM_RECORD_POSITIONS_P
351 ? scm_lreadrecparen (tok_buf, port, s_list, copy)
352 : scm_i_lreadparen (tok_buf, port, s_list, copy, ')');
353 case ')':
354 scm_i_input_error (FUNC_NAME, port,"unexpected \")\"", SCM_EOL);
355 goto tryagain;
356
357 #if SCM_ENABLE_ELISP
358 case '[':
359 if (SCM_ELISP_VECTORS_P)
360 {
361 p = scm_i_lreadparen (tok_buf, port, s_vector, copy, ']');
362 return scm_is_null (p) ? scm_nullvect : scm_vector (p);
363 }
364 goto read_token;
365 #endif
366 case '\'':
367 p = scm_sym_quote;
368 goto recquote;
369 case '`':
370 p = scm_sym_quasiquote;
371 goto recquote;
372 case ',':
373 c = scm_getc (port);
374 if ('@' == c)
375 p = scm_sym_uq_splicing;
376 else
377 {
378 scm_ungetc (c, port);
379 p = scm_sym_unquote;
380 }
381 recquote:
382 p = scm_cons2 (p,
383 scm_lreadr (tok_buf, port, copy),
384 SCM_EOL);
385 if (SCM_RECORD_POSITIONS_P)
386 scm_whash_insert (scm_source_whash,
387 p,
388 scm_make_srcprops (SCM_LINUM (port),
389 SCM_COL (port) - 1,
390 SCM_FILENAME (port),
391 SCM_COPY_SOURCE_P
392 ? (*copy = scm_cons2 (SCM_CAR (p),
393 SCM_CAR (SCM_CDR (p)),
394 SCM_EOL))
395 : SCM_UNDEFINED,
396 SCM_EOL));
397 return p;
398 case '#':
399 c = scm_getc (port);
400
401 {
402 /* Check for user-defined hash procedure first, to allow
403 overriding of builtin hash read syntaxes. */
404 SCM sharp = scm_get_hash_procedure (c);
405 if (scm_is_true (sharp))
406 {
407 int line = SCM_LINUM (port);
408 int column = SCM_COL (port) - 2;
409 SCM got;
410
411 got = scm_call_2 (sharp, SCM_MAKE_CHAR (c), port);
412 if (scm_is_eq (got, SCM_UNSPECIFIED))
413 goto handle_sharp;
414 if (SCM_RECORD_POSITIONS_P)
415 return *copy = recsexpr (got, line, column,
416 SCM_FILENAME (port));
417 else
418 return got;
419 }
420 }
421 handle_sharp:
422 switch (c)
423 {
424 /* Vector, arrays, both uniform and not are handled by this
425 one function. It also disambiguates between '#f' and
426 '#f32' and '#f64'.
427 */
428 case '0': case '1': case '2': case '3': case '4':
429 case '5': case '6': case '7': case '8': case '9':
430 case 'u': case 's': case 'f':
431 case '@':
432 case '(':
433 #if SCM_ENABLE_DEPRECATED
434 /* See below for 'i' and 'e'. */
435 case 'a':
436 case 'c':
437 case 'y':
438 case 'h':
439 case 'l':
440 #endif
441 return scm_i_read_array (port, c);
442
443 case 't':
444 case 'T':
445 return SCM_BOOL_T;
446
447 case 'F':
448 /* See above for lower case 'f'. */
449 return SCM_BOOL_F;
450
451
452 case 'i':
453 case 'e':
454 #if SCM_ENABLE_DEPRECATED
455 {
456 /* When next char is '(', it really is an old-style
457 uniform array. */
458 int next_c = scm_getc (port);
459 if (next_c != EOF)
460 scm_ungetc (next_c, port);
461 if (next_c == '(')
462 return scm_i_read_array (port, c);
463 /* Fall through. */
464 }
465 #endif
466 case 'b':
467 case 'B':
468 case 'o':
469 case 'O':
470 case 'd':
471 case 'D':
472 case 'x':
473 case 'X':
474 case 'I':
475 case 'E':
476 scm_ungetc (c, port);
477 c = '#';
478 goto num;
479
480 case '!':
481 /* should never happen, #!...!# block comments are skipped
482 over in scm_flush_ws. */
483 abort ();
484
485 #if SCM_HAVE_ARRAYS
486 case '*':
487 j = scm_read_token (c, tok_buf, port, 0);
488 p = scm_istr2bve (scm_c_substring_shared (*tok_buf, 1, j));
489 if (scm_is_true (p))
490 return p;
491 else
492 goto unkshrp;
493 #endif
494
495 case '{':
496 j = scm_read_token (c, tok_buf, port, 1);
497 return scm_string_to_symbol (scm_c_substring_copy (*tok_buf, 0, j));
498
499 case '\\':
500 c = scm_getc (port);
501 j = scm_read_token (c, tok_buf, port, 0);
502 if (j == 1)
503 return SCM_MAKE_CHAR (c);
504 if (c >= '0' && c < '8')
505 {
506 /* Dirk:FIXME:: This type of character syntax is not R5RS
507 * compliant. Further, it should be verified that the constant
508 * does only consist of octal digits. Finally, it should be
509 * checked whether the resulting fixnum is in the range of
510 * characters. */
511 p = scm_i_mem2number (scm_i_string_chars (*tok_buf), j, 8);
512 if (SCM_I_INUMP (p))
513 return SCM_MAKE_CHAR (SCM_I_INUM (p));
514 }
515 for (c = 0; c < scm_n_charnames; c++)
516 if (scm_charnames[c]
517 && (scm_i_casei_streq (scm_charnames[c],
518 scm_i_string_chars (*tok_buf), j)))
519 return SCM_MAKE_CHAR (scm_charnums[c]);
520 scm_i_input_error (FUNC_NAME, port, "unknown character name ~a",
521 scm_list_1 (scm_c_substring (*tok_buf, 0, j)));
522
523 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
524 case ':':
525 return scm_symbol_to_keyword (scm_read (port));
526
527 default:
528 callshrp:
529 {
530 SCM sharp = scm_get_hash_procedure (c);
531
532 if (scm_is_true (sharp))
533 {
534 int line = SCM_LINUM (port);
535 int column = SCM_COL (port) - 2;
536 SCM got;
537
538 got = scm_call_2 (sharp, SCM_MAKE_CHAR (c), port);
539 if (scm_is_eq (got, SCM_UNSPECIFIED))
540 goto unkshrp;
541 if (SCM_RECORD_POSITIONS_P)
542 return *copy = recsexpr (got, line, column,
543 SCM_FILENAME (port));
544 else
545 return got;
546 }
547 }
548 unkshrp:
549 scm_i_input_error (FUNC_NAME, port, "Unknown # object: ~S",
550 scm_list_1 (SCM_MAKE_CHAR (c)));
551 }
552
553 case '"':
554 j = 0;
555 while ('"' != (c = scm_getc (port)))
556 {
557 if (c == EOF)
558 str_eof: scm_i_input_error (FUNC_NAME, port,
559 "end of file in string constant",
560 SCM_EOL);
561
562 while (j + 2 >= scm_i_string_length (*tok_buf))
563 scm_grow_tok_buf (tok_buf);
564
565 if (c == '\\')
566 switch (c = scm_getc (port))
567 {
568 case EOF:
569 goto str_eof;
570 case '"':
571 case '\\':
572 break;
573 #if SCM_ENABLE_ELISP
574 case '(':
575 case ')':
576 if (SCM_ESCAPED_PARENS_P)
577 break;
578 goto bad_escaped;
579 #endif
580 case '\n':
581 continue;
582 case '0':
583 c = '\0';
584 break;
585 case 'f':
586 c = '\f';
587 break;
588 case 'n':
589 c = '\n';
590 break;
591 case 'r':
592 c = '\r';
593 break;
594 case 't':
595 c = '\t';
596 break;
597 case 'a':
598 c = '\007';
599 break;
600 case 'v':
601 c = '\v';
602 break;
603 case 'x':
604 {
605 int a, b;
606 a = scm_getc (port);
607 if (a == EOF) goto str_eof;
608 b = scm_getc (port);
609 if (b == EOF) goto str_eof;
610 if ('0' <= a && a <= '9') a -= '0';
611 else if ('A' <= a && a <= 'F') a = a - 'A' + 10;
612 else if ('a' <= a && a <= 'f') a = a - 'a' + 10;
613 else goto bad_escaped;
614 if ('0' <= b && b <= '9') b -= '0';
615 else if ('A' <= b && b <= 'F') b = b - 'A' + 10;
616 else if ('a' <= b && b <= 'f') b = b - 'a' + 10;
617 else goto bad_escaped;
618 c = a * 16 + b;
619 break;
620 }
621 default:
622 bad_escaped:
623 scm_i_input_error(FUNC_NAME, port,
624 "illegal character in escape sequence: ~S",
625 scm_list_1 (SCM_MAKE_CHAR (c)));
626 }
627 scm_c_string_set_x (*tok_buf, j, SCM_MAKE_CHAR (c));
628 ++j;
629 }
630 if (j == 0)
631 return scm_nullstr;
632
633 /* Change this to scm_c_substring_read_only when
634 SCM_STRING_CHARS has been removed.
635 */
636 return scm_c_substring_copy (*tok_buf, 0, j);
637
638 case '0': case '1': case '2': case '3': case '4':
639 case '5': case '6': case '7': case '8': case '9':
640 case '.':
641 case '-':
642 case '+':
643 num:
644 j = scm_read_token (c, tok_buf, port, 0);
645 if (j == 1 && (c == '+' || c == '-'))
646 /* Shortcut: Detected symbol '+ or '- */
647 goto tok;
648
649 p = scm_i_mem2number (scm_i_string_chars (*tok_buf), j, 10);
650 if (scm_is_true (p))
651 return p;
652 if (c == '#')
653 {
654 if ((j == 2) && (scm_getc (port) == '('))
655 {
656 scm_ungetc ('(', port);
657 c = scm_i_string_chars (*tok_buf)[1];
658 goto callshrp;
659 }
660 scm_i_input_error (FUNC_NAME, port, "unknown # object", SCM_EOL);
661 }
662 goto tok;
663
664 case ':':
665 if (scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE), scm_keyword_prefix))
666 return scm_symbol_to_keyword (scm_read (port));
667
668 /* fallthrough */
669 default:
670 #if SCM_ENABLE_ELISP
671 read_token:
672 #endif
673 j = scm_read_token (c, tok_buf, port, 0);
674 /* fallthrough */
675
676 tok:
677 return scm_string_to_symbol (scm_c_substring (*tok_buf, 0, j));
678 }
679 }
680 #undef FUNC_NAME
681
682
683 #ifdef _UNICOS
684 _Pragma ("noopt"); /* # pragma _CRI noopt */
685 #endif
686
687 size_t
688 scm_read_token (int ic, SCM *tok_buf, SCM port, int weird)
689 {
690 size_t j;
691 int c;
692
693 c = (SCM_CASE_INSENSITIVE_P ? scm_c_downcase(ic) : ic);
694
695 if (weird)
696 j = 0;
697 else
698 {
699 j = 0;
700 while (j + 2 >= scm_i_string_length (*tok_buf))
701 scm_grow_tok_buf (tok_buf);
702 scm_c_string_set_x (*tok_buf, j, SCM_MAKE_CHAR (c));
703 ++j;
704 }
705
706 while (1)
707 {
708 while (j + 2 >= scm_i_string_length (*tok_buf))
709 scm_grow_tok_buf (tok_buf);
710 c = scm_getc (port);
711 switch (c)
712 {
713 case '(':
714 case ')':
715 #if SCM_ENABLE_ELISP
716 case '[':
717 case ']':
718 #endif
719 case '"':
720 case ';':
721 case SCM_WHITE_SPACES:
722 case SCM_LINE_INCREMENTORS:
723 if (weird
724 #if SCM_ENABLE_ELISP
725 || ((!SCM_ELISP_VECTORS_P) && ((c == '[') || (c == ']')))
726 #endif
727 )
728 goto default_case;
729
730 scm_ungetc (c, port);
731 case EOF:
732 eof_case:
733 return j;
734 case '\\':
735 if (!weird)
736 goto default_case;
737 else
738 {
739 c = scm_getc (port);
740 if (c == EOF)
741 goto eof_case;
742 else
743 goto default_case;
744 }
745 case '}':
746 if (!weird)
747 goto default_case;
748
749 c = scm_getc (port);
750 if (c == '#')
751 {
752 return j;
753 }
754 else
755 {
756 scm_ungetc (c, port);
757 c = '}';
758 goto default_case;
759 }
760
761 default:
762 default_case:
763 {
764 c = (SCM_CASE_INSENSITIVE_P ? scm_c_downcase(c) : c);
765 scm_c_string_set_x (*tok_buf, j, SCM_MAKE_CHAR (c));
766 ++j;
767 }
768
769 }
770 }
771 }
772
773 #ifdef _UNICOS
774 _Pragma ("opt"); /* # pragma _CRI opt */
775 #endif
776
777 static SCM
778 scm_i_lreadparen (SCM *tok_buf, SCM port, char *name, SCM *copy, char term_char)
779 #define FUNC_NAME "scm_i_lreadparen"
780 {
781 SCM tmp;
782 SCM tl;
783 SCM ans;
784 int c;
785
786 c = scm_flush_ws (port, name);
787 if (term_char == c)
788 return SCM_EOL;
789 scm_ungetc (c, port);
790 if (scm_is_eq (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
791 {
792 ans = scm_lreadr (tok_buf, port, copy);
793 closeit:
794 if (term_char != (c = scm_flush_ws (port, name)))
795 scm_i_input_error (FUNC_NAME, port, "missing close paren", SCM_EOL);
796 return ans;
797 }
798 ans = tl = scm_cons (tmp, SCM_EOL);
799 while (term_char != (c = scm_flush_ws (port, name)))
800 {
801 scm_ungetc (c, port);
802 if (scm_is_eq (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
803 {
804 SCM_SETCDR (tl, scm_lreadr (tok_buf, port, copy));
805 goto closeit;
806 }
807 SCM_SETCDR (tl, scm_cons (tmp, SCM_EOL));
808 tl = SCM_CDR (tl);
809 }
810 return ans;
811 }
812 #undef FUNC_NAME
813
814
815 SCM
816 scm_lreadrecparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
817 #define FUNC_NAME "scm_lreadrecparen"
818 {
819 register int c;
820 register SCM tmp;
821 register SCM tl, tl2 = SCM_EOL;
822 SCM ans, ans2 = SCM_EOL;
823 /* Need to capture line and column numbers here. */
824 int line = SCM_LINUM (port);
825 int column = SCM_COL (port) - 1;
826
827 c = scm_flush_ws (port, name);
828 if (')' == c)
829 return SCM_EOL;
830 scm_ungetc (c, port);
831 if (scm_is_eq (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
832 {
833 ans = scm_lreadr (tok_buf, port, copy);
834 if (')' != (c = scm_flush_ws (port, name)))
835 scm_i_input_error (FUNC_NAME, port, "missing close paren", SCM_EOL);
836 return ans;
837 }
838 /* Build the head of the list structure. */
839 ans = tl = scm_cons (tmp, SCM_EOL);
840 if (SCM_COPY_SOURCE_P)
841 ans2 = tl2 = scm_cons (scm_is_pair (tmp)
842 ? *copy
843 : tmp,
844 SCM_EOL);
845 while (')' != (c = scm_flush_ws (port, name)))
846 {
847 SCM new_tail;
848
849 scm_ungetc (c, port);
850 if (scm_is_eq (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
851 {
852 SCM_SETCDR (tl, tmp = scm_lreadr (tok_buf, port, copy));
853 if (SCM_COPY_SOURCE_P)
854 SCM_SETCDR (tl2, scm_cons (scm_is_pair (tmp)
855 ? *copy
856 : tmp,
857 SCM_EOL));
858 if (')' != (c = scm_flush_ws (port, name)))
859 scm_i_input_error (FUNC_NAME, port,
860 "missing close paren", SCM_EOL);
861 goto exit;
862 }
863
864 new_tail = scm_cons (tmp, SCM_EOL);
865 SCM_SETCDR (tl, new_tail);
866 tl = new_tail;
867
868 if (SCM_COPY_SOURCE_P)
869 {
870 SCM new_tail2 = scm_cons (scm_is_pair (tmp) ? *copy : tmp, SCM_EOL);
871 SCM_SETCDR (tl2, new_tail2);
872 tl2 = new_tail2;
873 }
874 }
875 exit:
876 scm_whash_insert (scm_source_whash,
877 ans,
878 scm_make_srcprops (line,
879 column,
880 SCM_FILENAME (port),
881 SCM_COPY_SOURCE_P
882 ? *copy = ans2
883 : SCM_UNDEFINED,
884 SCM_EOL));
885 return ans;
886 }
887 #undef FUNC_NAME
888
889
890 \f
891
892 /* Manipulate the read-hash-procedures alist. This could be written in
893 Scheme, but maybe it will also be used by C code during initialisation. */
894 SCM_DEFINE (scm_read_hash_extend, "read-hash-extend", 2, 0, 0,
895 (SCM chr, SCM proc),
896 "Install the procedure @var{proc} for reading expressions\n"
897 "starting with the character sequence @code{#} and @var{chr}.\n"
898 "@var{proc} will be called with two arguments: the character\n"
899 "@var{chr} and the port to read further data from. The object\n"
900 "returned will be the return value of @code{read}.")
901 #define FUNC_NAME s_scm_read_hash_extend
902 {
903 SCM this;
904 SCM prev;
905
906 SCM_VALIDATE_CHAR (1, chr);
907 SCM_ASSERT (scm_is_false (proc)
908 || scm_is_eq (scm_procedure_p (proc), SCM_BOOL_T),
909 proc, SCM_ARG2, FUNC_NAME);
910
911 /* Check if chr is already in the alist. */
912 this = *scm_read_hash_procedures;
913 prev = SCM_BOOL_F;
914 while (1)
915 {
916 if (scm_is_null (this))
917 {
918 /* not found, so add it to the beginning. */
919 if (scm_is_true (proc))
920 {
921 *scm_read_hash_procedures =
922 scm_cons (scm_cons (chr, proc), *scm_read_hash_procedures);
923 }
924 break;
925 }
926 if (scm_is_eq (chr, SCM_CAAR (this)))
927 {
928 /* already in the alist. */
929 if (scm_is_false (proc))
930 {
931 /* remove it. */
932 if (scm_is_false (prev))
933 {
934 *scm_read_hash_procedures =
935 SCM_CDR (*scm_read_hash_procedures);
936 }
937 else
938 scm_set_cdr_x (prev, SCM_CDR (this));
939 }
940 else
941 {
942 /* replace it. */
943 scm_set_cdr_x (SCM_CAR (this), proc);
944 }
945 break;
946 }
947 prev = this;
948 this = SCM_CDR (this);
949 }
950
951 return SCM_UNSPECIFIED;
952 }
953 #undef FUNC_NAME
954
955 /* Recover the read-hash procedure corresponding to char c. */
956 static SCM
957 scm_get_hash_procedure (int c)
958 {
959 SCM rest = *scm_read_hash_procedures;
960
961 while (1)
962 {
963 if (scm_is_null (rest))
964 return SCM_BOOL_F;
965
966 if (SCM_CHAR (SCM_CAAR (rest)) == c)
967 return SCM_CDAR (rest);
968
969 rest = SCM_CDR (rest);
970 }
971 }
972
973 void
974 scm_init_read ()
975 {
976 scm_read_hash_procedures =
977 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL));
978
979 scm_init_opts (scm_read_options, scm_read_opts, SCM_N_READ_OPTIONS);
980 #include "libguile/read.x"
981 }
982
983 /*
984 Local Variables:
985 c-file-style: "gnu"
986 End:
987 */