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 */
59 #include "scm_validate.h"
64 SCM_SYMBOL (scm_keyword_prefix
, "prefix");
66 scm_option scm_read_opts
[] = {
67 { SCM_OPTION_BOOLEAN
, "copy", 0,
68 "Copy source code expressions." },
69 { SCM_OPTION_BOOLEAN
, "positions", 0,
70 "Record positions of source code expressions." },
71 { SCM_OPTION_BOOLEAN
, "case-insensitive", 0,
72 "Convert symbols to lower case."},
73 { SCM_OPTION_SCM
, "keywords", SCM_BOOL_F
,
74 "Style of keyword recognition: #f or 'prefix"}
77 SCM_DEFINE (scm_read_options
, "read-options-interface", 0, 1, 0,
80 #define FUNC_NAME s_scm_read_options
82 SCM ans
= scm_options (setting
,
86 if (SCM_COPY_SOURCE_P
)
87 SCM_RECORD_POSITIONS_P
= 1;
92 /* An association list mapping extra hash characters to procedures. */
93 static SCM
*scm_read_hash_procedures
;
95 SCM_DEFINE (scm_read
, "read", 0, 1, 0,
98 #define FUNC_NAME s_scm_read
103 if (SCM_UNBNDP (port
))
105 SCM_VALIDATE_OPINPORT (1,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
, ©
);
120 scm_grow_tok_buf (SCM
*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 (SCM port
, const char *eoferr
)
133 switch (c
= scm_getc (port
))
138 scm_wta (SCM_UNDEFINED
, "end of file in ", eoferr
);
142 switch (c
= scm_getc (port
))
148 case SCM_LINE_INCREMENTORS
:
152 case SCM_LINE_INCREMENTORS
:
153 case SCM_SINGLE_SPACES
:
164 scm_casei_streq (char *s1
, char *s2
)
167 if (scm_downcase((int)*s1
) != scm_downcase((int)*s2
))
174 return !(*s1
|| *s2
);
178 /* recsexpr is used when recording expressions
179 * constructed by read:sharp.
181 #ifndef DEBUG_EXTENSIONS
182 #define recsexpr(obj, line, column, filename) (obj)
185 recsexpr (SCM obj
,int line
,int column
,SCM filename
)
187 if (SCM_IMP (obj
) || SCM_NCONSP(obj
))
191 /* If this sexpr is visible in the read:sharp source, we want to
192 keep that information, so only record non-constant cons cells
193 which haven't previously been read by the reader. */
194 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash
, obj
)))
196 if (SCM_COPY_SOURCE_P
)
198 copy
= scm_cons (recsexpr (SCM_CAR (obj
), line
, column
, filename
),
200 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
202 SCM_SETCDR (copy
, scm_cons (recsexpr (SCM_CAR (tmp
),
207 copy
= SCM_CDR (copy
);
209 SCM_SETCDR (copy
, tmp
);
213 recsexpr (SCM_CAR (obj
), line
, column
, filename
);
214 while ((tmp
= SCM_CDR (tmp
)) && SCM_CONSP (tmp
))
215 recsexpr (SCM_CAR (tmp
), line
, column
, filename
);
216 copy
= SCM_UNDEFINED
;
218 scm_whash_insert (scm_source_whash
,
220 scm_make_srcprops (line
,
231 /* Consume an SCSH-style block comment. Assume that we've already
232 read the initial `#!', and eat characters until we get a
233 newline/exclamation-point/sharp-sign/newline sequence. */
236 skip_scsh_block_comment (SCM port
)
238 /* Is this portable? Dear God, spare me from the non-eight-bit
239 characters. But is it tasteful? */
244 int c
= scm_getc (port
);
247 scm_wta (SCM_UNDEFINED
,
248 "unterminated `#! ... !#' comment", "read");
249 history
= ((history
<< 8) | (c
& 0xff)) & 0xffffffff;
251 /* Were the last four characters read "\n!#\n"? */
252 if (history
== (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
257 static SCM
scm_get_hash_procedure(int c
);
259 static char s_list
[]="list";
262 scm_lreadr (SCM
*tok_buf
,SCM port
,SCM
*copy
)
269 c
= scm_flush_ws (port
, s_scm_read
);
270 tryagain_no_flush_ws
:
277 return SCM_RECORD_POSITIONS_P
278 ? scm_lreadrecparen (tok_buf
, port
, s_list
, copy
)
279 : scm_lreadparen (tok_buf
, port
, s_list
, copy
);
281 scm_wta (SCM_UNDEFINED
, "unexpected \")\"", "read");
288 p
= scm_sym_quasiquote
;
293 p
= scm_sym_uq_splicing
;
296 scm_ungetc (c
, port
);
301 scm_lreadr (tok_buf
, port
, copy
),
303 if (SCM_RECORD_POSITIONS_P
)
304 scm_whash_insert (scm_source_whash
,
306 scm_make_srcprops (SCM_LINUM (port
),
310 ? (*copy
= scm_cons2 (SCM_CAR (p
),
311 SCM_CAR (SCM_CDR (p
)),
321 p
= scm_lreadparen (tok_buf
, port
, "vector", copy
);
322 return SCM_NULLP (p
) ? scm_nullvect
: scm_vector (p
);
343 scm_ungetc (c
, port
);
348 /* start of a shell script. Parse as a block comment,
349 terminated by !#, just like SCSH. */
350 skip_scsh_block_comment (port
);
351 /* EOF is not an error here */
352 c
= scm_flush_ws (port
, (char *)NULL
);
353 goto tryagain_no_flush_ws
;
357 j
= scm_read_token (c
, tok_buf
, port
, 0);
358 p
= scm_istr2bve (SCM_CHARS (*tok_buf
) + 1, (long) (j
- 1));
366 j
= scm_read_token (c
, tok_buf
, port
, 1);
367 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
372 j
= scm_read_token (c
, tok_buf
, port
, 0);
374 return SCM_MAKICHR (c
);
375 if (c
>= '0' && c
< '8')
377 p
= scm_istr2int (SCM_CHARS (*tok_buf
), (long) j
, 8);
379 return SCM_MAKICHR (SCM_INUM (p
));
381 for (c
= 0; c
< scm_n_charnames
; c
++)
383 && (scm_casei_streq (scm_charnames
[c
], SCM_CHARS (*tok_buf
))))
384 return SCM_MAKICHR (scm_charnums
[c
]);
385 scm_wta (SCM_UNDEFINED
, "unknown # object: #\\", SCM_CHARS (*tok_buf
));
387 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
389 j
= scm_read_token ('-', tok_buf
, port
, 0);
390 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
391 return scm_make_keyword_from_dash_symbol (SCM_CAR (p
));
396 SCM sharp
= scm_get_hash_procedure (c
);
398 if (SCM_NIMP (sharp
))
400 int line
= SCM_LINUM (port
);
401 int column
= SCM_COL (port
) - 2;
404 got
= scm_apply (sharp
,
406 scm_acons (port
, SCM_EOL
, SCM_EOL
));
407 if (SCM_UNSPECIFIED
== got
)
409 if (SCM_RECORD_POSITIONS_P
)
410 return *copy
= recsexpr (got
, line
, column
,
411 SCM_FILENAME (port
));
417 scm_misc_error (s_scm_read
, "Unknown # object: ~S",
418 scm_listify (SCM_MAKICHR (c
), SCM_UNDEFINED
));
423 while ('"' != (c
= scm_getc (port
)))
425 SCM_ASSERT (EOF
!= c
, SCM_UNDEFINED
, "end of file in ", "string");
427 while (j
+ 2 >= SCM_LENGTH (*tok_buf
))
428 scm_grow_tok_buf (tok_buf
);
431 switch (c
= scm_getc (port
))
457 SCM_CHARS (*tok_buf
)[j
] = c
;
462 SCM_CHARS (*tok_buf
)[j
] = 0;
465 str
= scm_makfromstr (SCM_CHARS (*tok_buf
), j
, 0);
469 case'0':case '1':case '2':case '3':case '4':
470 case '5':case '6':case '7':case '8':case '9':
475 j
= scm_read_token (c
, tok_buf
, port
, 0);
476 p
= scm_istring2number (SCM_CHARS (*tok_buf
), (long) j
, 10L);
481 if ((j
== 2) && (scm_getc (port
) == '('))
483 scm_ungetc ('(', port
);
484 c
= SCM_CHARS (*tok_buf
)[1];
487 scm_wta (SCM_UNDEFINED
, "unknown # object", SCM_CHARS (*tok_buf
));
492 if (SCM_KEYWORD_STYLE
== scm_keyword_prefix
)
494 j
= scm_read_token ('-', tok_buf
, port
, 0);
495 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
496 return scm_make_keyword_from_dash_symbol (SCM_CAR (p
));
500 j
= scm_read_token (c
, tok_buf
, port
, 0);
504 p
= scm_intern (SCM_CHARS (*tok_buf
), j
);
510 _Pragma ("noopt"); /* # pragma _CRI noopt */
514 scm_read_token (int ic
, SCM
*tok_buf
, SCM port
, int weird
)
516 register scm_sizet j
;
520 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(ic
) : ic
);
521 p
= SCM_CHARS (*tok_buf
);
528 while (j
+ 2 >= SCM_LENGTH (*tok_buf
))
529 p
= scm_grow_tok_buf (tok_buf
);
536 while (j
+ 2 >= SCM_LENGTH (*tok_buf
))
537 p
= scm_grow_tok_buf (tok_buf
);
545 case SCM_WHITE_SPACES
:
546 case SCM_LINE_INCREMENTORS
:
550 scm_ungetc (c
, port
);
578 scm_ungetc (c
, port
);
586 c
= (SCM_CASE_INSENSITIVE_P
? scm_downcase(c
) : c
);
596 _Pragma ("opt"); /* # pragma _CRI opt */
600 scm_lreadparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
)
607 c
= scm_flush_ws (port
, name
);
610 scm_ungetc (c
, port
);
611 if (scm_sym_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
613 ans
= scm_lreadr (tok_buf
, port
, copy
);
615 if (')' != (c
= scm_flush_ws (port
, name
)))
616 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
619 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
620 while (')' != (c
= scm_flush_ws (port
, name
)))
622 scm_ungetc (c
, port
);
623 if (scm_sym_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
625 SCM_SETCDR (tl
, scm_lreadr (tok_buf
, port
, copy
));
628 SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
636 scm_lreadrecparen (SCM
*tok_buf
, SCM port
, char *name
, SCM
*copy
)
640 register SCM tl
, tl2
= SCM_EOL
;
641 SCM ans
, ans2
= SCM_EOL
;
642 /* Need to capture line and column numbers here. */
643 int line
= SCM_LINUM (port
);
644 int column
= SCM_COL (port
) - 1;
646 c
= scm_flush_ws (port
, name
);
649 scm_ungetc (c
, port
);
650 if (scm_sym_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
652 ans
= scm_lreadr (tok_buf
, port
, copy
);
653 if (')' != (c
= scm_flush_ws (port
, name
)))
654 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
657 /* Build the head of the list structure. */
658 ans
= tl
= scm_cons (tmp
, SCM_EOL
);
659 if (SCM_COPY_SOURCE_P
)
660 ans2
= tl2
= scm_cons (SCM_CONSP (tmp
)
664 while (')' != (c
= scm_flush_ws (port
, name
)))
666 scm_ungetc (c
, port
);
667 if (scm_sym_dot
== (tmp
= scm_lreadr (tok_buf
, port
, copy
)))
669 SCM_SETCDR (tl
, tmp
= scm_lreadr (tok_buf
, port
, copy
));
670 if (SCM_COPY_SOURCE_P
)
671 SCM_SETCDR (tl2
, scm_cons (SCM_CONSP (tmp
)
675 if (')' != (c
= scm_flush_ws (port
, name
)))
676 scm_wta (SCM_UNDEFINED
, "missing close paren", "");
679 tl
= SCM_SETCDR (tl
, scm_cons (tmp
, SCM_EOL
));
680 if (SCM_COPY_SOURCE_P
)
681 tl2
= SCM_SETCDR (tl2
, scm_cons (SCM_CONSP (tmp
)
687 scm_whash_insert (scm_source_whash
,
689 scm_make_srcprops (line
,
702 /* Manipulate the read-hash-procedures alist. This could be written in
703 Scheme, but maybe it will also be used by C code during initialisation. */
704 SCM_DEFINE (scm_read_hash_extend
, "read-hash-extend", 2, 0, 0,
707 #define FUNC_NAME s_scm_read_hash_extend
712 SCM_VALIDATE_ICHR (1,chr
);
713 SCM_ASSERT (SCM_FALSEP (proc
) || SCM_NIMP(proc
), proc
, SCM_ARG2
,
716 /* Check if chr is already in the alist. */
717 this = *scm_read_hash_procedures
;
721 if (SCM_NULLP (this))
723 /* not found, so add it to the beginning. */
724 if (SCM_NFALSEP (proc
))
726 *scm_read_hash_procedures
=
727 scm_cons (scm_cons (chr
, proc
), *scm_read_hash_procedures
);
731 if (chr
== SCM_CAAR (this))
733 /* already in the alist. */
734 if (SCM_FALSEP (proc
))
737 if (prev
== SCM_BOOL_F
)
739 *scm_read_hash_procedures
=
740 SCM_CDR (*scm_read_hash_procedures
);
743 scm_set_cdr_x (prev
, SCM_CDR (this));
748 scm_set_cdr_x (SCM_CAR (this), proc
);
753 this = SCM_CDR (this);
756 return SCM_UNSPECIFIED
;
760 /* Recover the read-hash procedure corresponding to char c. */
762 scm_get_hash_procedure (int c
)
764 SCM rest
= *scm_read_hash_procedures
;
768 if (SCM_NULLP (rest
))
771 if (SCM_ICHR (SCM_CAAR (rest
)) == c
)
772 return SCM_CDAR (rest
);
774 rest
= SCM_CDR (rest
);
781 scm_read_hash_procedures
=
782 SCM_CDRLOC (scm_sysintern ("read-hash-procedures", SCM_EOL
));
784 scm_init_opts (scm_read_options
, scm_read_opts
, SCM_N_READ_OPTIONS
);