Skip to content

Commit f954499

Browse files
authored
Merge pull request #1423 from dtolnay/codegen
Update code generator to use syn 2
2 parents c4976f1 + efe6fcb commit f954499

File tree

2 files changed

+29
-43
lines changed

2 files changed

+29
-43
lines changed

codegen/Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ anyhow = "1"
1111
color-backtrace = "0.4"
1212
indexmap = { version = "1", features = ["serde-1"] }
1313
inflections = "1.1"
14-
prettyplease = "0.1"
14+
prettyplease = "0.2.3"
1515
proc-macro2 = { version = "1.0.20", features = ["span-locations"] }
1616
quote = "1"
1717
semver = { version = "1", features = ["serde"] }
1818
serde = { version = "1.0.88", features = ["derive"] }
1919
serde_json = "1.0.38"
2020
syn-codegen = { path = "../json" }
21-
syn = { version = "1", features = ["derive", "parsing", "printing", "full"], default-features = false }
21+
syn = { version = "2", features = ["derive", "parsing", "printing", "full"], default-features = false }
2222
thiserror = "1"
2323
toml = "0.5"
2424

2525
[workspace]
26+
[patch.crates-io]
27+
syn = { path = ".." }

codegen/src/parse.rs

+25-41
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@ fn introspect_features(attrs: &[Attribute]) -> types::Features {
197197
let mut ret = types::Features::default();
198198

199199
for attr in attrs {
200-
if !attr.path.is_ident("cfg") {
200+
if !attr.path().is_ident("cfg") {
201201
continue;
202202
}
203203

204-
let features = parsing::parse_features.parse2(attr.tokens.clone()).unwrap();
204+
let features = attr.parse_args_with(parsing::parse_features).unwrap();
205205

206206
if ret.any.is_empty() {
207207
ret = features;
@@ -225,7 +225,7 @@ fn is_pub(vis: &Visibility) -> bool {
225225

226226
fn is_non_exhaustive(attrs: &[Attribute]) -> bool {
227227
for attr in attrs {
228-
if attr.path.is_ident("non_exhaustive") {
228+
if attr.path().is_ident("non_exhaustive") {
229229
return true;
230230
}
231231
}
@@ -234,11 +234,7 @@ fn is_non_exhaustive(attrs: &[Attribute]) -> bool {
234234

235235
fn is_doc_hidden(attrs: &[Attribute]) -> bool {
236236
for attr in attrs {
237-
if attr.path.is_ident("doc")
238-
&& parsing::parse_doc_hidden_attr
239-
.parse2(attr.tokens.clone())
240-
.is_ok()
241-
{
237+
if attr.path().is_ident("doc") && attr.parse_args::<parsing::kw::hidden>().is_ok() {
242238
return true;
243239
}
244240
}
@@ -282,9 +278,10 @@ mod parsing {
282278
use proc_macro2::TokenStream;
283279
use quote::quote;
284280
use std::collections::{BTreeMap, BTreeSet};
285-
use syn::parse::{ParseStream, Parser, Result};
281+
use syn::parse::{ParseStream, Result};
286282
use syn::{
287-
braced, bracketed, parenthesized, parse_quote, token, Attribute, Ident, LitStr, Path, Token,
283+
braced, bracketed, parenthesized, parse_quote, token, Attribute, Expr, Ident, Lit, LitStr,
284+
Path, Token,
288285
};
289286
use syn_codegen as types;
290287

@@ -421,7 +418,7 @@ mod parsing {
421418
})
422419
}
423420

424-
mod kw {
421+
pub mod kw {
425422
syn::custom_keyword!(hidden);
426423
syn::custom_keyword!(macro_rules);
427424
syn::custom_keyword!(Token);
@@ -458,56 +455,43 @@ mod parsing {
458455
pub fn parse_features(input: ParseStream) -> Result<types::Features> {
459456
let mut features = BTreeSet::new();
460457

461-
let level_1;
462-
parenthesized!(level_1 in input);
463-
464-
let i: Ident = level_1.fork().parse()?;
458+
let i: Ident = input.fork().parse()?;
465459

466460
if i == "any" {
467-
level_1.parse::<Ident>()?;
461+
input.parse::<Ident>()?;
468462

469-
let level_2;
470-
parenthesized!(level_2 in level_1);
463+
let nested;
464+
parenthesized!(nested in input);
471465

472-
while !level_2.is_empty() {
473-
features.insert(parse_feature(&level_2)?);
466+
while !nested.is_empty() {
467+
features.insert(parse_feature(&nested)?);
474468

475-
if !level_2.is_empty() {
476-
level_2.parse::<Token![,]>()?;
469+
if !nested.is_empty() {
470+
nested.parse::<Token![,]>()?;
477471
}
478472
}
479473
} else if i == "feature" {
480-
features.insert(parse_feature(&level_1)?);
481-
assert!(level_1.is_empty());
474+
features.insert(parse_feature(input)?);
475+
assert!(input.is_empty());
482476
} else {
483477
panic!("{:?}", i);
484478
}
485479

486-
assert!(input.is_empty());
487-
488480
Ok(types::Features { any: features })
489481
}
490482

491-
pub fn path_attr(attrs: &[Attribute]) -> Result<Option<LitStr>> {
483+
pub fn path_attr(attrs: &[Attribute]) -> Result<Option<&LitStr>> {
492484
for attr in attrs {
493-
if attr.path.is_ident("path") {
494-
fn parser(input: ParseStream) -> Result<LitStr> {
495-
input.parse::<Token![=]>()?;
496-
input.parse()
485+
if attr.path().is_ident("path") {
486+
if let Expr::Lit(expr) = &attr.meta.require_name_value()?.value {
487+
if let Lit::Str(lit) = &expr.lit {
488+
return Ok(Some(lit));
489+
}
497490
}
498-
let filename = parser.parse2(attr.tokens.clone())?;
499-
return Ok(Some(filename));
500491
}
501492
}
502493
Ok(None)
503494
}
504-
505-
pub fn parse_doc_hidden_attr(input: ParseStream) -> Result<()> {
506-
let content;
507-
parenthesized!(content in input);
508-
content.parse::<kw::hidden>()?;
509-
Ok(())
510-
}
511495
}
512496

513497
fn clone_features(features: &[Attribute]) -> Vec<Attribute> {
@@ -518,7 +502,7 @@ fn get_features(attrs: &[Attribute], base: &[Attribute]) -> Vec<Attribute> {
518502
let mut ret = clone_features(base);
519503

520504
for attr in attrs {
521-
if attr.path.is_ident("cfg") {
505+
if attr.path().is_ident("cfg") {
522506
ret.push(parse_quote!(#attr));
523507
}
524508
}

0 commit comments

Comments
 (0)