fcc211b7d35981bd65f47677227084f7483f5987
1 /* Copyright (C) 1995,1996,1997 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. */
59 SCM_SYMBOL (scm_keyword_prefix
, "prefix");
61 scm_option scm_read_opts
[] = {
62 { SCM_OPTION_BOOLEAN
, "copy", 0,
63 "Copy source code expressions." },
64 { SCM_OPTION_BOOLEAN
, "positions", 0,
65 "Record positions of source code expressions." },
66 { SCM_OPTION_BOOLEAN
, "case-insensitive", 0,
67 "Convert symbols to lower case."},
68 { SCM_OPTION_SCM
, "keywords", SCM_BOOL_F
,
69 "Style of keyword recognition: #f or 'prefix"}
72 SCM_PROC (s_read_options
, "read-options-interface", 0, 1, 0, scm_read_options
);
75 scm_read_options (setting
)
78 SCM ans
= scm_options (setting
,
82 if (SCM_COPY_SOURCE_P
)
83 SCM_RECORD_POSITIONS_P
= 1;
87 /* An association list mapping extra hash characters to procedures. */
88 static SCM
*scm_read_hash_procedures
;
90 SCM_PROC (s_read
, "read", 0, 1, 0, scm_read
);
99 if (SCM_UNBNDP (port
))
102 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
),
107 c
= scm_flush_ws (port
, (char *) NULL
);
110 scm_ungetc (c
, port
);
112 tok_buf
= scm_makstr (30L, 0);
113 return scm_lreadr (&tok_buf
, port
, ©
);
119 scm_grow_tok_buf (tok_buf
)
122 scm_vector_set_length_x (*tok_buf
, SCM_MAKINUM (2 * SCM_LENGTH (*tok_buf
)));
123 return SCM_CHARS (*tok_buf
);
129 scm_flush_ws (port
, eoferr
)
135 switch (c
= scm_getc (port
))
140 scm_wta (SCM_UNDEFINED
, "end of file in ", eoferr
);
144 switch (c
= scm_getc (port
))
150 case SCM_LINE_INCREMENTORS
:
154 case SCM_LINE_INCREMENTORS
:
155 case SCM_SINGLE_SPACES
:
166 scm_casei_streq (s1
, s2
)
171 if (scm_downcase((int)*s1
) != scm_downcase((int)*s2
))
178 return !(*s1
|| *s2
);
182 /* recsexpr is used when recording expressions
183 * constructed by read:sharp.
185 #ifndef DEBUG_EXTENSIONS
186 #define recsexpr(obj, line, column, filename) (obj)
188 static SCM recsexpr
SCM_P ((SCM obj
, int line
, int column
, SCM filename
));
191 recsexpr (obj
, line
, column
, filename
)
197 if (SCM_IMP (obj
) || SCM_NCONSP(obj
))
201 /* If this sexpr is visible in the read:sharp source, we want to
202 keep that information, so only record non-constant cons cells
203 which haven't previously been read by the reader. */
204 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash
, obj
)))
206 if (SCM_COPY_SOURCE_P
)
208 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
210 while (SCM_NIMP (tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
212 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
217 copy
= SCM_CDR (copy
);
219 SCM_SETCDR (copy
, tmp
);
223 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
224 while (SCM_NIMP (tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
225 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
226 copy
= SCM_UNDEFINED
;
228 scm_whash_insert (scm_source_whash
,
230 scm_make_srcprops (line
,
241 /* Consume an SCSH-style block comment. Assume that we've already
242 read the initial `#!', and eat characters until we get a
243 newline/exclamation-point/sharp-sign/newline sequence. */
246 skip_scsh_block_comment (port
)
249 /* Is this portable? Dear God, spare me from the non-eight-bit
250 characters. But is it tasteful? */
255 int c
= scm_getc (port
);
258 scm_wta (SCM_UNDEFINED
,
259 "unterminated `#! ... !#' comment", "read");
260 history
= ((history
<< 8) | (c
& 0xff)) & 0xffffffff;
262 /* Were the last four characters read "\n!#\n"? */
263 if (history
== (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
269 scm_get_hash_procedure
SCM_P ((int c
));
271 static char s_list
[]="list";
274 scm_lreadr (tok_buf
, port
, copy
)
284 c
= scm_flush_ws (port
, s_read
);
285 tryagain_no_flush_ws
:
292 return SCM_RECORD_POSITIONS_P
293 ? scm_lreadrecparen (tok_buf
, port
, s_list
, copy
)
294 : scm_lreadparen (tok_buf
, port
, s_list
, copy
);
296 scm_wta (SCM_UNDEFINED
, "unexpected \")\"", "read");
303 p
= scm_i_quasiquote
;
308 p
= scm_i_uq_splicing
;
311 scm_ungetc (c
, port
);
316 scm_lreadr (tok_buf
, port
, copy
),
318 if (SCM_RECORD_POSITIONS_P
)
319 scm_whash_insert (scm_source_whash
,
321 scm_make_srcprops (SCM_LINUM (port
),
325 ? (*copy
= scm_cons2 (SCM_CAR (p
),
326 SCM_CAR (SCM_CDR (p
)),
336 p
= scm_lreadparen (tok_buf
, port
, "vector", copy
);
337 return SCM_NULLP (p
) ? scm_nullvect
: scm_vector (p
);
358 scm_ungetc (c
, port
);
363 /* start of a shell script. Parse as a block comment,
364 terminated by !#, just like SCSH. */
365 skip_scsh_block_comment (port
);
366 /* EOF is not an error here */
367 c
= scm_flush_ws (port
, (char *)NULL
);
368 goto tryagain_no_flush_ws
;
371 j
= scm_read_token (c
, tok_buf
, port
, 0);
372 p
= scm_istr2bve (SCM_CHARS (*tok_buf
) + 1, (long) (j
- 1));
379 j
= scm_read_token (c
, tok_buf
, port
, 1);
380 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
385 j
= scm_read_token (c
, tok_buf
, port
, 0);
387 return SCM_MAKICHR (c
);
388 if (c
>= '0' && c
< '8')
390 p
= scm_istr2int (SCM_CHARS (*tok_buf
), (long) j
, 8);
392 return SCM_MAKICHR (SCM_INUM (p
));
394 for (c
= 0; c
< scm_n_charnames
; c
++)
396 && (scm_casei_streq (scm_charnames
[c
], SCM_CHARS (*tok_buf
))))
397 return SCM_MAKICHR (scm_charnums
[c
]);
398 scm_wta (SCM_UNDEFINED
, "unknown # object: #\\", SCM_CHARS (*tok_buf
));
400 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
402 j
= scm_read_token ('-', tok_buf
, port
, 0);
403 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
404 return scm_make_keyword_from_dash_symbol (SCM_CAR (p
));
409 SCM sharp
= scm_get_hash_procedure (c
);
411 if (SCM_NIMP (sharp
))
413 int line
= SCM_LINUM (port
);
414 int column
= SCM_COL (port
) - 2;
417 got
= scm_apply (sharp
,
419 scm_acons (port
, SCM_EOL
, SCM_EOL
));
420 if (SCM_UNSPECIFIED
== got
)
422 if (SCM_RECORD_POSITIONS_P
)
423 return *copy
= recsexpr (got
, line
, column
,
424 SCM_FILENAME (port
));
430 scm_misc_error (s_read
, "Unknown # object: %S",
431 scm_listify (SCM_MAKICHR (c
), SCM_UNDEFINED
));
436 while ('"' != (c
= scm_getc (port
)))
438 SCM_ASSERT (EOF
!= c
, SCM_UNDEFINED
, "end of file in ", "string");
440 while (j
+ 2 >= SCM_LENGTH (*tok_buf
))
441 scm_grow_tok_buf (tok_buf
);
444 switch (c
= scm_getc (port
))
470 SCM_CHARS (*tok_buf
)[j
] = c
;
475 SCM_CHARS (*tok_buf
)[j
] = 0;
478 str
= scm_makfromstr (SCM_CHARS (*tok_buf
), j
, 0);
482 case'0':case '1':case '2':case '3':case '4':
483 case '5':case '6':case '7':case '8':case '9':
488 j
= scm_read_token (c
, tok_buf
, port
, 0);
489 p
= scm_istring2number (SCM_CHARS (*tok_buf
), (long) j
, 10L);
494 if ((j
== 2) && (scm_getc (port
) == '('))
496 scm_ungetc ('(', port
);
497 c
= SCM_CHARS (*tok_buf
)[1];
500 scm_wta (SCM_UNDEFINED
, "unknown # object", SCM_CHARS (*tok_buf
));
505 if (SCM_KEYWORD_STYLE
== scm_keyword_prefix
)
507 j
= scm_read_token ('-', tok_buf
, port
, 0);
508 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
509 return scm_make_keyword_from_dash_symbol (SCM_CAR (p
));
513 j
= scm_read_token (c
, tok_buf
, port
, 0);
517 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
523 _Pragma ("noopt"); /* # pragma _CRI noopt */
527 scm_read_token (ic
, tok_buf
, port
, weird
)
533 register scm_sizet j
;
537 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(ic
) : ic
);
538 p
= SCM_CHARS (*tok_buf
);
545 while (j
+ 2 >= SCM_LENGTH (*tok_buf
))
546 p
= scm_grow_tok_buf (tok_buf
);
553 while (j
+ 2 >= SCM_LENGTH (*tok_buf
))
554 p
= scm_grow_tok_buf (tok_buf
);
562 case SCM_WHITE_SPACES
:
563 case SCM_LINE_INCREMENTORS
:
567 scm_ungetc (c
, port
);
595 scm_ungetc (c
, port
);
603 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(c
) : c
);
613 _Pragma ("opt"); /* # pragma _CRI opt */
617 scm_lreadparen (tok_buf
, port
, name
, copy
)
628 c
= scm_flush_ws (port
, name
);
631 scm_ungetc (c
, port
);
632 if (scm_i_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
634 ans
= scm_lreadr (tok_buf
, port
, copy
);
636 if (')' != (c
= scm_flush_ws (port
, name
)))
637 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
640 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
641 while (')' != (c
= scm_flush_ws (port
, name
)))
643 scm_ungetc (c
, port
);
644 if (scm_i_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
646 SCM_SETCDR (tl
, scm_lreadr (tok_buf
, port
, copy
));
649 SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
657 scm_lreadrecparen (tok_buf
, port
, name
, copy
)
665 register SCM tl
, tl2
= SCM_EOL
;
666 SCM ans
, ans2
= SCM_EOL
;
667 /* Need to capture line and column numbers here. */
668 int line
= SCM_LINUM (port
);
669 int column
= SCM_COL (port
) - 1;
671 c
= scm_flush_ws (port
, name
);
674 scm_ungetc (c
, port
);
675 if (scm_i_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
677 ans
= scm_lreadr (tok_buf
, port
, copy
);
678 if (')' != (c
= scm_flush_ws (port
, name
)))
679 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
682 /* Build the head of the list structure. */
683 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
684 if (SCM_COPY_SOURCE_P
)
685 ans2
= tl2
= scm_cons (SCM_NIMP (tmp
) && SCM_CONSP (tmp
)
689 while (')' != (c
= scm_flush_ws (port
, name
)))
691 scm_ungetc (c
, port
);
692 if (scm_i_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
694 SCM_SETCDR (tl
, tmp
= scm_lreadr (tok_buf
, port
, copy
));
695 if (SCM_COPY_SOURCE_P
)
696 SCM_SETCDR (tl2
, scm_cons (SCM_NIMP (tmp
) && SCM_CONSP (tmp
)
700 if (')' != (c
= scm_flush_ws (port
, name
)))
701 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
704 tl
= SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
705 if (SCM_COPY_SOURCE_P
)
706 tl2
= SCM_SETCDR (tl2
, scm_cons (SCM_NIMP (tmp
) && SCM_CONSP (tmp
)
712 scm_whash_insert (scm_source_whash
,
714 scm_make_srcprops (line
,
727 /* Manipulate the read-hash-procedures alist. This could be written in
728 Scheme, but maybe it will also be used by C code during initialisation. */
729 SCM_PROC (s_read_hash_extend
, "read-hash-extend", 2, 0, 0, scm_read_hash_extend
);
731 scm_read_hash_extend (chr
, proc
)
738 SCM_ASSERT (SCM_ICHRP(chr
), chr
, SCM_ARG1
, s_read_hash_extend
);
739 SCM_ASSERT (SCM_FALSEP (proc
) || SCM_NIMP(proc
), proc
, SCM_ARG2
,
742 /* Check if chr is already in the alist. */
743 this = *scm_read_hash_procedures
;
747 if (SCM_NULLP (this))
749 /* not found, so add it to the beginning. */
750 if (SCM_NFALSEP (proc
))
752 *scm_read_hash_procedures
=
753 scm_cons (scm_cons (chr
, proc
), *scm_read_hash_procedures
);
757 if (chr
== SCM_CAAR (this))
759 /* already in the alist. */
760 if (SCM_FALSEP (proc
))
763 if (prev
== SCM_BOOL_F
)
765 *scm_read_hash_procedures
=
766 SCM_CDR (*scm_read_hash_procedures
);
769 scm_set_cdr_x (prev
, SCM_CDR (this));
774 scm_set_cdr_x (SCM_CAR (this), proc
);
779 this = SCM_CDR (this);
782 return SCM_UNSPECIFIED
;
785 /* Recover the read-hash procedure corresponding to char c. */
787 scm_get_hash_procedure (c
)
790 SCM rest
= *scm_read_hash_procedures
;
794 if (SCM_NULLP (rest
))
797 if (SCM_ICHR (SCM_CAAR (rest
)) == c
)
798 return SCM_CDAR (rest
);
800 rest
= SCM_CDR (rest
);
807 scm_read_hash_procedures
=
808 SCM_CDRLOC (scm_sysintern ("read-hash-procedures", SCM_EOL
));
810 scm_init_opts (scm_read_options
, scm_read_opts
, SCM_N_READ_OPTIONS
);