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