emacs.d/clones/scheme/docs.racket-lang.org/guide/hash-lang_syntax.html

43 lines
20 KiB
HTML
Raw Normal View History

2022-09-30 11:00:09 +02:00
<!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.3.1&nbsp;Designating a #lang Language</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="icons.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,&quot;tocview_0&quot;);">&#9658;</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&nbsp;</td><td><a href="intro.html" class="tocviewlink" data-pltdoc="x">Welcome to Racket</a></td></tr><tr><td align="right">2&nbsp;</td><td><a href="to-scheme.html" class="tocviewlink" data-pltdoc="x">Racket Essentials</a></td></tr><tr><td align="right">3&nbsp;</td><td><a href="datatypes.html" class="tocviewlink" data-pltdoc="x">Built-<wbr></wbr>In Datatypes</a></td></tr><tr><td align="right">4&nbsp;</td><td><a href="scheme-forms.html" class="tocviewlink" data-pltdoc="x">Expressions and Definitions</a></td></tr><tr><td align="right">5&nbsp;</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&nbsp;</td><td><a href="modules.html" class="tocviewlink" data-pltdoc="x">Modules</a></td></tr><tr><td align="right">7&nbsp;</td><td><a href="contracts.html" class="tocviewlink" data-pltdoc="x">Contracts</a></td></tr><tr><td align="right">8&nbsp;</td><td><a href="i_o.html" class="tocviewlink" data-pltdoc="x">Input and Output</a></td></tr><tr><td align="right">9&nbsp;</td><td><a href="regexp.html" class="tocviewlink" data-pltdoc="x">Regular Expressions</a></td></tr><tr><td align="right">10&nbsp;</td><td><a href="control.html" class="tocviewlink" data-pltdoc="x">Exceptions and Control</a></td></tr><tr><td align="right">11&nbsp;</td><td><a href="for.html" class="tocviewlink" data-pltdoc="x">Iterations and Comprehensions</a></td></tr><tr><td align="right">12&nbsp;</td><td><a href="match.html" class="tocviewlink" data-pltdoc="x">Pattern Matching</a></td></tr><tr><td align="right">13&nbsp;</td><td><a href="classes.html" class="tocviewlink" data-pltdoc="x">Classes and Objects</a></td></tr><tr><td align="right">14&nbsp;</td><td><a href="units.html" class="tocviewlink" data-pltdoc="x">Units</a></td></tr><tr><td align="right">15&nbsp;</td><td><a href="reflection.html" class="tocviewlink" data-pltdoc="x">Reflection and Dynamic Evaluation</a></td></tr><tr><td align="right">16&nbsp;</td><td><a href="macros.html" class="tocviewlink" data-pltdoc="x">Macros</a></td></tr><tr><td align="right">17&nbsp;</td><td><a href="languages.html" class="tocviewselflink" data-pltdoc="x">Creating Languages</a></td
syntax of a module path as used in <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&amp;rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._require%2529%2529&amp;version=8.6" class="RktStxLink Sq" data-pltdoc="x">require</a></span> or as a
<a href="module-languages.html#%28tech._module._language%29" class="techoutside" data-pltdoc="x"><span class="techinside">module language</span></a>, so that names like <a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&amp;rel=index.html&amp;version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">racket</span></a>,
<a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&amp;rel=index.html&amp;version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">racket/base</span></a>, <a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=slideshow&amp;rel=index.html&amp;version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">slideshow</span></a>, or
<a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=scribble&amp;rel=manual.html&amp;version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">scribble/manual</span></a> can be used both as <a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a>
languages and as module paths.</p><p>At the same time, the syntax of <span class="RktVar">language</span> is far more
restricted than a module path, because only <span class="RktInBG"><span class="hspace"></span><span class="RktIn">a</span><span class="hspace"></span></span>-<span class="RktInBG"><span class="hspace"></span><span class="RktIn">z</span><span class="hspace"></span></span>,
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">A</span><span class="hspace"></span></span>-<span class="RktInBG"><span class="hspace"></span><span class="RktIn">Z</span><span class="hspace"></span></span>, <span class="RktInBG"><span class="hspace"></span><span class="RktIn">0</span><span class="hspace"></span></span>-<span class="RktInBG"><span class="hspace"></span><span class="RktIn">9</span><span class="hspace"></span></span>,
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">/</span><span class="hspace"></span></span> (not at the start or end),
<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> are allowed in a
<span class="RktVar">language</span> name. These restrictions keep the syntax of
<a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a> as simple as possible. Keeping the syntax of <a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a>
simple, in turn, is important because the syntax is inherently
inflexible and non-extensible; the <a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a> protocol allows a
<span class="RktVar">language</span> to refine and define syntax in a practically
unconstrained way, but the <a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a> protocol itself must remain
fixed so that various different tools can &ldquo;boot&rdquo; into the extended
world.</p><p>Fortunately, the <a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a> protocol provides a natural way to refer
to languages in ways other than the rigid <span class="RktVar">language</span> syntax:
by defining a <span class="RktVar">language</span> that implements its own nested
protocol. We have already seen one example (in <a href="module-languages.html#%28part._s-exp%29" data-pltdoc="x">Using <span class="RktModLink"><span class="RktMod">#lang</span></span><span class="stt"> </span><span class="RktModLink"><span class="RktSym">s-exp</span></span></a>): the
<a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&amp;rel=reader.html%23%2528mod-path._s-exp%2529&amp;version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">s-exp</span></a> <span class="RktVar">language</span> allows a programmer to
specify a <a href="module-languages.html#%28tech._module._language%29" class="techoutside" data-pltdoc="x"><span class="techinside">module language</span></a> using the general <a href="module-paths.html#%28tech._module._path%29" class="techoutside" data-pltdoc="x"><span class="techinside">module path</span></a>
syntax. Meanwhile, <a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&amp;rel=reader.html%23%2528mod-path._s-exp%2529&amp;version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">s-exp</span></a> takes care of the
<a href="Pairs__Lists__and_Racket_Syntax.html#%28tech._reader%29" class="techoutside" data-pltdoc="x"><span class="techinside">reader</span></a>-level responsibilities of a <a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a> language.</p><p>Unlike <a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&amp;rel=index.html&amp;version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">racket</span></a>, <a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&amp;rel=reader.html%23%2528mod-path._s-exp%2529&amp;version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">s-exp</span></a> cannot be used as a
module path with <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&amp;rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._require%2529%2529&amp;version=8.6" class="RktStxLink Sq" data-pltdoc="x">require</a></span>. Although the syntax of
<span class="RktVar">language</span> for <a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a> overlaps with the syntax of module
paths, a <span class="RktVar">language</span> is not used directly as a module
path. Instead, a <span class="RktVar">language</span> obtains a module path by trying two
locations: first, it looks for a <span class="RktSym">reader</span> submodule of the
main module for <span class="RktVar">language</span>. If this is not a valid module path,
then <span class="RktVar">language</span> is suffixed with <span class="RktSym">/lang/reader</span>.
(If neither is a valid module path, an error is raised.) The resulting
module supplies <span class="RktSym">read</span> and <span class="RktSym">read-syntax</span>
functions using a protocol that is similar to the one for
<span class="RktMeta">#reader</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><a href="hash-reader.html" data-pltdoc="x">Reader Extensions</a> introduces <span class="RktMeta">#reader</span>.</p></blockquote></blockquote></blockquote><p>A consequence of the way that a <a href="Module_Syntax.html#%28part._hash-lang%29" class="RktModLink" data-pltdoc="x"><span class="RktMod">#lang</span></a> <span class="RktVar">language</span> is
turned into a module path is that the language must be installed in a
<a href="module-basics.html#%28tech._collection%29" class="techoutside" data-pltdoc="x"><span class="techinside">collection</span></a>, similar to the way that <span class="stt">"racket"</span> or
<span class="stt">"slideshow"</span> are collections that are distributed with Racket.
Again, however, there&rsquo;s an escape from this restriction: the
<a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&amp;rel=reader.html%23%2528mod-path._reader%2529&amp;version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">reader</span></a> language lets you specify a <a href="Pairs__Lists__and_Racket_Syntax.html#%28tech._reader%29" class="techoutside" data-pltdoc="x"><span class="techinside">reader</span></a>-level
implementation of a language using a general <a href="module-paths.html#%28tech._module._path%29" class="techoutside" data-pltdoc="x"><span class="techinside">module path</span></a>.</p><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, &quot;8.6&quot;, &quot;../&quot;);"/></form>&nbsp;&nbsp;<a href="https://docs.racket-lang.org/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot(&quot;8.6&quot;);">top</a><span class="tocsettoggle">&nbsp;&nbsp;<a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright">&nbsp;&nbsp;<a href="hash-languages.html" title="backward to &quot;17.3 Defining new #lang Languages&quot;" data-pltdoc="x">&larr; prev</a>&nbsp;&nbsp;<a href="hash-languages.html" title="up to &quot;17.3 Defining new #lang Languages&quot;" data-pltdoc="x">up</a>&nbsp;&nbsp;<a href="hash-lang_reader.html" title="forward to &quot;17.3.2 Using #lang reader&quot;" data-pltdoc="x">next &rarr;</a></span>&nbsp;</div></div></div><div id="contextindicator">&nbsp;</div></body></html>