-
Notifications
You must be signed in to change notification settings - Fork 0
/
chap-23.texi
1546 lines (1098 loc) · 48.4 KB
/
chap-23.texi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@node Reader
@chapter Reader
@menu
* Reader Concepts::
* Reader Dictionary::
@end menu
@node Reader Concepts
@section Reader Concepts
@menu
* Dynamic Control of the Lisp Reader::
* Effect of Readtable Case on the Lisp Reader::
* Argument Conventions of Some Reader Functions::
@end menu
@node Dynamic Control of the Lisp Reader
@subsection Dynamic Control of the Lisp Reader
Various aspects of the @term{Lisp reader} can be controlled dynamically.
See @ref{Readtables}@spc{}and @ref{Variables that affect the Lisp Reader}.
@node Effect of Readtable Case on the Lisp Reader
@subsection Effect of Readtable Case on the Lisp Reader
The @term{readtable case} of the @term{current readtable} affects the @term{Lisp reader}
in the following ways:
@table @asis
@item @id{@kwd{upcase}}
When the @term{readtable case} is @kwd{upcase},
unescaped constituent @term{characters} are converted to @term{uppercase},
as specified in @ref{Reader Algorithm}.
@item @id{@kwd{downcase}}
When the @term{readtable case} is @kwd{downcase},
unescaped constituent @term{characters} are converted to @term{lowercase}.
@item @id{@kwd{preserve}}
When the @term{readtable case} is @kwd{preserve},
the case of all @term{characters} remains unchanged.
@item @id{@kwd{invert}}
When the @term{readtable case} is @kwd{invert},
then if all of the unescaped letters in the extended token are of the same @term{case},
those (unescaped) letters are converted to the opposite @term{case}.
@end table
@node Examples of Effect of Readtable Case on the Lisp Reader
@subsubsection Examples of Effect of Readtable Case on the Lisp Reader
@lisp
(defun test-readtable-case-reading ()
(let ((*readtable* (copy-readtable nil)))
(format t "READTABLE-CASE Input Symbol-name~
~%-----------------------------------~
~%")
(dolist (readtable-case '(:upcase :downcase :preserve :invert))
(setf (readtable-case *readtable*) readtable-case)
(dolist (input '("ZEBRA" "Zebra" "zebra"))
(format t "~&:~A~16T~A~24T~A"
(string-upcase readtable-case)
input
(symbol-name (read-from-string input)))))))
@end lisp
The output from @f{(test-readtable-case-reading)} should be as follows:
@lisp
READTABLE-CASE Input Symbol-name
-------------------------------------
:UPCASE ZEBRA ZEBRA
:UPCASE Zebra ZEBRA
:UPCASE zebra ZEBRA
:DOWNCASE ZEBRA zebra
:DOWNCASE Zebra zebra
:DOWNCASE zebra zebra
:PRESERVE ZEBRA ZEBRA
:PRESERVE Zebra Zebra
:PRESERVE zebra zebra
:INVERT ZEBRA zebra
:INVERT Zebra Zebra
:INVERT zebra ZEBRA
@end lisp
@node Argument Conventions of Some Reader Functions
@subsection Argument Conventions of Some Reader Functions
@node The EOF-ERROR-P argument
@subsubsection The EOF-ERROR-P argument
@param{Eof-error-p} in input function calls
controls what happens if input is from a file (or any other
input source that has a definite end) and the end of the file is reached.
If @param{eof-error-p} is @term{true} (the default),
an error @oftype{end-of-file} is signaled
at end of file. If it is @term{false}, then no error is signaled, and instead
the function returns @param{eof-value}.
Functions such as @symbolref{read, SYM} that read the representation
of an @term{object} rather than a single
character always signals an error, regardless of @param{eof-error-p}, if
the file ends in the middle of an object representation.
For example, if a file does
not contain enough right parentheses to balance the left parentheses in
it, @symbolref{read, SYM} signals an error. If a file ends in a
@term{symbol} or a @term{number}
immediately followed by end-of-file, @symbolref{read, SYM} reads the
@term{symbol} or
@term{number}
successfully and when called again will
act according to @param{eof-error-p}.
Similarly, @thefunction{read-line}
successfully reads the last line of a file even if that line
is terminated by end-of-file rather than the newline character.
Ignorable text, such as lines containing only @term{whitespace}@sub{2} or comments,
are not considered to begin an @term{object};
if @symbolref{read, SYM} begins to read an @term{expression} but sees only such
ignorable text, it does not consider the file to end in the middle of an @term{object}.
Thus an @param{eof-error-p} argument controls what happens
when the file ends between @term{objects}.
@node The RECURSIVE-P argument
@subsubsection The RECURSIVE-P argument
If @param{recursive-p} is supplied and not @nil{}, it specifies that
this function call is not an outermost call to @symbolref{read, SYM} but an
embedded call, typically from a @term{reader macro function}.
It is important to distinguish such recursive calls for three reasons.
@enumerate 1
@item
An outermost call establishes the context within which the
@f{#@param{n}=} and @f{#@param{n}#} syntax is scoped. Consider, for example,
the expression
@lisp
(cons '#3=(p q r) '(x y . #3#))
@end lisp
If the @term{single-quote} @term{reader macro} were defined in this way:
@lisp
(set-macro-character #@bsl{}' ;incorrect
#'(lambda (stream char)
(declare (ignore char))
(list 'quote (read stream))))
@end lisp
then each call to the @term{single-quote} @term{reader macro function} would establish
independent contexts for the scope of @symbolref{read, SYM} information, including the scope of
identifications between markers like ``@f{#3=}'' and ``@f{#3#}''. However, for
this expression, the scope was clearly intended to be determined by the outer set
of parentheses, so such a definition would be incorrect.
The correct way to define the @term{single-quote}
@term{reader macro} uses @param{recursive-p}:
@lisp
(set-macro-character #@bsl{}' ;correct
#'(lambda (stream char)
(declare (ignore char))
(list 'quote (read stream t nil t))))
@end lisp
@item
A recursive call does not alter whether the reading process
is to preserve @term{whitespace}@sub{2} or not (as determined by whether the
outermost call was to @symbolref{read, SYM} or @symbolref{read-preserving-whitespace, SYM}).
Suppose again that @term{single-quote}
were to be defined as shown above in the incorrect definition.
Then a call to @symbolref{read-preserving-whitespace, SYM}
that read the expression @f{'foo@SpaceChar{}} would fail to preserve the space
character following the symbol @f{foo} because the @term{single-quote}
@term{reader macro function} calls @symbolref{read, SYM},
not @symbolref{read-preserving-whitespace, SYM},
to read the following expression (in this case @f{foo}).
The correct definition, which passes the value @term{true} for @param{recursive-p}
to @symbolref{read, SYM}, allows the outermost call to determine
whether @term{whitespace}@sub{2} is preserved.
@item
When end-of-file is encountered and the @param{eof-error-p} argument
is not @nil{}, the kind of error that is signaled may depend on the value
of @param{recursive-p}. If @param{recursive-p}
is @term{true}, then the end-of-file
is deemed to have occurred within the middle of a printed representation;
if @param{recursive-p} is @term{false}, then the end-of-file may be deemed to have
occurred between @term{objects} rather than within the middle of one.
@end enumerate
@node Reader Dictionary
@section Reader Dictionary
@menu
* readtable::
* copy-readtable::
* make-dispatch-macro-character::
* read; read-preserving-whitespace::
* read-delimited-list::
* read-from-string::
* readtable-case::
* readtablep::
* set-dispatch-macro-character; get-dispatch-macro-character::
* set-macro-character; get-macro-character::
* set-syntax-from-char::
* with-standard-io-syntax::
* *read-base*::
* *read-default-float-format*::
* *read-eval*::
* *read-suppress*::
* *readtable*::
* reader-error::
@end menu
@node readtable
@syindexanchor{readtable, SYM}
@subsection readtable (System Class)
@cindex readtable
@subsubheading Class Precedence List:
@symbolref{readtable, SYM},
@symbolref{t, SC}
@subsubheading Description:
A @term{readtable} maps @term{characters} into @term{syntax types} for
the @term{Lisp reader}; see @ref{Syntax, Chapter 2 (Syntax)}.
A @term{readtable} also
contains associations between @term{macro characters}
and their @term{reader macro functions},
and records information about the case conversion rules
to be used by the @term{Lisp reader} when parsing @term{symbols}.
Each @term{simple} @term{character} must be representable in the @term{readtable}.
It is @term{implementation-defined} whether @term{non-simple} @term{characters}
can have syntax descriptions in the @term{readtable}.
@subsubheading See Also:
@ref{Readtables},
@ref{Printing Other Objects}
@node copy-readtable
@syindexanchor{copy-readtable, SYM}
@subsection copy-readtable (Function)
@cindex copy-readtable
@subsubheading Syntax:
@DefunWithValues{copy-readtable, @opt{} from-readtable to-readtable, readtable}
@subsubheading Arguments and Values:
@param{from-readtable}---a @term{readtable designator}.
@Default{the @term{current readtable}}
@param{to-readtable}---a @term{readtable} or @nil{}.
@Default{@nil{}}
@param{readtable}---the @param{to-readtable} if it is @term{non-nil},
or else a @term{fresh} @term{readtable}.
@subsubheading Description:
@symbolref{copy-readtable, SYM} copies @param{from-readtable}.
If @param{to-readtable} is @nil{}, a new @term{readtable} is created and returned.
Otherwise the @term{readtable} specified by @param{to-readtable} is modified and returned.
@symbolref{copy-readtable, SYM} copies the setting of @symbolref{readtable-case, SYM}.
@subsubheading Examples:
@lisp
(setq zvar 123) @EV{} 123
(set-syntax-from-char #@bsl{}z #@bsl{}' (setq table2 (copy-readtable))) @EV{} T
zvar @EV{} 123
(copy-readtable table2 *readtable*) @EV{} #<READTABLE 614000277>
zvar @EV{} VAR
(setq *readtable* (copy-readtable)) @EV{} #<READTABLE 46210223>
zvar @EV{} VAR
(setq *readtable* (copy-readtable nil)) @EV{} #<READTABLE 46302670>
zvar @EV{} 123
@end lisp
@subsubheading See Also:
@ref{readtable},
@ref{*readtable*}
@subsubheading Notes:
@lisp
(setq *readtable* (copy-readtable nil))
@end lisp
restores the input syntax to standard @clisp{}@spc{}syntax, even if
the @term{initial readtable} has been clobbered
(assuming it is not so badly clobbered that you cannot type in the above expression).
On the other hand,
@lisp
(setq *readtable* (copy-readtable))
@end lisp
replaces the current @term{readtable} with a copy of itself.
This is useful if you want to save a copy of a readtable for later use,
protected from alteration in the meantime. It is also useful if you want to
locally bind the readtable to a copy of itself, as in:
@lisp
(let ((*readtable* (copy-readtable))) ...)
@end lisp
@node make-dispatch-macro-character
@syindexanchor{make-dispatch-macro-character, SYM}
@subsection make-dispatch-macro-character (Function)
@cindex make-dispatch-macro-character
@subsubheading Syntax:
@DefunWithValues{make-dispatch-macro-character, char @opt{} non-terminating-p readtable, @symbolref{t, SC}}
@subsubheading Arguments and Values:
@param{char}---a @term{character}.
@param{non-terminating-p}---a @term{generalized boolean}.
@Default{@term{false}}
@param{readtable}---a @term{readtable}.
@Default{the @term{current readtable}}
@subsubheading Description:
@symbolref{make-dispatch-macro-character, SYM} makes @param{char}
be a @term{dispatching macro character} in @param{readtable}.
Initially, every @term{character} in the dispatch table
associated with the @param{char} has an associated function
that signals an error @oftype{reader-error}.
If @param{non-terminating-p} is @term{true},
the @term{dispatching macro character}
is made a @term{non-terminating} @term{macro character};
if @param{non-terminating-p} is @term{false},
the @term{dispatching macro character}
is made a @term{terminating} @term{macro character}.
@subsubheading Examples:
@lisp
(get-macro-character #@bsl{}@lbr{}) @EV{} NIL, @term{false}
(make-dispatch-macro-character #@bsl{}@lbr{}) @EV{} T
(not (get-macro-character #@bsl{}@lbr{})) @EV{} @term{false}
@end lisp
The @param{readtable} is altered.
@subsubheading See Also:
@ref{*readtable*}, @ref{set-dispatch-macro-character}
@node read; read-preserving-whitespace
@syindexanchor{read, SYM}
@syindexanchor{read-preserving-whitespace, SYM}
@subsection read, read-preserving-whitespace (Function)
@cindex read
@cindex read-preserving-whitespace
@anchor{read}
@anchor{read-preserving-whitespace}
@subsubheading Syntax:
@DefunWithValues{read, @opt{} input-stream eof-error-p eof-value recursive-p, object}
@DefunWithValuesNewline{read-preserving-whitespace, @opt{} @vtop{@hbox{input-stream eof-error-p} @hbox{eof-value recursive-p}}, object}
@subsubheading Arguments and Values:
@param{input-stream}---an @term{input} @term{stream designator}.
@param{eof-error-p}---a @term{generalized boolean}.
@Default{@term{true}}
@param{eof-value}---an @term{object}.
@Default{@nil{}}
@param{recursive-p}---a @term{generalized boolean}.
@Default{@term{false}}
@param{object}---an @term{object} (parsed by the @term{Lisp reader})
or the @param{eof-value}.
@subsubheading Description:
@symbolref{read, SYM} parses the printed representation of an @term{object}
from @param{input-stream} and builds such an @term{object}.
@symbolref{read-preserving-whitespace, SYM} is like @symbolref{read, SYM} but preserves
any @term{whitespace}@sub{2} @term{character}
that delimits the printed representation of the @term{object}.
@symbolref{read-preserving-whitespace, SYM} is exactly like @symbolref{read, SYM}
when the @param{recursive-p} @term{argument} to @symbolref{read-preserving-whitespace, SYM}
is @term{true}.
When @symbolref{*read-suppress*, SYM} is @term{false},
@symbolref{read, SYM} throws away the delimiting @term{character} required by
certain printed representations if it is a
@term{whitespace}@sub{2} @term{character};
but @symbolref{read, SYM} preserves the character
(using @symbolref{unread-char, SYM}) if it is
syntactically meaningful, because it could be the start of the next expression.
If a file ends in a @term{symbol} or a @term{number}
immediately followed by an @term{end of file}@sub{1},
@symbolref{read, SYM} reads the @term{symbol} or @term{number} successfully;
when called again, it sees the @term{end of file}@sub{1} and
only then acts according to @param{eof-error-p}.
If a file contains ignorable text at the end, such
as blank lines and comments, @symbolref{read, SYM}
does not consider it to end in the
middle of an @term{object}.
If @param{recursive-p} is @term{true}, the call to @symbolref{read, SYM} is
expected to be made
from within some function that itself
has been called from @symbolref{read, SYM} or from a similar input function, rather
than from the top level.
Both functions return the @term{object} read from @param{input-stream}.
@param{Eof-value} is returned if @param{eof-error-p} is @term{false} and end of file
is reached before the beginning of an @term{object}.
@subsubheading Examples:
@lisp
(read)
@OUT{} @IN{'a}
@EV{} (QUOTE A)
(with-input-from-string (is " ") (read is nil 'the-end)) @EV{} THE-END
(defun skip-then-read-char (s c n)
(if (char= c #@bsl{}@lcurly{}) (read s t nil t) (read-preserving-whitespace s))
(read-char-no-hang s)) @EV{} SKIP-THEN-READ-CHAR
(let ((*readtable* (copy-readtable nil)))
(set-dispatch-macro-character #@bsl{}# #@bsl{}@lcurly{} #'skip-then-read-char)
(set-dispatch-macro-character #@bsl{}# #@bsl{}@rcurly{} #'skip-then-read-char)
(with-input-from-string (is "#@lcurly{}123 x #@rcurly{}123 y")
(format t "~S ~S" (read is) (read is)))) @EV{} #@bsl{}x, #@bsl{}Space, NIL
@end lisp
As an example, consider this @term{reader macro} definition:
@lisp
(defun slash-reader (stream char)
(declare (ignore char))
`(path . ,(loop for dir = (read-preserving-whitespace stream t nil t)
then (progn (read-char stream t nil t)
(read-preserving-whitespace stream t nil t))
collect dir
while (eql (peek-char nil stream nil nil t) #@bsl{}/))))
(set-macro-character #@bsl{}/ #'slash-reader)
@end lisp
Consider now calling @symbolref{read, SYM} on this expression:
@lisp
(zyedh /usr/games/zork /usr/games/boggle)
@end lisp
The @f{/} macro reads objects separated by more @f{/} characters;
thus @f{/usr/games/zork} is intended to read as @f{(path usr games zork)}.
The entire example expression should therefore be read as
@lisp
(zyedh (path usr games zork) (path usr games boggle))
@end lisp
However, if @symbolref{read, SYM} had been used instead of
@symbolref{read-preserving-whitespace, SYM}, then after the reading of the symbol
@f{zork}, the following space would be discarded; the next call
to @symbolref{peek-char, SYM} would see the following @f{/}, and the loop would
continue, producing this interpretation:
@lisp
(zyedh (path usr games zork usr games boggle))
@end lisp
There are times when @term{whitespace}@sub{2} should be discarded.
If a command interpreter takes single-character commands,
but occasionally reads an @term{object} then if the @term{whitespace}@sub{2}
after a @term{symbol}
is not discarded it might be interpreted as a command
some time later after the @term{symbol} had been read.
@subsubheading Affected By:
@symbolref{*standard-input*, SYM},
@symbolref{*terminal-io*, SYM},
@symbolref{*readtable*, SYM},
@symbolref{*read-default-float-format*, SYM},
@symbolref{*read-base*, SYM},
@symbolref{*read-suppress*, SYM},
@symbolref{*package*, SYM},
@symbolref{*read-eval*, SYM}.
@subsubheading Exceptional Situations:
@symbolref{read, SYM} signals an error @oftype{end-of-file},
regardless of @param{eof-error-p}, if
the file ends in the middle of an @term{object} representation.
For example, if a file does
not contain enough right parentheses to balance the left parentheses in
it, @symbolref{read, SYM} signals an error.
This is detected when @symbolref{read, SYM} or @symbolref{read-preserving-whitespace, SYM}
is called with @param{recursive-p} and @param{eof-error-p} @term{non-nil},
and end-of-file is reached before the beginning of an @term{object}.
If @param{eof-error-p} is @term{true}, an error @oftype{end-of-file}
is signaled at the end of file.
@subsubheading See Also:
@ref{peek-char},
@ref{read-char},
@ref{unread-char},
@ref{read-from-string},
@ref{read-delimited-list},
@ref{parse-integer},
@ref{Syntax, Chapter 2 (Syntax)},
@ref{Reader Concepts}
@node read-delimited-list
@syindexanchor{read-delimited-list, SYM}
@subsection read-delimited-list (Function)
@cindex read-delimited-list
@subsubheading Syntax:
@DefunWithValues{read-delimited-list, char @opt{} input-stream recursive-p, list}
@subsubheading Arguments and Values:
@param{char}---a @term{character}.
@param{input-stream}---an @term{input} @term{stream designator}.
@Default{@term{standard input}}
@param{recursive-p}---a @term{generalized boolean}.
@Default{@term{false}}
@param{list}---a @term{list} of the @term{objects} read.
@subsubheading Description:
@symbolref{read-delimited-list, SYM} reads @term{objects} from @param{input-stream}
until the next character after an @term{object}'s
representation (ignoring @term{whitespace}@sub{2} characters and comments) is @param{char}.
@symbolref{read-delimited-list, SYM} looks ahead at each step
for the next non-@term{whitespace}@sub{2} @term{character}
and peeks at it as if with @symbolref{peek-char, SYM}.
If it is @param{char},
then the @term{character} is consumed and the @term{list} of @term{objects} is returned.
If it is a @term{constituent} or @term{escape} @term{character},
then @symbolref{read, SYM} is used to read an @term{object},
which is added to the end of the @term{list}.
If it is a @term{macro character},
its @term{reader macro function} is called;
if the function returns a @term{value},
that @term{value} is added to the @term{list}.
The peek-ahead process is then repeated.
If @param{recursive-p} is @term{true},
this call is expected to be embedded in a higher-level call to @symbolref{read, SYM}
or a similar function.
It is an error to reach end-of-file during the operation of
@symbolref{read-delimited-list, SYM}.
The consequences are undefined
if @param{char} has a @term{syntax type} of @term{whitespace}@sub{2}
in the @term{current readtable}.
@subsubheading Examples:
@lisp
(read-delimited-list #@bsl{}@rbracket{}) 1 2 3 4 5 6 @rbracket{}
@EV{} (1 2 3 4 5 6)
@end lisp
Suppose you wanted @f{#@lcurly{}@i{a} @i{b} @i{c} @mat{@ldots{}} @i{z}@rcurly{}}
to read as a list of all pairs of the elements @i{a}, @i{b}, @i{c},
@mat{@ldots{}}, @i{z}, for example.
@lisp
#@lcurly{}p q z a@rcurly{} reads as ((p q) (p z) (p a) (q z) (q a) (z a))
@end lisp
This can be done by specifying a macro-character definition for @f{#@lcurly{}}
that does two things: reads in all the items up to the @f{@rcurly{}},
and constructs the pairs. @symbolref{read-delimited-list, SYM} performs
the first task.
@lisp
(defun |#@lcurly{}-reader| (stream char arg)
(declare (ignore char arg))
(mapcon #'(lambda (x)
(mapcar #'(lambda (y) (list (car x) y)) (cdr x)))
(read-delimited-list #@bsl{}@rcurly{} stream t))) @EV{} |#@lcurly{}-reader|
(set-dispatch-macro-character #@bsl{}# #@bsl{}@lcurly{} #'|#@lcurly{}-reader|) @EV{} T
(set-macro-character #@bsl{}@rcurly{} (get-macro-character #@bsl{}) @nil{}))
@end lisp
Note that @term{true} is supplied for the @param{recursive-p} argument.
It is necessary here to give a definition to the character @f{@rcurly{}} as
well to prevent it from being a constituent.
If the line
@lisp
(set-macro-character #@bsl{}@rcurly{} (get-macro-character #@bsl{}) @nil{}))
@end lisp
shown above were not included, then the @f{@rcurly{}} in
@lisp
#@lcurly{} p q z a@rcurly{}
@end lisp
would be considered a constituent character, part of the symbol named
@f{a@rcurly{}}. This could be corrected by putting a space before
the @f{@rcurly{}}, but it is better to call
@symbolref{set-macro-character, SYM}.
Giving @f{@rcurly{}} the same
definition as the standard definition of the character @f{)} has the
twin benefit of making it terminate tokens for use with
@symbolref{read-delimited-list, SYM} and also making it invalid for use in any
other context. Attempting to read a stray @f{@rcurly{}} will signal an error.
@subsubheading Affected By:
@symbolref{*standard-input*, SYM},
@symbolref{*readtable*, SYM},
@symbolref{*terminal-io*, SYM}.
@subsubheading See Also:
@ref{read},
@ref{peek-char},
@ref{read-char},
@ref{unread-char}.
@subsubheading Notes:
@symbolref{read-delimited-list, SYM} is intended for use in implementing @term{reader macros}.
Usually it is desirable for @param{char} to be a @term{terminating} @term{macro character}
so that it can be used to delimit tokens; however, @symbolref{read-delimited-list, SYM}
makes no attempt to alter the syntax specified for @param{char} by the current
readtable. The caller must make any necessary changes to the readtable syntax
explicitly.
@node read-from-string
@syindexanchor{read-from-string, SYM}
@subsection read-from-string (Function)
@cindex read-from-string
@subsubheading Syntax:
@DefunWithValuesNewline{read-from-string, string @vtop{@hbox{@opt{} eof-error-p eof-value} @hbox{@keyparam{} start end preserve-whitespace}}, object\, position}
@subsubheading Arguments and Values:
@param{string}---a @term{string}.
@param{eof-error-p}---a @term{generalized boolean}.
@Default{@term{true}}
@param{eof-value}---an @term{object}.
@Default{@nil{}}
@param{start}, @param{end}---@term{bounding index designators} of @param{string}.
@Defaults{@param{start} and @param{end}, @f{0} and @nil{}}
@param{preserve-whitespace}---a @term{generalized boolean}.
@Default{@term{false}}
@param{object}---an @term{object} (parsed by the @term{Lisp reader})
or the @param{eof-value}.
@param{position}---an @term{integer} greater than or equal to zero,
and less than or equal to
one more than the @term{length} of the @param{string}.
@subsubheading Description:
Parses the printed representation of an @term{object}
from the subsequence of @param{string} @term{bounded} by @param{start} and @param{end},
as if @symbolref{read, SYM} had been called on an @term{input} @term{stream}
containing those same @term{characters}.
If @param{preserve-whitespace} is @term{true},
the operation will preserve @term{whitespace}@sub{2}
as @symbolref{read-preserving-whitespace, SYM} would do.
If an @term{object} is successfully parsed, the @term{primary value}, @param{object},
is the @term{object} that was parsed.
If @param{eof-error-p} is @term{false} and if the end of the @param{substring} is reached,
@param{eof-value} is returned.
The @term{secondary value}, @param{position}, is the index of the first @term{character}
in the @term{bounded} @param{string} that was not read.
The @param{position} may depend upon the value of @param{preserve-whitespace}.
If the entire @param{string} was read,
the @param{position} returned is either the @param{length} of the @param{string}
or one greater than the @param{length} of the @param{string}.
@subsubheading Examples:
@lisp
(read-from-string " 1 3 5" t nil :start 2) @EV{} 3, 5
(read-from-string "(a b c)") @EV{} (A B C), 7
@end lisp
@subsubheading Exceptional Situations:
If the end of the supplied substring
occurs before an @term{object} can be read, an
error is signaled if @param{eof-error-p} is @term{true}.
An error is signaled if the end of the @param{substring} occurs
in the middle of an incomplete @term{object}.
@subsubheading See Also:
@ref{read},
@ref{read-preserving-whitespace}
@subsubheading Notes:
The reason that @param{position} is allowed to be beyond the
@term{length} of the @param{string} is to permit (but not require)
the @term{implementation} to work by simulating the effect of a
trailing delimiter at the end of the @term{bounded} @param{string}.
When @param{preserve-whitespace} is @term{true},
the @param{position} might count the simulated delimiter.
@node readtable-case
@syindexanchor{readtable-case, SYM}
@subsection readtable-case (Accessor)
@cindex readtable-case
@subsubheading Syntax:
@DefunWithValues{readtable-case, readtable, mode}
@Defsetf{readtable-case, readtable, mode}
@subsubheading Arguments and Values:
@param{readtable}---a @term{readtable}.
@param{mode}---a @term{case sensitivity mode}.
@subsubheading Description:
@term{Accesses} the @term{readtable case} of @param{readtable},
which affects the way in which the @term{Lisp Reader} reads @term{symbols}
and the way in which the @term{Lisp Printer} writes @term{symbols}.
@subsubheading Examples:
See @ref{Examples of Effect of Readtable Case on the Lisp Reader}@spc{}and @ref{Examples of Effect of Readtable Case on the Lisp Printer}.
@subsubheading Exceptional Situations:
@Shouldchecktype{readtable, a @term{readtable}}
@Shouldchecktype{mode, a @term{case sensitivity mode}}
@subsubheading See Also:
@ref{*readtable*},
@ref{*print-escape*},
@ref{Reader Algorithm},
@ref{Effect of Readtable Case on the Lisp Reader},
@ref{Effect of Readtable Case on the Lisp Printer}
@subsubheading Notes:
@symbolref{copy-readtable, SYM} copies the @term{readtable case} of the @param{readtable}.
@node readtablep
@syindexanchor{readtablep, SYM}
@subsection readtablep (Function)
@cindex readtablep
@subsubheading Syntax:
@DefunWithValues{readtablep, object, generalized-boolean}
@subsubheading Arguments and Values:
@param{object}---an @term{object}.
@param{generalized-boolean}---a @term{generalized boolean}.
@subsubheading Description:
@TypePredicate{object, readtable}
@subsubheading Examples:
@lisp
(readtablep *readtable*) @EV{} @term{true}
(readtablep (copy-readtable)) @EV{} @term{true}
(readtablep '*readtable*) @EV{} @term{false}
@end lisp
@subsubheading Notes:
@lisp
(readtablep @param{object}) @EQ{} (typep @param{object} 'readtable)
@end lisp
@node set-dispatch-macro-character; get-dispatch-macro-character
@syindexanchor{set-dispatch-macro-character, SYM}
@syindexanchor{get-dispatch-macro-character, SYM}
@subsection set-dispatch-macro-character, get-dispatch-macro-character (Function)
@cindex set-dispatch-macro-character
@cindex get-dispatch-macro-character
@anchor{set-dispatch-macro-character}
@subsubheading Syntax:
@DefunWithValues{get-dispatch-macro-character, disp-char sub-char @opt{} readtable, function}
@DefunWithValues{set-dispatch-macro-character, disp-char sub-char new-function @opt{} readtable, @symbolref{t, SC}}
@subsubheading Arguments and Values:
@param{disp-char}---a @term{character}.
@param{sub-char}---a @term{character}.
@param{readtable}---a @term{readtable designator}.
@Default{the @term{current readtable}}
@param{function}---a @term{function designator} or @nil{}.
@param{new-function}---a @term{function designator}.
@subsubheading Description:
@symbolref{set-dispatch-macro-character, SYM} causes @param{new-function} to be called
when @param{disp-char} followed by @param{sub-char} is read.
If @param{sub-char} is a lowercase letter,
it is converted to its uppercase equivalent.
It is an error if @param{sub-char} is one of the ten decimal digits.
@symbolref{set-dispatch-macro-character, SYM} installs a @param{new-function} to be called
when a particular @term{dispatching macro character} pair is read.
@param{New-function} is installed as the dispatch function to be
called when @param{readtable} is in use and when @param{disp-char} is followed by
@param{sub-char}.
For more information about how the @param{new-function} is invoked,
see @ref{Macro Characters}.
@symbolref{get-dispatch-macro-character, SYM} retrieves
the dispatch function associated with @param{disp-char} and @param{sub-char}
in @param{readtable}.
@symbolref{get-dispatch-macro-character, SYM} returns the macro-character function
for @param{sub-char} under @param{disp-char}, or @nil{}@spc{}if there is no
function associated with @param{sub-char}.
If @param{sub-char} is a decimal digit, @symbolref{get-dispatch-macro-character, SYM}
returns @nil{}.
@subsubheading Examples:
@lisp
(get-dispatch-macro-character #@bsl{}# #@bsl{}@lcurly{}) @EV{} NIL
(set-dispatch-macro-character #@bsl{}# #@bsl{}@lcurly{} ;dispatch on #@lcurly{}
#'(lambda(s c n)
(let ((list (read s nil (values) t))) ;list is object after #n@lcurly{}
(when (consp list) ;return nth element of list
(unless (and n (< 0 n (length list))) (setq n 0))
(setq list (nth n list)))
list))) @EV{} T
#@lcurly{}(1 2 3 4) @EV{} 1
#3@lcurly{}(0 1 2 3) @EV{} 3
#@lcurly{}123 @EV{} 123
@end lisp
If it is desired that @f{#@dollar{}@i{foo}} :
as if it were @f{(dollars @i{foo})}.
@lisp
(defun |#$-reader| (stream subchar arg)
(declare (ignore subchar arg))
(list 'dollars (read stream t nil t))) @EV{} |#$-reader|
(set-dispatch-macro-character #@bsl{}# #@bsl{}@dollar{} #'|#@dollar{}-reader|) @EV{} T
@end lisp
@subsubheading See Also:
@ref{Macro Characters}
@subsubheading Side Effects:
The @param{readtable} is modified.
@subsubheading Affected By:
@symbolref{*readtable*, SYM}.
@subsubheading Exceptional Situations:
For either function, an error is signaled if @param{disp-char} is not
a @term{dispatching macro character} in @param{readtable}.
@subsubheading See Also:
@ref{*readtable*}
@subsubheading Notes:
It is necessary
to use @symbolref{make-dispatch-macro-character, SYM} to set up the
dispatch character before specifying its sub-characters.
@node set-macro-character; get-macro-character
@syindexanchor{set-macro-character, SYM}
@syindexanchor{get-macro-character, SYM}
@subsection set-macro-character, get-macro-character (Function)
@cindex set-macro-character
@cindex get-macro-character
@anchor{set-macro-character}
@subsubheading Syntax:
@DefunWithValues{get-macro-character, char @opt{} readtable, function\, non-terminating-p}
@DefunWithValues{set-macro-character, char new-function @opt{} non-terminating-p readtable, @symbolref{t, SC}}
@subsubheading Arguments and Values:
@param{char}---a @term{character}.
@param{non-terminating-p}---a @term{generalized boolean}.
@Default{@term{false}}
@param{readtable}---a @term{readtable designator}.
@Default{the @term{current readtable}}