Commit | Line | Data |
---|---|---|
4cc81ec6 | 1 | /* Copyright (C) 1995,1996,1997, 1999 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 JB |
47 | #include <stdio.h> |
48 | #include "_scm.h" | |
20e6290e | 49 | #include "chars.h" |
20e6290e JB |
50 | #include "eval.h" |
51 | #include "unif.h" | |
547e65b5 | 52 | #include "keywords.h" |
20e6290e | 53 | #include "alist.h" |
09a4f039 MD |
54 | #include "srcprop.h" |
55 | #include "hashtab.h" | |
56 | #include "hash.h" | |
f04d8caf | 57 | #include "ports.h" |
ba11fd4c | 58 | #include "root.h" |
7ab3fdd5 | 59 | #include "strings.h" |
003d1fd0 | 60 | #include "vectors.h" |
20e6290e | 61 | |
b6791b2e | 62 | #include "validate.h" |
0f2d19dd JB |
63 | #include "read.h" |
64 | ||
65 | \f | |
66 | ||
c7733771 GH |
67 | SCM_SYMBOL (scm_keyword_prefix, "prefix"); |
68 | ||
a16f6fe7 | 69 | scm_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 | 80 | SCM_DEFINE (scm_read_options, "read-options-interface", 0, 1, 0, |
1bbd0b84 GB |
81 | (SCM setting), |
82 | "") | |
83 | #define FUNC_NAME s_scm_read_options | |
a16f6fe7 | 84 | { |
b7ff98dd MD |
85 | SCM ans = scm_options (setting, |
86 | scm_read_opts, | |
87 | SCM_N_READ_OPTIONS, | |
1bbd0b84 | 88 | FUNC_NAME); |
b7ff98dd MD |
89 | if (SCM_COPY_SOURCE_P) |
90 | SCM_RECORD_POSITIONS_P = 1; | |
a16f6fe7 MD |
91 | return ans; |
92 | } | |
1bbd0b84 | 93 | #undef FUNC_NAME |
a16f6fe7 | 94 | |
14de3b42 GH |
95 | /* An association list mapping extra hash characters to procedures. */ |
96 | static SCM *scm_read_hash_procedures; | |
deca31e1 | 97 | |
a1ec6916 | 98 | SCM_DEFINE (scm_read, "read", 0, 1, 0, |
1bbd0b84 GB |
99 | (SCM port), |
100 | "") | |
101 | #define FUNC_NAME s_scm_read | |
0f2d19dd JB |
102 | { |
103 | int c; | |
09a4f039 | 104 | SCM tok_buf, copy; |
0f2d19dd JB |
105 | |
106 | if (SCM_UNBNDP (port)) | |
107 | port = scm_cur_inp; | |
3b3b36dd | 108 | SCM_VALIDATE_OPINPORT (1,port); |
0f2d19dd | 109 | |
0f2d19dd JB |
110 | c = scm_flush_ws (port, (char *) NULL); |
111 | if (EOF == c) | |
112 | return SCM_EOF_VAL; | |
b7f3516f | 113 | scm_ungetc (c, port); |
0f2d19dd JB |
114 | |
115 | tok_buf = scm_makstr (30L, 0); | |
deca31e1 | 116 | return scm_lreadr (&tok_buf, port, ©); |
0f2d19dd | 117 | } |
1bbd0b84 | 118 | #undef FUNC_NAME |
0f2d19dd JB |
119 | |
120 | ||
1cc91f1b | 121 | |
0f2d19dd | 122 | char * |
6e8d25a6 | 123 | scm_grow_tok_buf (SCM *tok_buf) |
0f2d19dd | 124 | { |
85ab9947 | 125 | scm_vector_set_length_x (*tok_buf, SCM_MAKINUM (2 * SCM_LENGTH (*tok_buf))); |
0f2d19dd JB |
126 | return SCM_CHARS (*tok_buf); |
127 | } | |
128 | ||
129 | ||
1cc91f1b | 130 | |
0f2d19dd | 131 | int |
6e8d25a6 | 132 | scm_flush_ws (SCM port, const char *eoferr) |
0f2d19dd JB |
133 | { |
134 | register int c; | |
135 | while (1) | |
b7f3516f | 136 | switch (c = scm_getc (port)) |
0f2d19dd JB |
137 | { |
138 | case EOF: | |
139 | goteof: | |
140 | if (eoferr) | |
141 | scm_wta (SCM_UNDEFINED, "end of file in ", eoferr); | |
142 | return c; | |
143 | case ';': | |
144 | lp: | |
b7f3516f | 145 | switch (c = scm_getc (port)) |
0f2d19dd JB |
146 | { |
147 | case EOF: | |
148 | goto goteof; | |
149 | default: | |
150 | goto lp; | |
151 | case SCM_LINE_INCREMENTORS: | |
152 | break; | |
153 | } | |
154 | break; | |
155 | case SCM_LINE_INCREMENTORS: | |
0f2d19dd | 156 | case SCM_SINGLE_SPACES: |
0f2d19dd | 157 | case '\t': |
0f2d19dd JB |
158 | break; |
159 | default: | |
160 | return c; | |
161 | } | |
162 | } | |
163 | ||
164 | ||
1cc91f1b | 165 | |
0f2d19dd | 166 | int |
6e8d25a6 | 167 | scm_casei_streq (char *s1, char *s2) |
0f2d19dd JB |
168 | { |
169 | while (*s1 && *s2) | |
170 | if (scm_downcase((int)*s1) != scm_downcase((int)*s2)) | |
171 | return 0; | |
172 | else | |
173 | { | |
174 | ++s1; | |
175 | ++s2; | |
176 | } | |
177 | return !(*s1 || *s2); | |
178 | } | |
179 | ||
180 | ||
09a4f039 MD |
181 | /* recsexpr is used when recording expressions |
182 | * constructed by read:sharp. | |
183 | */ | |
604d4dd9 JB |
184 | #ifndef DEBUG_EXTENSIONS |
185 | #define recsexpr(obj, line, column, filename) (obj) | |
186 | #else | |
09a4f039 | 187 | static SCM |
1bbd0b84 | 188 | recsexpr (SCM obj,int line,int column,SCM filename) |
09a4f039 MD |
189 | { |
190 | if (SCM_IMP (obj) || SCM_NCONSP(obj)) | |
191 | return obj; | |
192 | { | |
193 | SCM tmp = obj, copy; | |
194 | /* If this sexpr is visible in the read:sharp source, we want to | |
195 | keep that information, so only record non-constant cons cells | |
196 | which haven't previously been read by the reader. */ | |
197 | if (SCM_FALSEP (scm_whash_lookup (scm_source_whash, obj))) | |
198 | { | |
199 | if (SCM_COPY_SOURCE_P) | |
200 | { | |
201 | copy = scm_cons (recsexpr (SCM_CAR (obj), line, column, filename), | |
202 | SCM_UNDEFINED); | |
0c95b57d | 203 | while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp)) |
a6c64c3c MD |
204 | { |
205 | SCM_SETCDR (copy, scm_cons (recsexpr (SCM_CAR (tmp), | |
206 | line, | |
207 | column, | |
208 | filename), | |
209 | SCM_UNDEFINED)); | |
210 | copy = SCM_CDR (copy); | |
211 | } | |
212 | SCM_SETCDR (copy, tmp); | |
09a4f039 MD |
213 | } |
214 | else | |
215 | { | |
216 | recsexpr (SCM_CAR (obj), line, column, filename); | |
0c95b57d | 217 | while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp)) |
09a4f039 MD |
218 | recsexpr (SCM_CAR (tmp), line, column, filename); |
219 | copy = SCM_UNDEFINED; | |
220 | } | |
221 | scm_whash_insert (scm_source_whash, | |
222 | obj, | |
223 | scm_make_srcprops (line, | |
224 | column, | |
225 | filename, | |
226 | copy, | |
227 | SCM_EOL)); | |
228 | } | |
229 | return obj; | |
230 | } | |
231 | } | |
604d4dd9 | 232 | #endif |
f9c68a47 JB |
233 | |
234 | /* Consume an SCSH-style block comment. Assume that we've already | |
f9731264 JB |
235 | read the initial `#!', and eat characters until we get a |
236 | newline/exclamation-point/sharp-sign/newline sequence. */ | |
f9c68a47 JB |
237 | |
238 | static void | |
6e8d25a6 | 239 | skip_scsh_block_comment (SCM port) |
f9c68a47 | 240 | { |
f9731264 JB |
241 | /* Is this portable? Dear God, spare me from the non-eight-bit |
242 | characters. But is it tasteful? */ | |
243 | long history = 0; | |
f9c68a47 JB |
244 | |
245 | for (;;) | |
246 | { | |
b7f3516f | 247 | int c = scm_getc (port); |
f9c68a47 JB |
248 | |
249 | if (c == EOF) | |
250 | scm_wta (SCM_UNDEFINED, | |
251 | "unterminated `#! ... !#' comment", "read"); | |
f9731264 | 252 | history = ((history << 8) | (c & 0xff)) & 0xffffffff; |
f9c68a47 | 253 | |
f9731264 JB |
254 | /* Were the last four characters read "\n!#\n"? */ |
255 | if (history == (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n')) | |
256 | return; | |
f9c68a47 JB |
257 | } |
258 | } | |
259 | ||
1bbd0b84 | 260 | static SCM scm_get_hash_procedure(int c); |
f9c68a47 | 261 | |
09a4f039 | 262 | static char s_list[]="list"; |
1cc91f1b | 263 | |
0f2d19dd | 264 | SCM |
1bbd0b84 | 265 | scm_lreadr (SCM *tok_buf,SCM port,SCM *copy) |
0f2d19dd JB |
266 | { |
267 | int c; | |
268 | scm_sizet j; | |
269 | SCM p; | |
deca31e1 | 270 | |
0f2d19dd | 271 | tryagain: |
1bbd0b84 | 272 | c = scm_flush_ws (port, s_scm_read); |
b6356af7 | 273 | tryagain_no_flush_ws: |
0f2d19dd JB |
274 | switch (c) |
275 | { | |
276 | case EOF: | |
277 | return SCM_EOF_VAL; | |
278 | ||
279 | case '(': | |
09a4f039 | 280 | return SCM_RECORD_POSITIONS_P |
deca31e1 GH |
281 | ? scm_lreadrecparen (tok_buf, port, s_list, copy) |
282 | : scm_lreadparen (tok_buf, port, s_list, copy); | |
0f2d19dd JB |
283 | case ')': |
284 | scm_wta (SCM_UNDEFINED, "unexpected \")\"", "read"); | |
285 | goto tryagain; | |
286 | ||
287 | case '\'': | |
92e5aa0e | 288 | p = scm_sym_quote; |
09a4f039 | 289 | goto recquote; |
0f2d19dd | 290 | case '`': |
92e5aa0e | 291 | p = scm_sym_quasiquote; |
09a4f039 | 292 | goto recquote; |
0f2d19dd | 293 | case ',': |
b7f3516f | 294 | c = scm_getc (port); |
0f2d19dd | 295 | if ('@' == c) |
92e5aa0e | 296 | p = scm_sym_uq_splicing; |
0f2d19dd JB |
297 | else |
298 | { | |
b7f3516f | 299 | scm_ungetc (c, port); |
92e5aa0e | 300 | p = scm_sym_unquote; |
0f2d19dd | 301 | } |
09a4f039 MD |
302 | recquote: |
303 | p = scm_cons2 (p, | |
deca31e1 | 304 | scm_lreadr (tok_buf, port, copy), |
09a4f039 MD |
305 | SCM_EOL); |
306 | if (SCM_RECORD_POSITIONS_P) | |
307 | scm_whash_insert (scm_source_whash, | |
308 | p, | |
309 | scm_make_srcprops (SCM_LINUM (port), | |
310 | SCM_COL (port) - 1, | |
311 | SCM_FILENAME (port), | |
312 | SCM_COPY_SOURCE_P | |
313 | ? (*copy = scm_cons2 (SCM_CAR (p), | |
314 | SCM_CAR (SCM_CDR (p)), | |
315 | SCM_EOL)) | |
316 | : SCM_UNDEFINED, | |
317 | SCM_EOL)); | |
318 | return p; | |
0f2d19dd | 319 | case '#': |
b7f3516f | 320 | c = scm_getc (port); |
0f2d19dd JB |
321 | switch (c) |
322 | { | |
323 | case '(': | |
deca31e1 | 324 | p = scm_lreadparen (tok_buf, port, "vector", copy); |
0f2d19dd JB |
325 | return SCM_NULLP (p) ? scm_nullvect : scm_vector (p); |
326 | ||
327 | case 't': | |
328 | case 'T': | |
329 | return SCM_BOOL_T; | |
330 | case 'f': | |
331 | case 'F': | |
332 | return SCM_BOOL_F; | |
333 | ||
334 | case 'b': | |
335 | case 'B': | |
336 | case 'o': | |
337 | case 'O': | |
338 | case 'd': | |
339 | case 'D': | |
340 | case 'x': | |
341 | case 'X': | |
342 | case 'i': | |
343 | case 'I': | |
344 | case 'e': | |
345 | case 'E': | |
b7f3516f | 346 | scm_ungetc (c, port); |
0f2d19dd JB |
347 | c = '#'; |
348 | goto num; | |
349 | ||
f9c68a47 JB |
350 | case '!': |
351 | /* start of a shell script. Parse as a block comment, | |
352 | terminated by !#, just like SCSH. */ | |
353 | skip_scsh_block_comment (port); | |
b6356af7 MV |
354 | /* EOF is not an error here */ |
355 | c = scm_flush_ws (port, (char *)NULL); | |
356 | goto tryagain_no_flush_ws; | |
f9c68a47 | 357 | |
afe5177e | 358 | #ifdef HAVE_ARRAYS |
0f2d19dd | 359 | case '*': |
deca31e1 | 360 | j = scm_read_token (c, tok_buf, port, 0); |
0f2d19dd JB |
361 | p = scm_istr2bve (SCM_CHARS (*tok_buf) + 1, (long) (j - 1)); |
362 | if (SCM_NFALSEP (p)) | |
363 | return p; | |
364 | else | |
365 | goto unkshrp; | |
afe5177e | 366 | #endif |
0f2d19dd JB |
367 | |
368 | case '{': | |
deca31e1 | 369 | j = scm_read_token (c, tok_buf, port, 1); |
0f2d19dd | 370 | p = scm_intern (SCM_CHARS (*tok_buf), j); |
0f2d19dd JB |
371 | return SCM_CAR (p); |
372 | ||
373 | case '\\': | |
b7f3516f | 374 | c = scm_getc (port); |
deca31e1 | 375 | j = scm_read_token (c, tok_buf, port, 0); |
0f2d19dd | 376 | if (j == 1) |
7866a09b | 377 | return SCM_MAKE_CHAR (c); |
0f2d19dd JB |
378 | if (c >= '0' && c < '8') |
379 | { | |
380 | p = scm_istr2int (SCM_CHARS (*tok_buf), (long) j, 8); | |
381 | if (SCM_NFALSEP (p)) | |
7866a09b | 382 | return SCM_MAKE_CHAR (SCM_INUM (p)); |
0f2d19dd JB |
383 | } |
384 | for (c = 0; c < scm_n_charnames; c++) | |
385 | if (scm_charnames[c] | |
386 | && (scm_casei_streq (scm_charnames[c], SCM_CHARS (*tok_buf)))) | |
7866a09b | 387 | return SCM_MAKE_CHAR (scm_charnums[c]); |
0f2d19dd JB |
388 | scm_wta (SCM_UNDEFINED, "unknown # object: #\\", SCM_CHARS (*tok_buf)); |
389 | ||
50a095f1 JB |
390 | /* #:SYMBOL is a syntax for keywords supported in all contexts. */ |
391 | case ':': | |
392 | j = scm_read_token ('-', tok_buf, port, 0); | |
393 | p = scm_intern (SCM_CHARS (*tok_buf), j); | |
50a095f1 | 394 | return scm_make_keyword_from_dash_symbol (SCM_CAR (p)); |
0f2d19dd JB |
395 | |
396 | default: | |
397 | callshrp: | |
deca31e1 GH |
398 | { |
399 | SCM sharp = scm_get_hash_procedure (c); | |
400 | ||
401 | if (SCM_NIMP (sharp)) | |
402 | { | |
403 | int line = SCM_LINUM (port); | |
404 | int column = SCM_COL (port) - 2; | |
405 | SCM got; | |
406 | ||
407 | got = scm_apply (sharp, | |
7866a09b | 408 | SCM_MAKE_CHAR (c), |
deca31e1 | 409 | scm_acons (port, SCM_EOL, SCM_EOL)); |
54778cd3 | 410 | if (SCM_EQ_P (got, SCM_UNSPECIFIED)) |
deca31e1 GH |
411 | goto unkshrp; |
412 | if (SCM_RECORD_POSITIONS_P) | |
413 | return *copy = recsexpr (got, line, column, | |
414 | SCM_FILENAME (port)); | |
415 | else | |
416 | return got; | |
417 | } | |
418 | } | |
03bc4386 | 419 | unkshrp: |
70d63753 | 420 | scm_misc_error (s_scm_read, "Unknown # object: ~S", |
7866a09b | 421 | scm_listify (SCM_MAKE_CHAR (c), SCM_UNDEFINED)); |
0f2d19dd JB |
422 | } |
423 | ||
424 | case '"': | |
425 | j = 0; | |
b7f3516f | 426 | while ('"' != (c = scm_getc (port))) |
0f2d19dd JB |
427 | { |
428 | SCM_ASSERT (EOF != c, SCM_UNDEFINED, "end of file in ", "string"); | |
429 | ||
b7f3516f | 430 | while (j + 2 >= SCM_LENGTH (*tok_buf)) |
0f2d19dd JB |
431 | scm_grow_tok_buf (tok_buf); |
432 | ||
433 | if (c == '\\') | |
b7f3516f | 434 | switch (c = scm_getc (port)) |
0f2d19dd JB |
435 | { |
436 | case '\n': | |
437 | continue; | |
438 | case '0': | |
439 | c = '\0'; | |
440 | break; | |
441 | case 'f': | |
442 | c = '\f'; | |
443 | break; | |
444 | case 'n': | |
445 | c = '\n'; | |
446 | break; | |
447 | case 'r': | |
448 | c = '\r'; | |
449 | break; | |
450 | case 't': | |
451 | c = '\t'; | |
452 | break; | |
453 | case 'a': | |
454 | c = '\007'; | |
455 | break; | |
456 | case 'v': | |
457 | c = '\v'; | |
458 | break; | |
459 | } | |
b7f3516f TT |
460 | SCM_CHARS (*tok_buf)[j] = c; |
461 | ++j; | |
0f2d19dd JB |
462 | } |
463 | if (j == 0) | |
464 | return scm_nullstr; | |
465 | SCM_CHARS (*tok_buf)[j] = 0; | |
466 | { | |
467 | SCM str; | |
468 | str = scm_makfromstr (SCM_CHARS (*tok_buf), j, 0); | |
0f2d19dd JB |
469 | return str; |
470 | } | |
471 | ||
472 | case'0':case '1':case '2':case '3':case '4': | |
473 | case '5':case '6':case '7':case '8':case '9': | |
474 | case '.': | |
475 | case '-': | |
476 | case '+': | |
477 | num: | |
deca31e1 | 478 | j = scm_read_token (c, tok_buf, port, 0); |
0f2d19dd JB |
479 | p = scm_istring2number (SCM_CHARS (*tok_buf), (long) j, 10L); |
480 | if (SCM_NFALSEP (p)) | |
481 | return p; | |
482 | if (c == '#') | |
483 | { | |
b7f3516f | 484 | if ((j == 2) && (scm_getc (port) == '(')) |
0f2d19dd | 485 | { |
b7f3516f | 486 | scm_ungetc ('(', port); |
0f2d19dd JB |
487 | c = SCM_CHARS (*tok_buf)[1]; |
488 | goto callshrp; | |
489 | } | |
490 | scm_wta (SCM_UNDEFINED, "unknown # object", SCM_CHARS (*tok_buf)); | |
491 | } | |
492 | goto tok; | |
493 | ||
494 | case ':': | |
f1267706 | 495 | if (SCM_PACK (SCM_KEYWORD_STYLE) == scm_keyword_prefix) |
c7733771 GH |
496 | { |
497 | j = scm_read_token ('-', tok_buf, port, 0); | |
498 | p = scm_intern (SCM_CHARS (*tok_buf), j); | |
c7733771 GH |
499 | return scm_make_keyword_from_dash_symbol (SCM_CAR (p)); |
500 | } | |
501 | /* fallthrough */ | |
0f2d19dd | 502 | default: |
deca31e1 | 503 | j = scm_read_token (c, tok_buf, port, 0); |
0f2d19dd JB |
504 | /* fallthrough */ |
505 | ||
506 | tok: | |
507 | p = scm_intern (SCM_CHARS (*tok_buf), j); | |
0f2d19dd JB |
508 | return SCM_CAR (p); |
509 | } | |
510 | } | |
511 | ||
512 | #ifdef _UNICOS | |
513 | _Pragma ("noopt"); /* # pragma _CRI noopt */ | |
514 | #endif | |
1cc91f1b | 515 | |
0f2d19dd | 516 | scm_sizet |
6e8d25a6 | 517 | scm_read_token (int ic, SCM *tok_buf, SCM port, int weird) |
0f2d19dd JB |
518 | { |
519 | register scm_sizet j; | |
520 | register int c; | |
521 | register char *p; | |
522 | ||
deca31e1 | 523 | c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(ic) : ic); |
0f2d19dd JB |
524 | p = SCM_CHARS (*tok_buf); |
525 | ||
526 | if (weird) | |
527 | j = 0; | |
528 | else | |
529 | { | |
530 | j = 0; | |
b7f3516f | 531 | while (j + 2 >= SCM_LENGTH (*tok_buf)) |
0f2d19dd | 532 | p = scm_grow_tok_buf (tok_buf); |
b7f3516f TT |
533 | p[j] = c; |
534 | ++j; | |
0f2d19dd JB |
535 | } |
536 | ||
537 | while (1) | |
538 | { | |
b7f3516f | 539 | while (j + 2 >= SCM_LENGTH (*tok_buf)) |
0f2d19dd | 540 | p = scm_grow_tok_buf (tok_buf); |
b7f3516f | 541 | c = scm_getc (port); |
0f2d19dd JB |
542 | switch (c) |
543 | { | |
544 | case '(': | |
545 | case ')': | |
546 | case '"': | |
547 | case ';': | |
548 | case SCM_WHITE_SPACES: | |
549 | case SCM_LINE_INCREMENTORS: | |
550 | if (weird) | |
551 | goto default_case; | |
552 | ||
b7f3516f | 553 | scm_ungetc (c, port); |
0f2d19dd JB |
554 | case EOF: |
555 | eof_case: | |
556 | p[j] = 0; | |
557 | return j; | |
558 | case '\\': | |
559 | if (!weird) | |
560 | goto default_case; | |
561 | else | |
562 | { | |
b7f3516f | 563 | c = scm_getc (port); |
0f2d19dd JB |
564 | if (c == EOF) |
565 | goto eof_case; | |
566 | else | |
567 | goto default_case; | |
568 | } | |
569 | case '}': | |
570 | if (!weird) | |
571 | goto default_case; | |
572 | ||
b7f3516f | 573 | c = scm_getc (port); |
0f2d19dd JB |
574 | if (c == '#') |
575 | { | |
576 | p[j] = 0; | |
577 | return j; | |
578 | } | |
579 | else | |
580 | { | |
b7f3516f | 581 | scm_ungetc (c, port); |
0f2d19dd JB |
582 | c = '}'; |
583 | goto default_case; | |
584 | } | |
585 | ||
586 | default: | |
587 | default_case: | |
588 | { | |
deca31e1 | 589 | c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(c) : c); |
b7f3516f TT |
590 | p[j] = c; |
591 | ++j; | |
0f2d19dd JB |
592 | } |
593 | ||
594 | } | |
595 | } | |
596 | } | |
1cc91f1b | 597 | |
0f2d19dd JB |
598 | #ifdef _UNICOS |
599 | _Pragma ("opt"); /* # pragma _CRI opt */ | |
600 | #endif | |
601 | ||
0f2d19dd | 602 | SCM |
6e8d25a6 | 603 | scm_lreadparen (SCM *tok_buf, SCM port, char *name, SCM *copy) |
0f2d19dd JB |
604 | { |
605 | SCM tmp; | |
606 | SCM tl; | |
607 | SCM ans; | |
608 | int c; | |
609 | ||
610 | c = scm_flush_ws (port, name); | |
611 | if (')' == c) | |
612 | return SCM_EOL; | |
b7f3516f | 613 | scm_ungetc (c, port); |
54778cd3 | 614 | if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy)))) |
0f2d19dd | 615 | { |
deca31e1 | 616 | ans = scm_lreadr (tok_buf, port, copy); |
0f2d19dd JB |
617 | closeit: |
618 | if (')' != (c = scm_flush_ws (port, name))) | |
619 | scm_wta (SCM_UNDEFINED, "missing close paren", ""); | |
620 | return ans; | |
621 | } | |
622 | ans = tl = scm_cons (tmp, SCM_EOL); | |
623 | while (')' != (c = scm_flush_ws (port, name))) | |
624 | { | |
b7f3516f | 625 | scm_ungetc (c, port); |
54778cd3 | 626 | if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy)))) |
0f2d19dd | 627 | { |
deca31e1 | 628 | SCM_SETCDR (tl, scm_lreadr (tok_buf, port, copy)); |
0f2d19dd JB |
629 | goto closeit; |
630 | } | |
a6c64c3c MD |
631 | SCM_SETCDR (tl, scm_cons (tmp, SCM_EOL)); |
632 | tl = SCM_CDR (tl); | |
0f2d19dd JB |
633 | } |
634 | return ans; | |
635 | } | |
636 | ||
1cc91f1b | 637 | |
09a4f039 | 638 | SCM |
6e8d25a6 | 639 | scm_lreadrecparen (SCM *tok_buf, SCM port, char *name, SCM *copy) |
09a4f039 MD |
640 | { |
641 | register int c; | |
642 | register SCM tmp; | |
4dc2435a JB |
643 | register SCM tl, tl2 = SCM_EOL; |
644 | SCM ans, ans2 = SCM_EOL; | |
09a4f039 MD |
645 | /* Need to capture line and column numbers here. */ |
646 | int line = SCM_LINUM (port); | |
647 | int column = SCM_COL (port) - 1; | |
648 | ||
649 | c = scm_flush_ws (port, name); | |
650 | if (')' == c) | |
651 | return SCM_EOL; | |
b7f3516f | 652 | scm_ungetc (c, port); |
54778cd3 | 653 | if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy)))) |
09a4f039 | 654 | { |
deca31e1 | 655 | ans = scm_lreadr (tok_buf, port, copy); |
09a4f039 MD |
656 | if (')' != (c = scm_flush_ws (port, name))) |
657 | scm_wta (SCM_UNDEFINED, "missing close paren", ""); | |
658 | return ans; | |
659 | } | |
660 | /* Build the head of the list structure. */ | |
661 | ans = tl = scm_cons (tmp, SCM_EOL); | |
662 | if (SCM_COPY_SOURCE_P) | |
0c95b57d | 663 | ans2 = tl2 = scm_cons (SCM_CONSP (tmp) |
09a4f039 MD |
664 | ? *copy |
665 | : tmp, | |
666 | SCM_EOL); | |
667 | while (')' != (c = scm_flush_ws (port, name))) | |
668 | { | |
b7f3516f | 669 | scm_ungetc (c, port); |
54778cd3 | 670 | if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy)))) |
09a4f039 | 671 | { |
deca31e1 | 672 | SCM_SETCDR (tl, tmp = scm_lreadr (tok_buf, port, copy)); |
09a4f039 | 673 | if (SCM_COPY_SOURCE_P) |
0c95b57d | 674 | SCM_SETCDR (tl2, scm_cons (SCM_CONSP (tmp) |
09a4f039 MD |
675 | ? *copy |
676 | : tmp, | |
677 | SCM_EOL)); | |
678 | if (')' != (c = scm_flush_ws (port, name))) | |
679 | scm_wta (SCM_UNDEFINED, "missing close paren", ""); | |
680 | goto exit; | |
681 | } | |
682 | tl = SCM_SETCDR (tl, scm_cons (tmp, SCM_EOL)); | |
683 | if (SCM_COPY_SOURCE_P) | |
0c95b57d | 684 | tl2 = SCM_SETCDR (tl2, scm_cons (SCM_CONSP (tmp) |
09a4f039 MD |
685 | ? *copy |
686 | : tmp, | |
687 | SCM_EOL)); | |
688 | } | |
689 | exit: | |
690 | scm_whash_insert (scm_source_whash, | |
691 | ans, | |
692 | scm_make_srcprops (line, | |
693 | column, | |
694 | SCM_FILENAME (port), | |
695 | SCM_COPY_SOURCE_P | |
696 | ? *copy = ans2 | |
697 | : SCM_UNDEFINED, | |
698 | SCM_EOL)); | |
699 | return ans; | |
700 | } | |
701 | ||
0f2d19dd JB |
702 | |
703 | \f | |
704 | ||
14de3b42 GH |
705 | /* Manipulate the read-hash-procedures alist. This could be written in |
706 | Scheme, but maybe it will also be used by C code during initialisation. */ | |
a1ec6916 | 707 | SCM_DEFINE (scm_read_hash_extend, "read-hash-extend", 2, 0, 0, |
1bbd0b84 GB |
708 | (SCM chr, SCM proc), |
709 | "") | |
710 | #define FUNC_NAME s_scm_read_hash_extend | |
deca31e1 | 711 | { |
fed9c9a2 GH |
712 | SCM this; |
713 | SCM prev; | |
714 | ||
7866a09b | 715 | SCM_VALIDATE_CHAR (1,chr); |
fed9c9a2 | 716 | SCM_ASSERT (SCM_FALSEP (proc) || SCM_NIMP(proc), proc, SCM_ARG2, |
1bbd0b84 | 717 | FUNC_NAME); |
fed9c9a2 | 718 | |
14de3b42 GH |
719 | /* Check if chr is already in the alist. */ |
720 | this = *scm_read_hash_procedures; | |
721 | prev = SCM_BOOL_F; | |
fed9c9a2 GH |
722 | while (1) |
723 | { | |
724 | if (SCM_NULLP (this)) | |
725 | { | |
726 | /* not found, so add it to the beginning. */ | |
727 | if (SCM_NFALSEP (proc)) | |
728 | { | |
14de3b42 GH |
729 | *scm_read_hash_procedures = |
730 | scm_cons (scm_cons (chr, proc), *scm_read_hash_procedures); | |
fed9c9a2 GH |
731 | } |
732 | break; | |
733 | } | |
54778cd3 | 734 | if (SCM_EQ_P (chr, SCM_CAAR (this))) |
fed9c9a2 GH |
735 | { |
736 | /* already in the alist. */ | |
737 | if (SCM_FALSEP (proc)) | |
14de3b42 GH |
738 | { |
739 | /* remove it. */ | |
54778cd3 | 740 | if (SCM_FALSEP (prev)) |
14de3b42 GH |
741 | { |
742 | *scm_read_hash_procedures = | |
743 | SCM_CDR (*scm_read_hash_procedures); | |
744 | } | |
745 | else | |
746 | scm_set_cdr_x (prev, SCM_CDR (this)); | |
747 | } | |
fed9c9a2 | 748 | else |
14de3b42 GH |
749 | { |
750 | /* replace it. */ | |
751 | scm_set_cdr_x (SCM_CAR (this), proc); | |
752 | } | |
fed9c9a2 GH |
753 | break; |
754 | } | |
755 | prev = this; | |
756 | this = SCM_CDR (this); | |
757 | } | |
deca31e1 | 758 | |
deca31e1 GH |
759 | return SCM_UNSPECIFIED; |
760 | } | |
1bbd0b84 | 761 | #undef FUNC_NAME |
0f2d19dd | 762 | |
deca31e1 GH |
763 | /* Recover the read-hash procedure corresponding to char c. */ |
764 | static SCM | |
6e8d25a6 | 765 | scm_get_hash_procedure (int c) |
deca31e1 | 766 | { |
14de3b42 | 767 | SCM rest = *scm_read_hash_procedures; |
fed9c9a2 | 768 | |
deca31e1 GH |
769 | while (1) |
770 | { | |
771 | if (SCM_NULLP (rest)) | |
772 | return SCM_BOOL_F; | |
773 | ||
7866a09b | 774 | if (SCM_CHAR (SCM_CAAR (rest)) == c) |
deca31e1 GH |
775 | return SCM_CDAR (rest); |
776 | ||
777 | rest = SCM_CDR (rest); | |
778 | } | |
779 | } | |
1cc91f1b | 780 | |
0f2d19dd JB |
781 | void |
782 | scm_init_read () | |
0f2d19dd | 783 | { |
14de3b42 GH |
784 | scm_read_hash_procedures = |
785 | SCM_CDRLOC (scm_sysintern ("read-hash-procedures", SCM_EOL)); | |
fed9c9a2 | 786 | |
b7ff98dd | 787 | scm_init_opts (scm_read_options, scm_read_opts, SCM_N_READ_OPTIONS); |
0f2d19dd JB |
788 | #include "read.x" |
789 | } | |
89e00824 ML |
790 | |
791 | /* | |
792 | Local Variables: | |
793 | c-file-style: "gnu" | |
794 | End: | |
795 | */ |