diff --git a/lib/coffee_script/command_line.rb b/lib/coffee_script/command_line.rb index 30ad1e11..ada64f1b 100644 --- a/lib/coffee_script/command_line.rb +++ b/lib/coffee_script/command_line.rb @@ -141,7 +141,7 @@ Usage: options[:no_wrap] = true if @options[:no_wrap] options[:globals] = true if @options[:globals] CoffeeScript.compile(script, options) - rescue CoffeeScript::ParseError, SyntaxError => e + rescue CoffeeScript::ParseError => e STDERR.puts "#{source}: #{e.message}" exit(1) unless @options[:watch] nil diff --git a/lib/coffee_script/parse_error.rb b/lib/coffee_script/parse_error.rb index 6fbc86d1..0f23bc92 100644 --- a/lib/coffee_script/parse_error.rb +++ b/lib/coffee_script/parse_error.rb @@ -11,16 +11,16 @@ module CoffeeScript "\n" => 'newline' } - def initialize(token_id, value, stack) - @token_id, @value, @stack = token_id, value, stack + def initialize(token_id, value, stack=nil, message=nil) + @token_id, @value, @stack, @message = token_id, value, stack, message 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 = " for #{TOKEN_MAP[@value.to_s] || "'#{@value}'"}" - "#{line_part} syntax error#{val_part}#{id_part}" + id_part = @token_id != @value.to_s ? "unexpected #{@token_id.to_s.downcase}" : "" + val_part = @message || "for #{TOKEN_MAP[@value.to_s] || "'#{@value}'"}" + "#{line_part} syntax error, #{val_part}#{id_part}" end alias_method :inspect, :message diff --git a/lib/coffee_script/rewriter.rb b/lib/coffee_script/rewriter.rb index 1b802822..964aa191 100644 --- a/lib/coffee_script/rewriter.rb +++ b/lib/coffee_script/rewriter.rb @@ -171,18 +171,20 @@ module CoffeeScript # 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) + levels, lines = Hash.new(0), Hash.new 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 + lines[token[0]] = token[1].line 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 + sym = unclosed && unclosed[0] + raise ParseError.new(sym, Value.new(sym, lines[sym]), nil, "unclosed '#{sym}'") if unclosed end # We'd like to support syntax like this: