adding line number info to unclosed parens, objects, arrays, and indents

This commit is contained in:
Jeremy Ashkenas
2010-01-25 21:07:18 -05:00
parent 91e703052c
commit d6e206b420
3 changed files with 10 additions and 8 deletions

View File

@@ -141,7 +141,7 @@ Usage:
options[:no_wrap] = true if @options[:no_wrap] options[:no_wrap] = true if @options[:no_wrap]
options[:globals] = true if @options[:globals] options[:globals] = true if @options[:globals]
CoffeeScript.compile(script, options) CoffeeScript.compile(script, options)
rescue CoffeeScript::ParseError, SyntaxError => e rescue CoffeeScript::ParseError => e
STDERR.puts "#{source}: #{e.message}" STDERR.puts "#{source}: #{e.message}"
exit(1) unless @options[:watch] exit(1) unless @options[:watch]
nil nil

View File

@@ -11,16 +11,16 @@ module CoffeeScript
"\n" => 'newline' "\n" => 'newline'
} }
def initialize(token_id, value, stack) def initialize(token_id, value, stack=nil, message=nil)
@token_id, @value, @stack = token_id, value, stack @token_id, @value, @stack, @message = token_id, value, stack, message
end end
def message def message
line = @value.respond_to?(:line) ? @value.line : "END" line = @value.respond_to?(:line) ? @value.line : "END"
line_part = "line #{line}:" line_part = "line #{line}:"
id_part = @token_id != @value.inspect ? ", unexpected #{@token_id.to_s.downcase}" : "" id_part = @token_id != @value.to_s ? "unexpected #{@token_id.to_s.downcase}" : ""
val_part = " for #{TOKEN_MAP[@value.to_s] || "'#{@value}'"}" val_part = @message || "for #{TOKEN_MAP[@value.to_s] || "'#{@value}'"}"
"#{line_part} syntax error#{val_part}#{id_part}" "#{line_part} syntax error, #{val_part}#{id_part}"
end end
alias_method :inspect, :message alias_method :inspect, :message

View File

@@ -171,18 +171,20 @@ module CoffeeScript
# Ensure that all listed pairs of tokens are correctly balanced throughout # Ensure that all listed pairs of tokens are correctly balanced throughout
# the course of the token stream. # the course of the token stream.
def ensure_balance(*pairs) def ensure_balance(*pairs)
levels = Hash.new(0) levels, lines = Hash.new(0), Hash.new
scan_tokens do |prev, token, post, i| scan_tokens do |prev, token, post, i|
pairs.each do |pair| pairs.each do |pair|
open, close = *pair open, close = *pair
levels[open] += 1 if token[0] == open levels[open] += 1 if token[0] == open
levels[open] -= 1 if token[0] == close 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 raise ParseError.new(token[0], token[1], nil) if levels[open] < 0
end end
next 1 next 1
end end
unclosed = levels.detect {|k, v| v > 0 } 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 end
# We'd like to support syntax like this: # We'd like to support syntax like this: