1 /* Copyright (C) 1995,1996,1997, 1999, 2000 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 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,
83 #define FUNC_NAME s_scm_read_options
85 SCM ans
= scm_options (setting
,
89 if (SCM_COPY_SOURCE_P
)
90 SCM_RECORD_POSITIONS_P
= 1;
95 /* An association list mapping extra hash characters to procedures. */
96 static SCM
*scm_read_hash_procedures
;
98 SCM_DEFINE (scm_read
, "read", 0, 1, 0,
101 #define FUNC_NAME s_scm_read
106 if (SCM_UNBNDP (port
))
108 SCM_VALIDATE_OPINPORT (1,port
);
110 c
= scm_flush_ws (port
, (char *) NULL
);
113 scm_ungetc (c
, port
);
115 tok_buf
= scm_makstr (30L, 0);
116 return scm_lreadr (&tok_buf
, port
, ©
);
123 scm_grow_tok_buf (SCM
*tok_buf
)
125 unsigned long int oldlen
= SCM_STRING_LENGTH (*tok_buf
);
126 SCM newstr
= scm_makstr (2 * oldlen
, 0);
129 for (i
= 0; i
!= oldlen
; ++i
)
130 SCM_STRING_CHARS (newstr
) [i
] = SCM_STRING_CHARS (*tok_buf
) [i
];
133 return SCM_STRING_CHARS (newstr
);
139 scm_flush_ws (SCM port
, const char *eoferr
)
143 switch (c
= scm_getc (port
))
149 if (!SCM_FALSEP (SCM_FILENAME (port
)))
150 scm_misc_error (eoferr
,
152 SCM_LIST1 (SCM_FILENAME (port
)));
154 scm_misc_error (eoferr
, "end of file", SCM_EOL
);
159 switch (c
= scm_getc (port
))
165 case SCM_LINE_INCREMENTORS
:
169 case SCM_LINE_INCREMENTORS
:
170 case SCM_SINGLE_SPACES
:
181 scm_casei_streq (char *s1
, char *s2
)
184 if (scm_downcase((int)*s1
) != scm_downcase((int)*s2
))
191 return !(*s1
|| *s2
);
195 /* recsexpr is used when recording expressions
196 * constructed by read:sharp.
198 #ifndef DEBUG_EXTENSIONS
199 #define recsexpr(obj, line, column, filename) (obj)
202 recsexpr (SCM obj
,int line
,int column
,SCM filename
)
204 if (!SCM_CONSP(obj
)) {
208 /* If this sexpr is visible in the read:sharp source, we want to
209 keep that information, so only record non-constant cons cells
210 which haven't previously been read by the reader. */
211 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash
, obj
)))
213 if (SCM_COPY_SOURCE_P
)
215 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
217 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
219 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
224 copy
= SCM_CDR (copy
);
226 SCM_SETCDR (copy
, tmp
);
230 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
231 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
232 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
233 copy
= SCM_UNDEFINED
;
235 scm_whash_insert (scm_source_whash
,
237 scm_make_srcprops (line
,
248 /* Consume an SCSH-style block comment. Assume that we've already
249 read the initial `#!', and eat characters until we get a
250 newline/exclamation-point/sharp-sign/newline sequence. */
253 skip_scsh_block_comment (SCM port
)
255 /* Is this portable? Dear God, spare me from the non-eight-bit
256 characters. But is it tasteful? */
261 int c
= scm_getc (port
);
264 scm_wta (SCM_UNDEFINED
,
265 "unterminated `#! ... !#' comment", "read");
266 history
= ((history
<< 8) | (c
& 0xff)) & 0xffffffff;
268 /* Were the last four characters read "\n!#\n"? */
269 if (history
== (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
274 static SCM
scm_get_hash_procedure(int c
);
276 static char s_list
[]="list";
279 scm_lreadr (SCM
*tok_buf
,SCM port
,SCM
*copy
)
286 c
= scm_flush_ws (port
, s_scm_read
);
287 tryagain_no_flush_ws
:
294 return SCM_RECORD_POSITIONS_P
295 ? scm_lreadrecparen (tok_buf
, port
, s_list
, copy
)
296 : scm_lreadparen (tok_buf
, port
, s_list
, copy
);
298 scm_wta (SCM_UNDEFINED
, "unexpected \")\"", "read");
305 p
= scm_sym_quasiquote
;
310 p
= scm_sym_uq_splicing
;
313 scm_ungetc (c
, port
);
318 scm_lreadr (tok_buf
, port
, copy
),
320 if (SCM_RECORD_POSITIONS_P
)
321 scm_whash_insert (scm_source_whash
,
323 scm_make_srcprops (SCM_LINUM (port
),
327 ? (*copy
= scm_cons2 (SCM_CAR (p
),
328 SCM_CAR (SCM_CDR (p
)),
338 p
= scm_lreadparen (tok_buf
, port
, "vector", copy
);
339 return SCM_NULLP (p
) ? scm_nullvect
: scm_vector (p
);
360 scm_ungetc (c
, port
);
365 /* start of a shell script. Parse as a block comment,
366 terminated by !#, just like SCSH. */
367 skip_scsh_block_comment (port
);
368 /* EOF is not an error here */
369 c
= scm_flush_ws (port
, (char *)NULL
);
370 goto tryagain_no_flush_ws
;
374 j
= scm_read_token (c
, tok_buf
, port
, 0);
375 p
= scm_istr2bve (SCM_STRING_CHARS (*tok_buf
) + 1, (long) (j
- 1));
383 j
= scm_read_token (c
, tok_buf
, port
, 1);
384 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
388 j
= scm_read_token (c
, tok_buf
, port
, 0);
390 return SCM_MAKE_CHAR (c
);
391 if (c
>= '0' && c
< '8')
393 p
= scm_istr2int (SCM_STRING_CHARS (*tok_buf
), (long) j
, 8);
395 return SCM_MAKE_CHAR (SCM_INUM (p
));
397 for (c
= 0; c
< scm_n_charnames
; c
++)
399 && (scm_casei_streq (scm_charnames
[c
], SCM_STRING_CHARS (*tok_buf
))))
400 return SCM_MAKE_CHAR (scm_charnums
[c
]);
401 scm_wta (SCM_UNDEFINED
, "unknown # object: #\\", SCM_STRING_CHARS (*tok_buf
));
403 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
405 j
= scm_read_token ('-', tok_buf
, port
, 0);
406 p
= scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
407 return scm_make_keyword_from_dash_symbol (p
);
412 SCM sharp
= scm_get_hash_procedure (c
);
414 if (SCM_NIMP (sharp
))
416 int line
= SCM_LINUM (port
);
417 int column
= SCM_COL (port
) - 2;
420 got
= scm_apply (sharp
,
422 scm_acons (port
, SCM_EOL
, SCM_EOL
));
423 if (SCM_EQ_P (got
, SCM_UNSPECIFIED
))
425 if (SCM_RECORD_POSITIONS_P
)
426 return *copy
= recsexpr (got
, line
, column
,
427 SCM_FILENAME (port
));
433 scm_misc_error (s_scm_read
, "Unknown # object: ~S",
434 scm_listify (SCM_MAKE_CHAR (c
), SCM_UNDEFINED
));
439 while ('"' != (c
= scm_getc (port
)))
441 SCM_ASSERT (EOF
!= c
, SCM_UNDEFINED
, "end of file in ", "string");
443 while (j
+ 2 >= SCM_STRING_LENGTH (*tok_buf
))
444 scm_grow_tok_buf (tok_buf
);
447 switch (c
= scm_getc (port
))
473 SCM_STRING_CHARS (*tok_buf
)[j
] = c
;
478 SCM_STRING_CHARS (*tok_buf
)[j
] = 0;
481 str
= scm_makfromstr (SCM_STRING_CHARS (*tok_buf
), j
, 0);
485 case'0':case '1':case '2':case '3':case '4':
486 case '5':case '6':case '7':case '8':case '9':
491 j
= scm_read_token (c
, tok_buf
, port
, 0);
492 p
= scm_istring2number (SCM_STRING_CHARS (*tok_buf
), (long) j
, 10L);
497 if ((j
== 2) && (scm_getc (port
) == '('))
499 scm_ungetc ('(', port
);
500 c
= SCM_STRING_CHARS (*tok_buf
)[1];
503 scm_wta (SCM_UNDEFINED
, "unknown # object", SCM_STRING_CHARS (*tok_buf
));
508 if (SCM_EQ_P (SCM_PACK (SCM_KEYWORD_STYLE
), scm_keyword_prefix
))
510 j
= scm_read_token ('-', tok_buf
, port
, 0);
511 p
= scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
512 return scm_make_keyword_from_dash_symbol (p
);
516 j
= scm_read_token (c
, tok_buf
, port
, 0);
520 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf
), j
);
525 _Pragma ("noopt"); /* # pragma _CRI noopt */
529 scm_read_token (int ic
, SCM
*tok_buf
, SCM port
, int weird
)
531 register scm_sizet j
;
535 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(ic
) : ic
);
536 p
= SCM_STRING_CHARS (*tok_buf
);
543 while (j
+ 2 >= SCM_STRING_LENGTH (*tok_buf
))
544 p
= scm_grow_tok_buf (tok_buf
);
551 while (j
+ 2 >= SCM_STRING_LENGTH (*tok_buf
))
552 p
= scm_grow_tok_buf (tok_buf
);
560 case SCM_WHITE_SPACES
:
561 case SCM_LINE_INCREMENTORS
:
565 scm_ungetc (c
, port
);
593 scm_ungetc (c
, port
);
601 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(c
) : c
);
611 _Pragma ("opt"); /* # pragma _CRI opt */
615 scm_lreadparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
)
622 c
= scm_flush_ws (port
, name
);
625 scm_ungetc (c
, port
);
626 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
628 ans
= scm_lreadr (tok_buf
, port
, copy
);
630 if (')' != (c
= scm_flush_ws (port
, name
)))
631 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
634 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
635 while (')' != (c
= scm_flush_ws (port
, name
)))
637 scm_ungetc (c
, port
);
638 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
640 SCM_SETCDR (tl
, scm_lreadr (tok_buf
, port
, copy
));
643 SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
651 scm_lreadrecparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
)
655 register SCM tl
, tl2
= SCM_EOL
;
656 SCM ans
, ans2
= SCM_EOL
;
657 /* Need to capture line and column numbers here. */
658 int line
= SCM_LINUM (port
);
659 int column
= SCM_COL (port
) - 1;
661 c
= scm_flush_ws (port
, name
);
664 scm_ungetc (c
, port
);
665 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
667 ans
= scm_lreadr (tok_buf
, port
, copy
);
668 if (')' != (c
= scm_flush_ws (port
, name
)))
669 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
672 /* Build the head of the list structure. */
673 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
674 if (SCM_COPY_SOURCE_P
)
675 ans2
= tl2
= scm_cons (SCM_CONSP (tmp
)
679 while (')' != (c
= scm_flush_ws (port
, name
)))
683 scm_ungetc (c
, port
);
684 if (SCM_EQ_P (scm_sym_dot
, (tmp
= scm_lreadr (tok_buf
, port
, copy
))))
686 SCM_SETCDR (tl
, tmp
= scm_lreadr (tok_buf
, port
, copy
));
687 if (SCM_COPY_SOURCE_P
)
688 SCM_SETCDR (tl2
, scm_cons (SCM_CONSP (tmp
)
692 if (')' != (c
= scm_flush_ws (port
, name
)))
693 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
697 new_tail
= scm_cons (tmp
, SCM_EOL
);
698 SCM_SETCDR (tl
, new_tail
);
701 if (SCM_COPY_SOURCE_P
)
703 SCM new_tail2
= scm_cons (SCM_CONSP (tmp
) ? *copy
: tmp
, SCM_EOL
);
704 SCM_SETCDR (tl2
, new_tail2
);
709 scm_whash_insert (scm_source_whash
,
711 scm_make_srcprops (line
,
724 /* Manipulate the read-hash-procedures alist. This could be written in
725 Scheme, but maybe it will also be used by C code during initialisation. */
726 SCM_DEFINE (scm_read_hash_extend
, "read-hash-extend", 2, 0, 0,
729 #define FUNC_NAME s_scm_read_hash_extend
734 SCM_VALIDATE_CHAR (1,chr
);
735 SCM_ASSERT (SCM_FALSEP (proc
) || SCM_NIMP(proc
), proc
, SCM_ARG2
,
738 /* Check if chr is already in the alist. */
739 this = *scm_read_hash_procedures
;
743 if (SCM_NULLP (this))
745 /* not found, so add it to the beginning. */
746 if (SCM_NFALSEP (proc
))
748 *scm_read_hash_procedures
=
749 scm_cons (scm_cons (chr
, proc
), *scm_read_hash_procedures
);
753 if (SCM_EQ_P (chr
, SCM_CAAR (this)))
755 /* already in the alist. */
756 if (SCM_FALSEP (proc
))
759 if (SCM_FALSEP (prev
))
761 *scm_read_hash_procedures
=
762 SCM_CDR (*scm_read_hash_procedures
);
765 scm_set_cdr_x (prev
, SCM_CDR (this));
770 scm_set_cdr_x (SCM_CAR (this), proc
);
775 this = SCM_CDR (this);
778 return SCM_UNSPECIFIED
;
782 /* Recover the read-hash procedure corresponding to char c. */
784 scm_get_hash_procedure (int c
)
786 SCM rest
= *scm_read_hash_procedures
;
790 if (SCM_NULLP (rest
))
793 if (SCM_CHAR (SCM_CAAR (rest
)) == c
)
794 return SCM_CDAR (rest
);
796 rest
= SCM_CDR (rest
);
803 scm_read_hash_procedures
=
804 SCM_CDRLOC (scm_sysintern ("read-hash-procedures", SCM_EOL
));
806 scm_init_opts (scm_read_options
, scm_read_opts
, SCM_N_READ_OPTIONS
);
807 #ifndef SCM_MAGIC_SNARFER
808 #include "libguile/read.x"