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

Update to syn 2 #49

Merged
merged 1 commit into from
Oct 9, 2023
Merged
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
2 changes: 1 addition & 1 deletion genco-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ keywords = ["code-generation", "template"]
categories = ["template-engine"]

[dependencies]
syn = { version = "1.0.31", features = ["full"] }
syn = { version = "2.0.38", features = ["full"] }
q = { package = "quote", version = "1.0.3" }
proc-macro2 = { version = "1.0.10", features = ["span-locations"] }

Expand Down
15 changes: 8 additions & 7 deletions genco-macros/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;

use proc_macro2::{Span, TokenStream, TokenTree};
use syn::{spanned::Spanned, Token};
use syn::{Ident, LitChar, Token};

use crate::static_buffer::StaticBuffer;

Expand Down Expand Up @@ -64,9 +64,9 @@ pub(crate) enum Name {
/// The name is the `const` token.
Const(Token![const]),
/// Custom name.
Ident(Span, String),
Ident(Ident, String),
/// Character name.
Char(Span, char),
Char(LitChar, char),
}

impl Name {
Expand All @@ -80,11 +80,12 @@ impl Name {
}
}

impl Spanned for Name {
fn span(&self) -> Span {
impl q::ToTokens for Name {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Name::Const(t) => t.span,
Name::Ident(span, _) | Name::Char(span, _) => *span,
Name::Const(t) => t.to_tokens(tokens),
Name::Ident(_, name) => name.to_tokens(tokens),
Name::Char(_, c) => c.to_tokens(tokens),
}
}
}
Expand Down
58 changes: 17 additions & 41 deletions genco-macros/src/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl<'a> Quote<'a> {
let mut req = Requirements::default();

input.parse::<Token![for]>()?;
let pattern = input.parse::<syn::Pat>()?;
let pattern = syn::Pat::parse_single(input)?;
input.parse::<Token![in]>()?;
let expr = syn::Expr::parse_without_eager_brace(input)?;

Expand All @@ -151,7 +151,9 @@ impl<'a> Quote<'a> {
let content;
let paren = syn::parenthesized!(content in input);

let (r, join) = Quote::new(self.cx).with_span(paren.span)?.parse(&content)?;
let (r, join) = Quote::new(self.cx)
.with_span(paren.span.span())?
.parse(&content)?;
req.merge_with(r);

Some(join)
Expand Down Expand Up @@ -195,7 +197,7 @@ impl<'a> Quote<'a> {

while !body.is_empty() {
let attr = input.call(syn::Attribute::parse_outer)?;
let pattern = multi_pat_with_leading_vert(&body)?;
let pattern = syn::Pat::parse_multi_with_leading_vert(&body)?;

let condition = if body.peek(Token![if]) {
body.parse::<Token![if]>()?;
Expand All @@ -217,7 +219,9 @@ impl<'a> Quote<'a> {
let block;
let paren = syn::parenthesized!(block in body);

Quote::new(self.cx).with_span(paren.span)?.parse(&block)?
Quote::new(self.cx)
.with_span(paren.span.span())?
.parse(&block)?
} else {
let parser = Quote::new_until_comma(self.cx);
parser.parse(&body)?
Expand Down Expand Up @@ -282,7 +286,7 @@ impl<'a> Quote<'a> {
let scope;
let outer = syn::parenthesized!(scope in input);

let cursor = self.buf.join(start, outer.span)?;
let cursor = self.buf.join(start, outer.span.span())?;

let ast = if scope.peek(Token![if]) {
let (req, ast) = self.parse_condition(&scope)?;
Expand Down Expand Up @@ -409,7 +413,7 @@ impl<'a> Quote<'a> {
self.parse_group(
encoder,
Delimiter::Brace,
braces.span,
braces.span.span(),
&content,
group_depth,
)?;
Expand All @@ -422,7 +426,7 @@ impl<'a> Quote<'a> {
self.parse_group(
encoder,
Delimiter::Parenthesis,
braces.span,
braces.span.span(),
&content,
group_depth,
)?;
Expand All @@ -435,7 +439,7 @@ impl<'a> Quote<'a> {
self.parse_group(
encoder,
Delimiter::Bracket,
braces.span,
braces.span.span(),
&content,
group_depth,
)?;
Expand Down Expand Up @@ -495,10 +499,12 @@ pub(crate) fn parse_internal_function<'a>(
Name::Const(function.parse()?)
} else if function.peek(syn::LitChar) {
let c = function.parse::<syn::LitChar>()?;
Name::Char(c.span(), c.value())
let value = c.value();
Name::Char(c, value)
} else {
let ident = function.parse::<syn::Ident>()?;
Name::Ident(ident.span(), ident.to_string())
let str = ident.to_string();
Name::Ident(ident, str)
};

if !function.is_empty() {
Expand All @@ -513,35 +519,5 @@ pub(crate) fn parse_internal_function<'a>(
(None, brackets.span)
};

Ok(Some((name, content, [start.span(), end])))
}

// NB: copied from: https://github.com/dtolnay/syn/blob/80c6889684da7e0a30ef5d0b03e9d73fa6160541/src/pat.rs#L792
//
// TODO: remove once made public in syn.
fn multi_pat_with_leading_vert(input: ParseStream) -> Result<syn::Pat> {
let leading_vert: Option<Token![|]> = input.parse()?;
multi_pat_impl(input, leading_vert)
}

fn multi_pat_impl(input: ParseStream, leading_vert: Option<Token![|]>) -> Result<syn::Pat> {
let mut pat: syn::Pat = input.parse()?;
if leading_vert.is_some()
|| input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=])
{
let mut cases = syn::punctuated::Punctuated::new();
cases.push_value(pat);
while input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) {
let punct = input.parse()?;
cases.push_punct(punct);
let pat: syn::Pat = input.parse()?;
cases.push_value(pat);
}
pat = syn::Pat::Or(syn::PatOr {
attrs: Vec::new(),
leading_vert,
cases,
});
}
Ok(pat)
Ok(Some((name, content, [start.span(), end.span()])))
}