Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequences of ASCII characters available for use as operators in macros

Tags:

julia

The RecipesBase.jl @recipe macro makes use of a couple special operators constructed out of ASCII characters, namely --> and :=. These character sequences seem to have some special attribute that allows them to be parsed into an Expr. Compare --> to --:

julia> 1 --> 2
ERROR: syntax: invalid syntax 1 --> 2

julia> 1 -- 2
ERROR: syntax: invalid operator "--"

julia> :(1 --> 2)
:($(Expr(:-->, 1, 2)))

julia> :(1 -- 2)
ERROR: syntax: invalid operator "--"

Interestingly, 1 --> 2 is parsed with an expression head of :-->, whereas other binary operators, including Unicode binary operators such as (typed as \uparrow + TAB), are parsed with an expression head of :call:

julia> dump(:(1 --> 2))
Expr
  head: Symbol -->
  args: Array{Any}((2,))
    1: Int64 1
    2: Int64 2

julia> dump(:(1 ↑ 2))
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol ↑
    2: Int64 1
    3: Int64 2

So, I have a few related questions:

  1. What's up with --> and :=? (EDIT: In other words, why are those character sequences specially parsed?)
  2. Are there other sequences of ASCII characters that behave similarly to --> and := and that can therefore be used as operators in macros?
  3. Is there documentation somewhere that lists the various "special" sequences of ASCII characters?
like image 864
Cameron Bieganek Avatar asked Oct 13 '19 16:10

Cameron Bieganek


1 Answers

--> and := are specially parsed by the Julia parser.

Take a look at this file: https://github.com/JuliaLang/julia/blob/f54cdf45a9e04f1450ba22142ddac8234389fe05/src/julia-parser.scm

It lists all of the specially parsed character sequences, and I'm pretty sure you can also get the associativity from it.

like image 136
Anshul Singhvi Avatar answered Oct 07 '22 11:10

Anshul Singhvi