From ab4a4a55802b310d34c6ea78da2a918e33827443 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 26 Jan 2010 14:49:33 -0500 Subject: [PATCH] make nested implicit indentation just a little bit smarter about outdents and stack levels --- lib/coffee_script/rewriter.rb | 6 +++++- test/fixtures/execution/test_functions.coffee | 11 ++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/coffee_script/rewriter.rb b/lib/coffee_script/rewriter.rb index 6c5d60ce..7769694f 100644 --- a/lib/coffee_script/rewriter.rb +++ b/lib/coffee_script/rewriter.rb @@ -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| diff --git a/test/fixtures/execution/test_functions.coffee b/test/fixtures/execution/test_functions.coffee index ecc2ea56..9fe19c8b 100644 --- a/test/fixtures/execution/test_functions.coffee +++ b/test/fixtures/execution/test_functions.coffee @@ -54,4 +54,13 @@ print true unless false print true for i in [1..3] print_func: (f) -> print(f()) -print_func -> true \ No newline at end of file +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