Custom syntax for macro expressions?

63 views
Skip to first unread message

Joe Williamson

unread,
Jun 23, 2017, 2:20:04 AM6/23/17
to Haxe
I'm working on a macro tweening system whereby a user can specify a set of fields and values for them to tween to, avoiding the use of dynamics and reflection. At present this set is represented by a Map<ExprOf<FieldAccess>, Expr>.

e.g.
Tween.tween([p.x => 100, p.scale => [x => 50, y => 50]]);

This works, but ideally I would prefer a slightly more compact syntax that closer resembles object notation.

e.g.

Tween.tween({p.x: 100, p.scale: {x: 50, y: 50}});

Unfortunately the parser doesn't expect field expressions on the left hand side of an object assignment. If ":" were a BinOp then I could have an array of binop expressions, but sadly it is not. Is it possible to create custom syntax that doesn't just exploit existing syntax? If not, is there some other way around this?

It's not a huge issue so this is mostly academic. I know macro processing comes after parsing, but in theory it seems like it might be possible to leave cues for the parser to parse something differently, or perhaps see an Expr and hold off on parsing it by turning it into a string to be later parsed by Context.parse. It's a long shot, I know. :P

Nicolas Cannasse

unread,
Jun 24, 2017, 5:00:34 AM6/24/17
to haxe...@googlegroups.com
Hi Joe,

While the macro indeed have ways to somehow introduce new syntaxes by exploiting places where any Expr is allowed (for instance `for( a => b in map )`) in the case of structure declaration the key is a String so does not allow arbitrary expression. You could use "p.x" but I don't think that what you want here.

Another options is to use:

`Tween.tween({ p : { x : 100, scale : { x : 50, y : 50 } }});`

But that's maybe less readable depending of the nest levels you might have.

Best,
Nicolas

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Joe Williamson

unread,
Jun 24, 2017, 7:17:36 AM6/24/17
to Haxe
Hey Nicolas, thanks for the response. The map does the job so I'll stick with that. It's only a very minor aesthetic thing. :)
Reply all
Reply to author
Forward
0 new messages