patched up lexer to add indentation to single-line flavors of statements -- let's expand this idea

This commit is contained in:
Jeremy Ashkenas
2009-12-30 13:58:00 -05:00
parent df5c5d9fe2
commit 8e8efe4288
2 changed files with 21 additions and 1 deletions

View File

@@ -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.

View File

@@ -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