1 /* Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library 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 GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "libguile/_scm.h"
23 #include "libguile/chars.h"
24 #include "libguile/eval.h"
25 #include "libguile/unif.h"
26 #include "libguile/keywords.h"
27 #include "libguile/alist.h"
28 #include "libguile/srcprop.h"
29 #include "libguile/hashtab.h"
30 #include "libguile/hash.h"
31 #include "libguile/ports.h"
32 #include "libguile/root.h"
33 #include "libguile/strings.h"
34 #include "libguile/strports.h"
35 #include "libguile/vectors.h"
36 #include "libguile/validate.h"
38 #include "libguile/read.h"
42 SCM_SYMBOL (scm_keyword_prefix
, "prefix");
44 scm_t_option scm_read_opts
[] = {
45 { SCM_OPTION_BOOLEAN
, "copy", 0,
46 "Copy source code expressions." },
47 { SCM_OPTION_BOOLEAN
, "positions", 0,
48 "Record positions of source code expressions." },
49 { SCM_OPTION_BOOLEAN
, "case-insensitive", 0,
50 "Convert symbols to lower case."},
51 { SCM_OPTION_SCM
, "keywords", SCM_UNPACK (SCM_BOOL_F
),
52 "Style of keyword recognition: #f or 'prefix."}
56 Give meaningful error messages for errors
60 FILE:LINE:COL: MESSAGE
63 This is not standard GNU format, but the test-suite likes the real
64 message to be in front.
70 scm_input_error(char const * function
,
71 SCM port
, const char * message
, SCM arg
)
73 char *fn
= SCM_STRINGP (SCM_FILENAME(port
))
74 ? SCM_STRING_CHARS(SCM_FILENAME(port
))
77 SCM string_port
= scm_open_output_string ();
79 scm_simple_format (string_port
,
80 scm_makfrom0str ("~A:~S:~S: ~A"),
81 scm_list_4 (scm_makfrom0str (fn
),
82 scm_int2num (SCM_LINUM (port
) + 1),
83 scm_int2num (SCM_COL (port
) + 1),
84 scm_makfrom0str (message
)));
87 string
= scm_get_output_string (string_port
);
88 scm_close_output_port (string_port
);
89 scm_error_scm (scm_str2symbol ("read-error"),
90 scm_makfrom0str (function
),
97 SCM_DEFINE (scm_read_options
, "read-options-interface", 0, 1, 0,
99 "Option interface for the read options. Instead of using\n"
100 "this procedure directly, use the procedures @code{read-enable},\n"
101 "@code{read-disable}, @code{read-set!} and @code{read-options}.")
102 #define FUNC_NAME s_scm_read_options
104 SCM ans
= scm_options (setting
,
108 if (SCM_COPY_SOURCE_P
)
109 SCM_RECORD_POSITIONS_P
= 1;
114 /* An association list mapping extra hash characters to procedures. */
115 static SCM
*scm_read_hash_procedures
;
117 SCM_DEFINE (scm_read
, "read", 0, 1, 0,
119 "Read an s-expression from the input port @var{port}, or from\n"
120 "the current input port if @var{port} is not specified.\n"
121 "Any whitespace before the next token is discarded.")
122 #define FUNC_NAME s_scm_read
127 if (SCM_UNBNDP (port
))
129 SCM_VALIDATE_OPINPORT (1, port
);
131 c
= scm_flush_ws (port
, (char *) NULL
);
134 scm_ungetc (c
, port
);
136 tok_buf
= scm_allocate_string (30);
137 return scm_lreadr (&tok_buf
, port
, ©
);
144 scm_grow_tok_buf (SCM
*tok_buf
)
146 size_t oldlen
= SCM_STRING_LENGTH (*tok_buf
);
147 SCM newstr
= scm_allocate_string (2 * oldlen
);
150 for (i
= 0; i
!= oldlen
; ++i
)
151 SCM_STRING_CHARS (newstr
) [i
] = SCM_STRING_CHARS (*tok_buf
) [i
];
154 return SCM_STRING_CHARS (newstr
);
160 scm_flush_ws (SCM port
, const char *eoferr
)
164 switch (c
= scm_getc (port
))
170 scm_input_error (eoferr
,
178 switch (c
= scm_getc (port
))
184 case SCM_LINE_INCREMENTORS
:
188 case SCM_LINE_INCREMENTORS
:
189 case SCM_SINGLE_SPACES
:
200 scm_casei_streq (char *s1
, char *s2
)
203 if (scm_downcase((int)*s1
) != scm_downcase((int)*s2
))
210 return !(*s1
|| *s2
);
214 /* recsexpr is used when recording expressions
215 * constructed by read:sharp.
217 #ifndef DEBUG_EXTENSIONS
218 #define recsexpr(obj, line, column, filename) (obj)
221 recsexpr (SCM obj
, long line
, int column
, SCM filename
)
223 if (!SCM_CONSP(obj
)) {
227 /* If this sexpr is visible in the read:sharp source, we want to
228 keep that information, so only record non-constant cons cells
229 which haven't previously been read by the reader. */
230 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash
, obj
)))
232 if (SCM_COPY_SOURCE_P
)
234 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
236 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
238 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
243 copy
= SCM_CDR (copy
);
245 SCM_SETCDR (copy
, tmp
);
249 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
250 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
251 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
252 copy
= SCM_UNDEFINED
;
254 scm_whash_insert (scm_source_whash
,
256 scm_make_srcprops (line
,
267 /* Consume an SCSH-style block comment. Assume that we've already
268 read the initial `#!', and eat characters until we get a
269 newline/exclamation-point/sharp-sign/newline sequence. */
272 skip_scsh_block_comment (SCM port
)
273 #define FUNC_NAME "skip_scsh_block_comment"
275 /* Is this portable? Dear God, spare me from the non-eight-bit
276 characters. But is it tasteful? */
281 int c
= scm_getc (port
);
284 SCM_MISC_ERROR ("unterminated `#! ... !#' comment", SCM_EOL
);
285 history
= ((history
<< 8) | (c
& 0xff)) & 0xffffffff;
287 /* Were the last four characters read "\n!#\n"? */
288 if (history
== (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
295 static SCM
scm_get_hash_procedure(int c
);
297 static char s_list
[]="list";
300 scm_lreadr (SCM
*tok_buf
, SCM port
, SCM
*copy
)
301 #define FUNC_NAME "scm_lreadr"
308 c
= scm_flush_ws (port
, s_scm_read
);
309 tryagain_no_flush_ws
:
316 return SCM_RECORD_POSITIONS_P
317 ? scm_lreadrecparen (tok_buf
, port
, s_list
, copy
)
318 : scm_lreadparen (tok_buf
, port
, s_list
, copy SCM_ELISP_CLOSE
);
320 scm_input_error (FUNC_NAME
, port
,"unexpected \")\"", SCM_EOL
);
323 #ifdef SCM_ELISP_READ_EXTENSIONS
325 p
= scm_lreadparen (tok_buf
, port
, "vector", copy
, ']');
326 return SCM_NULLP (p
) ? scm_nullvect
: scm_vector (p
);
332 p
= scm_sym_quasiquote
;
337 p
= scm_sym_uq_splicing
;
340 scm_ungetc (c
, port
);
345 scm_lreadr (tok_buf
, port
, copy
),
347 if (SCM_RECORD_POSITIONS_P
)
348 scm_whash_insert (scm_source_whash
,
350 scm_make_srcprops (SCM_LINUM (port
),
354 ? (*copy
= scm_cons2 (SCM_CAR (p
),
355 SCM_CAR (SCM_CDR (p
)),
364 /* Check for user-defined hash procedure first, to allow
365 overriding of builtin hash read syntaxes. */
366 SCM sharp
= scm_get_hash_procedure (c
);
367 if (!SCM_FALSEP (sharp
))
369 int line
= SCM_LINUM (port
);
370 int column
= SCM_COL (port
) - 2;
373 got
= scm_call_2 (sharp
, SCM_MAKE_CHAR (c
), port
);
374 if (SCM_EQ_P (got
, SCM_UNSPECIFIED
))
376 if (SCM_RECORD_POSITIONS_P
)
377 return *copy
= recsexpr (got
, line
, column
,
378 SCM_FILENAME (port
));
387 p
= scm_lreadparen (tok_buf
, port
, "vector", copy SCM_ELISP_CLOSE
);
388 return SCM_NULLP (p
) ? scm_nullvect
: scm_vector (p
);
409 scm_ungetc (c
, port
);
414 /* start of a shell script. Parse as a block comment,
415 terminated by !#, just like SCSH. */
416 skip_scsh_block_comment (port
);
417 /* EOF is not an error here */
418 c
= scm_flush_ws (port
, (char *)NULL
);
419 goto tryagain_no_flush_ws
;
423 j
= scm_read_token (c
, tok_buf
, port
, 0);
424 p
= scm_istr2bve (SCM_STRING_CHARS (*tok_buf
) + 1, (long) (j
- 1));
432 j
= scm_read_token (c
, tok_buf
, port
, 1);
433 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
437 j
= scm_read_token (c
, tok_buf
, port
, 0);
439 return SCM_MAKE_CHAR (c
);
440 if (c
>= '0' && c
< '8')
442 /* Dirk:FIXME:: This type of character syntax is not R5RS
443 * compliant. Further, it should be verified that the constant
444 * does only consist of octal digits. Finally, it should be
445 * checked whether the resulting fixnum is in the range of
447 p
= scm_i_mem2number (SCM_STRING_CHARS (*tok_buf
), j
, 8);
449 return SCM_MAKE_CHAR (SCM_INUM (p
));
451 for (c
= 0; c
< scm_n_charnames
; c
++)
453 && (scm_casei_streq (scm_charnames
[c
], SCM_STRING_CHARS (*tok_buf
))))
454 return SCM_MAKE_CHAR (scm_charnums
[c
]);
455 scm_input_error (FUNC_NAME
, port
, "unknown # object", SCM_EOL
);
457 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
459 j
= scm_read_token ('-', tok_buf
, port
, 0);
460 p
= scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
461 return scm_make_keyword_from_dash_symbol (p
);
466 SCM sharp
= scm_get_hash_procedure (c
);
468 if (!SCM_FALSEP (sharp
))
470 int line
= SCM_LINUM (port
);
471 int column
= SCM_COL (port
) - 2;
474 got
= scm_call_2 (sharp
, SCM_MAKE_CHAR (c
), port
);
475 if (SCM_EQ_P (got
, SCM_UNSPECIFIED
))
477 if (SCM_RECORD_POSITIONS_P
)
478 return *copy
= recsexpr (got
, line
, column
,
479 SCM_FILENAME (port
));
485 scm_input_error (FUNC_NAME
, port
, "Unknown # object: ~S",
486 scm_list_1 (SCM_MAKE_CHAR (c
)));
491 while ('"' != (c
= scm_getc (port
)))
494 scm_input_error (FUNC_NAME
, port
, "end of file in string constant", SCM_EOL
);
496 while (j
+ 2 >= SCM_STRING_LENGTH (*tok_buf
))
497 scm_grow_tok_buf (tok_buf
);
500 switch (c
= scm_getc (port
))
526 SCM_STRING_CHARS (*tok_buf
)[j
] = c
;
531 SCM_STRING_CHARS (*tok_buf
)[j
] = 0;
532 return scm_mem2string (SCM_STRING_CHARS (*tok_buf
), j
);
534 case '0': case '1': case '2': case '3': case '4':
535 case '5': case '6': case '7': case '8': case '9':
540 j
= scm_read_token (c
, tok_buf
, port
, 0);
541 if (j
== 1 && (c
== '+' || c
== '-'))
542 /* Shortcut: Detected symbol '+ or '- */
545 p
= scm_i_mem2number (SCM_STRING_CHARS (*tok_buf
), j
, 10);
550 if ((j
== 2) && (scm_getc (port
) == '('))
552 scm_ungetc ('(', port
);
553 c
= SCM_STRING_CHARS (*tok_buf
)[1];
556 scm_input_error (FUNC_NAME
, port
, "unknown # object", SCM_EOL
);
561 if (SCM_EQ_P (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_prefix
))
563 j
= scm_read_token ('-', tok_buf
, port
, 0);
564 p
= scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
565 return scm_make_keyword_from_dash_symbol (p
);
569 j
= scm_read_token (c
, tok_buf
, port
, 0);
573 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
580 _Pragma ("noopt"); /* # pragma _CRI noopt */
584 scm_read_token (int ic
, SCM
*tok_buf
, SCM port
, int weird
)
590 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(ic
) : ic
);
591 p
= SCM_STRING_CHARS (*tok_buf
);
598 while (j
+ 2 >= SCM_STRING_LENGTH (*tok_buf
))
599 p
= scm_grow_tok_buf (tok_buf
);
606 while (j
+ 2 >= SCM_STRING_LENGTH (*tok_buf
))
607 p
= scm_grow_tok_buf (tok_buf
);
613 #ifdef SCM_ELISP_READ_EXTENSIONS
619 case SCM_WHITE_SPACES
:
620 case SCM_LINE_INCREMENTORS
:
624 scm_ungetc (c
, port
);
652 scm_ungetc (c
, port
);
660 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(c
) : c
);
670 _Pragma ("opt"); /* # pragma _CRI opt */
674 scm_lreadparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
675 #ifdef SCM_ELISP_READ_EXTENSIONS
678 #define term_char ')'
681 #define FUNC_NAME "scm_lreadparen"
688 c
= scm_flush_ws (port
, name
);
691 scm_ungetc (c
, port
);
692 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
694 ans
= scm_lreadr (tok_buf
, port
, copy
);
696 if (term_char
!= (c
= scm_flush_ws (port
, name
)))
697 scm_input_error (FUNC_NAME
, port
, "missing close paren", SCM_EOL
);
700 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
701 while (term_char
!= (c
= scm_flush_ws (port
, name
)))
703 scm_ungetc (c
, port
);
704 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
706 SCM_SETCDR (tl
, scm_lreadr (tok_buf
, port
, copy
));
709 SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
715 #ifndef SCM_ELISP_READ_EXTENSIONS
721 scm_lreadrecparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
)
722 #define FUNC_NAME "scm_lreadrecparen"
726 register SCM tl
, tl2
= SCM_EOL
;
727 SCM ans
, ans2
= SCM_EOL
;
728 /* Need to capture line and column numbers here. */
729 int line
= SCM_LINUM (port
);
730 int column
= SCM_COL (port
) - 1;
732 c
= scm_flush_ws (port
, name
);
735 scm_ungetc (c
, port
);
736 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
738 ans
= scm_lreadr (tok_buf
, port
, copy
);
739 if (')' != (c
= scm_flush_ws (port
, name
)))
740 scm_input_error (FUNC_NAME
, port
, "missing close paren", SCM_EOL
);
743 /* Build the head of the list structure. */
744 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
745 if (SCM_COPY_SOURCE_P
)
746 ans2
= tl2
= scm_cons (SCM_CONSP (tmp
)
750 while (')' != (c
= scm_flush_ws (port
, name
)))
754 scm_ungetc (c
, port
);
755 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
757 SCM_SETCDR (tl
, tmp
= scm_lreadr (tok_buf
, port
, copy
));
758 if (SCM_COPY_SOURCE_P
)
759 SCM_SETCDR (tl2
, scm_cons (SCM_CONSP (tmp
)
763 if (')' != (c
= scm_flush_ws (port
, name
)))
764 scm_input_error (FUNC_NAME
, port
, "missing close paren", SCM_EOL
);
768 new_tail
= scm_cons (tmp
, SCM_EOL
);
769 SCM_SETCDR (tl
, new_tail
);
772 if (SCM_COPY_SOURCE_P
)
774 SCM new_tail2
= scm_cons (SCM_CONSP (tmp
) ? *copy
: tmp
, SCM_EOL
);
775 SCM_SETCDR (tl2
, new_tail2
);
780 scm_whash_insert (scm_source_whash
,
782 scm_make_srcprops (line
,
796 /* Manipulate the read-hash-procedures alist. This could be written in
797 Scheme, but maybe it will also be used by C code during initialisation. */
798 SCM_DEFINE (scm_read_hash_extend
, "read-hash-extend", 2, 0, 0,
800 "Install the procedure @var{proc} for reading expressions\n"
801 "starting with the character sequence @code{#} and @var{chr}.\n"
802 "@var{proc} will be called with two arguments: the character\n"
803 "@var{chr} and the port to read further data from. The object\n"
804 "returned will be the return value of @code{read}.")
805 #define FUNC_NAME s_scm_read_hash_extend
810 SCM_VALIDATE_CHAR (1, chr
);
811 SCM_ASSERT (SCM_FALSEP (proc
)
812 || SCM_EQ_P (scm_procedure_p (proc
), SCM_BOOL_T
),
813 proc
, SCM_ARG2
, FUNC_NAME
);
815 /* Check if chr is already in the alist. */
816 this = *scm_read_hash_procedures
;
820 if (SCM_NULLP (this))
822 /* not found, so add it to the beginning. */
823 if (!SCM_FALSEP (proc
))
825 *scm_read_hash_procedures
=
826 scm_cons (scm_cons (chr
, proc
), *scm_read_hash_procedures
);
830 if (SCM_EQ_P (chr
, SCM_CAAR (this)))
832 /* already in the alist. */
833 if (SCM_FALSEP (proc
))
836 if (SCM_FALSEP (prev
))
838 *scm_read_hash_procedures
=
839 SCM_CDR (*scm_read_hash_procedures
);
842 scm_set_cdr_x (prev
, SCM_CDR (this));
847 scm_set_cdr_x (SCM_CAR (this), proc
);
852 this = SCM_CDR (this);
855 return SCM_UNSPECIFIED
;
859 /* Recover the read-hash procedure corresponding to char c. */
861 scm_get_hash_procedure (int c
)
863 SCM rest
= *scm_read_hash_procedures
;
867 if (SCM_NULLP (rest
))
870 if (SCM_CHAR (SCM_CAAR (rest
)) == c
)
871 return SCM_CDAR (rest
);
873 rest
= SCM_CDR (rest
);
880 scm_read_hash_procedures
=
881 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL
));
883 scm_init_opts (scm_read_options
, scm_read_opts
, SCM_N_READ_OPTIONS
);
884 #include "libguile/read.x"