Reclasify Ident and Span as being from proc_macro2
diff --git a/codegen/src/gen.rs b/codegen/src/gen.rs
index 4d18776..f852308 100644
--- a/codegen/src/gen.rs
+++ b/codegen/src/gen.rs
@@ -374,6 +374,9 @@
},
)
}
+ types::Type::Ext(t) if super::TERMINAL_TYPES.contains(&&t[..]) => {
+ Some(simple_visit(t, kind, name))
+ }
types::Type::Ext(_) | types::Type::Std(_) => None,
}
}
@@ -633,10 +636,23 @@
.unwrap();
}
+const TERMINAL_TYPES: &[&str] = &["Span", "Ident"];
+
pub fn generate(defs: &types::Definitions) {
+ let mut defs = defs.clone();
+
+ for &tt in TERMINAL_TYPES {
+ defs.insert(types::Node::Struct(types::Struct::new(
+ tt.to_string(),
+ types::Features::default(),
+ vec![],
+ true)
+ ));
+ }
+
let mut state = codegen::State::default();
for s in &defs.types {
- codegen::generate(&mut state, s, defs);
+ codegen::generate(&mut state, s, &defs);
}
let full_macro = quote! {
diff --git a/codegen/src/parse.rs b/codegen/src/parse.rs
index 1a2484b..5af7df2 100644
--- a/codegen/src/parse.rs
+++ b/codegen/src/parse.rs
@@ -1,6 +1,5 @@
use crate::types;
-use proc_macro2::Span;
use syn::{Data, DataStruct, DeriveInput, Ident, Item};
use std::collections::BTreeMap;
@@ -12,7 +11,6 @@
const TOKEN_SRC: &str = "../src/token.rs";
const IGNORED_MODS: &[&str] = &["fold", "visit", "visit_mut"];
const EXTRA_TYPES: &[&str] = &["Lifetime"];
-const TERMINAL_TYPES: &[&str] = &["Span", "Ident"];
// NOTE: BTreeMap is used here instead of HashMap to have deterministic output.
type ItemLookup = BTreeMap<Ident, AstItem>;
@@ -25,30 +23,6 @@
let token_lookup = load_token_file(TOKEN_SRC).unwrap();
- // Load in any terminal types
- for &tt in TERMINAL_TYPES {
- use syn::*;
- item_lookup.insert(
- Ident::new(&tt, Span::call_site()),
- AstItem {
- ast: DeriveInput {
- ident: Ident::new(tt, Span::call_site()),
- vis: Visibility::Public(VisPublic {
- pub_token: <Token![pub]>::default(),
- }),
- attrs: vec![],
- generics: Generics::default(),
- data: Data::Struct(DataStruct {
- fields: Fields::Unit,
- struct_token: <Token![struct]>::default(),
- semi_token: None,
- }),
- },
- features: vec![],
- },
- );
- }
-
let types = item_lookup
.values()
.map(|item| introspect_item(item, &item_lookup, &token_lookup))
@@ -179,7 +153,7 @@
"Brace" | "Bracket" | "Paren" | "Group" => {
types::Type::Group(last.ident.to_string())
}
- "TokenStream" | "Literal" => types::Type::Ext(last.ident.to_string()),
+ "TokenStream" | "Literal" | "Ident" | "Span" => types::Type::Ext(last.ident.to_string()),
"String" | "u32" | "usize" | "bool" => types::Type::Std(last.ident.to_string()),
_ => {
if items.get(&last.ident).is_some() {
diff --git a/codegen/src/types.rs b/codegen/src/types.rs
index b818fa8..32328dc 100644
--- a/codegen/src/types.rs
+++ b/codegen/src/types.rs
@@ -1,19 +1,20 @@
use std::collections::BTreeMap;
use std::ops;
+#[derive(Debug, Clone)]
pub struct Definitions {
pub types: Vec<Node>,
pub tokens: BTreeMap<String, String>,
}
-#[derive(Debug, Serialize)]
+#[derive(Debug, Clone, Serialize)]
#[serde(tag = "node", rename_all = "lowercase")]
pub enum Node {
Struct(Struct),
Enum(Enum),
}
-#[derive(Debug, Serialize)]
+#[derive(Debug, Clone, Serialize)]
pub struct Struct {
ident: String,
features: Features,
@@ -21,27 +22,27 @@
all_fields_pub: bool,
}
-#[derive(Debug, Serialize)]
+#[derive(Debug, Clone, Serialize)]
pub struct Enum {
ident: String,
features: Features,
variants: Vec<Variant>,
}
-#[derive(Debug, Serialize)]
+#[derive(Debug, Clone, Serialize)]
pub struct Variant {
ident: String,
fields: Vec<Type>,
}
-#[derive(Debug, Serialize)]
+#[derive(Debug, Clone, Serialize)]
pub struct Field {
ident: String,
#[serde(rename = "type")]
ty: Type,
}
-#[derive(Debug, Serialize)]
+#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Type {
/// Type defined by `syn`
@@ -69,7 +70,7 @@
Tuple(Vec<Type>),
}
-#[derive(Debug, Serialize)]
+#[derive(Debug, Clone, Serialize)]
pub struct Punctuated {
element: Box<Type>,
punct: String,
@@ -80,6 +81,12 @@
any: Vec<String>,
}
+impl Definitions {
+ pub fn insert(&mut self, node: Node) {
+ self.types.push(node);
+ }
+}
+
impl Node {
pub fn ident(&self) -> &str {
match self {