diff --git a/lib/coffee_script/lexer.rb b/lib/coffee_script/lexer.rb index 9dc9ec97..5f5fcbf8 100644 --- a/lib/coffee_script/lexer.rb +++ b/lib/coffee_script/lexer.rb @@ -63,6 +63,7 @@ module CoffeeScript close_indentation remove_mid_expression_newlines move_commas_outside_outdents + add_implicit_indentation ensure_balance(*BALANCED_PAIRS) rewrite_closing_parens @tokens @@ -243,6 +244,25 @@ module CoffeeScript @tokens.insert(i - 1, token) end end + + # Because our grammar is LALR(1), it can't handle some single-line + # expressions that lack ending delimiters. Use the lexer to add the implicit + # blocks, so it doesn't need to. + def add_implicit_indentation + scan_tokens do |prev, token, post, i| + if token[0] == :ELSE && post[0] != :INDENT + line = token[1].line + @tokens.insert(i + 1, [:INDENT, Value.new(2, line)]) + loop do + i += 1 + if !@tokens[i] || ["\n", ")", :OUTDENT].include?(@tokens[i][0]) + @tokens.insert(i, [:OUTDENT, Value.new(2, line)]) + break + end + end + end + end + end # Ensure that all listed pairs of tokens are correctly balanced throughout # the course of the token stream. diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index e82e9f1a..5aa5d277 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -694,7 +694,7 @@ module CoffeeScript # Rewrite a chain of IfNodes to add a default case as the final else. def add_else(expressions) - chain? ? @else_body.add_else(expressions) : @else_body = expressions + chain? ? @else_body.add_else(expressions) : @else_body = expressions.unwrap self end