Skip to content

Commit

Permalink
bare ... in an argument list is now a sugar for ...[]
Browse files Browse the repository at this point in the history
  • Loading branch information
satyr committed Jan 7, 2011
1 parent 3312c35 commit c1764e2
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 71 deletions.
2 changes: 1 addition & 1 deletion extras/coco.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions lib/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ bnf = {
return Value(Literal('this', true));
}), o('Value CALL( ArgList OptComma )CALL', function(){
return Value(Call($1, $3, $2));
}), o('Value CALL( ... )CALL', function(){
return Value(Call($1, null, $2));
})
],
Primitive: [
Expand Down
9 changes: 5 additions & 4 deletions lib/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,12 @@ exports.Call = Call = (function(_super){
function Call(callee, args, sym){
var _this = new _ctor;
_this.callee = callee;
_this.args = args;
_this.args || (_this.args = (_this.splat = true, [Literal('this'), Literal('arguments')]));
if (sym === '?(') {
_this.soak = true;
if (!args || args.length === 1 && args[0] instanceof Splat && args[0].it.isEmpty()) {
_this.splat = true;
args = [Literal('this'), Literal('arguments')];
}
_this.args = args;
_this.soak = sym === '?(';
return _this;
} Call.displayName = 'Call';
_proto.children = ['callee', 'args'];
Expand Down
116 changes: 58 additions & 58 deletions lib/parser.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/grammar.co
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ bnf =
o \Parenthetical ,-> Value $1
o \THIS ,-> Value Literal \this, true

o 'Value CALL( ArgList OptComma )CALL' ,-> Value Call $1, $3 , $2
o 'Value CALL( ... )CALL' ,-> Value Call $1, null, $2
o 'Value CALL( ArgList OptComma )CALL' ,-> Value Call $1, $3, $2
]

Primitive: [
Expand Down Expand Up @@ -178,6 +177,7 @@ bnf =
Arg: [
o \Expression
o '... Expression' ,-> Splat $2
o \... ,-> Splat Arr()
]
# **ArgList** is either the list of objects passed into a function call,
# the parameter list of a function, or the contents of an array literal
Expand Down
8 changes: 5 additions & 3 deletions src/nodes.co
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,11 @@ class exports.Value extends Node
#### Call
# `x(y)`
class exports.Call extends Node
(@callee, @args, sym) =>
@args ||= (@splat = true; [Literal \this; Literal \arguments])
@soak = true if sym is \?(
(@callee, args, sym) =>
if not args or
args.length is 1 and args.0 instanceof Splat and args.0.it.isEmpty()
@splat = true; args = [Literal \this; Literal \arguments]
@<<<{args, soak: sym is \?(}

children: <[ callee args ]>

Expand Down
19 changes: 18 additions & 1 deletion test/splats.co
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ obj =
0: {method: -> this is obj[0]}

eq obj.getNames(), 'moe,jane,ted'
ok obj[obj.index++].method(...[]), 'should cache base value'
ok obj[obj.index++].method(...[0]), 'should cache base value'


quartet = [0 to 3]
Expand Down Expand Up @@ -102,3 +102,20 @@ eq five, 5


eq '0.0', 0.toFixed ...[1]


# [coffee#870](https://github.com/jashkenas/coffee-script/issues/870)
[...[], a] = [1]
eq a, 1

# `...` is same as `...[]`
[..., a] = [1 to 3]
eq a, 3

[a, ..., b] = [1 2]
eq a, 1
eq b, 2

[a, ..., b] = [1 to 5]
eq a, 1
eq b, 5

0 comments on commit c1764e2

Please sign in to comment.