1 /* Copyright (C) 1995,1996,1997,1999,2000,2001,2003, 2004, 2006, 2007, 2008, 2009 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 License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * 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
33 #include "libguile/_scm.h"
34 #include "libguile/bytevectors.h"
35 #include "libguile/chars.h"
36 #include "libguile/eval.h"
37 #include "libguile/arrays.h"
38 #include "libguile/bitvectors.h"
39 #include "libguile/keywords.h"
40 #include "libguile/alist.h"
41 #include "libguile/srcprop.h"
42 #include "libguile/hashtab.h"
43 #include "libguile/hash.h"
44 #include "libguile/ports.h"
45 #include "libguile/root.h"
46 #include "libguile/strings.h"
47 #include "libguile/strports.h"
48 #include "libguile/vectors.h"
49 #include "libguile/validate.h"
50 #include "libguile/srfi-4.h"
51 #include "libguile/srfi-13.h"
53 #include "libguile/read.h"
54 #include "libguile/private-options.h"
59 SCM_GLOBAL_SYMBOL (scm_sym_dot
, ".");
60 SCM_SYMBOL (scm_keyword_prefix
, "prefix");
61 SCM_SYMBOL (scm_keyword_postfix
, "postfix");
63 scm_t_option scm_read_opts
[] = {
64 { SCM_OPTION_BOOLEAN
, "copy", 0,
65 "Copy source code expressions." },
66 { SCM_OPTION_BOOLEAN
, "positions", 0,
67 "Record positions of source code expressions." },
68 { SCM_OPTION_BOOLEAN
, "case-insensitive", 0,
69 "Convert symbols to lower case."},
70 { SCM_OPTION_SCM
, "keywords", SCM_UNPACK (SCM_BOOL_F
),
71 "Style of keyword recognition: #f, 'prefix or 'postfix."},
73 { SCM_OPTION_BOOLEAN
, "elisp-vectors", 0,
74 "Support Elisp vector syntax, namely `[...]'."},
75 { SCM_OPTION_BOOLEAN
, "elisp-strings", 0,
76 "Support `\\(' and `\\)' in strings."},
82 Give meaningful error messages for errors
86 FILE:LINE:COL: MESSAGE
89 This is not standard GNU format, but the test-suite likes the real
90 message to be in front.
96 scm_i_input_error (char const *function
,
97 SCM port
, const char *message
, SCM arg
)
99 SCM fn
= (scm_is_string (SCM_FILENAME(port
))
101 : scm_from_locale_string ("#<unknown port>"));
103 SCM string_port
= scm_open_output_string ();
104 SCM string
= SCM_EOL
;
105 scm_simple_format (string_port
,
106 scm_from_locale_string ("~A:~S:~S: ~A"),
108 scm_from_long (SCM_LINUM (port
) + 1),
109 scm_from_int (SCM_COL (port
) + 1),
110 scm_from_locale_string (message
)));
112 string
= scm_get_output_string (string_port
);
113 scm_close_output_port (string_port
);
114 scm_error_scm (scm_from_locale_symbol ("read-error"),
115 function
? scm_from_locale_string (function
) : SCM_BOOL_F
,
122 SCM_DEFINE (scm_read_options
, "read-options-interface", 0, 1, 0,
124 "Option interface for the read options. Instead of using\n"
125 "this procedure directly, use the procedures @code{read-enable},\n"
126 "@code{read-disable}, @code{read-set!} and @code{read-options}.")
127 #define FUNC_NAME s_scm_read_options
129 SCM ans
= scm_options (setting
,
132 if (SCM_COPY_SOURCE_P
)
133 SCM_RECORD_POSITIONS_P
= 1;
138 /* An association list mapping extra hash characters to procedures. */
139 static SCM
*scm_read_hash_procedures
;
146 /* Size of the C buffer used to read symbols and numbers. */
147 #define READER_BUFFER_SIZE 128
149 /* Size of the C buffer used to read strings. */
150 #define READER_STRING_BUFFER_SIZE 512
152 /* The maximum size of Scheme character names. */
153 #define READER_CHAR_NAME_MAX_SIZE 50
156 /* `isblank' is only in C99. */
157 #define CHAR_IS_BLANK_(_chr) \
158 (((_chr) == ' ') || ((_chr) == '\t') || ((_chr) == '\n') \
159 || ((_chr) == '\f') || ((_chr) == '\r'))
162 # define CHAR_IS_BLANK(_chr) \
163 ((CHAR_IS_BLANK_ (chr)) || ((_chr) == 26))
165 # define CHAR_IS_BLANK CHAR_IS_BLANK_
169 /* R5RS one-character delimiters (see section 7.1.1, ``Lexical
171 #define CHAR_IS_R5RS_DELIMITER(c) \
173 || (c == ')') || (c == '(') || (c == ';') || (c == '"'))
175 #define CHAR_IS_DELIMITER CHAR_IS_R5RS_DELIMITER
177 /* Exponent markers, as defined in section 7.1.1 of R5RS, ``Lexical
179 #define CHAR_IS_EXPONENT_MARKER(_chr) \
180 (((_chr) == 'e') || ((_chr) == 's') || ((_chr) == 'f') \
181 || ((_chr) == 'd') || ((_chr) == 'l'))
183 /* Read an SCSH block comment. */
184 static inline SCM
scm_read_scsh_block_comment (int chr
, SCM port
);
185 static SCM
scm_read_commented_expression (int chr
, SCM port
);
187 /* Read from PORT until a delimiter (e.g., a whitespace) is read. Return
188 zero if the whole token fits in BUF, non-zero otherwise. */
190 read_token (SCM port
, SCM buf
, size_t *read
)
195 buf
= scm_i_string_start_writing (buf
);
196 while (*read
< scm_i_string_length (buf
))
198 chr
= scm_getc (port
);
202 scm_i_string_stop_writing ();
206 chr
= (SCM_CASE_INSENSITIVE_P
? uc_tolower (chr
) : chr
);
208 if (CHAR_IS_DELIMITER (chr
))
210 scm_i_string_stop_writing ();
211 scm_ungetc (chr
, port
);
215 scm_i_string_set_x (buf
, *read
, chr
);
218 scm_i_string_stop_writing ();
224 read_complete_token (SCM port
, size_t *read
)
226 SCM buffer
, str
= SCM_EOL
;
230 buffer
= scm_i_make_string (READER_BUFFER_SIZE
, NULL
);
231 overflow
= read_token (port
, buffer
, read
);
233 return scm_i_substring (buffer
, 0, *read
);
235 str
= scm_string_copy (buffer
);
238 overflow
= read_token (port
, buffer
, &len
);
239 str
= scm_string_append (scm_list_2 (str
, buffer
));
244 return scm_i_substring (str
, 0, *read
);
247 /* Skip whitespace from PORT and return the first non-whitespace character
248 read. Raise an error on end-of-file. */
250 flush_ws (SCM port
, const char *eoferr
)
252 register scm_t_wchar c
;
254 switch (c
= scm_getc (port
))
260 scm_i_input_error (eoferr
,
269 switch (c
= scm_getc (port
))
275 case SCM_LINE_INCREMENTORS
:
281 switch (c
= scm_getc (port
))
284 eoferr
= "read_sharp";
287 scm_read_scsh_block_comment (c
, port
);
290 scm_read_commented_expression (c
, port
);
293 scm_ungetc (c
, port
);
298 case SCM_LINE_INCREMENTORS
:
299 case SCM_SINGLE_SPACES
:
314 static SCM
scm_read_expression (SCM port
);
315 static SCM
scm_read_sharp (int chr
, SCM port
);
316 static SCM
scm_get_hash_procedure (int c
);
317 static SCM
recsexpr (SCM obj
, long line
, int column
, SCM filename
);
321 scm_read_sexp (scm_t_wchar chr
, SCM port
)
322 #define FUNC_NAME "scm_i_lreadparen"
326 register SCM tl
, ans
= SCM_EOL
;
327 SCM tl2
= SCM_EOL
, ans2
= SCM_EOL
, copy
= SCM_BOOL_F
;
328 static const int terminating_char
= ')';
330 /* Need to capture line and column numbers here. */
331 long line
= SCM_LINUM (port
);
332 int column
= SCM_COL (port
) - 1;
335 c
= flush_ws (port
, FUNC_NAME
);
336 if (terminating_char
== c
)
339 scm_ungetc (c
, port
);
340 if (scm_is_eq (scm_sym_dot
,
341 (tmp
= scm_read_expression (port
))))
343 ans
= scm_read_expression (port
);
344 if (terminating_char
!= (c
= flush_ws (port
, FUNC_NAME
)))
345 scm_i_input_error (FUNC_NAME
, port
, "missing close paren",
350 /* Build the head of the list structure. */
351 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
353 if (SCM_COPY_SOURCE_P
)
354 ans2
= tl2
= scm_cons (scm_is_pair (tmp
)
359 while (terminating_char
!= (c
= flush_ws (port
, FUNC_NAME
)))
363 scm_ungetc (c
, port
);
364 if (scm_is_eq (scm_sym_dot
,
365 (tmp
= scm_read_expression (port
))))
367 SCM_SETCDR (tl
, tmp
= scm_read_expression (port
));
369 if (SCM_COPY_SOURCE_P
)
370 SCM_SETCDR (tl2
, scm_cons (scm_is_pair (tmp
) ? copy
: tmp
,
373 c
= flush_ws (port
, FUNC_NAME
);
374 if (terminating_char
!= c
)
375 scm_i_input_error (FUNC_NAME
, port
,
376 "in pair: missing close paren", SCM_EOL
);
380 new_tail
= scm_cons (tmp
, SCM_EOL
);
381 SCM_SETCDR (tl
, new_tail
);
384 if (SCM_COPY_SOURCE_P
)
386 SCM new_tail2
= scm_cons (scm_is_pair (tmp
)
389 SCM_SETCDR (tl2
, new_tail2
);
395 if (SCM_RECORD_POSITIONS_P
)
396 scm_whash_insert (scm_source_whash
,
398 scm_make_srcprops (line
, column
,
409 scm_read_string (int chr
, SCM port
)
410 #define FUNC_NAME "scm_lreadr"
412 /* For strings smaller than C_STR, this function creates only one Scheme
413 object (the string returned). */
415 SCM str
= SCM_BOOL_F
;
416 unsigned c_str_len
= 0;
419 str
= scm_i_make_string (READER_STRING_BUFFER_SIZE
, NULL
);
420 while ('"' != (c
= scm_getc (port
)))
425 scm_i_input_error (FUNC_NAME
, port
,
426 "end of file in string constant", SCM_EOL
);
429 if (c_str_len
+ 1 >= scm_i_string_length (str
))
431 SCM addy
= scm_i_make_string (READER_STRING_BUFFER_SIZE
, NULL
);
433 str
= scm_string_append (scm_list_2 (str
, addy
));
438 switch (c
= scm_getc (port
))
448 if (SCM_ESCAPED_PARENS_P
)
484 if ('0' <= a
&& a
<= '9')
486 else if ('A' <= a
&& a
<= 'F')
488 else if ('a' <= a
&& a
<= 'f')
495 if ('0' <= b
&& b
<= '9')
497 else if ('A' <= b
&& b
<= 'F')
499 else if ('a' <= b
&& b
<= 'f')
514 for (i
= 0; i
< 4; i
++)
519 if ('0' <= a
&& a
<= '9')
521 else if ('A' <= a
&& a
<= 'F')
523 else if ('a' <= a
&& a
<= 'f')
539 for (i
= 0; i
< 6; i
++)
544 if ('0' <= a
&& a
<= '9')
546 else if ('A' <= a
&& a
<= 'F')
548 else if ('a' <= a
&& a
<= 'f')
561 scm_i_input_error (FUNC_NAME
, port
,
562 "illegal character in escape sequence: ~S",
563 scm_list_1 (SCM_MAKE_CHAR (c
)));
566 str
= scm_i_string_start_writing (str
);
567 scm_i_string_set_x (str
, c_str_len
++, c
);
568 scm_i_string_stop_writing ();
573 return scm_i_substring_copy (str
, 0, c_str_len
);
582 scm_read_number (scm_t_wchar chr
, SCM port
)
588 scm_ungetc (chr
, port
);
589 buffer
= read_complete_token (port
, &read
);
590 result
= scm_string_to_number (buffer
, SCM_UNDEFINED
);
591 if (!scm_is_true (result
))
592 /* Return a symbol instead of a number. */
593 result
= scm_string_to_symbol (buffer
);
599 scm_read_mixed_case_symbol (scm_t_wchar chr
, SCM port
)
602 int ends_with_colon
= 0;
605 int postfix
= scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_postfix
);
607 scm_ungetc (chr
, port
);
608 buffer
= read_complete_token (port
, &read
);
610 ends_with_colon
= scm_i_string_ref (buffer
, read
- 1) == ':';
612 if (postfix
&& ends_with_colon
&& (read
> 1))
613 result
= scm_symbol_to_keyword (scm_string_to_symbol (scm_i_substring (buffer
, 0, read
- 1)));
615 result
= scm_string_to_symbol (buffer
);
621 scm_read_number_and_radix (scm_t_wchar chr
, SCM port
)
622 #define FUNC_NAME "scm_lreadr"
626 SCM buffer
= scm_i_make_string (READER_BUFFER_SIZE
, NULL
);
652 scm_ungetc (chr
, port
);
653 scm_ungetc ('#', port
);
657 buffer
= read_complete_token (port
, &read
);
658 result
= scm_string_to_number (buffer
, scm_from_uint (radix
));
660 if (scm_is_true (result
))
663 scm_i_input_error (FUNC_NAME
, port
, "unknown # object", SCM_EOL
);
670 scm_read_quote (int chr
, SCM port
)
673 long line
= SCM_LINUM (port
);
674 int column
= SCM_COL (port
) - 1;
679 p
= scm_sym_quasiquote
;
692 p
= scm_sym_uq_splicing
;
695 scm_ungetc (c
, port
);
702 fprintf (stderr
, "%s: unhandled quote character (%i)\n",
703 "scm_read_quote", chr
);
707 p
= scm_cons2 (p
, scm_read_expression (port
), SCM_EOL
);
708 if (SCM_RECORD_POSITIONS_P
)
709 scm_whash_insert (scm_source_whash
, p
,
710 scm_make_srcprops (line
, column
,
713 ? (scm_cons2 (SCM_CAR (p
),
714 SCM_CAR (SCM_CDR (p
)),
723 SCM_SYMBOL (sym_syntax
, "syntax");
724 SCM_SYMBOL (sym_quasisyntax
, "quasisyntax");
725 SCM_SYMBOL (sym_unsyntax
, "unsyntax");
726 SCM_SYMBOL (sym_unsyntax_splicing
, "unsyntax-splicing");
729 scm_read_syntax (int chr
, SCM port
)
732 long line
= SCM_LINUM (port
);
733 int column
= SCM_COL (port
) - 1;
751 p
= sym_unsyntax_splicing
;
754 scm_ungetc (c
, port
);
761 fprintf (stderr
, "%s: unhandled syntax character (%i)\n",
762 "scm_read_syntax", chr
);
766 p
= scm_cons2 (p
, scm_read_expression (port
), SCM_EOL
);
767 if (SCM_RECORD_POSITIONS_P
)
768 scm_whash_insert (scm_source_whash
, p
,
769 scm_make_srcprops (line
, column
,
772 ? (scm_cons2 (SCM_CAR (p
),
773 SCM_CAR (SCM_CDR (p
)),
783 scm_read_semicolon_comment (int chr
, SCM port
)
787 /* We use the get_byte here because there is no need to get the
788 locale correct with comment input. This presumes that newline
789 always represents itself no matter what the encoding is. */
790 for (c
= scm_get_byte_or_eof (port
);
791 (c
!= EOF
) && (c
!= '\n');
792 c
= scm_getc (port
));
794 return SCM_UNSPECIFIED
;
798 /* Sharp readers, i.e. readers called after a `#' sign has been read. */
801 scm_read_boolean (int chr
, SCM port
)
814 return SCM_UNSPECIFIED
;
818 scm_read_character (scm_t_wchar chr
, SCM port
)
819 #define FUNC_NAME "scm_lreadr"
821 SCM charname
= scm_i_make_string (READER_CHAR_NAME_MAX_SIZE
, NULL
);
826 overflow
= read_token (port
, charname
, &charname_len
);
827 charname
= scm_c_substring (charname
, 0, charname_len
);
832 if (charname_len
== 0)
834 chr
= scm_getc (port
);
836 scm_i_input_error (FUNC_NAME
, port
, "unexpected end of file "
837 "while reading character", SCM_EOL
);
839 /* CHR must be a token delimiter, like a whitespace. */
840 return (SCM_MAKE_CHAR (chr
));
843 if (charname_len
== 1)
844 return SCM_MAKE_CHAR (scm_i_string_ref (charname
, 0));
846 cp
= scm_i_string_ref (charname
, 0);
847 if (cp
>= '0' && cp
< '8')
849 /* Dirk:FIXME:: This type of character syntax is not R5RS
850 * compliant. Further, it should be verified that the constant
851 * does only consist of octal digits. Finally, it should be
852 * checked whether the resulting fixnum is in the range of
854 SCM p
= scm_string_to_number (charname
, scm_from_uint (8));
856 return SCM_MAKE_CHAR (SCM_I_INUM (p
));
859 /* The names of characters should never have non-Latin1
861 if (scm_i_is_narrow_string (charname
)
862 || scm_i_try_narrow_string (charname
))
863 { SCM ch
= scm_i_charname_to_char (scm_i_string_chars (charname
),
865 if (scm_is_true (ch
))
870 scm_i_input_error (FUNC_NAME
, port
, "unknown character name ~a",
871 scm_list_1 (charname
));
873 return SCM_UNSPECIFIED
;
878 scm_read_keyword (int chr
, SCM port
)
882 /* Read the symbol that comprises the keyword. Doing this instead of
883 invoking a specific symbol reader function allows `scm_read_keyword ()'
884 to adapt to the delimiters currently valid of symbols.
886 XXX: This implementation allows sloppy syntaxes like `#: key'. */
887 symbol
= scm_read_expression (port
);
888 if (!scm_is_symbol (symbol
))
889 scm_i_input_error ("scm_read_keyword", port
,
890 "keyword prefix `~a' not followed by a symbol: ~s",
891 scm_list_2 (SCM_MAKE_CHAR (chr
), symbol
));
893 return (scm_symbol_to_keyword (symbol
));
897 scm_read_vector (int chr
, SCM port
)
899 /* Note: We call `scm_read_sexp ()' rather than READER here in order to
900 guarantee that it's going to do what we want. After all, this is an
901 implementation detail of `scm_read_vector ()', not a desirable
903 return (scm_vector (scm_read_sexp (chr
, port
)));
907 scm_read_srfi4_vector (int chr
, SCM port
)
909 return scm_i_read_array (port
, chr
);
913 scm_read_bytevector (scm_t_wchar chr
, SCM port
)
915 chr
= scm_getc (port
);
919 chr
= scm_getc (port
);
923 chr
= scm_getc (port
);
927 return scm_u8_list_to_bytevector (scm_read_sexp (chr
, port
));
930 scm_i_input_error ("read_bytevector", port
,
931 "invalid bytevector prefix",
932 SCM_MAKE_CHAR (chr
));
933 return SCM_UNSPECIFIED
;
937 scm_read_guile_bit_vector (scm_t_wchar chr
, SCM port
)
939 /* Read the `#*10101'-style read syntax for bit vectors in Guile. This is
940 terribly inefficient but who cares? */
941 SCM s_bits
= SCM_EOL
;
943 for (chr
= scm_getc (port
);
944 (chr
!= EOF
) && ((chr
== '0') || (chr
== '1'));
945 chr
= scm_getc (port
))
947 s_bits
= scm_cons ((chr
== '0') ? SCM_BOOL_F
: SCM_BOOL_T
, s_bits
);
951 scm_ungetc (chr
, port
);
953 return scm_bitvector (scm_reverse_x (s_bits
, SCM_EOL
));
957 scm_read_scsh_block_comment (scm_t_wchar chr
, SCM port
)
961 /* We can use the get_byte here because there is no need to get the
962 locale correct when reading comments. This presumes that
963 hash and exclamation points always represent themselves no
964 matter what the source encoding is.*/
967 int c
= scm_get_byte_or_eof (port
);
970 scm_i_input_error ("skip_block_comment", port
,
971 "unterminated `#! ... !#' comment", SCM_EOL
);
975 else if (c
== '#' && bang_seen
)
981 return SCM_UNSPECIFIED
;
985 scm_read_commented_expression (scm_t_wchar chr
, SCM port
)
989 c
= flush_ws (port
, (char *) NULL
);
991 scm_i_input_error ("read_commented_expression", port
,
992 "no expression after #; comment", SCM_EOL
);
993 scm_ungetc (c
, port
);
994 scm_read_expression (port
);
995 return SCM_UNSPECIFIED
;
999 scm_read_extended_symbol (scm_t_wchar chr
, SCM port
)
1001 /* Guile's extended symbol read syntax looks like this:
1003 #{This is all a symbol name}#
1005 So here, CHR is expected to be `{'. */
1006 int saw_brace
= 0, finished
= 0;
1008 SCM buf
= scm_i_make_string (1024, NULL
);
1010 buf
= scm_i_string_start_writing (buf
);
1012 while ((chr
= scm_getc (port
)) != EOF
)
1024 scm_i_string_set_x (buf
, len
++, '}');
1025 scm_i_string_set_x (buf
, len
++, chr
);
1028 else if (chr
== '}')
1031 scm_i_string_set_x (buf
, len
++, chr
);
1033 if (len
>= scm_i_string_length (buf
) - 2)
1035 scm_i_string_stop_writing ();
1036 SCM addy
= scm_i_make_string (1024, NULL
);
1037 buf
= scm_string_append (scm_list_2 (buf
, addy
));
1039 buf
= scm_i_string_start_writing (buf
);
1045 scm_i_string_stop_writing ();
1047 return (scm_string_to_symbol (scm_c_substring (buf
, 0, len
)));
1052 /* Top-level token readers, i.e., dispatchers. */
1055 scm_read_sharp_extension (int chr
, SCM port
)
1059 proc
= scm_get_hash_procedure (chr
);
1060 if (scm_is_true (scm_procedure_p (proc
)))
1062 long line
= SCM_LINUM (port
);
1063 int column
= SCM_COL (port
) - 2;
1066 got
= scm_call_2 (proc
, SCM_MAKE_CHAR (chr
), port
);
1067 if (!scm_is_eq (got
, SCM_UNSPECIFIED
))
1069 if (SCM_RECORD_POSITIONS_P
)
1070 return (recsexpr (got
, line
, column
,
1071 SCM_FILENAME (port
)));
1077 return SCM_UNSPECIFIED
;
1080 /* The reader for the sharp `#' character. It basically dispatches reads
1081 among the above token readers. */
1083 scm_read_sharp (scm_t_wchar chr
, SCM port
)
1084 #define FUNC_NAME "scm_lreadr"
1088 chr
= scm_getc (port
);
1090 result
= scm_read_sharp_extension (chr
, port
);
1091 if (!scm_is_eq (result
, SCM_UNSPECIFIED
))
1097 return (scm_read_character (chr
, port
));
1099 return (scm_read_vector (chr
, port
));
1103 /* This one may return either a boolean or an SRFI-4 vector. */
1104 return (scm_read_srfi4_vector (chr
, port
));
1106 return (scm_read_bytevector (chr
, port
));
1108 return (scm_read_guile_bit_vector (chr
, port
));
1112 /* This one may return either a boolean or an SRFI-4 vector. */
1113 return (scm_read_boolean (chr
, port
));
1115 return (scm_read_keyword (chr
, port
));
1116 case '0': case '1': case '2': case '3': case '4':
1117 case '5': case '6': case '7': case '8': case '9':
1119 #if SCM_ENABLE_DEPRECATED
1120 /* See below for 'i' and 'e'. */
1127 return (scm_i_read_array (port
, chr
));
1131 #if SCM_ENABLE_DEPRECATED
1133 /* When next char is '(', it really is an old-style
1135 scm_t_wchar next_c
= scm_getc (port
);
1137 scm_ungetc (next_c
, port
);
1139 return scm_i_read_array (port
, chr
);
1153 return (scm_read_number_and_radix (chr
, port
));
1155 return (scm_read_extended_symbol (chr
, port
));
1157 return (scm_read_scsh_block_comment (chr
, port
));
1159 return (scm_read_commented_expression (chr
, port
));
1163 return (scm_read_syntax (chr
, port
));
1165 result
= scm_read_sharp_extension (chr
, port
);
1166 if (scm_is_eq (result
, SCM_UNSPECIFIED
))
1167 scm_i_input_error (FUNC_NAME
, port
, "Unknown # object: ~S",
1168 scm_list_1 (SCM_MAKE_CHAR (chr
)));
1173 return SCM_UNSPECIFIED
;
1178 scm_read_expression (SCM port
)
1179 #define FUNC_NAME "scm_read_expression"
1183 register scm_t_wchar chr
;
1185 chr
= scm_getc (port
);
1189 case SCM_WHITE_SPACES
:
1190 case SCM_LINE_INCREMENTORS
:
1193 (void) scm_read_semicolon_comment (chr
, port
);
1196 return (scm_read_sexp (chr
, port
));
1198 return (scm_read_string (chr
, port
));
1202 return (scm_read_quote (chr
, port
));
1206 result
= scm_read_sharp (chr
, port
);
1207 if (scm_is_eq (result
, SCM_UNSPECIFIED
))
1208 /* We read a comment or some such. */
1214 scm_i_input_error (FUNC_NAME
, port
, "unexpected \")\"", SCM_EOL
);
1219 if (scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_prefix
))
1220 return scm_symbol_to_keyword (scm_read_expression (port
));
1225 if (((chr
>= '0') && (chr
<= '9'))
1226 || (strchr ("+-.", chr
)))
1227 return (scm_read_number (chr
, port
));
1229 return (scm_read_mixed_case_symbol (chr
, port
));
1237 /* Actual reader. */
1239 SCM_DEFINE (scm_read
, "read", 0, 1, 0,
1241 "Read an s-expression from the input port @var{port}, or from\n"
1242 "the current input port if @var{port} is not specified.\n"
1243 "Any whitespace before the next token is discarded.")
1244 #define FUNC_NAME s_scm_read
1248 if (SCM_UNBNDP (port
))
1249 port
= scm_current_input_port ();
1250 SCM_VALIDATE_OPINPORT (1, port
);
1252 c
= flush_ws (port
, (char *) NULL
);
1255 scm_ungetc (c
, port
);
1257 return (scm_read_expression (port
));
1264 /* Used when recording expressions constructed by `scm_read_sharp ()'. */
1266 recsexpr (SCM obj
, long line
, int column
, SCM filename
)
1268 if (!scm_is_pair(obj
)) {
1271 SCM tmp
= obj
, copy
;
1272 /* If this sexpr is visible in the read:sharp source, we want to
1273 keep that information, so only record non-constant cons cells
1274 which haven't previously been read by the reader. */
1275 if (scm_is_false (scm_whash_lookup (scm_source_whash
, obj
)))
1277 if (SCM_COPY_SOURCE_P
)
1279 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
1281 while ((tmp
= SCM_CDR (tmp
)) && scm_is_pair (tmp
))
1283 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
1288 copy
= SCM_CDR (copy
);
1290 SCM_SETCDR (copy
, tmp
);
1294 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
1295 while ((tmp
= SCM_CDR (tmp
)) && scm_is_pair (tmp
))
1296 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
1297 copy
= SCM_UNDEFINED
;
1299 scm_whash_insert (scm_source_whash
,
1301 scm_make_srcprops (line
,
1311 /* Manipulate the read-hash-procedures alist. This could be written in
1312 Scheme, but maybe it will also be used by C code during initialisation. */
1313 SCM_DEFINE (scm_read_hash_extend
, "read-hash-extend", 2, 0, 0,
1314 (SCM chr
, SCM proc
),
1315 "Install the procedure @var{proc} for reading expressions\n"
1316 "starting with the character sequence @code{#} and @var{chr}.\n"
1317 "@var{proc} will be called with two arguments: the character\n"
1318 "@var{chr} and the port to read further data from. The object\n"
1319 "returned will be the return value of @code{read}. \n"
1320 "Passing @code{#f} for @var{proc} will remove a previous setting. \n"
1322 #define FUNC_NAME s_scm_read_hash_extend
1327 SCM_VALIDATE_CHAR (1, chr
);
1328 SCM_ASSERT (scm_is_false (proc
)
1329 || scm_is_eq (scm_procedure_p (proc
), SCM_BOOL_T
),
1330 proc
, SCM_ARG2
, FUNC_NAME
);
1332 /* Check if chr is already in the alist. */
1333 this = *scm_read_hash_procedures
;
1337 if (scm_is_null (this))
1339 /* not found, so add it to the beginning. */
1340 if (scm_is_true (proc
))
1342 *scm_read_hash_procedures
=
1343 scm_cons (scm_cons (chr
, proc
), *scm_read_hash_procedures
);
1347 if (scm_is_eq (chr
, SCM_CAAR (this)))
1349 /* already in the alist. */
1350 if (scm_is_false (proc
))
1353 if (scm_is_false (prev
))
1355 *scm_read_hash_procedures
=
1356 SCM_CDR (*scm_read_hash_procedures
);
1359 scm_set_cdr_x (prev
, SCM_CDR (this));
1364 scm_set_cdr_x (SCM_CAR (this), proc
);
1369 this = SCM_CDR (this);
1372 return SCM_UNSPECIFIED
;
1376 /* Recover the read-hash procedure corresponding to char c. */
1378 scm_get_hash_procedure (int c
)
1380 SCM rest
= *scm_read_hash_procedures
;
1384 if (scm_is_null (rest
))
1387 if (SCM_CHAR (SCM_CAAR (rest
)) == c
)
1388 return SCM_CDAR (rest
);
1390 rest
= SCM_CDR (rest
);
1394 #define SCM_ENCODING_SEARCH_SIZE (500)
1396 /* Search the first few hundred characters of a file for
1397 an emacs-like coding declaration. */
1399 scm_scan_for_encoding (SCM port
)
1401 char header
[SCM_ENCODING_SEARCH_SIZE
+1];
1403 char *encoding
= NULL
;
1409 bytes_read
= scm_c_read (port
, header
, SCM_ENCODING_SEARCH_SIZE
);
1410 scm_seek (port
, scm_from_int (0), scm_from_int (SEEK_SET
));
1413 && header
[0] == '\xef' && header
[1] == '\xbb' && header
[2] == '\xbf')
1416 /* search past "coding[:=]" */
1420 if ((pos
= strstr(pos
, "coding")) == NULL
)
1423 pos
+= strlen("coding");
1424 if (pos
- header
>= SCM_ENCODING_SEARCH_SIZE
||
1425 (*pos
== ':' || *pos
== '='))
1433 while (pos
- header
<= SCM_ENCODING_SEARCH_SIZE
&&
1434 (*pos
== ' ' || *pos
== '\t'))
1437 /* grab the next token */
1439 while (pos
+ i
- header
<= SCM_ENCODING_SEARCH_SIZE
1440 && (isalnum(pos
[i
]) || pos
[i
] == '_' || pos
[i
] == '-' || pos
[i
] == '.'))
1446 encoding
= scm_malloc (i
+1);
1447 memcpy (encoding
, pos
, i
);
1449 for (i
= 0; i
< strlen (encoding
); i
++)
1450 encoding
[i
] = toupper ((int) encoding
[i
]);
1452 /* push backwards to make sure we were in a comment */
1454 while (pos
- i
- header
> 0)
1456 if (*(pos
- i
) == '\n')
1458 /* This wasn't in a semicolon comment. Check for a
1459 hash-bang comment. */
1460 char *beg
= strstr (header
, "#!");
1461 char *end
= strstr (header
, "!#");
1462 if (beg
< pos
&& pos
< end
)
1466 if (*(pos
- i
) == ';')
1475 /* This wasn't in a comment */
1479 if (utf8_bom
&& strcmp(encoding
, "UTF-8"))
1480 scm_misc_error (NULL
,
1481 "the port input declares the encoding ~s but is encoded as UTF-8",
1482 scm_list_1 (scm_from_locale_string (encoding
)));
1487 SCM_DEFINE (scm_file_encoding
, "file-encoding", 1, 0, 0,
1489 "Scans the port for an EMACS-like character coding declaration\n"
1490 "near the top of the contents of a port with random-acessible contents.\n"
1491 "The coding declaration is of the form\n"
1492 "@code{coding: XXXXX} and must appear in a scheme comment.\n"
1494 "Returns a string containing the character encoding of the file\n"
1495 "if a declaration was found, or @code{#f} otherwise.\n")
1496 #define FUNC_NAME s_scm_file_encoding
1501 enc
= scm_scan_for_encoding (port
);
1506 s_enc
= scm_from_locale_string (enc
);
1518 scm_read_hash_procedures
=
1519 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL
));
1521 scm_init_opts (scm_read_options
, scm_read_opts
);
1522 #include "libguile/read.x"