mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
added nice syntax errors
This commit is contained in:
@@ -31,7 +31,7 @@ Usage:
|
||||
def compile_javascript
|
||||
@sources.each do |source|
|
||||
next tokens(source) if @options[:tokens]
|
||||
contents = CoffeeScript.compile(File.open(source))
|
||||
contents = compile(source)
|
||||
next puts(contents) if @options[:print]
|
||||
next lint(contents) if @options[:lint]
|
||||
File.open(path_for(source), 'w+') {|f| f.write(contents) }
|
||||
@@ -60,6 +60,15 @@ Usage:
|
||||
puts Lexer.new.tokenize(File.read(source)).inspect
|
||||
end
|
||||
|
||||
def compile(source)
|
||||
begin
|
||||
CoffeeScript.compile(File.open(source))
|
||||
rescue CoffeeScript::ParseError => e
|
||||
STDERR.puts e.message(source)
|
||||
exit(1)
|
||||
end
|
||||
end
|
||||
|
||||
# Write out JavaScript alongside CoffeeScript unless an output directory
|
||||
# is specified.
|
||||
def path_for(source)
|
||||
|
||||
@@ -294,13 +294,16 @@ rule
|
||||
end
|
||||
|
||||
---- inner
|
||||
def parse(code, show_tokens=false)
|
||||
def parse(code)
|
||||
# @yydebug = true
|
||||
@tokens = Lexer.new.tokenize(code)
|
||||
puts @tokens.inspect if show_tokens
|
||||
do_parse
|
||||
end
|
||||
|
||||
def next_token
|
||||
@tokens.shift
|
||||
end
|
||||
|
||||
def on_error(error_token_id, error_value, value_stack)
|
||||
raise CoffeeScript::ParseError.new(token_to_str(error_token_id), error_value, value_stack)
|
||||
end
|
||||
19
lib/coffee_script/parse_error.rb
Normal file
19
lib/coffee_script/parse_error.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
module CoffeeScript
|
||||
|
||||
class ParseError < Racc::ParseError
|
||||
|
||||
def initialize(token_id, value, stack)
|
||||
@token_id, @value, @stack = token_id, value, stack
|
||||
end
|
||||
|
||||
def message(source_file=nil)
|
||||
line = @value.respond_to?(:line) ? @value.line : "END"
|
||||
line_part = source_file ? "#{source_file}:#{line}:" : "line #{line}:"
|
||||
id_part = @token_id != @value.inspect ? ", unexpected #{@token_id.downcase}" : ""
|
||||
"#{line_part} syntax error for '#{@value.to_s}'#{id_part}"
|
||||
end
|
||||
alias_method :inspect, :message
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user