stoically / syn-rsx

syn-powered parser for JSX-like TokenStreams

Home Page:https://crates.io/crates/syn-rsx

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nested RSX expressions

chichid opened this issue · comments

Would it be possible (or is it planned) to support nested RSX expression within Blocks, without having to specify rsx! for each sub expression.

For instance can this:
quote! { <View> { children_array.map(|child| { let my_child = rsx! { <Test /> } some other code... my_child }) } </View>

Be simplified to this (without the nested RSX)
quote! { <View> { children_array.map(|child| { let my_child = <Test /> some other code... my_child }) } </View>

I've asked something similar upstream (dtolnay/syn#793), but after some thought my conclusion was no, not possible, unfortunately. I'd be interested in a solution if someone happens to stumble over one.

I think the only solution if syn is not ok adding that, is a fork of syn that allows that :-). I believe the ParseBuffer from syn is a good starting point.

I don't think that works. The crucial part is dtolnay/syn#793 (comment).

let uwu = <div></div>

simply isn't valid rust code, which means it won't compile and won't be passed to a proc macro.

I mean, sure, you can pass the whole thing to a proc macro like so,

some_macro!{ let uwu = <div></div>; }

but at that point you start reimplementing the rust parser. That would work, but will be a huge effort. Good luck with that if you want to go down that route.

yeah that's what I'm asking above. For sure attribute-like macros won't support it.

As for the problem of nested expressions, I believe doesn't necessarily involve the rust parser. All what's needed is to add "JSX" as a supported Syn expression. There are 40 today.
It sure is going to be challenging but it's in the feasible range.
Btw, it's how JSX is defined for React. The JSXExpression is an extension of the primary extension in Javascript.

I see. Yeah, I guess having the ability to hook into certain points in syns parsing might work without adding JSX/RSX parsing itself to syn.

Closing this bug for now as it answers my question, I may reopen it if I contribute something to syn to allow for hooks, and it makes it through obviously. But syn_rsx is definitely the right step into bringing true JSX ergonomics to the rust world.