1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

allowing any manner of indentation in the comments, by adjusting them in the lexer

This commit is contained in:
Jeremy Ashkenas 2009-12-30 22:24:40 -05:00
parent 942572d081
commit a6539a030c
2 changed files with 27 additions and 0 deletions

View file

@ -77,6 +77,7 @@ module CoffeeScript
end
puts "original stream: #{@tokens.inspect}" if ENV['VERBOSE']
close_indentation
adjust_comments
remove_mid_expression_newlines
move_commas_outside_outdents
add_implicit_indentation
@ -244,6 +245,24 @@ module CoffeeScript
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

View file

@ -0,0 +1,8 @@
func: =>
false
false # comment
false
# comment
true
print(func())