1 /* Copyright (C) 1995,1996,1997, 1999 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 */
63 SCM_SYMBOL (scm_keyword_prefix
, "prefix");
65 scm_option scm_read_opts
[] = {
66 { SCM_OPTION_BOOLEAN
, "copy", 0,
67 "Copy source code expressions." },
68 { SCM_OPTION_BOOLEAN
, "positions", 0,
69 "Record positions of source code expressions." },
70 { SCM_OPTION_BOOLEAN
, "case-insensitive", 0,
71 "Convert symbols to lower case."},
72 { SCM_OPTION_SCM
, "keywords", SCM_UNPACK (SCM_BOOL_F
),
73 "Style of keyword recognition: #f or 'prefix"}
76 SCM_DEFINE (scm_read_options
, "read-options-interface", 0, 1, 0,
79 #define FUNC_NAME s_scm_read_options
81 SCM ans
= scm_options (setting
,
85 if (SCM_COPY_SOURCE_P
)
86 SCM_RECORD_POSITIONS_P
= 1;
91 /* An association list mapping extra hash characters to procedures. */
92 static SCM
*scm_read_hash_procedures
;
94 SCM_DEFINE (scm_read
, "read", 0, 1, 0,
97 #define FUNC_NAME s_scm_read
102 if (SCM_UNBNDP (port
))
104 SCM_VALIDATE_OPINPORT (1,port
);
106 c
= scm_flush_ws (port
, (char *) NULL
);
109 scm_ungetc (c
, port
);
111 tok_buf
= scm_makstr (30L, 0);
112 return scm_lreadr (&tok_buf
, port
, ©
);
119 scm_grow_tok_buf (SCM
*tok_buf
)
121 scm_vector_set_length_x (*tok_buf
, SCM_MAKINUM (2 * SCM_LENGTH (*tok_buf
)));
122 return SCM_CHARS (*tok_buf
);
128 scm_flush_ws (SCM port
, const char *eoferr
)
132 switch (c
= scm_getc (port
))
137 scm_wta (SCM_UNDEFINED
, "end of file in ", eoferr
);
141 switch (c
= scm_getc (port
))
147 case SCM_LINE_INCREMENTORS
:
151 case SCM_LINE_INCREMENTORS
:
152 case SCM_SINGLE_SPACES
:
163 scm_casei_streq (char *s1
, char *s2
)
166 if (scm_downcase((int)*s1
) != scm_downcase((int)*s2
))
173 return !(*s1
|| *s2
);
177 /* recsexpr is used when recording expressions
178 * constructed by read:sharp.
180 #ifndef DEBUG_EXTENSIONS
181 #define recsexpr(obj, line, column, filename) (obj)
184 recsexpr (SCM obj
,int line
,int column
,SCM filename
)
186 if (SCM_IMP (obj
) || SCM_NCONSP(obj
))
190 /* If this sexpr is visible in the read:sharp source, we want to
191 keep that information, so only record non-constant cons cells
192 which haven't previously been read by the reader. */
193 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash
, obj
)))
195 if (SCM_COPY_SOURCE_P
)
197 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
199 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
201 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
206 copy
= SCM_CDR (copy
);
208 SCM_SETCDR (copy
, tmp
);
212 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
213 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
214 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
215 copy
= SCM_UNDEFINED
;
217 scm_whash_insert (scm_source_whash
,
219 scm_make_srcprops (line
,
230 /* Consume an SCSH-style block comment. Assume that we've already
231 read the initial `#!', and eat characters until we get a
232 newline/exclamation-point/sharp-sign/newline sequence. */
235 skip_scsh_block_comment (SCM port
)
237 /* Is this portable? Dear God, spare me from the non-eight-bit
238 characters. But is it tasteful? */
243 int c
= scm_getc (port
);
246 scm_wta (SCM_UNDEFINED
,
247 "unterminated `#! ... !#' comment", "read");
248 history
= ((history
<< 8) | (c
& 0xff)) & 0xffffffff;
250 /* Were the last four characters read "\n!#\n"? */
251 if (history
== (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
256 static SCM
scm_get_hash_procedure(int c
);
258 static char s_list
[]="list";
261 scm_lreadr (SCM
*tok_buf
,SCM port
,SCM
*copy
)
268 c
= scm_flush_ws (port
, s_scm_read
);
269 tryagain_no_flush_ws
:
276 return SCM_RECORD_POSITIONS_P
277 ? scm_lreadrecparen (tok_buf
, port
, s_list
, copy
)
278 : scm_lreadparen (tok_buf
, port
, s_list
, copy
);
280 scm_wta (SCM_UNDEFINED
, "unexpected \")\"", "read");
287 p
= scm_sym_quasiquote
;
292 p
= scm_sym_uq_splicing
;
295 scm_ungetc (c
, port
);
300 scm_lreadr (tok_buf
, port
, copy
),
302 if (SCM_RECORD_POSITIONS_P
)
303 scm_whash_insert (scm_source_whash
,
305 scm_make_srcprops (SCM_LINUM (port
),
309 ? (*copy
= scm_cons2 (SCM_CAR (p
),
310 SCM_CAR (SCM_CDR (p
)),
320 p
= scm_lreadparen (tok_buf
, port
, "vector", copy
);
321 return SCM_NULLP (p
) ? scm_nullvect
: scm_vector (p
);
342 scm_ungetc (c
, port
);
347 /* start of a shell script. Parse as a block comment,
348 terminated by !#, just like SCSH. */
349 skip_scsh_block_comment (port
);
350 /* EOF is not an error here */
351 c
= scm_flush_ws (port
, (char *)NULL
);
352 goto tryagain_no_flush_ws
;
356 j
= scm_read_token (c
, tok_buf
, port
, 0);
357 p
= scm_istr2bve (SCM_CHARS (*tok_buf
) + 1, (long) (j
- 1));
365 j
= scm_read_token (c
, tok_buf
, port
, 1);
366 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
371 j
= scm_read_token (c
, tok_buf
, port
, 0);
373 return SCM_MAKE_CHAR (c
);
374 if (c
>= '0' && c
< '8')
376 p
= scm_istr2int (SCM_CHARS (*tok_buf
), (long) j
, 8);
378 return SCM_MAKE_CHAR (SCM_INUM (p
));
380 for (c
= 0; c
< scm_n_charnames
; c
++)
382 && (scm_casei_streq (scm_charnames
[c
], SCM_CHARS (*tok_buf
))))
383 return SCM_MAKE_CHAR (scm_charnums
[c
]);
384 scm_wta (SCM_UNDEFINED
, "unknown # object: #\\", SCM_CHARS (*tok_buf
));
386 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
388 j
= scm_read_token ('-', tok_buf
, port
, 0);
389 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
390 return scm_make_keyword_from_dash_symbol (SCM_CAR (p
));
395 SCM sharp
= scm_get_hash_procedure (c
);
397 if (SCM_NIMP (sharp
))
399 int line
= SCM_LINUM (port
);
400 int column
= SCM_COL (port
) - 2;
403 got
= scm_apply (sharp
,
405 scm_acons (port
, SCM_EOL
, SCM_EOL
));
406 if (SCM_UNSPECIFIED
== got
)
408 if (SCM_RECORD_POSITIONS_P
)
409 return *copy
= recsexpr (got
, line
, column
,
410 SCM_FILENAME (port
));
416 scm_misc_error (s_scm_read
, "Unknown # object: ~S",
417 scm_listify (SCM_MAKE_CHAR (c
), SCM_UNDEFINED
));
422 while ('"' != (c
= scm_getc (port
)))
424 SCM_ASSERT (EOF
!= c
, SCM_UNDEFINED
, "end of file in ", "string");
426 while (j
+ 2 >= SCM_LENGTH (*tok_buf
))
427 scm_grow_tok_buf (tok_buf
);
430 switch (c
= scm_getc (port
))
456 SCM_CHARS (*tok_buf
)[j
] = c
;
461 SCM_CHARS (*tok_buf
)[j
] = 0;
464 str
= scm_makfromstr (SCM_CHARS (*tok_buf
), j
, 0);
468 case'0':case '1':case '2':case '3':case '4':
469 case '5':case '6':case '7':case '8':case '9':
474 j
= scm_read_token (c
, tok_buf
, port
, 0);
475 p
= scm_istring2number (SCM_CHARS (*tok_buf
), (long) j
, 10L);
480 if ((j
== 2) && (scm_getc (port
) == '('))
482 scm_ungetc ('(', port
);
483 c
= SCM_CHARS (*tok_buf
)[1];
486 scm_wta (SCM_UNDEFINED
, "unknown # object", SCM_CHARS (*tok_buf
));
491 if (SCM_PACK (SCM_KEYWORD_STYLE
) == scm_keyword_prefix
)
493 j
= scm_read_token ('-', tok_buf
, port
, 0);
494 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
495 return scm_make_keyword_from_dash_symbol (SCM_CAR (p
));
499 j
= scm_read_token (c
, tok_buf
, port
, 0);
503 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
509 _Pragma ("noopt"); /* # pragma _CRI noopt */
513 scm_read_token (int ic
, SCM
*tok_buf
, SCM port
, int weird
)
515 register scm_sizet j
;
519 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(ic
) : ic
);
520 p
= SCM_CHARS (*tok_buf
);
527 while (j
+ 2 >= SCM_LENGTH (*tok_buf
))
528 p
= scm_grow_tok_buf (tok_buf
);
535 while (j
+ 2 >= SCM_LENGTH (*tok_buf
))
536 p
= scm_grow_tok_buf (tok_buf
);
544 case SCM_WHITE_SPACES
:
545 case SCM_LINE_INCREMENTORS
:
549 scm_ungetc (c
, port
);
577 scm_ungetc (c
, port
);
585 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(c
) : c
);
595 _Pragma ("opt"); /* # pragma _CRI opt */
599 scm_lreadparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
)
606 c
= scm_flush_ws (port
, name
);
609 scm_ungetc (c
, port
);
610 if (scm_sym_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
612 ans
= scm_lreadr (tok_buf
, port
, copy
);
614 if (')' != (c
= scm_flush_ws (port
, name
)))
615 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
618 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
619 while (')' != (c
= scm_flush_ws (port
, name
)))
621 scm_ungetc (c
, port
);
622 if (scm_sym_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
624 SCM_SETCDR (tl
, scm_lreadr (tok_buf
, port
, copy
));
627 SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
635 scm_lreadrecparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
)
639 register SCM tl
, tl2
= SCM_EOL
;
640 SCM ans
, ans2
= SCM_EOL
;
641 /* Need to capture line and column numbers here. */
642 int line
= SCM_LINUM (port
);
643 int column
= SCM_COL (port
) - 1;
645 c
= scm_flush_ws (port
, name
);
648 scm_ungetc (c
, port
);
649 if (scm_sym_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
651 ans
= scm_lreadr (tok_buf
, port
, copy
);
652 if (')' != (c
= scm_flush_ws (port
, name
)))
653 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
656 /* Build the head of the list structure. */
657 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
658 if (SCM_COPY_SOURCE_P
)
659 ans2
= tl2
= scm_cons (SCM_CONSP (tmp
)
663 while (')' != (c
= scm_flush_ws (port
, name
)))
665 scm_ungetc (c
, port
);
666 if (scm_sym_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
668 SCM_SETCDR (tl
, tmp
= scm_lreadr (tok_buf
, port
, copy
));
669 if (SCM_COPY_SOURCE_P
)
670 SCM_SETCDR (tl2
, scm_cons (SCM_CONSP (tmp
)
674 if (')' != (c
= scm_flush_ws (port
, name
)))
675 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
678 tl
= SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
679 if (SCM_COPY_SOURCE_P
)
680 tl2
= SCM_SETCDR (tl2
, scm_cons (SCM_CONSP (tmp
)
686 scm_whash_insert (scm_source_whash
,
688 scm_make_srcprops (line
,
701 /* Manipulate the read-hash-procedures alist. This could be written in
702 Scheme, but maybe it will also be used by C code during initialisation. */
703 SCM_DEFINE (scm_read_hash_extend
, "read-hash-extend", 2, 0, 0,
706 #define FUNC_NAME s_scm_read_hash_extend
711 SCM_VALIDATE_CHAR (1,chr
);
712 SCM_ASSERT (SCM_FALSEP (proc
) || SCM_NIMP(proc
), proc
, SCM_ARG2
,
715 /* Check if chr is already in the alist. */
716 this = *scm_read_hash_procedures
;
720 if (SCM_NULLP (this))
722 /* not found, so add it to the beginning. */
723 if (SCM_NFALSEP (proc
))
725 *scm_read_hash_procedures
=
726 scm_cons (scm_cons (chr
, proc
), *scm_read_hash_procedures
);
730 if (chr
== SCM_CAAR (this))
732 /* already in the alist. */
733 if (SCM_FALSEP (proc
))
736 if (prev
== SCM_BOOL_F
)
738 *scm_read_hash_procedures
=
739 SCM_CDR (*scm_read_hash_procedures
);
742 scm_set_cdr_x (prev
, SCM_CDR (this));
747 scm_set_cdr_x (SCM_CAR (this), proc
);
752 this = SCM_CDR (this);
755 return SCM_UNSPECIFIED
;
759 /* Recover the read-hash procedure corresponding to char c. */
761 scm_get_hash_procedure (int c
)
763 SCM rest
= *scm_read_hash_procedures
;
767 if (SCM_NULLP (rest
))
770 if (SCM_CHAR (SCM_CAAR (rest
)) == c
)
771 return SCM_CDAR (rest
);
773 rest
= SCM_CDR (rest
);
780 scm_read_hash_procedures
=
781 SCM_CDRLOC (scm_sysintern ("read-hash-procedures", SCM_EOL
));
783 scm_init_opts (scm_read_options
, scm_read_opts
, SCM_N_READ_OPTIONS
);