@@ -488,6 +488,7 @@ function ExArg() {
488
488
// node rest
489
489
// node[] list
490
490
// node[] rlist
491
+ // node[] default_args
491
492
// node[] body
492
493
// string op
493
494
// string str
@@ -497,7 +498,7 @@ function ExArg() {
497
498
// TOPLEVEL .body
498
499
// COMMENT .str
499
500
// EXCMD .ea .str
500
- // FUNCTION .ea .body .left .rlist .attr .endfunction
501
+ // FUNCTION .ea .body .left .rlist .default_args . attr .endfunction
501
502
// ENDFUNCTION .ea
502
503
// DELFUNCTION .ea .left
503
504
// RETURN .ea .left
@@ -1639,6 +1640,7 @@ VimLParser.prototype.parse_cmd_function = function() {
1639
1640
node . ea = this . ea ;
1640
1641
node . left = left ;
1641
1642
node . rlist = [ ] ;
1643
+ node . default_args = [ ] ;
1642
1644
node . attr = { "range" :0 , "abort" :0 , "dict" :0 , "closure" :0 } ;
1643
1645
node . endfunction = NIL ;
1644
1646
this . reader . getn ( 1 ) ;
@@ -1662,6 +1664,13 @@ VimLParser.prototype.parse_cmd_function = function() {
1662
1664
varnode . pos = token . pos ;
1663
1665
varnode . value = token . value ;
1664
1666
viml_add ( node . rlist , varnode ) ;
1667
+ if ( tokenizer . peek ( ) . type == TOKEN_EQ ) {
1668
+ tokenizer . get ( ) ;
1669
+ viml_add ( node . default_args , this . parse_expr ( ) ) ;
1670
+ }
1671
+ else if ( viml_len ( node . default_args ) > 0 ) {
1672
+ throw Err ( "E989: Non-default argument follows default argument" , varnode . pos ) ;
1673
+ }
1665
1674
// XXX: Vim doesn't skip white space before comma. F(a ,b) => E475
1666
1675
if ( iswhite ( this . reader . p ( 0 ) ) && tokenizer . peek ( ) . type == TOKEN_COMMA ) {
1667
1676
throw Err ( "E475: Invalid argument: White space is not allowed before comma" , this . reader . getpos ( ) ) ;
@@ -4465,14 +4474,18 @@ Compiler.prototype.compile_excmd = function(node) {
4465
4474
Compiler . prototype . compile_function = function ( node ) {
4466
4475
var left = this . compile ( node . left ) ;
4467
4476
var rlist = node . rlist . map ( ( function ( vval ) { return this . compile ( vval ) ; } ) . bind ( this ) ) ;
4477
+ var default_args = node . default_args . map ( ( function ( vval ) { return this . compile ( vval ) ; } ) . bind ( this ) ) ;
4468
4478
if ( ! viml_empty ( rlist ) && rlist [ rlist . length - 1 ] == "..." ) {
4469
4479
rlist [ rlist . length - 1 ] = ". ..." ;
4470
4480
}
4471
4481
if ( viml_empty ( rlist ) ) {
4472
4482
this . out ( "(function (%s)" , left ) ;
4473
4483
}
4484
+ else if ( viml_empty ( default_args ) ) {
4485
+ this . out ( "(function (%s) (%s)" , left , viml_join ( rlist , " " ) ) ;
4486
+ }
4474
4487
else {
4475
- this . out ( "(function (%s %s)" , left , viml_join ( rlist , " " ) ) ;
4488
+ this . out ( "(function (%s) ( %s) (%s) " , left , viml_join ( rlist , " " ) , viml_join ( default_args , " " ) ) ;
4476
4489
}
4477
4490
this . incindent ( " " ) ;
4478
4491
this . compile_body ( node . body ) ;
0 commit comments