1 /* Copyright (C) 1995,1996,1997,1999,2000,2001,2003, 2004, 2006 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
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
))
137 port
= scm_current_input_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. */
486 j
= scm_read_token (c
, tok_buf
, port
, 0);
487 p
= scm_istr2bve (scm_c_substring_shared (*tok_buf
, 1, j
));
494 j
= scm_read_token (c
, tok_buf
, port
, 1);
495 return scm_string_to_symbol (scm_c_substring_copy (*tok_buf
, 0, j
));
499 j
= scm_read_token (c
, tok_buf
, port
, 0);
501 return SCM_MAKE_CHAR (c
);
502 if (c
>= '0' && c
< '8')
504 /* Dirk:FIXME:: This type of character syntax is not R5RS
505 * compliant. Further, it should be verified that the constant
506 * does only consist of octal digits. Finally, it should be
507 * checked whether the resulting fixnum is in the range of
509 p
= scm_c_locale_stringn_to_number (scm_i_string_chars (*tok_buf
),
512 return SCM_MAKE_CHAR (SCM_I_INUM (p
));
514 for (c
= 0; c
< scm_n_charnames
; c
++)
516 && (scm_i_casei_streq (scm_charnames
[c
],
517 scm_i_string_chars (*tok_buf
), j
)))
518 return SCM_MAKE_CHAR (scm_charnums
[c
]);
519 scm_i_input_error (FUNC_NAME
, port
, "unknown character name ~a",
520 scm_list_1 (scm_c_substring (*tok_buf
, 0, j
)));
522 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
524 return scm_symbol_to_keyword (scm_read (port
));
529 SCM sharp
= scm_get_hash_procedure (c
);
531 if (scm_is_true (sharp
))
533 int line
= SCM_LINUM (port
);
534 int column
= SCM_COL (port
) - 2;
537 got
= scm_call_2 (sharp
, SCM_MAKE_CHAR (c
), port
);
538 if (scm_is_eq (got
, SCM_UNSPECIFIED
))
540 if (SCM_RECORD_POSITIONS_P
)
541 return *copy
= recsexpr (got
, line
, column
,
542 SCM_FILENAME (port
));
548 scm_i_input_error (FUNC_NAME
, port
, "Unknown # object: ~S",
549 scm_list_1 (SCM_MAKE_CHAR (c
)));
554 while ('"' != (c
= scm_getc (port
)))
557 str_eof
: scm_i_input_error (FUNC_NAME
, port
,
558 "end of file in string constant",
561 while (j
+ 2 >= scm_i_string_length (*tok_buf
))
562 scm_grow_tok_buf (tok_buf
);
565 switch (c
= scm_getc (port
))
575 if (SCM_ESCAPED_PARENS_P
)
606 if (a
== EOF
) goto str_eof
;
608 if (b
== EOF
) goto str_eof
;
609 if ('0' <= a
&& a
<= '9') a
-= '0';
610 else if ('A' <= a
&& a
<= 'F') a
= a
- 'A' + 10;
611 else if ('a' <= a
&& a
<= 'f') a
= a
- 'a' + 10;
612 else goto bad_escaped
;
613 if ('0' <= b
&& b
<= '9') b
-= '0';
614 else if ('A' <= b
&& b
<= 'F') b
= b
- 'A' + 10;
615 else if ('a' <= b
&& b
<= 'f') b
= b
- 'a' + 10;
616 else goto bad_escaped
;
622 scm_i_input_error(FUNC_NAME
, port
,
623 "illegal character in escape sequence: ~S",
624 scm_list_1 (SCM_MAKE_CHAR (c
)));
626 scm_c_string_set_x (*tok_buf
, j
, SCM_MAKE_CHAR (c
));
632 /* Change this to scm_c_substring_read_only when
633 SCM_STRING_CHARS has been removed.
635 return scm_c_substring_copy (*tok_buf
, 0, j
);
637 case '0': case '1': case '2': case '3': case '4':
638 case '5': case '6': case '7': case '8': case '9':
643 j
= scm_read_token (c
, tok_buf
, port
, 0);
644 if (j
== 1 && (c
== '+' || c
== '-'))
645 /* Shortcut: Detected symbol '+ or '- */
648 p
= scm_c_locale_stringn_to_number (scm_i_string_chars (*tok_buf
), j
, 10);
653 if ((j
== 2) && (scm_getc (port
) == '('))
655 scm_ungetc ('(', port
);
656 c
= scm_i_string_chars (*tok_buf
)[1];
659 scm_i_input_error (FUNC_NAME
, port
, "unknown # object", SCM_EOL
);
664 if (scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_prefix
))
665 return scm_symbol_to_keyword (scm_read (port
));
672 j
= scm_read_token (c
, tok_buf
, port
, 0);
676 return scm_string_to_symbol (scm_c_substring (*tok_buf
, 0, j
));
683 _Pragma ("noopt"); /* # pragma _CRI noopt */
687 scm_read_token (int ic
, SCM
*tok_buf
, SCM port
, int weird
)
692 c
= (SCM_CASE_INSENSITIVE_P
? scm_c_downcase(ic
) : ic
);
699 while (j
+ 2 >= scm_i_string_length (*tok_buf
))
700 scm_grow_tok_buf (tok_buf
);
701 scm_c_string_set_x (*tok_buf
, j
, SCM_MAKE_CHAR (c
));
707 while (j
+ 2 >= scm_i_string_length (*tok_buf
))
708 scm_grow_tok_buf (tok_buf
);
720 case SCM_WHITE_SPACES
:
721 case SCM_LINE_INCREMENTORS
:
724 || ((!SCM_ELISP_VECTORS_P
) && ((c
== '[') || (c
== ']')))
729 scm_ungetc (c
, port
);
755 scm_ungetc (c
, port
);
763 c
= (SCM_CASE_INSENSITIVE_P
? scm_c_downcase(c
) : c
);
764 scm_c_string_set_x (*tok_buf
, j
, SCM_MAKE_CHAR (c
));
773 _Pragma ("opt"); /* # pragma _CRI opt */
777 scm_i_lreadparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
, char term_char
)
778 #define FUNC_NAME "scm_i_lreadparen"
785 c
= scm_flush_ws (port
, name
);
788 scm_ungetc (c
, port
);
789 if (scm_is_eq (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
791 ans
= scm_lreadr (tok_buf
, port
, copy
);
793 if (term_char
!= (c
= scm_flush_ws (port
, name
)))
794 scm_i_input_error (FUNC_NAME
, port
, "missing close paren", SCM_EOL
);
797 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
798 while (term_char
!= (c
= scm_flush_ws (port
, name
)))
800 scm_ungetc (c
, port
);
801 if (scm_is_eq (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
803 SCM_SETCDR (tl
, scm_lreadr (tok_buf
, port
, copy
));
806 SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
815 scm_lreadrecparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
)
816 #define FUNC_NAME "scm_lreadrecparen"
820 register SCM tl
, tl2
= SCM_EOL
;
821 SCM ans
, ans2
= SCM_EOL
;
822 /* Need to capture line and column numbers here. */
823 int line
= SCM_LINUM (port
);
824 int column
= SCM_COL (port
) - 1;
826 c
= scm_flush_ws (port
, name
);
829 scm_ungetc (c
, port
);
830 if (scm_is_eq (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
832 ans
= scm_lreadr (tok_buf
, port
, copy
);
833 if (')' != (c
= scm_flush_ws (port
, name
)))
834 scm_i_input_error (FUNC_NAME
, port
, "missing close paren", SCM_EOL
);
837 /* Build the head of the list structure. */
838 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
839 if (SCM_COPY_SOURCE_P
)
840 ans2
= tl2
= scm_cons (scm_is_pair (tmp
)
844 while (')' != (c
= scm_flush_ws (port
, name
)))
848 scm_ungetc (c
, port
);
849 if (scm_is_eq (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
851 SCM_SETCDR (tl
, tmp
= scm_lreadr (tok_buf
, port
, copy
));
852 if (SCM_COPY_SOURCE_P
)
853 SCM_SETCDR (tl2
, scm_cons (scm_is_pair (tmp
)
857 if (')' != (c
= scm_flush_ws (port
, name
)))
858 scm_i_input_error (FUNC_NAME
, port
,
859 "missing close paren", SCM_EOL
);
863 new_tail
= scm_cons (tmp
, SCM_EOL
);
864 SCM_SETCDR (tl
, new_tail
);
867 if (SCM_COPY_SOURCE_P
)
869 SCM new_tail2
= scm_cons (scm_is_pair (tmp
) ? *copy
: tmp
, SCM_EOL
);
870 SCM_SETCDR (tl2
, new_tail2
);
875 scm_whash_insert (scm_source_whash
,
877 scm_make_srcprops (line
,
891 /* Manipulate the read-hash-procedures alist. This could be written in
892 Scheme, but maybe it will also be used by C code during initialisation. */
893 SCM_DEFINE (scm_read_hash_extend
, "read-hash-extend", 2, 0, 0,
895 "Install the procedure @var{proc} for reading expressions\n"
896 "starting with the character sequence @code{#} and @var{chr}.\n"
897 "@var{proc} will be called with two arguments: the character\n"
898 "@var{chr} and the port to read further data from. The object\n"
899 "returned will be the return value of @code{read}.")
900 #define FUNC_NAME s_scm_read_hash_extend
905 SCM_VALIDATE_CHAR (1, chr
);
906 SCM_ASSERT (scm_is_false (proc
)
907 || scm_is_eq (scm_procedure_p (proc
), SCM_BOOL_T
),
908 proc
, SCM_ARG2
, FUNC_NAME
);
910 /* Check if chr is already in the alist. */
911 this = *scm_read_hash_procedures
;
915 if (scm_is_null (this))
917 /* not found, so add it to the beginning. */
918 if (scm_is_true (proc
))
920 *scm_read_hash_procedures
=
921 scm_cons (scm_cons (chr
, proc
), *scm_read_hash_procedures
);
925 if (scm_is_eq (chr
, SCM_CAAR (this)))
927 /* already in the alist. */
928 if (scm_is_false (proc
))
931 if (scm_is_false (prev
))
933 *scm_read_hash_procedures
=
934 SCM_CDR (*scm_read_hash_procedures
);
937 scm_set_cdr_x (prev
, SCM_CDR (this));
942 scm_set_cdr_x (SCM_CAR (this), proc
);
947 this = SCM_CDR (this);
950 return SCM_UNSPECIFIED
;
954 /* Recover the read-hash procedure corresponding to char c. */
956 scm_get_hash_procedure (int c
)
958 SCM rest
= *scm_read_hash_procedures
;
962 if (scm_is_null (rest
))
965 if (SCM_CHAR (SCM_CAAR (rest
)) == c
)
966 return SCM_CDAR (rest
);
968 rest
= SCM_CDR (rest
);
975 scm_read_hash_procedures
=
976 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL
));
978 scm_init_opts (scm_read_options
, scm_read_opts
, SCM_N_READ_OPTIONS
);
979 #include "libguile/read.x"