Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to syn 2 #54

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ license = "MIT"
bench = false

[dependencies]
proc-macro2 = "1.0.47"
quote = "1.0.21"
syn = { version = "1.0.102", features = ["full", "parsing", "extra-traits"] }
thiserror = "1.0.37"
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "2.0", features = ["full", "parsing", "extra-traits"] }
thiserror = "1.0"

[dev-dependencies]
criterion = "0.4.0"
eyre = "0.6.8"
criterion = "0.4"
eyre = "0.6"

[[bench]]
name = "bench"
Expand Down
6 changes: 3 additions & 3 deletions examples/html-to-string-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ license = "MIT"
proc-macro = true

[dependencies]
proc-macro2 = "1.0.47"
quote = "1.0.21"
syn = "1.0.102"
proc-macro2 = "1.0"
quote = "1.0"
syn = "2.0"
syn-rsx = { path = "../../" }
97 changes: 65 additions & 32 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
use std::{convert::TryFrom, fmt, ops::Deref};

use proc_macro2::{Punct, Span, TokenStream};
use quote::ToTokens;
use quote::{ToTokens, quote_spanned};
use syn::{
punctuated::{Pair, Punctuated},
spanned::Spanned,
Expr, ExprBlock, ExprLit, ExprPath, Ident, Lit,
};

Expand Down Expand Up @@ -87,16 +86,16 @@ impl Node {
}
}

impl Spanned for Node {
dgsantana marked this conversation as resolved.
Show resolved Hide resolved
fn span(&self) -> Span {
impl ToTokens for Node {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Node::Element(node) => node.span(),
Node::Attribute(node) => node.span(),
Node::Text(node) => node.span(),
Node::Comment(node) => node.span(),
Node::Doctype(node) => node.span(),
Node::Block(node) => node.span(),
Node::Fragment(node) => node.span(),
Node::Attribute(a) => a.to_tokens(tokens),
Node::Block(b) => b.to_tokens(tokens),
Node::Comment(c) => c.to_tokens(tokens),
Node::Doctype(d) => d.to_tokens(tokens),
Node::Fragment(f) => f.to_tokens(tokens),
Node::Element(e) => e.to_tokens(tokens),
Node::Text(t) => t.to_tokens(tokens),
}
}
}
Expand Down Expand Up @@ -144,9 +143,21 @@ impl fmt::Display for NodeElement {
}
}

