changing array comprehension filters from 'where' to 'when' to mirror case/when

This commit is contained in:
Jeremy Ashkenas
2009-12-29 09:55:37 -05:00
parent a3129e05ae
commit ae112d6087
5 changed files with 22 additions and 31 deletions

View File

@@ -204,7 +204,7 @@
</dict> </dict>
<dict> <dict>
<key>match</key> <key>match</key>
<string>\b(break|when|catch|continue|else|finally|for|if|return|switch|then|throw|try|unless|where|while)\b</string> <string>\b(break|catch|continue|else|finally|for|if|return|switch|then|throw|try|unless|when|while)\b</string>
<key>name</key> <key>name</key>
<string>keyword.control.coffee</string> <string>keyword.control.coffee</string>
</dict> </dict>

View File

@@ -8,7 +8,7 @@ token IDENTIFIER PROPERTY_ACCESS
token CODE PARAM NEW RETURN token CODE PARAM NEW RETURN
token TRY CATCH FINALLY THROW token TRY CATCH FINALLY THROW
token BREAK CONTINUE token BREAK CONTINUE
token FOR IN WHERE WHILE token FOR IN WHILE
token SWITCH WHEN token SWITCH WHEN
token DELETE INSTANCEOF TYPEOF token DELETE INSTANCEOF TYPEOF
token SUPER EXTENDS token SUPER EXTENDS
@@ -32,7 +32,7 @@ prechigh
left '.' left '.'
right INDENT right INDENT
left OUTDENT left OUTDENT
right THROW FOR IN WHERE WHILE NEW SUPER right THROW FOR IN WHILE WHEN NEW SUPER
left UNLESS IF ELSE EXTENDS left UNLESS IF ELSE EXTENDS
left ASSIGN '||=' '&&=' left ASSIGN '||=' '&&='
right RETURN right RETURN
@@ -313,7 +313,7 @@ rule
ForSource: ForSource:
IN PureExpression { result = [val[1]] } IN PureExpression { result = [val[1]] }
| IN PureExpression | IN PureExpression
WHERE Expression { result = [val[1], val[3]] } WHEN Expression { result = [val[1], val[3]] }
; ;
# Switch/When blocks. # Switch/When blocks.

View File

@@ -35,18 +35,16 @@ module CoffeeScript
MULTILINER = /\n/ MULTILINER = /\n/
COMMENT_CLEANER = /(^\s*#|\n\s*$)/ COMMENT_CLEANER = /(^\s*#|\n\s*$)/
# Tokens that always constitute the start of an expression.
# EXP_START = ['{', '(', '[']
# Tokens that always constitute the end of an expression.
# EXP_END = ['}', ')', ']']
# Assignment tokens. # Assignment tokens.
ASSIGN = [':', '='] ASSIGN = [':', '=']
# Tokens that must be balanced. # Tokens that must be balanced.
BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], [:INDENT, :OUTDENT]] BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], [:INDENT, :OUTDENT]]
# Outdents that come before these tokens don't signify the end of the
# expression. TODO: Is this safe?
EXPRESSION_TAIL = [:CATCH, :WHEN, :ELSE, ')', ']', '}']
# Scan by attempting to match tokens one character at a time. Slow and steady. # Scan by attempting to match tokens one character at a time. Slow and steady.
def tokenize(code) def tokenize(code)
@code = code.chomp # Cleanup code by remove extra line breaks @code = code.chomp # Cleanup code by remove extra line breaks
@@ -61,6 +59,7 @@ module CoffeeScript
end end
close_indentation close_indentation
remove_empty_outdents remove_empty_outdents
remove_mid_expression_newlines
ensure_balance(*BALANCED_PAIRS) ensure_balance(*BALANCED_PAIRS)
rewrite_closing_parens rewrite_closing_parens
@tokens @tokens
@@ -154,9 +153,7 @@ module CoffeeScript
token(:OUTDENT, last_indent) token(:OUTDENT, last_indent)
move_out -= last_indent move_out -= last_indent
end end
# TODO: Figure out what to do about blocks that close, ending the expression token("\n", "\n")
# versus blocks that occur mid-expression.
# token("\n", "\n")
@indent = @indents.last || 0 @indent = @indents.last || 0
end end
@@ -183,8 +180,6 @@ module CoffeeScript
value = @chunk[OPERATOR, 1] value = @chunk[OPERATOR, 1]
tag_parameters if value && value.match(CODE) tag_parameters if value && value.match(CODE)
value ||= @chunk[0,1] value ||= @chunk[0,1]
# skip_following_newlines if EXP_START.include?(value)
# remove_leading_newlines if EXP_END.include?(value)
tag = ASSIGN.include?(value) ? :ASSIGN : value tag = ASSIGN.include?(value) ? :ASSIGN : value
token(tag, value) token(tag, value)
@i += value.length @i += value.length
@@ -215,20 +210,6 @@ module CoffeeScript
end end
end end
# Consume and ignore newlines immediately after this point.
# def skip_following_newlines
# newlines = @code[(@i+1)..-1][NEWLINE, 1]
# if newlines
# @line += newlines.length
# @i += newlines.length
# end
# end
# Discard newlines immediately before this point.
# def remove_leading_newlines
# @tokens.pop if last_value == "\n"
# end
# Close up all remaining open blocks. # Close up all remaining open blocks.
def close_indentation def close_indentation
outdent_token(@indent) outdent_token(@indent)
@@ -259,6 +240,14 @@ module CoffeeScript
end end
end end
# Some blocks occur in the middle of expressions -- when we're expecting
# this, remove their trailing newlines.
def remove_mid_expression_newlines
scan_tokens do |prev, token, post, i|
@tokens.delete_at(i) if post && EXPRESSION_TAIL.include?(post[0]) && token[0] == "\n" && prev[0] == :OUTDENT
end
end
# We'd like to support syntax like this: # We'd like to support syntax like this:
# el.click(event => # el.click(event =>
# el.hide()) # el.hide())
@@ -286,6 +275,8 @@ module CoffeeScript
end end
end end
# Ensure that all listed pairs of tokens are correctly balanced throughout
# the course of the token stream.
def ensure_balance(*pairs) def ensure_balance(*pairs)
levels = Hash.new(0) levels = Hash.new(0)
scan_tokens do |prev, token, post, i| scan_tokens do |prev, token, post, i|

View File

@@ -1,4 +1,4 @@
nums: n * n for n in [1, 2, 3] where n % 2 isnt 0 nums: n * n for n in [1, 2, 3] when n % 2 isnt 0
results: n * 2 for n in nums results: n * 2 for n in nums
# next: for n in [1, 2, 3] if n % 2 isnt 0 # next: for n in [1, 2, 3] if n % 2 isnt 0

View File

@@ -21,7 +21,7 @@ func: =>
text = c.text text = c.text
} }
c.list: l for l in d.text.split('') where l is '-' c.list: l for l in d.text.split('') when l is '-'
c.single: c.list[1..1][0] c.single: c.list[1..1][0]