set-syntax-from-char
set-syntax-from-char Function
Syntax:
set-syntax-from-char to-char from-char &optional to-readtable from-readtable → t
Arguments and Values:
to-char—a character .
from-char—a character .
to-readtable—a readtable. The default is the current readtable.
from-readtable—a readtable designator . The default is the standard readtable.
Description:
set-syntax-from-char makes the syntax of to-char in to-readtable be the same as the syntax of from-char in from-readtable.
set-syntax-from-char copies the syntax types of from-char. If from-char is a macro character , its reader macro function is copied also. If the character is a dispatching macro character , its entire dispatch table of reader macro functions is copied. The constituent traits of from-char are not copied.
A macro definition from a character such as " can be copied to another character; the standard definition for " looks for another character that is the same as the character that invoked it. The definition of ( can not be meaningfully copied to {, on the other hand. The result is that lists are of the form {a b c), not {a b c}, because the definition always looks for a closing parenthesis, not a closing brace.
Examples:
(set-syntax-from-char #\7 #\;) → T
123579 → 1235
Side Effects:
The to-readtable is modified.
Affected By:
The existing values in the from-readtable.
See Also:
set-macro-character, make-dispatch-macro-character, Section 2.1.4 (Character Syntax Types)
Notes:
The constituent traits of a character are “hard wired” into the parser for extended tokens. For example, if the definition of S is copied to *, then * will become a constituent that is alphabetic2 but that cannot be used as a short float exponent marker . For further information, see Section 2.1.4.2 (Constituent Traits).
Expanded Reference: set-syntax-from-char
Copying syntax from one character to another
set-syntax-from-char makes the syntax of to-char be the same as the syntax of from-char. This includes the syntax type and, if from-char is a macro character, its reader macro function.
;; Give z the syntax of the single-quote character
(let ((*readtable* (copy-readtable)))
(set-syntax-from-char #\z #\')
(with-input-from-string (s "zhello")
(read s)))
=> (QUOTE HELLO)
Making a character behave as a comment
You can copy the syntax of ; (semicolon) to another character to make it act as a line comment.
(let ((*readtable* (copy-readtable)))
(set-syntax-from-char #\% #\;)
(with-input-from-string (s "hello % this is a comment
world")
(list (read s) (read s))))
=> (HELLO WORLD)
Making a digit a comment character
A more dramatic example: making a digit act as a comment character. The rest of the line after it is ignored.
(let ((*readtable* (copy-readtable)))
(set-syntax-from-char #\7 #\;)
(with-input-from-string (s "12357 this is ignored
89")
(list (read s) (read s))))
=> (1235 89)
Copying from a specific readtable
The fourth argument from-readtable specifies which readtable to copy the syntax from. By default it is the standard readtable (nil).
;; Copy the standard syntax of ( to a custom readtable
(let ((rt1 (copy-readtable))
(rt2 (copy-readtable)))
;; Modify ( in rt1 somehow, then restore it from the standard readtable
(set-syntax-from-char #\( #\( rt1 nil)
;; ( in rt1 now has standard behavior
(not (null (get-macro-character #\( rt1))))
=> T
Caveat: macro definitions look for the original character
When copying the macro definition from a character like ", the copied function still looks for the same terminating character. Copying " to | would make | start a string that ends at the next ", not at the next |.
;; Copying " to another character: the terminator behavior
;; is implementation-dependent
(let ((*readtable* (copy-readtable)))
(set-syntax-from-char #\^ #\")
(with-input-from-string (s "^hello^")
(read s)))
=> "hello"