mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-01-13 08:47:55 -05:00
Compare commits
303 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f582b73035 | ||
|
|
e795f41bd2 | ||
|
|
5d541232ef | ||
|
|
07513ba928 | ||
|
|
aded80c101 | ||
|
|
5d893947ea | ||
|
|
535cf28220 | ||
|
|
dbe5328c33 | ||
|
|
6e15a4da0e | ||
|
|
fc51f0ef6c | ||
|
|
1f2c8df5fa | ||
|
|
bea40a7a92 | ||
|
|
f679590bef | ||
|
|
bd61131f5c | ||
|
|
ff25361896 | ||
|
|
08dcc7e107 | ||
|
|
b027b5cf0d | ||
|
|
0f2a2ee11e | ||
|
|
a93229b14d | ||
|
|
2d3f6b80c1 | ||
|
|
9503ea3040 | ||
|
|
08539a156e | ||
|
|
b183b091ee | ||
|
|
c39415da44 | ||
|
|
2c461f6474 | ||
|
|
e213964793 | ||
|
|
dd753d3b78 | ||
|
|
45c0d4c2ea | ||
|
|
4f89f90dab | ||
|
|
0270e48a01 | ||
|
|
a278d8f018 | ||
|
|
d4a180c413 | ||
|
|
28a8c05513 | ||
|
|
879b3d76bd | ||
|
|
138692183b | ||
|
|
95f3e2f79f | ||
|
|
dec9950649 | ||
|
|
2f6b69b580 | ||
|
|
ff1fd97924 | ||
|
|
87e60dccf0 | ||
|
|
8ff977dc65 | ||
|
|
fbfa12c733 | ||
|
|
6a45d25777 | ||
|
|
2b5d596e10 | ||
|
|
e2a71d3c2c | ||
|
|
807e0b479d | ||
|
|
dfa63839bb | ||
|
|
0490cb2920 | ||
|
|
2d0ad73af8 | ||
|
|
5a81fcd42e | ||
|
|
6446e0004c | ||
|
|
edf5f4947e | ||
|
|
b674163a40 | ||
|
|
9f2badb3e9 | ||
|
|
0610e20a3c | ||
|
|
bedc005d67 | ||
|
|
a8a46257ae | ||
|
|
b41afe79b4 | ||
|
|
448ed36cd2 | ||
|
|
c4d19cd1fa | ||
|
|
495ca64c46 | ||
|
|
0f2cf552e9 | ||
|
|
f6a1f16146 | ||
|
|
db6bc0ba02 | ||
|
|
e4bb6c91e7 | ||
|
|
561a02c35f | ||
|
|
51e80484e2 | ||
|
|
79fa4723ab | ||
|
|
a3c8c0b492 | ||
|
|
2f389f1d51 | ||
|
|
85e5dffad5 | ||
|
|
fa63288f52 | ||
|
|
4ea8be8e0b | ||
|
|
48c501a7a2 | ||
|
|
63c2b2bc64 | ||
|
|
639be2ff09 | ||
|
|
8b1b3ea402 | ||
|
|
9c3040b704 | ||
|
|
e7291f57ba | ||
|
|
3f6eceac77 | ||
|
|
ba7a454f92 | ||
|
|
3092d74a08 | ||
|
|
ff8e0c9751 | ||
|
|
3e518e3cf9 | ||
|
|
7667e16732 | ||
|
|
e110042275 | ||
|
|
06677b0545 | ||
|
|
20d105ba4e | ||
|
|
e77f4f61aa | ||
|
|
9de729e825 | ||
|
|
c39c2e3599 | ||
|
|
ecfa212189 | ||
|
|
97fd126a7f | ||
|
|
844c756940 | ||
|
|
a90bf75395 | ||
|
|
79bb0da153 | ||
|
|
c88b1f6a15 | ||
|
|
b224d58a36 | ||
|
|
7d348b5eae | ||
|
|
02ac3edebf | ||
|
|
4bad3e0f4f | ||
|
|
8147ef554a | ||
|
|
785c4fb5a0 | ||
|
|
13b2dc8d31 | ||
|
|
a62923ff97 | ||
|
|
dd6be80fca | ||
|
|
8c077f0f65 | ||
|
|
2c4c4cc93e | ||
|
|
1ab3b183a8 | ||
|
|
e6a53bd852 | ||
|
|
b983b3fcdc | ||
|
|
89cac4071e | ||
|
|
506ea8aa52 | ||
|
|
b965fcf32d | ||
|
|
126f6c2d88 | ||
|
|
3dc456572b | ||
|
|
0f26072ad0 | ||
|
|
dc9cec2611 | ||
|
|
c9aeae757b | ||
|
|
094c2682bd | ||
|
|
066ee52615 | ||
|
|
ee1c9b284a | ||
|
|
d9fba94983 | ||
|
|
e02bedcf82 | ||
|
|
1552470413 | ||
|
|
e2f3c2259b | ||
|
|
249bd99656 | ||
|
|
b36196286a | ||
|
|
b21780b738 | ||
|
|
207ec81821 | ||
|
|
d61aaf393a | ||
|
|
19c44c9b62 | ||
|
|
4deabf5e01 | ||
|
|
1a6194e9f0 | ||
|
|
156a0b13d9 | ||
|
|
61a7f7a567 | ||
|
|
dbcb9df22b | ||
|
|
e2ad1190ac | ||
|
|
c0f9058f15 | ||
|
|
12859e575a | ||
|
|
3f765c356a | ||
|
|
fd8b540a66 | ||
|
|
29267593c2 | ||
|
|
1e74805aa4 | ||
|
|
12685aa54a | ||
|
|
04f07f4c15 | ||
|
|
df386a3b3f | ||
|
|
13c49ad865 | ||
|
|
7c01bba4f4 | ||
|
|
950d1199c2 | ||
|
|
38d1381c02 | ||
|
|
98f15d001f | ||
|
|
a379530d41 | ||
|
|
e19b67cb79 | ||
|
|
e883a559ca | ||
|
|
713f6f32e1 | ||
|
|
872b36c11d | ||
|
|
f761c25dcd | ||
|
|
38e1991f82 | ||
|
|
13d3b3a3ce | ||
|
|
0c2a13b468 | ||
|
|
4e7408dc25 | ||
|
|
76dac9c09c | ||
|
|
9339058fc3 | ||
|
|
8e3e06a6e9 | ||
|
|
fd80d784f4 | ||
|
|
ae4f6309e8 | ||
|
|
c466537a26 | ||
|
|
001c915c21 | ||
|
|
91a7102f11 | ||
|
|
a451e90374 | ||
|
|
c6a6788694 | ||
|
|
522df2a355 | ||
|
|
863de88671 | ||
|
|
cb57a1ca1f | ||
|
|
32098e5a13 | ||
|
|
210d673ef0 | ||
|
|
0b5b6113ee | ||
|
|
d5c98165ea | ||
|
|
b8d22bc572 | ||
|
|
69808ba523 | ||
|
|
aabfba9599 | ||
|
|
135620b14a | ||
|
|
78a4974de9 | ||
|
|
e2d75e6771 | ||
|
|
1aa966bba6 | ||
|
|
a347183f3d | ||
|
|
56499984ca | ||
|
|
7ec0a8d653 | ||
|
|
293c2ffb5b | ||
|
|
5ec096e40d | ||
|
|
47bc1d5fda | ||
|
|
6a59c5c9a9 | ||
|
|
decaea0f5f | ||
|
|
0a1873dc42 | ||
|
|
96eb7e2339 | ||
|
|
b795ae7fe1 | ||
|
|
74b9545dc8 | ||
|
|
0d56b89d12 | ||
|
|
dc7d0f1568 | ||
|
|
9b7cfe87b5 | ||
|
|
1587901367 | ||
|
|
df670d47d2 | ||
|
|
cb7a1033fa | ||
|
|
df588bc9e8 | ||
|
|
9648ae2de1 | ||
|
|
c5c841f2fc | ||
|
|
c8ac7f0533 | ||
|
|
681d4f44f4 | ||
|
|
db00cd6ed4 | ||
|
|
3a748755df | ||
|
|
a0572f161d | ||
|
|
bad50c9aee | ||
|
|
c6457e010d | ||
|
|
557cdbba71 | ||
|
|
84feab3492 | ||
|
|
e755188878 | ||
|
|
babeebcc1a | ||
|
|
f19360c6b9 | ||
|
|
eff2f4b520 | ||
|
|
854c796fd6 | ||
|
|
b0ecb39e9f | ||
|
|
f5a37035cf | ||
|
|
e08e99a403 | ||
|
|
ba2d9df25f | ||
|
|
83285fe170 | ||
|
|
5b2ab36246 | ||
|
|
2f3a94678f | ||
|
|
ca0a65ab95 | ||
|
|
3524d618d8 | ||
|
|
386d3dd307 | ||
|
|
e998a81b63 | ||
|
|
aa93d3c387 | ||
|
|
ab4a4a5580 | ||
|
|
3775f682de | ||
|
|
a9f016e292 | ||
|
|
55df898112 | ||
|
|
fb7fd53bdf | ||
|
|
29e4043f26 | ||
|
|
460b3f6d8e | ||
|
|
63b44a2b03 | ||
|
|
8efcaf6eec | ||
|
|
a732e578ea | ||
|
|
d6e206b420 | ||
|
|
91e703052c | ||
|
|
f393b1c897 | ||
|
|
2875de5e73 | ||
|
|
8d63d269b8 | ||
|
|
a5d39efdd2 | ||
|
|
70e3a6ef2f | ||
|
|
4b267b401a | ||
|
|
e6f010b983 | ||
|
|
af53a04932 | ||
|
|
817e8deb27 | ||
|
|
d728c3d669 | ||
|
|
9160500e84 | ||
|
|
c3ce2ea9b1 | ||
|
|
5f94186b40 | ||
|
|
791d874058 | ||
|
|
a8ae37a428 | ||
|
|
b9c09bfa4e | ||
|
|
63c9b5c2f0 | ||
|
|
80fbe02fda | ||
|
|
e514a39dd2 | ||
|
|
4a32c58221 | ||
|
|
4609ad78c2 | ||
|
|
2d90a751f7 | ||
|
|
8647b54a61 | ||
|
|
8e1f3c0eca | ||
|
|
c4d0903e6a | ||
|
|
e72ef1a61a | ||
|
|
d7d9cb8d28 | ||
|
|
f6c8e81ea6 | ||
|
|
52539ae7d2 | ||
|
|
95b362499f | ||
|
|
0bc4da2b51 | ||
|
|
9679fc0b52 | ||
|
|
9cb0564972 | ||
|
|
c6c0c7d059 | ||
|
|
62e946b8ce | ||
|
|
6c782b7723 | ||
|
|
9eff443032 | ||
|
|
8957feedb4 | ||
|
|
1cd7fa8ebe | ||
|
|
701cdb4c13 | ||
|
|
8dc5da9cc9 | ||
|
|
001cc29deb | ||
|
|
e77e520607 | ||
|
|
ed8a54995d | ||
|
|
2d206e7b60 | ||
|
|
bb9fdd3015 | ||
|
|
1e7d638435 | ||
|
|
0ceca0778c | ||
|
|
abd9ab5c71 | ||
|
|
ea349a1a59 | ||
|
|
f0d5db7e66 | ||
|
|
914ba1c244 | ||
|
|
844ea33274 | ||
|
|
87e04e9952 | ||
|
|
197914bcf7 | ||
|
|
8dfbd1a2a8 | ||
|
|
c19647ad33 | ||
|
|
27f7ef09af |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,6 +1,6 @@
|
||||
presentation
|
||||
test.coffee
|
||||
parser.output
|
||||
lib/coffee_script/parser.rb
|
||||
test/fixtures/underscore
|
||||
examples/beautiful_code/parse.coffee
|
||||
*.gem
|
||||
57
Cakefile
Normal file
57
Cakefile
Normal file
@@ -0,0 +1,57 @@
|
||||
fs: require 'fs'
|
||||
coffee: require 'coffee-script'
|
||||
|
||||
# Run a CoffeeScript through our node/coffee interpreter.
|
||||
run: (args) ->
|
||||
proc: process.createChildProcess 'bin/coffee', args
|
||||
proc.addListener 'error', (err) -> if err then puts err
|
||||
|
||||
|
||||
task 'install', 'install CoffeeScript into /usr/local', ->
|
||||
exec([
|
||||
'mkdir -p /usr/local/lib/coffee-script'
|
||||
'cp -rf bin lib LICENSE README package.json src vendor /usr/local/lib/coffee-script'
|
||||
'ln -sf /usr/local/lib/coffee-script/lib/bin/coffee /usr/local/bin/coffee'
|
||||
'ln -sf /usr/local/lib/coffee-script/lib/bin/cake /usr/local/bin/cake'
|
||||
].join(' && '))
|
||||
|
||||
|
||||
task 'build', 'build the CoffeeScript language from source', ->
|
||||
fs.readdir 'src', (err, files) ->
|
||||
files: 'src/' + file for file in files when file.match(/\.coffee$/)
|
||||
run ['-o', 'lib'].concat(files)
|
||||
|
||||
|
||||
task 'build:parser', 'rebuild the Jison parser (run build first)', ->
|
||||
parser: require('grammar').parser
|
||||
js: parser.generate()
|
||||
parser_path: 'lib/parser.js'
|
||||
fs.writeFile parser_path, js
|
||||
|
||||
|
||||
task 'build:ultraviolet', 'build and install the Ultraviolet syntax highlighter', ->
|
||||
exec('plist2syntax extras/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage').addCallback ->
|
||||
exec 'sudo mv coffeescript.yaml /usr/local/lib/ruby/gems/1.8/gems/ultraviolet-0.10.2/syntax/coffeescript.syntax'
|
||||
|
||||
|
||||
task 'build:underscore', 'rebuild the Underscore.coffee documentation page', ->
|
||||
exec 'uv -s coffeescript -t idle -h examples/underscore.coffee > documentation/underscore.html'
|
||||
|
||||
|
||||
task 'test', 'run the CoffeeScript language test suite', ->
|
||||
process.mixin require 'assert'
|
||||
test_count: 0
|
||||
start_time: new Date()
|
||||
[original_ok, original_throws]: [ok, throws]
|
||||
process.mixin {
|
||||
ok: (args...) -> test_count += 1; original_ok(args...)
|
||||
throws: (args...) -> test_count += 1; original_throws(args...)
|
||||
}
|
||||
process.addListener 'exit', ->
|
||||
time: ((new Date() - start_time) / 1000).toFixed(2)
|
||||
puts '\033[0;32mpassed ' + test_count + ' tests in ' + time + ' seconds\033[0m'
|
||||
fs.readdir 'test', (err, files) ->
|
||||
for file in files
|
||||
fs.readFile 'test/' + file, (err, source) ->
|
||||
js: coffee.compile source
|
||||
process.compile js, file
|
||||
14
README
14
README
@@ -22,20 +22,20 @@
|
||||
|
||||
CoffeeScript is a little language that compiles into JavaScript.
|
||||
|
||||
Install the compiler:
|
||||
gem install coffee-script
|
||||
Install Node.js, and then the CoffeeScript compiler:
|
||||
sudo bin/cake install
|
||||
|
||||
Compile a script:
|
||||
coffee /path/to/script.coffee
|
||||
|
||||
For documentation, usage, and examples, see:
|
||||
http://jashkenas.github.com/coffee-script/
|
||||
http://coffeescript.org/
|
||||
|
||||
To suggest a feature or report a bug:
|
||||
To suggest a feature, report a bug, or general discussion:
|
||||
http://github.com/jashkenas/coffee-script/issues/
|
||||
|
||||
If you'd like to chat, drop by #coffeescript on Freenode.
|
||||
|
||||
The source repository:
|
||||
git://github.com/jashkenas/coffee-script.git
|
||||
|
||||
To build CoffeeScript from source, install the "racc" gem and
|
||||
run "rake build:parser". Then bin/coffee will work.
|
||||
|
||||
|
||||
52
Rakefile
52
Rakefile
@@ -2,47 +2,12 @@ require 'erb'
|
||||
require 'fileutils'
|
||||
require 'rake/testtask'
|
||||
|
||||
desc "Run all tests"
|
||||
task :test do
|
||||
$LOAD_PATH.unshift(File.expand_path('test'))
|
||||
require 'redgreen' if Gem.available?('redgreen')
|
||||
require 'test/unit'
|
||||
Dir['test/*/**/test_*.rb'].each {|test| require test }
|
||||
end
|
||||
|
||||
namespace :build do
|
||||
|
||||
desc "Recompile the Racc parser (pass -v and -g for verbose debugging)"
|
||||
task :parser, :racc_args do |t, args|
|
||||
sh "racc #{args[:racc_args]} -o lib/coffee_script/parser.rb lib/coffee_script/grammar.y"
|
||||
end
|
||||
|
||||
desc "Compile the Narwhal interface for --interactive and --run"
|
||||
task :narwhal do
|
||||
sh "bin/coffee lib/coffee_script/narwhal/*.coffee -o lib/coffee_script/narwhal/lib/coffee-script"
|
||||
sh "mv lib/coffee_script/narwhal/lib/coffee-script/coffee-script.js lib/coffee_script/narwhal/lib/coffee-script.js"
|
||||
end
|
||||
|
||||
desc "Compile and install the Ultraviolet syntax highlighter"
|
||||
task :ultraviolet do
|
||||
sh "plist2syntax lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage"
|
||||
sh "sudo mv coffeescript.yaml /usr/local/lib/ruby/gems/1.8/gems/ultraviolet-0.10.2/syntax/coffeescript.syntax"
|
||||
end
|
||||
|
||||
desc "Rebuild the Underscore.coffee documentation page"
|
||||
task :underscore do
|
||||
sh "uv -s coffeescript -t idle -h examples/underscore.coffee > documentation/underscore.html"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
desc "Build the documentation page"
|
||||
task :doc do
|
||||
source = 'documentation/index.html.erb'
|
||||
child = fork { exec "bin/coffee documentation/coffee/*.coffee -o documentation/js -w" }
|
||||
at_exit { Process.kill("INT", child) }
|
||||
Signal.trap("INT") { exit }
|
||||
# `uv -t idle -s coffeescript -h examples/underscore.coffee > documentation/underscore.html`
|
||||
loop do
|
||||
mtime = File.stat(source).mtime
|
||||
if !@mtime || mtime > @mtime
|
||||
@@ -53,20 +18,3 @@ task :doc do
|
||||
sleep 1
|
||||
end
|
||||
end
|
||||
|
||||
namespace :gem do
|
||||
|
||||
desc 'Build and install the coffee-script gem'
|
||||
task :install do
|
||||
sh "gem build coffee-script.gemspec"
|
||||
sh "sudo gem install #{Dir['*.gem'].join(' ')} --local --no-ri --no-rdoc"
|
||||
end
|
||||
|
||||
desc 'Uninstall the coffee-script gem'
|
||||
task :uninstall do
|
||||
sh "sudo gem uninstall -x coffee-script"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
task :default => :test
|
||||
7
bin/cake
Executable file
7
bin/cake
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
process.mixin(require('sys'));
|
||||
|
||||
require.paths.unshift('./lib');
|
||||
|
||||
require('cake').run();
|
||||
@@ -1,5 +1,7 @@
|
||||
#!/usr/bin/env ruby
|
||||
#!/usr/bin/env node
|
||||
|
||||
require "#{File.dirname(__FILE__)}/../lib/coffee_script/command_line.rb"
|
||||
process.mixin(require('sys'));
|
||||
|
||||
CoffeeScript::CommandLine.new
|
||||
require.paths.unshift('./lib');
|
||||
|
||||
require('command_line').run();
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
Gem::Specification.new do |s|
|
||||
s.name = 'coffee-script'
|
||||
s.version = '0.2.4' # Keep version in sync with coffee-script.rb
|
||||
s.date = '2010-1-12'
|
||||
|
||||
s.homepage = "http://jashkenas.github.com/coffee-script/"
|
||||
s.summary = "The CoffeeScript Compiler"
|
||||
s.description = <<-EOS
|
||||
CoffeeScript is a little language that compiles into JavaScript. Think
|
||||
of it as JavaScript's less ostentatious kid brother -- the same genes,
|
||||
roughly the same height, but a different sense of style. Apart from a
|
||||
handful of bonus goodies, statements in CoffeeScript correspond
|
||||
one-to-one with their equivalent in JavaScript, it's just another
|
||||
way of saying it.
|
||||
EOS
|
||||
|
||||
s.authors = ['Jeremy Ashkenas']
|
||||
s.email = 'jashkenas@gmail.com'
|
||||
s.rubyforge_project = 'coffee-script'
|
||||
s.has_rdoc = false
|
||||
|
||||
s.require_paths = ['lib']
|
||||
s.executables = ['coffee']
|
||||
|
||||
s.files = Dir['bin/*', 'examples/*', 'lib/**/*', 'coffee-script.gemspec', 'LICENSE', 'README', 'package.json']
|
||||
end
|
||||
@@ -5,3 +5,5 @@ volume: 10 if band isnt spinal_tap
|
||||
let_the_wild_rumpus_begin() unless answer is no
|
||||
|
||||
if car.speed < speed_limit then accelerate()
|
||||
|
||||
print "My name is " + @name
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
backwards: =>
|
||||
alert(arguments.reverse())
|
||||
backwards: ->
|
||||
alert arguments.reverse()
|
||||
|
||||
backwards("stairway", "to", "heaven")
|
||||
backwards "stairway", "to", "heaven"
|
||||
@@ -4,4 +4,4 @@ lunch: eat(food) for food in ['toast', 'cheese', 'wine']
|
||||
# Naive collision detection.
|
||||
for roid in asteroids
|
||||
for roid2 in asteroids when roid isnt roid2
|
||||
roid.explode() if roid.overlaps(roid2)
|
||||
roid.explode() if roid.overlaps roid2
|
||||
@@ -1,4 +0,0 @@
|
||||
$('table.list').each() table =>
|
||||
$('tr.account', table).each() row =>
|
||||
row.show()
|
||||
row.highlight()
|
||||
5
documentation/coffee/cake_tasks.coffee
Normal file
5
documentation/coffee/cake_tasks.coffee
Normal file
@@ -0,0 +1,5 @@
|
||||
process.mixin require 'assert'
|
||||
|
||||
task 'test', 'run each of the unit tests', ->
|
||||
for test in test_files
|
||||
fs.readFile test, (err, code) -> eval coffee.compile code
|
||||
5
documentation/coffee/comparisons.coffee
Normal file
5
documentation/coffee/comparisons.coffee
Normal file
@@ -0,0 +1,5 @@
|
||||
cholesterol: 127
|
||||
|
||||
healthy: 200 > cholesterol > 60
|
||||
|
||||
|
||||
@@ -1 +1,8 @@
|
||||
solipsism: true if mind? and not world?
|
||||
solipsism: true if mind? and not world?
|
||||
|
||||
speed ?= 140
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
grade: student =>
|
||||
grade: (student) ->
|
||||
if student.excellent_work
|
||||
"A+"
|
||||
else if student.okay_stuff
|
||||
|
||||
@@ -2,5 +2,5 @@ alert(
|
||||
try
|
||||
nonexistent / undefined
|
||||
catch error
|
||||
"Caught an error: " + error
|
||||
"And the error is ... " + error
|
||||
)
|
||||
6
documentation/coffee/fat_arrow.coffee
Normal file
6
documentation/coffee/fat_arrow.coffee
Normal file
@@ -0,0 +1,6 @@
|
||||
Account: (customer, cart) ->
|
||||
@customer: customer
|
||||
@cart: cart
|
||||
|
||||
$('.shopping_cart').bind 'click', (event) =>
|
||||
@customer.purchase @cart
|
||||
@@ -1,2 +1,2 @@
|
||||
square: x => x * x
|
||||
cube: x => square(x) * x
|
||||
square: (x) -> x * x
|
||||
cube: (x) -> square(x) * x
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
weather_report: location =>
|
||||
weather_report: (location) ->
|
||||
# Make an Ajax request to fetch the weather...
|
||||
[location, 72, "Mostly Sunny"]
|
||||
|
||||
[city, temp, forecast]: weather_report("Berkeley, CA")
|
||||
[city, temp, forecast]: weather_report "Berkeley, CA"
|
||||
@@ -6,7 +6,7 @@ opposite_day: true
|
||||
number: -42 if opposite_day
|
||||
|
||||
# Functions:
|
||||
square: x => x * x
|
||||
square: (x) -> x * x
|
||||
|
||||
# Arrays:
|
||||
list: [1, 2, 3, 4, 5]
|
||||
@@ -15,15 +15,15 @@ list: [1, 2, 3, 4, 5]
|
||||
math: {
|
||||
root: Math.sqrt
|
||||
square: square
|
||||
cube: x => x * square(x)
|
||||
cube: (x) -> x * square x
|
||||
}
|
||||
|
||||
# Splats:
|
||||
race: winner, runners... =>
|
||||
print(winner, runners)
|
||||
race: (winner, runners...) ->
|
||||
print winner, runners
|
||||
|
||||
# Existence:
|
||||
alert("I knew it!") if elvis?
|
||||
alert "I knew it!" if elvis?
|
||||
|
||||
# Array comprehensions:
|
||||
cubed_list: math.cube(num) for num in list
|
||||
cubed_list: math.cube num for num in list
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
countdown: num for num in [10..1]
|
||||
|
||||
egg_delivery: =>
|
||||
egg_delivery: ->
|
||||
for i in [0...eggs.length] by 12
|
||||
dozen_eggs: eggs[i...i+12]
|
||||
deliver(new egg_carton(dozen))
|
||||
deliver new egg_carton(dozen)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
num: 1
|
||||
change_numbers: =>
|
||||
change_numbers: ->
|
||||
new_num: -1
|
||||
num: 10
|
||||
new_num: change_numbers()
|
||||
1
documentation/coffee/soaks.coffee
Normal file
1
documentation/coffee/soaks.coffee
Normal file
@@ -0,0 +1 @@
|
||||
lottery.draw_winner()?.address?.zipcode
|
||||
@@ -1,6 +1,6 @@
|
||||
gold: silver: the_field: "unknown"
|
||||
|
||||
medalists: first, second, rest... =>
|
||||
award_medals: (first, second, rest...) ->
|
||||
gold: first
|
||||
silver: second
|
||||
the_field: rest
|
||||
@@ -18,8 +18,8 @@ contenders: [
|
||||
"Usain Bolt"
|
||||
]
|
||||
|
||||
medalists(contenders...)
|
||||
award_medals contenders...
|
||||
|
||||
alert("Gold: " + gold)
|
||||
alert("Silver: " + silver)
|
||||
alert("The Field: " + the_field)
|
||||
alert "Gold: " + gold
|
||||
alert "Silver: " + silver
|
||||
alert "The Field: " + the_field
|
||||
@@ -1,21 +1,30 @@
|
||||
Animal: =>
|
||||
Animal::move: meters =>
|
||||
alert(this.name + " moved " + meters + "m.")
|
||||
Animal: ->
|
||||
|
||||
Animal::move: (meters) ->
|
||||
alert @name + " moved " + meters + "m."
|
||||
|
||||
Snake: (name) ->
|
||||
@name: name
|
||||
this
|
||||
|
||||
Snake: name => this.name: name
|
||||
Snake extends Animal
|
||||
Snake::move: =>
|
||||
alert("Slithering...")
|
||||
super(5)
|
||||
|
||||
Horse: name => this.name: name
|
||||
Snake::move: ->
|
||||
alert "Slithering..."
|
||||
super 5
|
||||
|
||||
Horse: (name) ->
|
||||
@name: name
|
||||
this
|
||||
|
||||
Horse extends Animal
|
||||
Horse::move: =>
|
||||
alert("Galloping...")
|
||||
super(45)
|
||||
|
||||
sam: new Snake("Sammy the Python")
|
||||
tom: new Horse("Tommy the Palomino")
|
||||
Horse::move: ->
|
||||
alert "Galloping..."
|
||||
super 45
|
||||
|
||||
sam: new Snake "Sammy the Python"
|
||||
tom: new Horse "Tommy the Palomino"
|
||||
|
||||
sam.move()
|
||||
tom.move()
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
switch day
|
||||
when "Tuesday" then eat_breakfast()
|
||||
when "Wednesday" then go_to_the_park()
|
||||
when "Saturday"
|
||||
when "Mon" then go_to_work()
|
||||
when "Tue" then go_to_the_park()
|
||||
when "Thu" then go_ice_fishing()
|
||||
when "Fri", "Sat"
|
||||
if day is bingo_day
|
||||
go_to_bingo()
|
||||
go_dancing()
|
||||
when "Sunday" then go_to_church()
|
||||
when "Sun" then go_to_church()
|
||||
else go_to_work()
|
||||
@@ -2,6 +2,6 @@ try
|
||||
all_hell_breaks_loose()
|
||||
cats_and_dogs_living_together()
|
||||
catch error
|
||||
print(error)
|
||||
print error
|
||||
finally
|
||||
clean_up()
|
||||
@@ -1,5 +1,10 @@
|
||||
while demand > supply
|
||||
sell()
|
||||
restock()
|
||||
# Econ 101
|
||||
if this.studying_economics
|
||||
while supply > demand then buy()
|
||||
while supply < demand then sell()
|
||||
|
||||
while supply > demand then buy()
|
||||
# Nursery Rhyme
|
||||
num: 6
|
||||
lyrics: while num -= 1
|
||||
num + " little monkeys, jumping on the bed.
|
||||
One fell out and bumped his head."
|
||||
|
||||
@@ -1,147 +0,0 @@
|
||||
pre.amy .PolymorphicVariants {
|
||||
color: #60B0FF;
|
||||
font-style: italic;
|
||||
}
|
||||
pre.amy .KeywordDecorator {
|
||||
color: #D0D0FF;
|
||||
}
|
||||
pre.amy .Punctuation {
|
||||
color: #805080;
|
||||
}
|
||||
pre.amy .InheritedClass {
|
||||
}
|
||||
pre.amy .InvalidDepricated {
|
||||
background-color: #CC66FF;
|
||||
color: #200020;
|
||||
}
|
||||
pre.amy .LibraryVariable {
|
||||
}
|
||||
pre.amy .TokenReferenceOcamlyacc {
|
||||
color: #3CB0D0;
|
||||
}
|
||||
pre.amy .Storage {
|
||||
color: #B0FFF0;
|
||||
}
|
||||
pre.amy .KeywordOperator {
|
||||
color: #A0A0FF;
|
||||
}
|
||||
pre.amy .CharacterConstant {
|
||||
color: #666666;
|
||||
}
|
||||
pre.amy .line-numbers {
|
||||
background-color: #800000;
|
||||
color: #000000;
|
||||
}
|
||||
pre.amy .ClassName {
|
||||
color: #70E080;
|
||||
}
|
||||
pre.amy .Int64Constant {
|
||||
font-style: italic;
|
||||
}
|
||||
pre.amy .NonTerminalReferenceOcamlyacc {
|
||||
color: #C0F0F0;
|
||||
}
|
||||
pre.amy .TokenDefinitionOcamlyacc {
|
||||
color: #3080A0;
|
||||
}
|
||||
pre.amy .ClassType {
|
||||
color: #70E0A0;
|
||||
}
|
||||
pre.amy .ControlKeyword {
|
||||
color: #80A0FF;
|
||||
}
|
||||
pre.amy .LineNumberDirectives {
|
||||
text-decoration: underline;
|
||||
color: #C080C0;
|
||||
}
|
||||
pre.amy .FloatingPointConstant {
|
||||
text-decoration: underline;
|
||||
}
|
||||
pre.amy .Int32Constant {
|
||||
font-weight: bold;
|
||||
}
|
||||
pre.amy .TagName {
|
||||
color: #009090;
|
||||
}
|
||||
pre.amy .ModuleTypeDefinitions {
|
||||
text-decoration: underline;
|
||||
color: #B000B0;
|
||||
}
|
||||
pre.amy .Integer {
|
||||
color: #7090B0;
|
||||
}
|
||||
pre.amy .Camlp4TempParser {
|
||||
}
|
||||
pre.amy .InvalidIllegal {
|
||||
font-weight: bold;
|
||||
background-color: #FFFF00;
|
||||
color: #400080;
|
||||
}
|
||||
pre.amy .LibraryConstant {
|
||||
background-color: #200020;
|
||||
}
|
||||
pre.amy .ModuleDefinitions {
|
||||
color: #B000B0;
|
||||
}
|
||||
pre.amy .Variants {
|
||||
color: #60B0FF;
|
||||
}
|
||||
pre.amy .CompilerDirectives {
|
||||
color: #C080C0;
|
||||
}
|
||||
pre.amy .FloatingPointInfixOperator {
|
||||
text-decoration: underline;
|
||||
}
|
||||
pre.amy .BuiltInConstant1 {
|
||||
}
|
||||
pre.amy {
|
||||
background-color: #200020;
|
||||
color: #D0D0FF;
|
||||
}
|
||||
pre.amy .FunctionArgument {
|
||||
color: #80B0B0;
|
||||
}
|
||||
pre.amy .FloatingPointPrefixOperator {
|
||||
text-decoration: underline;
|
||||
}
|
||||
pre.amy .NativeintConstant {
|
||||
font-weight: bold;
|
||||
}
|
||||
pre.amy .BuiltInConstant {
|
||||
color: #707090;
|
||||
}
|
||||
pre.amy .BooleanConstant {
|
||||
color: #8080A0;
|
||||
}
|
||||
pre.amy .LibraryClassType {
|
||||
}
|
||||
pre.amy .TagAttribute {
|
||||
}
|
||||
pre.amy .Keyword {
|
||||
color: #A080FF;
|
||||
}
|
||||
pre.amy .UserDefinedConstant {
|
||||
}
|
||||
pre.amy .String {
|
||||
color: #999999;
|
||||
}
|
||||
pre.amy .Camlp4Code {
|
||||
background-color: #350060;
|
||||
}
|
||||
pre.amy .NonTerminalDefinitionOcamlyacc {
|
||||
color: #90E0E0;
|
||||
}
|
||||
pre.amy .FunctionName {
|
||||
color: #50A0A0;
|
||||
}
|
||||
pre.amy .SupportModules {
|
||||
color: #A00050;
|
||||
}
|
||||
pre.amy .Variable {
|
||||
color: #008080;
|
||||
}
|
||||
pre.amy .Comment {
|
||||
background-color: #200020;
|
||||
color: #404080;
|
||||
font-style: italic;
|
||||
}
|
||||
@@ -1,29 +1,29 @@
|
||||
body {
|
||||
font-size: 14px;
|
||||
line-height: 20px;
|
||||
background: #f3f3f9;
|
||||
color: #191933;
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
}
|
||||
div.container {
|
||||
width: 850px;
|
||||
margin: 50px 0 50px 50px;
|
||||
width: 950px;
|
||||
margin: 100px 0 50px 50px;
|
||||
}
|
||||
p {
|
||||
padding-left: 13px;
|
||||
width: 625px;
|
||||
}
|
||||
a {
|
||||
color: #000055;
|
||||
}
|
||||
h1, h2, h3, h4, h5, h6 {
|
||||
padding-left: 13px;
|
||||
margin-top: 40px;
|
||||
}
|
||||
br.clear {
|
||||
height: 0;
|
||||
clear: both;
|
||||
}
|
||||
ul {
|
||||
padding-left: 20px;
|
||||
}
|
||||
b.header {
|
||||
color: #000055;
|
||||
display: block;
|
||||
@@ -31,7 +31,7 @@ b.header {
|
||||
font-size: 16px;
|
||||
}
|
||||
li {
|
||||
margin-bottom: 7px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
table {
|
||||
margin: 16px 0 0 13px; padding: 0;
|
||||
@@ -43,7 +43,7 @@ table {
|
||||
td {
|
||||
padding: 9px 15px 9px 0;
|
||||
}
|
||||
code, pre, tt {
|
||||
code, pre, tt, textarea {
|
||||
font-family: Monaco, Consolas, "Lucida Console", monospace;
|
||||
font-size: 12px;
|
||||
line-height: 18px;
|
||||
@@ -66,7 +66,7 @@ code, pre, tt {
|
||||
div.code {
|
||||
position: relative;
|
||||
border: 1px solid #cacaca;
|
||||
background: #fff;
|
||||
background: #fafaff;
|
||||
padding: 7px 0 10px 0;
|
||||
-moz-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px;
|
||||
-webkit-box-shadow: 0px 0px 7px #cacaca;
|
||||
@@ -75,13 +75,133 @@ div.code {
|
||||
position: absolute;
|
||||
right: 8px; bottom: 8px;
|
||||
}
|
||||
div.code pre {
|
||||
div.code pre, div.code textarea {
|
||||
float: left;
|
||||
width: 410px;
|
||||
width: 450px;
|
||||
background: #fafaff;
|
||||
border-left: 1px dotted #559;
|
||||
padding: 0 0 0 12px;
|
||||
margin: 0;
|
||||
}
|
||||
div.code pre:first-child {
|
||||
border-left: 0;
|
||||
}
|
||||
}
|
||||
|
||||
#fadeout {
|
||||
z-index: 50;
|
||||
position: fixed;
|
||||
left: 0; top: 0; right: 0;
|
||||
height: 100px;
|
||||
background: -webkit-gradient(linear, left top, left bottom, from(rgba(255, 255, 255, 255)), to(rgba(255, 255, 255, 0)));
|
||||
background: -moz-linear-gradient(top, rgba(255, 255, 255, 255), rgba(255, 255, 255, 0));
|
||||
}
|
||||
|
||||
#flybar {
|
||||
position: fixed;
|
||||
z-index: 100;
|
||||
height: 50px;
|
||||
left: 40px; right: 40px; top: 25px;
|
||||
background: #ddd;
|
||||
padding-left: 235px;
|
||||
background: -webkit-gradient(linear, left top, left bottom, from(#f5f5f5), to(#d0d0d0));
|
||||
background: -moz-linear-gradient(top, #f5f5f5, #d0d0d0);
|
||||
filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#F5F5F5', EndColorStr='#D0D0D0');
|
||||
-webkit-border-radius: 20px; -moz-border-radius: 20px; border-radius: 20px;
|
||||
-webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777;
|
||||
}
|
||||
#logo {
|
||||
display: block;
|
||||
width: 215px; height: 50px;
|
||||
background: url('logo.png');
|
||||
position: absolute;
|
||||
top: 0px; left: 10px;
|
||||
}
|
||||
.navigation {
|
||||
height: 50px;
|
||||
font: bold 11px/50px Arial;
|
||||
text-transform: uppercase;
|
||||
position: relative;
|
||||
float: left;
|
||||
padding: 0 20px;
|
||||
border: 1px solid #bbb;
|
||||
border-top: 0; border-bottom: 0;
|
||||
cursor: pointer;
|
||||
}
|
||||
.navigation.try {
|
||||
border-left: 0;
|
||||
}
|
||||
.navigation:hover {
|
||||
background: #d0d0d0;
|
||||
background: -webkit-gradient(linear, left top, left bottom, from(#f0f0f0), to(#c0c0c0));
|
||||
background: -moz-linear-gradient(top, #f0f0f0, #c0c0c0);
|
||||
filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#F0F0F0', EndColorStr='#C0C0C0');
|
||||
}
|
||||
.navigation .contents {
|
||||
display: none;
|
||||
position: absolute;
|
||||
background: #fff;
|
||||
top: 50px; left: 0;
|
||||
padding: 5px 0;
|
||||
-webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px;
|
||||
-webkit-border-top-left-radius: 0; -moz-border-radius-topleft: 0;
|
||||
-webkit-border-top-right-radius: 0; -moz-border-radius-topright: 0;
|
||||
-webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777;
|
||||
}
|
||||
.navigation:hover .contents {
|
||||
display: block;
|
||||
}
|
||||
.navigation .contents.repl_wrapper {
|
||||
left: -162px;
|
||||
width: 700px;
|
||||
padding: 0;
|
||||
}
|
||||
.navigation .contents.repl_wrapper .code {
|
||||
-webkit-box-shadow: none; -moz-box-shadow: none;
|
||||
background: transparent;
|
||||
border: 0;
|
||||
}
|
||||
.navigation .code button {
|
||||
bottom: 10px;
|
||||
}
|
||||
.navigation .compile {
|
||||
left: 240px; right: auto;
|
||||
}
|
||||
.navigation .contents a {
|
||||
display: block;
|
||||
width: 300px;
|
||||
text-transform: none;
|
||||
text-decoration: none;
|
||||
font-weight: normal;
|
||||
height: 12px;
|
||||
line-height: 12px;
|
||||
padding: 4px 10px;
|
||||
}
|
||||
.navigation .contents a:hover {
|
||||
background: #f0f0f0;
|
||||
}
|
||||
|
||||
.bookmark {
|
||||
display: block;
|
||||
width: 0; height: 0;
|
||||
position: relative;
|
||||
top: -90px;
|
||||
}
|
||||
|
||||
#repl_source, #repl_results {
|
||||
background: transparent;
|
||||
}
|
||||
#repl_source {
|
||||
border: 0;
|
||||
padding: 5px 7px;
|
||||
margin-left: 5px;
|
||||
min-height: 250px;
|
||||
resize: none;
|
||||
width: 295px;
|
||||
}
|
||||
#repl_results {
|
||||
font-family: Monaco, Consolas, "Lucida Console", monospace;
|
||||
text-transform: none;
|
||||
font-weight: normal;
|
||||
min-height: 260px;
|
||||
width: 355px;
|
||||
}
|
||||
@@ -28,6 +28,7 @@ pre.idle .LibraryConstant {
|
||||
color: #A535AE;
|
||||
}
|
||||
pre.idle .FunctionArgument {
|
||||
color: #0076ad;
|
||||
}
|
||||
pre.idle .BuiltInConstant {
|
||||
color: #A535AE;
|
||||
|
||||
BIN
documentation/css/logo.png
Normal file
BIN
documentation/css/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.9 KiB |
@@ -25,9 +25,66 @@
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
<div id="fadeout"></div>
|
||||
|
||||
<h1><sub style="font-size: 100px;">☕</sub> CoffeeScript</h1>
|
||||
<div id="flybar">
|
||||
<a id="logo" href="#top"> </a>
|
||||
<div class="navigation">
|
||||
<div class="button">
|
||||
Table of Contents
|
||||
</div>
|
||||
<div class="contents">
|
||||
<a href="#overview">Mini Overview</a>
|
||||
<a href="#installation">Installation and Usage</a>
|
||||
<a href="#language">Language Reference</a>
|
||||
<a href="#whitespace">Significant Whitespace</a>
|
||||
<a href="#functions">Functions and Invocation</a>
|
||||
<a href="#assignment">Assignment</a>
|
||||
<a href="#objects_and_arrays">Objects and Arrays</a>
|
||||
<a href="#lexical_scope">Lexical Scoping and Variable Safety</a>
|
||||
<a href="#conditionals">Conditionals, Ternaries, and Conditional Assignment</a>
|
||||
<a href="#aliases">Aliases</a>
|
||||
<a href="#splats">Splats...</a>
|
||||
<a href="#arguments">Arguments are Arrays</a>
|
||||
<a href="#while">While Loops</a>
|
||||
<a href="#comprehensions">Comprehensions (Arrays, Objects, and Ranges)</a>
|
||||
<a href="#slice_splice">Array Slicing and Splicing with Ranges</a>
|
||||
<a href="#expressions">Everything is an Expression</a>
|
||||
<a href="#existence">The Existential Operator</a>
|
||||
<a href="#inheritance">Inheritance, and Calling Super from a Subclass</a>
|
||||
<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="#try">Try/Catch/Finally</a>
|
||||
<a href="#comparisons">Chained Comparisons</a>
|
||||
<a href="#strings">Multiline Strings and Heredocs</a>
|
||||
<a href="#cake">Cake, and Cakefiles</a>
|
||||
<a href="#resources">Resources</a>
|
||||
<a href="#change_log">Change Log</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="navigation try">
|
||||
<div class="button">
|
||||
Try CoffeeScript
|
||||
</div>
|
||||
<div class="contents repl_wrapper">
|
||||
<div class="code">
|
||||
<textarea id="repl_source">reverse: (string) ->
|
||||
string.split('').reverse().join ''
|
||||
|
||||
alert reverse '!tpircseeffoC'</textarea>
|
||||
<pre id="repl_results"></pre>
|
||||
<button class="compile" onclick="javascript: repl_compile();">compile</button>
|
||||
<button class="run" onclick="javascript: repl_run();">run</button>
|
||||
<br class="clear" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<span class="bookmark" id="top"></span>
|
||||
|
||||
<p>
|
||||
CoffeeScript is a little language that compiles into JavaScript. Think
|
||||
@@ -39,8 +96,7 @@
|
||||
|
||||
<p>
|
||||
<b>Disclaimer:</b>
|
||||
CoffeeScript is just for fun and seriously alpha. I'm sure that there are still
|
||||
plenty of holes in the walls and leaks in the roof. <i>There are no guarantees
|
||||
CoffeeScript is just for fun. Until it reaches 1.0, <i>there are no guarantees
|
||||
that the syntax won't change between versions.</i> That said,
|
||||
it compiles into clean JavaScript (the good parts) that can use existing
|
||||
JavaScript libraries seamlessly, and passes through
|
||||
@@ -51,41 +107,13 @@
|
||||
|
||||
<p>
|
||||
<b>Latest Version:</b>
|
||||
<a href="http://gemcutter.org/gems/coffee-script">0.2.4</a>
|
||||
<a href="http://github.com/jashkenas/coffee-script/tarball/0.5.0">0.5.0</a>
|
||||
</p>
|
||||
|
||||
<h2>Table of Contents</h2>
|
||||
|
||||
<p>
|
||||
<a href="#overview">Mini Overview</a><br />
|
||||
<a href="#installation">Installation and Usage</a><br />
|
||||
<a href="#whitespace">Significant Whitespace</a><br />
|
||||
<a href="#functions">Functions and Invocation</a><br />
|
||||
<a href="#assignment">Assignment</a><br />
|
||||
<a href="#objects_and_arrays">Objects and Arrays</a><br />
|
||||
<a href="#lexical_scope">Lexical Scoping and Variable Safety</a><br />
|
||||
<a href="#conditionals">Conditionals, Ternaries, and Conditional Assignment</a><br />
|
||||
<a href="#existence">The Existence Operator</a><br />
|
||||
<a href="#aliases">Aliases</a><br />
|
||||
<a href="#splats">Splats...</a><br />
|
||||
<a href="#arguments">Arguments are Arrays</a><br />
|
||||
<a href="#while">While Loops</a><br />
|
||||
<a href="#comprehensions">Comprehensions (Arrays, Objects, and Ranges)</a><br />
|
||||
<a href="#slice_splice">Array Slicing and Splicing with Ranges</a><br />
|
||||
<a href="#expressions">Everything is an Expression</a><br />
|
||||
<a href="#inheritance">Inheritance, and Calling Super from a Subclass</a><br />
|
||||
<a href="#blocks">Blocks</a><br />
|
||||
<a href="#pattern_matching">Pattern Matching</a><br />
|
||||
<a href="#embedded">Embedded JavaScript</a><br />
|
||||
<a href="#switch">Switch/When/Else</a><br />
|
||||
<a href="#try">Try/Catch/Finally</a><br />
|
||||
<a href="#strings">Multiline Strings and Heredocs</a><br />
|
||||
<a href="#resources">Resources</a><br />
|
||||
<a href="#contributing">Contributing</a><br />
|
||||
<a href="#change_log">Change Log</a><br />
|
||||
</p>
|
||||
|
||||
<h2 id="overview">Mini Overview</h2>
|
||||
<h2>
|
||||
<span id="overview" class="bookmark"></span>
|
||||
Mini Overview
|
||||
</h2>
|
||||
|
||||
<p><i>CoffeeScript on the left, compiled JavaScript output on the right.</i></p>
|
||||
|
||||
@@ -104,21 +132,35 @@
|
||||
<a href="http://github.com/jashkenas/coffee-script/tree/master/examples/">examples</a> folder.
|
||||
</p>
|
||||
|
||||
<h2 id="installation">Installation and Usage</h2>
|
||||
<h2>
|
||||
<span id="installation" class="bookmark"></span>
|
||||
Installation and Usage
|
||||
</h2>
|
||||
|
||||
<p>
|
||||
The CoffeeScript compiler is written in pure Ruby, and is available
|
||||
as a Ruby Gem.
|
||||
The CoffeeScript compiler is written in pure CoffeeScript, and is available
|
||||
as a <a href="http://nodejs.org/">Node.js</a> utility. The core compiler however,
|
||||
does not depend on Node, and can be run in other server-side-JavaScript environments,
|
||||
or in the browser (see "Try CoffeeScript", above).
|
||||
</p>
|
||||
|
||||
<p>
|
||||
To install, first make sure you have a working version of
|
||||
<a href="http://nodejs.org/">Node.js</a>, 0.1.30 or higher. 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.5.0">0.5.0</a>.
|
||||
To install the CoffeeScript compiler system-wide
|
||||
under <tt>/usr/local</tt>, open the directory and run:
|
||||
</p>
|
||||
|
||||
<pre>
|
||||
gem install coffee-script</pre>
|
||||
sudo bin/cake install</pre>
|
||||
|
||||
<p>
|
||||
Installing the gem provides the <tt>coffee</tt> command, which can
|
||||
This provides the <tt>coffee</tt> command, which can
|
||||
be used to compile CoffeeScript <tt>.coffee</tt> files into JavaScript, as
|
||||
well as debug them. In conjunction with
|
||||
<a href="http://narwhaljs.org/">Narwhal</a>, the <tt>coffee</tt>
|
||||
well as debug them. The <tt>coffee</tt>
|
||||
command also provides direct evaluation and an interactive REPL.
|
||||
When compiling to JavaScript, <tt>coffee</tt> writes the output
|
||||
as <tt>.js</tt> files in the same directory by default, but output
|
||||
@@ -129,15 +171,14 @@ gem install coffee-script</pre>
|
||||
<tr>
|
||||
<td width="25%"><code>-i, --interactive</code></td>
|
||||
<td>
|
||||
Launch an interactive CoffeeScript session.
|
||||
Requires <a href="http://narwhaljs.org/">Narwhal</a>.
|
||||
Launch an interactive CoffeeScript session to try short snippets.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>-r, --run</code></td>
|
||||
<td>
|
||||
Compile and execute scripts without saving the intermediate
|
||||
JavaScript. Requires <a href="http://narwhaljs.org/">Narwhal</a>.
|
||||
Compile and execute a given CoffeeScript without saving the intermediate
|
||||
JavaScript.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
@@ -163,7 +204,9 @@ gem install coffee-script</pre>
|
||||
<tr>
|
||||
<td><code>-l, --lint</code></td>
|
||||
<td>
|
||||
If the <tt>jsl</tt> (JavaScript Lint) command is installed, use it
|
||||
If the <tt>jsl</tt>
|
||||
(<a href="http://www.javascriptlint.com/">JavaScript Lint</a>)
|
||||
command is installed, use it
|
||||
to check the compilation of a CoffeeScript file. (Handy in
|
||||
conjunction with <tt>--watch</tt>)
|
||||
</td>
|
||||
@@ -172,42 +215,36 @@ gem install coffee-script</pre>
|
||||
<td><code>-e, --eval</code></td>
|
||||
<td>
|
||||
Compile and print a little snippet of CoffeeScript directly from the
|
||||
command line (or from <b>stdin</b>). For example:<br /><tt>coffee -e "square: x => x * x"</tt>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>-t, --tokens</code></td>
|
||||
<td>
|
||||
Instead of parsing the CoffeeScript, just lex it, and print out the
|
||||
token stream: <tt>[:IDENTIFIER, "square"], [":", ":"], [:PARAM, "x"]</tt> ...
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>-v, --verbose</code></td>
|
||||
<td>
|
||||
As the JavaScript is being generated, print out every step of code
|
||||
generation, including lexical scope and the node in the
|
||||
AST.
|
||||
command line. For example:<br /><tt>coffee -e "square: (x) -> x * x"</tt>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>-n, --no-wrap</code></td>
|
||||
<td>
|
||||
Compile the JavaScript without the top-level function safety wrapper.
|
||||
(Used for CoffeeScript as a Narwhal module.)
|
||||
(Used for CoffeeScript as a Node.js module.)
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>-g, --globals</code></td>
|
||||
<td><code>-t, --tokens</code></td>
|
||||
<td>
|
||||
Suppress all variable declarations at the top-level, effectively adding
|
||||
those variables to the global scope. (Used by the REPL.)
|
||||
Instead of parsing the CoffeeScript, just lex it, and print out the
|
||||
token stream: <tt>[IDENTIFIER square] [ASSIGN :] [PARAM_START (]</tt> ...
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>--install-bundle</code></td>
|
||||
<td><code>-tr, --tree</code></td>
|
||||
<td>
|
||||
Install the TextMate bundle for CoffeeScript syntax highlighting.
|
||||
Instead of compiling the CoffeeScript, just lex and parse it, and print
|
||||
out the parse tree:
|
||||
<pre>
|
||||
Expressions
|
||||
Assign
|
||||
Value "square"
|
||||
Code "x"
|
||||
Op *
|
||||
Value "x"
|
||||
Value "x"</pre>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
@@ -222,7 +259,10 @@ coffee --interactive
|
||||
coffee --watch --lint experimental.coffee
|
||||
coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
|
||||
<h2>Language Reference</h2>
|
||||
<h2>
|
||||
<span id="language" class="bookmark"></span>
|
||||
Language Reference
|
||||
</h2>
|
||||
|
||||
<p>
|
||||
<i>
|
||||
@@ -234,7 +274,14 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
</i>
|
||||
</p>
|
||||
|
||||
<p id="whitespace">
|
||||
<p>
|
||||
<i>
|
||||
Many of the examples can be run (where it makes sense) by pressing the "run"
|
||||
button towards the bottom right. You can also paste examples into
|
||||
"Try CoffeeScript" in the toolbar, and play with them from there.
|
||||
</i>
|
||||
<p>
|
||||
<span id="whitespace" class="bookmark"></span>
|
||||
<b class="header">Significant Whitespace</b>
|
||||
CoffeeScript uses Python-style significant whitespace: You don't need to
|
||||
use semicolons <tt>;</tt> to terminate expressions, ending
|
||||
@@ -247,43 +294,57 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
You can use newlines to break up your expression into smaller pieces,
|
||||
as long as CoffeeScript can tell that the line hasn't finished
|
||||
(similar to how Ruby handles it). For example,
|
||||
if the line ends in an operator, dot, or keyword.
|
||||
You don't need to use parentheses to invoke a function if you're passing
|
||||
arguments:<br /><tt>print "coffee"</tt>
|
||||
</p>
|
||||
|
||||
<p id="functions">
|
||||
<p>
|
||||
You can use newlines to break up your expression into smaller pieces,
|
||||
as long as CoffeeScript can determine that the line hasn't finished yet,
|
||||
because it ends with an operator or a dot.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<span id="functions" class="bookmark"></span>
|
||||
<b class="header">Functions and Invocation</b>
|
||||
Functions are defined by a list of parameters, an arrow, and the
|
||||
function body. The empty function looks like this: <tt>=></tt>. All
|
||||
functions in CoffeeScript are named, for the benefit of debug messages.
|
||||
function body. The empty function looks like this: <tt>-></tt> All
|
||||
functions in CoffeeScript are named by default, for easier debugging.
|
||||
</p>
|
||||
<%= code_for('functions', 'cube(5)') %>
|
||||
<p>
|
||||
If you'd like to assign a function literal to a variable, but not have
|
||||
it be named, just wrap the function definition in parentheses:
|
||||
<tt>((x) -> x * x)</tt>
|
||||
</p>
|
||||
|
||||
<p id="assignment">
|
||||
<p>
|
||||
<span id="assignment" class="bookmark"></span>
|
||||
<b class="header">Assignment</b>
|
||||
Use a colon <tt>:</tt> to assign, as in
|
||||
<a href="http://json.org">JSON</a>. Equal signs are only needed for
|
||||
mathy things.
|
||||
mathy things. While colons are preferred, the two may be used interchangeably,
|
||||
even within object literals.
|
||||
</p>
|
||||
<%= code_for('assignment', 'greeting') %>
|
||||
<p>
|
||||
Declarations of new variables are pushed up to the top of the nearest
|
||||
All declaration of new variables is pushed up to the top of the nearest
|
||||
lexical scope, so that assignment may always be performed within expressions.
|
||||
</p>
|
||||
|
||||
<p id="objects_and_arrays">
|
||||
<p>
|
||||
<span id="objects_and_arrays" class="bookmark"></span>
|
||||
<b class="header">Objects and Arrays</b>
|
||||
Object and Array literals look very similar to their JavaScript cousins.
|
||||
When you spread out each assignment on a separate line, the commas are
|
||||
optional. In this way, assigning object properties looks the same as
|
||||
assigning local variables, and can be moved around freely. You can mix
|
||||
assigning local variables, and can be moved around freely. Feel free to mix
|
||||
and match the two styles.
|
||||
</p>
|
||||
<%= code_for('objects_and_arrays', 'song.join(",")') %>
|
||||
|
||||
<p id="lexical_scope">
|
||||
<p>
|
||||
<span id="lexical_scope" class="bookmark"></span>
|
||||
<b class="header">Lexical Scoping and Variable Safety</b>
|
||||
The CoffeeScript compiler takes care to make sure that all of your variables
|
||||
are properly declared within lexical scope — you never need to write
|
||||
@@ -303,12 +364,18 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
CoffeeScript output is wrapped in an anonymous function:
|
||||
<tt>(function(){ ... })();</tt> This safety wrapper, combined with the
|
||||
automatic generation of the <tt>var</tt> keyword, make it exceedingly difficult
|
||||
to pollute the global namespace by accident. If you'd like to create
|
||||
global variables, attach them as properties on <b>window</b>,
|
||||
or on the <b>exports</b> object in CommonJS.
|
||||
to pollute the global namespace by accident.
|
||||
</p>
|
||||
<p>
|
||||
If you'd like to create top-level variables for other scripts to use,
|
||||
attach them as properties on <b>window</b>, or on the <b>exports</b>
|
||||
object in CommonJS. The <b>existential operator</b> (below), gives you a
|
||||
reliable way to figure out where to add them, if you're targeting both
|
||||
CommonJS and the browser: <tt>root: exports ? this</tt>
|
||||
</p>
|
||||
|
||||
<p id="conditionals">
|
||||
<p>
|
||||
<span id="conditionals" class="bookmark"></span>
|
||||
<b class="header">Conditionals, Ternaries, and Conditional Assignment</b>
|
||||
<b>If/else</b> statements can be written without the use of parentheses and
|
||||
curly brackets. As with functions and other block expressions,
|
||||
@@ -327,17 +394,8 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
truthy variables.
|
||||
</p>
|
||||
|
||||
<p id="existence">
|
||||
<b class="header">The Existence Operator</b>
|
||||
It's a little difficult to check for the existence of a variable in
|
||||
JavaScript. <tt>if (variable) ...</tt> comes close, but fails for zero,
|
||||
the empty string, and false. The existence operator <tt>?</tt> returns true unless
|
||||
a variable is <b>null</b> or <b>undefined</b>, which makes it analogous
|
||||
to Ruby's <tt>nil?</tt>
|
||||
</p>
|
||||
<%= code_for('existence') %>
|
||||
|
||||
<p id="aliases">
|
||||
<p>
|
||||
<span id="aliases" class="bookmark"></span>
|
||||
<b class="header">Aliases</b>
|
||||
Because the <tt>==</tt> operator frequently causes undesirable coercion,
|
||||
is intransitive, and has a different meaning than in other languages,
|
||||
@@ -365,18 +423,23 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
<p>
|
||||
For single-line statements, <tt>unless</tt> can be used as the inverse of <tt>if</tt>.
|
||||
</p>
|
||||
<p>
|
||||
As a shortcut for <tt>this.property</tt>, you can use <tt>@property</tt>.
|
||||
</p>
|
||||
<%= code_for('aliases') %>
|
||||
|
||||
<p id="splats">
|
||||
<p>
|
||||
<span id="splats" class="bookmark"></span>
|
||||
<b class="header">Splats...</b>
|
||||
The JavaScript <b>arguments object</b> is a useful way to work with
|
||||
functions that accept variable numbers of arguments. CoffeeScript provides
|
||||
splats <tt>...</tt>, both for function definition as well as invocation,
|
||||
making variable arguments a little bit more palatable.
|
||||
making variable numbers of arguments a little bit more palatable.
|
||||
</p>
|
||||
<%= code_for('splats', true) %>
|
||||
|
||||
<p id="arguments">
|
||||
<p>
|
||||
<span id="arguments" class="bookmark"></span>
|
||||
<b class="header">Arguments are Arrays</b>
|
||||
If you reference the <b>arguments object</b> directly, it will be converted
|
||||
into a real Array, making all of the
|
||||
@@ -385,11 +448,15 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
</p>
|
||||
<%= code_for('arguments', true) %>
|
||||
|
||||
<p id="while">
|
||||
<p>
|
||||
<span id="while" class="bookmark"></span>
|
||||
<b class="header">While Loops</b>
|
||||
The only low-level loop that CoffeeScript provides is the while loop.
|
||||
The only low-level loop that CoffeeScript provides is the <b>while</b> loop. The
|
||||
main difference from JavaScript is that the <b>while</b> loop can be used
|
||||
as an expression, returning an array containing the result of each iteration
|
||||
through the loop.
|
||||
</p>
|
||||
<%= code_for('while') %>
|
||||
<%= code_for('while', 'lyrics.join("\n")') %>
|
||||
<p>
|
||||
Other JavaScript loops, such as <b>for</b> loops and <b>do-while</b> loops
|
||||
can be mimicked by variations on <b>while</b>, but the hope is that you
|
||||
@@ -397,7 +464,8 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
<b>each</b> (<b>forEach</b>) style iterators, or...
|
||||
</p>
|
||||
|
||||
<p id="comprehensions">
|
||||
<p>
|
||||
<span id="comprehensions" class="bookmark"></span>
|
||||
<b class="header">Comprehensions (Arrays, Objects, and Ranges)</b>
|
||||
For your looping needs, CoffeeScript provides array comprehensions
|
||||
similar to Python's. They replace (and compile into) <b>for</b> loops, with
|
||||
@@ -421,7 +489,8 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
</p>
|
||||
<%= code_for('object_comprehensions', 'ages.join(", ")') %>
|
||||
|
||||
<p id="slice_splice">
|
||||
<p>
|
||||
<span id="slice_splice" class="bookmark"></span>
|
||||
<b class="header">Array Slicing and Splicing with Ranges</b>
|
||||
CoffeeScript borrows Ruby's
|
||||
<a href="http://ruby-doc.org/core/classes/Range.html">range syntax</a>
|
||||
@@ -437,7 +506,8 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
</p>
|
||||
<%= code_for('splices', 'numbers') %>
|
||||
|
||||
<p id="expressions">
|
||||
<p>
|
||||
<span id="expressions" class="bookmark"></span>
|
||||
<b class="header">Everything is an Expression (at least, as much as possible)</b>
|
||||
You might have noticed how even though we don't add return statements
|
||||
to CoffeeScript functions, they nonetheless return their final value.
|
||||
@@ -469,8 +539,45 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
into a function call:
|
||||
</p>
|
||||
<%= code_for('expressions_try', true) %>
|
||||
<p>
|
||||
There are a handful of statements in JavaScript that can't be meaningfully
|
||||
converted into expressions, namely <tt>break</tt>, <tt>continue</tt>,
|
||||
and <tt>return</tt>. If you make use of them within a block of code,
|
||||
CoffeeScript won't try to perform the conversion.
|
||||
</p>
|
||||
|
||||
<p id="inheritance">
|
||||
<p>
|
||||
<span id="existence" class="bookmark"></span>
|
||||
<b class="header">The Existential Operator</b>
|
||||
It's a little difficult to check for the existence of a variable in
|
||||
JavaScript. <tt>if (variable) ...</tt> comes close, but fails for zero,
|
||||
the empty string, and false. CoffeeScript's existential operator <tt>?</tt> returns true unless
|
||||
a variable is <b>null</b> or <b>undefined</b>, which makes it analogous
|
||||
to Ruby's <tt>nil?</tt>
|
||||
</p>
|
||||
<p>
|
||||
It can also be used for safer conditional assignment than <tt>||=</tt>
|
||||
provides, for cases where you may be handling numbers or strings.
|
||||
</p>
|
||||
<%= code_for('existence', 'speed') %>
|
||||
<p>
|
||||
The accessor variant of the existential operator <tt>?.</tt> can be used to soak
|
||||
up null references in a chain of properties. Use it instead
|
||||
of the dot accessor <tt>.</tt> in cases where the base value may be <b>null</b>
|
||||
or <b>undefined</b>. If all of the properties exist then you'll get the expected
|
||||
result, if the chain is broken, <b>undefined</b> is returned instead of
|
||||
the <b>TypeError</b> that would be raised otherwise.
|
||||
</p>
|
||||
<%= code_for('soaks') %>
|
||||
<p>
|
||||
Soaking up nulls is similar to Ruby's
|
||||
<a href="http://andand.rubyforge.org/">andand gem</a>, and to the
|
||||
<a href="http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29">safe navigation operator</a>
|
||||
in Groovy.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<span id="inheritance" class="bookmark"></span>
|
||||
<b class="header">Inheritance, and Calling Super from a Subclass</b>
|
||||
JavaScript's prototypal inheritance has always been a bit of a
|
||||
brain-bender, with a whole family tree of libraries that provide a cleaner
|
||||
@@ -492,21 +599,8 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
</p>
|
||||
<%= code_for('super', true) %>
|
||||
|
||||
<p id="blocks">
|
||||
<b class="header">Blocks</b>
|
||||
Many common looping functions (in Prototype, jQuery, and Underscore,
|
||||
for example) take a single function as their final argument. To make
|
||||
final functions easier to pass, CoffeeScript includes block syntax,
|
||||
so you don't have to close the parentheses on the other side.
|
||||
</p>
|
||||
<%= code_for('blocks') %>
|
||||
<p>
|
||||
If you prefer not to use blocks, you'll need to add a pair of parentheses
|
||||
to help distinguish the arguments from the definition of the function:
|
||||
<tt>_.map(array, (num => num * 2))</tt>
|
||||
</p>
|
||||
|
||||
<p id="pattern_matching">
|
||||
<span id="pattern_matching" class="bookmark"></span>
|
||||
<b class="header">Pattern Matching (Destructuring Assignment)</b>
|
||||
To make extracting values from complex arrays and objects more convenient,
|
||||
CoffeeScript implements ECMAScript Harmony's proposed
|
||||
@@ -528,7 +622,20 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
</p>
|
||||
<%= code_for('object_extraction', 'poet + " — " + street') %>
|
||||
|
||||
<p id="embedded">
|
||||
<p>
|
||||
<span id="fat_arrow" class="bookmark"></span>
|
||||
<b class="header">Function binding</b>
|
||||
The fat arrow <tt>=></tt> can be used to both define a function, and to bind
|
||||
it to the current value of <tt>this</tt>, right on the spot. This is helpful
|
||||
when using callback-based libraries like Prototype or jQuery, for creating
|
||||
iterator functions to pass to <tt>each</tt>, or event-handler functions
|
||||
to use with <tt>bind</tt>. Functions created with the fat arrow are able to access
|
||||
properties of the <tt>this</tt> where they're defined.
|
||||
</p>
|
||||
<%= code_for('fat_arrow') %>
|
||||
|
||||
<p>
|
||||
<span id="embedded" class="bookmark"></span>
|
||||
<b class="header">Embedded JavaScript</b>
|
||||
Hopefully, you'll never need to use it, but if you ever need to intersperse
|
||||
snippets of JavaScript within your CoffeeScript, you can
|
||||
@@ -536,7 +643,8 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
</p>
|
||||
<%= code_for('embedded', 'hi()') %>
|
||||
|
||||
<p id="switch">
|
||||
<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
|
||||
@@ -546,16 +654,33 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
in a returnable, assignable expression. The format is: <tt>switch</tt> condition,
|
||||
<tt>when</tt> clauses, <tt>else</tt> the default case.
|
||||
</p>
|
||||
<p>
|
||||
As in Ruby, <b>switch</b> statements in CoffeeScript can take multiple
|
||||
values for each <b>when</b> clause. If any of the values match, the clause
|
||||
runs.
|
||||
</p>
|
||||
<%= code_for('switch') %>
|
||||
|
||||
<p id="try">
|
||||
<p>
|
||||
<span id="try" class="bookmark"></span>
|
||||
<b class="header">Try/Catch/Finally</b>
|
||||
Try/catch statements are just about the same as JavaScript (although
|
||||
they work as expressions).
|
||||
</p>
|
||||
<%= code_for('try') %>
|
||||
|
||||
<p id="strings">
|
||||
<p>
|
||||
<span id="comparisons" class="bookmark"></span>
|
||||
<b class="header">Chained Comparisons</b>
|
||||
CoffeeScript borrows
|
||||
<a href="http://docs.python.org/reference/expressions.html#notin">chained comparisons</a>
|
||||
from Python — making it easy to test if a value falls within a
|
||||
certain range.
|
||||
</p>
|
||||
<%= code_for('comparisons', 'healthy') %>
|
||||
|
||||
<p>
|
||||
<span id="strings" class="bookmark"></span>
|
||||
<b class="header">Multiline Strings and Heredocs</b>
|
||||
Multiline strings are allowed in CoffeeScript.
|
||||
</p>
|
||||
@@ -568,63 +693,102 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
</p>
|
||||
<%= code_for('heredocs') %>
|
||||
|
||||
<h2 id="resources">Resources</h2>
|
||||
<h2>
|
||||
<span id="cake" class="bookmark"></span>
|
||||
Cake, and Cakefiles
|
||||
</h2>
|
||||
|
||||
<p>
|
||||
CoffeeScript includes a simple build system similar to Make and Rake. Naturally,
|
||||
it's called Cake, and is used for the build and test tasks for the CoffeeScript
|
||||
language itself. Tasks are defined in a file named <tt>Cakefile</tt>, and
|
||||
can be invoked by running <tt>cake taskname</tt> from within the directory.
|
||||
To print a list of all the tasks, just run <tt>cake</tt>.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Task definitions are written in CoffeeScript, so you can put arbitrary code
|
||||
in your Cakefile. Define a task with a name, a long description, and the
|
||||
function to invoke when the task is run. Here's a hypothetical task
|
||||
that uses the Node.js API.
|
||||
</p>
|
||||
<%= code_for('cake_tasks') %>
|
||||
|
||||
<h2>
|
||||
<span id="resources" class="bookmark"></span>
|
||||
Resources
|
||||
</h2>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<a href="http://github.com/jashkenas/coffee-script/">Source Code</a><br />
|
||||
After checking out the source, make sure to run <tt>rake build:parser</tt>
|
||||
to generate an up-to-date version of the Racc parser.
|
||||
Use <tt>bin/coffee</tt> to test your changes,
|
||||
<tt>rake test</tt> to run the test suite,
|
||||
and <tt>rake gem:install</tt> to
|
||||
create and install a custom version of the gem.
|
||||
Use <tt>bin/coffee</tt> to test your changes,<br />
|
||||
<tt>bin/cake test</tt> to run the test suite,<br />
|
||||
<tt>bin/cake build</tt> to rebuild the CoffeeScript compiler, and <br />
|
||||
<tt>bin/cake build:parser</tt> to regenerate the Jison parser if you're
|
||||
working on the grammar.
|
||||
</li>
|
||||
<li>
|
||||
<a href="http://github.com/jashkenas/coffee-script/issues">Bugs, Feature Requests, and General Discussion</a>
|
||||
<a href="http://github.com/jashkenas/coffee-script/issues">CoffeeScript Issues</a><br />
|
||||
Bugs reports, feature requests, and general discussion all belong here.
|
||||
</li>
|
||||
<li>
|
||||
<a href="http://github.com/jnicklas/bistro_car">BistroCar</a><br />
|
||||
A Rails plugin by
|
||||
<a href="http://github.com/jnicklas">Jonas Nicklas</a>
|
||||
that includes CoffeeScript helpers,
|
||||
bundling and minification.
|
||||
If you'd like to chat, stop by <tt>#coffeescript</tt> on Freenode.
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h2 id="contributing">Contributing</h2>
|
||||
|
||||
<h2>
|
||||
<span id="change_log" class="bookmark"></span>
|
||||
Change Log
|
||||
</h2>
|
||||
|
||||
<p>
|
||||
Here's a wish list of things that would be wonderful to have contributed:
|
||||
<b class="header" style="margin-top: 20px;">0.5.0</b>
|
||||
CoffeeScript 0.5.0 is a major release, While there are no language changes,
|
||||
the Ruby compiler has been removed in favor of a self-hosting
|
||||
compiler written in pure CoffeeScript.
|
||||
</p>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<a href="http://github.com/jashkenas/coffee-script/issues#issue/8">
|
||||
A CoffeeScript version of the compiler.
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
Test cases for any syntax errors you encounter that you think CoffeeScript
|
||||
should be able to compile properly.
|
||||
</li>
|
||||
<li>
|
||||
A tutorial that introduces CoffeeScript from the ground up for folks
|
||||
without knowledge of JavaScript.
|
||||
</li>
|
||||
<li>
|
||||
Integration with Processing.js's JavaScript API (this would depend on
|
||||
having a JavaScript version of the compiler).
|
||||
</li>
|
||||
<li>
|
||||
A lot of the code generation in <tt>nodes.rb</tt> gets into messy
|
||||
string manipulation. Techniques for cleaning this up across the board
|
||||
would be appreciated.
|
||||
</li>
|
||||
</ul>
|
||||
<p>
|
||||
<b class="header" style="margin-top: 20px;">0.3.2</b>
|
||||
<tt>@property</tt> is now a shorthand for <tt>this.property</tt>.<br />
|
||||
Switched the default JavaScript engine from Narwhal to Node.js. Pass
|
||||
the <tt>--narwhal</tt> flag if you'd like to continue using it.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b class="header" style="margin-top: 20px;">0.3.0</b>
|
||||
CoffeeScript 0.3 includes major syntax changes:
|
||||
<br />
|
||||
The function symbol was changed to
|
||||
<tt>-></tt>, and the bound function symbol is now <tt>=></tt>.
|
||||
<br />
|
||||
Parameter lists in function definitions must now be wrapped in parentheses.
|
||||
<br />
|
||||
Added property soaking, with the <tt>?.</tt> operator.
|
||||
<br />
|
||||
Made parentheses optional, when invoking functions with arguments.
|
||||
<br />
|
||||
Removed the obsolete block literal syntax.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b class="header" style="margin-top: 20px;">0.2.6</b>
|
||||
Added Python-style chained comparisons, the conditional existence
|
||||
operator <tt>?=</tt>, and some examples from <i>Beautiful Code</i>.
|
||||
Bugfixes relating to statement-to-expression conversion, arguments-to-array
|
||||
conversion, and the TextMate syntax highlighter.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b class="header" style="margin-top: 20px;">0.2.5</b>
|
||||
The conditions in switch statements can now take multiple values at once —
|
||||
If any of them are true, the case will run. Added the long arrow <tt>==></tt>,
|
||||
which defines and immediately binds a function to <tt>this</tt>. While loops can
|
||||
now be used as expressions, in the same way that comprehensions can. Splats
|
||||
can be used within pattern matches to soak up the rest of an array.
|
||||
</p>
|
||||
|
||||
<h2 id="change_log">Change Log</h2>
|
||||
|
||||
<p>
|
||||
<b class="header" style="margin-top: 20px;">0.2.4</b>
|
||||
Added ECMAScript Harmony style destructuring assignment, for dealing with
|
||||
@@ -668,7 +832,7 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
<b class="header" style="margin-top: 20px;">0.2.0</b>
|
||||
Major release. Significant whitespace. Better statement-to-expression
|
||||
conversion. Splats. Splice literals. Object comprehensions. Blocks.
|
||||
The existence operator. Many thanks to all the folks who posted issues,
|
||||
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
|
||||
and expression help.
|
||||
@@ -740,5 +904,32 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
||||
|
||||
</div>
|
||||
|
||||
<script type="text/javascript" src="lib/rewriter.js"></script>
|
||||
<script type="text/javascript" src="lib/lexer.js"></script>
|
||||
<script type="text/javascript" src="lib/parser.js"></script>
|
||||
<script type="text/javascript" src="lib/scope.js"></script>
|
||||
<script type="text/javascript" src="lib/nodes.js"></script>
|
||||
<script type="text/javascript" src="lib/coffee-script.js"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
window.repl_compile = function() {
|
||||
var source = document.getElementById('repl_source').value;
|
||||
window.compiled_js = '';
|
||||
try {
|
||||
window.compiled_js = CoffeeScript.compile(source, {no_wrap: true});
|
||||
} catch(error) {
|
||||
alert(error);
|
||||
}
|
||||
document.getElementById('repl_results').innerHTML = window.compiled_js;
|
||||
}
|
||||
window.repl_run = function() {
|
||||
try {
|
||||
eval(window.compiled_js);
|
||||
} catch(error) {
|
||||
alert(error);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -10,4 +10,5 @@
|
||||
let_the_wild_rumpus_begin();
|
||||
}
|
||||
car.speed < speed_limit ? accelerate() : null;
|
||||
print("My name is " + this.name);
|
||||
})();
|
||||
@@ -1,7 +1,8 @@
|
||||
(function(){
|
||||
var backwards;
|
||||
backwards = function backwards() {
|
||||
return alert(Array.prototype.slice.call(arguments, 0).reverse());
|
||||
arguments = Array.prototype.slice.call(arguments, 0);
|
||||
return alert(arguments.reverse());
|
||||
};
|
||||
backwards("stairway", "to", "heaven");
|
||||
})();
|
||||
@@ -1,21 +1,21 @@
|
||||
(function(){
|
||||
var __a, __b, __c, __d, __e, __f, __g, food, lunch, roid, roid2;
|
||||
var _a, _b, _c, _d, _e, _f, _g, food, lunch, roid, roid2;
|
||||
// Eat lunch.
|
||||
lunch = (function() {
|
||||
__a = []; __b = ['toast', 'cheese', 'wine'];
|
||||
for (__c=0; __c<__b.length; __c++) {
|
||||
food = __b[__c];
|
||||
__a.push(eat(food));
|
||||
_a = []; _b = ['toast', 'cheese', 'wine'];
|
||||
for (_c = 0; _c < _b.length; _c++) {
|
||||
food = _b[_c];
|
||||
_a.push(eat(food));
|
||||
}
|
||||
return __a;
|
||||
})();
|
||||
return _a;
|
||||
}).call(this);
|
||||
// Naive collision detection.
|
||||
__d = asteroids;
|
||||
for (__e=0; __e<__d.length; __e++) {
|
||||
roid = __d[__e];
|
||||
__f = asteroids;
|
||||
for (__g=0; __g<__f.length; __g++) {
|
||||
roid2 = __f[__g];
|
||||
_d = asteroids;
|
||||
for (_e = 0; _e < _d.length; _e++) {
|
||||
roid = _d[_e];
|
||||
_f = asteroids;
|
||||
for (_g = 0; _g < _f.length; _g++) {
|
||||
roid2 = _f[_g];
|
||||
if (roid !== roid2) {
|
||||
if (roid.overlaps(roid2)) {
|
||||
roid.explode();
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
(function(){
|
||||
$('table.list').each(function(table) {
|
||||
return $('tr.account', table).each(function(row) {
|
||||
row.show();
|
||||
return row.highlight();
|
||||
});
|
||||
});
|
||||
})();
|
||||
14
documentation/js/cake_tasks.js
Normal file
14
documentation/js/cake_tasks.js
Normal file
@@ -0,0 +1,14 @@
|
||||
(function(){
|
||||
process.mixin(require('assert'));
|
||||
task('test', 'run each of the unit tests', function() {
|
||||
var _a, _b, _c, test;
|
||||
_a = []; _b = test_files;
|
||||
for (_c = 0; _c < _b.length; _c++) {
|
||||
test = _b[_c];
|
||||
_a.push(fs.readFile(test, function(err, code) {
|
||||
return eval(coffee.compile(code));
|
||||
}));
|
||||
}
|
||||
return _a;
|
||||
});
|
||||
})();
|
||||
5
documentation/js/comparisons.js
Normal file
5
documentation/js/comparisons.js
Normal file
@@ -0,0 +1,5 @@
|
||||
(function(){
|
||||
var cholesterol, healthy;
|
||||
cholesterol = 127;
|
||||
healthy = (200 > cholesterol) && (cholesterol > 60);
|
||||
})();
|
||||
@@ -1,5 +1,5 @@
|
||||
(function(){
|
||||
var date, mood;
|
||||
var date, expensive, mood;
|
||||
if (singing) {
|
||||
mood = greatly_improved;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
(function(){
|
||||
var solipsism;
|
||||
var solipsism, speed;
|
||||
if ((typeof mind !== "undefined" && mind !== null) && !(typeof world !== "undefined" && world !== null)) {
|
||||
solipsism = true;
|
||||
}
|
||||
speed = (typeof speed !== "undefined" && speed !== null) ? speed : 140;
|
||||
})();
|
||||
@@ -1,4 +1,4 @@
|
||||
(function(){
|
||||
var one, six, three, two;
|
||||
six = (one = 1) + (two = 2) + (three = 3);
|
||||
six = ((one = 1)) + ((two = 2)) + ((three = 3));
|
||||
})();
|
||||
@@ -1,13 +1,12 @@
|
||||
(function(){
|
||||
var __a, __b, globals, name;
|
||||
var _a, _b, globals, name;
|
||||
var __hasProp = Object.prototype.hasOwnProperty;
|
||||
// The first ten global properties.
|
||||
globals = ((function() {
|
||||
__a = []; __b = window;
|
||||
for (name in __b) {
|
||||
if (__b.hasOwnProperty(name)) {
|
||||
__a.push(name);
|
||||
}
|
||||
}
|
||||
return __a;
|
||||
})()).slice(0, 10);
|
||||
globals = (function() {
|
||||
_a = []; _b = window;
|
||||
for (name in _b) { if (__hasProp.call(_b, name)) {
|
||||
_a.push(name);
|
||||
}}
|
||||
return _a;
|
||||
}).call(this).slice(0, 10);
|
||||
})();
|
||||
@@ -3,7 +3,7 @@
|
||||
try {
|
||||
return nonexistent / undefined;
|
||||
} catch (error) {
|
||||
return "Caught an error: " + error;
|
||||
return "And the error is ... " + error;
|
||||
}
|
||||
})());
|
||||
}).call(this));
|
||||
})();
|
||||
15
documentation/js/fat_arrow.js
Normal file
15
documentation/js/fat_arrow.js
Normal file
@@ -0,0 +1,15 @@
|
||||
(function(){
|
||||
var Account;
|
||||
Account = function Account(customer, cart) {
|
||||
this.customer = customer;
|
||||
this.cart = cart;
|
||||
return $('.shopping_cart').bind('click', (function(__this) {
|
||||
var __func = function(event) {
|
||||
return this.customer.purchase(this.cart);
|
||||
};
|
||||
return (function() {
|
||||
return __func.apply(__this, arguments);
|
||||
});
|
||||
})(this));
|
||||
};
|
||||
})();
|
||||
@@ -1,11 +1,11 @@
|
||||
(function(){
|
||||
var __a, city, forecast, temp, weather_report;
|
||||
var _a, city, forecast, temp, weather_report;
|
||||
weather_report = function weather_report(location) {
|
||||
// Make an Ajax request to fetch the weather...
|
||||
return [location, 72, "Mostly Sunny"];
|
||||
};
|
||||
__a = weather_report("Berkeley, CA");
|
||||
city = __a[0];
|
||||
temp = __a[1];
|
||||
forecast = __a[2];
|
||||
_a = weather_report("Berkeley, CA");
|
||||
city = _a[0];
|
||||
temp = _a[1];
|
||||
forecast = _a[2];
|
||||
})();
|
||||
@@ -1,18 +1,17 @@
|
||||
(function(){
|
||||
var __a, __b, age, ages, child, years_old;
|
||||
var _a, _b, age, ages, child, years_old;
|
||||
var __hasProp = Object.prototype.hasOwnProperty;
|
||||
years_old = {
|
||||
max: 10,
|
||||
ida: 9,
|
||||
tim: 11
|
||||
};
|
||||
ages = (function() {
|
||||
__a = []; __b = years_old;
|
||||
for (child in __b) {
|
||||
age = __b[child];
|
||||
if (__b.hasOwnProperty(child)) {
|
||||
__a.push(child + " is " + age);
|
||||
}
|
||||
}
|
||||
return __a;
|
||||
})();
|
||||
_a = []; _b = years_old;
|
||||
for (child in _b) { if (__hasProp.call(_b, child)) {
|
||||
age = _b[child];
|
||||
_a.push(child + " is " + age);
|
||||
}}
|
||||
return _a;
|
||||
}).call(this);
|
||||
})();
|
||||
@@ -1,5 +1,5 @@
|
||||
(function(){
|
||||
var __a, __b, __c, city, futurists, poet, street;
|
||||
var _a, _b, _c, city, futurists, poet, street;
|
||||
futurists = {
|
||||
sculptor: "Umberto Boccioni",
|
||||
painter: "Vladimir Burliuk",
|
||||
@@ -8,10 +8,10 @@
|
||||
address: ["Via Roma 42R", "Bellagio, Italy 22021"]
|
||||
}
|
||||
};
|
||||
__a = futurists;
|
||||
__b = __a.poet;
|
||||
poet = __b.name;
|
||||
__c = __b.address;
|
||||
street = __c[0];
|
||||
city = __c[1];
|
||||
_a = futurists;
|
||||
_b = _a.poet;
|
||||
poet = _b.name;
|
||||
_c = _b.address;
|
||||
street = _c[0];
|
||||
city = _c[1];
|
||||
})();
|
||||
@@ -1,5 +1,5 @@
|
||||
(function(){
|
||||
var __a, __b, __c, cubed_list, list, math, num, number, opposite_day, race, square;
|
||||
var _a, _b, _c, cubed_list, list, math, num, number, opposite_day, race, square;
|
||||
// Assignment:
|
||||
number = 42;
|
||||
opposite_day = true;
|
||||
@@ -33,11 +33,11 @@
|
||||
}
|
||||
// Array comprehensions:
|
||||
cubed_list = (function() {
|
||||
__a = []; __b = list;
|
||||
for (__c=0; __c<__b.length; __c++) {
|
||||
num = __b[__c];
|
||||
__a.push(math.cube(num));
|
||||
_a = []; _b = list;
|
||||
for (_c = 0; _c < _b.length; _c++) {
|
||||
num = _b[_c];
|
||||
_a.push(math.cube(num));
|
||||
}
|
||||
return __a;
|
||||
})();
|
||||
return _a;
|
||||
}).call(this);
|
||||
})();
|
||||
@@ -1,8 +1,8 @@
|
||||
(function(){
|
||||
var __a, and_switch, bait;
|
||||
var _a, and_switch, bait;
|
||||
bait = 1000;
|
||||
and_switch = 0;
|
||||
__a = [and_switch, bait];
|
||||
bait = __a[0];
|
||||
and_switch = __a[1];
|
||||
_a = [and_switch, bait];
|
||||
bait = _a[0];
|
||||
and_switch = _a[1];
|
||||
})();
|
||||
@@ -1,21 +1,21 @@
|
||||
(function(){
|
||||
var __a, __b, __c, __d, __e, countdown, egg_delivery, num;
|
||||
var _a, _b, _c, _d, _e, countdown, egg_delivery, num;
|
||||
countdown = (function() {
|
||||
__a = []; __d = 10; __e = 1;
|
||||
for (__c=0, num=__d; (__d <= __e ? num <= __e : num >= __e); (__d <= __e ? num += 1 : num -= 1), __c++) {
|
||||
__a.push(num);
|
||||
_a = []; _d = 10; _e = 1;
|
||||
for (_c=0, num=_d; (_d <= _e ? num <= _e : num >= _e); (_d <= _e ? num += 1 : num -= 1), _c++) {
|
||||
_a.push(num);
|
||||
}
|
||||
return __a;
|
||||
})();
|
||||
return _a;
|
||||
}).call(this);
|
||||
egg_delivery = function egg_delivery() {
|
||||
var __f, __g, __h, __i, __j, dozen_eggs, i;
|
||||
__f = []; __i = 0; __j = eggs.length;
|
||||
for (__h=0, i=__i; (__i <= __j ? i < __j : i > __j); (__i <= __j ? i += 12 : i -= 12), __h++) {
|
||||
__f.push((function() {
|
||||
var _f, _g, _h, _i, _j, dozen_eggs, i;
|
||||
_f = []; _i = 0; _j = eggs.length;
|
||||
for (_h=0, i=_i; (_i <= _j ? i < _j : i > _j); (_i <= _j ? i += 12 : i -= 12), _h++) {
|
||||
_f.push((function() {
|
||||
dozen_eggs = eggs.slice(i, i + 12);
|
||||
return deliver(new egg_carton(dozen));
|
||||
})());
|
||||
}).call(this));
|
||||
}
|
||||
return __f;
|
||||
return _f;
|
||||
};
|
||||
})();
|
||||
4
documentation/js/soaks.js
Normal file
4
documentation/js/soaks.js
Normal file
@@ -0,0 +1,4 @@
|
||||
(function(){
|
||||
var _a;
|
||||
(_a = lottery.draw_winner()) == null ? undefined : _a.address == null ? undefined : _a.address.zipcode;
|
||||
})();
|
||||
@@ -1,7 +1,7 @@
|
||||
(function(){
|
||||
var contenders, gold, medalists, silver, the_field;
|
||||
gold = silver = the_field = "unknown";
|
||||
medalists = function medalists(first, second) {
|
||||
var award_medals, contenders, gold, silver, the_field;
|
||||
gold = (silver = (the_field = "unknown"));
|
||||
award_medals = function award_medals(first, second) {
|
||||
var rest;
|
||||
rest = Array.prototype.slice.call(arguments, 2);
|
||||
gold = first;
|
||||
@@ -9,7 +9,7 @@
|
||||
return the_field = rest;
|
||||
};
|
||||
contenders = ["Michael Phelps", "Liu Xiang", "Yao Ming", "Allyson Felix", "Shawn Johnson", "Roman Sebrle", "Guo Jingjing", "Tyson Gay", "Asafa Powell", "Usain Bolt"];
|
||||
medalists.apply(this, contenders);
|
||||
award_medals.apply(this, contenders);
|
||||
alert("Gold: " + gold);
|
||||
alert("Silver: " + silver);
|
||||
alert("The Field: " + the_field);
|
||||
|
||||
@@ -1,33 +1,30 @@
|
||||
(function(){
|
||||
var Animal, Horse, Snake, __a, __b, sam, tom;
|
||||
Animal = function Animal() {
|
||||
};
|
||||
var Animal, Horse, Snake, _a, _b, sam, tom;
|
||||
Animal = function Animal() { };
|
||||
Animal.prototype.move = function move(meters) {
|
||||
return alert(this.name + " moved " + meters + "m.");
|
||||
};
|
||||
Snake = function Snake(name) {
|
||||
var __a;
|
||||
__a = this.name = name;
|
||||
return Snake === this.constructor ? this : __a;
|
||||
this.name = name;
|
||||
return this;
|
||||
};
|
||||
__a = function(){};
|
||||
__a.prototype = Animal.prototype;
|
||||
_a = function(){};
|
||||
_a.prototype = Animal.prototype;
|
||||
Snake.__superClass__ = Animal.prototype;
|
||||
Snake.prototype = new __a();
|
||||
Snake.prototype = new _a();
|
||||
Snake.prototype.constructor = Snake;
|
||||
Snake.prototype.move = function move() {
|
||||
alert("Slithering...");
|
||||
return Snake.__superClass__.move.call(this, 5);
|
||||
};
|
||||
Horse = function Horse(name) {
|
||||
var __b;
|
||||
__b = this.name = name;
|
||||
return Horse === this.constructor ? this : __b;
|
||||
this.name = name;
|
||||
return this;
|
||||
};
|
||||
__b = function(){};
|
||||
__b.prototype = Animal.prototype;
|
||||
_b = function(){};
|
||||
_b.prototype = Animal.prototype;
|
||||
Horse.__superClass__ = Animal.prototype;
|
||||
Horse.prototype = new __b();
|
||||
Horse.prototype = new _b();
|
||||
Horse.prototype.constructor = Horse;
|
||||
Horse.prototype.move = function move() {
|
||||
alert("Galloping...");
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
(function(){
|
||||
if (day === "Tuesday") {
|
||||
eat_breakfast();
|
||||
} else if (day === "Wednesday") {
|
||||
if (day === "Mon") {
|
||||
go_to_work();
|
||||
} else if (day === "Tue") {
|
||||
go_to_the_park();
|
||||
} else if (day === "Saturday") {
|
||||
} else if (day === "Thu") {
|
||||
go_ice_fishing();
|
||||
} else if (day === "Fri" || day === "Sat") {
|
||||
if (day === bingo_day) {
|
||||
go_to_bingo();
|
||||
go_dancing();
|
||||
}
|
||||
} else if (day === "Sunday") {
|
||||
} else if (day === "Sun") {
|
||||
go_to_church();
|
||||
} else {
|
||||
go_to_work();
|
||||
|
||||
@@ -1,9 +1,22 @@
|
||||
(function(){
|
||||
while (demand > supply) {
|
||||
sell();
|
||||
restock();
|
||||
}
|
||||
while (supply > demand) {
|
||||
buy();
|
||||
var _a, lyrics, num;
|
||||
// Econ 101
|
||||
if (this.studying_economics) {
|
||||
while (supply > demand) {
|
||||
buy();
|
||||
}
|
||||
while (supply < demand) {
|
||||
sell();
|
||||
}
|
||||
}
|
||||
// Nursery Rhyme
|
||||
num = 6;
|
||||
lyrics = (function() {
|
||||
_a = [];
|
||||
while (num -= 1) {
|
||||
_a.push(num + " little monkeys, jumping on the bed. \
|
||||
One fell out and bumped his head.");
|
||||
}
|
||||
return _a;
|
||||
}).call(this);
|
||||
})();
|
||||
@@ -1,77 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
||||
<title>Quickie CoffeeScript Speed Tests</title>
|
||||
<script type="text/javascript" src="http://www.broofa.com/Tools/JSLitmus/JSLitmus.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>Quickie CoffeeScript Speed Tests</h1>
|
||||
|
||||
<script type="text/javascript">
|
||||
var num = 1000;
|
||||
var arr = [];
|
||||
while (num--) arr.push(num);
|
||||
|
||||
JSLitmus.test('current comprehensions', function() {
|
||||
__a = arr;
|
||||
__c = [];
|
||||
for (__b in __a) {
|
||||
if (__a.hasOwnProperty(__b)) {
|
||||
num = __a[__b];
|
||||
__d = num;
|
||||
__c.push(num);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
JSLitmus.test('raw for loop (best we can do)', function() {
|
||||
__a = arr;
|
||||
__c = new Array(__a.length);
|
||||
for (__b=0; __b < __a.length; __b++) {
|
||||
__c[__b] = __a[__b];
|
||||
}
|
||||
});
|
||||
|
||||
JSLitmus.test('current without hasOwnProperty check', function() {
|
||||
__a = arr;
|
||||
__c = [];
|
||||
for (__b in __a) {
|
||||
num = __a[__b];
|
||||
__d = num;
|
||||
__c.push(num);
|
||||
}
|
||||
});
|
||||
|
||||
JSLitmus.test('raw for..in loop', function() {
|
||||
__a = arr;
|
||||
__c = new Array(__a.length);
|
||||
for (__b in __a) {
|
||||
__c[__b] = __a[__b];
|
||||
}
|
||||
});
|
||||
|
||||
JSLitmus.test('weepy\'s comprehensions', function() {
|
||||
__c = []; __a = arr;
|
||||
__d = function(num, __b) {
|
||||
__c.push(num);
|
||||
};
|
||||
if (__a instanceof Array) {
|
||||
for (__b=0; __b<__a.length; __b++) __d(__a[__b], __b);
|
||||
} else {
|
||||
for (__b in __a) { if (__a.hasOwnProperty(__b)) __d(__a[__b], __b); }
|
||||
}
|
||||
});
|
||||
|
||||
JSLitmus.test('CoffeeScript 0.2.2 comprehensions', function() {
|
||||
__c = []; __a = arr;
|
||||
for (__b=0; __b<__a.length; __b++) {
|
||||
num = __a[__b];
|
||||
__c.push(num);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
File diff suppressed because it is too large
Load Diff
16
examples/beautiful_code/binary_search.coffee
Normal file
16
examples/beautiful_code/binary_search.coffee
Normal file
@@ -0,0 +1,16 @@
|
||||
# Beautiful Code, Chapter 6.
|
||||
# The implementation of binary search that is tested.
|
||||
|
||||
# Return the index of an element in a sorted list. (or -1, if not present)
|
||||
index: (list, target) ->
|
||||
[low, high]: [0, list.length]
|
||||
while low < high
|
||||
mid: (low + high) >> 1
|
||||
val: list[mid]
|
||||
return mid if val is target
|
||||
if val < target then low: mid + 1 else high: mid
|
||||
return -1
|
||||
|
||||
puts 2 is index([10, 20, 30, 40, 50], 30)
|
||||
puts 4 is index([-97, 35, 67, 88, 1200], 1200)
|
||||
puts 0 is index([0, 45, 70], 0)
|
||||
13
examples/beautiful_code/quicksort_runtime.coffee
Normal file
13
examples/beautiful_code/quicksort_runtime.coffee
Normal file
@@ -0,0 +1,13 @@
|
||||
# Beautiful Code, Chapter 3.
|
||||
# Produces the expected runtime of Quicksort, for every integer from 1 to N.
|
||||
|
||||
runtime: (N) ->
|
||||
[sum, t]: [0, 0]
|
||||
for n in [1..N]
|
||||
sum += 2 * t
|
||||
t: n - 1 + sum / n
|
||||
t
|
||||
|
||||
puts runtime(3) is 2.6666666666666665
|
||||
puts runtime(5) is 7.4
|
||||
puts runtime(8) is 16.92142857142857
|
||||
34
examples/beautiful_code/regular_expression_matcher.coffee
Normal file
34
examples/beautiful_code/regular_expression_matcher.coffee
Normal file
@@ -0,0 +1,34 @@
|
||||
# Beautiful Code, Chapter 1.
|
||||
# Implements a regular expression matcher that supports character matches,
|
||||
# '.', '^', '$', and '*'.
|
||||
|
||||
# Search for the regexp anywhere in the text.
|
||||
match: (regexp, text) ->
|
||||
return match_here(regexp.slice(1), text) if regexp[0] is '^'
|
||||
while text
|
||||
return true if match_here(regexp, text)
|
||||
text: text.slice(1)
|
||||
false
|
||||
|
||||
# Search for the regexp at the beginning of the text.
|
||||
match_here: (regexp, text) ->
|
||||
[cur, next]: [regexp[0], regexp[1]]
|
||||
if regexp.length is 0 then return true
|
||||
if next is '*' then return match_star(cur, regexp.slice(2), text)
|
||||
if cur is '$' and not next then return text.length is 0
|
||||
if text and (cur is '.' or cur is text[0]) then return match_here(regexp.slice(1), text.slice(1))
|
||||
false
|
||||
|
||||
# Search for a kleene star match at the beginning of the text.
|
||||
match_star: (c, regexp, text) ->
|
||||
while true
|
||||
return true if match_here(regexp, text)
|
||||
return false unless text and (text[0] is c or c is '.')
|
||||
text: text.slice(1)
|
||||
|
||||
puts match("ex", "some text")
|
||||
puts match("s..t", "spit")
|
||||
puts match("^..t", "buttercup")
|
||||
puts match("i..$", "cherries")
|
||||
puts match("o*m", "vrooooommm!")
|
||||
puts match("^hel*o$", "hellllllo")
|
||||
57
examples/blocks.coffee
Normal file
57
examples/blocks.coffee
Normal file
@@ -0,0 +1,57 @@
|
||||
# After wycats' http://yehudakatz.com/2010/02/07/the-building-blocks-of-ruby/
|
||||
|
||||
# Sinatra.
|
||||
get '/hello', ->
|
||||
'Hello World'
|
||||
|
||||
|
||||
# Append.
|
||||
append: (location, data) ->
|
||||
path: new Pathname location
|
||||
throw "Location does not exist" unless path.exists()
|
||||
|
||||
File.open path, 'a', (file) ->
|
||||
file.puts YAML.dump data
|
||||
|
||||
data
|
||||
|
||||
|
||||
# Rubinius' File.open implementation.
|
||||
File.open: (path, mode, block) ->
|
||||
io: new File path, mode
|
||||
|
||||
return io unless block
|
||||
|
||||
try
|
||||
block io
|
||||
finally
|
||||
try
|
||||
io.close() unless io.closed()
|
||||
catch error
|
||||
# nothing, just swallow them.
|
||||
|
||||
|
||||
# Write.
|
||||
write: (location, data) ->
|
||||
path = new Pathname location
|
||||
raise "Location does not exist" unless path.exists()
|
||||
|
||||
File.open path, 'w', (file) ->
|
||||
return false if Digest.MD5.hexdigest(file.read()) is data.hash()
|
||||
file.puts YAML.dump data
|
||||
true
|
||||
|
||||
|
||||
# Rails' respond_to.
|
||||
index: ->
|
||||
people: Person.find 'all'
|
||||
|
||||
respond_to (format) ->
|
||||
format.html()
|
||||
format.xml -> render { xml: people.xml() }
|
||||
|
||||
|
||||
# Synchronization.
|
||||
synchronize: (block) ->
|
||||
lock()
|
||||
try block() finally unlock()
|
||||
@@ -1,14 +1,14 @@
|
||||
# Functions:
|
||||
square: x => x * x
|
||||
square: (x) -> x * x
|
||||
|
||||
sum: x, y => x + y
|
||||
sum: (x, y) -> x + y
|
||||
|
||||
odd: x => x % 2 is 0
|
||||
odd: (x) -> x % 2 isnt 0
|
||||
|
||||
even: x => x % 2 isnt 0
|
||||
even: (x) -> x % 2 is 0
|
||||
|
||||
run_loop: =>
|
||||
fire_events(e => e.stopPropagation())
|
||||
run_loop: ->
|
||||
fire_events((e) -> e.stopPropagation())
|
||||
listen()
|
||||
wait()
|
||||
|
||||
@@ -22,14 +22,14 @@ spaced_out_multiline_object: {
|
||||
three: new Idea()
|
||||
|
||||
inner_obj: {
|
||||
freedom: => _.freedom()
|
||||
freedom: -> _.freedom()
|
||||
}
|
||||
}
|
||||
|
||||
# Arrays:
|
||||
stooges: [{moe: 45}, {curly: 43}, {larry: 46}]
|
||||
|
||||
exponents: [(x => x), (x => x * x), (x => x * x * x)]
|
||||
exponents: [(x) -> x, (x) -> x * x, (x) -> x * x * x]
|
||||
|
||||
empty: []
|
||||
|
||||
@@ -54,7 +54,7 @@ decoration: medal_of_honor if war_hero
|
||||
go_to_sleep() unless coffee
|
||||
|
||||
# Returning early:
|
||||
race: =>
|
||||
race: ->
|
||||
run()
|
||||
walk()
|
||||
crawl()
|
||||
@@ -103,7 +103,7 @@ while true
|
||||
|
||||
# Lexical scoping.
|
||||
v_1: 5
|
||||
change_a_and_set_b: =>
|
||||
change_a_and_set_b: ->
|
||||
v_1: 10
|
||||
v_2: 15
|
||||
v_2: 20
|
||||
@@ -128,7 +128,7 @@ activity: switch day
|
||||
else go_to_work()
|
||||
|
||||
# Semicolons can optionally be used instead of newlines.
|
||||
wednesday: => eat_breakfast(); go_to_work(); eat_dinner()
|
||||
wednesday: -> eat_breakfast(); go_to_work(); eat_dinner()
|
||||
|
||||
# Array slice literals.
|
||||
zero_to_nine: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
@@ -140,19 +140,19 @@ sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
|
||||
aliquam erat volutpat. Ut wisi enim ad."
|
||||
|
||||
# Inheritance and calling super.
|
||||
Animal: =>
|
||||
Animal::move: meters =>
|
||||
Animal: ->
|
||||
Animal::move: (meters) ->
|
||||
alert(this.name + " moved " + meters + "m.")
|
||||
|
||||
Snake: name => this.name: name
|
||||
Snake: (name) -> this.name: name
|
||||
Snake extends Animal
|
||||
Snake::move: =>
|
||||
Snake::move: ->
|
||||
alert('Slithering...')
|
||||
super(5)
|
||||
|
||||
Horse: name => this.name: name
|
||||
Horse: (name) -> this.name: name
|
||||
Horse extends Animal
|
||||
Horse::move: =>
|
||||
Horse::move: ->
|
||||
alert('Galloping...')
|
||||
super(45)
|
||||
|
||||
|
||||
4
examples/computer_science/README
Normal file
4
examples/computer_science/README
Normal file
@@ -0,0 +1,4 @@
|
||||
Ported from Nicholas Zakas' collection of computer science fundamentals, written
|
||||
in JavaScript. Originals available here:
|
||||
|
||||
http://github.com/nzakas/computer-science-in-javascript
|
||||
25
examples/computer_science/binary_search.coffee
Normal file
25
examples/computer_science/binary_search.coffee
Normal file
@@ -0,0 +1,25 @@
|
||||
# Uses a binary search algorithm to locate a value in the specified array.
|
||||
binary_search: (items, value) ->
|
||||
|
||||
start: 0
|
||||
stop: items.length - 1
|
||||
pivot: Math.floor((start + stop) / 2)
|
||||
|
||||
while items[pivot] isnt value and start < stop
|
||||
|
||||
# Adjust the search area.
|
||||
stop: pivot - 1 if value < items[pivot]
|
||||
start: pivot + 1 if value > items[pivot]
|
||||
|
||||
# Recalculate the pivot.
|
||||
pivot: Math.floor((stop + start) / 2)
|
||||
|
||||
# Make sure we've found the correct value.
|
||||
if items[pivot] is value then pivot else -1
|
||||
|
||||
|
||||
# Test the function.
|
||||
puts(2 is binary_search([10, 20, 30, 40, 50], 30))
|
||||
puts(4 is binary_search([-97, 35, 67, 88, 1200], 1200))
|
||||
puts(0 is binary_search([0, 45, 70], 0))
|
||||
puts(-1 is binary_search([0, 45, 70], 10))
|
||||
11
examples/computer_science/bubble_sort.coffee
Normal file
11
examples/computer_science/bubble_sort.coffee
Normal file
@@ -0,0 +1,11 @@
|
||||
# A bubble sort implementation, sorting the given array in-place.
|
||||
bubble_sort: (list) ->
|
||||
for i in [0...list.length]
|
||||
for j in [0...list.length - i]
|
||||
[list[j], list[j+1]]: [list[j+1], list[j]] if list[j] > list[j+1]
|
||||
list
|
||||
|
||||
|
||||
# Test the function.
|
||||
puts(bubble_sort([3, 2, 1]).join(' ') is '1 2 3')
|
||||
puts(bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9')
|
||||
106
examples/computer_science/linked_list.coffee
Normal file
106
examples/computer_science/linked_list.coffee
Normal file
@@ -0,0 +1,106 @@
|
||||
# "Classic" linked list implementation that doesn't keep track of its size.
|
||||
LinkedList: ->
|
||||
this._head: null # Pointer to the first item in the list.
|
||||
|
||||
|
||||
# Appends some data to the end of the list. This method traverses the existing
|
||||
# list and places the value at the end in a new node.
|
||||
LinkedList::add: (data) ->
|
||||
|
||||
# Create a new node object to wrap the data.
|
||||
node: {data: data, next: null}
|
||||
|
||||
current: this._head ||= node
|
||||
|
||||
if this._head isnt node
|
||||
current: current.next while current.next
|
||||
current.next: node
|
||||
|
||||
this
|
||||
|
||||
|
||||
# Retrieves the data at the given position in the list.
|
||||
LinkedList::item: (index) ->
|
||||
|
||||
# Check for out-of-bounds values.
|
||||
return null if index < 0
|
||||
|
||||
current: this._head or null
|
||||
i: -1
|
||||
|
||||
# Advance through the list.
|
||||
current: current.next while current and index > (i += 1)
|
||||
|
||||
# Return null if we've reached the end.
|
||||
current and current.data
|
||||
|
||||
|
||||
# Remove the item from the given location in the list.
|
||||
LinkedList::remove: (index) ->
|
||||
|
||||
# Check for out-of-bounds values.
|
||||
return null if index < 0
|
||||
|
||||
current: this._head or null
|
||||
i: -1
|
||||
|
||||
# Special case: removing the first item.
|
||||
if index is 0
|
||||
this._head: current.next
|
||||
else
|
||||
|
||||
# Find the right location.
|
||||
[previous, current]: [current, current.next] while index > (i += 1)
|
||||
|
||||
# Skip over the item to remove.
|
||||
previous.next: current.next
|
||||
|
||||
# Return the value.
|
||||
current and current.data
|
||||
|
||||
|
||||
# Calculate the number of items in the list.
|
||||
LinkedList::size: ->
|
||||
current: this._head
|
||||
count: 0
|
||||
|
||||
while current
|
||||
count += 1
|
||||
current: current.next
|
||||
|
||||
count
|
||||
|
||||
|
||||
# Convert the list into an array.
|
||||
LinkedList::toArray: ->
|
||||
result: []
|
||||
current: this._head
|
||||
|
||||
while current
|
||||
result.push(current.data)
|
||||
current: current.next
|
||||
|
||||
result
|
||||
|
||||
|
||||
# The string representation of the linked list.
|
||||
LinkedList::toString: -> this.toArray().toString()
|
||||
|
||||
|
||||
# Tests.
|
||||
list: new LinkedList()
|
||||
|
||||
list.add("Hi")
|
||||
puts(list.size() is 1)
|
||||
puts(list.item(0) is "Hi")
|
||||
puts(list.item(1) is null)
|
||||
|
||||
list: new LinkedList()
|
||||
list.add("zero").add("one").add("two")
|
||||
puts(list.size() is 3)
|
||||
puts(list.item(2) is "two")
|
||||
puts(list.remove(1) is "one")
|
||||
puts(list.item(0) is "zero")
|
||||
puts(list.item(1) is "two")
|
||||
puts(list.size() is 2)
|
||||
puts(list.item(-10) is null)
|
||||
36
examples/computer_science/luhn_algorithm.coffee
Normal file
36
examples/computer_science/luhn_algorithm.coffee
Normal file
@@ -0,0 +1,36 @@
|
||||
# Use the Luhn algorithm to validate a numeric identifier, such as credit card
|
||||
# numbers, national insurance numbers, etc.
|
||||
# See: http://en.wikipedia.org/wiki/Luhn_algorithm
|
||||
|
||||
is_valid_identifier: (identifier) ->
|
||||
|
||||
sum: 0
|
||||
alt: false
|
||||
|
||||
for i in [(identifier.length - 1)..0]
|
||||
|
||||
# Get the next digit.
|
||||
num: parseInt(identifier.charAt(i), 10)
|
||||
|
||||
# If it's not a valid number, abort.
|
||||
return false if isNaN(num)
|
||||
|
||||
# If it's an alternate number...
|
||||
if alt
|
||||
num *= 2
|
||||
num: (num % 10) + 1 if num > 9
|
||||
|
||||
# Flip the alternate bit.
|
||||
alt: !alt
|
||||
|
||||
# Add to the rest of the sum.
|
||||
sum += num
|
||||
|
||||
# Determine if it's valid.
|
||||
sum % 10 is 0
|
||||
|
||||
|
||||
# Tests.
|
||||
puts(is_valid_identifier("49927398716") is true)
|
||||
puts(is_valid_identifier("4408041234567893") is true)
|
||||
puts(is_valid_identifier("4408041234567890") is false)
|
||||
19
examples/computer_science/merge_sort.coffee
Normal file
19
examples/computer_science/merge_sort.coffee
Normal file
@@ -0,0 +1,19 @@
|
||||
# Sorts an array in ascending natural order using merge sort.
|
||||
merge_sort: (list) ->
|
||||
|
||||
return list if list.length is 1
|
||||
|
||||
result: []
|
||||
pivot: Math.floor(list.length / 2)
|
||||
left: merge_sort(list.slice(0, pivot))
|
||||
right: merge_sort(list.slice(pivot))
|
||||
|
||||
while left.length and right.length
|
||||
result.push(if left[0] < right[0] then left.shift() else right.shift())
|
||||
|
||||
result.concat(left).concat(right)
|
||||
|
||||
|
||||
# Test the function.
|
||||
puts(merge_sort([3, 2, 1]).join(' ') is '1 2 3')
|
||||
puts(merge_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9')
|
||||
23
examples/computer_science/selection_sort.coffee
Normal file
23
examples/computer_science/selection_sort.coffee
Normal file
@@ -0,0 +1,23 @@
|
||||
# An in-place selection sort.
|
||||
selection_sort: (list) ->
|
||||
len: list.length
|
||||
|
||||
# For each item in the list.
|
||||
for i in [0...len]
|
||||
|
||||
# Set the minimum to this position.
|
||||
min: i
|
||||
|
||||
# Check the rest of the array to see if anything is smaller.
|
||||
(min: j if list[j] < list[min]) for j in [(i+1)...len]
|
||||
|
||||
# Swap if a smaller item has been found.
|
||||
[list[i], list[min]]: [list[min], list[i]] if i isnt min
|
||||
|
||||
# The list is now sorted.
|
||||
list
|
||||
|
||||
|
||||
# Test the function.
|
||||
puts(selection_sort([3, 2, 1]).join(' ') is '1 2 3')
|
||||
puts(selection_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9')
|
||||
@@ -1,72 +0,0 @@
|
||||
# Document Model
|
||||
dc.model.Document: dc.Model.extend({
|
||||
|
||||
constructor: attributes => this.base(attributes)
|
||||
|
||||
# For display, show either the highlighted search results, or the summary,
|
||||
# if no highlights are available.
|
||||
# The import process will take care of this in the future, but the inline
|
||||
# version of the summary has all runs of whitespace squeezed out.
|
||||
displaySummary: =>
|
||||
text: this.get('highlight') or this.get('summary') or ''
|
||||
text and text.replace(/\s+/g, ' ')
|
||||
|
||||
# Return a list of the document's metadata. Think about caching this on the
|
||||
# document by binding to Metadata, instead of on-the-fly.
|
||||
metadata: =>
|
||||
docId: this.id
|
||||
_.select(Metadata.models(), (meta =>
|
||||
_.any(meta.get('instances'), instance =>
|
||||
instance.document_id is docId)))
|
||||
|
||||
bookmark: pageNumber =>
|
||||
bookmark: new dc.model.Bookmark({title: this.get('title'), page_number: pageNumber, document_id: this.id})
|
||||
Bookmarks.create(bookmark)
|
||||
|
||||
# Inspect.
|
||||
toString: => 'Document ' + this.id + ' "' + this.get('title') + '"'
|
||||
|
||||
})
|
||||
|
||||
# Document Set
|
||||
dc.model.DocumentSet: dc.model.RESTfulSet.extend({
|
||||
|
||||
resource: 'documents'
|
||||
|
||||
SELECTION_CHANGED: 'documents:selection_changed'
|
||||
|
||||
constructor: options =>
|
||||
this.base(options)
|
||||
_.bindAll(this, 'downloadSelectedViewers', 'downloadSelectedPDF', 'downloadSelectedFullText')
|
||||
|
||||
selected: => _.select(this.models(), m => m.get('selected'))
|
||||
|
||||
selectedIds: => _.pluck(this.selected(), 'id')
|
||||
|
||||
countSelected: => this.selected().length
|
||||
|
||||
downloadSelectedViewers: =>
|
||||
dc.app.download('/download/' + this.selectedIds().join('/') + '/document_viewer.zip')
|
||||
|
||||
downloadSelectedPDF: =>
|
||||
if this.countSelected() <= 1 then return window.open(this.selected()[0].get('pdf_url'))
|
||||
dc.app.download('/download/' + this.selectedIds().join('/') + '/document_pdfs.zip')
|
||||
|
||||
downloadSelectedFullText: =>
|
||||
if this.countSelected() <= 1 then return window.open(this.selected()[0].get('full_text_url'))
|
||||
dc.app.download('/download/' + this.selectedIds().join('/') + '/document_text.zip')
|
||||
|
||||
# We override "_onModelEvent" to fire selection changed events when documents
|
||||
# change their selected state.
|
||||
_onModelEvent: e, model =>
|
||||
this.base(e, model)
|
||||
fire: e == dc.Model.CHANGED and model.hasChanged('selected')
|
||||
if fire then _.defer(_(this.fire).bind(this, this.SELECTION_CHANGED, this))
|
||||
|
||||
})
|
||||
|
||||
# The main set of Documents, used by the search tab.
|
||||
window.Documents: new dc.model.DocumentSet()
|
||||
|
||||
# The set of documents that is used to look at a particular label.
|
||||
dc.app.LabeledDocuments: new dc.model.DocumentSet()
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
# ['toast', 'cheese', 'wine'].each { |food| print food.capitalize }
|
||||
|
||||
['toast', 'wine', 'cheese'].each(food => print(food.capitalize()))
|
||||
['toast', 'wine', 'cheese'].each (food) -> print(food.capitalize())
|
||||
|
||||
|
||||
|
||||
@@ -14,10 +14,43 @@
|
||||
# end
|
||||
|
||||
LotteryTicket: {
|
||||
get_picks: => this.picks
|
||||
set_picks: nums => this.picks: nums
|
||||
get_purchase: => this.purchase
|
||||
set_purchase: amount => this.purchase: amount
|
||||
get_picks: -> this.picks
|
||||
set_picks: (nums) -> this.picks: nums
|
||||
get_purchase: -> this.purchase
|
||||
set_purchase: (amount) -> this.purchase: amount
|
||||
}
|
||||
|
||||
|
||||
|
||||
# class << LotteryDraw
|
||||
# def play
|
||||
# result = LotteryTicket.new_random
|
||||
# winners = {}
|
||||
# @@tickets.each do |buyer, ticket_list|
|
||||
# ticket_list.each do |ticket|
|
||||
# score = ticket.score( result )
|
||||
# next if score.zero?
|
||||
# winners[buyer] ||= []
|
||||
# winners[buyer] << [ ticket, score ]
|
||||
# end
|
||||
# end
|
||||
# @@tickets.clear
|
||||
# winners
|
||||
# end
|
||||
# end
|
||||
|
||||
LotteryDraw: {
|
||||
play: ->
|
||||
result: LotteryTicket.new_random()
|
||||
winners: {}
|
||||
this.tickets.each (buyer, ticket_list) ->
|
||||
ticket_list.each (ticket) ->
|
||||
score: ticket.score(result)
|
||||
return if score is 0
|
||||
winners[buyer] ||= []
|
||||
winners[buyer].push([ticket, score])
|
||||
this.tickets: {}
|
||||
winners
|
||||
}
|
||||
|
||||
|
||||
@@ -32,8 +65,8 @@ LotteryTicket: {
|
||||
# end
|
||||
|
||||
WishScanner: {
|
||||
scan_for_a_wish: =>
|
||||
wish: this.read().detect(thought => thought.index('wish: ') is 0)
|
||||
scan_for_a_wish: ->
|
||||
wish: this.read().detect((thought) -> thought.index('wish: ') is 0)
|
||||
wish.replace('wish: ', '')
|
||||
}
|
||||
|
||||
@@ -78,7 +111,7 @@ WishScanner: {
|
||||
Creature : {
|
||||
|
||||
# This method applies a hit taken during a fight.
|
||||
hit: damage =>
|
||||
hit: (damage) ->
|
||||
p_up: Math.rand(this.charisma)
|
||||
if p_up % 9 is 7
|
||||
this.life += p_up / 4
|
||||
@@ -87,7 +120,7 @@ Creature : {
|
||||
if this.life <= 0 then puts("[" + this.name + " has died.]")
|
||||
|
||||
# This method takes one turn in a fight.
|
||||
fight: enemy, weapon =>
|
||||
fight: (enemy, weapon) ->
|
||||
if this.life <= 0 then return puts("[" + this.name + "is too dead to fight!]")
|
||||
|
||||
# Attack the opponent.
|
||||
@@ -123,12 +156,12 @@ Creature : {
|
||||
# Get evil idea and swap in code words
|
||||
print("Enter your new idea: ")
|
||||
idea: gets()
|
||||
code_words.each(real, code => idea.replace(real, code))
|
||||
code_words.each((real, code) -> idea.replace(real, code))
|
||||
|
||||
# Save the jibberish to a new file
|
||||
print("File encoded. Please enter a name for this idea: ")
|
||||
idea_name: gets().strip()
|
||||
File.open("idea-" + idea_name + '.txt', 'w', file => file.write(idea))
|
||||
File.open("idea-" + idea_name + '.txt', 'w', (file) -> file.write(idea))
|
||||
|
||||
|
||||
|
||||
@@ -144,7 +177,7 @@ File.open("idea-" + idea_name + '.txt', 'w', file => file.write(idea))
|
||||
# end
|
||||
# end
|
||||
|
||||
wipe_mutterings_from: sentence =>
|
||||
wipe_mutterings_from: (sentence) ->
|
||||
throw new Error("cannot wipe mutterings") unless sentence.indexOf
|
||||
while sentence.indexOf('(') >= 0
|
||||
open: sentence.indexOf('(') - 1
|
||||
|
||||
@@ -8,7 +8,7 @@ print("Odelay!") for i in [1..5]
|
||||
# add = (x, y): x + y.
|
||||
# add(2, 4) string print
|
||||
|
||||
add: x, y => x + y
|
||||
add: (x, y) -> x + y
|
||||
print(add(2, 4))
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ print({language: 'Potion', pointless: true}['language'])
|
||||
# minus = (x, y): x - y.
|
||||
# minus (y=10, x=6)
|
||||
|
||||
minus: x, y => x - y
|
||||
minus: (x, y) -> x - y
|
||||
minus(6, 10)
|
||||
|
||||
|
||||
@@ -53,8 +53,8 @@ for key, val of {dog: 'canine', cat: 'feline', fox: 'vulpine'}
|
||||
# Person print = ():
|
||||
# ('My name is ', /name, '.') join print.
|
||||
|
||||
Person: =>
|
||||
Person::print: =>
|
||||
Person: ->
|
||||
Person::print: ->
|
||||
print('My name is ' + this.name + '.')
|
||||
|
||||
|
||||
@@ -71,9 +71,9 @@ print(p.name)
|
||||
#
|
||||
# Policeman ('Constable') print
|
||||
|
||||
Policeman: rank => this.rank: rank
|
||||
Policeman: (rank) -> this.rank: rank
|
||||
Policeman extends Person
|
||||
Policeman::print: =>
|
||||
Policeman::print: ->
|
||||
print('My name is ' + this.name + " and I'm a " + this.rank + '.')
|
||||
|
||||
print(new Policeman('Constable'))
|
||||
@@ -115,13 +115,13 @@ table: {
|
||||
# String length = (): 10.
|
||||
|
||||
# this foul business...
|
||||
String::length: => 10
|
||||
String::length: -> 10
|
||||
|
||||
|
||||
# block = :
|
||||
# 'potion' print.
|
||||
|
||||
block: =>
|
||||
block: ->
|
||||
print('potion')
|
||||
|
||||
|
||||
@@ -178,7 +178,7 @@ if (3).gender?
|
||||
# HomePage get = (url):
|
||||
# session = url query ? at ('session').
|
||||
|
||||
HomePage::get: url =>
|
||||
HomePage::get: (url) ->
|
||||
session: url.query.session if url.query?
|
||||
|
||||
|
||||
@@ -187,7 +187,7 @@ HomePage::get: url =>
|
||||
# b /left = BTree ()
|
||||
# b /right = BTree ()
|
||||
|
||||
BTree: =>
|
||||
BTree: ->
|
||||
b: new BTree()
|
||||
b.left: new BTree()
|
||||
b.right: new BTree()
|
||||
@@ -199,7 +199,7 @@ b.right: new BTree()
|
||||
# if (b ? /left):
|
||||
# 'left path found!' print.
|
||||
|
||||
BTree: =>
|
||||
BTree: ->
|
||||
b: new BTree()
|
||||
|
||||
print('left path found!') if b.left?
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
# Identifiers run together:
|
||||
# a b c
|
||||
|
||||
# Trailing comma in array:
|
||||
# array: [1, 2, 3, 4, 5,]
|
||||
|
||||
# Unterminated object literal:
|
||||
# obj: { one: 1, two: 2
|
||||
|
||||
# Numbers run together:
|
||||
# 101 202
|
||||
|
||||
# Strings run together:
|
||||
# str: "broken" "words"
|
||||
|
||||
# Forgot to terminate a function:
|
||||
# obj: {
|
||||
# first: a => a[0].
|
||||
# last: a => a[a.length-1]
|
||||
# }
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
# Underscore.coffee
|
||||
# (c) 2009 Jeremy Ashkenas, DocumentCloud Inc.
|
||||
# (c) 2010 Jeremy Ashkenas, DocumentCloud Inc.
|
||||
# Underscore is freely distributable under the terms of the MIT license.
|
||||
# Portions of Underscore are inspired by or borrowed from Prototype.js,
|
||||
# Oliver Steele's Functional, and John Resig's Micro-Templating.
|
||||
@@ -21,7 +21,7 @@
|
||||
# If Underscore is called as a function, it returns a wrapped object that
|
||||
# can be used OO-style. This wrapper holds altered versions of all the
|
||||
# underscore functions. Wrapped objects may be chained.
|
||||
wrapper: obj =>
|
||||
wrapper: (obj) ->
|
||||
this._wrapped: obj
|
||||
this
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
|
||||
|
||||
# Create a safe reference to the Underscore object forreference below.
|
||||
_: root._: obj => new wrapper(obj)
|
||||
_: root._: (obj) -> new wrapper(obj)
|
||||
|
||||
|
||||
# Export the Underscore object for CommonJS.
|
||||
@@ -47,18 +47,18 @@
|
||||
|
||||
|
||||
# Current version.
|
||||
_.VERSION: '0.5.5'
|
||||
_.VERSION: '0.5.8'
|
||||
|
||||
|
||||
# ------------------------ Collection Functions: ---------------------------
|
||||
|
||||
# The cornerstone, an each implementation.
|
||||
# Handles objects implementing forEach, arrays, and raw objects.
|
||||
_.each: obj, iterator, context =>
|
||||
_.each: (obj, iterator, context) ->
|
||||
index: 0
|
||||
try
|
||||
return obj.forEach(iterator, context) if obj.forEach
|
||||
if _.isArray(obj) or _.isArguments(obj)
|
||||
if _.isNumber(obj.length)
|
||||
return iterator.call(context, obj[i], i, obj) for i in [0...obj.length]
|
||||
iterator.call(context, val, key, obj) for key, val of obj
|
||||
catch e
|
||||
@@ -68,36 +68,36 @@
|
||||
|
||||
# Return the results of applying the iterator to each element. Use JavaScript
|
||||
# 1.6's version of map, if possible.
|
||||
_.map: obj, iterator, context =>
|
||||
_.map: (obj, iterator, context) ->
|
||||
return obj.map(iterator, context) if (obj and _.isFunction(obj.map))
|
||||
results: []
|
||||
_.each(obj) value, index, list =>
|
||||
_.each obj, (value, index, list) ->
|
||||
results.push(iterator.call(context, value, index, list))
|
||||
results
|
||||
|
||||
|
||||
# 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 =>
|
||||
_.reduce: (obj, memo, iterator, context) ->
|
||||
return obj.reduce(_.bind(iterator, context), memo) if (obj and _.isFunction(obj.reduce))
|
||||
_.each(obj) value, index, list =>
|
||||
_.each obj, (value, index, list) ->
|
||||
memo: iterator.call(context, memo, value, index, list)
|
||||
memo
|
||||
|
||||
|
||||
# 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 =>
|
||||
_.reduceRight: (obj, memo, iterator, context) ->
|
||||
return obj.reduceRight(_.bind(iterator, context), memo) if (obj and _.isFunction(obj.reduceRight))
|
||||
_.each(_.clone(_.toArray(obj)).reverse()) value, index =>
|
||||
_.each _.clone(_.toArray(obj)).reverse(), (value, index) ->
|
||||
memo: iterator.call(context, memo, value, index, obj)
|
||||
memo
|
||||
|
||||
|
||||
# Return the first value which passes a truth test.
|
||||
_.detect: obj, iterator, context =>
|
||||
_.detect: (obj, iterator, context) ->
|
||||
result: null
|
||||
_.each(obj) value, index, list =>
|
||||
_.each obj, (value, index, list) ->
|
||||
if iterator.call(context, value, index, list)
|
||||
result: value
|
||||
_.breakLoop()
|
||||
@@ -106,97 +106,97 @@
|
||||
|
||||
# Return all the elements that pass a truth test. Use JavaScript 1.6's
|
||||
# filter(), if it exists.
|
||||
_.select: obj, iterator, context =>
|
||||
_.select: (obj, iterator, context) ->
|
||||
if obj and _.isFunction(obj.filter) then return obj.filter(iterator, context)
|
||||
results: []
|
||||
_.each(obj) value, index, list =>
|
||||
_.each obj, (value, index, list) ->
|
||||
results.push(value) if iterator.call(context, value, index, list)
|
||||
results
|
||||
|
||||
|
||||
# Return all the elements for which a truth test fails.
|
||||
_.reject: obj, iterator, context =>
|
||||
_.reject: (obj, iterator, context) ->
|
||||
results: []
|
||||
_.each(obj) value, index, list =>
|
||||
_.each obj, (value, index, list) ->
|
||||
results.push(value) if not iterator.call(context, value, index, list)
|
||||
results
|
||||
|
||||
|
||||
# Determine whether all of the elements match a truth test. Delegate to
|
||||
# JavaScript 1.6's every(), if it is present.
|
||||
_.all: obj, iterator, context =>
|
||||
_.all: (obj, iterator, context) ->
|
||||
iterator ||= _.identity
|
||||
return obj.every(iterator, context) if obj and _.isFunction(obj.every)
|
||||
result: true
|
||||
_.each(obj) value, index, list =>
|
||||
_.each obj, (value, index, list) ->
|
||||
_.breakLoop() unless (result: result and iterator.call(context, value, index, list))
|
||||
result
|
||||
|
||||
|
||||
# Determine if at least one element in the object matches a truth test. Use
|
||||
# JavaScript 1.6's some(), if it exists.
|
||||
_.any: obj, iterator, context =>
|
||||
_.any: (obj, iterator, context) ->
|
||||
iterator ||= _.identity
|
||||
return obj.some(iterator, context) if obj and _.isFunction(obj.some)
|
||||
result: false
|
||||
_.each(obj) value, index, list =>
|
||||
_.each obj, (value, index, list) ->
|
||||
_.breakLoop() if (result: iterator.call(context, value, index, list))
|
||||
result
|
||||
|
||||
|
||||
# Determine if a given value is included in the array or object,
|
||||
# based on '==='.
|
||||
_.include: obj, target =>
|
||||
return _.indexOf(obj, target) isnt -1 if _.isArray(obj)
|
||||
_.include: (obj, target) ->
|
||||
return _.indexOf(obj, target) isnt -1 if obj and _.isFunction(obj.indexOf)
|
||||
for key, val of obj
|
||||
return true if val is target
|
||||
false
|
||||
|
||||
|
||||
# Invoke a method with arguments on every item in a collection.
|
||||
_.invoke: obj, method =>
|
||||
_.invoke: (obj, method) ->
|
||||
args: _.rest(arguments, 2)
|
||||
(if method then val[method] else val).apply(val, args) for val in obj
|
||||
|
||||
|
||||
# Convenience version of a common use case of map: fetching a property.
|
||||
_.pluck: obj, key =>
|
||||
_.map(obj, (val => val[key]))
|
||||
_.pluck: (obj, key) ->
|
||||
_.map(obj, ((val) -> val[key]))
|
||||
|
||||
|
||||
# Return the maximum item or (item-based computation).
|
||||
_.max: obj, iterator, context =>
|
||||
_.max: (obj, iterator, context) ->
|
||||
return Math.max.apply(Math, obj) if not iterator and _.isArray(obj)
|
||||
result: {computed: -Infinity}
|
||||
_.each(obj) value, index, list =>
|
||||
_.each obj, (value, index, list) ->
|
||||
computed: if iterator then iterator.call(context, value, index, list) else value
|
||||
computed >= result.computed and (result: {value: value, computed: computed})
|
||||
result.value
|
||||
|
||||
|
||||
# Return the minimum element (or element-based computation).
|
||||
_.min: obj, iterator, context =>
|
||||
_.min: (obj, iterator, context) ->
|
||||
return Math.min.apply(Math, obj) if not iterator and _.isArray(obj)
|
||||
result: {computed: Infinity}
|
||||
_.each(obj) value, index, list =>
|
||||
_.each obj, (value, index, list) ->
|
||||
computed: if iterator then iterator.call(context, value, index, list) else value
|
||||
computed < result.computed and (result: {value: value, computed: computed})
|
||||
result.value
|
||||
|
||||
|
||||
# Sort the object's values by a criteria produced by an iterator.
|
||||
_.sortBy: obj, iterator, context =>
|
||||
_.pluck(((_.map(obj) value, index, list =>
|
||||
_.sortBy: (obj, iterator, context) ->
|
||||
_.pluck(((_.map obj, (value, index, list) ->
|
||||
{value: value, criteria: iterator.call(context, value, index, list)}
|
||||
).sort() left, right =>
|
||||
).sort((left, right) ->
|
||||
a: left.criteria; b: right.criteria
|
||||
if a < b then -1 else if a > b then 1 else 0
|
||||
), 'value')
|
||||
)), 'value')
|
||||
|
||||
|
||||
# Use a comparator function to figure out at what index an object should
|
||||
# be inserted so as to maintain order. Uses binary search.
|
||||
_.sortedIndex: array, obj, iterator =>
|
||||
_.sortedIndex: (array, obj, iterator) ->
|
||||
iterator ||= _.identity
|
||||
low: 0; high: array.length
|
||||
while low < high
|
||||
@@ -206,7 +206,7 @@
|
||||
|
||||
|
||||
# Convert anything iterable into a real, live array.
|
||||
_.toArray: iterable =>
|
||||
_.toArray: (iterable) ->
|
||||
return [] if (!iterable)
|
||||
return iterable.toArray() if (iterable.toArray)
|
||||
return iterable if (_.isArray(iterable))
|
||||
@@ -215,7 +215,7 @@
|
||||
|
||||
|
||||
# Return the number of elements in an object.
|
||||
_.size: obj => _.toArray(obj).length
|
||||
_.size: (obj) -> _.toArray(obj).length
|
||||
|
||||
|
||||
# -------------------------- Array Functions: ------------------------------
|
||||
@@ -223,7 +223,7 @@
|
||||
# Get the first element of an array. Passing "n" will return the first N
|
||||
# values in the array. Aliased as "head". The "guard" check allows it to work
|
||||
# with _.map.
|
||||
_.first: array, n, guard =>
|
||||
_.first: (array, n, guard) ->
|
||||
if n and not guard then slice.call(array, 0, n) else array[0]
|
||||
|
||||
|
||||
@@ -231,35 +231,35 @@
|
||||
# Especially useful on the arguments object. Passing an "index" will return
|
||||
# the rest of the values in the array from that index onward. The "guard"
|
||||
# check allows it to work with _.map.
|
||||
_.rest: array, index, guard =>
|
||||
_.rest: (array, index, guard) ->
|
||||
slice.call(array, if _.isUndefined(index) or guard then 1 else index)
|
||||
|
||||
|
||||
# Get the last element of an array.
|
||||
_.last: array => array[array.length - 1]
|
||||
_.last: (array) -> array[array.length - 1]
|
||||
|
||||
|
||||
# Trim out all falsy values from an array.
|
||||
_.compact: array => array[i] for i in [0...array.length] when array[i]
|
||||
_.compact: (array) -> item for item in array when item
|
||||
|
||||
|
||||
# Return a completely flattened version of an array.
|
||||
_.flatten: array =>
|
||||
_.reduce(array, []) memo, value =>
|
||||
_.flatten: (array) ->
|
||||
_.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).
|
||||
_.without: array =>
|
||||
_.without: (array) ->
|
||||
values: _.rest(arguments)
|
||||
val for val in _.toArray(array) when not _.include(values, val)
|
||||
|
||||
|
||||
# Produce a duplicate-free version of the array. If the array has already
|
||||
# been sorted, you have the option of using a faster algorithm.
|
||||
_.uniq: array, isSorted =>
|
||||
_.uniq: (array, isSorted) ->
|
||||
memo: []
|
||||
for el, i in _.toArray(array)
|
||||
memo.push(el) if i is 0 || (if isSorted is true then _.last(memo) isnt el else not _.include(memo, el))
|
||||
@@ -268,28 +268,27 @@
|
||||
|
||||
# Produce an array that contains every item shared between all the
|
||||
# passed-in arrays.
|
||||
_.intersect: array =>
|
||||
_.intersect: (array) ->
|
||||
rest: _.rest(arguments)
|
||||
_.select(_.uniq(array)) item =>
|
||||
_.all(rest) other =>
|
||||
_.select _.uniq(array), (item) ->
|
||||
_.all rest, (other) ->
|
||||
_.indexOf(other, item) >= 0
|
||||
|
||||
|
||||
# Zip together multiple lists into a single array -- elements that share
|
||||
# an index go together.
|
||||
_.zip: =>
|
||||
args: _.toArray(arguments)
|
||||
length: _.max(_.pluck(args, 'length'))
|
||||
_.zip: ->
|
||||
length: _.max(_.pluck(arguments, 'length'))
|
||||
results: new Array(length)
|
||||
for i in [0...length]
|
||||
results[i]: _.pluck(args, String(i))
|
||||
results[i]: _.pluck(arguments, String(i))
|
||||
results
|
||||
|
||||
|
||||
# If the browser doesn't supply us with indexOf (I'm looking at you, MSIE),
|
||||
# we need this function. Return the position of the first occurence of an
|
||||
# item in an array, or -1 if the item is not included in the array.
|
||||
_.indexOf: array, item =>
|
||||
_.indexOf: (array, item) ->
|
||||
return array.indexOf(item) if array.indexOf
|
||||
i: 0; l: array.length
|
||||
while l - i
|
||||
@@ -299,7 +298,7 @@
|
||||
|
||||
# Provide JavaScript 1.6's lastIndexOf, delegating to the native function,
|
||||
# if possible.
|
||||
_.lastIndexOf: array, item =>
|
||||
_.lastIndexOf: (array, item) ->
|
||||
return array.lastIndexOf(item) if array.lastIndexOf
|
||||
i: array.length
|
||||
while i
|
||||
@@ -310,11 +309,11 @@
|
||||
# Generate an integer Array containing an arithmetic progression. A port of
|
||||
# the native Python range() function. See:
|
||||
# http://docs.python.org/library/functions.html#range
|
||||
_.range: start, stop, step =>
|
||||
a: _.toArray(arguments)
|
||||
_.range: (start, stop, step) ->
|
||||
a: arguments
|
||||
solo: a.length <= 1
|
||||
i: start: if solo then 0 else a[0];
|
||||
stop: if solo then a[0] else a[1];
|
||||
i: start: if solo then 0 else a[0]
|
||||
stop: if solo then a[0] else a[1]
|
||||
step: a[2] or 1
|
||||
len: Math.ceil((stop - start) / step)
|
||||
return [] if len <= 0
|
||||
@@ -331,45 +330,45 @@
|
||||
|
||||
# Create a function bound to a given object (assigning 'this', and arguments,
|
||||
# optionally). Binding with arguments is also known as 'curry'.
|
||||
_.bind: func, obj =>
|
||||
_.bind: (func, obj) ->
|
||||
args: _.rest(arguments, 2)
|
||||
=> func.apply(obj or root, args.concat(_.toArray(arguments)))
|
||||
-> func.apply(obj or root, args.concat(arguments))
|
||||
|
||||
|
||||
# Bind all of an object's methods to that object. Useful for ensuring that
|
||||
# all callbacks defined on an object belong to it.
|
||||
_.bindAll: obj =>
|
||||
_.bindAll: (obj) ->
|
||||
funcs: if arguments.length > 1 then _.rest(arguments) else _.functions(obj)
|
||||
_.each(funcs, (f => obj[f]: _.bind(obj[f], obj)))
|
||||
_.each(funcs, (f) -> obj[f]: _.bind(obj[f], obj))
|
||||
obj
|
||||
|
||||
|
||||
# Delays a function for the given number of milliseconds, and then calls
|
||||
# it with the arguments supplied.
|
||||
_.delay: func, wait =>
|
||||
_.delay: (func, wait) ->
|
||||
args: _.rest(arguments, 2)
|
||||
setTimeout((=> func.apply(func, args)), wait)
|
||||
setTimeout((-> func.apply(func, args)), wait)
|
||||
|
||||
|
||||
# Defers a function, scheduling it to run after the current call stack has
|
||||
# cleared.
|
||||
_.defer: func =>
|
||||
_.defer: (func) ->
|
||||
_.delay.apply(_, [func, 1].concat(_.rest(arguments)))
|
||||
|
||||
|
||||
# Returns the first function passed as an argument to the second,
|
||||
# allowing you to adjust arguments, run code before and after, and
|
||||
# conditionally execute the original function.
|
||||
_.wrap: func, wrapper =>
|
||||
=> wrapper.apply(wrapper, [func].concat(_.toArray(arguments)))
|
||||
_.wrap: (func, wrapper) ->
|
||||
-> wrapper.apply(wrapper, [func].concat(arguments))
|
||||
|
||||
|
||||
# Returns a function that is the composition of a list of functions, each
|
||||
# consuming the return value of the function that follows.
|
||||
_.compose: =>
|
||||
funcs: _.toArray(arguments)
|
||||
=>
|
||||
args: _.toArray(arguments)
|
||||
_.compose: ->
|
||||
funcs: arguments
|
||||
->
|
||||
args: arguments
|
||||
for i in [(funcs.length - 1)..0]
|
||||
args: [funcs[i].apply(this, args)]
|
||||
args[0]
|
||||
@@ -378,43 +377,43 @@
|
||||
# ------------------------- Object Functions: ----------------------------
|
||||
|
||||
# Retrieve the names of an object's properties.
|
||||
_.keys: obj =>
|
||||
_.keys: (obj) ->
|
||||
return _.range(0, obj.length) if _.isArray(obj)
|
||||
key for key, val of obj
|
||||
|
||||
|
||||
# Retrieve the values of an object's properties.
|
||||
_.values: obj =>
|
||||
_.values: (obj) ->
|
||||
_.map(obj, _.identity)
|
||||
|
||||
|
||||
# Return a sorted list of the function names available in Underscore.
|
||||
_.functions: obj =>
|
||||
_.select(_.keys(obj), key => _.isFunction(obj[key])).sort()
|
||||
_.functions: (obj) ->
|
||||
_.select(_.keys(obj), (key) -> _.isFunction(obj[key])).sort()
|
||||
|
||||
|
||||
# Extend a given object with all of the properties in a source object.
|
||||
_.extend: destination, source =>
|
||||
_.extend: (destination, source) ->
|
||||
for key, val of source
|
||||
destination[key]: val
|
||||
destination
|
||||
|
||||
|
||||
# Create a (shallow-cloned) duplicate of an object.
|
||||
_.clone: obj =>
|
||||
_.clone: (obj) ->
|
||||
return obj.slice(0) if _.isArray(obj)
|
||||
_.extend({}, obj)
|
||||
|
||||
|
||||
# Invokes interceptor with the obj, and then returns obj.
|
||||
# The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
|
||||
_.tap: obj, interceptor =>
|
||||
_.tap: (obj, interceptor) ->
|
||||
interceptor(obj)
|
||||
obj
|
||||
|
||||
|
||||
# Perform a deep comparison to check if two objects are equal.
|
||||
_.isEqual: a, b =>
|
||||
_.isEqual: (a, b) ->
|
||||
# Check object identity.
|
||||
return true if a is b
|
||||
# Different types?
|
||||
@@ -450,93 +449,104 @@
|
||||
|
||||
|
||||
# Is a given array or object empty?
|
||||
_.isEmpty: obj => _.keys(obj).length is 0
|
||||
_.isEmpty: (obj) -> _.keys(obj).length is 0
|
||||
|
||||
|
||||
# Is a given value a DOM element?
|
||||
_.isElement: obj => obj and obj.nodeType is 1
|
||||
_.isElement: (obj) -> obj and obj.nodeType is 1
|
||||
|
||||
|
||||
# Is a given value an array?
|
||||
_.isArray: obj => !!(obj and obj.concat and obj.unshift)
|
||||
_.isArray: (obj) -> !!(obj and obj.concat and obj.unshift)
|
||||
|
||||
|
||||
# Is a given variable an arguments object?
|
||||
_.isArguments: obj => obj and _.isNumber(obj.length) and !_.isArray(obj) and !propertyIsEnumerable.call(obj, 'length')
|
||||
_.isArguments: (obj) -> obj and _.isNumber(obj.length) and not obj.concat and
|
||||
not obj.substr and not obj.apply and not propertyIsEnumerable.call(obj, 'length')
|
||||
|
||||
|
||||
# Is the given value a function?
|
||||
_.isFunction: obj => !!(obj and obj.constructor and obj.call and obj.apply)
|
||||
_.isFunction: (obj) -> !!(obj and obj.constructor and obj.call and obj.apply)
|
||||
|
||||
|
||||
# Is the given value a string?
|
||||
_.isString: obj => !!(obj is '' or (obj and obj.charCodeAt and obj.substr))
|
||||
_.isString: (obj) -> !!(obj is '' or (obj and obj.charCodeAt and obj.substr))
|
||||
|
||||
|
||||
# Is a given value a number?
|
||||
_.isNumber: obj => toString.call(obj) is '[object Number]'
|
||||
_.isNumber: (obj) -> (obj is +obj) or toString.call(obj) is '[object Number]'
|
||||
|
||||
|
||||
# Is a given value a Date?
|
||||
_.isDate: obj => !!(obj and obj.getTimezoneOffset and obj.setUTCFullYear)
|
||||
_.isDate: (obj) -> !!(obj and obj.getTimezoneOffset and obj.setUTCFullYear)
|
||||
|
||||
|
||||
# Is the given value a regular expression?
|
||||
_.isRegExp: obj => !!(obj and obj.exec and (obj.ignoreCase or obj.ignoreCase is false))
|
||||
_.isRegExp: (obj) -> !!(obj and obj.exec and (obj.ignoreCase or obj.ignoreCase is false))
|
||||
|
||||
|
||||
# Is the given value NaN -- this one is interesting. NaN != NaN, and
|
||||
# isNaN(undefined) == true, so we make sure it's a number first.
|
||||
_.isNaN: obj => _.isNumber(obj) and window.isNaN(obj)
|
||||
_.isNaN: (obj) -> _.isNumber(obj) and window.isNaN(obj)
|
||||
|
||||
|
||||
# Is a given value equal to null?
|
||||
_.isNull: obj => obj is null
|
||||
_.isNull: (obj) -> obj is null
|
||||
|
||||
|
||||
# Is a given variable undefined?
|
||||
_.isUndefined: obj => typeof obj is 'undefined'
|
||||
_.isUndefined: (obj) -> typeof obj is 'undefined'
|
||||
|
||||
|
||||
# -------------------------- Utility Functions: --------------------------
|
||||
|
||||
# Run Underscore.js in noConflict mode, returning the '_' variable to its
|
||||
# previous owner. Returns a reference to the Underscore object.
|
||||
_.noConflict: =>
|
||||
_.noConflict: ->
|
||||
root._: previousUnderscore
|
||||
this
|
||||
|
||||
|
||||
# Keep the identity function around for default iterators.
|
||||
_.identity: value => value
|
||||
_.identity: (value) -> value
|
||||
|
||||
|
||||
# Break out of the middle of an iteration.
|
||||
_.breakLoop: => throw breaker
|
||||
_.breakLoop: -> throw breaker
|
||||
|
||||
|
||||
# Generate a unique integer id (unique within the entire client session).
|
||||
# Useful for temporary DOM ids.
|
||||
idCounter: 0
|
||||
_.uniqueId: prefix =>
|
||||
_.uniqueId: (prefix) ->
|
||||
(prefix or '') + idCounter++
|
||||
|
||||
|
||||
# By default, Underscore uses ERB-style template delimiters, change the
|
||||
# following template settings to use alternative delimiters.
|
||||
_.templateSettings: {
|
||||
start: '<%'
|
||||
end: '%>'
|
||||
interpolate: /<%=(.+?)%>/g
|
||||
}
|
||||
|
||||
|
||||
# JavaScript templating a-la ERB, pilfered from John Resig's
|
||||
# "Secrets of the JavaScript Ninja", page 83.
|
||||
_.template: str, data =>
|
||||
`var fn = new Function('obj',
|
||||
# Single-quote fix from Rick Strahl's version.
|
||||
_.template: (str, data) ->
|
||||
c: _.templateSettings
|
||||
fn: new Function 'obj',
|
||||
'var p=[],print=function(){p.push.apply(p,arguments);};' +
|
||||
'with(obj){p.push(\'' +
|
||||
str.
|
||||
replace(/[\r\t\n]/g, " ").
|
||||
split("<%").join("\t").
|
||||
replace(/((^|%>)[^\t]*)'/g, "$1\r").
|
||||
replace(/\t=(.*?)%>/g, "',$1,'").
|
||||
split("\t").join("');").
|
||||
split("%>").join("p.push('").
|
||||
split("\r").join("\\'") +
|
||||
"');}return p.join('');")`
|
||||
str.replace(/[\r\t\n]/g, " ")
|
||||
.replace(new RegExp("'(?=[^"+c.end[0]+"]*"+c.end+")","g"),"\t")
|
||||
.split("'").join("\\'")
|
||||
.split("\t").join("'")
|
||||
.replace(c.interpolate, "',$1,'")
|
||||
.split(c.start).join("');")
|
||||
.split(c.end).join("p.push('") +
|
||||
"');}return p.join('');"
|
||||
if data then fn(data) else fn
|
||||
|
||||
|
||||
@@ -553,42 +563,41 @@
|
||||
_.methods: _.functions
|
||||
|
||||
|
||||
# /*------------------------ Setup the OOP Wrapper: --------------------------*/
|
||||
# ------------------------ Setup the OOP Wrapper: --------------------------
|
||||
|
||||
# Helper function to continue chaining intermediate results.
|
||||
result: obj, chain =>
|
||||
result: (obj, chain) ->
|
||||
if chain then _(obj).chain() else obj
|
||||
|
||||
|
||||
# Add all of the Underscore functions to the wrapper object.
|
||||
_.each(_.functions(_)) name =>
|
||||
_.each _.functions(_), (name) ->
|
||||
method: _[name]
|
||||
wrapper.prototype[name]: =>
|
||||
args: _.toArray(arguments)
|
||||
unshift.call(args, this._wrapped)
|
||||
result(method.apply(_, args), this._chain)
|
||||
wrapper.prototype[name]: ->
|
||||
unshift.call(arguments, this._wrapped)
|
||||
result(method.apply(_, arguments), this._chain)
|
||||
|
||||
|
||||
# Add all mutator Array functions to the wrapper.
|
||||
_.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift']) name =>
|
||||
_.each ['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], (name) ->
|
||||
method: Array.prototype[name]
|
||||
wrapper.prototype[name]: =>
|
||||
wrapper.prototype[name]: ->
|
||||
method.apply(this._wrapped, arguments)
|
||||
result(this._wrapped, this._chain)
|
||||
|
||||
|
||||
# Add all accessor Array functions to the wrapper.
|
||||
_.each(['concat', 'join', 'slice']) name =>
|
||||
_.each ['concat', 'join', 'slice'], (name) ->
|
||||
method: Array.prototype[name]
|
||||
wrapper.prototype[name]: =>
|
||||
wrapper.prototype[name]: ->
|
||||
result(method.apply(this._wrapped, arguments), this._chain)
|
||||
|
||||
|
||||
# Start chaining a wrapped Underscore object.
|
||||
wrapper::chain: =>
|
||||
wrapper::chain: ->
|
||||
this._chain: true
|
||||
this
|
||||
|
||||
|
||||
# Extracts the result from a wrapped and chained object.
|
||||
wrapper::value: => this._wrapped
|
||||
wrapper::value: -> this._wrapped
|
||||
|
||||
12
examples/web_server.coffee
Normal file
12
examples/web_server.coffee
Normal file
@@ -0,0 +1,12 @@
|
||||
# Contributed by Jason Huggins
|
||||
|
||||
http: require 'http'
|
||||
|
||||
server: http.createServer (req, res) ->
|
||||
res.sendHeader 200, {'Content-Type': 'text/plain'}
|
||||
res.sendBody 'Hello, World!'
|
||||
res.finish()
|
||||
|
||||
server.listen 3000
|
||||
|
||||
puts "Server running at http://localhost:3000/"
|
||||
@@ -19,52 +19,31 @@
|
||||
<dict>
|
||||
<key>captures</key>
|
||||
<dict>
|
||||
<key>1</key>
|
||||
<key>1</key>
|
||||
<dict>
|
||||
<key>name</key>
|
||||
<string>entity.name.function.coffee</string>
|
||||
<string>variable.parameter.function.coffee</string>
|
||||
</dict>
|
||||
<key>2</key>
|
||||
<dict>
|
||||
<key>name</key>
|
||||
<string>keyword.operator.coffee</string>
|
||||
</dict>
|
||||
<key>3</key>
|
||||
<dict>
|
||||
<key>name</key>
|
||||
<string>variable.parameter.function.coffee</string>
|
||||
</dict>
|
||||
<key>4</key>
|
||||
<dict>
|
||||
<key>name</key>
|
||||
<string>storage.type.function.coffee</string>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>comment</key>
|
||||
<string>match stuff like: funcName: => … </string>
|
||||
<key>match</key>
|
||||
<string>([a-zA-Z0-9_?.$:*]*)\s*(=|:)\s*([\w,\s]*?)\s*(=>)</string>
|
||||
<key>name</key>
|
||||
<string>meta.function.coffee</string>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>captures</key>
|
||||
<dict>
|
||||
<key>1</key>
|
||||
<dict>
|
||||
<key>name</key>
|
||||
<string>variable.parameter.function.coffee</string>
|
||||
</dict>
|
||||
<key>2</key>
|
||||
<key>5</key>
|
||||
<dict>
|
||||
<key>name</key>
|
||||
<string>storage.type.function.coffee</string>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>comment</key>
|
||||
<string>match stuff like: a => … </string>
|
||||
<string>match stuff like: a -> … </string>
|
||||
<key>match</key>
|
||||
<string>([a-zA-Z0-9_?., $:*]*)\s*(=>)</string>
|
||||
<string>(\()([a-zA-Z0-9_?.$]*(,\s*[a-zA-Z0-9_?.$]+)*)(\))\s*((=|-)>)</string>
|
||||
<key>name</key>
|
||||
<string>meta.inline.function.coffee</string>
|
||||
</dict>
|
||||
@@ -93,6 +72,12 @@
|
||||
<key>name</key>
|
||||
<string>constant.numeric.coffee</string>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>match</key>
|
||||
<string>(@)([a-zA-Z_$]\w*)?</string>
|
||||
<key>name</key>
|
||||
<string>variable.other.readwrite.instance.coffee</string>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>name</key>
|
||||
<string>string.quoted.heredoc.coffee</string>
|
||||
@@ -230,6 +215,39 @@
|
||||
<key>name</key>
|
||||
<string>comment.line.coffee</string>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>begin</key>
|
||||
<string>(?<=[=(:]|^|return)\s*(/)(?![/*+{}?])</string>
|
||||
<key>beginCaptures</key>
|
||||
<dict>
|
||||
<key>1</key>
|
||||
<dict>
|
||||
<key>name</key>
|
||||
<string>punctuation.definition.string.begin.coffee</string>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>end</key>
|
||||
<string>(/)[igm]*</string>
|
||||
<key>endCaptures</key>
|
||||
<dict>
|
||||
<key>1</key>
|
||||
<dict>
|
||||
<key>name</key>
|
||||
<string>punctuation.definition.string.end.coffee</string>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>name</key>
|
||||
<string>string.regexp.coffee</string>
|
||||
<key>patterns</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>match</key>
|
||||
<string>\\.</string>
|
||||
<key>name</key>
|
||||
<string>constant.character.escape.coffee</string>
|
||||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>match</key>
|
||||
<string>\b(break|by|catch|continue|else|finally|for|in|of|if|return|switch|then|throw|try|unless|when|while)\b</string>
|
||||
@@ -238,7 +256,7 @@
|
||||
</dict>
|
||||
<dict>
|
||||
<key>match</key>
|
||||
<string>\b([a-zA-Z$_](\w|\$|:|\.)*)(\:)\s</string>
|
||||
<string>\b([a-zA-Z$_](\w|\$|:|\.)*\s*(?=\:))</string>
|
||||
<key>name</key>
|
||||
<string>variable.assignment.coffee</string>
|
||||
<key>captures</key>
|
||||
@@ -248,11 +266,6 @@
|
||||
<key>name</key>
|
||||
<string>entity.name.function.coffee</string>
|
||||
</dict>
|
||||
<key>3</key>
|
||||
<dict>
|
||||
<key>name</key>
|
||||
<string>keyword.operator.coffee</string>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
<dict>
|
||||
@@ -285,6 +298,12 @@
|
||||
<key>name</key>
|
||||
<string>keyword.other.coffee</string>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>match</key>
|
||||
<string>(=|-)></string>
|
||||
<key>name</key>
|
||||
<string>storage.type.function.coffee</string>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>match</key>
|
||||
<string>!|%|&|\*|\/|\-\-|\-|\+\+|\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\?|\|\||\:|\*=|(?<!\()/=|%=|\+=|\-=|&=|\^=|\b(instanceof|new|delete|typeof|and|or|is|isnt|not)\b</string>
|
||||
@@ -297,39 +316,6 @@
|
||||
<key>name</key>
|
||||
<string>constant.language.coffee</string>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>begin</key>
|
||||
<string>(?<=[=(:]|^|return)\s*(/)(?![/*+{}?])</string>
|
||||
<key>beginCaptures</key>
|
||||
<dict>
|
||||
<key>1</key>
|
||||
<dict>
|
||||
<key>name</key>
|
||||
<string>punctuation.definition.string.begin.coffee</string>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>end</key>
|
||||
<string>(/)[igm]*</string>
|
||||
<key>endCaptures</key>
|
||||
<dict>
|
||||
<key>1</key>
|
||||
<dict>
|
||||
<key>name</key>
|
||||
<string>punctuation.definition.string.end.coffee</string>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>name</key>
|
||||
<string>string.regexp.coffee</string>
|
||||
<key>patterns</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>match</key>
|
||||
<string>\\.</string>
|
||||
<key>name</key>
|
||||
<string>constant.character.escape.coffee</string>
|
||||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>match</key>
|
||||
<string>\;</string>
|
||||
20
extras/EXTRAS
Normal file
20
extras/EXTRAS
Normal file
@@ -0,0 +1,20 @@
|
||||
This folder includes rough cuts of CoffeeScript syntax highlighters for
|
||||
TextMate and Vim. Improvements to their lexing ability are always welcome.
|
||||
|
||||
To install the TextMate bundle, drop it into:
|
||||
~/Library/Application Support/TextMate/Bundles
|
||||
|
||||
To install the Vim highlighter, copy "coffee.vim" into the "syntax" directory of
|
||||
your vim72, and enable it in either of the following two ways:
|
||||
|
||||
* Manually, by running `:set syntax=coffee`
|
||||
|
||||
* Or automatically, by creating a "filetype.vim" file within "~/.vim", which
|
||||
contains something along these lines:
|
||||
|
||||
if exists("did_load_filetypes")
|
||||
finish
|
||||
end
|
||||
augroup filetypedetect
|
||||
au! BufRead,BufNewFile *.coffee setfiletype coffee
|
||||
augroup END
|
||||
117
extras/coffee.vim
Normal file
117
extras/coffee.vim
Normal file
@@ -0,0 +1,117 @@
|
||||
" Vim syntax file
|
||||
" Language: CoffeeScript
|
||||
" Maintainer: Jeff Olson <olson.jeffery@gmail.com>
|
||||
" URL: http://github.com/olsonjeffery
|
||||
" Changes: (jro) initial port from javascript
|
||||
" Last Change: 2006 Jun 19
|
||||
" Adaptation of javascript.vim syntax file (distro'd w/ vim72),
|
||||
" maintained by Claudio Fleiner <claudio@fleiner.com>
|
||||
" with updates from Scott Shattuck (ss) <ss@technicalpursuit.com>
|
||||
|
||||
if !exists("main_syntax")
|
||||
if version < 600
|
||||
syntax clear
|
||||
elseif exists("b:current_syntax")
|
||||
finish
|
||||
endif
|
||||
let main_syntax = 'coffee'
|
||||
endif
|
||||
|
||||
syn case ignore
|
||||
|
||||
syn match coffeeLineComment "#.*" contains=@Spell,CoffeeCommentTodo
|
||||
syn match coffeeSpecial "\\\d\d\d\|\\."
|
||||
syn region coffeeStringD start=+"+ skip=+\\\\\|\\"+ end=+"\|$+ contains=coffeeSpecial,@htmlPreproc
|
||||
syn region coffeeStringS start=+'+ skip=+\\\\\|\\'+ end=+'\|$+ contains=coffeeSpecial,@htmlPreproc
|
||||
|
||||
syn match coffeeSpecialCharacter "'\\.'"
|
||||
syn match coffeeNumber "-\=\<\d\+L\=\>\|0[xX][0-9a-fA-F]\+\>"
|
||||
syn region coffeeRegexpString start=+/[^/*]+me=e-1 skip=+\\\\\|\\/+ end=+/[gi]\{0,2\}\s*$+ end=+/[gi]\{0,2\}\s*[;.,)\]}]+me=e-1 contains=@htmlPreproc oneline
|
||||
|
||||
syn match coffeeFunctionParams "([^)]*)\s*->"
|
||||
syn match coffeeBindFunctionParams "([^)]*)\s*=>"
|
||||
syn match coffeePrototypeAccess "::"
|
||||
syn match coffeeBindFunction "=[1]>[1]"
|
||||
syn match coffeeFunction "->"
|
||||
|
||||
syn keyword coffeeExtends extends
|
||||
syn keyword coffeeConditional if else switch then not
|
||||
syn keyword coffeeRepeat while for in of
|
||||
syn keyword coffeeBranch break continue
|
||||
syn keyword coffeeOperator delete instanceof typeof
|
||||
syn keyword coffeeType Array Boolean Date Function Number Object String RegExp
|
||||
syn keyword coffeeStatement return with
|
||||
syn keyword coffeeBoolean true false
|
||||
syn keyword coffeeNull null undefined
|
||||
syn keyword coffeeIdentifier arguments this var
|
||||
syn keyword coffeeLabel case default
|
||||
syn keyword coffeeException try catch finally throw
|
||||
syn keyword coffeeMessage alert confirm prompt status
|
||||
syn keyword coffeeGlobal self window top parent
|
||||
syn keyword coffeeMember document event location
|
||||
syn keyword coffeeDeprecated escape unescape
|
||||
syn keyword coffeeReserved abstract boolean byte char class const debugger double enum export final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile
|
||||
|
||||
syn sync fromstart
|
||||
syn sync maxlines=100
|
||||
|
||||
if main_syntax == "coffee"
|
||||
syn sync ccomment coffeeComment
|
||||
endif
|
||||
|
||||
" Define the default highlighting.
|
||||
" For version 5.7 and earlier: only when not done already
|
||||
" For version 5.8 and later: only when an item doesn't have highlighting yet
|
||||
if version >= 508 || !exists("did_coffee_syn_inits")
|
||||
if version < 508
|
||||
let did_coffee_syn_inits = 1
|
||||
command -nargs=+ HiLink hi link <args>
|
||||
else
|
||||
command -nargs=+ HiLink hi def link <args>
|
||||
endif
|
||||
HiLink coffeePrototypeAccess Keyword
|
||||
HiLink coffeeExtends Keyword
|
||||
HiLink coffeeLineComment Comment
|
||||
HiLink coffeeSpecial Special
|
||||
HiLink coffeeStringS String
|
||||
HiLink coffeeStringD String
|
||||
HiLink coffeeCharacter Character
|
||||
HiLink coffeeSpecialCharacter coffeeSpecial
|
||||
HiLink coffeeNumber coffeeValue
|
||||
HiLink coffeeConditional Conditional
|
||||
HiLink coffeeRepeat Repeat
|
||||
HiLink coffeeBranch Conditional
|
||||
HiLink coffeeOperator Operator
|
||||
HiLink coffeeType Type
|
||||
HiLink coffeeStatement Statement
|
||||
HiLink coffeeBindFunctionParams Function
|
||||
HiLink coffeeFunctionParams Function
|
||||
HiLink coffeeFunction Function
|
||||
HiLink coffeeBindFunction Function
|
||||
HiLink coffeeBraces Function
|
||||
HiLink coffeeError Error
|
||||
HiLink coffeeScrParenError coffeeError
|
||||
HiLink coffeeNull Keyword
|
||||
HiLink coffeeBoolean Boolean
|
||||
HiLink coffeeRegexpString String
|
||||
|
||||
HiLink coffeeIdentifier Identifier
|
||||
HiLink coffeeLabel Label
|
||||
HiLink coffeeException Exception
|
||||
HiLink coffeeMessage Keyword
|
||||
HiLink coffeeGlobal Keyword
|
||||
HiLink coffeeMember Keyword
|
||||
HiLink coffeeDeprecated Exception
|
||||
HiLink coffeeReserved Keyword
|
||||
HiLink coffeeDebug Debug
|
||||
HiLink coffeeConstant Label
|
||||
|
||||
delcommand HiLink
|
||||
endif
|
||||
|
||||
let b:current_syntax = "coffee"
|
||||
if main_syntax == 'coffee'
|
||||
unlet main_syntax
|
||||
endif
|
||||
|
||||
" vim: ts=8
|
||||
1119
index.html
1119
index.html
File diff suppressed because it is too large
Load Diff
7
lib/bin/cake
Executable file
7
lib/bin/cake
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
process.mixin(require('sys'));
|
||||
|
||||
require.paths.unshift('/usr/local/lib/coffee-script/lib');
|
||||
|
||||
require('cake').run();
|
||||
7
lib/bin/coffee
Executable file
7
lib/bin/coffee
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
process.mixin(require('sys'));
|
||||
|
||||
require.paths.unshift('/usr/local/lib/coffee-script/lib');
|
||||
|
||||
require('command_line').run();
|
||||
80
lib/cake.js
Executable file
80
lib/cake.js
Executable file
@@ -0,0 +1,80 @@
|
||||
(function(){
|
||||
var coffee, fs, no_such_task, path, print_tasks, tasks;
|
||||
var __hasProp = Object.prototype.hasOwnProperty;
|
||||
// `cake` is a simplified version of Make (Rake, Jake) for CoffeeScript.
|
||||
fs = require('fs');
|
||||
path = require('path');
|
||||
coffee = require('coffee-script');
|
||||
tasks = {};
|
||||
no_such_task = function no_such_task(task) {
|
||||
process.stdio.writeError('No such task: "' + task + '"\n');
|
||||
return process.exit(1);
|
||||
};
|
||||
// Mixin the Cake functionality.
|
||||
process.mixin({
|
||||
// Define a task with a name, a description, and the action itself.
|
||||
task: function task(name, description, action) {
|
||||
return tasks[name] = {
|
||||
name: name,
|
||||
description: description,
|
||||
action: action
|
||||
};
|
||||
},
|
||||
// Invoke another task in the Cakefile.
|
||||
invoke: function invoke(name) {
|
||||
if (!(tasks[name])) {
|
||||
no_such_task(name);
|
||||
}
|
||||
return tasks[name].action();
|
||||
}
|
||||
});
|
||||
// Display the list of Cake tasks.
|
||||
print_tasks = function print_tasks() {
|
||||
var _a, _b, _c, _d, _e, _f, _g, i, name, spaces, task;
|
||||
_a = []; _b = tasks;
|
||||
for (name in _b) { if (__hasProp.call(_b, name)) {
|
||||
task = _b[name];
|
||||
_a.push((function() {
|
||||
spaces = 20 - name.length;
|
||||
spaces = spaces > 0 ? (function() {
|
||||
_c = []; _f = 0; _g = spaces;
|
||||
for (_e=0, i=_f; (_f <= _g ? i <= _g : i >= _g); (_f <= _g ? i += 1 : i -= 1), _e++) {
|
||||
_c.push(' ');
|
||||
}
|
||||
return _c;
|
||||
}).call(this).join('') : '';
|
||||
return puts("cake " + name + spaces + ' # ' + task.description);
|
||||
}).call(this));
|
||||
}}
|
||||
return _a;
|
||||
};
|
||||
// Running `cake` runs the tasks you pass asynchronously (node-style), or
|
||||
// prints them out, with no arguments.
|
||||
exports.run = function run() {
|
||||
return path.exists('Cakefile', function(exists) {
|
||||
var args;
|
||||
if (!(exists)) {
|
||||
throw new Error('Cakefile not found in ' + process.cwd());
|
||||
}
|
||||
args = process.ARGV.slice(2, process.ARGV.length);
|
||||
return fs.readFile('Cakefile', function(err, source) {
|
||||
var _a, _b, _c, arg;
|
||||
eval(coffee.compile(source));
|
||||
if (!(args.length)) {
|
||||
return print_tasks();
|
||||
}
|
||||
_a = []; _b = args;
|
||||
for (_c = 0; _c < _b.length; _c++) {
|
||||
arg = _b[_c];
|
||||
_a.push((function() {
|
||||
if (!(tasks[arg])) {
|
||||
no_such_task(arg);
|
||||
}
|
||||
return tasks[arg].action();
|
||||
}).call(this));
|
||||
}
|
||||
return _a;
|
||||
});
|
||||
});
|
||||
};
|
||||
})();
|
||||
61
lib/coffee-script.js
Normal file
61
lib/coffee-script.js
Normal file
@@ -0,0 +1,61 @@
|
||||
(function(){
|
||||
var lexer, parser, path;
|
||||
// Set up for both the browser and the server.
|
||||
if ((typeof process !== "undefined" && process !== null)) {
|
||||
process.mixin(require('nodes'));
|
||||
path = require('path');
|
||||
lexer = new (require('lexer').Lexer)();
|
||||
parser = require('parser').parser;
|
||||
} else {
|
||||
lexer = new Lexer();
|
||||
parser = exports.parser;
|
||||
this.exports = (this.CoffeeScript = {});
|
||||
}
|
||||
// Thin wrapper for Jison compatibility around the real lexer.
|
||||
parser.lexer = {
|
||||
lex: function lex() {
|
||||
var token;
|
||||
token = this.tokens[this.pos] || [""];
|
||||
this.pos += 1;
|
||||
this.yylineno = token[2];
|
||||
this.yytext = token[1];
|
||||
return token[0];
|
||||
},
|
||||
setInput: function setInput(tokens) {
|
||||
this.tokens = tokens;
|
||||
return this.pos = 0;
|
||||
},
|
||||
upcomingInput: function upcomingInput() {
|
||||
return "";
|
||||
},
|
||||
showPosition: function showPosition() {
|
||||
return this.pos;
|
||||
}
|
||||
};
|
||||
exports.VERSION = '0.5.0';
|
||||
// Compile CoffeeScript to JavaScript, using the Coffee/Jison compiler.
|
||||
exports.compile = function compile(code, options) {
|
||||
return (parser.parse(lexer.tokenize(code))).compile(options);
|
||||
};
|
||||
// Just the tokens.
|
||||
exports.tokenize = function tokenize(code) {
|
||||
return lexer.tokenize(code);
|
||||
};
|
||||
// Just the nodes.
|
||||
exports.tree = function tree(code) {
|
||||
return parser.parse(lexer.tokenize(code));
|
||||
};
|
||||
// Pretty-print a token stream.
|
||||
exports.print_tokens = function print_tokens(tokens) {
|
||||
var _a, _b, _c, strings, token;
|
||||
strings = (function() {
|
||||
_a = []; _b = tokens;
|
||||
for (_c = 0; _c < _b.length; _c++) {
|
||||
token = _b[_c];
|
||||
_a.push('[' + token[0] + ' ' + token[1].toString().replace(/\n/, '\\n') + ']');
|
||||
}
|
||||
return _a;
|
||||
}).call(this);
|
||||
return puts(strings.join(' '));
|
||||
};
|
||||
})();
|
||||
@@ -1,21 +0,0 @@
|
||||
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
||||
require "coffee_script/lexer"
|
||||
require "coffee_script/parser"
|
||||
require "coffee_script/nodes"
|
||||
require "coffee_script/value"
|
||||
require "coffee_script/scope"
|
||||
require "coffee_script/rewriter"
|
||||
require "coffee_script/parse_error"
|
||||
|
||||
# Namespace for all CoffeeScript internal classes.
|
||||
module CoffeeScript
|
||||
|
||||
VERSION = '0.2.4' # Keep in sync with the gemspec.
|
||||
|
||||
# Compile a script (String or IO) to JavaScript.
|
||||
def self.compile(script, options={})
|
||||
script = script.read if script.respond_to?(:read)
|
||||
Parser.new.parse(script).compile(options)
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,227 +0,0 @@
|
||||
require 'optparse'
|
||||
require 'fileutils'
|
||||
require 'open3'
|
||||
begin
|
||||
require File.expand_path(File.dirname(__FILE__) + '/../coffee-script')
|
||||
rescue LoadError => e
|
||||
puts(e.message)
|
||||
puts("use \"rake build:parser\" to regenerate parser.rb")
|
||||
exit(1)
|
||||
end
|
||||
|
||||
module CoffeeScript
|
||||
|
||||
# The CommandLine handles all of the functionality of the `coffee`
|
||||
# utility.
|
||||
class CommandLine
|
||||
|
||||
BANNER = <<-EOS
|
||||
coffee compiles CoffeeScript source files into JavaScript.
|
||||
|
||||
Usage:
|
||||
coffee path/to/script.coffee
|
||||
EOS
|
||||
|
||||
# Seconds to pause between checks for changed source files.
|
||||
WATCH_INTERVAL = 0.5
|
||||
|
||||
# Command to execute in Narwhal
|
||||
PACKAGE = File.expand_path(File.dirname(__FILE__) + '/../..')
|
||||
LAUNCHER = "narwhal -p #{PACKAGE} -e 'require(\"coffee-script\").run(system.args);'"
|
||||
|
||||
# Run the CommandLine off the contents of ARGV.
|
||||
def initialize
|
||||
@mtimes = {}
|
||||
parse_options
|
||||
return launch_repl if @options[:interactive]
|
||||
return eval_scriptlet if @options[:eval]
|
||||
check_sources
|
||||
return run_scripts if @options[:run]
|
||||
@sources.each {|source| compile_javascript(source) }
|
||||
watch_coffee_scripts if @options[:watch]
|
||||
end
|
||||
|
||||
# The "--help" usage message.
|
||||
def usage
|
||||
puts "\n#{@option_parser}\n"
|
||||
exit
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
# Compiles (or partially compiles) the source CoffeeScript file, returning
|
||||
# the desired JS, tokens, or lint results.
|
||||
def compile_javascript(source)
|
||||
script = File.read(source)
|
||||
return tokens(script) if @options[:tokens]
|
||||
js = compile(script, source)
|
||||
return unless js
|
||||
return puts(js) if @options[:print]
|
||||
return lint(js) if @options[:lint]
|
||||
File.open(path_for(source), 'w+') {|f| f.write(js) }
|
||||
end
|
||||
|
||||
# Spins up a watcher thread to keep track of the modification times of the
|
||||
# source files, recompiling them whenever they're saved.
|
||||
def watch_coffee_scripts
|
||||
watch_thread = Thread.start do
|
||||
loop do
|
||||
@sources.each do |source|
|
||||
mtime = File.stat(source).mtime
|
||||
@mtimes[source] ||= mtime
|
||||
if mtime > @mtimes[source]
|
||||
@mtimes[source] = mtime
|
||||
compile_javascript(source)
|
||||
end
|
||||
end
|
||||
sleep WATCH_INTERVAL
|
||||
end
|
||||
end
|
||||
Signal.trap("INT") { watch_thread.kill }
|
||||
watch_thread.join
|
||||
end
|
||||
|
||||
# Ensure that all of the source files exist.
|
||||
def check_sources
|
||||
usage if @sources.empty?
|
||||
missing = @sources.detect {|s| !File.exists?(s) }
|
||||
if missing
|
||||
STDERR.puts("File not found: '#{missing}'")
|
||||
exit(1)
|
||||
end
|
||||
end
|
||||
|
||||
# Pipe compiled JS through JSLint (requires a working 'jsl' command).
|
||||
def lint(js)
|
||||
stdin, stdout, stderr = Open3.popen3('jsl -nologo -stdin')
|
||||
stdin.write(js)
|
||||
stdin.close
|
||||
puts stdout.read.tr("\n", '')
|
||||
errs = stderr.read.chomp
|
||||
puts errs unless errs.empty?
|
||||
stdout.close and stderr.close
|
||||
end
|
||||
|
||||
# Eval a little piece of CoffeeScript directly from the command line.
|
||||
def eval_scriptlet
|
||||
script = STDIN.tty? ? @sources.join(' ') : STDIN.read
|
||||
return tokens(script) if @options[:tokens]
|
||||
js = compile(script)
|
||||
return lint(js) if @options[:lint]
|
||||
puts js
|
||||
end
|
||||
|
||||
# Use Narwhal to run an interactive CoffeeScript session.
|
||||
def launch_repl
|
||||
exec "#{LAUNCHER}"
|
||||
rescue Errno::ENOENT
|
||||
puts "Error: Narwhal must be installed to use the interactive REPL."
|
||||
exit(1)
|
||||
end
|
||||
|
||||
# Use Narwhal to compile and execute CoffeeScripts.
|
||||
def run_scripts
|
||||
sources = @sources.join(' ')
|
||||
exec "#{LAUNCHER} #{sources}"
|
||||
rescue Errno::ENOENT
|
||||
puts "Error: Narwhal must be installed in order to execute CoffeeScripts."
|
||||
exit(1)
|
||||
end
|
||||
|
||||
# Print the tokens that the lexer generates from a source script.
|
||||
def tokens(script)
|
||||
puts Lexer.new.tokenize(script).inspect
|
||||
end
|
||||
|
||||
# Compile a single source file to JavaScript.
|
||||
def compile(script, source='error')
|
||||
begin
|
||||
options = {}
|
||||
options[:no_wrap] = true if @options[:no_wrap]
|
||||
options[:globals] = true if @options[:globals]
|
||||
CoffeeScript.compile(script, options)
|
||||
rescue CoffeeScript::ParseError, SyntaxError => e
|
||||
STDERR.puts "#{source}: #{e.message}"
|
||||
exit(1) unless @options[:watch]
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
# Write out JavaScript alongside CoffeeScript unless an output directory
|
||||
# is specified.
|
||||
def path_for(source)
|
||||
filename = File.basename(source, File.extname(source)) + '.js'
|
||||
dir = @options[:output] || File.dirname(source)
|
||||
File.join(dir, filename)
|
||||
end
|
||||
|
||||
# Install the CoffeeScript TextMate bundle to ~/Library.
|
||||
def install_bundle
|
||||
bundle_dir = File.expand_path('~/Library/Application Support/TextMate/Bundles/')
|
||||
FileUtils.cp_r(File.dirname(__FILE__) + '/CoffeeScript.tmbundle', bundle_dir)
|
||||
end
|
||||
|
||||
# Use OptionParser for all the options.
|
||||
def parse_options
|
||||
@options = {}
|
||||
@option_parser = OptionParser.new do |opts|
|
||||
opts.on('-i', '--interactive', 'run a CoffeeScript REPL (requires Narwhal)') do |i|
|
||||
@options[:interactive] = true
|
||||
end
|
||||
opts.on('-r', '--run', 'compile and run a script (requires Narwhal)') do |r|
|
||||
@options[:run] = true
|
||||
end
|
||||
opts.on('-o', '--output [DIR]', 'set the directory for compiled JavaScript') do |d|
|
||||
@options[:output] = d
|
||||
FileUtils.mkdir_p(d) unless File.exists?(d)
|
||||
end
|
||||
opts.on('-w', '--watch', 'watch scripts for changes, and recompile') do |w|
|
||||
@options[:watch] = true
|
||||
end
|
||||
opts.on('-p', '--print', 'print the compiled JavaScript to stdout') do |d|
|
||||
@options[:print] = true
|
||||
end
|
||||
opts.on('-l', '--lint', 'pipe the compiled JavaScript through JSLint') do |l|
|
||||
@options[:lint] = true
|
||||
end
|
||||
opts.on('-e', '--eval', 'compile a cli scriptlet or read from stdin') do |e|
|
||||
@options[:eval] = true
|
||||
end
|
||||
opts.on('-t', '--tokens', 'print the tokens that the lexer produces') do |t|
|
||||
@options[:tokens] = true
|
||||
end
|
||||
opts.on('-v', '--verbose', 'print at every step of code generation') do |v|
|
||||
ENV['VERBOSE'] = 'true'
|
||||
end
|
||||
opts.on('-n', '--no-wrap', 'raw output, no function safety wrapper') do |n|
|
||||
@options[:no_wrap] = true
|
||||
end
|
||||
opts.on('-g', '--globals', 'attach all top-level variable as globals') do |n|
|
||||
@options[:globals] = true
|
||||
end
|
||||
opts.on_tail('--install-bundle', 'install the CoffeeScript TextMate bundle') do |i|
|
||||
install_bundle
|
||||
exit
|
||||
end
|
||||
opts.on_tail('--version', 'display CoffeeScript version') do
|
||||
puts "CoffeeScript version #{CoffeeScript::VERSION}"
|
||||
exit
|
||||
end
|
||||
opts.on_tail('-h', '--help', 'display this help message') do
|
||||
usage
|
||||
end
|
||||
end
|
||||
@option_parser.banner = BANNER
|
||||
begin
|
||||
@option_parser.parse!(ARGV)
|
||||
rescue OptionParser::InvalidOption => e
|
||||
puts e.message
|
||||
exit(1)
|
||||
end
|
||||
@sources = ARGV
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,454 +0,0 @@
|
||||
class Parser
|
||||
|
||||
# Declare terminal tokens produced by the lexer.
|
||||
token IF ELSE UNLESS
|
||||
token NUMBER STRING REGEX
|
||||
token TRUE FALSE YES NO ON OFF
|
||||
token IDENTIFIER PROPERTY_ACCESS PROTOTYPE_ACCESS
|
||||
token CODE PARAM NEW RETURN
|
||||
token TRY CATCH FINALLY THROW
|
||||
token BREAK CONTINUE
|
||||
token FOR IN OF BY WHEN WHILE
|
||||
token SWITCH LEADING_WHEN
|
||||
token DELETE INSTANCEOF TYPEOF
|
||||
token SUPER EXTENDS
|
||||
token ARGUMENTS
|
||||
token NEWLINE
|
||||
token COMMENT
|
||||
token JS
|
||||
token INDENT OUTDENT
|
||||
|
||||
# Declare order of operations.
|
||||
prechigh
|
||||
left '?'
|
||||
nonassoc UMINUS NOT '!' '!!' '~' '++' '--'
|
||||
left '*' '/' '%'
|
||||
left '+' '-'
|
||||
left '<<' '>>' '>>>'
|
||||
left '&' '|' '^'
|
||||
left '<=' '<' '>' '>='
|
||||
right '==' '!=' IS ISNT
|
||||
left '&&' '||' AND OR
|
||||
right '-=' '+=' '/=' '*=' '%='
|
||||
right DELETE INSTANCEOF TYPEOF
|
||||
left '.'
|
||||
right INDENT
|
||||
left OUTDENT
|
||||
right WHEN LEADING_WHEN IN OF BY
|
||||
right THROW FOR NEW SUPER
|
||||
left EXTENDS
|
||||
left ASSIGN '||=' '&&='
|
||||
right RETURN
|
||||
right '=>' UNLESS IF ELSE WHILE
|
||||
preclow
|
||||
|
||||
rule
|
||||
|
||||
# All parsing will end in this rule, being the trunk of the AST.
|
||||
Root:
|
||||
/* nothing */ { result = Expressions.new }
|
||||
| Terminator { result = Expressions.new }
|
||||
| Expressions { result = val[0] }
|
||||
| Block Terminator { result = val[0] }
|
||||
;
|
||||
|
||||
# Any list of expressions or method body, seperated by line breaks or semis.
|
||||
Expressions:
|
||||
Expression { result = Expressions.wrap(val) }
|
||||
| Expressions Terminator Expression { result = val[0] << val[2] }
|
||||
| Expressions Terminator { result = val[0] }
|
||||
;
|
||||
|
||||
# All types of expressions in our language. The basic unit of CoffeeScript
|
||||
# is the expression.
|
||||
Expression:
|
||||
Value
|
||||
| Call
|
||||
| Code
|
||||
| Operation
|
||||
| Assign
|
||||
| If
|
||||
| Try
|
||||
| Throw
|
||||
| Return
|
||||
| While
|
||||
| For
|
||||
| Switch
|
||||
| Extends
|
||||
| Splat
|
||||
| Existence
|
||||
| Comment
|
||||
;
|
||||
|
||||
# A block of expressions. Note that the Rewriter will convert some postfix
|
||||
# forms into blocks for us, by altering the token stream.
|
||||
Block:
|
||||
INDENT Expressions OUTDENT { result = val[1] }
|
||||
| INDENT OUTDENT { result = Expressions.new }
|
||||
;
|
||||
|
||||
# Tokens that can terminate an expression.
|
||||
Terminator:
|
||||
"\n"
|
||||
| ";"
|
||||
;
|
||||
|
||||
# All hard-coded values. These can be printed straight to JavaScript.
|
||||
Literal:
|
||||
NUMBER { result = LiteralNode.new(val[0]) }
|
||||
| STRING { result = LiteralNode.new(val[0]) }
|
||||
| JS { result = LiteralNode.new(val[0]) }
|
||||
| REGEX { result = LiteralNode.new(val[0]) }
|
||||
| BREAK { result = LiteralNode.new(val[0]) }
|
||||
| CONTINUE { result = LiteralNode.new(val[0]) }
|
||||
| ARGUMENTS { result = LiteralNode.new(val[0]) }
|
||||
| TRUE { result = LiteralNode.new(true) }
|
||||
| FALSE { result = LiteralNode.new(false) }
|
||||
| YES { result = LiteralNode.new(true) }
|
||||
| NO { result = LiteralNode.new(false) }
|
||||
| ON { result = LiteralNode.new(true) }
|
||||
| OFF { result = LiteralNode.new(false) }
|
||||
;
|
||||
|
||||
# Assignment to a variable (or index).
|
||||
Assign:
|
||||
Value ASSIGN Expression { result = AssignNode.new(val[0], val[2]) }
|
||||
;
|
||||
|
||||
# Assignment within an object literal (can be quoted).
|
||||
AssignObj:
|
||||
IDENTIFIER ASSIGN Expression { result = AssignNode.new(ValueNode.new(val[0]), val[2], :object) }
|
||||
| STRING ASSIGN Expression { result = AssignNode.new(ValueNode.new(LiteralNode.new(val[0])), val[2], :object) }
|
||||
| Comment { result = val[0] }
|
||||
;
|
||||
|
||||
# A return statement.
|
||||
Return:
|
||||
RETURN Expression { result = ReturnNode.new(val[1]) }
|
||||
| RETURN { result = ReturnNode.new(ValueNode.new(Value.new('null'))) }
|
||||
;
|
||||
|
||||
# A comment.
|
||||
Comment:
|
||||
COMMENT { result = CommentNode.new(val[0]) }
|
||||
;
|
||||
|
||||
# Arithmetic and logical operators
|
||||
# For Ruby's Operator precedence, see:
|
||||
# https://www.cs.auckland.ac.nz/references/ruby/ProgrammingRuby/language.html
|
||||
Operation:
|
||||
'!' Expression { result = OpNode.new(val[0], val[1]) }
|
||||
| '!!' Expression { result = OpNode.new(val[0], val[1]) }
|
||||
| '-' Expression = UMINUS { result = OpNode.new(val[0], val[1]) }
|
||||
| NOT Expression { result = OpNode.new(val[0], val[1]) }
|
||||
| '~' Expression { result = OpNode.new(val[0], val[1]) }
|
||||
| '--' Expression { result = OpNode.new(val[0], val[1]) }
|
||||
| '++' Expression { result = OpNode.new(val[0], val[1]) }
|
||||
| DELETE Expression { result = OpNode.new(val[0], val[1]) }
|
||||
| TYPEOF Expression { result = OpNode.new(val[0], val[1]) }
|
||||
| Expression '--' { result = OpNode.new(val[1], val[0], nil, true) }
|
||||
| Expression '++' { result = OpNode.new(val[1], val[0], nil, true) }
|
||||
|
||||
| Expression '*' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '/' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '%' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
|
||||
| Expression '+' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '-' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
|
||||
| Expression '<<' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '>>' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '>>>' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
|
||||
| Expression '&' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '|' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '^' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
|
||||
| Expression '<=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '<' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '>' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '>=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
|
||||
| Expression '==' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '!=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression IS Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression ISNT Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
|
||||
| Expression '&&' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '||' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression AND Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression OR Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
|
||||
| Expression '-=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '+=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '/=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '*=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '%=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '||=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression '&&=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
|
||||
| Expression INSTANCEOF Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
| Expression IN Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||
;
|
||||
|
||||
# The existence operator.
|
||||
Existence:
|
||||
Expression '?' { result = ExistenceNode.new(val[0]) }
|
||||
;
|
||||
|
||||
# Function definition.
|
||||
Code:
|
||||
ParamList "=>" Block { result = CodeNode.new(val[0], val[2]) }
|
||||
| "=>" Block { result = CodeNode.new([], val[1]) }
|
||||
;
|
||||
|
||||
# The parameters to a function definition.
|
||||
ParamList:
|
||||
Param { result = val }
|
||||
| ParamList "," Param { result = val[0] << val[2] }
|
||||
;
|
||||
|
||||
# A Parameter (or ParamSplat) in a function definition.
|
||||
Param:
|
||||
PARAM
|
||||
| PARAM "." "." "." { result = ParamSplatNode.new(val[0]) }
|
||||
;
|
||||
|
||||
# A regular splat.
|
||||
Splat:
|
||||
Expression "." "." "." { result = ArgSplatNode.new(val[0])}
|
||||
;
|
||||
|
||||
# Expressions that can be treated as values.
|
||||
Value:
|
||||
IDENTIFIER { result = ValueNode.new(val[0]) }
|
||||
| Literal { result = ValueNode.new(val[0]) }
|
||||
| Array { result = ValueNode.new(val[0]) }
|
||||
| Object { result = ValueNode.new(val[0]) }
|
||||
| Parenthetical { result = ValueNode.new(val[0]) }
|
||||
| Range { result = ValueNode.new(val[0]) }
|
||||
| Value Accessor { result = val[0] << val[1] }
|
||||
| Invocation Accessor { result = ValueNode.new(val[0], [val[1]]) }
|
||||
;
|
||||
|
||||
# Accessing into an object or array, through dot or index notation.
|
||||
Accessor:
|
||||
PROPERTY_ACCESS IDENTIFIER { result = AccessorNode.new(val[1]) }
|
||||
| PROTOTYPE_ACCESS IDENTIFIER { result = AccessorNode.new(val[1], true) }
|
||||
| Index { result = val[0] }
|
||||
| Range { result = SliceNode.new(val[0]) }
|
||||
;
|
||||
|
||||
# Indexing into an object or array.
|
||||
Index:
|
||||
"[" Expression "]" { result = IndexNode.new(val[1]) }
|
||||
;
|
||||
|
||||
# An object literal.
|
||||
Object:
|
||||
"{" AssignList "}" { result = ObjectNode.new(val[1]) }
|
||||
;
|
||||
|
||||
# Assignment within an object literal (comma or newline separated).
|
||||
AssignList:
|
||||
/* nothing */ { result = [] }
|
||||
| AssignObj { result = val }
|
||||
| AssignList "," AssignObj { result = val[0] << val[2] }
|
||||
| AssignList Terminator AssignObj { result = val[0] << val[2] }
|
||||
| AssignList ","
|
||||
Terminator AssignObj { result = val[0] << val[3] }
|
||||
| INDENT AssignList OUTDENT { result = val[1] }
|
||||
;
|
||||
|
||||
# All flavors of function call (instantiation, super, and regular).
|
||||
Call:
|
||||
Invocation { result = val[0] }
|
||||
| NEW Invocation { result = val[1].new_instance }
|
||||
| Super { result = val[0] }
|
||||
;
|
||||
|
||||
# Extending an object's prototype.
|
||||
Extends:
|
||||
Value EXTENDS Value { result = ExtendsNode.new(val[0], val[2]) }
|
||||
;
|
||||
|
||||
# A generic function invocation.
|
||||
Invocation:
|
||||
Value Arguments { result = CallNode.new(val[0], val[1]) }
|
||||
| Invocation Arguments { result = CallNode.new(val[0], val[1]) }
|
||||
# | Invocation Code { result = val[0] << val[1] }
|
||||
;
|
||||
|
||||
# The list of arguments to a function invocation.
|
||||
Arguments:
|
||||
"(" ArgList ")" { result = val[1] }
|
||||
| "(" ArgList ")" Code { result = val[1] << val[3] }
|
||||
;
|
||||
|
||||
# Calling super.
|
||||
Super:
|
||||
SUPER "(" ArgList ")" { result = CallNode.new(:super, val[2]) }
|
||||
;
|
||||
|
||||
# The range literal.
|
||||
Range:
|
||||
"[" Expression
|
||||
"." "." Expression "]" { result = RangeNode.new(val[1], val[4]) }
|
||||
| "[" Expression
|
||||
"." "." "." Expression "]" { result = RangeNode.new(val[1], val[5], true) }
|
||||
;
|
||||
|
||||
# The array literal.
|
||||
Array:
|
||||
"[" ArgList "]" { result = ArrayNode.new(val[1]) }
|
||||
;
|
||||
|
||||
# A list of arguments to a method call, or as the contents of an array.
|
||||
ArgList:
|
||||
/* nothing */ { result = [] }
|
||||
| Expression { result = val }
|
||||
| INDENT Expression { result = [val[1]] }
|
||||
| ArgList "," Expression { result = val[0] << val[2] }
|
||||
| ArgList Terminator Expression { result = val[0] << val[2] }
|
||||
| ArgList "," Terminator Expression { result = val[0] << val[3] }
|
||||
| ArgList "," INDENT Expression { result = val[0] << val[3] }
|
||||
| ArgList OUTDENT { result = val[0] }
|
||||
;
|
||||
|
||||
# Try/catch/finally exception handling blocks.
|
||||
Try:
|
||||
TRY Block Catch { result = TryNode.new(val[1], val[2][0], val[2][1]) }
|
||||
| TRY Block FINALLY Block { result = TryNode.new(val[1], nil, nil, val[3]) }
|
||||
| TRY Block Catch
|
||||
FINALLY Block { result = TryNode.new(val[1], val[2][0], val[2][1], val[4]) }
|
||||
;
|
||||
|
||||
# A catch clause.
|
||||
Catch:
|
||||
CATCH IDENTIFIER Block { result = [val[1], val[2]] }
|
||||
;
|
||||
|
||||
# Throw an exception.
|
||||
Throw:
|
||||
THROW Expression { result = ThrowNode.new(val[1]) }
|
||||
;
|
||||
|
||||
# Parenthetical expressions.
|
||||
Parenthetical:
|
||||
"(" Expression ")" { result = ParentheticalNode.new(val[1], val[0].line) }
|
||||
;
|
||||
|
||||
# The while loop. (there is no do..while).
|
||||
While:
|
||||
WHILE Expression Block { result = WhileNode.new(val[1], val[2]) }
|
||||
| WHILE Expression { result = WhileNode.new(val[1], nil) }
|
||||
;
|
||||
|
||||
# Array comprehensions, including guard and current index.
|
||||
# Looks a little confusing, check nodes.rb for the arguments to ForNode.
|
||||
For:
|
||||
Expression FOR
|
||||
ForVariables ForSource { result = ForNode.new(val[0], val[3], val[2][0], val[2][1]) }
|
||||
| FOR ForVariables ForSource Block { result = ForNode.new(val[3], val[2], val[1][0], val[1][1]) }
|
||||
;
|
||||
|
||||
# An array comprehension has variables for the current element and index.
|
||||
ForVariables:
|
||||
IDENTIFIER { result = val }
|
||||
| IDENTIFIER "," IDENTIFIER { result = [val[0], val[2]] }
|
||||
;
|
||||
|
||||
# The source of the array comprehension can optionally be filtered.
|
||||
ForSource:
|
||||
IN Expression { result = {:source => val[1]} }
|
||||
| OF Expression { result = {:source => val[1], :object => true} }
|
||||
| ForSource
|
||||
WHEN Expression { result = val[0].merge(:filter => val[2]) }
|
||||
| ForSource
|
||||
BY Expression { result = val[0].merge(:step => val[2]) }
|
||||
;
|
||||
|
||||
# Switch/When blocks.
|
||||
Switch:
|
||||
SWITCH Expression INDENT
|
||||
Whens OUTDENT { result = val[3].rewrite_condition(val[1]) }
|
||||
| SWITCH Expression INDENT
|
||||
Whens ELSE Block OUTDENT { result = val[3].rewrite_condition(val[1]).add_else(val[5]) }
|
||||
;
|
||||
|
||||
# The inner list of whens.
|
||||
Whens:
|
||||
When { result = val[0] }
|
||||
| Whens When { result = val[0] << val[1] }
|
||||
;
|
||||
|
||||
# An individual when.
|
||||
When:
|
||||
LEADING_WHEN Expression Block { result = IfNode.new(val[1], val[2], nil, {:statement => true}) }
|
||||
| LEADING_WHEN Expression Block
|
||||
Terminator { result = IfNode.new(val[1], val[2], nil, {:statement => true}) }
|
||||
| Comment Terminator When { result = val[2].add_comment(val[0]) }
|
||||
;
|
||||
|
||||
# The most basic form of "if".
|
||||
IfBlock:
|
||||
IF Expression Block { result = IfNode.new(val[1], val[2]) }
|
||||
;
|
||||
|
||||
# An elsif portion of an if-else block.
|
||||
ElsIf:
|
||||
ELSE IfBlock { result = val[1].force_statement }
|
||||
;
|
||||
|
||||
# Multiple elsifs can be chained together.
|
||||
ElsIfs:
|
||||
ElsIf { result = val[0] }
|
||||
| ElsIfs ElsIf { result = val[0].add_else(val[1]) }
|
||||
;
|
||||
|
||||
# Terminating else bodies are strictly optional.
|
||||
ElseBody
|
||||
/* nothing */ { result = nil }
|
||||
| ELSE Block { result = val[1] }
|
||||
;
|
||||
|
||||
# All the alternatives for ending an if-else block.
|
||||
IfEnd:
|
||||
ElseBody { result = val[0] }
|
||||
| ElsIfs ElseBody { result = val[0].add_else(val[1]) }
|
||||
;
|
||||
|
||||
# The full complement of if blocks, including postfix one-liner ifs and unlesses.
|
||||
If:
|
||||
IfBlock IfEnd { result = val[0].add_else(val[1]) }
|
||||
| Expression IF Expression { result = IfNode.new(val[2], Expressions.wrap(val[0]), nil, {:statement => true}) }
|
||||
| Expression UNLESS Expression { result = IfNode.new(val[2], Expressions.wrap(val[0]), nil, {:statement => true, :invert => true}) }
|
||||
;
|
||||
|
||||
end
|
||||
|
||||
---- header
|
||||
module CoffeeScript
|
||||
|
||||
---- inner
|
||||
# Lex and parse a CoffeeScript.
|
||||
def parse(code)
|
||||
# Uncomment the following line to enable grammar debugging, in combination
|
||||
# with the -g flag in the Rake build task.
|
||||
# @yydebug = true
|
||||
@tokens = Lexer.new.tokenize(code)
|
||||
do_parse
|
||||
end
|
||||
|
||||
# Retrieve the next token from the list.
|
||||
def next_token
|
||||
@tokens.shift
|
||||
end
|
||||
|
||||
# Raise a custom error class that knows about line numbers.
|
||||
def on_error(error_token_id, error_value, value_stack)
|
||||
raise ParseError.new(token_to_str(error_token_id), error_value, value_stack)
|
||||
end
|
||||
|
||||
---- footer
|
||||
end
|
||||
@@ -1,252 +0,0 @@
|
||||
module CoffeeScript
|
||||
|
||||
# The lexer reads a stream of CoffeeScript and divvys it up into tagged
|
||||
# tokens. A minor bit of the ambiguity in the grammar has been avoided by
|
||||
# pushing some extra smarts into the Lexer.
|
||||
class Lexer
|
||||
|
||||
# The list of keywords passed verbatim to the parser.
|
||||
KEYWORDS = ["if", "else", "then", "unless",
|
||||
"true", "false", "yes", "no", "on", "off",
|
||||
"and", "or", "is", "isnt", "not",
|
||||
"new", "return",
|
||||
"try", "catch", "finally", "throw",
|
||||
"break", "continue",
|
||||
"for", "in", "of", "by", "where", "while",
|
||||
"switch", "when",
|
||||
"super", "extends",
|
||||
"arguments",
|
||||
"delete", "instanceof", "typeof"]
|
||||
|
||||
# Token matching regexes.
|
||||
IDENTIFIER = /\A([a-zA-Z$_](\w|\$)*)/
|
||||
NUMBER = /\A(\b((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?(e[+\-]?[0-9]+)?)))\b/i
|
||||
STRING = /\A(""|''|"(.*?)([^\\]|\\\\)"|'(.*?)([^\\]|\\\\)')/m
|
||||
HEREDOC = /\A("{6}|'{6}|"{3}\n?(\s*)(.*?)\n?(\s*)"{3}|'{3}\n?(\s*)(.*?)\n?(\s*)'{3})/m
|
||||
JS = /\A(``|`(.*?)([^\\]|\\\\)`)/m
|
||||
OPERATOR = /\A([+\*&|\/\-%=<>:!]+)/
|
||||
WHITESPACE = /\A([ \t]+)/
|
||||
COMMENT = /\A(((\n?[ \t]*)?#.*$)+)/
|
||||
CODE = /\A(=>)/
|
||||
REGEX = /\A(\/(.*?)([^\\]|\\\\)\/[imgy]{0,4})/
|
||||
MULTI_DENT = /\A((\n([ \t]*))+)(\.)?/
|
||||
LAST_DENT = /\n([ \t]*)/
|
||||
ASSIGNMENT = /\A(:|=)\Z/
|
||||
|
||||
# Token cleaning regexes.
|
||||
JS_CLEANER = /(\A`|`\Z)/
|
||||
MULTILINER = /\n/
|
||||
COMMENT_CLEANER = /(^\s*#|\n\s*$)/
|
||||
NO_NEWLINE = /\A([+\*&|\/\-%=<>:!.\\][<>=&|]*|and|or|is|isnt|not|delete|typeof|instanceof)\Z/
|
||||
|
||||
# Tokens which a regular expression will never immediately follow, but which
|
||||
# a division operator might.
|
||||
# See: http://www.mozilla.org/js/language/js20-2002-04/rationale/syntax.html#regular-expressions
|
||||
NOT_REGEX = [
|
||||
:IDENTIFIER, :NUMBER, :REGEX, :STRING,
|
||||
')', '++', '--', ']', '}',
|
||||
:FALSE, :NULL, :THIS, :TRUE
|
||||
]
|
||||
|
||||
# Scan by attempting to match tokens one character at a time. Slow and steady.
|
||||
def tokenize(code)
|
||||
@code = code.chomp # Cleanup code by remove extra line breaks
|
||||
@i = 0 # Current character position we're parsing
|
||||
@line = 1 # The current line.
|
||||
@indent = 0 # The current indent level.
|
||||
@indents = [] # The stack of all indent levels we are currently within.
|
||||
@tokens = [] # Collection of all parsed tokens in the form [:TOKEN_TYPE, value]
|
||||
while @i < @code.length
|
||||
@chunk = @code[@i..-1]
|
||||
extract_next_token
|
||||
end
|
||||
puts "original stream: #{@tokens.inspect}" if ENV['VERBOSE']
|
||||
close_indentation
|
||||
Rewriter.new.rewrite(@tokens)
|
||||
end
|
||||
|
||||
# At every position, run through this list of attempted matches,
|
||||
# short-circuiting if any of them succeed.
|
||||
def extract_next_token
|
||||
return if identifier_token
|
||||
return if number_token
|
||||
return if heredoc_token
|
||||
return if string_token
|
||||
return if js_token
|
||||
return if regex_token
|
||||
return if indent_token
|
||||
return if comment_token
|
||||
return if whitespace_token
|
||||
return literal_token
|
||||
end
|
||||
|
||||
# Tokenizers ==========================================================
|
||||
|
||||
# Matches identifying literals: variables, keywords, method names, etc.
|
||||
def identifier_token
|
||||
return false unless identifier = @chunk[IDENTIFIER, 1]
|
||||
# Keywords are special identifiers tagged with their own name,
|
||||
# 'if' will result in an [:IF, "if"] token.
|
||||
tag = KEYWORDS.include?(identifier) ? identifier.upcase.to_sym : :IDENTIFIER
|
||||
tag = :LEADING_WHEN if tag == :WHEN && [:OUTDENT, :INDENT, "\n"].include?(last_tag)
|
||||
@tokens[-1][0] = :PROPERTY_ACCESS if tag == :IDENTIFIER && last_value == '.' && !(@tokens[-2] && @tokens[-2][1] == '.')
|
||||
@tokens[-1][0] = :PROTOTYPE_ACCESS if tag == :IDENTIFIER && last_value == '::'
|
||||
token(tag, identifier)
|
||||
@i += identifier.length
|
||||
end
|
||||
|
||||
# Matches numbers, including decimals, hex, and exponential notation.
|
||||
def number_token
|
||||
return false unless number = @chunk[NUMBER, 1]
|
||||
token(:NUMBER, number)
|
||||
@i += number.length
|
||||
end
|
||||
|
||||
# Matches strings, including multi-line strings.
|
||||
def string_token
|
||||
return false unless string = @chunk[STRING, 1]
|
||||
escaped = string.gsub(MULTILINER, " \\\n")
|
||||
token(:STRING, escaped)
|
||||
@line += string.count("\n")
|
||||
@i += string.length
|
||||
end
|
||||
|
||||
# Matches heredocs, adjusting indentation to the correct level.
|
||||
def heredoc_token
|
||||
return false unless match = @chunk.match(HEREDOC)
|
||||
indent = match[2] || match[5]
|
||||
doc = match[3] || match[6]
|
||||
doc.gsub!(/\n#{indent}/, "\\n")
|
||||
doc.gsub!('"', '\\"')
|
||||
token(:STRING, "\"#{doc}\"")
|
||||
@line += match[1].count("\n")
|
||||
@i += match[1].length
|
||||
end
|
||||
|
||||
# Matches interpolated JavaScript.
|
||||
def js_token
|
||||
return false unless script = @chunk[JS, 1]
|
||||
token(:JS, script.gsub(JS_CLEANER, ''))
|
||||
@i += script.length
|
||||
end
|
||||
|
||||
# Matches regular expression literals.
|
||||
def regex_token
|
||||
return false unless regex = @chunk[REGEX, 1]
|
||||
return false if NOT_REGEX.include?(last_tag)
|
||||
token(:REGEX, regex)
|
||||
@i += regex.length
|
||||
end
|
||||
|
||||
# Matches and consumes comments.
|
||||
def comment_token
|
||||
return false unless comment = @chunk[COMMENT, 1]
|
||||
@line += comment.scan(MULTILINER).length
|
||||
token(:COMMENT, comment.gsub(COMMENT_CLEANER, '').split(MULTILINER))
|
||||
token("\n", "\n")
|
||||
@i += comment.length
|
||||
end
|
||||
|
||||
# Record tokens for indentation differing from the previous line.
|
||||
def indent_token
|
||||
return false unless indent = @chunk[MULTI_DENT, 1]
|
||||
@line += indent.scan(MULTILINER).size
|
||||
@i += indent.size
|
||||
next_character = @chunk[MULTI_DENT, 4]
|
||||
no_newlines = next_character == '.' || (last_value.to_s.match(NO_NEWLINE) && last_value != "=>")
|
||||
return suppress_newlines(indent) if no_newlines
|
||||
size = indent.scan(LAST_DENT).last.last.length
|
||||
return newline_token(indent) if size == @indent
|
||||
if size > @indent
|
||||
token(:INDENT, size - @indent)
|
||||
@indents << (size - @indent)
|
||||
else
|
||||
outdent_token(@indent - size)
|
||||
end
|
||||
@indent = size
|
||||
end
|
||||
|
||||
# Record an oudent token or tokens, if we're moving back inwards past
|
||||
# multiple recorded indents.
|
||||
def outdent_token(move_out)
|
||||
while move_out > 0 && !@indents.empty?
|
||||
last_indent = @indents.pop
|
||||
token(:OUTDENT, last_indent)
|
||||
move_out -= last_indent
|
||||
end
|
||||
token("\n", "\n")
|
||||
end
|
||||
|
||||
# Matches and consumes non-meaningful whitespace.
|
||||
def whitespace_token
|
||||
return false unless whitespace = @chunk[WHITESPACE, 1]
|
||||
@i += whitespace.length
|
||||
end
|
||||
|
||||
# Multiple newlines get merged together.
|
||||
# Use a trailing \ to escape newlines.
|
||||
def newline_token(newlines)
|
||||
token("\n", "\n") unless last_value == "\n"
|
||||
true
|
||||
end
|
||||
|
||||
# Tokens to explicitly escape newlines are removed once their job is done.
|
||||
def suppress_newlines(newlines)
|
||||
@tokens.pop if last_value == "\\"
|
||||
true
|
||||
end
|
||||
|
||||
# We treat all other single characters as a token. Eg.: ( ) , . !
|
||||
# Multi-character operators are also literal tokens, so that Racc can assign
|
||||
# the proper order of operations.
|
||||
def literal_token
|
||||
value = @chunk[OPERATOR, 1]
|
||||
tag_parameters if value && value.match(CODE)
|
||||
value ||= @chunk[0,1]
|
||||
tag = value.match(ASSIGNMENT) ? :ASSIGN : value
|
||||
token(tag, value)
|
||||
@i += value.length
|
||||
end
|
||||
|
||||
# Helpers ==========================================================
|
||||
|
||||
# Add a token to the results, taking note of the line number, and
|
||||
# immediately-preceding comment.
|
||||
def token(tag, value)
|
||||
@tokens << [tag, Value.new(value, @line)]
|
||||
end
|
||||
|
||||
# Peek at the previous token's value.
|
||||
def last_value
|
||||
@tokens.last && @tokens.last[1]
|
||||
end
|
||||
|
||||
# Peek at the previous token's tag.
|
||||
def last_tag
|
||||
@tokens.last && @tokens.last[0]
|
||||
end
|
||||
|
||||
# A source of ambiguity in our grammar was parameter lists in function
|
||||
# definitions (as opposed to argument lists in function calls). Tag
|
||||
# parameter identifiers in order to avoid this. Also, parameter lists can
|
||||
# make use of splats.
|
||||
def tag_parameters
|
||||
i = 0
|
||||
loop do
|
||||
i -= 1
|
||||
tok = @tokens[i]
|
||||
return if !tok
|
||||
next if ['.', ','].include?(tok[0])
|
||||
return if tok[0] != :IDENTIFIER
|
||||
tok[0] = :PARAM
|
||||
end
|
||||
end
|
||||
|
||||
# Close up all remaining open blocks. IF the first token is an indent,
|
||||
# axe it.
|
||||
def close_indentation
|
||||
outdent_token(@indent)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -1,62 +0,0 @@
|
||||
# This (javascript) file is generated from lib/coffee_script/narwhal/coffee-script.coffee
|
||||
|
||||
# Executes the `coffee` Ruby program to convert from CoffeeScript
|
||||
# to Javascript. Eventually this will hopefully happen entirely within JS.
|
||||
|
||||
# Require external dependencies.
|
||||
OS: require('os')
|
||||
File: require('file')
|
||||
Readline: require('readline')
|
||||
|
||||
# The path to the CoffeeScript Compiler.
|
||||
coffeePath: File.path(module.path).dirname().dirname().dirname().dirname().dirname().join('bin', 'coffee')
|
||||
|
||||
# Our general-purpose error handler.
|
||||
checkForErrors: coffeeProcess =>
|
||||
return true if coffeeProcess.wait() is 0
|
||||
system.stderr.print(coffeeProcess.stderr.read())
|
||||
throw new Error("CoffeeScript compile error")
|
||||
|
||||
# Run a simple REPL, round-tripping to the CoffeeScript compiler for every
|
||||
# command.
|
||||
exports.run: args =>
|
||||
if args.length
|
||||
for path, i in args
|
||||
exports.evalCS(File.read(path))
|
||||
delete args[i]
|
||||
return true
|
||||
|
||||
while true
|
||||
try
|
||||
system.stdout.write('coffee> ').flush()
|
||||
result: exports.evalCS(Readline.readline(), ['--globals'])
|
||||
print(result) if result isnt undefined
|
||||
catch e
|
||||
print(e)
|
||||
|
||||
# Compile a given CoffeeScript file into JavaScript.
|
||||
exports.compileFile: path =>
|
||||
coffee: OS.popen([coffeePath, "--print", "--no-wrap", path])
|
||||
checkForErrors(coffee)
|
||||
coffee.stdout.read()
|
||||
|
||||
# Compile a string of CoffeeScript into JavaScript.
|
||||
exports.compile: source, flags =>
|
||||
coffee: OS.popen([coffeePath, "--eval", "--no-wrap"].concat(flags or []))
|
||||
coffee.stdin.write(source).flush().close()
|
||||
checkForErrors(coffee)
|
||||
coffee.stdout.read()
|
||||
|
||||
# Evaluating a string of CoffeeScript first compiles it externally.
|
||||
exports.evalCS: source, flags =>
|
||||
eval(exports.compile(source, flags))
|
||||
|
||||
# Make a factory for the CoffeeScript environment.
|
||||
exports.makeNarwhalFactory: path =>
|
||||
code: exports.compileFile(path)
|
||||
factoryText: "function(require,exports,module,system,print){" + code + "/**/\n}"
|
||||
if system.engine is "rhino"
|
||||
Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null)
|
||||
else
|
||||
# eval requires parentheses, but parentheses break compileFunction.
|
||||
eval("(" + factoryText + ")")
|
||||
@@ -1,77 +0,0 @@
|
||||
(function(){
|
||||
var File, OS, Readline, checkForErrors, coffeePath;
|
||||
// This (javascript) file is generated from lib/coffee_script/narwhal/coffee-script.coffee
|
||||
// Executes the `coffee` Ruby program to convert from CoffeeScript
|
||||
// to Javascript. Eventually this will hopefully happen entirely within JS.
|
||||
// Require external dependencies.
|
||||
OS = require('os');
|
||||
File = require('file');
|
||||
Readline = require('readline');
|
||||
// The path to the CoffeeScript Compiler.
|
||||
coffeePath = File.path(module.path).dirname().dirname().dirname().dirname().dirname().join('bin', 'coffee');
|
||||
// Our general-purpose error handler.
|
||||
checkForErrors = function checkForErrors(coffeeProcess) {
|
||||
if (coffeeProcess.wait() === 0) {
|
||||
return true;
|
||||
}
|
||||
system.stderr.print(coffeeProcess.stderr.read());
|
||||
throw new Error("CoffeeScript compile error");
|
||||
};
|
||||
// Run a simple REPL, round-tripping to the CoffeeScript compiler for every
|
||||
// command.
|
||||
exports.run = function run(args) {
|
||||
var __a, i, path, result;
|
||||
if (args.length) {
|
||||
__a = args;
|
||||
for (i=0; i<__a.length; i++) {
|
||||
path = __a[i];
|
||||
exports.evalCS(File.read(path));
|
||||
delete args[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
while (true) {
|
||||
try {
|
||||
system.stdout.write('coffee> ').flush();
|
||||
result = exports.evalCS(Readline.readline(), ['--globals']);
|
||||
if (result !== undefined) {
|
||||
print(result);
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
// Compile a given CoffeeScript file into JavaScript.
|
||||
exports.compileFile = function compileFile(path) {
|
||||
var coffee;
|
||||
coffee = OS.popen([coffeePath, "--print", "--no-wrap", path]);
|
||||
checkForErrors(coffee);
|
||||
return coffee.stdout.read();
|
||||
};
|
||||
// Compile a string of CoffeeScript into JavaScript.
|
||||
exports.compile = function compile(source, flags) {
|
||||
var coffee;
|
||||
coffee = OS.popen([coffeePath, "--eval", "--no-wrap"].concat(flags || []));
|
||||
coffee.stdin.write(source).flush().close();
|
||||
checkForErrors(coffee);
|
||||
return coffee.stdout.read();
|
||||
};
|
||||
// Evaluating a string of CoffeeScript first compiles it externally.
|
||||
exports.evalCS = function evalCS(source, flags) {
|
||||
return eval(exports.compile(source, flags));
|
||||
};
|
||||
// Make a factory for the CoffeeScript environment.
|
||||
exports.makeNarwhalFactory = function makeNarwhalFactory(path) {
|
||||
var code, factoryText;
|
||||
code = exports.compileFile(path);
|
||||
factoryText = "function(require,exports,module,system,print){" + code + "/**/\n}";
|
||||
if (system.engine === "rhino") {
|
||||
return Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null);
|
||||
} else {
|
||||
// eval requires parentheses, but parentheses break compileFunction.
|
||||
return eval("(" + factoryText + ")");
|
||||
}
|
||||
};
|
||||
})();
|
||||
@@ -1,21 +0,0 @@
|
||||
(function(){
|
||||
var coffeescript, factories, loader;
|
||||
// This (javascript) file is generated from lib/coffee_script/narwhal/loader.coffee
|
||||
coffeescript = null;
|
||||
factories = {
|
||||
};
|
||||
loader = {
|
||||
// Reload the coffee-script environment from source.
|
||||
reload: function reload(topId, path) {
|
||||
coffeescript = coffeescript || require('coffee-script');
|
||||
return factories[topId] = function() {
|
||||
return coffeescript.makeNarwhalFactory(path);
|
||||
};
|
||||
},
|
||||
// Ensure that the coffee-script environment is loaded.
|
||||
load: function load(topId, path) {
|
||||
return factories[topId] = factories[topId] || this.reload(topId, path);
|
||||
}
|
||||
};
|
||||
require.loader.loaders.unshift([".coffee", loader]);
|
||||
})();
|
||||
@@ -1,19 +0,0 @@
|
||||
# This (javascript) file is generated from lib/coffee_script/narwhal/loader.coffee
|
||||
|
||||
coffeescript: null
|
||||
factories: {}
|
||||
|
||||
loader: {
|
||||
|
||||
# Reload the coffee-script environment from source.
|
||||
reload: topId, path =>
|
||||
coffeescript ||= require('coffee-script')
|
||||
factories[topId]: => coffeescript.makeNarwhalFactory(path)
|
||||
|
||||
# Ensure that the coffee-script environment is loaded.
|
||||
load: topId, path =>
|
||||
factories[topId] ||= this.reload(topId, path)
|
||||
|
||||
}
|
||||
|
||||
require.loader.loaders.unshift([".coffee", loader])
|
||||
@@ -1,895 +0,0 @@
|
||||
module CoffeeScript
|
||||
|
||||
# The abstract base class for all CoffeeScript nodes.
|
||||
# All nodes are implement a "compile_node" method, which performs the
|
||||
# code generation for that node. To compile a node, call the "compile"
|
||||
# method, which wraps "compile_node" in some extra smarts, to know when the
|
||||
# generated code should be wrapped up in a closure. An options hash is passed
|
||||
# and cloned throughout, containing messages from higher in the AST,
|
||||
# information about the current scope, and indentation level.
|
||||
class Node
|
||||
# Tabs are two spaces for pretty-printing.
|
||||
TAB = ' '
|
||||
|
||||
# Tag this node as a statement, meaning that it can't be used directly as
|
||||
# the result of an expression.
|
||||
def self.statement
|
||||
class_eval "def statement?; true; end"
|
||||
end
|
||||
|
||||
# Tag this node as a statement that cannot be transformed into an expression.
|
||||
# (break, continue, etc.) It doesn't make sense to try to transform it.
|
||||
def self.statement_only
|
||||
statement
|
||||
class_eval "def statement_only?; true; end"
|
||||
end
|
||||
|
||||
def write(code)
|
||||
puts "#{self.class.to_s}:\n#{@options.inspect}\n#{code}\n\n" if ENV['VERBOSE']
|
||||
code
|
||||
end
|
||||
|
||||
# This is extremely important -- we convert JS statements into expressions
|
||||
# by wrapping them in a closure, only if it's possible, and we're not at
|
||||
# the top level of a block (which would be unnecessary), and we haven't
|
||||
# already been asked to return the result.
|
||||
def compile(o={})
|
||||
@options = o.dup
|
||||
@indent = o[:indent]
|
||||
top = self.is_a?(ForNode) ? @options[:top] : @options.delete(:top)
|
||||
closure = statement? && !statement_only? && !top && !@options[:return]
|
||||
closure ? compile_closure(@options) : compile_node(@options)
|
||||
end
|
||||
|
||||
def compile_closure(o={})
|
||||
indent = o[:indent]
|
||||
@indent = (o[:indent] = idt(1))
|
||||
"(function() {\n#{compile_node(o.merge(:return => true))}\n#{indent}})()"
|
||||
end
|
||||
|
||||
# Quick short method for the current indentation level, plus tabbing in.
|
||||
def idt(tabs=0)
|
||||
@indent + (TAB * tabs)
|
||||
end
|
||||
|
||||
# Default implementations of the common node methods.
|
||||
def unwrap; self; end
|
||||
def statement?; false; end
|
||||
def statement_only?; false; end
|
||||
end
|
||||
|
||||
# A collection of nodes, each one representing an expression.
|
||||
class Expressions < Node
|
||||
statement
|
||||
attr_reader :expressions
|
||||
|
||||
TRAILING_WHITESPACE = /\s+$/
|
||||
UPPERCASE = /[A-Z]/
|
||||
|
||||
# Wrap up a node as an Expressions, unless it already is.
|
||||
def self.wrap(*nodes)
|
||||
return nodes[0] if nodes.length == 1 && nodes[0].is_a?(Expressions)
|
||||
Expressions.new(*nodes)
|
||||
end
|
||||
|
||||
def initialize(*nodes)
|
||||
@expressions = nodes.flatten
|
||||
end
|
||||
|
||||
# Tack an expression on to the end of this expression list.
|
||||
def <<(node)
|
||||
@expressions << node
|
||||
self
|
||||
end
|
||||
|
||||
# Tack an expression on to the beginning of this expression list.
|
||||
def unshift(node)
|
||||
@expressions.unshift(node)
|
||||
self
|
||||
end
|
||||
|
||||
# If this Expressions consists of a single node, pull it back out.
|
||||
def unwrap
|
||||
@expressions.length == 1 ? @expressions.first : self
|
||||
end
|
||||
|
||||
# Is the node last in this block of expressions.
|
||||
def last?(node)
|
||||
@last_index ||= @expressions.last.is_a?(CommentNode) ? -2 : -1
|
||||
node == @expressions[@last_index]
|
||||
end
|
||||
|
||||
# Determine if this is the expressions body within a constructor function.
|
||||
# Constructors are capitalized by CoffeeScript convention.
|
||||
def constructor?(o)
|
||||
o[:top] && o[:last_assign] && o[:last_assign][0..0][UPPERCASE]
|
||||
end
|
||||
|
||||
def compile(o={})
|
||||
o[:scope] ? super(o) : compile_root(o)
|
||||
end
|
||||
|
||||
# Compile each expression in the Expressions body.
|
||||
def compile_node(options={})
|
||||
write(@expressions.map {|n| compile_expression(n, options.dup) }.join("\n"))
|
||||
end
|
||||
|
||||
# If this is the top-level Expressions, wrap everything in a safety closure.
|
||||
def compile_root(o={})
|
||||
indent = o[:no_wrap] ? '' : TAB
|
||||
@indent = indent
|
||||
o.merge!(:indent => indent, :scope => Scope.new(nil, self))
|
||||
code = o[:globals] ? compile_node(o) : compile_with_declarations(o)
|
||||
code.gsub!(TRAILING_WHITESPACE, '')
|
||||
write(o[:no_wrap] ? code : "(function(){\n#{code}\n})();")
|
||||
end
|
||||
|
||||
# Compile the expressions body, with declarations of all inner variables
|
||||
# at the top.
|
||||
def compile_with_declarations(o={})
|
||||
code = compile_node(o)
|
||||
return code unless o[:scope].declarations?(self)
|
||||
write("#{idt}var #{o[:scope].declared_variables.join(', ')};\n#{code}")
|
||||
end
|
||||
|
||||
# Compiles a single expression within the expression list.
|
||||
def compile_expression(node, o)
|
||||
@indent = o[:indent]
|
||||
stmt = node.statement?
|
||||
# We need to return the result if this is the last node in the expressions body.
|
||||
returns = o.delete(:return) && last?(node) && !node.statement_only?
|
||||
# Return the regular compile of the node, unless we need to return the result.
|
||||
return "#{stmt ? '' : idt}#{node.compile(o.merge(:top => true))}#{stmt ? '' : ';'}" unless returns
|
||||
# If it's a statement, the node knows how to return itself.
|
||||
return node.compile(o.merge(:return => true)) if node.statement?
|
||||
# If it's not part of a constructor, we can just return the value of the expression.
|
||||
return "#{idt}return #{node.compile(o)};" unless constructor?(o)
|
||||
# It's the last line of a constructor, add a safety check.
|
||||
temp = o[:scope].free_variable
|
||||
"#{idt}#{temp} = #{node.compile(o)};\n#{idt}return #{o[:last_assign]} === this.constructor ? this : #{temp};"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# Literals are static values that have a Ruby representation, eg.: a string, a number,
|
||||
# true, false, nil, etc.
|
||||
class LiteralNode < Node
|
||||
|
||||
# Values of a literal node that much be treated as a statement -- no
|
||||
# sense returning or assigning them.
|
||||
STATEMENTS = ['break', 'continue']
|
||||
|
||||
# If we get handed a literal reference to an arguments object, convert
|
||||
# it to an array.
|
||||
ARG_ARRAY = 'Array.prototype.slice.call(arguments, 0)'
|
||||
|
||||
attr_reader :value
|
||||
|
||||
# Wrap up a compiler-generated string as a LiteralNode.
|
||||
def self.wrap(string)
|
||||
self.new(Value.new(string))
|
||||
end
|
||||
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
|
||||
def statement?
|
||||
STATEMENTS.include?(@value.to_s)
|
||||
end
|
||||
alias_method :statement_only?, :statement?
|
||||
|
||||
def compile_node(o)
|
||||
@value = ARG_ARRAY if @value.to_s.to_sym == :arguments
|
||||
indent = statement? ? idt : ''
|
||||
ending = statement? ? ';' : ''
|
||||
write "#{indent}#{@value}#{ending}"
|
||||
end
|
||||
end
|
||||
|
||||
# Return an expression, or wrap it in a closure and return it.
|
||||
class ReturnNode < Node
|
||||
statement_only
|
||||
|
||||
attr_reader :expression
|
||||
|
||||
def initialize(expression)
|
||||
@expression = expression
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
return write(@expression.compile(o.merge(:return => true))) if @expression.statement?
|
||||
compiled = @expression.compile(o)
|
||||
write(@expression.statement? ? "#{compiled}\n#{idt}return null;" : "#{idt}return #{compiled};")
|
||||
end
|
||||
end
|
||||
|
||||
# Pass through CoffeeScript comments into JavaScript comments at the
|
||||
# same position.
|
||||
class CommentNode < Node
|
||||
statement_only
|
||||
|
||||
def initialize(lines)
|
||||
@lines = lines.value
|
||||
end
|
||||
|
||||
def compile_node(o={})
|
||||
delimiter = "\n#{idt}//"
|
||||
write("#{delimiter}#{@lines.join(delimiter)}")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# Node for a function invocation. Takes care of converting super() calls into
|
||||
# calls against the prototype's function of the same name.
|
||||
class CallNode < Node
|
||||
attr_reader :variable, :arguments
|
||||
|
||||
def initialize(variable, arguments=[])
|
||||
@variable, @arguments = variable, arguments
|
||||
end
|
||||
|
||||
def new_instance
|
||||
@new = true
|
||||
self
|
||||
end
|
||||
|
||||
def super?
|
||||
@variable == :super
|
||||
end
|
||||
|
||||
def prefix
|
||||
@new ? "new " : ''
|
||||
end
|
||||
|
||||
def splat?
|
||||
@arguments.any? {|a| a.is_a?(ArgSplatNode) }
|
||||
end
|
||||
|
||||
def <<(argument)
|
||||
@arguments << argument
|
||||
end
|
||||
|
||||
# Compile a vanilla function call.
|
||||
def compile_node(o)
|
||||
return write(compile_splat(o)) if splat?
|
||||
args = @arguments.map{|a| a.compile(o) }.join(', ')
|
||||
return write(compile_super(args, o)) if super?
|
||||
write("#{prefix}#{@variable.compile(o)}(#{args})")
|
||||
end
|
||||
|
||||
# Compile a call against the superclass's implementation of the current function.
|
||||
def compile_super(args, o)
|
||||
methname = o[:last_assign]
|
||||
arg_part = args.empty? ? '' : ", #{args}"
|
||||
meth = o[:proto_assign] ? "#{o[:proto_assign]}.__superClass__.#{methname}" :
|
||||
"#{methname}.__superClass__.constructor"
|
||||
"#{meth}.call(this#{arg_part})"
|
||||
end
|
||||
|
||||
# Compile a function call being passed variable arguments.
|
||||
def compile_splat(o)
|
||||
meth = @variable.compile(o)
|
||||
obj = @variable.source || 'this'
|
||||
args = @arguments.map do |arg|
|
||||
code = arg.compile(o)
|
||||
code = arg.is_a?(ArgSplatNode) ? code : "[#{code}]"
|
||||
arg.equal?(@arguments.first) ? code : ".concat(#{code})"
|
||||
end
|
||||
"#{prefix}#{meth}.apply(#{obj}, #{args.join('')})"
|
||||
end
|
||||
end
|
||||
|
||||
# Node to extend an object's prototype with an ancestor object.
|
||||
# After goog.inherits from the Closure Library.
|
||||
class ExtendsNode < Node
|
||||
statement
|
||||
attr_reader :sub_object, :super_object
|
||||
|
||||
def initialize(sub_object, super_object)
|
||||
@sub_object, @super_object = sub_object, super_object
|
||||
end
|
||||
|
||||
# Hooking one constructor into another's prototype chain.
|
||||
def compile_node(o={})
|
||||
constructor = o[:scope].free_variable
|
||||
sub, sup = @sub_object.compile(o), @super_object.compile(o)
|
||||
"#{idt}#{constructor} = function(){};\n#{idt}" +
|
||||
"#{constructor}.prototype = #{sup}.prototype;\n#{idt}" +
|
||||
"#{sub}.__superClass__ = #{sup}.prototype;\n#{idt}" +
|
||||
"#{sub}.prototype = new #{constructor}();\n#{idt}" +
|
||||
"#{sub}.prototype.constructor = #{sub};"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# A value, indexed or dotted into, or vanilla.
|
||||
class ValueNode < Node
|
||||
attr_reader :base, :properties, :last, :source
|
||||
|
||||
def initialize(base, properties=[])
|
||||
@base, @properties = base, properties
|
||||
end
|
||||
|
||||
def <<(other)
|
||||
@properties << other
|
||||
self
|
||||
end
|
||||
|
||||
def properties?
|
||||
return !@properties.empty?
|
||||
end
|
||||
|
||||
def array?
|
||||
@base.is_a?(ArrayNode) && !properties?
|
||||
end
|
||||
|
||||
def object?
|
||||
@base.is_a?(ObjectNode) && !properties?
|
||||
end
|
||||
|
||||
def splice?
|
||||
properties? && @properties.last.is_a?(SliceNode)
|
||||
end
|
||||
|
||||
# Values are statements if their base is a statement.
|
||||
def statement?
|
||||
@base.is_a?(Node) && @base.statement? && !properties?
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
only = o.delete(:only_first)
|
||||
props = only ? @properties[0...-1] : @properties
|
||||
parts = [@base, props].flatten.map {|val| val.compile(o) }
|
||||
@last = parts.last
|
||||
@source = parts.length > 1 ? parts[0...-1].join('') : nil
|
||||
write(parts.join(''))
|
||||
end
|
||||
end
|
||||
|
||||
# A dotted accessor into a part of a value, or the :: shorthand for
|
||||
# an accessor into the object's prototype.
|
||||
class AccessorNode < Node
|
||||
attr_reader :name
|
||||
|
||||
def initialize(name, prototype=false)
|
||||
@name, @prototype = name, prototype
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
proto = @prototype ? "prototype." : ''
|
||||
write(".#{proto}#{@name}")
|
||||
end
|
||||
end
|
||||
|
||||
# An indexed accessor into a part of an array or object.
|
||||
class IndexNode < Node
|
||||
attr_reader :index
|
||||
|
||||
def initialize(index)
|
||||
@index = index
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
write("[#{@index.compile(o)}]")
|
||||
end
|
||||
end
|
||||
|
||||
# A range literal. Ranges can be used to extract portions (slices) of arrays,
|
||||
# or to specify a range for array comprehensions.
|
||||
class RangeNode < Node
|
||||
attr_reader :from, :to
|
||||
|
||||
def initialize(from, to, exclusive=false)
|
||||
@from, @to, @exclusive = from, to, exclusive
|
||||
end
|
||||
|
||||
def exclusive?
|
||||
@exclusive
|
||||
end
|
||||
|
||||
def compile_variables(o)
|
||||
@indent = o[:indent]
|
||||
@from_var, @to_var = o[:scope].free_variable, o[:scope].free_variable
|
||||
from_val, to_val = @from.compile(o), @to.compile(o)
|
||||
write("#{@from_var} = #{from_val}; #{@to_var} = #{to_val};\n#{idt}")
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
return compile_array(o) unless o[:index]
|
||||
idx, step = o.delete(:index), o.delete(:step)
|
||||
vars = "#{idx}=#{@from_var}"
|
||||
step = step ? step.compile(o) : '1'
|
||||
equals = @exclusive ? '' : '='
|
||||
compare = "(#{@from_var} <= #{@to_var} ? #{idx} <#{equals} #{@to_var} : #{idx} >#{equals} #{@to_var})"
|
||||
incr = "(#{@from_var} <= #{@to_var} ? #{idx} += #{step} : #{idx} -= #{step})"
|
||||
write("#{vars}; #{compare}; #{incr}")
|
||||
end
|
||||
|
||||
# Expand the range into the equivalent array, if it's not being used as
|
||||
# part of a comprehension, slice, or splice.
|
||||
# TODO: This generates pretty ugly code ... shrink it.
|
||||
def compile_array(o)
|
||||
body = Expressions.wrap(LiteralNode.wrap('i'))
|
||||
arr = Expressions.wrap(ForNode.new(body, {:source => ValueNode.new(self)}, Value.new('i')))
|
||||
ParentheticalNode.new(CallNode.new(CodeNode.new([], arr))).compile(o)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# An array slice literal. Unlike JavaScript's Array#slice, the second parameter
|
||||
# specifies the index of the end of the slice (just like the first parameter)
|
||||
# is the index of the beginning.
|
||||
class SliceNode < Node
|
||||
attr_reader :range
|
||||
|
||||
def initialize(range)
|
||||
@range = range
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
from = @range.from.compile(o)
|
||||
to = @range.to.compile(o)
|
||||
plus_part = @range.exclusive? ? '' : ' + 1'
|
||||
write(".slice(#{from}, #{to}#{plus_part})")
|
||||
end
|
||||
end
|
||||
|
||||
# Setting the value of a local variable, or the value of an object property.
|
||||
class AssignNode < Node
|
||||
PROTO_ASSIGN = /\A(\S+)\.prototype/
|
||||
LEADING_DOT = /\A\.(prototype\.)?/
|
||||
|
||||
attr_reader :variable, :value, :context
|
||||
|
||||
def initialize(variable, value, context=nil)
|
||||
@variable, @value, @context = variable, value, context
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
return compile_pattern_match(o) if @variable.array? || @variable.object?
|
||||
return compile_splice(o) if @variable.splice?
|
||||
stmt = o.delete(:as_statement)
|
||||
name = @variable.compile(o)
|
||||
last = @variable.last.to_s.sub(LEADING_DOT, '')
|
||||
proto = name[PROTO_ASSIGN, 1]
|
||||
o = o.merge(:last_assign => last, :proto_assign => proto)
|
||||
o[:immediate_assign] = last if @value.is_a?(CodeNode) && last.match(Lexer::IDENTIFIER)
|
||||
return write("#{name}: #{@value.compile(o)}") if @context == :object
|
||||
o[:scope].find(name) unless @variable.properties?
|
||||
val = "#{name} = #{@value.compile(o)}"
|
||||
return write("#{idt}#{val};") if stmt
|
||||
write(o[:return] ? "#{idt}return (#{val})" : val)
|
||||
end
|
||||
|
||||
def statement?
|
||||
@variable.array? || @variable.object?
|
||||
end
|
||||
|
||||
# Implementation of recursive pattern matching, when assigning array or
|
||||
# object literals to a value. Peeks at their properties to assign inner names.
|
||||
# See: http://wiki.ecmascript.org/doku.php?id=harmony:destructuring
|
||||
def compile_pattern_match(o)
|
||||
val_var = o[:scope].free_variable
|
||||
assigns = ["#{idt}#{val_var} = #{@value.compile(o)};"]
|
||||
o.merge!(:top => true, :as_statement => true)
|
||||
@variable.base.objects.each_with_index do |obj, i|
|
||||
obj, i = obj.value, obj.variable.base if @variable.object?
|
||||
access_class = @variable.array? ? IndexNode : AccessorNode
|
||||
assigns << AssignNode.new(
|
||||
obj, ValueNode.new(Value.new(val_var), [access_class.new(Value.new(i.to_s))])
|
||||
).compile(o)
|
||||
end
|
||||
write(assigns.join("\n"))
|
||||
end
|
||||
|
||||
def compile_splice(o)
|
||||
var = @variable.compile(o.merge(:only_first => true))
|
||||
range = @variable.properties.last.range
|
||||
plus = range.exclusive? ? '' : ' + 1'
|
||||
from = range.from.compile(o)
|
||||
to = "#{range.to.compile(o)} - #{from}#{plus}"
|
||||
write("#{var}.splice.apply(#{var}, [#{from}, #{to}].concat(#{@value.compile(o)}))")
|
||||
end
|
||||
end
|
||||
|
||||
# Simple Arithmetic and logical operations. Performs some conversion from
|
||||
# CoffeeScript operations into their JavaScript equivalents.
|
||||
class OpNode < Node
|
||||
CONVERSIONS = {
|
||||
:== => "===",
|
||||
:'!=' => "!==",
|
||||
:and => '&&',
|
||||
:or => '||',
|
||||
:is => '===',
|
||||
:isnt => "!==",
|
||||
:not => '!'
|
||||
}
|
||||
CONDITIONALS = [:'||=', :'&&=']
|
||||
PREFIX_OPERATORS = [:typeof, :delete]
|
||||
|
||||
attr_reader :operator, :first, :second
|
||||
|
||||
def initialize(operator, first, second=nil, flip=false)
|
||||
@first, @second, @flip = first, second, flip
|
||||
@operator = CONVERSIONS[operator.to_sym] || operator
|
||||
end
|
||||
|
||||
def unary?
|
||||
@second.nil?
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
return write(compile_conditional(o)) if CONDITIONALS.include?(@operator.to_sym)
|
||||
return write(compile_unary(o)) if unary?
|
||||
write("#{@first.compile(o)} #{@operator} #{@second.compile(o)}")
|
||||
end
|
||||
|
||||
def compile_conditional(o)
|
||||
first, second = @first.compile(o), @second.compile(o)
|
||||
sym = @operator[0..1]
|
||||
"#{first} = #{first} #{sym} #{second}"
|
||||
end
|
||||
|
||||
def compile_unary(o)
|
||||
space = PREFIX_OPERATORS.include?(@operator.to_sym) ? ' ' : ''
|
||||
parts = [@operator.to_s, space, @first.compile(o)]
|
||||
parts.reverse! if @flip
|
||||
parts.join('')
|
||||
end
|
||||
end
|
||||
|
||||
# A function definition. The only node that creates a new Scope.
|
||||
class CodeNode < Node
|
||||
attr_reader :params, :body
|
||||
|
||||
def initialize(params, body)
|
||||
@params = params
|
||||
@body = body
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
shared_scope = o.delete(:shared_scope)
|
||||
o[:scope] = shared_scope || Scope.new(o[:scope], @body)
|
||||
o[:return] = true
|
||||
o[:top] = true
|
||||
o[:indent] = idt(1)
|
||||
o.delete(:no_wrap)
|
||||
o.delete(:globals)
|
||||
name = o.delete(:immediate_assign)
|
||||
if @params.last.is_a?(ParamSplatNode)
|
||||
splat = @params.pop
|
||||
splat.index = @params.length
|
||||
@body.unshift(splat)
|
||||
end
|
||||
@params.each {|id| o[:scope].parameter(id.to_s) }
|
||||
code = @body.compile_with_declarations(o)
|
||||
name_part = name ? " #{name}" : ''
|
||||
write("function#{name_part}(#{@params.join(', ')}) {\n#{code}\n#{idt}}")
|
||||
end
|
||||
end
|
||||
|
||||
# A parameter splat in a function definition.
|
||||
class ParamSplatNode < Node
|
||||
attr_accessor :index
|
||||
attr_reader :name
|
||||
|
||||
def initialize(name)
|
||||
@name = name
|
||||
end
|
||||
|
||||
def compile_node(o={})
|
||||
o[:scope].find(@name)
|
||||
write("#{@name} = Array.prototype.slice.call(arguments, #{@index})")
|
||||
end
|
||||
end
|
||||
|
||||
class ArgSplatNode < Node
|
||||
attr_reader :value
|
||||
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
|
||||
def compile_node(o={})
|
||||
write(@value.compile(o))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# An object literal.
|
||||
class ObjectNode < Node
|
||||
attr_reader :properties
|
||||
alias_method :objects, :properties
|
||||
|
||||
def initialize(properties = [])
|
||||
@properties = properties
|
||||
end
|
||||
|
||||
# All the mucking about with commas is to make sure that CommentNodes and
|
||||
# AssignNodes get interleaved correctly, with no trailing commas or
|
||||
# commas affixed to comments. TODO: Extract this and add it to ArrayNode.
|
||||
def compile_node(o)
|
||||
o[:indent] = idt(1)
|
||||
joins = Hash.new("\n")
|
||||
non_comments = @properties.select {|p| !p.is_a?(CommentNode) }
|
||||
non_comments.each {|p| joins[p] = p == non_comments.last ? "\n" : ",\n" }
|
||||
props = @properties.map { |prop|
|
||||
join = joins[prop]
|
||||
join = '' if prop == @properties.last
|
||||
indent = prop.is_a?(CommentNode) ? '' : idt(1)
|
||||
"#{indent}#{prop.compile(o)}#{join}"
|
||||
}.join('')
|
||||
write("{\n#{props}\n#{idt}}")
|
||||
end
|
||||
end
|
||||
|
||||
# An array literal.
|
||||
class ArrayNode < Node
|
||||
attr_reader :objects
|
||||
|
||||
def initialize(objects=[])
|
||||
@objects = objects
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
o[:indent] = idt(1)
|
||||
objects = @objects.map { |obj|
|
||||
code = obj.compile(o)
|
||||
obj.is_a?(CommentNode) ? "\n#{code}\n#{o[:indent]}" :
|
||||
obj == @objects.last ? code : "#{code}, "
|
||||
}.join('')
|
||||
ending = objects.include?("\n") ? "\n#{idt}]" : ']'
|
||||
write("[#{objects}#{ending}")
|
||||
end
|
||||
end
|
||||
|
||||
# A while loop, the only sort of low-level loop exposed by CoffeeScript. From
|
||||
# it, all other loops can be manufactured.
|
||||
class WhileNode < Node
|
||||
statement
|
||||
|
||||
attr_reader :condition, :body
|
||||
|
||||
def initialize(condition, body)
|
||||
@condition, @body = condition, body
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
returns = o.delete(:return)
|
||||
o[:indent] = idt(1)
|
||||
o[:top] = true
|
||||
cond = @condition.compile(o)
|
||||
post = returns ? "\n#{idt}return null;" : ''
|
||||
return write("#{idt}while (#{cond}) null;#{post}") if @body.nil?
|
||||
write("#{idt}while (#{cond}) {\n#{@body.compile(o)}\n#{idt}}#{post}")
|
||||
end
|
||||
end
|
||||
|
||||
# The replacement for the for loop is an array comprehension (that compiles)
|
||||
# into a for loop. Also acts as an expression, able to return the result
|
||||
# of the comprehenion. Unlike Python array comprehensions, it's able to pass
|
||||
# the current index of the loop as a second parameter.
|
||||
class ForNode < Node
|
||||
statement
|
||||
|
||||
attr_reader :body, :source, :name, :index, :filter, :step
|
||||
|
||||
def initialize(body, source, name, index=nil)
|
||||
@body, @name, @index = body, name, index
|
||||
@source = source[:source]
|
||||
@filter = source[:filter]
|
||||
@step = source[:step]
|
||||
@object = !!source[:object]
|
||||
@name, @index = @index, @name if @object
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
top_level = o.delete(:top) && !o[:return]
|
||||
range = @source.is_a?(ValueNode) && @source.base.is_a?(RangeNode) && @source.properties.empty?
|
||||
source = range ? @source.base : @source
|
||||
scope = o[:scope]
|
||||
name_found = @name && scope.find(@name)
|
||||
index_found = @index && scope.find(@index)
|
||||
body_dent = idt(1)
|
||||
rvar = scope.free_variable unless top_level
|
||||
svar = scope.free_variable
|
||||
ivar = range ? name : @index ? @index : scope.free_variable
|
||||
if range
|
||||
index_var = scope.free_variable
|
||||
source_part = source.compile_variables(o)
|
||||
for_part = "#{index_var}=0, #{source.compile(o.merge(:index => ivar, :step => @step))}, #{index_var}++"
|
||||
var_part = ''
|
||||
else
|
||||
index_var = nil
|
||||
source_part = "#{svar} = #{source.compile(o)};\n#{idt}"
|
||||
for_part = @object ? "#{ivar} in #{svar}" : "#{ivar}=0; #{ivar}<#{svar}.length; #{ivar}++"
|
||||
var_part = @name ? "#{body_dent}#{@name} = #{svar}[#{ivar}];\n" : ''
|
||||
end
|
||||
body = @body
|
||||
set_result = rvar ? "#{idt}#{rvar} = []; " : idt
|
||||
return_result = rvar || ''
|
||||
if top_level
|
||||
body = Expressions.wrap(body)
|
||||
else
|
||||
body = Expressions.wrap(CallNode.new(
|
||||
ValueNode.new(LiteralNode.new(rvar), [AccessorNode.new('push')]), [body.unwrap]
|
||||
))
|
||||
end
|
||||
if o[:return]
|
||||
return_result = "return #{return_result}" if o[:return]
|
||||
o.delete(:return)
|
||||
body = IfNode.new(@filter, body, nil, :statement => true) if @filter
|
||||
elsif @filter
|
||||
body = Expressions.wrap(IfNode.new(@filter, body))
|
||||
end
|
||||
if @object
|
||||
body = Expressions.wrap(IfNode.new(
|
||||
CallNode.new(ValueNode.new(LiteralNode.wrap(svar), [AccessorNode.new(Value.new('hasOwnProperty'))]), [LiteralNode.wrap(ivar)]),
|
||||
Expressions.wrap(body),
|
||||
nil,
|
||||
{:statement => true}
|
||||
))
|
||||
end
|
||||
|
||||
return_result = "\n#{idt}#{return_result};" unless top_level
|
||||
body = body.compile(o.merge(:indent => body_dent, :top => true))
|
||||
vars = range ? @name : "#{@name}, #{ivar}"
|
||||
return write(set_result + source_part + "for (#{for_part}) {\n#{var_part}#{body}\n#{idt}}\n#{idt}#{return_result}")
|
||||
end
|
||||
end
|
||||
|
||||
# A try/catch/finally block.
|
||||
class TryNode < Node
|
||||
statement
|
||||
|
||||
attr_reader :try, :error, :recovery, :finally
|
||||
|
||||
def initialize(try, error, recovery, finally=nil)
|
||||
@try, @error, @recovery, @finally = try, error, recovery, finally
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
o[:indent] = idt(1)
|
||||
o[:top] = true
|
||||
error_part = @error ? " (#{@error}) " : ' '
|
||||
catch_part = @recovery && " catch#{error_part}{\n#{@recovery.compile(o)}\n#{idt}}"
|
||||
finally_part = @finally && " finally {\n#{@finally.compile(o.merge(:return => nil))}\n#{idt}}"
|
||||
write("#{idt}try {\n#{@try.compile(o)}\n#{idt}}#{catch_part}#{finally_part}")
|
||||
end
|
||||
end
|
||||
|
||||
# Throw an exception.
|
||||
class ThrowNode < Node
|
||||
statement_only
|
||||
|
||||
attr_reader :expression
|
||||
|
||||
def initialize(expression)
|
||||
@expression = expression
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
write("#{idt}throw #{@expression.compile(o)};")
|
||||
end
|
||||
end
|
||||
|
||||
# Check an expression for existence (meaning not null or undefined).
|
||||
class ExistenceNode < Node
|
||||
attr_reader :expression
|
||||
|
||||
def initialize(expression)
|
||||
@expression = expression
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
val = @expression.compile(o)
|
||||
write("(typeof #{val} !== \"undefined\" && #{val} !== null)")
|
||||
end
|
||||
end
|
||||
|
||||
# An extra set of parentheses, supplied by the script source.
|
||||
# You can't wrap parentheses around bits that get compiled into JS statements,
|
||||
# unfortunately.
|
||||
class ParentheticalNode < Node
|
||||
attr_reader :expressions
|
||||
|
||||
def initialize(expressions, line=nil)
|
||||
@expressions = expressions.unwrap
|
||||
@line = line
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
compiled = @expressions.compile(o)
|
||||
compiled = compiled[0...-1] if compiled[-1..-1] == ';'
|
||||
write("(#{compiled})")
|
||||
end
|
||||
end
|
||||
|
||||
# If/else statements. Switch/whens get compiled into these. Acts as an
|
||||
# expression by pushing down requested returns to the expression bodies.
|
||||
# Single-expression IfNodes are compiled into ternary operators if possible,
|
||||
# because ternaries are first-class returnable assignable expressions.
|
||||
class IfNode < Node
|
||||
attr_reader :condition, :body, :else_body
|
||||
|
||||
def initialize(condition, body, else_body=nil, tags={})
|
||||
@condition = condition
|
||||
@body = body && body.unwrap
|
||||
@else_body = else_body && else_body.unwrap
|
||||
@tags = tags
|
||||
@condition = OpNode.new("!", ParentheticalNode.new(@condition)) if @tags[:invert]
|
||||
end
|
||||
|
||||
def <<(else_body)
|
||||
eb = else_body.unwrap
|
||||
@else_body ? @else_body << eb : @else_body = eb
|
||||
self
|
||||
end
|
||||
|
||||
def add_comment(comment)
|
||||
@comment = comment
|
||||
self
|
||||
end
|
||||
|
||||
def force_statement
|
||||
@tags[:statement] = true
|
||||
self
|
||||
end
|
||||
|
||||
# Rewrite a chain of IfNodes with their switch condition for equality.
|
||||
def rewrite_condition(expression)
|
||||
@condition = OpNode.new("is", expression, @condition)
|
||||
@else_body.rewrite_condition(expression) if chain?
|
||||
self
|
||||
end
|
||||
|
||||
# Rewrite a chain of IfNodes to add a default case as the final else.
|
||||
def add_else(exprs)
|
||||
chain? ? @else_body.add_else(exprs) : @else_body = (exprs && exprs.unwrap)
|
||||
self
|
||||
end
|
||||
|
||||
# If the else_body is an IfNode itself, then we've got an if-else chain.
|
||||
def chain?
|
||||
@chain ||= @else_body && @else_body.is_a?(IfNode)
|
||||
end
|
||||
|
||||
# The IfNode only compiles into a statement if either of the bodies needs
|
||||
# to be a statement.
|
||||
def statement?
|
||||
@is_statement ||= !!(@comment || @tags[:statement] || @body.statement? || (@else_body && @else_body.statement?))
|
||||
end
|
||||
|
||||
def compile_node(o)
|
||||
write(statement? ? compile_statement(o) : compile_ternary(o))
|
||||
end
|
||||
|
||||
# Compile the IfNode as a regular if-else statement. Flattened chains
|
||||
# force sub-else bodies into statement form.
|
||||
def compile_statement(o)
|
||||
child = o.delete(:chain_child)
|
||||
cond_o = o.dup
|
||||
cond_o.delete(:return)
|
||||
o[:indent] = idt(1)
|
||||
o[:top] = true
|
||||
if_dent = child ? '' : idt
|
||||
com_dent = child ? idt : ''
|
||||
prefix = @comment ? @comment.compile(cond_o) + "\n#{com_dent}" : ''
|
||||
if_part = "#{prefix}#{if_dent}if (#{@condition.compile(cond_o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{idt}}"
|
||||
return if_part unless @else_body
|
||||
else_part = chain? ?
|
||||
" else #{@else_body.compile(o.merge(:indent => idt, :chain_child => true))}" :
|
||||
" else {\n#{Expressions.wrap(@else_body).compile(o)}\n#{idt}}"
|
||||
if_part + else_part
|
||||
end
|
||||
|
||||
# Compile the IfNode into a ternary operator.
|
||||
def compile_ternary(o)
|
||||
if_part = "#{@condition.compile(o)} ? #{@body.compile(o)}"
|
||||
else_part = @else_body ? "#{@else_body.compile(o)}" : 'null'
|
||||
"#{if_part} : #{else_part}"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,23 +0,0 @@
|
||||
module CoffeeScript
|
||||
|
||||
# Racc will raise this Exception whenever a syntax error occurs. The main
|
||||
# benefit over the Racc::ParseError is that the CoffeeScript::ParseError is
|
||||
# line-number aware.
|
||||
class ParseError < Racc::ParseError
|
||||
|
||||
def initialize(token_id, value, stack)
|
||||
@token_id, @value, @stack = token_id, value, stack
|
||||
end
|
||||
|
||||
def message
|
||||
line = @value.respond_to?(:line) ? @value.line : "END"
|
||||
line_part = "line #{line}:"
|
||||
id_part = @token_id != @value.inspect ? ", unexpected #{@token_id.to_s.downcase}" : ""
|
||||
val_part = ['INDENT', 'OUTDENT'].include?(@token_id) ? '' : " for '#{@value.to_s}'"
|
||||
"#{line_part} syntax error#{val_part}#{id_part}"
|
||||
end
|
||||
alias_method :inspect, :message
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,212 +0,0 @@
|
||||
module CoffeeScript
|
||||
|
||||
# In order to keep the grammar simple, the stream of tokens that the Lexer
|
||||
# emits is rewritten by the Rewriter, smoothing out ambiguities, mis-nested
|
||||
# indentation, and single-line flavors of expressions.
|
||||
class Rewriter
|
||||
|
||||
# Tokens that must be balanced.
|
||||
BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], [:INDENT, :OUTDENT]]
|
||||
|
||||
# Tokens that signal the start of a balanced pair.
|
||||
EXPRESSION_START = BALANCED_PAIRS.map {|pair| pair.first }
|
||||
|
||||
# Tokens that signal the end of a balanced pair.
|
||||
EXPRESSION_TAIL = BALANCED_PAIRS.map {|pair| pair.last }
|
||||
|
||||
# Tokens that indicate the close of a clause of an expression.
|
||||
EXPRESSION_CLOSE = [:CATCH, :WHEN, :ELSE, :FINALLY] + EXPRESSION_TAIL
|
||||
|
||||
# The inverse mappings of token pairs we're trying to fix up.
|
||||
INVERSES = BALANCED_PAIRS.inject({}) do |memo, pair|
|
||||
memo[pair.first] = pair.last
|
||||
memo[pair.last] = pair.first
|
||||
memo
|
||||
end
|
||||
|
||||
# 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 = ["\n", :CATCH, :FINALLY, :ELSE, :OUTDENT, :LEADING_WHEN]
|
||||
|
||||
# Rewrite the token stream in multiple passes, one logical filter at
|
||||
# a time. This could certainly be changed into a single pass through the
|
||||
# stream, with a big ol' efficient switch, but it's much nicer like this.
|
||||
def rewrite(tokens)
|
||||
@tokens = tokens
|
||||
adjust_comments
|
||||
remove_mid_expression_newlines
|
||||
move_commas_outside_outdents
|
||||
add_implicit_indentation
|
||||
ensure_balance(*BALANCED_PAIRS)
|
||||
rewrite_closing_parens
|
||||
@tokens
|
||||
end
|
||||
|
||||
# Rewrite the token stream, looking one token ahead and behind.
|
||||
# Allow the return value of the block to tell us how many tokens to move
|
||||
# forwards (or backwards) in the stream, to make sure we don't miss anything
|
||||
# as the stream changes length under our feet.
|
||||
def scan_tokens
|
||||
i = 0
|
||||
loop do
|
||||
break unless @tokens[i]
|
||||
move = yield(@tokens[i - 1], @tokens[i], @tokens[i + 1], i)
|
||||
i += move
|
||||
end
|
||||
end
|
||||
|
||||
# Massage newlines and indentations so that comments don't have to be
|
||||
# correctly indented, or appear on their own line.
|
||||
def adjust_comments
|
||||
scan_tokens do |prev, token, post, i|
|
||||
next 1 unless token[0] == :COMMENT
|
||||
before, after = @tokens[i - 2], @tokens[i + 2]
|
||||
if before && after &&
|
||||
((before[0] == :INDENT && after[0] == :OUTDENT) ||
|
||||
(before[0] == :OUTDENT && after[0] == :INDENT)) &&
|
||||
before[1] == after[1]
|
||||
@tokens.delete_at(i + 2)
|
||||
@tokens.delete_at(i - 2)
|
||||
next 0
|
||||
elsif prev[0] == "\n" && [:INDENT, :OUTDENT].include?(after[0])
|
||||
@tokens.delete_at(i + 2)
|
||||
@tokens[i - 1] = after
|
||||
next 1
|
||||
elsif !["\n", :INDENT, :OUTDENT].include?(prev[0])
|
||||
@tokens.insert(i, ["\n", Value.new("\n", token[1].line)])
|
||||
next 2
|
||||
else
|
||||
next 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Some blocks occur in the middle of expressions -- when we're expecting
|
||||
# this, remove their trailing newlines.
|
||||
def remove_mid_expression_newlines
|
||||
scan_tokens do |prev, token, post, i|
|
||||
next 1 unless post && EXPRESSION_CLOSE.include?(post[0]) && token[0] == "\n"
|
||||
@tokens.delete_at(i)
|
||||
next 0
|
||||
end
|
||||
end
|
||||
|
||||
# Make sure that we don't accidentally break trailing commas, which need
|
||||
# to go on the outside of expression closers.
|
||||
def move_commas_outside_outdents
|
||||
scan_tokens do |prev, token, post, i|
|
||||
if token[0] == :OUTDENT && prev[0] == ','
|
||||
@tokens.delete_at(i)
|
||||
@tokens.insert(i - 1, token)
|
||||
end
|
||||
next 1
|
||||
end
|
||||
end
|
||||
|
||||
# Because our grammar is LALR(1), it can't handle some single-line
|
||||
# expressions that lack ending delimiters. Use the lexer to add the implicit
|
||||
# blocks, so it doesn't need to.
|
||||
# ')' can close a single-line block, but we need to make sure it's balanced.
|
||||
def add_implicit_indentation
|
||||
scan_tokens do |prev, token, post, i|
|
||||
next 1 unless SINGLE_LINERS.include?(token[0]) && post[0] != :INDENT &&
|
||||
!(token[0] == :ELSE && post[0] == :IF) # Elsifs shouldn't get blocks.
|
||||
line = token[1].line
|
||||
@tokens.insert(i + 1, [:INDENT, Value.new(2, line)])
|
||||
idx = i + 1
|
||||
parens = 0
|
||||
loop do
|
||||
idx += 1
|
||||
tok = @tokens[idx]
|
||||
if !tok || SINGLE_CLOSERS.include?(tok[0]) ||
|
||||
(tok[0] == ')' && parens == 0)
|
||||
@tokens.insert(idx, [:OUTDENT, Value.new(2, line)])
|
||||
break
|
||||
end
|
||||
parens += 1 if tok[0] == '('
|
||||
parens -= 1 if tok[0] == ')'
|
||||
end
|
||||
next 1 unless token[0] == :THEN
|
||||
@tokens.delete_at(i)
|
||||
next 0
|
||||
end
|
||||
end
|
||||
|
||||
# Ensure that all listed pairs of tokens are correctly balanced throughout
|
||||
# the course of the token stream.
|
||||
def ensure_balance(*pairs)
|
||||
levels = Hash.new(0)
|
||||
scan_tokens do |prev, token, post, i|
|
||||
pairs.each do |pair|
|
||||
open, close = *pair
|
||||
levels[open] += 1 if token[0] == open
|
||||
levels[open] -= 1 if token[0] == close
|
||||
raise ParseError.new(token[0], token[1], nil) if levels[open] < 0
|
||||
end
|
||||
next 1
|
||||
end
|
||||
unclosed = levels.detect {|k, v| v > 0 }
|
||||
raise SyntaxError, "unclosed '#{unclosed[0]}'" if unclosed
|
||||
end
|
||||
|
||||
# We'd like to support syntax like this:
|
||||
# el.click(event =>
|
||||
# el.hide())
|
||||
# In order to accomplish this, move outdents that follow closing parens
|
||||
# inwards, safely. The steps to accomplish this are:
|
||||
#
|
||||
# 1. Check that all paired tokens are balanced and in order.
|
||||
# 2. Rewrite the stream with a stack: if you see an '(' or INDENT, add it
|
||||
# to the stack. If you see an ')' or OUTDENT, pop the stack and replace
|
||||
# it with the inverse of what we've just popped.
|
||||
# 3. Keep track of "debt" for tokens that we fake, to make sure we end
|
||||
# up balanced in the end.
|
||||
#
|
||||
def rewrite_closing_parens
|
||||
verbose = ENV['VERBOSE']
|
||||
stack, debt = [], Hash.new(0)
|
||||
stack_stats = lambda { "stack: #{stack.inspect} debt: #{debt.inspect}\n\n" }
|
||||
puts "rewrite_closing_original: #{@tokens.inspect}" if verbose
|
||||
scan_tokens do |prev, token, post, i|
|
||||
tag, inv = token[0], INVERSES[token[0]]
|
||||
# Push openers onto the stack.
|
||||
if EXPRESSION_START.include?(tag)
|
||||
stack.push(token)
|
||||
puts "pushing #{tag} #{stack_stats[]}" if verbose
|
||||
next 1
|
||||
# The end of an expression, check stack and debt for a pair.
|
||||
elsif EXPRESSION_TAIL.include?(tag)
|
||||
puts @tokens[i..-1].inspect if verbose
|
||||
# If the tag is already in our debt, swallow it.
|
||||
if debt[inv] > 0
|
||||
debt[inv] -= 1
|
||||
@tokens.delete_at(i)
|
||||
puts "tag in debt #{tag} #{stack_stats[]}" if verbose
|
||||
next 0
|
||||
else
|
||||
# Pop the stack of open delimiters.
|
||||
match = stack.pop
|
||||
mtag = match[0]
|
||||
# Continue onwards if it's the expected tag.
|
||||
if tag == INVERSES[mtag]
|
||||
puts "expected tag #{tag} #{stack_stats[]}" if verbose
|
||||
next 1
|
||||
else
|
||||
# Unexpected close, insert correct close, adding to the debt.
|
||||
debt[mtag] += 1
|
||||
puts "unexpected #{tag}, replacing with #{INVERSES[mtag]} #{stack_stats[]}" if verbose
|
||||
val = mtag == :INDENT ? match[1] : INVERSES[mtag]
|
||||
@tokens.insert(i, [INVERSES[mtag], Value.new(val, token[1].line)])
|
||||
next 1
|
||||
end
|
||||
end
|
||||
else
|
||||
# Uninteresting token:
|
||||
next 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -1,65 +0,0 @@
|
||||
module CoffeeScript
|
||||
|
||||
# Scope objects form a tree corresponding to the shape of the function
|
||||
# definitions present in the script. They provide lexical scope, to determine
|
||||
# whether a variable has been seen before or if it needs to be declared.
|
||||
class Scope
|
||||
|
||||
attr_reader :parent, :expressions, :variables, :temp_variable
|
||||
|
||||
# Initialize a scope with its parent, for lookups up the chain,
|
||||
# as well as the Expressions body where it should declare its variables.
|
||||
def initialize(parent, expressions)
|
||||
@parent, @expressions = parent, expressions
|
||||
@variables = {}
|
||||
@temp_variable = @parent ? @parent.temp_variable.dup : '__a'
|
||||
end
|
||||
|
||||
# Look up a variable in lexical scope, or declare it if not found.
|
||||
def find(name, remote=false)
|
||||
found = check(name)
|
||||
return found if found || remote
|
||||
@variables[name.to_sym] = :var
|
||||
found
|
||||
end
|
||||
|
||||
# Define a local variable as originating from a parameter in current scope
|
||||
# -- no var required.
|
||||
def parameter(name)
|
||||
@variables[name.to_sym] = :param
|
||||
end
|
||||
|
||||
# Just check to see if a variable has already been declared.
|
||||
def check(name)
|
||||
return true if @variables[name.to_sym]
|
||||
!!(@parent && @parent.check(name))
|
||||
end
|
||||
|
||||
# You can reset a found variable on the immediate scope.
|
||||
def reset(name)
|
||||
@variables[name.to_sym] = false
|
||||
end
|
||||
|
||||
# Find an available, short, name for a compiler-generated variable.
|
||||
def free_variable
|
||||
@temp_variable.succ! while check(@temp_variable)
|
||||
@variables[@temp_variable.to_sym] = :var
|
||||
@temp_variable.dup
|
||||
end
|
||||
|
||||
def declarations?(body)
|
||||
!declared_variables.empty? && body == @expressions
|
||||
end
|
||||
|
||||
# Return the list of variables first declared in current scope.
|
||||
def declared_variables
|
||||
@variables.select {|k, v| v == :var }.map {|pair| pair[0].to_s }.sort
|
||||
end
|
||||
|
||||
def inspect
|
||||
"<Scope:#{__id__} #{@variables.inspect}>"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,46 +0,0 @@
|
||||
module CoffeeScript
|
||||
|
||||
# Instead of producing raw Ruby objects, the Lexer produces values of this
|
||||
# class, wrapping native objects tagged with line number information.
|
||||
class Value
|
||||
attr_reader :value, :line
|
||||
|
||||
def initialize(value, line=nil)
|
||||
@value, @line = value, line
|
||||
end
|
||||
|
||||
def to_str
|
||||
@value.to_s
|
||||
end
|
||||
alias_method :to_s, :to_str
|
||||
|
||||
def to_sym
|
||||
to_str.to_sym
|
||||
end
|
||||
|
||||
def compile(o={})
|
||||
to_s
|
||||
end
|
||||
|
||||
def inspect
|
||||
@value.inspect
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
@value == other
|
||||
end
|
||||
|
||||
def [](index)
|
||||
@value[index]
|
||||
end
|
||||
|
||||
def eql?(other)
|
||||
@value.eql?(other)
|
||||
end
|
||||
|
||||
def hash
|
||||
@value.hash
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
201
lib/command_line.js
Normal file
201
lib/command_line.js
Normal file
@@ -0,0 +1,201 @@
|
||||
(function(){
|
||||
var BANNER, SWITCHES, coffee, compile_script, compile_scripts, fs, lint, option_parser, options, optparse, parse_options, path, sources, usage, version, watch_scripts, write_js;
|
||||
fs = require('fs');
|
||||
path = require('path');
|
||||
coffee = require('coffee-script');
|
||||
optparse = require('optparse');
|
||||
BANNER = "coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee";
|
||||
SWITCHES = [['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-r', '--run', 'compile and run a CoffeeScript'], ['-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'], ['-e', '--eval', 'compile a string from the command line'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-tr', '--tree', 'print the parse tree that Jison produces'], ['-n', '--no-wrap', 'compile without the top-level function wrapper'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']];
|
||||
options = {};
|
||||
sources = [];
|
||||
option_parser = null;
|
||||
// The CommandLine handles all of the functionality of the `coffee` utility.
|
||||
exports.run = function run() {
|
||||
var flags, separator;
|
||||
parse_options();
|
||||
if (options.interactive) {
|
||||
return require('repl');
|
||||
}
|
||||
if (options.eval) {
|
||||
return compile_script('terminal', sources[0]);
|
||||
}
|
||||
if (!(sources.length)) {
|
||||
usage();
|
||||
}
|
||||
separator = sources.indexOf('--');
|
||||
flags = [];
|
||||
if (separator >= 0) {
|
||||
flags = sources.slice((separator + 1), sources.length);
|
||||
sources = sources.slice(0, separator);
|
||||
}
|
||||
process.ARGV = flags;
|
||||
if (options.watch) {
|
||||
watch_scripts();
|
||||
}
|
||||
compile_scripts();
|
||||
return this;
|
||||
};
|
||||
// The "--help" usage message.
|
||||
usage = function usage() {
|
||||
puts('\n' + option_parser.help() + '\n');
|
||||
return process.exit(0);
|
||||
};
|
||||
// The "--version" message.
|
||||
version = function version() {
|
||||
puts("CoffeeScript version " + coffee.VERSION);
|
||||
return process.exit(0);
|
||||
};
|
||||
// Compiles the source CoffeeScript, returning the desired JavaScript, tokens,
|
||||
// or JSLint results.
|
||||
compile_scripts = function compile_scripts() {
|
||||
var _a, _b, _c, compile, source;
|
||||
compile = function compile(source) {
|
||||
return fs.readFile(source, function(err, code) {
|
||||
return compile_script(source, code);
|
||||
});
|
||||
};
|
||||
_a = []; _b = sources;
|
||||
for (_c = 0; _c < _b.length; _c++) {
|
||||
source = _b[_c];
|
||||
_a.push(compile(source));
|
||||
}
|
||||
return _a;
|
||||
};
|
||||
// Compile a single source script, containing the given code, according to the
|
||||
// requested options. Both compile_scripts and watch_scripts share this method.
|
||||
compile_script = function compile_script(source, code) {
|
||||
var js, o, opts;
|
||||
opts = options;
|
||||
o = opts.no_wrap ? {
|
||||
no_wrap: true
|
||||
} : {};
|
||||
try {
|
||||
if (opts.tokens) {
|
||||
return coffee.print_tokens(coffee.tokenize(code));
|
||||
} else if (opts.tree) {
|
||||
return puts(coffee.tree(code).toString());
|
||||
} else {
|
||||
js = coffee.compile(code, o);
|
||||
if (opts.run) {
|
||||
return eval(js);
|
||||
} else if (opts.print) {
|
||||
return puts(js);
|
||||
} else if (opts.lint) {
|
||||
return lint(js);
|
||||
} else {
|
||||
return write_js(source, js);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (opts.watch) {
|
||||
return puts(err.message);
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
// Watch a list of source CoffeeScript files, recompiling them every time the
|
||||
// files are updated.
|
||||
watch_scripts = function watch_scripts() {
|
||||
var _a, _b, _c, source, watch;
|
||||
watch = function watch(source) {
|
||||
return process.watchFile(source, {
|
||||
persistent: true,
|
||||
interval: 500
|
||||
}, function(curr, prev) {
|
||||
if (curr.mtime.getTime() === prev.mtime.getTime()) {
|
||||
return null;
|
||||
}
|
||||
return fs.readFile(source, function(err, code) {
|
||||
return compile_script(source, code);
|
||||
});
|
||||
});
|
||||
};
|
||||
_a = []; _b = sources;
|
||||
for (_c = 0; _c < _b.length; _c++) {
|
||||
source = _b[_c];
|
||||
_a.push(watch(source));
|
||||
}
|
||||
return _a;
|
||||
};
|
||||
// Write out a JavaScript source file with the compiled code.
|
||||
write_js = function write_js(source, js) {
|
||||
var dir, filename, js_path;
|
||||
filename = path.basename(source, path.extname(source)) + '.js';
|
||||
dir = options.output || path.dirname(source);
|
||||
js_path = path.join(dir, filename);
|
||||
return fs.writeFile(js_path, js);
|
||||
};
|
||||
// Pipe compiled JS through JSLint (requires a working 'jsl' command).
|
||||
lint = function lint(js) {
|
||||
var jsl;
|
||||
jsl = process.createChildProcess('jsl', ['-nologo', '-stdin']);
|
||||
jsl.addListener('output', function(result) {
|
||||
if (result) {
|
||||
return puts(result.replace(/\n/g, ''));
|
||||
}
|
||||
});
|
||||
jsl.addListener('error', function(result) {
|
||||
if (result) {
|
||||
return puts(result);
|
||||
}
|
||||
});
|
||||
jsl.write(js);
|
||||
return jsl.close();
|
||||
};
|
||||
// Use OptionParser for all the options.
|
||||
parse_options = function parse_options() {
|
||||
var oparser, opts, paths;
|
||||
opts = (options = {});
|
||||
oparser = (option_parser = new optparse.OptionParser(SWITCHES));
|
||||
oparser.banner = BANNER;
|
||||
oparser.add('interactive', function() {
|
||||
return opts.interactive = true;
|
||||
});
|
||||
oparser.add('run', function() {
|
||||
return opts.run = true;
|
||||
});
|
||||
oparser.add('output', function(dir) {
|
||||
return opts.output = dir;
|
||||
});
|
||||
oparser.add('watch', function() {
|
||||
return opts.watch = true;
|
||||
});
|
||||
oparser.add('print', function() {
|
||||
return opts.print = true;
|
||||
});
|
||||
oparser.add('lint', function() {
|
||||
return opts.lint = true;
|
||||
});
|
||||
oparser.add('eval', function() {
|
||||
return opts.eval = true;
|
||||
});
|
||||
oparser.add('tokens', function() {
|
||||
return opts.tokens = true;
|
||||
});
|
||||
oparser.add('tree', function() {
|
||||
return opts.tree = true;
|
||||
});
|
||||
oparser.add('no-wrap', function() {
|
||||
return opts.no_wrap = true;
|
||||
});
|
||||
oparser.add('help', (function(__this) {
|
||||
var __func = function() {
|
||||
return usage();
|
||||
};
|
||||
return (function() {
|
||||
return __func.apply(__this, arguments);
|
||||
});
|
||||
})(this));
|
||||
oparser.add('version', (function(__this) {
|
||||
var __func = function() {
|
||||
return version();
|
||||
};
|
||||
return (function() {
|
||||
return __func.apply(__this, arguments);
|
||||
});
|
||||
})(this));
|
||||
paths = oparser.parse(process.ARGV);
|
||||
return sources = paths.slice(2, paths.length);
|
||||
};
|
||||
})();
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user