sweet-js / sweet-core

Sweeten your JavaScript.
https://www.sweetjs.org
BSD 2-Clause "Simplified" License
4.58k stars 208 forks source link

Operator syntax not working for "^^", "->" and others #707

Open jayrbolton opened 7 years ago

jayrbolton commented 7 years ago

for example:

 operator ^^ left 1 = (l, r) => #``

Throws:

Error: expecting an identifier
__^__ left 1 = ( l , r ) => #` `

The same happens with -> as well, but it works for me with the tutorial example >>=

gabejohnson commented 7 years ago

^^ and -> aren't valid JavaScript identifiers. If it's not already an operator or keyword and you can't do

var ^^ = 5;

Then you can't use it to define an operator.

That will change with #687.

@disnet should we make this explicit in the docs?

Edit: Added note about pre-existing operators and keywords

jayrbolton commented 7 years ago

Thanks for the reply, I think a note about that in the docs would be good. Also note the example with >>= right here: http://www.sweetjs.org/doc/tutorial#sweet-operators. As far I know you can't do var >>= = 5; either.

gabejohnson commented 7 years ago

You got me 😄 .

It works with pre-existing operators and keywords too. I'll update the comment.

disnet commented 7 years ago

Syntax bindings (both syntax ... and operator ...) allow identifiers, keywords, and punctuators to be the binding, while runtime declarations can only be identifiers and keywords (depending on the context). The reason ^^ doesn't work is that it is actually two ^ tokens. Some of the existing operators "look" like two tokens but are lexed as one (e.g. >>=, =>, etc.). Readtables will essentially allow you to change the lexer and make ^^ into a single token.