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