impl Spanned for NodeElement {
fn span(&self) -> Span {
self.span
impl ToTokens for NodeElement {
fn to_tokens(&self, tokens: &mut TokenStream) {

let name = &self.name;
let attributes = &self.attributes;
let children = &self.children;

// self closing
if children.is_empty() {
tokens.extend(quote_spanned!(self.span =>
<#name #(#attributes)* /> ))
} else {
tokens.extend(quote_spanned!(self.span =>
<#name #(#attributes)*> #(#children)* </#name> ))
}
}
}

Expand All @@ -172,9 +183,20 @@ impl fmt::Display for NodeAttribute {
}
}

impl Spanned for NodeAttribute {
fn span(&self) -> Span {
self.span
impl ToTokens for NodeAttribute {
fn to_tokens(&self, tokens: &mut TokenStream) {

let key = &self.key;
let value = &self.value;

// self closing
if let Some(value) = value{
tokens.extend(quote_spanned!(self.span =>
#key = #value ))
} else {
tokens.extend(quote_spanned!(self.span =>
#key ))
}
}
}

Expand All @@ -197,9 +219,9 @@ impl fmt::Display for NodeText {
}
}

impl Spanned for NodeText {
fn span(&self) -> Span {
self.value.span()
impl ToTokens for NodeText {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.value.to_tokens(tokens);
}
}

Expand All @@ -224,12 +246,14 @@ impl fmt::Display for NodeComment {
}
}

impl Spanned for NodeComment {
fn span(&self) -> Span {
self.span
impl ToTokens for NodeComment {
fn to_tokens(&self, tokens: &mut TokenStream) {
let value = &self.value;
tokens.extend(quote_spanned!(self.span=> <!-- #value -->))
}
}


/// Doctype node.
///
/// Doctype declaration: `<!DOCTYPE html>` (case insensitive), `html` is the
Expand All @@ -251,9 +275,10 @@ impl fmt::Display for NodeDoctype {
}
}

impl Spanned for NodeDoctype {
fn span(&self) -> Span {
self.span
impl ToTokens for NodeDoctype {
fn to_tokens(&self, tokens: &mut TokenStream) {
let value = &self.value;
tokens.extend(quote_spanned!(self.span=> <! #value >))
}
}

Expand All @@ -277,9 +302,10 @@ impl fmt::Display for NodeFragment {
}
}

impl Spanned for NodeFragment {
fn span(&self) -> Span {
self.span
impl ToTokens for NodeFragment {
fn to_tokens(&self, tokens: &mut TokenStream) {
let childrens = &self.children;
tokens.extend(quote_spanned!(self.span => <> #(#childrens)* </>))
}
}

Expand All @@ -298,9 +324,9 @@ impl fmt::Display for NodeBlock {
}
}

impl Spanned for NodeBlock {
fn span(&self) -> Span {
self.value.span()
impl ToTokens for NodeBlock {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.value.to_tokens(tokens)
}
}

Expand Down Expand Up @@ -412,6 +438,13 @@ impl NodeValueExpr {
}
}

impl ToTokens for NodeValueExpr {
fn to_tokens(&self, tokens: &mut TokenStream) {
let obj = self.as_ref();
obj.to_tokens(tokens)
}
}

impl AsRef<Expr> for NodeValueExpr {
fn as_ref(&self) -> &Expr {
&self.expr
Expand Down
37 changes: 20 additions & 17 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

use std::vec;

use proc_macro2::{Punct, Span, TokenStream, TokenTree};
use proc_macro2::{Punct, Span, TokenStream, TokenTree, Group};
use syn::{
braced,
ext::IdentExt,
parse::{discouraged::Speculative, Parse, ParseStream, Parser as _, Peek},
punctuated::Punctuated,
spanned::Spanned,
token::{Brace, Colon, Colon2},
token::{Brace, Colon, PathSep},
Block, Error, Expr, ExprBlock, ExprLit, ExprPath, Ident, Path, PathSegment, Result, Token,
};

Expand Down Expand Up @@ -126,9 +126,8 @@ impl Parser {
match transform_fn(&forked_block_content) {
Ok(transformed_tokens) => match transformed_tokens {
Some(tokens) => {
let parser = move |input: ParseStream| {
Ok(self.block_content_to_block(input, block_content.span()))
};
let parser =
move |input: ParseStream| Ok(self.block_content_to_block(input, block_content.span()));
let transformed_content = parser.parse2(tokens)?;
block_content.advance_to(&forked_block_content);
transformed_content
Expand All @@ -153,11 +152,13 @@ impl Parser {

/// Parse the given stream and span as [`Expr::Block`].
fn block_content_to_block(&self, input: ParseStream, span: Span) -> Result<Expr> {
let mut delim_span = Group::new(proc_macro2::Delimiter::None, TokenStream::new());
delim_span.set_span(span);
Ok(ExprBlock {
attrs: vec![],
label: None,
block: Block {
brace_token: Brace { span },
brace_token: Brace { span: delim_span.delim_span() },
stmts: Block::parse_within(input)?,
},
}
Expand Down Expand Up @@ -443,18 +444,20 @@ impl Parser {

/// Parse the stream as [`NodeName`].
fn node_name(&self, input: ParseStream) -> Result<NodeName> {
if input.peek2(Colon2) {
self.node_name_punctuated_ident::<Colon2, fn(_) -> Colon2, PathSegment>(input, Colon2)
.map(|segments| {
NodeName::Path(ExprPath {
attrs: vec![],
qself: None,
path: Path {
leading_colon: None,
segments,
},
})
if input.peek2(PathSep) {
self.node_name_punctuated_ident::<PathSep, fn(_) -> PathSep, PathSegment>(
input, PathSep,
)
.map(|segments| {
NodeName::Path(ExprPath {
attrs: vec![],
qself: None,
path: Path {
leading_colon: None,
segments,
},
})
})
} else if input.peek2(Colon) || input.peek2(Dash) {
self.node_name_punctuated_ident_with_alternate::<Punct, fn(_) -> Colon, fn(_) -> Dash, Ident>(
input, Colon, Dash,
Expand Down
6 changes: 3 additions & 3 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::convert::TryFrom;

use eyre::Result;
use quote::quote;
use syn::ExprBlock;
use syn::{ExprBlock, Stmt};
use syn_rsx::{
parse2, parse2_with_config, Node, NodeAttribute, NodeElement, NodeType, ParserConfig,
};
Expand Down Expand Up @@ -245,7 +245,7 @@ fn test_type_of_top_level_nodes() -> Result<()> {

#[test]
fn test_transform_block_some() -> Result<()> {
use syn::{Expr, Lit, Stmt, Token};
use syn::{Expr, Lit, Token};

let tokens = quote! {
<div>{%}</div>
Expand All @@ -263,7 +263,7 @@ fn test_transform_block_some() -> Result<()> {
match block.value.as_ref() {
Expr::Block(expr) => {
match &expr.block.stmts[0] {
Stmt::Expr(Expr::Lit(expr)) => match &expr.lit {
Stmt::Expr(Expr::Lit(expr), _) => match &expr.lit {
Lit::Str(lit_str) => Some(lit_str.value()),
_ => None,
},
Expand Down