node.rs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. use super::*;
  2. use proc_macro2::{Span, TokenStream as TokenStream2};
  3. use quote::{quote, ToTokens, TokenStreamExt};
  4. use syn::{
  5. braced,
  6. parse::{Parse, ParseStream},
  7. spanned::Spanned,
  8. token, Expr, ExprIf, LitStr, Pat, Result,
  9. };
  10. /*
  11. Parse
  12. -> div {}
  13. -> Component {}
  14. -> component()
  15. -> "text {with_args}"
  16. -> (0..10).map(|f| rsx!("asd")), // <--- notice the comma - must be a complete expr
  17. */
  18. #[derive(PartialEq, Eq, Clone, Debug, Hash)]
  19. pub enum BodyNode {
  20. Element(Element),
  21. Component(Component),
  22. ForLoop(ForLoop),
  23. IfChain(ExprIf),
  24. Text(IfmtInput),
  25. RawExpr(Expr),
  26. }
  27. impl BodyNode {
  28. pub fn is_litstr(&self) -> bool {
  29. matches!(self, BodyNode::Text { .. })
  30. }
  31. pub fn span(&self) -> Span {
  32. match self {
  33. BodyNode::Element(el) => el.name.span(),
  34. BodyNode::Component(component) => component.name.span(),
  35. BodyNode::Text(text) => text.source.span(),
  36. BodyNode::RawExpr(exp) => exp.span(),
  37. BodyNode::ForLoop(fl) => fl.for_token.span(),
  38. BodyNode::IfChain(f) => f.if_token.span(),
  39. }
  40. }
  41. }
  42. impl Parse for BodyNode {
  43. fn parse(stream: ParseStream) -> Result<Self> {
  44. if stream.peek(LitStr) {
  45. return Ok(BodyNode::Text(stream.parse()?));
  46. }
  47. let body_stream = stream.fork();
  48. if let Ok(path) = body_stream.parse::<syn::Path>() {
  49. // this is an Element if path match of:
  50. // - one ident
  51. // - followed by `{`
  52. // - 1st char is lowercase
  53. // - no underscores (reserved for components)
  54. //
  55. // example:
  56. // div {}
  57. if let Some(ident) = path.get_ident() {
  58. let el_name = ident.to_string();
  59. let first_char = el_name.chars().next().unwrap();
  60. if body_stream.peek(token::Brace)
  61. && first_char.is_ascii_lowercase()
  62. && !el_name.contains('_')
  63. {
  64. return Ok(BodyNode::Element(stream.parse::<Element>()?));
  65. }
  66. }
  67. // Otherwise this should be Component, allowed syntax:
  68. // - syn::Path
  69. // - PathArguments can only apper in last segment
  70. // - followed by `{` or `(`, note `(` cannot be used with one ident
  71. //
  72. // example
  73. // Div {}
  74. // ::Div {}
  75. // crate::Div {}
  76. // component {} <-- already handled by elements
  77. // ::component {}
  78. // crate::component{}
  79. // Input::<InputProps<'_, i32> {}
  80. // crate::Input::<InputProps<'_, i32> {}
  81. if body_stream.peek(token::Brace) {
  82. Component::validate_component_path(&path)?;
  83. return Ok(BodyNode::Component(stream.parse()?));
  84. }
  85. }
  86. // Transform for loops into into_iter calls
  87. if stream.peek(Token![for]) {
  88. return Ok(BodyNode::ForLoop(stream.parse()?));
  89. }
  90. // Transform unterminated if statements into terminated optional if statements
  91. if stream.peek(Token![if]) {
  92. return Ok(BodyNode::IfChain(stream.parse()?));
  93. }
  94. Ok(BodyNode::RawExpr(stream.parse::<Expr>()?))
  95. }
  96. }
  97. impl ToTokens for BodyNode {
  98. fn to_tokens(&self, tokens: &mut TokenStream2) {
  99. match &self {
  100. BodyNode::Element(el) => el.to_tokens(tokens),
  101. BodyNode::Component(comp) => comp.to_tokens(tokens),
  102. BodyNode::Text(txt) => tokens.append_all(quote! {
  103. __cx.text_node(#txt)
  104. }),
  105. BodyNode::RawExpr(exp) => tokens.append_all(quote! {
  106. __cx.make_node(#exp)
  107. }),
  108. BodyNode::ForLoop(exp) => {
  109. let ForLoop {
  110. pat, expr, body, ..
  111. } = exp;
  112. let renderer: TemplateRenderer = TemplateRenderer { roots: body };
  113. tokens.append_all(quote! {
  114. __cx.make_node(
  115. (#expr).into_iter().map(|#pat| { #renderer })
  116. )
  117. })
  118. }
  119. BodyNode::IfChain(chain) => {
  120. if is_if_chain_terminated(chain) {
  121. tokens.append_all(quote! {
  122. __cx.make_node(#chain)
  123. });
  124. } else {
  125. let ExprIf {
  126. cond,
  127. then_branch,
  128. else_branch,
  129. ..
  130. } = chain;
  131. let mut body = TokenStream2::new();
  132. body.append_all(quote! {
  133. if #cond {
  134. Some(#then_branch)
  135. }
  136. });
  137. let mut elif = else_branch;
  138. while let Some((_, ref branch)) = elif {
  139. match branch.as_ref() {
  140. Expr::If(ref eelif) => {
  141. let ExprIf {
  142. cond,
  143. then_branch,
  144. else_branch,
  145. ..
  146. } = eelif;
  147. body.append_all(quote! {
  148. else if #cond {
  149. Some(#then_branch)
  150. }
  151. });
  152. elif = else_branch;
  153. }
  154. _ => {
  155. body.append_all(quote! {
  156. else {
  157. #branch
  158. }
  159. });
  160. break;
  161. }
  162. }
  163. }
  164. body.append_all(quote! {
  165. else { None }
  166. });
  167. tokens.append_all(quote! {
  168. __cx.make_node(#body)
  169. });
  170. }
  171. }
  172. }
  173. }
  174. }
  175. #[derive(PartialEq, Eq, Clone, Debug, Hash)]
  176. pub struct ForLoop {
  177. pub for_token: Token![for],
  178. pub pat: Pat,
  179. pub in_token: Token![in],
  180. pub expr: Box<Expr>,
  181. pub body: Vec<BodyNode>,
  182. pub brace_token: token::Brace,
  183. }
  184. impl Parse for ForLoop {
  185. fn parse(input: ParseStream) -> Result<Self> {
  186. let for_token: Token![for] = input.parse()?;
  187. let pat = input.parse()?;
  188. let in_token: Token![in] = input.parse()?;
  189. let expr: Expr = input.call(Expr::parse_without_eager_brace)?;
  190. let content;
  191. let brace_token = braced!(content in input);
  192. let mut children = vec![];
  193. while !content.is_empty() {
  194. children.push(content.parse()?);
  195. }
  196. Ok(Self {
  197. for_token,
  198. pat,
  199. in_token,
  200. body: children,
  201. expr: Box::new(expr),
  202. brace_token,
  203. })
  204. }
  205. }
  206. fn is_if_chain_terminated(chain: &ExprIf) -> bool {
  207. let mut current = chain;
  208. loop {
  209. if let Some((_, else_block)) = &current.else_branch {
  210. if let Expr::If(else_if) = else_block.as_ref() {
  211. current = else_if;
  212. } else {
  213. return true;
  214. }
  215. } else {
  216. return false;
  217. }
  218. }
  219. }