mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-01-14 01:07:55 -05:00
patched up lexer to add indentation to single-line flavors of statements -- let's expand this idea
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user