Skip to content

Commit

Permalink
Replace \> with />>
Browse files Browse the repository at this point in the history
This makes it visually clear which argument is being piped into
  • Loading branch information
c42f committed Feb 18, 2023
1 parent 5f4be24 commit a72c62a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
args[1] = Expr(headsym, args[1].args...)
headsym = :const
end
elseif headsym == Symbol("/>") || headsym == Symbol("\\>")
elseif headsym == Symbol("/>") || headsym == Symbol("/>>")
callex = only(args)
@assert Meta.isexpr(callex, :call)
args = callex.args
Expand All @@ -350,7 +350,7 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
return Expr(:call, func, args...)
end
elseif headsym == :chain
if kind(node_args[1]) in KSet"/> \>"
if kind(node_args[1]) in KSet"/> />>"
return Expr(:call, :(JuliaSyntax.compose_chain), args...)
else
return Expr(:call, :(JuliaSyntax.chain), args...)
Expand All @@ -360,7 +360,7 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
end

#-------------------------------------------------------------------------------
# Targets for lowering /> and \> syntax
# Targets for lowering /> and />> syntax

# For use with />
struct FixButFirst{F,Args,Kws}
Expand All @@ -376,7 +376,7 @@ Fix all arguments except for the first
"""
fixbutfirst(f, args...; kws...) = FixButFirst(f, args, kws)

# For use with \>
# For use with />>
struct FixButLast{F,Args,Kws}
f::F
args::Args
Expand All @@ -394,7 +394,7 @@ chain(x, f, fs...) = chain(f(x), fs...)
chain(x) = x

# An example of how chain() can be used to rewrite
# `x \> map(f) \> reduce(g)` into `mapreduce(f, g, x)`
# `x />> map(f) />> reduce(g)` into `mapreduce(f, g, x)`
function chain(x, f1::FixButLast{typeof(map)}, f2::FixButLast{typeof(reduce)}, fs...)
chain(x, fixbutlast(mapreduce, f1.args..., f2.args...; f1.kwargs..., f2.kwargs...), fs...)
end
Expand Down
2 changes: 1 addition & 1 deletion src/kinds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ const _kind_names =
".'"
"->"
"/>"
"\\>"
"/>>"

"BEGIN_UNICODE_OPS"
"¬"
Expand Down
6 changes: 3 additions & 3 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -826,14 +826,14 @@ end
function parse_curry_chain(ps::ParseState)
mark = position(ps)
nterms = 0
if (k = peek(ps); k != K"/>" && k != K"\>")
if (k = peek(ps); k != K"/>" && k != K"/>>")
# x /> f(a) ==> (chain x (/> (call f a)))
parse_range(ps)
nterms += 1
else
# /> f(a) ==> (/> (call f a))
end
while (k = peek(ps); k == K"/>" || k == K"\>")
while (k = peek(ps); k == K"/>" || k == K"/>>")
m = position(ps)
bump(ps, TRIVIA_FLAG)
parse_range(ps)
Expand All @@ -847,7 +847,7 @@ function parse_curry_chain(ps::ParseState)
# x /> f(a) /> g(b) ==> (chain x (/> (call f a)) (/> (call g b)))
# x /> A.f(a,b) ==> (chain x (/> (call (. A (quote f)) a b)))
# /> f(a) /> g(b) ==> (chain (/> (call f a)) (/> (call g b)))
# x /> f() \> g() ==> (chain x (/> (call f)) (\> (call g)))
# x /> f() />> g() ==> (chain x (/> (call f)) (/>> (call g)))
# x /> $call ==> (chain x (/> ($ call)))
# x /> notcall[] ==> (chain x (/> (error (ref notcall))))
emit(ps, mark, K"chain")
Expand Down
8 changes: 5 additions & 3 deletions src/tokenize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,11 @@ function lex_forwardslash(l::Lexer)
elseif accept(l, '=')
return emit(l, K"/=")
elseif accept(l, '>')
return emit(l, K"/>")
if accept(l, '>')
return emit(l, K"/>>")
else
return emit(l, K"/>")
end
else
return emit(l, K"/")
end
Expand All @@ -942,8 +946,6 @@ end
function lex_backslash(l::Lexer)
if accept(l, '=')
return emit(l, K"\=")
elseif accept(l, '>')
return emit(l, K"\>")
end
return emit(l, K"\\")
end
Expand Down
2 changes: 1 addition & 1 deletion test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ tests = [
"x /> f(a) /> g(b)" => "(chain x (/> (call f a)) (/> (call g b)))"
"x /> A.f(a,b)" => "(chain x (/> (call (. A (quote f)) a b)))"
"/> f(a) /> g(b)" => "(chain (/> (call f a)) (/> (call g b)))"
"x /> f() \\> g()" => "(chain x (/> (call f)) (\\> (call g)))"
"x /> f() />> g()" => "(chain x (/> (call f)) (/>> (call g)))"
"x /> \$call" => "(chain x (/> (\$ call)))"
"x /> notcall[]" => "(chain x (/> (error (ref notcall))))"
],
Expand Down

0 comments on commit a72c62a

Please sign in to comment.