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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
48 #include "libguile/_scm.h"
49 #include "libguile/chars.h"
50 #include "libguile/eval.h"
51 #include "libguile/unif.h"
52 #include "libguile/keywords.h"
53 #include "libguile/alist.h"
54 #include "libguile/srcprop.h"
55 #include "libguile/hashtab.h"
56 #include "libguile/hash.h"
57 #include "libguile/ports.h"
58 #include "libguile/root.h"
59 #include "libguile/strings.h"
60 #include "libguile/vectors.h"
62 #include "libguile/validate.h"
63 #include "libguile/read.h"
67 SCM_SYMBOL (scm_keyword_prefix
, "prefix");
69 scm_option_t scm_read_opts
[] = {
70 { SCM_OPTION_BOOLEAN
, "copy", 0,
71 "Copy source code expressions." },
72 { SCM_OPTION_BOOLEAN
, "positions", 0,
73 "Record positions of source code expressions." },
74 { SCM_OPTION_BOOLEAN
, "case-insensitive", 0,
75 "Convert symbols to lower case."},
76 { SCM_OPTION_SCM
, "keywords", SCM_UNPACK (SCM_BOOL_F
),
77 "Style of keyword recognition: #f or 'prefix"}
80 SCM_DEFINE (scm_read_options
, "read-options-interface", 0, 1, 0,
82 "Option interface for the read options. Instead of using\n"
83 "this procedure directly, use the procedures @code{read-enable},\n"
84 "@code{read-disable}, @code{read-set!} and @var{read-options}.")
85 #define FUNC_NAME s_scm_read_options
87 SCM ans
= scm_options (setting
,
91 if (SCM_COPY_SOURCE_P
)
92 SCM_RECORD_POSITIONS_P
= 1;
97 /* An association list mapping extra hash characters to procedures. */
98 static SCM
*scm_read_hash_procedures
;
100 SCM_DEFINE (scm_read
, "read", 0, 1, 0,
102 "Read an s-expression from the input port @var{port}, or from\n"
103 "the current input port if @var{port} is not specified.\n"
104 "Any whitespace before the next token is discarded.")
105 #define FUNC_NAME s_scm_read
110 if (SCM_UNBNDP (port
))
112 SCM_VALIDATE_OPINPORT (1,port
);
114 c
= scm_flush_ws (port
, (char *) NULL
);
117 scm_ungetc (c
, port
);
119 tok_buf
= scm_allocate_string (30);
120 return scm_lreadr (&tok_buf
, port
, ©
);
127 scm_grow_tok_buf (SCM
*tok_buf
)
129 size_t oldlen
= SCM_STRING_LENGTH (*tok_buf
);
130 SCM newstr
= scm_allocate_string (2 * oldlen
);
133 for (i
= 0; i
!= oldlen
; ++i
)
134 SCM_STRING_CHARS (newstr
) [i
] = SCM_STRING_CHARS (*tok_buf
) [i
];
137 return SCM_STRING_CHARS (newstr
);
143 scm_flush_ws (SCM port
, const char *eoferr
)
147 switch (c
= scm_getc (port
))
153 if (!SCM_FALSEP (SCM_FILENAME (port
)))
154 scm_misc_error (eoferr
,
156 SCM_LIST1 (SCM_FILENAME (port
)));
158 scm_misc_error (eoferr
, "end of file", SCM_EOL
);
163 switch (c
= scm_getc (port
))
169 case SCM_LINE_INCREMENTORS
:
173 case SCM_LINE_INCREMENTORS
:
174 case SCM_SINGLE_SPACES
:
185 scm_casei_streq (char *s1
, char *s2
)
188 if (scm_downcase((int)*s1
) != scm_downcase((int)*s2
))
195 return !(*s1
|| *s2
);
199 /* recsexpr is used when recording expressions
200 * constructed by read:sharp.
202 #ifndef DEBUG_EXTENSIONS
203 #define recsexpr(obj, line, column, filename) (obj)
206 recsexpr (SCM obj
, long line
, int column
, SCM filename
)
208 if (!SCM_CONSP(obj
)) {
212 /* If this sexpr is visible in the read:sharp source, we want to
213 keep that information, so only record non-constant cons cells
214 which haven't previously been read by the reader. */
215 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash
, obj
)))
217 if (SCM_COPY_SOURCE_P
)
219 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
221 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
223 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
228 copy
= SCM_CDR (copy
);
230 SCM_SETCDR (copy
, tmp
);
234 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
235 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
236 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
237 copy
= SCM_UNDEFINED
;
239 scm_whash_insert (scm_source_whash
,
241 scm_make_srcprops (line
,
252 /* Consume an SCSH-style block comment. Assume that we've already
253 read the initial `#!', and eat characters until we get a
254 newline/exclamation-point/sharp-sign/newline sequence. */
257 skip_scsh_block_comment (SCM port
)
258 #define FUNC_NAME "skip_scsh_block_comment"
260 /* Is this portable? Dear God, spare me from the non-eight-bit
261 characters. But is it tasteful? */
266 int c
= scm_getc (port
);
269 SCM_MISC_ERROR ("unterminated `#! ... !#' comment", SCM_EOL
);
270 history
= ((history
<< 8) | (c
& 0xff)) & 0xffffffff;
272 /* Were the last four characters read "\n!#\n"? */
273 if (history
== (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
280 static SCM
scm_get_hash_procedure(int c
);
282 static char s_list
[]="list";
285 scm_lreadr (SCM
*tok_buf
,SCM port
,SCM
*copy
)
286 #define FUNC_NAME "scm_lreadr"
293 c
= scm_flush_ws (port
, s_scm_read
);
294 tryagain_no_flush_ws
:
301 return SCM_RECORD_POSITIONS_P
302 ? scm_lreadrecparen (tok_buf
, port
, s_list
, copy
)
303 : scm_lreadparen (tok_buf
, port
, s_list
, copy
);
305 SCM_MISC_ERROR ("unexpected \")\"", SCM_EOL
);
312 p
= scm_sym_quasiquote
;
317 p
= scm_sym_uq_splicing
;
320 scm_ungetc (c
, port
);
325 scm_lreadr (tok_buf
, port
, copy
),
327 if (SCM_RECORD_POSITIONS_P
)
328 scm_whash_insert (scm_source_whash
,
330 scm_make_srcprops (SCM_LINUM (port
),
334 ? (*copy
= scm_cons2 (SCM_CAR (p
),
335 SCM_CAR (SCM_CDR (p
)),
345 p
= scm_lreadparen (tok_buf
, port
, "vector", copy
);
346 return SCM_NULLP (p
) ? scm_nullvect
: scm_vector (p
);
367 scm_ungetc (c
, port
);
372 /* start of a shell script. Parse as a block comment,
373 terminated by !#, just like SCSH. */
374 skip_scsh_block_comment (port
);
375 /* EOF is not an error here */
376 c
= scm_flush_ws (port
, (char *)NULL
);
377 goto tryagain_no_flush_ws
;
381 j
= scm_read_token (c
, tok_buf
, port
, 0);
382 p
= scm_istr2bve (SCM_STRING_CHARS (*tok_buf
) + 1, (long) (j
- 1));
390 j
= scm_read_token (c
, tok_buf
, port
, 1);
391 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
395 j
= scm_read_token (c
, tok_buf
, port
, 0);
397 return SCM_MAKE_CHAR (c
);
398 if (c
>= '0' && c
< '8')
400 p
= scm_istr2int (SCM_STRING_CHARS (*tok_buf
), (long) j
, 8);
402 return SCM_MAKE_CHAR (SCM_INUM (p
));
404 for (c
= 0; c
< scm_n_charnames
; c
++)
406 && (scm_casei_streq (scm_charnames
[c
], SCM_STRING_CHARS (*tok_buf
))))
407 return SCM_MAKE_CHAR (scm_charnums
[c
]);
408 SCM_MISC_ERROR ("unknown # object", SCM_EOL
);
410 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
412 j
= scm_read_token ('-', tok_buf
, port
, 0);
413 p
= scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
414 return scm_make_keyword_from_dash_symbol (p
);
419 SCM sharp
= scm_get_hash_procedure (c
);
421 if (SCM_NIMP (sharp
))
423 int line
= SCM_LINUM (port
);
424 int column
= SCM_COL (port
) - 2;
427 got
= scm_apply (sharp
,
429 scm_acons (port
, SCM_EOL
, SCM_EOL
));
430 if (SCM_EQ_P (got
, SCM_UNSPECIFIED
))
432 if (SCM_RECORD_POSITIONS_P
)
433 return *copy
= recsexpr (got
, line
, column
,
434 SCM_FILENAME (port
));
440 scm_misc_error (s_scm_read
, "Unknown # object: ~S",
441 SCM_LIST1 (SCM_MAKE_CHAR (c
)));
446 while ('"' != (c
= scm_getc (port
)))
449 SCM_MISC_ERROR ("end of file in string constant", SCM_EOL
);
451 while (j
+ 2 >= SCM_STRING_LENGTH (*tok_buf
))
452 scm_grow_tok_buf (tok_buf
);
455 switch (c
= scm_getc (port
))
481 SCM_STRING_CHARS (*tok_buf
)[j
] = c
;
486 SCM_STRING_CHARS (*tok_buf
)[j
] = 0;
489 str
= scm_makfromstr (SCM_STRING_CHARS (*tok_buf
), j
, 0);
493 case'0':case '1':case '2':case '3':case '4':
494 case '5':case '6':case '7':case '8':case '9':
499 j
= scm_read_token (c
, tok_buf
, port
, 0);
500 p
= scm_istring2number (SCM_STRING_CHARS (*tok_buf
), (long) j
, 10L);
505 if ((j
== 2) && (scm_getc (port
) == '('))
507 scm_ungetc ('(', port
);
508 c
= SCM_STRING_CHARS (*tok_buf
)[1];
511 SCM_MISC_ERROR ("unknown # object", SCM_EOL
);
516 if (SCM_EQ_P (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_prefix
))
518 j
= scm_read_token ('-', tok_buf
, port
, 0);
519 p
= scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
520 return scm_make_keyword_from_dash_symbol (p
);
524 j
= scm_read_token (c
, tok_buf
, port
, 0);
528 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
535 _Pragma ("noopt"); /* # pragma _CRI noopt */
539 scm_read_token (int ic
, SCM
*tok_buf
, SCM port
, int weird
)
545 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(ic
) : ic
);
546 p
= SCM_STRING_CHARS (*tok_buf
);
553 while (j
+ 2 >= SCM_STRING_LENGTH (*tok_buf
))
554 p
= scm_grow_tok_buf (tok_buf
);
561 while (j
+ 2 >= SCM_STRING_LENGTH (*tok_buf
))
562 p
= scm_grow_tok_buf (tok_buf
);
570 case SCM_WHITE_SPACES
:
571 case SCM_LINE_INCREMENTORS
:
575 scm_ungetc (c
, port
);
603 scm_ungetc (c
, port
);
611 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(c
) : c
);
621 _Pragma ("opt"); /* # pragma _CRI opt */
625 scm_lreadparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
)
626 #define FUNC_NAME "scm_lreadparen"
633 c
= scm_flush_ws (port
, name
);
636 scm_ungetc (c
, port
);
637 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
639 ans
= scm_lreadr (tok_buf
, port
, copy
);
641 if (')' != (c
= scm_flush_ws (port
, name
)))
642 SCM_MISC_ERROR ("missing close paren", SCM_EOL
);
645 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
646 while (')' != (c
= scm_flush_ws (port
, name
)))
648 scm_ungetc (c
, port
);
649 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
651 SCM_SETCDR (tl
, scm_lreadr (tok_buf
, port
, copy
));
654 SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
663 scm_lreadrecparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
)
664 #define FUNC_NAME "scm_lreadrecparen"
668 register SCM tl
, tl2
= SCM_EOL
;
669 SCM ans
, ans2
= SCM_EOL
;
670 /* Need to capture line and column numbers here. */
671 int line
= SCM_LINUM (port
);
672 int column
= SCM_COL (port
) - 1;
674 c
= scm_flush_ws (port
, name
);
677 scm_ungetc (c
, port
);
678 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
680 ans
= scm_lreadr (tok_buf
, port
, copy
);
681 if (')' != (c
= scm_flush_ws (port
, name
)))
682 SCM_MISC_ERROR ("missing close paren", SCM_EOL
);
685 /* Build the head of the list structure. */
686 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
687 if (SCM_COPY_SOURCE_P
)
688 ans2
= tl2
= scm_cons (SCM_CONSP (tmp
)
692 while (')' != (c
= scm_flush_ws (port
, name
)))
696 scm_ungetc (c
, port
);
697 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
699 SCM_SETCDR (tl
, tmp
= scm_lreadr (tok_buf
, port
, copy
));
700 if (SCM_COPY_SOURCE_P
)
701 SCM_SETCDR (tl2
, scm_cons (SCM_CONSP (tmp
)
705 if (')' != (c
= scm_flush_ws (port
, name
)))
706 SCM_MISC_ERROR ("missing close paren", SCM_EOL
);
710 new_tail
= scm_cons (tmp
, SCM_EOL
);
711 SCM_SETCDR (tl
, new_tail
);
714 if (SCM_COPY_SOURCE_P
)
716 SCM new_tail2
= scm_cons (SCM_CONSP (tmp
) ? *copy
: tmp
, SCM_EOL
);
717 SCM_SETCDR (tl2
, new_tail2
);
722 scm_whash_insert (scm_source_whash
,
724 scm_make_srcprops (line
,
738 /* Manipulate the read-hash-procedures alist. This could be written in
739 Scheme, but maybe it will also be used by C code during initialisation. */
740 SCM_DEFINE (scm_read_hash_extend
, "read-hash-extend", 2, 0, 0,
742 "Install the procedure @var{proc} for reading expressions\n"
743 "starting with the character sequence @code{#} and @var{chr}.\n"
744 "@var{proc} will be called with two arguments: the character\n"
745 "@var{chr} and the port to read further data from. The object\n"
746 "returned will be the return value of @code{read}.")
747 #define FUNC_NAME s_scm_read_hash_extend
752 SCM_VALIDATE_CHAR (1,chr
);
753 SCM_ASSERT (SCM_FALSEP (proc
) || SCM_NIMP(proc
), proc
, SCM_ARG2
,
756 /* Check if chr is already in the alist. */
757 this = *scm_read_hash_procedures
;
761 if (SCM_NULLP (this))
763 /* not found, so add it to the beginning. */
764 if (SCM_NFALSEP (proc
))
766 *scm_read_hash_procedures
=
767 scm_cons (scm_cons (chr
, proc
), *scm_read_hash_procedures
);
771 if (SCM_EQ_P (chr
, SCM_CAAR (this)))
773 /* already in the alist. */
774 if (SCM_FALSEP (proc
))
777 if (SCM_FALSEP (prev
))
779 *scm_read_hash_procedures
=
780 SCM_CDR (*scm_read_hash_procedures
);
783 scm_set_cdr_x (prev
, SCM_CDR (this));
788 scm_set_cdr_x (SCM_CAR (this), proc
);
793 this = SCM_CDR (this);
796 return SCM_UNSPECIFIED
;
800 /* Recover the read-hash procedure corresponding to char c. */
802 scm_get_hash_procedure (int c
)
804 SCM rest
= *scm_read_hash_procedures
;
808 if (SCM_NULLP (rest
))
811 if (SCM_CHAR (SCM_CAAR (rest
)) == c
)
812 return SCM_CDAR (rest
);
814 rest
= SCM_CDR (rest
);
821 scm_read_hash_procedures
=
822 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL
));
824 scm_init_opts (scm_read_options
, scm_read_opts
, SCM_N_READ_OPTIONS
);
825 #ifndef SCM_MAGIC_SNARFER
826 #include "libguile/read.x"