Skip to content

Commit

Permalink
completely reorganized for a gem and the 'coffee-script' command
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Dec 17, 2009
1 parent 5acc91c commit 290aa25
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 1,482 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2009 Jeremy Ashkenas

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
15 changes: 15 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
desc "Recompile the Racc parser (pass -v and -g for verbose debugging)"
task :build, :extra_args do |t, args|
sh "racc #{args[:extra_args]} -o lib/coffee_script/parser.rb lib/coffee_script/grammar.y"
end


# # Pipe compiled JS through JSLint.
# puts "\n\n"
# require 'open3'
# stdin, stdout, stderr = Open3.popen3('jsl -nologo -stdin')
# stdin.write(js)
# stdin.close
# puts stdout.read
# stdout.close
# stderr.close
5 changes: 4 additions & 1 deletion WISHLIST → TODO
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
TODO:

* Is it possible to close blocks (functions, ifs, trys) without an explicit
block delimiter or significant whitespace?

* Is it possible to pass comments through cleanly and have them show up on
the other end? This includes comments in the middle of array and object
literals, and argument lists.
literals, and argument lists.

5 changes: 5 additions & 0 deletions bin/coffee-script
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby

require "#{File.dirname(__FILE__)}/../lib/coffee_script/command_line.rb"

CoffeeScript::CommandLine.new
2 changes: 0 additions & 2 deletions code.cs → examples/code.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# TODO: Add range indexing: array[5..7] => array.slice(5, 7)

# Functions:
square: x => x * x.

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions lib/coffee-script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
require "coffee_script/lexer"
require "coffee_script/parser"
require "coffee_script/nodes"

# Namespace for all CoffeeScript internal classes.
module CoffeeScript

VERSION = '0.1.0'

def self.compile(script)
script = script.read if script.respond_to?(:read)
Parser.new.parse(script).compile
end

end
99 changes: 99 additions & 0 deletions lib/coffee_script/command_line.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require 'optparse'
require 'fileutils'
require 'open3'
require File.expand_path(File.dirname(__FILE__) + '/../coffee-script')

module CoffeeScript

class CommandLine

BANNER = <<-EOS
coffee-script compiles CoffeeScript files into JavaScript.
Usage:
coffee-script path/to/script.cs
EOS

def initialize
parse_options
check_sources
compile_javascript
end

def usage
puts "\n#{@option_parser}\n"
exit
end


private

def compile_javascript
@sources.each do |source|
contents = CoffeeScript.compile(File.open(source))
next puts(contents) if @options[:print]
next lint(contents) if @options[:lint]
File.open(path_for(source), 'w+') {|f| f.write(contents) }
end
end

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.
def lint(js)
stdin, stdout, stderr = Open3.popen3('jsl -nologo -stdin')
stdin.write(js)
stdin.close
print stdout.read
stdout.close and stderr.close
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

def parse_options
@options = {}
@option_parser = OptionParser.new do |opts|
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('-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_tail('-v', '--version', 'display coffee-script version') do
puts "coffee-script 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
4 changes: 0 additions & 4 deletions grammar.y → lib/coffee_script/grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,6 @@ rule
end
---- header
require "lexer"
require "nodes"
---- inner
def parse(code, show_tokens=false)
# @yydebug = true
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 290aa25

Please sign in to comment.