0

Is it possible to use Sweet.Js to expand

{ "foo": "bar" }

to

{ "bar": "foo" }

for example?

My naïve attempt here doesn’t work and I don’t see an example in the documentation.

Thanks.

Ethan Kent
  • 381
  • 1
  • 4
  • 20

1 Answers1

1

The trick is that : has special meaning in patterns so you need to escape it with $[:]

macro bar {
  rule { {$x $[:] $y} } =>  { {$y: $x} }
}

var o = bar { "bax": "quux" }
timdisney
  • 5,287
  • 9
  • 35
  • 31
  • To try this (and a few other examples), I pasted your code into the editor at http://sweetjs.org/browser/editor.html, and hit "Compile". But I get this error: `Error: [assertion error]: cannot get the val of a delimiter`. How do I use this? – Sphinxxx Mar 30 '16 at 22:14