nicer syntax error messages for newlines and indentation

This commit is contained in:
Jeremy Ashkenas
2010-01-12 17:44:03 -05:00
parent 8dfbd1a2a8
commit 197914bcf7

View File

@@ -5,6 +5,12 @@ module CoffeeScript
# line-number aware.
class ParseError < Racc::ParseError
TOKEN_MAP = {
'INDENT' => 'indent',
'OUTDENT' => 'outdent',
"\n" => 'newline'
}
def initialize(token_id, value, stack)
@token_id, @value, @stack = token_id, value, stack
end
@@ -13,7 +19,7 @@ module CoffeeScript
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 = ['INDENT', 'OUTDENT'].include?(@token_id) ? '' : " for '#{@value.to_s}'"
val_part = " for #{TOKEN_MAP[@value.to_s] || "'#{@value}'"}"
"#{line_part} syntax error#{val_part}#{id_part}"
end
alias_method :inspect, :message