Cameron Bieganek
Cameron Bieganek

Reputation: 7674

Sequences of ASCII characters available for use as operators in macros

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?

Upvotes: 7

Views: 142

Answers (1)

Anshul Singhvi
Anshul Singhvi

Reputation: 1742

--> 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.

Upvotes: 4

Related Questions