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"
39 #include "libguile/read.h"
43 SCM_GLOBAL_SYMBOL (scm_sym_dot
, ".");
44 SCM_SYMBOL (scm_keyword_prefix
, "prefix");
46 scm_t_option scm_read_opts
[] = {
47 { SCM_OPTION_BOOLEAN
, "copy", 0,
48 "Copy source code expressions." },
49 { SCM_OPTION_BOOLEAN
, "positions", 0,
50 "Record positions of source code expressions." },
51 { SCM_OPTION_BOOLEAN
, "case-insensitive", 0,
52 "Convert symbols to lower case."},
53 { SCM_OPTION_SCM
, "keywords", SCM_UNPACK (SCM_BOOL_F
),
54 "Style of keyword recognition: #f or 'prefix."}
57 { SCM_OPTION_BOOLEAN
, "elisp-vectors", 0,
58 "Support Elisp vector syntax, namely `[...]'."},
59 { SCM_OPTION_BOOLEAN
, "elisp-strings", 0,
60 "Support `\\(' and `\\)' in strings."}
65 Give meaningful error messages for errors
69 FILE:LINE:COL: MESSAGE
72 This is not standard GNU format, but the test-suite likes the real
73 message to be in front.
79 scm_input_error (char const *function
,
80 SCM port
, const char *message
, SCM arg
)
82 SCM fn
= (scm_is_string (SCM_FILENAME(port
))
84 : scm_from_locale_string ("#<unknown port>"));
86 SCM string_port
= scm_open_output_string ();
88 scm_simple_format (string_port
,
89 scm_from_locale_string ("~A:~S:~S: ~A"),
91 scm_from_int (SCM_LINUM (port
) + 1),
92 scm_from_int (SCM_COL (port
) + 1),
93 scm_from_locale_string (message
)));
95 string
= scm_get_output_string (string_port
);
96 scm_close_output_port (string_port
);
97 scm_error_scm (scm_from_locale_symbol ("read-error"),
98 scm_from_locale_string (function
),
105 SCM_DEFINE (scm_read_options
, "read-options-interface", 0, 1, 0,
107 "Option interface for the read options. Instead of using\n"
108 "this procedure directly, use the procedures @code{read-enable},\n"
109 "@code{read-disable}, @code{read-set!} and @code{read-options}.")
110 #define FUNC_NAME s_scm_read_options
112 SCM ans
= scm_options (setting
,
116 if (SCM_COPY_SOURCE_P
)
117 SCM_RECORD_POSITIONS_P
= 1;
122 /* An association list mapping extra hash characters to procedures. */
123 static SCM
*scm_read_hash_procedures
;
125 SCM_DEFINE (scm_read
, "read", 0, 1, 0,
127 "Read an s-expression from the input port @var{port}, or from\n"
128 "the current input port if @var{port} is not specified.\n"
129 "Any whitespace before the next token is discarded.")
130 #define FUNC_NAME s_scm_read
135 if (SCM_UNBNDP (port
))
137 SCM_VALIDATE_OPINPORT (1, port
);
139 c
= scm_flush_ws (port
, (char *) NULL
);
142 scm_ungetc (c
, port
);
144 tok_buf
= scm_c_make_string (30, SCM_UNDEFINED
);
145 return scm_lreadr (&tok_buf
, port
, ©
);
152 scm_grow_tok_buf (SCM
*tok_buf
)
154 size_t oldlen
= scm_i_string_length (*tok_buf
);
155 const char *olddata
= scm_i_string_chars (*tok_buf
);
157 SCM newstr
= scm_i_make_string (2 * oldlen
, &newdata
);
160 for (i
= 0; i
!= oldlen
; ++i
)
161 newdata
[i
] = olddata
[i
];
167 /* Consume an SCSH-style block comment. Assume that we've already
168 read the initial `#!', and eat characters until we get a
169 exclamation-point/sharp-sign sequence.
173 skip_scsh_block_comment (SCM port
)
179 int c
= scm_getc (port
);
182 scm_input_error ("skip_block_comment", port
,
183 "unterminated `#! ... !#' comment", SCM_EOL
);
187 else if (c
== '#' && bang_seen
)
195 scm_flush_ws (SCM port
, const char *eoferr
)
199 switch (c
= scm_getc (port
))
205 scm_input_error (eoferr
,
213 switch (c
= scm_getc (port
))
219 case SCM_LINE_INCREMENTORS
:
224 switch (c
= scm_getc (port
))
227 eoferr
= "read_sharp";
230 skip_scsh_block_comment (port
);
233 scm_ungetc (c
, port
);
237 case SCM_LINE_INCREMENTORS
:
238 case SCM_SINGLE_SPACES
:
249 scm_casei_streq (char *s1
, char *s2
)
252 if (scm_c_downcase((int)*s1
) != scm_c_downcase((int)*s2
))
259 return !(*s1
|| *s2
);
263 scm_i_casei_streq (const char *s1
, const char *s2
, size_t len2
)
265 while (*s1
&& len2
> 0)
266 if (scm_c_downcase((int)*s1
) != scm_c_downcase((int)*s2
))
274 return !(*s1
|| len2
> 0);
277 /* recsexpr is used when recording expressions
278 * constructed by read:sharp.
281 recsexpr (SCM obj
, long line
, int column
, SCM filename
)
283 if (!SCM_CONSP(obj
)) {
287 /* If this sexpr is visible in the read:sharp source, we want to
288 keep that information, so only record non-constant cons cells
289 which haven't previously been read by the reader. */
290 if (scm_is_false (scm_whash_lookup (scm_source_whash
, obj
)))
292 if (SCM_COPY_SOURCE_P
)
294 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
296 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
298 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
303 copy
= SCM_CDR (copy
);
305 SCM_SETCDR (copy
, tmp
);
309 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
310 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
311 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
312 copy
= SCM_UNDEFINED
;
314 scm_whash_insert (scm_source_whash
,
316 scm_make_srcprops (line
,
327 static SCM
scm_get_hash_procedure(int c
);
328 static SCM
scm_i_lreadparen (SCM
*, SCM
, char *, SCM
*, char);
330 static char s_list
[]="list";
331 static char s_vector
[]="vector";
334 scm_lreadr (SCM
*tok_buf
, SCM port
, SCM
*copy
)
335 #define FUNC_NAME "scm_lreadr"
342 c
= scm_flush_ws (port
, s_scm_read
);
349 return SCM_RECORD_POSITIONS_P
350 ? scm_lreadrecparen (tok_buf
, port
, s_list
, copy
)
351 : scm_i_lreadparen (tok_buf
, port
, s_list
, copy
, ')');
353 scm_input_error (FUNC_NAME
, port
,"unexpected \")\"", SCM_EOL
);
358 if (SCM_ELISP_VECTORS_P
)
360 p
= scm_i_lreadparen (tok_buf
, port
, s_vector
, copy
, ']');
361 return SCM_NULLP (p
) ? scm_nullvect
: scm_vector (p
);
369 p
= scm_sym_quasiquote
;
374 p
= scm_sym_uq_splicing
;
377 scm_ungetc (c
, port
);
382 scm_lreadr (tok_buf
, port
, copy
),
384 if (SCM_RECORD_POSITIONS_P
)
385 scm_whash_insert (scm_source_whash
,
387 scm_make_srcprops (SCM_LINUM (port
),
391 ? (*copy
= scm_cons2 (SCM_CAR (p
),
392 SCM_CAR (SCM_CDR (p
)),
401 /* Check for user-defined hash procedure first, to allow
402 overriding of builtin hash read syntaxes. */
403 SCM sharp
= scm_get_hash_procedure (c
);
404 if (scm_is_true (sharp
))
406 int line
= SCM_LINUM (port
);
407 int column
= SCM_COL (port
) - 2;
410 got
= scm_call_2 (sharp
, SCM_MAKE_CHAR (c
), port
);
411 if (scm_is_eq (got
, SCM_UNSPECIFIED
))
413 if (SCM_RECORD_POSITIONS_P
)
414 return *copy
= recsexpr (got
, line
, column
,
415 SCM_FILENAME (port
));
424 p
= scm_i_lreadparen (tok_buf
, port
, s_vector
, copy
, ')');
425 return SCM_NULLP (p
) ? scm_nullvect
: scm_vector (p
);
446 scm_ungetc (c
, port
);
451 /* should never happen, #!...!# block comments are skipped
452 over in scm_flush_ws. */
457 j
= scm_read_token (c
, tok_buf
, port
, 0);
458 p
= scm_istr2bve (scm_c_substring_shared (*tok_buf
, 1, j
-1));
466 j
= scm_read_token (c
, tok_buf
, port
, 1);
467 return scm_string_to_symbol (scm_c_substring_copy (*tok_buf
, 0, j
));
471 j
= scm_read_token (c
, tok_buf
, port
, 0);
473 return SCM_MAKE_CHAR (c
);
474 if (c
>= '0' && c
< '8')
476 /* Dirk:FIXME:: This type of character syntax is not R5RS
477 * compliant. Further, it should be verified that the constant
478 * does only consist of octal digits. Finally, it should be
479 * checked whether the resulting fixnum is in the range of
481 p
= scm_i_mem2number (scm_i_string_chars (*tok_buf
), j
, 8);
483 return SCM_MAKE_CHAR (SCM_I_INUM (p
));
485 for (c
= 0; c
< scm_n_charnames
; c
++)
487 && (scm_i_casei_streq (scm_charnames
[c
],
488 scm_i_string_chars (*tok_buf
), j
)))
489 return SCM_MAKE_CHAR (scm_charnums
[c
]);
490 scm_input_error (FUNC_NAME
, port
, "unknown character name ~a",
491 scm_list_1 (scm_c_substring (*tok_buf
, 0, j
)));
493 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
495 j
= scm_read_token ('-', tok_buf
, port
, 0);
496 p
= scm_string_to_symbol (scm_c_substring_copy (*tok_buf
, 0, j
));
497 return scm_make_keyword_from_dash_symbol (p
);
502 SCM sharp
= scm_get_hash_procedure (c
);
504 if (scm_is_true (sharp
))
506 int line
= SCM_LINUM (port
);
507 int column
= SCM_COL (port
) - 2;
510 got
= scm_call_2 (sharp
, SCM_MAKE_CHAR (c
), port
);
511 if (scm_is_eq (got
, SCM_UNSPECIFIED
))
513 if (SCM_RECORD_POSITIONS_P
)
514 return *copy
= recsexpr (got
, line
, column
,
515 SCM_FILENAME (port
));
521 scm_input_error (FUNC_NAME
, port
, "Unknown # object: ~S",
522 scm_list_1 (SCM_MAKE_CHAR (c
)));
527 while ('"' != (c
= scm_getc (port
)))
530 str_eof
: scm_input_error (FUNC_NAME
, port
, "end of file in string constant", SCM_EOL
);
532 while (j
+ 2 >= scm_i_string_length (*tok_buf
))
533 scm_grow_tok_buf (tok_buf
);
536 switch (c
= scm_getc (port
))
546 if (SCM_ESCAPED_PARENS_P
)
577 if (a
== EOF
) goto str_eof
;
579 if (b
== EOF
) goto str_eof
;
580 if ('0' <= a
&& a
<= '9') a
-= '0';
581 else if ('A' <= a
&& a
<= 'F') a
= a
- 'A' + 10;
582 else if ('a' <= a
&& a
<= 'f') a
= a
- 'a' + 10;
583 else goto bad_escaped
;
584 if ('0' <= b
&& b
<= '9') b
-= '0';
585 else if ('A' <= b
&& b
<= 'F') b
= b
- 'A' + 10;
586 else if ('a' <= b
&& b
<= 'f') b
= b
- 'a' + 10;
587 else goto bad_escaped
;
593 scm_input_error(FUNC_NAME
, port
,
594 "illegal character in escape sequence: ~S",
595 scm_list_1 (SCM_MAKE_CHAR (c
)));
597 scm_c_string_set_x (*tok_buf
, j
, SCM_MAKE_CHAR (c
));
602 return scm_c_substring_copy (*tok_buf
, 0, j
);
604 case '0': case '1': case '2': case '3': case '4':
605 case '5': case '6': case '7': case '8': case '9':
610 j
= scm_read_token (c
, tok_buf
, port
, 0);
611 if (j
== 1 && (c
== '+' || c
== '-'))
612 /* Shortcut: Detected symbol '+ or '- */
615 p
= scm_i_mem2number (scm_i_string_chars (*tok_buf
), j
, 10);
620 if ((j
== 2) && (scm_getc (port
) == '('))
622 scm_ungetc ('(', port
);
623 c
= scm_i_string_chars (*tok_buf
)[1];
626 scm_input_error (FUNC_NAME
, port
, "unknown # object", SCM_EOL
);
631 if (scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_prefix
))
633 j
= scm_read_token ('-', tok_buf
, port
, 0);
634 p
= scm_string_to_symbol (scm_c_substring (*tok_buf
, 0, j
));
635 return scm_make_keyword_from_dash_symbol (p
);
642 j
= scm_read_token (c
, tok_buf
, port
, 0);
646 return scm_string_to_symbol (scm_c_substring (*tok_buf
, 0, j
));
653 _Pragma ("noopt"); /* # pragma _CRI noopt */
657 scm_read_token (int ic
, SCM
*tok_buf
, SCM port
, int weird
)
662 c
= (SCM_CASE_INSENSITIVE_P
? scm_c_downcase(ic
) : ic
);
669 while (j
+ 2 >= scm_i_string_length (*tok_buf
))
670 scm_grow_tok_buf (tok_buf
);
671 scm_c_string_set_x (*tok_buf
, j
, SCM_MAKE_CHAR (c
));
677 while (j
+ 2 >= scm_i_string_length (*tok_buf
))
678 scm_grow_tok_buf (tok_buf
);
690 case SCM_WHITE_SPACES
:
691 case SCM_LINE_INCREMENTORS
:
694 || ((!SCM_ELISP_VECTORS_P
) && ((c
== '[') || (c
== ']')))
699 scm_ungetc (c
, port
);
725 scm_ungetc (c
, port
);
733 c
= (SCM_CASE_INSENSITIVE_P
? scm_c_downcase(c
) : c
);
734 scm_c_string_set_x (*tok_buf
, j
, SCM_MAKE_CHAR (c
));
743 _Pragma ("opt"); /* # pragma _CRI opt */
747 scm_i_lreadparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
, char term_char
)
748 #define FUNC_NAME "scm_i_lreadparen"
755 c
= scm_flush_ws (port
, name
);
758 scm_ungetc (c
, port
);
759 if (scm_is_eq (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
761 ans
= scm_lreadr (tok_buf
, port
, copy
);
763 if (term_char
!= (c
= scm_flush_ws (port
, name
)))
764 scm_input_error (FUNC_NAME
, port
, "missing close paren", SCM_EOL
);
767 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
768 while (term_char
!= (c
= scm_flush_ws (port
, name
)))
770 scm_ungetc (c
, port
);
771 if (scm_is_eq (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
773 SCM_SETCDR (tl
, scm_lreadr (tok_buf
, port
, copy
));
776 SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
785 scm_lreadrecparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
)
786 #define FUNC_NAME "scm_lreadrecparen"
790 register SCM tl
, tl2
= SCM_EOL
;
791 SCM ans
, ans2
= SCM_EOL
;
792 /* Need to capture line and column numbers here. */
793 int line
= SCM_LINUM (port
);
794 int column
= SCM_COL (port
) - 1;
796 c
= scm_flush_ws (port
, name
);
799 scm_ungetc (c
, port
);
800 if (scm_is_eq (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
802 ans
= scm_lreadr (tok_buf
, port
, copy
);
803 if (')' != (c
= scm_flush_ws (port
, name
)))
804 scm_input_error (FUNC_NAME
, port
, "missing close paren", SCM_EOL
);
807 /* Build the head of the list structure. */
808 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
809 if (SCM_COPY_SOURCE_P
)
810 ans2
= tl2
= scm_cons (SCM_CONSP (tmp
)
814 while (')' != (c
= scm_flush_ws (port
, name
)))
818 scm_ungetc (c
, port
);
819 if (scm_is_eq (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
821 SCM_SETCDR (tl
, tmp
= scm_lreadr (tok_buf
, port
, copy
));
822 if (SCM_COPY_SOURCE_P
)
823 SCM_SETCDR (tl2
, scm_cons (SCM_CONSP (tmp
)
827 if (')' != (c
= scm_flush_ws (port
, name
)))
828 scm_input_error (FUNC_NAME
, port
, "missing close paren", SCM_EOL
);
832 new_tail
= scm_cons (tmp
, SCM_EOL
);
833 SCM_SETCDR (tl
, new_tail
);
836 if (SCM_COPY_SOURCE_P
)
838 SCM new_tail2
= scm_cons (SCM_CONSP (tmp
) ? *copy
: tmp
, SCM_EOL
);
839 SCM_SETCDR (tl2
, new_tail2
);
844 scm_whash_insert (scm_source_whash
,
846 scm_make_srcprops (line
,
860 /* Manipulate the read-hash-procedures alist. This could be written in
861 Scheme, but maybe it will also be used by C code during initialisation. */
862 SCM_DEFINE (scm_read_hash_extend
, "read-hash-extend", 2, 0, 0,
864 "Install the procedure @var{proc} for reading expressions\n"
865 "starting with the character sequence @code{#} and @var{chr}.\n"
866 "@var{proc} will be called with two arguments: the character\n"
867 "@var{chr} and the port to read further data from. The object\n"
868 "returned will be the return value of @code{read}.")
869 #define FUNC_NAME s_scm_read_hash_extend
874 SCM_VALIDATE_CHAR (1, chr
);
875 SCM_ASSERT (scm_is_false (proc
)
876 || scm_is_eq (scm_procedure_p (proc
), SCM_BOOL_T
),
877 proc
, SCM_ARG2
, FUNC_NAME
);
879 /* Check if chr is already in the alist. */
880 this = *scm_read_hash_procedures
;
884 if (SCM_NULLP (this))
886 /* not found, so add it to the beginning. */
887 if (scm_is_true (proc
))
889 *scm_read_hash_procedures
=
890 scm_cons (scm_cons (chr
, proc
), *scm_read_hash_procedures
);
894 if (scm_is_eq (chr
, SCM_CAAR (this)))
896 /* already in the alist. */
897 if (scm_is_false (proc
))
900 if (scm_is_false (prev
))
902 *scm_read_hash_procedures
=
903 SCM_CDR (*scm_read_hash_procedures
);
906 scm_set_cdr_x (prev
, SCM_CDR (this));
911 scm_set_cdr_x (SCM_CAR (this), proc
);
916 this = SCM_CDR (this);
919 return SCM_UNSPECIFIED
;
923 /* Recover the read-hash procedure corresponding to char c. */
925 scm_get_hash_procedure (int c
)
927 SCM rest
= *scm_read_hash_procedures
;
931 if (SCM_NULLP (rest
))
934 if (SCM_CHAR (SCM_CAAR (rest
)) == c
)
935 return SCM_CDAR (rest
);
937 rest
= SCM_CDR (rest
);
944 scm_read_hash_procedures
=
945 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL
));
947 scm_init_opts (scm_read_options
, scm_read_opts
, SCM_N_READ_OPTIONS
);
948 #include "libguile/read.x"