Skip to content

Commit

Permalink
allowing any manner of indentation in the comments, by adjusting them…
Browse files Browse the repository at this point in the history
… in the lexer
  • Loading branch information
jashkenas committed Dec 31, 2009
1 parent e4ae324 commit 3ad9316
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/coffee_script/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def tokenize(code)
end
puts "original stream: #{@tokens.inspect}" if ENV['VERBOSE']
close_indentation
adjust_comments
remove_mid_expression_newlines
move_commas_outside_outdents
add_implicit_indentation
Expand Down Expand Up @@ -244,6 +245,24 @@ def scan_tokens
end
end

# Massage newlines and indentations so that comments don't have to be
# correctly indented, or appear on their own line.
def adjust_comments
scan_tokens do |prev, token, post, i|
next unless token[0] == :COMMENT
before, after = @tokens[i - 2], @tokens[i + 2]
if before && after &&
((before[0] == :INDENT && after[0] == :OUTDENT) ||
(before[0] == :OUTDENT && after[0] == :INDENT)) &&
before[1] == after[1]
@tokens.delete_at(i + 2)
@tokens.delete_at(i - 2)
elsif !["\n", :INDENT, :OUTDENT].include?(prev[0])
@tokens.insert(i, ["\n", Value.new("\n", token[1].line)])
end
end
end

# Some blocks occur in the middle of expressions -- when we're expecting
# this, remove their trailing newlines.
def remove_mid_expression_newlines
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/execution/test_funky_comments.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
func: =>
false
false # comment
false
# comment
true

print(func())

0 comments on commit 3ad9316

Please sign in to comment.