Reader

Reader's global variables:

*read-base*
number base to be read; default is decimal ten
*readtable*
current readtable which determines reader syntax

Reader's default macro characters:


(   read list 

" read string
' read quoted expression
# dispatch macro
; comment until end of line
` back-quote
, list-time eval
@ append
% read C-like mathematical forms

Escape characters:


$\backslash$   single character escape 

$\vert...\vert$ multiple character escape

When an unescaped symbol is read, all the constituent characters are converted to upcase by default, and upcase-character symbol is stored internally. For example, 'abc and 'ABC are regarded as the same symbol. Escape is necessary to distinguish between them. '$\vert$ABC$\vert$, 'ABC and 'abc are identical, while '$\vert$abc$\vert$ and 'abc are different symbols. By default, even if you enter a symbol with upcase letters, When symbols are printed, EusLisp's printer converts them into lowercase from internal upcase representation. This conversion is suppressed by setting *print-case* to :UPCASE.

Note that 10. is read as integer 10, not floating 10.0. Since ':' is reserved for package marker, it must be escaped when used as a constituent of a symbol, like '$\vert g:pcube\vert$. This restriction is imposed not by the syntax of the character ':', but by the attribute which determines the alphabetical order and the meaning of the letter. The attributes of characters are hardwired in the reader. Thus, although you may change the syntax of a certain character by creating a new readtable by copy-readtable and resetting the syntactic meaning for the character by set-syntax-from-char, you cannot change its attribute anyway. In other words, digits are always digits, alphabets are alphabets, and we cannot use letters like '#$%@' to represent numbers.

String is denoted by two double quotes '"' at the beginning and at the end. No case conversion is taken inside the quotes. A back-slash 'ís used as an escape to include a double quote. Therefore, "He said, Ï like Lisp.SPMquot;" is read as a string including two double quotes. To enter a back-slash, two back-slashes are needed. Note that shift-JIS encoding of Japanese text is inadequate for this read-string convention, since some characters happen to have the code of a back-slash (#x5c) as their second byte. Use of EUC coding is preferrable.

% is an extended read-macro character specific to EusLisp. Preceding % to a mathematical formula written in infix notation, the formula is converted to lisp's prefix form. For an instance, %(1 + 2 * 3 / 4.0) is transformed to (+ 1 (/ (* 2 3) 4.0)) and 2.5 is resulted. C-like function calls and array references are converted to lisp forms, too, thus, %(sin(x) + a[1]) is evaluated to (+ (sin x) (aref a 1)). Functions having more than one arguments and arrays of more than two dimeisions are notated as func(a b c ...) and ary[1 2 3 ...], not func(a,b,c) nor ary[1][2][3]. Relative expressions and assignments are also properly handled, so, %(a $<$ b) is converted to ($<$ a b), and %(a[0] = b[0] * c[0]) is to (setf (aref b 0) (* (aref b 0) (aref c 0))). A simple optimization is performed to reduce duplicated function calls and array references. %(sin(x) + cos(x) / sin(x)) is converted into (let* ((temp (sin x))) (+ temp (/ (cos x) temp))).

Dispatch macros are preceeded by the # character. A number (integer) argument can be given between # and a dispatch macro character. This means that any digits (0 .. 9) cannot be defined as dispatch macro characters. Reader's standard dispatch macro characters follow:

#nA(..)
array
#B
binary number
#D
degree to radian conversion; #D180 = 3.14
#F(...)
floatvector
#nF((..))
float array; #2F((..) (..)) is matrix
#I(...)
integer-vector
#nI((...))
integer array
#J(...)
general object #J(myclass ....); obsolete
#O
octal number
#P
pathname
#R
radian to degree conversion; #R3.14 = 180.0
#S(classname slotname1 val1 slotname2 val2 ...)
structure (any object)
#V(...)
vector #V(vectorclass ...)
#X
hexadecimal number
#(...)
vector
#n#
label reference
#n=
label definition
#'
FUNCTION; compiled-code or lambda-closure
#$\backslash$
character
#,
read-time evaluation
#+
conditional read (positive)
#-
conditional read (negative)
#*
bit vector
#:
uninterned symbol
#$\vert$...$\vert$#
comment; can be nested

Some reader functions have eof-error-p, eof-value and recursive-p parameters. The first two parameters control the behavior when the reader encounters with end-of-file. The default of eof-error-p is t, which causes an error at eof. If you want to know the occurrence of eof and don't want the system's error-handler to snatch control, specify nil to eof-error-p. Thus, when an eof appears during reading, the reader returns the eof-value instead of entering an error loop. Eof-value is defaulted to nil. So, you cannot know if nil is actually read, or eof appears. To distinguish them, give a value which can never appear in the stream. Use cons or gensym to make such unique data object.

Recursive-p is often used in read-macro functions, which call reader recursively. Non-nil value of recursive-p tells the reader that the read operation has been started somewhere else and it should not reset the internal table for reading forms labeled by #n= and #n#.



read &optional stream (eof-error-p t) (eof-value nil) recursive-p [function]

reads one S-expression.


read-delimited-list delim-char &optional stream recursive-p [function]

reads s-expression which is delimited by delim-char. This is useful to read comma-separated list, or to read a sequence terminated by a special character like #$\backslash$].


read-line &optional stream (eof-error-p t) (eof-value nil) [function]

reads a line which is terminated by a #$\backslash$newline. The string returned does not contain the last newline character.


read-char &optional stream (eof-error-p t) (eof-value nil) [function]

reads one character and returns its integer representation.


read-from-string string &optional (eof-error-p t) (eof-value nil) [function]

reads one s-expression from string. Only the first s-expression can be read. If successive read operations need to be performed on a string containing more than one expression, use string-stream made by make-string-input-stream.


unread-char char &optional stream [function]

puts the char back to the stream. More than one characters cannot be put back successively.


peek-char &optional stream (eof-error-p t) (eof-value nil) [function]

reads a character from the stream without removing it from the buffer of the stream. This is equivalent to a read-char followed by a unread-char.


y-or-n-p &optional format-string &rest args [function]

prints format-string and args on your terminal, and asks ``y-or-n''. Repeat query until your response begins with either of ``y'' or ``n'', and returns T or NIL. Case does not matter.


yes-or-no-p &optional stream [function]

prints format-string and args on your terminal, and asks ``yes-or-no''. Repeat query until your response is either of ``yes'' or ``no'', and returns T or NIL. Case does not matter.


In the readtable manipulating functions, the default value of readtable is the value of the global variable *readtable*.



readtable-p x [function]

T if x is an readtable.


copy-readtable &optional from-readtable to-readtable [function]
If no to-readtable is specified, a new one is created. All the information in from-readtable is transferd to to-readtable. The information included is, syntax table, read-macro table and dispatch-macro table, each of which has 256 elements.


set-syntax-from-char to-char to-char [to-readtable from-readtable] [function]

copies syntax and read-macro definition of from-char in from-readtable to that of to-char in to-readtable.


set-macro-character char func [non-teminating-p readtable] [function]

defines func as the read-macro function for char.


get-macro-character char [readtable] [function]
returns the read-macro function for char.


set-dispatch-macro-character dispchar char func [readtable] [function]
defines func as the dispatch read-macro function for the combination of dispchar and char.


get-dispatch-macro-character dispchar char [readtable] [function]
returns the dispatch read-macro function for the combination of dispchar and char.


k-okada 2013-05-21