1 /* Copyright (C) 1995,1996,1997,1999,2000,2001,2003, 2004, 2006, 2007, 2008 Free Software
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "libguile/_scm.h"
31 #include "libguile/chars.h"
32 #include "libguile/eval.h"
33 #include "libguile/unif.h"
34 #include "libguile/keywords.h"
35 #include "libguile/alist.h"
36 #include "libguile/srcprop.h"
37 #include "libguile/hashtab.h"
38 #include "libguile/hash.h"
39 #include "libguile/ports.h"
40 #include "libguile/root.h"
41 #include "libguile/strings.h"
42 #include "libguile/strports.h"
43 #include "libguile/vectors.h"
44 #include "libguile/validate.h"
45 #include "libguile/srfi-4.h"
46 #include "libguile/srfi-13.h"
48 #include "libguile/read.h"
49 #include "libguile/private-options.h"
54 SCM_GLOBAL_SYMBOL (scm_sym_dot
, ".");
55 SCM_SYMBOL (scm_keyword_prefix
, "prefix");
56 SCM_SYMBOL (scm_keyword_postfix
, "postfix");
58 scm_t_option scm_read_opts
[] = {
59 { SCM_OPTION_BOOLEAN
, "copy", 0,
60 "Copy source code expressions." },
61 { SCM_OPTION_BOOLEAN
, "positions", 0,
62 "Record positions of source code expressions." },
63 { SCM_OPTION_BOOLEAN
, "case-insensitive", 0,
64 "Convert symbols to lower case."},
65 { SCM_OPTION_SCM
, "keywords", SCM_UNPACK (SCM_BOOL_F
),
66 "Style of keyword recognition: #f, 'prefix or 'postfix."},
68 { SCM_OPTION_BOOLEAN
, "elisp-vectors", 0,
69 "Support Elisp vector syntax, namely `[...]'."},
70 { SCM_OPTION_BOOLEAN
, "elisp-strings", 0,
71 "Support `\\(' and `\\)' in strings."},
77 Give meaningful error messages for errors
81 FILE:LINE:COL: MESSAGE
84 This is not standard GNU format, but the test-suite likes the real
85 message to be in front.
91 scm_i_input_error (char const *function
,
92 SCM port
, const char *message
, SCM arg
)
94 SCM fn
= (scm_is_string (SCM_FILENAME(port
))
96 : scm_from_locale_string ("#<unknown port>"));
98 SCM string_port
= scm_open_output_string ();
100 scm_simple_format (string_port
,
101 scm_from_locale_string ("~A:~S:~S: ~A"),
103 scm_from_long (SCM_LINUM (port
) + 1),
104 scm_from_int (SCM_COL (port
) + 1),
105 scm_from_locale_string (message
)));
107 string
= scm_get_output_string (string_port
);
108 scm_close_output_port (string_port
);
109 scm_error_scm (scm_from_locale_symbol ("read-error"),
110 function
? scm_from_locale_string (function
) : SCM_BOOL_F
,
117 SCM_DEFINE (scm_read_options
, "read-options-interface", 0, 1, 0,
119 "Option interface for the read options. Instead of using\n"
120 "this procedure directly, use the procedures @code{read-enable},\n"
121 "@code{read-disable}, @code{read-set!} and @code{read-options}.")
122 #define FUNC_NAME s_scm_read_options
124 SCM ans
= scm_options (setting
,
127 if (SCM_COPY_SOURCE_P
)
128 SCM_RECORD_POSITIONS_P
= 1;
133 /* An association list mapping extra hash characters to procedures. */
134 static SCM
*scm_read_hash_procedures
;
141 /* Size of the C buffer used to read symbols and numbers. */
142 #define READER_BUFFER_SIZE 128
144 /* Size of the C buffer used to read strings. */
145 #define READER_STRING_BUFFER_SIZE 512
147 /* The maximum size of Scheme character names. */
148 #define READER_CHAR_NAME_MAX_SIZE 50
151 /* `isblank' is only in C99. */
152 #define CHAR_IS_BLANK_(_chr) \
153 (((_chr) == ' ') || ((_chr) == '\t') || ((_chr) == '\n') \
154 || ((_chr) == '\f') || ((_chr) == '\r'))
157 # define CHAR_IS_BLANK(_chr) \
158 ((CHAR_IS_BLANK_ (chr)) || ((_chr) == 26))
160 # define CHAR_IS_BLANK CHAR_IS_BLANK_
164 /* R5RS one-character delimiters (see section 7.1.1, ``Lexical
166 #define CHAR_IS_R5RS_DELIMITER(c) \
168 || (c == ')') || (c == '(') || (c == ';') || (c == '"'))
170 #define CHAR_IS_DELIMITER CHAR_IS_R5RS_DELIMITER
172 /* Exponent markers, as defined in section 7.1.1 of R5RS, ``Lexical
174 #define CHAR_IS_EXPONENT_MARKER(_chr) \
175 (((_chr) == 'e') || ((_chr) == 's') || ((_chr) == 'f') \
176 || ((_chr) == 'd') || ((_chr) == 'l'))
178 /* An inlinable version of `scm_c_downcase ()'. */
179 #define CHAR_DOWNCASE(_chr) \
180 (((_chr) <= UCHAR_MAX) ? tolower (_chr) : (_chr))
183 /* Read an SCSH block comment. */
184 static inline SCM
scm_read_scsh_block_comment (int chr
, SCM port
);
186 /* Read from PORT until a delimiter (e.g., a whitespace) is read. Return
187 zero if the whole token fits in BUF, non-zero otherwise. */
189 read_token (SCM port
, char *buf
, size_t buf_size
, size_t *read
)
193 while (*read
< buf_size
)
197 chr
= scm_getc (port
);
198 chr
= (SCM_CASE_INSENSITIVE_P
? CHAR_DOWNCASE (chr
) : chr
);
202 else if (CHAR_IS_DELIMITER (chr
))
204 scm_ungetc (chr
, port
);
218 /* Skip whitespace from PORT and return the first non-whitespace character
219 read. Raise an error on end-of-file. */
221 flush_ws (SCM port
, const char *eoferr
)
225 switch (c
= scm_getc (port
))
231 scm_i_input_error (eoferr
,
240 switch (c
= scm_getc (port
))
246 case SCM_LINE_INCREMENTORS
:
252 switch (c
= scm_getc (port
))
255 eoferr
= "read_sharp";
258 scm_read_scsh_block_comment (c
, port
);
261 scm_ungetc (c
, port
);
266 case SCM_LINE_INCREMENTORS
:
267 case SCM_SINGLE_SPACES
:
282 static SCM
scm_read_expression (SCM port
);
283 static SCM
scm_read_sharp (int chr
, SCM port
);
284 static SCM
scm_get_hash_procedure (int c
);
285 static SCM
recsexpr (SCM obj
, long line
, int column
, SCM filename
);
289 scm_read_sexp (int chr
, SCM port
)
290 #define FUNC_NAME "scm_i_lreadparen"
294 register SCM tl
, ans
= SCM_EOL
;
295 SCM tl2
= SCM_EOL
, ans2
= SCM_EOL
, copy
= SCM_BOOL_F
;
296 static const int terminating_char
= ')';
298 /* Need to capture line and column numbers here. */
299 long line
= SCM_LINUM (port
);
300 int column
= SCM_COL (port
) - 1;
303 c
= flush_ws (port
, FUNC_NAME
);
304 if (terminating_char
== c
)
307 scm_ungetc (c
, port
);
308 if (scm_is_eq (scm_sym_dot
,
309 (tmp
= scm_read_expression (port
))))
311 ans
= scm_read_expression (port
);
312 if (terminating_char
!= (c
= flush_ws (port
, FUNC_NAME
)))
313 scm_i_input_error (FUNC_NAME
, port
, "missing close paren",
318 /* Build the head of the list structure. */
319 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
321 if (SCM_COPY_SOURCE_P
)
322 ans2
= tl2
= scm_cons (scm_is_pair (tmp
)
327 while (terminating_char
!= (c
= flush_ws (port
, FUNC_NAME
)))
331 scm_ungetc (c
, port
);
332 if (scm_is_eq (scm_sym_dot
,
333 (tmp
= scm_read_expression (port
))))
335 SCM_SETCDR (tl
, tmp
= scm_read_expression (port
));
337 if (SCM_COPY_SOURCE_P
)
338 SCM_SETCDR (tl2
, scm_cons (scm_is_pair (tmp
) ? copy
: tmp
,
341 c
= flush_ws (port
, FUNC_NAME
);
342 if (terminating_char
!= c
)
343 scm_i_input_error (FUNC_NAME
, port
,
344 "in pair: missing close paren", SCM_EOL
);
348 new_tail
= scm_cons (tmp
, SCM_EOL
);
349 SCM_SETCDR (tl
, new_tail
);
352 if (SCM_COPY_SOURCE_P
)
354 SCM new_tail2
= scm_cons (scm_is_pair (tmp
)
357 SCM_SETCDR (tl2
, new_tail2
);
363 if (SCM_RECORD_POSITIONS_P
)
364 scm_whash_insert (scm_source_whash
,
366 scm_make_srcprops (line
, column
,
377 scm_read_string (int chr
, SCM port
)
378 #define FUNC_NAME "scm_lreadr"
380 /* For strings smaller than C_STR, this function creates only one Scheme
381 object (the string returned). */
383 SCM str
= SCM_BOOL_F
;
384 char c_str
[READER_STRING_BUFFER_SIZE
];
385 unsigned c_str_len
= 0;
388 while ('"' != (c
= scm_getc (port
)))
391 str_eof
: scm_i_input_error (FUNC_NAME
, port
,
392 "end of file in string constant",
395 if (c_str_len
+ 1 >= sizeof (c_str
))
397 /* Flush the C buffer onto a Scheme string. */
400 if (str
== SCM_BOOL_F
)
401 str
= scm_c_make_string (0, SCM_MAKE_CHAR ('X'));
403 addy
= scm_from_locale_stringn (c_str
, c_str_len
);
404 str
= scm_string_append_shared (scm_list_2 (str
, addy
));
410 switch (c
= scm_getc (port
))
420 if (SCM_ESCAPED_PARENS_P
)
451 if (a
== EOF
) goto str_eof
;
453 if (b
== EOF
) goto str_eof
;
454 if ('0' <= a
&& a
<= '9') a
-= '0';
455 else if ('A' <= a
&& a
<= 'F') a
= a
- 'A' + 10;
456 else if ('a' <= a
&& a
<= 'f') a
= a
- 'a' + 10;
457 else goto bad_escaped
;
458 if ('0' <= b
&& b
<= '9') b
-= '0';
459 else if ('A' <= b
&& b
<= 'F') b
= b
- 'A' + 10;
460 else if ('a' <= b
&& b
<= 'f') b
= b
- 'a' + 10;
461 else goto bad_escaped
;
467 scm_i_input_error (FUNC_NAME
, port
,
468 "illegal character in escape sequence: ~S",
469 scm_list_1 (SCM_MAKE_CHAR (c
)));
471 c_str
[c_str_len
++] = c
;
478 addy
= scm_from_locale_stringn (c_str
, c_str_len
);
479 if (str
== SCM_BOOL_F
)
482 str
= scm_string_append_shared (scm_list_2 (str
, addy
));
485 str
= (str
== SCM_BOOL_F
) ? scm_nullstr
: str
;
493 scm_read_number (int chr
, SCM port
)
495 SCM result
, str
= SCM_EOL
;
496 char buffer
[READER_BUFFER_SIZE
];
500 scm_ungetc (chr
, port
);
503 overflow
= read_token (port
, buffer
, sizeof (buffer
), &read
);
505 if ((overflow
) || (scm_is_pair (str
)))
506 str
= scm_cons (scm_from_locale_stringn (buffer
, read
), str
);
510 if (scm_is_pair (str
))
514 str
= scm_string_concatenate (scm_reverse_x (str
, SCM_EOL
));
515 result
= scm_string_to_number (str
, SCM_UNDEFINED
);
516 if (!scm_is_true (result
))
517 /* Return a symbol instead of a number. */
518 result
= scm_string_to_symbol (str
);
522 result
= scm_c_locale_stringn_to_number (buffer
, read
, 10);
523 if (!scm_is_true (result
))
524 /* Return a symbol instead of a number. */
525 result
= scm_from_locale_symboln (buffer
, read
);
532 scm_read_mixed_case_symbol (int chr
, SCM port
)
534 SCM result
, str
= SCM_EOL
;
535 int overflow
= 0, ends_with_colon
= 0;
536 char buffer
[READER_BUFFER_SIZE
];
538 int postfix
= scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_postfix
);
540 scm_ungetc (chr
, port
);
543 overflow
= read_token (port
, buffer
, sizeof (buffer
), &read
);
546 ends_with_colon
= (buffer
[read
- 1] == ':');
548 if ((overflow
) || (scm_is_pair (str
)))
549 str
= scm_cons (scm_from_locale_stringn (buffer
, read
), str
);
553 if (scm_is_pair (str
))
557 str
= scm_string_concatenate (scm_reverse_x (str
, SCM_EOL
));
558 len
= scm_c_string_length (str
);
560 /* Per SRFI-88, `:' alone is an identifier, not a keyword. */
561 if (postfix
&& ends_with_colon
&& (len
> 1))
563 /* Strip off colon. */
564 str
= scm_c_substring (str
, 0, len
-1);
565 result
= scm_string_to_symbol (str
);
566 result
= scm_symbol_to_keyword (result
);
569 result
= scm_string_to_symbol (str
);
573 /* For symbols smaller than `sizeof (buffer)', we don't need to recur
574 to Scheme strings. Therefore, we only create one Scheme object (a
575 symbol) per symbol read. */
576 if (postfix
&& ends_with_colon
&& (read
> 1))
577 result
= scm_from_locale_keywordn (buffer
, read
- 1);
579 result
= scm_from_locale_symboln (buffer
, read
);
586 scm_read_number_and_radix (int chr
, SCM port
)
587 #define FUNC_NAME "scm_lreadr"
589 SCM result
, str
= SCM_EOL
;
591 char buffer
[READER_BUFFER_SIZE
];
618 scm_ungetc (chr
, port
);
619 scm_ungetc ('#', port
);
625 overflow
= read_token (port
, buffer
, sizeof (buffer
), &read
);
627 if ((overflow
) || (scm_is_pair (str
)))
628 str
= scm_cons (scm_from_locale_stringn (buffer
, read
), str
);
632 if (scm_is_pair (str
))
634 str
= scm_string_concatenate (scm_reverse_x (str
, SCM_EOL
));
635 result
= scm_string_to_number (str
, scm_from_uint (radix
));
638 result
= scm_c_locale_stringn_to_number (buffer
, read
, radix
);
640 if (scm_is_true (result
))
643 scm_i_input_error (FUNC_NAME
, port
, "unknown # object", SCM_EOL
);
650 scm_read_quote (int chr
, SCM port
)
653 long line
= SCM_LINUM (port
);
654 int column
= SCM_COL (port
) - 1;
659 p
= scm_sym_quasiquote
;
672 p
= scm_sym_uq_splicing
;
675 scm_ungetc (c
, port
);
682 fprintf (stderr
, "%s: unhandled quote character (%i)\n",
683 "scm_read_quote", chr
);
687 p
= scm_cons2 (p
, scm_read_expression (port
), SCM_EOL
);
688 if (SCM_RECORD_POSITIONS_P
)
689 scm_whash_insert (scm_source_whash
, p
,
690 scm_make_srcprops (line
, column
,
693 ? (scm_cons2 (SCM_CAR (p
),
694 SCM_CAR (SCM_CDR (p
)),
704 scm_read_semicolon_comment (int chr
, SCM port
)
708 for (c
= scm_getc (port
);
709 (c
!= EOF
) && (c
!= '\n');
710 c
= scm_getc (port
));
712 return SCM_UNSPECIFIED
;
716 /* Sharp readers, i.e. readers called after a `#' sign has been read. */
719 scm_read_boolean (int chr
, SCM port
)
732 return SCM_UNSPECIFIED
;
736 scm_read_character (int chr
, SCM port
)
737 #define FUNC_NAME "scm_lreadr"
740 char charname
[READER_CHAR_NAME_MAX_SIZE
];
743 if (read_token (port
, charname
, sizeof (charname
), &charname_len
))
746 if (charname_len
== 0)
748 chr
= scm_getc (port
);
750 scm_i_input_error (FUNC_NAME
, port
, "unexpected end of file "
751 "while reading character", SCM_EOL
);
753 /* CHR must be a token delimiter, like a whitespace. */
754 return (SCM_MAKE_CHAR (chr
));
757 if (charname_len
== 1)
758 return SCM_MAKE_CHAR (charname
[0]);
760 if (*charname
>= '0' && *charname
< '8')
762 /* Dirk:FIXME:: This type of character syntax is not R5RS
763 * compliant. Further, it should be verified that the constant
764 * does only consist of octal digits. Finally, it should be
765 * checked whether the resulting fixnum is in the range of
767 SCM p
= scm_c_locale_stringn_to_number (charname
, charname_len
, 8);
769 return SCM_MAKE_CHAR (SCM_I_INUM (p
));
772 for (c
= 0; c
< scm_n_charnames
; c
++)
774 && (!strncasecmp (scm_charnames
[c
], charname
, charname_len
)))
775 return SCM_MAKE_CHAR (scm_charnums
[c
]);
778 scm_i_input_error (FUNC_NAME
, port
, "unknown character name ~a",
779 scm_list_1 (scm_from_locale_stringn (charname
,
782 return SCM_UNSPECIFIED
;
787 scm_read_keyword (int chr
, SCM port
)
791 /* Read the symbol that comprises the keyword. Doing this instead of
792 invoking a specific symbol reader function allows `scm_read_keyword ()'
793 to adapt to the delimiters currently valid of symbols.
795 XXX: This implementation allows sloppy syntaxes like `#: key'. */
796 symbol
= scm_read_expression (port
);
797 if (!scm_is_symbol (symbol
))
798 scm_i_input_error ("scm_read_keyword", port
,
799 "keyword prefix `~a' not followed by a symbol: ~s",
800 scm_list_2 (SCM_MAKE_CHAR (chr
), symbol
));
802 return (scm_symbol_to_keyword (symbol
));
806 scm_read_vector (int chr
, SCM port
)
808 /* Note: We call `scm_read_sexp ()' rather than READER here in order to
809 guarantee that it's going to do what we want. After all, this is an
810 implementation detail of `scm_read_vector ()', not a desirable
812 return (scm_vector (scm_read_sexp (chr
, port
)));
816 scm_read_srfi4_vector (int chr
, SCM port
)
818 return scm_i_read_array (port
, chr
);
822 scm_read_guile_bit_vector (int chr
, SCM port
)
824 /* Read the `#*10101'-style read syntax for bit vectors in Guile. This is
825 terribly inefficient but who cares? */
826 SCM s_bits
= SCM_EOL
;
828 for (chr
= scm_getc (port
);
829 (chr
!= EOF
) && ((chr
== '0') || (chr
== '1'));
830 chr
= scm_getc (port
))
832 s_bits
= scm_cons ((chr
== '0') ? SCM_BOOL_F
: SCM_BOOL_T
, s_bits
);
836 scm_ungetc (chr
, port
);
838 return scm_bitvector (scm_reverse_x (s_bits
, SCM_EOL
));
842 scm_read_scsh_block_comment (int chr
, SCM port
)
848 int c
= scm_getc (port
);
851 scm_i_input_error ("skip_block_comment", port
,
852 "unterminated `#! ... !#' comment", SCM_EOL
);
856 else if (c
== '#' && bang_seen
)
862 return SCM_UNSPECIFIED
;
866 scm_read_extended_symbol (int chr
, SCM port
)
868 /* Guile's extended symbol read syntax looks like this:
870 #{This is all a symbol name}#
872 So here, CHR is expected to be `{'. */
874 int saw_brace
= 0, finished
= 0;
878 result
= scm_c_make_string (0, SCM_MAKE_CHAR ('X'));
880 while ((chr
= scm_getc (port
)) != EOF
)
901 if (len
>= sizeof (buf
) - 2)
903 scm_string_append (scm_list_2 (result
,
904 scm_from_locale_stringn (buf
, len
)));
913 result
= scm_string_append (scm_list_2
915 scm_from_locale_stringn (buf
, len
)));
917 return (scm_string_to_symbol (result
));
922 /* Top-level token readers, i.e., dispatchers. */
925 scm_read_sharp_extension (int chr
, SCM port
)
929 proc
= scm_get_hash_procedure (chr
);
930 if (scm_is_true (scm_procedure_p (proc
)))
932 long line
= SCM_LINUM (port
);
933 int column
= SCM_COL (port
) - 2;
936 got
= scm_call_2 (proc
, SCM_MAKE_CHAR (chr
), port
);
937 if (!scm_is_eq (got
, SCM_UNSPECIFIED
))
939 if (SCM_RECORD_POSITIONS_P
)
940 return (recsexpr (got
, line
, column
,
941 SCM_FILENAME (port
)));
947 return SCM_UNSPECIFIED
;
950 /* The reader for the sharp `#' character. It basically dispatches reads
951 among the above token readers. */
953 scm_read_sharp (int chr
, SCM port
)
954 #define FUNC_NAME "scm_lreadr"
958 chr
= scm_getc (port
);
960 result
= scm_read_sharp_extension (chr
, port
);
961 if (!scm_is_eq (result
, SCM_UNSPECIFIED
))
967 return (scm_read_character (chr
, port
));
969 return (scm_read_vector (chr
, port
));
973 /* This one may return either a boolean or an SRFI-4 vector. */
974 return (scm_read_srfi4_vector (chr
, port
));
976 return (scm_read_guile_bit_vector (chr
, port
));
980 /* This one may return either a boolean or an SRFI-4 vector. */
981 return (scm_read_boolean (chr
, port
));
983 return (scm_read_keyword (chr
, port
));
984 case '0': case '1': case '2': case '3': case '4':
985 case '5': case '6': case '7': case '8': case '9':
987 #if SCM_ENABLE_DEPRECATED
988 /* See below for 'i' and 'e'. */
995 return (scm_i_read_array (port
, chr
));
999 #if SCM_ENABLE_DEPRECATED
1001 /* When next char is '(', it really is an old-style
1003 int next_c
= scm_getc (port
);
1005 scm_ungetc (next_c
, port
);
1007 return scm_i_read_array (port
, chr
);
1021 return (scm_read_number_and_radix (chr
, port
));
1023 return (scm_read_extended_symbol (chr
, port
));
1025 return (scm_read_scsh_block_comment (chr
, port
));
1027 result
= scm_read_sharp_extension (chr
, port
);
1028 if (scm_is_eq (result
, SCM_UNSPECIFIED
))
1029 scm_i_input_error (FUNC_NAME
, port
, "Unknown # object: ~S",
1030 scm_list_1 (SCM_MAKE_CHAR (chr
)));
1035 return SCM_UNSPECIFIED
;
1040 scm_read_expression (SCM port
)
1041 #define FUNC_NAME "scm_read_expression"
1047 chr
= scm_getc (port
);
1051 case SCM_WHITE_SPACES
:
1052 case SCM_LINE_INCREMENTORS
:
1055 (void) scm_read_semicolon_comment (chr
, port
);
1058 return (scm_read_sexp (chr
, port
));
1060 return (scm_read_string (chr
, port
));
1064 return (scm_read_quote (chr
, port
));
1068 result
= scm_read_sharp (chr
, port
);
1069 if (scm_is_eq (result
, SCM_UNSPECIFIED
))
1070 /* We read a comment or some such. */
1076 scm_i_input_error (FUNC_NAME
, port
, "unexpected \")\"", SCM_EOL
);
1081 if (scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_prefix
))
1082 return scm_symbol_to_keyword (scm_read_expression (port
));
1087 if (((chr
>= '0') && (chr
<= '9'))
1088 || (strchr ("+-.", chr
)))
1089 return (scm_read_number (chr
, port
));
1091 return (scm_read_mixed_case_symbol (chr
, port
));
1099 /* Actual reader. */
1101 SCM_DEFINE (scm_read
, "read", 0, 1, 0,
1103 "Read an s-expression from the input port @var{port}, or from\n"
1104 "the current input port if @var{port} is not specified.\n"
1105 "Any whitespace before the next token is discarded.")
1106 #define FUNC_NAME s_scm_read
1110 if (SCM_UNBNDP (port
))
1111 port
= scm_current_input_port ();
1112 SCM_VALIDATE_OPINPORT (1, port
);
1114 c
= flush_ws (port
, (char *) NULL
);
1117 scm_ungetc (c
, port
);
1119 return (scm_read_expression (port
));
1126 /* Used when recording expressions constructed by `scm_read_sharp ()'. */
1128 recsexpr (SCM obj
, long line
, int column
, SCM filename
)
1130 if (!scm_is_pair(obj
)) {
1133 SCM tmp
= obj
, copy
;
1134 /* If this sexpr is visible in the read:sharp source, we want to
1135 keep that information, so only record non-constant cons cells
1136 which haven't previously been read by the reader. */
1137 if (scm_is_false (scm_whash_lookup (scm_source_whash
, obj
)))
1139 if (SCM_COPY_SOURCE_P
)
1141 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
1143 while ((tmp
= SCM_CDR (tmp
)) && scm_is_pair (tmp
))
1145 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
1150 copy
= SCM_CDR (copy
);
1152 SCM_SETCDR (copy
, tmp
);
1156 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
1157 while ((tmp
= SCM_CDR (tmp
)) && scm_is_pair (tmp
))
1158 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
1159 copy
= SCM_UNDEFINED
;
1161 scm_whash_insert (scm_source_whash
,
1163 scm_make_srcprops (line
,
1173 /* Manipulate the read-hash-procedures alist. This could be written in
1174 Scheme, but maybe it will also be used by C code during initialisation. */
1175 SCM_DEFINE (scm_read_hash_extend
, "read-hash-extend", 2, 0, 0,
1176 (SCM chr
, SCM proc
),
1177 "Install the procedure @var{proc} for reading expressions\n"
1178 "starting with the character sequence @code{#} and @var{chr}.\n"
1179 "@var{proc} will be called with two arguments: the character\n"
1180 "@var{chr} and the port to read further data from. The object\n"
1181 "returned will be the return value of @code{read}. \n"
1182 "Passing @code{#f} for @var{proc} will remove a previous setting. \n"
1184 #define FUNC_NAME s_scm_read_hash_extend
1189 SCM_VALIDATE_CHAR (1, chr
);
1190 SCM_ASSERT (scm_is_false (proc
)
1191 || scm_is_eq (scm_procedure_p (proc
), SCM_BOOL_T
),
1192 proc
, SCM_ARG2
, FUNC_NAME
);
1194 /* Check if chr is already in the alist. */
1195 this = *scm_read_hash_procedures
;
1199 if (scm_is_null (this))
1201 /* not found, so add it to the beginning. */
1202 if (scm_is_true (proc
))
1204 *scm_read_hash_procedures
=
1205 scm_cons (scm_cons (chr
, proc
), *scm_read_hash_procedures
);
1209 if (scm_is_eq (chr
, SCM_CAAR (this)))
1211 /* already in the alist. */
1212 if (scm_is_false (proc
))
1215 if (scm_is_false (prev
))
1217 *scm_read_hash_procedures
=
1218 SCM_CDR (*scm_read_hash_procedures
);
1221 scm_set_cdr_x (prev
, SCM_CDR (this));
1226 scm_set_cdr_x (SCM_CAR (this), proc
);
1231 this = SCM_CDR (this);
1234 return SCM_UNSPECIFIED
;
1238 /* Recover the read-hash procedure corresponding to char c. */
1240 scm_get_hash_procedure (int c
)
1242 SCM rest
= *scm_read_hash_procedures
;
1246 if (scm_is_null (rest
))
1249 if (SCM_CHAR (SCM_CAAR (rest
)) == c
)
1250 return SCM_CDAR (rest
);
1252 rest
= SCM_CDR (rest
);
1259 scm_read_hash_procedures
=
1260 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL
));
1262 scm_init_opts (scm_read_options
, scm_read_opts
);
1263 #include "libguile/read.x"