Debian Codegraph @

src/read.c

raw
1 /*
2 * read.c - reader
3 *
4 * Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <math.h>
37 #define LIBGAUCHE_BODY
38 #include "gauche.h"
39 #include "gauche/priv/configP.h"
40 #include "gauche/char_attr.h"
41 #include "gauche/priv/portP.h"
42 #include "gauche/priv/builtin-syms.h"
43 #include "gauche/priv/readerP.h"
44
45 /*
46 * READ
47 */
48
49 static void read_context_flush(ScmReadContext *ctx);
50 static ScmObj read_internal(ScmPort *port, ScmReadContext *ctx);
51 static ScmObj read_item(ScmPort *port, ScmReadContext *ctx);
52 static ScmObj read_list(ScmPort *port, ScmChar closer, ScmReadContext *ctx);
53 static ScmObj read_vector(ScmPort *port, ScmChar closer, ScmReadContext *ctx);
54 static ScmObj read_string(ScmPort *port, int incompletep, ScmReadContext *ctx);
55 static ScmObj read_quoted(ScmPort *port, ScmObj quoter, ScmReadContext *ctx);
56 static ScmObj read_char(ScmPort *port, ScmReadContext *ctx);
57 static ScmObj read_word(ScmPort *port, ScmChar initial, ScmReadContext *ctx,
58 int temp_case_fold, int include_hash_sign);
59 static ScmObj read_symbol(ScmPort *port, ScmChar initial, ScmReadContext *ctx);
60 static ScmObj read_immediate_symbol(ScmPort *port, ScmChar initial,
61 int interned, const char *preceding,
62 ScmReadContext *ctx);
63 static ScmObj read_number(ScmPort *port, ScmChar initial,
64 int radix, /* #<radix>r case */
65 ScmReadContext *ctx);
66 static ScmObj read_symbol_or_number(ScmPort *port, ScmChar initial, ScmReadContext *ctx);
67 static ScmObj read_escaped_symbol(ScmPort *port, ScmChar delim, int interned,
68 ScmReadContext *ctx);
69 static ScmObj read_keyword(ScmPort *port, ScmReadContext *ctx);
70 static ScmObj read_regexp(ScmPort *port);
71 static ScmObj read_charset(ScmPort *port);
72 static ScmObj read_sharp_comma(ScmPort *port, ScmReadContext *ctx);
73 static ScmObj read_sharp_asterisk(ScmPort *port, ScmReadContext *ctx);
74 static ScmObj process_sharp_comma(ScmPort *port, int line,
75 ScmObj key, ScmObj args,
76 ScmReadContext *ctx, int has_ref);
77 static ScmObj read_shebang(ScmPort *port, ScmReadContext *ctx);
78 static ScmObj read_num_prefixed(ScmPort *port, ScmChar ch, ScmReadContext *ctx);
79 static ScmObj read_sharp_word(ScmPort *port, char c, ScmReadContext *ctx);
80
81 /* Table of 'read-time constructor' in SRFI-10 */
82 static struct {
83 ScmHashTable *table;
84 ScmInternalMutex mutex;
85 } readCtorData;
86
87 /* Table of hash-bang directive */
88 static struct {
89 ScmHashTable *table;
90 ScmInternalMutex mutex;
91 } hashBangData;
92
93 /* Parameter location for default reader mode */
94 static ScmPrimitiveParameter *defaultReadContext;
95
96 /*----------------------------------------------------------------
97 * Entry points
98 * Note: Entire read operation are done while locking the input port.
99 * So we can use 'unsafe' version of port operations inside this file.
100 * The lock is removed if reader routine signals an error. It is OK
101 * to call read routine recursively.
102 */
103 ScmObj Scm_ReadWithContext(ScmObj port, ScmReadContext *ctx)
104 {
105 ScmVM *vm = Scm_VM();
106 volatile ScmObj r = SCM_NIL;
107 if (!SCM_PORTP(port) || SCM_PORT_DIR(port) != SCM_PORT_INPUT) {
108 Scm_Error("input port required: %S", port);
109 }
110 if (!(ctx->flags & RCTX_RECURSIVELY)) {
111 ctx->table = NULL;
112 ctx->pending = SCM_NIL;
113 }
114 if (PORT_LOCKED(SCM_PORT(port), vm)) {
115 r = read_item(SCM_PORT(port), ctx);
116 } else {
117 PORT_LOCK(SCM_PORT(port), vm);
118 PORT_SAFE_CALL(SCM_PORT(port),
119 r = read_item(SCM_PORT(port), ctx), /*no cleanup*/);
120 PORT_UNLOCK(SCM_PORT(port));
121 }
122 if (!(ctx->flags & RCTX_RECURSIVELY)) {
123 read_context_flush(ctx);
124 }
125 return r;
126 }
127
128 ScmObj Scm_Read(ScmObj port)
129 {
130 return Scm_ReadWithContext(port, Scm_MakeReadContext(NULL));
131 }
132
133 /* Convenience functions */
134 ScmObj Scm_ReadFromString(ScmString *str)
135 {
136 ScmObj inp = Scm_MakeInputStringPort(str, TRUE);
137 ScmReadContext *ctx = Scm_MakeReadContext(NULL);
138 ScmObj r = read_item(SCM_PORT(inp), ctx);
139 read_context_flush(ctx);
140 return r;
141 }
142
143 ScmObj Scm_ReadFromCString(const char *cstr)
144 {
145 ScmObj s = SCM_MAKE_STR_IMMUTABLE(cstr);
146 ScmObj inp = Scm_MakeInputStringPort(SCM_STRING(s), TRUE);
147 ScmReadContext *ctx = Scm_MakeReadContext(NULL);
148 ScmObj r = read_item(SCM_PORT(inp), ctx);
149 read_context_flush(ctx);
150 return r;
151 }
152
153 ScmObj Scm_ReadListWithContext(ScmObj port, ScmChar closer, ScmReadContext *ctx)
154 {
155 ScmVM *vm = Scm_VM();
156 volatile ScmObj r = SCM_NIL;
157 if (!SCM_PORTP(port) || SCM_PORT_DIR(port) != SCM_PORT_INPUT) {
158 Scm_Error("input port required: %S", port);
159 }
160 if (!(ctx->flags & RCTX_RECURSIVELY)) {
161 ctx->table = NULL;
162 ctx->pending = SCM_NIL;
163 }
164 if (PORT_LOCKED(SCM_PORT(port), vm)) {
165 r = read_list(SCM_PORT(port), closer, ctx);
166 } else {
167 PORT_LOCK(SCM_PORT(port), vm);
168 PORT_SAFE_CALL(SCM_PORT(port),
169 r = read_list(SCM_PORT(port), closer, ctx),
170 /*no cleanup*/);
171 PORT_UNLOCK(SCM_PORT(port));
172 }
173 if (!(ctx->flags & RCTX_RECURSIVELY)) {
174 read_context_flush(ctx);
175 }
176 return r;
177 }
178
179 ScmObj Scm_ReadList(ScmObj port, ScmChar closer)
180 {
181 ScmReadContext *ctx = Scm_MakeReadContext(NULL);
182 return Scm_ReadListWithContext(port, closer, ctx);
183 }
184
185 /*----------------------------------------------------------------
186 * Read context
187 */
188
189 ScmReadContext *Scm_CurrentReadContext()
190 {
191 ScmObj c = Scm_PrimitiveParameterRef(Scm_VM(), defaultReadContext);
192 SCM_ASSERT(SCM_READ_CONTEXT_P(c));
193 return SCM_READ_CONTEXT(c);
194 }
195
196 ScmReadContext *Scm_SetCurrentReadContext(ScmReadContext *ctx)
197 {
198 ScmObj p = Scm_PrimitiveParameterSet(Scm_VM(), defaultReadContext,
199 SCM_OBJ(ctx));
200 SCM_ASSERT(SCM_READ_CONTEXT_P(p));
201 return SCM_READ_CONTEXT(p);
202 }
203
204 static ScmReadContext *make_read_context(ScmReadContext *proto)
205 {
206 ScmReadContext *ctx = SCM_NEW(ScmReadContext);
207 SCM_SET_CLASS(ctx, SCM_CLASS_READ_CONTEXT);
208 ctx->flags = proto ? proto->flags : RCTX_SOURCE_INFO;
209 ctx->table = NULL;
210 ctx->pending = SCM_NIL;
211 return ctx;
212 }
213
214 ScmReadContext *Scm_MakeReadContext(ScmReadContext *proto)
215 {
216 return make_read_context(proto? proto : Scm_CurrentReadContext());
217 }
218
219 static void read_context_print(ScmObj obj, ScmPort *port,
220 ScmWriteContext *ctx SCM_UNUSED)
221 {
222 Scm_Printf(port, "#<read-context %p>", obj);
223 }
224
225 SCM_DEFINE_BUILTIN_CLASS_SIMPLE(Scm_ReadContextClass, read_context_print);
226
227 /* public api */
228 int Scm_ReadContextLiteralImmutable(ScmReadContext *ctx)
229 {
230 return (ctx->flags & RCTX_LITERAL_IMMUTABLE);
231 }
232
233 /*----------------------------------------------------------------
234 * Error
235 */
236
237 void Scm_ReadError(ScmPort *port, const char *msg, ...)
238 {
239 ScmObj ostr = Scm_MakeOutputStringPort(TRUE);
240 ScmObj name = Scm_PortName(port);
241 ScmSize line = Scm_PortLine(port);
242
243 Scm_Printf(SCM_PORT(ostr), "Read error at %S:",
244 SCM_STRINGP(name)? name : SCM_OBJ(SCM_MAKE_STR("??")));
245 if (line >= 0) {
246 Scm_Printf(SCM_PORT(ostr), "line %d: ", line);
247 }
248 va_list ap;
249 va_start(ap, msg);
250 Scm_Vprintf(SCM_PORT(ostr), msg, ap, TRUE);
251 va_end(ap);
252
253 ScmObj rerr = Scm_MakeReadError(Scm_GetOutputString(SCM_PORT(ostr), 0),
254 port, line);
255 Scm_Raise(rerr, 0);
256 }
257
258 /*----------------------------------------------------------------
259 * Read reference
260 */
261
262 /* Read reference is a proxy object to for referenced object (#N=).
263 */
264
265 static void read_reference_print(ScmObj obj, ScmPort *port,
266 ScmWriteContext *ctx);
267 SCM_DEFINE_BUILTIN_CLASS_SIMPLE(Scm_ReadReferenceClass, read_reference_print);
268
269 ScmObj Scm_MakeReadReference(void)
270 {
271 ScmReadReference *a = SCM_NEW(ScmReadReference);
272 SCM_SET_CLASS(a, SCM_CLASS_READ_REFERENCE);
273 a->value = SCM_UNBOUND;
274 return SCM_OBJ(a);
275 }
276
277 static void read_reference_print(ScmObj obj SCM_UNUSED, ScmPort *port,
278 ScmWriteContext *ctx SCM_UNUSED)
279 {
280 Scm_Printf(port, "#<read-reference>");
281 }
282
283 static void ref_push(ScmReadContext *ctx, ScmObj obj, ScmObj finisher)
284 {
285 ctx->pending = Scm_Acons(obj, finisher, ctx->pending);
286 }
287
288 static ScmObj ref_val(ScmObj ref)
289 {
290 if (!SCM_READ_REFERENCE_REALIZED(ref)) {
291 Scm_Error("reader encontered unresolved read reference. Implementation error?");
292 }
293 return SCM_READ_REFERENCE(ref)->value;
294 }
295
296 static ScmObj ref_register(ScmReadContext *ctx, ScmObj obj, int refnum)
297 {
298 SCM_ASSERT(ctx->table);
299 Scm_HashTableSet(ctx->table, SCM_MAKE_INT(refnum), obj, 0);
300 return obj;
301 }
302
303 /* ctx->pending contains an assoc list of objects who contains read reference
304 which should be resolved.
305 The car of each entry is the object that needs to be fixed, and the
306 cdr of eacy entry may contain a finisher procedure (if the object is
307 created by read-time constructor.
308 */
309 static void read_context_flush(ScmReadContext *ctx)
310 {
311 ScmObj cp, ep;
312
313 SCM_FOR_EACH(cp, ctx->pending) {
314 ScmObj entry = SCM_CAR(cp);
315 SCM_ASSERT(SCM_PAIRP(entry));
316 ScmObj obj = SCM_CAR(entry);
317 ScmObj finisher = SCM_CDR(entry);
318
319 if (!SCM_FALSEP(finisher)) {
320 Scm_ApplyRec(finisher, SCM_LIST1(obj));
321 } else if (SCM_PAIRP(obj)) {
322 SCM_FOR_EACH(ep, obj) {
323 if (SCM_READ_REFERENCE_P(SCM_CAR(ep))) {
324 SCM_SET_CAR_UNCHECKED(ep, ref_val(SCM_CAR(ep)));
325 }
326 if (SCM_READ_REFERENCE_P(SCM_CDR(ep))) {
327 /* in case we have (... . #N#) */
328 SCM_SET_CDR_UNCHECKED(ep, ref_val(SCM_CDR(ep)));
329 break;
330 }
331 }
332 } else if (SCM_VECTORP(obj)) {
333 int i, len = SCM_VECTOR_SIZE(obj);
334 for (i=0; i<len; i++) {
335 ep = SCM_VECTOR_ELEMENT(obj, i);
336 if (SCM_READ_REFERENCE_P(ep)) {
337 SCM_VECTOR_ELEMENTS(obj)[i] = ref_val(ep);
338 }
339 }
340 } else {
341 Scm_Error("read_context_flush: recursive reference only supported with vector and lists");
342 }
343 }
344 }
345
346 /*----------------------------------------------------------------
347 * Miscellaneous routines
348 */
349
350 /* Table of initial 128 bytes of ASCII characters to dispatch for
351 special meanings.
352 bit 0 : a valid constituent char of words
353 bit 1 : candidate of case folding
354 */
355 static const unsigned char ctypes[] = {
356 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
357 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
358 /* ! " # $ % & ' ( ) * + , - . / */
359 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1,
360 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
361 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
362 /* @ A B C D E F G H I J K L M N O */
363 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
364 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
365 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 1, 1,
366 /* ` a b c d e f g h i j k l m n o */
367 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
368 /* p q r s t u v w x y z { | } ~ ^? */
369 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0,
370 };
371
372 /* For characters >= 0x80, we follow R[67]RS. */
373 inline static int char_word_constituent(int c, int include_hash_sign)
374 {
375 if (c < 128) {
376 return ((c >= 0 && (ctypes[(unsigned char)c]&1))
377 || (c == '#' && include_hash_sign));
378 } else if (c == 0x200c || c == 0x200d) {
379 return TRUE;
380 } else {
381 switch (Scm_CharGeneralCategory(c)) {
382 case SCM_CHAR_CATEGORY_Lu:
383 case SCM_CHAR_CATEGORY_Ll:
384 case SCM_CHAR_CATEGORY_Lt:
385 case SCM_CHAR_CATEGORY_Lm:
386 case SCM_CHAR_CATEGORY_Lo:
387 case SCM_CHAR_CATEGORY_Mn:
388 case SCM_CHAR_CATEGORY_Nl:
389 case SCM_CHAR_CATEGORY_No:
390 case SCM_CHAR_CATEGORY_Pd:
391 case SCM_CHAR_CATEGORY_Pc:
392 case SCM_CHAR_CATEGORY_Po:
393 case SCM_CHAR_CATEGORY_Sc:
394 case SCM_CHAR_CATEGORY_Sm:
395 case SCM_CHAR_CATEGORY_Sk:
396 case SCM_CHAR_CATEGORY_So:
397 case SCM_CHAR_CATEGORY_Co: return TRUE;
398 default: return FALSE;
399 }
400 }
401 }
402
403 inline static int char_word_case_fold(int c)
404 {
405 /*NB: Should we adopt full-fledged case folding for #!fold-case mode?
406 It seems overkill to me. For now, we only handles ASCII range.*/
407 return (c >= 0 && c < 128 && (ctypes[(unsigned char)c]&2));
408 }
409
410 /* For external API
411 * We may use flags for include_hash_sign in future.
412 */
413 int Scm_CharWordConstituent(ScmChar ch, u_long flags SCM_UNUSED)
414 {
415 return char_word_constituent((int)ch, FALSE);
416 }
417
418 /* R7RS 7.1.1 <delimiter> */
419 static int char_is_delimiter(ScmChar ch)
420 {
421 return ((ch < 0x80 && strchr("()\";| \t\n\r", ch) != NULL)
422 || SCM_CHAR_EXTRA_WHITESPACE(ch));
423 }
424
425 static void read_nested_comment(ScmPort *port,
426 ScmReadContext *ctx SCM_UNUSED)
427 {
428 int nesting = 0;
429 ScmSize line = Scm_PortLine(port);
430
431 for (ScmChar c = Scm_GetcUnsafe(port);;) {
432 switch (c) {
433 case '#': {
434 ScmChar c1 = Scm_GetcUnsafe(port);
435 if (c1 == '|') { nesting++; break; }
436 else if (c1 == EOF) goto eof;
437 else c = c1;
438 continue;
439 }
440 case '|': {
441 ScmChar c1 = Scm_GetcUnsafe(port);
442 if (c1 == '#') {
443 if (nesting-- == 0) {
444 return;
445 }
446 break;
447 }
448 else if (c1 == EOF) goto eof;
449 else c = c1;
450 continue;
451 }
452 case EOF:
453 eof:
454 Scm_ReadError(port, "encountered EOF inside nested multi-line comment (comment begins at line %d)", line);
455 default:
456 break;
457 }
458 c = Scm_GetcUnsafe(port);
459 }
460 }
461
462 static void read_comment(ScmPort *port) /* leading semicolon is already read */
463 {
464 for (;;) {
465 /* NB: comment may contain unexpected character code.
466 for the safety, we read bytes here. */
467 int c = Scm_GetbUnsafe(port);
468 if (c == '\n' || c == EOF) break;
469 }
470 }
471
472 static int skipws(ScmPort *port, ScmReadContext *ctx SCM_UNUSED)
473 {
474 for (;;) {
475 int c = Scm_GetcUnsafe(port);
476 if (c == EOF) return c;
477 if (c <= 127) {
478 if (isspace(c)) continue;
479 if (c == ';') {
480 read_comment(port);
481 continue;
482 }
483 return c;
484 }
485 else if (!SCM_CHAR_EXTRA_WHITESPACE(c)) return c;
486 }
487 }
488
489 static void reject_in_r7(ScmPort *port,
490 ScmReadContext *ctx SCM_UNUSED,
491 const char *token)
492 {
493 if (SCM_EQ(Scm_GetPortReaderLexicalMode(port), SCM_SYM_STRICT_R7)) {
494 Scm_ReadError(port,
495 "lexical syntax %s isn't allowed in strict R7RS mode",
496 token);
497 }
498 }
499
500 static ScmObj read_internal(ScmPort *port, ScmReadContext *ctx)
501 {
502 int c = skipws(port, ctx);
503 switch (c) {
504 case '(':
505 return read_list(port, ')', ctx);
506 case '"':
507 return read_string(port, FALSE, ctx);
508 case '#':
509 {
510 int c1 = Scm_GetcUnsafe(port);
511 switch (c1) {
512 case EOF:
513 Scm_ReadError(port, "premature #-sequence at EOF");
514 return SCM_UNDEFINED; /* dummy */
515 case 't':; case 'T': return read_sharp_word(port, 't', ctx);
516 case 'f':; case 'F': return read_sharp_word(port, 'f', ctx);
517 case 's':; case 'S': return read_sharp_word(port, 's', ctx);
518 case 'u':; case 'U': return read_sharp_word(port, 'u', ctx);
519 case 'c':; case 'C': return read_sharp_word(port, 'c', ctx);
520 case '(':
521 return read_vector(port, ')', ctx);
522 case '\\':
523 return read_char(port, ctx);
524 case 'x':; case 'X':; case 'o':; case 'O':;
525 case 'b':; case 'B':; case 'd':; case 'D':;
526 case 'e':; case 'E':; case 'i':; case 'I':;
527 Scm_UngetcUnsafe(c1, port);
528 return read_number(port, c, 0, ctx); /* let StringToNumber handle radix prefix */
529 case '!':
530 /* #! is either a script shebang or a reader directive */
531 return read_shebang(port, ctx);
532 case '"': {
533 /* #"..." - string interpolation */
534 reject_in_r7(port, ctx, "#\"...\"");
535 Scm_UngetcUnsafe(c1, port);
536 int line = Scm_PortLine(port);
537 ScmObj form = read_item(port, ctx);
538 return process_sharp_comma(port, line,
539 SCM_SYM_STRING_INTERPOLATE,
540 SCM_LIST4(form, SCM_FALSE,
541 Scm_PortName(port),
542 SCM_MAKE_INT(line)),
543 ctx, FALSE);
544 }
545 case '/':
546 /* #/.../ literal regexp */
547 reject_in_r7(port, ctx, "#/.../");
548 return read_regexp(port);
549 case '[':
550 /* #[...] literal charset */
551 reject_in_r7(port, ctx, "#[...]");
552 return read_charset(port);
553 case ',':
554 /* #,(form) - SRFI-10 read-time macro */
555 return read_sharp_comma(port, ctx);
556 case '|':
557 /* #| - block comment (SRFI-30)
558 it is equivalent to whitespace, so we return #<undef> */
559 read_nested_comment(port, ctx);
560 return SCM_UNDEFINED;
561 case '`': {
562 /* #`"..." - Legacy string interpolation syntax */
563 reject_in_r7(port, ctx, "#`\"...\"");
564 int line = Scm_PortLine(port);
565 ScmObj form = read_item(port, ctx);
566 return process_sharp_comma(port, line,
567 SCM_SYM_STRING_INTERPOLATE,
568 SCM_LIST4(form, SCM_TRUE,
569 Scm_PortName(port),
570 SCM_MAKE_INT(line)),
571 ctx, FALSE);
572 }
573 case '?': {
574 /* #? - debug directives */
575 reject_in_r7(port, ctx, "#?");
576 int c2 = Scm_GetcUnsafe(port);
577 switch (c2) {
578 case '=': {
579 /* #?=form - debug print */
580 ScmObj form = read_item(port, ctx);
581 return SCM_LIST2(SCM_SYM_DEBUG_PRINT, form);
582 }
583 case ',': {
584 /* #?,form - debug funcall */
585 ScmObj form = read_item(port, ctx);
586 return SCM_LIST2(SCM_SYM_DEBUG_FUNCALL, form);
587 }
588 case '?': {
589 /* #??= or #??, - conditinal debug stub */
590 ScmObj sym = SCM_FALSE;
591 int c3 = Scm_GetcUnsafe(port);
592 switch (c3) {
593 case '=': sym = SCM_SYM_DEBUG_PRINT_CONDITIONALLY; break;
594 case ',': sym = SCM_SYM_DEBUG_FUNCALL_CONDITIONALLY; break;
595 default:
596 Scm_ReadError(port, "unsupported #?-syntax: #??%C", c3);
597 return SCM_UNDEFINED; /* dummy */
598 }
599 ScmObj test = read_item(port, ctx);
600 ScmObj form = read_item(port, ctx);
601 return SCM_LIST3(sym, test, form);
602 }
603 case '@': {
604 int c3 = Scm_GetcUnsafe(port);
605 switch (c3) {
606 case '=': {
607 /* #?@=form - debug thread log */
608 ScmObj form = read_item(port, ctx);
609 return SCM_LIST2(SCM_SYM_DEBUG_THREAD_LOG, form);
610 }
611 default:
612 Scm_ReadError(port, "unsupported #?-syntax: #?@%C", c3);
613 return SCM_UNDEFINED; /* dummy */
614 }
615 }
616 case EOF:
617 return SCM_EOF;
618 default:
619 Scm_ReadError(port, "unsupported #?-syntax: #?%C", c2);
620 return SCM_UNDEFINED; /* dummy */
621 }
622 }
623 case '0': case '1': case '2': case '3': case '4':
624 case '5': case '6': case '7': case '8': case '9':
625 /* #N#, #N= or #Nr form */
626 return read_num_prefixed(port, c1, ctx);
627 case '*': {
628 reject_in_r7(port, ctx, "#*");
629 /* #**"...." byte string
630 #*01001001 for bit vector. */
631 return read_sharp_asterisk(port, ctx);
632 }
633 case ':': {
634 reject_in_r7(port, ctx, "#:");
635 /* #:name - uninterned symbol */
636 return read_immediate_symbol(port, Scm_GetcUnsafe(port),
637 FALSE, "#:", ctx);
638 }
639 case ';': {
640 /* #;expr - comment out sexpr */
641 int orig = ctx->flags;
642 ctx->flags |= RCTX_DISABLE_CTOR;
643 read_item(port, ctx); /* read and discard */
644 ctx->flags = orig;
645 return SCM_UNDEFINED; /* indicate this is a comment */
646 }
647 default:
648 Scm_ReadError(port, "unsupported #-syntax: #%C", c1);
649 return SCM_UNDEFINED; /* dummy */
650 }
651 }
652 case '\'': return read_quoted(port, SCM_SYM_QUOTE, ctx);
653 case '`': return read_quoted(port, SCM_SYM_QUASIQUOTE, ctx);
654 case ':':
655 /* Would be removed once we make keywords a subtype of symbols. */
656 if (SCM_EQ(Scm_GetPortReaderLexicalMode(port), SCM_SYM_STRICT_R7)) {
657 return read_symbol(port, c, ctx);
658 } else {
659 return read_keyword(port, ctx);
660 }
661 case ',':
662 {
663 int c1 = Scm_GetcUnsafe(port);
664 if (c1 == EOF) {
665 Scm_ReadError(port, "unterminated unquote");
666 return SCM_UNDEFINED; /* dummy */
667 } else if (c1 == '@') {
668 return read_quoted(port, SCM_SYM_UNQUOTE_SPLICING, ctx);
669 } else {
670 Scm_UngetcUnsafe(c1, port);
671 return read_quoted(port, SCM_SYM_UNQUOTE, ctx);
672 }
673 }
674 case '|':
675 return read_escaped_symbol(port, '|', TRUE, ctx);
676 case '[':
677 /* TODO: make it customizable */
678 reject_in_r7(port, ctx, "[]");
679 return read_list(port, ']', ctx);
680 case '{':
681 reject_in_r7(port, ctx, "{}");
682 return read_list(port, '}', ctx);
683 case '+':; case '-':
684 /* Note: R5RS doesn't permit identifiers beginning with '+' or '-',
685 but some Scheme programs use such identifiers. */
686 return read_symbol_or_number(port, c, ctx);
687 case '.':
688 {
689 int c1 = Scm_GetcUnsafe(port);
690 if (!char_word_constituent(c1, FALSE)) {
691 Scm_ReadError(port, "dot in wrong context");
692 }
693 Scm_UngetcUnsafe(c1, port);
694 return read_symbol_or_number(port, c, ctx);
695 }
696 case '0':; case '1':; case '2':; case '3':; case '4':;
697 case '5':; case '6':; case '7':; case '8':; case '9':;
698 /* Note: R5RS doesn't permit identifiers beginning with digits,
699 but some Scheme programs use such identifiers. */
700 return read_symbol_or_number(port, c, ctx);
701 case ')':; case ']':; case '}':;
702 Scm_ReadError(port, "extra close parenthesis `%c'", c);
703 return SCM_UNDEFINED; /* dummy */
704 case EOF:
705 return SCM_EOF;
706 default:
707 return read_symbol(port, c, ctx);
708 }
709 }
710
711 static ScmObj read_item(ScmPort *port, ScmReadContext *ctx)
712 {
713 for (;;) {
714 ScmObj obj = read_internal(port, ctx);
715 if (!SCM_UNDEFINEDP(obj)) return obj;
716 }
717 }
718
719 /*--------------------------------------------------------
720 * Common routine to handle hex-digit escape \xNN etc.
721 */
722 /* TODO: Redesign these for clearer and simpler API. */
723
724 /*
725 Hex-escape sequence can appear in the following places:
726
727 - character literals #\x1b
728 - string literals "\x1b;("
729 - symbol literals |\x1b;|
730 - character set literals #[\x1b;-\x1f;]
731 - regexp literals #/\x1b;+ /
732
733 Legacy Gauche supports fixed-digit syntax.
734
735 \xN{2} - char code up to 256 in _native encoding_
736 \uN{4} - unicode codepoint in BMP
737 \UN{8} - unicode codepoint
738
739 New (R7RS) syntax has variable number of digits, terminated by ';'
740
741 \xN{1,}; - unicode codepoint
742
743 There's an ambiguity in '\x' case - we first try to read it as R7RS syntax,
744 then falls back to legacy syntax, by default. Because of this backtrack,
745 the API is a bit complicated.
746
747 Scm_ReadXdigitsFromString reads from char* buffer, up to buflen octets.
748 The preceding backslash and a character 'x', 'u' or 'U' should already
749 be read. The character is passed to KEY. It also receives
750 ScmReaderLexicalMode.
751
752 Character literal doesn't take terminating ';'. TERMINATOR flag
753 indicates whether we expect terminator or not.
754
755 NEXTBUF is an output variable points to the next character on success.
756 Returns the retrieved decoded character, or SCM_CHAR_INVALID on error.
757
758 Scm_ReadXdigitsFromPort is a convenience routine on top of
759 Scm_ReadXdigitsFromString, when the caller is reading from a port.
760 Since we need look ahead arbitrary number of characters, we can't just
761 return a retrieved character. The procedure takes ScmDString to which
762 the read characters (the decoded character, plus remaining hexdigit
763 characters) are accumulated. When the escape syntax is invalid,
764 it returns #<string> of prefetched characters. On success, it returns
765 #t.
766 */
767
768 ScmChar Scm_ReadXdigitsFromString(const char *buf,
769 int buflen,
770 ScmChar key, /* x, u or U */
771 ScmObj mode, /* Reader lexical mode */
772 int terminator, /* TRUE expecting ';' */
773 const char **nextbuf)
774 {
775 int legacy_fallback = FALSE;
776
777 if (key == 'x' && !SCM_EQ(mode, SCM_SYM_LEGACY)) {
778 int val = 0, i;
779 int overflow = FALSE;
780 for (i=0; i<buflen; i++) {
781 if (isxdigit(buf[i])) {
782 val = val*16 + Scm_DigitToInt(buf[i], 16, FALSE);
783 if (val > SCM_CHAR_MAX) overflow = TRUE;
784 } else if (terminator && buf[i] == ';' && i > 0) {
785 /* R7RS syntax */
786 *nextbuf = buf+i+1;
787 return overflow? SCM_CHAR_INVALID : Scm_UcsToChar(val);
788 } else if (terminator && i < 2) {
789 return SCM_CHAR_INVALID;
790 } else {
791 break;
792 }
793 }
794 if (!terminator && i == buflen) {
795 *nextbuf = buf+i;
796 return overflow? SCM_CHAR_INVALID:Scm_UcsToChar(val);
797 }
798 /* Fallback to legacy syntax */
799 legacy_fallback = TRUE;
800 }
801 if (SCM_EQ(mode, SCM_SYM_STRICT_R7)) return SCM_CHAR_INVALID;
802 if (key == 'x' && SCM_EQ(mode, SCM_SYM_WARN_LEGACY)) {
803 Scm_Warn("Legacy \\x hex-escape: \\x%c%c", buf[0], buf[1]);
804 }
805
806 {
807 int val = 0;
808 int ndigits = (key == 'u')? 4 : (key == 'x')? 2 : 8;
809 if (ndigits > buflen) return SCM_CHAR_INVALID;
810 for (int i=0; i<ndigits; i++) {
811 if (!isxdigit(buf[i])) return SCM_CHAR_INVALID;
812 val = val * 16 + Scm_DigitToInt(buf[i], 16, FALSE);
813 if (val > SCM_CHAR_MAX) return SCM_CHAR_INVALID;
814 }
815 *nextbuf = buf + ndigits;
816 if (!legacy_fallback && key != 'x') val = Scm_UcsToChar(val);
817 return val;
818 }
819 }
820
821 /* On success, parsed char and other prefetched hexdigits are added
822 to BUF and returns #t. Otherwise the prefetched string is returned. */
823 ScmObj Scm_ReadXdigitsFromPort(ScmPort *port, int key, ScmObj mode,
824 int incompletep, ScmDString *buf)
825 {
826 ScmDString ds;
827
828 Scm_DStringInit(&ds);
829 for (;;) {
830 int ch = Scm_GetcUnsafe(port);
831 if (ch == ';') {
832 Scm_DStringPutc(&ds, ch);
833 break;
834 }
835 if (ch == EOF || ch >= 0x80 || !isxdigit(ch)) {
836 Scm_UngetcUnsafe(ch, port);
837 break;
838 }
839 Scm_DStringPutc(&ds, ch);
840 }
841
842 ScmSmallInt numchars;
843 const char *chars = Scm_DStringPeek(&ds, &numchars, NULL);
844 const char *next;
845
846 int r = Scm_ReadXdigitsFromString(chars, numchars, key, mode, TRUE, &next);
847 if (r != SCM_CHAR_INVALID) {
848 if (incompletep) Scm_DStringPutb(buf, r);
849 else Scm_DStringPutc(buf, r);
850 if (next - chars < numchars) {
851 Scm_DStringPutz(buf, next, numchars - (next-chars));
852 }
853 return SCM_TRUE;
854 } else {
855 return Scm_MakeString(chars, numchars, -1, SCM_STRING_COPYING);
856 }
857 }
858
859 /* Another utility procedure. Read sequence of digits from PORT and made it
860 long and return. CH can be prefetched digit char; if you don't
861 have one, pass SCM_CHAR_INVALID. The first nondigit char read
862 is stored in NEXT, and number of digits read is stored in NUMREAD.
863 If the number overflows LONG_MAX or there's no digits to be read,
864 returns -1 (you can check NUMREAD to see which is the case).
865 NB: This may throw an error if ch is neither a valid digit char
866 nor SCM_CHAR_INVALID. */
867 /* NB: Call for refactoring, using Scm_ParseDigitsAsLong */
868 long Scm_ReadDigitsAsLong(ScmPort *port, ScmChar ch, int radix,
869 ScmChar *next, /*out*/
870 ScmSize *numread /*out*/)
871 {
872 long val = 0;
873 ScmSize nchars = 0;
874 if (ch != SCM_CHAR_INVALID) {
875 int v = Scm_DigitToInt(ch, radix, FALSE);
876 if (v < 0) {
877 /* This is for safety net. The caller should filter out
878 this case. */
879 Scm_ReadError(port, "Digit char expected, but got %C", ch);
880 }
881 val = v;
882 }
883 for (;;) {
884 ch = Scm_Getc(port);
885 nchars++;
886 int v = Scm_DigitToInt(ch, radix, FALSE);
887 if (v < 0) { /* EOF case is covered here */
888 *next = ch;
889 *numread = nchars;
890 return val;
891 }
892 if (val >= (LONG_MAX/radix+1)) {
893 /* we'll overflow */
894 *next = ch;
895 *numread = nchars;
896 return -1;
897 }
898 val = val*radix+v;
899 }
900 /* NOTREACHED */
901 }
902
903 /* Read long digits from BUF up to LEN. BUF must only contain single-byte
904 chars. */
905 long Scm_ParseDigitsAsLong(const char *buf, ScmSize len, int radix,
906 ScmSize *numread) /*out*/
907 {
908 long val = 0;
909 ScmSize nchars = 0;
910 for (; nchars < len; nchars++, buf++) {
911 int v = Scm_DigitToInt((ScmChar)*buf, radix, FALSE);
912 if (v < 0) {
913 *numread = nchars;
914 return val;
915 }
916 if (val >= (LONG_MAX/radix+1)) {
917 /* we'll overflow */
918 *numread = nchars;
919 return -1;
920 }
921 val = val*radix+v;
922 }
923 *numread = nchars;
924 return (nchars == 0)? -1 : val;
925 }
926
927 /*----------------------------------------------------------------
928 * List
929 */
930
931 /* Internal read_list. returns whether the list contains unresolved
932 reference or not within the flag has_ref */
933 static ScmObj read_list_int(ScmPort *port, ScmChar closer,
934 ScmReadContext *ctx, int *has_ref, int start_line)
935 {
936 ScmObj start = SCM_NIL, last = SCM_NIL;
937 int dot_seen = FALSE, ref_seen = FALSE;
938
939 for (;;) {
940 int c = skipws(port, ctx);
941 if (c == EOF) goto eoferr;
942 if (c == closer) {
943 *has_ref = !!ref_seen;
944 return start;
945 }
946
947 /* NB: We need to keep reading after we see the dotted pair, since
948 there can be a case like (a . b #;c), which is valid syntax.
949 We only reject when we see a next item after reading dotted pair. */
950 ScmObj item;
951 if (c == '.') {
952 if (dot_seen) goto baddot;
953 int c2 = Scm_GetcUnsafe(port);
954 if (c2 == closer) {
955 goto baddot;
956 } else if (c2 == EOF) {
957 goto eoferr;
958 } else if (!char_word_constituent(c2, FALSE)) {
959 /* can be a dot pair at the end */
960 if (start == SCM_NIL) goto baddot;
961 Scm_UngetcUnsafe(c2, port);
962 item = read_item(port, ctx);
963 if (SCM_READ_REFERENCE_P(item)) ref_seen = TRUE;
964 SCM_SET_CDR_UNCHECKED(last, item);
965 dot_seen = TRUE;
966 continue;
967 }
968 Scm_UngetcUnsafe(c2, port);
969 item = read_symbol_or_number(port, c, ctx);
970 } else {
971 Scm_UngetcUnsafe(c, port);
972 item = read_internal(port, ctx);
973 if (SCM_UNDEFINEDP(item)) continue; /* it was just a comment */
974 if (dot_seen) goto baddot;
975 if (SCM_READ_REFERENCE_P(item)) ref_seen = TRUE;
976 }
977 SCM_APPEND1(start, last, item);
978 }
979 eoferr:
980 if (start_line >= 0) {
981 Scm_ReadError(port, "EOF inside a list (starting from line %d)",
982 start_line);
983 } else {
984 Scm_ReadError(port, "EOF inside a list");
985 }
986 baddot:
987 Scm_ReadError(port, "bad dot syntax");
988 return SCM_NIL; /* dummy */
989 }
990
991 static ScmObj read_list(ScmPort *port, ScmChar closer, ScmReadContext *ctx)
992 {
993 int has_ref;
994 ScmSize line = -1;
995
996 if (ctx->flags & RCTX_SOURCE_INFO) line = Scm_PortLine(port);
997
998 ScmObj r = read_list_int(port, closer, ctx, &has_ref, line);
999
1000 if (SCM_PAIRP(r) && (ctx->flags & RCTX_SOURCE_INFO) && line >= 0) {
1001 /* Swap the head of the list for an extended pair to record
1002 source-code info.*/
1003 r = Scm_ExtendedCons(SCM_CAR(r), SCM_CDR(r));
1004 Scm_PairAttrSet(SCM_PAIR(r), SCM_SYM_SOURCE_INFO,
1005 SCM_LIST2(Scm_PortName(port), SCM_MAKE_INT(line)));
1006 }
1007
1008 if (has_ref) ref_push(ctx, r, SCM_FALSE);
1009 return r;
1010 }
1011
1012 static ScmObj read_vector(ScmPort *port, ScmChar closer, ScmReadContext *ctx)
1013 {
1014 int has_ref;
1015 ScmSize line = -1;
1016 ScmObj r;
1017
1018 if (ctx->flags & RCTX_SOURCE_INFO) line = Scm_PortLine(port);
1019 r = read_list_int(port, closer, ctx, &has_ref, line);
1020 r = Scm_ListToVector(r, 0, -1);
1021 if (has_ref) ref_push(ctx, r, SCM_FALSE);
1022 return r;
1023 }
1024
1025 static ScmObj read_quoted(ScmPort *port, ScmObj quoter, ScmReadContext *ctx)
1026 {
1027 ScmSize line = -1;
1028 ScmObj r;
1029
1030 if (ctx->flags & RCTX_SOURCE_INFO) line = Scm_PortLine(port);
1031 ScmObj item = read_item(port, ctx);
1032 if (SCM_EOFP(item)) Scm_ReadError(port, "unterminated quote");
1033 if (line >= 0) {
1034 r = Scm_ExtendedCons(quoter, Scm_Cons(item, SCM_NIL));
1035 Scm_PairAttrSet(SCM_PAIR(r), SCM_SYM_SOURCE_INFO,
1036 SCM_LIST2(Scm_PortName(port), SCM_MAKE_INT(line)));
1037 } else {
1038 r = Scm_Cons(quoter, Scm_Cons(item, SCM_NIL));
1039 }
1040 if (SCM_READ_REFERENCE_P(item)) ref_push(ctx, SCM_CDR(r), SCM_FALSE);
1041 return r;
1042 }
1043
1044 /*----------------------------------------------------------------
1045 * String
1046 */
1047
1048 /* Utility API to read string-literal like thingy.
1049 * The caller already read and consumed the opening double-quote.
1050 *
1051 * FLAGS are ScmStringFlags. Only SCM_STRING_INCOMPLETE is recognized.
1052 */
1053 ScmObj Scm_ReadStringLiteral(ScmPort *port,
1054 ScmReadContext *ctx, /* can be NULL */
1055 u_long flags,
1056 ScmChar terminator SCM_UNUSED) /* reserved */
1057 {
1058 int incompletep = flags & SCM_STRING_INCOMPLETE;
1059 return read_string(port, incompletep, ctx);
1060 }
1061
1062 /* Handling \xNN;, \uNNNN, \UNNNNNNNN escapes. To make it easier
1063 to support both legacy \xN{2} syntax and new \xN{1,} syntax,
1064 we read-ahead as many hexdigits as possible first, then parse it,
1065 and append the result to BUF, along with any unused digits. */
1066 static void read_string_xdigits(ScmPort *port, int key,
1067 int incompletep, ScmDString *buf)
1068 {
1069 ScmObj bad = Scm_ReadXdigitsFromPort(port, key,
1070 Scm_GetPortReaderLexicalMode(port),
1071 incompletep, buf);
1072 if (SCM_STRINGP(bad)) {
1073 /* skip chars to the end of string, so that the reader will read
1074 after the erroneous string */
1075 for (;;) {
1076 int c;
1077 if (incompletep) c = Scm_GetbUnsafe(port);
1078 else c = Scm_GetcUnsafe(port);
1079 if (c == EOF || c == '"') break;
1080 if (c == '\\') {
1081 if (incompletep) c = Scm_GetbUnsafe(port);
1082 else c = Scm_GetcUnsafe(port);
1083 }
1084 }
1085 Scm_ReadError(port,
1086 "Bad \\%c escape sequence in a string literal: `\\%c%A'",
1087 key, key, bad);
1088 }
1089 }
1090
1091 static ScmObj read_string(ScmPort *port, int incompletep,
1092 ScmReadContext *ctx SCM_UNUSED)
1093 {
1094 int c = 0;
1095 ScmDString ds;
1096 Scm_DStringInit(&ds);
1097
1098 #define FETCH(var) \
1099 if (incompletep) { var = Scm_GetbUnsafe(port); } \
1100 else { var = Scm_GetcUnsafe(port); }
1101 #define ACCUMULATE(var) \
1102 if (incompletep) { SCM_DSTRING_PUTB(&ds, var); } \
1103 else { SCM_DSTRING_PUTC(&ds, var); }
1104 #define INTRALINE_WS(var) \
1105 ((var)==' ' || (var)=='\t' || SCM_CHAR_EXTRA_WHITESPACE_INTRALINE(var))
1106
1107 for (;;) {
1108 FETCH(c);
1109 switch (c) {
1110 case EOF: goto eof_exit;
1111 case '"': goto finish;
1112 backslash:
1113 case '\\': {
1114 FETCH(c);
1115 switch (c) {
1116 case EOF: goto eof_exit;
1117 case 'n': ACCUMULATE('\n'); break;
1118 case 'r': ACCUMULATE('\r'); break;
1119 case 'f': ACCUMULATE('\f'); break;
1120 case 't': ACCUMULATE('\t'); break;
1121 case 'a': ACCUMULATE(0x07); break; /* alarm */
1122 case 'b': ACCUMULATE(0x08); break; /* backspace */
1123 case '\\': ACCUMULATE('\\'); break;
1124 case '0': ACCUMULATE(0); break;
1125 case 'x': {
1126 read_string_xdigits(port, 'x', incompletep, &ds);
1127 break;
1128 }
1129 case 'u': case 'U': {
1130 if (SCM_EQ(Scm_GetPortReaderLexicalMode(port),
1131 SCM_SYM_STRICT_R7)) {
1132 Scm_ReadError(port, "\\%c in string literal isn't allowed "
1133 "in strinct-r7rs mode", c);
1134 }
1135 read_string_xdigits(port, c, incompletep, &ds);
1136 break;
1137 }
1138 /* R6RS-style line continuation handling*/
1139 line_continuation:
1140 case ' ':
1141 case '\t': {
1142 for (;;) {
1143 FETCH(c);
1144 if (c == EOF) goto eof_exit;
1145 if (c == '\r') goto cont_cr;
1146 if (c == '\n') goto cont_lf;
1147 if (!INTRALINE_WS(c)) goto cont_err;
1148 }
1149 }
1150 /*FALLTHROUGH*/
1151 cont_cr:
1152 case '\r': {
1153 FETCH(c);
1154 if (c == EOF) goto eof_exit;
1155 if (c == '"') goto finish;
1156 if (c == '\\') goto backslash;
1157 if (c != '\n' && !INTRALINE_WS(c)) {
1158 ACCUMULATE(c);
1159 break;
1160 }
1161 }
1162 /*FALLTHROUGH*/
1163 cont_lf:
1164 case '\n': {
1165 for (;;) {
1166 FETCH(c);
1167 if (c == EOF) goto eof_exit;
1168 if (c == '"') goto finish;
1169 if (c == '\\') goto backslash;
1170 if (!INTRALINE_WS(c)) {
1171 ACCUMULATE(c);
1172 break;
1173 }
1174 }
1175 break;
1176 }
1177 default:
1178 if (SCM_CHAR_EXTRA_WHITESPACE_INTRALINE(c)) goto line_continuation;
1179 else ACCUMULATE(c);
1180 }
1181 break;
1182 }
1183 default: ACCUMULATE(c); break;
1184 }
1185 }
1186 eof_exit:
1187 Scm_ReadError(port, "EOF encountered in a string literal: %S",
1188 Scm_DStringGet(&ds, 0));
1189 cont_err:
1190 Scm_ReadError(port, "Invalid line continuation sequence in a string literal: %S...",
1191 Scm_DStringGet(&ds, 0));
1192 finish:;
1193 int flags = ((incompletep? SCM_STRING_INCOMPLETE:0) | SCM_STRING_IMMUTABLE);
1194 return Scm_DStringGet(&ds, flags);
1195 }
1196
1197 /*----------------------------------------------------------------
1198 * Character
1199 */
1200
1201 static struct char_name {
1202 const char *name;
1203 u_int size;
1204 ScmObj ch;
1205 } char_names[] = {
1206 #define DEFCHAR(name, char) \
1207 { #name, sizeof(#name)-1, SCM_MAKE_CHAR(char) }
1208 DEFCHAR(alarm, 0x07),
1209 DEFCHAR(backspace, 0x08),
1210 DEFCHAR(space, ' '),
1211 DEFCHAR(newline, '\n'),
1212 DEFCHAR(nl, '\n'),
1213 DEFCHAR(lf, '\n'),
1214 DEFCHAR(return, '\r'),
1215 DEFCHAR(cr, '\r'),
1216 DEFCHAR(tab, '\t'),
1217 DEFCHAR(ht, '\t'),
1218 DEFCHAR(page, '\f'),
1219 DEFCHAR(escape, 0x1b),
1220 DEFCHAR(esc, 0x1b),
1221 DEFCHAR(delete, 0x7f),
1222 DEFCHAR(del, 0x7f),
1223 DEFCHAR(null, 0),
1224 { NULL, 0, 0 }
1225 };
1226
1227 static ScmObj read_char(ScmPort *port, ScmReadContext *ctx)
1228 {
1229 ScmString *name;
1230
1231 int c = Scm_GetcUnsafe(port);
1232 switch (c) {
1233 case EOF:
1234 Scm_ReadError(port, "EOF encountered in character literal");
1235 break;
1236 case '(':; case ')':; case '[':; case ']':; case '{':; case '}':;
1237 case '"':; case ' ':; case '\\':; case '|':; case ';':;
1238 case '#':;
1239 return SCM_MAKE_CHAR(c);
1240 default: {
1241 /* need to read word to see if it is a character name */
1242 name = SCM_STRING(read_word(port, c, ctx, TRUE, FALSE));
1243
1244 if (SCM_EQ(Scm_GetPortReaderLexicalMode(port), SCM_SYM_STRICT_R7)) {
1245 ScmChar following = Scm_GetcUnsafe(port);
1246 if (!char_is_delimiter(following)) {
1247 Scm_Error("Character literal isn't delimited: #\\%A%C ...",
1248 name, following);
1249 }
1250 Scm_UngetcUnsafe(following, port);
1251 }
1252
1253 ScmSmallInt namelen, namesize;
1254 const char *cname = Scm_GetStringContent(name, &namesize, &namelen,
1255 NULL);
1256 if (namelen == 1) {
1257 return SCM_MAKE_CHAR(c);
1258 }
1259 if (namelen != namesize) {
1260 /* no character name contains multibyte chars */
1261
1262 goto unknown;
1263 }
1264
1265 ScmObj lexmode = Scm_GetPortReaderLexicalMode(port);
1266
1267 /* handle #\x1f etc. */
1268 if (cname[0] == 'x' && isxdigit(cname[1])) {
1269 const char *nextptr;
1270 ScmChar code = Scm_ReadXdigitsFromString(cname+1, namesize-1, 'x',
1271 lexmode, FALSE, &nextptr);
1272 if (code == SCM_CHAR_INVALID || *nextptr != '\0') goto unknown;
1273 return SCM_MAKE_CHAR(code);
1274 }
1275 /* handle legacy #\uxxxx or #\uxxxxxxxx */
1276 if ((cname[0] == 'u') && isxdigit(cname[1])
1277 && (!SCM_EQ(lexmode, SCM_SYM_STRICT_R7))) {
1278 if (namesize >= 5 && namesize <= 9) {
1279 const char *nextptr;
1280 /* NB: We want to allow variable number of digits, so
1281 we pass 'x' as key (instead of 'u') here. */
1282 ScmChar code = Scm_ReadXdigitsFromString(cname+1, namesize-1,
1283 'x', lexmode, FALSE,
1284 &nextptr);
1285 if (code >= 0 && *nextptr == '\0') {
1286 return SCM_MAKE_CHAR(code);
1287 }
1288 }
1289 /* if we come here, it's an error. */
1290 Scm_ReadError(port, "Bad UCS character code: #\\%s", cname);
1291 }
1292
1293 struct char_name *cntab = char_names;
1294 while (cntab->name) {
1295 if (cntab->size == (unsigned)namesize
1296 && strncmp(cntab->name, cname, namesize) == 0) {
1297 return cntab->ch;
1298 }
1299 cntab++;
1300 }
1301 unknown:
1302 Scm_ReadError(port, "Unknown character name: #\\%A", name);
1303 }
1304 }
1305 return SCM_UNDEFINED; /* dummy */
1306 }
1307
1308 /*----------------------------------------------------------------
1309 * Symbols and Numbers
1310 */
1311
1312 /* Reads a sequence of word-constituent characters from PORT, and returns
1313 ScmString. INITIAL may be a readahead character, or SCM_CHAR_INVALID
1314 if there's none. TEMP_CASE_FOLD turns on case-fold mode regardless of
1315 the read context setting. INCLUDE_HASH_SIGN allows '#' to appear in
1316 the word.
1317 */
1318 static ScmObj read_word(ScmPort *port, ScmChar initial,
1319 ScmReadContext *ctx SCM_UNUSED,
1320 int temp_case_fold, int include_hash_sign)
1321 {
1322 int case_fold = temp_case_fold || SCM_PORT_CASE_FOLDING(port);
1323 ScmDString ds;
1324 Scm_DStringInit(&ds);
1325 if (initial != SCM_CHAR_INVALID) {
1326 if (case_fold && char_word_case_fold(initial)) initial = tolower(initial);
1327 SCM_DSTRING_PUTC(&ds, initial);
1328 }
1329
1330 for (;;) {
1331 int c = Scm_GetcUnsafe(port);
1332 if (c == EOF || !char_word_constituent(c, include_hash_sign)) {
1333 Scm_UngetcUnsafe(c, port);
1334 return Scm_DStringGet(&ds, 0);
1335 }
1336 if (case_fold && char_word_case_fold(c)) c = tolower(c);
1337 SCM_DSTRING_PUTC(&ds, c);
1338 }
1339 }
1340
1341 /* Kaveat: We don't allow '#' in symbols, but we need to read '#'
1342 for numbers. To allow weird identifers like '1+', we need to read the
1343 word as a number fist and convert it to a symbol when the read word
1344 can't be interpreted as a number. For the consistency, we read
1345 with '#' and then check it in read_symbol, too. */
1346 static void check_valid_symbol(ScmString *s)
1347 {
1348 ScmObj r = Scm_StringScanChar(s, SCM_CHAR('#'), SCM_STRING_SCAN_INDEX);
1349 if (!SCM_FALSEP(r)) {
1350 Scm_Error("invalid symbol name: %S", SCM_OBJ(s));
1351 }
1352 }
1353
1354 /* Read a symbol starting with INITIAL (assuming unescaped), interned. */
1355 static ScmObj read_symbol(ScmPort *port, ScmChar initial, ScmReadContext *ctx)
1356 {
1357 ScmString *s = SCM_STRING(read_word(port, initial, ctx, FALSE, TRUE));
1358 check_valid_symbol(s);
1359 return Scm_Intern(s);
1360 }
1361
1362 /* This is called when a symbol (either bare or escaped) is expected
1363 starting with INITIAL char; e.g. after #: or #!. An error is thrown
1364 if a symbol isn't immediately followed. PRECEDING is used in the
1365 error message. */
1366 static ScmObj read_immediate_symbol(ScmPort *port, ScmChar initial,
1367 int interned, const char *preceding,
1368 ScmReadContext *ctx)
1369 {
1370 if (initial == '|') {
1371 return read_escaped_symbol(port, initial, interned, ctx);
1372 } else if (char_word_constituent(initial, FALSE)) {
1373 ScmString *s = SCM_STRING(read_word(port, initial, ctx, FALSE, TRUE));
1374 check_valid_symbol(s);
1375 /* we have to exclude numbers. a bit ugly - call for cleanup */
1376 if (!(isdigit(initial) || initial == '+' || initial == '-')
1377 || SCM_FALSEP(Scm_StringToNumber(s, 10, 0))) {
1378 return Scm_MakeSymbol(s, interned);
1379 }
1380 }
1381 /* If we come here, we have invalid syntax. */
1382 if (initial == EOF) {
1383 Scm_ReadError(port, "'%s' followed by nothing", preceding);
1384 } else {
1385 Scm_ReadError(port, "invalid %s syntax near '%s%C'", preceding,
1386 preceding, initial);
1387 }
1388 return SCM_UNDEFINED; /* dummy */
1389 }
1390
1391 static ScmObj read_number(ScmPort *port, ScmChar initial, int radix,
1392 ScmReadContext *ctx)
1393 {
1394 ScmString *s = SCM_STRING(read_word(port, initial, ctx, FALSE, TRUE));
1395 u_long flags = radix >=2 ? SCM_NUMBER_FORMAT_ALT_RADIX : 0;
1396 if (SCM_EQ(Scm_GetPortReaderLexicalMode(port), SCM_SYM_STRICT_R7)) {
1397 flags |= SCM_NUMBER_FORMAT_STRICT_R7RS;
1398 }
1399 int default_radix = radix >= 2? radix : 10;
1400 ScmObj num = Scm_StringToNumber(s, default_radix, flags);
1401 if (num == SCM_FALSE) {
1402 if (radix >= 2) {
1403 /* In this case, we've read #<radix>r syntax */
1404 Scm_ReadError(port, "bad numeric format: \"#%dr%A\"", radix, s);
1405 } else {
1406 Scm_ReadError(port, "bad numeric format: %S", s);
1407 }
1408 }
1409 return num;
1410 }
1411
1412 static ScmObj read_symbol_or_number(ScmPort *port, ScmChar initial, ScmReadContext *ctx)
1413 {
1414 ScmString *s = SCM_STRING(read_word(port, initial, ctx, FALSE, TRUE));
1415 u_long flags = 0;
1416 if (SCM_EQ(Scm_GetPortReaderLexicalMode(port), SCM_SYM_STRICT_R7)) {
1417 flags |= SCM_NUMBER_FORMAT_STRICT_R7RS;
1418 }
1419 ScmObj num = Scm_StringToNumber(s, 10, flags);
1420 if (num != SCM_FALSE) return num;
1421 check_valid_symbol(s);
1422 return Scm_Intern(s);
1423 }
1424
1425 static ScmObj read_keyword(ScmPort *port, ScmReadContext *ctx)
1426 {
1427 int c2 = Scm_GetcUnsafe(port);
1428
1429 if (c2 == '|') {
1430 ScmObj name = read_escaped_symbol(port, c2, FALSE, ctx); /* read as uninterned */
1431 return Scm_MakeKeyword(SCM_SYMBOL_NAME(name));
1432 } else {
1433 Scm_UngetcUnsafe(c2, port);
1434 ScmObj name = read_word(port, SCM_CHAR_INVALID, ctx, FALSE, FALSE);
1435 return Scm_MakeKeyword(SCM_STRING(name));
1436 }
1437 }
1438
1439 static ScmObj read_escaped_symbol(ScmPort *port, ScmChar delim, int interned,
1440 ScmReadContext *ctx SCM_UNUSED)
1441 {
1442 ScmDString ds;
1443 Scm_DStringInit(&ds);
1444 ScmObj xmode = Scm_GetPortReaderLexicalMode(port);
1445
1446 for (;;) {
1447 int c = Scm_GetcUnsafe(port);
1448 if (c == EOF) {
1449 goto err;
1450 } else if (c == delim) {
1451 ScmString *s = SCM_STRING(Scm_DStringGet(&ds, 0));
1452 return Scm_MakeSymbol(s, interned);
1453 } else if (c == '\\') {
1454 /* CL-style single escape */
1455 c = Scm_GetcUnsafe(port);
1456 if (c == EOF) goto err;
1457 if (SCM_EQ(xmode, SCM_SYM_LEGACY)) {
1458 SCM_DSTRING_PUTC(&ds, c);
1459 } else {
1460 switch (c) {
1461 case 'x': {
1462 /* R7RS-style hex escape. */
1463 ScmObj bad = Scm_ReadXdigitsFromPort(port, 'x', xmode,
1464 FALSE, &ds);
1465 if (SCM_STRINGP(bad)) {
1466 Scm_ReadError(port, "invalid hex escape in a symbol literal: \\x%C", bad);
1467 }
1468 break;
1469 }
1470 case '\\': case '|': SCM_DSTRING_PUTC(&ds, c); break;
1471 case 'a': SCM_DSTRING_PUTC(&ds, '\a'); break;
1472 case 'b': SCM_DSTRING_PUTC(&ds, '\b'); break;
1473 case 't': SCM_DSTRING_PUTC(&ds, '\t'); break;
1474 case 'n': SCM_DSTRING_PUTC(&ds, '\n'); break;
1475 case 'r': SCM_DSTRING_PUTC(&ds, '\r'); break;
1476 default:
1477 if (SCM_EQ(xmode, SCM_SYM_STRICT_R7)) {
1478 Scm_ReadError(port, "invalid backslash-escape in a symbol literal: \\%A", SCM_MAKE_CHAR(c));
1479 } else {
1480 SCM_DSTRING_PUTC(&ds, c);
1481 }
1482 }
1483 }
1484 } else {
1485 SCM_DSTRING_PUTC(&ds, c);
1486 }
1487 }
1488 err:
1489 Scm_ReadError(port, "unterminated escaped symbol: |%s ...",
1490 Scm_DStringGetz(&ds));
1491 return SCM_UNDEFINED; /* dummy */
1492 }
1493
1494 /*----------------------------------------------------------------
1495 * Regexp & charset
1496 */
1497
1498 /* gauche extension : #/regexp/ */
1499 static ScmObj read_regexp(ScmPort *port)
1500 {
1501 ScmDString ds;
1502 Scm_DStringInit(&ds);
1503 for (;;) {
1504 ScmChar c = Scm_GetcUnsafe(port);
1505 if (c == SCM_CHAR_INVALID) {
1506 Scm_ReadError(port, "unterminated literal regexp");
1507 }
1508 if (c == '\\') {
1509 /* NB: We "eat" a backslash before '/', since it is only dealt
1510 with the reader and nothing to do with regexp parser itself. */
1511 ScmChar c1 = Scm_GetcUnsafe(port);
1512 if (c1 == SCM_CHAR_INVALID) {
1513 Scm_ReadError(port, "unterminated literal regexp");
1514 }
1515 if (c1 != '/') SCM_DSTRING_PUTC(&ds, c);
1516 SCM_DSTRING_PUTC(&ds, c1);
1517 } else if (c == '/') {
1518 /* Read one more char to see if we have a flag */
1519 int flags = 0;
1520 c = Scm_GetcUnsafe(port);
1521 if (c == 'i') flags |= SCM_REGEXP_CASE_FOLD;
1522 else Scm_UngetcUnsafe(c, port);
1523 return Scm_RegComp(SCM_STRING(Scm_DStringGet(&ds, 0)), flags);
1524 } else {
1525 SCM_DSTRING_PUTC(&ds, c);
1526 }
1527 }
1528 }
1529
1530 /* gauche extension : #[charset] */
1531 static ScmObj read_charset(ScmPort *port)
1532 {
1533 ScmCharSet *cs = SCM_CHAR_SET(Scm_CharSetRead(port, NULL, TRUE, FALSE));
1534 return Scm_CharSetFreezeX(cs); /* literal is immutable */
1535 }
1536
1537 /*----------------------------------------------------------------
1538 * Numeric prefixed (#N#, #N=, #Nr)
1539 */
1540
1541 static ScmObj read_num_prefixed(ScmPort *port, ScmChar ch, ScmReadContext *ctx)
1542 {
1543 ScmObj e = SCM_UNBOUND;
1544 ScmChar ch2;
1545 ScmSize nread;
1546 long prefix = Scm_ReadDigitsAsLong(port, ch, 10, &ch2, &nread);
1547
1548 if (ch2 == EOF) Scm_ReadError(port, "unterminated reference form (#digits)");
1549 if (prefix < 0) Scm_ReadError(port, "#-prefix number overflow");
1550
1551 switch (ch2) {
1552 case '#':
1553 /* #digit# - back reference */
1554 if (ctx->table == NULL
1555 || SCM_UNBOUNDP(e = Scm_HashTableRef(ctx->table,
1556 Scm_MakeInteger(prefix),
1557 SCM_UNBOUND))) {
1558 Scm_ReadError(port, "invalid reference number in #%d#", prefix);
1559 }
1560 if (SCM_READ_REFERENCE_P(e) && SCM_READ_REFERENCE_REALIZED(e)) {
1561 return SCM_READ_REFERENCE(e)->value;
1562 } else {
1563 return e;
1564 }
1565 case '=':
1566 /* #digit= - register */
1567 {
1568 ScmObj ref = Scm_MakeReadReference();
1569 if (ctx->table == NULL) {
1570 ctx->table =
1571 SCM_HASH_TABLE(Scm_MakeHashTableSimple(SCM_HASH_EQV, 0));
1572 }
1573 if (!SCM_UNBOUNDP(Scm_HashTableRef(ctx->table,
1574 Scm_MakeInteger(prefix),
1575 SCM_UNBOUND))) {
1576 Scm_ReadError(port, "duplicate back-reference number in #%d=",
1577 prefix);
1578 }
1579 ref_register(ctx, ref, prefix);
1580 ScmObj val = read_item(port, ctx);
1581 if (ref == val) {
1582 /* an edge case: #0=#0# */
1583 Scm_ReadError(port, "indeterminate read reference: #%d=#%d#",
1584 prefix, prefix);
1585 }
1586 SCM_READ_REFERENCE(ref)->value = val;
1587 return val;
1588 }
1589 case 'r': case 'R':
1590 /* #digitR - radix */
1591 if (SCM_EQ(Scm_GetPortReaderLexicalMode(port), SCM_SYM_STRICT_R7)) {
1592 Scm_ReadError(port, "Radix prefix isn't allowed in strict R7RS mode.");
1593 }
1594 if (prefix < SCM_RADIX_MIN || prefix > SCM_RADIX_MAX) {
1595 Scm_ReadError(port, "Radix prefix out of range: radix in #<radix>R must be between %d and %d inclusive, but got: %d",
1596 SCM_RADIX_MIN, SCM_RADIX_MAX, prefix);
1597 return SCM_UNDEFINED; /* dummy */
1598 } else {
1599 int ch = Scm_GetcUnsafe(port);
1600 if (ch == EOF) {
1601 Scm_ReadError(port, "Premature end of radix-prefixed number: #%dr",
1602 prefix);
1603 }
1604 return read_number(port, ch, prefix, ctx);
1605 }
1606 default:
1607 Scm_ReadError(port, "invalid numeric prefix (#, =, r or R is expected) : #%d%A", prefix, SCM_MAKE_CHAR(ch2));
1608 return SCM_UNDEFINED;
1609 }
1610 }
1611
1612 /*----------------------------------------------------------------
1613 * SRFI-10 support
1614 */
1615
1616 /* NB: The 'module' argument of DefineReaderCtor and GetReaderCtor may
1617 be used in future to make reader-ctor binding associated with modules.
1618 For now, it is not used and the caller should pass SCM_FALSE. */
1619
1620 ScmObj Scm_DefineReaderCtor(ScmObj symbol, ScmObj proc, ScmObj finisher,
1621 ScmObj module SCM_UNUSED /*reserved*/)
1622 {
1623 if (!SCM_PROCEDUREP(proc)) {
1624 Scm_Error("procedure required, but got %S", proc);
1625 }
1626 ScmObj pair = Scm_Cons(proc, finisher);
1627 (void)SCM_INTERNAL_MUTEX_LOCK(readCtorData.mutex);
1628 Scm_HashTableSet(readCtorData.table, symbol, pair, 0);
1629 (void)SCM_INTERNAL_MUTEX_UNLOCK(readCtorData.mutex);
1630 return SCM_UNDEFINED;
1631 }
1632
1633 ScmObj Scm_GetReaderCtor(ScmObj symbol, ScmObj module SCM_UNUSED/*reserved*/)
1634 {
1635 (void)SCM_INTERNAL_MUTEX_LOCK(readCtorData.mutex);
1636 ScmObj r = Scm_HashTableRef(readCtorData.table, symbol, SCM_FALSE);
1637 (void)SCM_INTERNAL_MUTEX_UNLOCK(readCtorData.mutex);
1638 return r;
1639 }
1640
1641 static ScmObj read_sharp_comma(ScmPort *port, ScmReadContext *ctx)
1642 {
1643 ScmChar next = Scm_GetcUnsafe(port);
1644 if (next != '(') {
1645 Scm_ReadError(port, "bad #,-form: '(' should be followed, but got %C",
1646 next);
1647 }
1648
1649 int has_ref;
1650 ScmSize line = -1;
1651 if (ctx->flags & RCTX_SOURCE_INFO) line = Scm_PortLine(port);
1652
1653 ScmObj form = read_list_int(port, ')', ctx, &has_ref, line);
1654 int len = Scm_Length(form);
1655 if (len <= 0) {
1656 Scm_ReadError(port, "bad #,-form: #,%S", form);
1657 }
1658 ScmObj r = process_sharp_comma(port, line, SCM_CAR(form), SCM_CDR(form), ctx,
1659 has_ref);
1660 return r;
1661 }
1662
1663 static ScmObj process_sharp_comma(ScmPort *port, int line,
1664 ScmObj key, ScmObj args,
1665 ScmReadContext *ctx, int has_ref)
1666 {
1667 if (ctx->flags & RCTX_DISABLE_CTOR) return SCM_FALSE;
1668
1669 (void)SCM_INTERNAL_MUTEX_LOCK(readCtorData.mutex);
1670 ScmObj e = Scm_HashTableRef(readCtorData.table, key, SCM_FALSE);
1671 (void)SCM_INTERNAL_MUTEX_UNLOCK(readCtorData.mutex);
1672
1673 if (!SCM_PAIRP(e)) Scm_ReadError(port, "unknown #,-key: %S", key);
1674 ScmObj r = Scm_ApplyRec(SCM_CAR(e), args);
1675 if (SCM_PAIRP(r) && (ctx->flags & RCTX_SOURCE_INFO) && line >= 0) {
1676 r = Scm_ExtendedCons(SCM_CAR(r), SCM_CDR(r));
1677 Scm_PairAttrSet(SCM_PAIR(r), SCM_SYM_SOURCE_INFO,
1678 SCM_LIST2(Scm_PortName(port), SCM_MAKE_INT(line)));
1679 }
1680 if (has_ref) ref_push(ctx, r, SCM_CDR(e));
1681 return r;
1682 }
1683
1684 static ScmObj reader_ctor(ScmObj *args, int nargs, void *data SCM_UNUSED)
1685 {
1686 ScmObj optarg = (nargs > 2? args[2] : SCM_FALSE);
1687 return Scm_DefineReaderCtor(args[0], args[1], optarg, SCM_FALSE);
1688 }
1689
1690 /*----------------------------------------------------------------
1691 * #!-support
1692 */
1693
1694 ScmObj Scm_DefineReaderDirective(ScmObj symbol, ScmObj proc)
1695 {
1696 (void)SCM_INTERNAL_MUTEX_LOCK(hashBangData.mutex);
1697 Scm_HashTableSet(hashBangData.table, symbol, proc, 0);
1698 (void)SCM_INTERNAL_MUTEX_UNLOCK(hashBangData.mutex);
1699 return SCM_UNDEFINED;
1700 }
1701
1702 static ScmObj read_shebang(ScmPort *port, ScmReadContext *ctx)
1703 {
1704 /* If '#!' appears at the beginning of the input port, and it is
1705 followed by '/' or a space or a newline, then we consider it as
1706 the beginning of shebang and discard the entire line.
1707 Otherwise, we take this as #!<identifier> directive as
1708 specified in R6RS, and calls appropriate handler.
1709
1710 R6RS is actually not very clean at this corner. It requires
1711 distinct modes to parse a script (which always begins with
1712 shebang) and to parse R6RS program (in which '#!' always
1713 introduces #!<identifier> token). There's no way for just one
1714 parser that strictly covers both situation.
1715 */
1716 int c2 = Scm_GetcUnsafe(port);
1717 if (Scm_PortBytes(port) == 3 && (c2 == '/' || c2 == ' ' || c2 == '\n')) {
1718 /* shebang */
1719 for (;;) {
1720 if (c2 == '\n') return SCM_UNDEFINED;
1721 if (c2 == EOF) return SCM_EOF;
1722 c2 = Scm_GetcUnsafe(port);
1723 }
1724 /*NOTREACHED*/
1725 } else {
1726 ScmObj id = read_immediate_symbol(port, c2, TRUE, "#!", ctx);
1727 (void)SCM_INTERNAL_MUTEX_LOCK(hashBangData.mutex);
1728 ScmObj e = Scm_HashTableRef(hashBangData.table, id, SCM_FALSE);
1729 (void)SCM_INTERNAL_MUTEX_UNLOCK(hashBangData.mutex);
1730 if (SCM_FALSEP(e)) {
1731 Scm_Warn("Ignoring unrecognized hash-bang directive: #!%S", id);
1732 return SCM_UNDEFINED;
1733 }
1734 /* Reader directive may return zero or one value. When it returns
1735 no values, we call Scm_VMSetResult to adjust the number of values.
1736 */
1737 ScmObj r = Scm_ApplyRec3(e, id, SCM_OBJ(port), SCM_OBJ(ctx));
1738 if (Scm_VMGetNumResults(Scm_VM()) == 1) return r;
1739 else { Scm_VMSetResult(SCM_UNDEFINED); return SCM_UNDEFINED; }
1740 }
1741 }
1742
1743 /*----------------------------------------------------------------
1744 * #f, #t, #false, #true, and UVector literals
1745 */
1746
1747 /* Pre-0.9.4 reader. #t and #f delimit themselves (except '1', '3' or '6'
1748 follows '#f'.) I doubt any code breaks if we change that, but there
1749 may be a data files around that somehow relies on this behavior. So
1750 we keep this in 'legacy' reader mode. */
1751 static ScmObj read_sharp_word_legacy(ScmPort *port, char ch, ScmReadContext *ctx)
1752 {
1753 ScmChar c1, c2 = SCM_CHAR_INVALID;
1754 char *tag = NULL;
1755
1756 if (ch == 't') return SCM_TRUE;
1757
1758 c1 = Scm_GetcUnsafe(port);
1759 if (ch == 'f') {
1760 if (c1 != '1' && c1 != '3' && c1 != '6') {
1761 Scm_UngetcUnsafe(c1, port);
1762 return SCM_FALSE;
1763 }
1764 c2 = Scm_GetcUnsafe(port);
1765 if (c1 == '3' && c2 == '2') tag = "f32";
1766 else if (c1 == '6' && c2 == '4') tag = "f64";
1767 else if (c1 == '1' && c2 == '6') tag = "f16";
1768 } else {
1769 if (c1 == '8') tag = (ch == 's')? "s8" : "u8";
1770 else if (c1 == '1') {
1771 c2 = Scm_GetcUnsafe(port);
1772 if (c2 == '6') tag = (ch == 's')? "s16" : "u16";
1773 }
1774 else if (c1 == '3') {
1775 c2 = Scm_GetcUnsafe(port);
1776 if (c2 == '2') tag = (ch == 's')? "s32" : "u32";
1777 }
1778 else if (c1 == '6') {
1779 c2 = Scm_GetcUnsafe(port);
1780 if (c2 == '4') tag = (ch == 's')? "s64" : "u64";
1781 }
1782 }
1783 if (tag == NULL) {
1784 char buf[SCM_CHAR_MAX_BYTES*4], *bufp = buf;
1785 *bufp++ = ch;
1786 SCM_CHAR_PUT(bufp, c1);
1787 bufp += SCM_CHAR_NBYTES(c1);
1788 if (c2 != SCM_CHAR_INVALID) {
1789 SCM_CHAR_PUT(bufp, c2);
1790 bufp += SCM_CHAR_NBYTES(c2);
1791 }
1792 *bufp = '\0';
1793 Scm_ReadError(port, "invalid uniform vector tag: %s", buf);
1794 }
1795 return Scm_ReadUVector(port, tag, ctx);
1796 }
1797
1798 /* A 'new' version, friendly to R7RS */
1799 static ScmObj read_sharp_word_1(ScmPort *port, char ch, ScmReadContext *ctx)
1800 {
1801 ScmString *s = SCM_STRING(read_word(port, ch, ctx, TRUE, FALSE));
1802 const char *w = Scm_GetStringConst(s);
1803 const char *tag = NULL;
1804
1805 switch (ch) {
1806 case 'c':
1807 if (strcmp(w, "c32") == 0
1808 || strcmp(w, "c64") == 0
1809 || strcmp(w, "c128") == 0) {
1810 tag = w;
1811 }
1812 break;
1813 case 'f':
1814 if (strcmp(w, "f16") == 0
1815 || strcmp(w, "f32") == 0
1816 || strcmp(w, "f64") == 0) {
1817 tag = w;
1818 } else if (w[1] == '\0' || strcmp(w, "false") == 0) {
1819 return SCM_FALSE;
1820 }
1821 break;
1822 case 's':
1823 if (strcmp(w, "s8") == 0
1824 || strcmp(w, "s16") == 0
1825 || strcmp(w, "s32") == 0
1826 || strcmp(w, "s64") == 0) {
1827 tag = w;
1828 }
1829 break;
1830 case 'u':
1831 if (strcmp(w, "u8") == 0
1832 || strcmp(w, "u16") == 0
1833 || strcmp(w, "u32") == 0
1834 || strcmp(w, "u64") == 0) {
1835 tag = w;
1836 }
1837 break;
1838 case 't':
1839 if (w[1] == '\0' || strcmp(w, "true") == 0) {
1840 return SCM_TRUE;
1841 }
1842 }
1843 if (tag == NULL) {
1844 Scm_ReadError(port, "invalid #-token: #%s", w);
1845 }
1846 return Scm_ReadUVector(port, tag, ctx);
1847 }
1848
1849 static ScmObj read_sharp_word(ScmPort *port, char ch, ScmReadContext *ctx)
1850 {
1851 if (SCM_EQ(Scm_GetPortReaderLexicalMode(port), SCM_SYM_LEGACY)) {
1852 return read_sharp_word_legacy(port, ch, ctx);
1853 } else {
1854 return read_sharp_word_1(port, ch, ctx);
1855 }
1856 }
1857
1858 /*----------------------------------------------------------------
1859 * #**"....", #*1010010...
1860 */
1861 static ScmObj read_sharp_asterisk(ScmPort *port, ScmReadContext *ctx)
1862 {
1863 /* We used to use #*"..." for incomplete string literals, but it turned
1864 out it can be read as an zero-length bitvector #* followed by an
1865 ordinary string (since a double quote is a <delimiter> in R7RS).
1866
1867 So we changed the literal to #**"..." (yeah, ugly, but incomplete
1868 strings are anomalies and shouldn't be used often).
1869
1870 The reader behavior can be customized by reader-lexical-mode:
1871
1872 permissive (default) - For the next release, #*"..." is read as
1873 an incomplete string, without warning. In future, it warns.
1874 legacy - #*"..." is read as an incomplete string, without warning.
1875 warn-legacy - #*"..." is read as an incomplete string, but warns.
1876 strict-r7 - #*"..." is read as a zero-length bitvector followed by
1877 a string. #**"..." is rejected.
1878 */
1879
1880 int c = Scm_GetcUnsafe(port);
1881 if (c == '*') {
1882 c = Scm_GetcUnsafe(port);
1883 if (c == '"') return read_string(port, TRUE, ctx);
1884 Scm_ReadError(port, "Invalid #* syntax: #**%C", c);
1885 }
1886 if (c == '"') {
1887 ScmObj m = Scm_GetPortReaderLexicalMode(port);
1888 if (SCM_EQ(m, SCM_SYM_PERMISSIVE)) {
1889 /* TRANSIENT: We switch this default behavior to warn in future */
1890 return read_string(port, TRUE, ctx);
1891 }
1892 if (SCM_EQ(m, SCM_SYM_LEGACY)) {
1893 return read_string(port, TRUE, ctx);
1894 }
1895 if (SCM_EQ(m, SCM_SYM_WARN_LEGACY)) {
1896 Scm_Warn("Deprecated incomplete string syntax #*\"...\" at %A:%ld",
1897 Scm_PortName(port), Scm_PortLine(port));
1898 return read_string(port, TRUE, ctx);
1899 }
1900 if (SCM_EQ(m, SCM_SYM_STRICT_R7)) {
1901 Scm_UngetcUnsafe(c, port);
1902 return Scm_MakeBitvector(0, SCM_FALSE);
1903 }
1904 Scm_Panic("invalid reader-lexical-mode");
1905 }
1906
1907 ScmDString ds;
1908 Scm_DStringInit(&ds);
1909 for (;;) {
1910 switch (c) {
1911 case EOF:
1912 goto finish;
1913 case '(': case ')': case '"': case '|': case ';':
1914 Scm_UngetcUnsafe(c, port);
1915 goto finish;
1916 case '0': case '1':
1917 Scm_DStringPutc(&ds, c);
1918 break;
1919 default:
1920 if ((SCM_CHAR_ASCII_P(c) && isspace(c))
1921 || (!SCM_CHAR_ASCII_P(c) && SCM_CHAR_EXTRA_WHITESPACE(c))) {
1922 Scm_UngetcUnsafe(c, port);
1923 goto finish;
1924 }
1925 Scm_ReadError(port, "Invalid char in bitvector literal: %C", c);
1926 }
1927 c = Scm_GetcUnsafe(port);
1928 }
1929
1930 finish:;
1931 ScmObj s = Scm_DStringGet(&ds, 0);
1932 return Scm_StringToBitvector(SCM_STRING(s), FALSE);
1933 }
1934
1935 /* OBSOLETED: gauche.uvector used to call this to set up reader pointer.
1936 Now it is read in src/vector.c. We keep this entry for ABI compatibility.
1937 Remove on 1.0 release. */
1938 void Scm__InstallReadUvectorHook(ScmObj (*f)(ScmPort*, const char*, ScmReadContext *) SCM_UNUSED)
1939 {
1940 }
1941
1942
1943 /*----------------------------------------------------------------
1944 * Initialization
1945 */
1946
1947 void Scm__InitRead(void)
1948 {
1949 readCtorData.table =
1950 SCM_HASH_TABLE(Scm_MakeHashTableSimple(SCM_HASH_EQ, 0));
1951 (void)SCM_INTERNAL_MUTEX_INIT(readCtorData.mutex);
1952 Scm_DefineReaderCtor(SCM_SYM_DEFINE_READER_CTOR,
1953 Scm_MakeSubr(reader_ctor, NULL, 2, 1,
1954 SCM_SYM_DEFINE_READER_CTOR),
1955 SCM_FALSE, SCM_FALSE);
1956
1957 hashBangData.table =
1958 SCM_HASH_TABLE(Scm_MakeHashTableSimple(SCM_HASH_EQ, 0));
1959 (void)SCM_INTERNAL_MUTEX_INIT(hashBangData.mutex);
1960
1961 defaultReadContext =
1962 Scm_MakePrimitiveParameter(SCM_CLASS_PRIMITIVE_PARAMETER, SCM_FALSE,
1963 SCM_OBJ(make_read_context(NULL)),
1964 0);
1965 }