make nested implicit indentation just a little bit smarter about outdents and stack levels

This commit is contained in:
Jeremy Ashkenas 2010-01-26 14:49:33 -05:00
parent 3775f682de
commit ab4a4a5580
2 changed files with 15 additions and 2 deletions

View File

@ -158,6 +158,10 @@ module CoffeeScript
stack = [0]
scan_tokens do |prev, token, post, i|
stack.push(0) if token[0] == :INDENT
if token[0] == :OUTDENT
last = stack.pop
stack[-1] += last
end
if (stack.last > 0 && (IMPLICIT_END.include?(token[0]) || post.nil?)) &&
!(token[0] == :PARAM_START && prev[0] == ',')
idx = token[0] == :OUTDENT ? i + 1 : i
@ -165,7 +169,6 @@ module CoffeeScript
size, stack[-1] = stack[-1] + 1, 0
next size
end
stack.pop if token[0] == :OUTDENT
next 1 unless IMPLICIT_FUNC.include?(prev[0]) && IMPLICIT_CALL.include?(token[0])
@tokens.insert(i, ['(', Value.new('(', token[1].line)])
stack[-1] += 1
@ -176,6 +179,7 @@ module CoffeeScript
# Ensure that all listed pairs of tokens are correctly balanced throughout
# the course of the token stream.
def ensure_balance(*pairs)
puts "\nbefore ensure_balance: #{@tokens.inspect}" if ENV['VERBOSE']
levels, lines = Hash.new(0), Hash.new
scan_tokens do |prev, token, post, i|
pairs.each do |pair|

View File

@ -54,4 +54,13 @@ print true unless false
print true for i in [1..3]
print_func: (f) -> print(f())
print_func -> true
print_func -> true
# Optional parens can be used in a nested fashion.
call: (func) -> func()
result: call ->
inner: call ->
Math.Add(5, 5)
print result is 10