Skip to content

Commit

Permalink
allow multiple --load-readtable flags
Browse files Browse the repository at this point in the history
  • Loading branch information
jlongster committed Jun 26, 2014
1 parent 8a4fe95 commit 2bed05c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
13 changes: 8 additions & 5 deletions doc/main/sweet.html
Expand Up @@ -878,10 +878,13 @@ <h1 id="reader-extensions"><span class="header-section-number">9</span> Reader E
}
});</code></pre>
<p>And you can set the readtable programmatically:</p>
<pre><code>sweet.setReadtable(readtable);</code></pre>
<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">sweet</span>.<span class="fu">setReadtable</span>(readtable);</code></pre>
<p>Or load it via a command line option. If you do this, the exported value of the module must be the readtable:</p>
<pre><code>sjs --load-readtable ./readtable.js</code></pre>
<p>By continually extending the current readtable, we can install the extensions in a modular way and build up a final readtable with our desired behaviors. You can pass multiple <code>--load-readtable</code> flags to load multiple readtables extensions. If multiple readtables with the same character are installed, the last one loaded wins. There is currently no way to handle these kinds of collisions; every character must have a single unique handler (or none).</p>
<pre><code>sjs --load-readtable ./readtable.js

# Or the shorthand
sjs -l ./readtable.js</code></pre>
<p>By continually extending the current readtable, we can install the extensions in a modular way and build up a final readtable with our desired behaviors. You can pass multiple <code>-l</code> (or <code>--load-readtable</code>) flags to load multiple readtables extensions. If multiple readtables with the same character are installed, the last one loaded wins. There is currently no way to handle these kinds of collisions; every character must have a single unique handler (or none).</p>
<h2 id="readtables"><span class="header-section-number">9.1</span> Readtables</h2>
<p>A readtable is an object that maps characters to handler functions, and an <code>extend</code> method. Internally, a base readtable exists which is the current readtable by default. To create a new one, you always extend an existing one as explained above:</p>
<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> readtable = <span class="ot">sweet</span>.<span class="fu">currentReadtable</span>().<span class="fu">extend</span>({
Expand All @@ -904,7 +907,7 @@ <h2 id="readtables"><span class="header-section-number">9.1</span> Readtables</h
<p>There are two functions on the sweet module for working with readtables:</p>
<ul>
<li><p><code>sweet.currentReadtable()</code> — Gets the current readtable, which always has an <code>extend</code> method on it to install new extensions.</p></li>
<li><p><code>sweet.setReadtable(rt)</code> — Mutates the reader state to use the specified readtable when reading. Most likely users will use one or more <code>--load-readtable</code> flags to <code>sjs</code>, but you can set it progammatically with this method.</p></li>
<li><p><code>sweet.setReadtable(rt)</code> — Mutates the reader state to use the specified readtable when reading. Most likely users will use one or more <code>-l</code> or <code>--load-readtable</code> flags to <code>sjs</code>, but you can set it progammatically with this method.</p></li>
</ul>
<h2 id="reader-api"><span class="header-section-number">9.2</span> Reader API</h2>
<p>A reader object is always passed as the second argument when invoking a reader extension. It has several properties that are readable and writable:</p>
Expand Down Expand Up @@ -946,7 +949,7 @@ <h2 id="reader-api"><span class="header-section-number">9.2</span> Reader API</h
<li><code>reader.isIdentifierPart(charCode)</code> — Check if <code>charCode</code> is a valid non-starting character for an identifier</li>
<li><code>reader.isLineTerminator(charCode)</code> — Check if <code>charCode</code> is a character that terminates a line</li>
<li><code>reader.peekQueued(offset)</code> — Inspects the tokens on the reader queue, returning the nth (based on <code>offset</code>) token from the front (0 is the next token). Reader extensions are invoked from <code>readToken</code>, but can return multiple tokens, so the rest are pushed onto a queue. You may need to be aware of this with recursive reader extensions or other complicated scenarios.</li>
<li><code>reader.getQueued</code> — Gets the next token from the reader queue and removes it from the queue</li>
<li><code>reader.getQueued()</code> — Gets the next token from the reader queue and removes it from the queue</li>
<li><code>reader.suppressReadError(readFunc)</code> — Suppresses exceptions from any of the <code>read*</code> functions and returns <code>null</code> instead if something went wrong. Example: <code>reader.suppressReadError(reader.readIdentifier)</code></li>
<li><code>reader.throwSyntaxError(name, message, token)</code> — Throws a syntax error. <code>name</code> is just a general identifier for your project, and <code>message</code> is the specific message to display. You must pass a <code>token</code> so it can properly locate where the error occurred in the original source.</li>
</ul>
Expand Down
19 changes: 11 additions & 8 deletions doc/main/sweet.md
Expand Up @@ -882,7 +882,7 @@ var readtable = sweet.currentReadtable().extend({

And you can set the readtable programmatically:

```
```javascript
sweet.setReadtable(readtable);
```

Expand All @@ -891,15 +891,18 @@ value of the module must be the readtable:

```
sjs --load-readtable ./readtable.js
# Or the shorthand
sjs -l ./readtable.js
```

By continually extending the current readtable, we can install the
extensions in a modular way and build up a final readtable with our
desired behaviors. You can pass multiple `--load-readtable` flags to
load multiple readtables extensions. If multiple readtables with the
same character are installed, the last one loaded wins. There is
currently no way to handle these kinds of collisions; every character
must have a single unique handler (or none).
desired behaviors. You can pass multiple `-l` (or `--load-readtable`)
flags to load multiple readtables extensions. If multiple readtables
with the same character are installed, the last one loaded wins. There
is currently no way to handle these kinds of collisions; every
character must have a single unique handler (or none).

## Readtables

Expand Down Expand Up @@ -952,7 +955,7 @@ There are two functions on the sweet module for working with readtables:

* `sweet.setReadtable(rt)` &mdash; Mutates the reader state to use the
specified readtable when reading. Most likely users will use one or
more `--load-readtable` flags to `sjs`, but you can set it
more `-l` or `--load-readtable` flags to `sjs`, but you can set it
progammatically with this method.


Expand Down Expand Up @@ -1032,7 +1035,7 @@ The following functions are simple utility functions:
`readToken`, but can return multiple tokens, so the rest are pushed
onto a queue. You may need to be aware of this with recursive reader
extensions or other complicated scenarios.
* `reader.getQueued` &mdash; Gets the next token from the reader queue
* `reader.getQueued()` &mdash; Gets the next token from the reader queue
and removes it from the queue
* `reader.suppressReadError(readFunc)` &mdash; Suppresses exceptions
from any of the `read*` functions and returns `null` instead if
Expand Down
13 changes: 10 additions & 3 deletions src/sjs.js
Expand Up @@ -34,6 +34,7 @@ var argv = require("optimist")
.describe('r', 'remove as many hygienic renames as possible (ES5 code only!)')
.boolean('readable-names')
.describe('format-indent', 'number of spaces for indentation')
.alias('l', 'load-readtable')
.describe('load-readtable', 'readtable module to install')
.argv;

Expand All @@ -50,7 +51,7 @@ exports.run = function() {
var displayHygiene = argv['step-hygiene'];
var readableNames = argv['readable-names'];
var formatIndent = parseInt(argv['format-indent'], 10);
var readtableModule = argv['load-readtable'];
var readtableModules = argv['load-readtable'];
if (formatIndent !== formatIndent) {
formatIndent = 4;
}
Expand All @@ -72,8 +73,14 @@ exports.run = function() {
return sweet.loadNodeModule(cwd, path);
});

if(readtableModule) {
sweet.setReadtable(readtableModule);
if(readtableModules) {
readtableModules = (Array.isArray(readtableModules) ?
readtableModules :
[readtableModules]);

readtableModules.forEach(function(mod) {
sweet.setReadtable(mod);
});
}

var options = {
Expand Down

0 comments on commit 2bed05c

Please sign in to comment.