Use new vector elements API or simple vector API, as appropriate.
[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 case '*':
486 j = scm_read_token (c, tok_buf, port, 0);
487 p = scm_istr2bve (scm_c_substring_shared (*tok_buf, 1, j));
488 if (scm_is_true (p))
489 return p;
490 else
491 goto unkshrp;
492
493 case '{':
494 j = scm_read_token (c, tok_buf, port, 1);
495 return scm_string_to_symbol (scm_c_substring_copy (*tok_buf, 0, j));
496
497 case '\\':
498 c = scm_getc (port);
499 j = scm_read_token (c, tok_buf, port, 0);
500 if (j == 1)
501 return SCM_MAKE_CHAR (c);
502 if (c >= '0' && c < '8')
503 {
504 /* Dirk:FIXME:: This type of character syntax is not R5RS
505 * compliant. Further, it should be verified that the constant
506 * does only consist of octal digits. Finally, it should be
507 * checked whether the resulting fixnum is in the range of
508 * characters. */
509 p = scm_i_mem2number (scm_i_string_chars (*tok_buf), j, 8);
510 if (SCM_I_INUMP (p))
511 return SCM_MAKE_CHAR (SCM_I_INUM (p));
512 }
513 for (c = 0; c < scm_n_charnames; c++)
514 if (scm_charnames[c]
515 && (scm_i_casei_streq (scm_charnames[c],
516 scm_i_string_chars (*tok_buf), j)))
517 return SCM_MAKE_CHAR (scm_charnums[c]);
518 scm_i_input_error (FUNC_NAME, port, "unknown character name ~a",
519 scm_list_1 (scm_c_substring (*tok_buf, 0, j)));
520
521 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
522 case ':':
523 return scm_symbol_to_keyword (scm_read (port));
524
525 default:
526 callshrp:
527 {
528 SCM sharp = scm_get_hash_procedure (c);
529
530 if (scm_is_true (sharp))
531 {
532 int line = SCM_LINUM (port);
533 int column = SCM_COL (port) - 2;
534 SCM got;
535
536 got = scm_call_2 (sharp, SCM_MAKE_CHAR (c), port);
537 if (scm_is_eq (got, SCM_UNSPECIFIED))
538 goto unkshrp;
539 if (SCM_RECORD_POSITIONS_P)
540 return *copy = recsexpr (got, line, column,
541 SCM_FILENAME (port));
542 else
543 return got;
544 }
545 }
546 unkshrp:
547 scm_i_input_error (FUNC_NAME, port, "Unknown # object: ~S",
548 scm_list_1 (SCM_MAKE_CHAR (c)));
549 }
550
551 case '"':
552 j = 0;
553 while ('"' != (c = scm_getc (port)))
554 {
555 if (c == EOF)
556 str_eof: scm_i_input_error (FUNC_NAME, port,
557 "end of file in string constant",
558 SCM_EOL);
559
560 while (j + 2 >= scm_i_string_length (*tok_buf))
561 scm_grow_tok_buf (tok_buf);
562
563 if (c == '\\')
564 switch (c = scm_getc (port))
565 {
566 case EOF:
567 goto str_eof;
568 case '"':
569 case '\\':
570 break;
571 #if SCM_ENABLE_ELISP
572 case '(':
573 case ')':
574 if (SCM_ESCAPED_PARENS_P)
575 break;
576 goto bad_escaped;
577 #endif
578 case '\n':
579 continue;
580 case '0':
581 c = '\0';
582 break;
583 case 'f':
584 c = '\f';
585 break;
586 case 'n':
587 c = '\n';
588 break;
589 case 'r':
590 c = '\r';
591 break;
592 case 't':
593 c = '\t';
594 break;
595 case 'a':
596 c = '\007';
597 break;
598 case 'v':
599 c = '\v';
600 break;
601 case 'x':
602 {
603 int a, b;
604 a = scm_getc (port);
605 if (a == EOF) goto str_eof;
606 b = scm_getc (port);
607 if (b == EOF) goto str_eof;
608 if ('0' <= a && a <= '9') a -= '0';
609 else if ('A' <= a && a <= 'F') a = a - 'A' + 10;
610 else if ('a' <= a && a <= 'f') a = a - 'a' + 10;
611 else goto bad_escaped;
612 if ('0' <= b && b <= '9') b -= '0';
613 else if ('A' <= b && b <= 'F') b = b - 'A' + 10;
614 else if ('a' <= b && b <= 'f') b = b - 'a' + 10;
615 else goto bad_escaped;
616 c = a * 16 + b;
617 break;
618 }
619 default:
620 bad_escaped:
621 scm_i_input_error(FUNC_NAME, port,
622 "illegal character in escape sequence: ~S",
623 scm_list_1 (SCM_MAKE_CHAR (c)));
624 }
625 scm_c_string_set_x (*tok_buf, j, SCM_MAKE_CHAR (c));
626 ++j;
627 }
628 if (j == 0)
629 return scm_nullstr;
630
631 /* Change this to scm_c_substring_read_only when
632 SCM_STRING_CHARS has been removed.
633 */
634 return scm_c_substring_copy (*tok_buf, 0, j);
635
636 case '0': case '1': case '2': case '3': case '4':
637 case '5': case '6': case '7': case '8': case '9':
638 case '.':
639 case '-':
640 case '+':
641 num:
642 j = scm_read_token (c, tok_buf, port, 0);
643 if (j == 1 && (c == '+' || c == '-'))
644 /* Shortcut: Detected symbol '+ or '- */
645 goto tok;
646
647 p = scm_i_mem2number (scm_i_string_chars (*tok_buf), j, 10);
648 if (scm_is_true (p))
649 return p;
650 if (c == '#')
651 {
652 if ((j == 2) && (scm_getc (port) == '('))
653 {
654 scm_ungetc ('(', port);
655 c = scm_i_string_chars (*tok_buf)[1];
656 goto callshrp;
657 }
658 scm_i_input_error (FUNC_NAME, port, "unknown # object", SCM_EOL);
659 }
660 goto tok;
661
662 case ':':
663 if (scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE), scm_keyword_prefix))
664 return scm_symbol_to_keyword (scm_read (port));
665
666 /* fallthrough */
667 default:
668 #if SCM_ENABLE_ELISP
669 read_token:
670 #endif
671 j = scm_read_token (c, tok_buf, port, 0);
672 /* fallthrough */
673
674 tok:
675 return scm_string_to_symbol (scm_c_substring (*tok_buf, 0, j));
676 }
677 }
678 #undef FUNC_NAME
679
680
681 #ifdef _UNICOS
682 _Pragma ("noopt"); /* # pragma _CRI noopt */
683 #endif
684
685 size_t
686 scm_read_token (int ic, SCM *tok_buf, SCM port, int weird)
687 {
688 size_t j;
689 int c;
690
691 c = (SCM_CASE_INSENSITIVE_P ? scm_c_downcase(ic) : ic);
692
693 if (weird)
694 j = 0;
695 else
696 {
697 j = 0;
698 while (j + 2 >= scm_i_string_length (*tok_buf))
699 scm_grow_tok_buf (tok_buf);
700 scm_c_string_set_x (*tok_buf, j, SCM_MAKE_CHAR (c));
701 ++j;
702 }
703
704 while (1)
705 {
706 while (j + 2 >= scm_i_string_length (*tok_buf))
707 scm_grow_tok_buf (tok_buf);
708 c = scm_getc (port);
709 switch (c)
710 {
711 case '(':
712 case ')':
713 #if SCM_ENABLE_ELISP
714 case '[':
715 case ']':
716 #endif
717 case '"':
718 case ';':
719 case SCM_WHITE_SPACES:
720 case SCM_LINE_INCREMENTORS:
721 if (weird
722 #if SCM_ENABLE_ELISP
723 || ((!SCM_ELISP_VECTORS_P) && ((c == '[') || (c == ']')))
724 #endif
725 )
726 goto default_case;
727
728 scm_ungetc (c, port);
729 case EOF:
730 eof_case:
731 return j;
732 case '\\':
733 if (!weird)
734 goto default_case;
735 else
736 {
737 c = scm_getc (port);
738 if (c == EOF)
739 goto eof_case;
740 else
741 goto default_case;
742 }
743 case '}':
744 if (!weird)
745 goto default_case;
746
747 c = scm_getc (port);
748 if (c == '#')
749 {
750 return j;
751 }
752 else
753 {
754 scm_ungetc (c, port);
755 c = '}';
756 goto default_case;
757 }
758
759 default:
760 default_case:
761 {
762 c = (SCM_CASE_INSENSITIVE_P ? scm_c_downcase(c) : c);
763 scm_c_string_set_x (*tok_buf, j, SCM_MAKE_CHAR (c));
764 ++j;
765 }
766
767 }
768 }
769 }
770
771 #ifdef _UNICOS
772 _Pragma ("opt"); /* # pragma _CRI opt */
773 #endif
774
775 static SCM
776 scm_i_lreadparen (SCM *tok_buf, SCM port, char *name, SCM *copy, char term_char)
777 #define FUNC_NAME "scm_i_lreadparen"
778 {
779 SCM tmp;
780 SCM tl;
781 SCM ans;
782 int c;
783
784 c = scm_flush_ws (port, name);
785 if (term_char == c)
786 return SCM_EOL;
787 scm_ungetc (c, port);
788 if (scm_is_eq (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
789 {
790 ans = scm_lreadr (tok_buf, port, copy);
791 closeit:
792 if (term_char != (c = scm_flush_ws (port, name)))
793 scm_i_input_error (FUNC_NAME, port, "missing close paren", SCM_EOL);
794 return ans;
795 }
796 ans = tl = scm_cons (tmp, SCM_EOL);
797 while (term_char != (c = scm_flush_ws (port, name)))
798 {
799 scm_ungetc (c, port);
800 if (scm_is_eq (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
801 {
802 SCM_SETCDR (tl, scm_lreadr (tok_buf, port, copy));
803 goto closeit;
804 }
805 SCM_SETCDR (tl, scm_cons (tmp, SCM_EOL));
806 tl = SCM_CDR (tl);
807 }
808 return ans;
809 }
810 #undef FUNC_NAME
811
812
813 SCM
814 scm_lreadrecparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
815 #define FUNC_NAME "scm_lreadrecparen"
816 {
817 register int c;
818 register SCM tmp;
819 register SCM tl, tl2 = SCM_EOL;
820 SCM ans, ans2 = SCM_EOL;
821 /* Need to capture line and column numbers here. */
822 int line = SCM_LINUM (port);
823 int column = SCM_COL (port) - 1;
824
825 c = scm_flush_ws (port, name);
826 if (')' == c)
827 return SCM_EOL;
828 scm_ungetc (c, port);
829 if (scm_is_eq (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
830 {
831 ans = scm_lreadr (tok_buf, port, copy);
832 if (')' != (c = scm_flush_ws (port, name)))
833 scm_i_input_error (FUNC_NAME, port, "missing close paren", SCM_EOL);
834 return ans;
835 }
836 /* Build the head of the list structure. */
837 ans = tl = scm_cons (tmp, SCM_EOL);
838 if (SCM_COPY_SOURCE_P)
839 ans2 = tl2 = scm_cons (scm_is_pair (tmp)
840 ? *copy
841 : tmp,
842 SCM_EOL);
843 while (')' != (c = scm_flush_ws (port, name)))
844 {
845 SCM new_tail;
846
847 scm_ungetc (c, port);
848 if (scm_is_eq (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
849 {
850 SCM_SETCDR (tl, tmp = scm_lreadr (tok_buf, port, copy));
851 if (SCM_COPY_SOURCE_P)
852 SCM_SETCDR (tl2, scm_cons (scm_is_pair (tmp)
853 ? *copy
854 : tmp,
855 SCM_EOL));
856 if (')' != (c = scm_flush_ws (port, name)))
857 scm_i_input_error (FUNC_NAME, port,
858 "missing close paren", SCM_EOL);
859 goto exit;
860 }
861
862 new_tail = scm_cons (tmp, SCM_EOL);
863 SCM_SETCDR (tl, new_tail);
864 tl = new_tail;
865
866 if (SCM_COPY_SOURCE_P)
867 {
868 SCM new_tail2 = scm_cons (scm_is_pair (tmp) ? *copy : tmp, SCM_EOL);
869 SCM_SETCDR (tl2, new_tail2);
870 tl2 = new_tail2;
871 }
872 }
873 exit:
874 scm_whash_insert (scm_source_whash,
875 ans,
876 scm_make_srcprops (line,
877 column,
878 SCM_FILENAME (port),
879 SCM_COPY_SOURCE_P
880 ? *copy = ans2
881 : SCM_UNDEFINED,
882 SCM_EOL));
883 return ans;
884 }
885 #undef FUNC_NAME
886
887
888 \f
889
890 /* Manipulate the read-hash-procedures alist. This could be written in
891 Scheme, but maybe it will also be used by C code during initialisation. */
892 SCM_DEFINE (scm_read_hash_extend, "read-hash-extend", 2, 0, 0,
893 (SCM chr, SCM proc),
894 "Install the procedure @var{proc} for reading expressions\n"
895 "starting with the character sequence @code{#} and @var{chr}.\n"
896 "@var{proc} will be called with two arguments: the character\n"
897 "@var{chr} and the port to read further data from. The object\n"
898 "returned will be the return value of @code{read}.")
899 #define FUNC_NAME s_scm_read_hash_extend
900 {
901 SCM this;
902 SCM prev;
903
904 SCM_VALIDATE_CHAR (1, chr);
905 SCM_ASSERT (scm_is_false (proc)
906 || scm_is_eq (scm_procedure_p (proc), SCM_BOOL_T),
907 proc, SCM_ARG2, FUNC_NAME);
908
909 /* Check if chr is already in the alist. */
910 this = *scm_read_hash_procedures;
911 prev = SCM_BOOL_F;
912 while (1)
913 {
914 if (scm_is_null (this))
915 {
916 /* not found, so add it to the beginning. */
917 if (scm_is_true (proc))
918 {
919 *scm_read_hash_procedures =
920 scm_cons (scm_cons (chr, proc), *scm_read_hash_procedures);
921 }
922 break;
923 }
924 if (scm_is_eq (chr, SCM_CAAR (this)))
925 {
926 /* already in the alist. */
927 if (scm_is_false (proc))
928 {
929 /* remove it. */
930 if (scm_is_false (prev))
931 {
932 *scm_read_hash_procedures =
933 SCM_CDR (*scm_read_hash_procedures);
934 }
935 else
936 scm_set_cdr_x (prev, SCM_CDR (this));
937 }
938 else
939 {
940 /* replace it. */
941 scm_set_cdr_x (SCM_CAR (this), proc);
942 }
943 break;
944 }
945 prev = this;
946 this = SCM_CDR (this);
947 }
948
949 return SCM_UNSPECIFIED;
950 }
951 #undef FUNC_NAME
952
953 /* Recover the read-hash procedure corresponding to char c. */
954 static SCM
955 scm_get_hash_procedure (int c)
956 {
957 SCM rest = *scm_read_hash_procedures;
958
959 while (1)
960 {
961 if (scm_is_null (rest))
962 return SCM_BOOL_F;
963
964 if (SCM_CHAR (SCM_CAAR (rest)) == c)
965 return SCM_CDAR (rest);
966
967 rest = SCM_CDR (rest);
968 }
969 }
970
971 void
972 scm_init_read ()
973 {
974 scm_read_hash_procedures =
975 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL));
976
977 scm_init_opts (scm_read_options, scm_read_opts, SCM_N_READ_OPTIONS);
978 #include "libguile/read.x"
979 }
980
981 /*
982 Local Variables:
983 c-file-style: "gnu"
984 End:
985 */