1 /* Copyright (C) 1995,1996,1997,1999,2000,2001,2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 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
34 #include "libguile/_scm.h"
35 #include "libguile/bytevectors.h"
36 #include "libguile/chars.h"
37 #include "libguile/eval.h"
38 #include "libguile/arrays.h"
39 #include "libguile/bitvectors.h"
40 #include "libguile/keywords.h"
41 #include "libguile/alist.h"
42 #include "libguile/srcprop.h"
43 #include "libguile/hashtab.h"
44 #include "libguile/hash.h"
45 #include "libguile/ports.h"
46 #include "libguile/fports.h"
47 #include "libguile/root.h"
48 #include "libguile/strings.h"
49 #include "libguile/strports.h"
50 #include "libguile/vectors.h"
51 #include "libguile/validate.h"
52 #include "libguile/srfi-4.h"
53 #include "libguile/srfi-13.h"
55 #include "libguile/read.h"
56 #include "libguile/private-options.h"
61 SCM_GLOBAL_SYMBOL (scm_sym_dot
, ".");
62 SCM_SYMBOL (scm_keyword_prefix
, "prefix");
63 SCM_SYMBOL (scm_keyword_postfix
, "postfix");
64 SCM_SYMBOL (sym_nil
, "nil");
66 scm_t_option scm_read_opts
[] = {
67 { SCM_OPTION_BOOLEAN
, "copy", 0,
68 "Copy source code expressions." },
69 { SCM_OPTION_BOOLEAN
, "positions", 0,
70 "Record positions of source code expressions." },
71 { SCM_OPTION_BOOLEAN
, "case-insensitive", 0,
72 "Convert symbols to lower case."},
73 { SCM_OPTION_SCM
, "keywords", (scm_t_bits
) SCM_BOOL_F
,
74 "Style of keyword recognition: #f, 'prefix or 'postfix."},
75 { SCM_OPTION_BOOLEAN
, "r6rs-hex-escapes", 0,
76 "Use R6RS variable-length character and string hex escapes."},
77 { SCM_OPTION_BOOLEAN
, "square-brackets", 1,
78 "Treat `[' and `]' as parentheses, for R6RS compatibility."},
79 { SCM_OPTION_BOOLEAN
, "hungry-eol-escapes", 0,
80 "In strings, consume leading whitespace after an escaped end-of-line."},
85 Give meaningful error messages for errors
89 FILE:LINE:COL: MESSAGE
92 This is not standard GNU format, but the test-suite likes the real
93 message to be in front.
99 scm_i_input_error (char const *function
,
100 SCM port
, const char *message
, SCM arg
)
102 SCM fn
= (scm_is_string (SCM_FILENAME(port
))
104 : scm_from_locale_string ("#<unknown port>"));
106 SCM string_port
= scm_open_output_string ();
107 SCM string
= SCM_EOL
;
108 scm_simple_format (string_port
,
109 scm_from_locale_string ("~A:~S:~S: ~A"),
111 scm_from_long (SCM_LINUM (port
) + 1),
112 scm_from_int (SCM_COL (port
) + 1),
113 scm_from_locale_string (message
)));
115 string
= scm_get_output_string (string_port
);
116 scm_close_output_port (string_port
);
117 scm_error_scm (scm_from_latin1_symbol ("read-error"),
118 function
? scm_from_locale_string (function
) : SCM_BOOL_F
,
125 SCM_DEFINE (scm_read_options
, "read-options-interface", 0, 1, 0,
127 "Option interface for the read options. Instead of using\n"
128 "this procedure directly, use the procedures @code{read-enable},\n"
129 "@code{read-disable}, @code{read-set!} and @code{read-options}.")
130 #define FUNC_NAME s_scm_read_options
132 SCM ans
= scm_options (setting
,
135 if (SCM_COPY_SOURCE_P
)
136 SCM_RECORD_POSITIONS_P
= 1;
141 /* A fluid referring to an association list mapping extra hash
142 characters to procedures. */
143 static SCM
*scm_i_read_hash_procedures
;
146 scm_i_read_hash_procedures_ref (void)
148 return scm_fluid_ref (*scm_i_read_hash_procedures
);
152 scm_i_read_hash_procedures_set_x (SCM value
)
154 scm_fluid_set_x (*scm_i_read_hash_procedures
, value
);
161 /* Size of the C buffer used to read symbols and numbers. */
162 #define READER_BUFFER_SIZE 128
164 /* Size of the C buffer used to read strings. */
165 #define READER_STRING_BUFFER_SIZE 512
167 /* The maximum size of Scheme character names. */
168 #define READER_CHAR_NAME_MAX_SIZE 50
171 /* `isblank' is only in C99. */
172 #define CHAR_IS_BLANK_(_chr) \
173 (((_chr) == ' ') || ((_chr) == '\t') || ((_chr) == '\n') \
174 || ((_chr) == '\f') || ((_chr) == '\r'))
177 # define CHAR_IS_BLANK(_chr) \
178 ((CHAR_IS_BLANK_ (chr)) || ((_chr) == 26))
180 # define CHAR_IS_BLANK CHAR_IS_BLANK_
184 /* R5RS one-character delimiters (see section 7.1.1, ``Lexical
186 #define CHAR_IS_R5RS_DELIMITER(c) \
188 || (c == ')') || (c == '(') || (c == ';') || (c == '"') \
189 || (SCM_SQUARE_BRACKETS_P && ((c == '[') || (c == ']'))))
191 #define CHAR_IS_DELIMITER CHAR_IS_R5RS_DELIMITER
193 /* Exponent markers, as defined in section 7.1.1 of R5RS, ``Lexical
195 #define CHAR_IS_EXPONENT_MARKER(_chr) \
196 (((_chr) == 'e') || ((_chr) == 's') || ((_chr) == 'f') \
197 || ((_chr) == 'd') || ((_chr) == 'l'))
199 /* Read an SCSH block comment. */
200 static inline SCM
scm_read_scsh_block_comment (scm_t_wchar
, SCM
);
201 static SCM
scm_read_r6rs_block_comment (scm_t_wchar
, SCM
);
202 static SCM
scm_read_commented_expression (scm_t_wchar
, SCM
);
203 static SCM
scm_read_shebang (scm_t_wchar
, SCM
);
204 static SCM
scm_get_hash_procedure (int);
206 /* Read from PORT until a delimiter (e.g., a whitespace) is read. Put the
207 result in the pre-allocated buffer BUF. Return zero if the whole token has
208 fewer than BUF_SIZE bytes, non-zero otherwise. READ will be set the number of
209 bytes actually read. */
211 read_token (SCM port
, char *buf
, const size_t buf_size
, size_t *read
)
215 while (*read
< buf_size
)
219 chr
= scm_get_byte_or_eof (port
);
223 else if (CHAR_IS_DELIMITER (chr
))
225 scm_unget_byte (chr
, port
);
238 /* Read from PORT until a delimiter (e.g., a whitespace) is read. Put the
239 result in the pre-allocated buffer BUFFER, if the whole token has fewer than
240 BUFFER_SIZE bytes, or into OVERFLOW_BUFFER, allocated here to be freed by the
241 caller. Return zero if the token fits in BUFFER, non-zero otherwise. READ
242 will be set the number of bytes actually read. */
244 read_complete_token (SCM port
, char *buffer
, const size_t buffer_size
,
245 char **overflow_buffer
, size_t *read
)
248 size_t bytes_read
, overflow_size
;
250 *overflow_buffer
= NULL
;
255 overflow
= read_token (port
, buffer
, buffer_size
, &bytes_read
);
258 if (overflow
|| overflow_size
!= 0)
260 if (overflow_size
== 0)
262 *overflow_buffer
= scm_malloc (bytes_read
);
263 memcpy (*overflow_buffer
, buffer
, bytes_read
);
264 overflow_size
= bytes_read
;
268 *overflow_buffer
= scm_realloc (*overflow_buffer
, overflow_size
+ bytes_read
);
269 memcpy (*overflow_buffer
+ overflow_size
, buffer
, bytes_read
);
270 overflow_size
+= bytes_read
;
277 *read
= overflow_size
;
281 return (overflow_size
!= 0);
284 /* Skip whitespace from PORT and return the first non-whitespace character
285 read. Raise an error on end-of-file. */
287 flush_ws (SCM port
, const char *eoferr
)
289 register scm_t_wchar c
;
291 switch (c
= scm_getc (port
))
297 scm_i_input_error (eoferr
,
306 switch (c
= scm_getc (port
))
312 case SCM_LINE_INCREMENTORS
:
318 switch (c
= scm_getc (port
))
321 eoferr
= "read_sharp";
324 scm_read_shebang (c
, port
);
327 scm_read_commented_expression (c
, port
);
330 if (scm_is_false (scm_get_hash_procedure (c
)))
332 scm_read_r6rs_block_comment (c
, port
);
337 scm_ungetc (c
, port
);
342 case SCM_LINE_INCREMENTORS
:
343 case SCM_SINGLE_SPACES
:
358 static SCM
scm_read_expression (SCM port
);
359 static SCM
scm_read_sharp (int chr
, SCM port
);
360 static SCM
recsexpr (SCM obj
, long line
, int column
, SCM filename
);
364 scm_read_sexp (scm_t_wchar chr
, SCM port
)
365 #define FUNC_NAME "scm_i_lreadparen"
369 register SCM tl
, ans
= SCM_EOL
;
370 SCM tl2
= SCM_EOL
, ans2
= SCM_EOL
, copy
= SCM_BOOL_F
;
371 const int terminating_char
= ((chr
== '[') ? ']' : ')');
373 /* Need to capture line and column numbers here. */
374 long line
= SCM_LINUM (port
);
375 int column
= SCM_COL (port
) - 1;
378 c
= flush_ws (port
, FUNC_NAME
);
379 if (terminating_char
== c
)
382 scm_ungetc (c
, port
);
383 if (scm_is_eq (scm_sym_dot
,
384 (tmp
= scm_read_expression (port
))))
386 ans
= scm_read_expression (port
);
387 if (terminating_char
!= (c
= flush_ws (port
, FUNC_NAME
)))
388 scm_i_input_error (FUNC_NAME
, port
, "missing close paren",
393 /* Build the head of the list structure. */
394 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
396 if (SCM_COPY_SOURCE_P
)
397 ans2
= tl2
= scm_cons (scm_is_pair (tmp
)
402 while (terminating_char
!= (c
= flush_ws (port
, FUNC_NAME
)))
406 if (c
== ')' || (SCM_SQUARE_BRACKETS_P
&& c
== ']'))
407 scm_i_input_error (FUNC_NAME
, port
,
408 "in pair: mismatched close paren: ~A",
409 scm_list_1 (SCM_MAKE_CHAR (c
)));
411 scm_ungetc (c
, port
);
412 tmp
= scm_read_expression (port
);
414 if (scm_is_eq (scm_sym_dot
, tmp
))
416 SCM_SETCDR (tl
, tmp
= scm_read_expression (port
));
418 if (SCM_COPY_SOURCE_P
)
419 SCM_SETCDR (tl2
, scm_cons (scm_is_pair (tmp
) ? copy
: tmp
,
422 c
= flush_ws (port
, FUNC_NAME
);
423 if (terminating_char
!= c
)
424 scm_i_input_error (FUNC_NAME
, port
,
425 "in pair: missing close paren", SCM_EOL
);
429 new_tail
= scm_cons (tmp
, SCM_EOL
);
430 SCM_SETCDR (tl
, new_tail
);
433 if (SCM_COPY_SOURCE_P
)
435 SCM new_tail2
= scm_cons (scm_is_pair (tmp
)
438 SCM_SETCDR (tl2
, new_tail2
);
444 if (SCM_RECORD_POSITIONS_P
)
445 scm_whash_insert (scm_source_whash
,
447 scm_make_srcprops (line
, column
,
458 /* Read a hexadecimal number NDIGITS in length. Put its value into the variable
459 C. If TERMINATOR is non-null, terminate early if the TERMINATOR character is
461 #define SCM_READ_HEX_ESCAPE(ndigits, terminator) \
467 while (i < ndigits) \
469 a = scm_getc (port); \
473 && (a == (scm_t_wchar) terminator) \
476 if ('0' <= a && a <= '9') \
478 else if ('A' <= a && a <= 'F') \
480 else if ('a' <= a && a <= 'f') \
493 skip_intraline_whitespace (SCM port
)
503 while (c
== '\t' || uc_is_general_category (c
, UC_SPACE_SEPARATOR
));
505 scm_ungetc (c
, port
);
509 scm_read_string (int chr
, SCM port
)
510 #define FUNC_NAME "scm_lreadr"
512 /* For strings smaller than C_STR, this function creates only one Scheme
513 object (the string returned). */
515 SCM str
= SCM_BOOL_F
;
516 unsigned c_str_len
= 0;
519 str
= scm_i_make_string (READER_STRING_BUFFER_SIZE
, NULL
);
520 while ('"' != (c
= scm_getc (port
)))
525 scm_i_input_error (FUNC_NAME
, port
,
526 "end of file in string constant", SCM_EOL
);
529 if (c_str_len
+ 1 >= scm_i_string_length (str
))
531 SCM addy
= scm_i_make_string (READER_STRING_BUFFER_SIZE
, NULL
);
533 str
= scm_string_append (scm_list_2 (str
, addy
));
538 switch (c
= scm_getc (port
))
546 if (SCM_HUNGRY_EOL_ESCAPES_P
)
547 skip_intraline_whitespace (port
);
574 if (SCM_R6RS_ESCAPES_P
)
575 SCM_READ_HEX_ESCAPE (10, ';');
577 SCM_READ_HEX_ESCAPE (2, '\0');
580 if (!SCM_R6RS_ESCAPES_P
)
582 SCM_READ_HEX_ESCAPE (4, '\0');
586 if (!SCM_R6RS_ESCAPES_P
)
588 SCM_READ_HEX_ESCAPE (6, '\0');
593 scm_i_input_error (FUNC_NAME
, port
,
594 "illegal character in escape sequence: ~S",
595 scm_list_1 (SCM_MAKE_CHAR (c
)));
598 str
= scm_i_string_start_writing (str
);
599 scm_i_string_set_x (str
, c_str_len
++, c
);
600 scm_i_string_stop_writing ();
605 return scm_i_substring_copy (str
, 0, c_str_len
);
614 scm_read_number (scm_t_wchar chr
, SCM port
)
616 SCM result
, str
= SCM_EOL
;
617 char buffer
[READER_BUFFER_SIZE
];
618 char *overflow_buffer
= NULL
;
621 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
623 scm_ungetc (chr
, port
);
624 overflow
= read_complete_token (port
, buffer
, sizeof (buffer
),
625 &overflow_buffer
, &bytes_read
);
628 str
= scm_from_stringn (buffer
, bytes_read
, pt
->encoding
, pt
->ilseq_handler
);
630 str
= scm_from_stringn (overflow_buffer
, bytes_read
, pt
->encoding
,
633 result
= scm_string_to_number (str
, SCM_UNDEFINED
);
634 if (!scm_is_true (result
))
636 /* Return a symbol instead of a number */
637 if (SCM_CASE_INSENSITIVE_P
)
638 str
= scm_string_downcase_x (str
);
639 result
= scm_string_to_symbol (str
);
643 free (overflow_buffer
);
644 SCM_COL (port
) += scm_i_string_length (str
);
649 scm_read_mixed_case_symbol (scm_t_wchar chr
, SCM port
)
652 int ends_with_colon
= 0;
654 int postfix
= scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_postfix
);
656 char buffer
[READER_BUFFER_SIZE
], *overflow_buffer
;
657 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
660 scm_ungetc (chr
, port
);
661 overflow
= read_complete_token (port
, buffer
, READER_BUFFER_SIZE
,
662 &overflow_buffer
, &bytes_read
);
666 ends_with_colon
= buffer
[bytes_read
- 1] == ':';
668 ends_with_colon
= overflow_buffer
[bytes_read
- 1] == ':';
671 if (postfix
&& ends_with_colon
&& (bytes_read
> 1))
674 str
= scm_from_stringn (buffer
, bytes_read
- 1, pt
->encoding
, pt
->ilseq_handler
);
676 str
= scm_from_stringn (overflow_buffer
, bytes_read
- 1, pt
->encoding
,
679 if (SCM_CASE_INSENSITIVE_P
)
680 str
= scm_string_downcase_x (str
);
681 result
= scm_symbol_to_keyword (scm_string_to_symbol (str
));
686 str
= scm_from_stringn (buffer
, bytes_read
, pt
->encoding
, pt
->ilseq_handler
);
688 str
= scm_from_stringn (overflow_buffer
, bytes_read
, pt
->encoding
,
691 if (SCM_CASE_INSENSITIVE_P
)
692 str
= scm_string_downcase_x (str
);
693 result
= scm_string_to_symbol (str
);
697 free (overflow_buffer
);
698 SCM_COL (port
) += scm_i_string_length (str
);
703 scm_read_number_and_radix (scm_t_wchar chr
, SCM port
)
704 #define FUNC_NAME "scm_lreadr"
708 char buffer
[READER_BUFFER_SIZE
], *overflow_buffer
;
737 scm_ungetc (chr
, port
);
738 scm_ungetc ('#', port
);
742 overflow
= read_complete_token (port
, buffer
, sizeof (buffer
),
743 &overflow_buffer
, &read
);
745 pt
= SCM_PTAB_ENTRY (port
);
747 str
= scm_from_stringn (buffer
, read
, pt
->encoding
, pt
->ilseq_handler
);
749 str
= scm_from_stringn (overflow_buffer
, read
, pt
->encoding
,
752 result
= scm_string_to_number (str
, scm_from_uint (radix
));
755 free (overflow_buffer
);
757 SCM_COL (port
) += scm_i_string_length (str
);
759 if (scm_is_true (result
))
762 scm_i_input_error (FUNC_NAME
, port
, "unknown # object", SCM_EOL
);
769 scm_read_quote (int chr
, SCM port
)
772 long line
= SCM_LINUM (port
);
773 int column
= SCM_COL (port
) - 1;
778 p
= scm_sym_quasiquote
;
791 p
= scm_sym_uq_splicing
;
794 scm_ungetc (c
, port
);
801 fprintf (stderr
, "%s: unhandled quote character (%i)\n",
802 "scm_read_quote", chr
);
806 p
= scm_cons2 (p
, scm_read_expression (port
), SCM_EOL
);
807 if (SCM_RECORD_POSITIONS_P
)
808 scm_whash_insert (scm_source_whash
, p
,
809 scm_make_srcprops (line
, column
,
812 ? (scm_cons2 (SCM_CAR (p
),
813 SCM_CAR (SCM_CDR (p
)),
822 SCM_SYMBOL (sym_syntax
, "syntax");
823 SCM_SYMBOL (sym_quasisyntax
, "quasisyntax");
824 SCM_SYMBOL (sym_unsyntax
, "unsyntax");
825 SCM_SYMBOL (sym_unsyntax_splicing
, "unsyntax-splicing");
828 scm_read_syntax (int chr
, SCM port
)
831 long line
= SCM_LINUM (port
);
832 int column
= SCM_COL (port
) - 1;
850 p
= sym_unsyntax_splicing
;
853 scm_ungetc (c
, port
);
860 fprintf (stderr
, "%s: unhandled syntax character (%i)\n",
861 "scm_read_syntax", chr
);
865 p
= scm_cons2 (p
, scm_read_expression (port
), SCM_EOL
);
866 if (SCM_RECORD_POSITIONS_P
)
867 scm_whash_insert (scm_source_whash
, p
,
868 scm_make_srcprops (line
, column
,
871 ? (scm_cons2 (SCM_CAR (p
),
872 SCM_CAR (SCM_CDR (p
)),
882 scm_read_nil (int chr
, SCM port
)
884 SCM id
= scm_read_mixed_case_symbol (chr
, port
);
886 if (!scm_is_eq (id
, sym_nil
))
887 scm_i_input_error ("scm_read_nil", port
,
888 "unexpected input while reading #nil: ~a",
891 return SCM_ELISP_NIL
;
895 scm_read_semicolon_comment (int chr
, SCM port
)
899 /* We use the get_byte here because there is no need to get the
900 locale correct with comment input. This presumes that newline
901 always represents itself no matter what the encoding is. */
902 for (c
= scm_get_byte_or_eof (port
);
903 (c
!= EOF
) && (c
!= '\n');
904 c
= scm_get_byte_or_eof (port
));
906 return SCM_UNSPECIFIED
;
910 /* Sharp readers, i.e. readers called after a `#' sign has been read. */
913 scm_read_boolean (int chr
, SCM port
)
926 return SCM_UNSPECIFIED
;
930 scm_read_character (scm_t_wchar chr
, SCM port
)
931 #define FUNC_NAME "scm_lreadr"
933 char buffer
[READER_CHAR_NAME_MAX_SIZE
];
935 size_t charname_len
, bytes_read
;
940 overflow
= read_token (port
, buffer
, READER_CHAR_NAME_MAX_SIZE
, &bytes_read
);
942 scm_i_input_error (FUNC_NAME
, port
, "character name too long", SCM_EOL
);
946 chr
= scm_getc (port
);
948 scm_i_input_error (FUNC_NAME
, port
, "unexpected end of file "
949 "while reading character", SCM_EOL
);
951 /* CHR must be a token delimiter, like a whitespace. */
952 return (SCM_MAKE_CHAR (chr
));
955 pt
= SCM_PTAB_ENTRY (port
);
957 /* Simple ASCII characters can be processed immediately. Also, simple
958 ISO-8859-1 characters can be processed immediately if the encoding for this
959 port is ISO-8859-1. */
960 if (bytes_read
== 1 && ((unsigned char) buffer
[0] <= 127 || pt
->encoding
== NULL
))
963 return SCM_MAKE_CHAR (buffer
[0]);
966 /* Otherwise, convert the buffer into a proper scheme string for
968 charname
= scm_from_stringn (buffer
, bytes_read
, pt
->encoding
,
970 charname_len
= scm_i_string_length (charname
);
971 SCM_COL (port
) += charname_len
;
972 cp
= scm_i_string_ref (charname
, 0);
973 if (charname_len
== 1)
974 return SCM_MAKE_CHAR (cp
);
976 /* Ignore dotted circles, which may be used to keep combining characters from
977 combining with the backslash in #\charname. */
978 if (cp
== SCM_CODEPOINT_DOTTED_CIRCLE
&& charname_len
== 2)
979 return SCM_MAKE_CHAR (scm_i_string_ref (charname
, 1));
981 if (cp
>= '0' && cp
< '8')
983 /* Dirk:FIXME:: This type of character syntax is not R5RS
984 * compliant. Further, it should be verified that the constant
985 * does only consist of octal digits. */
986 SCM p
= scm_string_to_number (charname
, scm_from_uint (8));
989 scm_t_wchar c
= scm_to_uint32 (p
);
990 if (SCM_IS_UNICODE_CHAR (c
))
991 return SCM_MAKE_CHAR (c
);
993 scm_i_input_error (FUNC_NAME
, port
,
994 "out-of-range octal character escape: ~a",
995 scm_list_1 (charname
));
999 if (cp
== 'x' && (charname_len
> 1))
1003 /* Convert from hex, skipping the initial 'x' character in CHARNAME */
1004 p
= scm_string_to_number (scm_c_substring (charname
, 1, charname_len
),
1005 scm_from_uint (16));
1006 if (SCM_I_INUMP (p
))
1008 scm_t_wchar c
= scm_to_uint32 (p
);
1009 if (SCM_IS_UNICODE_CHAR (c
))
1010 return SCM_MAKE_CHAR (c
);
1012 scm_i_input_error (FUNC_NAME
, port
,
1013 "out-of-range hex character escape: ~a",
1014 scm_list_1 (charname
));
1018 /* The names of characters should never have non-Latin1
1020 if (scm_i_is_narrow_string (charname
)
1021 || scm_i_try_narrow_string (charname
))
1022 { SCM ch
= scm_i_charname_to_char (scm_i_string_chars (charname
),
1024 if (scm_is_true (ch
))
1028 scm_i_input_error (FUNC_NAME
, port
, "unknown character name ~a",
1029 scm_list_1 (charname
));
1031 return SCM_UNSPECIFIED
;
1036 scm_read_keyword (int chr
, SCM port
)
1040 /* Read the symbol that comprises the keyword. Doing this instead of
1041 invoking a specific symbol reader function allows `scm_read_keyword ()'
1042 to adapt to the delimiters currently valid of symbols.
1044 XXX: This implementation allows sloppy syntaxes like `#: key'. */
1045 symbol
= scm_read_expression (port
);
1046 if (!scm_is_symbol (symbol
))
1047 scm_i_input_error ("scm_read_keyword", port
,
1048 "keyword prefix `~a' not followed by a symbol: ~s",
1049 scm_list_2 (SCM_MAKE_CHAR (chr
), symbol
));
1051 return (scm_symbol_to_keyword (symbol
));
1055 scm_read_vector (int chr
, SCM port
)
1057 /* Note: We call `scm_read_sexp ()' rather than READER here in order to
1058 guarantee that it's going to do what we want. After all, this is an
1059 implementation detail of `scm_read_vector ()', not a desirable
1061 return (scm_vector (scm_read_sexp (chr
, port
)));
1065 scm_read_srfi4_vector (int chr
, SCM port
)
1067 return scm_i_read_array (port
, chr
);
1071 scm_read_bytevector (scm_t_wchar chr
, SCM port
)
1073 chr
= scm_getc (port
);
1077 chr
= scm_getc (port
);
1081 chr
= scm_getc (port
);
1085 return scm_u8_list_to_bytevector (scm_read_sexp (chr
, port
));
1088 scm_i_input_error ("read_bytevector", port
,
1089 "invalid bytevector prefix",
1090 SCM_MAKE_CHAR (chr
));
1091 return SCM_UNSPECIFIED
;
1095 scm_read_guile_bit_vector (scm_t_wchar chr
, SCM port
)
1097 /* Read the `#*10101'-style read syntax for bit vectors in Guile. This is
1098 terribly inefficient but who cares? */
1099 SCM s_bits
= SCM_EOL
;
1101 for (chr
= scm_getc (port
);
1102 (chr
!= EOF
) && ((chr
== '0') || (chr
== '1'));
1103 chr
= scm_getc (port
))
1105 s_bits
= scm_cons ((chr
== '0') ? SCM_BOOL_F
: SCM_BOOL_T
, s_bits
);
1109 scm_ungetc (chr
, port
);
1111 return scm_bitvector (scm_reverse_x (s_bits
, SCM_EOL
));
1115 scm_read_scsh_block_comment (scm_t_wchar chr
, SCM port
)
1119 /* We can use the get_byte here because there is no need to get the
1120 locale correct when reading comments. This presumes that
1121 hash and exclamation points always represent themselves no
1122 matter what the source encoding is.*/
1125 int c
= scm_get_byte_or_eof (port
);
1128 scm_i_input_error ("skip_block_comment", port
,
1129 "unterminated `#! ... !#' comment", SCM_EOL
);
1133 else if (c
== '#' && bang_seen
)
1139 return SCM_UNSPECIFIED
;
1143 scm_read_shebang (scm_t_wchar chr
, SCM port
)
1146 if ((c
= scm_get_byte_or_eof (port
)) != 'r')
1148 scm_ungetc (c
, port
);
1149 return scm_read_scsh_block_comment (chr
, port
);
1151 if ((c
= scm_get_byte_or_eof (port
)) != '6')
1153 scm_ungetc (c
, port
);
1154 scm_ungetc ('r', port
);
1155 return scm_read_scsh_block_comment (chr
, port
);
1157 if ((c
= scm_get_byte_or_eof (port
)) != 'r')
1159 scm_ungetc (c
, port
);
1160 scm_ungetc ('6', port
);
1161 scm_ungetc ('r', port
);
1162 return scm_read_scsh_block_comment (chr
, port
);
1164 if ((c
= scm_get_byte_or_eof (port
)) != 's')
1166 scm_ungetc (c
, port
);
1167 scm_ungetc ('r', port
);
1168 scm_ungetc ('6', port
);
1169 scm_ungetc ('r', port
);
1170 return scm_read_scsh_block_comment (chr
, port
);
1173 return SCM_UNSPECIFIED
;
1177 scm_read_r6rs_block_comment (scm_t_wchar chr
, SCM port
)
1179 /* Unlike SCSH-style block comments, SRFI-30/R6RS block comments may be
1180 nested. So care must be taken. */
1181 int nesting_level
= 1;
1182 int opening_seen
= 0, closing_seen
= 0;
1184 while (nesting_level
> 0)
1186 int c
= scm_getc (port
);
1189 scm_i_input_error ("scm_read_r6rs_block_comment", port
,
1190 "unterminated `#| ... |#' comment", SCM_EOL
);
1198 else if (closing_seen
)
1209 opening_seen
= closing_seen
= 0;
1212 return SCM_UNSPECIFIED
;
1216 scm_read_commented_expression (scm_t_wchar chr
, SCM port
)
1220 c
= flush_ws (port
, (char *) NULL
);
1222 scm_i_input_error ("read_commented_expression", port
,
1223 "no expression after #; comment", SCM_EOL
);
1224 scm_ungetc (c
, port
);
1225 scm_read_expression (port
);
1226 return SCM_UNSPECIFIED
;
1230 scm_read_extended_symbol (scm_t_wchar chr
, SCM port
)
1232 /* Guile's extended symbol read syntax looks like this:
1234 #{This is all a symbol name}#
1236 So here, CHR is expected to be `{'. */
1237 int saw_brace
= 0, finished
= 0;
1239 SCM buf
= scm_i_make_string (1024, NULL
);
1241 buf
= scm_i_string_start_writing (buf
);
1243 while ((chr
= scm_getc (port
)) != EOF
)
1255 scm_i_string_set_x (buf
, len
++, '}');
1256 scm_i_string_set_x (buf
, len
++, chr
);
1259 else if (chr
== '}')
1262 scm_i_string_set_x (buf
, len
++, chr
);
1264 if (len
>= scm_i_string_length (buf
) - 2)
1268 scm_i_string_stop_writing ();
1269 addy
= scm_i_make_string (1024, NULL
);
1270 buf
= scm_string_append (scm_list_2 (buf
, addy
));
1272 buf
= scm_i_string_start_writing (buf
);
1278 scm_i_string_stop_writing ();
1280 return (scm_string_to_symbol (scm_c_substring (buf
, 0, len
)));
1285 /* Top-level token readers, i.e., dispatchers. */
1288 scm_read_sharp_extension (int chr
, SCM port
)
1292 proc
= scm_get_hash_procedure (chr
);
1293 if (scm_is_true (scm_procedure_p (proc
)))
1295 long line
= SCM_LINUM (port
);
1296 int column
= SCM_COL (port
) - 2;
1299 got
= scm_call_2 (proc
, SCM_MAKE_CHAR (chr
), port
);
1300 if (!scm_is_eq (got
, SCM_UNSPECIFIED
))
1302 if (SCM_RECORD_POSITIONS_P
)
1303 return (recsexpr (got
, line
, column
,
1304 SCM_FILENAME (port
)));
1310 return SCM_UNSPECIFIED
;
1313 /* The reader for the sharp `#' character. It basically dispatches reads
1314 among the above token readers. */
1316 scm_read_sharp (scm_t_wchar chr
, SCM port
)
1317 #define FUNC_NAME "scm_lreadr"
1321 chr
= scm_getc (port
);
1323 result
= scm_read_sharp_extension (chr
, port
);
1324 if (!scm_is_eq (result
, SCM_UNSPECIFIED
))
1330 return (scm_read_character (chr
, port
));
1332 return (scm_read_vector (chr
, port
));
1336 /* This one may return either a boolean or an SRFI-4 vector. */
1337 return (scm_read_srfi4_vector (chr
, port
));
1339 return (scm_read_bytevector (chr
, port
));
1341 return (scm_read_guile_bit_vector (chr
, port
));
1345 /* This one may return either a boolean or an SRFI-4 vector. */
1346 return (scm_read_boolean (chr
, port
));
1348 return (scm_read_keyword (chr
, port
));
1349 case '0': case '1': case '2': case '3': case '4':
1350 case '5': case '6': case '7': case '8': case '9':
1352 #if SCM_ENABLE_DEPRECATED
1353 /* See below for 'i' and 'e'. */
1360 return (scm_i_read_array (port
, chr
));
1364 #if SCM_ENABLE_DEPRECATED
1366 /* When next char is '(', it really is an old-style
1368 scm_t_wchar next_c
= scm_getc (port
);
1370 scm_ungetc (next_c
, port
);
1372 return scm_i_read_array (port
, chr
);
1386 return (scm_read_number_and_radix (chr
, port
));
1388 return (scm_read_extended_symbol (chr
, port
));
1390 return (scm_read_shebang (chr
, port
));
1392 return (scm_read_commented_expression (chr
, port
));
1396 return (scm_read_syntax (chr
, port
));
1398 return (scm_read_nil (chr
, port
));
1400 result
= scm_read_sharp_extension (chr
, port
);
1401 if (scm_is_eq (result
, SCM_UNSPECIFIED
))
1403 /* To remain compatible with 1.8 and earlier, the following
1404 characters have lower precedence than `read-hash-extend'
1409 return scm_read_r6rs_block_comment (chr
, port
);
1411 scm_i_input_error (FUNC_NAME
, port
, "Unknown # object: ~S",
1412 scm_list_1 (SCM_MAKE_CHAR (chr
)));
1419 return SCM_UNSPECIFIED
;
1424 scm_read_expression (SCM port
)
1425 #define FUNC_NAME "scm_read_expression"
1429 register scm_t_wchar chr
;
1431 chr
= scm_getc (port
);
1435 case SCM_WHITE_SPACES
:
1436 case SCM_LINE_INCREMENTORS
:
1439 (void) scm_read_semicolon_comment (chr
, port
);
1442 if (!SCM_SQUARE_BRACKETS_P
)
1443 return (scm_read_mixed_case_symbol (chr
, port
));
1444 /* otherwise fall through */
1446 return (scm_read_sexp (chr
, port
));
1448 return (scm_read_string (chr
, port
));
1452 return (scm_read_quote (chr
, port
));
1456 result
= scm_read_sharp (chr
, port
);
1457 if (scm_is_eq (result
, SCM_UNSPECIFIED
))
1458 /* We read a comment or some such. */
1464 scm_i_input_error (FUNC_NAME
, port
, "unexpected \")\"", SCM_EOL
);
1467 if (SCM_SQUARE_BRACKETS_P
)
1468 scm_i_input_error (FUNC_NAME
, port
, "unexpected \"]\"", SCM_EOL
);
1469 /* otherwise fall through */
1473 if (scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_prefix
))
1474 return scm_symbol_to_keyword (scm_read_expression (port
));
1479 if (((chr
>= '0') && (chr
<= '9'))
1480 || (strchr ("+-.", chr
)))
1481 return (scm_read_number (chr
, port
));
1483 return (scm_read_mixed_case_symbol (chr
, port
));
1491 /* Actual reader. */
1493 SCM_DEFINE (scm_read
, "read", 0, 1, 0,
1495 "Read an s-expression from the input port @var{port}, or from\n"
1496 "the current input port if @var{port} is not specified.\n"
1497 "Any whitespace before the next token is discarded.")
1498 #define FUNC_NAME s_scm_read
1502 if (SCM_UNBNDP (port
))
1503 port
= scm_current_input_port ();
1504 SCM_VALIDATE_OPINPORT (1, port
);
1506 c
= flush_ws (port
, (char *) NULL
);
1509 scm_ungetc (c
, port
);
1511 return (scm_read_expression (port
));
1518 /* Used when recording expressions constructed by `scm_read_sharp ()'. */
1520 recsexpr (SCM obj
, long line
, int column
, SCM filename
)
1522 if (!scm_is_pair(obj
)) {
1525 SCM tmp
= obj
, copy
;
1526 /* If this sexpr is visible in the read:sharp source, we want to
1527 keep that information, so only record non-constant cons cells
1528 which haven't previously been read by the reader. */
1529 if (scm_is_false (scm_whash_lookup (scm_source_whash
, obj
)))
1531 if (SCM_COPY_SOURCE_P
)
1533 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
1535 while ((tmp
= SCM_CDR (tmp
)) && scm_is_pair (tmp
))
1537 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
1542 copy
= SCM_CDR (copy
);
1544 SCM_SETCDR (copy
, tmp
);
1548 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
1549 while ((tmp
= SCM_CDR (tmp
)) && scm_is_pair (tmp
))
1550 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
1551 copy
= SCM_UNDEFINED
;
1553 scm_whash_insert (scm_source_whash
,
1555 scm_make_srcprops (line
,
1565 /* Manipulate the read-hash-procedures alist. This could be written in
1566 Scheme, but maybe it will also be used by C code during initialisation. */
1567 SCM_DEFINE (scm_read_hash_extend
, "read-hash-extend", 2, 0, 0,
1568 (SCM chr
, SCM proc
),
1569 "Install the procedure @var{proc} for reading expressions\n"
1570 "starting with the character sequence @code{#} and @var{chr}.\n"
1571 "@var{proc} will be called with two arguments: the character\n"
1572 "@var{chr} and the port to read further data from. The object\n"
1573 "returned will be the return value of @code{read}. \n"
1574 "Passing @code{#f} for @var{proc} will remove a previous setting. \n"
1576 #define FUNC_NAME s_scm_read_hash_extend
1581 SCM_VALIDATE_CHAR (1, chr
);
1582 SCM_ASSERT (scm_is_false (proc
)
1583 || scm_is_eq (scm_procedure_p (proc
), SCM_BOOL_T
),
1584 proc
, SCM_ARG2
, FUNC_NAME
);
1586 /* Check if chr is already in the alist. */
1587 this = scm_i_read_hash_procedures_ref ();
1591 if (scm_is_null (this))
1593 /* not found, so add it to the beginning. */
1594 if (scm_is_true (proc
))
1596 SCM
new = scm_cons (scm_cons (chr
, proc
),
1597 scm_i_read_hash_procedures_ref ());
1598 scm_i_read_hash_procedures_set_x (new);
1602 if (scm_is_eq (chr
, SCM_CAAR (this)))
1604 /* already in the alist. */
1605 if (scm_is_false (proc
))
1608 if (scm_is_false (prev
))
1610 SCM rest
= SCM_CDR (scm_i_read_hash_procedures_ref ());
1611 scm_i_read_hash_procedures_set_x (rest
);
1614 scm_set_cdr_x (prev
, SCM_CDR (this));
1619 scm_set_cdr_x (SCM_CAR (this), proc
);
1624 this = SCM_CDR (this);
1627 return SCM_UNSPECIFIED
;
1631 /* Recover the read-hash procedure corresponding to char c. */
1633 scm_get_hash_procedure (int c
)
1635 SCM rest
= scm_i_read_hash_procedures_ref ();
1639 if (scm_is_null (rest
))
1642 if (SCM_CHAR (SCM_CAAR (rest
)) == c
)
1643 return SCM_CDAR (rest
);
1645 rest
= SCM_CDR (rest
);
1649 #define SCM_ENCODING_SEARCH_SIZE (500)
1651 /* Search the first few hundred characters of a file for an Emacs-like coding
1652 declaration. Returns either NULL or a string whose storage has been
1653 allocated with `scm_gc_malloc ()'. */
1655 scm_i_scan_for_encoding (SCM port
)
1657 char header
[SCM_ENCODING_SEARCH_SIZE
+1];
1658 size_t bytes_read
, encoding_length
, i
;
1659 char *encoding
= NULL
;
1661 char *pos
, *encoding_start
;
1664 if (SCM_FPORTP (port
) && !SCM_FDES_RANDOM_P (SCM_FPORT_FDES (port
)))
1665 /* PORT is a non-seekable file port (e.g., as created by Bash when using
1666 "guile <(echo '(display "hello")')") so bail out. */
1669 bytes_read
= scm_c_read (port
, header
, SCM_ENCODING_SEARCH_SIZE
);
1670 header
[bytes_read
] = '\0';
1672 scm_seek (port
, scm_from_int (0), scm_from_int (SEEK_SET
));
1675 && header
[0] == '\xef' && header
[1] == '\xbb' && header
[2] == '\xbf')
1678 /* search past "coding[:=]" */
1682 if ((pos
= strstr(pos
, "coding")) == NULL
)
1685 pos
+= strlen("coding");
1686 if (pos
- header
>= SCM_ENCODING_SEARCH_SIZE
||
1687 (*pos
== ':' || *pos
== '='))
1695 while (pos
- header
<= SCM_ENCODING_SEARCH_SIZE
&&
1696 (*pos
== ' ' || *pos
== '\t'))
1699 /* grab the next token */
1700 encoding_start
= pos
;
1702 while (encoding_start
+ i
- header
<= SCM_ENCODING_SEARCH_SIZE
1703 && encoding_start
+ i
- header
< bytes_read
1704 && (isalnum ((int) encoding_start
[i
])
1705 || strchr ("_-.:/,+=()", encoding_start
[i
]) != NULL
))
1708 encoding_length
= i
;
1709 if (encoding_length
== 0)
1712 encoding
= scm_gc_strndup (encoding_start
, encoding_length
, "encoding");
1713 for (i
= 0; i
< encoding_length
; i
++)
1714 encoding
[i
] = toupper ((int) encoding
[i
]);
1716 /* push backwards to make sure we were in a comment */
1718 pos
= encoding_start
;
1719 while (pos
>= header
)
1723 /* This wasn't in a semicolon comment. Check for a
1724 hash-bang comment. */
1725 char *beg
= strstr (header
, "#!");
1726 char *end
= strstr (header
, "!#");
1727 if (beg
< encoding_start
&& encoding_start
+ encoding_length
< end
)
1739 /* This wasn't in a comment */
1742 if (utf8_bom
&& strcmp(encoding
, "UTF-8"))
1743 scm_misc_error (NULL
,
1744 "the port input declares the encoding ~s but is encoded as UTF-8",
1745 scm_list_1 (scm_from_locale_string (encoding
)));
1750 SCM_DEFINE (scm_file_encoding
, "file-encoding", 1, 0, 0,
1752 "Scans the port for an Emacs-like character coding declaration\n"
1753 "near the top of the contents of a port with random-acessible contents.\n"
1754 "The coding declaration is of the form\n"
1755 "@code{coding: XXXXX} and must appear in a scheme comment.\n"
1757 "Returns a string containing the character encoding of the file\n"
1758 "if a declaration was found, or @code{#f} otherwise.\n")
1759 #define FUNC_NAME s_scm_file_encoding
1764 enc
= scm_i_scan_for_encoding (port
);
1769 s_enc
= scm_from_locale_string (enc
);
1780 SCM read_hash_procs
;
1782 read_hash_procs
= scm_make_fluid ();
1783 scm_fluid_set_x (read_hash_procs
, SCM_EOL
);
1785 scm_i_read_hash_procedures
=
1786 SCM_VARIABLE_LOC (scm_c_define ("%read-hash-procedures", read_hash_procs
));
1788 scm_init_opts (scm_read_options
, scm_read_opts
);
1789 #include "libguile/read.x"