* Grammar fix.
[bpt/guile.git] / libguile / read.c
CommitLineData
be54b15d 1/* Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
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)
6 * any later version.
7 *
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.
12 *
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
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.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
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.
37 *
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd
JB
45\f
46
0f2d19dd 47#include <stdio.h>
a0599745
MD
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"
61
62#include "libguile/validate.h"
63#include "libguile/read.h"
0f2d19dd
JB
64
65\f
66
c7733771
GH
67SCM_SYMBOL (scm_keyword_prefix, "prefix");
68
92c2555f 69scm_t_option scm_read_opts[] = {
b7ff98dd
MD
70 { SCM_OPTION_BOOLEAN, "copy", 0,
71 "Copy source code expressions." },
ac74fc22 72 { SCM_OPTION_BOOLEAN, "positions", 0,
deca31e1
GH
73 "Record positions of source code expressions." },
74 { SCM_OPTION_BOOLEAN, "case-insensitive", 0,
c7733771 75 "Convert symbols to lower case."},
f1267706 76 { SCM_OPTION_SCM, "keywords", SCM_UNPACK (SCM_BOOL_F),
c7733771 77 "Style of keyword recognition: #f or 'prefix"}
a16f6fe7
MD
78};
79
a1ec6916 80SCM_DEFINE (scm_read_options, "read-options-interface", 0, 1, 0,
1bbd0b84 81 (SCM setting),
dc7fa443
MG
82 "Option interface for the read options. Instead of using\n"
83 "this procedure directly, use the procedures @code{read-enable},\n"
84 "@code{read-disable}, @code{read-set!} and @var{read-options}.")
1bbd0b84 85#define FUNC_NAME s_scm_read_options
a16f6fe7 86{
b7ff98dd
MD
87 SCM ans = scm_options (setting,
88 scm_read_opts,
89 SCM_N_READ_OPTIONS,
1bbd0b84 90 FUNC_NAME);
b7ff98dd
MD
91 if (SCM_COPY_SOURCE_P)
92 SCM_RECORD_POSITIONS_P = 1;
a16f6fe7
MD
93 return ans;
94}
1bbd0b84 95#undef FUNC_NAME
a16f6fe7 96
14de3b42
GH
97/* An association list mapping extra hash characters to procedures. */
98static SCM *scm_read_hash_procedures;
deca31e1 99
a1ec6916 100SCM_DEFINE (scm_read, "read", 0, 1, 0,
1bbd0b84 101 (SCM port),
dc7fa443
MG
102 "Read an s-expression from the input port @var{port}, or from\n"
103 "the current input port if @var{port} is not specified.\n"
104 "Any whitespace before the next token is discarded.")
1bbd0b84 105#define FUNC_NAME s_scm_read
0f2d19dd
JB
106{
107 int c;
09a4f039 108 SCM tok_buf, copy;
0f2d19dd
JB
109
110 if (SCM_UNBNDP (port))
111 port = scm_cur_inp;
3b3b36dd 112 SCM_VALIDATE_OPINPORT (1,port);
0f2d19dd 113
0f2d19dd
JB
114 c = scm_flush_ws (port, (char *) NULL);
115 if (EOF == c)
116 return SCM_EOF_VAL;
b7f3516f 117 scm_ungetc (c, port);
0f2d19dd 118
be54b15d 119 tok_buf = scm_allocate_string (30);
deca31e1 120 return scm_lreadr (&tok_buf, port, &copy);
0f2d19dd 121}
1bbd0b84 122#undef FUNC_NAME
0f2d19dd
JB
123
124
1cc91f1b 125
0f2d19dd 126char *
6e8d25a6 127scm_grow_tok_buf (SCM *tok_buf)
0f2d19dd 128{
1be6b49c 129 size_t oldlen = SCM_STRING_LENGTH (*tok_buf);
be54b15d 130 SCM newstr = scm_allocate_string (2 * oldlen);
1be6b49c 131 size_t i;
94115ae3
DH
132
133 for (i = 0; i != oldlen; ++i)
134 SCM_STRING_CHARS (newstr) [i] = SCM_STRING_CHARS (*tok_buf) [i];
135
136 *tok_buf = newstr;
137 return SCM_STRING_CHARS (newstr);
0f2d19dd
JB
138}
139
140
1cc91f1b 141
0f2d19dd 142int
6e8d25a6 143scm_flush_ws (SCM port, const char *eoferr)
0f2d19dd
JB
144{
145 register int c;
146 while (1)
b7f3516f 147 switch (c = scm_getc (port))
0f2d19dd
JB
148 {
149 case EOF:
150 goteof:
151 if (eoferr)
d156d3b7 152 {
d1ca2c64 153 if (!SCM_FALSEP (SCM_FILENAME (port)))
d156d3b7
MV
154 scm_misc_error (eoferr,
155 "end of file in ~A",
156 SCM_LIST1 (SCM_FILENAME (port)));
157 else
158 scm_misc_error (eoferr, "end of file", SCM_EOL);
159 }
0f2d19dd
JB
160 return c;
161 case ';':
162 lp:
b7f3516f 163 switch (c = scm_getc (port))
0f2d19dd
JB
164 {
165 case EOF:
166 goto goteof;
167 default:
168 goto lp;
169 case SCM_LINE_INCREMENTORS:
170 break;
171 }
172 break;
173 case SCM_LINE_INCREMENTORS:
0f2d19dd 174 case SCM_SINGLE_SPACES:
0f2d19dd 175 case '\t':
0f2d19dd
JB
176 break;
177 default:
178 return c;
179 }
180}
181
182
1cc91f1b 183
0f2d19dd 184int
6e8d25a6 185scm_casei_streq (char *s1, char *s2)
0f2d19dd
JB
186{
187 while (*s1 && *s2)
188 if (scm_downcase((int)*s1) != scm_downcase((int)*s2))
189 return 0;
190 else
191 {
192 ++s1;
193 ++s2;
194 }
195 return !(*s1 || *s2);
196}
197
198
09a4f039
MD
199/* recsexpr is used when recording expressions
200 * constructed by read:sharp.
201 */
604d4dd9
JB
202#ifndef DEBUG_EXTENSIONS
203#define recsexpr(obj, line, column, filename) (obj)
204#else
09a4f039 205static SCM
1be6b49c 206recsexpr (SCM obj, long line, int column, SCM filename)
09a4f039 207{
fee7ef83 208 if (!SCM_CONSP(obj)) {
09a4f039 209 return obj;
fee7ef83 210 } else {
09a4f039
MD
211 SCM tmp = obj, copy;
212 /* If this sexpr is visible in the read:sharp source, we want to
213 keep that information, so only record non-constant cons cells
214 which haven't previously been read by the reader. */
215 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash, obj)))
216 {
217 if (SCM_COPY_SOURCE_P)
218 {
219 copy = scm_cons (recsexpr (SCM_CAR (obj), line, column, filename),
220 SCM_UNDEFINED);
0c95b57d 221 while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
a6c64c3c
MD
222 {
223 SCM_SETCDR (copy, scm_cons (recsexpr (SCM_CAR (tmp),
224 line,
225 column,
226 filename),
227 SCM_UNDEFINED));
228 copy = SCM_CDR (copy);
229 }
230 SCM_SETCDR (copy, tmp);
09a4f039
MD
231 }
232 else
233 {
234 recsexpr (SCM_CAR (obj), line, column, filename);
0c95b57d 235 while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
09a4f039
MD
236 recsexpr (SCM_CAR (tmp), line, column, filename);
237 copy = SCM_UNDEFINED;
238 }
239 scm_whash_insert (scm_source_whash,
240 obj,
241 scm_make_srcprops (line,
242 column,
243 filename,
244 copy,
245 SCM_EOL));
246 }
247 return obj;
248 }
249}
604d4dd9 250#endif
f9c68a47
JB
251
252/* Consume an SCSH-style block comment. Assume that we've already
f9731264
JB
253 read the initial `#!', and eat characters until we get a
254 newline/exclamation-point/sharp-sign/newline sequence. */
f9c68a47
JB
255
256static void
6e8d25a6 257skip_scsh_block_comment (SCM port)
db4b4ca6 258#define FUNC_NAME "skip_scsh_block_comment"
f9c68a47 259{
f9731264
JB
260 /* Is this portable? Dear God, spare me from the non-eight-bit
261 characters. But is it tasteful? */
262 long history = 0;
f9c68a47
JB
263
264 for (;;)
265 {
b7f3516f 266 int c = scm_getc (port);
f9c68a47
JB
267
268 if (c == EOF)
db4b4ca6 269 SCM_MISC_ERROR ("unterminated `#! ... !#' comment", SCM_EOL);
f9731264 270 history = ((history << 8) | (c & 0xff)) & 0xffffffff;
f9c68a47 271
f9731264
JB
272 /* Were the last four characters read "\n!#\n"? */
273 if (history == (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
274 return;
f9c68a47
JB
275 }
276}
db4b4ca6
DH
277#undef FUNC_NAME
278
f9c68a47 279
1bbd0b84 280static SCM scm_get_hash_procedure(int c);
f9c68a47 281
09a4f039 282static char s_list[]="list";
1cc91f1b 283
0f2d19dd 284SCM
1bbd0b84 285scm_lreadr (SCM *tok_buf,SCM port,SCM *copy)
db4b4ca6 286#define FUNC_NAME "scm_lreadr"
0f2d19dd
JB
287{
288 int c;
1be6b49c 289 size_t j;
0f2d19dd 290 SCM p;
deca31e1 291
b858464a 292 tryagain:
1bbd0b84 293 c = scm_flush_ws (port, s_scm_read);
b858464a 294 tryagain_no_flush_ws:
0f2d19dd
JB
295 switch (c)
296 {
297 case EOF:
298 return SCM_EOF_VAL;
299
300 case '(':
09a4f039 301 return SCM_RECORD_POSITIONS_P
b858464a
MG
302 ? scm_lreadrecparen (tok_buf, port, s_list, copy)
303 : scm_lreadparen (tok_buf, port, s_list, copy);
0f2d19dd 304 case ')':
db4b4ca6 305 SCM_MISC_ERROR ("unexpected \")\"", SCM_EOL);
0f2d19dd
JB
306 goto tryagain;
307
308 case '\'':
92e5aa0e 309 p = scm_sym_quote;
09a4f039 310 goto recquote;
0f2d19dd 311 case '`':
92e5aa0e 312 p = scm_sym_quasiquote;
09a4f039 313 goto recquote;
0f2d19dd 314 case ',':
b7f3516f 315 c = scm_getc (port);
0f2d19dd 316 if ('@' == c)
92e5aa0e 317 p = scm_sym_uq_splicing;
0f2d19dd
JB
318 else
319 {
b7f3516f 320 scm_ungetc (c, port);
92e5aa0e 321 p = scm_sym_unquote;
0f2d19dd 322 }
09a4f039
MD
323 recquote:
324 p = scm_cons2 (p,
deca31e1 325 scm_lreadr (tok_buf, port, copy),
09a4f039
MD
326 SCM_EOL);
327 if (SCM_RECORD_POSITIONS_P)
328 scm_whash_insert (scm_source_whash,
329 p,
330 scm_make_srcprops (SCM_LINUM (port),
331 SCM_COL (port) - 1,
332 SCM_FILENAME (port),
333 SCM_COPY_SOURCE_P
334 ? (*copy = scm_cons2 (SCM_CAR (p),
335 SCM_CAR (SCM_CDR (p)),
336 SCM_EOL))
337 : SCM_UNDEFINED,
338 SCM_EOL));
339 return p;
0f2d19dd 340 case '#':
b7f3516f 341 c = scm_getc (port);
b858464a
MG
342
343 {
344 /* Check for user-defined hash procedure first, to allow
345 overriding of builtin hash read syntaxes. */
346 SCM sharp = scm_get_hash_procedure (c);
347 if (!SCM_FALSEP (sharp))
348 {
349 int line = SCM_LINUM (port);
350 int column = SCM_COL (port) - 2;
351 SCM got;
352
353 got = scm_call_2 (sharp, SCM_MAKE_CHAR (c), port);
354 if (SCM_EQ_P (got, SCM_UNSPECIFIED))
355 goto unkshrp;
356 if (SCM_RECORD_POSITIONS_P)
357 return *copy = recsexpr (got, line, column,
358 SCM_FILENAME (port));
359 else
360 return got;
361 }
362 }
0f2d19dd
JB
363 switch (c)
364 {
365 case '(':
deca31e1 366 p = scm_lreadparen (tok_buf, port, "vector", copy);
0f2d19dd
JB
367 return SCM_NULLP (p) ? scm_nullvect : scm_vector (p);
368
369 case 't':
370 case 'T':
371 return SCM_BOOL_T;
372 case 'f':
373 case 'F':
374 return SCM_BOOL_F;
375
376 case 'b':
377 case 'B':
378 case 'o':
379 case 'O':
380 case 'd':
381 case 'D':
382 case 'x':
383 case 'X':
384 case 'i':
385 case 'I':
386 case 'e':
387 case 'E':
b7f3516f 388 scm_ungetc (c, port);
0f2d19dd
JB
389 c = '#';
390 goto num;
391
f9c68a47
JB
392 case '!':
393 /* start of a shell script. Parse as a block comment,
394 terminated by !#, just like SCSH. */
395 skip_scsh_block_comment (port);
b6356af7
MV
396 /* EOF is not an error here */
397 c = scm_flush_ws (port, (char *)NULL);
398 goto tryagain_no_flush_ws;
f9c68a47 399
afe5177e 400#ifdef HAVE_ARRAYS
0f2d19dd 401 case '*':
deca31e1 402 j = scm_read_token (c, tok_buf, port, 0);
405aaef9 403 p = scm_istr2bve (SCM_STRING_CHARS (*tok_buf) + 1, (long) (j - 1));
36284627 404 if (!SCM_FALSEP (p))
0f2d19dd
JB
405 return p;
406 else
407 goto unkshrp;
afe5177e 408#endif
0f2d19dd
JB
409
410 case '{':
deca31e1 411 j = scm_read_token (c, tok_buf, port, 1);
38ae064c 412 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
0f2d19dd
JB
413
414 case '\\':
b7f3516f 415 c = scm_getc (port);
deca31e1 416 j = scm_read_token (c, tok_buf, port, 0);
0f2d19dd 417 if (j == 1)
7866a09b 418 return SCM_MAKE_CHAR (c);
0f2d19dd
JB
419 if (c >= '0' && c < '8')
420 {
405aaef9 421 p = scm_istr2int (SCM_STRING_CHARS (*tok_buf), (long) j, 8);
36284627 422 if (!SCM_FALSEP (p))
7866a09b 423 return SCM_MAKE_CHAR (SCM_INUM (p));
0f2d19dd
JB
424 }
425 for (c = 0; c < scm_n_charnames; c++)
426 if (scm_charnames[c]
405aaef9 427 && (scm_casei_streq (scm_charnames[c], SCM_STRING_CHARS (*tok_buf))))
7866a09b 428 return SCM_MAKE_CHAR (scm_charnums[c]);
db4b4ca6 429 SCM_MISC_ERROR ("unknown # object", SCM_EOL);
0f2d19dd 430
50a095f1
JB
431 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
432 case ':':
433 j = scm_read_token ('-', tok_buf, port, 0);
38ae064c
DH
434 p = scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
435 return scm_make_keyword_from_dash_symbol (p);
0f2d19dd
JB
436
437 default:
438 callshrp:
deca31e1
GH
439 {
440 SCM sharp = scm_get_hash_procedure (c);
441
36284627 442 if (!SCM_FALSEP (sharp))
deca31e1
GH
443 {
444 int line = SCM_LINUM (port);
445 int column = SCM_COL (port) - 2;
446 SCM got;
447
fdc28395 448 got = scm_call_2 (sharp, SCM_MAKE_CHAR (c), port);
54778cd3 449 if (SCM_EQ_P (got, SCM_UNSPECIFIED))
deca31e1
GH
450 goto unkshrp;
451 if (SCM_RECORD_POSITIONS_P)
452 return *copy = recsexpr (got, line, column,
453 SCM_FILENAME (port));
454 else
455 return got;
456 }
457 }
03bc4386 458 unkshrp:
b858464a
MG
459 scm_misc_error (s_scm_read, "Unknown # object: ~S",
460 SCM_LIST1 (SCM_MAKE_CHAR (c)));
0f2d19dd
JB
461 }
462
463 case '"':
464 j = 0;
b7f3516f 465 while ('"' != (c = scm_getc (port)))
0f2d19dd 466 {
b3fcac34
DH
467 if (c == EOF)
468 SCM_MISC_ERROR ("end of file in string constant", SCM_EOL);
0f2d19dd 469
94115ae3 470 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
0f2d19dd
JB
471 scm_grow_tok_buf (tok_buf);
472
473 if (c == '\\')
b7f3516f 474 switch (c = scm_getc (port))
0f2d19dd
JB
475 {
476 case '\n':
477 continue;
478 case '0':
479 c = '\0';
480 break;
481 case 'f':
482 c = '\f';
483 break;
484 case 'n':
485 c = '\n';
486 break;
487 case 'r':
488 c = '\r';
489 break;
490 case 't':
491 c = '\t';
492 break;
493 case 'a':
494 c = '\007';
495 break;
496 case 'v':
497 c = '\v';
498 break;
499 }
405aaef9 500 SCM_STRING_CHARS (*tok_buf)[j] = c;
b7f3516f 501 ++j;
0f2d19dd
JB
502 }
503 if (j == 0)
504 return scm_nullstr;
405aaef9 505 SCM_STRING_CHARS (*tok_buf)[j] = 0;
36284627 506 return scm_mem2string (SCM_STRING_CHARS (*tok_buf), j);
0f2d19dd 507
b858464a 508 case'0':case '1':case '2':case '3':case '4':
0f2d19dd
JB
509 case '5':case '6':case '7':case '8':case '9':
510 case '.':
511 case '-':
512 case '+':
513 num:
b858464a
MG
514 j = scm_read_token (c, tok_buf, port, 0);
515 p = scm_istring2number (SCM_STRING_CHARS (*tok_buf), (long) j, 10L);
516 if (!SCM_FALSEP (p))
517 return p;
518 if (c == '#')
519 {
520 if ((j == 2) && (scm_getc (port) == '('))
521 {
522 scm_ungetc ('(', port);
523 c = SCM_STRING_CHARS (*tok_buf)[1];
524 goto callshrp;
525 }
526 SCM_MISC_ERROR ("unknown # object", SCM_EOL);
527 }
528 goto tok;
0f2d19dd
JB
529
530 case ':':
fee7ef83 531 if (SCM_EQ_P (SCM_PACK (SCM_KEYWORD_STYLE), scm_keyword_prefix))
c7733771
GH
532 {
533 j = scm_read_token ('-', tok_buf, port, 0);
38ae064c
DH
534 p = scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
535 return scm_make_keyword_from_dash_symbol (p);
c7733771
GH
536 }
537 /* fallthrough */
0f2d19dd 538 default:
deca31e1 539 j = scm_read_token (c, tok_buf, port, 0);
0f2d19dd
JB
540 /* fallthrough */
541
542 tok:
38ae064c 543 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
0f2d19dd
JB
544 }
545}
db4b4ca6
DH
546#undef FUNC_NAME
547
0f2d19dd
JB
548
549#ifdef _UNICOS
550_Pragma ("noopt"); /* # pragma _CRI noopt */
551#endif
1cc91f1b 552
1be6b49c 553size_t
6e8d25a6 554scm_read_token (int ic, SCM *tok_buf, SCM port, int weird)
0f2d19dd 555{
1be6b49c 556 register size_t j;
0f2d19dd
JB
557 register int c;
558 register char *p;
559
deca31e1 560 c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(ic) : ic);
405aaef9 561 p = SCM_STRING_CHARS (*tok_buf);
0f2d19dd
JB
562
563 if (weird)
564 j = 0;
565 else
566 {
567 j = 0;
94115ae3 568 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
0f2d19dd 569 p = scm_grow_tok_buf (tok_buf);
b7f3516f
TT
570 p[j] = c;
571 ++j;
0f2d19dd
JB
572 }
573
574 while (1)
575 {
94115ae3 576 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
0f2d19dd 577 p = scm_grow_tok_buf (tok_buf);
b7f3516f 578 c = scm_getc (port);
0f2d19dd
JB
579 switch (c)
580 {
581 case '(':
582 case ')':
583 case '"':
584 case ';':
585 case SCM_WHITE_SPACES:
586 case SCM_LINE_INCREMENTORS:
587 if (weird)
588 goto default_case;
589
b7f3516f 590 scm_ungetc (c, port);
0f2d19dd
JB
591 case EOF:
592 eof_case:
593 p[j] = 0;
594 return j;
595 case '\\':
596 if (!weird)
597 goto default_case;
598 else
599 {
b7f3516f 600 c = scm_getc (port);
0f2d19dd
JB
601 if (c == EOF)
602 goto eof_case;
603 else
604 goto default_case;
605 }
606 case '}':
607 if (!weird)
608 goto default_case;
609
b7f3516f 610 c = scm_getc (port);
0f2d19dd
JB
611 if (c == '#')
612 {
613 p[j] = 0;
614 return j;
615 }
616 else
617 {
b7f3516f 618 scm_ungetc (c, port);
0f2d19dd
JB
619 c = '}';
620 goto default_case;
621 }
622
623 default:
624 default_case:
625 {
deca31e1 626 c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(c) : c);
b7f3516f
TT
627 p[j] = c;
628 ++j;
0f2d19dd
JB
629 }
630
631 }
632 }
633}
1cc91f1b 634
0f2d19dd
JB
635#ifdef _UNICOS
636_Pragma ("opt"); /* # pragma _CRI opt */
637#endif
638
0f2d19dd 639SCM
6e8d25a6 640scm_lreadparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
db4b4ca6 641#define FUNC_NAME "scm_lreadparen"
0f2d19dd
JB
642{
643 SCM tmp;
644 SCM tl;
645 SCM ans;
646 int c;
647
648 c = scm_flush_ws (port, name);
649 if (')' == c)
650 return SCM_EOL;
b7f3516f 651 scm_ungetc (c, port);
54778cd3 652 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
0f2d19dd 653 {
deca31e1 654 ans = scm_lreadr (tok_buf, port, copy);
0f2d19dd
JB
655 closeit:
656 if (')' != (c = scm_flush_ws (port, name)))
db4b4ca6 657 SCM_MISC_ERROR ("missing close paren", SCM_EOL);
0f2d19dd
JB
658 return ans;
659 }
660 ans = tl = scm_cons (tmp, SCM_EOL);
661 while (')' != (c = scm_flush_ws (port, name)))
662 {
b7f3516f 663 scm_ungetc (c, port);
54778cd3 664 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
0f2d19dd 665 {
deca31e1 666 SCM_SETCDR (tl, scm_lreadr (tok_buf, port, copy));
0f2d19dd
JB
667 goto closeit;
668 }
a6c64c3c
MD
669 SCM_SETCDR (tl, scm_cons (tmp, SCM_EOL));
670 tl = SCM_CDR (tl);
0f2d19dd
JB
671 }
672 return ans;
673}
db4b4ca6 674#undef FUNC_NAME
0f2d19dd 675
1cc91f1b 676
09a4f039 677SCM
6e8d25a6 678scm_lreadrecparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
db4b4ca6 679#define FUNC_NAME "scm_lreadrecparen"
09a4f039
MD
680{
681 register int c;
682 register SCM tmp;
4dc2435a
JB
683 register SCM tl, tl2 = SCM_EOL;
684 SCM ans, ans2 = SCM_EOL;
09a4f039
MD
685 /* Need to capture line and column numbers here. */
686 int line = SCM_LINUM (port);
687 int column = SCM_COL (port) - 1;
688
689 c = scm_flush_ws (port, name);
690 if (')' == c)
691 return SCM_EOL;
b7f3516f 692 scm_ungetc (c, port);
54778cd3 693 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
09a4f039 694 {
deca31e1 695 ans = scm_lreadr (tok_buf, port, copy);
09a4f039 696 if (')' != (c = scm_flush_ws (port, name)))
db4b4ca6 697 SCM_MISC_ERROR ("missing close paren", SCM_EOL);
09a4f039
MD
698 return ans;
699 }
700 /* Build the head of the list structure. */
701 ans = tl = scm_cons (tmp, SCM_EOL);
702 if (SCM_COPY_SOURCE_P)
0c95b57d 703 ans2 = tl2 = scm_cons (SCM_CONSP (tmp)
09a4f039
MD
704 ? *copy
705 : tmp,
706 SCM_EOL);
707 while (')' != (c = scm_flush_ws (port, name)))
708 {
62850ef3
DH
709 SCM new_tail;
710
b7f3516f 711 scm_ungetc (c, port);
54778cd3 712 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
09a4f039 713 {
deca31e1 714 SCM_SETCDR (tl, tmp = scm_lreadr (tok_buf, port, copy));
09a4f039 715 if (SCM_COPY_SOURCE_P)
0c95b57d 716 SCM_SETCDR (tl2, scm_cons (SCM_CONSP (tmp)
09a4f039
MD
717 ? *copy
718 : tmp,
719 SCM_EOL));
720 if (')' != (c = scm_flush_ws (port, name)))
db4b4ca6 721 SCM_MISC_ERROR ("missing close paren", SCM_EOL);
09a4f039
MD
722 goto exit;
723 }
62850ef3
DH
724
725 new_tail = scm_cons (tmp, SCM_EOL);
726 SCM_SETCDR (tl, new_tail);
727 tl = new_tail;
728
09a4f039 729 if (SCM_COPY_SOURCE_P)
62850ef3
DH
730 {
731 SCM new_tail2 = scm_cons (SCM_CONSP (tmp) ? *copy : tmp, SCM_EOL);
732 SCM_SETCDR (tl2, new_tail2);
733 tl2 = new_tail2;
734 }
09a4f039
MD
735 }
736exit:
737 scm_whash_insert (scm_source_whash,
738 ans,
739 scm_make_srcprops (line,
740 column,
741 SCM_FILENAME (port),
742 SCM_COPY_SOURCE_P
743 ? *copy = ans2
744 : SCM_UNDEFINED,
745 SCM_EOL));
746 return ans;
747}
db4b4ca6 748#undef FUNC_NAME
09a4f039 749
0f2d19dd
JB
750
751\f
752
14de3b42
GH
753/* Manipulate the read-hash-procedures alist. This could be written in
754 Scheme, but maybe it will also be used by C code during initialisation. */
a1ec6916 755SCM_DEFINE (scm_read_hash_extend, "read-hash-extend", 2, 0, 0,
1bbd0b84 756 (SCM chr, SCM proc),
dc7fa443
MG
757 "Install the procedure @var{proc} for reading expressions\n"
758 "starting with the character sequence @code{#} and @var{chr}.\n"
759 "@var{proc} will be called with two arguments: the character\n"
760 "@var{chr} and the port to read further data from. The object\n"
761 "returned will be the return value of @code{read}.")
1bbd0b84 762#define FUNC_NAME s_scm_read_hash_extend
deca31e1 763{
fed9c9a2
GH
764 SCM this;
765 SCM prev;
766
36284627
DH
767 SCM_VALIDATE_CHAR (1, chr);
768 SCM_ASSERT (SCM_FALSEP (proc)
769 || SCM_EQ_P (scm_procedure_p (proc), SCM_BOOL_T),
770 proc, SCM_ARG2, FUNC_NAME);
fed9c9a2 771
14de3b42
GH
772 /* Check if chr is already in the alist. */
773 this = *scm_read_hash_procedures;
774 prev = SCM_BOOL_F;
fed9c9a2
GH
775 while (1)
776 {
777 if (SCM_NULLP (this))
778 {
779 /* not found, so add it to the beginning. */
36284627 780 if (!SCM_FALSEP (proc))
fed9c9a2 781 {
14de3b42
GH
782 *scm_read_hash_procedures =
783 scm_cons (scm_cons (chr, proc), *scm_read_hash_procedures);
fed9c9a2
GH
784 }
785 break;
786 }
54778cd3 787 if (SCM_EQ_P (chr, SCM_CAAR (this)))
fed9c9a2
GH
788 {
789 /* already in the alist. */
790 if (SCM_FALSEP (proc))
14de3b42
GH
791 {
792 /* remove it. */
54778cd3 793 if (SCM_FALSEP (prev))
14de3b42
GH
794 {
795 *scm_read_hash_procedures =
796 SCM_CDR (*scm_read_hash_procedures);
797 }
798 else
799 scm_set_cdr_x (prev, SCM_CDR (this));
800 }
fed9c9a2 801 else
14de3b42
GH
802 {
803 /* replace it. */
804 scm_set_cdr_x (SCM_CAR (this), proc);
805 }
fed9c9a2
GH
806 break;
807 }
808 prev = this;
809 this = SCM_CDR (this);
810 }
deca31e1 811
deca31e1
GH
812 return SCM_UNSPECIFIED;
813}
1bbd0b84 814#undef FUNC_NAME
0f2d19dd 815
deca31e1
GH
816/* Recover the read-hash procedure corresponding to char c. */
817static SCM
6e8d25a6 818scm_get_hash_procedure (int c)
deca31e1 819{
14de3b42 820 SCM rest = *scm_read_hash_procedures;
fed9c9a2 821
deca31e1
GH
822 while (1)
823 {
824 if (SCM_NULLP (rest))
825 return SCM_BOOL_F;
826
7866a09b 827 if (SCM_CHAR (SCM_CAAR (rest)) == c)
deca31e1
GH
828 return SCM_CDAR (rest);
829
830 rest = SCM_CDR (rest);
831 }
832}
1cc91f1b 833
0f2d19dd
JB
834void
835scm_init_read ()
0f2d19dd 836{
14de3b42 837 scm_read_hash_procedures =
86d31dfe 838 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL));
fed9c9a2 839
b7ff98dd 840 scm_init_opts (scm_read_options, scm_read_opts, SCM_N_READ_OPTIONS);
8dc9439f 841#ifndef SCM_MAGIC_SNARFER
a0599745 842#include "libguile/read.x"
8dc9439f 843#endif
0f2d19dd 844}
89e00824
ML
845
846/*
847 Local Variables:
848 c-file-style: "gnu"
849 End:
850*/