~ chicken-core (master) /manual/Module (chicken read-syntax)
Trap1[[tags: manual]]2[[toc:]]34== Module (chicken read-syntax)56This module provides procedures which can be used to extend the reader7with custom read syntax. When using the interpreter, these procedures8take immediate effect. During compilation, however, these procedures9might need to be evaluated before the source code is read for compilation. You can use10the {{-X}} option when invoking {{csc}} to load source or compiled code11to make sure your read-syntax extensions are available.1213=== define-reader-ctor1415<procedure>(define-reader-ctor SYMBOL PROC)</procedure>1617Define new read-time constructor for {{#,}} read syntax. For further information, see18the documentation for [[http://srfi.schemers.org/srfi-10/srfi-10.html|SRFI-10]].192021=== set-read-syntax!2223<procedure>(set-read-syntax! CHAR-OR-SYMBOL PROC)</procedure>2425When the reader encounters the non-whitespace character {{CHAR}} while reading26an expression from a given port, then the procedure {{PROC}} will be called with27that port as its argument. The procedure should return a value that will be returned28to the reader:2930<enscript highlight=scheme>31 ; A simple RGB color syntax:3233 (set-read-syntax! #\%34 (lambda (port)35 (apply vector36 (map (cut string->number <> 16)37 (string-chop (read-string 6 port) 2) ) ) ) )3839 (with-input-from-string "(1 2 %f0f0f0 3)" read)40 ; ==> (1 2 #(240 240 240) 3)41</enscript>4243If {{CHAR-OR-SYMBOL}} is a symbol, then a so-called ''read-mark'' handler is defined.44In that case the handler procedure will be called when a character-sequence of the45form {{#!SYMBOL}} is encountered.4647You can undo special handling of read-syntax by passing {{#f}} as the second argument48(if the syntax was previously defined via {{set-read-syntax!}}).4950As a special case, your handler can return zero values, via {{(values)}}. This causes51the reader to completely ignore whatever input you've read, rather than returning some52possibly unspecified value. This can be useful in macro context, reading comments,53conditional compilation, and so forth. Available in CHICKEN 4.6.6 and later.5455Note that all of CHICKEN's special non-standard read-syntax is handled directly by the reader.56To disable built-in read-syntax, define a handler that triggers an error (for example).575859=== set-sharp-read-syntax!6061<procedure>(set-sharp-read-syntax! CHAR-OR-SYMBOL PROC)</procedure>6263Similar to {{set-read-syntax!}}, but allows defining new {{#<CHAR> ...}} reader syntax.64If the first argument is a symbol, then this procedure is equivalent to {{set-read-syntax!}}.6566{{PROC}} may be {{#f}} to disable previously defined "sharp" read syntax.676869=== set-parameterized-read-syntax!7071<procedure>(set-parameterized-read-syntax! CHAR-OR-SYMBOL PROC)</procedure>7273Similar to {{set-sharp-read-syntax!}}, but intended for defining reader syntax of the74form {{#<NUMBER><CHAR> ...}}. The handler procedure {{PROC}} will be called with two75arguments: the input port and the number preceding76the dispatching character.77If the first argument is a symbol, then this procedure is equivalent to {{set-read-syntax!}}.7879{{PROC}} may be {{#f}} to disable previously defined parameterized read syntax.808182=== copy-read-table8384<procedure>(copy-read-table READ-TABLE)</procedure>8586Returns a copy of the given read-table. You can access the currently87active read-table with {{(current-read-table)}}. This procedure can88be useful to restore an old read-table after temporarily introducing89new read syntax.909192=== current-read-table9394<parameter>(current-read-table)</parameter>9596A read-table object that holds read-procedures for special non-standard97read-syntax (see {{set-read-syntax!}} for more information).9899100---101Previous: [[Module (chicken random)]]102103Next: [[Module (chicken repl)]]