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"}
70 #ifdef HAVE_RL_GETC_FUNCTION
72 { SCM_OPTION_BOOLEAN
, "history-file", 1,
73 "Use history file." },
74 { SCM_OPTION_INTEGER
, "history-length", 200,
76 #endif /* HAVE_RL_GETC_FUNCTION */
79 #ifdef HAVE_RL_GETC_FUNCTION
80 extern void stifle_history (int max
);
83 SCM_PROC (s_read_options
, "read-options-interface", 0, 1, 0, scm_read_options
);
86 scm_read_options (setting
)
89 SCM ans
= scm_options (setting
,
93 if (SCM_COPY_SOURCE_P
)
94 SCM_RECORD_POSITIONS_P
= 1;
95 #ifdef HAVE_RL_GETC_FUNCTION
96 stifle_history (SCM_HISTORY_LENGTH
);
101 /* An association list mapping extra hash characters to procedures. */
102 static SCM
*scm_read_hash_procedures
;
104 SCM_PROC (s_read
, "read", 0, 1, 0, scm_read
);
113 if (SCM_UNBNDP (port
))
116 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
),
121 c
= scm_flush_ws (port
, (char *) NULL
);
124 scm_ungetc (c
, port
);
126 tok_buf
= scm_makstr (30L, 0);
127 return scm_lreadr (&tok_buf
, port
, ©
);
133 scm_grow_tok_buf (tok_buf
)
136 scm_vector_set_length_x (*tok_buf
, SCM_MAKINUM (2 * SCM_LENGTH (*tok_buf
)));
137 return SCM_CHARS (*tok_buf
);
143 scm_flush_ws (port
, eoferr
)
149 switch (c
= scm_getc (port
))
154 scm_wta (SCM_UNDEFINED
, "end of file in ", eoferr
);
158 switch (c
= scm_getc (port
))
164 case SCM_LINE_INCREMENTORS
:
168 case SCM_LINE_INCREMENTORS
:
169 case SCM_SINGLE_SPACES
:
180 scm_casei_streq (s1
, s2
)
185 if (scm_downcase((int)*s1
) != scm_downcase((int)*s2
))
192 return !(*s1
|| *s2
);
196 /* recsexpr is used when recording expressions
197 * constructed by read:sharp.
199 #ifndef DEBUG_EXTENSIONS
200 #define recsexpr(obj, line, column, filename) (obj)
202 static SCM recsexpr
SCM_P ((SCM obj
, int line
, int column
, SCM filename
));
205 recsexpr (obj
, line
, column
, filename
)
211 if (SCM_IMP (obj
) || SCM_NCONSP(obj
))
215 /* If this sexpr is visible in the read:sharp source, we want to
216 keep that information, so only record non-constant cons cells
217 which haven't previously been read by the reader. */
218 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash
, obj
)))
220 if (SCM_COPY_SOURCE_P
)
222 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
224 while (SCM_NIMP (tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
226 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
231 copy
= SCM_CDR (copy
);
233 SCM_SETCDR (copy
, tmp
);
237 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
238 while (SCM_NIMP (tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
239 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
240 copy
= SCM_UNDEFINED
;
242 scm_whash_insert (scm_source_whash
,
244 scm_make_srcprops (line
,
255 /* Consume an SCSH-style block comment. Assume that we've already
256 read the initial `#!', and eat characters until we get a
257 newline/exclamation-point/sharp-sign/newline sequence. */
260 skip_scsh_block_comment (port
)
263 /* Is this portable? Dear God, spare me from the non-eight-bit
264 characters. But is it tasteful? */
269 int c
= scm_getc (port
);
272 scm_wta (SCM_UNDEFINED
,
273 "unterminated `#! ... !#' comment", "read");
274 history
= ((history
<< 8) | (c
& 0xff)) & 0xffffffff;
276 /* Were the last four characters read "\n!#\n"? */
277 if (history
== (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
283 scm_get_hash_procedure
SCM_P ((int c
));
285 static char s_list
[]="list";
288 scm_lreadr (tok_buf
, port
, copy
)
298 c
= scm_flush_ws (port
, s_read
);
299 tryagain_no_flush_ws
:
306 return SCM_RECORD_POSITIONS_P
307 ? scm_lreadrecparen (tok_buf
, port
, s_list
, copy
)
308 : scm_lreadparen (tok_buf
, port
, s_list
, copy
);
310 scm_wta (SCM_UNDEFINED
, "unexpected \")\"", "read");
317 p
= scm_i_quasiquote
;
322 p
= scm_i_uq_splicing
;
325 scm_ungetc (c
, port
);
330 scm_lreadr (tok_buf
, port
, copy
),
332 if (SCM_RECORD_POSITIONS_P
)
333 scm_whash_insert (scm_source_whash
,
335 scm_make_srcprops (SCM_LINUM (port
),
339 ? (*copy
= scm_cons2 (SCM_CAR (p
),
340 SCM_CAR (SCM_CDR (p
)),
350 p
= scm_lreadparen (tok_buf
, port
, "vector", copy
);
351 return SCM_NULLP (p
) ? scm_nullvect
: scm_vector (p
);
372 scm_ungetc (c
, port
);
377 /* start of a shell script. Parse as a block comment,
378 terminated by !#, just like SCSH. */
379 skip_scsh_block_comment (port
);
380 /* EOF is not an error here */
381 c
= scm_flush_ws (port
, (char *)NULL
);
382 goto tryagain_no_flush_ws
;
385 j
= scm_read_token (c
, tok_buf
, port
, 0);
386 p
= scm_istr2bve (SCM_CHARS (*tok_buf
) + 1, (long) (j
- 1));
393 j
= scm_read_token (c
, tok_buf
, port
, 1);
394 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
399 j
= scm_read_token (c
, tok_buf
, port
, 0);
401 return SCM_MAKICHR (c
);
402 if (c
>= '0' && c
< '8')
404 p
= scm_istr2int (SCM_CHARS (*tok_buf
), (long) j
, 8);
406 return SCM_MAKICHR (SCM_INUM (p
));
408 for (c
= 0; c
< scm_n_charnames
; c
++)
410 && (scm_casei_streq (scm_charnames
[c
], SCM_CHARS (*tok_buf
))))
411 return SCM_MAKICHR (scm_charnums
[c
]);
412 scm_wta (SCM_UNDEFINED
, "unknown # object: #\\", SCM_CHARS (*tok_buf
));
414 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
416 j
= scm_read_token ('-', tok_buf
, port
, 0);
417 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
418 return scm_make_keyword_from_dash_symbol (SCM_CAR (p
));
423 SCM sharp
= scm_get_hash_procedure (c
);
425 if (SCM_NIMP (sharp
))
427 int line
= SCM_LINUM (port
);
428 int column
= SCM_COL (port
) - 2;
431 got
= scm_apply (sharp
,
433 scm_acons (port
, SCM_EOL
, SCM_EOL
));
434 if (SCM_UNSPECIFIED
== got
)
436 if (SCM_RECORD_POSITIONS_P
)
437 return *copy
= recsexpr (got
, line
, column
,
438 SCM_FILENAME (port
));
444 scm_misc_error (s_read
, "Unknown # object: %S",
445 scm_listify (SCM_MAKICHR (c
), SCM_UNDEFINED
));
450 while ('"' != (c
= scm_getc (port
)))
452 SCM_ASSERT (EOF
!= c
, SCM_UNDEFINED
, "end of file in ", "string");
454 while (j
+ 2 >= SCM_LENGTH (*tok_buf
))
455 scm_grow_tok_buf (tok_buf
);
458 switch (c
= scm_getc (port
))
484 SCM_CHARS (*tok_buf
)[j
] = c
;
489 SCM_CHARS (*tok_buf
)[j
] = 0;
492 str
= scm_makfromstr (SCM_CHARS (*tok_buf
), j
, 0);
496 case'0':case '1':case '2':case '3':case '4':
497 case '5':case '6':case '7':case '8':case '9':
502 j
= scm_read_token (c
, tok_buf
, port
, 0);
503 p
= scm_istring2number (SCM_CHARS (*tok_buf
), (long) j
, 10L);
508 if ((j
== 2) && (scm_getc (port
) == '('))
510 scm_ungetc ('(', port
);
511 c
= SCM_CHARS (*tok_buf
)[1];
514 scm_wta (SCM_UNDEFINED
, "unknown # object", SCM_CHARS (*tok_buf
));
519 if (SCM_KEYWORD_STYLE
== scm_keyword_prefix
)
521 j
= scm_read_token ('-', tok_buf
, port
, 0);
522 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
523 return scm_make_keyword_from_dash_symbol (SCM_CAR (p
));
527 j
= scm_read_token (c
, tok_buf
, port
, 0);
531 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
537 _Pragma ("noopt"); /* # pragma _CRI noopt */
541 scm_read_token (ic
, tok_buf
, port
, weird
)
547 register scm_sizet j
;
551 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(ic
) : ic
);
552 p
= SCM_CHARS (*tok_buf
);
559 while (j
+ 2 >= SCM_LENGTH (*tok_buf
))
560 p
= scm_grow_tok_buf (tok_buf
);
567 while (j
+ 2 >= SCM_LENGTH (*tok_buf
))
568 p
= scm_grow_tok_buf (tok_buf
);
576 case SCM_WHITE_SPACES
:
577 case SCM_LINE_INCREMENTORS
:
581 scm_ungetc (c
, port
);
609 scm_ungetc (c
, port
);
617 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(c
) : c
);
627 _Pragma ("opt"); /* # pragma _CRI opt */
631 scm_lreadparen (tok_buf
, port
, name
, copy
)
642 c
= scm_flush_ws (port
, name
);
645 scm_ungetc (c
, port
);
646 if (scm_i_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
648 ans
= scm_lreadr (tok_buf
, port
, copy
);
650 if (')' != (c
= scm_flush_ws (port
, name
)))
651 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
654 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
655 while (')' != (c
= scm_flush_ws (port
, name
)))
657 scm_ungetc (c
, port
);
658 if (scm_i_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
660 SCM_SETCDR (tl
, scm_lreadr (tok_buf
, port
, copy
));
663 SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
671 scm_lreadrecparen (tok_buf
, port
, name
, copy
)
679 register SCM tl
, tl2
= SCM_EOL
;
680 SCM ans
, ans2
= SCM_EOL
;
681 /* Need to capture line and column numbers here. */
682 int line
= SCM_LINUM (port
);
683 int column
= SCM_COL (port
) - 1;
685 c
= scm_flush_ws (port
, name
);
688 scm_ungetc (c
, port
);
689 if (scm_i_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
691 ans
= scm_lreadr (tok_buf
, port
, copy
);
692 if (')' != (c
= scm_flush_ws (port
, name
)))
693 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
696 /* Build the head of the list structure. */
697 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
698 if (SCM_COPY_SOURCE_P
)
699 ans2
= tl2
= scm_cons (SCM_NIMP (tmp
) && SCM_CONSP (tmp
)
703 while (')' != (c
= scm_flush_ws (port
, name
)))
705 scm_ungetc (c
, port
);
706 if (scm_i_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
708 SCM_SETCDR (tl
, tmp
= scm_lreadr (tok_buf
, port
, copy
));
709 if (SCM_COPY_SOURCE_P
)
710 SCM_SETCDR (tl2
, scm_cons (SCM_NIMP (tmp
) && SCM_CONSP (tmp
)
714 if (')' != (c
= scm_flush_ws (port
, name
)))
715 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
718 tl
= SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
719 if (SCM_COPY_SOURCE_P
)
720 tl2
= SCM_SETCDR (tl2
, scm_cons (SCM_NIMP (tmp
) && SCM_CONSP (tmp
)
726 scm_whash_insert (scm_source_whash
,
728 scm_make_srcprops (line
,
741 /* Manipulate the read-hash-procedures alist. This could be written in
742 Scheme, but maybe it will also be used by C code during initialisation. */
743 SCM_PROC (s_read_hash_extend
, "read-hash-extend", 2, 0, 0, scm_read_hash_extend
);
745 scm_read_hash_extend (chr
, proc
)
752 SCM_ASSERT (SCM_ICHRP(chr
), chr
, SCM_ARG1
, s_read_hash_extend
);
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 (chr
== SCM_CAAR (this))
773 /* already in the alist. */
774 if (SCM_FALSEP (proc
))
777 if (prev
== SCM_BOOL_F
)
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
;
799 /* Recover the read-hash procedure corresponding to char c. */
801 scm_get_hash_procedure (c
)
804 SCM rest
= *scm_read_hash_procedures
;
808 if (SCM_NULLP (rest
))
811 if (SCM_ICHR (SCM_CAAR (rest
)) == c
)
812 return SCM_CDAR (rest
);
814 rest
= SCM_CDR (rest
);
821 scm_read_hash_procedures
=
822 SCM_CDRLOC (scm_sysintern ("read-hash-procedures", SCM_EOL
));
824 scm_init_opts (scm_read_options
, scm_read_opts
, SCM_N_READ_OPTIONS
);