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 1 commit
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 = "../../" }
57 changes: 0 additions & 57 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use proc_macro2::{Punct, Span, TokenStream};
use quote::ToTokens;
use syn::{
punctuated::{Pair, Punctuated},
spanned::Spanned,
Expr, ExprBlock, ExprLit, ExprPath, Ident, Lit,
};

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

impl Spanned for Node {
dgsantana marked this conversation as resolved.
Show resolved Hide resolved
fn span(&self) -> Span {
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(),
}
}
}

impl fmt::Display for Node {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
Expand Down Expand Up @@ -144,12 +129,6 @@ impl fmt::Display for NodeElement {
}
}

impl Spanned for NodeElement {
fn span(&self) -> Span {
self.span
}
}

/// Attribute node.
///
/// Attributes of opening tags. Every attribute is itself a node.
Expand All @@ -172,12 +151,6 @@ impl fmt::Display for NodeAttribute {
}
}

impl Spanned for NodeAttribute {
fn span(&self) -> Span {
self.span
}
}

/// Text node.
///
/// Quoted text. It's [planned to support unquoted text] as well
Expand All @@ -197,12 +170,6 @@ impl fmt::Display for NodeText {
}
}

impl Spanned for NodeText {
fn span(&self) -> Span {
self.value.span()
}
}

/// Comment node.
///
/// Comment: `<!-- "comment" -->`, currently has the same restrictions as
Expand All @@ -224,12 +191,6 @@ impl fmt::Display for NodeComment {
}
}

impl Spanned for NodeComment {
fn span(&self) -> Span {
self.span
}
}

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

impl Spanned for NodeDoctype {
fn span(&self) -> Span {
self.span
}
}

/// Fragement node.
///
/// Fragment: `<></>`
Expand All @@ -277,12 +232,6 @@ impl fmt::Display for NodeFragment {
}
}

impl Spanned for NodeFragment {
fn span(&self) -> Span {
self.span
}
}

/// Block node.
///
/// Arbitrary rust code in braced `{}` blocks.
Expand All @@ -298,12 +247,6 @@ impl fmt::Display for NodeBlock {
}
}

impl Spanned for NodeBlock {
fn span(&self) -> Span {
self.value.span()
}
}

/// Name of the node.
#[derive(Debug)]
pub enum NodeName {
Expand Down
56 changes: 30 additions & 26 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use syn::{
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,14 +126,13 @@ 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));
let transformed_content = parser.parse2(tokens)?;
block_content.advance_to(&forked_block_content);
transformed_content
}
None => self.block_content_to_block(block_content, block_content.span()),
None => self.block_content_to_block(block_content),
},
Err(error) => Err(error),
}
Expand All @@ -152,16 +151,19 @@ impl Parser {
}

/// Parse the given stream and span as [`Expr::Block`].
fn block_content_to_block(&self, input: ParseStream, span: Span) -> Result<Expr> {
Ok(ExprBlock {
attrs: vec![],
label: None,
block: Block {
brace_token: Brace { span },
stmts: Block::parse_within(input)?,
},
}
.into())
fn block_content_to_block(&self, input: ParseStream) -> Result<Expr> {
// let _content;
dgsantana marked this conversation as resolved.
Show resolved Hide resolved
// let brace = braced!(_content in input);
// Ok(ExprBlock {
// attrs: vec![],
// label: None,
// block: Block {
// brace_token: brace,
// stmts: Block::parse_within(input)?,
// },
// }
// .into())
input.parse()
}

/// Parse the given stream as [`Expr::Block`].
Expand Down Expand Up @@ -443,18 +445,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
17 changes: 7 additions & 10 deletions tests/test.rs
Original file line number Diff line number Diff line change
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 @@ -259,17 +259,14 @@ fn test_transform_block_some() -> Result<()> {
let nodes = parse2_with_config(tokens, config)?;
let Node::Block(block) = get_element_child(&nodes, 0, 0) else { panic!("expected block") };

dbg!(block.value.as_ref());

assert_eq!(
match block.value.as_ref() {
Expr::Block(expr) => {
match &expr.block.stmts[0] {
Stmt::Expr(Expr::Lit(expr)) => match &expr.lit {
Lit::Str(lit_str) => Some(lit_str.value()),
_ => None,
},
_ => None,
}
}
Expr::Lit(exp) => match &exp.lit {
dgsantana marked this conversation as resolved.
Show resolved Hide resolved
Lit::Str(lit_str) => Some(lit_str.value()),
_ => None,
},
_ => None,
},
Some("percent".to_owned())
Expand Down