(skip_scsh_block_comment): Also recognize '\r' as a line terminator.
[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
39 #include "libguile/read.h"
40
41 \f
42
43 SCM_GLOBAL_SYMBOL (scm_sym_dot, ".");
44 SCM_SYMBOL (scm_keyword_prefix, "prefix");
45
46 scm_t_option scm_read_opts[] = {
47 { SCM_OPTION_BOOLEAN, "copy", 0,
48 "Copy source code expressions." },
49 { SCM_OPTION_BOOLEAN, "positions", 0,
50 "Record positions of source code expressions." },
51 { SCM_OPTION_BOOLEAN, "case-insensitive", 0,
52 "Convert symbols to lower case."},
53 { SCM_OPTION_SCM, "keywords", SCM_UNPACK (SCM_BOOL_F),
54 "Style of keyword recognition: #f or 'prefix."}
55 #if SCM_ENABLE_ELISP
56 ,
57 { SCM_OPTION_BOOLEAN, "elisp-vectors", 0,
58 "Support Elisp vector syntax, namely `[...]'."},
59 { SCM_OPTION_BOOLEAN, "elisp-strings", 0,
60 "Support `\\(' and `\\)' in strings."}
61 #endif
62 };
63
64 /*
65 Give meaningful error messages for errors
66
67 We use the format
68
69 FILE:LINE:COL: MESSAGE
70 This happened in ....
71
72 This is not standard GNU format, but the test-suite likes the real
73 message to be in front.
74
75 */
76
77
78 static void
79 scm_input_error(char const * function,
80 SCM port, const char * message, SCM arg)
81 {
82 char *fn = SCM_STRINGP (SCM_FILENAME(port))
83 ? SCM_STRING_CHARS(SCM_FILENAME(port))
84 : "#<unknown port>";
85
86 SCM string_port = scm_open_output_string ();
87 SCM string = SCM_EOL;
88 scm_simple_format (string_port,
89 scm_makfrom0str ("~A:~S:~S: ~A"),
90 scm_list_4 (scm_makfrom0str (fn),
91 scm_int2num (SCM_LINUM (port) + 1),
92 scm_int2num (SCM_COL (port) + 1),
93 scm_makfrom0str (message)));
94
95
96 string = scm_get_output_string (string_port);
97 scm_close_output_port (string_port);
98 scm_error_scm (scm_str2symbol ("read-error"),
99 scm_makfrom0str (function),
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_allocate_string (30);
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_STRING_LENGTH (*tok_buf);
156 SCM newstr = scm_allocate_string (2 * oldlen);
157 size_t i;
158
159 for (i = 0; i != oldlen; ++i)
160 SCM_STRING_CHARS (newstr) [i] = SCM_STRING_CHARS (*tok_buf) [i];
161
162 *tok_buf = newstr;
163 return SCM_STRING_CHARS (newstr);
164 }
165
166
167
168 int
169 scm_flush_ws (SCM port, const char *eoferr)
170 {
171 register int c;
172 while (1)
173 switch (c = scm_getc (port))
174 {
175 case EOF:
176 goteof:
177 if (eoferr)
178 {
179 scm_input_error (eoferr,
180 port,
181 "end of file",
182 SCM_EOL);
183 }
184 return c;
185 case ';':
186 lp:
187 switch (c = scm_getc (port))
188 {
189 case EOF:
190 goto goteof;
191 default:
192 goto lp;
193 case SCM_LINE_INCREMENTORS:
194 break;
195 }
196 break;
197 case SCM_LINE_INCREMENTORS:
198 case SCM_SINGLE_SPACES:
199 case '\t':
200 break;
201 default:
202 return c;
203 }
204 }
205
206
207
208 int
209 scm_casei_streq (char *s1, char *s2)
210 {
211 while (*s1 && *s2)
212 if (scm_downcase((int)*s1) != scm_downcase((int)*s2))
213 return 0;
214 else
215 {
216 ++s1;
217 ++s2;
218 }
219 return !(*s1 || *s2);
220 }
221
222
223 /* recsexpr is used when recording expressions
224 * constructed by read:sharp.
225 */
226 static SCM
227 recsexpr (SCM obj, long line, int column, SCM filename)
228 {
229 if (!SCM_CONSP(obj)) {
230 return obj;
231 } else {
232 SCM tmp = obj, copy;
233 /* If this sexpr is visible in the read:sharp source, we want to
234 keep that information, so only record non-constant cons cells
235 which haven't previously been read by the reader. */
236 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash, obj)))
237 {
238 if (SCM_COPY_SOURCE_P)
239 {
240 copy = scm_cons (recsexpr (SCM_CAR (obj), line, column, filename),
241 SCM_UNDEFINED);
242 while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
243 {
244 SCM_SETCDR (copy, scm_cons (recsexpr (SCM_CAR (tmp),
245 line,
246 column,
247 filename),
248 SCM_UNDEFINED));
249 copy = SCM_CDR (copy);
250 }
251 SCM_SETCDR (copy, tmp);
252 }
253 else
254 {
255 recsexpr (SCM_CAR (obj), line, column, filename);
256 while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
257 recsexpr (SCM_CAR (tmp), line, column, filename);
258 copy = SCM_UNDEFINED;
259 }
260 scm_whash_insert (scm_source_whash,
261 obj,
262 scm_make_srcprops (line,
263 column,
264 filename,
265 copy,
266 SCM_EOL));
267 }
268 return obj;
269 }
270 }
271
272 /* Consume an SCSH-style block comment. Assume that we've already
273 read the initial `#!', and eat characters until we get a
274 newline/exclamation-point/sharp-sign/newline sequence.
275
276 A carriage return is also reocgnized as a newline. */
277
278 static void
279 skip_scsh_block_comment (SCM port)
280 #define FUNC_NAME "skip_scsh_block_comment"
281 {
282 int state = 0;
283
284 for (;;)
285 {
286 int c = scm_getc (port);
287
288 if (c == EOF)
289 SCM_MISC_ERROR ("unterminated `#! ... !#' comment", SCM_EOL);
290
291 if (state == 1 && c == '!')
292 state = 2;
293 else if (state == 2 && c == '#')
294 state = 3;
295 else if (state == 3 && (c == '\n' || c == '\r'))
296 return;
297 else if (c == '\n' || c == '\r')
298 state = 1;
299 else
300 state = 0;
301 }
302 }
303 #undef FUNC_NAME
304
305
306 static SCM scm_get_hash_procedure(int c);
307 static SCM scm_i_lreadparen (SCM *, SCM, char *, SCM *, char);
308
309 static char s_list[]="list";
310 static char s_vector[]="vector";
311
312 SCM
313 scm_lreadr (SCM *tok_buf, SCM port, SCM *copy)
314 #define FUNC_NAME "scm_lreadr"
315 {
316 int c;
317 size_t j;
318 SCM p;
319
320 tryagain:
321 c = scm_flush_ws (port, s_scm_read);
322 tryagain_no_flush_ws:
323 switch (c)
324 {
325 case EOF:
326 return SCM_EOF_VAL;
327
328 case '(':
329 return SCM_RECORD_POSITIONS_P
330 ? scm_lreadrecparen (tok_buf, port, s_list, copy)
331 : scm_i_lreadparen (tok_buf, port, s_list, copy, ')');
332 case ')':
333 scm_input_error (FUNC_NAME, port,"unexpected \")\"", SCM_EOL);
334 goto tryagain;
335
336 #if SCM_ENABLE_ELISP
337 case '[':
338 if (SCM_ELISP_VECTORS_P)
339 {
340 p = scm_i_lreadparen (tok_buf, port, s_vector, copy, ']');
341 return SCM_NULLP (p) ? scm_nullvect : scm_vector (p);
342 }
343 goto read_token;
344 #endif
345 case '\'':
346 p = scm_sym_quote;
347 goto recquote;
348 case '`':
349 p = scm_sym_quasiquote;
350 goto recquote;
351 case ',':
352 c = scm_getc (port);
353 if ('@' == c)
354 p = scm_sym_uq_splicing;
355 else
356 {
357 scm_ungetc (c, port);
358 p = scm_sym_unquote;
359 }
360 recquote:
361 p = scm_cons2 (p,
362 scm_lreadr (tok_buf, port, copy),
363 SCM_EOL);
364 if (SCM_RECORD_POSITIONS_P)
365 scm_whash_insert (scm_source_whash,
366 p,
367 scm_make_srcprops (SCM_LINUM (port),
368 SCM_COL (port) - 1,
369 SCM_FILENAME (port),
370 SCM_COPY_SOURCE_P
371 ? (*copy = scm_cons2 (SCM_CAR (p),
372 SCM_CAR (SCM_CDR (p)),
373 SCM_EOL))
374 : SCM_UNDEFINED,
375 SCM_EOL));
376 return p;
377 case '#':
378 c = scm_getc (port);
379
380 {
381 /* Check for user-defined hash procedure first, to allow
382 overriding of builtin hash read syntaxes. */
383 SCM sharp = scm_get_hash_procedure (c);
384 if (!SCM_FALSEP (sharp))
385 {
386 int line = SCM_LINUM (port);
387 int column = SCM_COL (port) - 2;
388 SCM got;
389
390 got = scm_call_2 (sharp, SCM_MAKE_CHAR (c), port);
391 if (SCM_EQ_P (got, SCM_UNSPECIFIED))
392 goto handle_sharp;
393 if (SCM_RECORD_POSITIONS_P)
394 return *copy = recsexpr (got, line, column,
395 SCM_FILENAME (port));
396 else
397 return got;
398 }
399 }
400 handle_sharp:
401 switch (c)
402 {
403 case '(':
404 p = scm_i_lreadparen (tok_buf, port, s_vector, copy, ')');
405 return SCM_NULLP (p) ? scm_nullvect : scm_vector (p);
406
407 case 't':
408 case 'T':
409 return SCM_BOOL_T;
410 case 'f':
411 case 'F':
412 return SCM_BOOL_F;
413
414 case 'b':
415 case 'B':
416 case 'o':
417 case 'O':
418 case 'd':
419 case 'D':
420 case 'x':
421 case 'X':
422 case 'i':
423 case 'I':
424 case 'e':
425 case 'E':
426 scm_ungetc (c, port);
427 c = '#';
428 goto num;
429
430 case '!':
431 /* start of a shell script. Parse as a block comment,
432 terminated by !#, just like SCSH. */
433 skip_scsh_block_comment (port);
434 /* EOF is not an error here */
435 c = scm_flush_ws (port, (char *)NULL);
436 goto tryagain_no_flush_ws;
437
438 #if SCM_HAVE_ARRAYS
439 case '*':
440 j = scm_read_token (c, tok_buf, port, 0);
441 p = scm_istr2bve (SCM_STRING_CHARS (*tok_buf) + 1, (long) (j - 1));
442 if (!SCM_FALSEP (p))
443 return p;
444 else
445 goto unkshrp;
446 #endif
447
448 case '{':
449 j = scm_read_token (c, tok_buf, port, 1);
450 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
451
452 case '\\':
453 c = scm_getc (port);
454 j = scm_read_token (c, tok_buf, port, 0);
455 if (j == 1)
456 return SCM_MAKE_CHAR (c);
457 if (c >= '0' && c < '8')
458 {
459 /* Dirk:FIXME:: This type of character syntax is not R5RS
460 * compliant. Further, it should be verified that the constant
461 * does only consist of octal digits. Finally, it should be
462 * checked whether the resulting fixnum is in the range of
463 * characters. */
464 p = scm_i_mem2number (SCM_STRING_CHARS (*tok_buf), j, 8);
465 if (SCM_INUMP (p))
466 return SCM_MAKE_CHAR (SCM_INUM (p));
467 }
468 for (c = 0; c < scm_n_charnames; c++)
469 if (scm_charnames[c]
470 && (scm_casei_streq (scm_charnames[c], SCM_STRING_CHARS (*tok_buf))))
471 return SCM_MAKE_CHAR (scm_charnums[c]);
472 scm_input_error (FUNC_NAME, port, "unknown # object", SCM_EOL);
473
474 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
475 case ':':
476 j = scm_read_token ('-', tok_buf, port, 0);
477 p = scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
478 return scm_make_keyword_from_dash_symbol (p);
479
480 default:
481 callshrp:
482 {
483 SCM sharp = scm_get_hash_procedure (c);
484
485 if (!SCM_FALSEP (sharp))
486 {
487 int line = SCM_LINUM (port);
488 int column = SCM_COL (port) - 2;
489 SCM got;
490
491 got = scm_call_2 (sharp, SCM_MAKE_CHAR (c), port);
492 if (SCM_EQ_P (got, SCM_UNSPECIFIED))
493 goto unkshrp;
494 if (SCM_RECORD_POSITIONS_P)
495 return *copy = recsexpr (got, line, column,
496 SCM_FILENAME (port));
497 else
498 return got;
499 }
500 }
501 unkshrp:
502 scm_input_error (FUNC_NAME, port, "Unknown # object: ~S",
503 scm_list_1 (SCM_MAKE_CHAR (c)));
504 }
505
506 case '"':
507 j = 0;
508 while ('"' != (c = scm_getc (port)))
509 {
510 if (c == EOF)
511 str_eof: scm_input_error (FUNC_NAME, port, "end of file in string constant", SCM_EOL);
512
513 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
514 scm_grow_tok_buf (tok_buf);
515
516 if (c == '\\')
517 switch (c = scm_getc (port))
518 {
519 case EOF:
520 goto str_eof;
521 case '"':
522 case '\\':
523 break;
524 #if SCM_ENABLE_ELISP
525 case '(':
526 case ')':
527 if (SCM_ESCAPED_PARENS_P)
528 break;
529 goto bad_escaped;
530 #endif
531 case '\n':
532 continue;
533 case '0':
534 c = '\0';
535 break;
536 case 'f':
537 c = '\f';
538 break;
539 case 'n':
540 c = '\n';
541 break;
542 case 'r':
543 c = '\r';
544 break;
545 case 't':
546 c = '\t';
547 break;
548 case 'a':
549 c = '\007';
550 break;
551 case 'v':
552 c = '\v';
553 break;
554 case 'x':
555 {
556 int a, b;
557 a = scm_getc (port);
558 if (a == EOF) goto str_eof;
559 b = scm_getc (port);
560 if (b == EOF) goto str_eof;
561 if ('0' <= a && a <= '9') a -= '0';
562 else if ('A' <= a && a <= 'F') a = a - 'A' + 10;
563 else if ('a' <= a && a <= 'f') a = a - 'a' + 10;
564 else goto bad_escaped;
565 if ('0' <= b && b <= '9') b -= '0';
566 else if ('A' <= b && b <= 'F') b = b - 'A' + 10;
567 else if ('a' <= b && b <= 'f') b = b - 'a' + 10;
568 else goto bad_escaped;
569 c = a * 16 + b;
570 break;
571 }
572 default:
573 bad_escaped:
574 scm_input_error(FUNC_NAME, port,
575 "illegal character in escape sequence: ~S",
576 scm_list_1 (SCM_MAKE_CHAR (c)));
577 }
578 SCM_STRING_CHARS (*tok_buf)[j] = c;
579 ++j;
580 }
581 if (j == 0)
582 return scm_nullstr;
583 SCM_STRING_CHARS (*tok_buf)[j] = 0;
584 return scm_mem2string (SCM_STRING_CHARS (*tok_buf), j);
585
586 case '0': case '1': case '2': case '3': case '4':
587 case '5': case '6': case '7': case '8': case '9':
588 case '.':
589 case '-':
590 case '+':
591 num:
592 j = scm_read_token (c, tok_buf, port, 0);
593 if (j == 1 && (c == '+' || c == '-'))
594 /* Shortcut: Detected symbol '+ or '- */
595 goto tok;
596
597 p = scm_i_mem2number (SCM_STRING_CHARS (*tok_buf), j, 10);
598 if (!SCM_FALSEP (p))
599 return p;
600 if (c == '#')
601 {
602 if ((j == 2) && (scm_getc (port) == '('))
603 {
604 scm_ungetc ('(', port);
605 c = SCM_STRING_CHARS (*tok_buf)[1];
606 goto callshrp;
607 }
608 scm_input_error (FUNC_NAME, port, "unknown # object", SCM_EOL);
609 }
610 goto tok;
611
612 case ':':
613 if (SCM_EQ_P (SCM_PACK (SCM_KEYWORD_STYLE), scm_keyword_prefix))
614 {
615 j = scm_read_token ('-', tok_buf, port, 0);
616 p = scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
617 return scm_make_keyword_from_dash_symbol (p);
618 }
619 /* fallthrough */
620 default:
621 #if SCM_ENABLE_ELISP
622 read_token:
623 #endif
624 j = scm_read_token (c, tok_buf, port, 0);
625 /* fallthrough */
626
627 tok:
628 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
629 }
630 }
631 #undef FUNC_NAME
632
633
634 #ifdef _UNICOS
635 _Pragma ("noopt"); /* # pragma _CRI noopt */
636 #endif
637
638 size_t
639 scm_read_token (int ic, SCM *tok_buf, SCM port, int weird)
640 {
641 register size_t j;
642 register int c;
643 register char *p;
644
645 c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(ic) : ic);
646 p = SCM_STRING_CHARS (*tok_buf);
647
648 if (weird)
649 j = 0;
650 else
651 {
652 j = 0;
653 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
654 p = scm_grow_tok_buf (tok_buf);
655 p[j] = c;
656 ++j;
657 }
658
659 while (1)
660 {
661 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
662 p = scm_grow_tok_buf (tok_buf);
663 c = scm_getc (port);
664 switch (c)
665 {
666 case '(':
667 case ')':
668 #if SCM_ENABLE_ELISP
669 case '[':
670 case ']':
671 #endif
672 case '"':
673 case ';':
674 case SCM_WHITE_SPACES:
675 case SCM_LINE_INCREMENTORS:
676 if (weird
677 #if SCM_ENABLE_ELISP
678 || ((!SCM_ELISP_VECTORS_P) && ((c == '[') || (c == ']')))
679 #endif
680 )
681 goto default_case;
682
683 scm_ungetc (c, port);
684 case EOF:
685 eof_case:
686 p[j] = 0;
687 return j;
688 case '\\':
689 if (!weird)
690 goto default_case;
691 else
692 {
693 c = scm_getc (port);
694 if (c == EOF)
695 goto eof_case;
696 else
697 goto default_case;
698 }
699 case '}':
700 if (!weird)
701 goto default_case;
702
703 c = scm_getc (port);
704 if (c == '#')
705 {
706 p[j] = 0;
707 return j;
708 }
709 else
710 {
711 scm_ungetc (c, port);
712 c = '}';
713 goto default_case;
714 }
715
716 default:
717 default_case:
718 {
719 c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(c) : c);
720 p[j] = c;
721 ++j;
722 }
723
724 }
725 }
726 }
727
728 #ifdef _UNICOS
729 _Pragma ("opt"); /* # pragma _CRI opt */
730 #endif
731
732 static SCM
733 scm_i_lreadparen (SCM *tok_buf, SCM port, char *name, SCM *copy, char term_char)
734 #define FUNC_NAME "scm_i_lreadparen"
735 {
736 SCM tmp;
737 SCM tl;
738 SCM ans;
739 int c;
740
741 c = scm_flush_ws (port, name);
742 if (term_char == c)
743 return SCM_EOL;
744 scm_ungetc (c, port);
745 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
746 {
747 ans = scm_lreadr (tok_buf, port, copy);
748 closeit:
749 if (term_char != (c = scm_flush_ws (port, name)))
750 scm_input_error (FUNC_NAME, port, "missing close paren", SCM_EOL);
751 return ans;
752 }
753 ans = tl = scm_cons (tmp, SCM_EOL);
754 while (term_char != (c = scm_flush_ws (port, name)))
755 {
756 scm_ungetc (c, port);
757 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
758 {
759 SCM_SETCDR (tl, scm_lreadr (tok_buf, port, copy));
760 goto closeit;
761 }
762 SCM_SETCDR (tl, scm_cons (tmp, SCM_EOL));
763 tl = SCM_CDR (tl);
764 }
765 return ans;
766 }
767 #undef FUNC_NAME
768
769
770 SCM
771 scm_lreadrecparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
772 #define FUNC_NAME "scm_lreadrecparen"
773 {
774 register int c;
775 register SCM tmp;
776 register SCM tl, tl2 = SCM_EOL;
777 SCM ans, ans2 = SCM_EOL;
778 /* Need to capture line and column numbers here. */
779 int line = SCM_LINUM (port);
780 int column = SCM_COL (port) - 1;
781
782 c = scm_flush_ws (port, name);
783 if (')' == c)
784 return SCM_EOL;
785 scm_ungetc (c, port);
786 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
787 {
788 ans = scm_lreadr (tok_buf, port, copy);
789 if (')' != (c = scm_flush_ws (port, name)))
790 scm_input_error (FUNC_NAME, port, "missing close paren", SCM_EOL);
791 return ans;
792 }
793 /* Build the head of the list structure. */
794 ans = tl = scm_cons (tmp, SCM_EOL);
795 if (SCM_COPY_SOURCE_P)
796 ans2 = tl2 = scm_cons (SCM_CONSP (tmp)
797 ? *copy
798 : tmp,
799 SCM_EOL);
800 while (')' != (c = scm_flush_ws (port, name)))
801 {
802 SCM new_tail;
803
804 scm_ungetc (c, port);
805 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
806 {
807 SCM_SETCDR (tl, tmp = scm_lreadr (tok_buf, port, copy));
808 if (SCM_COPY_SOURCE_P)
809 SCM_SETCDR (tl2, scm_cons (SCM_CONSP (tmp)
810 ? *copy
811 : tmp,
812 SCM_EOL));
813 if (')' != (c = scm_flush_ws (port, name)))
814 scm_input_error (FUNC_NAME, port, "missing close paren", SCM_EOL);
815 goto exit;
816 }
817
818 new_tail = scm_cons (tmp, SCM_EOL);
819 SCM_SETCDR (tl, new_tail);
820 tl = new_tail;
821
822 if (SCM_COPY_SOURCE_P)
823 {
824 SCM new_tail2 = scm_cons (SCM_CONSP (tmp) ? *copy : tmp, SCM_EOL);
825 SCM_SETCDR (tl2, new_tail2);
826 tl2 = new_tail2;
827 }
828 }
829 exit:
830 scm_whash_insert (scm_source_whash,
831 ans,
832 scm_make_srcprops (line,
833 column,
834 SCM_FILENAME (port),
835 SCM_COPY_SOURCE_P
836 ? *copy = ans2
837 : SCM_UNDEFINED,
838 SCM_EOL));
839 return ans;
840 }
841 #undef FUNC_NAME
842
843
844 \f
845
846 /* Manipulate the read-hash-procedures alist. This could be written in
847 Scheme, but maybe it will also be used by C code during initialisation. */
848 SCM_DEFINE (scm_read_hash_extend, "read-hash-extend", 2, 0, 0,
849 (SCM chr, SCM proc),
850 "Install the procedure @var{proc} for reading expressions\n"
851 "starting with the character sequence @code{#} and @var{chr}.\n"
852 "@var{proc} will be called with two arguments: the character\n"
853 "@var{chr} and the port to read further data from. The object\n"
854 "returned will be the return value of @code{read}.")
855 #define FUNC_NAME s_scm_read_hash_extend
856 {
857 SCM this;
858 SCM prev;
859
860 SCM_VALIDATE_CHAR (1, chr);
861 SCM_ASSERT (SCM_FALSEP (proc)
862 || SCM_EQ_P (scm_procedure_p (proc), SCM_BOOL_T),
863 proc, SCM_ARG2, FUNC_NAME);
864
865 /* Check if chr is already in the alist. */
866 this = *scm_read_hash_procedures;
867 prev = SCM_BOOL_F;
868 while (1)
869 {
870 if (SCM_NULLP (this))
871 {
872 /* not found, so add it to the beginning. */
873 if (!SCM_FALSEP (proc))
874 {
875 *scm_read_hash_procedures =
876 scm_cons (scm_cons (chr, proc), *scm_read_hash_procedures);
877 }
878 break;
879 }
880 if (SCM_EQ_P (chr, SCM_CAAR (this)))
881 {
882 /* already in the alist. */
883 if (SCM_FALSEP (proc))
884 {
885 /* remove it. */
886 if (SCM_FALSEP (prev))
887 {
888 *scm_read_hash_procedures =
889 SCM_CDR (*scm_read_hash_procedures);
890 }
891 else
892 scm_set_cdr_x (prev, SCM_CDR (this));
893 }
894 else
895 {
896 /* replace it. */
897 scm_set_cdr_x (SCM_CAR (this), proc);
898 }
899 break;
900 }
901 prev = this;
902 this = SCM_CDR (this);
903 }
904
905 return SCM_UNSPECIFIED;
906 }
907 #undef FUNC_NAME
908
909 /* Recover the read-hash procedure corresponding to char c. */
910 static SCM
911 scm_get_hash_procedure (int c)
912 {
913 SCM rest = *scm_read_hash_procedures;
914
915 while (1)
916 {
917 if (SCM_NULLP (rest))
918 return SCM_BOOL_F;
919
920 if (SCM_CHAR (SCM_CAAR (rest)) == c)
921 return SCM_CDAR (rest);
922
923 rest = SCM_CDR (rest);
924 }
925 }
926
927 void
928 scm_init_read ()
929 {
930 scm_read_hash_procedures =
931 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL));
932
933 scm_init_opts (scm_read_options, scm_read_opts, SCM_N_READ_OPTIONS);
934 #include "libguile/read.x"
935 }
936
937 /*
938 Local Variables:
939 c-file-style: "gnu"
940 End:
941 */