@@ -319,7 +319,7 @@ endfunction
319
319
" TOPLEVEL .body
320
320
" COMMENT .str
321
321
" EXCMD .ea .str
322
- " FUNCTION .ea .body .left .rlist .attr .endfunction
322
+ " FUNCTION .ea .body .left .rlist .default_args . attr .endfunction
323
323
" ENDFUNCTION .ea
324
324
" DELFUNCTION .ea .left
325
325
" RETURN .ea .left
@@ -1358,6 +1358,7 @@ function! s:VimLParser.parse_cmd_function() abort
1358
1358
let node.ea = self .ea
1359
1359
let node.left = left
1360
1360
let node.rlist = []
1361
+ let node.default_args = []
1361
1362
let node.attr = {' range' : 0 , ' abort' : 0 , ' dict' : 0 , ' closure' : 0 }
1362
1363
let node.endfunction = s: NIL
1363
1364
call self .reader.getn (1 )
@@ -1379,6 +1380,12 @@ function! s:VimLParser.parse_cmd_function() abort
1379
1380
let varnode.pos = token.pos
1380
1381
let varnode.value = token.value
1381
1382
call add (node.rlist, varnode)
1383
+ if tokenizer.peek ().type == # s: TOKEN_EQ
1384
+ call tokenizer.get ()
1385
+ call add (node.default_args, self .parse_expr ())
1386
+ elseif len (node.default_args) > 0
1387
+ throw s: Err (' E989: Non-default argument follows default argument' , varnode.pos)
1388
+ endif
1382
1389
" XXX: Vim doesn't skip white space before comma. F(a ,b) => E475
1383
1390
if s: iswhite (self .reader.p (0 )) && tokenizer.peek ().type == # s: TOKEN_COMMA
1384
1391
throw s: Err (' E475: Invalid argument: White space is not allowed before comma' , self .reader.getpos ())
@@ -4962,13 +4969,16 @@ endfunction
4962
4969
function ! s: Compiler .compile_function (node) abort
4963
4970
let left = self .compile (a: node .left )
4964
4971
let rlist = map (a: node .rlist, ' self.compile(v:val)' )
4972
+ let default_args = map (a: node .default_args, ' self.compile(v:val)' )
4965
4973
if ! empty (rlist) && rlist[-1 ] == # ' ...'
4966
4974
let rlist[-1 ] = ' . ...'
4967
4975
endif
4968
4976
if empty (rlist)
4969
4977
call self .out (' (function (%s)' , left )
4978
+ elseif empty (default_args)
4979
+ call self .out (' (function (%s) (%s)' , left , join (rlist, ' ' ))
4970
4980
else
4971
- call self .out (' (function (%s %s)' , left , join (rlist, ' ' ))
4981
+ call self .out (' (function (%s) ( %s) (%s) ' , left , join (rlist, ' ' ), join (default_args , ' ' ))
4972
4982
endif
4973
4983
call self .incindent (' ' )
4974
4984
call self .compile_body (a: node .body)
0 commit comments