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
);
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
, char *buf
, size_t buf_size
, size_t *read
)
194 while (*read
< buf_size
)
198 chr
= scm_getc (port
);
199 chr
= (SCM_CASE_INSENSITIVE_P
? CHAR_DOWNCASE (chr
) : chr
);
203 else if (CHAR_IS_DELIMITER (chr
))
205 scm_ungetc (chr
, port
);
219 /* Skip whitespace from PORT and return the first non-whitespace character
220 read. Raise an error on end-of-file. */
222 flush_ws (SCM port
, const char *eoferr
)
226 switch (c
= scm_getc (port
))
232 scm_i_input_error (eoferr
,
241 switch (c
= scm_getc (port
))
247 case SCM_LINE_INCREMENTORS
:
253 switch (c
= scm_getc (port
))
256 eoferr
= "read_sharp";
259 scm_read_scsh_block_comment (c
, port
);
262 scm_read_commented_expression (c
, port
);
265 scm_ungetc (c
, port
);
270 case SCM_LINE_INCREMENTORS
:
271 case SCM_SINGLE_SPACES
:
286 static SCM
scm_read_expression (SCM port
);
287 static SCM
scm_read_sharp (int chr
, SCM port
);
288 static SCM
scm_get_hash_procedure (int c
);
289 static SCM
recsexpr (SCM obj
, long line
, int column
, SCM filename
);
293 scm_read_sexp (int chr
, SCM port
)
294 #define FUNC_NAME "scm_i_lreadparen"
298 register SCM tl
, ans
= SCM_EOL
;
299 SCM tl2
= SCM_EOL
, ans2
= SCM_EOL
, copy
= SCM_BOOL_F
;
300 static const int terminating_char
= ')';
302 /* Need to capture line and column numbers here. */
303 long line
= SCM_LINUM (port
);
304 int column
= SCM_COL (port
) - 1;
307 c
= flush_ws (port
, FUNC_NAME
);
308 if (terminating_char
== c
)
311 scm_ungetc (c
, port
);
312 if (scm_is_eq (scm_sym_dot
,
313 (tmp
= scm_read_expression (port
))))
315 ans
= scm_read_expression (port
);
316 if (terminating_char
!= (c
= flush_ws (port
, FUNC_NAME
)))
317 scm_i_input_error (FUNC_NAME
, port
, "missing close paren",
322 /* Build the head of the list structure. */
323 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
325 if (SCM_COPY_SOURCE_P
)
326 ans2
= tl2
= scm_cons (scm_is_pair (tmp
)
331 while (terminating_char
!= (c
= flush_ws (port
, FUNC_NAME
)))
335 scm_ungetc (c
, port
);
336 if (scm_is_eq (scm_sym_dot
,
337 (tmp
= scm_read_expression (port
))))
339 SCM_SETCDR (tl
, tmp
= scm_read_expression (port
));
341 if (SCM_COPY_SOURCE_P
)
342 SCM_SETCDR (tl2
, scm_cons (scm_is_pair (tmp
) ? copy
: tmp
,
345 c
= flush_ws (port
, FUNC_NAME
);
346 if (terminating_char
!= c
)
347 scm_i_input_error (FUNC_NAME
, port
,
348 "in pair: missing close paren", SCM_EOL
);
352 new_tail
= scm_cons (tmp
, SCM_EOL
);
353 SCM_SETCDR (tl
, new_tail
);
356 if (SCM_COPY_SOURCE_P
)
358 SCM new_tail2
= scm_cons (scm_is_pair (tmp
)
361 SCM_SETCDR (tl2
, new_tail2
);
367 if (SCM_RECORD_POSITIONS_P
)
368 scm_whash_insert (scm_source_whash
,
370 scm_make_srcprops (line
, column
,
381 scm_read_string (int chr
, SCM port
)
382 #define FUNC_NAME "scm_lreadr"
384 /* For strings smaller than C_STR, this function creates only one Scheme
385 object (the string returned). */
387 SCM str
= SCM_BOOL_F
;
388 char c_str
[READER_STRING_BUFFER_SIZE
];
389 unsigned c_str_len
= 0;
392 while ('"' != (c
= scm_getc (port
)))
395 str_eof
: scm_i_input_error (FUNC_NAME
, port
,
396 "end of file in string constant",
399 if (c_str_len
+ 1 >= sizeof (c_str
))
401 /* Flush the C buffer onto a Scheme string. */
404 if (str
== SCM_BOOL_F
)
405 str
= scm_c_make_string (0, SCM_MAKE_CHAR ('X'));
407 addy
= scm_from_locale_stringn (c_str
, c_str_len
);
408 str
= scm_string_append_shared (scm_list_2 (str
, addy
));
414 switch (c
= scm_getc (port
))
424 if (SCM_ESCAPED_PARENS_P
)
455 if (a
== EOF
) goto str_eof
;
457 if (b
== EOF
) goto str_eof
;
458 if ('0' <= a
&& a
<= '9') a
-= '0';
459 else if ('A' <= a
&& a
<= 'F') a
= a
- 'A' + 10;
460 else if ('a' <= a
&& a
<= 'f') a
= a
- 'a' + 10;
461 else goto bad_escaped
;
462 if ('0' <= b
&& b
<= '9') b
-= '0';
463 else if ('A' <= b
&& b
<= 'F') b
= b
- 'A' + 10;
464 else if ('a' <= b
&& b
<= 'f') b
= b
- 'a' + 10;
465 else goto bad_escaped
;
471 scm_i_input_error (FUNC_NAME
, port
,
472 "illegal character in escape sequence: ~S",
473 scm_list_1 (SCM_MAKE_CHAR (c
)));
475 c_str
[c_str_len
++] = c
;
482 addy
= scm_from_locale_stringn (c_str
, c_str_len
);
483 if (str
== SCM_BOOL_F
)
486 str
= scm_string_append_shared (scm_list_2 (str
, addy
));
489 str
= (str
== SCM_BOOL_F
) ? scm_nullstr
: str
;
497 scm_read_number (int chr
, SCM port
)
499 SCM result
, str
= SCM_EOL
;
500 char buffer
[READER_BUFFER_SIZE
];
504 scm_ungetc (chr
, port
);
507 overflow
= read_token (port
, buffer
, sizeof (buffer
), &read
);
509 if ((overflow
) || (scm_is_pair (str
)))
510 str
= scm_cons (scm_from_locale_stringn (buffer
, read
), str
);
514 if (scm_is_pair (str
))
518 str
= scm_string_concatenate (scm_reverse_x (str
, SCM_EOL
));
519 result
= scm_string_to_number (str
, SCM_UNDEFINED
);
520 if (!scm_is_true (result
))
521 /* Return a symbol instead of a number. */
522 result
= scm_string_to_symbol (str
);
526 result
= scm_c_locale_stringn_to_number (buffer
, read
, 10);
527 if (!scm_is_true (result
))
528 /* Return a symbol instead of a number. */
529 result
= scm_from_locale_symboln (buffer
, read
);
536 scm_read_mixed_case_symbol (int chr
, SCM port
)
538 SCM result
, str
= SCM_EOL
;
539 int overflow
= 0, ends_with_colon
= 0;
540 char buffer
[READER_BUFFER_SIZE
];
542 int postfix
= scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_postfix
);
544 scm_ungetc (chr
, port
);
547 overflow
= read_token (port
, buffer
, sizeof (buffer
), &read
);
550 ends_with_colon
= (buffer
[read
- 1] == ':');
552 if ((overflow
) || (scm_is_pair (str
)))
553 str
= scm_cons (scm_from_locale_stringn (buffer
, read
), str
);
557 if (scm_is_pair (str
))
561 str
= scm_string_concatenate (scm_reverse_x (str
, SCM_EOL
));
562 len
= scm_c_string_length (str
);
564 /* Per SRFI-88, `:' alone is an identifier, not a keyword. */
565 if (postfix
&& ends_with_colon
&& (len
> 1))
567 /* Strip off colon. */
568 str
= scm_c_substring (str
, 0, len
-1);
569 result
= scm_string_to_symbol (str
);
570 result
= scm_symbol_to_keyword (result
);
573 result
= scm_string_to_symbol (str
);
577 /* For symbols smaller than `sizeof (buffer)', we don't need to recur
578 to Scheme strings. Therefore, we only create one Scheme object (a
579 symbol) per symbol read. */
580 if (postfix
&& ends_with_colon
&& (read
> 1))
581 result
= scm_from_locale_keywordn (buffer
, read
- 1);
583 result
= scm_from_locale_symboln (buffer
, read
);
590 scm_read_number_and_radix (int chr
, SCM port
)
591 #define FUNC_NAME "scm_lreadr"
593 SCM result
, str
= SCM_EOL
;
595 char buffer
[READER_BUFFER_SIZE
];
622 scm_ungetc (chr
, port
);
623 scm_ungetc ('#', port
);
629 overflow
= read_token (port
, buffer
, sizeof (buffer
), &read
);
631 if ((overflow
) || (scm_is_pair (str
)))
632 str
= scm_cons (scm_from_locale_stringn (buffer
, read
), str
);
636 if (scm_is_pair (str
))
638 str
= scm_string_concatenate (scm_reverse_x (str
, SCM_EOL
));
639 result
= scm_string_to_number (str
, scm_from_uint (radix
));
642 result
= scm_c_locale_stringn_to_number (buffer
, read
, radix
);
644 if (scm_is_true (result
))
647 scm_i_input_error (FUNC_NAME
, port
, "unknown # object", SCM_EOL
);
654 scm_read_quote (int chr
, SCM port
)
657 long line
= SCM_LINUM (port
);
658 int column
= SCM_COL (port
) - 1;
663 p
= scm_sym_quasiquote
;
676 p
= scm_sym_uq_splicing
;
679 scm_ungetc (c
, port
);
686 fprintf (stderr
, "%s: unhandled quote character (%i)\n",
687 "scm_read_quote", chr
);
691 p
= scm_cons2 (p
, scm_read_expression (port
), SCM_EOL
);
692 if (SCM_RECORD_POSITIONS_P
)
693 scm_whash_insert (scm_source_whash
, p
,
694 scm_make_srcprops (line
, column
,
697 ? (scm_cons2 (SCM_CAR (p
),
698 SCM_CAR (SCM_CDR (p
)),
707 SCM_SYMBOL (sym_syntax
, "syntax");
708 SCM_SYMBOL (sym_quasisyntax
, "quasisyntax");
709 SCM_SYMBOL (sym_unsyntax
, "unsyntax");
710 SCM_SYMBOL (sym_unsyntax_splicing
, "unsyntax-splicing");
713 scm_read_syntax (int chr
, SCM port
)
716 long line
= SCM_LINUM (port
);
717 int column
= SCM_COL (port
) - 1;
735 p
= sym_unsyntax_splicing
;
738 scm_ungetc (c
, port
);
745 fprintf (stderr
, "%s: unhandled syntax character (%i)\n",
746 "scm_read_syntax", chr
);
750 p
= scm_cons2 (p
, scm_read_expression (port
), SCM_EOL
);
751 if (SCM_RECORD_POSITIONS_P
)
752 scm_whash_insert (scm_source_whash
, p
,
753 scm_make_srcprops (line
, column
,
756 ? (scm_cons2 (SCM_CAR (p
),
757 SCM_CAR (SCM_CDR (p
)),
767 scm_read_semicolon_comment (int chr
, SCM port
)
771 for (c
= scm_getc (port
);
772 (c
!= EOF
) && (c
!= '\n');
773 c
= scm_getc (port
));
775 return SCM_UNSPECIFIED
;
779 /* Sharp readers, i.e. readers called after a `#' sign has been read. */
782 scm_read_boolean (int chr
, SCM port
)
795 return SCM_UNSPECIFIED
;
799 scm_read_character (int chr
, SCM port
)
800 #define FUNC_NAME "scm_lreadr"
803 char charname
[READER_CHAR_NAME_MAX_SIZE
];
806 if (read_token (port
, charname
, sizeof (charname
), &charname_len
))
809 if (charname_len
== 0)
811 chr
= scm_getc (port
);
813 scm_i_input_error (FUNC_NAME
, port
, "unexpected end of file "
814 "while reading character", SCM_EOL
);
816 /* CHR must be a token delimiter, like a whitespace. */
817 return (SCM_MAKE_CHAR (chr
));
820 if (charname_len
== 1)
821 return SCM_MAKE_CHAR (charname
[0]);
823 if (*charname
>= '0' && *charname
< '8')
825 /* Dirk:FIXME:: This type of character syntax is not R5RS
826 * compliant. Further, it should be verified that the constant
827 * does only consist of octal digits. Finally, it should be
828 * checked whether the resulting fixnum is in the range of
830 SCM p
= scm_c_locale_stringn_to_number (charname
, charname_len
, 8);
832 return SCM_MAKE_CHAR (SCM_I_INUM (p
));
835 for (c
= 0; c
< scm_n_charnames
; c
++)
837 && (!strncasecmp (scm_charnames
[c
], charname
, charname_len
)))
838 return SCM_MAKE_CHAR (scm_charnums
[c
]);
841 scm_i_input_error (FUNC_NAME
, port
, "unknown character name ~a",
842 scm_list_1 (scm_from_locale_stringn (charname
,
845 return SCM_UNSPECIFIED
;
850 scm_read_keyword (int chr
, SCM port
)
854 /* Read the symbol that comprises the keyword. Doing this instead of
855 invoking a specific symbol reader function allows `scm_read_keyword ()'
856 to adapt to the delimiters currently valid of symbols.
858 XXX: This implementation allows sloppy syntaxes like `#: key'. */
859 symbol
= scm_read_expression (port
);
860 if (!scm_is_symbol (symbol
))
861 scm_i_input_error ("scm_read_keyword", port
,
862 "keyword prefix `~a' not followed by a symbol: ~s",
863 scm_list_2 (SCM_MAKE_CHAR (chr
), symbol
));
865 return (scm_symbol_to_keyword (symbol
));
869 scm_read_vector (int chr
, SCM port
)
871 /* Note: We call `scm_read_sexp ()' rather than READER here in order to
872 guarantee that it's going to do what we want. After all, this is an
873 implementation detail of `scm_read_vector ()', not a desirable
875 return (scm_vector (scm_read_sexp (chr
, port
)));
879 scm_read_srfi4_vector (int chr
, SCM port
)
881 return scm_i_read_array (port
, chr
);
885 scm_read_guile_bit_vector (int chr
, SCM port
)
887 /* Read the `#*10101'-style read syntax for bit vectors in Guile. This is
888 terribly inefficient but who cares? */
889 SCM s_bits
= SCM_EOL
;
891 for (chr
= scm_getc (port
);
892 (chr
!= EOF
) && ((chr
== '0') || (chr
== '1'));
893 chr
= scm_getc (port
))
895 s_bits
= scm_cons ((chr
== '0') ? SCM_BOOL_F
: SCM_BOOL_T
, s_bits
);
899 scm_ungetc (chr
, port
);
901 return scm_bitvector (scm_reverse_x (s_bits
, SCM_EOL
));
905 scm_read_scsh_block_comment (int chr
, SCM port
)
911 int c
= scm_getc (port
);
914 scm_i_input_error ("skip_block_comment", port
,
915 "unterminated `#! ... !#' comment", SCM_EOL
);
919 else if (c
== '#' && bang_seen
)
925 return SCM_UNSPECIFIED
;
929 scm_read_commented_expression (int chr
, SCM port
)
933 c
= flush_ws (port
, (char *) NULL
);
935 scm_i_input_error ("read_commented_expression", port
,
936 "no expression after #; comment", SCM_EOL
);
937 scm_ungetc (c
, port
);
938 scm_read_expression (port
);
939 return SCM_UNSPECIFIED
;
943 scm_read_extended_symbol (int chr
, SCM port
)
945 /* Guile's extended symbol read syntax looks like this:
947 #{This is all a symbol name}#
949 So here, CHR is expected to be `{'. */
951 int saw_brace
= 0, finished
= 0;
955 result
= scm_c_make_string (0, SCM_MAKE_CHAR ('X'));
957 while ((chr
= scm_getc (port
)) != EOF
)
978 if (len
>= sizeof (buf
) - 2)
980 scm_string_append (scm_list_2 (result
,
981 scm_from_locale_stringn (buf
, len
)));
990 result
= scm_string_append (scm_list_2
992 scm_from_locale_stringn (buf
, len
)));
994 return (scm_string_to_symbol (result
));
999 /* Top-level token readers, i.e., dispatchers. */
1002 scm_read_sharp_extension (int chr
, SCM port
)
1006 proc
= scm_get_hash_procedure (chr
);
1007 if (scm_is_true (scm_procedure_p (proc
)))
1009 long line
= SCM_LINUM (port
);
1010 int column
= SCM_COL (port
) - 2;
1013 got
= scm_call_2 (proc
, SCM_MAKE_CHAR (chr
), port
);
1014 if (!scm_is_eq (got
, SCM_UNSPECIFIED
))
1016 if (SCM_RECORD_POSITIONS_P
)
1017 return (recsexpr (got
, line
, column
,
1018 SCM_FILENAME (port
)));
1024 return SCM_UNSPECIFIED
;
1027 /* The reader for the sharp `#' character. It basically dispatches reads
1028 among the above token readers. */
1030 scm_read_sharp (int chr
, SCM port
)
1031 #define FUNC_NAME "scm_lreadr"
1035 chr
= scm_getc (port
);
1037 result
= scm_read_sharp_extension (chr
, port
);
1038 if (!scm_is_eq (result
, SCM_UNSPECIFIED
))
1044 return (scm_read_character (chr
, port
));
1046 return (scm_read_vector (chr
, port
));
1050 /* This one may return either a boolean or an SRFI-4 vector. */
1051 return (scm_read_srfi4_vector (chr
, port
));
1053 return (scm_read_guile_bit_vector (chr
, port
));
1057 /* This one may return either a boolean or an SRFI-4 vector. */
1058 return (scm_read_boolean (chr
, port
));
1060 return (scm_read_keyword (chr
, port
));
1061 case '0': case '1': case '2': case '3': case '4':
1062 case '5': case '6': case '7': case '8': case '9':
1064 #if SCM_ENABLE_DEPRECATED
1065 /* See below for 'i' and 'e'. */
1072 return (scm_i_read_array (port
, chr
));
1076 #if SCM_ENABLE_DEPRECATED
1078 /* When next char is '(', it really is an old-style
1080 int next_c
= scm_getc (port
);
1082 scm_ungetc (next_c
, port
);
1084 return scm_i_read_array (port
, chr
);
1098 return (scm_read_number_and_radix (chr
, port
));
1100 return (scm_read_extended_symbol (chr
, port
));
1102 return (scm_read_scsh_block_comment (chr
, port
));
1104 return (scm_read_commented_expression (chr
, port
));
1108 return (scm_read_syntax (chr
, port
));
1110 result
= scm_read_sharp_extension (chr
, port
);
1111 if (scm_is_eq (result
, SCM_UNSPECIFIED
))
1112 scm_i_input_error (FUNC_NAME
, port
, "Unknown # object: ~S",
1113 scm_list_1 (SCM_MAKE_CHAR (chr
)));
1118 return SCM_UNSPECIFIED
;
1123 scm_read_expression (SCM port
)
1124 #define FUNC_NAME "scm_read_expression"
1130 chr
= scm_getc (port
);
1134 case SCM_WHITE_SPACES
:
1135 case SCM_LINE_INCREMENTORS
:
1138 (void) scm_read_semicolon_comment (chr
, port
);
1141 return (scm_read_sexp (chr
, port
));
1143 return (scm_read_string (chr
, port
));
1147 return (scm_read_quote (chr
, port
));
1151 result
= scm_read_sharp (chr
, port
);
1152 if (scm_is_eq (result
, SCM_UNSPECIFIED
))
1153 /* We read a comment or some such. */
1159 scm_i_input_error (FUNC_NAME
, port
, "unexpected \")\"", SCM_EOL
);
1164 if (scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_prefix
))
1165 return scm_symbol_to_keyword (scm_read_expression (port
));
1170 if (((chr
>= '0') && (chr
<= '9'))
1171 || (strchr ("+-.", chr
)))
1172 return (scm_read_number (chr
, port
));
1174 return (scm_read_mixed_case_symbol (chr
, port
));
1182 /* Actual reader. */
1184 SCM_DEFINE (scm_read
, "read", 0, 1, 0,
1186 "Read an s-expression from the input port @var{port}, or from\n"
1187 "the current input port if @var{port} is not specified.\n"
1188 "Any whitespace before the next token is discarded.")
1189 #define FUNC_NAME s_scm_read
1193 if (SCM_UNBNDP (port
))
1194 port
= scm_current_input_port ();
1195 SCM_VALIDATE_OPINPORT (1, port
);
1197 c
= flush_ws (port
, (char *) NULL
);
1200 scm_ungetc (c
, port
);
1202 return (scm_read_expression (port
));
1209 /* Used when recording expressions constructed by `scm_read_sharp ()'. */
1211 recsexpr (SCM obj
, long line
, int column
, SCM filename
)
1213 if (!scm_is_pair(obj
)) {
1216 SCM tmp
= obj
, copy
;
1217 /* If this sexpr is visible in the read:sharp source, we want to
1218 keep that information, so only record non-constant cons cells
1219 which haven't previously been read by the reader. */
1220 if (scm_is_false (scm_whash_lookup (scm_source_whash
, obj
)))
1222 if (SCM_COPY_SOURCE_P
)
1224 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
1226 while ((tmp
= SCM_CDR (tmp
)) && scm_is_pair (tmp
))
1228 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
1233 copy
= SCM_CDR (copy
);
1235 SCM_SETCDR (copy
, tmp
);
1239 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
1240 while ((tmp
= SCM_CDR (tmp
)) && scm_is_pair (tmp
))
1241 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
1242 copy
= SCM_UNDEFINED
;
1244 scm_whash_insert (scm_source_whash
,
1246 scm_make_srcprops (line
,
1256 /* Manipulate the read-hash-procedures alist. This could be written in
1257 Scheme, but maybe it will also be used by C code during initialisation. */
1258 SCM_DEFINE (scm_read_hash_extend
, "read-hash-extend", 2, 0, 0,
1259 (SCM chr
, SCM proc
),
1260 "Install the procedure @var{proc} for reading expressions\n"
1261 "starting with the character sequence @code{#} and @var{chr}.\n"
1262 "@var{proc} will be called with two arguments: the character\n"
1263 "@var{chr} and the port to read further data from. The object\n"
1264 "returned will be the return value of @code{read}. \n"
1265 "Passing @code{#f} for @var{proc} will remove a previous setting. \n"
1267 #define FUNC_NAME s_scm_read_hash_extend
1272 SCM_VALIDATE_CHAR (1, chr
);
1273 SCM_ASSERT (scm_is_false (proc
)
1274 || scm_is_eq (scm_procedure_p (proc
), SCM_BOOL_T
),
1275 proc
, SCM_ARG2
, FUNC_NAME
);
1277 /* Check if chr is already in the alist. */
1278 this = *scm_read_hash_procedures
;
1282 if (scm_is_null (this))
1284 /* not found, so add it to the beginning. */
1285 if (scm_is_true (proc
))
1287 *scm_read_hash_procedures
=
1288 scm_cons (scm_cons (chr
, proc
), *scm_read_hash_procedures
);
1292 if (scm_is_eq (chr
, SCM_CAAR (this)))
1294 /* already in the alist. */
1295 if (scm_is_false (proc
))
1298 if (scm_is_false (prev
))
1300 *scm_read_hash_procedures
=
1301 SCM_CDR (*scm_read_hash_procedures
);
1304 scm_set_cdr_x (prev
, SCM_CDR (this));
1309 scm_set_cdr_x (SCM_CAR (this), proc
);
1314 this = SCM_CDR (this);
1317 return SCM_UNSPECIFIED
;
1321 /* Recover the read-hash procedure corresponding to char c. */
1323 scm_get_hash_procedure (int c
)
1325 SCM rest
= *scm_read_hash_procedures
;
1329 if (scm_is_null (rest
))
1332 if (SCM_CHAR (SCM_CAAR (rest
)) == c
)
1333 return SCM_CDAR (rest
);
1335 rest
= SCM_CDR (rest
);
1342 scm_read_hash_procedures
=
1343 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL
));
1345 scm_init_opts (scm_read_options
, scm_read_opts
);
1346 #include "libguile/read.x"