Macro to combine field expressions?

61 views
Skip to first unread message

Joe Williamson

unread,
Jun 25, 2017, 2:54:17 PM6/25/17
to Haxe
I'm looking for a macro to combine two field expressions which may also include arrays, calls, casts and potentially other expressions that I haven't thought of.

For example, a.b[0] and c().d would become a.b[0].c().d.

I think to write such a macro would involve traversing down the right hand expression tree until reaching a EConst CIdent and turning it into an EField of the left hand expression. Does this sound sensible?

It certainly seems like something that probably already exists, though. Does anyone know where I could find it if so?

Juraj Kirchheim

unread,
Jun 25, 2017, 3:15:00 PM6/25/17
to haxe...@googlegroups.com
On Sun, Jun 25, 2017 at 8:54 PM, Joe Williamson <hur...@gmail.com> wrote:
I'm looking for a macro to combine two field expressions which may also include arrays, calls, casts and potentially other expressions that I haven't thought of.

For example, a.b[0] and c().d would become a.b[0].c().d.

I think to write such a macro would involve traversing down the right hand expression tree until reaching a EConst CIdent and turning it into an EField of the left hand expression. Does this sound sensible?

Sounds exactly like what you're after. You could probably pull of something with Context.parseInlineString, but the error reporting will almost certainly be less accurate.

It certainly seems like something that probably already exists, though. Does anyone know where I could find it if so?

FWIW I'm relatively sure that I haven't seen it anywhere.

Best,
Juraj
Message has been deleted

Juraj Kirchheim

unread,
Jun 25, 2017, 5:36:36 PM6/25/17
to haxe...@googlegroups.com
On Sun, Jun 25, 2017 at 10:43 PM, Joe Williamson <hur...@gmail.com> wrote:
Thanks, Juraj. Simn suggested a solution with parse, also, converting the Exprs into strings, concatenating with a "." and then parsing it again. I'm not sure how to convert the Exprs into code strings, though. Do you know how I can do this?

This is done with haxe.macro.Printer. The most simple way is using haxe.macro.Tools; and then just use  expr.toString().

Best,
Juraj

Joe Williamson

unread,
Jun 26, 2017, 3:04:47 PM6/26/17
to Haxe
Thanks! Not sure how I didn't know about that given how useful it is. It's all working now. :)
Reply all
Reply to author
Forward
0 new messages