@@ -303,25 +303,74 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
303
303
args[1 ] = Expr (headsym, args[1 ]. args... )
304
304
headsym = :const
305
305
end
306
- elseif headsym == Symbol (" />" )
306
+ elseif headsym == Symbol (" />" ) || headsym == Symbol ( " \\ > " )
307
307
freearg = gensym ()
308
- cargs = [args[1 ], freearg, args[2 : end ]. .. ]
309
- reorder_parameters! (cargs, 2 )
310
- return Expr (:-> , freearg, Expr (:call , cargs... ))
311
- elseif headsym == Symbol (" \\ >" )
312
- freearg = gensym ()
313
- cargs = [args[1 ], args[2 : end ]. .. , freearg]
314
- reorder_parameters! (cargs, 2 )
315
- return Expr (:-> , freearg, Expr (:call , cargs... ))
308
+ callex = only (args)
309
+ @assert Meta. isexpr (callex, :call )
310
+ args = callex. args
311
+ func = headsym == Symbol (" />" ) ?
312
+ :(JuliaSyntax. fixbutfirst) :
313
+ :(JuliaSyntax. fixbutlast)
314
+ if length (args) >= 2 && Meta. isexpr (args[2 ], :parameters )
315
+ return Expr (:call , func, args[2 ], args[1 ], args[3 : end ]. .. )
316
+ else
317
+ return Expr (:call , func, args... )
318
+ end
316
319
elseif headsym == :chain
317
- return Expr (:call , :(JuliaSyntax. chain), args... )
320
+ if kind (node_args[1 ]) in KSet " /> \> "
321
+ return Expr (:call , :(JuliaSyntax. compose_chain), args... )
322
+ else
323
+ return Expr (:call , :(JuliaSyntax. chain), args... )
324
+ end
318
325
end
319
326
return Expr (headsym, args... )
320
327
end
321
328
329
+ # -------------------------------------------------------------------------------
330
+ # Targets for lowering /> and \> syntax
331
+
332
+ # For use with />
333
+ struct FixButFirst{F,Args,Kws}
334
+ f:: F
335
+ args:: Args
336
+ kwargs:: Kws
337
+ end
338
+
339
+ (f:: FixButFirst )(x) = f. f (x, f. args... ; f. kwargs... )
340
+
341
+ """
342
+ Fix all arguments except for the first
343
+ """
344
+ fixbutfirst (f, args... ; kws... ) = FixButFirst (f, args, kws)
345
+
346
+ # For use with \>
347
+ struct FixButLast{F,Args,Kws}
348
+ f:: F
349
+ args:: Args
350
+ kwargs:: Kws
351
+ end
352
+
353
+ (f:: FixButLast )(x) = f. f (f. args... , x; f. kwargs... )
354
+
355
+ """
356
+ Fix all arguments except for the last
357
+ """
358
+ fixbutlast (f, args... ; kws... ) = FixButLast (f, args, kws)
359
+
322
360
chain (x, f, fs... ) = chain (f (x), fs... )
323
361
chain (x) = x
324
362
363
+ struct ComposeChain{Funcs}
364
+ fs:: Funcs
365
+ end
366
+
367
+ (f:: ComposeChain )(x) = chain (x, f. fs... )
368
+
369
+ compose_chain (fs... ) = ComposeChain (fs)
370
+
371
+
372
+ # -------------------------------------------------------------------------------
373
+
325
374
Base. Expr (node:: SyntaxNode ) = _to_expr (node)
326
375
327
376
function build_tree (:: Type{Expr} , stream:: ParseStream ; kws... )
0 commit comments