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