PageRenderTime 142ms CodeModel.GetById 105ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 1ms

/documentation/docs/grammar.html

http://github.com/jashkenas/coffee-script
HTML | 1628 lines | 1147 code | 481 blank | 0 comment | 0 complexity | 8b9b8aba3fd9799dfcf99bb234e07778 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1<!DOCTYPE html>
  2
  3<html>
  4<head>
  5  <title>grammar.coffee</title>
  6  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  7  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  8  <link rel="stylesheet" media="all" href="docco.css" />
  9</head>
 10<body>
 11  <div id="container">
 12    <div id="background"></div>
 13    
 14      <ul id="jump_to">
 15        <li>
 16          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
 17          <a class="small" href="javascript:void(0);">+</a>
 18          <div id="jump_wrapper">
 19          <div id="jump_page_wrapper">
 20            <div id="jump_page">
 21              
 22                
 23                <a class="source" href="browser.html">
 24                  browser.coffee
 25                </a>
 26              
 27                
 28                <a class="source" href="cake.html">
 29                  cake.coffee
 30                </a>
 31              
 32                
 33                <a class="source" href="coffee-script.html">
 34                  coffee-script.coffee
 35                </a>
 36              
 37                
 38                <a class="source" href="command.html">
 39                  command.coffee
 40                </a>
 41              
 42                
 43                <a class="source" href="grammar.html">
 44                  grammar.coffee
 45                </a>
 46              
 47                
 48                <a class="source" href="helpers.html">
 49                  helpers.coffee
 50                </a>
 51              
 52                
 53                <a class="source" href="index.html">
 54                  index.coffee
 55                </a>
 56              
 57                
 58                <a class="source" href="lexer.html">
 59                  lexer.coffee
 60                </a>
 61              
 62                
 63                <a class="source" href="nodes.html">
 64                  nodes.coffee
 65                </a>
 66              
 67                
 68                <a class="source" href="optparse.html">
 69                  optparse.coffee
 70                </a>
 71              
 72                
 73                <a class="source" href="register.html">
 74                  register.coffee
 75                </a>
 76              
 77                
 78                <a class="source" href="repl.html">
 79                  repl.coffee
 80                </a>
 81              
 82                
 83                <a class="source" href="rewriter.html">
 84                  rewriter.coffee
 85                </a>
 86              
 87                
 88                <a class="source" href="scope.html">
 89                  scope.litcoffee
 90                </a>
 91              
 92                
 93                <a class="source" href="sourcemap.html">
 94                  sourcemap.litcoffee
 95                </a>
 96              
 97            </div>
 98          </div>
 99        </li>
