1 /* Copyright (C) 1995,1996,1997,1999,2000,2001,2003, 2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "libguile/_scm.h"
24 #include "libguile/chars.h"
25 #include "libguile/eval.h"
26 #include "libguile/unif.h"
27 #include "libguile/keywords.h"
28 #include "libguile/alist.h"
29 #include "libguile/srcprop.h"
30 #include "libguile/hashtab.h"
31 #include "libguile/hash.h"
32 #include "libguile/ports.h"
33 #include "libguile/root.h"
34 #include "libguile/strings.h"
35 #include "libguile/strports.h"
36 #include "libguile/vectors.h"
37 #include "libguile/validate.h"
38 #include "libguile/srfi-4.h"
40 #include "libguile/read.h"
44 SCM_GLOBAL_SYMBOL (scm_sym_dot
, ".");
45 SCM_SYMBOL (scm_keyword_prefix
, "prefix");
47 scm_t_option scm_read_opts
[] = {
48 { SCM_OPTION_BOOLEAN
, "copy", 0,
49 "Copy source code expressions." },
50 { SCM_OPTION_BOOLEAN
, "positions", 0,
51 "Record positions of source code expressions." },
52 { SCM_OPTION_BOOLEAN
, "case-insensitive", 0,
53 "Convert symbols to lower case."},
54 { SCM_OPTION_SCM
, "keywords", SCM_UNPACK (SCM_BOOL_F
),
55 "Style of keyword recognition: #f or 'prefix."}
58 { SCM_OPTION_BOOLEAN
, "elisp-vectors", 0,
59 "Support Elisp vector syntax, namely `[...]'."},
60 { SCM_OPTION_BOOLEAN
, "elisp-strings", 0,
61 "Support `\\(' and `\\)' in strings."}
66 Give meaningful error messages for errors
70 FILE:LINE:COL: MESSAGE
73 This is not standard GNU format, but the test-suite likes the real
74 message to be in front.
80 scm_i_input_error (char const *function
,
81 SCM port
, const char *message
, SCM arg
)
83 SCM fn
= (scm_is_string (SCM_FILENAME(port
))
85 : scm_from_locale_string ("#<unknown port>"));
87 SCM string_port
= scm_open_output_string ();
89 scm_simple_format (string_port
,
90 scm_from_locale_string ("~A:~S:~S: ~A"),
92 scm_from_int (SCM_LINUM (port
) + 1),
93 scm_from_int (SCM_COL (port
) + 1),
94 scm_from_locale_string (message
)));
96 string
= scm_get_output_string (string_port
);
97 scm_close_output_port (string_port
);
98 scm_error_scm (scm_from_locale_symbol ("read-error"),
99 function
? scm_from_locale_string (function
) : SCM_BOOL_F
,
106 SCM_DEFINE (scm_read_options
, "read-options-interface", 0, 1, 0,
108 "Option interface for the read options. Instead of using\n"
109 "this procedure directly, use the procedures @code{read-enable},\n"
110 "@code{read-disable}, @code{read-set!} and @code{read-options}.")
111 #define FUNC_NAME s_scm_read_options
113 SCM ans
= scm_options (setting
,
117 if (SCM_COPY_SOURCE_P
)
118 SCM_RECORD_POSITIONS_P
= 1;
123 /* An association list mapping extra hash characters to procedures. */
124 static SCM
*scm_read_hash_procedures
;
126 SCM_DEFINE (scm_read
, "read", 0, 1, 0,
128 "Read an s-expression from the input port @var{port}, or from\n"
129 "the current input port if @var{port} is not specified.\n"
130 "Any whitespace before the next token is discarded.")
131 #define FUNC_NAME s_scm_read
136 if (SCM_UNBNDP (port
))
138 SCM_VALIDATE_OPINPORT (1, port
);
140 c
= scm_flush_ws (port
, (char *) NULL
);
143 scm_ungetc (c
, port
);
145 tok_buf
= scm_c_make_string (30, SCM_UNDEFINED
);
146 return scm_lreadr (&tok_buf
, port
, ©
);
153 scm_grow_tok_buf (SCM
*tok_buf
)
155 size_t oldlen
= scm_i_string_length (*tok_buf
);
156 const char *olddata
= scm_i_string_chars (*tok_buf
);
158 SCM newstr
= scm_i_make_string (2 * oldlen
, &newdata
);
161 for (i
= 0; i
!= oldlen
; ++i
)
162 newdata
[i
] = olddata
[i
];
168 /* Consume an SCSH-style block comment. Assume that we've already
169 read the initial `#!', and eat characters until we get a
170 exclamation-point/sharp-sign sequence.
174 skip_scsh_block_comment (SCM port
)
180 int c
= scm_getc (port
);
183 scm_i_input_error ("skip_block_comment", port
,
184 "unterminated `#! ... !#' comment", SCM_EOL
);
188 else if (c
== '#' && bang_seen
)
196 scm_flush_ws (SCM port
, const char *eoferr
)
200 switch (c
= scm_getc (port
))
206 scm_i_input_error (eoferr
,
214 switch (c
= scm_getc (port
))
220 case SCM_LINE_INCREMENTORS
:
225 switch (c
= scm_getc (port
))
228 eoferr
= "read_sharp";
231 skip_scsh_block_comment (port
);
234 scm_ungetc (c
, port
);
238 case SCM_LINE_INCREMENTORS
:
239 case SCM_SINGLE_SPACES
:
250 scm_casei_streq (char *s1
, char *s2
)
253 if (scm_c_downcase((int)*s1
) != scm_c_downcase((int)*s2
))
260 return !(*s1
|| *s2
);
264 scm_i_casei_streq (const char *s1
, const char *s2
, size_t len2
)
266 while (*s1
&& len2
> 0)
267 if (scm_c_downcase((int)*s1
) != scm_c_downcase((int)*s2
))
275 return !(*s1
|| len2
> 0);
278 /* recsexpr is used when recording expressions
279 * constructed by read:sharp.
282 recsexpr (SCM obj
, long line
, int column
, SCM filename
)
284 if (!scm_is_pair(obj
)) {
288 /* If this sexpr is visible in the read:sharp source, we want to
289 keep that information, so only record non-constant cons cells
290 which haven't previously been read by the reader. */
291 if (scm_is_false (scm_whash_lookup (scm_source_whash
, obj
)))
293 if (SCM_COPY_SOURCE_P
)
295 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
297 while ((tmp
= SCM_CDR (tmp
)) && scm_is_pair (tmp
))
299 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
304 copy
= SCM_CDR (copy
);
306 SCM_SETCDR (copy
, tmp
);
310 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
311 while ((tmp
= SCM_CDR (tmp
)) && scm_is_pair (tmp
))
312 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
313 copy
= SCM_UNDEFINED
;
315 scm_whash_insert (scm_source_whash
,
317 scm_make_srcprops (line
,
328 static SCM
scm_get_hash_procedure(int c
);
329 static SCM
scm_i_lreadparen (SCM
*, SCM
, char *, SCM
*, char);
331 static char s_list
[]="list";
332 static char s_vector
[]="vector";
335 scm_lreadr (SCM
*tok_buf
, SCM port
, SCM
*copy
)
336 #define FUNC_NAME "scm_lreadr"
343 c
= scm_flush_ws (port
, s_scm_read
);
350 return SCM_RECORD_POSITIONS_P
351 ? scm_lreadrecparen (tok_buf
, port
, s_list
, copy
)
352 : scm_i_lreadparen (tok_buf
, port
, s_list
, copy
, ')');
354 scm_i_input_error (FUNC_NAME
, port
,"unexpected \")\"", SCM_EOL
);
359 if (SCM_ELISP_VECTORS_P
)
361 p
= scm_i_lreadparen (tok_buf
, port
, s_vector
, copy
, ']');
362 return scm_is_null (p
) ? scm_nullvect
: scm_vector (p
);
370 p
= scm_sym_quasiquote
;
375 p
= scm_sym_uq_splicing
;
378 scm_ungetc (c
, port
);
383 scm_lreadr (tok_buf
, port
, copy
),
385 if (SCM_RECORD_POSITIONS_P
)
386 scm_whash_insert (scm_source_whash
,
388 scm_make_srcprops (SCM_LINUM (port
),
392 ? (*copy
= scm_cons2 (SCM_CAR (p
),
393 SCM_CAR (SCM_CDR (p
)),
402 /* Check for user-defined hash procedure first, to allow
403 overriding of builtin hash read syntaxes. */
404 SCM sharp
= scm_get_hash_procedure (c
);
405 if (scm_is_true (sharp
))
407 int line
= SCM_LINUM (port
);
408 int column
= SCM_COL (port
) - 2;
411 got
= scm_call_2 (sharp
, SCM_MAKE_CHAR (c
), port
);
412 if (scm_is_eq (got
, SCM_UNSPECIFIED
))
414 if (SCM_RECORD_POSITIONS_P
)
415 return *copy
= recsexpr (got
, line
, column
,
416 SCM_FILENAME (port
));
424 /* Vector, arrays, both uniform and not are handled by this
425 one function. It also disambiguates between '#f' and
428 case '0': case '1': case '2': case '3': case '4':
429 case '5': case '6': case '7': case '8': case '9':
430 case 'u': case 's': case 'f':
433 #if SCM_ENABLE_DEPRECATED
434 /* See below for 'i' and 'e'. */
441 return scm_i_read_array (port
, c
);
448 /* See above for lower case 'f'. */
454 #if SCM_ENABLE_DEPRECATED
456 /* When next char is '(', it really is an old-style
458 int next_c
= scm_getc (port
);
460 scm_ungetc (next_c
, port
);
462 return scm_i_read_array (port
, c
);
476 scm_ungetc (c
, port
);
481 /* should never happen, #!...!# block comments are skipped
482 over in scm_flush_ws. */
487 j
= scm_read_token (c
, tok_buf
, port
, 0);
488 p
= scm_istr2bve (scm_c_substring_shared (*tok_buf
, 1, j
));
496 j
= scm_read_token (c
, tok_buf
, port
, 1);
497 return scm_string_to_symbol (scm_c_substring_copy (*tok_buf
, 0, j
));
501 j
= scm_read_token (c
, tok_buf
, port
, 0);
503 return SCM_MAKE_CHAR (c
);
504 if (c
>= '0' && c
< '8')
506 /* Dirk:FIXME:: This type of character syntax is not R5RS
507 * compliant. Further, it should be verified that the constant
508 * does only consist of octal digits. Finally, it should be
509 * checked whether the resulting fixnum is in the range of
511 p
= scm_i_mem2number (scm_i_string_chars (*tok_buf
), j
, 8);
513 return SCM_MAKE_CHAR (SCM_I_INUM (p
));
515 for (c
= 0; c
< scm_n_charnames
; c
++)
517 && (scm_i_casei_streq (scm_charnames
[c
],
518 scm_i_string_chars (*tok_buf
), j
)))
519 return SCM_MAKE_CHAR (scm_charnums
[c
]);
520 scm_i_input_error (FUNC_NAME
, port
, "unknown character name ~a",
521 scm_list_1 (scm_c_substring (*tok_buf
, 0, j
)));
523 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
525 return scm_symbol_to_keyword (scm_read (port
));
530 SCM sharp
= scm_get_hash_procedure (c
);
532 if (scm_is_true (sharp
))
534 int line
= SCM_LINUM (port
);
535 int column
= SCM_COL (port
) - 2;
538 got
= scm_call_2 (sharp
, SCM_MAKE_CHAR (c
), port
);
539 if (scm_is_eq (got
, SCM_UNSPECIFIED
))
541 if (SCM_RECORD_POSITIONS_P
)
542 return *copy
= recsexpr (got
, line
, column
,
543 SCM_FILENAME (port
));
549 scm_i_input_error (FUNC_NAME
, port
, "Unknown # object: ~S",
550 scm_list_1 (SCM_MAKE_CHAR (c
)));
555 while ('"' != (c
= scm_getc (port
)))
558 str_eof
: scm_i_input_error (FUNC_NAME
, port
,
559 "end of file in string constant",
562 while (j
+ 2 >= scm_i_string_length (*tok_buf
))
563 scm_grow_tok_buf (tok_buf
);
566 switch (c
= scm_getc (port
))
576 if (SCM_ESCAPED_PARENS_P
)
607 if (a
== EOF
) goto str_eof
;
609 if (b
== EOF
) goto str_eof
;
610 if ('0' <= a
&& a
<= '9') a
-= '0';
611 else if ('A' <= a
&& a
<= 'F') a
= a
- 'A' + 10;
612 else if ('a' <= a
&& a
<= 'f') a
= a
- 'a' + 10;
613 else goto bad_escaped
;
614 if ('0' <= b
&& b
<= '9') b
-= '0';
615 else if ('A' <= b
&& b
<= 'F') b
= b
- 'A' + 10;
616 else if ('a' <= b
&& b
<= 'f') b
= b
- 'a' + 10;
617 else goto bad_escaped
;
623 scm_i_input_error(FUNC_NAME
, port
,
624 "illegal character in escape sequence: ~S",
625 scm_list_1 (SCM_MAKE_CHAR (c
)));
627 scm_c_string_set_x (*tok_buf
, j
, SCM_MAKE_CHAR (c
));
633 /* Change this to scm_c_substring_read_only when
634 SCM_STRING_CHARS has been removed.
636 return scm_c_substring_copy (*tok_buf
, 0, j
);
638 case '0': case '1': case '2': case '3': case '4':
639 case '5': case '6': case '7': case '8': case '9':
644 j
= scm_read_token (c
, tok_buf
, port
, 0);
645 if (j
== 1 && (c
== '+' || c
== '-'))
646 /* Shortcut: Detected symbol '+ or '- */
649 p
= scm_i_mem2number (scm_i_string_chars (*tok_buf
), j
, 10);
654 if ((j
== 2) && (scm_getc (port
) == '('))
656 scm_ungetc ('(', port
);
657 c
= scm_i_string_chars (*tok_buf
)[1];
660 scm_i_input_error (FUNC_NAME
, port
, "unknown # object", SCM_EOL
);
665 if (scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_prefix
))
666 return scm_symbol_to_keyword (scm_read (port
));
673 j
= scm_read_token (c
, tok_buf
, port
, 0);
677 return scm_string_to_symbol (scm_c_substring (*tok_buf
, 0, j
));
684 _Pragma ("noopt"); /* # pragma _CRI noopt */
688 scm_read_token (int ic
, SCM
*tok_buf
, SCM port
, int weird
)
693 c
= (SCM_CASE_INSENSITIVE_P
? scm_c_downcase(ic
) : ic
);
700 while (j
+ 2 >= scm_i_string_length (*tok_buf
))
701 scm_grow_tok_buf (tok_buf
);
702 scm_c_string_set_x (*tok_buf
, j
, SCM_MAKE_CHAR (c
));
708 while (j
+ 2 >= scm_i_string_length (*tok_buf
))
709 scm_grow_tok_buf (tok_buf
);
721 case SCM_WHITE_SPACES
:
722 case SCM_LINE_INCREMENTORS
:
725 || ((!SCM_ELISP_VECTORS_P
) && ((c
== '[') || (c
== ']')))
730 scm_ungetc (c
, port
);
756 scm_ungetc (c
, port
);
764 c
= (SCM_CASE_INSENSITIVE_P
? scm_c_downcase(c
) : c
);
765 scm_c_string_set_x (*tok_buf
, j
, SCM_MAKE_CHAR (c
));
774 _Pragma ("opt"); /* # pragma _CRI opt */
778 scm_i_lreadparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
, char term_char
)
779 #define FUNC_NAME "scm_i_lreadparen"
786 c
= scm_flush_ws (port
, name
);
789 scm_ungetc (c
, port
);
790 if (scm_is_eq (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
792 ans
= scm_lreadr (tok_buf
, port
, copy
);
794 if (term_char
!= (c
= scm_flush_ws (port
, name
)))
795 scm_i_input_error (FUNC_NAME
, port
, "missing close paren", SCM_EOL
);
798 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
799 while (term_char
!= (c
= scm_flush_ws (port
, name
)))
801 scm_ungetc (c
, port
);
802 if (scm_is_eq (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
804 SCM_SETCDR (tl
, scm_lreadr (tok_buf
, port
, copy
));
807 SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
816 scm_lreadrecparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
)
817 #define FUNC_NAME "scm_lreadrecparen"
821 register SCM tl
, tl2
= SCM_EOL
;
822 SCM ans
, ans2
= SCM_EOL
;
823 /* Need to capture line and column numbers here. */
824 int line
= SCM_LINUM (port
);
825 int column
= SCM_COL (port
) - 1;
827 c
= scm_flush_ws (port
, name
);
830 scm_ungetc (c
, port
);
831 if (scm_is_eq (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
833 ans
= scm_lreadr (tok_buf
, port
, copy
);
834 if (')' != (c
= scm_flush_ws (port
, name
)))
835 scm_i_input_error (FUNC_NAME
, port
, "missing close paren", SCM_EOL
);
838 /* Build the head of the list structure. */
839 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
840 if (SCM_COPY_SOURCE_P
)
841 ans2
= tl2
= scm_cons (scm_is_pair (tmp
)
845 while (')' != (c
= scm_flush_ws (port
, name
)))
849 scm_ungetc (c
, port
);
850 if (scm_is_eq (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
852 SCM_SETCDR (tl
, tmp
= scm_lreadr (tok_buf
, port
, copy
));
853 if (SCM_COPY_SOURCE_P
)
854 SCM_SETCDR (tl2
, scm_cons (scm_is_pair (tmp
)
858 if (')' != (c
= scm_flush_ws (port
, name
)))
859 scm_i_input_error (FUNC_NAME
, port
,
860 "missing close paren", SCM_EOL
);
864 new_tail
= scm_cons (tmp
, SCM_EOL
);
865 SCM_SETCDR (tl
, new_tail
);
868 if (SCM_COPY_SOURCE_P
)
870 SCM new_tail2
= scm_cons (scm_is_pair (tmp
) ? *copy
: tmp
, SCM_EOL
);
871 SCM_SETCDR (tl2
, new_tail2
);
876 scm_whash_insert (scm_source_whash
,
878 scm_make_srcprops (line
,
892 /* Manipulate the read-hash-procedures alist. This could be written in
893 Scheme, but maybe it will also be used by C code during initialisation. */
894 SCM_DEFINE (scm_read_hash_extend
, "read-hash-extend", 2, 0, 0,
896 "Install the procedure @var{proc} for reading expressions\n"
897 "starting with the character sequence @code{#} and @var{chr}.\n"
898 "@var{proc} will be called with two arguments: the character\n"
899 "@var{chr} and the port to read further data from. The object\n"
900 "returned will be the return value of @code{read}.")
901 #define FUNC_NAME s_scm_read_hash_extend
906 SCM_VALIDATE_CHAR (1, chr
);
907 SCM_ASSERT (scm_is_false (proc
)
908 || scm_is_eq (scm_procedure_p (proc
), SCM_BOOL_T
),
909 proc
, SCM_ARG2
, FUNC_NAME
);
911 /* Check if chr is already in the alist. */
912 this = *scm_read_hash_procedures
;
916 if (scm_is_null (this))
918 /* not found, so add it to the beginning. */
919 if (scm_is_true (proc
))
921 *scm_read_hash_procedures
=
922 scm_cons (scm_cons (chr
, proc
), *scm_read_hash_procedures
);
926 if (scm_is_eq (chr
, SCM_CAAR (this)))
928 /* already in the alist. */
929 if (scm_is_false (proc
))
932 if (scm_is_false (prev
))
934 *scm_read_hash_procedures
=
935 SCM_CDR (*scm_read_hash_procedures
);
938 scm_set_cdr_x (prev
, SCM_CDR (this));
943 scm_set_cdr_x (SCM_CAR (this), proc
);
948 this = SCM_CDR (this);
951 return SCM_UNSPECIFIED
;
955 /* Recover the read-hash procedure corresponding to char c. */
957 scm_get_hash_procedure (int c
)
959 SCM rest
= *scm_read_hash_procedures
;
963 if (scm_is_null (rest
))
966 if (SCM_CHAR (SCM_CAAR (rest
)) == c
)
967 return SCM_CDAR (rest
);
969 rest
= SCM_CDR (rest
);
976 scm_read_hash_procedures
=
977 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL
));
979 scm_init_opts (scm_read_options
, scm_read_opts
, SCM_N_READ_OPTIONS
);
980 #include "libguile/read.x"