mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-01-14 01:07:55 -05:00
Compare commits
96 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a2631759c0 | ||
|
|
2b87cabbb4 | ||
|
|
d8465ce767 | ||
|
|
60f80e2698 | ||
|
|
c782c2ec1c | ||
|
|
f0d778ce49 | ||
|
|
a3c224e57a | ||
|
|
b727245834 | ||
|
|
6b19e61bd0 | ||
|
|
38ce0cfd9a | ||
|
|
61a39e04fc | ||
|
|
ea3aa6803a | ||
|
|
3a20d7dacb | ||
|
|
e5837b4ee9 | ||
|
|
d1f31c5143 | ||
|
|
4af41e9bfb | ||
|
|
121110a485 | ||
|
|
d41a414b5c | ||
|
|
9bd3cca7c4 | ||
|
|
18cbddff6a | ||
|
|
df414dab02 | ||
|
|
904207ba8f | ||
|
|
44618d5765 | ||
|
|
70cfd54ad4 | ||
|
|
62bf0a2bc9 | ||
|
|
c9289155b4 | ||
|
|
cd67ec6e69 | ||
|
|
cd6261d477 | ||
|
|
3b60aad487 | ||
|
|
493780efab | ||
|
|
9290e508c6 | ||
|
|
a1ebb14495 | ||
|
|
eb9a524ea1 | ||
|
|
0caa731291 | ||
|
|
704fbf499b | ||
|
|
04fd24e068 | ||
|
|
dc6a83c030 | ||
|
|
bcecbd051b | ||
|
|
6607224493 | ||
|
|
6224edd6ce | ||
|
|
9598b11c77 | ||
|
|
fa95f743f3 | ||
|
|
e2c46d14f0 | ||
|
|
bd3471b3d1 | ||
|
|
1b88d18d61 | ||
|
|
b4de17d504 | ||
|
|
f90fac0e55 | ||
|
|
9fd92bf884 | ||
|
|
117204a784 | ||
|
|
e7834de929 | ||
|
|
d5d5de55ae | ||
|
|
143c4d5efc | ||
|
|
13adc44867 | ||
|
|
87693d84cb | ||
|
|
6ed33fcc6d | ||
|
|
2e59cc4807 | ||
|
|
4ddd65a4c4 | ||
|
|
bf6bafa3ac | ||
|
|
24f1174b16 | ||
|
|
098caa9979 | ||
|
|
b608d4a5ea | ||
|
|
4d32c47bee | ||
|
|
ec54b50c67 | ||
|
|
387c690530 | ||
|
|
536e24b024 | ||
|
|
1b05cd81f0 | ||
|
|
db181e2a36 | ||
|
|
f41ca2e5e8 | ||
|
|
a8c6a641d7 | ||
|
|
474c372b17 | ||
|
|
5b9b45814f | ||
|
|
01cd5476a0 | ||
|
|
027b9e9dc3 | ||
|
|
197f576cab | ||
|
|
0b8facc66f | ||
|
|
7a0d95c612 | ||
|
|
975b82f09b | ||
|
|
f496cc229b | ||
|
|
9894eeb8e9 | ||
|
|
129e950c59 | ||
|
|
c90a75ebc5 | ||
|
|
1d6eca76f8 | ||
|
|
6555d86328 | ||
|
|
c71f2794eb | ||
|
|
ca18f1fad6 | ||
|
|
ead9b1041c | ||
|
|
1eebbfe2bc | ||
|
|
d30c8b321c | ||
|
|
083500fc0e | ||
|
|
fd6e9a1e66 | ||
|
|
8c45aa480b | ||
|
|
d704afa0e9 | ||
|
|
2ec857ef8d | ||
|
|
ba02ebc3dc | ||
|
|
ac752a46bc | ||
|
|
398ec3be5a |
3
Cakefile
3
Cakefile
@@ -97,10 +97,9 @@ task 'test', 'run the CoffeeScript language test suite', ->
|
||||
passedTests = failedTests = 0
|
||||
startTime = new Date
|
||||
originalOk = ok
|
||||
helpers.extend global, {
|
||||
helpers.extend global,
|
||||
ok: (args...) -> passedTests += 1; originalOk(args...)
|
||||
CoffeeScript: CoffeeScript
|
||||
}
|
||||
process.on 'exit', ->
|
||||
time = ((new Date - startTime) / 1000).toFixed(2)
|
||||
message = "passed #{passedTests} tests in #{time} seconds#{reset}"
|
||||
|
||||
2
README
2
README
@@ -56,10 +56,12 @@
|
||||
Timothy Jones (Tesco)
|
||||
Chris Lloyd (chrislloyd)
|
||||
Matt Lyon (mattly)
|
||||
Satoshi Murakami (satyr)
|
||||
Jeff Olson (olsonjeffery)
|
||||
Nathan Ostgard (noonat)
|
||||
Samuel Reis (grgh)
|
||||
Tom Robinson (tlrobinson)
|
||||
Tim Smart (Tim-Smart)
|
||||
Sam Stephenson (sstephenson)
|
||||
Dr. Nic Williams (drnic)
|
||||
|
||||
4
Rakefile
4
Rakefile
@@ -6,7 +6,7 @@ require 'yui/compressor'
|
||||
|
||||
HEADER = <<-EOS
|
||||
/**
|
||||
* CoffeeScript Compiler v0.9.1
|
||||
* CoffeeScript Compiler v0.9.3
|
||||
* http://coffeescript.org
|
||||
*
|
||||
* Copyright 2010, Jeremy Ashkenas
|
||||
@@ -33,7 +33,7 @@ end
|
||||
|
||||
desc "Build the single concatenated and minified script for the browser"
|
||||
task :browser do
|
||||
sources = %w(helpers.js rewriter.js lexer.js parser.js scope.js nodes.js coffee-script.js)
|
||||
sources = %w(helpers.js rewriter.js lexer.js parser.js scope.js nodes.js coffee-script.js browser.js)
|
||||
code = sources.map {|s| File.read('lib/' + s) }.join('')
|
||||
code = YUI::JavaScriptCompressor.new.compress(code)
|
||||
File.open('extras/coffee-script.js', 'w+') {|f| f.write(HEADER + code) }
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
###
|
||||
CoffeeScript Compiler v0.9.1
|
||||
CoffeeScript Compiler v0.9.3
|
||||
Released under the MIT License
|
||||
###
|
||||
@@ -1,8 +1,8 @@
|
||||
mobyDick = "Call me Ishmael. Some years ago --
|
||||
never mind how long precisely -- having little
|
||||
or no money in my purse, and nothing particular
|
||||
to interest me on shore, I thought I would sail
|
||||
about a little and see the watery part of the
|
||||
world..."
|
||||
never mind how long precisely -- having little
|
||||
or no money in my purse, and nothing particular
|
||||
to interest me on shore, I thought I would sail
|
||||
about a little and see the watery part of the
|
||||
world..."
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
switch day
|
||||
when "Mon" then goToWork()
|
||||
when "Tue" then goToThePark()
|
||||
when "Thu" then goIceFishing()
|
||||
when "Mon" then go work
|
||||
when "Tue" then go relax
|
||||
when "Thu" then go iceFishing
|
||||
when "Fri", "Sat"
|
||||
if day is bingoDay
|
||||
goToBingo()
|
||||
goDancing()
|
||||
when "Sun" then goToChurch()
|
||||
else goToWork()
|
||||
go bingo
|
||||
go dancing
|
||||
when "Sun" then go church
|
||||
else go work
|
||||
@@ -15,7 +15,7 @@ a {
|
||||
color: #000055;
|
||||
}
|
||||
h1, h2, h3, h4, h5, h6 {
|
||||
margin-top: 40px;
|
||||
margin-top: 50px;
|
||||
}
|
||||
br.clear {
|
||||
height: 0;
|
||||
|
||||
26
documentation/docs/browser.html
Normal file
26
documentation/docs/browser.html
Normal file
@@ -0,0 +1,26 @@
|
||||
<!DOCTYPE html> <html> <head> <title>browser.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> browser.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>Activate CoffeeScript in the browser by having it compile and evaluate
|
||||
all script tags with a content-type of <code>text/coffeescript</code>.
|
||||
This happens on page load.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">if</span> <span class="nb">document</span><span class="o">?</span><span class="p">.</span><span class="nx">getElementsByTagName</span>
|
||||
<span class="nv">grind = </span><span class="p">(</span><span class="nx">coffee</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nx">setTimeout</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">coffee</span>
|
||||
<span class="nv">grindRemote = </span><span class="p">(</span><span class="nx">url</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">xhr = </span><span class="k">new</span> <span class="p">(</span><span class="nb">window</span><span class="p">.</span><span class="nx">ActiveXObject</span> <span class="o">or</span> <span class="nx">XMLHttpRequest</span><span class="p">)(</span><span class="s1">'Microsoft.XMLHTTP'</span><span class="p">)</span>
|
||||
<span class="nx">xhr</span><span class="p">.</span><span class="nx">open</span> <span class="s1">'GET'</span><span class="p">,</span> <span class="nx">url</span><span class="p">,</span> <span class="kc">true</span>
|
||||
<span class="nx">xhr</span><span class="p">.</span><span class="nx">overrideMimeType</span> <span class="s1">'text/plain'</span> <span class="k">if</span> <span class="s1">'overrideMimeType'</span> <span class="k">of</span> <span class="nx">xhr</span>
|
||||
<span class="nv">xhr.onreadystatechange = </span><span class="o">-></span>
|
||||
<span class="nx">grind</span> <span class="nx">xhr</span><span class="p">.</span><span class="nx">responseText</span> <span class="k">if</span> <span class="nx">xhr</span><span class="p">.</span><span class="nx">readyState</span> <span class="o">is</span> <span class="mi">4</span>
|
||||
<span class="nx">xhr</span><span class="p">.</span><span class="nx">send</span> <span class="kc">null</span>
|
||||
<span class="nv">processScripts = </span><span class="o">-></span>
|
||||
<span class="k">for</span> <span class="nx">script</span> <span class="k">in</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementsByTagName</span> <span class="s1">'script'</span>
|
||||
<span class="k">if</span> <span class="nx">script</span><span class="p">.</span><span class="nx">type</span> <span class="o">is</span> <span class="s1">'text/coffeescript'</span>
|
||||
<span class="k">if</span> <span class="nx">script</span><span class="p">.</span><span class="nx">src</span>
|
||||
<span class="nx">grindRemote</span> <span class="nx">script</span><span class="p">.</span><span class="nx">src</span>
|
||||
<span class="k">else</span>
|
||||
<span class="nx">grind</span> <span class="nx">script</span><span class="p">.</span><span class="nx">innerHTML</span>
|
||||
<span class="kc">null</span>
|
||||
<span class="k">if</span> <span class="nb">window</span><span class="p">.</span><span class="nx">addEventListener</span>
|
||||
<span class="nx">addEventListener</span> <span class="s1">'DOMContentLoaded'</span><span class="p">,</span> <span class="nx">processScripts</span><span class="p">,</span> <span class="kc">false</span>
|
||||
<span class="k">else</span>
|
||||
<span class="nx">attachEvent</span> <span class="s1">'onload'</span><span class="p">,</span> <span class="nx">processScripts</span>
|
||||
|
||||
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html> <html> <head> <title>cake.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="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> cake.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p><code>cake</code> is a simplified version of <a href="http://www.gnu.org/software/make/">Make</a>
|
||||
<!DOCTYPE html> <html> <head> <title>cake.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> cake.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p><code>cake</code> is a simplified version of <a href="http://www.gnu.org/software/make/">Make</a>
|
||||
(<a href="http://rake.rubyforge.org/">Rake</a>, <a href="http://github.com/280north/jake">Jake</a>)
|
||||
for CoffeeScript. You define tasks with names and descriptions in a Cakefile,
|
||||
and can call them from the command line, or invoke them from other tasks.</p>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html> <html> <head> <title>coffee-script.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="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> coffee-script.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>CoffeeScript can be used both on the server, as a command-line compiler based
|
||||
<!DOCTYPE html> <html> <head> <title>coffee-script.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> coffee-script.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>CoffeeScript can be used both on the server, as a command-line compiler based
|
||||
on Node.js/V8, or to run CoffeeScripts directly in the browser. This module
|
||||
contains the main entry functions for tokenzing, parsing, and compiling source
|
||||
CoffeeScript into JavaScript.</p>
|
||||
@@ -16,23 +16,22 @@ execute all scripts present in <code>text/coffeescript</code> tags.</p>
|
||||
<span class="k">this</span><span class="p">.</span><span class="nv">exports = </span><span class="k">this</span><span class="p">.</span><span class="nv">CoffeeScript = </span><span class="p">{}</span>
|
||||
<span class="nv">Lexer = </span><span class="k">this</span><span class="p">.</span><span class="nx">Lexer</span>
|
||||
<span class="nv">parser = </span><span class="k">this</span><span class="p">.</span><span class="nx">parser</span>
|
||||
<span class="nv">helpers = </span><span class="k">this</span><span class="p">.</span><span class="nx">helpers</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-3">#</a> </div> <p>The current CoffeeScript version number.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.VERSION = </span><span class="s1">'0.9.1'</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-4">#</a> </div> <p>Instantiate a Lexer for our use here.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">lexer = </span><span class="k">new</span> <span class="nx">Lexer</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-5">#</a> </div> <p>Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison
|
||||
<span class="nv">helpers = </span><span class="k">this</span><span class="p">.</span><span class="nx">helpers</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-3">#</a> </div> <p>The current CoffeeScript version number.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.VERSION = </span><span class="s1">'0.9.3'</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-4">#</a> </div> <p>Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison
|
||||
compiler.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.compile = compile = </span><span class="p">(</span><span class="nx">code</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nx">options</span> <span class="o">or=</span> <span class="p">{}</span>
|
||||
<span class="k">try</span>
|
||||
<span class="p">(</span><span class="nx">parser</span><span class="p">.</span><span class="nx">parse</span> <span class="nx">lexer</span><span class="p">.</span><span class="nx">tokenize</span> <span class="nx">code</span><span class="p">).</span><span class="nx">compile</span> <span class="nx">options</span>
|
||||
<span class="k">catch</span> <span class="nx">err</span>
|
||||
<span class="nv">err.message = </span><span class="s2">"In #{options.fileName}, #{err.message}"</span> <span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nx">fileName</span>
|
||||
<span class="k">throw</span> <span class="nx">err</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-6">#</a> </div> <p>Tokenize a string of CoffeeScript code, and return the array of tokens.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.tokens = </span><span class="p">(</span><span class="nx">code</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nx">lexer</span><span class="p">.</span><span class="nx">tokenize</span> <span class="nx">code</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-7">#</a> </div> <p>Tokenize and parse a string of CoffeeScript code, and return the AST. You can
|
||||
<span class="k">throw</span> <span class="nx">err</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-5">#</a> </div> <p>Tokenize a string of CoffeeScript code, and return the array of tokens.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.tokens = </span><span class="p">(</span><span class="nx">code</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nx">lexer</span><span class="p">.</span><span class="nx">tokenize</span> <span class="nx">code</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-6">#</a> </div> <p>Tokenize and parse a string of CoffeeScript code, and return the AST. You can
|
||||
then compile it by calling <code>.compile()</code> on the root, or traverse it by using
|
||||
<code>.traverse()</code> with a callback.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.nodes = </span><span class="p">(</span><span class="nx">code</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nx">parser</span><span class="p">.</span><span class="nx">parse</span> <span class="nx">lexer</span><span class="p">.</span><span class="nx">tokenize</span> <span class="nx">code</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-8">#</a> </div> <p>Compile and execute a string of CoffeeScript (on the server), correctly
|
||||
setting <code>__filename</code>, <code>__dirname</code>, and relative <code>require()</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.run = </span><span class="p">((</span><span class="nx">code</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nx">parser</span><span class="p">.</span><span class="nx">parse</span> <span class="nx">lexer</span><span class="p">.</span><span class="nx">tokenize</span> <span class="nx">code</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-7">#</a> </div> <p>Compile and execute a string of CoffeeScript (on the server), correctly
|
||||
setting <code>__filename</code>, <code>__dirname</code>, and relative <code>require()</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.run = </span><span class="p">(</span><span class="nx">code</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">module.filename = __filename = </span><span class="nx">options</span><span class="p">.</span><span class="nx">fileName</span>
|
||||
<span class="nv">__dirname = </span><span class="nx">path</span><span class="p">.</span><span class="nx">dirname</span> <span class="nx">__filename</span>
|
||||
<span class="nb">eval</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">code</span><span class="p">,</span> <span class="nx">options</span>
|
||||
<span class="p">)</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-9">#</a> </div> <p>The real Lexer produces a generic stream of tokens. This object provides a
|
||||
<span class="nb">eval</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">code</span><span class="p">,</span> <span class="nx">options</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-8">#</a> </div> <p>Instantiate a Lexer for our use here.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">lexer = </span><span class="k">new</span> <span class="nx">Lexer</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-9">#</a> </div> <p>The real Lexer produces a generic stream of tokens. This object provides a
|
||||
thin wrapper around it, compatible with the Jison API. We can then pass it
|
||||
directly as a "Jison lexer".</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">parser.lexer =</span>
|
||||
<span class="nx">lex</span><span class="o">:</span> <span class="o">-></span>
|
||||
@@ -44,16 +43,6 @@ directly as a "Jison lexer".</p> </td> <td class="code">
|
||||
<span class="nx">setInput</span><span class="o">:</span> <span class="p">(</span><span class="nx">tokens</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="vi">@tokens = </span><span class="nx">tokens</span>
|
||||
<span class="vi">@pos = </span><span class="mi">0</span>
|
||||
<span class="nx">upcomingInput</span><span class="o">:</span> <span class="o">-></span> <span class="s2">""</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-10">#</a> </div> <p>Activate CoffeeScript in the browser by having it compile and evaluate
|
||||
all script tags with a content-type of <code>text/coffeescript</code>. This happens
|
||||
on page load. Unfortunately, the text contents of remote scripts cannot be
|
||||
accessed from the browser, so only inline script tags will work.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">if</span> <span class="nb">document</span><span class="o">?</span> <span class="o">and</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementsByTagName</span>
|
||||
<span class="nv">processScripts = </span><span class="o">-></span>
|
||||
<span class="k">for</span> <span class="nx">tag</span> <span class="k">in</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementsByTagName</span><span class="p">(</span><span class="s1">'script'</span><span class="p">)</span> <span class="k">when</span> <span class="nx">tag</span><span class="p">.</span><span class="nx">type</span> <span class="o">is</span> <span class="s1">'text/coffeescript'</span>
|
||||
<span class="nb">eval</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">tag</span><span class="p">.</span><span class="nx">innerHTML</span>
|
||||
<span class="k">if</span> <span class="nb">window</span><span class="p">.</span><span class="nx">addEventListener</span>
|
||||
<span class="nb">window</span><span class="p">.</span><span class="nx">addEventListener</span> <span class="s1">'load'</span><span class="p">,</span> <span class="nx">processScripts</span><span class="p">,</span> <span class="kc">false</span>
|
||||
<span class="k">else</span> <span class="k">if</span> <span class="nb">window</span><span class="p">.</span><span class="nx">attachEvent</span>
|
||||
<span class="nb">window</span><span class="p">.</span><span class="nx">attachEvent</span> <span class="s1">'onload'</span><span class="p">,</span> <span class="nx">processScripts</span>
|
||||
<span class="nx">upcomingInput</span><span class="o">:</span> <span class="o">-></span> <span class="s2">""</span>
|
||||
|
||||
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html> <html> <head> <title>command.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="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> command.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>The <code>coffee</code> utility. Handles command-line compilation of CoffeeScript
|
||||
<!DOCTYPE html> <html> <head> <title>command.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> command.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>The <code>coffee</code> utility. Handles command-line compilation of CoffeeScript
|
||||
into various forms: saved into <code>.js</code> files or printed to stdout, piped to
|
||||
<a href="http://javascriptlint.com/">JSLint</a> or recompiled every time the source is
|
||||
saved, printed as a token stream or as the syntax tree, or launch an
|
||||
@@ -29,24 +29,24 @@ interactive REPL.</p> </td> <td class="code">
|
||||
<span class="p">[</span><span class="s1">'-n'</span><span class="p">,</span> <span class="s1">'--nodes'</span><span class="p">,</span> <span class="s1">'print the parse tree that Jison produces'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s1">'-v'</span><span class="p">,</span> <span class="s1">'--version'</span><span class="p">,</span> <span class="s1">'display CoffeeScript version'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s1">'-h'</span><span class="p">,</span> <span class="s1">'--help'</span><span class="p">,</span> <span class="s1">'display this help message'</span><span class="p">]</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-6">#</a> </div> <p>Top-level objects shared by all the functions.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">options = </span><span class="p">{}</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-6">#</a> </div> <p>Top-level objects shared by all the functions.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">opts = </span><span class="p">{}</span>
|
||||
<span class="nv">sources = </span><span class="p">[]</span>
|
||||
<span class="nv">optionParser = </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-7">#</a> </div> <p>Run <code>coffee</code> by parsing passed options and determining what action to take.
|
||||
Many flags cause us to divert before compiling anything. Flags passed after
|
||||
<code>--</code> will be passed verbatim to your script as arguments in <code>process.argv</code></p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.run = </span><span class="o">-></span>
|
||||
<span class="nx">parseOptions</span><span class="p">()</span>
|
||||
<span class="k">return</span> <span class="nx">usage</span><span class="p">()</span> <span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nx">help</span>
|
||||
<span class="k">return</span> <span class="nx">version</span><span class="p">()</span> <span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nx">version</span>
|
||||
<span class="k">return</span> <span class="nx">require</span> <span class="s1">'./repl'</span> <span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nx">interactive</span>
|
||||
<span class="k">return</span> <span class="nx">compileStdio</span><span class="p">()</span> <span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nx">stdio</span>
|
||||
<span class="k">return</span> <span class="nx">compileScript</span> <span class="s1">'console'</span><span class="p">,</span> <span class="nx">sources</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nb">eval</span>
|
||||
<span class="k">return</span> <span class="nx">usage</span><span class="p">()</span> <span class="k">if</span> <span class="nx">opts</span><span class="p">.</span><span class="nx">help</span>
|
||||
<span class="k">return</span> <span class="nx">version</span><span class="p">()</span> <span class="k">if</span> <span class="nx">opts</span><span class="p">.</span><span class="nx">version</span>
|
||||
<span class="k">return</span> <span class="nx">require</span> <span class="s1">'./repl'</span> <span class="k">if</span> <span class="nx">opts</span><span class="p">.</span><span class="nx">interactive</span>
|
||||
<span class="k">return</span> <span class="nx">compileStdio</span><span class="p">()</span> <span class="k">if</span> <span class="nx">opts</span><span class="p">.</span><span class="nx">stdio</span>
|
||||
<span class="k">return</span> <span class="nx">compileScript</span> <span class="s1">'console'</span><span class="p">,</span> <span class="nx">sources</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="nx">opts</span><span class="p">.</span><span class="nb">eval</span>
|
||||
<span class="k">return</span> <span class="nx">require</span> <span class="s1">'./repl'</span> <span class="nx">unless</span> <span class="nx">sources</span><span class="p">.</span><span class="nx">length</span>
|
||||
<span class="nv">separator = </span><span class="nx">sources</span><span class="p">.</span><span class="nx">indexOf</span> <span class="s1">'--'</span>
|
||||
<span class="nv">flags = </span><span class="p">[]</span>
|
||||
<span class="k">if</span> <span class="nx">separator</span> <span class="o">>=</span> <span class="mi">0</span>
|
||||
<span class="nv">flags = </span><span class="nx">sources</span><span class="p">[(</span><span class="nx">separator</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)...</span><span class="nx">sources</span><span class="p">.</span><span class="nx">length</span><span class="p">]</span>
|
||||
<span class="nv">sources = </span><span class="nx">sources</span><span class="p">[</span><span class="mi">0</span><span class="p">...</span><span class="nx">separator</span><span class="p">]</span>
|
||||
<span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nx">run</span>
|
||||
<span class="k">if</span> <span class="nx">opts</span><span class="p">.</span><span class="nx">run</span>
|
||||
<span class="nv">flags = </span><span class="nx">sources</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="nx">sources</span><span class="p">.</span><span class="nx">length</span><span class="p">].</span><span class="nx">concat</span> <span class="nx">flags</span>
|
||||
<span class="nv">sources = </span><span class="p">[</span><span class="nx">sources</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
|
||||
<span class="nv">process.ARGV = process.argv = </span><span class="nx">flags</span>
|
||||
@@ -65,30 +65,32 @@ compile them. If a directory is passed, recursively compile all
|
||||
<span class="nx">compile</span> <span class="nx">path</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">source</span><span class="p">,</span> <span class="nx">file</span><span class="p">)</span>
|
||||
<span class="k">else</span> <span class="k">if</span> <span class="nx">topLevel</span> <span class="o">or</span> <span class="nx">path</span><span class="p">.</span><span class="nx">extname</span><span class="p">(</span><span class="nx">source</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'.coffee'</span>
|
||||
<span class="nx">fs</span><span class="p">.</span><span class="nx">readFile</span> <span class="nx">source</span><span class="p">,</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">code</span><span class="p">)</span> <span class="o">-></span> <span class="nx">compileScript</span><span class="p">(</span><span class="nx">source</span><span class="p">,</span> <span class="nx">code</span><span class="p">.</span><span class="nx">toString</span><span class="p">(),</span> <span class="nx">base</span><span class="p">)</span>
|
||||
<span class="nx">watch</span> <span class="nx">source</span><span class="p">,</span> <span class="nx">base</span> <span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nx">watch</span>
|
||||
<span class="nx">watch</span> <span class="nx">source</span><span class="p">,</span> <span class="nx">base</span> <span class="k">if</span> <span class="nx">opts</span><span class="p">.</span><span class="nx">watch</span>
|
||||
<span class="nx">compile</span> <span class="nx">source</span><span class="p">,</span> <span class="kc">true</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-9">#</a> </div> <p>Compile a single source script, containing the given code, according to the
|
||||
requested options. If evaluating the script directly sets <code>__filename</code>,
|
||||
<code>__dirname</code> and <code>module.filename</code> to be correct relative to the script's path.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">compileScript = </span><span class="p">(</span><span class="nx">source</span><span class="p">,</span> <span class="nx">code</span><span class="p">,</span> <span class="nx">base</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">o = </span><span class="nx">options</span>
|
||||
<span class="nv">codeOpts = </span><span class="nx">compileOptions</span> <span class="nx">source</span>
|
||||
<code>__dirname</code> and <code>module.filename</code> to be correct relative to the script's path.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">compileScript = </span><span class="p">(</span><span class="nx">file</span><span class="p">,</span> <span class="nx">input</span><span class="p">,</span> <span class="nx">base</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">o = </span><span class="nx">opts</span>
|
||||
<span class="nv">options = </span><span class="nx">compileOptions</span> <span class="nx">file</span>
|
||||
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">require</span>
|
||||
<span class="nx">require</span> <span class="nx">fs</span><span class="p">.</span><span class="nx">realpathSync</span> <span class="nx">file</span> <span class="k">for</span> <span class="nx">file</span> <span class="k">in</span> <span class="nx">o</span><span class="p">.</span><span class="nx">require</span>
|
||||
<span class="nx">require</span><span class="p">(</span><span class="k">if</span> <span class="nx">helpers</span><span class="p">.</span><span class="nx">starts</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="s1">'.'</span><span class="p">)</span> <span class="k">then</span> <span class="nx">fs</span><span class="p">.</span><span class="nx">realpathSync</span><span class="p">(</span><span class="nx">req</span><span class="p">)</span> <span class="k">else</span> <span class="nx">req</span><span class="p">)</span> <span class="k">for</span> <span class="nx">req</span> <span class="k">in</span> <span class="nx">o</span><span class="p">.</span><span class="nx">require</span>
|
||||
<span class="k">try</span>
|
||||
<span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">emit</span> <span class="s1">'compile'</span><span class="p">,</span> <span class="p">{</span><span class="nx">source</span><span class="p">,</span> <span class="nx">code</span><span class="p">,</span> <span class="nx">base</span><span class="p">,</span> <span class="nx">options</span><span class="p">}</span>
|
||||
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">tokens</span> <span class="k">then</span> <span class="nx">printTokens</span> <span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">tokens</span> <span class="nx">code</span>
|
||||
<span class="k">else</span> <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">nodes</span> <span class="k">then</span> <span class="nx">puts</span> <span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">nodes</span><span class="p">(</span><span class="nx">code</span><span class="p">).</span><span class="nx">toString</span><span class="p">()</span>
|
||||
<span class="k">else</span> <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">run</span> <span class="k">then</span> <span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">run</span> <span class="nx">code</span><span class="p">,</span> <span class="nx">codeOpts</span>
|
||||
<span class="nv">t = task = </span><span class="p">{</span><span class="nx">file</span><span class="p">,</span> <span class="nx">input</span><span class="p">,</span> <span class="nx">options</span><span class="p">}</span>
|
||||
<span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">emit</span> <span class="s1">'compile'</span><span class="p">,</span> <span class="nx">task</span>
|
||||
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">tokens</span> <span class="k">then</span> <span class="nx">printTokens</span> <span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">tokens</span> <span class="nx">t</span><span class="p">.</span><span class="nx">input</span>
|
||||
<span class="k">else</span> <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">nodes</span> <span class="k">then</span> <span class="nx">puts</span> <span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">nodes</span><span class="p">(</span><span class="nx">t</span><span class="p">.</span><span class="nx">input</span><span class="p">).</span><span class="nx">toString</span><span class="p">()</span>
|
||||
<span class="k">else</span> <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">run</span> <span class="k">then</span> <span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">run</span> <span class="nx">t</span><span class="p">.</span><span class="nx">input</span><span class="p">,</span> <span class="nx">t</span><span class="p">.</span><span class="nx">options</span>
|
||||
<span class="k">else</span>
|
||||
<span class="nv">js = </span><span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">code</span><span class="p">,</span> <span class="nx">codeOpts</span>
|
||||
<span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">emit</span> <span class="s1">'success'</span><span class="p">,</span> <span class="nx">js</span>
|
||||
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">print</span> <span class="k">then</span> <span class="nx">print</span> <span class="nx">js</span>
|
||||
<span class="k">else</span> <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">compile</span> <span class="k">then</span> <span class="nx">writeJs</span> <span class="nx">source</span><span class="p">,</span> <span class="nx">js</span><span class="p">,</span> <span class="nx">base</span>
|
||||
<span class="k">else</span> <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">lint</span> <span class="k">then</span> <span class="nx">lint</span> <span class="nx">js</span>
|
||||
<span class="nv">t.output = </span><span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">t</span><span class="p">.</span><span class="nx">input</span><span class="p">,</span> <span class="nx">t</span><span class="p">.</span><span class="nx">options</span>
|
||||
<span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">emit</span> <span class="s1">'success'</span><span class="p">,</span> <span class="nx">task</span>
|
||||
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">print</span> <span class="k">then</span> <span class="nx">print</span> <span class="nx">t</span><span class="p">.</span><span class="nx">output</span>
|
||||
<span class="k">else</span> <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">compile</span> <span class="k">then</span> <span class="nx">writeJs</span> <span class="nx">t</span><span class="p">.</span><span class="nx">file</span><span class="p">,</span> <span class="nx">t</span><span class="p">.</span><span class="nx">output</span><span class="p">,</span> <span class="nx">base</span>
|
||||
<span class="k">else</span> <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">lint</span> <span class="k">then</span> <span class="nx">lint</span> <span class="nx">t</span><span class="p">.</span><span class="nx">output</span>
|
||||
<span class="k">catch</span> <span class="nx">err</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-10">#</a> </div> <p>Avoid using 'error' as it is a special event -- if there is no handler,
|
||||
node will print a stack trace and exit the program.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">emit</span> <span class="s1">'failure'</span><span class="p">,</span> <span class="nx">err</span>
|
||||
node will print a stack trace and exit the program.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">emit</span> <span class="s1">'failure'</span><span class="p">,</span> <span class="nx">err</span><span class="p">,</span> <span class="nx">task</span>
|
||||
<span class="k">return</span> <span class="k">if</span> <span class="nx">CoffeeScript</span><span class="p">.</span><span class="nx">listeners</span><span class="p">(</span><span class="s1">'failure'</span><span class="p">).</span><span class="nx">length</span>
|
||||
<span class="nx">error</span><span class="p">(</span><span class="nx">err</span><span class="p">.</span><span class="nx">stack</span><span class="p">)</span> <span class="o">and</span> <span class="nx">process</span><span class="p">.</span><span class="nx">exit</span> <span class="mi">1</span> <span class="nx">unless</span> <span class="nx">o</span><span class="p">.</span><span class="nx">watch</span>
|
||||
<span class="nx">puts</span> <span class="nx">err</span><span class="p">.</span><span class="nx">message</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-11">#</a> </div> <p>Attach the appropriate listeners to compile scripts incoming over <strong>stdin</strong>,
|
||||
<span class="k">return</span> <span class="nx">puts</span> <span class="nx">err</span><span class="p">.</span><span class="nx">message</span> <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">watch</span>
|
||||
<span class="nx">error</span> <span class="nx">err</span><span class="p">.</span><span class="nx">stack</span>
|
||||
<span class="nx">process</span><span class="p">.</span><span class="nx">exit</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-11">#</a> </div> <p>Attach the appropriate listeners to compile scripts incoming over <strong>stdin</strong>,
|
||||
and write them back to <strong>stdout</strong>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">compileStdio = </span><span class="o">-></span>
|
||||
<span class="nv">code = </span><span class="s1">''</span>
|
||||
<span class="nv">stdin = </span><span class="nx">process</span><span class="p">.</span><span class="nx">openStdin</span><span class="p">()</span>
|
||||
@@ -99,24 +101,27 @@ and write them back to <strong>stdout</strong>.</p> </td>
|
||||
time the file is updated. May be used in combination with other options,
|
||||
such as <code>--lint</code> or <code>--print</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">watch = </span><span class="p">(</span><span class="nx">source</span><span class="p">,</span> <span class="nx">base</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nx">fs</span><span class="p">.</span><span class="nx">watchFile</span> <span class="nx">source</span><span class="p">,</span> <span class="p">{</span><span class="nx">persistent</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span> <span class="nx">interval</span><span class="o">:</span> <span class="mi">500</span><span class="p">},</span> <span class="p">(</span><span class="nx">curr</span><span class="p">,</span> <span class="nx">prev</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">return</span> <span class="k">if</span> <span class="nx">curr</span><span class="p">.</span><span class="nx">mtime</span><span class="p">.</span><span class="nx">getTime</span><span class="p">()</span> <span class="o">is</span> <span class="nx">prev</span><span class="p">.</span><span class="nx">mtime</span><span class="p">.</span><span class="nx">getTime</span><span class="p">()</span>
|
||||
<span class="nx">fs</span><span class="p">.</span><span class="nx">readFile</span> <span class="nx">source</span><span class="p">,</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">code</span><span class="p">)</span> <span class="o">-></span> <span class="nx">compileScript</span><span class="p">(</span><span class="nx">source</span><span class="p">,</span> <span class="nx">code</span><span class="p">.</span><span class="nx">toString</span><span class="p">(),</span> <span class="nx">base</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-13">#</a> </div> <p>Write out a JavaScript source file with the compiled code. By default, files
|
||||
<span class="k">return</span> <span class="k">if</span> <span class="nx">curr</span><span class="p">.</span><span class="nx">size</span> <span class="o">is</span> <span class="nx">prev</span><span class="p">.</span><span class="nx">size</span> <span class="o">and</span> <span class="nx">curr</span><span class="p">.</span><span class="nx">mtime</span><span class="p">.</span><span class="nx">getTime</span><span class="p">()</span> <span class="o">is</span> <span class="nx">prev</span><span class="p">.</span><span class="nx">mtime</span><span class="p">.</span><span class="nx">getTime</span><span class="p">()</span>
|
||||
<span class="nx">fs</span><span class="p">.</span><span class="nx">readFile</span> <span class="nx">source</span><span class="p">,</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">code</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">throw</span> <span class="nx">err</span> <span class="k">if</span> <span class="nx">err</span>
|
||||
<span class="nx">compileScript</span><span class="p">(</span><span class="nx">source</span><span class="p">,</span> <span class="nx">code</span><span class="p">.</span><span class="nx">toString</span><span class="p">(),</span> <span class="nx">base</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-13">#</a> </div> <p>Write out a JavaScript source file with the compiled code. By default, files
|
||||
are written out in <code>cwd</code> as <code>.js</code> files with the same name, but the output
|
||||
directory can be customized with <code>--output</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">writeJs = </span><span class="p">(</span><span class="nx">source</span><span class="p">,</span> <span class="nx">js</span><span class="p">,</span> <span class="nx">base</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">filename = </span><span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">source</span><span class="p">,</span> <span class="nx">path</span><span class="p">.</span><span class="nx">extname</span><span class="p">(</span><span class="nx">source</span><span class="p">))</span> <span class="o">+</span> <span class="s1">'.js'</span>
|
||||
<span class="nv">srcDir = </span><span class="nx">path</span><span class="p">.</span><span class="nx">dirname</span> <span class="nx">source</span>
|
||||
<span class="nv">baseDir = </span><span class="nx">srcDir</span><span class="p">.</span><span class="nx">substring</span> <span class="nx">base</span><span class="p">.</span><span class="nx">length</span>
|
||||
<span class="nv">dir = </span><span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nx">output</span> <span class="k">then</span> <span class="nx">path</span><span class="p">.</span><span class="nx">join</span> <span class="nx">options</span><span class="p">.</span><span class="nx">output</span><span class="p">,</span> <span class="nx">baseDir</span> <span class="k">else</span> <span class="nx">srcDir</span>
|
||||
<span class="nv">dir = </span><span class="k">if</span> <span class="nx">opts</span><span class="p">.</span><span class="nx">output</span> <span class="k">then</span> <span class="nx">path</span><span class="p">.</span><span class="nx">join</span> <span class="nx">opts</span><span class="p">.</span><span class="nx">output</span><span class="p">,</span> <span class="nx">baseDir</span> <span class="k">else</span> <span class="nx">srcDir</span>
|
||||
<span class="nv">jsPath = </span><span class="nx">path</span><span class="p">.</span><span class="nx">join</span> <span class="nx">dir</span><span class="p">,</span> <span class="nx">filename</span>
|
||||
<span class="nv">compile = </span><span class="o">-></span>
|
||||
<span class="nv">js = </span><span class="s1">' '</span> <span class="k">if</span> <span class="nx">js</span><span class="p">.</span><span class="nx">length</span> <span class="o"><=</span> <span class="mi">0</span>
|
||||
<span class="nx">fs</span><span class="p">.</span><span class="nx">writeFile</span> <span class="nx">jsPath</span><span class="p">,</span> <span class="nx">js</span><span class="p">,</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nx">puts</span> <span class="s2">"Compiled #{source}"</span> <span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nx">compile</span> <span class="o">and</span> <span class="nx">options</span><span class="p">.</span><span class="nx">watch</span>
|
||||
<span class="nx">puts</span> <span class="s2">"Compiled #{source}"</span> <span class="k">if</span> <span class="nx">opts</span><span class="p">.</span><span class="nx">compile</span> <span class="o">and</span> <span class="nx">opts</span><span class="p">.</span><span class="nx">watch</span>
|
||||
<span class="nx">path</span><span class="p">.</span><span class="nx">exists</span> <span class="nx">dir</span><span class="p">,</span> <span class="p">(</span><span class="nx">exists</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">if</span> <span class="nx">exists</span> <span class="k">then</span> <span class="nx">compile</span><span class="p">()</span> <span class="k">else</span> <span class="nx">exec</span> <span class="s2">"mkdir -p #{dir}"</span><span class="p">,</span> <span class="nx">compile</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-14">#</a> </div> <p>Pipe compiled JS through JSLint (requires a working <code>jsl</code> command), printing
|
||||
any errors or warnings that arise.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">lint = </span><span class="p">(</span><span class="nx">js</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">printIt = </span><span class="p">(</span><span class="nx">buffer</span><span class="p">)</span> <span class="o">-></span> <span class="nx">print</span> <span class="nx">buffer</span><span class="p">.</span><span class="nx">toString</span><span class="p">()</span>
|
||||
<span class="nv">jsl = </span><span class="nx">spawn</span> <span class="s1">'jsl'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'-nologo'</span><span class="p">,</span> <span class="s1">'-stdin'</span><span class="p">]</span>
|
||||
<span class="nv">printIt = </span><span class="p">(</span><span class="nx">buffer</span><span class="p">)</span> <span class="o">-></span> <span class="nx">puts</span> <span class="nx">buffer</span><span class="p">.</span><span class="nx">toString</span><span class="p">().</span><span class="nx">trim</span><span class="p">()</span>
|
||||
<span class="nv">conf = </span><span class="nx">__dirname</span> <span class="o">+</span> <span class="s1">'/../extras/jsl.conf'</span>
|
||||
<span class="nv">jsl = </span><span class="nx">spawn</span> <span class="s1">'jsl'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'-nologo'</span><span class="p">,</span> <span class="s1">'-stdin'</span><span class="p">,</span> <span class="s1">'-conf'</span><span class="p">,</span> <span class="nx">conf</span><span class="p">]</span>
|
||||
<span class="nx">jsl</span><span class="p">.</span><span class="nx">stdout</span><span class="p">.</span><span class="kc">on</span> <span class="s1">'data'</span><span class="p">,</span> <span class="nx">printIt</span>
|
||||
<span class="nx">jsl</span><span class="p">.</span><span class="nx">stderr</span><span class="p">.</span><span class="kc">on</span> <span class="s1">'data'</span><span class="p">,</span> <span class="nx">printIt</span>
|
||||
<span class="nx">jsl</span><span class="p">.</span><span class="nx">stdin</span><span class="p">.</span><span class="nx">write</span> <span class="nx">js</span>
|
||||
@@ -126,14 +131,14 @@ any errors or warnings that arise.</p> </td> <td class="
|
||||
<span class="s2">"[#{tag} #{value}]"</span>
|
||||
<span class="nx">puts</span> <span class="nx">strings</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">' '</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-16">#</a> </div> <p>Use the <a href="optparse.html">OptionParser module</a> to extract all options from
|
||||
<code>process.argv</code> that are specified in <code>SWITCHES</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">parseOptions = </span><span class="o">-></span>
|
||||
<span class="nv">optionParser = </span><span class="k">new</span> <span class="nx">optparse</span><span class="p">.</span><span class="nx">OptionParser</span> <span class="nx">SWITCHES</span><span class="p">,</span> <span class="nx">BANNER</span>
|
||||
<span class="nv">o = options = </span><span class="nx">optionParser</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">process</span><span class="p">.</span><span class="nx">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">...</span><span class="nx">process</span><span class="p">.</span><span class="nx">argv</span><span class="p">.</span><span class="nx">length</span><span class="p">])</span>
|
||||
<span class="nx">options</span><span class="p">.</span><span class="nx">compile</span> <span class="o">or=</span> <span class="o">!!</span><span class="nx">o</span><span class="p">.</span><span class="nx">output</span>
|
||||
<span class="nv">options.run = </span><span class="o">not</span> <span class="p">(</span><span class="nx">o</span><span class="p">.</span><span class="nx">compile</span> <span class="o">or</span> <span class="nx">o</span><span class="p">.</span><span class="nx">print</span> <span class="o">or</span> <span class="nx">o</span><span class="p">.</span><span class="nx">lint</span><span class="p">)</span>
|
||||
<span class="nv">options.print = </span><span class="o">!!</span> <span class="p">(</span><span class="nx">o</span><span class="p">.</span><span class="nx">print</span> <span class="o">or</span> <span class="p">(</span><span class="nx">o</span><span class="p">.</span><span class="nb">eval</span> <span class="o">or</span> <span class="nx">o</span><span class="p">.</span><span class="nx">stdio</span> <span class="o">and</span> <span class="nx">o</span><span class="p">.</span><span class="nx">compile</span><span class="p">))</span>
|
||||
<span class="nv">sources = </span><span class="nx">options</span><span class="p">.</span><span class="nx">arguments</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-17">#</a> </div> <p>The compile-time options to pass to the CoffeeScript compiler.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">compileOptions = </span><span class="p">(</span><span class="nx">fileName</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">optionParser = </span><span class="k">new</span> <span class="nx">optparse</span><span class="p">.</span><span class="nx">OptionParser</span> <span class="nx">SWITCHES</span><span class="p">,</span> <span class="nx">BANNER</span>
|
||||
<span class="nv">o = opts = </span><span class="nx">optionParser</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">process</span><span class="p">.</span><span class="nx">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">...</span><span class="nx">process</span><span class="p">.</span><span class="nx">argv</span><span class="p">.</span><span class="nx">length</span><span class="p">])</span>
|
||||
<span class="nx">o</span><span class="p">.</span><span class="nx">compile</span> <span class="o">or=</span> <span class="o">!!</span><span class="nx">o</span><span class="p">.</span><span class="nx">output</span>
|
||||
<span class="nv">o.run = </span><span class="o">not</span> <span class="p">(</span><span class="nx">o</span><span class="p">.</span><span class="nx">compile</span> <span class="o">or</span> <span class="nx">o</span><span class="p">.</span><span class="nx">print</span> <span class="o">or</span> <span class="nx">o</span><span class="p">.</span><span class="nx">lint</span><span class="p">)</span>
|
||||
<span class="nv">o.print = </span><span class="o">!!</span> <span class="p">(</span><span class="nx">o</span><span class="p">.</span><span class="nx">print</span> <span class="o">or</span> <span class="p">(</span><span class="nx">o</span><span class="p">.</span><span class="nb">eval</span> <span class="o">or</span> <span class="nx">o</span><span class="p">.</span><span class="nx">stdio</span> <span class="o">and</span> <span class="nx">o</span><span class="p">.</span><span class="nx">compile</span><span class="p">))</span>
|
||||
<span class="nv">sources = </span><span class="nx">o</span><span class="p">.</span><span class="nx">arguments</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-17">#</a> </div> <p>The compile-time options to pass to the CoffeeScript compiler.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">compileOptions = </span><span class="p">(</span><span class="nx">fileName</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">o = </span><span class="p">{</span><span class="nx">fileName</span><span class="p">}</span>
|
||||
<span class="nv">o.noWrap = </span><span class="nx">options</span><span class="p">[</span><span class="s1">'no-wrap'</span><span class="p">]</span>
|
||||
<span class="nv">o.noWrap = </span><span class="nx">opts</span><span class="p">[</span><span class="s1">'no-wrap'</span><span class="p">]</span>
|
||||
<span class="nx">o</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-18">#</a> </div> <p>Print the <code>--help</code> usage message and exit.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">usage = </span><span class="o">-></span>
|
||||
<span class="nx">puts</span> <span class="nx">optionParser</span><span class="p">.</span><span class="nx">help</span><span class="p">()</span>
|
||||
<span class="nx">process</span><span class="p">.</span><span class="nx">exit</span> <span class="mi">0</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-19">#</a> </div> <p>Print the <code>--version</code> message and exit.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">version = </span><span class="o">-></span>
|
||||
|
||||
@@ -26,7 +26,7 @@ h1, h2, h3, h4, h5, h6 {
|
||||
}
|
||||
#background {
|
||||
position: fixed;
|
||||
top: 0; left: 580px; right: 0; bottom: 0;
|
||||
top: 0; left: 575px; right: 0; bottom: 0;
|
||||
background: #f5f5ff;
|
||||
border-left: 1px solid #e5e5ee;
|
||||
z-index: -1;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<!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="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="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>The CoffeeScript parser is generated by <a href="http://github.com/zaach/jison">Jison</a>
|
||||
<!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="octowrap"> <a class="octothorpe" 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>
|
||||
@@ -49,6 +49,7 @@ all parsing must end here.</p> </td> <td class="code">
|
||||
<span class="nx">o</span> <span class="s2">"Throw"</span>
|
||||
<span class="nx">o</span> <span class="s2">"BREAK"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">LiteralNode</span> <span class="nx">$1</span>
|
||||
<span class="nx">o</span> <span class="s2">"CONTINUE"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">LiteralNode</span> <span class="nx">$1</span>
|
||||
<span class="nx">o</span> <span class="s2">"DEBUGGER"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">LiteralNode</span> <span class="nx">$1</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" 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. Expressions serve as the building blocks of many other rules, making
|
||||
@@ -65,7 +66,6 @@ them somewhat circular.</p> </td> <td class="code">
|
||||
<span class="nx">o</span> <span class="s2">"Switch"</span>
|
||||
<span class="nx">o</span> <span class="s2">"Extends"</span>
|
||||
<span class="nx">o</span> <span class="s2">"Class"</span>
|
||||
<span class="nx">o</span> <span class="s2">"Splat"</span>
|
||||
<span class="nx">o</span> <span class="s2">"Existence"</span>
|
||||
<span class="nx">o</span> <span class="s2">"Comment"</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-13">#</a> </div> <p>An indented block of expressions. Note that the <a href="rewriter.html">Rewriter</a>
|
||||
@@ -187,69 +187,80 @@ and optional references to the superclass.</p> </td> <td
|
||||
<span class="nx">o</span> <span class="s2">"ClassAssign"</span><span class="p">,</span> <span class="o">-></span> <span class="p">[</span><span class="nx">$1</span><span class="p">]</span>
|
||||
<span class="nx">o</span> <span class="s2">"ClassBody TERMINATOR ClassAssign"</span><span class="p">,</span> <span class="o">-></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="s2">"{ ClassBody }"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$2</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-38">#</a> </div> <p>The three flavors of function call: normal, object instantiation with <code>new</code>,
|
||||
and calling <code>super()</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Call</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-38">#</a> </div> <p>The two flavors of function call: normal, and object instantiation with <code>new</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Call</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"Invocation"</span>
|
||||
<span class="nx">o</span> <span class="s2">"Super"</span>
|
||||
<span class="nx">o</span> <span class="s2">"NEW Invocation"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$2</span><span class="p">.</span><span class="nx">newInstance</span><span class="p">()</span>
|
||||
<span class="nx">o</span> <span class="s2">"NEW Value"</span><span class="p">,</span> <span class="o">-></span> <span class="p">(</span><span class="k">new</span> <span class="nx">CallNode</span><span class="p">(</span><span class="nx">$2</span><span class="p">,</span> <span class="p">[])).</span><span class="nx">newInstance</span><span class="p">()</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-39"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-39">#</a> </div> <p>Extending an object by setting its prototype chain to reference a parent
|
||||
object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Extends</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"SimpleAssignable EXTENDS Value"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">ExtendsNode</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-40"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-40">#</a> </div> <p>Ordinary function invocation, or a chained series of calls.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Invocation</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"Value Arguments"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">CallNode</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span> <span class="s2">"Invocation Arguments"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">CallNode</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$2</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-41"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-41">#</a> </div> <p>The list of arguments to a function call.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Arguments</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"CALL_START ArgList OptComma CALL_END"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$2</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-42">#</a> </div> <p>Calling super.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Super</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"Value OptFuncExist Arguments"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">CallNode</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="s2">"Invocation OptFuncExist Arguments"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">CallNode</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="s2">"SUPER"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">CallNode</span> <span class="s1">'super'</span><span class="p">,</span> <span class="p">[</span><span class="k">new</span> <span class="nx">SplatNode</span><span class="p">(</span><span class="k">new</span> <span class="nx">LiteralNode</span><span class="p">(</span><span class="s1">'arguments'</span><span class="p">))]</span>
|
||||
<span class="nx">o</span> <span class="s2">"SUPER Arguments"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">CallNode</span> <span class="s1">'super'</span><span class="p">,</span> <span class="nx">$2</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-41"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-41">#</a> </div> <p>An optional existence check on a function.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">OptFuncExist</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">""</span><span class="p">,</span> <span class="o">-></span> <span class="kc">no</span>
|
||||
<span class="nx">o</span> <span class="s2">"FUNC_EXIST"</span><span class="p">,</span> <span class="o">-></span> <span class="kc">yes</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-42">#</a> </div> <p>The list of arguments to a function call.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Arguments</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"CALL_START CALL_END"</span><span class="p">,</span> <span class="o">-></span> <span class="p">[]</span>
|
||||
<span class="nx">o</span> <span class="s2">"CALL_START ArgList OptComma CALL_END"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$2</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-43"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-43">#</a> </div> <p>A reference to the <em>this</em> current object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">This</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"THIS"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">ValueNode</span> <span class="k">new</span> <span class="nx">LiteralNode</span> <span class="s1">'this'</span>
|
||||
<span class="nx">o</span> <span class="s2">"@"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">ValueNode</span> <span class="k">new</span> <span class="nx">LiteralNode</span> <span class="s1">'this'</span>
|
||||
<span class="p">]</span>
|
||||
|
||||
<span class="nx">RangeDots</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">". ."</span><span class="p">,</span> <span class="o">-></span> <span class="s1">'inclusive'</span>
|
||||
<span class="nx">o</span> <span class="s2">". . ."</span><span class="p">,</span> <span class="o">-></span> <span class="s1">'exclusive'</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-44"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-44">#</a> </div> <p>A reference to a property on <em>this</em>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ThisProperty</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"@ Identifier"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">ValueNode</span> <span class="k">new</span> <span class="nx">LiteralNode</span><span class="p">(</span><span class="s1">'this'</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="nx">AccessorNode</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-45"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-45">#</a> </div> <p>The CoffeeScript range literal.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Range</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"[ Expression . . Expression ]"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">RangeNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$5</span>
|
||||
<span class="nx">o</span> <span class="s2">"[ Expression . . . Expression ]"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">RangeNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$6</span><span class="p">,</span> <span class="kc">true</span>
|
||||
<span class="nx">o</span> <span class="s2">"[ Expression RangeDots Expression ]"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">RangeNode</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-46"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-46">#</a> </div> <p>The slice literal.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Slice</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"INDEX_START Expression . . Expression INDEX_END"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">RangeNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$5</span>
|
||||
<span class="nx">o</span> <span class="s2">"INDEX_START Expression . . . Expression INDEX_END"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">RangeNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$6</span><span class="p">,</span> <span class="kc">true</span>
|
||||
<span class="nx">o</span> <span class="s2">"INDEX_START Expression RangeDots Expression INDEX_END"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">RangeNode</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="nx">o</span> <span class="s2">"INDEX_START Expression RangeDots INDEX_END"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">RangeNode</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="s2">"INDEX_START RangeDots Expression INDEX_END"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">RangeNode</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">$2</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-47"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-47">#</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="s2">"[ ]"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">ArrayNode</span> <span class="p">[]</span>
|
||||
<span class="nx">o</span> <span class="s2">"[ ArgList OptComma ]"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">ArrayNode</span> <span class="nx">$2</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-48"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-48">#</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="nx">ArgList</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">""</span><span class="p">,</span> <span class="o">-></span> <span class="p">[]</span>
|
||||
<span class="nx">o</span> <span class="s2">"Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="p">[</span><span class="nx">$1</span><span class="p">]</span>
|
||||
<span class="nx">o</span> <span class="s2">"ArgList , Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="p">[</span><span class="nx">$3</span><span class="p">]</span>
|
||||
<span class="nx">o</span> <span class="s2">"ArgList OptComma TERMINATOR Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="p">[</span><span class="nx">$4</span><span class="p">]</span>
|
||||
<span class="nx">o</span> <span class="s2">"Arg"</span><span class="p">,</span> <span class="o">-></span> <span class="p">[</span><span class="nx">$1</span><span class="p">]</span>
|
||||
<span class="nx">o</span> <span class="s2">"ArgList , Arg"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="p">[</span><span class="nx">$3</span><span class="p">]</span>
|
||||
<span class="nx">o</span> <span class="s2">"ArgList OptComma TERMINATOR Arg"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span> <span class="p">[</span><span class="nx">$4</span><span class="p">]</span>
|
||||
<span class="nx">o</span> <span class="s2">"INDENT ArgList OptComma OUTDENT"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span> <span class="s2">"ArgList OptComma INDENT ArgList OptComma OUTDENT"</span><span class="p">,</span> <span class="o">-></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-49"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-49">#</a> </div> <p>Just simple, comma-separated, required arguments (no fancy syntax). We need
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-49"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-49">#</a> </div> <p>Valid arguments are Expressions or Splats.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Arg</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"Expression"</span>
|
||||
<span class="nx">o</span> <span class="s2">"Splat"</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-50"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-50">#</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="nx">SimpleArgs</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"Expression"</span>
|
||||
<span class="nx">o</span> <span class="s2">"SimpleArgs , Expression"</span><span class="p">,</span> <span class="o">-></span>
|
||||
<span class="k">if</span> <span class="nx">$1</span> <span class="k">instanceof</span> <span class="nb">Array</span> <span class="k">then</span> <span class="nx">$1</span><span class="p">.</span><span class="nx">concat</span><span class="p">([</span><span class="nx">$3</span><span class="p">])</span> <span class="k">else</span> <span class="p">[</span><span class="nx">$1</span><span class="p">].</span><span class="nx">concat</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="octowrap"> <a class="octothorpe" href="#section-50">#</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="nx">Try</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-51"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-51">#</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="nx">Try</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"TRY Block Catch"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">TryNode</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="s2">"TRY Block FINALLY Block"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">TryNode</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="s2">"TRY Block Catch FINALLY Block"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">TryNode</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-51"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-51">#</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="nx">Catch</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-52">#</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="nx">Catch</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"CATCH Identifier Block"</span><span class="p">,</span> <span class="o">-></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-52"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-52">#</a> </div> <p>Throw an exception object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Throw</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-53">#</a> </div> <p>Throw an exception object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Throw</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"THROW Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">ThrowNode</span> <span class="nx">$2</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-53">#</a> </div> <p>Parenthetical expressions. Note that the <strong>Parenthetical</strong> is a <strong>Value</strong>,
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-54">#</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="nx">Parenthetical</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"( Line )"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">ParentheticalNode</span> <span class="nx">$2</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-54">#</a> </div> <p>The condition portion of a while loop.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">WhileSource</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"( )"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">ParentheticalNode</span> <span class="k">new</span> <span class="nx">LiteralNode</span> <span class="s1">''</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-55"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-55">#</a> </div> <p>The condition portion of a while loop.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">WhileSource</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"WHILE Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">WhileNode</span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span> <span class="s2">"WHILE Expression WHEN Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">WhileNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">guard</span><span class="o">:</span> <span class="nx">$4</span>
|
||||
<span class="nx">o</span> <span class="s2">"UNTIL Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">WhileNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">invert</span><span class="o">:</span> <span class="kc">true</span>
|
||||
<span class="nx">o</span> <span class="s2">"UNTIL Expression WHEN Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">WhileNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">invert</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span> <span class="nx">guard</span><span class="o">:</span> <span class="nx">$4</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-55"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-55">#</a> </div> <p>The while loop can either be normal, with a block of expressions to execute,
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-56"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-56">#</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="nx">While</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"WhileSource Block"</span><span class="p">,</span> <span class="o">-></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="s2">"Statement WhileSource"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$2</span><span class="p">.</span><span class="nx">addBody</span> <span class="nx">Expressions</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="nx">$1</span><span class="p">]</span>
|
||||
@@ -260,7 +271,7 @@ or postfix, with a single expression. There is no do..while.</p> </t
|
||||
<span class="nx">Loop</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"LOOP Block"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">WhileNode</span><span class="p">(</span><span class="k">new</span> <span class="nx">LiteralNode</span> <span class="s1">'true'</span><span class="p">).</span><span class="nx">addBody</span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span> <span class="s2">"LOOP Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">WhileNode</span><span class="p">(</span><span class="k">new</span> <span class="nx">LiteralNode</span> <span class="s1">'true'</span><span class="p">).</span><span class="nx">addBody</span> <span class="nx">Expressions</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-56"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-56">#</a> </div> <p>Array, object, and range comprehensions, at the most generic level.
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-57">#</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="nx">For</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"Statement ForBody"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">ForNode</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$2</span><span class="p">.</span><span class="nx">vars</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nx">$2</span><span class="p">.</span><span class="nx">vars</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
||||
@@ -276,17 +287,17 @@ or postfix, with a single expression.</p> </td> <td clas
|
||||
<span class="nx">ForStart</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"FOR ForVariables"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span> <span class="s2">"FOR ALL ForVariables"</span><span class="p">,</span> <span class="o">-></span> <span class="nv">$3.raw = </span><span class="kc">true</span><span class="p">;</span> <span class="nx">$3</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-57">#</a> </div> <p>An array of all accepted values for a variable inside the loop. This
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-58"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-58">#</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="nx">ForValue</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"Identifier"</span>
|
||||
<span class="nx">o</span> <span class="s2">"Array"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">ValueNode</span> <span class="nx">$1</span>
|
||||
<span class="nx">o</span> <span class="s2">"Object"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">ValueNode</span> <span class="nx">$1</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-58"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-58">#</a> </div> <p>An array or range comprehension has variables for the current element and
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-59">#</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="nx">ForVariables</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"ForValue"</span><span class="p">,</span> <span class="o">-></span> <span class="p">[</span><span class="nx">$1</span><span class="p">]</span>
|
||||
<span class="nx">o</span> <span class="s2">"ForValue , ForValue"</span><span class="p">,</span> <span class="o">-></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-59"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-59">#</a> </div> <p>The source of a comprehension is an array or object with an optional guard
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-60"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-60">#</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="nx">ForSource</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"IN Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">source</span><span class="o">:</span> <span class="nx">$2</span>
|
||||
@@ -296,92 +307,70 @@ in fixed-size increments.</p> </td> <td class="code">
|
||||
<span class="nx">o</span> <span class="s2">"IN Expression BY Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">source</span><span class="o">:</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">step</span><span class="o">:</span> <span class="nx">$4</span>
|
||||
<span class="nx">o</span> <span class="s2">"IN Expression WHEN Expression BY Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">source</span><span class="o">:</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">guard</span><span class="o">:</span> <span class="nx">$4</span><span class="p">,</span> <span class="nx">step</span><span class="o">:</span> <span class="nx">$6</span>
|
||||
<span class="nx">o</span> <span class="s2">"IN Expression BY Expression WHEN Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">source</span><span class="o">:</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">step</span><span class="o">:</span> <span class="nx">$4</span><span class="p">,</span> <span class="nx">guard</span><span class="o">:</span> <span class="nx">$6</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-60"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-60">#</a> </div> <p>The CoffeeScript switch/when/else block replaces the JavaScript
|
||||
switch/case/default by compiling into an if-else chain.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Switch</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"SWITCH Expression INDENT Whens OUTDENT"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$4</span><span class="p">.</span><span class="nx">switchesOver</span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span> <span class="s2">"SWITCH Expression INDENT Whens ELSE Block OUTDENT"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$4</span><span class="p">.</span><span class="nx">switchesOver</span><span class="p">(</span><span class="nx">$2</span><span class="p">).</span><span class="nx">addElse</span> <span class="nx">$6</span><span class="p">,</span> <span class="kc">true</span>
|
||||
<span class="nx">o</span> <span class="s2">"SWITCH INDENT Whens OUTDENT"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$3</span>
|
||||
<span class="nx">o</span> <span class="s2">"SWITCH INDENT Whens ELSE Block OUTDENT"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$3</span><span class="p">.</span><span class="nx">addElse</span> <span class="nx">$5</span><span class="p">,</span> <span class="kc">true</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-61"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-61">#</a> </div> <p>The inner list of whens is left recursive. At code-generation time, the
|
||||
IfNode will rewrite them into a proper chain.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Whens</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="p">]</span>
|
||||
|
||||
<span class="nx">Switch</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"SWITCH Expression INDENT Whens OUTDENT"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">SwitchNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$4</span>
|
||||
<span class="nx">o</span> <span class="s2">"SWITCH Expression INDENT Whens ELSE Block OUTDENT"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">SwitchNode</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="s2">"SWITCH INDENT Whens OUTDENT"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">SwitchNode</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">$3</span>
|
||||
<span class="nx">o</span> <span class="s2">"SWITCH INDENT Whens ELSE Block OUTDENT"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">SwitchNode</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="nx">Whens</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"When"</span>
|
||||
<span class="nx">o</span> <span class="s2">"Whens When"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">addElse</span> <span class="nx">$2</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-62">#</a> </div> <p>An individual <strong>When</strong> clause, with action.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">When</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"LEADING_WHEN SimpleArgs Block"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">IfNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">statement</span><span class="o">:</span> <span class="kc">true</span>
|
||||
<span class="nx">o</span> <span class="s2">"LEADING_WHEN SimpleArgs Block TERMINATOR"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">IfNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">statement</span><span class="o">:</span> <span class="kc">true</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-63">#</a> </div> <p>The most basic form of <em>if</em> is a condition and an action. The following
|
||||
<span class="nx">o</span> <span class="s2">"Whens When"</span><span class="p">,</span> <span class="o">-></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-61"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-61">#</a> </div> <p>An individual <strong>When</strong> clause, with action.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">When</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"LEADING_WHEN SimpleArgs Block"</span><span class="p">,</span> <span class="o">-></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="s2">"LEADING_WHEN SimpleArgs Block TERMINATOR"</span><span class="p">,</span> <span class="o">-></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-62"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-62">#</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="nx">IfBlock</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"IF Expression Block"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">IfNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$3</span>
|
||||
<span class="nx">o</span> <span class="s2">"UNLESS Expression Block"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">IfNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">invert</span><span class="o">:</span> <span class="kc">true</span>
|
||||
<span class="nx">o</span> <span class="s2">"IfBlock ELSE IF Expression Block"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">addElse</span> <span class="p">(</span><span class="k">new</span> <span class="nx">IfNode</span><span class="p">(</span><span class="nx">$4</span><span class="p">,</span> <span class="nx">$5</span><span class="p">)).</span><span class="nx">forceStatement</span><span class="p">()</span>
|
||||
<span class="nx">o</span> <span class="s2">"IfBlock ELSE Block"</span><span class="p">,</span> <span class="o">-></span> <span class="nx">$1</span><span class="p">.</span><span class="nx">addElse</span> <span class="nx">$3</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-64">#</a> </div> <p>The full complement of <em>if</em> expressions, including postfix one-liner
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-63">#</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="nx">If</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"IfBlock"</span>
|
||||
<span class="nx">o</span> <span class="s2">"Statement IF Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">IfNode</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">Expressions</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">statement</span><span class="o">:</span> <span class="kc">true</span>
|
||||
<span class="nx">o</span> <span class="s2">"Expression IF Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">IfNode</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">Expressions</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">statement</span><span class="o">:</span> <span class="kc">true</span>
|
||||
<span class="nx">o</span> <span class="s2">"Statement UNLESS Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">IfNode</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">Expressions</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">statement</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span> <span class="nx">invert</span><span class="o">:</span> <span class="kc">true</span>
|
||||
<span class="nx">o</span> <span class="s2">"Expression UNLESS Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">IfNode</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">Expressions</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">statement</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span> <span class="nx">invert</span><span class="o">:</span> <span class="kc">true</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-65">#</a> </div> <p>Arithmetic and logical operators, working on one or more operands.
|
||||
<span class="nx">o</span> <span class="s2">"Statement POST_IF Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">IfNode</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">Expressions</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">statement</span><span class="o">:</span> <span class="kc">true</span>
|
||||
<span class="nx">o</span> <span class="s2">"Expression POST_IF Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">IfNode</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">Expressions</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">statement</span><span class="o">:</span> <span class="kc">true</span>
|
||||
<span class="nx">o</span> <span class="s2">"Statement POST_UNLESS Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">IfNode</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">Expressions</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">statement</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span> <span class="nx">invert</span><span class="o">:</span> <span class="kc">true</span>
|
||||
<span class="nx">o</span> <span class="s2">"Expression POST_UNLESS Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">IfNode</span> <span class="nx">$3</span><span class="p">,</span> <span class="nx">Expressions</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">statement</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span> <span class="nx">invert</span><span class="o">:</span> <span class="kc">true</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-64">#</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="nx">Operation</span><span class="o">:</span> <span class="p">[</span>
|
||||
<span class="nx">o</span> <span class="s2">"! Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'!'</span><span class="p">,</span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span> <span class="s2">"!! Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'!!'</span><span class="p">,</span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span><span class="p">(</span><span class="s2">"- Expression"</span><span class="p">,</span> <span class="p">(</span><span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span><span class="p">(</span><span class="s1">'-'</span><span class="p">,</span> <span class="nx">$2</span><span class="p">)),</span> <span class="p">{</span><span class="nx">prec</span><span class="o">:</span> <span class="s1">'UMINUS'</span><span class="p">})</span>
|
||||
<span class="nx">o</span><span class="p">(</span><span class="s2">"+ Expression"</span><span class="p">,</span> <span class="p">(</span><span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span><span class="p">(</span><span class="s1">'+'</span><span class="p">,</span> <span class="nx">$2</span><span class="p">)),</span> <span class="p">{</span><span class="nx">prec</span><span class="o">:</span> <span class="s1">'UPLUS'</span><span class="p">})</span>
|
||||
<span class="nx">o</span> <span class="s2">"~ Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'~'</span><span class="p">,</span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span> <span class="s2">"UNARY Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span><span class="p">(</span><span class="s2">"- Expression"</span><span class="p">,</span> <span class="p">(</span><span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span><span class="p">(</span><span class="s1">'-'</span><span class="p">,</span> <span class="nx">$2</span><span class="p">)),</span> <span class="p">{</span><span class="nx">prec</span><span class="o">:</span> <span class="s1">'UNARY'</span><span class="p">})</span>
|
||||
<span class="nx">o</span><span class="p">(</span><span class="s2">"+ Expression"</span><span class="p">,</span> <span class="p">(</span><span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span><span class="p">(</span><span class="s1">'+'</span><span class="p">,</span> <span class="nx">$2</span><span class="p">)),</span> <span class="p">{</span><span class="nx">prec</span><span class="o">:</span> <span class="s1">'UNARY'</span><span class="p">})</span>
|
||||
|
||||
<span class="nx">o</span> <span class="s2">"-- Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'--'</span><span class="p">,</span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span> <span class="s2">"++ Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'++'</span><span class="p">,</span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span> <span class="s2">"DELETE Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'delete'</span><span class="p">,</span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span> <span class="s2">"TYPEOF Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'typeof'</span><span class="p">,</span> <span class="nx">$2</span>
|
||||
<span class="nx">o</span> <span class="s2">"Expression --"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'--'</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="s2">"Expression ++"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'++'</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="s2">"Expression * Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'*'</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="s2">"Expression / Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'/'</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="s2">"Expression % Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'%'</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="s2">"Expression ? Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'?'</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="s2">"Expression + Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'+'</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="s2">"Expression - Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'-'</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="s2">"Expression << Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'<<'</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="s2">"Expression >> Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'>>'</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="s2">"Expression >>> Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'>>>'</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="s2">"Expression & Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'&'</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="s2">"Expression | Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'|'</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="s2">"Expression ^ Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'^'</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="s2">"Expression <= Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'<='</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="s2">"Expression < Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'<'</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="s2">"Expression > Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'>'</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="s2">"Expression >= Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'>='</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="s2">"Expression == Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'=='</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="s2">"Expression != Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'!='</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="s2">"Expression && Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'&&'</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="s2">"Expression || Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'||'</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="s2">"Expression OP? Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'?'</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="s2">"Expression MATH Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</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="s2">"Expression SHIFT Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</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="s2">"Expression COMPARE Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</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="s2">"Expression LOGIC Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</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="s2">"Value COMPOUND_ASSIGN Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</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="s2">"Value COMPOUND_ASSIGN INDENT Expression OUTDENT"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$4</span>
|
||||
|
||||
<span class="nx">o</span> <span class="s2">"Expression -= Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'-='</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="s2">"Expression += Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'+='</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="s2">"Expression /= Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'/='</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="s2">"Expression *= Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'*='</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="s2">"Expression %= Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'%='</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="s2">"Expression ||= Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'||='</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="s2">"Expression &&= Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'&&='</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="s2">"Expression ?= Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'?='</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="s2">"Expression INSTANCEOF Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'instanceof'</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="s2">"Expression IN Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">InNode</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$3</span>
|
||||
<span class="nx">o</span> <span class="s2">"Expression OF Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'in'</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="s2">"Expression ! IN Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'!'</span><span class="p">,</span> <span class="k">new</span> <span class="nx">InNode</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$4</span>
|
||||
<span class="nx">o</span> <span class="s2">"Expression ! OF Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'!'</span><span class="p">,</span> <span class="k">new</span> <span class="nx">ParentheticalNode</span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'in'</span><span class="p">,</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-66"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-66">#</a> </div> <h2>Precedence</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-67">#</a> </div> <p>Operators at the top of this list have higher precedence than the ones lower
|
||||
<span class="nx">o</span> <span class="s2">"Expression INSTANCEOF Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'instanceof'</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="s2">"Expression UNARY IN Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="k">new</span> <span class="nx">InNode</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$4</span>
|
||||
<span class="nx">o</span> <span class="s2">"Expression UNARY OF Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="k">new</span> <span class="nx">ParentheticalNode</span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'in'</span><span class="p">,</span> <span class="nx">$1</span><span class="p">,</span> <span class="nx">$4</span>
|
||||
<span class="nx">o</span> <span class="s2">"Expression UNARY INSTANCEOF Expression"</span><span class="p">,</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="nx">$2</span><span class="p">,</span> <span class="k">new</span> <span class="nx">ParentheticalNode</span> <span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'instanceof'</span><span class="p">,</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-65"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-65">#</a> </div> <h2>Precedence</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-66"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-66">#</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)
|
||||
@@ -391,26 +380,25 @@ down. Following these rules is what makes <code>2 + 3 * 4</code> parse as:</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="s2">"left"</span><span class="p">,</span> <span class="s1">'?'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"nonassoc"</span><span class="p">,</span> <span class="s1">'UMINUS'</span><span class="p">,</span> <span class="s1">'UPLUS'</span><span class="p">,</span> <span class="s1">'!'</span><span class="p">,</span> <span class="s1">'!!'</span><span class="p">,</span> <span class="s1">'~'</span><span class="p">,</span> <span class="s1">'++'</span><span class="p">,</span> <span class="s1">'--'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'*'</span><span class="p">,</span> <span class="s1">'/'</span><span class="p">,</span> <span class="s1">'%'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"right"</span><span class="p">,</span> <span class="s1">'?'</span><span class="p">,</span> <span class="s1">'NEW'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'CALL_START'</span><span class="p">,</span> <span class="s1">'CALL_END'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"nonassoc"</span><span class="p">,</span> <span class="s1">'++'</span><span class="p">,</span> <span class="s1">'--'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"right"</span><span class="p">,</span> <span class="s1">'UNARY'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'MATH'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'+'</span><span class="p">,</span> <span class="s1">'-'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'<<'</span><span class="p">,</span> <span class="s1">'>>'</span><span class="p">,</span> <span class="s1">'>>>'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'&'</span><span class="p">,</span> <span class="s1">'|'</span><span class="p">,</span> <span class="s1">'^'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'<='</span><span class="p">,</span> <span class="s1">'<'</span><span class="p">,</span> <span class="s1">'>'</span><span class="p">,</span> <span class="s1">'>='</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"right"</span><span class="p">,</span> <span class="s1">'DELETE'</span><span class="p">,</span> <span class="s1">'INSTANCEOF'</span><span class="p">,</span> <span class="s1">'TYPEOF'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'SHIFT'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'COMPARE'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'INSTANCEOF'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'=='</span><span class="p">,</span> <span class="s1">'!='</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'&&'</span><span class="p">,</span> <span class="s1">'||'</span><span class="p">,</span> <span class="s1">'OP?'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"right"</span><span class="p">,</span> <span class="s1">'-='</span><span class="p">,</span> <span class="s1">'+='</span><span class="p">,</span> <span class="s1">'/='</span><span class="p">,</span> <span class="s1">'*='</span><span class="p">,</span> <span class="s1">'%='</span><span class="p">,</span> <span class="s1">'||='</span><span class="p">,</span> <span class="s1">'&&='</span><span class="p">,</span> <span class="s1">'?='</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'LOGIC'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"right"</span><span class="p">,</span> <span class="s1">'COMPOUND_ASSIGN'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'.'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"right"</span><span class="p">,</span> <span class="s1">'INDENT'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'OUTDENT'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"nonassoc"</span><span class="p">,</span> <span class="s1">'INDENT'</span><span class="p">,</span> <span class="s1">'OUTDENT'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"right"</span><span class="p">,</span> <span class="s1">'WHEN'</span><span class="p">,</span> <span class="s1">'LEADING_WHEN'</span><span class="p">,</span> <span class="s1">'IN'</span><span class="p">,</span> <span class="s1">'OF'</span><span class="p">,</span> <span class="s1">'BY'</span><span class="p">,</span> <span class="s1">'THROW'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"right"</span><span class="p">,</span> <span class="s1">'FOR'</span><span class="p">,</span> <span class="s1">'WHILE'</span><span class="p">,</span> <span class="s1">'UNTIL'</span><span class="p">,</span> <span class="s1">'LOOP'</span><span class="p">,</span> <span class="s1">'NEW'</span><span class="p">,</span> <span class="s1">'SUPER'</span><span class="p">,</span> <span class="s1">'CLASS'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"left"</span><span class="p">,</span> <span class="s1">'EXTENDS'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"right"</span><span class="p">,</span> <span class="s1">'IF'</span><span class="p">,</span> <span class="s1">'UNLESS'</span><span class="p">,</span> <span class="s1">'ELSE'</span><span class="p">,</span> <span class="s1">'FOR'</span><span class="p">,</span> <span class="s1">'WHILE'</span><span class="p">,</span> <span class="s1">'UNTIL'</span><span class="p">,</span> <span class="s1">'LOOP'</span><span class="p">,</span> <span class="s1">'SUPER'</span><span class="p">,</span> <span class="s1">'CLASS'</span><span class="p">,</span> <span class="s1">'EXTENDS'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"right"</span><span class="p">,</span> <span class="s1">'='</span><span class="p">,</span> <span class="s1">':'</span><span class="p">,</span> <span class="s1">'RETURN'</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="s2">"right"</span><span class="p">,</span> <span class="s1">'->'</span><span class="p">,</span> <span class="s1">'=>'</span><span class="p">,</span> <span class="s1">'UNLESS'</span><span class="p">,</span> <span class="s1">'IF'</span><span class="p">,</span> <span class="s1">'ELSE'</span><span class="p">]</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-68"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-68">#</a> </div> <h2>Wrapping Up</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-69"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-69">#</a> </div> <p>Finally, now what we have our <strong>grammar</strong> and our <strong>operators</strong>, we can create
|
||||
<span class="p">[</span><span class="s2">"right"</span><span class="p">,</span> <span class="s1">'->'</span><span class="p">,</span> <span class="s1">'=>'</span><span class="p">,</span> <span class="s1">'UNLESS'</span><span class="p">,</span> <span class="s1">'POST_IF'</span><span class="p">,</span> <span class="s1">'POST_UNLESS'</span><span class="p">]</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-67">#</a> </div> <h2>Wrapping Up</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-68"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-68">#</a> </div> <p>Finally, now what 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>
|
||||
@@ -419,14 +407,13 @@ as "tokens".</p> </td> <td class="code"> <
|
||||
<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="s1">' '</span>
|
||||
<span class="nx">tokens</span><span class="p">.</span><span class="nx">push</span> <span class="nx">token</span> <span class="nx">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="s2">"return #{alt[1]}"</span> <span class="k">if</span> <span class="nx">name</span> <span class="o">is</span> <span class="s1">'Root'</span>
|
||||
<span class="nx">alt</span></pre></div> </td> </tr> <tr id="section-70"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-70">#</a> </div> <p>Initialize the <strong>Parser</strong> with our list of terminal <strong>tokens</strong>, our <strong>grammar</strong>
|
||||
<span class="nx">alt</span></pre></div> </td> </tr> <tr id="section-69"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-69">#</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="p">{</span>
|
||||
(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="nx">tokens</span><span class="o">:</span> <span class="nx">tokens</span><span class="p">.</span><span class="nx">join</span> <span class="s1">' '</span>
|
||||
<span class="nx">bnf</span><span class="o">:</span> <span class="nx">grammar</span>
|
||||
<span class="nx">operators</span><span class="o">:</span> <span class="nx">operators</span><span class="p">.</span><span class="nx">reverse</span><span class="p">()</span>
|
||||
<span class="nx">startSymbol</span><span class="o">:</span> <span class="s1">'Root'</span>
|
||||
<span class="p">}</span>
|
||||
|
||||
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html> <html> <head> <title>helpers.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="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> helpers.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>This file contains the common helper functions that we'd like to share among
|
||||
<!DOCTYPE html> <html> <head> <title>helpers.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> helpers.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>This file contains the common helper functions that we'd like to share among
|
||||
the <strong>Lexer</strong>, <strong>Rewriter</strong>, and the <strong>Nodes</strong>. Merge objects, flatten
|
||||
arrays, count characters, that sort of thing.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-2">#</a> </div> <p>Set up exported variables for both <strong>Node.js</strong> and the browser.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">this</span><span class="p">.</span><span class="nv">exports = </span><span class="k">this</span> <span class="nx">unless</span> <span class="nx">process</span><span class="o">?</span>
|
||||
<span class="nv">helpers = exports.helpers = </span><span class="p">{}</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-3">#</a> </div> <p>Cross-browser indexOf, so that IE can join the party.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">helpers.indexOf = indexOf = </span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">item</span><span class="p">,</span> <span class="nx">from</span><span class="p">)</span> <span class="o">-></span>
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<!DOCTYPE html> <html> <head> <title>index.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="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> index.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>Loader for CoffeeScript as a Node.js library.</p> </td> <td class="code"> <div class="highlight"><pre><span class="p">(</span><span class="nx">exports</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">val</span><span class="p">)</span> <span class="k">for</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">require</span> <span class="s1">'./coffee-script'</span>
|
||||
<!DOCTYPE html> <html> <head> <title>index.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> index.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>Loader for CoffeeScript as a Node.js library.</p> </td> <td class="code"> <div class="highlight"><pre><span class="p">(</span><span class="nx">exports</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">val</span><span class="p">)</span> <span class="k">for</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">require</span> <span class="s1">'./coffee-script'</span>
|
||||
|
||||
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html> <html> <head> <title>optparse.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="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> optparse.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>A simple <strong>OptionParser</strong> class to parse option flags from the command-line.
|
||||
<!DOCTYPE html> <html> <head> <title>optparse.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> optparse.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>A simple <strong>OptionParser</strong> class to parse option flags from the command-line.
|
||||
Use it like so:</p>
|
||||
|
||||
<pre><code>parser = new OptionParser switches, helpBanner
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html> <html> <head> <title>repl.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="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> repl.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>A very simple Read-Eval-Print-Loop. Compiles one line at a time to JavaScript
|
||||
<!DOCTYPE html> <html> <head> <title>repl.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> repl.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>A very simple Read-Eval-Print-Loop. Compiles one line at a time to JavaScript
|
||||
and evaluates it. Good for simple tests, or poking around the <strong>Node.js</strong> API.
|
||||
Using it looks like this:</p>
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html> <html> <head> <title>rewriter.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="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> rewriter.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>The CoffeeScript language has a good deal of optional syntax, implicit syntax,
|
||||
<!DOCTYPE html> <html> <head> <title>rewriter.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> rewriter.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>The CoffeeScript language has a good deal of optional syntax, implicit syntax,
|
||||
and shorthand syntax. This can greatly complicate a grammar and bloat
|
||||
the resulting parse table. Instead of making the parser handle it all, we take
|
||||
a series of passes over the token stream, using this <strong>Rewriter</strong> to convert
|
||||
@@ -21,6 +21,7 @@ corrected before implicit parentheses can be wrapped around blocks of code.</p>
|
||||
<span class="nx">@closeOpenCalls</span><span class="p">()</span>
|
||||
<span class="nx">@closeOpenIndexes</span><span class="p">()</span>
|
||||
<span class="nx">@addImplicitIndentation</span><span class="p">()</span>
|
||||
<span class="nx">@tagPostfixConditionals</span><span class="p">()</span>
|
||||
<span class="nx">@addImplicitBraces</span><span class="p">()</span>
|
||||
<span class="nx">@addImplicitParentheses</span><span class="p">()</span>
|
||||
<span class="nx">@ensureBalance</span> <span class="nx">BALANCED_PAIRS</span>
|
||||
@@ -33,7 +34,7 @@ our feet.</p> </td> <td class="code"> <div
|
||||
<span class="nv">i = </span><span class="mi">0</span>
|
||||
<span class="nx">loop</span>
|
||||
<span class="k">break</span> <span class="nx">unless</span> <span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span>
|
||||
<span class="nv">move = </span><span class="nx">block</span> <span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span> <span class="nx">i</span>
|
||||
<span class="nv">move = </span><span class="nx">block</span><span class="p">.</span><span class="nx">call</span> <span class="k">this</span><span class="p">,</span> <span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span> <span class="nx">i</span>
|
||||
<span class="nx">i</span> <span class="o">+=</span> <span class="nx">move</span>
|
||||
<span class="kc">true</span>
|
||||
|
||||
@@ -48,7 +49,7 @@ our feet.</p> </td> <td class="code"> <div
|
||||
<span class="nx">i</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||
<span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-8">#</a> </div> <p>Massage newlines and indentations so that comments don't have to be
|
||||
correctly indented, or appear on a line of their own.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">adjustComments</span><span class="o">:</span> <span class="o">-></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">return</span> <span class="mi">1</span> <span class="nx">unless</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'HERECOMMENT'</span>
|
||||
<span class="p">[</span><span class="nx">before</span><span class="p">,</span> <span class="nx">prev</span><span class="p">,</span> <span class="nx">post</span><span class="p">,</span> <span class="nx">after</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">2</span><span class="p">],</span> <span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span> <span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">],</span> <span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">2</span><span class="p">]]</span>
|
||||
<span class="k">if</span> <span class="nx">after</span> <span class="o">and</span> <span class="nx">after</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'INDENT'</span>
|
||||
@@ -69,19 +70,23 @@ correctly indented, or appear on a line of their own.</p> </td>
|
||||
dispatch them here.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">removeLeadingNewlines</span><span class="o">:</span> <span class="o">-></span>
|
||||
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">shift</span><span class="p">()</span> <span class="k">while</span> <span class="nx">@tokens</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">and</span> <span class="nx">@tokens</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'TERMINATOR'</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-10">#</a> </div> <p>Some blocks occur in the middle of expressions -- when we're expecting
|
||||
this, remove their trailing newlines.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">removeMidExpressionNewlines</span><span class="o">:</span> <span class="o">-></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">return</span> <span class="mi">1</span> <span class="nx">unless</span> <span class="nx">include</span><span class="p">(</span><span class="nx">EXPRESSION_CLOSE</span><span class="p">,</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span> <span class="o">and</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'TERMINATOR'</span>
|
||||
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="mi">1</span>
|
||||
<span class="k">return</span> <span class="mi">0</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-11">#</a> </div> <p>The lexer has tagged the opening parenthesis of a method call. Match it with
|
||||
its paired close.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">closeOpenCalls</span><span class="o">:</span> <span class="o">-></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span>
|
||||
its paired close. We have the mis-nested outdent case included here for
|
||||
calls that close on the same line, just before their outdent.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">closeOpenCalls</span><span class="o">:</span> <span class="o">-></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'CALL_START'</span>
|
||||
<span class="nv">condition = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="p">[</span><span class="s1">')'</span><span class="p">,</span> <span class="s1">'CALL_END'</span><span class="p">]</span>
|
||||
<span class="nv">action = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'CALL_END'</span>
|
||||
<span class="nv">condition = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="p">(</span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="p">[</span><span class="s1">')'</span><span class="p">,</span> <span class="s1">'CALL_END'</span><span class="p">])</span> <span class="o">or</span> <span class="p">(</span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'OUTDENT'</span> <span class="o">and</span> <span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">')'</span><span class="p">)</span>
|
||||
<span class="nv">action = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">idx = </span><span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'OUTDENT'</span> <span class="k">then</span> <span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span> <span class="k">else</span> <span class="nx">i</span>
|
||||
<span class="nx">@tokens</span><span class="p">[</span><span class="nx">idx</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'CALL_END'</span>
|
||||
<span class="nx">@detectEnd</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">condition</span><span class="p">,</span> <span class="nx">action</span>
|
||||
<span class="k">return</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-12">#</a> </div> <p>The lexer has tagged the opening parenthesis of an indexing operation call.
|
||||
Match it with its paired close.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">closeOpenIndexes</span><span class="o">:</span> <span class="o">-></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'INDEX_START'</span>
|
||||
<span class="nv">condition = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="p">[</span><span class="s1">']'</span><span class="p">,</span> <span class="s1">'INDEX_END'</span><span class="p">]</span>
|
||||
<span class="nv">action = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'INDEX_END'</span>
|
||||
@@ -89,7 +94,7 @@ Match it with its paired close.</p> </td> <td class="cod
|
||||
<span class="k">return</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-13">#</a> </div> <p>Object literals may be written with implicit braces, for simple cases.
|
||||
Insert the missing braces here, so that the parser doesn't have to.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">addImplicitBraces</span><span class="o">:</span> <span class="o">-></span>
|
||||
<span class="nv">stack = </span><span class="p">[]</span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">if</span> <span class="nx">include</span> <span class="nx">EXPRESSION_START</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
||||
<span class="nx">stack</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'INDENT'</span> <span class="o">and</span> <span class="p">(</span><span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'{'</span><span class="p">))</span> <span class="k">then</span> <span class="s1">'{'</span> <span class="k">else</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
|
||||
<span class="k">if</span> <span class="nx">include</span> <span class="nx">EXPRESSION_END</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
||||
@@ -98,7 +103,9 @@ Insert the missing braces here, so that the parser doesn't have to.</p>
|
||||
<span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">':'</span> <span class="o">and</span> <span class="p">(</span><span class="o">not</span> <span class="nx">last</span> <span class="o">or</span> <span class="nx">last</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">isnt</span> <span class="s1">'{'</span><span class="p">)</span>
|
||||
<span class="nx">stack</span><span class="p">.</span><span class="nx">push</span> <span class="s1">'{'</span>
|
||||
<span class="nv">idx = </span><span class="k">if</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'@'</span> <span class="k">then</span> <span class="nx">i</span> <span class="o">-</span> <span class="mi">2</span> <span class="k">else</span> <span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span>
|
||||
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">idx</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="p">[</span><span class="s1">'{'</span><span class="p">,</span> <span class="s1">'{'</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]]</span>
|
||||
<span class="nv">tok = </span><span class="p">[</span><span class="s1">'{'</span><span class="p">,</span> <span class="s1">'{'</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]]</span>
|
||||
<span class="nv">tok.generated = </span><span class="kc">yes</span>
|
||||
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">idx</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">tok</span>
|
||||
<span class="nv">condition = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="p">[</span><span class="nx">one</span><span class="p">,</span> <span class="nx">two</span><span class="p">,</span> <span class="nx">three</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@tokens</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">4</span><span class="p">)</span>
|
||||
<span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="s1">'HERECOMMENT'</span> <span class="k">in</span> <span class="p">[</span><span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span>
|
||||
@@ -111,37 +118,50 @@ Insert the missing braces here, so that the parser doesn't have to.</p>
|
||||
<span class="k">return</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-14">#</a> </div> <p>Methods may be optionally called without parentheses, for simple cases.
|
||||
Insert the implicit parentheses here, so that the parser doesn't have to
|
||||
deal with them.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">addImplicitParentheses</span><span class="o">:</span> <span class="o">-></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span>
|
||||
<span class="nv">prev = </span><span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
|
||||
<span class="k">if</span> <span class="nx">prev</span> <span class="o">and</span> <span class="nx">prev</span><span class="p">.</span><span class="nx">spaced</span> <span class="o">and</span> <span class="nx">include</span><span class="p">(</span><span class="nx">IMPLICIT_FUNC</span><span class="p">,</span> <span class="nx">prev</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">and</span> <span class="nx">include</span><span class="p">(</span><span class="nx">IMPLICIT_CALL</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">and</span>
|
||||
<span class="o">not</span> <span class="p">(</span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'!'</span> <span class="o">and</span> <span class="p">(</span><span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'IN'</span><span class="p">,</span> <span class="s1">'OF'</span><span class="p">]))</span>
|
||||
<span class="nv">classLine = </span><span class="kc">no</span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">classLine = </span><span class="kc">yes</span> <span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'CLASS'</span>
|
||||
<span class="nv">prev = </span><span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
|
||||
<span class="nv">next = </span><span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
|
||||
<span class="nv">idx = </span><span class="mi">1</span>
|
||||
<span class="nv">callObject = </span><span class="o">not</span> <span class="nx">classLine</span> <span class="o">and</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'INDENT'</span> <span class="o">and</span> <span class="nx">next</span> <span class="o">and</span> <span class="nx">next</span><span class="p">.</span><span class="nx">generated</span> <span class="o">and</span> <span class="nx">next</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'{'</span> <span class="o">and</span> <span class="nx">prev</span> <span class="o">and</span> <span class="nx">include</span><span class="p">(</span><span class="nx">IMPLICIT_FUNC</span><span class="p">,</span> <span class="nx">prev</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
|
||||
<span class="nv">idx = </span><span class="mi">2</span> <span class="k">if</span> <span class="nx">callObject</span>
|
||||
<span class="nv">seenSingle = </span><span class="kc">no</span>
|
||||
<span class="nv">classLine = </span><span class="kc">no</span> <span class="k">if</span> <span class="nx">include</span><span class="p">(</span><span class="nx">LINEBREAKS</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
|
||||
<span class="nv">token.call = </span><span class="kc">yes</span> <span class="k">if</span> <span class="nx">prev</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">prev</span><span class="p">.</span><span class="nx">spaced</span> <span class="o">and</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'?'</span>
|
||||
<span class="k">if</span> <span class="nx">prev</span> <span class="o">and</span> <span class="p">(</span><span class="nx">prev</span><span class="p">.</span><span class="nx">spaced</span> <span class="o">and</span> <span class="p">(</span><span class="nx">include</span><span class="p">(</span><span class="nx">IMPLICIT_FUNC</span><span class="p">,</span> <span class="nx">prev</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">or</span> <span class="nx">prev</span><span class="p">.</span><span class="nx">call</span><span class="p">)</span> <span class="o">and</span> <span class="nx">include</span><span class="p">(</span><span class="nx">IMPLICIT_CALL</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">and</span>
|
||||
<span class="o">not</span> <span class="p">(</span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'UNARY'</span> <span class="o">and</span> <span class="p">(</span><span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'IN'</span><span class="p">,</span> <span class="s1">'OF'</span><span class="p">,</span> <span class="s1">'INSTANCEOF'</span><span class="p">])))</span> <span class="o">or</span> <span class="nx">callObject</span>
|
||||
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="p">[</span><span class="s1">'CALL_START'</span><span class="p">,</span> <span class="s1">'('</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]]</span>
|
||||
<span class="nv">condition = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">return</span> <span class="kc">yes</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">seenSingle</span> <span class="o">and</span> <span class="nx">token</span><span class="p">.</span><span class="nx">fromThen</span>
|
||||
<span class="nv">seenSingle = </span><span class="kc">yes</span> <span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'IF'</span><span class="p">,</span> <span class="s1">'ELSE'</span><span class="p">,</span> <span class="s1">'UNLESS'</span><span class="p">,</span> <span class="s1">'->'</span><span class="p">,</span> <span class="s1">'=>'</span><span class="p">]</span>
|
||||
<span class="p">(</span><span class="o">not</span> <span class="nx">token</span><span class="p">.</span><span class="nx">generated</span> <span class="o">and</span> <span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">isnt</span> <span class="s1">','</span> <span class="o">and</span> <span class="nx">include</span><span class="p">(</span><span class="nx">IMPLICIT_END</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">and</span>
|
||||
<span class="o">not</span> <span class="p">(</span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'INDENT'</span> <span class="o">and</span> <span class="p">(</span><span class="nx">include</span><span class="p">(</span><span class="nx">IMPLICIT_BLOCK</span><span class="p">,</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="o">or</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'CLASS'</span><span class="p">)))</span> <span class="o">or</span>
|
||||
<span class="o">not</span> <span class="p">(</span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'INDENT'</span> <span class="o">and</span> <span class="p">(</span><span class="nx">include</span><span class="p">(</span><span class="nx">IMPLICIT_BLOCK</span><span class="p">,</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="o">or</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'CLASS'</span> <span class="o">or</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'{'</span><span class="p">)))</span> <span class="o">or</span>
|
||||
<span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'PROPERTY_ACCESS'</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'OUTDENT'</span>
|
||||
<span class="nv">action = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">idx = </span><span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'OUTDENT'</span> <span class="k">then</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">else</span> <span class="nx">i</span>
|
||||
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">idx</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="p">[</span><span class="s1">'CALL_END'</span><span class="p">,</span> <span class="s1">')'</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]]</span>
|
||||
<span class="nx">@detectEnd</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">condition</span><span class="p">,</span> <span class="nx">action</span>
|
||||
<span class="nx">@detectEnd</span> <span class="nx">i</span> <span class="o">+</span> <span class="nx">idx</span><span class="p">,</span> <span class="nx">condition</span><span class="p">,</span> <span class="nx">action</span>
|
||||
<span class="nx">prev</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'FUNC_EXIST'</span> <span class="k">if</span> <span class="nx">prev</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'?'</span>
|
||||
<span class="k">return</span> <span class="mi">2</span>
|
||||
<span class="k">return</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-15">#</a> </div> <p>Because our grammar is LALR(1), it can't handle some single-line
|
||||
expressions that lack ending delimiters. The <strong>Rewriter</strong> adds the implicit
|
||||
blocks, so it doesn't need to. ')' can close a single-line block,
|
||||
but we need to make sure it's balanced.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">addImplicitIndentation</span><span class="o">:</span> <span class="o">-></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'ELSE'</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">isnt</span> <span class="s1">'OUTDENT'</span>
|
||||
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">@indentation</span><span class="p">(</span><span class="nx">token</span><span class="p">)...</span>
|
||||
<span class="k">return</span> <span class="mi">2</span>
|
||||
<span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'CATCH'</span> <span class="o">and</span>
|
||||
<span class="p">(</span><span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">2</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'TERMINATOR'</span> <span class="o">or</span> <span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">2</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'FINALLY'</span><span class="p">)</span>
|
||||
<span class="p">(</span><span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'TERMINATOR'</span> <span class="o">or</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'FINALLY'</span><span class="p">)</span>
|
||||
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">@indentation</span><span class="p">(</span><span class="nx">token</span><span class="p">)...</span>
|
||||
<span class="k">return</span> <span class="mi">4</span>
|
||||
<span class="k">if</span> <span class="nx">include</span><span class="p">(</span><span class="nx">SINGLE_LINERS</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">isnt</span> <span class="s1">'INDENT'</span> <span class="o">and</span>
|
||||
<span class="o">not</span> <span class="p">(</span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'ELSE'</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'IF'</span><span class="p">)</span>
|
||||
<span class="nv">starter = </span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
||||
<span class="p">[</span><span class="nx">indent</span><span class="p">,</span> <span class="nx">outdent</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@indentation</span> <span class="nx">token</span>
|
||||
<span class="nv">indent.generated = outdent.generated = </span><span class="kc">true</span>
|
||||
<span class="nv">indent.fromThen = </span><span class="kc">true</span> <span class="k">if</span> <span class="nx">starter</span> <span class="o">is</span> <span class="s1">'THEN'</span>
|
||||
<span class="nv">indent.generated = outdent.generated = </span><span class="kc">true</span>
|
||||
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">indent</span>
|
||||
<span class="nv">condition = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="p">(</span><span class="nx">include</span><span class="p">(</span><span class="nx">SINGLE_CLOSERS</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">and</span> <span class="nx">token</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">isnt</span> <span class="s1">';'</span><span class="p">)</span> <span class="o">and</span>
|
||||
@@ -152,11 +172,22 @@ but we need to make sure it's balanced.</p> </td> <td cl
|
||||
<span class="nx">@detectEnd</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">2</span><span class="p">,</span> <span class="nx">condition</span><span class="p">,</span> <span class="nx">action</span>
|
||||
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="mi">1</span> <span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'THEN'</span>
|
||||
<span class="k">return</span> <span class="mi">2</span>
|
||||
<span class="k">return</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-16">#</a> </div> <p>Ensure that all listed pairs of tokens are correctly balanced throughout
|
||||
<span class="k">return</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-16">#</a> </div> <p>Tag postfix conditionals as such, so that we can parse them with a
|
||||
different precedence.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">tagPostfixConditionals</span><span class="o">:</span> <span class="o">-></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'IF'</span><span class="p">,</span> <span class="s1">'UNLESS'</span><span class="p">]</span>
|
||||
<span class="nv">original = </span><span class="nx">token</span>
|
||||
<span class="nv">condition = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'TERMINATOR'</span><span class="p">,</span> <span class="s1">'INDENT'</span><span class="p">]</span>
|
||||
<span class="nv">action = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nx">original</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'POST_'</span> <span class="o">+</span> <span class="nx">original</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">isnt</span> <span class="s1">'INDENT'</span>
|
||||
<span class="nx">@detectEnd</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">condition</span><span class="p">,</span> <span class="nx">action</span>
|
||||
<span class="k">return</span> <span class="mi">1</span>
|
||||
<span class="k">return</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-17">#</a> </div> <p>Ensure that all listed pairs of tokens are correctly balanced throughout
|
||||
the course of the token stream.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ensureBalance</span><span class="o">:</span> <span class="p">(</span><span class="nx">pairs</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">levels = </span><span class="p">{}</span>
|
||||
<span class="nv">openLine = </span><span class="p">{}</span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">for</span> <span class="nx">pair</span> <span class="k">in</span> <span class="nx">pairs</span>
|
||||
<span class="p">[</span><span class="nx">open</span><span class="p">,</span> <span class="nx">close</span><span class="p">]</span> <span class="o">=</span> <span class="nx">pair</span>
|
||||
<span class="nx">levels</span><span class="p">[</span><span class="nx">open</span><span class="p">]</span> <span class="o">or=</span> <span class="mi">0</span>
|
||||
@@ -170,7 +201,7 @@ the course of the token stream.</p> </td> <td class="cod
|
||||
<span class="k">if</span> <span class="nx">unclosed</span><span class="p">.</span><span class="nx">length</span>
|
||||
<span class="nv">open = </span><span class="nx">unclosed</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
||||
<span class="nv">line = </span><span class="nx">openLine</span><span class="p">[</span><span class="nx">open</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
|
||||
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s2">"unclosed #{open} on line #{line}"</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-17">#</a> </div> <p>We'd like to support syntax like this:</p>
|
||||
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s2">"unclosed #{open} on line #{line}"</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-18">#</a> </div> <p>We'd like to support syntax like this:</p>
|
||||
|
||||
<pre><code>el.click((event) ->
|
||||
el.hide())
|
||||
@@ -192,7 +223,7 @@ rewriting.</li>
|
||||
<span class="nv">stack = </span><span class="p">[]</span>
|
||||
<span class="nv">debt = </span><span class="p">{}</span>
|
||||
<span class="p">(</span><span class="nx">debt</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="k">for</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">INVERSES</span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span>
|
||||
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">tag = </span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
||||
<span class="nv">inv = </span><span class="nx">INVERSES</span><span class="p">[</span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
|
||||
<span class="k">if</span> <span class="nx">include</span> <span class="nx">EXPRESSION_START</span><span class="p">,</span> <span class="nx">tag</span>
|
||||
@@ -217,20 +248,20 @@ rewriting.</li>
|
||||
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">val</span>
|
||||
<span class="k">return</span> <span class="mi">1</span>
|
||||
<span class="k">else</span>
|
||||
<span class="k">return</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-18">#</a> </div> <p>Generate the indentation tokens, based on another token on the same line.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">indentation</span><span class="o">:</span> <span class="p">(</span><span class="nx">token</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="p">[[</span><span class="s1">'INDENT'</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]],</span> <span class="p">[</span><span class="s1">'OUTDENT'</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]]]</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-19">#</a> </div> <p>Look up a tag by token index.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">tag</span><span class="o">:</span> <span class="p">(</span><span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">and</span> <span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-20">#</a> </div> <h2>Constants</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-21">#</a> </div> <p>List of the token pairs that must be balanced.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">BALANCED_PAIRS = </span><span class="p">[[</span><span class="s1">'('</span><span class="p">,</span> <span class="s1">')'</span><span class="p">],</span> <span class="p">[</span><span class="s1">'['</span><span class="p">,</span> <span class="s1">']'</span><span class="p">],</span> <span class="p">[</span><span class="s1">'{'</span><span class="p">,</span> <span class="s1">'}'</span><span class="p">],</span> <span class="p">[</span><span class="s1">'INDENT'</span><span class="p">,</span> <span class="s1">'OUTDENT'</span><span class="p">],</span>
|
||||
<span class="p">[</span><span class="s1">'PARAM_START'</span><span class="p">,</span> <span class="s1">'PARAM_END'</span><span class="p">],</span> <span class="p">[</span><span class="s1">'CALL_START'</span><span class="p">,</span> <span class="s1">'CALL_END'</span><span class="p">],</span> <span class="p">[</span><span class="s1">'INDEX_START'</span><span class="p">,</span> <span class="s1">'INDEX_END'</span><span class="p">]]</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-22">#</a> </div> <p>The inverse mappings of <code>BALANCED_PAIRS</code> we're trying to fix up, so we can
|
||||
<span class="k">return</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-19">#</a> </div> <p>Generate the indentation tokens, based on another token on the same line.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">indentation</span><span class="o">:</span> <span class="p">(</span><span class="nx">token</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="p">[[</span><span class="s1">'INDENT'</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]],</span> <span class="p">[</span><span class="s1">'OUTDENT'</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]]]</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-20">#</a> </div> <p>Look up a tag by token index.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">tag</span><span class="o">:</span> <span class="p">(</span><span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">and</span> <span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-21">#</a> </div> <h2>Constants</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-22">#</a> </div> <p>List of the token pairs that must be balanced.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">BALANCED_PAIRS = </span><span class="p">[[</span><span class="s1">'('</span><span class="p">,</span> <span class="s1">')'</span><span class="p">],</span> <span class="p">[</span><span class="s1">'['</span><span class="p">,</span> <span class="s1">']'</span><span class="p">],</span> <span class="p">[</span><span class="s1">'{'</span><span class="p">,</span> <span class="s1">'}'</span><span class="p">],</span> <span class="p">[</span><span class="s1">'INDENT'</span><span class="p">,</span> <span class="s1">'OUTDENT'</span><span class="p">],</span>
|
||||
<span class="p">[</span><span class="s1">'PARAM_START'</span><span class="p">,</span> <span class="s1">'PARAM_END'</span><span class="p">],</span> <span class="p">[</span><span class="s1">'CALL_START'</span><span class="p">,</span> <span class="s1">'CALL_END'</span><span class="p">],</span> <span class="p">[</span><span class="s1">'INDEX_START'</span><span class="p">,</span> <span class="s1">'INDEX_END'</span><span class="p">]]</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-23">#</a> </div> <p>The inverse mappings of <code>BALANCED_PAIRS</code> we're trying to fix up, so we can
|
||||
look things up from either end.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">INVERSES = </span><span class="p">{}</span>
|
||||
<span class="k">for</span> <span class="nx">pair</span> <span class="k">in</span> <span class="nx">BALANCED_PAIRS</span>
|
||||
<span class="nx">INVERSES</span><span class="p">[</span><span class="nx">pair</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="nx">pair</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
||||
<span class="nx">INVERSES</span><span class="p">[</span><span class="nx">pair</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span> <span class="o">=</span> <span class="nx">pair</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-23">#</a> </div> <p>The tokens that signal the start of a balanced pair.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">EXPRESSION_START = </span><span class="nx">pair</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="nx">pair</span> <span class="k">in</span> <span class="nx">BALANCED_PAIRS</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-24">#</a> </div> <p>The tokens that signal the end of a balanced pair.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">EXPRESSION_END = </span><span class="nx">pair</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="nx">pair</span> <span class="k">in</span> <span class="nx">BALANCED_PAIRS</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-25">#</a> </div> <p>Tokens that indicate the close of a clause of an expression.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">EXPRESSION_CLOSE = </span><span class="p">[</span><span class="s1">'CATCH'</span><span class="p">,</span> <span class="s1">'WHEN'</span><span class="p">,</span> <span class="s1">'ELSE'</span><span class="p">,</span> <span class="s1">'FINALLY'</span><span class="p">].</span><span class="nx">concat</span> <span class="nx">EXPRESSION_END</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-26">#</a> </div> <p>Tokens that, if followed by an <code>IMPLICIT_CALL</code>, indicate a function invocation.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IMPLICIT_FUNC = </span><span class="p">[</span><span class="s1">'IDENTIFIER'</span><span class="p">,</span> <span class="s1">'SUPER'</span><span class="p">,</span> <span class="s1">')'</span><span class="p">,</span> <span class="s1">'CALL_END'</span><span class="p">,</span> <span class="s1">']'</span><span class="p">,</span> <span class="s1">'INDEX_END'</span><span class="p">,</span> <span class="s1">'@'</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-27">#</a> </div> <p>If preceded by an <code>IMPLICIT_FUNC</code>, indicates a function invocation.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IMPLICIT_CALL = </span><span class="p">[</span>
|
||||
<span class="nx">INVERSES</span><span class="p">[</span><span class="nx">pair</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span> <span class="o">=</span> <span class="nx">pair</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-24">#</a> </div> <p>The tokens that signal the start of a balanced pair.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">EXPRESSION_START = </span><span class="nx">pair</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="nx">pair</span> <span class="k">in</span> <span class="nx">BALANCED_PAIRS</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-25">#</a> </div> <p>The tokens that signal the end of a balanced pair.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">EXPRESSION_END = </span><span class="nx">pair</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="nx">pair</span> <span class="k">in</span> <span class="nx">BALANCED_PAIRS</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-26">#</a> </div> <p>Tokens that indicate the close of a clause of an expression.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">EXPRESSION_CLOSE = </span><span class="p">[</span><span class="s1">'CATCH'</span><span class="p">,</span> <span class="s1">'WHEN'</span><span class="p">,</span> <span class="s1">'ELSE'</span><span class="p">,</span> <span class="s1">'FINALLY'</span><span class="p">].</span><span class="nx">concat</span> <span class="nx">EXPRESSION_END</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-27">#</a> </div> <p>Tokens that, if followed by an <code>IMPLICIT_CALL</code>, indicate a function invocation.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IMPLICIT_FUNC = </span><span class="p">[</span><span class="s1">'IDENTIFIER'</span><span class="p">,</span> <span class="s1">'SUPER'</span><span class="p">,</span> <span class="s1">')'</span><span class="p">,</span> <span class="s1">'CALL_END'</span><span class="p">,</span> <span class="s1">']'</span><span class="p">,</span> <span class="s1">'INDEX_END'</span><span class="p">,</span> <span class="s1">'@'</span><span class="p">,</span> <span class="s1">'THIS'</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-28">#</a> </div> <p>If preceded by an <code>IMPLICIT_FUNC</code>, indicates a function invocation.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IMPLICIT_CALL = </span><span class="p">[</span>
|
||||
<span class="s1">'IDENTIFIER'</span><span class="p">,</span> <span class="s1">'NUMBER'</span><span class="p">,</span> <span class="s1">'STRING'</span><span class="p">,</span> <span class="s1">'JS'</span><span class="p">,</span> <span class="s1">'REGEX'</span><span class="p">,</span> <span class="s1">'NEW'</span><span class="p">,</span> <span class="s1">'PARAM_START'</span><span class="p">,</span> <span class="s1">'CLASS'</span><span class="p">,</span>
|
||||
<span class="s1">'TRY'</span><span class="p">,</span> <span class="s1">'DELETE'</span><span class="p">,</span> <span class="s1">'TYPEOF'</span><span class="p">,</span> <span class="s1">'SWITCH'</span><span class="p">,</span> <span class="s1">'THIS'</span><span class="p">,</span> <span class="s1">'NULL'</span><span class="p">,</span>
|
||||
<span class="s1">'IF'</span><span class="p">,</span> <span class="s1">'UNLESS'</span><span class="p">,</span> <span class="s1">'TRY'</span><span class="p">,</span> <span class="s1">'SWITCH'</span><span class="p">,</span> <span class="s1">'THIS'</span><span class="p">,</span> <span class="s1">'NULL'</span><span class="p">,</span> <span class="s1">'UNARY'</span>
|
||||
<span class="s1">'TRUE'</span><span class="p">,</span> <span class="s1">'FALSE'</span><span class="p">,</span> <span class="s1">'YES'</span><span class="p">,</span> <span class="s1">'NO'</span><span class="p">,</span> <span class="s1">'ON'</span><span class="p">,</span> <span class="s1">'OFF'</span><span class="p">,</span>
|
||||
<span class="s1">'!'</span><span class="p">,</span> <span class="s1">'!!'</span><span class="p">,</span> <span class="s1">'@'</span><span class="p">,</span> <span class="s1">'->'</span><span class="p">,</span> <span class="s1">'=>'</span><span class="p">,</span> <span class="s1">'['</span><span class="p">,</span> <span class="s1">'('</span><span class="p">,</span> <span class="s1">'{'</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-28">#</a> </div> <p>Tokens indicating that the implicit call must enclose a block of expressions.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IMPLICIT_BLOCK = </span><span class="p">[</span><span class="s1">'->'</span><span class="p">,</span> <span class="s1">'=>'</span><span class="p">,</span> <span class="s1">'{'</span><span class="p">,</span> <span class="s1">'['</span><span class="p">,</span> <span class="s1">','</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-29">#</a> </div> <p>Tokens that always mark the end of an implicit call for single-liners.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IMPLICIT_END = </span><span class="p">[</span><span class="s1">'IF'</span><span class="p">,</span> <span class="s1">'UNLESS'</span><span class="p">,</span> <span class="s1">'FOR'</span><span class="p">,</span> <span class="s1">'WHILE'</span><span class="p">,</span> <span class="s1">'UNTIL'</span><span class="p">,</span> <span class="s1">'LOOP'</span><span class="p">,</span> <span class="s1">'TERMINATOR'</span><span class="p">,</span> <span class="s1">'INDENT'</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-30">#</a> </div> <p>Single-line flavors of block expressions that have unclosed endings.
|
||||
<span class="s1">'@'</span><span class="p">,</span> <span class="s1">'->'</span><span class="p">,</span> <span class="s1">'=>'</span><span class="p">,</span> <span class="s1">'['</span><span class="p">,</span> <span class="s1">'('</span><span class="p">,</span> <span class="s1">'{'</span>
|
||||
<span class="p">]</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-29">#</a> </div> <p>Tokens indicating that the implicit call must enclose a block of expressions.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IMPLICIT_BLOCK = </span><span class="p">[</span><span class="s1">'->'</span><span class="p">,</span> <span class="s1">'=>'</span><span class="p">,</span> <span class="s1">'{'</span><span class="p">,</span> <span class="s1">'['</span><span class="p">,</span> <span class="s1">','</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-30">#</a> </div> <p>Tokens that always mark the end of an implicit call for single-liners.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IMPLICIT_END = </span><span class="p">[</span><span class="s1">'POST_IF'</span><span class="p">,</span> <span class="s1">'POST_UNLESS'</span><span class="p">,</span> <span class="s1">'FOR'</span><span class="p">,</span> <span class="s1">'WHILE'</span><span class="p">,</span> <span class="s1">'UNTIL'</span><span class="p">,</span> <span class="s1">'LOOP'</span><span class="p">,</span> <span class="s1">'TERMINATOR'</span><span class="p">,</span> <span class="s1">'INDENT'</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-31">#</a> </div> <p>Single-line flavors of block expressions that have unclosed endings.
|
||||
The grammar can't disambiguate them, so we insert the implicit indentation.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">SINGLE_LINERS = </span><span class="p">[</span><span class="s1">'ELSE'</span><span class="p">,</span> <span class="s2">"->"</span><span class="p">,</span> <span class="s2">"=>"</span><span class="p">,</span> <span class="s1">'TRY'</span><span class="p">,</span> <span class="s1">'FINALLY'</span><span class="p">,</span> <span class="s1">'THEN'</span><span class="p">]</span>
|
||||
<span class="nv">SINGLE_CLOSERS = </span><span class="p">[</span><span class="s1">'TERMINATOR'</span><span class="p">,</span> <span class="s1">'CATCH'</span><span class="p">,</span> <span class="s1">'FINALLY'</span><span class="p">,</span> <span class="s1">'ELSE'</span><span class="p">,</span> <span class="s1">'OUTDENT'</span><span class="p">,</span> <span class="s1">'LEADING_WHEN'</span><span class="p">]</span>
|
||||
<span class="nv">SINGLE_CLOSERS = </span><span class="p">[</span><span class="s1">'TERMINATOR'</span><span class="p">,</span> <span class="s1">'CATCH'</span><span class="p">,</span> <span class="s1">'FINALLY'</span><span class="p">,</span> <span class="s1">'ELSE'</span><span class="p">,</span> <span class="s1">'OUTDENT'</span><span class="p">,</span> <span class="s1">'LEADING_WHEN'</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-32">#</a> </div> <p>Tokens that end a line.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">LINEBREAKS = </span><span class="p">[</span><span class="s1">'TERMINATOR'</span><span class="p">,</span> <span class="s1">'INDENT'</span><span class="p">,</span> <span class="s1">'OUTDENT'</span><span class="p">]</span>
|
||||
|
||||
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html> <html> <head> <title>scope.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="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> scope.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>The <strong>Scope</strong> class regulates lexical scoping within CoffeeScript. As you
|
||||
<!DOCTYPE html> <html> <head> <title>scope.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> scope.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> <p>The <strong>Scope</strong> class regulates lexical scoping within CoffeeScript. As you
|
||||
generate code, you create a tree of scopes in the same shape as the nested
|
||||
function bodies. Each scope knows about the variables declared within it,
|
||||
and has a reference to its parent enclosing scope. In this way, we know which
|
||||
@@ -16,16 +16,18 @@ it wraps.</p> </td> <td class="code"> <div
|
||||
<span class="k">else</span>
|
||||
<span class="nv">Scope.root = </span><span class="k">this</span>
|
||||
<span class="vi">@tempVar = </span><span class="s1">'_a'</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-5">#</a> </div> <p>Look up a variable name in lexical scope, and declare it if it does not
|
||||
already exist.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">find</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">return</span> <span class="kc">true</span> <span class="k">if</span> <span class="nx">@check</span> <span class="nx">name</span>
|
||||
already exist.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">find</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">return</span> <span class="kc">true</span> <span class="k">if</span> <span class="nx">@check</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">options</span>
|
||||
<span class="nx">@variables</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'var'</span>
|
||||
<span class="kc">false</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-6">#</a> </div> <p>Test variables and return true the first time fn(v, k) returns true</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">any</span><span class="o">:</span> <span class="p">(</span><span class="nx">fn</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">for</span> <span class="nx">v</span><span class="p">,</span> <span class="nx">k</span> <span class="k">of</span> <span class="nx">@variables</span> <span class="k">when</span> <span class="nx">fn</span><span class="p">(</span><span class="nx">v</span><span class="p">,</span> <span class="nx">k</span><span class="p">)</span>
|
||||
<span class="k">return</span> <span class="kc">true</span>
|
||||
<span class="k">return</span> <span class="kc">false</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-7">#</a> </div> <p>Reserve a variable name as originating from a function parameter for this
|
||||
scope. No <code>var</code> required for internal references.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">parameter</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nx">@variables</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'param'</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-8">#</a> </div> <p>Just check to see if a variable has already been declared, without reserving.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">check</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="k">return</span> <span class="kc">true</span> <span class="k">if</span> <span class="nx">@variables</span><span class="p">.</span><span class="nx">hasOwnProperty</span> <span class="nx">name</span>
|
||||
<span class="nx">@variables</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'param'</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-8">#</a> </div> <p>Just check to see if a variable has already been declared, without reserving,
|
||||
walks up to the root scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">check</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">-></span>
|
||||
<span class="nv">immediate = </span><span class="nb">Object</span><span class="o">::</span><span class="nx">hasOwnProperty</span><span class="p">.</span><span class="nx">call</span> <span class="nx">@variables</span><span class="p">,</span> <span class="nx">name</span>
|
||||
<span class="k">return</span> <span class="nx">immediate</span> <span class="k">if</span> <span class="nx">immediate</span> <span class="o">or</span> <span class="p">(</span><span class="nx">options</span> <span class="o">and</span> <span class="nx">options</span><span class="p">.</span><span class="nx">immediate</span><span class="p">)</span>
|
||||
<span class="o">!!</span><span class="p">(</span><span class="nx">@parent</span> <span class="o">and</span> <span class="nx">@parent</span><span class="p">.</span><span class="nx">check</span><span class="p">(</span><span class="nx">name</span><span class="p">))</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-9">#</a> </div> <p>If we need to store an intermediate result, find an available name for a
|
||||
compiler-generated variable. <code>_a</code>, <code>_b</code>, and so on...</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">freeVariable</span><span class="o">:</span> <span class="o">-></span>
|
||||
<span class="k">while</span> <span class="nx">@check</span> <span class="nx">@tempVar</span>
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -53,7 +53,7 @@
|
||||
<a href="#pattern_matching">Pattern Matching</a>
|
||||
<a href="#fat_arrow">Function Binding</a>
|
||||
<a href="#embedded">Embedded JavaScript</a>
|
||||
<a href="#switch">Switch/When/Else</a>
|
||||
<a href="#switch">The Switch Statement</a>
|
||||
<a href="#try">Try/Catch/Finally</a>
|
||||
<a href="#comparisons">Chained Comparisons</a>
|
||||
<a href="#interpolation">String and RegExp Interpolation</a>
|
||||
@@ -127,7 +127,7 @@ alert reverse '.eeffoC yrT'</textarea></div>
|
||||
|
||||
<p>
|
||||
<b>Latest Version:</b>
|
||||
<a href="http://github.com/jashkenas/coffee-script/tarball/0.9.1">0.9.1</a>
|
||||
<a href="http://github.com/jashkenas/coffee-script/tarball/0.9.3">0.9.3</a>
|
||||
</p>
|
||||
|
||||
<h2>
|
||||
@@ -172,7 +172,7 @@ alert reverse '.eeffoC yrT'</textarea></div>
|
||||
Then clone the CoffeeScript
|
||||
<a href="http://github.com/jashkenas/coffee-script">source repository</a>
|
||||
from GitHub, or download the latest
|
||||
release: <a href="http://github.com/jashkenas/coffee-script/tarball/0.9.1">0.9.1</a>.
|
||||
release: <a href="http://github.com/jashkenas/coffee-script/tarball/0.9.3">0.9.3</a>.
|
||||
To install the CoffeeScript compiler system-wide
|
||||
under <tt>/usr/local</tt>, open the directory and run:
|
||||
</p>
|
||||
@@ -716,12 +716,11 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
<p>
|
||||
<span id="switch" class="bookmark"></span>
|
||||
<b class="header">Switch/When/Else</b>
|
||||
<b>Switch</b> statements in JavaScript are rather broken. You can only
|
||||
do comparisons based on string equality, and need to remember to <b>break</b> at the end of
|
||||
every <b>case</b> statement to avoid accidentally falling through to
|
||||
the default case. CoffeeScript compiles <b>switch</b> statements into JavaScript if-else chains, allowing you to
|
||||
compare any object (via <b>===</b>), preventing fall-through, and resulting
|
||||
in a returnable, assignable expression. The format is: <tt>switch</tt> condition,
|
||||
<b>Switch</b> statements in JavaScript are a bit awkward. You need to
|
||||
remember to <b>break</b> at the end of every <b>case</b> statement to
|
||||
avoid accidentally falling through to the default case.
|
||||
CoffeeScript prevents accidental fall-through, and can convert the <tt>switch</tt>
|
||||
into a returnable, assignable expression. The format is: <tt>switch</tt> condition,
|
||||
<tt>when</tt> clauses, <tt>else</tt> the default case.
|
||||
</p>
|
||||
<p>
|
||||
@@ -752,9 +751,8 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
<p>
|
||||
<span id="interpolation" class="bookmark"></span>
|
||||
<b class="header">String and RegExp Interpolation</b>
|
||||
A version of <a href="http://wiki.ecmascript.org/doku.php?id=strawman:string_interpolation">ECMAScript Harmony's proposed string interpolation</a>
|
||||
is included in CoffeeScript. Simple variables can be included by prefixing
|
||||
them with a hash mark.
|
||||
Ruby-style string interpolation is included in CoffeeScript. Double-quoted
|
||||
strings allow for interpolated values, while single-quoted strings are literal.
|
||||
</p>
|
||||
<%= code_for('interpolation', 'quote') %>
|
||||
<p>
|
||||
@@ -907,7 +905,7 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
</li>
|
||||
<li>
|
||||
<b>dsc</b>'s <a href="http://github.com/dsc/coffeecup">CoffeeCup</a>
|
||||
— A Python WSGI middleware that compiles CoffeeScript to JavaScript
|
||||
— a Python WSGI middleware that compiles CoffeeScript to JavaScript
|
||||
on-demand during development.
|
||||
</li>
|
||||
<li>
|
||||
@@ -920,9 +918,14 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
— a custom filter for rendering CoffeeScript inline within
|
||||
<a href="http://haml-lang.com/">HAML</a> templates.
|
||||
</li>
|
||||
<li>
|
||||
<b>chrislloyd</b>'s <a href="http://github.com/chrislloyd/roast">Roast</a>
|
||||
— a CoffeeScript compiler plug-in that allows you to include external
|
||||
source files.
|
||||
</li>
|
||||
<li>
|
||||
<b>jashkenas</b>'s <a href="http://jashkenas.github.com/docco/">Docco</a>
|
||||
— A quick-and-dirty literate-programming-style documentation generator
|
||||
— a quick-and-dirty literate-programming-style documentation generator
|
||||
for CoffeeScript. Used to produce the annotated source.
|
||||
</li>
|
||||
</ul>
|
||||
@@ -947,10 +950,33 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
Change Log
|
||||
</h2>
|
||||
|
||||
<p>
|
||||
<b class="header" style="margin-top: 20px;">0.9.3</b>
|
||||
CoffeeScript <tt>switch</tt> statements now compile into JS <tt>switch</tt>
|
||||
statements — they previously compiled into <tt>if/else</tt> chains
|
||||
for JavaScript 1.3 compatibility.
|
||||
Soaking a function invocation is now supported. Users of the RubyMine
|
||||
editor should now be able to use <tt>--watch</tt> mode.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b class="header" style="margin-top: 20px;">0.9.2</b>
|
||||
Specifying the start and end of a range literal is now optional, eg. <tt>array[3..]</tt>.
|
||||
You can now say <tt>a not instanceof b</tt>.
|
||||
Fixed important bugs with nested significant and non-significant indentation (Issue #637).
|
||||
Added a <tt>--require</tt> flag that allows you to hook into the <tt>coffee</tt> command.
|
||||
Added a custom <tt>jsl.conf</tt> file for our preferred JavaScriptLint setup.
|
||||
Sped up Jison grammar compilation time by flattening rules for operations.
|
||||
Block comments can now be used with JavaScript-minifier-friendly syntax.
|
||||
Added JavaScript's compound assignment bitwise operators. Bugfixes to
|
||||
implicit object literals with leading number and string keys, as the subject
|
||||
of implicit calls, and as part of compound assignment.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b class="header" style="margin-top: 20px;">0.9.1</b>
|
||||
Bugfix release for <b>0.9.1</b>. Greatly improves the handling of mixed
|
||||
implicit objects, implicit function calls, and implicit indentation.
|
||||
implicit objects, implicit function calls, and implicit indentation.
|
||||
String and regex interpolation is now strictly <tt>#{ ... }</tt> (Ruby style).
|
||||
The compiler now takes a <tt>--require</tt> flag, which specifies scripts
|
||||
to run before compilation.
|
||||
@@ -964,7 +990,7 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
literals use <tt>:</tt>, as in JavaScript. This allows us to have implicit
|
||||
object literals, and YAML-style object definitions. Half assignments are
|
||||
removed, in favor of <tt>+=</tt>, <tt>or=</tt>, and friends.
|
||||
Interpolation now uses a hash mark <tt>#</tt> instead of the dollar sign
|
||||
Interpolation now uses a hash mark <tt>#</tt> instead of the dollar sign
|
||||
<tt>$</tt> — because dollar signs may be part of a valid JS identifier.
|
||||
Downwards range comprehensions are now safe again, and are optimized to
|
||||
straight for loops when created with integer endpoints.
|
||||
@@ -979,7 +1005,7 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
on the object — useful in constructors and setter functions.
|
||||
Constructor functions can now take splats.
|
||||
</p>
|
||||
|
||||
|
||||
<p>
|
||||
<b class="header" style="margin-top: 20px;">0.7.2</b>
|
||||
Quick bugfix (right after 0.7.1) for a problem that prevented <tt>coffee</tt>
|
||||
@@ -1185,7 +1211,7 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
conversion. Splats. Splice literals. Object comprehensions. Blocks.
|
||||
The existential operator. Many thanks to all the folks who posted issues,
|
||||
with special thanks to
|
||||
<a href="http://github.com/kamatsu">Liam O'Connor-Davis</a> for whitespace
|
||||
<a href="http://github.com/liamoc">Liam O'Connor-Davis</a> for whitespace
|
||||
and expression help.
|
||||
</p>
|
||||
|
||||
|
||||
@@ -5,12 +5,12 @@ if (ignition === true) {
|
||||
if (band !== SpinalTap) {
|
||||
volume = 10;
|
||||
}
|
||||
if (!(answer === false)) {
|
||||
if (answer !== false) {
|
||||
letTheWildRumpusBegin();
|
||||
}
|
||||
if (car.speed < limit) {
|
||||
accelerate();
|
||||
};
|
||||
}
|
||||
if ((47 === pick || 92 === pick || 13 === pick)) {
|
||||
winner = true;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
/*
|
||||
CoffeeScript Compiler v0.9.1
|
||||
/*CoffeeScript Compiler v0.9.3
|
||||
Released under the MIT License
|
||||
*/
|
||||
@@ -6,5 +6,5 @@ task('build:parser', 'rebuild the Jison parser', function(options) {
|
||||
require('jison');
|
||||
code = require('./lib/grammar').parser.generate();
|
||||
dir = options.output || 'lib';
|
||||
return fs.writeFile(("" + (dir) + "/parser.js"), code);
|
||||
return fs.writeFile("" + (dir) + "/parser.js", code);
|
||||
});
|
||||
@@ -5,7 +5,7 @@ var __extends = function(child, parent) {
|
||||
child.prototype = new ctor();
|
||||
child.prototype.constructor = child;
|
||||
if (typeof parent.extended === "function") parent.extended(child);
|
||||
child.__superClass__ = parent.prototype;
|
||||
child.__super__ = parent.prototype;
|
||||
};
|
||||
Animal = function(_a) {
|
||||
this.name = _a;
|
||||
@@ -20,7 +20,7 @@ Snake = function() {
|
||||
__extends(Snake, Animal);
|
||||
Snake.prototype.move = function() {
|
||||
alert("Slithering...");
|
||||
return Snake.__superClass__.move.call(this, 5);
|
||||
return Snake.__super__.move.call(this, 5);
|
||||
};
|
||||
Horse = function() {
|
||||
return Animal.apply(this, arguments);
|
||||
@@ -28,7 +28,7 @@ Horse = function() {
|
||||
__extends(Horse, Animal);
|
||||
Horse.prototype.move = function() {
|
||||
alert("Galloping...");
|
||||
return Horse.__superClass__.move.call(this, 45);
|
||||
return Horse.__super__.move.call(this, 45);
|
||||
};
|
||||
sam = new Snake("Sammy the Python");
|
||||
tom = new Horse("Tommy the Palomino");
|
||||
|
||||
@@ -9,4 +9,4 @@ if (happy && knowsIt) {
|
||||
showIt();
|
||||
}
|
||||
date = friday ? sue : jill;
|
||||
options = options || defaults;
|
||||
options || (options = defaults);
|
||||
@@ -1,4 +1,4 @@
|
||||
var dates, sentence, sep;
|
||||
sentence = ("" + (22 / 7) + " is a decent approximation of π");
|
||||
sep = "[.\\/\\- ]";
|
||||
dates = (new RegExp(("\\d+" + (sep) + "\\d+" + (sep) + "\\d+"), "g"));
|
||||
dates = (new RegExp("\\d+" + (sep) + "\\d+" + (sep) + "\\d+", "g"));
|
||||
@@ -21,7 +21,7 @@ race = function(winner) {
|
||||
runners = __slice.call(arguments, 1);
|
||||
return print(winner, runners);
|
||||
};
|
||||
if ((typeof elvis !== "undefined" && elvis !== null)) {
|
||||
if (typeof elvis !== "undefined" && elvis !== null) {
|
||||
alert("I knew it!");
|
||||
}
|
||||
cubes = (function() {
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
var _a, _b;
|
||||
(_b = (typeof (_a = (lottery.drawWinner())) === "undefined" || _a == undefined ? undefined : _a.address)) == undefined ? undefined : _b.zipcode;
|
||||
(typeof (_b = ((_a = lottery.drawWinner()))) === "undefined" || _b === null) ? undefined : _b.address == null ? undefined : _b.address.zipcode;
|
||||
@@ -1,7 +1,7 @@
|
||||
var mobyDick;
|
||||
mobyDick = "Call me Ishmael. Some years ago -- \
|
||||
never mind how long precisely -- having little \
|
||||
or no money in my purse, and nothing particular \
|
||||
to interest me on shore, I thought I would sail \
|
||||
about a little and see the watery part of the \
|
||||
world...";
|
||||
mobyDick = "Call me Ishmael. Some years ago --\
|
||||
never mind how long precisely -- having little\
|
||||
or no money in my purse, and nothing particular\
|
||||
to interest me on shore, I thought I would sail\
|
||||
about a little and see the watery part of the\
|
||||
world...";
|
||||
@@ -1,16 +1,23 @@
|
||||
if (day === "Mon") {
|
||||
goToWork();
|
||||
} else if (day === "Tue") {
|
||||
goToThePark();
|
||||
} else if (day === "Thu") {
|
||||
goIceFishing();
|
||||
} else if (day === "Fri" || day === "Sat") {
|
||||
switch (day) {
|
||||
case "Mon":
|
||||
go(work);
|
||||
break;
|
||||
case "Tue":
|
||||
go(relax);
|
||||
break;
|
||||
case "Thu":
|
||||
go(iceFishing);
|
||||
break;
|
||||
case "Fri":
|
||||
case "Sat":
|
||||
if (day === bingoDay) {
|
||||
goToBingo();
|
||||
goDancing();
|
||||
go(bingo);
|
||||
go(dancing);
|
||||
}
|
||||
} else if (day === "Sun") {
|
||||
goToChurch();
|
||||
} else {
|
||||
goToWork();
|
||||
break;
|
||||
case "Sun":
|
||||
go(church);
|
||||
break;
|
||||
default:
|
||||
go(work);
|
||||
}
|
||||
@@ -11,8 +11,8 @@ num = 6;
|
||||
lyrics = (function() {
|
||||
_a = [];
|
||||
while (num -= 1) {
|
||||
_a.push(num + " little monkeys, jumping on the bed. \
|
||||
One fell out and bumped his head.");
|
||||
_a.push(num + " little monkeys, jumping on the bed.\
|
||||
One fell out and bumped his head.");
|
||||
}
|
||||
return _a;
|
||||
})();
|
||||
@@ -71,7 +71,7 @@
|
||||
|
||||
|
||||
# Current version.
|
||||
_.VERSION = '1.0.2'
|
||||
_.VERSION = '1.1.0'
|
||||
|
||||
|
||||
# Collection Functions
|
||||
@@ -104,8 +104,10 @@
|
||||
|
||||
# **Reduce** builds up a single result from a list of values. Also known as
|
||||
# **inject**, or **foldl**. Uses JavaScript 1.8's version of **reduce**, if possible.
|
||||
_.reduce = (obj, memo, iterator, context) ->
|
||||
return obj.reduce(_.bind(iterator, context), memo) if nativeReduce and obj.reduce is nativeReduce
|
||||
_.reduce = (obj, iterator, memo, context) ->
|
||||
if nativeReduce and obj.reduce is nativeReduce
|
||||
iterator = _.bind iterator, context if context
|
||||
return obj.reduce iterator, memo
|
||||
_.each obj, (value, index, list) ->
|
||||
memo = iterator.call context, memo, value, index, list
|
||||
memo
|
||||
@@ -113,11 +115,12 @@
|
||||
|
||||
# The right-associative version of **reduce**, also known as **foldr**. Uses
|
||||
# JavaScript 1.8's version of **reduceRight**, if available.
|
||||
_.reduceRight = (obj, memo, iterator, context) ->
|
||||
return obj.reduceRight(_.bind(iterator, context), memo) if nativeReduceRight and obj.reduceRight is nativeReduceRight
|
||||
_.each _.clone(_.toArray(obj)).reverse(), (value, index) ->
|
||||
memo = iterator.call context, memo, value, index, obj
|
||||
memo
|
||||
_.reduceRight = (obj, iterator, memo, context) ->
|
||||
if nativeReduceRight and obj.reduceRight is nativeReduceRight
|
||||
iterator = _.bind iterator, context if context
|
||||
return obj.reduceRight iterator, memo
|
||||
reversed = _.clone(_.toArray(obj)).reverse()
|
||||
_.reduce reversed, iterator, memo, context
|
||||
|
||||
|
||||
# Return the first value which passes a truth test.
|
||||
@@ -273,10 +276,11 @@
|
||||
|
||||
# Return a completely flattened version of an array.
|
||||
_.flatten = (array) ->
|
||||
_.reduce array, [], (memo, value) ->
|
||||
_.reduce array, (memo, value) ->
|
||||
return memo.concat(_.flatten(value)) if _.isArray value
|
||||
memo.push value
|
||||
memo
|
||||
, []
|
||||
|
||||
|
||||
# Return a version of the array that does not contain the specified value(s).
|
||||
@@ -378,6 +382,16 @@
|
||||
setTimeout((-> func.apply(func, args)), wait)
|
||||
|
||||
|
||||
# Memoize an expensive function by storing its results.
|
||||
_.memoize = (func, hasher) ->
|
||||
memo = {}
|
||||
hasher or= _.identity
|
||||
->
|
||||
key = hasher.apply this, arguments
|
||||
return memo[key] if key of memo
|
||||
memo[key] = func.apply this, arguments
|
||||
|
||||
|
||||
# Defers a function, scheduling it to run after the current call stack has
|
||||
# cleared.
|
||||
_.defer = (func) ->
|
||||
@@ -457,7 +471,7 @@
|
||||
# Check dates' integer values.
|
||||
return a.getTime() is b.getTime() if _.isDate(a) and _.isDate(b)
|
||||
# Both are NaN?
|
||||
return true if _.isNaN(a) and _.isNaN(b)
|
||||
return false if _.isNaN(a) and _.isNaN(b)
|
||||
# Compare regular expressions.
|
||||
if _.isRegExp(a) and _.isRegExp(b)
|
||||
return a.source is b.source and
|
||||
@@ -473,13 +487,13 @@
|
||||
# Different object sizes?
|
||||
return false if aKeys.length isnt bKeys.length
|
||||
# Recursive comparison of contents.
|
||||
(return false) for key, val of a when !(key in b) or !_.isEqual(val, b[key])
|
||||
(return false) for all key, val of a when !(key of b) or !_.isEqual(val, b[key])
|
||||
true
|
||||
|
||||
|
||||
# Is a given array or object empty?
|
||||
_.isEmpty = (obj) ->
|
||||
return obj.length is 0 if _.isArray obj
|
||||
return obj.length is 0 if _.isArray(obj) or _.isString(obj)
|
||||
(return false) for key of obj when hasOwnProperty.call(obj, key)
|
||||
true
|
||||
|
||||
@@ -582,16 +596,19 @@
|
||||
# JavaScript templating a-la **ERB**, pilfered from John Resig's
|
||||
# *Secrets of the JavaScript Ninja*, page 83.
|
||||
# Single-quote fix from Rick Strahl.
|
||||
# With alterations for arbitrary delimiters, and to preserve whitespace.
|
||||
_.template = (str, data) ->
|
||||
c = _.templateSettings
|
||||
endMatch = new RegExp("'(?=[^"+c.end.substr(0, 1)+"]*"+escapeRegExp(c.end)+")","g")
|
||||
fn = new Function 'obj',
|
||||
'var p=[],print=function(){p.push.apply(p,arguments);};' +
|
||||
'with(obj){p.push(\'' +
|
||||
str.replace(/[\r\t\n]/g, " ")
|
||||
.replace(endMatch,"\t")
|
||||
'with(obj||{}){p.push(\'' +
|
||||
str.replace(/\r/g, '\\r')
|
||||
.replace(/\n/g, '\\n')
|
||||
.replace(/\t/g, '\\t')
|
||||
.replace(endMatch,"✄")
|
||||
.split("'").join("\\'")
|
||||
.split("\t").join("'")
|
||||
.split("✄").join("'")
|
||||
.replace(c.interpolate, "',$1,'")
|
||||
.split(c.start).join("');")
|
||||
.split(c.end).join("p.push('") +
|
||||
@@ -602,15 +619,16 @@
|
||||
# Aliases
|
||||
# -------
|
||||
|
||||
_.forEach = _.each
|
||||
_.foldl = _.inject = _.reduce
|
||||
_.foldr = _.reduceRight
|
||||
_.select = _.filter
|
||||
_.all = _.every
|
||||
_.any = _.some
|
||||
_.head = _.first
|
||||
_.tail = _.rest
|
||||
_.methods = _.functions
|
||||
_.forEach = _.each
|
||||
_.foldl = _.inject = _.reduce
|
||||
_.foldr = _.reduceRight
|
||||
_.select = _.filter
|
||||
_.all = _.every
|
||||
_.any = _.some
|
||||
_.contains = _.include
|
||||
_.head = _.first
|
||||
_.tail = _.rest
|
||||
_.methods = _.functions
|
||||
|
||||
|
||||
# Setup the OOP Wrapper
|
||||
|
||||
File diff suppressed because one or more lines are too long
44
extras/jsl.conf
Normal file
44
extras/jsl.conf
Normal file
@@ -0,0 +1,44 @@
|
||||
# JavaScriptLint configuration file for CoffeeScript.
|
||||
|
||||
+no_return_value # function {0} does not always return a value
|
||||
+duplicate_formal # duplicate formal argument {0}
|
||||
-equal_as_assign # test for equality (==) mistyped as assignment (=)?{0}
|
||||
+var_hides_arg # variable {0} hides argument
|
||||
+redeclared_var # redeclaration of {0} {1}
|
||||
+anon_no_return_value # anonymous function does not always return a value
|
||||
+missing_semicolon # missing semicolon
|
||||
+meaningless_block # meaningless block; curly braces have no impact
|
||||
+comma_separated_stmts # multiple statements separated by commas (use semicolons?)
|
||||
+unreachable_code # unreachable code
|
||||
+missing_break # missing break statement
|
||||
+missing_break_for_last_case # missing break statement for last case in switch
|
||||
-comparison_type_conv # comparisons against null, 0, true, false, or an empty string allowing implicit type conversion (use === or !==)
|
||||
+inc_dec_within_stmt # increment (++) and decrement (--) operators used as part of greater statement
|
||||
+useless_void # use of the void type may be unnecessary (void is always undefined)
|
||||
+multiple_plus_minus # unknown order of operations for successive plus (e.g. x+++y) or minus (e.g. x---y) signs
|
||||
+use_of_label # use of label
|
||||
-block_without_braces # block statement without curly braces
|
||||
+leading_decimal_point # leading decimal point may indicate a number or an object member
|
||||
+trailing_decimal_point # trailing decimal point may indicate a number or an object member
|
||||
+octal_number # leading zeros make an octal number
|
||||
+nested_comment # nested comment
|
||||
+misplaced_regex # regular expressions should be preceded by a left parenthesis, assignment, colon, or comma
|
||||
+ambiguous_newline # unexpected end of line; it is ambiguous whether these lines are part of the same statement
|
||||
+empty_statement # empty statement or extra semicolon
|
||||
-missing_option_explicit # the "option explicit" control comment is missing
|
||||
+partial_option_explicit # the "option explicit" control comment, if used, must be in the first script tag
|
||||
+dup_option_explicit # duplicate "option explicit" control comment
|
||||
+useless_assign # useless assignment
|
||||
+ambiguous_nested_stmt # block statements containing block statements should use curly braces to resolve ambiguity
|
||||
+ambiguous_else_stmt # the else statement could be matched with one of multiple if statements (use curly braces to indicate intent)
|
||||
+missing_default_case # missing default case in switch statement
|
||||
+duplicate_case_in_switch # duplicate case in switch statements
|
||||
+default_not_at_end # the default case is not at the end of the switch statement
|
||||
+legacy_cc_not_understood # couldn't understand control comment using /*@keyword@*/ syntax
|
||||
+jsl_cc_not_understood # couldn't understand control comment using /*jsl:keyword*/ syntax
|
||||
+useless_comparison # useless comparison; comparing identical expressions
|
||||
+with_statement # with statement hides undeclared variables; use temporary variable instead
|
||||
+trailing_comma_in_array # extra comma is not recommended in array initializers
|
||||
+assign_to_function_call # assignment to a function call
|
||||
+parseint_missing_radix # parseInt missing radix parameter
|
||||
+lambda_assign_requires_semicolon
|
||||
196
index.html
196
index.html
@@ -40,7 +40,7 @@
|
||||
<a href="#pattern_matching">Pattern Matching</a>
|
||||
<a href="#fat_arrow">Function Binding</a>
|
||||
<a href="#embedded">Embedded JavaScript</a>
|
||||
<a href="#switch">Switch/When/Else</a>
|
||||
<a href="#switch">The Switch Statement</a>
|
||||
<a href="#try">Try/Catch/Finally</a>
|
||||
<a href="#comparisons">Chained Comparisons</a>
|
||||
<a href="#interpolation">String and RegExp Interpolation</a>
|
||||
@@ -114,7 +114,7 @@ alert reverse '.eeffoC yrT'</textarea></div>
|
||||
|
||||
<p>
|
||||
<b>Latest Version:</b>
|
||||
<a href="http://github.com/jashkenas/coffee-script/tarball/0.9.1">0.9.1</a>
|
||||
<a href="http://github.com/jashkenas/coffee-script/tarball/0.9.3">0.9.3</a>
|
||||
</p>
|
||||
|
||||
<h2>
|
||||
@@ -175,7 +175,7 @@ math <span class="Keyword">=</span> {
|
||||
runners <span class="Keyword">=</span> __slice.<span class="LibraryFunction">call</span>(arguments, <span class="Number">1</span>);
|
||||
<span class="Keyword">return</span> <span class="LibraryFunction">print</span>(winner, runners);
|
||||
};
|
||||
<span class="Keyword">if</span> ((<span class="Keyword">typeof</span> elvis <span class="Keyword">!</span><span class="Keyword">==</span> <span class="String"><span class="String">"</span>undefined<span class="String">"</span></span> <span class="Keyword">&</span><span class="Keyword">&</span> elvis <span class="Keyword">!</span><span class="Keyword">==</span> <span class="BuiltInConstant">null</span>)) {
|
||||
<span class="Keyword">if</span> (<span class="Keyword">typeof</span> elvis <span class="Keyword">!</span><span class="Keyword">==</span> <span class="String"><span class="String">"</span>undefined<span class="String">"</span></span> <span class="Keyword">&</span><span class="Keyword">&</span> elvis <span class="Keyword">!</span><span class="Keyword">==</span> <span class="BuiltInConstant">null</span>) {
|
||||
<span class="LibraryFunction">alert</span>(<span class="String"><span class="String">"</span>I knew it!<span class="String">"</span></span>);
|
||||
}
|
||||
cubes <span class="Keyword">=</span> (<span class="Storage">function</span>() {
|
||||
@@ -209,7 +209,7 @@ race = function(winner) {
|
||||
runners = __slice.call(arguments, 1);
|
||||
return print(winner, runners);
|
||||
};
|
||||
if ((typeof elvis !== "undefined" && elvis !== null)) {
|
||||
if (typeof elvis !== "undefined" && elvis !== null) {
|
||||
alert("I knew it!");
|
||||
}
|
||||
cubes = (function() {
|
||||
@@ -254,7 +254,7 @@ cubes = (function() {
|
||||
Then clone the CoffeeScript
|
||||
<a href="http://github.com/jashkenas/coffee-script">source repository</a>
|
||||
from GitHub, or download the latest
|
||||
release: <a href="http://github.com/jashkenas/coffee-script/tarball/0.9.1">0.9.1</a>.
|
||||
release: <a href="http://github.com/jashkenas/coffee-script/tarball/0.9.3">0.9.3</a>.
|
||||
To install the CoffeeScript compiler system-wide
|
||||
under <tt>/usr/local</tt>, open the directory and run:
|
||||
</p>
|
||||
@@ -612,7 +612,7 @@ inner = changeNumbers();;alert(inner);'>run: inner</button><br class='clear' /><
|
||||
|
||||
date <span class="Keyword">=</span> <span class="Keyword">if</span> friday <span class="Keyword">then</span> sue <span class="Keyword">else</span> jill
|
||||
|
||||
options or<span class="Keyword">=</span> defaults
|
||||
options <span class="Keyword">or=</span> defaults
|
||||
</pre><pre class="idle"><span class="Storage">var</span> date, mood, options;
|
||||
<span class="Keyword">if</span> (singing) {
|
||||
mood <span class="Keyword">=</span> greatlyImproved;
|
||||
@@ -624,7 +624,7 @@ options or<span class="Keyword">=</span> defaults
|
||||
showIt();
|
||||
}
|
||||
date <span class="Keyword">=</span> friday ? sue : jill;
|
||||
options <span class="Keyword">=</span> options <span class="Keyword">||</span> defaults;
|
||||
options <span class="Keyword">||</span> (options <span class="Keyword">=</span> defaults);
|
||||
</pre><br class='clear' /></div>
|
||||
|
||||
<p>
|
||||
@@ -681,12 +681,12 @@ print inspect <span class="String"><span class="String">"</span>My name is
|
||||
<span class="Keyword">if</span> (band <span class="Keyword">!</span><span class="Keyword">==</span> SpinalTap) {
|
||||
volume <span class="Keyword">=</span> <span class="Number">10</span>;
|
||||
}
|
||||
<span class="Keyword">if</span> (<span class="Keyword">!</span>(answer <span class="Keyword">===</span> <span class="BuiltInConstant">false</span>)) {
|
||||
<span class="Keyword">if</span> (answer <span class="Keyword">!</span><span class="Keyword">==</span> <span class="BuiltInConstant">false</span>) {
|
||||
letTheWildRumpusBegin();
|
||||
}
|
||||
<span class="Keyword">if</span> (car.speed <span class="Keyword"><</span> limit) {
|
||||
accelerate();
|
||||
};
|
||||
}
|
||||
<span class="Keyword">if</span> ((<span class="Number">47</span> <span class="Keyword">===</span> pick <span class="Keyword">||</span> <span class="Number">92</span> <span class="Keyword">===</span> pick <span class="Keyword">||</span> <span class="Number">13</span> <span class="Keyword">===</span> pick)) {
|
||||
winner <span class="Keyword">=</span> <span class="BuiltInConstant">true</span>;
|
||||
}
|
||||
@@ -788,8 +788,8 @@ num <span class="Keyword">=</span> <span class="Number">6</span>;
|
||||
lyrics <span class="Keyword">=</span> (<span class="Storage">function</span>() {
|
||||
_a <span class="Keyword">=</span> [];
|
||||
<span class="Keyword">while</span> (num <span class="Keyword">-</span><span class="Keyword">=</span> <span class="Number">1</span>) {
|
||||
_a.<span class="LibraryFunction">push</span>(num <span class="Keyword">+</span> <span class="String"><span class="String">"</span> little monkeys, jumping on the bed. \</span>
|
||||
<span class="String">One fell out and bumped his head.<span class="String">"</span></span>);
|
||||
_a.<span class="LibraryFunction">push</span>(num <span class="Keyword">+</span> <span class="String"><span class="String">"</span> little monkeys, jumping on the bed.\</span>
|
||||
<span class="String"> One fell out and bumped his head.<span class="String">"</span></span>);
|
||||
}
|
||||
<span class="Keyword">return</span> _a;
|
||||
})();
|
||||
@@ -806,8 +806,8 @@ num = 6;
|
||||
lyrics = (function() {
|
||||
_a = [];
|
||||
while (num -= 1) {
|
||||
_a.push(num + " little monkeys, jumping on the bed. \
|
||||
One fell out and bumped his head.");
|
||||
_a.push(num + " little monkeys, jumping on the bed.\
|
||||
One fell out and bumped his head.");
|
||||
}
|
||||
return _a;
|
||||
})();;alert(lyrics.join("\n"));'>run: lyrics.join("\n")</button><br class='clear' /></div>
|
||||
@@ -1156,7 +1156,7 @@ speed = (typeof speed !== "undefined" && speed !== null) ? speed : 140;;alert(sp
|
||||
</p>
|
||||
<div class='code'><pre class="idle">lottery.drawWinner()<span class="Keyword">?</span>.address<span class="Keyword">?</span>.zipcode
|
||||
</pre><pre class="idle"><span class="Storage">var</span> _a, _b;
|
||||
(_b <span class="Keyword">=</span> (<span class="Keyword">typeof</span> (_a <span class="Keyword">=</span> (lottery.drawWinner())) <span class="Keyword">===</span> <span class="String"><span class="String">"</span>undefined<span class="String">"</span></span> <span class="Keyword">||</span> _a <span class="Keyword">==</span> undefined ? undefined : _a.address)) <span class="Keyword">==</span> undefined ? undefined : _b.zipcode;
|
||||
(<span class="Keyword">typeof</span> (_b <span class="Keyword">=</span> ((_a <span class="Keyword">=</span> lottery.drawWinner()))) <span class="Keyword">===</span> <span class="String"><span class="String">"</span>undefined<span class="String">"</span></span> <span class="Keyword">||</span> _b <span class="Keyword">===</span> <span class="BuiltInConstant">null</span>) ? undefined : _b.address <span class="Keyword">==</span> <span class="BuiltInConstant">null</span> ? undefined : _b.address.zipcode;
|
||||
</pre><br class='clear' /></div>
|
||||
<p>
|
||||
Soaking up nulls is similar to Ruby's
|
||||
@@ -1186,18 +1186,18 @@ speed = (typeof speed !== "undefined" && speed !== null) ? speed : 140;;alert(sp
|
||||
set the superclass, assign prototypal properties, and define the constructor,
|
||||
in a single assignable expression.
|
||||
</p>
|
||||
<div class='code'><pre class="idle"><span class="Storage">class</span><span class="TypeName"> Animal</span>
|
||||
<div class='code'><pre class="idle"><span class="Storage">class</span> <span class="TypeName">Animal</span>
|
||||
<span class="FunctionName">constructor</span><span class="Keyword">:</span> (<span class="Variable">@name</span>) <span class="Storage">-></span>
|
||||
|
||||
<span class="FunctionName">move</span><span class="Keyword">:</span> <span class="FunctionArgument">(</span><span class="FunctionArgument">meters</span><span class="FunctionArgument">)</span> <span class="Storage">-></span>
|
||||
alert <span class="Variable">@name</span> <span class="Keyword">+</span> <span class="String"><span class="String">"</span> moved <span class="String">"</span></span> <span class="Keyword">+</span> meters <span class="Keyword">+</span> <span class="String"><span class="String">"</span>m.<span class="String">"</span></span>
|
||||
|
||||
<span class="Storage">class</span><span class="TypeName"> Snake</span><span class="InheritedClass"> <span class="Keyword">extends</span> Animal</span>
|
||||
<span class="Storage">class</span> <span class="TypeName">Snake</span><span class="InheritedClass"> <span class="Keyword">extends</span> Animal</span>
|
||||
<span class="FunctionName">move</span><span class="Keyword">:</span> <span class="Storage">-></span>
|
||||
alert <span class="String"><span class="String">"</span>Slithering...<span class="String">"</span></span>
|
||||
<span class="Variable">super</span> <span class="Number">5</span>
|
||||
|
||||
<span class="Storage">class</span><span class="TypeName"> Horse</span><span class="InheritedClass"> <span class="Keyword">extends</span> Animal</span>
|
||||
<span class="Storage">class</span> <span class="TypeName">Horse</span><span class="InheritedClass"> <span class="Keyword">extends</span> Animal</span>
|
||||
<span class="FunctionName">move</span><span class="Keyword">:</span> <span class="Storage">-></span>
|
||||
alert <span class="String"><span class="String">"</span>Galloping...<span class="String">"</span></span>
|
||||
<span class="Variable">super</span> <span class="Number">45</span>
|
||||
@@ -1218,7 +1218,7 @@ tom.move()
|
||||
<span class="LibraryClassType">child</span>.<span class="LibraryConstant">prototype</span> = <span class="Keyword">new</span> <span class="TypeName">ctor</span>();
|
||||
<span class="LibraryClassType">child</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">constructor</span> = child;
|
||||
<span class="Keyword">if</span> (<span class="Keyword">typeof</span> parent.extended <span class="Keyword">===</span> <span class="String"><span class="String">"</span>function<span class="String">"</span></span>) parent.extended(child);
|
||||
child.__superClass__ <span class="Keyword">=</span> parent.<span class="LibraryConstant">prototype</span>;
|
||||
child.__super__ <span class="Keyword">=</span> parent.<span class="LibraryConstant">prototype</span>;
|
||||
};
|
||||
<span class="FunctionName">Animal</span> = <span class="Storage">function</span>(<span class="FunctionArgument">_a</span>) {
|
||||
<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">=</span> _a;
|
||||
@@ -1233,7 +1233,7 @@ tom.move()
|
||||
__extends(Snake, Animal);
|
||||
<span class="LibraryClassType">Snake</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">move</span> = <span class="Storage">function</span>() {
|
||||
<span class="LibraryFunction">alert</span>(<span class="String"><span class="String">"</span>Slithering...<span class="String">"</span></span>);
|
||||
<span class="Keyword">return</span> Snake.__superClass__.move.<span class="LibraryFunction">call</span>(<span class="Variable">this</span>, <span class="Number">5</span>);
|
||||
<span class="Keyword">return</span> Snake.__super__.move.<span class="LibraryFunction">call</span>(<span class="Variable">this</span>, <span class="Number">5</span>);
|
||||
};
|
||||
<span class="FunctionName">Horse</span> = <span class="Storage">function</span>() {
|
||||
<span class="Keyword">return</span> Animal.<span class="LibraryFunction">apply</span>(<span class="Variable">this</span>, arguments);
|
||||
@@ -1241,7 +1241,7 @@ __extends(Snake, Animal);
|
||||
__extends(Horse, Animal);
|
||||
<span class="LibraryClassType">Horse</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">move</span> = <span class="Storage">function</span>() {
|
||||
<span class="LibraryFunction">alert</span>(<span class="String"><span class="String">"</span>Galloping...<span class="String">"</span></span>);
|
||||
<span class="Keyword">return</span> Horse.__superClass__.move.<span class="LibraryFunction">call</span>(<span class="Variable">this</span>, <span class="Number">45</span>);
|
||||
<span class="Keyword">return</span> Horse.__super__.move.<span class="LibraryFunction">call</span>(<span class="Variable">this</span>, <span class="Number">45</span>);
|
||||
};
|
||||
sam <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">Snake</span>(<span class="String"><span class="String">"</span>Sammy the Python<span class="String">"</span></span>);
|
||||
tom <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">Horse</span>(<span class="String"><span class="String">"</span>Tommy the Palomino<span class="String">"</span></span>);
|
||||
@@ -1254,7 +1254,7 @@ var __extends = function(child, parent) {
|
||||
child.prototype = new ctor();
|
||||
child.prototype.constructor = child;
|
||||
if (typeof parent.extended === "function") parent.extended(child);
|
||||
child.__superClass__ = parent.prototype;
|
||||
child.__super__ = parent.prototype;
|
||||
};
|
||||
Animal = function(_a) {
|
||||
this.name = _a;
|
||||
@@ -1269,7 +1269,7 @@ Snake = function() {
|
||||
__extends(Snake, Animal);
|
||||
Snake.prototype.move = function() {
|
||||
alert("Slithering...");
|
||||
return Snake.__superClass__.move.call(this, 5);
|
||||
return Snake.__super__.move.call(this, 5);
|
||||
};
|
||||
Horse = function() {
|
||||
return Animal.apply(this, arguments);
|
||||
@@ -1277,7 +1277,7 @@ Horse = function() {
|
||||
__extends(Horse, Animal);
|
||||
Horse.prototype.move = function() {
|
||||
alert("Galloping...");
|
||||
return Horse.__superClass__.move.call(this, 45);
|
||||
return Horse.__super__.move.call(this, 45);
|
||||
};
|
||||
sam = new Snake("Sammy the Python");
|
||||
tom = new Horse("Tommy the Palomino");
|
||||
@@ -1491,12 +1491,11 @@ hi = function() {
|
||||
<p>
|
||||
<span id="switch" class="bookmark"></span>
|
||||
<b class="header">Switch/When/Else</b>
|
||||
<b>Switch</b> statements in JavaScript are rather broken. You can only
|
||||
do comparisons based on string equality, and need to remember to <b>break</b> at the end of
|
||||
every <b>case</b> statement to avoid accidentally falling through to
|
||||
the default case. CoffeeScript compiles <b>switch</b> statements into JavaScript if-else chains, allowing you to
|
||||
compare any object (via <b>===</b>), preventing fall-through, and resulting
|
||||
in a returnable, assignable expression. The format is: <tt>switch</tt> condition,
|
||||
<b>Switch</b> statements in JavaScript are a bit awkward. You need to
|
||||
remember to <b>break</b> at the end of every <b>case</b> statement to
|
||||
avoid accidentally falling through to the default case.
|
||||
CoffeeScript prevents accidental fall-through, and can convert the <tt>switch</tt>
|
||||
into a returnable, assignable expression. The format is: <tt>switch</tt> condition,
|
||||
<tt>when</tt> clauses, <tt>else</tt> the default case.
|
||||
</p>
|
||||
<p>
|
||||
@@ -1505,30 +1504,37 @@ hi = function() {
|
||||
runs.
|
||||
</p>
|
||||
<div class='code'><pre class="idle"><span class="Keyword">switch</span> day
|
||||
<span class="Keyword">when</span> <span class="String"><span class="String">"</span>Mon<span class="String">"</span></span> <span class="Keyword">then</span> goToWork()
|
||||
<span class="Keyword">when</span> <span class="String"><span class="String">"</span>Tue<span class="String">"</span></span> <span class="Keyword">then</span> goToThePark()
|
||||
<span class="Keyword">when</span> <span class="String"><span class="String">"</span>Thu<span class="String">"</span></span> <span class="Keyword">then</span> goIceFishing()
|
||||
<span class="Keyword">when</span> <span class="String"><span class="String">"</span>Mon<span class="String">"</span></span> <span class="Keyword">then</span> go work
|
||||
<span class="Keyword">when</span> <span class="String"><span class="String">"</span>Tue<span class="String">"</span></span> <span class="Keyword">then</span> go relax
|
||||
<span class="Keyword">when</span> <span class="String"><span class="String">"</span>Thu<span class="String">"</span></span> <span class="Keyword">then</span> go iceFishing
|
||||
<span class="Keyword">when</span> <span class="String"><span class="String">"</span>Fri<span class="String">"</span></span>, <span class="String"><span class="String">"</span>Sat<span class="String">"</span></span>
|
||||
<span class="Keyword">if</span> day <span class="Keyword">is</span> bingoDay
|
||||
goToBingo()
|
||||
goDancing()
|
||||
<span class="Keyword">when</span> <span class="String"><span class="String">"</span>Sun<span class="String">"</span></span> <span class="Keyword">then</span> goToChurch()
|
||||
<span class="Keyword">else</span> goToWork()
|
||||
</pre><pre class="idle"><span class="Keyword">if</span> (day <span class="Keyword">===</span> <span class="String"><span class="String">"</span>Mon<span class="String">"</span></span>) {
|
||||
goToWork();
|
||||
} <span class="Keyword">else</span> <span class="Keyword">if</span> (day <span class="Keyword">===</span> <span class="String"><span class="String">"</span>Tue<span class="String">"</span></span>) {
|
||||
goToThePark();
|
||||
} <span class="Keyword">else</span> <span class="Keyword">if</span> (day <span class="Keyword">===</span> <span class="String"><span class="String">"</span>Thu<span class="String">"</span></span>) {
|
||||
goIceFishing();
|
||||
} <span class="Keyword">else</span> <span class="Keyword">if</span> (day <span class="Keyword">===</span> <span class="String"><span class="String">"</span>Fri<span class="String">"</span></span> <span class="Keyword">||</span> day <span class="Keyword">===</span> <span class="String"><span class="String">"</span>Sat<span class="String">"</span></span>) {
|
||||
go bingo
|
||||
go dancing
|
||||
<span class="Keyword">when</span> <span class="String"><span class="String">"</span>Sun<span class="String">"</span></span> <span class="Keyword">then</span> go church
|
||||
<span class="Keyword">else</span> go work
|
||||
</pre><pre class="idle"><span class="Keyword">switch</span> (day) {
|
||||
<span class="Keyword">case</span> <span class="String"><span class="String">"</span>Mon<span class="String">"</span></span>:
|
||||
<span class="LibraryFunction">go</span>(work);
|
||||
<span class="Keyword">break</span>;
|
||||
<span class="Keyword">case</span> <span class="String"><span class="String">"</span>Tue<span class="String">"</span></span>:
|
||||
<span class="LibraryFunction">go</span>(relax);
|
||||
<span class="Keyword">break</span>;
|
||||
<span class="Keyword">case</span> <span class="String"><span class="String">"</span>Thu<span class="String">"</span></span>:
|
||||
<span class="LibraryFunction">go</span>(iceFishing);
|
||||
<span class="Keyword">break</span>;
|
||||
<span class="Keyword">case</span> <span class="String"><span class="String">"</span>Fri<span class="String">"</span></span>:
|
||||
<span class="Keyword">case</span> <span class="String"><span class="String">"</span>Sat<span class="String">"</span></span>:
|
||||
<span class="Keyword">if</span> (day <span class="Keyword">===</span> bingoDay) {
|
||||
goToBingo();
|
||||
goDancing();
|
||||
<span class="LibraryFunction">go</span>(bingo);
|
||||
<span class="LibraryFunction">go</span>(dancing);
|
||||
}
|
||||
} <span class="Keyword">else</span> <span class="Keyword">if</span> (day <span class="Keyword">===</span> <span class="String"><span class="String">"</span>Sun<span class="String">"</span></span>) {
|
||||
goToChurch();
|
||||
} <span class="Keyword">else</span> {
|
||||
goToWork();
|
||||
<span class="Keyword">break</span>;
|
||||
<span class="Keyword">case</span> <span class="String"><span class="String">"</span>Sun<span class="String">"</span></span>:
|
||||
<span class="LibraryFunction">go</span>(church);
|
||||
<span class="Keyword">break</span>;
|
||||
<span class="Keyword">default</span>:
|
||||
<span class="LibraryFunction">go</span>(work);
|
||||
}
|
||||
</pre><br class='clear' /></div>
|
||||
|
||||
@@ -1578,9 +1584,8 @@ healthy = (200 > cholesterol) && (cholesterol > 60);;alert(healthy);'>run: healt
|
||||
<p>
|
||||
<span id="interpolation" class="bookmark"></span>
|
||||
<b class="header">String and RegExp Interpolation</b>
|
||||
A version of <a href="http://wiki.ecmascript.org/doku.php?id=strawman:string_interpolation">ECMAScript Harmony's proposed string interpolation</a>
|
||||
is included in CoffeeScript. Simple variables can be included by prefixing
|
||||
them with a hash mark.
|
||||
Ruby-style string interpolation is included in CoffeeScript. Double-quoted
|
||||
strings allow for interpolated values, while single-quoted strings are literal.
|
||||
</p>
|
||||
<div class='code'><pre class="idle">author <span class="Keyword">=</span> <span class="String"><span class="String">"</span>Wittgenstein<span class="String">"</span></span>
|
||||
quote <span class="Keyword">=</span> <span class="String"><span class="String">"</span>A picture is a fact. -- <span class="String"><span class="String">#{</span>author<span class="String">}</span></span><span class="String">"</span></span>
|
||||
@@ -1603,11 +1608,11 @@ dates <span class="Keyword">=</span><span class="String"> /\d+#{sep}\d+#{sep}\d+
|
||||
</pre><pre class="idle"><span class="Storage">var</span> dates, sentence, sep;
|
||||
sentence <span class="Keyword">=</span> (<span class="String"><span class="String">"</span><span class="String">"</span></span> <span class="Keyword">+</span> (<span class="Number">22</span> / <span class="Number">7</span>) <span class="Keyword">+</span> <span class="String"><span class="String">"</span> is a decent approximation of π<span class="String">"</span></span>);
|
||||
sep <span class="Keyword">=</span> <span class="String"><span class="String">"</span>[.<span class="UserDefinedConstant">\\</span>/<span class="UserDefinedConstant">\\</span>- ]<span class="String">"</span></span>;
|
||||
dates <span class="Keyword">=</span> (<span class="Keyword">new</span> <span class="TypeName">RegExp</span>((<span class="String"><span class="String">"</span><span class="UserDefinedConstant">\\</span>d+<span class="String">"</span></span> <span class="Keyword">+</span> (sep) <span class="Keyword">+</span> <span class="String"><span class="String">"</span><span class="UserDefinedConstant">\\</span>d+<span class="String">"</span></span> <span class="Keyword">+</span> (sep) <span class="Keyword">+</span> <span class="String"><span class="String">"</span><span class="UserDefinedConstant">\\</span>d+<span class="String">"</span></span>), <span class="String"><span class="String">"</span>g<span class="String">"</span></span>));
|
||||
dates <span class="Keyword">=</span> (<span class="Keyword">new</span> <span class="TypeName">RegExp</span>(<span class="String"><span class="String">"</span><span class="UserDefinedConstant">\\</span>d+<span class="String">"</span></span> <span class="Keyword">+</span> (sep) <span class="Keyword">+</span> <span class="String"><span class="String">"</span><span class="UserDefinedConstant">\\</span>d+<span class="String">"</span></span> <span class="Keyword">+</span> (sep) <span class="Keyword">+</span> <span class="String"><span class="String">"</span><span class="UserDefinedConstant">\\</span>d+<span class="String">"</span></span>, <span class="String"><span class="String">"</span>g<span class="String">"</span></span>));
|
||||
</pre><button onclick='javascript: var dates, sentence, sep;
|
||||
sentence = ("" + (22 / 7) + " is a decent approximation of π");
|
||||
sep = "[.\\/\\- ]";
|
||||
dates = (new RegExp(("\\d+" + (sep) + "\\d+" + (sep) + "\\d+"), "g"));;alert(sentence);'>run: sentence</button><br class='clear' /></div>
|
||||
dates = (new RegExp("\\d+" + (sep) + "\\d+" + (sep) + "\\d+", "g"));;alert(sentence);'>run: sentence</button><br class='clear' /></div>
|
||||
|
||||
<p>
|
||||
<span id="heredocs" class="bookmark"></span>
|
||||
@@ -1615,27 +1620,27 @@ dates = (new RegExp(("\\d+" + (sep) + "\\d+" + (sep) + "\\d+"), "g"));;alert(sen
|
||||
Multiline strings are allowed in CoffeeScript.
|
||||
</p>
|
||||
<div class='code'><pre class="idle">mobyDick <span class="Keyword">=</span> <span class="String"><span class="String">"</span>Call me Ishmael. Some years ago --</span>
|
||||
<span class="String">never mind how long precisely -- having little</span>
|
||||
<span class="String">or no money in my purse, and nothing particular</span>
|
||||
<span class="String">to interest me on shore, I thought I would sail</span>
|
||||
<span class="String">about a little and see the watery part of the</span>
|
||||
<span class="String">world...<span class="String">"</span></span>
|
||||
<span class="String"> never mind how long precisely -- having little</span>
|
||||
<span class="String"> or no money in my purse, and nothing particular</span>
|
||||
<span class="String"> to interest me on shore, I thought I would sail</span>
|
||||
<span class="String"> about a little and see the watery part of the</span>
|
||||
<span class="String"> world...<span class="String">"</span></span>
|
||||
|
||||
|
||||
</pre><pre class="idle"><span class="Storage">var</span> mobyDick;
|
||||
mobyDick <span class="Keyword">=</span> <span class="String"><span class="String">"</span>Call me Ishmael. Some years ago -- \</span>
|
||||
<span class="String">never mind how long precisely -- having little \</span>
|
||||
<span class="String">or no money in my purse, and nothing particular \</span>
|
||||
<span class="String">to interest me on shore, I thought I would sail \</span>
|
||||
<span class="String">about a little and see the watery part of the \</span>
|
||||
<span class="String">world...<span class="String">"</span></span>;
|
||||
mobyDick <span class="Keyword">=</span> <span class="String"><span class="String">"</span>Call me Ishmael. Some years ago --\</span>
|
||||
<span class="String"> never mind how long precisely -- having little\</span>
|
||||
<span class="String"> or no money in my purse, and nothing particular\</span>
|
||||
<span class="String"> to interest me on shore, I thought I would sail\</span>
|
||||
<span class="String"> about a little and see the watery part of the\</span>
|
||||
<span class="String"> world...<span class="String">"</span></span>;
|
||||
</pre><button onclick='javascript: var mobyDick;
|
||||
mobyDick = "Call me Ishmael. Some years ago -- \
|
||||
never mind how long precisely -- having little \
|
||||
or no money in my purse, and nothing particular \
|
||||
to interest me on shore, I thought I would sail \
|
||||
about a little and see the watery part of the \
|
||||
world...";;alert(mobyDick);'>run: mobyDick</button><br class='clear' /></div>
|
||||
mobyDick = "Call me Ishmael. Some years ago --\
|
||||
never mind how long precisely -- having little\
|
||||
or no money in my purse, and nothing particular\
|
||||
to interest me on shore, I thought I would sail\
|
||||
about a little and see the watery part of the\
|
||||
world...";;alert(mobyDick);'>run: mobyDick</button><br class='clear' /></div>
|
||||
<p>
|
||||
Heredocs can be used to hold formatted or indentation-sensitive text
|
||||
(or, if you just don't feel like escaping quotes and apostrophes). The
|
||||
@@ -1660,11 +1665,10 @@ html <span class="Keyword">=</span> <span class="String"><span class="String">'<
|
||||
are preserved in the generated code.
|
||||
</p>
|
||||
<div class='code'><pre class="idle"><span class="Comment"><span class="Comment">###</span></span>
|
||||
<span class="Comment">CoffeeScript Compiler v0.9.1</span>
|
||||
<span class="Comment">CoffeeScript Compiler v0.9.3</span>
|
||||
<span class="Comment">Released under the MIT License</span>
|
||||
<span class="Comment"><span class="Comment">###</span></span>
|
||||
</pre><pre class="idle"><span class="Comment"><span class="Comment">/*</span></span>
|
||||
<span class="Comment">CoffeeScript Compiler v0.9.1</span>
|
||||
<span class="Comment">###</span>
|
||||
</pre><pre class="idle"><span class="Comment"><span class="Comment">/*</span>CoffeeScript Compiler v0.9.3</span>
|
||||
<span class="Comment">Released under the MIT License</span>
|
||||
<span class="Comment"><span class="Comment">*/</span></span>
|
||||
</pre><br class='clear' /></div>
|
||||
@@ -1709,7 +1713,7 @@ task(<span class="String"><span class="String">'</span>build:parser<span class="
|
||||
require(<span class="String"><span class="String">'</span>jison<span class="String">'</span></span>);
|
||||
code <span class="Keyword">=</span> require(<span class="String"><span class="String">'</span>./lib/grammar<span class="String">'</span></span>).parser.generate();
|
||||
dir <span class="Keyword">=</span> options.output <span class="Keyword">||</span> <span class="String"><span class="String">'</span>lib<span class="String">'</span></span>;
|
||||
<span class="Keyword">return</span> fs.writeFile((<span class="String"><span class="String">"</span><span class="String">"</span></span> <span class="Keyword">+</span> (dir) <span class="Keyword">+</span> <span class="String"><span class="String">"</span>/parser.js<span class="String">"</span></span>), code);
|
||||
<span class="Keyword">return</span> fs.writeFile(<span class="String"><span class="String">"</span><span class="String">"</span></span> <span class="Keyword">+</span> (dir) <span class="Keyword">+</span> <span class="String"><span class="String">"</span>/parser.js<span class="String">"</span></span>, code);
|
||||
});
|
||||
</pre><br class='clear' /></div>
|
||||
|
||||
@@ -1808,7 +1812,7 @@ task(<span class="String"><span class="String">'</span>build:parser<span class="
|
||||
</li>
|
||||
<li>
|
||||
<b>dsc</b>'s <a href="http://github.com/dsc/coffeecup">CoffeeCup</a>
|
||||
— A Python WSGI middleware that compiles CoffeeScript to JavaScript
|
||||
— a Python WSGI middleware that compiles CoffeeScript to JavaScript
|
||||
on-demand during development.
|
||||
</li>
|
||||
<li>
|
||||
@@ -1821,9 +1825,14 @@ task(<span class="String"><span class="String">'</span>build:parser<span class="
|
||||
— a custom filter for rendering CoffeeScript inline within
|
||||
<a href="http://haml-lang.com/">HAML</a> templates.
|
||||
</li>
|
||||
<li>
|
||||
<b>chrislloyd</b>'s <a href="http://github.com/chrislloyd/roast">Roast</a>
|
||||
— a CoffeeScript compiler plug-in that allows you to include external
|
||||
source files.
|
||||
</li>
|
||||
<li>
|
||||
<b>jashkenas</b>'s <a href="http://jashkenas.github.com/docco/">Docco</a>
|
||||
— A quick-and-dirty literate-programming-style documentation generator
|
||||
— a quick-and-dirty literate-programming-style documentation generator
|
||||
for CoffeeScript. Used to produce the annotated source.
|
||||
</li>
|
||||
</ul>
|
||||
@@ -1848,10 +1857,33 @@ task(<span class="String"><span class="String">'</span>build:parser<span class="
|
||||
Change Log
|
||||
</h2>
|
||||
|
||||
<p>
|
||||
<b class="header" style="margin-top: 20px;">0.9.3</b>
|
||||
CoffeeScript <tt>switch</tt> statements now compile into JS <tt>switch</tt>
|
||||
statements — they previously compiled into <tt>if/else</tt> chains
|
||||
for JavaScript 1.3 compatibility.
|
||||
Soaking a function invocation is now supported. Users of the RubyMine
|
||||
editor should now be able to use <tt>--watch</tt> mode.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b class="header" style="margin-top: 20px;">0.9.2</b>
|
||||
Specifying the start and end of a range literal is now optional, eg. <tt>array[3..]</tt>.
|
||||
You can now say <tt>a not instanceof b</tt>.
|
||||
Fixed important bugs with nested significant and non-significant indentation (Issue #637).
|
||||
Added a <tt>--require</tt> flag that allows you to hook into the <tt>coffee</tt> command.
|
||||
Added a custom <tt>jsl.conf</tt> file for our preferred JavaScriptLint setup.
|
||||
Sped up Jison grammar compilation time by flattening rules for operations.
|
||||
Block comments can now be used with JavaScript-minifier-friendly syntax.
|
||||
Added JavaScript's compound assignment bitwise operators. Bugfixes to
|
||||
implicit object literals with leading number and string keys, as the subject
|
||||
of implicit calls, and as part of compound assignment.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b class="header" style="margin-top: 20px;">0.9.1</b>
|
||||
Bugfix release for <b>0.9.1</b>. Greatly improves the handling of mixed
|
||||
implicit objects, implicit function calls, and implicit indentation.
|
||||
implicit objects, implicit function calls, and implicit indentation.
|
||||
String and regex interpolation is now strictly <tt>#{ ... }</tt> (Ruby style).
|
||||
The compiler now takes a <tt>--require</tt> flag, which specifies scripts
|
||||
to run before compilation.
|
||||
@@ -1865,7 +1897,7 @@ task(<span class="String"><span class="String">'</span>build:parser<span class="
|
||||
literals use <tt>:</tt>, as in JavaScript. This allows us to have implicit
|
||||
object literals, and YAML-style object definitions. Half assignments are
|
||||
removed, in favor of <tt>+=</tt>, <tt>or=</tt>, and friends.
|
||||
Interpolation now uses a hash mark <tt>#</tt> instead of the dollar sign
|
||||
Interpolation now uses a hash mark <tt>#</tt> instead of the dollar sign
|
||||
<tt>$</tt> — because dollar signs may be part of a valid JS identifier.
|
||||
Downwards range comprehensions are now safe again, and are optimized to
|
||||
straight for loops when created with integer endpoints.
|
||||
@@ -1880,7 +1912,7 @@ task(<span class="String"><span class="String">'</span>build:parser<span class="
|
||||
on the object — useful in constructors and setter functions.
|
||||
Constructor functions can now take splats.
|
||||
</p>
|
||||
|
||||
|
||||
<p>
|
||||
<b class="header" style="margin-top: 20px;">0.7.2</b>
|
||||
Quick bugfix (right after 0.7.1) for a problem that prevented <tt>coffee</tt>
|
||||
@@ -2086,7 +2118,7 @@ task(<span class="String"><span class="String">'</span>build:parser<span class="
|
||||
conversion. Splats. Splice literals. Object comprehensions. Blocks.
|
||||
The existential operator. Many thanks to all the folks who posted issues,
|
||||
with special thanks to
|
||||
<a href="http://github.com/kamatsu">Liam O'Connor-Davis</a> for whitespace
|
||||
<a href="http://github.com/liamoc">Liam O'Connor-Davis</a> for whitespace
|
||||
and expression help.
|
||||
</p>
|
||||
|
||||
|
||||
42
lib/browser.js
Normal file
42
lib/browser.js
Normal file
@@ -0,0 +1,42 @@
|
||||
(function() {
|
||||
var grind, grindRemote, processScripts;
|
||||
if ((typeof document === "undefined" || document === null) ? undefined : document.getElementsByTagName) {
|
||||
grind = function(coffee) {
|
||||
return setTimeout(exports.compile(coffee));
|
||||
};
|
||||
grindRemote = function(url) {
|
||||
var xhr;
|
||||
xhr = new (window.ActiveXObject || XMLHttpRequest)('Microsoft.XMLHTTP');
|
||||
xhr.open('GET', url, true);
|
||||
if ('overrideMimeType' in xhr) {
|
||||
xhr.overrideMimeType('text/plain');
|
||||
}
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
return grind(xhr.responseText);
|
||||
}
|
||||
};
|
||||
return xhr.send(null);
|
||||
};
|
||||
processScripts = function() {
|
||||
var _a, _b, _c, script;
|
||||
_b = document.getElementsByTagName('script');
|
||||
for (_a = 0, _c = _b.length; _a < _c; _a++) {
|
||||
script = _b[_a];
|
||||
if (script.type === 'text/coffeescript') {
|
||||
if (script.src) {
|
||||
grindRemote(script.src);
|
||||
} else {
|
||||
grind(script.innerHTML);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
if (window.addEventListener) {
|
||||
addEventListener('DOMContentLoaded', processScripts, false);
|
||||
} else {
|
||||
attachEvent('onload', processScripts);
|
||||
}
|
||||
}
|
||||
})();
|
||||
@@ -37,7 +37,7 @@
|
||||
return path.exists('Cakefile', function(exists) {
|
||||
var _a, _b, _c, _d, arg, args;
|
||||
if (!(exists)) {
|
||||
throw new Error(("Cakefile not found in " + (process.cwd())));
|
||||
throw new Error("Cakefile not found in " + (process.cwd()));
|
||||
}
|
||||
args = process.argv.slice(2, process.argv.length);
|
||||
CoffeeScript.run(fs.readFileSync('Cakefile').toString(), {
|
||||
@@ -71,14 +71,14 @@
|
||||
return _b;
|
||||
})().join('') : '';
|
||||
desc = task.description ? ("# " + (task.description)) : '';
|
||||
puts(("cake " + (name) + (spaces) + " " + (desc)));
|
||||
puts("cake " + (name) + (spaces) + " " + (desc));
|
||||
}
|
||||
if (switches.length) {
|
||||
return puts(oparse.help());
|
||||
}
|
||||
};
|
||||
missingTask = function(task) {
|
||||
puts(("No such task: \"" + (task) + "\""));
|
||||
puts("No such task: \"" + (task) + "\"");
|
||||
return process.exit(1);
|
||||
};
|
||||
})();
|
||||
|
||||
34
lib/coffee-script.js
Normal file → Executable file
34
lib/coffee-script.js
Normal file → Executable file
@@ -1,6 +1,6 @@
|
||||
(function() {
|
||||
var Lexer, compile, helpers, lexer, parser, path, processScripts;
|
||||
if ((typeof process !== "undefined" && process !== null)) {
|
||||
var Lexer, compile, helpers, lexer, parser, path;
|
||||
if (typeof process !== "undefined" && process !== null) {
|
||||
path = require('path');
|
||||
Lexer = require('./lexer').Lexer;
|
||||
parser = require('./parser').parser;
|
||||
@@ -10,17 +10,16 @@
|
||||
require.registerExtension('.coffee', function(content) {
|
||||
return compile(content);
|
||||
});
|
||||
};
|
||||
}
|
||||
} else {
|
||||
this.exports = (this.CoffeeScript = {});
|
||||
Lexer = this.Lexer;
|
||||
parser = this.parser;
|
||||
helpers = this.helpers;
|
||||
}
|
||||
exports.VERSION = '0.9.1';
|
||||
lexer = new Lexer();
|
||||
exports.VERSION = '0.9.3';
|
||||
exports.compile = (compile = function(code, options) {
|
||||
options = options || {};
|
||||
options || (options = {});
|
||||
try {
|
||||
return (parser.parse(lexer.tokenize(code))).compile(options);
|
||||
} catch (err) {
|
||||
@@ -36,12 +35,13 @@
|
||||
exports.nodes = function(code) {
|
||||
return parser.parse(lexer.tokenize(code));
|
||||
};
|
||||
exports.run = (function(code, options) {
|
||||
exports.run = function(code, options) {
|
||||
var __dirname, __filename;
|
||||
module.filename = (__filename = options.fileName);
|
||||
__dirname = path.dirname(__filename);
|
||||
return eval(exports.compile(code, options));
|
||||
});
|
||||
};
|
||||
lexer = new Lexer();
|
||||
parser.lexer = {
|
||||
lex: function() {
|
||||
var token;
|
||||
@@ -59,22 +59,4 @@
|
||||
return "";
|
||||
}
|
||||
};
|
||||
if ((typeof document !== "undefined" && document !== null) && document.getElementsByTagName) {
|
||||
processScripts = function() {
|
||||
var _a, _b, _c, _d, tag;
|
||||
_a = []; _c = document.getElementsByTagName('script');
|
||||
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
||||
tag = _c[_b];
|
||||
if (tag.type === 'text/coffeescript') {
|
||||
_a.push(eval(exports.compile(tag.innerHTML)));
|
||||
};
|
||||
}
|
||||
return _a;
|
||||
};
|
||||
if (window.addEventListener) {
|
||||
window.addEventListener('load', processScripts, false);
|
||||
} else if (window.attachEvent) {
|
||||
window.attachEvent('onload', processScripts);
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
(function() {
|
||||
var BANNER, CoffeeScript, EventEmitter, SWITCHES, _a, _b, _c, compileOptions, compileScript, compileScripts, compileStdio, exec, fs, helpers, lint, optionParser, options, optparse, parseOptions, path, printTokens, sources, spawn, usage, version, watch, writeJs;
|
||||
var BANNER, CoffeeScript, EventEmitter, SWITCHES, _a, _b, _c, compileOptions, compileScript, compileScripts, compileStdio, exec, fs, helpers, lint, optionParser, optparse, opts, parseOptions, path, printTokens, sources, spawn, usage, version, watch, writeJs;
|
||||
fs = require('fs');
|
||||
path = require('path');
|
||||
optparse = require('./optparse');
|
||||
@@ -15,25 +15,25 @@
|
||||
global.CoffeeScript = CoffeeScript;
|
||||
BANNER = 'coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee';
|
||||
SWITCHES = [['-c', '--compile', 'compile to JavaScript and save as .js files'], ['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-o', '--output [DIR]', 'set the directory for compiled JavaScript'], ['-w', '--watch', 'watch scripts for changes, and recompile'], ['-p', '--print', 'print the compiled JavaScript to stdout'], ['-l', '--lint', 'pipe the compiled JavaScript through JSLint'], ['-s', '--stdio', 'listen for and compile scripts over stdio'], ['-e', '--eval', 'compile a string from the command line'], ['-r', '--require [FILE*]', 'require a library before executing your script'], ['--no-wrap', 'compile without the top-level function wrapper'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-n', '--nodes', 'print the parse tree that Jison produces'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']];
|
||||
options = {};
|
||||
opts = {};
|
||||
sources = [];
|
||||
optionParser = null;
|
||||
exports.run = function() {
|
||||
var flags, separator;
|
||||
parseOptions();
|
||||
if (options.help) {
|
||||
if (opts.help) {
|
||||
return usage();
|
||||
}
|
||||
if (options.version) {
|
||||
if (opts.version) {
|
||||
return version();
|
||||
}
|
||||
if (options.interactive) {
|
||||
if (opts.interactive) {
|
||||
return require('./repl');
|
||||
}
|
||||
if (options.stdio) {
|
||||
if (opts.stdio) {
|
||||
return compileStdio();
|
||||
}
|
||||
if (options.eval) {
|
||||
if (opts.eval) {
|
||||
return compileScript('console', sources[0]);
|
||||
}
|
||||
if (!(sources.length)) {
|
||||
@@ -45,7 +45,7 @@
|
||||
flags = sources.slice((separator + 1), sources.length);
|
||||
sources = sources.slice(0, separator);
|
||||
}
|
||||
if (options.run) {
|
||||
if (opts.run) {
|
||||
flags = sources.slice(1, sources.length + 1).concat(flags);
|
||||
sources = [sources[0]];
|
||||
}
|
||||
@@ -64,7 +64,7 @@
|
||||
compile = function(source, topLevel) {
|
||||
return path.exists(source, function(exists) {
|
||||
if (!(exists)) {
|
||||
throw new Error(("File not found: " + (source)));
|
||||
throw new Error("File not found: " + (source));
|
||||
}
|
||||
return fs.stat(source, function(err, stats) {
|
||||
if (stats.isDirectory()) {
|
||||
@@ -81,7 +81,7 @@
|
||||
fs.readFile(source, function(err, code) {
|
||||
return compileScript(source, code.toString(), base);
|
||||
});
|
||||
if (options.watch) {
|
||||
if (opts.watch) {
|
||||
return watch(source, base);
|
||||
}
|
||||
}
|
||||
@@ -94,50 +94,51 @@
|
||||
}
|
||||
return _d;
|
||||
};
|
||||
compileScript = function(source, code, base) {
|
||||
var _d, _e, _f, codeOpts, file, js, o;
|
||||
o = options;
|
||||
codeOpts = compileOptions(source);
|
||||
compileScript = function(file, input, base) {
|
||||
var _d, _e, _f, o, options, req, t, task;
|
||||
o = opts;
|
||||
options = compileOptions(file);
|
||||
if (o.require) {
|
||||
_e = o.require;
|
||||
for (_d = 0, _f = _e.length; _d < _f; _d++) {
|
||||
file = _e[_d];
|
||||
require(fs.realpathSync(file));
|
||||
req = _e[_d];
|
||||
require(helpers.starts(req, '.') ? fs.realpathSync(req) : req);
|
||||
}
|
||||
}
|
||||
try {
|
||||
CoffeeScript.emit('compile', {
|
||||
source: source,
|
||||
code: code,
|
||||
base: base,
|
||||
t = (task = {
|
||||
file: file,
|
||||
input: input,
|
||||
options: options
|
||||
});
|
||||
CoffeeScript.emit('compile', task);
|
||||
if (o.tokens) {
|
||||
return printTokens(CoffeeScript.tokens(code));
|
||||
return printTokens(CoffeeScript.tokens(t.input));
|
||||
} else if (o.nodes) {
|
||||
return puts(CoffeeScript.nodes(code).toString());
|
||||
return puts(CoffeeScript.nodes(t.input).toString());
|
||||
} else if (o.run) {
|
||||
return CoffeeScript.run(code, codeOpts);
|
||||
return CoffeeScript.run(t.input, t.options);
|
||||
} else {
|
||||
js = CoffeeScript.compile(code, codeOpts);
|
||||
CoffeeScript.emit('success', js);
|
||||
t.output = CoffeeScript.compile(t.input, t.options);
|
||||
CoffeeScript.emit('success', task);
|
||||
if (o.print) {
|
||||
return print(js);
|
||||
return print(t.output);
|
||||
} else if (o.compile) {
|
||||
return writeJs(source, js, base);
|
||||
return writeJs(t.file, t.output, base);
|
||||
} else if (o.lint) {
|
||||
return lint(js);
|
||||
return lint(t.output);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
CoffeeScript.emit('failure', err);
|
||||
CoffeeScript.emit('failure', err, task);
|
||||
if (CoffeeScript.listeners('failure').length) {
|
||||
return null;
|
||||
}
|
||||
if (!(o.watch)) {
|
||||
error(err.stack) && process.exit(1);
|
||||
if (o.watch) {
|
||||
return puts(err.message);
|
||||
}
|
||||
return puts(err.message);
|
||||
error(err.stack);
|
||||
return process.exit(1);
|
||||
}
|
||||
};
|
||||
compileStdio = function() {
|
||||
@@ -158,10 +159,13 @@
|
||||
persistent: true,
|
||||
interval: 500
|
||||
}, function(curr, prev) {
|
||||
if (curr.mtime.getTime() === prev.mtime.getTime()) {
|
||||
if (curr.size === prev.size && curr.mtime.getTime() === prev.mtime.getTime()) {
|
||||
return null;
|
||||
}
|
||||
return fs.readFile(source, function(err, code) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
return compileScript(source, code.toString(), base);
|
||||
});
|
||||
});
|
||||
@@ -171,28 +175,29 @@
|
||||
filename = path.basename(source, path.extname(source)) + '.js';
|
||||
srcDir = path.dirname(source);
|
||||
baseDir = srcDir.substring(base.length);
|
||||
dir = options.output ? path.join(options.output, baseDir) : srcDir;
|
||||
dir = opts.output ? path.join(opts.output, baseDir) : srcDir;
|
||||
jsPath = path.join(dir, filename);
|
||||
compile = function() {
|
||||
if (js.length <= 0) {
|
||||
js = ' ';
|
||||
}
|
||||
return fs.writeFile(jsPath, js, function(err) {
|
||||
if (options.compile && options.watch) {
|
||||
return puts(("Compiled " + (source)));
|
||||
if (opts.compile && opts.watch) {
|
||||
return puts("Compiled " + (source));
|
||||
}
|
||||
});
|
||||
};
|
||||
return path.exists(dir, function(exists) {
|
||||
return exists ? compile() : exec(("mkdir -p " + (dir)), compile);
|
||||
return exists ? compile() : exec("mkdir -p " + (dir), compile);
|
||||
});
|
||||
};
|
||||
lint = function(js) {
|
||||
var jsl, printIt;
|
||||
var conf, jsl, printIt;
|
||||
printIt = function(buffer) {
|
||||
return print(buffer.toString());
|
||||
return puts(buffer.toString().trim());
|
||||
};
|
||||
jsl = spawn('jsl', ['-nologo', '-stdin']);
|
||||
conf = __dirname + '/../extras/jsl.conf';
|
||||
jsl = spawn('jsl', ['-nologo', '-stdin', '-conf', conf]);
|
||||
jsl.stdout.on('data', printIt);
|
||||
jsl.stderr.on('data', printIt);
|
||||
jsl.stdin.write(js);
|
||||
@@ -218,18 +223,18 @@
|
||||
parseOptions = function() {
|
||||
var o;
|
||||
optionParser = new optparse.OptionParser(SWITCHES, BANNER);
|
||||
o = (options = optionParser.parse(process.argv.slice(2, process.argv.length)));
|
||||
options.compile = options.compile || (!!o.output);
|
||||
options.run = !(o.compile || o.print || o.lint);
|
||||
options.print = !!(o.print || (o.eval || o.stdio && o.compile));
|
||||
return (sources = options.arguments);
|
||||
o = (opts = optionParser.parse(process.argv.slice(2, process.argv.length)));
|
||||
o.compile || (o.compile = (!!o.output));
|
||||
o.run = !(o.compile || o.print || o.lint);
|
||||
o.print = !!(o.print || (o.eval || o.stdio && o.compile));
|
||||
return (sources = o.arguments);
|
||||
};
|
||||
compileOptions = function(fileName) {
|
||||
var o;
|
||||
o = {
|
||||
fileName: fileName
|
||||
};
|
||||
o.noWrap = options['no-wrap'];
|
||||
o.noWrap = opts['no-wrap'];
|
||||
return o;
|
||||
};
|
||||
usage = function() {
|
||||
@@ -237,7 +242,7 @@
|
||||
return process.exit(0);
|
||||
};
|
||||
version = function() {
|
||||
puts(("CoffeeScript version " + (CoffeeScript.VERSION)));
|
||||
puts("CoffeeScript version " + (CoffeeScript.VERSION));
|
||||
return process.exit(0);
|
||||
};
|
||||
})();
|
||||
|
||||
206
lib/grammar.js
206
lib/grammar.js
@@ -32,9 +32,11 @@
|
||||
return new LiteralNode($1);
|
||||
}), o("CONTINUE", function() {
|
||||
return new LiteralNode($1);
|
||||
}), o("DEBUGGER", function() {
|
||||
return new LiteralNode($1);
|
||||
})
|
||||
],
|
||||
Expression: [o("Value"), o("Call"), o("Code"), o("Operation"), o("Assign"), o("If"), o("Try"), o("While"), o("For"), o("Switch"), o("Extends"), o("Class"), o("Splat"), o("Existence"), o("Comment")],
|
||||
Expression: [o("Value"), o("Call"), o("Code"), o("Operation"), o("Assign"), o("If"), o("Try"), o("While"), o("For"), o("Switch"), o("Extends"), o("Class"), o("Existence"), o("Comment")],
|
||||
Block: [
|
||||
o("INDENT Body OUTDENT", function() {
|
||||
return $2;
|
||||
@@ -253,7 +255,7 @@
|
||||
})
|
||||
],
|
||||
Call: [
|
||||
o("Invocation"), o("Super"), o("NEW Invocation", function() {
|
||||
o("Invocation"), o("NEW Invocation", function() {
|
||||
return $2.newInstance();
|
||||
}), o("NEW Value", function() {
|
||||
return (new CallNode($2, [])).newInstance();
|
||||
@@ -265,24 +267,30 @@
|
||||
})
|
||||
],
|
||||
Invocation: [
|
||||
o("Value Arguments", function() {
|
||||
return new CallNode($1, $2);
|
||||
}), o("Invocation Arguments", function() {
|
||||
return new CallNode($1, $2);
|
||||
})
|
||||
],
|
||||
Arguments: [
|
||||
o("CALL_START ArgList OptComma CALL_END", function() {
|
||||
return $2;
|
||||
})
|
||||
],
|
||||
Super: [
|
||||
o("SUPER", function() {
|
||||
o("Value OptFuncExist Arguments", function() {
|
||||
return new CallNode($1, $3, $2);
|
||||
}), o("Invocation OptFuncExist Arguments", function() {
|
||||
return new CallNode($1, $3, $2);
|
||||
}), o("SUPER", function() {
|
||||
return new CallNode('super', [new SplatNode(new LiteralNode('arguments'))]);
|
||||
}), o("SUPER Arguments", function() {
|
||||
return new CallNode('super', $2);
|
||||
})
|
||||
],
|
||||
OptFuncExist: [
|
||||
o("", function() {
|
||||
return false;
|
||||
}), o("FUNC_EXIST", function() {
|
||||
return true;
|
||||
})
|
||||
],
|
||||
Arguments: [
|
||||
o("CALL_START CALL_END", function() {
|
||||
return [];
|
||||
}), o("CALL_START ArgList OptComma CALL_END", function() {
|
||||
return $2;
|
||||
})
|
||||
],
|
||||
This: [
|
||||
o("THIS", function() {
|
||||
return new ValueNode(new LiteralNode('this'));
|
||||
@@ -290,43 +298,53 @@
|
||||
return new ValueNode(new LiteralNode('this'));
|
||||
})
|
||||
],
|
||||
RangeDots: [
|
||||
o(". .", function() {
|
||||
return 'inclusive';
|
||||
}), o(". . .", function() {
|
||||
return 'exclusive';
|
||||
})
|
||||
],
|
||||
ThisProperty: [
|
||||
o("@ Identifier", function() {
|
||||
return new ValueNode(new LiteralNode('this'), [new AccessorNode($2)]);
|
||||
})
|
||||
],
|
||||
Range: [
|
||||
o("[ Expression . . Expression ]", function() {
|
||||
return new RangeNode($2, $5);
|
||||
}), o("[ Expression . . . Expression ]", function() {
|
||||
return new RangeNode($2, $6, true);
|
||||
o("[ Expression RangeDots Expression ]", function() {
|
||||
return new RangeNode($2, $4, $3);
|
||||
})
|
||||
],
|
||||
Slice: [
|
||||
o("INDEX_START Expression . . Expression INDEX_END", function() {
|
||||
return new RangeNode($2, $5);
|
||||
}), o("INDEX_START Expression . . . Expression INDEX_END", function() {
|
||||
return new RangeNode($2, $6, true);
|
||||
o("INDEX_START Expression RangeDots Expression INDEX_END", function() {
|
||||
return new RangeNode($2, $4, $3);
|
||||
}), o("INDEX_START Expression RangeDots INDEX_END", function() {
|
||||
return new RangeNode($2, null, $3);
|
||||
}), o("INDEX_START RangeDots Expression INDEX_END", function() {
|
||||
return new RangeNode(null, $3, $2);
|
||||
})
|
||||
],
|
||||
Array: [
|
||||
o("[ ArgList OptComma ]", function() {
|
||||
o("[ ]", function() {
|
||||
return new ArrayNode([]);
|
||||
}), o("[ ArgList OptComma ]", function() {
|
||||
return new ArrayNode($2);
|
||||
})
|
||||
],
|
||||
ArgList: [
|
||||
o("", function() {
|
||||
return [];
|
||||
}), o("Expression", function() {
|
||||
o("Arg", function() {
|
||||
return [$1];
|
||||
}), o("ArgList , Expression", function() {
|
||||
}), o("ArgList , Arg", function() {
|
||||
return $1.concat([$3]);
|
||||
}), o("ArgList OptComma TERMINATOR Expression", function() {
|
||||
}), o("ArgList OptComma TERMINATOR Arg", function() {
|
||||
return $1.concat([$4]);
|
||||
}), o("INDENT ArgList OptComma OUTDENT", function() {
|
||||
return $2;
|
||||
}), o("ArgList OptComma INDENT ArgList OptComma OUTDENT", function() {
|
||||
return $1.concat($4);
|
||||
})
|
||||
],
|
||||
Arg: [o("Expression"), o("Splat")],
|
||||
SimpleArgs: [
|
||||
o("Expression"), o("SimpleArgs , Expression", function() {
|
||||
return $1 instanceof Array ? $1.concat([$3]) : [$1].concat([$3]);
|
||||
@@ -354,6 +372,8 @@
|
||||
Parenthetical: [
|
||||
o("( Line )", function() {
|
||||
return new ParentheticalNode($2);
|
||||
}), o("( )", function() {
|
||||
return new ParentheticalNode(new LiteralNode(''));
|
||||
})
|
||||
],
|
||||
WhileSource: [
|
||||
@@ -477,29 +497,25 @@
|
||||
],
|
||||
Switch: [
|
||||
o("SWITCH Expression INDENT Whens OUTDENT", function() {
|
||||
return $4.switchesOver($2);
|
||||
return new SwitchNode($2, $4);
|
||||
}), o("SWITCH Expression INDENT Whens ELSE Block OUTDENT", function() {
|
||||
return $4.switchesOver($2).addElse($6, true);
|
||||
return new SwitchNode($2, $4, $6);
|
||||
}), o("SWITCH INDENT Whens OUTDENT", function() {
|
||||
return $3;
|
||||
return new SwitchNode(null, $3);
|
||||
}), o("SWITCH INDENT Whens ELSE Block OUTDENT", function() {
|
||||
return $3.addElse($5, true);
|
||||
return new SwitchNode(null, $3, $5);
|
||||
})
|
||||
],
|
||||
Whens: [
|
||||
o("When"), o("Whens When", function() {
|
||||
return $1.addElse($2);
|
||||
return $1.concat($2);
|
||||
})
|
||||
],
|
||||
When: [
|
||||
o("LEADING_WHEN SimpleArgs Block", function() {
|
||||
return new IfNode($2, $3, {
|
||||
statement: true
|
||||
});
|
||||
return [[$2, $3]];
|
||||
}), o("LEADING_WHEN SimpleArgs Block TERMINATOR", function() {
|
||||
return new IfNode($2, $3, {
|
||||
statement: true
|
||||
});
|
||||
return [[$2, $3]];
|
||||
})
|
||||
],
|
||||
IfBlock: [
|
||||
@@ -516,20 +532,20 @@
|
||||
})
|
||||
],
|
||||
If: [
|
||||
o("IfBlock"), o("Statement IF Expression", function() {
|
||||
o("IfBlock"), o("Statement POST_IF Expression", function() {
|
||||
return new IfNode($3, Expressions.wrap([$1]), {
|
||||
statement: true
|
||||
});
|
||||
}), o("Expression IF Expression", function() {
|
||||
}), o("Expression POST_IF Expression", function() {
|
||||
return new IfNode($3, Expressions.wrap([$1]), {
|
||||
statement: true
|
||||
});
|
||||
}), o("Statement UNLESS Expression", function() {
|
||||
}), o("Statement POST_UNLESS Expression", function() {
|
||||
return new IfNode($3, Expressions.wrap([$1]), {
|
||||
statement: true,
|
||||
invert: true
|
||||
});
|
||||
}), o("Expression UNLESS Expression", function() {
|
||||
}), o("Expression POST_UNLESS Expression", function() {
|
||||
return new IfNode($3, Expressions.wrap([$1]), {
|
||||
statement: true,
|
||||
invert: true
|
||||
@@ -537,102 +553,62 @@
|
||||
})
|
||||
],
|
||||
Operation: [
|
||||
o("! Expression", function() {
|
||||
return new OpNode('!', $2);
|
||||
}), o("!! Expression", function() {
|
||||
return new OpNode('!!', $2);
|
||||
}), o("- Expression", (function() {
|
||||
o("UNARY Expression", function() {
|
||||
return new OpNode($1, $2);
|
||||
}), o("- Expression", function() {
|
||||
return new OpNode('-', $2);
|
||||
}), {
|
||||
prec: 'UMINUS'
|
||||
}), o("+ Expression", (function() {
|
||||
}, {
|
||||
prec: 'UNARY'
|
||||
}), o("+ Expression", function() {
|
||||
return new OpNode('+', $2);
|
||||
}), {
|
||||
prec: 'UPLUS'
|
||||
}), o("~ Expression", function() {
|
||||
return new OpNode('~', $2);
|
||||
}, {
|
||||
prec: 'UNARY'
|
||||
}), o("-- Expression", function() {
|
||||
return new OpNode('--', $2);
|
||||
}), o("++ Expression", function() {
|
||||
return new OpNode('++', $2);
|
||||
}), o("DELETE Expression", function() {
|
||||
return new OpNode('delete', $2);
|
||||
}), o("TYPEOF Expression", function() {
|
||||
return new OpNode('typeof', $2);
|
||||
}), o("Expression --", function() {
|
||||
return new OpNode('--', $1, null, true);
|
||||
}), o("Expression ++", function() {
|
||||
return new OpNode('++', $1, null, true);
|
||||
}), o("Expression * Expression", function() {
|
||||
return new OpNode('*', $1, $3);
|
||||
}), o("Expression / Expression", function() {
|
||||
return new OpNode('/', $1, $3);
|
||||
}), o("Expression % Expression", function() {
|
||||
return new OpNode('%', $1, $3);
|
||||
}), o("Expression ? Expression", function() {
|
||||
return new OpNode('?', $1, $3);
|
||||
}), o("Expression + Expression", function() {
|
||||
return new OpNode('+', $1, $3);
|
||||
}), o("Expression - Expression", function() {
|
||||
return new OpNode('-', $1, $3);
|
||||
}), o("Expression << Expression", function() {
|
||||
return new OpNode('<<', $1, $3);
|
||||
}), o("Expression >> Expression", function() {
|
||||
return new OpNode('>>', $1, $3);
|
||||
}), o("Expression >>> Expression", function() {
|
||||
return new OpNode('>>>', $1, $3);
|
||||
}), o("Expression & Expression", function() {
|
||||
return new OpNode('&', $1, $3);
|
||||
}), o("Expression | Expression", function() {
|
||||
return new OpNode('|', $1, $3);
|
||||
}), o("Expression ^ Expression", function() {
|
||||
return new OpNode('^', $1, $3);
|
||||
}), o("Expression <= Expression", function() {
|
||||
return new OpNode('<=', $1, $3);
|
||||
}), o("Expression < Expression", function() {
|
||||
return new OpNode('<', $1, $3);
|
||||
}), o("Expression > Expression", function() {
|
||||
return new OpNode('>', $1, $3);
|
||||
}), o("Expression >= Expression", function() {
|
||||
return new OpNode('>=', $1, $3);
|
||||
}), o("Expression == Expression", function() {
|
||||
return new OpNode('==', $1, $3);
|
||||
}), o("Expression != Expression", function() {
|
||||
return new OpNode('!=', $1, $3);
|
||||
}), o("Expression && Expression", function() {
|
||||
return new OpNode('&&', $1, $3);
|
||||
}), o("Expression || Expression", function() {
|
||||
return new OpNode('||', $1, $3);
|
||||
}), o("Expression OP? Expression", function() {
|
||||
return new OpNode('?', $1, $3);
|
||||
}), o("Expression -= Expression", function() {
|
||||
return new OpNode('-=', $1, $3);
|
||||
}), o("Expression += Expression", function() {
|
||||
return new OpNode('+=', $1, $3);
|
||||
}), o("Expression /= Expression", function() {
|
||||
return new OpNode('/=', $1, $3);
|
||||
}), o("Expression *= Expression", function() {
|
||||
return new OpNode('*=', $1, $3);
|
||||
}), o("Expression %= Expression", function() {
|
||||
return new OpNode('%=', $1, $3);
|
||||
}), o("Expression ||= Expression", function() {
|
||||
return new OpNode('||=', $1, $3);
|
||||
}), o("Expression &&= Expression", function() {
|
||||
return new OpNode('&&=', $1, $3);
|
||||
}), o("Expression ?= Expression", function() {
|
||||
return new OpNode('?=', $1, $3);
|
||||
}), o("Expression INSTANCEOF Expression", function() {
|
||||
return new OpNode('instanceof', $1, $3);
|
||||
}), o("Expression MATH Expression", function() {
|
||||
return new OpNode($2, $1, $3);
|
||||
}), o("Expression SHIFT Expression", function() {
|
||||
return new OpNode($2, $1, $3);
|
||||
}), o("Expression COMPARE Expression", function() {
|
||||
return new OpNode($2, $1, $3);
|
||||
}), o("Expression LOGIC Expression", function() {
|
||||
return new OpNode($2, $1, $3);
|
||||
}), o("Value COMPOUND_ASSIGN Expression", function() {
|
||||
return new OpNode($2, $1, $3);
|
||||
}), o("Value COMPOUND_ASSIGN INDENT Expression OUTDENT", function() {
|
||||
return new OpNode($2, $1, $4);
|
||||
}), o("Expression IN Expression", function() {
|
||||
return new InNode($1, $3);
|
||||
}), o("Expression OF Expression", function() {
|
||||
return new OpNode('in', $1, $3);
|
||||
}), o("Expression ! IN Expression", function() {
|
||||
return new OpNode('!', new InNode($1, $4));
|
||||
}), o("Expression ! OF Expression", function() {
|
||||
return new OpNode('!', new ParentheticalNode(new OpNode('in', $1, $4)));
|
||||
}), o("Expression INSTANCEOF Expression", function() {
|
||||
return new OpNode('instanceof', $1, $3);
|
||||
}), o("Expression UNARY IN Expression", function() {
|
||||
return new OpNode($2, new InNode($1, $4));
|
||||
}), o("Expression UNARY OF Expression", function() {
|
||||
return new OpNode($2, new ParentheticalNode(new OpNode('in', $1, $4)));
|
||||
}), o("Expression UNARY INSTANCEOF Expression", function() {
|
||||
return new OpNode($2, new ParentheticalNode(new OpNode('instanceof', $1, $4)));
|
||||
})
|
||||
]
|
||||
};
|
||||
operators = [["left", '?'], ["nonassoc", 'UMINUS', 'UPLUS', '!', '!!', '~', '++', '--'], ["left", '*', '/', '%'], ["left", '+', '-'], ["left", '<<', '>>', '>>>'], ["left", '&', '|', '^'], ["left", '<=', '<', '>', '>='], ["right", 'DELETE', 'INSTANCEOF', 'TYPEOF'], ["left", '==', '!='], ["left", '&&', '||', 'OP?'], ["right", '-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?='], ["left", '.'], ["right", 'INDENT'], ["left", 'OUTDENT'], ["right", 'WHEN', 'LEADING_WHEN', 'IN', 'OF', 'BY', 'THROW'], ["right", 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'NEW', 'SUPER', 'CLASS'], ["left", 'EXTENDS'], ["right", '=', ':', 'RETURN'], ["right", '->', '=>', 'UNLESS', 'IF', 'ELSE']];
|
||||
operators = [["right", '?', 'NEW'], ["left", 'CALL_START', 'CALL_END'], ["nonassoc", '++', '--'], ["right", 'UNARY'], ["left", 'MATH'], ["left", '+', '-'], ["left", 'SHIFT'], ["left", 'COMPARE'], ["left", 'INSTANCEOF'], ["left", '==', '!='], ["left", 'LOGIC'], ["right", 'COMPOUND_ASSIGN'], ["left", '.'], ["nonassoc", 'INDENT', 'OUTDENT'], ["right", 'WHEN', 'LEADING_WHEN', 'IN', 'OF', 'BY', 'THROW'], ["right", 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'EXTENDS'], ["right", '=', ':', 'RETURN'], ["right", '->', '=>', 'UNLESS', 'POST_IF', 'POST_UNLESS']];
|
||||
tokens = [];
|
||||
_a = grammar;
|
||||
for (name in _a) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(function() {
|
||||
var compact, count, del, ends, extend, flatten, helpers, include, indexOf, merge, starts;
|
||||
if (!((typeof process !== "undefined" && process !== null))) {
|
||||
if (!(typeof process !== "undefined" && process !== null)) {
|
||||
this.exports = this;
|
||||
}
|
||||
helpers = (exports.helpers = {});
|
||||
@@ -36,7 +36,7 @@
|
||||
item = _c[_b];
|
||||
if (item) {
|
||||
_a.push(item);
|
||||
};
|
||||
}
|
||||
}
|
||||
return _a;
|
||||
});
|
||||
@@ -72,7 +72,7 @@
|
||||
_a = []; _b = properties;
|
||||
for (key in _b) {
|
||||
val = _b[key];
|
||||
_a.push((object[key] = val));
|
||||
_a.push(object[key] = val);
|
||||
}
|
||||
return _a;
|
||||
});
|
||||
@@ -86,7 +86,7 @@
|
||||
memo = memo.concat(item);
|
||||
} else {
|
||||
memo.push(item);
|
||||
};
|
||||
}
|
||||
}
|
||||
return memo;
|
||||
});
|
||||
|
||||
149
lib/lexer.js
149
lib/lexer.js
@@ -1,7 +1,7 @@
|
||||
(function() {
|
||||
var ASSIGNED, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_KEYWORDS, COMMENT, CONVERSIONS, HEREDOC, HEREDOC_INDENT, IDENTIFIER, JS_CLEANER, JS_FORBIDDEN, JS_KEYWORDS, LAST_DENT, LAST_DENTS, LINE_BREAK, Lexer, MULTILINER, MULTI_DENT, NEXT_CHARACTER, NOT_REGEX, NO_NEWLINE, NUMBER, OPERATOR, REGEX_END, REGEX_ESCAPE, REGEX_INTERPOLATION, REGEX_START, RESERVED, Rewriter, STRING_NEWLINES, WHITESPACE, _a, _b, _c, compact, count, helpers, include, starts;
|
||||
var ASSIGNED, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, CONVERSIONS, HEREDOC, HEREDOC_INDENT, IDENTIFIER, JS_CLEANER, JS_FORBIDDEN, JS_KEYWORDS, LAST_DENT, LAST_DENTS, LINE_BREAK, LOGIC, Lexer, MATH, MULTILINER, MULTI_DENT, NEXT_CHARACTER, NOT_REGEX, NO_NEWLINE, NUMBER, OPERATOR, REGEX_END, REGEX_ESCAPE, REGEX_INTERPOLATION, REGEX_START, RESERVED, Rewriter, SHIFT, UNARY, WHITESPACE, _a, _b, _c, compact, count, helpers, include, starts;
|
||||
var __slice = Array.prototype.slice;
|
||||
if ((typeof process !== "undefined" && process !== null)) {
|
||||
if (typeof process !== "undefined" && process !== null) {
|
||||
_a = require('./rewriter');
|
||||
Rewriter = _a.Rewriter;
|
||||
_b = require('./helpers');
|
||||
@@ -26,6 +26,7 @@
|
||||
this.i = 0;
|
||||
this.line = o.line || 0;
|
||||
this.indent = 0;
|
||||
this.indebt = 0;
|
||||
this.outdebt = 0;
|
||||
this.indents = [];
|
||||
this.tokens = [];
|
||||
@@ -86,6 +87,9 @@
|
||||
if (id === 'all' && this.tag() === 'FOR') {
|
||||
tag = 'ALL';
|
||||
}
|
||||
if (include(UNARY, tag)) {
|
||||
tag = 'UNARY';
|
||||
}
|
||||
if (include(JS_FORBIDDEN, id)) {
|
||||
if (forcedIdentifier) {
|
||||
tag = 'STRING';
|
||||
@@ -105,6 +109,12 @@
|
||||
if (include(COFFEE_ALIASES, id)) {
|
||||
tag = (id = CONVERSIONS[id]);
|
||||
}
|
||||
if (include(LOGIC, id)) {
|
||||
tag = 'LOGIC';
|
||||
}
|
||||
if (id === '!') {
|
||||
tag = 'UNARY';
|
||||
}
|
||||
}
|
||||
this.token(tag, id);
|
||||
if (close_index) {
|
||||
@@ -132,7 +142,7 @@
|
||||
if (!(string = this.balancedToken(['"', '"'], ['#{', '}']) || this.balancedToken(["'", "'"]))) {
|
||||
return false;
|
||||
}
|
||||
this.interpolateString(string.replace(STRING_NEWLINES, " \\\n"));
|
||||
this.interpolateString(string.replace(/\n/g, '\\\n'));
|
||||
this.line += count(string, "\n");
|
||||
this.i += string.length;
|
||||
return true;
|
||||
@@ -154,17 +164,17 @@
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.commentToken = function() {
|
||||
var comment, match;
|
||||
var match;
|
||||
if (!(match = this.chunk.match(COMMENT))) {
|
||||
return false;
|
||||
}
|
||||
this.line += count(match[1], "\n");
|
||||
this.i += match[1].length;
|
||||
if (match[2]) {
|
||||
comment = this.sanitizeHeredoc(match[2], {
|
||||
herecomment: true
|
||||
});
|
||||
this.token('HERECOMMENT', comment.split(MULTILINER));
|
||||
if (match[4]) {
|
||||
this.token('HERECOMMENT', this.sanitizeHeredoc(match[4], {
|
||||
herecomment: true,
|
||||
indent: match[3]
|
||||
}));
|
||||
this.token('TERMINATOR', '\n');
|
||||
}
|
||||
return true;
|
||||
@@ -182,8 +192,11 @@
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.regexToken = function() {
|
||||
var end, flags, regex, str;
|
||||
if (!(this.chunk.match(REGEX_START))) {
|
||||
var _d, end, first, flags, regex, str;
|
||||
if (!(first = this.chunk.match(REGEX_START))) {
|
||||
return false;
|
||||
}
|
||||
if (first[1] === ' ' && !('CALL_START' === (_d = this.tag()) || '=' === _d)) {
|
||||
return false;
|
||||
}
|
||||
if (include(NOT_REGEX, this.tag())) {
|
||||
@@ -204,7 +217,7 @@
|
||||
return '\\' + escaped;
|
||||
});
|
||||
this.tokens = this.tokens.concat([['(', '('], ['NEW', 'new'], ['IDENTIFIER', 'RegExp'], ['CALL_START', '(']]);
|
||||
this.interpolateString(("\"" + (str) + "\""), {
|
||||
this.interpolateString("\"" + (str) + "\"", {
|
||||
escapeQuotes: true
|
||||
});
|
||||
if (flags) {
|
||||
@@ -233,20 +246,22 @@
|
||||
size = indent.match(LAST_DENTS).reverse()[0].match(LAST_DENT)[1].length;
|
||||
nextCharacter = this.match(NEXT_CHARACTER, 1);
|
||||
noNewlines = nextCharacter === '.' || nextCharacter === ',' || this.unfinished();
|
||||
if (size === this.indent) {
|
||||
if (size - this.indebt === this.indent) {
|
||||
if (noNewlines) {
|
||||
return this.suppressNewlines();
|
||||
}
|
||||
return this.newlineToken(indent);
|
||||
} else if (size > this.indent) {
|
||||
if (noNewlines) {
|
||||
this.indebt = size - this.indent;
|
||||
return this.suppressNewlines();
|
||||
}
|
||||
this.outdebt = 0;
|
||||
diff = size - this.indent;
|
||||
diff = size - this.indent + this.outdebt;
|
||||
this.token('INDENT', diff);
|
||||
this.indents.push(diff);
|
||||
this.outdebt = (this.indebt = 0);
|
||||
} else {
|
||||
this.indebt = 0;
|
||||
this.outdentToken(this.indent - size, noNewlines);
|
||||
}
|
||||
this.indent = size;
|
||||
@@ -293,7 +308,7 @@
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.newlineToken = function(newlines) {
|
||||
if (!(this.tag() === 'TERMINATOR')) {
|
||||
if (this.tag() !== 'TERMINATOR') {
|
||||
this.token('TERMINATOR', "\n");
|
||||
}
|
||||
return true;
|
||||
@@ -305,31 +320,45 @@
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.literalToken = function() {
|
||||
var _d, match, prevSpaced, space, tag, value;
|
||||
var _d, match, prev, space, spaced, tag, value;
|
||||
match = this.chunk.match(OPERATOR);
|
||||
value = match && match[1];
|
||||
space = match && match[2];
|
||||
if (value && value.match(CODE)) {
|
||||
this.tagParameters();
|
||||
}
|
||||
value = value || this.chunk.substr(0, 1);
|
||||
value || (value = this.chunk.substr(0, 1));
|
||||
this.i += value.length;
|
||||
prevSpaced = this.prev() && this.prev().spaced;
|
||||
spaced = (prev = this.prev()) && prev.spaced;
|
||||
tag = value;
|
||||
if (value === '=') {
|
||||
if (include(JS_FORBIDDEN, this.value())) {
|
||||
this.assignmentError();
|
||||
}
|
||||
if (('or' === (_d = this.value()) || 'and' === _d)) {
|
||||
return this.tag(1, CONVERSIONS[this.value()] + '=');
|
||||
this.tokens.splice(this.tokens.length - 1, 1, ['COMPOUND_ASSIGN', CONVERSIONS[this.value()] + '=', prev[2]]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (value === ';') {
|
||||
tag = 'TERMINATOR';
|
||||
} else if (value === '?' && prevSpaced) {
|
||||
tag = 'OP?';
|
||||
} else if (include(CALLABLE, this.tag()) && !prevSpaced) {
|
||||
} else if (include(LOGIC, value)) {
|
||||
tag = 'LOGIC';
|
||||
} else if (include(MATH, value)) {
|
||||
tag = 'MATH';
|
||||
} else if (include(COMPARE, value)) {
|
||||
tag = 'COMPARE';
|
||||
} else if (include(COMPOUND_ASSIGN, value)) {
|
||||
tag = 'COMPOUND_ASSIGN';
|
||||
} else if (include(UNARY, value)) {
|
||||
tag = 'UNARY';
|
||||
} else if (include(SHIFT, value)) {
|
||||
tag = 'SHIFT';
|
||||
} else if (include(CALLABLE, this.tag()) && !spaced) {
|
||||
if (value === '(') {
|
||||
if (prev[0] === '?') {
|
||||
prev[0] = 'FUNC_EXIST';
|
||||
}
|
||||
tag = 'CALL_START';
|
||||
} else if (value === '[') {
|
||||
tag = 'INDEX_START';
|
||||
@@ -367,20 +396,27 @@
|
||||
};
|
||||
Lexer.prototype.sanitizeHeredoc = function(doc, options) {
|
||||
var _d, attempt, indent, match;
|
||||
while ((match = HEREDOC_INDENT.exec(doc))) {
|
||||
attempt = (typeof (_d = match[2]) !== "undefined" && _d !== null) ? match[2] : match[3];
|
||||
if (!indent || attempt.length < indent.length) {
|
||||
indent = attempt;
|
||||
indent = options.indent;
|
||||
if (options.herecomment && !include(doc, '\n')) {
|
||||
return doc;
|
||||
}
|
||||
if (!(options.herecomment)) {
|
||||
while ((match = HEREDOC_INDENT.exec(doc)) !== null) {
|
||||
attempt = (typeof (_d = match[2]) !== "undefined" && _d !== null) ? match[2] : match[3];
|
||||
if (!(typeof indent !== "undefined" && indent !== null) || attempt.length < indent.length) {
|
||||
indent = attempt;
|
||||
}
|
||||
}
|
||||
}
|
||||
doc = doc.replace(new RegExp("^" + indent, 'gm'), '');
|
||||
indent || (indent = '');
|
||||
doc = doc.replace(new RegExp("^" + indent, 'gm'), '').replace(/^\n/, '');
|
||||
if (options.herecomment) {
|
||||
return doc;
|
||||
}
|
||||
return doc.replace(MULTILINER, "\\n").replace(new RegExp(options.quote, 'g'), ("\\" + (options.quote)));
|
||||
return doc.replace(MULTILINER, "\\n").replace(new RegExp(options.quote, 'g'), "\\" + (options.quote));
|
||||
};
|
||||
Lexer.prototype.tagParameters = function() {
|
||||
var _d, i, tok;
|
||||
var i, tok;
|
||||
if (this.tag() !== ')') {
|
||||
return null;
|
||||
}
|
||||
@@ -391,11 +427,15 @@
|
||||
if (!tok) {
|
||||
return null;
|
||||
}
|
||||
if ((_d = tok[0]) === 'IDENTIFIER') {
|
||||
switch (tok[0]) {
|
||||
case 'IDENTIFIER':
|
||||
tok[0] = 'PARAM';
|
||||
} else if (_d === ')') {
|
||||
break;
|
||||
case ')':
|
||||
tok[0] = 'PARAM_END';
|
||||
} else if (_d === '(' || _d === 'CALL_START') {
|
||||
break;
|
||||
case '(':
|
||||
case 'CALL_START':
|
||||
return (tok[0] = 'PARAM_START');
|
||||
}
|
||||
}
|
||||
@@ -405,14 +445,14 @@
|
||||
return this.outdentToken(this.indent);
|
||||
};
|
||||
Lexer.prototype.identifierError = function(word) {
|
||||
throw new Error(("SyntaxError: Reserved word \"" + (word) + "\" on line " + (this.line + 1)));
|
||||
throw new Error("SyntaxError: Reserved word \"" + (word) + "\" on line " + (this.line + 1));
|
||||
};
|
||||
Lexer.prototype.assignmentError = function() {
|
||||
throw new Error(("SyntaxError: Reserved word \"" + (this.value()) + "\" on line " + (this.line + 1) + " can't be assigned"));
|
||||
throw new Error("SyntaxError: Reserved word \"" + (this.value()) + "\" on line " + (this.line + 1) + " can't be assigned");
|
||||
};
|
||||
Lexer.prototype.balancedString = function(str, delimited, options) {
|
||||
var _d, _e, _f, _g, close, i, levels, open, pair, slash;
|
||||
options = options || {};
|
||||
options || (options = {});
|
||||
slash = delimited[0][0] === '/';
|
||||
levels = [];
|
||||
i = 0;
|
||||
@@ -449,13 +489,13 @@
|
||||
if (slash) {
|
||||
return false;
|
||||
}
|
||||
throw new Error(("SyntaxError: Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1)));
|
||||
throw new Error("SyntaxError: Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1));
|
||||
}
|
||||
return !i ? false : str.substring(0, i);
|
||||
};
|
||||
Lexer.prototype.interpolateString = function(str, options) {
|
||||
var _d, _e, _f, _g, _h, _i, escaped, expr, i, idx, inner, interpolated, lexer, nested, pi, quote, tag, tok, token, tokens, value;
|
||||
options = options || {};
|
||||
options || (options = {});
|
||||
if (str.length < 3 || !starts(str, '"')) {
|
||||
return this.token('STRING', str);
|
||||
} else {
|
||||
@@ -468,7 +508,7 @@
|
||||
while (i < str.length - 1) {
|
||||
if (starts(str, '\\', i)) {
|
||||
i += 1;
|
||||
} else if ((expr = this.balancedString(str.substring(i), [['#{', '}']]))) {
|
||||
} else if (expr = this.balancedString(str.substring(i), [['#{', '}']])) {
|
||||
if (pi < i) {
|
||||
tokens.push(['STRING', quote + str.substring(pi, i) + quote]);
|
||||
}
|
||||
@@ -477,7 +517,7 @@
|
||||
if (options.heredoc) {
|
||||
inner = inner.replace(new RegExp('\\\\' + quote, 'g'), quote);
|
||||
}
|
||||
nested = lexer.tokenize(("(" + (inner) + ")"), {
|
||||
nested = lexer.tokenize("(" + (inner) + ")", {
|
||||
line: this.line
|
||||
});
|
||||
_e = nested;
|
||||
@@ -485,7 +525,7 @@
|
||||
tok = _e[idx];
|
||||
if (tok[0] === 'CALL_END') {
|
||||
(tok[0] = ')');
|
||||
};
|
||||
}
|
||||
}
|
||||
nested.pop();
|
||||
tokens.push(['TOKENS', nested]);
|
||||
@@ -500,7 +540,7 @@
|
||||
if (pi < i && pi < str.length - 1) {
|
||||
tokens.push(['STRING', quote + str.substring(pi, i) + quote]);
|
||||
}
|
||||
if (!(tokens[0][0] === 'STRING')) {
|
||||
if (tokens[0][0] !== 'STRING') {
|
||||
tokens.unshift(['STRING', '""']);
|
||||
}
|
||||
interpolated = tokens.length > 1;
|
||||
@@ -517,7 +557,7 @@
|
||||
this.tokens = this.tokens.concat(value);
|
||||
} else if (tag === 'STRING' && options.escapeQuotes) {
|
||||
escaped = value.substring(1, value.length - 1).replace(/"/g, '\\"');
|
||||
this.token(tag, ("\"" + (escaped) + "\""));
|
||||
this.token(tag, "\"" + (escaped) + "\"");
|
||||
} else {
|
||||
this.token(tag, value);
|
||||
}
|
||||
@@ -539,7 +579,7 @@
|
||||
if (!(tok = this.prev(index))) {
|
||||
return null;
|
||||
}
|
||||
if ((typeof newTag !== "undefined" && newTag !== null)) {
|
||||
if (typeof newTag !== "undefined" && newTag !== null) {
|
||||
return (tok[0] = newTag);
|
||||
}
|
||||
return tok[0];
|
||||
@@ -549,7 +589,7 @@
|
||||
if (!(tok = this.prev(index))) {
|
||||
return null;
|
||||
}
|
||||
if ((typeof val !== "undefined" && val !== null)) {
|
||||
if (typeof val !== "undefined" && val !== null) {
|
||||
return (tok[1] = val);
|
||||
}
|
||||
return tok[1];
|
||||
@@ -571,32 +611,37 @@
|
||||
};
|
||||
return Lexer;
|
||||
})();
|
||||
JS_KEYWORDS = ["if", "else", "true", "false", "new", "return", "try", "catch", "finally", "throw", "break", "continue", "for", "in", "while", "delete", "instanceof", "typeof", "switch", "super", "extends", "class", "this", "null"];
|
||||
JS_KEYWORDS = ["if", "else", "true", "false", "new", "return", "try", "catch", "finally", "throw", "break", "continue", "for", "in", "while", "delete", "instanceof", "typeof", "switch", "super", "extends", "class", "this", "null", "debugger"];
|
||||
COFFEE_ALIASES = ["and", "or", "is", "isnt", "not"];
|
||||
COFFEE_KEYWORDS = COFFEE_ALIASES.concat(["then", "unless", "until", "loop", "yes", "no", "on", "off", "of", "by", "where", "when"]);
|
||||
RESERVED = ["case", "default", "do", "function", "var", "void", "with", "const", "let", "enum", "export", "import", "native", "__hasProp", "__extends", "__slice"];
|
||||
JS_FORBIDDEN = JS_KEYWORDS.concat(RESERVED);
|
||||
IDENTIFIER = /^([a-zA-Z\$_](\w|\$)*)/;
|
||||
NUMBER = /^(((\b0(x|X)[0-9a-fA-F]+)|((\b[0-9]+(\.[0-9]+)?|\.[0-9]+)(e[+\-]?[0-9]+)?)))\b/i;
|
||||
HEREDOC = /^("{6}|'{6}|"{3}\n?([\s\S]*?)\n?([ \t]*)"{3}|'{3}\n?([\s\S]*?)\n?([ \t]*)'{3})/;
|
||||
OPERATOR = /^(-[\-=>]?|\+[+=]?|[*&|\/%=<>:!?]+)([ \t]*)/;
|
||||
HEREDOC = /^("{6}|'{6}|"{3}([\s\S]*?)\n?([ \t]*)"{3}|'{3}([\s\S]*?)\n?([ \t]*)'{3})/;
|
||||
OPERATOR = /^(-[\-=>]?|\+[+=]?|[*&|\/%=<>^:!?]+)([ \t]*)/;
|
||||
WHITESPACE = /^([ \t]+)/;
|
||||
COMMENT = /^(\s*\#{3}(?!#)[ \t]*\n+([\s\S]*?)[ \t]*\n+[ \t]*\#{3}|(\s*#(?!##[^#])[^\n]*)+)/;
|
||||
COMMENT = /^(([ \t]*\n)*([ \t]*)###([^#][\s\S]*?)(###[ \t]*\n|(###)?$)|(\s*#(?!##[^#])[^\n]*)+)/;
|
||||
CODE = /^((-|=)>)/;
|
||||
MULTI_DENT = /^((\n([ \t]*))+)(\.)?/;
|
||||
LAST_DENTS = /\n([ \t]*)/g;
|
||||
LAST_DENT = /\n([ \t]*)/;
|
||||
REGEX_START = /^\/[^\/ ]/;
|
||||
REGEX_START = /^\/([^\/])/;
|
||||
REGEX_INTERPOLATION = /([^\\]#\{.*[^\\]\})/;
|
||||
REGEX_END = /^(([imgy]{1,4})\b|\W|$)/;
|
||||
REGEX_ESCAPE = /\\[^\$]/g;
|
||||
JS_CLEANER = /(^`|`$)/g;
|
||||
MULTILINER = /\n/g;
|
||||
STRING_NEWLINES = /\n[ \t]*/g;
|
||||
NO_NEWLINE = /^([+\*&|\/\-%=<>!.\\][<>=&|]*|and|or|is|isnt|not|delete|typeof|instanceof)$/;
|
||||
HEREDOC_INDENT = /(\n+([ \t]*)|^([ \t]+))/g;
|
||||
ASSIGNED = /^\s*([a-zA-Z\$_@]\w*[ \t]*?[:=][^=])/;
|
||||
ASSIGNED = /^\s*(([a-zA-Z\$_@]\w*|["'][^\r\n]+?["']|\d+)[ \t]*?[:=][^:=])/;
|
||||
NEXT_CHARACTER = /^\s*(\S)/;
|
||||
COMPOUND_ASSIGN = ['-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?=', '<<=', '>>=', '>>>=', '&=', '^=', '|='];
|
||||
UNARY = ['UMINUS', 'UPLUS', '!', '!!', '~', 'TYPEOF', 'DELETE'];
|
||||
LOGIC = ['&', '|', '^', '&&', '||'];
|
||||
SHIFT = ['<<', '>>', '>>>'];
|
||||
COMPARE = ['<=', '<', '>', '>='];
|
||||
MATH = ['*', '/', '%'];
|
||||
NOT_REGEX = ['NUMBER', 'REGEX', '++', '--', 'FALSE', 'NULL', 'TRUE', ']'];
|
||||
CALLABLE = ['IDENTIFIER', 'SUPER', ')', ']', '}', 'STRING', '@', 'THIS', '?', '::'];
|
||||
LINE_BREAK = ['INDENT', 'OUTDENT', 'TERMINATOR'];
|
||||
|
||||
596
lib/nodes.js
596
lib/nodes.js
File diff suppressed because it is too large
Load Diff
@@ -28,7 +28,7 @@
|
||||
}
|
||||
}
|
||||
if (isOption && !matchedRule) {
|
||||
throw new Error(("unrecognized option: " + (arg)));
|
||||
throw new Error("unrecognized option: " + (arg));
|
||||
}
|
||||
if (!isOption) {
|
||||
options.arguments = args.slice(i, args.length);
|
||||
@@ -41,7 +41,7 @@
|
||||
var _a, _b, _c, _d, i, letPart, lines, rule, spaces;
|
||||
lines = ['Available options:'];
|
||||
if (this.banner) {
|
||||
lines.unshift(("" + (this.banner) + "\n"));
|
||||
lines.unshift("" + (this.banner) + "\n");
|
||||
}
|
||||
_b = this.rules;
|
||||
for (_a = 0, _c = _b.length; _a < _c; _a++) {
|
||||
@@ -83,7 +83,7 @@
|
||||
var match;
|
||||
match = longFlag.match(OPTIONAL);
|
||||
longFlag = longFlag.match(LONG_FLAG)[1];
|
||||
options = options || {};
|
||||
options || (options = {});
|
||||
return {
|
||||
name: longFlag.substr(2),
|
||||
shortFlag: shortFlag,
|
||||
@@ -100,7 +100,7 @@
|
||||
_b = args;
|
||||
for (_a = 0, _c = _b.length; _a < _c; _a++) {
|
||||
arg = _b[_a];
|
||||
if ((match = arg.match(MULTI_FLAG))) {
|
||||
if (match = arg.match(MULTI_FLAG)) {
|
||||
_e = match[1].split('');
|
||||
for (_d = 0, _f = _e.length; _d < _f; _d++) {
|
||||
l = _e[_d];
|
||||
|
||||
290
lib/parser.js
290
lib/parser.js
File diff suppressed because one or more lines are too long
148
lib/rewriter.js
148
lib/rewriter.js
@@ -1,9 +1,7 @@
|
||||
(function() {
|
||||
var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, helpers, include, pair;
|
||||
var __bind = function(func, context) {
|
||||
return function(){ return func.apply(context, arguments); };
|
||||
}, __hasProp = Object.prototype.hasOwnProperty;
|
||||
if ((typeof process !== "undefined" && process !== null)) {
|
||||
var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, LINEBREAKS, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, helpers, include, pair;
|
||||
var __hasProp = Object.prototype.hasOwnProperty;
|
||||
if (typeof process !== "undefined" && process !== null) {
|
||||
_a = require('./helpers');
|
||||
helpers = _a.helpers;
|
||||
} else {
|
||||
@@ -22,6 +20,7 @@
|
||||
this.closeOpenCalls();
|
||||
this.closeOpenIndexes();
|
||||
this.addImplicitIndentation();
|
||||
this.tagPostfixConditionals();
|
||||
this.addImplicitBraces();
|
||||
this.addImplicitParentheses();
|
||||
this.ensureBalance(BALANCED_PAIRS);
|
||||
@@ -35,7 +34,7 @@
|
||||
if (!(this.tokens[i])) {
|
||||
break;
|
||||
}
|
||||
move = block(this.tokens[i], i);
|
||||
move = block.call(this, this.tokens[i], i);
|
||||
i += move;
|
||||
}
|
||||
return true;
|
||||
@@ -62,9 +61,9 @@
|
||||
return i - 1;
|
||||
};
|
||||
Rewriter.prototype.adjustComments = function() {
|
||||
return this.scanTokens(__bind(function(token, i) {
|
||||
return this.scanTokens(function(token, i) {
|
||||
var _c, _d, after, before, post, prev;
|
||||
if (!(token[0] === 'HERECOMMENT')) {
|
||||
if (token[0] !== 'HERECOMMENT') {
|
||||
return 1;
|
||||
}
|
||||
_c = [this.tokens[i - 2], this.tokens[i - 1], this.tokens[i + 1], this.tokens[i + 2]];
|
||||
@@ -78,20 +77,20 @@
|
||||
this.tokens.splice(i - 2, 1);
|
||||
} else {
|
||||
this.tokens.splice(i, 0, after);
|
||||
};
|
||||
}
|
||||
} else if (prev && !('TERMINATOR' === (_d = prev[0]) || 'INDENT' === _d || 'OUTDENT' === _d)) {
|
||||
if (post && post[0] === 'TERMINATOR' && after && after[0] === 'OUTDENT') {
|
||||
this.tokens.splice.apply(this.tokens, [i + 2, 0].concat(this.tokens.splice(i, 2)));
|
||||
if (this.tokens[i + 2][0] !== 'TERMINATOR') {
|
||||
this.tokens.splice(i + 2, 0, ['TERMINATOR', "\n", prev[2]]);
|
||||
};
|
||||
}
|
||||
} else {
|
||||
this.tokens.splice(i, 0, ['TERMINATOR', "\n", prev[2]]);
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
return 1;
|
||||
}, this));
|
||||
});
|
||||
};
|
||||
Rewriter.prototype.removeLeadingNewlines = function() {
|
||||
var _c;
|
||||
@@ -102,32 +101,34 @@
|
||||
return _c;
|
||||
};
|
||||
Rewriter.prototype.removeMidExpressionNewlines = function() {
|
||||
return this.scanTokens(__bind(function(token, i) {
|
||||
return this.scanTokens(function(token, i) {
|
||||
if (!(include(EXPRESSION_CLOSE, this.tag(i + 1)) && token[0] === 'TERMINATOR')) {
|
||||
return 1;
|
||||
}
|
||||
this.tokens.splice(i, 1);
|
||||
return 0;
|
||||
}, this));
|
||||
});
|
||||
};
|
||||
Rewriter.prototype.closeOpenCalls = function() {
|
||||
return this.scanTokens(__bind(function(token, i) {
|
||||
return this.scanTokens(function(token, i) {
|
||||
var action, condition;
|
||||
if (token[0] === 'CALL_START') {
|
||||
condition = function(token, i) {
|
||||
var _c;
|
||||
return (')' === (_c = token[0]) || 'CALL_END' === _c);
|
||||
return ((')' === (_c = token[0]) || 'CALL_END' === _c)) || (token[0] === 'OUTDENT' && this.tokens[i - 1][0] === ')');
|
||||
};
|
||||
action = function(token, i) {
|
||||
return (token[0] = 'CALL_END');
|
||||
var idx;
|
||||
idx = token[0] === 'OUTDENT' ? i - 1 : i;
|
||||
return (this.tokens[idx][0] = 'CALL_END');
|
||||
};
|
||||
this.detectEnd(i + 1, condition, action);
|
||||
}
|
||||
return 1;
|
||||
}, this));
|
||||
});
|
||||
};
|
||||
Rewriter.prototype.closeOpenIndexes = function() {
|
||||
return this.scanTokens(__bind(function(token, i) {
|
||||
return this.scanTokens(function(token, i) {
|
||||
var action, condition;
|
||||
if (token[0] === 'INDEX_START') {
|
||||
condition = function(token, i) {
|
||||
@@ -140,24 +141,26 @@
|
||||
this.detectEnd(i + 1, condition, action);
|
||||
}
|
||||
return 1;
|
||||
}, this));
|
||||
});
|
||||
};
|
||||
Rewriter.prototype.addImplicitBraces = function() {
|
||||
var stack;
|
||||
stack = [];
|
||||
return this.scanTokens(__bind(function(token, i) {
|
||||
var action, condition, idx, last;
|
||||
return this.scanTokens(function(token, i) {
|
||||
var action, condition, idx, last, tok;
|
||||
if (include(EXPRESSION_START, token[0])) {
|
||||
stack.push((token[0] === 'INDENT' && (this.tag(i - 1) === '{')) ? '{' : token[0]);
|
||||
};
|
||||
}
|
||||
if (include(EXPRESSION_END, token[0])) {
|
||||
stack.pop();
|
||||
};
|
||||
}
|
||||
last = stack[stack.length - 1];
|
||||
if (token[0] === ':' && (!last || last[0] !== '{')) {
|
||||
stack.push('{');
|
||||
idx = this.tag(i - 2) === '@' ? i - 2 : i - 1;
|
||||
this.tokens.splice(idx, 0, ['{', '{', token[2]]);
|
||||
tok = ['{', '{', token[2]];
|
||||
tok.generated = true;
|
||||
this.tokens.splice(idx, 0, tok);
|
||||
condition = function(token, i) {
|
||||
var _c, _d, _e, one, three, two;
|
||||
_c = this.tokens.slice(i + 1, i + 4);
|
||||
@@ -176,36 +179,63 @@
|
||||
return 2;
|
||||
}
|
||||
return 1;
|
||||
}, this));
|
||||
});
|
||||
};
|
||||
Rewriter.prototype.addImplicitParentheses = function() {
|
||||
return this.scanTokens(__bind(function(token, i) {
|
||||
var _c, action, condition, prev;
|
||||
var classLine;
|
||||
classLine = false;
|
||||
return this.scanTokens(function(token, i) {
|
||||
var _c, action, callObject, condition, idx, next, prev, seenSingle;
|
||||
if (token[0] === 'CLASS') {
|
||||
classLine = true;
|
||||
}
|
||||
prev = this.tokens[i - 1];
|
||||
if (prev && prev.spaced && include(IMPLICIT_FUNC, prev[0]) && include(IMPLICIT_CALL, token[0]) && !(token[0] === '!' && (('IN' === (_c = this.tag(i + 1)) || 'OF' === _c)))) {
|
||||
next = this.tokens[i + 1];
|
||||
idx = 1;
|
||||
callObject = !classLine && token[0] === 'INDENT' && next && next.generated && next[0] === '{' && prev && include(IMPLICIT_FUNC, prev[0]);
|
||||
if (callObject) {
|
||||
idx = 2;
|
||||
}
|
||||
seenSingle = false;
|
||||
if (include(LINEBREAKS, token[0])) {
|
||||
classLine = false;
|
||||
}
|
||||
if (prev && !prev.spaced && token[0] === '?') {
|
||||
token.call = true;
|
||||
}
|
||||
if (prev && (prev.spaced && (include(IMPLICIT_FUNC, prev[0]) || prev.call) && include(IMPLICIT_CALL, token[0]) && !(token[0] === 'UNARY' && (('IN' === (_c = this.tag(i + 1)) || 'OF' === _c || 'INSTANCEOF' === _c)))) || callObject) {
|
||||
this.tokens.splice(i, 0, ['CALL_START', '(', token[2]]);
|
||||
condition = function(token, i) {
|
||||
return (!token.generated && this.tokens[i - 1][0] !== ',' && include(IMPLICIT_END, token[0]) && !(token[0] === 'INDENT' && (include(IMPLICIT_BLOCK, this.tag(i - 1)) || this.tag(i - 2) === 'CLASS'))) || token[0] === 'PROPERTY_ACCESS' && this.tag(i - 1) === 'OUTDENT';
|
||||
var _c;
|
||||
if (!seenSingle && token.fromThen) {
|
||||
return true;
|
||||
}
|
||||
if (('IF' === (_c = token[0]) || 'ELSE' === _c || 'UNLESS' === _c || '->' === _c || '=>' === _c)) {
|
||||
seenSingle = true;
|
||||
}
|
||||
return (!token.generated && this.tokens[i - 1][0] !== ',' && include(IMPLICIT_END, token[0]) && !(token[0] === 'INDENT' && (include(IMPLICIT_BLOCK, this.tag(i - 1)) || this.tag(i - 2) === 'CLASS' || this.tag(i + 1) === '{'))) || token[0] === 'PROPERTY_ACCESS' && this.tag(i - 1) === 'OUTDENT';
|
||||
};
|
||||
action = function(token, i) {
|
||||
var idx;
|
||||
idx = token[0] === 'OUTDENT' ? i + 1 : i;
|
||||
return this.tokens.splice(idx, 0, ['CALL_END', ')', token[2]]);
|
||||
};
|
||||
this.detectEnd(i + 1, condition, action);
|
||||
this.detectEnd(i + idx, condition, action);
|
||||
if (prev[0] === '?') {
|
||||
prev[0] = 'FUNC_EXIST';
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
return 1;
|
||||
}, this));
|
||||
});
|
||||
};
|
||||
Rewriter.prototype.addImplicitIndentation = function() {
|
||||
return this.scanTokens(__bind(function(token, i) {
|
||||
return this.scanTokens(function(token, i) {
|
||||
var _c, action, condition, indent, outdent, starter;
|
||||
if (token[0] === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') {
|
||||
this.tokens.splice.apply(this.tokens, [i, 0].concat(this.indentation(token)));
|
||||
return 2;
|
||||
}
|
||||
if (token[0] === 'CATCH' && (this.tokens[i + 2][0] === 'TERMINATOR' || this.tokens[i + 2][0] === 'FINALLY')) {
|
||||
if (token[0] === 'CATCH' && (this.tag(i + 2) === 'TERMINATOR' || this.tag(i + 2) === 'FINALLY')) {
|
||||
this.tokens.splice.apply(this.tokens, [i + 2, 0].concat(this.indentation(token)));
|
||||
return 4;
|
||||
}
|
||||
@@ -214,6 +244,9 @@
|
||||
_c = this.indentation(token);
|
||||
indent = _c[0];
|
||||
outdent = _c[1];
|
||||
if (starter === 'THEN') {
|
||||
indent.fromThen = true;
|
||||
}
|
||||
indent.generated = (outdent.generated = true);
|
||||
this.tokens.splice(i + 1, 0, indent);
|
||||
condition = function(token, i) {
|
||||
@@ -231,13 +264,33 @@
|
||||
return 2;
|
||||
}
|
||||
return 1;
|
||||
}, this));
|
||||
});
|
||||
};
|
||||
Rewriter.prototype.tagPostfixConditionals = function() {
|
||||
return this.scanTokens(function(token, i) {
|
||||
var _c, action, condition, original;
|
||||
if (('IF' === (_c = token[0]) || 'UNLESS' === _c)) {
|
||||
original = token;
|
||||
condition = function(token, i) {
|
||||
var _c;
|
||||
return ('TERMINATOR' === (_c = token[0]) || 'INDENT' === _c);
|
||||
};
|
||||
action = function(token, i) {
|
||||
if (token[0] !== 'INDENT') {
|
||||
return (original[0] = 'POST_' + original[0]);
|
||||
}
|
||||
};
|
||||
this.detectEnd(i + 1, condition, action);
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
};
|
||||
Rewriter.prototype.ensureBalance = function(pairs) {
|
||||
var _c, _d, key, levels, line, open, openLine, unclosed, value;
|
||||
levels = {};
|
||||
openLine = {};
|
||||
this.scanTokens(__bind(function(token, i) {
|
||||
this.scanTokens(function(token, i) {
|
||||
var _c, _d, _e, _f, close, open, pair;
|
||||
_d = pairs;
|
||||
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
||||
@@ -245,7 +298,7 @@
|
||||
_f = pair;
|
||||
open = _f[0];
|
||||
close = _f[1];
|
||||
levels[open] = levels[open] || 0;
|
||||
levels[open] || (levels[open] = 0);
|
||||
if (token[0] === open) {
|
||||
if (levels[open] === 0) {
|
||||
openLine[open] = token[2];
|
||||
@@ -256,11 +309,11 @@
|
||||
levels[open] -= 1;
|
||||
}
|
||||
if (levels[open] < 0) {
|
||||
throw new Error(("too many " + (token[1]) + " on line " + (token[2] + 1)));
|
||||
throw new Error("too many " + (token[1]) + " on line " + (token[2] + 1));
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}, this));
|
||||
});
|
||||
unclosed = (function() {
|
||||
_c = []; _d = levels;
|
||||
for (key in _d) {
|
||||
@@ -268,14 +321,14 @@
|
||||
value = _d[key];
|
||||
if (value > 0) {
|
||||
_c.push(key);
|
||||
};
|
||||
}
|
||||
}
|
||||
return _c;
|
||||
})();
|
||||
if (unclosed.length) {
|
||||
open = unclosed[0];
|
||||
line = openLine[open] + 1;
|
||||
throw new Error(("unclosed " + (open) + " on line " + (line)));
|
||||
throw new Error("unclosed " + (open) + " on line " + (line));
|
||||
}
|
||||
};
|
||||
Rewriter.prototype.rewriteClosingParens = function() {
|
||||
@@ -288,7 +341,7 @@
|
||||
val = _c[key];
|
||||
(debt[key] = 0);
|
||||
}
|
||||
return this.scanTokens(__bind(function(token, i) {
|
||||
return this.scanTokens(function(token, i) {
|
||||
var inv, match, mtag, oppos, tag;
|
||||
tag = token[0];
|
||||
inv = INVERSES[token[0]];
|
||||
@@ -309,7 +362,7 @@
|
||||
}
|
||||
debt[mtag] += 1;
|
||||
val = [oppos, mtag === 'INDENT' ? match[1] : oppos];
|
||||
if ((this.tokens[i + 2] == undefined ? undefined : this.tokens[i + 2][0]) === mtag) {
|
||||
if ((this.tokens[i + 2] == null ? undefined : this.tokens[i + 2][0]) === mtag) {
|
||||
this.tokens.splice(i + 3, 0, val);
|
||||
stack.push(match);
|
||||
} else {
|
||||
@@ -320,7 +373,7 @@
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}, this));
|
||||
});
|
||||
};
|
||||
Rewriter.prototype.indentation = function(token) {
|
||||
return [['INDENT', 2, token[2]], ['OUTDENT', 2, token[2]]];
|
||||
@@ -355,10 +408,11 @@
|
||||
return _j;
|
||||
})();
|
||||
EXPRESSION_CLOSE = ['CATCH', 'WHEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_END);
|
||||
IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@'];
|
||||
IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS', 'TRY', 'DELETE', 'TYPEOF', 'SWITCH', 'THIS', 'NULL', 'TRUE', 'FALSE', 'YES', 'NO', 'ON', 'OFF', '!', '!!', '@', '->', '=>', '[', '(', '{'];
|
||||
IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS'];
|
||||
IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'UNLESS', 'TRY', 'SWITCH', 'THIS', 'NULL', 'UNARY', 'TRUE', 'FALSE', 'YES', 'NO', 'ON', 'OFF', '@', '->', '=>', '[', '(', '{'];
|
||||
IMPLICIT_BLOCK = ['->', '=>', '{', '[', ','];
|
||||
IMPLICIT_END = ['IF', 'UNLESS', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'TERMINATOR', 'INDENT'];
|
||||
IMPLICIT_END = ['POST_IF', 'POST_UNLESS', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'TERMINATOR', 'INDENT'];
|
||||
SINGLE_LINERS = ['ELSE', "->", "=>", 'TRY', 'FINALLY', 'THEN'];
|
||||
SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN'];
|
||||
LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT'];
|
||||
})();
|
||||
|
||||
20
lib/scope.js
20
lib/scope.js
@@ -1,7 +1,7 @@
|
||||
(function() {
|
||||
var Scope;
|
||||
var __hasProp = Object.prototype.hasOwnProperty;
|
||||
if (!((typeof process !== "undefined" && process !== null))) {
|
||||
if (!(typeof process !== "undefined" && process !== null)) {
|
||||
this.exports = this;
|
||||
}
|
||||
exports.Scope = (function() {
|
||||
@@ -21,8 +21,8 @@
|
||||
return this;
|
||||
};
|
||||
Scope.root = null;
|
||||
Scope.prototype.find = function(name) {
|
||||
if (this.check(name)) {
|
||||
Scope.prototype.find = function(name, options) {
|
||||
if (this.check(name, options)) {
|
||||
return true;
|
||||
}
|
||||
this.variables[name] = 'var';
|
||||
@@ -43,9 +43,11 @@
|
||||
Scope.prototype.parameter = function(name) {
|
||||
return (this.variables[name] = 'param');
|
||||
};
|
||||
Scope.prototype.check = function(name) {
|
||||
if (this.variables.hasOwnProperty(name)) {
|
||||
return true;
|
||||
Scope.prototype.check = function(name, options) {
|
||||
var immediate;
|
||||
immediate = Object.prototype.hasOwnProperty.call(this.variables, name);
|
||||
if (immediate || (options && options.immediate)) {
|
||||
return immediate;
|
||||
}
|
||||
return !!(this.parent && this.parent.check(name));
|
||||
};
|
||||
@@ -83,7 +85,7 @@
|
||||
val = _b[key];
|
||||
if (val === 'var') {
|
||||
_a.push(key);
|
||||
};
|
||||
}
|
||||
}
|
||||
return _a;
|
||||
}).call(this).sort();
|
||||
@@ -95,8 +97,8 @@
|
||||
if (!__hasProp.call(_b, key)) continue;
|
||||
val = _b[key];
|
||||
if (val.assigned) {
|
||||
_a.push(("" + (key) + " = " + (val.value)));
|
||||
};
|
||||
_a.push("" + (key) + " = " + (val.value));
|
||||
}
|
||||
}
|
||||
return _a;
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"description": "Unfancy JavaScript",
|
||||
"keywords": ["javascript", "language", "coffeescript", "compiler"],
|
||||
"author": "Jeremy Ashkenas",
|
||||
"version": "0.9.1",
|
||||
"version": "0.9.3",
|
||||
"licenses": [{
|
||||
"type": "MIT",
|
||||
"url": "http://github.com/jashkenas/coffee-script/raw/master/LICENSE"
|
||||
@@ -11,6 +11,10 @@
|
||||
"engines": {
|
||||
"node": ">=0.1.99"
|
||||
},
|
||||
"directories" : {
|
||||
"lib" : "./lib"
|
||||
},
|
||||
"main" : "./lib/coffee-script",
|
||||
"bin": {
|
||||
"coffee": "./bin/coffee",
|
||||
"cake": "./bin/cake"
|
||||
|
||||
25
src/browser.coffee
Normal file
25
src/browser.coffee
Normal file
@@ -0,0 +1,25 @@
|
||||
# Activate CoffeeScript in the browser by having it compile and evaluate
|
||||
# all script tags with a content-type of `text/coffeescript`.
|
||||
# This happens on page load.
|
||||
if document?.getElementsByTagName
|
||||
grind = (coffee) ->
|
||||
setTimeout exports.compile coffee
|
||||
grindRemote = (url) ->
|
||||
xhr = new (window.ActiveXObject or XMLHttpRequest)('Microsoft.XMLHTTP')
|
||||
xhr.open 'GET', url, true
|
||||
xhr.overrideMimeType 'text/plain' if 'overrideMimeType' of xhr
|
||||
xhr.onreadystatechange = ->
|
||||
grind xhr.responseText if xhr.readyState is 4
|
||||
xhr.send null
|
||||
processScripts = ->
|
||||
for script in document.getElementsByTagName 'script'
|
||||
if script.type is 'text/coffeescript'
|
||||
if script.src
|
||||
grindRemote script.src
|
||||
else
|
||||
grind script.innerHTML
|
||||
null
|
||||
if window.addEventListener
|
||||
addEventListener 'DOMContentLoaded', processScripts, false
|
||||
else
|
||||
attachEvent 'onload', processScripts
|
||||
24
src/coffee-script.coffee
Normal file → Executable file
24
src/coffee-script.coffee
Normal file → Executable file
@@ -22,10 +22,7 @@ else
|
||||
helpers = this.helpers
|
||||
|
||||
# The current CoffeeScript version number.
|
||||
exports.VERSION = '0.9.1'
|
||||
|
||||
# Instantiate a Lexer for our use here.
|
||||
lexer = new Lexer
|
||||
exports.VERSION = '0.9.3'
|
||||
|
||||
# Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison
|
||||
# compiler.
|
||||
@@ -49,11 +46,13 @@ exports.nodes = (code) ->
|
||||
|
||||
# Compile and execute a string of CoffeeScript (on the server), correctly
|
||||
# setting `__filename`, `__dirname`, and relative `require()`.
|
||||
exports.run = ((code, options) ->
|
||||
exports.run = (code, options) ->
|
||||
module.filename = __filename = options.fileName
|
||||
__dirname = path.dirname __filename
|
||||
eval exports.compile code, options
|
||||
)
|
||||
|
||||
# Instantiate a Lexer for our use here.
|
||||
lexer = new Lexer
|
||||
|
||||
# The real Lexer produces a generic stream of tokens. This object provides a
|
||||
# thin wrapper around it, compatible with the Jison API. We can then pass it
|
||||
@@ -69,16 +68,3 @@ parser.lexer =
|
||||
@tokens = tokens
|
||||
@pos = 0
|
||||
upcomingInput: -> ""
|
||||
|
||||
# Activate CoffeeScript in the browser by having it compile and evaluate
|
||||
# all script tags with a content-type of `text/coffeescript`. This happens
|
||||
# on page load. Unfortunately, the text contents of remote scripts cannot be
|
||||
# accessed from the browser, so only inline script tags will work.
|
||||
if document? and document.getElementsByTagName
|
||||
processScripts = ->
|
||||
for tag in document.getElementsByTagName('script') when tag.type is 'text/coffeescript'
|
||||
eval exports.compile tag.innerHTML
|
||||
if window.addEventListener
|
||||
window.addEventListener 'load', processScripts, false
|
||||
else if window.attachEvent
|
||||
window.attachEvent 'onload', processScripts
|
||||
|
||||
@@ -44,7 +44,7 @@ SWITCHES = [
|
||||
]
|
||||
|
||||
# Top-level objects shared by all the functions.
|
||||
options = {}
|
||||
opts = {}
|
||||
sources = []
|
||||
optionParser = null
|
||||
|
||||
@@ -53,18 +53,18 @@ optionParser = null
|
||||
# `--` will be passed verbatim to your script as arguments in `process.argv`
|
||||
exports.run = ->
|
||||
parseOptions()
|
||||
return usage() if options.help
|
||||
return version() if options.version
|
||||
return require './repl' if options.interactive
|
||||
return compileStdio() if options.stdio
|
||||
return compileScript 'console', sources[0] if options.eval
|
||||
return usage() if opts.help
|
||||
return version() if opts.version
|
||||
return require './repl' if opts.interactive
|
||||
return compileStdio() if opts.stdio
|
||||
return compileScript 'console', sources[0] if opts.eval
|
||||
return require './repl' unless sources.length
|
||||
separator = sources.indexOf '--'
|
||||
flags = []
|
||||
if separator >= 0
|
||||
flags = sources[(separator + 1)...sources.length]
|
||||
sources = sources[0...separator]
|
||||
if options.run
|
||||
if opts.run
|
||||
flags = sources[1..sources.length].concat flags
|
||||
sources = [sources[0]]
|
||||
process.ARGV = process.argv = flags
|
||||
@@ -86,35 +86,37 @@ compileScripts = ->
|
||||
compile path.join(source, file)
|
||||
else if topLevel or path.extname(source) is '.coffee'
|
||||
fs.readFile source, (err, code) -> compileScript(source, code.toString(), base)
|
||||
watch source, base if options.watch
|
||||
watch source, base if opts.watch
|
||||
compile source, true
|
||||
|
||||
# Compile a single source script, containing the given code, according to the
|
||||
# requested options. If evaluating the script directly sets `__filename`,
|
||||
# `__dirname` and `module.filename` to be correct relative to the script's path.
|
||||
compileScript = (source, code, base) ->
|
||||
o = options
|
||||
codeOpts = compileOptions source
|
||||
compileScript = (file, input, base) ->
|
||||
o = opts
|
||||
options = compileOptions file
|
||||
if o.require
|
||||
require fs.realpathSync file for file in o.require
|
||||
require(if helpers.starts(req, '.') then fs.realpathSync(req) else req) for req in o.require
|
||||
try
|
||||
CoffeeScript.emit 'compile', {source, code, base, options}
|
||||
if o.tokens then printTokens CoffeeScript.tokens code
|
||||
else if o.nodes then puts CoffeeScript.nodes(code).toString()
|
||||
else if o.run then CoffeeScript.run code, codeOpts
|
||||
t = task = {file, input, options}
|
||||
CoffeeScript.emit 'compile', task
|
||||
if o.tokens then printTokens CoffeeScript.tokens t.input
|
||||
else if o.nodes then puts CoffeeScript.nodes(t.input).toString()
|
||||
else if o.run then CoffeeScript.run t.input, t.options
|
||||
else
|
||||
js = CoffeeScript.compile code, codeOpts
|
||||
CoffeeScript.emit 'success', js
|
||||
if o.print then print js
|
||||
else if o.compile then writeJs source, js, base
|
||||
else if o.lint then lint js
|
||||
t.output = CoffeeScript.compile t.input, t.options
|
||||
CoffeeScript.emit 'success', task
|
||||
if o.print then print t.output
|
||||
else if o.compile then writeJs t.file, t.output, base
|
||||
else if o.lint then lint t.output
|
||||
catch err
|
||||
# Avoid using 'error' as it is a special event -- if there is no handler,
|
||||
# node will print a stack trace and exit the program.
|
||||
CoffeeScript.emit 'failure', err
|
||||
CoffeeScript.emit 'failure', err, task
|
||||
return if CoffeeScript.listeners('failure').length
|
||||
error(err.stack) and process.exit 1 unless o.watch
|
||||
puts err.message
|
||||
return puts err.message if o.watch
|
||||
error err.stack
|
||||
process.exit 1
|
||||
|
||||
# Attach the appropriate listeners to compile scripts incoming over **stdin**,
|
||||
# and write them back to **stdout**.
|
||||
@@ -131,8 +133,10 @@ compileStdio = ->
|
||||
# such as `--lint` or `--print`.
|
||||
watch = (source, base) ->
|
||||
fs.watchFile source, {persistent: true, interval: 500}, (curr, prev) ->
|
||||
return if curr.mtime.getTime() is prev.mtime.getTime()
|
||||
fs.readFile source, (err, code) -> compileScript(source, code.toString(), base)
|
||||
return if curr.size is prev.size and curr.mtime.getTime() is prev.mtime.getTime()
|
||||
fs.readFile source, (err, code) ->
|
||||
throw err if err
|
||||
compileScript(source, code.toString(), base)
|
||||
|
||||
# Write out a JavaScript source file with the compiled code. By default, files
|
||||
# are written out in `cwd` as `.js` files with the same name, but the output
|
||||
@@ -141,20 +145,21 @@ writeJs = (source, js, base) ->
|
||||
filename = path.basename(source, path.extname(source)) + '.js'
|
||||
srcDir = path.dirname source
|
||||
baseDir = srcDir.substring base.length
|
||||
dir = if options.output then path.join options.output, baseDir else srcDir
|
||||
dir = if opts.output then path.join opts.output, baseDir else srcDir
|
||||
jsPath = path.join dir, filename
|
||||
compile = ->
|
||||
js = ' ' if js.length <= 0
|
||||
fs.writeFile jsPath, js, (err) ->
|
||||
puts "Compiled #{source}" if options.compile and options.watch
|
||||
puts "Compiled #{source}" if opts.compile and opts.watch
|
||||
path.exists dir, (exists) ->
|
||||
if exists then compile() else exec "mkdir -p #{dir}", compile
|
||||
|
||||
# Pipe compiled JS through JSLint (requires a working `jsl` command), printing
|
||||
# any errors or warnings that arise.
|
||||
lint = (js) ->
|
||||
printIt = (buffer) -> print buffer.toString()
|
||||
jsl = spawn 'jsl', ['-nologo', '-stdin']
|
||||
printIt = (buffer) -> puts buffer.toString().trim()
|
||||
conf = __dirname + '/../extras/jsl.conf'
|
||||
jsl = spawn 'jsl', ['-nologo', '-stdin', '-conf', conf]
|
||||
jsl.stdout.on 'data', printIt
|
||||
jsl.stderr.on 'data', printIt
|
||||
jsl.stdin.write js
|
||||
@@ -170,17 +175,17 @@ printTokens = (tokens) ->
|
||||
# Use the [OptionParser module](optparse.html) to extract all options from
|
||||
# `process.argv` that are specified in `SWITCHES`.
|
||||
parseOptions = ->
|
||||
optionParser = new optparse.OptionParser SWITCHES, BANNER
|
||||
o = options = optionParser.parse(process.argv[2...process.argv.length])
|
||||
options.compile or= !!o.output
|
||||
options.run = not (o.compile or o.print or o.lint)
|
||||
options.print = !! (o.print or (o.eval or o.stdio and o.compile))
|
||||
sources = options.arguments
|
||||
optionParser = new optparse.OptionParser SWITCHES, BANNER
|
||||
o = opts = optionParser.parse(process.argv[2...process.argv.length])
|
||||
o.compile or= !!o.output
|
||||
o.run = not (o.compile or o.print or o.lint)
|
||||
o.print = !! (o.print or (o.eval or o.stdio and o.compile))
|
||||
sources = o.arguments
|
||||
|
||||
# The compile-time options to pass to the CoffeeScript compiler.
|
||||
compileOptions = (fileName) ->
|
||||
o = {fileName}
|
||||
o.noWrap = options['no-wrap']
|
||||
o.noWrap = opts['no-wrap']
|
||||
o
|
||||
|
||||
# Print the `--help` usage message and exit.
|
||||
|
||||
@@ -78,6 +78,7 @@ grammar =
|
||||
o "Throw"
|
||||
o "BREAK", -> new LiteralNode $1
|
||||
o "CONTINUE", -> new LiteralNode $1
|
||||
o "DEBUGGER", -> new LiteralNode $1
|
||||
]
|
||||
|
||||
# All the different types of expressions in our language. The basic unit of
|
||||
@@ -97,7 +98,6 @@ grammar =
|
||||
o "Switch"
|
||||
o "Extends"
|
||||
o "Class"
|
||||
o "Splat"
|
||||
o "Existence"
|
||||
o "Comment"
|
||||
]
|
||||
@@ -296,11 +296,9 @@ grammar =
|
||||
o "{ ClassBody }", -> $2
|
||||
]
|
||||
|
||||
# The three flavors of function call: normal, object instantiation with `new`,
|
||||
# and calling `super()`
|
||||
# The two flavors of function call: normal, and object instantiation with `new`.
|
||||
Call: [
|
||||
o "Invocation"
|
||||
o "Super"
|
||||
o "NEW Invocation", -> $2.newInstance()
|
||||
o "NEW Value", -> (new CallNode($2, [])).newInstance()
|
||||
]
|
||||
@@ -313,27 +311,35 @@ grammar =
|
||||
|
||||
# Ordinary function invocation, or a chained series of calls.
|
||||
Invocation: [
|
||||
o "Value Arguments", -> new CallNode $1, $2
|
||||
o "Invocation Arguments", -> new CallNode $1, $2
|
||||
o "Value OptFuncExist Arguments", -> new CallNode $1, $3, $2
|
||||
o "Invocation OptFuncExist Arguments", -> new CallNode $1, $3, $2
|
||||
o "SUPER", -> new CallNode 'super', [new SplatNode(new LiteralNode('arguments'))]
|
||||
o "SUPER Arguments", -> new CallNode 'super', $2
|
||||
]
|
||||
|
||||
# An optional existence check on a function.
|
||||
OptFuncExist: [
|
||||
o "", -> no
|
||||
o "FUNC_EXIST", -> yes
|
||||
]
|
||||
|
||||
# The list of arguments to a function call.
|
||||
Arguments: [
|
||||
o "CALL_START CALL_END", -> []
|
||||
o "CALL_START ArgList OptComma CALL_END", -> $2
|
||||
]
|
||||
|
||||
# Calling super.
|
||||
Super: [
|
||||
o "SUPER", -> new CallNode 'super', [new SplatNode(new LiteralNode('arguments'))]
|
||||
o "SUPER Arguments", -> new CallNode 'super', $2
|
||||
]
|
||||
|
||||
# A reference to the *this* current object.
|
||||
This: [
|
||||
o "THIS", -> new ValueNode new LiteralNode 'this'
|
||||
o "@", -> new ValueNode new LiteralNode 'this'
|
||||
]
|
||||
|
||||
RangeDots: [
|
||||
o ". .", -> 'inclusive'
|
||||
o ". . .", -> 'exclusive'
|
||||
]
|
||||
|
||||
# A reference to a property on *this*.
|
||||
ThisProperty: [
|
||||
o "@ Identifier", -> new ValueNode new LiteralNode('this'), [new AccessorNode($2)]
|
||||
@@ -341,18 +347,19 @@ grammar =
|
||||
|
||||
# The CoffeeScript range literal.
|
||||
Range: [
|
||||
o "[ Expression . . Expression ]", -> new RangeNode $2, $5
|
||||
o "[ Expression . . . Expression ]", -> new RangeNode $2, $6, true
|
||||
o "[ Expression RangeDots Expression ]", -> new RangeNode $2, $4, $3
|
||||
]
|
||||
|
||||
# The slice literal.
|
||||
Slice: [
|
||||
o "INDEX_START Expression . . Expression INDEX_END", -> new RangeNode $2, $5
|
||||
o "INDEX_START Expression . . . Expression INDEX_END", -> new RangeNode $2, $6, true
|
||||
o "INDEX_START Expression RangeDots Expression INDEX_END", -> new RangeNode $2, $4, $3
|
||||
o "INDEX_START Expression RangeDots INDEX_END", -> new RangeNode $2, null, $3
|
||||
o "INDEX_START RangeDots Expression INDEX_END", -> new RangeNode null, $3, $2
|
||||
]
|
||||
|
||||
# The array literal.
|
||||
Array: [
|
||||
o "[ ]", -> new ArrayNode []
|
||||
o "[ ArgList OptComma ]", -> new ArrayNode $2
|
||||
]
|
||||
|
||||
@@ -360,13 +367,19 @@ grammar =
|
||||
# as well as the contents of an array literal
|
||||
# (i.e. comma-separated expressions). Newlines work as well.
|
||||
ArgList: [
|
||||
o "", -> []
|
||||
o "Expression", -> [$1]
|
||||
o "ArgList , Expression", -> $1.concat [$3]
|
||||
o "ArgList OptComma TERMINATOR Expression", -> $1.concat [$4]
|
||||
o "Arg", -> [$1]
|
||||
o "ArgList , Arg", -> $1.concat [$3]
|
||||
o "ArgList OptComma TERMINATOR Arg", -> $1.concat [$4]
|
||||
o "INDENT ArgList OptComma OUTDENT", -> $2
|
||||
o "ArgList OptComma INDENT ArgList OptComma OUTDENT", -> $1.concat $4
|
||||
]
|
||||
|
||||
# Valid arguments are Expressions or Splats.
|
||||
Arg: [
|
||||
o "Expression"
|
||||
o "Splat"
|
||||
]
|
||||
|
||||
# Just simple, comma-separated, required arguments (no fancy syntax). We need
|
||||
# this to be separate from the **ArgList** for use in **Switch** blocks, where
|
||||
# having the newlines wouldn't make sense.
|
||||
@@ -399,6 +412,7 @@ grammar =
|
||||
# the trick.
|
||||
Parenthetical: [
|
||||
o "( Line )", -> new ParentheticalNode $2
|
||||
o "( )", -> new ParentheticalNode new LiteralNode ''
|
||||
]
|
||||
|
||||
# The condition portion of a while loop.
|
||||
@@ -471,26 +485,22 @@ grammar =
|
||||
o "IN Expression BY Expression WHEN Expression", -> source: $2, step: $4, guard: $6
|
||||
]
|
||||
|
||||
# The CoffeeScript switch/when/else block replaces the JavaScript
|
||||
# switch/case/default by compiling into an if-else chain.
|
||||
Switch: [
|
||||
o "SWITCH Expression INDENT Whens OUTDENT", -> $4.switchesOver $2
|
||||
o "SWITCH Expression INDENT Whens ELSE Block OUTDENT", -> $4.switchesOver($2).addElse $6, true
|
||||
o "SWITCH INDENT Whens OUTDENT", -> $3
|
||||
o "SWITCH INDENT Whens ELSE Block OUTDENT", -> $3.addElse $5, true
|
||||
o "SWITCH Expression INDENT Whens OUTDENT", -> new SwitchNode $2, $4
|
||||
o "SWITCH Expression INDENT Whens ELSE Block OUTDENT", -> new SwitchNode $2, $4, $6
|
||||
o "SWITCH INDENT Whens OUTDENT", -> new SwitchNode null, $3
|
||||
o "SWITCH INDENT Whens ELSE Block OUTDENT", -> new SwitchNode null, $3, $5
|
||||
]
|
||||
|
||||
# The inner list of whens is left recursive. At code-generation time, the
|
||||
# IfNode will rewrite them into a proper chain.
|
||||
Whens: [
|
||||
o "When"
|
||||
o "Whens When", -> $1.addElse $2
|
||||
o "Whens When", -> $1.concat $2
|
||||
]
|
||||
|
||||
# An individual **When** clause, with action.
|
||||
When: [
|
||||
o "LEADING_WHEN SimpleArgs Block", -> new IfNode $2, $3, statement: true
|
||||
o "LEADING_WHEN SimpleArgs Block TERMINATOR", -> new IfNode $2, $3, statement: true
|
||||
o "LEADING_WHEN SimpleArgs Block", -> [[$2, $3]]
|
||||
o "LEADING_WHEN SimpleArgs Block TERMINATOR", -> [[$2, $3]]
|
||||
]
|
||||
|
||||
# The most basic form of *if* is a condition and an action. The following
|
||||
@@ -507,10 +517,10 @@ grammar =
|
||||
# *if* and *unless*.
|
||||
If: [
|
||||
o "IfBlock"
|
||||
o "Statement IF Expression", -> new IfNode $3, Expressions.wrap([$1]), statement: true
|
||||
o "Expression IF Expression", -> new IfNode $3, Expressions.wrap([$1]), statement: true
|
||||
o "Statement UNLESS Expression", -> new IfNode $3, Expressions.wrap([$1]), statement: true, invert: true
|
||||
o "Expression UNLESS Expression", -> new IfNode $3, Expressions.wrap([$1]), statement: true, invert: true
|
||||
o "Statement POST_IF Expression", -> new IfNode $3, Expressions.wrap([$1]), statement: true
|
||||
o "Expression POST_IF Expression", -> new IfNode $3, Expressions.wrap([$1]), statement: true
|
||||
o "Statement POST_UNLESS Expression", -> new IfNode $3, Expressions.wrap([$1]), statement: true, invert: true
|
||||
o "Expression POST_UNLESS Expression", -> new IfNode $3, Expressions.wrap([$1]), statement: true, invert: true
|
||||
]
|
||||
|
||||
# Arithmetic and logical operators, working on one or more operands.
|
||||
@@ -520,58 +530,34 @@ grammar =
|
||||
# -type rule, but in order to make the precedence binding possible, separate
|
||||
# rules are necessary.
|
||||
Operation: [
|
||||
o "! Expression", -> new OpNode '!', $2
|
||||
o "!! Expression", -> new OpNode '!!', $2
|
||||
o("- Expression", (-> new OpNode('-', $2)), {prec: 'UMINUS'})
|
||||
o("+ Expression", (-> new OpNode('+', $2)), {prec: 'UPLUS'})
|
||||
o "~ Expression", -> new OpNode '~', $2
|
||||
o "UNARY Expression", -> new OpNode $1, $2
|
||||
o("- Expression", (-> new OpNode('-', $2)), {prec: 'UNARY'})
|
||||
o("+ Expression", (-> new OpNode('+', $2)), {prec: 'UNARY'})
|
||||
|
||||
o "-- Expression", -> new OpNode '--', $2
|
||||
o "++ Expression", -> new OpNode '++', $2
|
||||
o "DELETE Expression", -> new OpNode 'delete', $2
|
||||
o "TYPEOF Expression", -> new OpNode 'typeof', $2
|
||||
o "Expression --", -> new OpNode '--', $1, null, true
|
||||
o "Expression ++", -> new OpNode '++', $1, null, true
|
||||
|
||||
o "Expression * Expression", -> new OpNode '*', $1, $3
|
||||
o "Expression / Expression", -> new OpNode '/', $1, $3
|
||||
o "Expression % Expression", -> new OpNode '%', $1, $3
|
||||
|
||||
o "Expression ? Expression", -> new OpNode '?', $1, $3
|
||||
o "Expression + Expression", -> new OpNode '+', $1, $3
|
||||
o "Expression - Expression", -> new OpNode '-', $1, $3
|
||||
|
||||
o "Expression << Expression", -> new OpNode '<<', $1, $3
|
||||
o "Expression >> Expression", -> new OpNode '>>', $1, $3
|
||||
o "Expression >>> Expression", -> new OpNode '>>>', $1, $3
|
||||
o "Expression & Expression", -> new OpNode '&', $1, $3
|
||||
o "Expression | Expression", -> new OpNode '|', $1, $3
|
||||
o "Expression ^ Expression", -> new OpNode '^', $1, $3
|
||||
|
||||
o "Expression <= Expression", -> new OpNode '<=', $1, $3
|
||||
o "Expression < Expression", -> new OpNode '<', $1, $3
|
||||
o "Expression > Expression", -> new OpNode '>', $1, $3
|
||||
o "Expression >= Expression", -> new OpNode '>=', $1, $3
|
||||
|
||||
o "Expression == Expression", -> new OpNode '==', $1, $3
|
||||
o "Expression != Expression", -> new OpNode '!=', $1, $3
|
||||
|
||||
o "Expression && Expression", -> new OpNode '&&', $1, $3
|
||||
o "Expression || Expression", -> new OpNode '||', $1, $3
|
||||
o "Expression OP? Expression", -> new OpNode '?', $1, $3
|
||||
o "Expression MATH Expression", -> new OpNode $2, $1, $3
|
||||
o "Expression SHIFT Expression", -> new OpNode $2, $1, $3
|
||||
o "Expression COMPARE Expression", -> new OpNode $2, $1, $3
|
||||
o "Expression LOGIC Expression", -> new OpNode $2, $1, $3
|
||||
o "Value COMPOUND_ASSIGN Expression", -> new OpNode $2, $1, $3
|
||||
o "Value COMPOUND_ASSIGN INDENT Expression OUTDENT", -> new OpNode $2, $1, $4
|
||||
|
||||
o "Expression -= Expression", -> new OpNode '-=', $1, $3
|
||||
o "Expression += Expression", -> new OpNode '+=', $1, $3
|
||||
o "Expression /= Expression", -> new OpNode '/=', $1, $3
|
||||
o "Expression *= Expression", -> new OpNode '*=', $1, $3
|
||||
o "Expression %= Expression", -> new OpNode '%=', $1, $3
|
||||
o "Expression ||= Expression", -> new OpNode '||=', $1, $3
|
||||
o "Expression &&= Expression", -> new OpNode '&&=', $1, $3
|
||||
o "Expression ?= Expression", -> new OpNode '?=', $1, $3
|
||||
|
||||
o "Expression INSTANCEOF Expression", -> new OpNode 'instanceof', $1, $3
|
||||
o "Expression IN Expression", -> new InNode $1, $3
|
||||
o "Expression OF Expression", -> new OpNode 'in', $1, $3
|
||||
o "Expression ! IN Expression", -> new OpNode '!', new InNode $1, $4
|
||||
o "Expression ! OF Expression", -> new OpNode '!', new ParentheticalNode new OpNode 'in', $1, $4
|
||||
o "Expression INSTANCEOF Expression", -> new OpNode 'instanceof', $1, $3
|
||||
o "Expression UNARY IN Expression", -> new OpNode $2, new InNode $1, $4
|
||||
o "Expression UNARY OF Expression", -> new OpNode $2, new ParentheticalNode new OpNode 'in', $1, $4
|
||||
o "Expression UNARY INSTANCEOF Expression", -> new OpNode $2, new ParentheticalNode new OpNode 'instanceof', $1, $4
|
||||
]
|
||||
|
||||
|
||||
@@ -587,25 +573,24 @@ grammar =
|
||||
#
|
||||
# (2 + 3) * 4
|
||||
operators = [
|
||||
["left", '?']
|
||||
["nonassoc", 'UMINUS', 'UPLUS', '!', '!!', '~', '++', '--']
|
||||
["left", '*', '/', '%']
|
||||
["right", '?', 'NEW']
|
||||
["left", 'CALL_START', 'CALL_END']
|
||||
["nonassoc", '++', '--']
|
||||
["right", 'UNARY']
|
||||
["left", 'MATH']
|
||||
["left", '+', '-']
|
||||
["left", '<<', '>>', '>>>']
|
||||
["left", '&', '|', '^']
|
||||
["left", '<=', '<', '>', '>=']
|
||||
["right", 'DELETE', 'INSTANCEOF', 'TYPEOF']
|
||||
["left", 'SHIFT']
|
||||
["left", 'COMPARE']
|
||||
["left", 'INSTANCEOF']
|
||||
["left", '==', '!=']
|
||||
["left", '&&', '||', 'OP?']
|
||||
["right", '-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?=']
|
||||
["left", 'LOGIC']
|
||||
["right", 'COMPOUND_ASSIGN']
|
||||
["left", '.']
|
||||
["right", 'INDENT']
|
||||
["left", 'OUTDENT']
|
||||
["nonassoc", 'INDENT', 'OUTDENT']
|
||||
["right", 'WHEN', 'LEADING_WHEN', 'IN', 'OF', 'BY', 'THROW']
|
||||
["right", 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'NEW', 'SUPER', 'CLASS']
|
||||
["left", 'EXTENDS']
|
||||
["right", 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'EXTENDS']
|
||||
["right", '=', ':', 'RETURN']
|
||||
["right", '->', '=>', 'UNLESS', 'IF', 'ELSE']
|
||||
["right", '->', '=>', 'UNLESS', 'POST_IF', 'POST_UNLESS']
|
||||
]
|
||||
|
||||
# Wrapping Up
|
||||
@@ -627,9 +612,8 @@ for name, alternatives of grammar
|
||||
# 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 [Yacc](http://dinosaur.compilertools.net/yacc/index.html)).
|
||||
exports.parser = new Parser {
|
||||
exports.parser = new Parser
|
||||
tokens: tokens.join ' '
|
||||
bnf: grammar
|
||||
operators: operators.reverse()
|
||||
startSymbol: 'Root'
|
||||
}
|
||||
|
||||
100
src/lexer.coffee
100
src/lexer.coffee
@@ -9,8 +9,8 @@
|
||||
|
||||
# Set up the Lexer for both Node.js and the browser, depending on where we are.
|
||||
if process?
|
||||
{Rewriter} = require('./rewriter')
|
||||
{helpers} = require('./helpers')
|
||||
{Rewriter} = require './rewriter'
|
||||
{helpers} = require './helpers'
|
||||
else
|
||||
this.exports = this
|
||||
Rewriter = this.Rewriter
|
||||
@@ -46,11 +46,12 @@ exports.Lexer = class Lexer
|
||||
@i = 0 # Current character position we're parsing.
|
||||
@line = o.line or 0 # The current line.
|
||||
@indent = 0 # The current indentation level.
|
||||
@outdebt = 0 # The under-outdentation of the last outdent.
|
||||
@indebt = 0 # The over-indentation at the current level.
|
||||
@outdebt = 0 # The under-outdentation at the current level.
|
||||
@indents = [] # The stack of all current indentation levels.
|
||||
@tokens = [] # Stream of parsed tokens in the form ['TYPE', value, line]
|
||||
while @i < @code.length
|
||||
@chunk = @code.slice @i
|
||||
@chunk = @code[@i..]
|
||||
@extractNextToken()
|
||||
@closeIndentation()
|
||||
return @tokens if o.rewrite is off
|
||||
@@ -88,6 +89,7 @@ exports.Lexer = class Lexer
|
||||
tag = id.toUpperCase() if include(JS_KEYWORDS, id) or (not forcedIdentifier and include(COFFEE_KEYWORDS, id))
|
||||
tag = 'LEADING_WHEN' if tag is 'WHEN' and include LINE_BREAK, @tag()
|
||||
tag = 'ALL' if id is 'all' and @tag() is 'FOR'
|
||||
tag = 'UNARY' if include UNARY, tag
|
||||
if include(JS_FORBIDDEN, id)
|
||||
if forcedIdentifier
|
||||
tag = 'STRING'
|
||||
@@ -100,6 +102,8 @@ exports.Lexer = class Lexer
|
||||
@identifierError id
|
||||
unless forcedIdentifier
|
||||
tag = id = CONVERSIONS[id] if include COFFEE_ALIASES, id
|
||||
tag = 'LOGIC' if include LOGIC, id
|
||||
tag = 'UNARY' if id is '!'
|
||||
@token tag, id
|
||||
@token ']', ']' if close_index
|
||||
true
|
||||
@@ -120,7 +124,7 @@ exports.Lexer = class Lexer
|
||||
return false unless string =
|
||||
@balancedToken(['"', '"'], ['#{', '}']) or
|
||||
@balancedToken ["'", "'"]
|
||||
@interpolateString string.replace STRING_NEWLINES, " \\\n"
|
||||
@interpolateString string.replace /\n/g, '\\\n'
|
||||
@line += count string, "\n"
|
||||
@i += string.length
|
||||
true
|
||||
@@ -128,7 +132,7 @@ exports.Lexer = class Lexer
|
||||
# Matches heredocs, adjusting indentation to the correct level, as heredocs
|
||||
# preserve whitespace, but ignore indentation to the left.
|
||||
heredocToken: ->
|
||||
return false unless match = @chunk.match(HEREDOC)
|
||||
return false unless match = @chunk.match HEREDOC
|
||||
quote = match[1].substr 0, 1
|
||||
doc = @sanitizeHeredoc match[2] or match[4] or '', {quote}
|
||||
@interpolateString quote + doc + quote, heredoc: yes
|
||||
@@ -141,9 +145,8 @@ exports.Lexer = class Lexer
|
||||
return false unless match = @chunk.match(COMMENT)
|
||||
@line += count match[1], "\n"
|
||||
@i += match[1].length
|
||||
if match[2]
|
||||
comment = @sanitizeHeredoc match[2], herecomment: true
|
||||
@token 'HERECOMMENT', comment.split MULTILINER
|
||||
if match[4]
|
||||
@token 'HERECOMMENT', @sanitizeHeredoc match[4], herecomment: true, indent: match[3]
|
||||
@token 'TERMINATOR', '\n'
|
||||
true
|
||||
|
||||
@@ -160,7 +163,8 @@ exports.Lexer = class Lexer
|
||||
# JavaScript and Ruby, borrow slash balancing from `@balancedToken`, and
|
||||
# borrow interpolation from `@interpolateString`.
|
||||
regexToken: ->
|
||||
return false unless @chunk.match REGEX_START
|
||||
return false unless first = @chunk.match REGEX_START
|
||||
return false if first[1] is ' ' and @tag() not in ['CALL_START', '=']
|
||||
return false if include NOT_REGEX, @tag()
|
||||
return false unless regex = @balancedToken ['/', '/']
|
||||
return false unless end = @chunk.substr(regex.length).match REGEX_END
|
||||
@@ -200,16 +204,19 @@ exports.Lexer = class Lexer
|
||||
size = indent.match(LAST_DENTS).reverse()[0].match(LAST_DENT)[1].length
|
||||
nextCharacter = @match NEXT_CHARACTER, 1
|
||||
noNewlines = nextCharacter is '.' or nextCharacter is ',' or @unfinished()
|
||||
if size is @indent
|
||||
if size - @indebt is @indent
|
||||
return @suppressNewlines() if noNewlines
|
||||
return @newlineToken indent
|
||||
else if size > @indent
|
||||
return @suppressNewlines() if noNewlines
|
||||
@outdebt = 0
|
||||
diff = size - @indent
|
||||
if noNewlines
|
||||
@indebt = size - @indent
|
||||
return @suppressNewlines()
|
||||
diff = size - @indent + @outdebt
|
||||
@token 'INDENT', diff
|
||||
@indents.push diff
|
||||
@outdebt = @indebt = 0
|
||||
else
|
||||
@indebt = 0
|
||||
@outdentToken @indent - size, noNewlines
|
||||
@indent = size
|
||||
true
|
||||
@@ -269,18 +276,23 @@ exports.Lexer = class Lexer
|
||||
@tagParameters() if value and value.match CODE
|
||||
value or= @chunk.substr 0, 1
|
||||
@i += value.length
|
||||
prevSpaced = @prev() and @prev().spaced
|
||||
spaced = (prev = @prev()) and prev.spaced
|
||||
tag = value
|
||||
if value is '='
|
||||
@assignmentError() if include JS_FORBIDDEN, @value()
|
||||
if @value() in ['or', 'and']
|
||||
return @tag 1, CONVERSIONS[@value()] + '='
|
||||
if value is ';'
|
||||
tag = 'TERMINATOR'
|
||||
else if value is '?' and prevSpaced
|
||||
tag = 'OP?'
|
||||
else if include(CALLABLE, @tag()) and not prevSpaced
|
||||
@tokens.splice(@tokens.length - 1, 1, ['COMPOUND_ASSIGN', CONVERSIONS[@value()] + '=', prev[2]])
|
||||
return true
|
||||
if value is ';' then tag = 'TERMINATOR'
|
||||
else if include(LOGIC, value) then tag = 'LOGIC'
|
||||
else if include(MATH, value) then tag = 'MATH'
|
||||
else if include(COMPARE, value) then tag = 'COMPARE'
|
||||
else if include(COMPOUND_ASSIGN, value) then tag = 'COMPOUND_ASSIGN'
|
||||
else if include(UNARY, value) then tag = 'UNARY'
|
||||
else if include(SHIFT, value) then tag = 'SHIFT'
|
||||
else if include(CALLABLE, @tag()) and not spaced
|
||||
if value is '('
|
||||
prev[0] = 'FUNC_EXIST' if prev[0] is '?'
|
||||
tag = 'CALL_START'
|
||||
else if value is '['
|
||||
tag = 'INDEX_START'
|
||||
@@ -312,10 +324,14 @@ exports.Lexer = class Lexer
|
||||
# Sanitize a heredoc or herecomment by escaping internal double quotes and
|
||||
# erasing all external indentation on the left-hand side.
|
||||
sanitizeHeredoc: (doc, options) ->
|
||||
while (match = HEREDOC_INDENT.exec(doc))
|
||||
attempt = if match[2]? then match[2] else match[3]
|
||||
indent = attempt if not indent or attempt.length < indent.length
|
||||
doc = doc.replace(new RegExp("^" +indent, 'gm'), '')
|
||||
indent = options.indent
|
||||
return doc if options.herecomment and not include doc, '\n'
|
||||
unless options.herecomment
|
||||
while (match = HEREDOC_INDENT.exec(doc)) isnt null
|
||||
attempt = if match[2]? then match[2] else match[3]
|
||||
indent = attempt if not indent? or attempt.length < indent.length
|
||||
indent or= ''
|
||||
doc = doc.replace(new RegExp("^" + indent, 'gm'), '').replace(/^\n/, '')
|
||||
return doc if options.herecomment
|
||||
doc.replace(MULTILINER, "\\n")
|
||||
.replace(new RegExp(options.quote, 'g'), "\\#{options.quote}")
|
||||
@@ -482,7 +498,7 @@ JS_KEYWORDS = [
|
||||
"for", "in", "while",
|
||||
"delete", "instanceof", "typeof",
|
||||
"switch", "super", "extends", "class",
|
||||
"this", "null"
|
||||
"this", "null", "debugger"
|
||||
]
|
||||
|
||||
# CoffeeScript-only keywords, which we're more relaxed about allowing. They can't
|
||||
@@ -510,17 +526,17 @@ JS_FORBIDDEN = JS_KEYWORDS.concat RESERVED
|
||||
# Token matching regexes.
|
||||
IDENTIFIER = /^([a-zA-Z\$_](\w|\$)*)/
|
||||
NUMBER = /^(((\b0(x|X)[0-9a-fA-F]+)|((\b[0-9]+(\.[0-9]+)?|\.[0-9]+)(e[+\-]?[0-9]+)?)))\b/i
|
||||
HEREDOC = /^("{6}|'{6}|"{3}\n?([\s\S]*?)\n?([ \t]*)"{3}|'{3}\n?([\s\S]*?)\n?([ \t]*)'{3})/
|
||||
OPERATOR = /^(-[\-=>]?|\+[+=]?|[*&|\/%=<>:!?]+)([ \t]*)/
|
||||
HEREDOC = /^("{6}|'{6}|"{3}([\s\S]*?)\n?([ \t]*)"{3}|'{3}([\s\S]*?)\n?([ \t]*)'{3})/
|
||||
OPERATOR = /^(-[\-=>]?|\+[+=]?|[*&|\/%=<>^:!?]+)([ \t]*)/
|
||||
WHITESPACE = /^([ \t]+)/
|
||||
COMMENT = /^(\s*\#{3}(?!#)[ \t]*\n+([\s\S]*?)[ \t]*\n+[ \t]*\#{3}|(\s*#(?!##[^#])[^\n]*)+)/
|
||||
COMMENT = /^(([ \t]*\n)*([ \t]*)###([^#][\s\S]*?)(###[ \t]*\n|(###)?$)|(\s*#(?!##[^#])[^\n]*)+)/
|
||||
CODE = /^((-|=)>)/
|
||||
MULTI_DENT = /^((\n([ \t]*))+)(\.)?/
|
||||
LAST_DENTS = /\n([ \t]*)/g
|
||||
LAST_DENT = /\n([ \t]*)/
|
||||
|
||||
# Regex-matching-regexes.
|
||||
REGEX_START = /^\/[^\/ ]/
|
||||
REGEX_START = /^\/([^\/])/
|
||||
REGEX_INTERPOLATION = /([^\\]#\{.*[^\\]\})/
|
||||
REGEX_END = /^(([imgy]{1,4})\b|\W|$)/
|
||||
REGEX_ESCAPE = /\\[^\$]/g
|
||||
@@ -528,12 +544,29 @@ REGEX_ESCAPE = /\\[^\$]/g
|
||||
# Token cleaning regexes.
|
||||
JS_CLEANER = /(^`|`$)/g
|
||||
MULTILINER = /\n/g
|
||||
STRING_NEWLINES = /\n[ \t]*/g
|
||||
NO_NEWLINE = /^([+\*&|\/\-%=<>!.\\][<>=&|]*|and|or|is|isnt|not|delete|typeof|instanceof)$/
|
||||
HEREDOC_INDENT = /(\n+([ \t]*)|^([ \t]+))/g
|
||||
ASSIGNED = /^\s*([a-zA-Z\$_@]\w*[ \t]*?[:=][^=])/
|
||||
ASSIGNED = /^\s*(([a-zA-Z\$_@]\w*|["'][^\r\n]+?["']|\d+)[ \t]*?[:=][^:=])/
|
||||
NEXT_CHARACTER = /^\s*(\S)/
|
||||
|
||||
# Compound assignment tokens.
|
||||
COMPOUND_ASSIGN = ['-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?=', '<<=', '>>=', '>>>=', '&=', '^=', '|=']
|
||||
|
||||
# Unary tokens.
|
||||
UNARY = ['UMINUS', 'UPLUS', '!', '!!', '~', 'TYPEOF', 'DELETE']
|
||||
|
||||
# Logical tokens.
|
||||
LOGIC = ['&', '|', '^', '&&', '||']
|
||||
|
||||
# Bit-shifting tokens.
|
||||
SHIFT = ['<<', '>>', '>>>']
|
||||
|
||||
# Comparison tokens.
|
||||
COMPARE = ['<=', '<', '>', '>=']
|
||||
|
||||
# Mathmatical tokens.
|
||||
MATH = ['*', '/', '%']
|
||||
|
||||
# Tokens which a regular expression will never immediately follow, but which
|
||||
# a division operator might.
|
||||
#
|
||||
@@ -553,11 +586,10 @@ CALLABLE = ['IDENTIFIER', 'SUPER', ')', ']', '}', 'STRING', '@', 'THIS', '?', ':
|
||||
LINE_BREAK = ['INDENT', 'OUTDENT', 'TERMINATOR']
|
||||
|
||||
# Conversions from CoffeeScript operators into JavaScript ones.
|
||||
CONVERSIONS = {
|
||||
CONVERSIONS =
|
||||
'and': '&&'
|
||||
'or': '||'
|
||||
'is': '=='
|
||||
'isnt': '!='
|
||||
'not': '!'
|
||||
'===': '=='
|
||||
}
|
||||
|
||||
365
src/nodes.coffee
365
src/nodes.coffee
@@ -29,6 +29,9 @@ else
|
||||
# scope, and indentation level.
|
||||
exports.BaseNode = class BaseNode
|
||||
|
||||
constructor: ->
|
||||
@tags = {}
|
||||
|
||||
# Common logic for determining whether to wrap this node in a closure before
|
||||
# compiling it, or to compile directly. We need to wrap if this node is a
|
||||
# *statement*, and it's not a *pureStatement*, and we're not at
|
||||
@@ -42,12 +45,10 @@ exports.BaseNode = class BaseNode
|
||||
compile: (o) ->
|
||||
@options = merge o or {}
|
||||
@tab = o.indent
|
||||
unless this instanceof ValueNode or this instanceof CallNode
|
||||
del @options, 'operation'
|
||||
del @options, 'chainRoot' unless this instanceof AccessorNode or this instanceof IndexNode
|
||||
del @options, 'chainRoot' unless this instanceof AccessorNode or this instanceof IndexNode
|
||||
top = if @topSensitive() then @options.top else del @options, 'top'
|
||||
closure = @isStatement() and not @isPureStatement() and not top and
|
||||
not @options.asStatement and not (this instanceof CommentNode) and
|
||||
closure = @isStatement(o) and not @isPureStatement() and not top and
|
||||
not @options.asStatement and this not instanceof CommentNode and
|
||||
not @containsPureStatement()
|
||||
if closure then @compileClosure(@options) else @compileNode(@options)
|
||||
|
||||
@@ -63,7 +64,7 @@ exports.BaseNode = class BaseNode
|
||||
# by assigning it to a temporary variable.
|
||||
compileReference: (o, options) ->
|
||||
options or= {}
|
||||
pair = if not ((this instanceof CallNode or @contains((n) -> n instanceof CallNode)) or
|
||||
pair = if not (@containsType(CallNode) or
|
||||
(this instanceof ValueNode and (not (@base instanceof LiteralNode) or @hasProperties())))
|
||||
[this, this]
|
||||
else if this instanceof ValueNode and options.assignment
|
||||
@@ -157,6 +158,7 @@ exports.Expressions = class Expressions extends BaseNode
|
||||
isStatement: -> yes
|
||||
|
||||
constructor: (nodes) ->
|
||||
super()
|
||||
@expressions = compact flatten nodes or []
|
||||
|
||||
# Tack an expression on to the end of this expression list.
|
||||
@@ -221,7 +223,7 @@ exports.Expressions = class Expressions extends BaseNode
|
||||
compileExpression: (node, o) ->
|
||||
@tab = o.indent
|
||||
compiledNode = node.compile merge o, top: true
|
||||
if node.isStatement() then compiledNode else "#{@idt()}#{compiledNode};"
|
||||
if node.isStatement(o) then compiledNode else "#{@idt()}#{compiledNode};"
|
||||
|
||||
# Wrap up the given nodes as an **Expressions**, unless it already happens
|
||||
# to be one.
|
||||
@@ -239,6 +241,7 @@ exports.LiteralNode = class LiteralNode extends BaseNode
|
||||
class: 'LiteralNode'
|
||||
|
||||
constructor: (@value) ->
|
||||
super()
|
||||
|
||||
makeReturn: ->
|
||||
if @isStatement() then this else super()
|
||||
@@ -246,12 +249,12 @@ exports.LiteralNode = class LiteralNode extends BaseNode
|
||||
# Break and continue must be treated as pure statements -- they lose their
|
||||
# meaning when wrapped in a closure.
|
||||
isStatement: ->
|
||||
@value is 'break' or @value is 'continue'
|
||||
@value is 'break' or @value is 'continue' or @value is 'debugger'
|
||||
isPureStatement: LiteralNode::isStatement
|
||||
|
||||
compileNode: (o) ->
|
||||
idt = if @isStatement() then @idt() else ''
|
||||
end = if @isStatement() then ';' else ''
|
||||
idt = if @isStatement(o) then @idt() else ''
|
||||
end = if @isStatement(o) then ';' else ''
|
||||
idt + @value + end
|
||||
|
||||
toString: (idt) ->
|
||||
@@ -269,6 +272,7 @@ exports.ReturnNode = class ReturnNode extends BaseNode
|
||||
children: ['expression']
|
||||
|
||||
constructor: (@expression) ->
|
||||
super()
|
||||
|
||||
makeReturn: ->
|
||||
this
|
||||
@@ -279,7 +283,7 @@ exports.ReturnNode = class ReturnNode extends BaseNode
|
||||
super o
|
||||
|
||||
compileNode: (o) ->
|
||||
o.asStatement = true if @expression.isStatement()
|
||||
o.asStatement = true if @expression.isStatement(o)
|
||||
"#{@tab}return #{@expression.compile(o)};"
|
||||
|
||||
#### ValueNode
|
||||
@@ -288,13 +292,12 @@ exports.ReturnNode = class ReturnNode extends BaseNode
|
||||
# or vanilla.
|
||||
exports.ValueNode = class ValueNode extends BaseNode
|
||||
|
||||
SOAK: " == undefined ? undefined : "
|
||||
|
||||
class: 'ValueNode'
|
||||
children: ['base', 'properties']
|
||||
|
||||
# A **ValueNode** has a base and a list of property accesses.
|
||||
constructor: (@base, @properties) ->
|
||||
super()
|
||||
@properties or= []
|
||||
|
||||
# Add a property access to the list.
|
||||
@@ -325,24 +328,19 @@ exports.ValueNode = class ValueNode extends BaseNode
|
||||
if @properties.length then this else @base
|
||||
|
||||
# Values are considered to be statements if their base is a statement.
|
||||
isStatement: ->
|
||||
@base.isStatement and @base.isStatement() and not @hasProperties()
|
||||
isStatement: (o) ->
|
||||
@base.isStatement and @base.isStatement(o) and not @hasProperties()
|
||||
|
||||
isNumber: ->
|
||||
@base instanceof LiteralNode and @base.value.match NUMBER
|
||||
|
||||
# Works out if the value is the start of a chain.
|
||||
isStart: (o) ->
|
||||
return true if this is o.chainRoot and @properties[0] instanceof AccessorNode
|
||||
node = o.chainRoot.base or o.chainRoot.variable
|
||||
while node instanceof CallNode then node = node.variable
|
||||
node is this
|
||||
|
||||
# If the value node has indexes containing function calls, and the value node
|
||||
# needs to be used twice, in compound assignment ... then we need to cache
|
||||
# the value of the indexes.
|
||||
cacheIndexes: (o) ->
|
||||
copy = new ValueNode @base, @properties.slice 0
|
||||
copy = new ValueNode @base, @properties[0..]
|
||||
if @base instanceof CallNode
|
||||
[@base, copy.base] = @base.compileReference o
|
||||
for prop, i in copy.properties
|
||||
if prop instanceof IndexNode and prop.contains((n) -> n instanceof CallNode)
|
||||
[index, indexVar] = prop.index.compileReference o
|
||||
@@ -360,9 +358,14 @@ exports.ValueNode = class ValueNode extends BaseNode
|
||||
# evaluate a anything twice when building the soak chain.
|
||||
compileNode: (o) ->
|
||||
only = del o, 'onlyFirst'
|
||||
op = del o, 'operation'
|
||||
op = @tags.operation
|
||||
props = if only then @properties[0...@properties.length - 1] else @properties
|
||||
o.chainRoot or= this
|
||||
for prop in props
|
||||
hasSoak = yes if prop.soakNode
|
||||
if hasSoak and @containsType CallNode
|
||||
[me, copy] = @cacheIndexes o
|
||||
@base.parenthetical = yes if @parenthetical and not props.length
|
||||
baseline = @base.compile o
|
||||
baseline = "(#{baseline})" if @hasProperties() and (@base instanceof ObjectNode or @isNumber())
|
||||
complete = @last = baseline
|
||||
@@ -370,14 +373,20 @@ exports.ValueNode = class ValueNode extends BaseNode
|
||||
for prop, i in props
|
||||
@source = baseline
|
||||
if prop.soakNode
|
||||
if @base instanceof CallNode or @base.contains((n) -> n instanceof CallNode) and i is 0
|
||||
if @base.containsType(CallNode) and i is 0
|
||||
temp = o.scope.freeVariable()
|
||||
complete = "(#{ baseline = temp } = (#{complete}))"
|
||||
complete = "typeof #{complete} === \"undefined\" || #{baseline}" if i is 0 and @isStart(o)
|
||||
complete += @SOAK + (baseline += prop.compile(o))
|
||||
complete = if i is 0
|
||||
"(typeof #{complete} === \"undefined\" || #{baseline} === null) ? undefined : "
|
||||
else
|
||||
"#{complete} == null ? undefined : "
|
||||
complete += (baseline += prop.compile(o))
|
||||
else
|
||||
part = prop.compile(o)
|
||||
baseline += part
|
||||
if hasSoak and prop.containsType CallNode
|
||||
baseline += copy.properties[i].compile o
|
||||
else
|
||||
baseline += part
|
||||
complete += part
|
||||
@last = part
|
||||
|
||||
@@ -392,14 +401,14 @@ exports.CommentNode = class CommentNode extends BaseNode
|
||||
class: 'CommentNode'
|
||||
isStatement: -> yes
|
||||
|
||||
constructor: (@lines) ->
|
||||
constructor: (@comment) ->
|
||||
super()
|
||||
|
||||
makeReturn: ->
|
||||
this
|
||||
|
||||
compileNode: (o) ->
|
||||
sep = '\n' + @tab
|
||||
"#{@tab}/*#{sep + @lines.join(sep) }\n#{@tab}*/"
|
||||
@tab + '/*' + @comment.replace(/\r?\n/g, '\n' + @tab) + '*/'
|
||||
|
||||
#### CallNode
|
||||
|
||||
@@ -410,11 +419,13 @@ exports.CallNode = class CallNode extends BaseNode
|
||||
class: 'CallNode'
|
||||
children: ['variable', 'args']
|
||||
|
||||
constructor: (variable, @args) ->
|
||||
constructor: (variable, @args, @exist) ->
|
||||
super()
|
||||
@isNew = false
|
||||
@isSuper = variable is 'super'
|
||||
@variable = if @isSuper then null else variable
|
||||
@args or= []
|
||||
@first = @last = ''
|
||||
@compileSplatArguments = (o) ->
|
||||
SplatNode.compileSplattedArray.call(this, @args, o)
|
||||
|
||||
@@ -428,23 +439,34 @@ exports.CallNode = class CallNode extends BaseNode
|
||||
|
||||
# Grab the reference to the superclass' implementation of the current method.
|
||||
superReference: (o) ->
|
||||
throw new Error "cannot call super outside of a function" unless o.scope.method
|
||||
methname = o.scope.method.name
|
||||
meth = if o.scope.method.proto
|
||||
"#{o.scope.method.proto}.__superClass__.#{methname}"
|
||||
"#{o.scope.method.proto}.__super__.#{methname}"
|
||||
else if methname
|
||||
"#{methname}.__superClass__.constructor"
|
||||
"#{methname}.__super__.constructor"
|
||||
else throw new Error "cannot call super on an anonymous function."
|
||||
|
||||
# Compile a vanilla function call.
|
||||
compileNode: (o) ->
|
||||
o.chainRoot = this unless o.chainRoot
|
||||
op = @tags.operation
|
||||
if @exist
|
||||
[@first, @meth] = @variable.compileReference o, precompile: yes
|
||||
@first = "(typeof #{@first} === \"function\" ? "
|
||||
@last = " : undefined)"
|
||||
else if @variable then @meth = @variable.compile o
|
||||
for arg in @args when arg instanceof SplatNode
|
||||
compilation = @compileSplat(o)
|
||||
unless compilation
|
||||
args = (arg.compile(o) for arg in @args).join(', ')
|
||||
compilation = if @isSuper then @compileSuper(args, o)
|
||||
else "#{@prefix()}#{@variable.compile(o)}(#{args})"
|
||||
if o.operation and @wrapped then "(#{compilation})" else compilation
|
||||
code = @compileSplat(o)
|
||||
if not code
|
||||
args = for arg in @args
|
||||
arg.parenthetical = true
|
||||
arg.compile o
|
||||
code = if @isSuper
|
||||
@compileSuper(args.join(', '), o)
|
||||
else
|
||||
"#{@first}#{@prefix()}#{@meth}(#{ args.join(', ') })#{@last}"
|
||||
if op and @variable and @variable.wrapped then "(#{code})" else code
|
||||
|
||||
# `super()` is converted into a call against the superclass's implementation
|
||||
# of the current function.
|
||||
@@ -456,23 +478,29 @@ exports.CallNode = class CallNode extends BaseNode
|
||||
# If it's a constructor, then things get real tricky. We have to inject an
|
||||
# inner constructor in order to be able to pass the varargs.
|
||||
compileSplat: (o) ->
|
||||
meth = if @variable then @variable.compile(o) else @superReference(o)
|
||||
obj = @variable and @variable.source or 'this'
|
||||
meth = @meth or @superReference(o)
|
||||
obj = @variable and @variable.source or 'this'
|
||||
if obj.match(/\(/)
|
||||
temp = o.scope.freeVariable()
|
||||
obj = temp
|
||||
obj = temp
|
||||
meth = "(#{temp} = #{ @variable.source })#{ @variable.last }"
|
||||
if @isNew
|
||||
mentionsArgs = no
|
||||
for arg in @args
|
||||
arg.contains (n) -> mentionsArgs or= n instanceof LiteralNode and (n.value is 'arguments')
|
||||
utility 'extends'
|
||||
a = o.scope.freeVariable()
|
||||
b = o.scope.freeVariable()
|
||||
c = o.scope.freeVariable()
|
||||
"""
|
||||
(function() {
|
||||
#{@first}(function() {
|
||||
#{@idt(1)}var ctor = function(){};
|
||||
#{@idt(1)}__extends(ctor, #{meth});
|
||||
#{@idt(1)}return #{meth}.apply(new ctor, #{ @compileSplatArguments(o) });
|
||||
#{@tab}}).call(this)
|
||||
#{@idt(1)}__extends(ctor, #{a} = #{meth});
|
||||
#{@idt(1)}return typeof (#{b} = #{a}.apply(#{c} = new ctor, #{ @compileSplatArguments(o) })) === "object" ? #{b} : #{c};
|
||||
#{@tab}}).#{ if mentionsArgs then 'apply(this, arguments)' else 'call(this)'}#{@last}
|
||||
"""
|
||||
else
|
||||
"#{@prefix()}#{meth}.apply(#{obj}, #{ @compileSplatArguments(o) })"
|
||||
"#{@first}#{@prefix()}#{meth}.apply(#{obj}, #{ @compileSplatArguments(o) })#{@last}"
|
||||
|
||||
#### ExtendsNode
|
||||
|
||||
@@ -485,6 +513,7 @@ exports.ExtendsNode = class ExtendsNode extends BaseNode
|
||||
children: ['child', 'parent']
|
||||
|
||||
constructor: (@child, @parent) ->
|
||||
super()
|
||||
|
||||
# Hooks one constructor into another's prototype chain.
|
||||
compileNode: (o) ->
|
||||
@@ -501,6 +530,7 @@ exports.AccessorNode = class AccessorNode extends BaseNode
|
||||
children: ['name']
|
||||
|
||||
constructor: (@name, tag) ->
|
||||
super()
|
||||
@prototype = if tag is 'prototype' then '.prototype' else ''
|
||||
@soakNode = tag is 'soak'
|
||||
|
||||
@@ -519,6 +549,7 @@ exports.IndexNode = class IndexNode extends BaseNode
|
||||
children: ['index']
|
||||
|
||||
constructor: (@index) ->
|
||||
super()
|
||||
|
||||
compileNode: (o) ->
|
||||
o.chainRoot.wrapped or= @soakNode
|
||||
@@ -536,8 +567,9 @@ exports.RangeNode = class RangeNode extends BaseNode
|
||||
class: 'RangeNode'
|
||||
children: ['from', 'to']
|
||||
|
||||
constructor: (@from, @to, exclusive) ->
|
||||
@exclusive = !!exclusive
|
||||
constructor: (@from, @to, tag) ->
|
||||
super()
|
||||
@exclusive = tag is 'exclusive'
|
||||
@equals = if @exclusive then '' else '='
|
||||
|
||||
# Compiles the range's source variables -- where it starts and where it ends.
|
||||
@@ -608,12 +640,14 @@ exports.SliceNode = class SliceNode extends BaseNode
|
||||
children: ['range']
|
||||
|
||||
constructor: (@range) ->
|
||||
super()
|
||||
|
||||
compileNode: (o) ->
|
||||
from = @range.from.compile(o)
|
||||
to = @range.to.compile(o)
|
||||
plusPart = if @range.exclusive then '' else ' + 1'
|
||||
".slice(#{from}, #{to}#{plusPart})"
|
||||
from = if @range.from then @range.from.compile(o) else '0'
|
||||
to = if @range.to then @range.to.compile(o) else ''
|
||||
to += if not to or @range.exclusive then '' else ' + 1'
|
||||
to = ', ' + to if to
|
||||
".slice(#{from}#{to})"
|
||||
|
||||
#### ObjectNode
|
||||
|
||||
@@ -626,12 +660,13 @@ exports.ObjectNode = class ObjectNode extends BaseNode
|
||||
topSensitive: -> true
|
||||
|
||||
constructor: (props) ->
|
||||
super()
|
||||
@objects = @properties = props or []
|
||||
|
||||
compileNode: (o) ->
|
||||
top = del o, 'top'
|
||||
o.indent = @idt 1
|
||||
nonComments = prop for prop in @properties when not (prop instanceof CommentNode)
|
||||
nonComments = prop for prop in @properties when (prop not instanceof CommentNode)
|
||||
lastNoncom = nonComments[nonComments.length - 1]
|
||||
props = for prop, i in @properties
|
||||
join = ",\n"
|
||||
@@ -653,6 +688,7 @@ exports.ArrayNode = class ArrayNode extends BaseNode
|
||||
children: ['objects']
|
||||
|
||||
constructor: (@objects) ->
|
||||
super()
|
||||
@objects or= []
|
||||
@compileSplatLiteral = (o) ->
|
||||
SplatNode.compileSplattedArray.call(this, @objects, o)
|
||||
@@ -688,6 +724,7 @@ exports.ClassNode = class ClassNode extends BaseNode
|
||||
# Initialize a **ClassNode** with its name, an optional superclass, and a
|
||||
# list of prototype property assignments.
|
||||
constructor: (@variable, @parent, @properties) ->
|
||||
super()
|
||||
@properties or= []
|
||||
@returns = false
|
||||
|
||||
@@ -726,12 +763,15 @@ exports.ClassNode = class ClassNode extends BaseNode
|
||||
constructor = func
|
||||
continue
|
||||
if func instanceof CodeNode and func.bound
|
||||
func.bound = false
|
||||
constScope or= new Scope(o.scope, constructor.body, constructor)
|
||||
me or= constScope.freeVariable()
|
||||
pname = pvar.compile(o)
|
||||
constructor.body.push new ReturnNode literal 'this' if constructor.body.empty()
|
||||
constructor.body.unshift literal "this.#{pname} = function(){ return #{className}.prototype.#{pname}.apply(#{me}, arguments); }"
|
||||
if prop.context is 'this'
|
||||
func.context = className
|
||||
else
|
||||
func.bound = false
|
||||
constScope or= new Scope(o.scope, constructor.body, constructor)
|
||||
me or= constScope.freeVariable()
|
||||
pname = pvar.compile(o)
|
||||
constructor.body.push new ReturnNode literal 'this' if constructor.body.empty()
|
||||
constructor.body.unshift literal "this.#{pname} = function(){ return #{className}.prototype.#{pname}.apply(#{me}, arguments); }"
|
||||
if pvar
|
||||
access = if prop.context is 'this' then pvar.base.properties[0] else new AccessorNode(pvar, 'prototype')
|
||||
val = new ValueNode(@variable, [access])
|
||||
@@ -759,6 +799,7 @@ exports.AssignNode = class AssignNode extends BaseNode
|
||||
children: ['variable', 'value']
|
||||
|
||||
constructor: (@variable, @value, @context) ->
|
||||
super()
|
||||
|
||||
topSensitive: ->
|
||||
true
|
||||
@@ -781,7 +822,7 @@ exports.AssignNode = class AssignNode extends BaseNode
|
||||
# has not been seen yet within the current scope, declare it.
|
||||
compileNode: (o) ->
|
||||
top = del o, 'top'
|
||||
return @compilePatternMatch(o) if @isStatement()
|
||||
return @compilePatternMatch(o) if @isStatement(o)
|
||||
return @compileSplice(o) if @isValue() and @variable.isSplice()
|
||||
stmt = del o, 'asStatement'
|
||||
name = @variable.compile(o)
|
||||
@@ -796,7 +837,7 @@ exports.AssignNode = class AssignNode extends BaseNode
|
||||
o.scope.find name unless @isValue() and (@variable.hasProperties() or @variable.namespaced)
|
||||
val = "#{name} = #{val}"
|
||||
return "#{@tab}#{val};" if stmt
|
||||
if top then val else "(#{val})"
|
||||
if top or @parenthetical then val else "(#{val})"
|
||||
|
||||
# Brief implementation of recursive pattern matching, when assigning array or
|
||||
# object literals to a value. Peeks at their properties to assign inner names.
|
||||
@@ -804,7 +845,7 @@ exports.AssignNode = class AssignNode extends BaseNode
|
||||
# for details.
|
||||
compilePatternMatch: (o) ->
|
||||
valVar = o.scope.freeVariable()
|
||||
value = if @value.isStatement() then ClosureNode.wrap(@value) else @value
|
||||
value = if @value.isStatement(o) then ClosureNode.wrap(@value) else @value
|
||||
assigns = ["#{@tab}#{valVar} = #{ value.compile(o) };"]
|
||||
o.top = true
|
||||
o.asStatement = true
|
||||
@@ -842,8 +883,8 @@ exports.AssignNode = class AssignNode extends BaseNode
|
||||
l = @variable.properties.length
|
||||
range = @variable.properties[l - 1].range
|
||||
plus = if range.exclusive then '' else ' + 1'
|
||||
from = range.from.compile(o)
|
||||
to = range.to.compile(o) + ' - ' + from + plus
|
||||
from = if range.from then range.from.compile(o) else '0'
|
||||
to = if range.to then range.to.compile(o) + ' - ' + from + plus else "#{name}.length"
|
||||
val = @value.compile(o)
|
||||
"#{name}.splice.apply(#{name}, [#{from}, #{to}].concat(#{val}))"
|
||||
|
||||
@@ -858,9 +899,11 @@ exports.CodeNode = class CodeNode extends BaseNode
|
||||
children: ['params', 'body']
|
||||
|
||||
constructor: (@params, @body, tag) ->
|
||||
@params or= []
|
||||
@body or= new Expressions
|
||||
@bound = tag is 'boundfunc'
|
||||
super()
|
||||
@params or= []
|
||||
@body or= new Expressions
|
||||
@bound = tag is 'boundfunc'
|
||||
@context = 'this' if @bound
|
||||
|
||||
# Compilation creates a new scope unless explicitly asked to share with the
|
||||
# outer scope. Handles splat parameters in the parameter list by peeking at
|
||||
@@ -902,7 +945,7 @@ exports.CodeNode = class CodeNode extends BaseNode
|
||||
(o.scope.parameter(param)) for param in params
|
||||
code = if @body.expressions.length then "\n#{ @body.compileWithDeclarations(o) }\n" else ''
|
||||
func = "function(#{ params.join(', ') }) {#{code}#{ code and @tab }}"
|
||||
return "#{utility('bind')}(#{func}, this)" if @bound
|
||||
return "#{utility('bind')}(#{func}, #{@context})" if @bound
|
||||
if top then "(#{func})" else func
|
||||
|
||||
topSensitive: ->
|
||||
@@ -928,6 +971,7 @@ exports.ParamNode = class ParamNode extends BaseNode
|
||||
children: ['name']
|
||||
|
||||
constructor: (@name, @attach, @splat) ->
|
||||
super()
|
||||
@value = literal @name
|
||||
|
||||
compileNode: (o) ->
|
||||
@@ -946,6 +990,7 @@ exports.SplatNode = class SplatNode extends BaseNode
|
||||
children: ['name']
|
||||
|
||||
constructor: (name) ->
|
||||
super()
|
||||
name = literal(name) unless name.compile
|
||||
@name = name
|
||||
|
||||
@@ -986,7 +1031,7 @@ exports.SplatNode = class SplatNode extends BaseNode
|
||||
for arg, i in list
|
||||
code = arg.compile o
|
||||
prev = args[last = args.length - 1]
|
||||
if not (arg instanceof SplatNode)
|
||||
if arg not instanceof SplatNode
|
||||
if prev and starts(prev, '[') and ends(prev, ']')
|
||||
args[last] = "#{prev.substr(0, prev.length - 1)}, #{code}]"
|
||||
continue
|
||||
@@ -1010,6 +1055,7 @@ exports.WhileNode = class WhileNode extends BaseNode
|
||||
isStatement: -> yes
|
||||
|
||||
constructor: (condition, opts) ->
|
||||
super()
|
||||
if opts and opts.invert
|
||||
condition = new ParentheticalNode condition if condition instanceof OpNode
|
||||
condition = new OpNode('!', condition)
|
||||
@@ -1034,6 +1080,7 @@ exports.WhileNode = class WhileNode extends BaseNode
|
||||
top = del(o, 'top') and not @returns
|
||||
o.indent = @idt 1
|
||||
o.top = true
|
||||
@condition.parenthetical = yes
|
||||
cond = @condition.compile(o)
|
||||
set = ''
|
||||
unless top
|
||||
@@ -1055,10 +1102,14 @@ exports.WhileNode = class WhileNode extends BaseNode
|
||||
exports.OpNode = class OpNode extends BaseNode
|
||||
|
||||
# The map of conversions from CoffeeScript to JavaScript symbols.
|
||||
CONVERSIONS: {
|
||||
CONVERSIONS:
|
||||
'==': '==='
|
||||
'!=': '!=='
|
||||
}
|
||||
|
||||
# The map of invertible operators.
|
||||
INVERSIONS:
|
||||
'!==': '==='
|
||||
'===': '!=='
|
||||
|
||||
# The list of operators for which we perform
|
||||
# [Python-style comparison chaining](http://docs.python.org/reference/expressions.html#notin).
|
||||
@@ -1074,26 +1125,41 @@ exports.OpNode = class OpNode extends BaseNode
|
||||
children: ['first', 'second']
|
||||
|
||||
constructor: (@operator, @first, @second, flip) ->
|
||||
super()
|
||||
@operator = @CONVERSIONS[@operator] or @operator
|
||||
@flip = !!flip
|
||||
if @first instanceof ValueNode and @first.base instanceof ObjectNode
|
||||
@first = new ParentheticalNode @first
|
||||
@first.tags.operation = yes
|
||||
@second.tags.operation = yes if @second
|
||||
|
||||
isUnary: ->
|
||||
not @second
|
||||
|
||||
isInvertible: ->
|
||||
(@operator in ['===', '!==']) and
|
||||
not (@first instanceof OpNode) and not (@second instanceof OpNode)
|
||||
|
||||
|
||||
isMutator: ->
|
||||
ends(@operator, '=') and not (@operator in ['===', '!=='])
|
||||
|
||||
isChainable: ->
|
||||
indexOf(@CHAINABLE, @operator) >= 0
|
||||
include(@CHAINABLE, @operator)
|
||||
|
||||
invert: ->
|
||||
@operator = @INVERSIONS[@operator]
|
||||
|
||||
toString: (idt) ->
|
||||
super(idt, @class + ' ' + @operator)
|
||||
|
||||
compileNode: (o) ->
|
||||
o.operation = true
|
||||
return @compileChain(o) if @isChainable() and @first.unwrap() instanceof OpNode and @first.unwrap().isChainable()
|
||||
return @compileAssignment(o) if indexOf(@ASSIGNMENT, @operator) >= 0
|
||||
return @compileUnary(o) if @isUnary()
|
||||
return @compileExistence(o) if @operator is '?'
|
||||
@first = new ParentheticalNode(@first) if @first instanceof OpNode and @first.isMutator()
|
||||
@second = new ParentheticalNode(@second) if @second instanceof OpNode and @second.isMutator()
|
||||
[@first.compile(o), @operator, @second.compile(o)].join ' '
|
||||
|
||||
# Mimic Python's chained comparisons when multiple comparison operators are
|
||||
@@ -1115,15 +1181,14 @@ exports.OpNode = class OpNode extends BaseNode
|
||||
second = @second.compile o
|
||||
second = "(#{second})" if @second instanceof OpNode
|
||||
o.scope.find(first) if first.match(IDENTIFIER)
|
||||
return "#{first} = #{ ExistenceNode.compileTest(o, literal(firstVar)) } ? #{firstVar} : #{second}" if @operator is '?='
|
||||
"#{first} = #{firstVar} #{ @operator.substr(0, 2) } #{second}"
|
||||
return "#{first} = #{ ExistenceNode.compileTest(o, literal(firstVar))[0] } ? #{firstVar} : #{second}" if @operator is '?='
|
||||
"#{first} #{ @operator.substr(0, 2) } (#{firstVar} = #{second})"
|
||||
|
||||
# If this is an existence operator, we delegate to `ExistenceNode.compileTest`
|
||||
# to give us the safe references for the variables.
|
||||
compileExistence: (o) ->
|
||||
[first, second] = [@first.compile(o), @second.compile(o)]
|
||||
test = ExistenceNode.compileTest(o, @first)
|
||||
"#{test} ? #{first} : #{second}"
|
||||
[test, ref] = ExistenceNode.compileTest(o, @first)
|
||||
"#{test} ? #{ref} : #{ @second.compile(o) }"
|
||||
|
||||
# Compile a unary **OpNode**.
|
||||
compileUnary: (o) ->
|
||||
@@ -1139,6 +1204,7 @@ exports.InNode = class InNode extends BaseNode
|
||||
children: ['object', 'array']
|
||||
|
||||
constructor: (@object, @array) ->
|
||||
super()
|
||||
|
||||
isArray: ->
|
||||
@array instanceof ValueNode and @array.isArray()
|
||||
@@ -1168,6 +1234,7 @@ exports.TryNode = class TryNode extends BaseNode
|
||||
isStatement: -> yes
|
||||
|
||||
constructor: (@attempt, @error, @recovery, @ensure) ->
|
||||
super()
|
||||
|
||||
makeReturn: ->
|
||||
@attempt = @attempt.makeReturn() if @attempt
|
||||
@@ -1195,6 +1262,7 @@ exports.ThrowNode = class ThrowNode extends BaseNode
|
||||
isStatement: -> yes
|
||||
|
||||
constructor: (@expression) ->
|
||||
super()
|
||||
|
||||
# A **ThrowNode** is already a return, of sorts...
|
||||
makeReturn: ->
|
||||
@@ -1214,16 +1282,18 @@ exports.ExistenceNode = class ExistenceNode extends BaseNode
|
||||
children: ['expression']
|
||||
|
||||
constructor: (@expression) ->
|
||||
super()
|
||||
|
||||
compileNode: (o) ->
|
||||
ExistenceNode.compileTest(o, @expression)
|
||||
test = ExistenceNode.compileTest(o, @expression)[0]
|
||||
if @parenthetical then test.substring(1, test.length - 1) else test
|
||||
|
||||
# The meat of the **ExistenceNode** is in this static `compileTest` method
|
||||
# because other nodes like to check the existence of their variables as well.
|
||||
# Be careful not to double-evaluate anything.
|
||||
@compileTest: (o, variable) ->
|
||||
[first, second] = variable.compileReference o
|
||||
"(typeof #{first.compile(o)} !== \"undefined\" && #{second.compile(o)} !== null)"
|
||||
[first, second] = variable.compileReference o, precompile: yes
|
||||
["(typeof #{first} !== \"undefined\" && #{second} !== null)", second]
|
||||
|
||||
#### ParentheticalNode
|
||||
|
||||
@@ -1238,9 +1308,10 @@ exports.ParentheticalNode = class ParentheticalNode extends BaseNode
|
||||
children: ['expression']
|
||||
|
||||
constructor: (@expression) ->
|
||||
super()
|
||||
|
||||
isStatement: ->
|
||||
@expression.isStatement()
|
||||
isStatement: (o) ->
|
||||
@expression.isStatement(o)
|
||||
|
||||
makeReturn: ->
|
||||
@expression.makeReturn()
|
||||
@@ -1250,12 +1321,12 @@ exports.ParentheticalNode = class ParentheticalNode extends BaseNode
|
||||
|
||||
compileNode: (o) ->
|
||||
top = del o, 'top'
|
||||
@expression.parenthetical = true
|
||||
code = @expression.compile(o)
|
||||
if @isStatement()
|
||||
return (if top then @tab + code + ';' else code)
|
||||
l = code.length
|
||||
code = code.substr(o, l-1) if code.substr(l-1, 1) is ';'
|
||||
if @expression instanceof AssignNode then code else "(#{code})"
|
||||
return code if top and @expression.isPureStatement o
|
||||
if @parenthetical or @isStatement o
|
||||
return if top then @tab + code + ';' else code
|
||||
"(#{code})"
|
||||
|
||||
#### ForNode
|
||||
|
||||
@@ -1273,6 +1344,7 @@ exports.ForNode = class ForNode extends BaseNode
|
||||
isStatement: -> yes
|
||||
|
||||
constructor: (@body, source, @name, @index) ->
|
||||
super()
|
||||
@index or= null
|
||||
@source = source.source
|
||||
@guard = source.guard
|
||||
@@ -1308,8 +1380,8 @@ exports.ForNode = class ForNode extends BaseNode
|
||||
scope = o.scope
|
||||
name = (@name and @name.compile(o)) or scope.freeVariable()
|
||||
index = @index and @index.compile o
|
||||
scope.find name if name and not @pattern and (range or not codeInBody)
|
||||
scope.find index if index
|
||||
scope.find(name, immediate: yes) if name and not @pattern and (range or not codeInBody)
|
||||
scope.find(index, immediate: yes) if index
|
||||
rvar = scope.freeVariable() unless topLevel
|
||||
ivar = if codeInBody then scope.freeVariable() else if range then name else index or scope.freeVariable()
|
||||
varPart = ''
|
||||
@@ -1350,23 +1422,64 @@ exports.ForNode = class ForNode extends BaseNode
|
||||
vars = if range then name else "#{name}, #{ivar}"
|
||||
"#{sourcePart}for (#{forPart}) {#{guardPart}\n#{varPart}#{body}\n#{@tab}}#{returnResult}"
|
||||
|
||||
#### SwitchNode
|
||||
|
||||
# A JavaScript *switch* statement. Converts into a returnable expression on-demand.
|
||||
exports.SwitchNode = class SwitchNode extends BaseNode
|
||||
|
||||
class: 'SwitchNode'
|
||||
children: ['subject', 'cases', 'otherwise']
|
||||
|
||||
isStatement: -> yes
|
||||
|
||||
constructor: (@subject, @cases, @otherwise) ->
|
||||
super()
|
||||
@tags.subjectless = !@subject
|
||||
@subject or= literal 'true'
|
||||
|
||||
makeReturn: ->
|
||||
pair[1].makeReturn() for pair in @cases
|
||||
@otherwise.makeReturn() if @otherwise
|
||||
this
|
||||
|
||||
compileNode: (o) ->
|
||||
idt = o.indent = @idt 1
|
||||
o.top = yes
|
||||
code = "#{ @tab }switch (#{ @subject.compile o }) {"
|
||||
for pair in @cases
|
||||
[conditions, block] = pair
|
||||
exprs = block.expressions
|
||||
for condition in flatten [conditions]
|
||||
condition = new OpNode '!!', new ParentheticalNode condition if @tags.subjectless
|
||||
code += "\n#{ @tab }case #{ condition.compile o }:"
|
||||
code += "\n#{ block.compile o }"
|
||||
code += "\n#{ idt }break;" unless exprs[exprs.length - 1] instanceof ReturnNode
|
||||
if @otherwise
|
||||
code += "\n#{ @tab }default:\n#{ @otherwise.compile o }"
|
||||
code += "\n#{ @tab }}"
|
||||
code
|
||||
|
||||
#### IfNode
|
||||
|
||||
# *If/else* statements. Our *switch/when* will be compiled into this. Acts as an
|
||||
# expression by pushing down requested returns to the last line of each clause.
|
||||
# *If/else* statements. Acts as an expression by pushing down requested returns
|
||||
# to the last line of each clause.
|
||||
#
|
||||
# Single-expression **IfNodes** are compiled into ternary operators if possible,
|
||||
# because ternaries are already proper expressions, and don't need conversion.
|
||||
exports.IfNode = class IfNode extends BaseNode
|
||||
|
||||
class: 'IfNode'
|
||||
children: ['condition', 'switchSubject', 'body', 'elseBody', 'assigner']
|
||||
children: ['condition', 'body', 'elseBody', 'assigner']
|
||||
|
||||
topSensitive: -> true
|
||||
|
||||
constructor: (@condition, @body, @tags) ->
|
||||
@tags or= {}
|
||||
@condition = new OpNode('!', new ParentheticalNode(@condition)) if @tags.invert
|
||||
@tags or= {}
|
||||
if @tags.invert
|
||||
if @condition instanceof OpNode and @condition.isInvertible()
|
||||
@condition.invert()
|
||||
else
|
||||
@condition = new OpNode '!', new ParentheticalNode @condition
|
||||
@elseBody = null
|
||||
@isChain = false
|
||||
|
||||
@@ -1377,28 +1490,6 @@ exports.IfNode = class IfNode extends BaseNode
|
||||
@tags.statement = true
|
||||
this
|
||||
|
||||
# Tag a chain of **IfNodes** with their object(s) to switch on for equality
|
||||
# tests. `rewriteSwitch` will perform the actual change at compile time.
|
||||
switchesOver: (expression) ->
|
||||
@switchSubject = expression
|
||||
this
|
||||
|
||||
# Rewrite a chain of **IfNodes** with their switch condition for equality.
|
||||
# Ensure that the switch expression isn't evaluated more than once.
|
||||
rewriteSwitch: (o) ->
|
||||
@assigner = @switchSubject
|
||||
unless (@switchSubject.unwrap() instanceof LiteralNode)
|
||||
variable = literal(o.scope.freeVariable())
|
||||
@assigner = new AssignNode(variable, @switchSubject)
|
||||
@switchSubject = variable
|
||||
@condition = for cond, i in flatten [@condition]
|
||||
cond = new ParentheticalNode(cond) if cond instanceof OpNode
|
||||
new OpNode('==', (if i is 0 then @assigner else @switchSubject), cond)
|
||||
@elseBodyNode().switchesOver(@switchSubject) if @isChain
|
||||
# prevent this rewrite from happening again
|
||||
@switchSubject = undefined
|
||||
this
|
||||
|
||||
# Rewrite a chain of **IfNodes** to add a default case as the final *else*.
|
||||
addElse: (elseBody, statement) ->
|
||||
if @isChain
|
||||
@@ -1410,14 +1501,16 @@ exports.IfNode = class IfNode extends BaseNode
|
||||
|
||||
# The **IfNode** only compiles into a statement if either of its bodies needs
|
||||
# to be a statement. Otherwise a ternary is safe.
|
||||
isStatement: ->
|
||||
@statement or= !!(@tags.statement or @bodyNode().isStatement() or (@elseBody and @elseBodyNode().isStatement()))
|
||||
isStatement: (o) ->
|
||||
@statement or= !!((o and o.top) or @tags.statement or @bodyNode().isStatement(o) or (@elseBody and @elseBodyNode().isStatement(o)))
|
||||
|
||||
compileCondition: (o) ->
|
||||
(cond.compile(o) for cond in flatten([@condition])).join(' || ')
|
||||
conditions = flatten [@condition]
|
||||
conditions[0].parenthetical = yes if conditions.length is 1
|
||||
(cond.compile(o) for cond in conditions).join(' || ')
|
||||
|
||||
compileNode: (o) ->
|
||||
if o.top or @isStatement() then @compileStatement(o) else @compileTernary(o)
|
||||
if @isStatement(o) then @compileStatement(o) else @compileTernary(o)
|
||||
|
||||
makeReturn: ->
|
||||
if @isStatement()
|
||||
@@ -1433,13 +1526,12 @@ exports.IfNode = class IfNode extends BaseNode
|
||||
# Compile the **IfNode** as a regular *if-else* statement. Flattened chains
|
||||
# force inner *else* bodies into statement form.
|
||||
compileStatement: (o) ->
|
||||
@rewriteSwitch(o) if @switchSubject
|
||||
top = del o, 'top'
|
||||
child = del o, 'chainChild'
|
||||
condO = merge o
|
||||
o.indent = @idt 1
|
||||
o.top = true
|
||||
ifDent = if child or (top and not @isStatement()) then '' else @idt()
|
||||
ifDent = if child or (top and not @isStatement(o)) then '' else @idt()
|
||||
comDent = if child then @idt() else ''
|
||||
body = @body.compile(o)
|
||||
ifPart = "#{ifDent}if (#{ @compileCondition(condO) }) {\n#{body}\n#{@tab}}"
|
||||
@@ -1452,10 +1544,12 @@ exports.IfNode = class IfNode extends BaseNode
|
||||
|
||||
# Compile the IfNode as a ternary operator.
|
||||
compileTernary: (o) ->
|
||||
o.operation = true
|
||||
@bodyNode().tags.operation = @condition.tags.operation = yes
|
||||
@elseBodyNode().tags.operation = yes if @elseBody
|
||||
ifPart = @condition.compile(o) + ' ? ' + @bodyNode().compile(o)
|
||||
elsePart = if @elseBody then @elseBodyNode().compile(o) else 'null'
|
||||
"#{ifPart} : #{elsePart}"
|
||||
code = "#{ifPart} : #{elsePart}"
|
||||
if @tags.operation then "(#{code})" else code
|
||||
|
||||
# Faux-Nodes
|
||||
# ----------
|
||||
@@ -1466,8 +1560,7 @@ exports.IfNode = class IfNode extends BaseNode
|
||||
# generation to generate other combinations of nodes. The **PushNode** creates
|
||||
# the tree for `array.push(value)`, which is helpful for recording the result
|
||||
# arrays from comprehensions.
|
||||
PushNode = exports.PushNode = {
|
||||
|
||||
PushNode = exports.PushNode =
|
||||
wrap: (array, expressions) ->
|
||||
expr = expressions.unwrap()
|
||||
return expressions if expr.isPureStatement() or expr.containsPureStatement()
|
||||
@@ -1475,12 +1568,10 @@ PushNode = exports.PushNode = {
|
||||
new ValueNode(literal(array), [new AccessorNode(literal('push'))]), [expr]
|
||||
)])
|
||||
|
||||
}
|
||||
|
||||
#### ClosureNode
|
||||
|
||||
# A faux-node used to wrap an expressions body in a closure.
|
||||
ClosureNode = exports.ClosureNode = {
|
||||
ClosureNode = exports.ClosureNode =
|
||||
|
||||
# Wrap the expressions body, unless it contains a pure statement,
|
||||
# in which case, no dice. If the body mentions `this` or `arguments`,
|
||||
@@ -1502,12 +1593,10 @@ ClosureNode = exports.ClosureNode = {
|
||||
call = new CallNode(func, args)
|
||||
if statement then Expressions.wrap([call]) else call
|
||||
|
||||
}
|
||||
|
||||
# Utility Functions
|
||||
# -----------------
|
||||
|
||||
UTILITIES = {
|
||||
UTILITIES =
|
||||
|
||||
# Correctly set up a prototype chain for inheritance, including a reference
|
||||
# to the superclass for `super()` calls. See:
|
||||
@@ -1519,7 +1608,7 @@ UTILITIES = {
|
||||
child.prototype = new ctor();
|
||||
child.prototype.constructor = child;
|
||||
if (typeof parent.extended === "function") parent.extended(child);
|
||||
child.__superClass__ = parent.prototype;
|
||||
child.__super__ = parent.prototype;
|
||||
}
|
||||
"""
|
||||
|
||||
@@ -1534,8 +1623,6 @@ UTILITIES = {
|
||||
hasProp: 'Object.prototype.hasOwnProperty'
|
||||
slice: 'Array.prototype.slice'
|
||||
|
||||
}
|
||||
|
||||
# Constants
|
||||
# ---------
|
||||
|
||||
@@ -1549,7 +1636,7 @@ TRAILING_WHITESPACE = /[ \t]+$/gm
|
||||
# Keep these identifier regexes in sync with the Lexer.
|
||||
IDENTIFIER = /^[a-zA-Z\$_](\w|\$)*$/
|
||||
NUMBER = /^(((\b0(x|X)[0-9a-fA-F]+)|((\b[0-9]+(\.[0-9]+)?|\.[0-9]+)(e[+\-]?[0-9]+)?)))\b$/i
|
||||
SIMPLENUM = /^-?\d+/
|
||||
SIMPLENUM = /^-?\d+$/
|
||||
|
||||
# Is a literal value a string?
|
||||
IS_STRING = /^['"]/
|
||||
|
||||
@@ -35,6 +35,7 @@ exports.Rewriter = class Rewriter
|
||||
@closeOpenCalls()
|
||||
@closeOpenIndexes()
|
||||
@addImplicitIndentation()
|
||||
@tagPostfixConditionals()
|
||||
@addImplicitBraces()
|
||||
@addImplicitParentheses()
|
||||
@ensureBalance BALANCED_PAIRS
|
||||
@@ -50,7 +51,7 @@ exports.Rewriter = class Rewriter
|
||||
i = 0
|
||||
loop
|
||||
break unless @tokens[i]
|
||||
move = block @tokens[i], i
|
||||
move = block.call this, @tokens[i], i
|
||||
i += move
|
||||
true
|
||||
|
||||
@@ -68,7 +69,7 @@ exports.Rewriter = class Rewriter
|
||||
# Massage newlines and indentations so that comments don't have to be
|
||||
# correctly indented, or appear on a line of their own.
|
||||
adjustComments: ->
|
||||
@scanTokens (token, i) =>
|
||||
@scanTokens (token, i) ->
|
||||
return 1 unless token[0] is 'HERECOMMENT'
|
||||
[before, prev, post, after] = [@tokens[i - 2], @tokens[i - 1], @tokens[i + 1], @tokens[i + 2]]
|
||||
if after and after[0] is 'INDENT'
|
||||
@@ -95,25 +96,29 @@ exports.Rewriter = class Rewriter
|
||||
# Some blocks occur in the middle of expressions -- when we're expecting
|
||||
# this, remove their trailing newlines.
|
||||
removeMidExpressionNewlines: ->
|
||||
@scanTokens (token, i) =>
|
||||
@scanTokens (token, i) ->
|
||||
return 1 unless include(EXPRESSION_CLOSE, @tag(i + 1)) and token[0] is 'TERMINATOR'
|
||||
@tokens.splice i, 1
|
||||
return 0
|
||||
|
||||
# The lexer has tagged the opening parenthesis of a method call. Match it with
|
||||
# its paired close.
|
||||
# its paired close. We have the mis-nested outdent case included here for
|
||||
# calls that close on the same line, just before their outdent.
|
||||
closeOpenCalls: ->
|
||||
@scanTokens (token, i) =>
|
||||
@scanTokens (token, i) ->
|
||||
if token[0] is 'CALL_START'
|
||||
condition = (token, i) -> token[0] in [')', 'CALL_END']
|
||||
action = (token, i) -> token[0] = 'CALL_END'
|
||||
condition = (token, i) ->
|
||||
(token[0] in [')', 'CALL_END']) or (token[0] is 'OUTDENT' and @tokens[i - 1][0] is ')')
|
||||
action = (token, i) ->
|
||||
idx = if token[0] is 'OUTDENT' then i - 1 else i
|
||||
@tokens[idx][0] = 'CALL_END'
|
||||
@detectEnd i + 1, condition, action
|
||||
return 1
|
||||
|
||||
# The lexer has tagged the opening parenthesis of an indexing operation call.
|
||||
# Match it with its paired close.
|
||||
closeOpenIndexes: ->
|
||||
@scanTokens (token, i) =>
|
||||
@scanTokens (token, i) ->
|
||||
if token[0] is 'INDEX_START'
|
||||
condition = (token, i) -> token[0] in [']', 'INDEX_END']
|
||||
action = (token, i) -> token[0] = 'INDEX_END'
|
||||
@@ -124,7 +129,7 @@ exports.Rewriter = class Rewriter
|
||||
# Insert the missing braces here, so that the parser doesn't have to.
|
||||
addImplicitBraces: ->
|
||||
stack = []
|
||||
@scanTokens (token, i) =>
|
||||
@scanTokens (token, i) ->
|
||||
if include EXPRESSION_START, token[0]
|
||||
stack.push(if (token[0] is 'INDENT' and (@tag(i - 1) is '{')) then '{' else token[0])
|
||||
if include EXPRESSION_END, token[0]
|
||||
@@ -133,7 +138,9 @@ exports.Rewriter = class Rewriter
|
||||
if token[0] is ':' and (not last or last[0] isnt '{')
|
||||
stack.push '{'
|
||||
idx = if @tag(i - 2) is '@' then i - 2 else i - 1
|
||||
@tokens.splice idx, 0, ['{', '{', token[2]]
|
||||
tok = ['{', '{', token[2]]
|
||||
tok.generated = yes
|
||||
@tokens.splice idx, 0, tok
|
||||
condition = (token, i) ->
|
||||
[one, two, three] = @tokens.slice(i + 1, i + 4)
|
||||
return false if 'HERECOMMENT' in [@tag(i + 1), @tag(i - 1)]
|
||||
@@ -145,24 +152,35 @@ exports.Rewriter = class Rewriter
|
||||
return 2
|
||||
return 1
|
||||
|
||||
|
||||
# Methods may be optionally called without parentheses, for simple cases.
|
||||
# Insert the implicit parentheses here, so that the parser doesn't have to
|
||||
# deal with them.
|
||||
addImplicitParentheses: ->
|
||||
@scanTokens (token, i) =>
|
||||
prev = @tokens[i - 1]
|
||||
if prev and prev.spaced and include(IMPLICIT_FUNC, prev[0]) and include(IMPLICIT_CALL, token[0]) and
|
||||
not (token[0] is '!' and (@tag(i + 1) in ['IN', 'OF']))
|
||||
classLine = no
|
||||
@scanTokens (token, i) ->
|
||||
classLine = yes if token[0] is 'CLASS'
|
||||
prev = @tokens[i - 1]
|
||||
next = @tokens[i + 1]
|
||||
idx = 1
|
||||
callObject = not classLine and token[0] is 'INDENT' and next and next.generated and next[0] is '{' and prev and include(IMPLICIT_FUNC, prev[0])
|
||||
idx = 2 if callObject
|
||||
seenSingle = no
|
||||
classLine = no if include(LINEBREAKS, token[0])
|
||||
token.call = yes if prev and not prev.spaced and token[0] is '?'
|
||||
if prev and (prev.spaced and (include(IMPLICIT_FUNC, prev[0]) or prev.call) and include(IMPLICIT_CALL, token[0]) and
|
||||
not (token[0] is 'UNARY' and (@tag(i + 1) in ['IN', 'OF', 'INSTANCEOF']))) or callObject
|
||||
@tokens.splice i, 0, ['CALL_START', '(', token[2]]
|
||||
condition = (token, i) ->
|
||||
return yes if not seenSingle and token.fromThen
|
||||
seenSingle = yes if token[0] in ['IF', 'ELSE', 'UNLESS', '->', '=>']
|
||||
(not token.generated and @tokens[i - 1][0] isnt ',' and include(IMPLICIT_END, token[0]) and
|
||||
not (token[0] is 'INDENT' and (include(IMPLICIT_BLOCK, @tag(i - 1)) or @tag(i - 2) is 'CLASS'))) or
|
||||
not (token[0] is 'INDENT' and (include(IMPLICIT_BLOCK, @tag(i - 1)) or @tag(i - 2) is 'CLASS' or @tag(i + 1) is '{'))) or
|
||||
token[0] is 'PROPERTY_ACCESS' and @tag(i - 1) is 'OUTDENT'
|
||||
action = (token, i) ->
|
||||
idx = if token[0] is 'OUTDENT' then i + 1 else i
|
||||
@tokens.splice idx, 0, ['CALL_END', ')', token[2]]
|
||||
@detectEnd i + 1, condition, action
|
||||
@detectEnd i + idx, condition, action
|
||||
prev[0] = 'FUNC_EXIST' if prev[0] is '?'
|
||||
return 2
|
||||
return 1
|
||||
|
||||
@@ -171,19 +189,20 @@ exports.Rewriter = class Rewriter
|
||||
# blocks, so it doesn't need to. ')' can close a single-line block,
|
||||
# but we need to make sure it's balanced.
|
||||
addImplicitIndentation: ->
|
||||
@scanTokens (token, i) =>
|
||||
@scanTokens (token, i) ->
|
||||
if token[0] is 'ELSE' and @tag(i - 1) isnt 'OUTDENT'
|
||||
@tokens.splice i, 0, @indentation(token)...
|
||||
return 2
|
||||
if token[0] is 'CATCH' and
|
||||
(@tokens[i + 2][0] is 'TERMINATOR' or @tokens[i + 2][0] is 'FINALLY')
|
||||
(@tag(i + 2) is 'TERMINATOR' or @tag(i + 2) is 'FINALLY')
|
||||
@tokens.splice i + 2, 0, @indentation(token)...
|
||||
return 4
|
||||
if include(SINGLE_LINERS, token[0]) and @tag(i + 1) isnt 'INDENT' and
|
||||
not (token[0] is 'ELSE' and @tag(i + 1) is 'IF')
|
||||
starter = token[0]
|
||||
[indent, outdent] = @indentation token
|
||||
indent.generated = outdent.generated = true
|
||||
indent.fromThen = true if starter is 'THEN'
|
||||
indent.generated = outdent.generated = true
|
||||
@tokens.splice i + 1, 0, indent
|
||||
condition = (token, i) ->
|
||||
(include(SINGLE_CLOSERS, token[0]) and token[1] isnt ';') and
|
||||
@@ -196,12 +215,26 @@ exports.Rewriter = class Rewriter
|
||||
return 2
|
||||
return 1
|
||||
|
||||
# Tag postfix conditionals as such, so that we can parse them with a
|
||||
# different precedence.
|
||||
tagPostfixConditionals: ->
|
||||
@scanTokens (token, i) ->
|
||||
if token[0] in ['IF', 'UNLESS']
|
||||
original = token
|
||||
condition = (token, i) ->
|
||||
token[0] in ['TERMINATOR', 'INDENT']
|
||||
action = (token, i) ->
|
||||
original[0] = 'POST_' + original[0] if token[0] isnt 'INDENT'
|
||||
@detectEnd i + 1, condition, action
|
||||
return 1
|
||||
return 1
|
||||
|
||||
# Ensure that all listed pairs of tokens are correctly balanced throughout
|
||||
# the course of the token stream.
|
||||
ensureBalance: (pairs) ->
|
||||
levels = {}
|
||||
openLine = {}
|
||||
@scanTokens (token, i) =>
|
||||
@scanTokens (token, i) ->
|
||||
for pair in pairs
|
||||
[open, close] = pair
|
||||
levels[open] or= 0
|
||||
@@ -237,7 +270,7 @@ exports.Rewriter = class Rewriter
|
||||
stack = []
|
||||
debt = {}
|
||||
(debt[key] = 0) for key, val of INVERSES
|
||||
@scanTokens (token, i) =>
|
||||
@scanTokens (token, i) ->
|
||||
tag = token[0]
|
||||
inv = INVERSES[token[0]]
|
||||
if include EXPRESSION_START, tag
|
||||
@@ -296,23 +329,26 @@ EXPRESSION_END = pair[1] for pair in BALANCED_PAIRS
|
||||
EXPRESSION_CLOSE = ['CATCH', 'WHEN', 'ELSE', 'FINALLY'].concat EXPRESSION_END
|
||||
|
||||
# Tokens that, if followed by an `IMPLICIT_CALL`, indicate a function invocation.
|
||||
IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@']
|
||||
IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS']
|
||||
|
||||
# If preceded by an `IMPLICIT_FUNC`, indicates a function invocation.
|
||||
IMPLICIT_CALL = [
|
||||
'IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS',
|
||||
'TRY', 'DELETE', 'TYPEOF', 'SWITCH', 'THIS', 'NULL',
|
||||
'IF', 'UNLESS', 'TRY', 'SWITCH', 'THIS', 'NULL', 'UNARY'
|
||||
'TRUE', 'FALSE', 'YES', 'NO', 'ON', 'OFF',
|
||||
'!', '!!', '@', '->', '=>', '[', '(', '{'
|
||||
'@', '->', '=>', '[', '(', '{'
|
||||
]
|
||||
|
||||
# Tokens indicating that the implicit call must enclose a block of expressions.
|
||||
IMPLICIT_BLOCK = ['->', '=>', '{', '[', ',']
|
||||
|
||||
# Tokens that always mark the end of an implicit call for single-liners.
|
||||
IMPLICIT_END = ['IF', 'UNLESS', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'TERMINATOR', 'INDENT']
|
||||
IMPLICIT_END = ['POST_IF', 'POST_UNLESS', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'TERMINATOR', 'INDENT']
|
||||
|
||||
# Single-line flavors of block expressions that have unclosed endings.
|
||||
# The grammar can't disambiguate them, so we insert the implicit indentation.
|
||||
SINGLE_LINERS = ['ELSE', "->", "=>", 'TRY', 'FINALLY', 'THEN']
|
||||
SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN']
|
||||
|
||||
# Tokens that end a line.
|
||||
LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT']
|
||||
|
||||
@@ -28,8 +28,8 @@ exports.Scope = class Scope
|
||||
|
||||
# Look up a variable name in lexical scope, and declare it if it does not
|
||||
# already exist.
|
||||
find: (name) ->
|
||||
return true if @check name
|
||||
find: (name, options) ->
|
||||
return true if @check name, options
|
||||
@variables[name] = 'var'
|
||||
false
|
||||
|
||||
@@ -44,9 +44,11 @@ exports.Scope = class Scope
|
||||
parameter: (name) ->
|
||||
@variables[name] = 'param'
|
||||
|
||||
# Just check to see if a variable has already been declared, without reserving.
|
||||
check: (name) ->
|
||||
return true if @variables.hasOwnProperty name
|
||||
# Just check to see if a variable has already been declared, without reserving,
|
||||
# walks up to the root scope.
|
||||
check: (name, options) ->
|
||||
immediate = Object::hasOwnProperty.call @variables, name
|
||||
return immediate if immediate or (options and options.immediate)
|
||||
!!(@parent and @parent.check(name))
|
||||
|
||||
# If we need to store an intermediate result, find an available name for a
|
||||
|
||||
@@ -8,11 +8,10 @@ ok results.join(' ') is '1 4 9'
|
||||
# Chained blocks, with proper indentation levels:
|
||||
results = []
|
||||
|
||||
counter = {
|
||||
counter =
|
||||
tick: (func) ->
|
||||
results.push func()
|
||||
this
|
||||
}
|
||||
|
||||
counter
|
||||
.tick ->
|
||||
@@ -53,3 +52,17 @@ obj
|
||||
)
|
||||
|
||||
ok result is 3
|
||||
|
||||
|
||||
# Test newline-supressed call chains with nested functions.
|
||||
obj =
|
||||
call: -> this
|
||||
func = ->
|
||||
obj
|
||||
.call ->
|
||||
one two
|
||||
.call ->
|
||||
three four
|
||||
101
|
||||
|
||||
ok func() is 101
|
||||
|
||||
@@ -37,11 +37,20 @@ ok six is 6
|
||||
|
||||
|
||||
# Ensure that indented array literals don't trigger whitespace rewriting.
|
||||
# func = () ->
|
||||
# ok arguments.length is 1
|
||||
#
|
||||
# func(
|
||||
# [[[[[],
|
||||
# []],
|
||||
# [[]]]],
|
||||
# []])
|
||||
func = () ->
|
||||
ok arguments.length is 1
|
||||
|
||||
func(
|
||||
[[[[[],
|
||||
[]],
|
||||
[[]]]],
|
||||
[]])
|
||||
|
||||
id = (x) -> x
|
||||
|
||||
greeting = id(
|
||||
"""
|
||||
Hello
|
||||
""")
|
||||
|
||||
ok greeting is "Hello"
|
||||
|
||||
@@ -27,6 +27,13 @@ result = (new ThirdChild).func 'four'
|
||||
ok result is 'zero/one/two/three/four'
|
||||
ok Base.static('word') is 'static/word'
|
||||
|
||||
FirstChild::func = (string) ->
|
||||
super('one/').length + string
|
||||
|
||||
result = (new ThirdChild).func 'four'
|
||||
|
||||
ok result is '9two/three/four'
|
||||
|
||||
|
||||
class TopClass
|
||||
constructor: (arg) ->
|
||||
@@ -48,7 +55,11 @@ class OneClass
|
||||
|
||||
class TwoClass extends OneClass
|
||||
|
||||
ok (new TwoClass('three')).name is 'three'
|
||||
Function.prototype.new = -> new this arguments...
|
||||
|
||||
ok (TwoClass.new('three')).name is 'three'
|
||||
|
||||
delete Function.prototype.new
|
||||
|
||||
|
||||
# And now the same tests, but written in the manual style:
|
||||
@@ -131,6 +142,7 @@ ok instance.name() is 'class'
|
||||
|
||||
|
||||
# Classes with methods that are pre-bound to the instance.
|
||||
# ... or statically, to the class.
|
||||
class Dog
|
||||
|
||||
constructor: (name) ->
|
||||
@@ -139,12 +151,19 @@ class Dog
|
||||
bark: =>
|
||||
"#{@name} woofs!"
|
||||
|
||||
@static: =>
|
||||
new this('Dog')
|
||||
|
||||
spark = new Dog('Spark')
|
||||
fido = new Dog('Fido')
|
||||
fido.bark = spark.bark
|
||||
|
||||
ok fido.bark() is 'Spark woofs!'
|
||||
|
||||
obj = func: Dog.static
|
||||
|
||||
ok obj.func().name is 'Dog'
|
||||
|
||||
|
||||
# Testing a bound function in a bound function.
|
||||
class Mini
|
||||
@@ -199,13 +218,13 @@ class Element extends Base
|
||||
@node = node
|
||||
|
||||
ok Element.extended is Base.extended
|
||||
ok Element.__superClass__ is Base.prototype
|
||||
ok Element.__super__ is Base.prototype
|
||||
|
||||
class MyElement extends Element
|
||||
|
||||
ok MyElement.extended is Base.extended
|
||||
ok MyElement.fromHTML is Element.fromHTML
|
||||
ok MyElement.__superClass__ is Element.prototype
|
||||
ok MyElement.__super__ is Element.prototype
|
||||
|
||||
|
||||
# Test classes wrapped in decorators.
|
||||
|
||||
@@ -141,3 +141,14 @@ ok all.sort().join(' ') is 'Whiskers cream tabby'
|
||||
exxes = 'x' for [0...10]
|
||||
ok exxes.join(' ') is 'x x x x x x x x x x'
|
||||
|
||||
|
||||
# Comprehensions safely redeclare parameters if they're not present in closest
|
||||
# scope.
|
||||
rule = (x) -> x
|
||||
|
||||
learn = ->
|
||||
rule for rule in [1, 2, 3]
|
||||
|
||||
ok learn().join(' ') is '1 2 3'
|
||||
|
||||
ok rule(101) is 101
|
||||
@@ -17,9 +17,15 @@ ok a is 10 and b is 10
|
||||
# The existential operator.
|
||||
z = null
|
||||
x = z ? "EX"
|
||||
|
||||
ok z is null and x is "EX"
|
||||
|
||||
i = 9
|
||||
func = -> i += 1
|
||||
result = func() ? 101
|
||||
ok result is 10
|
||||
|
||||
ok (non ? existent ? variables ? 1) is 1
|
||||
|
||||
|
||||
# Only evaluate once.
|
||||
counter = 0
|
||||
@@ -69,6 +75,24 @@ result = value?.toString().toLowerCase()
|
||||
|
||||
ok result is '10'
|
||||
|
||||
ok(process.exit.nothing?.property() or 101)
|
||||
|
||||
counter = 0
|
||||
func = ->
|
||||
counter += 1
|
||||
'prop'
|
||||
obj =
|
||||
prop: -> this
|
||||
value: 25
|
||||
|
||||
ok obj[func()]()[func()]()[func()]()?.value is 25
|
||||
ok counter is 3
|
||||
|
||||
|
||||
# Soaks inner values.
|
||||
ident = (obj) -> obj
|
||||
ok ident(non?.existent().method()) is undefined
|
||||
|
||||
|
||||
# Soaks constructor invocations.
|
||||
a = 0
|
||||
@@ -98,3 +122,20 @@ ok x is - 1
|
||||
# Things that compile to ternaries should force parentheses, like operators do.
|
||||
duration = if options?.animated then 150 else 0
|
||||
ok duration is 0
|
||||
|
||||
|
||||
# function soak
|
||||
plus1 = (x) -> x + 1
|
||||
|
||||
ok plus1?(41) is 42
|
||||
ok (plus1? 41) is 42
|
||||
ok plus2?(41) is undefined
|
||||
ok (plus2? 41) is undefined
|
||||
|
||||
maybe_close = (f, arg) -> if typeof f is 'function' then () -> f(arg) else -1
|
||||
|
||||
ok maybe_close(plus1, 41)?() is 42
|
||||
ok (maybe_close plus1, 41)?() is 42
|
||||
ok (maybe_close 'string', 41)?() is undefined
|
||||
|
||||
ok 2?(3) is undefined
|
||||
|
||||
@@ -25,4 +25,16 @@ obj = {
|
||||
}
|
||||
|
||||
ok obj.num is obj.func()
|
||||
ok obj.num is obj.result
|
||||
ok obj.num is obj.result
|
||||
|
||||
|
||||
# Should be able to look at prototypes on keywords.
|
||||
obj =
|
||||
withAt: -> @::prop
|
||||
withThis: -> this::prop
|
||||
proto:
|
||||
prop: 100
|
||||
|
||||
obj.prototype = obj.proto
|
||||
ok obj.withAt() is 100
|
||||
ok obj.withThis() is 100
|
||||
@@ -220,3 +220,27 @@ obj =
|
||||
ok obj.func(101, 102, 103, 104) is undefined
|
||||
ok obj.param is 101
|
||||
ok obj.rest.join(' ') is '102 103 104'
|
||||
|
||||
|
||||
# `@` and `this` should both be able to invoke a method.
|
||||
func = (arg) -> ok arg is true
|
||||
func.withAt = -> @ true
|
||||
func.withThis = -> this true
|
||||
|
||||
func.withAt()
|
||||
func.withThis()
|
||||
|
||||
|
||||
# Ensure that constructors invoked with splats return a new object.
|
||||
args = [1, 2, 3]
|
||||
Type = (@args) ->
|
||||
type = new Type args...
|
||||
|
||||
ok type and type instanceof Type
|
||||
ok v is args[i] for v, i in type.args
|
||||
|
||||
|
||||
# Ensure that constructors invoked with splats cache the function.
|
||||
called = 0
|
||||
get = -> if called++ then false else class Type
|
||||
new get() args...
|
||||
|
||||
@@ -15,6 +15,15 @@ a = '''
|
||||
ok a is "a\n \"b\nc"
|
||||
|
||||
|
||||
a = """
|
||||
a
|
||||
b
|
||||
c
|
||||
"""
|
||||
|
||||
ok a is "a\n b\n c"
|
||||
|
||||
|
||||
a = '''one-liner'''
|
||||
|
||||
ok a is 'one-liner'
|
||||
|
||||
@@ -64,10 +64,20 @@ ok result is undefined
|
||||
|
||||
# Return an if with no else.
|
||||
func = ->
|
||||
return (if false then callback())
|
||||
return if false then callback()
|
||||
|
||||
ok func() is null
|
||||
|
||||
func = ->
|
||||
return unless false then 100 else -100
|
||||
|
||||
ok func() is 100
|
||||
|
||||
ident = (x) -> x
|
||||
result = ident if false then 300 else 100
|
||||
|
||||
ok result is 100
|
||||
|
||||
|
||||
# If-to-ternary with instanceof requires parentheses (no comment).
|
||||
if {} instanceof Object
|
||||
@@ -80,3 +90,43 @@ try
|
||||
ok yes
|
||||
catch e
|
||||
ok no
|
||||
|
||||
|
||||
# If-to-ternary as part of a larger operation requires parens.
|
||||
x = 1
|
||||
result = x + if false then 10 else 1
|
||||
ok result is 2
|
||||
|
||||
|
||||
# If/else indented within an assignment.
|
||||
func = ->
|
||||
a =
|
||||
if false
|
||||
3
|
||||
else
|
||||
5
|
||||
101
|
||||
a
|
||||
|
||||
ok func() is 5
|
||||
|
||||
|
||||
# Unmatched 'then' should catch implicit calls.
|
||||
i = 1
|
||||
isTrue = (x) -> x is true
|
||||
|
||||
if isTrue yes then i += 1
|
||||
|
||||
ok i is 2
|
||||
|
||||
# If/else with a suppressed indentation via assignment.
|
||||
result =
|
||||
if false then 10
|
||||
else if no then 20
|
||||
else if 0 then 30
|
||||
else if NaN then 40
|
||||
else 50 +
|
||||
if false then 10
|
||||
else 20
|
||||
|
||||
ok result is 70
|
||||
|
||||
@@ -60,8 +60,8 @@ ok money$ is '\(\(\(dollars\)\)\)'
|
||||
|
||||
|
||||
multiline = "one
|
||||
two
|
||||
three"
|
||||
two
|
||||
three"
|
||||
|
||||
ok multiline is 'one two three'
|
||||
|
||||
@@ -165,9 +165,31 @@ ok obj.options.value is yes
|
||||
ok obj.fn() is null
|
||||
|
||||
|
||||
# Implicit arguments to function calls:
|
||||
func = (obj) -> obj.a
|
||||
|
||||
result = func
|
||||
a: 10
|
||||
|
||||
ok result is 10
|
||||
|
||||
result = func
|
||||
"a": 20
|
||||
|
||||
ok result is 20
|
||||
|
||||
third = (a, b, c) -> c
|
||||
obj =
|
||||
one: 'one'
|
||||
two: third 'one', 'two', 'three'
|
||||
|
||||
ok obj.one is 'one'
|
||||
ok obj.two is 'three'
|
||||
|
||||
|
||||
# Implicit objects with wacky indentation:
|
||||
obj =
|
||||
reverse: (obj) ->
|
||||
'reverse': (obj) ->
|
||||
Array.prototype.reverse.call obj
|
||||
abc: ->
|
||||
@reverse(
|
||||
@@ -197,11 +219,23 @@ ok obj.misdent.toString() is ',,,'
|
||||
|
||||
second = (x, y) -> y
|
||||
obj = then second 'the',
|
||||
one: 1
|
||||
1: 1
|
||||
two:
|
||||
three: ->
|
||||
four five,
|
||||
six: seven
|
||||
three: 3
|
||||
|
||||
ok obj.three is 3
|
||||
ok obj[1] is 1
|
||||
ok obj.three is 3
|
||||
|
||||
|
||||
# Implicit objects as part of chained calls.
|
||||
identity = (x) -> x.a
|
||||
|
||||
b = identity identity identity
|
||||
a:
|
||||
a:
|
||||
a: 100
|
||||
|
||||
ok b is 100
|
||||
|
||||
@@ -95,8 +95,66 @@ count = 0
|
||||
list[key()] ?= 100
|
||||
ok list.join(' ') is '0 100 5 10'
|
||||
|
||||
count = 0
|
||||
key = ->
|
||||
count += 1
|
||||
key
|
||||
|
||||
key().val or= 100
|
||||
|
||||
ok key.val is 100
|
||||
ok count is 1
|
||||
|
||||
key().val ?= 200
|
||||
|
||||
ok key.val is 100
|
||||
ok count is 2
|
||||
|
||||
|
||||
# Ensure that RHS is treated as a group.
|
||||
a = b = false
|
||||
a and= b or true
|
||||
ok a is false
|
||||
|
||||
|
||||
# Bitwise operators:
|
||||
ok (10 & 3) is 2
|
||||
ok (10 | 3) is 11
|
||||
ok (10 ^ 3) is 9
|
||||
ok (10 << 3) is 80
|
||||
ok (10 >> 3) is 1
|
||||
ok (10 >>> 3) is 1
|
||||
|
||||
num = 10; ok (num <<= 3) is 80
|
||||
num = 10; ok (num >>= 3) is 1
|
||||
num = 10; ok (num >>>= 3) is 1
|
||||
num = 10; ok (num &= 3) is 2
|
||||
num = 10; ok (num ^= 3) is 9
|
||||
num = 10; ok (num |= 3) is 11
|
||||
|
||||
|
||||
# Compound assignment with implicit objects.
|
||||
obj = undefined
|
||||
obj ?=
|
||||
one: 1
|
||||
|
||||
ok obj.one is 1
|
||||
|
||||
obj and=
|
||||
two: 2
|
||||
|
||||
ok not obj.one
|
||||
ok obj.two is 2
|
||||
|
||||
|
||||
# Compound assignment as a sub expression.
|
||||
[a, b, c] = [1, 2, 3]
|
||||
ok (a + b += c) is 6
|
||||
ok a is 1
|
||||
ok b is 5
|
||||
ok c is 3
|
||||
|
||||
|
||||
# Instanceof.
|
||||
ok new String instanceof String
|
||||
ok new Number not instanceof String
|
||||
|
||||
@@ -56,3 +56,20 @@ array[5..10] = [0, 0, 0]
|
||||
ok array.join(' ') is '0 1 2 3 4 0 0 0'
|
||||
|
||||
|
||||
# Slices and splices that omit their beginning or end.
|
||||
array = [0..10]
|
||||
|
||||
ok array[7..].join(' ') is '7 8 9 10'
|
||||
ok array[-2..].join(' ') is '9 10'
|
||||
|
||||
ok array[...3].join(' ') is '0 1 2'
|
||||
ok array[..-5].join(' ') is '0 1 2 3 4 5 6'
|
||||
|
||||
array[3..] = [9, 8, 7]
|
||||
|
||||
ok array.join(' ') is '0 1 2 9 8 7'
|
||||
|
||||
array[...3] = [7, 8, 9]
|
||||
|
||||
ok array.join(' ') is '7 8 9 9 8 7'
|
||||
|
||||
|
||||
@@ -20,4 +20,9 @@ obj = {
|
||||
}
|
||||
id = 2
|
||||
|
||||
ok ' '.match(/ /)[0] is ' '
|
||||
|
||||
regexp = / /
|
||||
ok ' '.match regexp
|
||||
|
||||
ok (obj.width()/id - obj.height()/id) is -5
|
||||
|
||||
Reference in New Issue
Block a user