Skip to content

Commit

Permalink
nicer syntax error messages for newlines and indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Jan 12, 2010
1 parent 8dfbd1a commit 197914b
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/coffee_script/parse_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,7 +19,7 @@ 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 = ['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
Expand Down

0 comments on commit 197914b

Please sign in to comment.