1 /* Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
46 #include "libguile/_scm.h"
47 #include "libguile/chars.h"
48 #include "libguile/eval.h"
49 #include "libguile/unif.h"
50 #include "libguile/keywords.h"
51 #include "libguile/alist.h"
52 #include "libguile/srcprop.h"
53 #include "libguile/hashtab.h"
54 #include "libguile/hash.h"
55 #include "libguile/ports.h"
56 #include "libguile/root.h"
57 #include "libguile/strings.h"
58 #include "libguile/strports.h"
59 #include "libguile/vectors.h"
60 #include "libguile/validate.h"
62 #include "libguile/read.h"
66 SCM_SYMBOL (scm_keyword_prefix
, "prefix");
68 scm_t_option scm_read_opts
[] = {
69 { SCM_OPTION_BOOLEAN
, "copy", 0,
70 "Copy source code expressions." },
71 { SCM_OPTION_BOOLEAN
, "positions", 0,
72 "Record positions of source code expressions." },
73 { SCM_OPTION_BOOLEAN
, "case-insensitive", 0,
74 "Convert symbols to lower case."},
75 { SCM_OPTION_SCM
, "keywords", SCM_UNPACK (SCM_BOOL_F
),
76 "Style of keyword recognition: #f or 'prefix."}
80 Give meaningful error messages for errors
84 FILE:LINE:COL: MESSAGE
87 This is not standard GNU format, but the test-suite likes the real
88 message to be in front.
94 scm_input_error(char const * function
,
95 SCM port
, const char * message
, SCM arg
)
97 char *fn
= SCM_STRINGP (SCM_FILENAME(port
))
98 ? SCM_STRING_CHARS(SCM_FILENAME(port
))
101 SCM string_port
= scm_open_output_string ();
102 SCM string
= SCM_EOL
;
103 scm_simple_format (string_port
,
104 scm_makfrom0str ("~A:~S:~S: ~A"),
105 scm_list_4 (scm_makfrom0str (fn
),
106 scm_int2num (SCM_LINUM (port
) + 1),
107 scm_int2num (SCM_COL (port
) + 1),
108 scm_makfrom0str (message
)));
111 string
= scm_get_output_string (string_port
);
112 scm_close_output_port (string_port
);
113 scm_error_scm (scm_str2symbol ("read-error"),
114 scm_makfrom0str (function
),
121 SCM_DEFINE (scm_read_options
, "read-options-interface", 0, 1, 0,
123 "Option interface for the read options. Instead of using\n"
124 "this procedure directly, use the procedures @code{read-enable},\n"
125 "@code{read-disable}, @code{read-set!} and @code{read-options}.")
126 #define FUNC_NAME s_scm_read_options
128 SCM ans
= scm_options (setting
,
132 if (SCM_COPY_SOURCE_P
)
133 SCM_RECORD_POSITIONS_P
= 1;
138 /* An association list mapping extra hash characters to procedures. */
139 static SCM
*scm_read_hash_procedures
;
141 SCM_DEFINE (scm_read
, "read", 0, 1, 0,
143 "Read an s-expression from the input port @var{port}, or from\n"
144 "the current input port if @var{port} is not specified.\n"
145 "Any whitespace before the next token is discarded.")
146 #define FUNC_NAME s_scm_read
151 if (SCM_UNBNDP (port
))
153 SCM_VALIDATE_OPINPORT (1, port
);
155 c
= scm_flush_ws (port
, (char *) NULL
);
158 scm_ungetc (c
, port
);
160 tok_buf
= scm_allocate_string (30);
161 return scm_lreadr (&tok_buf
, port
, ©
);
168 scm_grow_tok_buf (SCM
*tok_buf
)
170 size_t oldlen
= SCM_STRING_LENGTH (*tok_buf
);
171 SCM newstr
= scm_allocate_string (2 * oldlen
);
174 for (i
= 0; i
!= oldlen
; ++i
)
175 SCM_STRING_CHARS (newstr
) [i
] = SCM_STRING_CHARS (*tok_buf
) [i
];
178 return SCM_STRING_CHARS (newstr
);
184 scm_flush_ws (SCM port
, const char *eoferr
)
188 switch (c
= scm_getc (port
))
194 scm_input_error (eoferr
,
202 switch (c
= scm_getc (port
))
208 case SCM_LINE_INCREMENTORS
:
212 case SCM_LINE_INCREMENTORS
:
213 case SCM_SINGLE_SPACES
:
224 scm_casei_streq (char *s1
, char *s2
)
227 if (scm_downcase((int)*s1
) != scm_downcase((int)*s2
))
234 return !(*s1
|| *s2
);
238 /* recsexpr is used when recording expressions
239 * constructed by read:sharp.
241 #ifndef DEBUG_EXTENSIONS
242 #define recsexpr(obj, line, column, filename) (obj)
245 recsexpr (SCM obj
, long line
, int column
, SCM filename
)
247 if (!SCM_CONSP(obj
)) {
251 /* If this sexpr is visible in the read:sharp source, we want to
252 keep that information, so only record non-constant cons cells
253 which haven't previously been read by the reader. */
254 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash
, obj
)))
256 if (SCM_COPY_SOURCE_P
)
258 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
260 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
262 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
267 copy
= SCM_CDR (copy
);
269 SCM_SETCDR (copy
, tmp
);
273 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
274 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
275 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
276 copy
= SCM_UNDEFINED
;
278 scm_whash_insert (scm_source_whash
,
280 scm_make_srcprops (line
,
291 /* Consume an SCSH-style block comment. Assume that we've already
292 read the initial `#!', and eat characters until we get a
293 newline/exclamation-point/sharp-sign/newline sequence. */
296 skip_scsh_block_comment (SCM port
)
297 #define FUNC_NAME "skip_scsh_block_comment"
299 /* Is this portable? Dear God, spare me from the non-eight-bit
300 characters. But is it tasteful? */
305 int c
= scm_getc (port
);
308 SCM_MISC_ERROR ("unterminated `#! ... !#' comment", SCM_EOL
);
309 history
= ((history
<< 8) | (c
& 0xff)) & 0xffffffff;
311 /* Were the last four characters read "\n!#\n"? */
312 if (history
== (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
319 static SCM
scm_get_hash_procedure(int c
);
321 static char s_list
[]="list";
324 scm_lreadr (SCM
*tok_buf
, SCM port
, SCM
*copy
)
325 #define FUNC_NAME "scm_lreadr"
332 c
= scm_flush_ws (port
, s_scm_read
);
333 tryagain_no_flush_ws
:
340 return SCM_RECORD_POSITIONS_P
341 ? scm_lreadrecparen (tok_buf
, port
, s_list
, copy
)
342 : scm_lreadparen (tok_buf
, port
, s_list
, copy SCM_ELISP_CLOSE
);
344 scm_input_error (FUNC_NAME
, port
,"unexpected \")\"", SCM_EOL
);
347 #ifdef SCM_ELISP_READ_EXTENSIONS
349 p
= scm_lreadparen (tok_buf
, port
, "vector", copy
, ']');
350 return SCM_NULLP (p
) ? scm_nullvect
: scm_vector (p
);
356 p
= scm_sym_quasiquote
;
361 p
= scm_sym_uq_splicing
;
364 scm_ungetc (c
, port
);
369 scm_lreadr (tok_buf
, port
, copy
),
371 if (SCM_RECORD_POSITIONS_P
)
372 scm_whash_insert (scm_source_whash
,
374 scm_make_srcprops (SCM_LINUM (port
),
378 ? (*copy
= scm_cons2 (SCM_CAR (p
),
379 SCM_CAR (SCM_CDR (p
)),
388 /* Check for user-defined hash procedure first, to allow
389 overriding of builtin hash read syntaxes. */
390 SCM sharp
= scm_get_hash_procedure (c
);
391 if (!SCM_FALSEP (sharp
))
393 int line
= SCM_LINUM (port
);
394 int column
= SCM_COL (port
) - 2;
397 got
= scm_call_2 (sharp
, SCM_MAKE_CHAR (c
), port
);
398 if (SCM_EQ_P (got
, SCM_UNSPECIFIED
))
400 if (SCM_RECORD_POSITIONS_P
)
401 return *copy
= recsexpr (got
, line
, column
,
402 SCM_FILENAME (port
));
411 p
= scm_lreadparen (tok_buf
, port
, "vector", copy SCM_ELISP_CLOSE
);
412 return SCM_NULLP (p
) ? scm_nullvect
: scm_vector (p
);
433 scm_ungetc (c
, port
);
438 /* start of a shell script. Parse as a block comment,
439 terminated by !#, just like SCSH. */
440 skip_scsh_block_comment (port
);
441 /* EOF is not an error here */
442 c
= scm_flush_ws (port
, (char *)NULL
);
443 goto tryagain_no_flush_ws
;
447 j
= scm_read_token (c
, tok_buf
, port
, 0);
448 p
= scm_istr2bve (SCM_STRING_CHARS (*tok_buf
) + 1, (long) (j
- 1));
456 j
= scm_read_token (c
, tok_buf
, port
, 1);
457 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
461 j
= scm_read_token (c
, tok_buf
, port
, 0);
463 return SCM_MAKE_CHAR (c
);
464 if (c
>= '0' && c
< '8')
466 /* Dirk:FIXME:: This type of character syntax is not R5RS
467 * compliant. Further, it should be verified that the constant
468 * does only consist of octal digits. Finally, it should be
469 * checked whether the resulting fixnum is in the range of
471 p
= scm_i_mem2number (SCM_STRING_CHARS (*tok_buf
), j
, 8);
473 return SCM_MAKE_CHAR (SCM_INUM (p
));
475 for (c
= 0; c
< scm_n_charnames
; c
++)
477 && (scm_casei_streq (scm_charnames
[c
], SCM_STRING_CHARS (*tok_buf
))))
478 return SCM_MAKE_CHAR (scm_charnums
[c
]);
479 scm_input_error (FUNC_NAME
, port
, "unknown # object", SCM_EOL
);
481 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
483 j
= scm_read_token ('-', tok_buf
, port
, 0);
484 p
= scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
485 return scm_make_keyword_from_dash_symbol (p
);
490 SCM sharp
= scm_get_hash_procedure (c
);
492 if (!SCM_FALSEP (sharp
))
494 int line
= SCM_LINUM (port
);
495 int column
= SCM_COL (port
) - 2;
498 got
= scm_call_2 (sharp
, SCM_MAKE_CHAR (c
), port
);
499 if (SCM_EQ_P (got
, SCM_UNSPECIFIED
))
501 if (SCM_RECORD_POSITIONS_P
)
502 return *copy
= recsexpr (got
, line
, column
,
503 SCM_FILENAME (port
));
509 scm_input_error (FUNC_NAME
, port
, "Unknown # object: ~S",
510 scm_list_1 (SCM_MAKE_CHAR (c
)));
515 while ('"' != (c
= scm_getc (port
)))
518 scm_input_error (FUNC_NAME
, port
, "end of file in string constant", SCM_EOL
);
520 while (j
+ 2 >= SCM_STRING_LENGTH (*tok_buf
))
521 scm_grow_tok_buf (tok_buf
);
524 switch (c
= scm_getc (port
))
550 SCM_STRING_CHARS (*tok_buf
)[j
] = c
;
555 SCM_STRING_CHARS (*tok_buf
)[j
] = 0;
556 return scm_mem2string (SCM_STRING_CHARS (*tok_buf
), j
);
558 case '0': case '1': case '2': case '3': case '4':
559 case '5': case '6': case '7': case '8': case '9':
564 j
= scm_read_token (c
, tok_buf
, port
, 0);
565 if (j
== 1 && (c
== '+' || c
== '-'))
566 /* Shortcut: Detected symbol '+ or '- */
569 p
= scm_i_mem2number (SCM_STRING_CHARS (*tok_buf
), j
, 10);
574 if ((j
== 2) && (scm_getc (port
) == '('))
576 scm_ungetc ('(', port
);
577 c
= SCM_STRING_CHARS (*tok_buf
)[1];
580 scm_input_error (FUNC_NAME
, port
, "unknown # object", SCM_EOL
);
585 if (SCM_EQ_P (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_prefix
))
587 j
= scm_read_token ('-', tok_buf
, port
, 0);
588 p
= scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
589 return scm_make_keyword_from_dash_symbol (p
);
593 j
= scm_read_token (c
, tok_buf
, port
, 0);
597 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
604 _Pragma ("noopt"); /* # pragma _CRI noopt */
608 scm_read_token (int ic
, SCM
*tok_buf
, SCM port
, int weird
)
614 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(ic
) : ic
);
615 p
= SCM_STRING_CHARS (*tok_buf
);
622 while (j
+ 2 >= SCM_STRING_LENGTH (*tok_buf
))
623 p
= scm_grow_tok_buf (tok_buf
);
630 while (j
+ 2 >= SCM_STRING_LENGTH (*tok_buf
))
631 p
= scm_grow_tok_buf (tok_buf
);
637 #ifdef SCM_ELISP_READ_EXTENSIONS
643 case SCM_WHITE_SPACES
:
644 case SCM_LINE_INCREMENTORS
:
648 scm_ungetc (c
, port
);
676 scm_ungetc (c
, port
);
684 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(c
) : c
);
694 _Pragma ("opt"); /* # pragma _CRI opt */
698 scm_lreadparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
699 #ifdef SCM_ELISP_READ_EXTENSIONS
702 #define term_char ')'
705 #define FUNC_NAME "scm_lreadparen"
712 c
= scm_flush_ws (port
, name
);
715 scm_ungetc (c
, port
);
716 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
718 ans
= scm_lreadr (tok_buf
, port
, copy
);
720 if (term_char
!= (c
= scm_flush_ws (port
, name
)))
721 scm_input_error (FUNC_NAME
, port
, "missing close paren", SCM_EOL
);
724 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
725 while (term_char
!= (c
= scm_flush_ws (port
, name
)))
727 scm_ungetc (c
, port
);
728 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
730 SCM_SETCDR (tl
, scm_lreadr (tok_buf
, port
, copy
));
733 SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
739 #ifndef SCM_ELISP_READ_EXTENSIONS
745 scm_lreadrecparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
)
746 #define FUNC_NAME "scm_lreadrecparen"
750 register SCM tl
, tl2
= SCM_EOL
;
751 SCM ans
, ans2
= SCM_EOL
;
752 /* Need to capture line and column numbers here. */
753 int line
= SCM_LINUM (port
);
754 int column
= SCM_COL (port
) - 1;
756 c
= scm_flush_ws (port
, name
);
759 scm_ungetc (c
, port
);
760 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
762 ans
= scm_lreadr (tok_buf
, port
, copy
);
763 if (')' != (c
= scm_flush_ws (port
, name
)))
764 scm_input_error (FUNC_NAME
, port
, "missing close paren", SCM_EOL
);
767 /* Build the head of the list structure. */
768 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
769 if (SCM_COPY_SOURCE_P
)
770 ans2
= tl2
= scm_cons (SCM_CONSP (tmp
)
774 while (')' != (c
= scm_flush_ws (port
, name
)))
778 scm_ungetc (c
, port
);
779 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
781 SCM_SETCDR (tl
, tmp
= scm_lreadr (tok_buf
, port
, copy
));
782 if (SCM_COPY_SOURCE_P
)
783 SCM_SETCDR (tl2
, scm_cons (SCM_CONSP (tmp
)
787 if (')' != (c
= scm_flush_ws (port
, name
)))
788 scm_input_error (FUNC_NAME
, port
, "missing close paren", SCM_EOL
);
792 new_tail
= scm_cons (tmp
, SCM_EOL
);
793 SCM_SETCDR (tl
, new_tail
);
796 if (SCM_COPY_SOURCE_P
)
798 SCM new_tail2
= scm_cons (SCM_CONSP (tmp
) ? *copy
: tmp
, SCM_EOL
);
799 SCM_SETCDR (tl2
, new_tail2
);
804 scm_whash_insert (scm_source_whash
,
806 scm_make_srcprops (line
,
820 /* Manipulate the read-hash-procedures alist. This could be written in
821 Scheme, but maybe it will also be used by C code during initialisation. */
822 SCM_DEFINE (scm_read_hash_extend
, "read-hash-extend", 2, 0, 0,
824 "Install the procedure @var{proc} for reading expressions\n"
825 "starting with the character sequence @code{#} and @var{chr}.\n"
826 "@var{proc} will be called with two arguments: the character\n"
827 "@var{chr} and the port to read further data from. The object\n"
828 "returned will be the return value of @code{read}.")
829 #define FUNC_NAME s_scm_read_hash_extend
834 SCM_VALIDATE_CHAR (1, chr
);
835 SCM_ASSERT (SCM_FALSEP (proc
)
836 || SCM_EQ_P (scm_procedure_p (proc
), SCM_BOOL_T
),
837 proc
, SCM_ARG2
, FUNC_NAME
);
839 /* Check if chr is already in the alist. */
840 this = *scm_read_hash_procedures
;
844 if (SCM_NULLP (this))
846 /* not found, so add it to the beginning. */
847 if (!SCM_FALSEP (proc
))
849 *scm_read_hash_procedures
=
850 scm_cons (scm_cons (chr
, proc
), *scm_read_hash_procedures
);
854 if (SCM_EQ_P (chr
, SCM_CAAR (this)))
856 /* already in the alist. */
857 if (SCM_FALSEP (proc
))
860 if (SCM_FALSEP (prev
))
862 *scm_read_hash_procedures
=
863 SCM_CDR (*scm_read_hash_procedures
);
866 scm_set_cdr_x (prev
, SCM_CDR (this));
871 scm_set_cdr_x (SCM_CAR (this), proc
);
876 this = SCM_CDR (this);
879 return SCM_UNSPECIFIED
;
883 /* Recover the read-hash procedure corresponding to char c. */
885 scm_get_hash_procedure (int c
)
887 SCM rest
= *scm_read_hash_procedures
;
891 if (SCM_NULLP (rest
))
894 if (SCM_CHAR (SCM_CAAR (rest
)) == c
)
895 return SCM_CDAR (rest
);
897 rest
= SCM_CDR (rest
);
904 scm_read_hash_procedures
=
905 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL
));
907 scm_init_opts (scm_read_options
, scm_read_opts
, SCM_N_READ_OPTIONS
);
908 #include "libguile/read.x"