Files
Nim/filters.html
github-actions[bot] c64aa1769b Deploy to GitHub pages
2025-11-12 18:11:48 +00:00

247 lines
21 KiB
HTML

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- This file is generated by Nim. -->
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Source Code Filters</title>
<!-- Google fonts -->
<link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
<!-- Favicon -->
<link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
<link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
<!-- JS -->
<script type="text/javascript" src="dochack.js?v=2.3.1"></script>
</head>
<body>
<div class="document" id="documentId">
<div class="container">
<h1 class="title">Source Code Filters</h1>
<div class="row">
<div class="three columns">
<div class="theme-select-wrapper">
<label for="theme-select">Theme:&nbsp;</label>
<select id="theme-select" onchange="setTheme(this.value)">
<option value="auto">🌗 Match OS</option>
<option value="dark">🌑 Dark</option>
<option value="light">🌕 Light</option>
</select>
</div>
<div id="global-links">
<ul class="simple-boot">
<li><a href="manual.html">Manual</a></li>
<li><a href="lib.html">Standard library</a></li>
<li> <a id="indexLink" href="theindex.html">Index</a></li>
<li><a href="compiler/theindex.html">Compiler docs</a></li>
<li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
<li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
</ul>
</div>
<div id="searchInputDiv">
Search: <input type="search" id="searchInput"
oninput="search()" />
</div>
<div class="search-groupby">
Group by:
<select onchange="groupBy(this.value)">
<option value="section">Section</option>
<option value="type">Type</option>
</select>
</div>
<ul class="simple simple-toc" id="toc-list">
<li><a class="reference" id="usage_toc" href="#usage">Usage</a></li>
<li><a class="reference" id="pipe-operator_toc" href="#pipe-operator">Pipe operator</a></li>
<li><a class="reference" id="available-filters_toc" href="#available-filters">Available filters</a></li>
<ul class="simple"><li><a class="reference" id="available-filters-replace-filter_toc" href="#available-filters-replace-filter">Replace filter</a></li>
<li><a class="reference" id="available-filters-strip-filter_toc" href="#available-filters-strip-filter">Strip filter</a></li>
<li><a class="reference" id="available-filters-stdtmpl-filter_toc" href="#available-filters-stdtmpl-filter">StdTmpl filter</a></li>
</ul>
</ul>
</div>
<div class="nine columns" id="content">
<a href="https://github.com/nim-lang/Nim/tree/devel/doc/filters.md#L1" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/doc/filters.md#L1" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
<div id="tocRoot"></div>
<p class="module-desc"><p>A <tt class="docutils literal"><span class="pre">Source Code Filter (SCF)</span></tt> transforms the input character stream to an in-memory output stream before parsing. A filter can be used to provide templating systems or preprocessors.</p>
<p>To use a filter for a source file the <tt class="docutils literal"><span class="pre">#?</span></tt> notation is used:</p>
<pre>#? stdtmpl(subsChar = '$', metaChar = '#')
#proc generateXML(name, age: string): string =
# result = &quot;&quot;
&lt;xml&gt;
&lt;name&gt;$name&lt;/name&gt;
&lt;age&gt;$age&lt;/age&gt;
&lt;/xml&gt;</pre>
<p>As the example shows, passing arguments to a filter can be done just like an ordinary procedure call with named or positional arguments. The available parameters depend on the invoked filter. Before version 0.12.0 of the language <tt class="docutils literal"><span class="pre">#!</span></tt> was used instead of <tt class="docutils literal"><span class="pre">#?</span></tt>.</p>
<p><strong>Hint:</strong> With <tt class="docutils literal"><span class="pre option">--hint:codeBegin:on</span></tt> or <tt class="docutils literal"><span class="pre option">--verbosity:2</span></tt> (or higher) while compiling or <tt class="docutils literal"><span class="pre"><span class="program">nim</span> <span class="option">check</span></span></tt>, Nim lists the processed code after each filter application.</p>
<h1><a class="toc-backref" id="usage" href="#usage">Usage</a></h1><p>First, put your SCF code in a separate file with filters specified in the first line. <strong>Note:</strong> You can name your SCF file with any file extension you want, but the conventional extension is <tt class="docutils literal"><span class="pre">.nimf</span></tt> (it used to be <tt class="docutils literal"><span class="pre">.tmpl</span></tt> but that was too generic, for example preventing github to recognize it as Nim source file).</p>
<p>If we use <tt class="docutils literal"><span class="pre">generateXML</span></tt> code shown above and call the SCF file <tt class="docutils literal"><span class="pre">xmlGen.nimf</span></tt> In your <tt class="docutils literal"><span class="pre">main.nim</span></tt>:</p>
<p><pre class="listing"><span class="Keyword">include</span> <span class="StringLit">&quot;xmlGen.nimf&quot;</span>
<span class="Identifier">echo</span> <span class="Identifier">generateXML</span><span class="Punctuation">(</span><span class="StringLit">&quot;John Smith&quot;</span><span class="Punctuation">,</span><span class="StringLit">&quot;42&quot;</span><span class="Punctuation">)</span></pre></p>
<h1><a class="toc-backref" id="pipe-operator" href="#pipe-operator">Pipe operator</a></h1><p>Filters can be combined with the <tt class="docutils literal"><span class="pre">|</span></tt> pipe operator:</p>
<pre>#? strip(startswith=&quot;&lt;&quot;) | stdtmpl
#proc generateXML(name, age: string): string =
# result = &quot;&quot;
&lt;xml&gt;
&lt;name&gt;$name&lt;/name&gt;
&lt;age&gt;$age&lt;/age&gt;
&lt;/xml&gt;</pre>
<h1><a class="toc-backref" id="available-filters" href="#available-filters">Available filters</a></h1>
<h2><a class="toc-backref" id="available-filters-replace-filter" href="#available-filters-replace-filter">Replace filter</a></h2><p>The replace filter replaces substrings in each line.</p>
<p>Parameters and their defaults:</p>
<ul class="simple"><li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">sub: string = &quot;&quot;</span></tt></dt>
<dd>the substring that is searched for</dd>
</dl>
</li>
<li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">by: string = &quot;&quot;</span></tt></dt>
<dd>the string the substring is replaced with</dd>
</dl>
</li>
</ul>
<h2><a class="toc-backref" id="available-filters-strip-filter" href="#available-filters-strip-filter">Strip filter</a></h2><p>The strip filter simply removes leading and trailing whitespace from each line.</p>
<p>Parameters and their defaults:</p>
<ul class="simple"><li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">startswith: string = &quot;&quot;</span></tt></dt>
<dd>strip only the lines that start with <em>startswith</em> (ignoring leading whitespace). If empty every line is stripped.</dd>
</dl>
</li>
<li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">leading: bool = true</span></tt></dt>
<dd>strip leading whitespace</dd>
</dl>
</li>
<li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">trailing: bool = true</span></tt></dt>
<dd>strip trailing whitespace</dd>
</dl>
</li>
</ul>
<h2><a class="toc-backref" id="available-filters-stdtmpl-filter" href="#available-filters-stdtmpl-filter">StdTmpl filter</a></h2><p>The stdtmpl filter provides a simple templating engine for Nim. The filter uses a line based parser: Lines prefixed with a <em>meta character</em> (default: <tt class="docutils literal"><span class="pre">#</span></tt>) contain Nim code, other lines are verbatim. Because indentation-based parsing is not suited for a templating engine, control flow statements need <tt class="docutils literal"><span class="pre">end X</span></tt> delimiters.</p>
<p>Parameters and their defaults:</p>
<ul class="simple"><li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">metaChar: char = '#'</span></tt></dt>
<dd>prefix for a line that contains Nim code</dd>
</dl>
</li>
<li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">subsChar: char = '$'</span></tt></dt>
<dd>prefix for a Nim expression within a template line</dd>
</dl>
</li>
<li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">conc: string = &quot; &amp; &quot;</span></tt></dt>
<dd>the operation for concatenation</dd>
</dl>
</li>
<li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">emit: string = &quot;result.add&quot;</span></tt></dt>
<dd>the operation to emit a string literal</dd>
</dl>
</li>
<li><dl class="docutils"><dt><tt class="docutils literal"><span class="pre">toString: string = &quot;$&quot;</span></tt></dt>
<dd>the operation that is applied to each expression</dd>
</dl>
</li>
</ul>
<p>Example:</p>
<pre>#? stdtmpl | standard
#proc generateHTMLPage(title, currentTab, content: string,
# tabs: openArray[string]): string =
# result = &quot;&quot;
&lt;head&gt;&lt;title&gt;$title&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;menu&quot;&gt;
&lt;ul&gt;
#for tab in items(tabs):
#if currentTab == tab:
&lt;li&gt;&lt;a id=&quot;selected&quot;
#else:
&lt;li&gt;&lt;a
#end if
href=&quot;${tab}.html&quot;&gt;$tab&lt;/a&gt;&lt;/li&gt;
#end for
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&quot;content&quot;&gt;
$content
A dollar: $$.
&lt;/div&gt;
&lt;/body&gt;</pre>
<p>The filter transforms this into:</p>
<p><pre class="listing"><span class="Keyword">proc</span> <span class="Identifier">generateHTMLPage</span><span class="Punctuation">(</span><span class="Identifier">title</span><span class="Punctuation">,</span> <span class="Identifier">currentTab</span><span class="Punctuation">,</span> <span class="Identifier">content</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">,</span>
<span class="Identifier">tabs</span><span class="Punctuation">:</span> <span class="Identifier">openArray</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">]</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">string</span> <span class="Operator">=</span>
<span class="Identifier">result</span> <span class="Operator">=</span> <span class="StringLit">&quot;&quot;</span>
<span class="Identifier">result</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="StringLit">&quot;&lt;head&gt;&lt;title&gt;&quot;</span> <span class="Operator">&amp;</span> <span class="Operator">$</span><span class="Punctuation">(</span><span class="Identifier">title</span><span class="Punctuation">)</span> <span class="Operator">&amp;</span> <span class="StringLit">&quot;&lt;/title&gt;&lt;/head&gt;</span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span> <span class="Operator">&amp;</span>
<span class="StringLit">&quot;&lt;body&gt;</span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span> <span class="Operator">&amp;</span>
<span class="StringLit">&quot; &lt;div id=</span><span class="EscapeSequence">\&quot;</span><span class="StringLit">menu</span><span class="EscapeSequence">\&quot;</span><span class="StringLit">&gt;</span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span> <span class="Operator">&amp;</span>
<span class="StringLit">&quot; &lt;ul&gt;</span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span><span class="Punctuation">)</span>
<span class="Keyword">for</span> <span class="Identifier">tab</span> <span class="Keyword">in</span> <span class="Identifier">items</span><span class="Punctuation">(</span><span class="Identifier">tabs</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Keyword">if</span> <span class="Identifier">currentTab</span> <span class="Operator">==</span> <span class="Identifier">tab</span><span class="Punctuation">:</span>
<span class="Identifier">result</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="StringLit">&quot; &lt;li&gt;&lt;a id=</span><span class="EscapeSequence">\&quot;</span><span class="StringLit">selected</span><span class="EscapeSequence">\&quot;</span><span class="StringLit"> </span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span><span class="Punctuation">)</span>
<span class="Keyword">else</span><span class="Punctuation">:</span>
<span class="Identifier">result</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="StringLit">&quot; &lt;li&gt;&lt;a</span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span><span class="Punctuation">)</span>
<span class="Comment">#end</span>
<span class="Identifier">result</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="StringLit">&quot; href=</span><span class="EscapeSequence">\&quot;</span><span class="StringLit">&quot;</span> <span class="Operator">&amp;</span> <span class="Operator">$</span><span class="Punctuation">(</span><span class="Identifier">tab</span><span class="Punctuation">)</span> <span class="Operator">&amp;</span> <span class="StringLit">&quot;.html</span><span class="EscapeSequence">\&quot;</span><span class="StringLit">&gt;&quot;</span> <span class="Operator">&amp;</span> <span class="Operator">$</span><span class="Punctuation">(</span><span class="Identifier">tab</span><span class="Punctuation">)</span> <span class="Operator">&amp;</span> <span class="StringLit">&quot;&lt;/a&gt;&lt;/li&gt;</span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span><span class="Punctuation">)</span>
<span class="Comment">#end</span>
<span class="Identifier">result</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="StringLit">&quot; &lt;/ul&gt;</span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span> <span class="Operator">&amp;</span>
<span class="StringLit">&quot; &lt;/div&gt;</span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span> <span class="Operator">&amp;</span>
<span class="StringLit">&quot; &lt;div id=</span><span class="EscapeSequence">\&quot;</span><span class="StringLit">content</span><span class="EscapeSequence">\&quot;</span><span class="StringLit">&gt;</span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span> <span class="Operator">&amp;</span>
<span class="StringLit">&quot; &quot;</span> <span class="Operator">&amp;</span> <span class="Operator">$</span><span class="Punctuation">(</span><span class="Identifier">content</span><span class="Punctuation">)</span> <span class="Operator">&amp;</span> <span class="StringLit">&quot;</span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span> <span class="Operator">&amp;</span>
<span class="StringLit">&quot; A dollar: $.</span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span> <span class="Operator">&amp;</span>
<span class="StringLit">&quot; &lt;/div&gt;</span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span> <span class="Operator">&amp;</span>
<span class="StringLit">&quot;&lt;/body&gt;</span><span class="EscapeSequence">\n</span><span class="StringLit">&quot;</span><span class="Punctuation">)</span></pre></p>
<p>Each line that does not start with the meta character (ignoring leading whitespace) is converted to a string literal that is added to <tt class="docutils literal"><span class="pre">result</span></tt>.</p>
<p>The substitution character introduces a Nim expression <em>e</em> within the string literal. <em>e</em> is converted to a string with the <em>toString</em> operation which defaults to <tt class="docutils literal"><span class="pre">$</span></tt>. For strong type checking, set <tt class="docutils literal"><span class="pre">toString</span></tt> to the empty string. <em>e</em> must match this PEG pattern:</p>
<pre>e &lt;- [a-zA-Z\128-\255][a-zA-Z0-9\128-\255_.]* / '{' x '}'
x &lt;- '{' x+ '}' / [^}]*</pre>
<p>To produce a single substitution character it has to be doubled: <tt class="docutils literal"><span class="pre">$$</span></tt> produces <tt class="docutils literal"><span class="pre">$</span></tt>.</p>
<p>The template engine is quite flexible. It is easy to produce a procedure that writes the template code directly to a file:</p>
<pre>#? stdtmpl(emit=&quot;f.write&quot;) | standard
#proc writeHTMLPage(f: File, title, currentTab, content: string,
# tabs: openArray[string]) =
&lt;head&gt;&lt;title&gt;$title&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;menu&quot;&gt;
&lt;ul&gt;
#for tab in items(tabs):
#if currentTab == tab:
&lt;li&gt;&lt;a id=&quot;selected&quot;
#else:
&lt;li&gt;&lt;a
#end if
href=&quot;${tab}.html&quot; title = &quot;$title - $tab&quot;&gt;$tab&lt;/a&gt;&lt;/li&gt;
#end for
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&quot;content&quot;&gt;
$content
A dollar: $$.
&lt;/div&gt;
&lt;/body&gt;</pre>
</p>
</div>
</div>
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br>
<small style="color: var(--hint);">Made with Nim. Generated: 2025-11-12 18:10:40 UTC</small>
</div>
</div>
</div>
<script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
</body>
</html>