84 lines
92 KiB
HTML
84 lines
92 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
|
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=0.8"/><title>17.2 Reader Extensions</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="icons.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../doc-site.css" title="default"/><script type="text/javascript" src="../scribble-common.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../doc-site.js"></script><script type="text/javascript" src="../local-redirect/local-redirect.js"></script><script type="text/javascript" src="../local-redirect/local-user-redirect.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="doc-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_0");">►</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">The Racket Guide</a></td></tr></table></div><div class="tocviewsublisttop" style="display: none;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1 </td><td><a href="intro.html" class="tocviewlink" data-pltdoc="x">Welcome to Racket</a></td></tr><tr><td align="right">2 </td><td><a href="to-scheme.html" class="tocviewlink" data-pltdoc="x">Racket Essentials</a></td></tr><tr><td align="right">3 </td><td><a href="datatypes.html" class="tocviewlink" data-pltdoc="x">Built-<wbr></wbr>In Datatypes</a></td></tr><tr><td align="right">4 </td><td><a href="scheme-forms.html" class="tocviewlink" data-pltdoc="x">Expressions and Definitions</a></td></tr><tr><td align="right">5 </td><td><a href="define-struct.html" class="tocviewlink" data-pltdoc="x">Programmer-<wbr></wbr>Defined Datatypes</a></td></tr><tr><td align="right">6 </td><td><a href="modules.html" class="tocviewlink" data-pltdoc="x">Modules</a></td></tr><tr><td align="right">7 </td><td><a href="contracts.html" class="tocviewlink" data-pltdoc="x">Contracts</a></td></tr><tr><td align="right">8 </td><td><a href="i_o.html" class="tocviewlink" data-pltdoc="x">Input and Output</a></td></tr><tr><td align="right">9 </td><td><a href="regexp.html" class="tocviewlink" data-pltdoc="x">Regular Expressions</a></td></tr><tr><td align="right">10 </td><td><a href="control.html" class="tocviewlink" data-pltdoc="x">Exceptions and Control</a></td></tr><tr><td align="right">11 </td><td><a href="for.html" class="tocviewlink" data-pltdoc="x">Iterations and Comprehensions</a></td></tr><tr><td align="right">12 </td><td><a href="match.html" class="tocviewlink" data-pltdoc="x">Pattern Matching</a></td></tr><tr><td align="right">13 </td><td><a href="classes.html" class="tocviewlink" data-pltdoc="x">Classes and Objects</a></td></tr><tr><td align="right">14 </td><td><a href="units.html" class="tocviewlink" data-pltdoc="x">Units</a></td></tr><tr><td align="right">15 </td><td><a href="reflection.html" class="tocviewlink" data-pltdoc="x">Reflection and Dynamic Evaluation</a></td></tr><tr><td align="right">16 </td><td><a href="macros.html" class="tocviewlink" data-pltdoc="x">Macros</a></td></tr><tr><td align="right">17 </td><td><a href="languages.html" class="tocviewselflink" data-pltdoc="x">Creating Languages</a></td></tr><tr><td
|
||
|
the <span class="RktMeta">#reader</span> form. A reader extension is implemented
|
||
|
as a module that is named after <span class="RktMeta">#reader</span>. The module
|
||
|
exports functions that parse raw characters into a form to be consumed
|
||
|
by the <a href="Pairs__Lists__and_Racket_Syntax.html#%28tech._expander%29" class="techoutside" data-pltdoc="x"><span class="techinside">expander</span></a> layer.</p><p>The syntax of <span class="RktMeta">#reader</span> is</p><blockquote class="SCodeFlow"><p><span class="RktInBG"><span class="hspace"></span><span class="RktIn">#reader</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">module-path</span>›</span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">reader-specific</span>›</span></p></blockquote><p>where <span class="sroman">‹<span style="font-style: italic">module-path</span>›</span> names a module that provides
|
||
|
<span class="RktSym">read</span> and <span class="RktSym">read-syntax</span> functions. The
|
||
|
<span class="sroman">‹<span style="font-style: italic">reader-specific</span>›</span> part is a sequence of characters that is
|
||
|
parsed as determined by the <span class="RktSym">read</span> and
|
||
|
<span class="RktSym">read-syntax</span> functions from <span class="sroman">‹<span style="font-style: italic">module-path</span>›</span>.</p><p>For example, suppose that file <span class="stt">"five.rkt"</span> contains</p><blockquote class="SCodeFlow"><blockquote class="Rfilebox"><p class="Rfiletitle"><span class="Rfilename"><span class="stt">"five.rkt"</span></span></p><blockquote class="Rfilecontent"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a><span class="hspace"> </span><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">racket/base</span></a></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._provide%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">provide</a></span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read</a></span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read-syntax%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read-syntax</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read</a></span><span class="hspace"> </span><span class="RktSym">in</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">list</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Byte_and_String_Input.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read-string%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read-string</a></span><span class="hspace"> </span><span class="RktVal">5</span><span class="hspace"> </span><span class="RktSym">in</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="h
|
||
|
functions of <span class="stt">"five.rkt"</span> both read five characters from the
|
||
|
input stream and put them into a string and then a list. The reader
|
||
|
functions from <span class="stt">"five.rkt"</span> are not obliged to follow Racket
|
||
|
lexical conventions and treat the continuous sequence <span class="RktInBG"><span class="hspace"></span><span class="RktIn">234567</span><span class="hspace"></span></span>
|
||
|
as a single number. Since only the <span class="RktInBG"><span class="hspace"></span><span class="RktIn">23456</span><span class="hspace"></span></span> part is consumed by
|
||
|
<span class="RktSym">read</span> or <span class="RktSym">read-syntax</span>, the <span class="RktInBG"><span class="hspace"></span><span class="RktIn">7</span><span class="hspace"></span></span>
|
||
|
remains to be parsed in the usual Racket way. Similarly, the reader
|
||
|
functions from <span class="stt">"five.rkt"</span> are not obliged to ignore
|
||
|
whitespace, and</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a><span class="hspace"> </span><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">racket/base</span></a></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktVal">'</span><span class="RktVal">(</span><span class="RktVal">1</span><span class="hspace"> </span><span class="RktMeta">#reader</span><span class="RktVal">"five.rkt"</span><span class="hspace"> </span><span class="stt">2345</span><span class="RktVal">67</span><span class="hspace"> </span><span class="RktVal">8</span><span class="RktVal">)</span></td></tr></table></blockquote><p>is equivalent to</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a><span class="hspace"> </span><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">racket/base</span></a></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktVal">'</span><span class="RktVal">(</span><span class="RktVal">1</span><span class="hspace"> </span><span class="RktVal">(</span><span class="RktVal">" 2345"</span><span class="RktVal">)</span><span class="hspace"> </span><span class="RktVal">67</span><span class="hspace"> </span><span class="RktVal">8</span><span class="RktVal">)</span></td></tr></table></blockquote><p>since the first character immediately after <span class="RktVal">"five.rkt"</span> is a
|
||
|
space.</p><p>A <span class="RktMeta">#reader</span> form can be used in the <a href="intro.html#%28tech._repl%29" class="techoutside" data-pltdoc="x"><span class="techinside">REPL</span></a>, too:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktVal">'</span><span class="RktMeta">#reader</span><span class="RktVal">"five.rkt"</span><span class="stt">abcde</span></td></tr><tr><td><p><span class="RktRes">'("abcde")</span></p></td></tr></table></blockquote><h5 x-source-module="(lib "scribblings/guide/guide.scrbl")" x-source-pkg="racket-doc" x-part-tag=""Source_Locations"">17.2.1<tt> </tt><a name="(part._.Source_.Locations)"></a>Source Locations</h5><p>The difference between <span class="RktSym">read</span> and
|
||
|
<span class="RktSym">read-syntax</span> is that <span class="RktSym">read</span> is meant to be
|
||
|
used for data while <span class="RktSym">read-syntax</span> is meant to be used to
|
||
|
parse programs. More precisely, the <span class="RktSym">read</span> function will
|
||
|
be used when the enclosing stream is being parsed by the Racket
|
||
|
<span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read</a></span>, and <span class="RktSym">read-syntax</span> is used when the
|
||
|
enclosing stream is being parsed by the Racket <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read-syntax%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read-syntax</a></span>
|
||
|
function. Nothing requires <span class="RktSym">read</span> and
|
||
|
<span class="RktSym">read-syntax</span> to parse input in the same way, but making
|
||
|
them different would confuse programmers and tools.</p><p>The <span class="RktSym">read-syntax</span> function can return the same kind of
|
||
|
value as <span class="RktSym">read</span>, but it should normally return a
|
||
|
<a href="stx-obj.html#%28tech._syntax._object%29" class="techoutside" data-pltdoc="x"><span class="techinside">syntax object</span></a> that connects the parsed expression with source
|
||
|
locations. Unlike the <span class="stt">"five.rkt"</span> example, the
|
||
|
<span class="RktSym">read-syntax</span> function is typically implemented directly
|
||
|
to produce <a href="stx-obj.html#%28tech._syntax._object%29" class="techoutside" data-pltdoc="x"><span class="techinside">syntax objects</span></a>, and then <span class="RktSym">read</span> can use
|
||
|
<span class="RktSym">read-syntax</span> and strip away <a href="stx-obj.html#%28tech._syntax._object%29" class="techoutside" data-pltdoc="x"><span class="techinside">syntax object</span></a>
|
||
|
wrappers to produce a raw result.</p><p>The following <span class="stt">"arith.rkt"</span> module implements a reader to
|
||
|
parse simple infix arithmetic expressions into Racket forms. For
|
||
|
example, <span class="RktInBG"><span class="hspace"></span><span class="RktIn">1*2+3</span><span class="hspace"></span></span> parses into the Racket form <span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">+</a></span><span class="stt"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252A%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">*</a></span><span class="stt"> </span><span class="RktVal">1</span><span class="stt"> </span><span class="RktVal">2</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktVal">3</span><span class="RktPn">)</span>. The supported operators are <span class="RktInBG"><span class="hspace"></span><span class="RktIn">+</span><span class="hspace"></span></span>, <span class="RktInBG"><span class="hspace"></span><span class="RktIn">-</span><span class="hspace"></span></span>,
|
||
|
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">*</span><span class="hspace"></span></span>, and <span class="RktInBG"><span class="hspace"></span><span class="RktIn">/</span><span class="hspace"></span></span>, while operands can be unsigned integers
|
||
|
or single-letter variables. The implementation uses
|
||
|
<span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=linecol.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._port-next-location%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">port-next-location</a></span> to obtain the current source location, and
|
||
|
it uses <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._datum-%7E3esyntax%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">datum->syntax</a></span> to turn raw values into <a href="stx-obj.html#%28tech._syntax._object%29" class="techoutside" data-pltdoc="x"><span class="techinside">syntax
|
||
|
objects</span></a>.</p><blockquote class="SCodeFlow"><blockquote class="Rfilebox"><p class="Rfiletitle"><span class="Rfilename"><span class="stt">"arith.rkt"</span></span></p><blockquote class="Rfilecontent"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a><span class="hspace"> </span><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">racket</span></a></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._require%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">require</a></span><span class="hspace"> </span><span class="RktSym">syntax/readerr</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._provide%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">provide</a></span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read</a></span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read-syntax%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read-syntax</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read</a></span><span class="hspace"> </span><span class="RktSym">in</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._syntax-%7E3edatum%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">syntax->datum</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read-syntax%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read-syntax</a></span><span class="hspace"> </span><span class="RktVal">#f</span><span class="hspace"> </span><span class="RktSym">in</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/rele
|
||
|
then its parse result will be treated as a Racket expression. If it is
|
||
|
used in a quoted form, however, then it just produces a number or a
|
||
|
list:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktMeta">#reader</span><span class="RktVal">"arith.rkt"</span><span class="hspace"> </span><span class="stt">1*2+3</span></td></tr><tr><td><p><span class="RktRes">5</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktVal">'</span><span class="RktMeta">#reader</span><span class="RktVal">"arith.rkt"</span><span class="hspace"> </span><span class="stt">1*2+3</span></td></tr><tr><td><p><span class="RktRes">'(+ (* 1 2) 3)</span></p></td></tr></table></blockquote><p>The <span class="stt">"arith.rkt"</span> reader could also be used in positions that
|
||
|
make no sense. Since the <span class="RktSym">read-syntax</span> implementation
|
||
|
tracks source locations, syntax errors can at least refer to parts of
|
||
|
the input in terms of their original locations (at the beginning of
|
||
|
the error message):</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let</a></span><span class="hspace"> </span><span class="RktMeta">#reader</span><span class="RktVal">"arith.rkt"</span><span class="hspace"> </span><span class="stt">1*2+3</span><span class="hspace"> </span><span class="RktVal">8</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">repl:1:27: let: bad syntax (not an identifier and expression</span></p></td></tr><tr><td><p><span class="RktErr">for a binding)</span></p></td></tr><tr><td><p><span class="RktErr"></span><span class="hspace"> </span><span class="RktErr">at: +</span></p></td></tr><tr><td><p><span class="RktErr"></span><span class="hspace"> </span><span class="RktErr">in: (let (+ (* 1 2) 3) 8)</span></p></td></tr></table></blockquote><h5 x-source-module="(lib "scribblings/guide/guide.scrbl")" x-source-pkg="racket-doc" x-part-tag=""readtable"">17.2.2<tt> </tt><a name="(part._readtable)"></a>Readtables</h5><p>A reader extension’s ability to parse input characters in an arbitrary
|
||
|
way can be powerful, but many cases of lexical extension call for a
|
||
|
less general but more composable approach. In much the same way that
|
||
|
the <a href="Pairs__Lists__and_Racket_Syntax.html#%28tech._expander%29" class="techoutside" data-pltdoc="x"><span class="techinside">expander</span></a> level of Racket syntax can be extended through
|
||
|
<a href="macros.html#%28tech._macro%29" class="techoutside" data-pltdoc="x"><span class="techinside">macros</span></a>, the <a href="Pairs__Lists__and_Racket_Syntax.html#%28tech._reader%29" class="techoutside" data-pltdoc="x"><span class="techinside">reader</span></a> level of Racket syntax can be
|
||
|
composably extended through a <a name="(tech._readtable)"></a><span style="font-style: italic">readtable</span>.</p><p>The Racket reader is a recursive-descent parser, and the
|
||
|
<a href="hash-reader.html#%28tech._readtable%29" class="techoutside" data-pltdoc="x"><span class="techinside">readtable</span></a> maps characters to parsing handlers. For example, the
|
||
|
default readtable maps <span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span> to a handler that recursively
|
||
|
parses subforms until it finds a <span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span>. The
|
||
|
<span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._current-readtable%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">current-readtable</a></span> <a href="parameterize.html#%28tech._parameter%29" class="techoutside" data-pltdoc="x"><span class="techinside">parameter</span></a> determines the
|
||
|
<a href="hash-reader.html#%28tech._readtable%29" class="techoutside" data-pltdoc="x"><span class="techinside">readtable</span></a> that is used by <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read</a></span> or
|
||
|
<span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read-syntax%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read-syntax</a></span>. Rather than parsing raw characters directly, a
|
||
|
reader extension can install an extended <a href="hash-reader.html#%28tech._readtable%29" class="techoutside" data-pltdoc="x"><span class="techinside">readtable</span></a> and then
|
||
|
chain to <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read</a></span> or <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read-syntax%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read-syntax</a></span>.</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="finger.png" alt="+" width="24" height="24"/></span>See <a href="parameterize.html" data-pltdoc="x">Dynamic Binding: <span class="RktSym"><span class="RktStxLink">parameterize</span></span></a> for an introduction to
|
||
|
<a href="parameterize.html#%28tech._parameter%29" class="techoutside" data-pltdoc="x"><span class="techinside">parameters</span></a>.</p></blockquote></blockquote></blockquote><p>The <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=readtables.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._make-readtable%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">make-readtable</a></span> function constructs a new <a href="hash-reader.html#%28tech._readtable%29" class="techoutside" data-pltdoc="x"><span class="techinside">readtable</span></a>
|
||
|
as an extension of an existing one. It accepts a sequence of
|
||
|
specifications in terms of a character, a type of mapping for the
|
||
|
character, and (for certain types of mappings) a parsing
|
||
|
procedure. For example, to extend the readtable so that <span class="RktInBG"><span class="hspace"></span><span class="RktIn">$</span><span class="hspace"></span></span>
|
||
|
can be used to start and end infix expressions, implement a
|
||
|
<span class="RktSym">read-dollar</span> function and use:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=readtables.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._make-readtable%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">make-readtable</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._current-readtable%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">current-readtable</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">#\$</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">terminating-macro</span><span class="hspace"> </span><span class="RktSym">read-dollar</span><span class="RktPn">)</span></td></tr></table></blockquote><p>The protocol for <span class="RktSym">read-dollar</span> requires the function to accept
|
||
|
different numbers of arguments depending on whether it is being used
|
||
|
in <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read</a></span> or <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read-syntax%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read-syntax</a></span> mode. In <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read</a></span> mode,
|
||
|
the parser function is given two arguments: the character that
|
||
|
triggered the parser function and the input port that is being
|
||
|
read. In <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read-syntax%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read-syntax</a></span> mode, the function must accept four
|
||
|
additional arguments that provide the source location of the
|
||
|
character.</p><p>The following <span class="stt">"dollar.rkt"</span> module defines a
|
||
|
<span class="RktSym">read-dollar</span> function in terms of the <span class="RktSym">read</span> and
|
||
|
<span class="RktSym">read-syntax</span> functions provided by <span class="stt">"arith.rkt"</span>,
|
||
|
and it puts <span class="RktSym">read-dollar</span> together with new <span class="RktSym">read</span> and
|
||
|
<span class="RktSym">read-syntax</span> functions that install the readtable and
|
||
|
chain to Racket’s <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read</a></span> or <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read-syntax%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read-syntax</a></span>:</p><blockquote class="SCodeFlow"><blockquote class="Rfilebox"><p class="Rfiletitle"><span class="Rfilename"><span class="stt">"dollar.rkt"</span></span></p><blockquote class="Rfilecontent"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a><span class="hspace"> </span><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">racket</span></a></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._require%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">require</a></span><span class="hspace"> </span><span class="RktSym">syntax/readerr</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._prefix-in%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">prefix-in</a></span><span class="hspace"> </span><span class="RktSym">arith:</span><span class="hspace"> </span><span class="RktVal">"arith.rkt"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._provide%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">provide</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._rename-out%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">rename-out</a></span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym">$-read</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read</a></span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym">$-read-syntax</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read-syntax%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">read-syntax</a></span><span class="RktPn">]</span><span class="RktPn">)</span><spa
|
||
|
used at the beginning of an expression to enable multiple uses of
|
||
|
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">$</span><span class="hspace"></span></span> that switch to infix arithmetic:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktMeta">#reader</span><span class="RktVal">"dollar.rkt"</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let</a></span><span class="stt"> </span><span class="RktPn">(</span><span class="RktPn">[</span><span class="RktSym">a</span><span class="stt"> </span><span class="stt">$1*2+3$</span><span class="RktPn">]</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">b</span><span class="stt"> </span><span class="stt">$5/6$</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">$a+b$</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">35/6</span></p></td></tr></table></blockquote><div class="navsetbottom"><span class="navleft"><form class="searchform"><input class="searchbox" id="searchbox" type="text" tabindex="1" placeholder="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, "8.6", "../");"/></form> <a href="https://docs.racket-lang.org/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot("8.6");">top</a><span class="tocsettoggle"> <a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright"> <a href="module-languages.html" title="backward to "17.1 Module Languages"" data-pltdoc="x">← prev</a> <a href="languages.html" title="up to "17 Creating Languages"" data-pltdoc="x">up</a> <a href="hash-languages.html" title="forward to "17.3 Defining new #lang Languages"" data-pltdoc="x">next →</a></span> </div></div></div><div id="contextindicator"> </div></body></html>
|