100      </ul>
101    
102    <ul class="sections">
103        
104          <li id="title">
105              <div class="annotation">
106                  <h1>grammar.coffee</h1>
107              </div>
108          </li>
109        
110        
111        
112        <li id="section-1">
113            <div class="annotation">
114              
115              <div class="pilwrap ">
116                <a class="pilcrow" href="#section-1">&#182;</a>
117              </div>
118              <p>The CoffeeScript parser is generated by <a href="http://github.com/zaach/jison">Jison</a>
119from this grammar file. Jison is a bottom-up parser generator, similar in
120style to <a href="http://www.gnu.org/software/bison">Bison</a>, implemented in JavaScript.
121It can recognize <a href="http://en.wikipedia.org/wiki/LR_grammar">LALR(1), LR(0), SLR(1), and LR(1)</a>
122type grammars. To create the Jison parser, we list the pattern to match
123on the left-hand side, and the action to take (usually the creation of syntax
124tree nodes) on the right. As the parser runs, it
125shifts tokens from our token stream, from left to right, and
126<a href="http://en.wikipedia.org/wiki/Bottom-up_parsing">attempts to match</a>
127the token sequence against the rules below. When a match can be made, it
128reduces into the <a href="http://en.wikipedia.org/wiki/Terminal_and_nonterminal_symbols">nonterminal</a>
129(the enclosing name at the top), and we proceed from there.</p>
130<p>If you run the <code>cake build:parser</code> command, Jison constructs a parse table
131from our rules and saves it into <code>lib/parser.js</code>.</p>
132
133            </div>
134            
135        </li>
136        
137        
138        <li id="section-2">
139            <div class="annotation">
140              
141              <div class="pilwrap ">
142                <a class="pilcrow" href="#section-2">&#182;</a>
143              </div>
144              <p>The only dependency is on the <strong>Jison.Parser</strong>.</p>
145
146            </div>
147            
148            <div class="content"><div class='highlight'><pre>{Parser} = <span class="hljs-built_in">require</span> <span class="hljs-string">'jison'</span></pre></div></div>
149            
150        </li>
151        
152        
153        <li id="section-3">
154            <div class="annotation">
155              
156              <div class="pilwrap ">
157                <a class="pilcrow" href="#section-3">&#182;</a>
158              </div>
159              <h2 id="jison-dsl">Jison DSL</h2>
160
161            </div>
162            
163        </li>
164        
165        
166        <li id="section-4">
167            <div class="annotation">
168              
169              <div class="pilwrap ">
170                <a class="pilcrow" href="#section-4">&#182;</a>
171              </div>
172              
173            </div>
174            
175        </li>
176        
177        
178        <li id="section-5">
179            <div class="annotation">
180              
181              <div class="pilwrap ">
182                <a class="pilcrow" href="#section-5">&#182;</a>
183              </div>
184              <p>Since we’re going to be wrapped in a function by Jison in any case, if our
185action immediately returns a value, we can optimize by removing the function
186wrapper and just returning the value directly.</p>
187
188            </div>
189            
190            <div class="content"><div class='highlight'><pre>unwrap = <span class="hljs-regexp">/^function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/</span></pre></div></div>
191            
192        </li>
193        
194        
195        <li id="section-6">
196            <div class="annotation">
197              
198              <div class="pilwrap ">
199                <a class="pilcrow" href="#section-6">&#182;</a>
200              </div>
201              <p>Our handy DSL for Jison grammar generation, thanks to
202<a href="http://github.com/creationix">Tim Caswell</a>. For every rule in the grammar,
203we pass the pattern-defining string, the action to run, and extra options,
204optionally. If no action is specified, we simply pass the value of the
205previous nonterminal.</p>
206
207            </div>
208            
209            <div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">o</span> = <span class="hljs-params">(patternString, action, options)</span> -&gt;</span>
210  patternString = patternString.replace <span class="hljs-regexp">/\s{2,}/g</span>, <span class="hljs-string">' '</span>
211  patternCount = patternString.split(<span class="hljs-string">' '</span>).length
212  <span class="hljs-keyword">return</span> [patternString, <span class="hljs-string">'$$ = $1;'</span>, options] <span class="hljs-keyword">unless</span> action
213  action = <span class="hljs-keyword">if</span> match = unwrap.exec action <span class="hljs-keyword">then</span> match[<span class="hljs-number">1</span>] <span class="hljs-keyword">else</span> <span class="hljs-string">"(<span class="hljs-subst">#{action}</span>())"</span></pre></div></div>
214            
215        </li>
216        
217        
218        <li id="section-7">
219            <div class="annotation">
220              
221              <div class="pilwrap ">
222                <a class="pilcrow" href="#section-7">&#182;</a>
223              </div>
224              <p>All runtime functions we need are defined on “yy”</p>
225
226            </div>
227            
228            <div class="content"><div class='highlight'><pre>  action = action.replace <span class="hljs-regexp">/\bnew /g</span>, <span class="hljs-string">'$&amp;yy.'</span>
229  action = action.replace <span class="hljs-regexp">/\b(?:Block\.wrap|extend)\b/g</span>, <span class="hljs-string">'yy.$&amp;'</span></pre></div></div>
230            
231        </li>
232        
233        
234        <li id="section-8">
235            <div class="annotation">
236              
237              <div class="pilwrap ">
238                <a class="pilcrow" href="#section-8">&#182;</a>
239              </div>
240              <p>Returns a function which adds location data to the first parameter passed
241in, and returns the parameter.  If the parameter is not a node, it will
242just be passed through unaffected.</p>
243
244            </div>
245            
246            <div class="content"><div class='highlight'><pre>  <span class="hljs-function"><span class="hljs-title">addLocationDataFn</span> = <span class="hljs-params">(first, last)</span> -&gt;</span>
247    <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> last
248      <span class="hljs-string">"yy.addLocationDataFn(@<span class="hljs-subst">#{first}</span>)"</span>
249    <span class="hljs-keyword">else</span>
250      <span class="hljs-string">"yy.addLocationDataFn(@<span class="hljs-subst">#{first}</span>, @<span class="hljs-subst">#{last}</span>)"</span>
251
252  action = action.replace <span class="hljs-regexp">/LOC\(([0-9]*)\)/g</span>, addLocationDataFn(<span class="hljs-string">'$1'</span>)
253  action = action.replace <span class="hljs-regexp">/LOC\(([0-9]*),\s*([0-9]*)\)/g</span>, addLocationDataFn(<span class="hljs-string">'$1'</span>, <span class="hljs-string">'$2'</span>)
254
255  [patternString, <span class="hljs-string">"$$ = <span class="hljs-subst">#{addLocationDataFn(<span class="hljs-number">1</span>, patternCount)}</span>(<span class="hljs-subst">#{action}</span>);"</span>, options]</pre></div></div>
256            
257        </li>
258        
259        
260        <li id="section-9">
261            <div class="annotation">
262              
263              <div class="pilwrap ">
264                <a class="pilcrow" href="#section-9">&#182;</a>
265              </div>
266              <h2 id="grammatical-rules">Grammatical Rules</h2>
267
268            </div>
269            
270        </li>
271        
272        
273        <li id="section-10">
274            <div class="annotation">
275              
276              <div class="pilwrap ">
277                <a class="pilcrow" href="#section-10">&#182;</a>
278              </div>
279              
280            </div>
281            
282        </li>
283        
284        
285        <li id="section-11">
286            <div class="annotation">
287              
288              <div class="pilwrap ">
289                <a class="pilcrow" href="#section-11">&#182;</a>
290              </div>
291              <p>In all of the rules that follow, you’ll see the name of the nonterminal as
292the key to a list of alternative matches. With each match’s action, the
293dollar-sign variables are provided by Jison as references to the value of
294their numeric position, so in this rule:</p>
295<pre><code><span class="hljs-string">"Expression UNLESS Expression"</span>
296</code></pre><p><code>$1</code> would be the value of the first <code>Expression</code>, <code>$2</code> would be the token
297for the <code>UNLESS</code> terminal, and <code>$3</code> would be the value of the second
298<code>Expression</code>.</p>
299
300            </div>
301            
302            <div class="content"><div class='highlight'><pre>grammar =</pre></div></div>
303            
304        </li>
305        
306        
307        <li id="section-12">
308            <div class="annotation">
309              
310              <div class="pilwrap ">
311                <a class="pilcrow" href="#section-12">&#182;</a>
312              </div>
313              <p>The <strong>Root</strong> is the top-level node in the syntax tree. Since we parse bottom-up,
314all parsing must end here.</p>
315
316            </div>
317            
318            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Root</span>: [
319    o <span class="hljs-string">''</span>,<span class="hljs-function">                                       -&gt;</span> <span class="hljs-keyword">new</span> Block
320    o <span class="hljs-string">'Body'</span>
321  ]</pre></div></div>
322            
323        </li>
324        
325        
326        <li id="section-13">
327            <div class="annotation">
328              
329              <div class="pilwrap ">
330                <a class="pilcrow" href="#section-13">&#182;</a>
331              </div>
332              <p>Any list of statements and expressions, separated by line breaks or semicolons.</p>
333
334            </div>
335            
336            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Body</span>: [
337    o <span class="hljs-string">'Line'</span>,<span class="hljs-function">                                   -&gt;</span> Block.wrap [$<span class="hljs-number">1</span>]
338    o <span class="hljs-string">'Body TERMINATOR Line'</span>,<span class="hljs-function">                   -&gt;</span> $<span class="hljs-number">1.</span>push $<span class="hljs-number">3</span>
339    o <span class="hljs-string">'Body TERMINATOR'</span>
340  ]</pre></div></div>
341            
342        </li>
343        
344        
345        <li id="section-14">
346            <div class="annotation">
347              
348              <div class="pilwrap ">
349                <a class="pilcrow" href="#section-14">&#182;</a>
350              </div>
351              <p>Block and statements, which make up a line in a body.</p>
352
353            </div>
354            
355            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Line</span>: [
356    o <span class="hljs-string">'Expression'</span>
357    o <span class="hljs-string">'Statement'</span>
358  ]</pre></div></div>
359            
360        </li>
361        
362        
363        <li id="section-15">
364            <div class="annotation">
365              
366              <div class="pilwrap ">
367                <a class="pilcrow" href="#section-15">&#182;</a>
368              </div>
369              <p>Pure statements which cannot be expressions.</p>
370
371            </div>
372            
373            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Statement</span>: [
374    o <span class="hljs-string">'Return'</span>
375    o <span class="hljs-string">'Comment'</span>
376    o <span class="hljs-string">'STATEMENT'</span>,<span class="hljs-function">                              -&gt;</span> <span class="hljs-keyword">new</span> Literal $<span class="hljs-number">1</span>
377  ]</pre></div></div>
378            
379        </li>
380        
381        
382        <li id="section-16">
383            <div class="annotation">
384              
385              <div class="pilwrap ">
386                <a class="pilcrow" href="#section-16">&#182;</a>
387              </div>
388              <p>All the different types of expressions in our language. The basic unit of
389CoffeeScript is the <strong>Expression</strong> — everything that can be an expression
390is one. Blocks serve as the building blocks of many other rules, making
391them somewhat circular.</p>
392
393            </div>
394            
395            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Expression</span>: [
396    o <span class="hljs-string">'Value'</span>
397    o <span class="hljs-string">'Invocation'</span>
398    o <span class="hljs-string">'Code'</span>
399    o <span class="hljs-string">'Operation'</span>
400    o <span class="hljs-string">'Assign'</span>
401    o <span class="hljs-string">'If'</span>
402    o <span class="hljs-string">'Try'</span>
403    o <span class="hljs-string">'While'</span>
404    o <span class="hljs-string">'For'</span>
405    o <span class="hljs-string">'Switch'</span>
406    o <span class="hljs-string">'Class'</span>
407    o <span class="hljs-string">'Throw'</span>
408  ]</pre></div></div>
409            
410        </li>
411        
412        
413        <li id="section-17">
414            <div class="annotation">
415              
416              <div class="pilwrap ">
417                <a class="pilcrow" href="#section-17">&#182;</a>
418              </div>
419              <p>An indented block of expressions. Note that the <a href="rewriter.html">Rewriter</a>
420will convert some postfix forms into blocks for us, by adjusting the
421token stream.</p>
422
423            </div>
424            
425            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Block</span>: [
426    o <span class="hljs-string">'INDENT OUTDENT'</span>,<span class="hljs-function">                         -&gt;</span> <span class="hljs-keyword">new</span> Block
427    o <span class="hljs-string">'INDENT Body OUTDENT'</span>,<span class="hljs-function">                    -&gt;</span> $<span class="hljs-number">2</span>
428  ]</pre></div></div>
429            
430        </li>
431        
432        
433        <li id="section-18">
434            <div class="annotation">
435              
436              <div class="pilwrap ">
437                <a class="pilcrow" href="#section-18">&#182;</a>
438              </div>
439              <p>A literal identifier, a variable name or property.</p>
440
441            </div>
442            
443            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Identifier</span>: [
444    o <span class="hljs-string">'IDENTIFIER'</span>,<span class="hljs-function">                             -&gt;</span> <span class="hljs-keyword">new</span> Literal $<span class="hljs-number">1</span>
445  ]</pre></div></div>
446            
447        </li>
448        
449        
450        <li id="section-19">
451            <div class="annotation">
452              
453              <div class="pilwrap ">
454                <a class="pilcrow" href="#section-19">&#182;</a>
455              </div>
456              <p>Alphanumerics are separated from the other <strong>Literal</strong> matchers because
457they can also serve as keys in object literals.</p>
458
459            </div>
460            
461            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">AlphaNumeric</span>: [
462    o <span class="hljs-string">'NUMBER'</span>,<span class="hljs-function">                                 -&gt;</span> <span class="hljs-keyword">new</span> Literal $<span class="hljs-number">1</span>
463    o <span class="hljs-string">'String'</span>
464  ]
465
466  <span class="hljs-attribute">String</span>: [
467    o <span class="hljs-string">'STRING'</span>,<span class="hljs-function">                                 -&gt;</span> <span class="hljs-keyword">new</span> Literal $<span class="hljs-number">1</span>
468    o <span class="hljs-string">'STRING_START Body STRING_END'</span>,<span class="hljs-function">           -&gt;</span> <span class="hljs-keyword">new</span> Parens $<span class="hljs-number">2</span>
469  ]
470
471  <span class="hljs-attribute">Regex</span>: [
472    o <span class="hljs-string">'REGEX'</span>,<span class="hljs-function">                                  -&gt;</span> <span class="hljs-keyword">new</span> Literal $<span class="hljs-number">1</span>
473    o <span class="hljs-string">'REGEX_START Invocation REGEX_END'</span>,<span class="hljs-function">       -&gt;</span> $<span class="hljs-number">2</span>
474  ]</pre></div></div>
475            
476        </li>
477        
478        
479        <li id="section-20">
480            <div class="annotation">
481              
482              <div class="pilwrap ">
483                <a class="pilcrow" href="#section-20">&#182;</a>
484              </div>
485              <p>All of our immediate values. Generally these can be passed straight
486through and printed to JavaScript.</p>
487
488            </div>
489            
490            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Literal</span>: [
491    o <span class="hljs-string">'AlphaNumeric'</span>
492    o <span class="hljs-string">'JS'</span>,<span class="hljs-function">                                     -&gt;</span> <span class="hljs-keyword">new</span> Literal $<span class="hljs-number">1</span>
493    o <span class="hljs-string">'Regex'</span>
494    o <span class="hljs-string">'DEBUGGER'</span>,<span class="hljs-function">                               -&gt;</span> <span class="hljs-keyword">new</span> Literal $<span class="hljs-number">1</span>
495    o <span class="hljs-string">'UNDEFINED'</span>,<span class="hljs-function">                              -&gt;</span> <span class="hljs-keyword">new</span> Undefined
496    o <span class="hljs-string">'NULL'</span>,<span class="hljs-function">                                   -&gt;</span> <span class="hljs-keyword">new</span> Null
497    o <span class="hljs-string">'BOOL'</span>,<span class="hljs-function">                                   -&gt;</span> <span class="hljs-keyword">new</span> Bool $<span class="hljs-number">1</span>
498  ]</pre></div></div>
499            
500        </li>
501        
502        
503        <li id="section-21">
504            <div class="annotation">
505              
506              <div class="pilwrap ">
507                <a class="pilcrow" href="#section-21">&#182;</a>
508              </div>
509              <p>Assignment of a variable, property, or index to a value.</p>
510
511            </div>
512            
513            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Assign</span>: [
514    o <span class="hljs-string">'Assignable = Expression'</span>,<span class="hljs-function">                -&gt;</span> <span class="hljs-keyword">new</span> Assign $<span class="hljs-number">1</span>, $<span class="hljs-number">3</span>
515    o <span class="hljs-string">'Assignable = TERMINATOR Expression'</span>,<span class="hljs-function">     -&gt;</span> <span class="hljs-keyword">new</span> Assign $<span class="hljs-number">1</span>, $<span class="hljs-number">4</span>
516    o <span class="hljs-string">'Assignable = INDENT Expression OUTDENT'</span>,<span class="hljs-function"> -&gt;</span> <span class="hljs-keyword">new</span> Assign $<span class="hljs-number">1</span>, $<span class="hljs-number">4</span>
517  ]</pre></div></div>
518            
519        </li>
520        
521        
522        <li id="section-22">
523            <div class="annotation">
524              
525              <div class="pilwrap ">
526                <a class="pilcrow" href="#section-22">&#182;</a>
527              </div>
528              <p>Assignment when it happens within an object literal. The difference from
529the ordinary <strong>Assign</strong> is that these allow numbers and strings as keys.</p>
530
531            </div>
532            
533            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">AssignObj</span>: [
534    o <span class="hljs-string">'ObjAssignable'</span>,<span class="hljs-function">                          -&gt;</span> <span class="hljs-keyword">new</span> Value $<span class="hljs-number">1</span>
535    o <span class="hljs-string">'ObjAssignable : Expression'</span>,<span class="hljs-function">             -&gt;</span> <span class="hljs-keyword">new</span> Assign LOC(<span class="hljs-number">1</span>)(<span class="hljs-keyword">new</span> Value $<span class="hljs-number">1</span>), $<span class="hljs-number">3</span>, <span class="hljs-string">'object'</span>,
536                                                              <span class="hljs-attribute">operatorToken</span>: LOC(<span class="hljs-number">2</span>)(<span class="hljs-keyword">new</span> Literal $<span class="hljs-number">2</span>)
537    o <span class="hljs-string">'ObjAssignable :
538       INDENT Expression OUTDENT'</span>,<span class="hljs-function">              -&gt;</span> <span class="hljs-keyword">new</span> Assign LOC(<span class="hljs-number">1</span>)(<span class="hljs-keyword">new</span> Value $<span class="hljs-number">1</span>), $<span class="hljs-number">4</span>, <span class="hljs-string">'object'</span>,
539                                                              <span class="hljs-attribute">operatorToken</span>: LOC(<span class="hljs-number">2</span>)(<span class="hljs-keyword">new</span> Literal $<span class="hljs-number">2</span>)
540    o <span class="hljs-string">'SimpleObjAssignable = Expression'</span>,<span class="hljs-function">       -&gt;</span> <span class="hljs-keyword">new</span> Assign LOC(<span class="hljs-number">1</span>)(<span class="hljs-keyword">new</span> Value $<span class="hljs-number">1</span>), $<span class="hljs-number">3</span>, <span class="hljs-literal">null</span>,
541                                                              <span class="hljs-attribute">operatorToken</span>: LOC(<span class="hljs-number">2</span>)(<span class="hljs-keyword">new</span> Literal $<span class="hljs-number">2</span>)
542    o <span class="hljs-string">'SimpleObjAssignable =
543       INDENT Expression OUTDENT'</span>,<span class="hljs-function">              -&gt;</span> <span class="hljs-keyword">new</span> Assign LOC(<span class="hljs-number">1</span>)(<span class="hljs-keyword">new</span> Value $<span class="hljs-number">1</span>), $<span class="hljs-number">4</span>, <span class="hljs-literal">null</span>,
544                                                              <span class="hljs-attribute">operatorToken</span>: LOC(<span class="hljs-number">2</span>)(<span class="hljs-keyword">new</span> Literal $<span class="hljs-number">2</span>)
545    o <span class="hljs-string">'Comment'</span>
546  ]
547
548  <span class="hljs-attribute">SimpleObjAssignable</span>: [
549    o <span class="hljs-string">'Identifier'</span>
550    o <span class="hljs-string">'ThisProperty'</span>
551  ]
552
553  <span class="hljs-attribute">ObjAssignable</span>: [
554    o <span class="hljs-string">'SimpleObjAssignable'</span>
555    o <span class="hljs-string">'AlphaNumeric'</span>
556  ]</pre></div></div>
557            
558        </li>
559        
560        
561        <li id="section-23">
562            <div class="annotation">
563              
564              <div class="pilwrap ">
565                <a class="pilcrow" href="#section-23">&#182;</a>
566              </div>
567              <p>A return statement from a function body.</p>
568
569            </div>
570            
571            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Return</span>: [
572    o <span class="hljs-string">'RETURN Expression'</span>,<span class="hljs-function">                      -&gt;</span> <span class="hljs-keyword">new</span> Return $<span class="hljs-number">2</span>
573    o <span class="hljs-string">'RETURN'</span>,<span class="hljs-function">                                 -&gt;</span> <span class="hljs-keyword">new</span> Return
574  ]</pre></div></div>
575            
576        </li>
577        
578        
579        <li id="section-24">
580            <div class="annotation">
581              
582              <div class="pilwrap ">
583                <a class="pilcrow" href="#section-24">&#182;</a>
584              </div>
585              <p>A block comment.</p>
586
587            </div>
588            
589            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Comment</span>: [
590    o <span class="hljs-string">'HERECOMMENT'</span>,<span class="hljs-function">                            -&gt;</span> <span class="hljs-keyword">new</span> Comment $<span class="hljs-number">1</span>
591  ]</pre></div></div>
592            
593        </li>
594        
595        
596        <li id="section-25">
597            <div class="annotation">
598              
599              <div class="pilwrap ">
600                <a class="pilcrow" href="#section-25">&#182;</a>
601              </div>
602              <p>The <strong>Code</strong> node is the function literal. It’s defined by an indented block
603of <strong>Block</strong> preceded by a function arrow, with an optional parameter
604list.</p>
605
606            </div>
607            
608            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Code</span>: [
609    o <span class="hljs-string">'PARAM_START ParamList PARAM_END FuncGlyph Block'</span>,<span class="hljs-function"> -&gt;</span> <span class="hljs-keyword">new</span> Code $<span class="hljs-number">2</span>, $<span class="hljs-number">5</span>, $<span class="hljs-number">4</span>
610    o <span class="hljs-string">'FuncGlyph Block'</span>,<span class="hljs-function">                        -&gt;</span> <span class="hljs-keyword">new</span> Code [], $<span class="hljs-number">2</span>, $<span class="hljs-number">1</span>
611  ]</pre></div></div>
612            
613        </li>
614        
615        
616        <li id="section-26">
617            <div class="annotation">
618              
619              <div class="pilwrap ">
620                <a class="pilcrow" href="#section-26">&#182;</a>
621              </div>
622              <p>CoffeeScript has two different symbols for functions. <code>-&gt;</code> is for ordinary
623functions, and <code>=&gt;</code> is for functions bound to the current value of <em>this</em>.</p>
624
625            </div>
626            
627            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">FuncGlyph</span>: [
628    o <span class="hljs-string">'-&gt;'</span>,<span class="hljs-function">                                     -&gt;</span> <span class="hljs-string">'func'</span>
629    o <span class="hljs-string">'=&gt;'</span>,<span class="hljs-function">                                     -&gt;</span> <span class="hljs-string">'boundfunc'</span>
630  ]</pre></div></div>
631            
632        </li>
633        
634        
635        <li id="section-27">
636            <div class="annotation">
637              
638              <div class="pilwrap ">
639                <a class="pilcrow" href="#section-27">&#182;</a>
640              </div>
641              <p>An optional, trailing comma.</p>
642
643            </div>
644            
645            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">OptComma</span>: [
646    o <span class="hljs-string">''</span>
647    o <span class="hljs-string">','</span>
648  ]</pre></div></div>
649            
650        </li>
651        
652        
653        <li id="section-28">
654            <div class="annotation">
655              
656              <div class="pilwrap ">
657                <a class="pilcrow" href="#section-28">&#182;</a>
658              </div>
659              <p>The list of parameters that a function accepts can be of any length.</p>
660
661            </div>
662            
663            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">ParamList</span>: [
664    o <span class="hljs-string">''</span>,<span class="hljs-function">                                       -&gt;</span> []
665    o <span class="hljs-string">'Param'</span>,<span class="hljs-function">                                  -&gt;</span> [$<span class="hljs-number">1</span>]
666    o <span class="hljs-string">'ParamList , Param'</span>,<span class="hljs-function">                      -&gt;</span> $<span class="hljs-number">1.</span>concat $<span class="hljs-number">3</span>
667    o <span class="hljs-string">'ParamList OptComma TERMINATOR Param'</span>,<span class="hljs-function">    -&gt;</span> $<span class="hljs-number">1.</span>concat $<span class="hljs-number">4</span>
668    o <span class="hljs-string">'ParamList OptComma INDENT ParamList OptComma OUTDENT'</span>,<span class="hljs-function"> -&gt;</span> $<span class="hljs-number">1.</span>concat $<span class="hljs-number">4</span>
669  ]</pre></div></div>
670            
671        </li>
672        
673        
674        <li id="section-29">
675            <div class="annotation">
676              
677              <div class="pilwrap ">
678                <a class="pilcrow" href="#section-29">&#182;</a>
679              </div>
680              <p>A single parameter in a function definition can be ordinary, or a splat
681that hoovers up the remaining arguments.</p>
682
683            </div>
684            
685            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Param</span>: [
686    o <span class="hljs-string">'ParamVar'</span>,<span class="hljs-function">                               -&gt;</span> <span class="hljs-keyword">new</span> Param $<span class="hljs-number">1</span>
687    o <span class="hljs-string">'ParamVar ...'</span>,<span class="hljs-function">                           -&gt;</span> <span class="hljs-keyword">new</span> Param $<span class="hljs-number">1</span>, <span class="hljs-literal">null</span>, <span class="hljs-literal">on</span>
688    o <span class="hljs-string">'ParamVar = Expression'</span>,<span class="hljs-function">                  -&gt;</span> <span class="hljs-keyword">new</span> Param $<span class="hljs-number">1</span>, $<span class="hljs-number">3</span>
689    o <span class="hljs-string">'...'</span>,<span class="hljs-function">                                    -&gt;</span> <span class="hljs-keyword">new</span> Expansion
690  ]</pre></div></div>
691            
692        </li>
693        
694        
695        <li id="section-30">
696            <div class="annotation">
697              
698              <div class="pilwrap ">
699                <a class="pilcrow" href="#section-30">&#182;</a>
700              </div>
701              <p>Function Parameters</p>
702
703            </div>
704            
705            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">ParamVar</span>: [
706    o <span class="hljs-string">'Identifier'</span>
707    o <span class="hljs-string">'ThisProperty'</span>
708    o <span class="hljs-string">'Array'</span>
709    o <span class="hljs-string">'Object'</span>
710  ]</pre></div></div>
711            
712        </li>
713        
714        
715        <li id="section-31">
716            <div class="annotation">
717              
718              <div class="pilwrap ">
719                <a class="pilcrow" href="#section-31">&#182;</a>
720              </div>
721              <p>A splat that occurs outside of a parameter list.</p>
722
723            </div>
724            
725            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Splat</span>: [
726    o <span class="hljs-string">'Expression ...'</span>,<span class="hljs-function">                         -&gt;</span> <span class="hljs-keyword">new</span> Splat $<span class="hljs-number">1</span>
727  ]</pre></div></div>
728            
729        </li>
730        
731        
732        <li id="section-32">
733            <div class="annotation">
734              
735              <div class="pilwrap ">
736                <a class="pilcrow" href="#section-32">&#182;</a>
737              </div>
738              <p>Variables and properties that can be assigned to.</p>
739
740            </div>
741            
742            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">SimpleAssignable</span>: [
743    o <span class="hljs-string">'Identifier'</span>,<span class="hljs-function">                             -&gt;</span> <span class="hljs-keyword">new</span> Value $<span class="hljs-number">1</span>
744    o <span class="hljs-string">'Value Accessor'</span>,<span class="hljs-function">                         -&gt;</span> $<span class="hljs-number">1.</span>add $<span class="hljs-number">2</span>
745    o <span class="hljs-string">'Invocation Accessor'</span>,<span class="hljs-function">                    -&gt;</span> <span class="hljs-keyword">new</span> Value $<span class="hljs-number">1</span>, [].concat $<span class="hljs-number">2</span>
746    o <span class="hljs-string">'ThisProperty'</span>
747  ]</pre></div></div>
748            
749        </li>
750        
751        
752        <li id="section-33">
753            <div class="annotation">
754              
755              <div class="pilwrap ">
756                <a class="pilcrow" href="#section-33">&#182;</a>
757              </div>
758              <p>Everything that can be assigned to.</p>
759
760            </div>
761            
762            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Assignable</span>: [
763    o <span class="hljs-string">'SimpleAssignable'</span>
764    o <span class="hljs-string">'Array'</span>,<span class="hljs-function">                                  -&gt;</span> <span class="hljs-keyword">new</span> Value $<span class="hljs-number">1</span>
765    o <span class="hljs-string">'Object'</span>,<span class="hljs-function">                                 -&gt;</span> <span class="hljs-keyword">new</span> Value $<span class="hljs-number">1</span>
766  ]</pre></div></div>
767            
768        </li>
769        
770        
771        <li id="section-34">
772            <div class="annotation">
773              
774              <div class="pilwrap ">
775                <a class="pilcrow" href="#section-34">&#182;</a>
776              </div>
777              <p>The types of things that can be treated as values — assigned to, invoked
778as functions, indexed into, named as a class, etc.</p>
779
780            </div>
781            
782            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Value</span>: [
783    o <span class="hljs-string">'Assignable'</span>
784    o <span class="hljs-string">'Literal'</span>,<span class="hljs-function">                                -&gt;</span> <span class="hljs-keyword">new</span> Value $<span class="hljs-number">1</span>
785    o <span class="hljs-string">'Parenthetical'</span>,<span class="hljs-function">                          -&gt;</span> <span class="hljs-keyword">new</span> Value $<span class="hljs-number">1</span>
786    o <span class="hljs-string">'Range'</span>,<span class="hljs-function">                                  -&gt;</span> <span class="hljs-keyword">new</span> Value $<span class="hljs-number">1</span>
787    o <span class="hljs-string">'This'</span>
788  ]</pre></div></div>
789            
790        </li>
791        
792        
793        <li id="section-35">
794            <div class="annotation">
795              
796              <div class="pilwrap ">
797                <a class="pilcrow" href="#section-35">&#182;</a>
798              </div>
799              <p>The general group of accessors into an object, by property, by prototype
800or by array index or slice.</p>
801
802            </div>
803            
804            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Accessor</span>: [
805    o <span class="hljs-string">'.  Identifier'</span>,<span class="hljs-function">                          -&gt;</span> <span class="hljs-keyword">new</span> Access $<span class="hljs-number">2</span>
806    o <span class="hljs-string">'?. Identifier'</span>,<span class="hljs-function">                          -&gt;</span> <span class="hljs-keyword">new</span> Access $<span class="hljs-number">2</span>, <span class="hljs-string">'soak'</span>
807    o <span class="hljs-string">':: Identifier'</span>,<span class="hljs-function">                          -&gt;</span> [LOC(<span class="hljs-number">1</span>)(<span class="hljs-keyword">new</span> Access <span class="hljs-keyword">new</span> Literal(<span class="hljs-string">'prototype'</span>)), LOC(<span class="hljs-number">2</span>)(<span class="hljs-keyword">new</span> Access $<span class="hljs-number">2</span>)]
808    o <span class="hljs-string">'?:: Identifier'</span>,<span class="hljs-function">                         -&gt;</span> [LOC(<span class="hljs-number">1</span>)(<span class="hljs-keyword">new</span> Access <span class="hljs-keyword">new</span> Literal(<span class="hljs-string">'prototype'</span>), <span class="hljs-string">'soak'</span>), LOC(<span class="hljs-number">2</span>)(<span class="hljs-keyword">new</span> Access $<span class="hljs-number">2</span>)]
809    o <span class="hljs-string">'::'</span>,<span class="hljs-function">                                     -&gt;</span> <span class="hljs-keyword">new</span> Access <span class="hljs-keyword">new</span> Literal <span class="hljs-string">'prototype'</span>
810    o <span class="hljs-string">'Index'</span>
811  ]</pre></div></div>
812            
813        </li>
814        
815        
816        <li id="section-36">
817            <div class="annotation">
818              
819              <div class="pilwrap ">
820                <a class="pilcrow" href="#section-36">&#182;</a>
821              </div>
822              <p>Indexing into an object or array using bracket notation.</p>
823
824            </div>
825            
826            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Index</span>: [
827    o <span class="hljs-string">'INDEX_START IndexValue INDEX_END'</span>,<span class="hljs-function">       -&gt;</span> $<span class="hljs-number">2</span>
828    o <span class="hljs-string">'INDEX_SOAK  Index'</span>,<span class="hljs-function">                      -&gt;</span> extend $<span class="hljs-number">2</span>, soak : <span class="hljs-literal">yes</span>
829  ]
830
831  <span class="hljs-attribute">IndexValue</span>: [
832    o <span class="hljs-string">'Expression'</span>,<span class="hljs-function">                             -&gt;</span> <span class="hljs-keyword">new</span> Index $<span class="hljs-number">1</span>
833    o <span class="hljs-string">'Slice'</span>,<span class="hljs-function">                                  -&gt;</span> <span class="hljs-keyword">new</span> Slice $<span class="hljs-number">1</span>
834  ]</pre></div></div>
835            
836        </li>
837        
838        
839        <li id="section-37">
840            <div class="annotation">
841              
842              <div class="pilwrap ">
843                <a class="pilcrow" href="#section-37">&#182;</a>
844              </div>
845              <p>In CoffeeScript, an object literal is simply a list of assignments.</p>
846
847            </div>
848            
849            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Object</span>: [
850    o <span class="hljs-string">'{ AssignList OptComma }'</span>,<span class="hljs-function">                -&gt;</span> <span class="hljs-keyword">new</span> Obj $<span class="hljs-number">2</span>, $<span class="hljs-number">1.</span>generated
851  ]</pre></div></div>
852            
853        </li>
854        
855        
856        <li id="section-38">
857            <div class="annotation">
858              
859              <div class="pilwrap ">
860                <a class="pilcrow" href="#section-38">&#182;</a>
861              </div>
862              <p>Assignment of properties within an object literal can be separated by
863comma, as in JavaScript, or simply by newline.</p>
864
865            </div>
866            
867            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">AssignList</span>: [
868    o <span class="hljs-string">''</span>,<span class="hljs-function">                                                       -&gt;</span> []
869    o <span class="hljs-string">'AssignObj'</span>,<span class="hljs-function">                                              -&gt;</span> [$<span class="hljs-number">1</span>]
870    o <span class="hljs-string">'AssignList , AssignObj'</span>,<span class="hljs-function">                                 -&gt;</span> $<span class="hljs-number">1.</span>concat $<span class="hljs-number">3</span>
871    o <span class="hljs-string">'AssignList OptComma TERMINATOR AssignObj'</span>,<span class="hljs-function">               -&gt;</span> $<span class="hljs-number">1.</span>concat $<span class="hljs-number">4</span>
872    o <span class="hljs-string">'AssignList OptComma INDENT AssignList OptComma OUTDENT'</span>,<span class="hljs-function"> -&gt;</span> $<span class="hljs-number">1.</span>concat $<span class="hljs-number">4</span>
873  ]</pre></div></div>
874            
875        </li>
876        
877        
878        <li id="section-39">
879            <div class="annotation">
880              
881              <div class="pilwrap ">
882                <a class="pilcrow" href="#section-39">&#182;</a>
883              </div>
884              <p>Class definitions have optional bodies of prototype property assignments,
885and optional references to the superclass.</p>
886
887            </div>
888            
889            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Class</span>: [
890    o <span class="hljs-string">'CLASS'</span>,<span class="hljs-function">                                           -&gt;</span> <span class="hljs-keyword">new</span> Class
891    o <span class="hljs-string">'CLASS Block'</span>,<span class="hljs-function">                                     -&gt;</span> <span class="hljs-keyword">new</span> Class <span class="hljs-literal">null</span>, <span class="hljs-literal">null</span>, $<span class="hljs-number">2</span>
892    o <span class="hljs-string">'CLASS EXTENDS Expression'</span>,<span class="hljs-function">                        -&gt;</span> <span class="hljs-keyword">new</span> Class <span class="hljs-literal">null</span>, $<span class="hljs-number">3</span>
893    o <span class="hljs-string">'CLASS EXTENDS Expression Block'</span>,<span class="hljs-function">                  -&gt;</span> <span class="hljs-keyword">new</span> Class <span class="hljs-literal">null</span>, $<span class="hljs-number">3</span>, $<span class="hljs-number">4</span>
894    o <span class="hljs-string">'CLASS SimpleAssignable'</span>,<span class="hljs-function">                          -&gt;</span> <span class="hljs-keyword">new</span> Class $<span class="hljs-number">2</span>
895    o <span class="hljs-string">'CLASS SimpleAssignable Block'</span>,<span class="hljs-function">                    -&gt;</span> <span class="hljs-keyword">new</span> Class $<span class="hljs-number">2</span>, <span class="hljs-literal">null</span>, $<span class="hljs-number">3</span>
896    o <span class="hljs-string">'CLASS SimpleAssignable EXTENDS Expression'</span>,<span class="hljs-function">       -&gt;</span> <span class="hljs-keyword">new</span> Class $<span class="hljs-number">2</span>, $<span class="hljs-number">4</span>
897    o <span class="hljs-string">'CLASS SimpleAssignable EXTENDS Expression Block'</span>,<span class="hljs-function"> -&gt;</span> <span class="hljs-keyword">new</span> Class $<span class="hljs-number">2</span>, $<span class="hljs-number">4</span>, $<span class="hljs-number">5</span>
898  ]</pre></div></div>
899            
900        </li>
901        
902        
903        <li id="section-40">
904            <div class="annotation">
905              
906              <div class="pilwrap ">
907                <a class="pilcrow" href="#section-40">&#182;</a>
908              </div>
909              <p>Ordinary function invocation, or a chained series of calls.</p>
910
911            </div>
912            
913            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Invocation</span>: [
914    o <span class="hljs-string">'Value OptFuncExist Arguments'</span>,<span class="hljs-function">           -&gt;</span> <span class="hljs-keyword">new</span> Call $<span class="hljs-number">1</span>, $<span class="hljs-number">3</span>, $<span class="hljs-number">2</span>
915    o <span class="hljs-string">'Invocation OptFuncExist Arguments'</span>,<span class="hljs-function">      -&gt;</span> <span class="hljs-keyword">new</span> Call $<span class="hljs-number">1</span>, $<span class="hljs-number">3</span>, $<span class="hljs-number">2</span>
916    o <span class="hljs-string">'SUPER'</span>,<span class="hljs-function">                                  -&gt;</span> <span class="hljs-keyword">new</span> Call <span class="hljs-string">'super'</span>, [<span class="hljs-keyword">new</span> Splat <span class="hljs-keyword">new</span> Literal <span class="hljs-string">'arguments'</span>]
917    o <span class="hljs-string">'SUPER Arguments'</span>,<span class="hljs-function">                        -&gt;</span> <span class="hljs-keyword">new</span> Call <span class="hljs-string">'super'</span>, $<span class="hljs-number">2</span>
918  ]</pre></div></div>
919            
920        </li>
921        
922        
923        <li id="section-41">
924            <div class="annotation">
925              
926              <div class="pilwrap ">
927                <a class="pilcrow" href="#section-41">&#182;</a>
928              </div>
929              <p>An optional existence check on a function.</p>
930
931            </div>
932            
933            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">OptFuncExist</span>: [
934    o <span class="hljs-string">''</span>,<span class="hljs-function">                                       -&gt;</span> <span class="hljs-literal">no</span>
935    o <span class="hljs-string">'FUNC_EXIST'</span>,<span class="hljs-function">                             -&gt;</span> <span class="hljs-literal">yes</span>
936  ]</pre></div></div>
937            
938        </li>
939        
940        
941        <li id="section-42">
942            <div class="annotation">
943              
944              <div class="pilwrap ">
945                <a class="pilcrow" href="#section-42">&#182;</a>
946              </div>
947              <p>The list of arguments to a function call.</p>
948
949            </div>
950            
951            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">Arguments</span>: [
952    o <span class="hljs-string">'CALL_START CALL_END'</span>,<span class="hljs-function">                    -&gt;</span> []
953    o <span class="hljs-string">'CALL_START ArgList OptComma CALL_END'</span>,<span class="hljs-function">   -&gt;</span> $<span class="hljs-number">2</span>
954  ]</pre></div></div>
955            
956        </li>
957        
958        
959        <li id="section-43">
960            <div class="annotation">
961              
962              <div class="pilwrap ">
963                <a class="pilcrow" href="#section-43">&#182;</a>
964              </div>
965              <p>A reference to the <em>this</em> current object.</p>
966
967            </div>
968            
969            <div class="content"><div class='highlight'><pre>  <span class="hljs-attribute">This</span>: [
970    o <span class="hljs-string">'THIS'</span>,<span class="hljs-function">                                   -&gt;</span> <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> Literal <span class="hljs-string">'this'</span>
971    o <span class="hljs-string">'@'</span>,<span class="hljs-function">                                      -&gt;</span> <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> Literal <span class="hljs-string">'this'</span>
972  ]</pre></div></div>
973            
974        </li>

Large files files are truncated, but you can click here to view the full file