completely reorganized for a gem and the 'coffee-script' command
This commit is contained in:
parent
5acc91c07c
commit
290aa257de
|
@ -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.
|
|
@ -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
|
|
@ -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.
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require "#{File.dirname(__FILE__)}/../lib/coffee_script/command_line.rb"
|
||||
|
||||
CoffeeScript::CommandLine.new
|
|
@ -1,5 +1,3 @@
|
|||
# TODO: Add range indexing: array[5..7] => array.slice(5, 7)
|
||||
|
||||
# Functions:
|
||||
square: x => x * x.
|
||||
|
|
@ -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
|
|
@ -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
|
|
@ -281,10 +281,6 @@ rule
|
|||
|
||||
end
|
||||
|
||||
---- header
|
||||
require "lexer"
|
||||
require "nodes"
|
||||
|
||||
---- inner
|
||||
def parse(code, show_tokens=false)
|
||||
# @yydebug = true
|
Loading…
Reference in New Issue