407 lines
98 KiB
HTML
407 lines
98 KiB
HTML
<!DOCTYPE html> <html> <head> <title>grammar.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To … <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="browser.html"> browser.coffee </a> <a class="source" href="cake.html"> cake.coffee </a> <a class="source" href="coffee-script.html"> coffee-script.coffee </a> <a class="source" href="command.html"> command.coffee </a> <a class="source" href="grammar.html"> grammar.coffee </a> <a class="source" href="helpers.html"> helpers.coffee </a> <a class="source" href="index.html"> index.coffee </a> <a class="source" href="lexer.html"> lexer.coffee </a> <a class="source" href="nodes.html"> nodes.coffee </a> <a class="source" href="optparse.html"> optparse.coffee </a> <a class="source" href="repl.html"> repl.coffee </a> <a class="source" href="rewriter.html"> rewriter.coffee </a> <a class="source" href="scope.html"> scope.coffee </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> grammar.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <p>The CoffeeScript parser is generated by <a href="http://github.com/zaach/jison">Jison</a>
|
|
from this grammar file. Jison is a bottom-up parser generator, similar in
|
|
style to <a href="http://www.gnu.org/software/bison">Bison</a>, implemented in JavaScript.
|
|
It can recognize <a href="http://en.wikipedia.org/wiki/LR_grammar">LALR(1), LR(0), SLR(1), and LR(1)</a>
|
|
type grammars. To create the Jison parser, we list the pattern to match
|
|
on the left-hand side, and the action to take (usually the creation of syntax
|
|
tree nodes) on the right. As the parser runs, it
|
|
shifts tokens from our token stream, from left to right, and
|
|
<a href="http://en.wikipedia.org/wiki/Bottom-up_parsing">attempts to match</a>
|
|
the token sequence against the rules below. When a match can be made, it
|
|
reduces into the <a href="http://en.wikipedia.org/wiki/Terminal_and_nonterminal_symbols">nonterminal</a>
|
|
(the enclosing name at the top), and we proceed from there.</p>
|
|
|
|
<p>If you run the <code>cake build:parser</code> command, Jison constructs a parse table
|
|
from our rules and saves it into <code>lib/parser.js</code>.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <p>The only dependency is on the <strong>Jison.Parser</strong>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="p">{</span><span class="nx">Parser</span><span class="p">}</span> <span class="o">=</span> <span class="nx">require</span> <span class="s">'jison'</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <h2>Jison DSL</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <p>Since we're going to be wrapped in a function by Jison in any case, if our
|
|
action immediately returns a value, we can optimize by removing the function
|
|
wrapper and just returning the value directly.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">unwrap = </span><span class="sr">/^function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <p>Our handy DSL for Jison grammar generation, thanks to
|
|
<a href="http://github.com/creationix">Tim Caswell</a>. For every rule in the grammar,
|
|
we pass the pattern-defining string, the action to run, and extra options,
|
|
optionally. If no action is specified, we simply pass the value of the
|
|
previous nonterminal.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">o = </span><span class="nf">(patternString, action, options) -></span>
|
|
<span class="nv">patternString = </span><span class="nx">patternString</span><span class="p">.</span><span class="nx">replace</span> <span class="sr">/\s{2,}/g</span><span class="p">,</span> <span class="s">' '</span>
|
|
<span class="k">return</span> <span class="p">[</span><span class="nx">patternString</span><span class="p">,</span> <span class="s">'$$ = $1;'</span><span class="p">,</span> <span class="nx">options</span><span class="p">]</span> <span class="k">unless</span> <span class="nx">action</span>
|
|
<span class="nv">action = </span><span class="k">if</span> <span class="nv">match = </span><span class="nx">unwrap</span><span class="p">.</span><span class="nx">exec</span> <span class="nx">action</span> <span class="k">then</span> <span class="nx">match</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">else</span> <span class="s">"(</span><span class="si">#{</span><span class="nx">action</span><span class="si">}</span><span class="s">())"</span>
|
|
<span class="nv">action = </span><span class="nx">action</span><span class="p">.</span><span class="nx">replace</span> <span class="sr">/\bnew /g</span><span class="p">,</span> <span class="s">'$&yy.'</span>
|
|
<span class="nv">action = </span><span class="nx">action</span><span class="p">.</span><span class="nx">replace</span> <span class="sr">/\b(?:Block\.wrap|extend)\b/g</span><span class="p">,</span> <span class="s">'yy.$&'</span>
|
|
<span class="p">[</span><span class="nx">patternString</span><span class="p">,</span> <span class="s">"$$ = </span><span class="si">#{</span><span class="nx">action</span><span class="si">}</span><span class="s">;"</span><span class="p">,</span> <span class="nx">options</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <h2>Grammatical Rules</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>In all of the rules that follow, you'll see the name of the nonterminal as
|
|
the key to a list of alternative matches. With each match's action, the
|
|
dollar-sign variables are provided by Jison as references to the value of
|
|
their numeric position, so in this rule:</p>
|
|
|
|
<pre><code>"Expression UNLESS Expression"
|
|
</code></pre>
|
|
|
|
<p><code>$1</code> would be the value of the first <code>Expression</code>, <code>$2</code> would be the token
|
|
for the <code>UNLESS</code> terminal, and <code>$3</code> would be the value of the second
|
|
<code>Expression</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">grammar =</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <p>The <strong>Root</strong> is the top-level node in the syntax tree. Since we parse bottom-up,
|
|
all parsing must end here.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Root: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">''</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Block</span>
|
|
<span class="nx">o</span> <span class="s">'Body'</span>
|
|
<span class="nx">o</span> <span class="s">'Block TERMINATOR'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</a> </div> <p>Any list of statements and expressions, separated by line breaks or semicolons.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Body: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Line'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">Block</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="nx">$1</span><span class="p">]</span>
|
|
<span class="nx">o</span> <span class="s">'Body TERMINATOR Line'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">push</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'Body TERMINATOR'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">¶</a> </div> <p>Block and statements, which make up a line in a body.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Line: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Expression'</span>
|
|
<span class="nx">o</span> <span class="s">'Statement'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <p>Pure statements which cannot be expressions.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Statement: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Return'</span>
|
|
<span class="nx">o</span> <span class="s">'Comment'</span>
|
|
<span class="nx">o</span> <span class="s">'STATEMENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Literal</span> <span class="nx">$1</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <p>All the different types of expressions in our language. The basic unit of
|
|
CoffeeScript is the <strong>Expression</strong> -- everything that can be an expression
|
|
is one. Blocks serve as the building blocks of many other rules, making
|
|
them somewhat circular.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Expression: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Value'</span>
|
|
<span class="nx">o</span> <span class="s">'Invocation'</span>
|
|
<span class="nx">o</span> <span class="s">'Code'</span>
|
|
<span class="nx">o</span> <span class="s">'Operation'</span>
|
|
<span class="nx">o</span> <span class="s">'Assign'</span>
|
|
<span class="nx">o</span> <span class="s">'If'</span>
|
|
<span class="nx">o</span> <span class="s">'Try'</span>
|
|
<span class="nx">o</span> <span class="s">'While'</span>
|
|
<span class="nx">o</span> <span class="s">'For'</span>
|
|
<span class="nx">o</span> <span class="s">'Switch'</span>
|
|
<span class="nx">o</span> <span class="s">'Class'</span>
|
|
<span class="nx">o</span> <span class="s">'Throw'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div> <p>An indented block of expressions. Note that the <a href="rewriter.html">Rewriter</a>
|
|
will convert some postfix forms into blocks for us, by adjusting the
|
|
token stream.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Block: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'INDENT OUTDENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Block</span>
|
|
<span class="nx">o</span> <span class="s">'INDENT Body OUTDENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$2</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">¶</a> </div> <p>A literal identifier, a variable name or property.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Identifier: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'IDENTIFIER'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Literal</span> <span class="nx">$1</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">¶</a> </div> <p>Alphanumerics are separated from the other <strong>Literal</strong> matchers because
|
|
they can also serve as keys in object literals.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">AlphaNumeric: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'NUMBER'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Literal</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'STRING'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Literal</span> <span class="nx">$1</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">¶</a> </div> <p>All of our immediate values. Generally these can be passed straight
|
|
through and printed to JavaScript.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Literal: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'AlphaNumeric'</span>
|
|
<span class="nx">o</span> <span class="s">'JS'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Literal</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'REGEX'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Literal</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'DEBUGGER'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Literal</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'UNDEFINED'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Undefined</span>
|
|
<span class="nx">o</span> <span class="s">'NULL'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Null</span>
|
|
<span class="nx">o</span> <span class="s">'BOOL'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Bool</span> <span class="nx">$1</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">¶</a> </div> <p>Assignment of a variable, property, or index to a value.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Assign: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Assignable = Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'Assignable = TERMINATOR Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$4</span>
|
|
<span class="nx">o</span> <span class="s">'Assignable = INDENT Expression OUTDENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$4</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">¶</a> </div> <p>Assignment when it happens within an object literal. The difference from
|
|
the ordinary <strong>Assign</strong> is that these allow numbers and strings as keys.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">AssignObj: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'ObjAssignable'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Value</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'ObjAssignable : Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Assign</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">$1</span><span class="p">),</span> <span class="nx">$3</span><span class="p">,</span> <span class="s">'object'</span>
|
|
<span class="nx">o</span> <span class="s">'ObjAssignable :</span>
|
|
<span class="s"> INDENT Expression OUTDENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Assign</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">$1</span><span class="p">),</span> <span class="nx">$4</span><span class="p">,</span> <span class="s">'object'</span>
|
|
<span class="nx">o</span> <span class="s">'Comment'</span>
|
|
<span class="p">]</span>
|
|
|
|
<span class="nv">ObjAssignable: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Identifier'</span>
|
|
<span class="nx">o</span> <span class="s">'AlphaNumeric'</span>
|
|
<span class="nx">o</span> <span class="s">'ThisProperty'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">¶</a> </div> <p>A return statement from a function body.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Return: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'RETURN Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Return</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'RETURN'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Return</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">¶</a> </div> <p>A block comment.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Comment: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'HERECOMMENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Comment</span> <span class="nx">$1</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">¶</a> </div> <p>The <strong>Code</strong> node is the function literal. It's defined by an indented block
|
|
of <strong>Block</strong> preceded by a function arrow, with an optional parameter
|
|
list.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Code: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'PARAM_START ParamList PARAM_END FuncGlyph Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Code</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$5</span><span class="p">,</span> <span class="nx">$4</span>
|
|
<span class="nx">o</span> <span class="s">'FuncGlyph Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Code</span> <span class="p">[],</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$1</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">¶</a> </div> <p>CoffeeScript has two different symbols for functions. <code>-></code> is for ordinary
|
|
functions, and <code>=></code> is for functions bound to the current value of <em>this</em>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">FuncGlyph: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'->'</span><span class="p">,</span> <span class="nf">-></span> <span class="s">'func'</span>
|
|
<span class="nx">o</span> <span class="s">'=>'</span><span class="p">,</span> <span class="nf">-></span> <span class="s">'boundfunc'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</a> </div> <p>An optional, trailing comma.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">OptComma: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">''</span>
|
|
<span class="nx">o</span> <span class="s">','</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">¶</a> </div> <p>The list of parameters that a function accepts can be of any length.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">ParamList: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">''</span><span class="p">,</span> <span class="nf">-></span> <span class="p">[]</span>
|
|
<span class="nx">o</span> <span class="s">'Param'</span><span class="p">,</span> <span class="nf">-></span> <span class="p">[</span><span class="nx">$1</span><span class="p">]</span>
|
|
<span class="nx">o</span> <span class="s">'ParamList , Param'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'ParamList OptComma TERMINATOR Param'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">$4</span>
|
|
<span class="nx">o</span> <span class="s">'ParamList OptComma INDENT ParamList OptComma OUTDENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">$4</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">¶</a> </div> <p>A single parameter in a function definition can be ordinary, or a splat
|
|
that hoovers up the remaining arguments.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Param: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'ParamVar'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Param</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'ParamVar ...'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Param</span> <span class="nx">$1</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="kc">on</span>
|
|
<span class="nx">o</span> <span class="s">'ParamVar = Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Param</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">¶</a> </div> <p>Function Parameters</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">ParamVar: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Identifier'</span>
|
|
<span class="nx">o</span> <span class="s">'ThisProperty'</span>
|
|
<span class="nx">o</span> <span class="s">'Array'</span>
|
|
<span class="nx">o</span> <span class="s">'Object'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">¶</a> </div> <p>A splat that occurs outside of a parameter list.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Splat: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Expression ...'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Splat</span> <span class="nx">$1</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">¶</a> </div> <p>Variables and properties that can be assigned to.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">SimpleAssignable: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Identifier'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Value</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'Value Accessor'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">add</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'Invocation Accessor'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Value</span> <span class="nx">$1</span><span class="p">,</span> <span class="p">[].</span><span class="nx">concat</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'ThisProperty'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">¶</a> </div> <p>Everything that can be assigned to.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Assignable: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'SimpleAssignable'</span>
|
|
<span class="nx">o</span> <span class="s">'Array'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Value</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'Object'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Value</span> <span class="nx">$1</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">¶</a> </div> <p>The types of things that can be treated as values -- assigned to, invoked
|
|
as functions, indexed into, named as a class, etc.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Value: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Assignable'</span>
|
|
<span class="nx">o</span> <span class="s">'Literal'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Value</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'Parenthetical'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Value</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'Range'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Value</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'This'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">¶</a> </div> <p>The general group of accessors into an object, by property, by prototype
|
|
or by array index or slice.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Accessor: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'. Identifier'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Access</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'?. Identifier'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Access</span> <span class="nx">$2</span><span class="p">,</span> <span class="s">'soak'</span>
|
|
<span class="nx">o</span> <span class="s">':: Identifier'</span><span class="p">,</span> <span class="nf">-></span> <span class="p">[(</span><span class="k">new</span> <span class="nx">Access</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s">'prototype'</span><span class="p">),</span> <span class="k">new</span> <span class="nx">Access</span> <span class="nx">$2</span><span class="p">]</span>
|
|
<span class="nx">o</span> <span class="s">'::'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Access</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s">'prototype'</span>
|
|
<span class="nx">o</span> <span class="s">'Index'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">¶</a> </div> <p>Indexing into an object or array using bracket notation.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Index: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'INDEX_START IndexValue INDEX_END'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'INDEX_SOAK Index'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">extend</span> <span class="nx">$2</span><span class="p">,</span> <span class="nv">soak : </span><span class="kc">yes</span>
|
|
<span class="p">]</span>
|
|
|
|
<span class="nv">IndexValue: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Index</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'Slice'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Slice</span> <span class="nx">$1</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">¶</a> </div> <p>In CoffeeScript, an object literal is simply a list of assignments.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nb">Object</span><span class="o">:</span> <span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'{ AssignList OptComma }'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Obj</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$1</span><span class="p">.</span><span class="nx">generated</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">¶</a> </div> <p>Assignment of properties within an object literal can be separated by
|
|
comma, as in JavaScript, or simply by newline.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">AssignList: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">''</span><span class="p">,</span> <span class="nf">-></span> <span class="p">[]</span>
|
|
<span class="nx">o</span> <span class="s">'AssignObj'</span><span class="p">,</span> <span class="nf">-></span> <span class="p">[</span><span class="nx">$1</span><span class="p">]</span>
|
|
<span class="nx">o</span> <span class="s">'AssignList , AssignObj'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'AssignList OptComma TERMINATOR AssignObj'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">$4</span>
|
|
<span class="nx">o</span> <span class="s">'AssignList OptComma INDENT AssignList OptComma OUTDENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">$4</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-35">¶</a> </div> <p>Class definitions have optional bodies of prototype property assignments,
|
|
and optional references to the superclass.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Class: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'CLASS'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Class</span>
|
|
<span class="nx">o</span> <span class="s">'CLASS Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Class</span> <span class="kc">null</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'CLASS EXTENDS Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Class</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'CLASS EXTENDS Expression Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Class</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">$4</span>
|
|
<span class="nx">o</span> <span class="s">'CLASS SimpleAssignable'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Class</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'CLASS SimpleAssignable Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Class</span> <span class="nx">$2</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'CLASS SimpleAssignable EXTENDS Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Class</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$4</span>
|
|
<span class="nx">o</span> <span class="s">'CLASS SimpleAssignable EXTENDS Expression Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Class</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$4</span><span class="p">,</span> <span class="nx">$5</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">¶</a> </div> <p>Ordinary function invocation, or a chained series of calls.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Invocation: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Value OptFuncExist Arguments'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Call</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'Invocation OptFuncExist Arguments'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Call</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'SUPER'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Call</span> <span class="s">'super'</span><span class="p">,</span> <span class="p">[</span><span class="k">new</span> <span class="nx">Splat</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s">'arguments'</span><span class="p">]</span>
|
|
<span class="nx">o</span> <span class="s">'SUPER Arguments'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Call</span> <span class="s">'super'</span><span class="p">,</span> <span class="nx">$2</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-37"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-37">¶</a> </div> <p>An optional existence check on a function.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">OptFuncExist: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">''</span><span class="p">,</span> <span class="nf">-></span> <span class="kc">no</span>
|
|
<span class="nx">o</span> <span class="s">'FUNC_EXIST'</span><span class="p">,</span> <span class="nf">-></span> <span class="kc">yes</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-38">¶</a> </div> <p>The list of arguments to a function call.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Arguments: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'CALL_START CALL_END'</span><span class="p">,</span> <span class="nf">-></span> <span class="p">[]</span>
|
|
<span class="nx">o</span> <span class="s">'CALL_START ArgList OptComma CALL_END'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$2</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-39"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-39">¶</a> </div> <p>A reference to the <em>this</em> current object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">This: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'THIS'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Value</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s">'this'</span>
|
|
<span class="nx">o</span> <span class="s">'@'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Value</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s">'this'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-40"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-40">¶</a> </div> <p>A reference to a property on <em>this</em>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">ThisProperty: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'@ Identifier'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Value</span> <span class="k">new</span> <span class="nx">Literal</span><span class="p">(</span><span class="s">'this'</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="nx">Access</span><span class="p">(</span><span class="nx">$2</span><span class="p">)],</span> <span class="s">'this'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-41"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-41">¶</a> </div> <p>The array literal.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nb">Array</span><span class="o">:</span> <span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'[ ]'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Arr</span> <span class="p">[]</span>
|
|
<span class="nx">o</span> <span class="s">'[ ArgList OptComma ]'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Arr</span> <span class="nx">$2</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-42">¶</a> </div> <p>Inclusive and exclusive range dots.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">RangeDots: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'..'</span><span class="p">,</span> <span class="nf">-></span> <span class="s">'inclusive'</span>
|
|
<span class="nx">o</span> <span class="s">'...'</span><span class="p">,</span> <span class="nf">-></span> <span class="s">'exclusive'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-43"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-43">¶</a> </div> <p>The CoffeeScript range literal.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Range: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'[ Expression RangeDots Expression ]'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Range</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$4</span><span class="p">,</span> <span class="nx">$3</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-44"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-44">¶</a> </div> <p>Array slice literals.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Slice: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Expression RangeDots Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Range</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'Expression RangeDots'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Range</span> <span class="nx">$1</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'RangeDots Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Range</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'RangeDots'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Range</span> <span class="kc">null</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">$1</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-45"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-45">¶</a> </div> <p>The <strong>ArgList</strong> is both the list of objects passed into a function call,
|
|
as well as the contents of an array literal
|
|
(i.e. comma-separated expressions). Newlines work as well.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">ArgList: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Arg'</span><span class="p">,</span> <span class="nf">-></span> <span class="p">[</span><span class="nx">$1</span><span class="p">]</span>
|
|
<span class="nx">o</span> <span class="s">'ArgList , Arg'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'ArgList OptComma TERMINATOR Arg'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">$4</span>
|
|
<span class="nx">o</span> <span class="s">'INDENT ArgList OptComma OUTDENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'ArgList OptComma INDENT ArgList OptComma OUTDENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">$4</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-46"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-46">¶</a> </div> <p>Valid arguments are Blocks or Splats.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Arg: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Expression'</span>
|
|
<span class="nx">o</span> <span class="s">'Splat'</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-47"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-47">¶</a> </div> <p>Just simple, comma-separated, required arguments (no fancy syntax). We need
|
|
this to be separate from the <strong>ArgList</strong> for use in <strong>Switch</strong> blocks, where
|
|
having the newlines wouldn't make sense.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">SimpleArgs: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Expression'</span>
|
|
<span class="nx">o</span> <span class="s">'SimpleArgs , Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="p">[].</span><span class="nx">concat</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-48"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-48">¶</a> </div> <p>The variants of <em>try/catch/finally</em> exception handling blocks.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Try: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'TRY Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Try</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'TRY Block Catch'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Try</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$3</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nx">$3</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
|
<span class="nx">o</span> <span class="s">'TRY Block FINALLY Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Try</span> <span class="nx">$2</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">$4</span>
|
|
<span class="nx">o</span> <span class="s">'TRY Block Catch FINALLY Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Try</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$3</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nx">$3</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="nx">$5</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-49"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-49">¶</a> </div> <p>A catch clause names its error and runs a block of code.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Catch: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'CATCH Identifier Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="p">[</span><span class="nx">$2</span><span class="p">,</span> <span class="nx">$3</span><span class="p">]</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-50"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-50">¶</a> </div> <p>Throw an exception object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Throw: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'THROW Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Throw</span> <span class="nx">$2</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-51"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-51">¶</a> </div> <p>Parenthetical expressions. Note that the <strong>Parenthetical</strong> is a <strong>Value</strong>,
|
|
not an <strong>Expression</strong>, so if you need to use an expression in a place
|
|
where only values are accepted, wrapping it in parentheses will always do
|
|
the trick.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Parenthetical: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'( Body )'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Parens</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'( INDENT Body OUTDENT )'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Parens</span> <span class="nx">$3</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-52">¶</a> </div> <p>The condition portion of a while loop.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">WhileSource: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'WHILE Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">While</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'WHILE Expression WHEN Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">While</span> <span class="nx">$2</span><span class="p">,</span> <span class="nv">guard: </span><span class="nx">$4</span>
|
|
<span class="nx">o</span> <span class="s">'UNTIL Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">While</span> <span class="nx">$2</span><span class="p">,</span> <span class="nv">invert: </span><span class="kc">true</span>
|
|
<span class="nx">o</span> <span class="s">'UNTIL Expression WHEN Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">While</span> <span class="nx">$2</span><span class="p">,</span> <span class="nv">invert: </span><span class="kc">true</span><span class="p">,</span> <span class="nv">guard: </span><span class="nx">$4</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-53">¶</a> </div> <p>The while loop can either be normal, with a block of expressions to execute,
|
|
or postfix, with a single expression. There is no do..while.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">While: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'WhileSource Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">addBody</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'Statement WhileSource'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$2</span><span class="p">.</span><span class="nx">addBody</span> <span class="nx">Block</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="nx">$1</span><span class="p">]</span>
|
|
<span class="nx">o</span> <span class="s">'Expression WhileSource'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$2</span><span class="p">.</span><span class="nx">addBody</span> <span class="nx">Block</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="nx">$1</span><span class="p">]</span>
|
|
<span class="nx">o</span> <span class="s">'Loop'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span>
|
|
<span class="p">]</span>
|
|
|
|
<span class="nv">Loop: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'LOOP Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">While</span><span class="p">(</span><span class="k">new</span> <span class="nx">Literal</span> <span class="s">'true'</span><span class="p">).</span><span class="nx">addBody</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'LOOP Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">While</span><span class="p">(</span><span class="k">new</span> <span class="nx">Literal</span> <span class="s">'true'</span><span class="p">).</span><span class="nx">addBody</span> <span class="nx">Block</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="nx">$2</span><span class="p">]</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-54">¶</a> </div> <p>Array, object, and range comprehensions, at the most generic level.
|
|
Comprehensions can either be normal, with a block of expressions to execute,
|
|
or postfix, with a single expression.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">For: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Statement ForBody'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">For</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'Expression ForBody'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">For</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'ForBody Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">For</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$1</span>
|
|
<span class="p">]</span>
|
|
|
|
<span class="nv">ForBody: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'FOR Range'</span><span class="p">,</span> <span class="nf">-></span> <span class="nv">source: </span><span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">$2</span><span class="p">)</span>
|
|
<span class="nx">o</span> <span class="s">'ForStart ForSource'</span><span class="p">,</span> <span class="nf">-></span> <span class="nv">$2.own = </span><span class="nx">$1</span><span class="p">.</span><span class="nx">own</span><span class="p">;</span> <span class="nv">$2.name = </span><span class="nx">$1</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="nv">$2.index = </span><span class="nx">$1</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span> <span class="nx">$2</span>
|
|
<span class="p">]</span>
|
|
|
|
<span class="nv">ForStart: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'FOR ForVariables'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'FOR OWN ForVariables'</span><span class="p">,</span> <span class="nf">-></span> <span class="nv">$3.own = </span><span class="kc">yes</span><span class="p">;</span> <span class="nx">$3</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-55"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-55">¶</a> </div> <p>An array of all accepted values for a variable inside the loop.
|
|
This enables support for pattern matching.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">ForValue: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'Identifier'</span>
|
|
<span class="nx">o</span> <span class="s">'ThisProperty'</span>
|
|
<span class="nx">o</span> <span class="s">'Array'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Value</span> <span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'Object'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Value</span> <span class="nx">$1</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-56"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-56">¶</a> </div> <p>An array or range comprehension has variables for the current element
|
|
and (optional) reference to the current index. Or, <em>key, value</em>, in the case
|
|
of object comprehensions.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">ForVariables: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'ForValue'</span><span class="p">,</span> <span class="nf">-></span> <span class="p">[</span><span class="nx">$1</span><span class="p">]</span>
|
|
<span class="nx">o</span> <span class="s">'ForValue , ForValue'</span><span class="p">,</span> <span class="nf">-></span> <span class="p">[</span><span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span><span class="p">]</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-57">¶</a> </div> <p>The source of a comprehension is an array or object with an optional guard
|
|
clause. If it's an array comprehension, you can also choose to step through
|
|
in fixed-size increments.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">ForSource: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'FORIN Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="nv">source: </span><span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'FOROF Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="nv">source: </span><span class="nx">$2</span><span class="p">,</span> <span class="nv">object: </span><span class="kc">yes</span>
|
|
<span class="nx">o</span> <span class="s">'FORIN Expression WHEN Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="nv">source: </span><span class="nx">$2</span><span class="p">,</span> <span class="nv">guard: </span><span class="nx">$4</span>
|
|
<span class="nx">o</span> <span class="s">'FOROF Expression WHEN Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="nv">source: </span><span class="nx">$2</span><span class="p">,</span> <span class="nv">guard: </span><span class="nx">$4</span><span class="p">,</span> <span class="nv">object: </span><span class="kc">yes</span>
|
|
<span class="nx">o</span> <span class="s">'FORIN Expression BY Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="nv">source: </span><span class="nx">$2</span><span class="p">,</span> <span class="nv">step: </span> <span class="nx">$4</span>
|
|
<span class="nx">o</span> <span class="s">'FORIN Expression WHEN Expression BY Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="nv">source: </span><span class="nx">$2</span><span class="p">,</span> <span class="nv">guard: </span><span class="nx">$4</span><span class="p">,</span> <span class="nv">step: </span><span class="nx">$6</span>
|
|
<span class="nx">o</span> <span class="s">'FORIN Expression BY Expression WHEN Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="nv">source: </span><span class="nx">$2</span><span class="p">,</span> <span class="nv">step: </span> <span class="nx">$4</span><span class="p">,</span> <span class="nv">guard: </span><span class="nx">$6</span>
|
|
<span class="p">]</span>
|
|
|
|
<span class="nv">Switch: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'SWITCH Expression INDENT Whens OUTDENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Switch</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$4</span>
|
|
<span class="nx">o</span> <span class="s">'SWITCH Expression INDENT Whens ELSE Block OUTDENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Switch</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$4</span><span class="p">,</span> <span class="nx">$6</span>
|
|
<span class="nx">o</span> <span class="s">'SWITCH INDENT Whens OUTDENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Switch</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'SWITCH INDENT Whens ELSE Block OUTDENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Switch</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">$5</span>
|
|
<span class="p">]</span>
|
|
|
|
<span class="nv">Whens: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'When'</span>
|
|
<span class="nx">o</span> <span class="s">'Whens When'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">$2</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-58"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-58">¶</a> </div> <p>An individual <strong>When</strong> clause, with action.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">When: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'LEADING_WHEN SimpleArgs Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="p">[[</span><span class="nx">$2</span><span class="p">,</span> <span class="nx">$3</span><span class="p">]]</span>
|
|
<span class="nx">o</span> <span class="s">'LEADING_WHEN SimpleArgs Block TERMINATOR'</span><span class="p">,</span> <span class="nf">-></span> <span class="p">[[</span><span class="nx">$2</span><span class="p">,</span> <span class="nx">$3</span><span class="p">]]</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">¶</a> </div> <p>The most basic form of <em>if</em> is a condition and an action. The following
|
|
if-related rules are broken up along these lines in order to avoid
|
|
ambiguity.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">IfBlock: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'IF Expression Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">If</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$3</span><span class="p">,</span> <span class="nv">type: </span><span class="nx">$1</span>
|
|
<span class="nx">o</span> <span class="s">'IfBlock ELSE IF Expression Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">addElse</span> <span class="k">new</span> <span class="nx">If</span> <span class="nx">$4</span><span class="p">,</span> <span class="nx">$5</span><span class="p">,</span> <span class="nv">type: </span><span class="nx">$3</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-60"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-60">¶</a> </div> <p>The full complement of <em>if</em> expressions, including postfix one-liner
|
|
<em>if</em> and <em>unless</em>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">If: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'IfBlock'</span>
|
|
<span class="nx">o</span> <span class="s">'IfBlock ELSE Block'</span><span class="p">,</span> <span class="nf">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">addElse</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'Statement POST_IF Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">If</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">Block</span><span class="p">.</span><span class="nx">wrap</span><span class="p">([</span><span class="nx">$1</span><span class="p">]),</span> <span class="nv">type: </span><span class="nx">$2</span><span class="p">,</span> <span class="nv">statement: </span><span class="kc">true</span>
|
|
<span class="nx">o</span> <span class="s">'Expression POST_IF Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">If</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">Block</span><span class="p">.</span><span class="nx">wrap</span><span class="p">([</span><span class="nx">$1</span><span class="p">]),</span> <span class="nv">type: </span><span class="nx">$2</span><span class="p">,</span> <span class="nv">statement: </span><span class="kc">true</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-61"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-61">¶</a> </div> <p>Arithmetic and logical operators, working on one or more operands.
|
|
Here they are grouped by order of precedence. The actual precedence rules
|
|
are defined at the bottom of the page. It would be shorter if we could
|
|
combine most of these rules into a single generic <em>Operand OpSymbol Operand</em>
|
|
-type rule, but in order to make the precedence binding possible, separate
|
|
rules are necessary.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">Operation: </span><span class="p">[</span>
|
|
<span class="nx">o</span> <span class="s">'UNARY Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Op</span> <span class="nx">$1</span> <span class="p">,</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'- Expression'</span><span class="p">,</span> <span class="p">(</span><span class="nf">-></span> <span class="k">new</span> <span class="nx">Op</span> <span class="s">'-'</span><span class="p">,</span> <span class="nx">$2</span><span class="p">),</span> <span class="nv">prec: </span><span class="s">'UNARY'</span>
|
|
<span class="nx">o</span> <span class="s">'+ Expression'</span><span class="p">,</span> <span class="p">(</span><span class="nf">-></span> <span class="k">new</span> <span class="nx">Op</span> <span class="s">'+'</span><span class="p">,</span> <span class="nx">$2</span><span class="p">),</span> <span class="nv">prec: </span><span class="s">'UNARY'</span>
|
|
|
|
<span class="nx">o</span> <span class="s">'-- SimpleAssignable'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Op</span> <span class="s">'--'</span><span class="p">,</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'++ SimpleAssignable'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Op</span> <span class="s">'++'</span><span class="p">,</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'SimpleAssignable --'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Op</span> <span class="s">'--'</span><span class="p">,</span> <span class="nx">$1</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="kc">true</span>
|
|
<span class="nx">o</span> <span class="s">'SimpleAssignable ++'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Op</span> <span class="s">'++'</span><span class="p">,</span> <span class="nx">$1</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="kc">true</span></pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-62">¶</a> </div> <p><a href="http://jashkenas.github.com/coffee-script/#existence">The existential operator</a>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">o</span> <span class="s">'Expression ?'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Existence</span> <span class="nx">$1</span>
|
|
|
|
<span class="nx">o</span> <span class="s">'Expression + Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Op</span> <span class="s">'+'</span> <span class="p">,</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'Expression - Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Op</span> <span class="s">'-'</span> <span class="p">,</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span>
|
|
|
|
<span class="nx">o</span> <span class="s">'Expression MATH Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Op</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'Expression SHIFT Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Op</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'Expression COMPARE Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Op</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'Expression LOGIC Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Op</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span>
|
|
<span class="nx">o</span> <span class="s">'Expression RELATION Expression'</span><span class="p">,</span> <span class="nf">-></span>
|
|
<span class="k">if</span> <span class="nx">$2</span><span class="p">.</span><span class="nx">charAt</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="o">is</span> <span class="s">'!'</span>
|
|
<span class="k">new</span> <span class="nx">Op</span><span class="p">(</span><span class="nx">$2</span><span class="p">[</span><span class="mi">1</span><span class="p">..],</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span><span class="p">).</span><span class="nx">invert</span><span class="p">()</span>
|
|
<span class="k">else</span>
|
|
<span class="k">new</span> <span class="nx">Op</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span>
|
|
|
|
<span class="nx">o</span> <span class="s">'SimpleAssignable COMPOUND_ASSIGN</span>
|
|
<span class="s"> Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'SimpleAssignable COMPOUND_ASSIGN</span>
|
|
<span class="s"> INDENT Expression OUTDENT'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$4</span><span class="p">,</span> <span class="nx">$2</span>
|
|
<span class="nx">o</span> <span class="s">'SimpleAssignable EXTENDS Expression'</span><span class="p">,</span> <span class="nf">-></span> <span class="k">new</span> <span class="nx">Extends</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-63">¶</a> </div> <h2>Precedence</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-64">¶</a> </div> <p>Operators at the top of this list have higher precedence than the ones lower
|
|
down. Following these rules is what makes <code>2 + 3 * 4</code> parse as:</p>
|
|
|
|
<pre><code>2 + (3 * 4)
|
|
</code></pre>
|
|
|
|
<p>And not:</p>
|
|
|
|
<pre><code>(2 + 3) * 4
|
|
</code></pre> </td> <td class="code"> <div class="highlight"><pre><span class="nv">operators = </span><span class="p">[</span>
|
|
<span class="p">[</span><span class="s">'left'</span><span class="p">,</span> <span class="s">'.'</span><span class="p">,</span> <span class="s">'?.'</span><span class="p">,</span> <span class="s">'::'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'left'</span><span class="p">,</span> <span class="s">'CALL_START'</span><span class="p">,</span> <span class="s">'CALL_END'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'nonassoc'</span><span class="p">,</span> <span class="s">'++'</span><span class="p">,</span> <span class="s">'--'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'left'</span><span class="p">,</span> <span class="s">'?'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'right'</span><span class="p">,</span> <span class="s">'UNARY'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'left'</span><span class="p">,</span> <span class="s">'MATH'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'left'</span><span class="p">,</span> <span class="s">'+'</span><span class="p">,</span> <span class="s">'-'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'left'</span><span class="p">,</span> <span class="s">'SHIFT'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'left'</span><span class="p">,</span> <span class="s">'RELATION'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'left'</span><span class="p">,</span> <span class="s">'COMPARE'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'left'</span><span class="p">,</span> <span class="s">'LOGIC'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'nonassoc'</span><span class="p">,</span> <span class="s">'INDENT'</span><span class="p">,</span> <span class="s">'OUTDENT'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'right'</span><span class="p">,</span> <span class="s">'='</span><span class="p">,</span> <span class="s">':'</span><span class="p">,</span> <span class="s">'COMPOUND_ASSIGN'</span><span class="p">,</span> <span class="s">'RETURN'</span><span class="p">,</span> <span class="s">'THROW'</span><span class="p">,</span> <span class="s">'EXTENDS'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'right'</span><span class="p">,</span> <span class="s">'FORIN'</span><span class="p">,</span> <span class="s">'FOROF'</span><span class="p">,</span> <span class="s">'BY'</span><span class="p">,</span> <span class="s">'WHEN'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'right'</span><span class="p">,</span> <span class="s">'IF'</span><span class="p">,</span> <span class="s">'ELSE'</span><span class="p">,</span> <span class="s">'FOR'</span><span class="p">,</span> <span class="s">'WHILE'</span><span class="p">,</span> <span class="s">'UNTIL'</span><span class="p">,</span> <span class="s">'LOOP'</span><span class="p">,</span> <span class="s">'SUPER'</span><span class="p">,</span> <span class="s">'CLASS'</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s">'right'</span><span class="p">,</span> <span class="s">'POST_IF'</span><span class="p">]</span>
|
|
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-65">¶</a> </div> <h2>Wrapping Up</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-66"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-66">¶</a> </div> <p>Finally, now that we have our <strong>grammar</strong> and our <strong>operators</strong>, we can create
|
|
our <strong>Jison.Parser</strong>. We do this by processing all of our rules, recording all
|
|
terminals (every symbol which does not appear as the name of a rule above)
|
|
as "tokens".</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">tokens = </span><span class="p">[]</span>
|
|
<span class="k">for</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">alternatives</span> <span class="k">of</span> <span class="nx">grammar</span>
|
|
<span class="nx">grammar</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="k">for</span> <span class="nx">alt</span> <span class="k">in</span> <span class="nx">alternatives</span>
|
|
<span class="k">for</span> <span class="nx">token</span> <span class="k">in</span> <span class="nx">alt</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">split</span> <span class="s">' '</span>
|
|
<span class="nx">tokens</span><span class="p">.</span><span class="nx">push</span> <span class="nx">token</span> <span class="k">unless</span> <span class="nx">grammar</span><span class="p">[</span><span class="nx">token</span><span class="p">]</span>
|
|
<span class="nx">alt</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s">"return </span><span class="si">#{</span><span class="nx">alt</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s">"</span> <span class="k">if</span> <span class="nx">name</span> <span class="o">is</span> <span class="s">'Root'</span>
|
|
<span class="nx">alt</span></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-67">¶</a> </div> <p>Initialize the <strong>Parser</strong> with our list of terminal <strong>tokens</strong>, our <strong>grammar</strong>
|
|
rules, and the name of the root. Reverse the operators because Jison orders
|
|
precedence from low to high, and we have it high to low
|
|
(as in <a href="http://dinosaur.compilertools.net/yacc/index.html">Yacc</a>).</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.parser = </span><span class="k">new</span> <span class="nx">Parser</span>
|
|
<span class="nv">tokens : </span><span class="nx">tokens</span><span class="p">.</span><span class="nx">join</span> <span class="s">' '</span>
|
|
<span class="nv">bnf : </span><span class="nx">grammar</span>
|
|
<span class="nv">operators : </span><span class="nx">operators</span><span class="p">.</span><span class="nx">reverse</span><span class="p">()</span>
|
|
<span class="nv">startSymbol : </span><span class="s">'Root'</span>
|
|
|
|
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html> |