@@ -324,25 +324,74 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
324324 args[1 ] = Expr (headsym, args[1 ]. args... )
325325 headsym = :const
326326 end
327- elseif headsym == Symbol (" />" )
327+ elseif headsym == Symbol (" />" ) || headsym == Symbol ( " \\ > " )
328328 freearg = gensym ()
329- cargs = [args[1 ], freearg, args[2 : end ]. .. ]
330- reorder_parameters! (cargs, 2 )
331- return Expr (:-> , freearg, Expr (:call , cargs... ))
332- elseif headsym == Symbol (" \\ >" )
333- freearg = gensym ()
334- cargs = [args[1 ], args[2 : end ]. .. , freearg]
335- reorder_parameters! (cargs, 2 )
336- return Expr (:-> , freearg, Expr (:call , cargs... ))
329+ callex = only (args)
330+ @assert Meta. isexpr (callex, :call )
331+ args = callex. args
332+ func = headsym == Symbol (" />" ) ?
333+ :(JuliaSyntax. fixbutfirst) :
334+ :(JuliaSyntax. fixbutlast)
335+ if length (args) >= 2 && Meta. isexpr (args[2 ], :parameters )
336+ return Expr (:call , func, args[2 ], args[1 ], args[3 : end ]. .. )
337+ else
338+ return Expr (:call , func, args... )
339+ end
337340 elseif headsym == :chain
338- return Expr (:call , :(JuliaSyntax. chain), args... )
341+ if kind (node_args[1 ]) in KSet " /> \> "
342+ return Expr (:call , :(JuliaSyntax. compose_chain), args... )
343+ else
344+ return Expr (:call , :(JuliaSyntax. chain), args... )
345+ end
339346 end
340347 return Expr (headsym, args... )
341348end
342349
350+ # -------------------------------------------------------------------------------
351+ # Targets for lowering /> and \> syntax
352+
353+ # For use with />
354+ struct FixButFirst{F,Args,Kws}
355+ f:: F
356+ args:: Args
357+ kwargs:: Kws
358+ end
359+
360+ (f:: FixButFirst )(x) = f. f (x, f. args... ; f. kwargs... )
361+
362+ """
363+ Fix all arguments except for the first
364+ """
365+ fixbutfirst (f, args... ; kws... ) = FixButFirst (f, args, kws)
366+
367+ # For use with \>
368+ struct FixButLast{F,Args,Kws}
369+ f:: F
370+ args:: Args
371+ kwargs:: Kws
372+ end
373+
374+ (f:: FixButLast )(x) = f. f (f. args... , x; f. kwargs... )
375+
376+ """
377+ Fix all arguments except for the last
378+ """
379+ fixbutlast (f, args... ; kws... ) = FixButLast (f, args, kws)
380+
343381chain (x, f, fs... ) = chain (f (x), fs... )
344382chain (x) = x
345383
384+ struct ComposeChain{Funcs}
385+ fs:: Funcs
386+ end
387+
388+ (f:: ComposeChain )(x) = chain (x, f. fs... )
389+
390+ compose_chain (fs... ) = ComposeChain (fs)
391+
392+
393+ # -------------------------------------------------------------------------------
394+
346395Base. Expr (node:: SyntaxNode ) = _to_expr (node)
347396
348397function build_tree (:: Type{Expr} , stream:: ParseStream ; kws... )
0 commit comments