Skip to content

Commit

Permalink
Update syn to 2.0 (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejhirsz committed Apr 10, 2023
1 parent 91eb831 commit 3c19bb1
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 29 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@ members = ["logos", "logos-cli", "logos-codegen", "logos-derive", "tests"]
[profile]
release = { lto = true }
bench = { lto = true }

[patch.crates-io]
logos-derive = { path = "./logos-derive" }
2 changes: 1 addition & 1 deletion logos-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ edition = "2018"
anyhow = "1.0.57"
clap = { version = "3.1.18", features = ["derive"] }
fs-err = "2.7.0"
logos-codegen = { path = "../logos-codegen", version = "0.12.0" }
logos-codegen = { version = "0.12.0", path = "../logos-codegen" }
proc-macro2 = "1.0.39"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion logos-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ edition = "2018"
[dependencies]
beef = "0.5.0"
fnv = "1.0.6"
syn = { version = "1.0.17", features = ["full"] }
syn = { version = "2.0.13", features = ["full"] }
quote = "1.0.3"
proc-macro2 = "1.0.9"
regex-syntax = "0.6"
Expand Down
33 changes: 20 additions & 13 deletions logos-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use parser::{IgnoreFlags, Mode, Parser};
use quote::ToTokens;
use util::MaybeVoid;

use proc_macro2::TokenStream;
use proc_macro2::{TokenStream, TokenTree};
use quote::quote;
use syn::parse_quote;
use syn::spanned::Spanned;
Expand Down Expand Up @@ -107,7 +107,7 @@ pub fn generate(input: TokenStream) -> TokenStream {
let leaf = move |span| Leaf::new(var_ident, span).field(field.clone());

for attr in &mut variant.attrs {
let attr_name = match attr.path.get_ident() {
let attr_name = match attr.path().get_ident() {
Some(ident) => ident.to_string(),
None => continue,
};
Expand Down Expand Up @@ -314,14 +314,21 @@ pub fn strip_attributes(input: TokenStream) -> TokenStream {
strip_attrs_from_vec(&mut item.attrs);

for attr in &mut item.attrs {
if attr.path.is_ident("derive") {
if let Ok(syn::Meta::List(mut meta)) = attr.parse_meta() {
meta.nested = meta.nested.into_iter().filter(|nested| !matches!(nested, syn::NestedMeta::Meta(nested) if nested.path().is_ident("Logos"))).collect();

attr.tokens = TokenStream::new();
meta.paren_token.surround(&mut attr.tokens, |tokens| {
meta.nested.to_tokens(tokens);
});
if let syn::Meta::List(meta) = &mut attr.meta {
if meta.path.is_ident("derive") {
let mut tokens =
std::mem::replace(&mut meta.tokens, TokenStream::new()).into_iter();

while let Some(TokenTree::Ident(ident)) = tokens.next() {
let punct = tokens.next();

if ident == "Logos" {
continue;
}

meta.tokens.extend([TokenTree::Ident(ident)]);
meta.tokens.extend(punct);
}
}
}
}
Expand All @@ -341,7 +348,7 @@ fn strip_attrs_from_vec(attrs: &mut Vec<syn::Attribute>) {
}

fn is_logos_attr(attr: &syn::Attribute) -> bool {
attr.path.is_ident(LOGOS_ATTR)
|| attr.path.is_ident(TOKEN_ATTR)
|| attr.path.is_ident(REGEX_ATTR)
attr.path().is_ident(LOGOS_ATTR)
|| attr.path().is_ident(TOKEN_ATTR)
|| attr.path().is_ident(REGEX_ATTR)
}
12 changes: 7 additions & 5 deletions logos-codegen/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use beef::lean::Cow;
use proc_macro2::{Span, TokenStream, TokenTree};
use quote::quote;
use syn::spanned::Spanned;
use syn::{Attribute, GenericParam, Lit, Type};
use syn::{Attribute, GenericParam, Lit, Meta, Type};

use crate::error::Errors;
use crate::leaf::{Callback, InlineCallback};
Expand Down Expand Up @@ -65,18 +65,20 @@ impl Parser {
}

fn parse_attr(&mut self, attr: &mut Attribute) -> Option<AttributeParser> {
let mut tokens = std::mem::replace(&mut attr.tokens, TokenStream::new()).into_iter();
match &mut attr.meta {
Meta::List(list) => {
let tokens = std::mem::replace(&mut list.tokens, TokenStream::new());

match tokens.next() {
Some(TokenTree::Group(group)) => Some(AttributeParser::new(group.stream())),
Some(AttributeParser::new(tokens))
}
_ => None,
}
}

/// Try to parse the main `#[logos(...)]`, does nothing if
/// the attribute's name isn't `logos`.
pub fn try_parse_logos(&mut self, attr: &mut Attribute) {
if !attr.path.is_ident(LOGOS_ATTR) {
if !attr.path().is_ident(LOGOS_ATTR) {
return;
}

Expand Down
8 changes: 4 additions & 4 deletions logos-codegen/src/parser/type_params.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use syn::spanned::Spanned;
use syn::{Lifetime, LifetimeDef, Path, Type};
use syn::{Lifetime, LifetimeParam, Path, Type};

use crate::error::Errors;

Expand All @@ -12,7 +12,7 @@ pub struct TypeParams {
}

impl TypeParams {
pub fn explicit_lifetime(&mut self, lt: LifetimeDef, errors: &mut Errors) {
pub fn explicit_lifetime(&mut self, lt: LifetimeParam, errors: &mut Errors) {
if self.lifetime {
let span = lt.span();

Expand Down Expand Up @@ -180,8 +180,8 @@ fn traverse_path(path: &mut Path, f: &mut impl FnMut(&mut Type)) {
syn::GenericArgument::Type(ty) => {
traverse_type(ty, f);
}
syn::GenericArgument::Binding(bind) => {
traverse_type(&mut bind.ty, f);
syn::GenericArgument::AssocType(assoc) => {
traverse_type(&mut assoc.ty, f);
}
_ => (),
}
Expand Down
2 changes: 1 addition & 1 deletion logos-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ name = "logos_derive"
proc-macro = true

[dependencies]
logos-codegen = { path = "../logos-codegen", version = "0.12.0" }
logos-codegen = { version = "0.12.0", path = "../logos-codegen" }
2 changes: 1 addition & 1 deletion logos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ readme = "../README.md"
edition = "2018"

[dependencies]
logos-derive = { path = "../logos-derive", version = "0.12.1", optional = true }
logos-derive = { version = "0.12.1", path = "../logos-derive", optional = true }

[features]
default = ["export_derive", "std"]
Expand Down

0 comments on commit 3c19bb1

Please sign in to comment.