Skip to content

Commit

Permalink
Merge pull request #3677 from alubbe/master
Browse files Browse the repository at this point in the history
implemented proper precedence for 'yield'
  • Loading branch information
jashkenas committed Oct 13, 2014
2 parents 158ca0d + dd5da7f commit 4f82e59
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/coffee-script/grammar.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/coffee-script/parser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/grammar.coffee
Expand Up @@ -589,7 +589,6 @@ operators = [
['nonassoc', '++', '--']
['left', '?']
['right', 'UNARY']
['right', 'YIELD']
['right', '**']
['right', 'UNARY_MATH']
['left', 'MATH']
Expand All @@ -599,6 +598,7 @@ operators = [
['left', 'COMPARE']
['left', 'LOGIC']
['nonassoc', 'INDENT', 'OUTDENT']
['right', 'YIELD']
['right', '=', ':', 'COMPOUND_ASSIGN', 'RETURN', 'THROW', 'EXTENDS']
['right', 'FORIN', 'FOROF', 'BY', 'WHEN']
['right', 'IF', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS']
Expand Down
26 changes: 25 additions & 1 deletion test/generators.coffee
Expand Up @@ -63,7 +63,7 @@ test "empty generator", ->
x = do -> yield return

y = x.next()
ok y.value is undefined and y.done is true
ok y.value is undefined and y.done is true

test "`yield` by itself not at the end of a function errors", ->
throws -> CoffeeScript.compile 'x = -> yield; return'
Expand All @@ -79,3 +79,27 @@ test "yield in if statements", ->

y = x.next 1
ok y.value is 3 and y.done is true

test "symbolic operators has precedence over the `yield`", ->

symbolic = '+ - * / << >> & | || && ** ^ // or and'.split ' '
compound = ("#{op}=" for op in symbolic)
relations = '< > == != <= >= is isnt'.split ' '

operators = [symbolic..., '=', compound..., relations...]

collect = (gen) -> ref.value until (ref = gen.next()).done

values = [0, 1, 2, 3]

for op in operators

expression = "i #{op} 2"

yielded = CoffeeScript.eval "(arr) -> yield #{expression} for i in arr"
mapped = CoffeeScript.eval "(arr) -> (#{expression} for i in arr)"

expected = mapped values
actual = collect yielded values

arrayEq actual, expected

0 comments on commit 4f82e59

Please sign in to comment.