wip: add whitespace parsing to rsx

This commit is contained in:
Jonathan Kelley committed 2022-09-30 10:51:53 -07:00
1 parent 5313fc0cc6
commit 181a51a63b
14 files changed
+278 -24

No files matched your search

+3 -5
View File
@@ -238,11 +238,9 @@ impl Parse for ComponentField {
let forked = input.fork();
let t: LitStr = forked.parse()?;
// the string literal must either be the end of the input or a followed by a comma
if forked.is_empty() || forked.peek(Token![,]) {
if is_literal_foramtted(&t) {
let content = ContentField::Formatted(input.parse()?);
return Ok(Self { name, content });
}
if (forked.is_empty() || forked.peek(Token![,])) && is_literal_foramtted(&t) {
let content = ContentField::Formatted(input.parse()?);
return Ok(Self { name, content });
}
}
+3
View File
@@ -18,6 +18,9 @@ mod component;
mod element;
mod ifmt;
mod node;
mod whitespace;
mod raw_expr;
mod text;
// Re-export the namespaces into each other
pub use component::*;
+10 -9
View File
@@ -1,3 +1,5 @@
use crate::{raw_expr::RawExprNode, text::TextNode};
use super::*;
use proc_macro2::{Span, TokenStream as TokenStream2};
@@ -5,7 +7,7 @@ use quote::{quote, ToTokens, TokenStreamExt};
use syn::{
parse::{Parse, ParseStream},
spanned::Spanned,
token, Expr, LitStr, Result,
token, LitStr, Result,
};
/*
@@ -16,17 +18,17 @@ Parse
-> "text {with_args}"
-> (0..10).map(|f| rsx!("asd")), // <--- notice the comma - must be a complete expr
*/
#[derive(PartialEq, Eq, Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum BodyNode {
Element(Element),
Component(Component),
Text(LitStr),
RawExpr(Expr),
Text(TextNode),
RawExpr(RawExprNode),
}
impl BodyNode {
pub fn is_litstr(&self) -> bool {
matches!(self, BodyNode::Text(_))
matches!(self, BodyNode::Text { .. })
}
pub fn span(&self) -> Span {
@@ -83,12 +85,11 @@ impl Parse for BodyNode {
// crate::Input::<InputProps<'_, i32> {}
if body_stream.peek(token::Brace) {
Component::validate_component_path(&path)?;
return Ok(BodyNode::Component(stream.parse()?));
}
}
Ok(BodyNode::RawExpr(stream.parse::<Expr>()?))
Ok(BodyNode::RawExpr(stream.parse()?))
}
}
@@ -97,8 +98,8 @@ impl ToTokens for BodyNode {
match &self {
BodyNode::Element(el) => el.to_tokens(tokens),
BodyNode::Component(comp) => comp.to_tokens(tokens),
BodyNode::Text(txt) => tokens.append_all(quote! {
__cx.text(format_args_f!(#txt))
BodyNode::Text(text) => tokens.append_all(quote! {
__cx.text(format_args_f!(#text))
}),
BodyNode::RawExpr(exp) => tokens.append_all(quote! {
__cx.fragment_from_iter(#exp)
+41
View File
@@ -0,0 +1,41 @@
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{ToTokens, TokenStreamExt};
use syn::{parse::Parse, spanned::Spanned, Expr};
use crate::whitespace::Whitespace;
#[derive(Debug, PartialEq, Eq)]
pub struct RawExprNode {
pub ws: Whitespace,
pub expr: Expr,
}
impl RawExprNode {
pub fn span(&self) -> Span {
self.expr.span()
}
}
impl Parse for RawExprNode {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let before_cursor = input.cursor();
let expr = input.parse()?;
let after = input.cursor();
dbg!(before_cursor.span().start(), after.span().end());
Ok(Self {
ws: Whitespace::default(),
expr,
})
}
}
impl ToTokens for RawExprNode {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let expr = &self.expr;
tokens.append_all(quote::quote! { #expr });
}
}
+33
View File
@@ -0,0 +1,33 @@
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{quote, ToTokens, TokenStreamExt};
use syn::{parse::Parse, LitStr};
use crate::whitespace::Whitespace;
#[derive(Debug, PartialEq, Eq)]
pub struct TextNode {
pub ws: Whitespace,
pub text: LitStr,
}
impl TextNode {
pub fn span(&self) -> Span {
self.text.span()
}
}
impl Parse for TextNode {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
Ok(Self {
ws: Whitespace::default(),
text: input.parse()?,
})
}
}
impl ToTokens for TextNode {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let text = &self.text;
tokens.append_all(quote! { #text });
}
}
+17
View File
@@ -0,0 +1,17 @@
#![allow(dead_code)]
use proc_macro2::Span;
#[derive(Debug, Default)]
pub struct Whitespace {
span_start: Option<Span>,
span_end: Option<Span>,
}
// right now we dont care if whitespace is different, sorry
impl Eq for Whitespace {}
impl PartialEq for Whitespace {
fn eq(&self, _other: &Self) -> bool {
true
}
}