blob: 448188df641e9ccaca4d7d1ff113d0cd52d947fc [file] [log] [blame]
Carl Lerche058ff472019-02-13 16:23:52 -08001use crate::types;
2
David Tolnay14d463e2019-02-15 14:23:51 -08003use indexmap::IndexMap;
Carl Lerche058ff472019-02-13 16:23:52 -08004use syn::{Data, DataStruct, DeriveInput, Ident, Item};
5
6use std::collections::BTreeMap;
7use std::fs::File;
8use std::io::Read;
9use std::path::Path;
10
11const SYN_CRATE_ROOT: &str = "../src/lib.rs";
12const TOKEN_SRC: &str = "../src/token.rs";
13const IGNORED_MODS: &[&str] = &["fold", "visit", "visit_mut"];
14const EXTRA_TYPES: &[&str] = &["Lifetime"];
Carl Lerche058ff472019-02-13 16:23:52 -080015
16// NOTE: BTreeMap is used here instead of HashMap to have deterministic output.
17type ItemLookup = BTreeMap<Ident, AstItem>;
18type TokenLookup = BTreeMap<String, String>;
19
20/// Parse the contents of `src` and return a list of AST types.
David Tolnayf9bb8ff2019-02-15 13:10:14 -080021pub fn parse() -> types::Definitions {
Carl Lerche058ff472019-02-13 16:23:52 -080022 let mut item_lookup = BTreeMap::new();
23 load_file(SYN_CRATE_ROOT, &[], &mut item_lookup).unwrap();
24
25 let token_lookup = load_token_file(TOKEN_SRC).unwrap();
26
David Tolnayf9bb8ff2019-02-15 13:10:14 -080027 let types = item_lookup
Carl Lerche058ff472019-02-13 16:23:52 -080028 .values()
29 .map(|item| introspect_item(item, &item_lookup, &token_lookup))
David Tolnayf9bb8ff2019-02-15 13:10:14 -080030 .collect();
31
David Tolnay47fe7402019-02-15 14:35:25 -080032 let tokens = token_lookup
33 .into_iter()
34 .map(|(name, ty)| (ty, name))
35 .collect();
David Tolnayf9bb8ff2019-02-15 13:10:14 -080036
37 types::Definitions { types, tokens }
Carl Lerche058ff472019-02-13 16:23:52 -080038}
39
40/// Data extracted from syn source
41#[derive(Clone)]
42pub struct AstItem {
43 ast: DeriveInput,
44 features: Vec<syn::Attribute>,
45}
46
David Tolnayf9bb8ff2019-02-15 13:10:14 -080047fn introspect_item(item: &AstItem, items: &ItemLookup, tokens: &TokenLookup) -> types::Node {
Carl Lerche058ff472019-02-13 16:23:52 -080048 let features = introspect_features(&item.features);
49
50 match &item.ast.data {
David Tolnayc2be7b22019-02-15 18:48:31 -080051 Data::Enum(ref data) => types::Node {
52 ident: item.ast.ident.to_string(),
Carl Lerche058ff472019-02-13 16:23:52 -080053 features,
David Tolnayc2be7b22019-02-15 18:48:31 -080054 data: types::Data::Enum(introspect_enum(data, items, tokens)),
55 },
56 Data::Struct(ref data) => types::Node {
57 ident: item.ast.ident.to_string(),
Carl Lerche058ff472019-02-13 16:23:52 -080058 features,
David Tolnayc2be7b22019-02-15 18:48:31 -080059 data: {
60 if data.fields.iter().all(|f| is_pub(&f.vis)) {
61 types::Data::Struct(introspect_struct(data, items, tokens))
62 } else {
63 types::Data::Private
64 }
65 },
66 },
Carl Lerche058ff472019-02-13 16:23:52 -080067 Data::Union(..) => panic!("Union not supported"),
68 }
69}
70
71fn introspect_enum(
Carl Lerche058ff472019-02-13 16:23:52 -080072 item: &syn::DataEnum,
73 items: &ItemLookup,
74 tokens: &TokenLookup,
David Tolnayc2be7b22019-02-15 18:48:31 -080075) -> Vec<types::Variant> {
76 item.variants
Carl Lerche058ff472019-02-13 16:23:52 -080077 .iter()
78 .map(|variant| {
79 let fields = match &variant.fields {
80 syn::Fields::Unnamed(fields) => fields
81 .unnamed
82 .iter()
83 .map(|field| introspect_type(&field.ty, items, tokens))
84 .collect(),
85 syn::Fields::Unit => vec![],
86 _ => panic!("Enum representation not supported"),
87 };
88
David Tolnayfa67ab02019-02-15 20:17:30 -080089 types::Variant {
90 ident: variant.ident.to_string(),
91 fields,
92 }
Carl Lerche058ff472019-02-13 16:23:52 -080093 })
David Tolnayc2be7b22019-02-15 18:48:31 -080094 .collect()
Carl Lerche058ff472019-02-13 16:23:52 -080095}
96
97fn introspect_struct(
Carl Lerche058ff472019-02-13 16:23:52 -080098 item: &syn::DataStruct,
99 items: &ItemLookup,
100 tokens: &TokenLookup,
David Tolnayc2be7b22019-02-15 18:48:31 -0800101) -> IndexMap<String, types::Type> {
102 match &item.fields {
Carl Lerche058ff472019-02-13 16:23:52 -0800103 syn::Fields::Named(fields) => fields
104 .named
105 .iter()
106 .map(|field| {
David Tolnay14d463e2019-02-15 14:23:51 -0800107 (
Carl Lerche058ff472019-02-13 16:23:52 -0800108 field.ident.as_ref().unwrap().to_string(),
109 introspect_type(&field.ty, items, tokens),
110 )
111 })
112 .collect(),
David Tolnay14d463e2019-02-15 14:23:51 -0800113 syn::Fields::Unit => IndexMap::new(),
Carl Lerche058ff472019-02-13 16:23:52 -0800114 _ => panic!("Struct representation not supported"),
David Tolnayc2be7b22019-02-15 18:48:31 -0800115 }
Carl Lerche058ff472019-02-13 16:23:52 -0800116}
117
118fn introspect_type(item: &syn::Type, items: &ItemLookup, tokens: &TokenLookup) -> types::Type {
119 match item {
120 syn::Type::Path(syn::TypePath {
121 qself: None,
122 ref path,
123 }) => {
124 let last = path.segments.last().unwrap().into_value();
125
126 match &last.ident.to_string()[..] {
127 "Option" => {
128 let nested = introspect_type(first_arg(&last.arguments), items, tokens);
129 types::Type::Option(Box::new(nested))
130 }
131 "Punctuated" => {
132 let nested = introspect_type(first_arg(&last.arguments), items, tokens);
133 let punct = match introspect_type(last_arg(&last.arguments), items, tokens) {
134 types::Type::Token(s) => s,
135 _ => panic!(),
136 };
137
David Tolnayfa67ab02019-02-15 20:17:30 -0800138 types::Type::Punctuated(types::Punctuated {
139 element: Box::new(nested),
140 punct,
141 })
Carl Lerche058ff472019-02-13 16:23:52 -0800142 }
143 "Vec" => {
144 let nested = introspect_type(first_arg(&last.arguments), items, tokens);
145 types::Type::Vec(Box::new(nested))
146 }
147 "Box" => {
148 let nested = introspect_type(first_arg(&last.arguments), items, tokens);
149 types::Type::Box(Box::new(nested))
150 }
151 "Brace" | "Bracket" | "Paren" | "Group" => {
David Tolnay295141b2019-02-15 12:45:33 -0800152 types::Type::Group(last.ident.to_string())
Carl Lerche058ff472019-02-13 16:23:52 -0800153 }
David Tolnay47fe7402019-02-15 14:35:25 -0800154 "TokenStream" | "Literal" | "Ident" | "Span" => {
155 types::Type::Ext(last.ident.to_string())
156 }
Carl Lerche058ff472019-02-13 16:23:52 -0800157 "String" | "u32" | "usize" | "bool" => types::Type::Std(last.ident.to_string()),
158 _ => {
159 if items.get(&last.ident).is_some() {
David Tolnayd3076572019-02-15 13:32:44 -0800160 types::Type::Syn(last.ident.to_string())
Carl Lerche058ff472019-02-13 16:23:52 -0800161 } else {
162 unimplemented!("{}", last.ident.to_string());
163 }
164 }
165 }
166 }
167 syn::Type::Tuple(syn::TypeTuple { ref elems, .. }) => {
168 let tys = elems
169 .iter()
170 .map(|ty| introspect_type(&ty, items, tokens))
171 .collect();
172 types::Type::Tuple(tys)
173 }
174 syn::Type::Macro(syn::TypeMacro { ref mac })
175 if mac.path.segments.last().unwrap().into_value().ident == "Token" =>
176 {
177 let content = mac.tts.to_string();
178 let ty = tokens.get(&content).unwrap().to_string();
179
David Tolnay157c7eb2019-02-15 13:21:48 -0800180 types::Type::Token(ty)
Carl Lerche058ff472019-02-13 16:23:52 -0800181 }
182 _ => panic!("{}", quote!(#item).to_string()),
183 }
184}
185
186fn introspect_features(attrs: &[syn::Attribute]) -> types::Features {
187 let mut ret = types::Features::default();
188
189 for attr in attrs {
190 if !attr.path.is_ident("cfg") {
191 continue;
192 }
193
194 let features: types::Features = syn::parse2(attr.tts.clone()).unwrap();
195 ret.join(&features);
196 }
197
198 ret
199}
200
201fn is_pub(vis: &syn::Visibility) -> bool {
202 match vis {
203 syn::Visibility::Public(_) => true,
204 _ => false,
205 }
206}
207
208fn first_arg(params: &syn::PathArguments) -> &syn::Type {
209 let data = match *params {
210 syn::PathArguments::AngleBracketed(ref data) => data,
211 _ => panic!("Expected at least 1 type argument here"),
212 };
213
214 match **data
215 .args
216 .first()
217 .expect("Expected at least 1 type argument here")
218 .value()
219 {
220 syn::GenericArgument::Type(ref ty) => ty,
221 _ => panic!("Expected at least 1 type argument here"),
222 }
223}
224
225fn last_arg(params: &syn::PathArguments) -> &syn::Type {
226 let data = match *params {
227 syn::PathArguments::AngleBracketed(ref data) => data,
228 _ => panic!("Expected at least 1 type argument here"),
229 };
230
231 match **data
232 .args
233 .last()
234 .expect("Expected at least 1 type argument here")
235 .value()
236 {
237 syn::GenericArgument::Type(ref ty) => ty,
238 _ => panic!("Expected at least 1 type argument here"),
239 }
240}
241
242mod parsing {
243 use super::{AstItem, TokenLookup};
244 use crate::types;
245
246 use proc_macro2::TokenStream;
247 use syn;
248 use syn::parse::{Parse, ParseStream, Result};
249 use syn::*;
250
251 use std::collections::BTreeMap;
252
253 fn peek_tag(input: ParseStream, tag: &str) -> bool {
254 let ahead = input.fork();
255 ahead.parse::<Token![#]>().is_ok()
256 && ahead
257 .parse::<Ident>()
258 .map(|ident| ident == tag)
259 .unwrap_or(false)
260 }
261
262 // Parses #full - returns #[cfg(feature = "full")] if it is present, and
263 // nothing otherwise.
264 fn full(input: ParseStream) -> Vec<syn::Attribute> {
265 if peek_tag(input, "full") {
266 input.parse::<Token![#]>().unwrap();
267 input.parse::<Ident>().unwrap();
268 vec![parse_quote!(#[cfg(feature = "full")])]
269 } else {
270 vec![]
271 }
272 }
273
274 fn skip_manual_extra_traits(input: ParseStream) {
275 if peek_tag(input, "manual_extra_traits") {
276 input.parse::<Token![#]>().unwrap();
277 input.parse::<Ident>().unwrap();
278 }
279 }
280
281 // Parses a simple AstStruct without the `pub struct` prefix.
282 fn ast_struct_inner(input: ParseStream) -> Result<AstItem> {
283 let ident: Ident = input.parse()?;
284 let features = full(input);
285 skip_manual_extra_traits(input);
286 let rest: TokenStream = input.parse()?;
287 Ok(AstItem {
288 ast: syn::parse2(quote! {
289 pub struct #ident #rest
290 })?,
291 features,
292 })
293 }
294
295 // ast_struct! parsing
296 pub struct AstStruct(pub(super) Vec<AstItem>);
297 impl Parse for AstStruct {
298 fn parse(input: ParseStream) -> Result<Self> {
299 input.call(Attribute::parse_outer)?;
300 input.parse::<Token![pub]>()?;
301 input.parse::<Token![struct]>()?;
302 let res = input.call(ast_struct_inner)?;
303 Ok(AstStruct(vec![res]))
304 }
305 }
306
307 fn no_visit(input: ParseStream) -> bool {
308 if peek_tag(input, "no_visit") {
309 input.parse::<Token![#]>().unwrap();
310 input.parse::<Ident>().unwrap();
311 true
312 } else {
313 false
314 }
315 }
316
317 // ast_enum! parsing
318 pub struct AstEnum(pub Vec<AstItem>);
319 impl Parse for AstEnum {
320 fn parse(input: ParseStream) -> Result<Self> {
321 input.call(Attribute::parse_outer)?;
322 input.parse::<Token![pub]>()?;
323 input.parse::<Token![enum]>()?;
324 let ident: Ident = input.parse()?;
325 let no_visit = no_visit(input);
326 let rest: TokenStream = input.parse()?;
327 Ok(AstEnum(if no_visit {
328 vec![]
329 } else {
330 vec![AstItem {
331 ast: syn::parse2(quote! {
332 pub enum #ident #rest
333 })?,
334 features: vec![],
335 }]
336 }))
337 }
338 }
339
340 // A single variant of an ast_enum_of_structs!
341 struct EosVariant {
342 name: Ident,
343 member: Option<Path>,
344 inner: Option<AstItem>,
345 }
346 fn eos_variant(input: ParseStream) -> Result<EosVariant> {
347 input.call(Attribute::parse_outer)?;
348 input.parse::<Token![pub]>()?;
349 let variant: Ident = input.parse()?;
350 let (member, inner) = if input.peek(token::Paren) {
351 let content;
352 parenthesized!(content in input);
353 if content.fork().call(ast_struct_inner).is_ok() {
354 let item = content.call(ast_struct_inner)?;
355 (Some(Path::from(item.ast.ident.clone())), Some(item))
356 } else {
357 let path: Path = content.parse()?;
358 (Some(path), None)
359 }
360 } else {
361 (None, None)
362 };
363 input.parse::<Token![,]>()?;
364 Ok(EosVariant {
365 name: variant,
366 member,
367 inner,
368 })
369 }
370
371 // ast_enum_of_structs! parsing
372 pub struct AstEnumOfStructs(pub Vec<AstItem>);
373 impl Parse for AstEnumOfStructs {
374 fn parse(input: ParseStream) -> Result<Self> {
375 input.call(Attribute::parse_outer)?;
376 input.parse::<Token![pub]>()?;
377 input.parse::<Token![enum]>()?;
378 let ident: Ident = input.parse()?;
379
380 let content;
381 braced!(content in input);
382 let mut variants = Vec::new();
383 while !content.is_empty() {
384 variants.push(content.call(eos_variant)?);
385 }
386
387 if let Some(ident) = input.parse::<Option<Ident>>()? {
388 assert_eq!(ident, "do_not_generate_to_tokens");
389 }
390
391 let enum_item = {
392 let variants = variants.iter().map(|v| {
393 let name = v.name.clone();
394 match v.member {
395 Some(ref member) => quote!(#name(#member)),
396 None => quote!(#name),
397 }
398 });
399 parse_quote! {
400 pub enum #ident {
401 #(#variants),*
402 }
403 }
404 };
405 let mut items = vec![AstItem {
406 ast: enum_item,
407 features: vec![],
408 }];
409 items.extend(variants.into_iter().filter_map(|v| v.inner));
410 Ok(AstEnumOfStructs(items))
411 }
412 }
413
414 pub struct TokenMacro(pub TokenLookup);
415 impl Parse for TokenMacro {
416 fn parse(input: ParseStream) -> Result<Self> {
417 let mut tokens = BTreeMap::new();
418 while !input.is_empty() {
419 let content;
420 parenthesized!(content in input);
421 let token = content.parse::<TokenStream>()?.to_string();
422 input.parse::<Token![=]>()?;
423 input.parse::<Token![>]>()?;
424 let content;
425 braced!(content in input);
426 input.parse::<Token![;]>()?;
427 content.parse::<token::Dollar>()?;
428 let path: Path = content.parse()?;
429 let ty = path.segments.last().unwrap().into_value().ident.to_string();
430 tokens.insert(token, ty.to_string());
431 }
432 Ok(TokenMacro(tokens))
433 }
434 }
435
436 fn parse_feature(input: ParseStream) -> Result<String> {
437 let i: syn::Ident = input.parse()?;
438 assert_eq!(i, "feature");
439
440 input.parse::<Token![=]>()?;
441 let s = input.parse::<syn::LitStr>()?;
442
443 Ok(s.value())
444 }
445
446 impl Parse for types::Features {
447 fn parse(input: ParseStream) -> Result<Self> {
448 let mut features = vec![];
449
450 let level_1;
451 parenthesized!(level_1 in input);
452
453 let i: syn::Ident = level_1.fork().parse()?;
454
455 if i == "any" {
456 level_1.parse::<syn::Ident>()?;
457
458 let level_2;
459 parenthesized!(level_2 in level_1);
460
461 while !level_2.is_empty() {
462 features.push(parse_feature(&level_2)?);
463
464 if !level_2.is_empty() {
465 level_2.parse::<Token![,]>()?;
466 }
467 }
468 } else if i == "feature" {
469 features.push(parse_feature(&level_1)?);
470 assert!(level_1.is_empty());
471 } else {
472 panic!("{:?}", i);
473 }
474
475 assert!(input.is_empty());
476
David Tolnayfa67ab02019-02-15 20:17:30 -0800477 Ok(types::Features { any: features })
Carl Lerche058ff472019-02-13 16:23:52 -0800478 }
479 }
480}
481
482fn get_features(attrs: &[syn::Attribute], base: &[syn::Attribute]) -> Vec<syn::Attribute> {
483 let mut ret = base.to_owned();
484
485 for attr in attrs {
486 if attr.path.is_ident("cfg") {
487 ret.push(attr.clone());
488 }
489 }
490
491 ret
492}
493
494type Error = Box<::std::error::Error>;
495
496fn load_file<P: AsRef<Path>>(
497 name: P,
498 features: &[syn::Attribute],
499 lookup: &mut ItemLookup,
500) -> Result<(), Error> {
501 let name = name.as_ref();
502 let parent = name.parent().ok_or("no parent path")?;
503
504 let mut f = File::open(name)?;
505 let mut src = String::new();
506 f.read_to_string(&mut src)?;
507
508 // Parse the file
509 let file = syn::parse_file(&src)?;
510
511 // Collect all of the interesting AstItems declared in this file or submodules.
512 'items: for item in file.items {
513 match item {
514 Item::Mod(item) => {
515 // Don't inspect inline modules.
516 if item.content.is_some() {
517 continue;
518 }
519
520 // We don't want to try to load the generated rust files and
521 // parse them, so we ignore them here.
522 for name in IGNORED_MODS {
523 if item.ident == name {
524 continue 'items;
525 }
526 }
527
528 // Lookup any #[cfg()] attributes on the module and add them to
529 // the feature set.
530 //
531 // The derive module is weird because it is built with either
532 // `full` or `derive` but exported only under `derive`.
533 let features = if item.ident == "derive" {
534 vec![parse_quote!(#[cfg(feature = "derive")])]
535 } else {
536 get_features(&item.attrs, features)
537 };
538
539 // Look up the submodule file, and recursively parse it.
540 // XXX: Only handles same-directory .rs file submodules.
541 let path = parent.join(&format!("{}.rs", item.ident));
542 load_file(path, &features, lookup)?;
543 }
544 Item::Macro(item) => {
545 // Lookip any #[cfg()] attributes directly on the macro
546 // invocation, and add them to the feature set.
547 let features = get_features(&item.attrs, features);
548
549 // Try to parse the AstItem declaration out of the item.
550 let tts = &item.mac.tts;
551 let found = if item.mac.path.is_ident("ast_struct") {
552 syn::parse2::<parsing::AstStruct>(quote!(#tts))?.0
553 } else if item.mac.path.is_ident("ast_enum") {
554 syn::parse2::<parsing::AstEnum>(quote!(#tts))?.0
555 } else if item.mac.path.is_ident("ast_enum_of_structs") {
556 syn::parse2::<parsing::AstEnumOfStructs>(quote!(#tts))?.0
557 } else {
558 continue;
559 };
560
561 // Record our features on the parsed AstItems.
562 for mut item in found {
563 item.features.extend(features.clone());
564 lookup.insert(item.ast.ident.clone(), item);
565 }
566 }
567 Item::Struct(item) => {
568 let ident = item.ident;
569 if EXTRA_TYPES.contains(&&ident.to_string()[..]) {
570 lookup.insert(
571 ident.clone(),
572 AstItem {
573 ast: DeriveInput {
574 ident,
575 vis: item.vis,
576 attrs: item.attrs,
577 generics: item.generics,
578 data: Data::Struct(DataStruct {
579 fields: item.fields,
580 struct_token: item.struct_token,
581 semi_token: item.semi_token,
582 }),
583 },
584 features: features.to_owned(),
585 },
586 );
587 }
588 }
589 _ => {}
590 }
591 }
592 Ok(())
593}
594
595fn load_token_file<P: AsRef<Path>>(name: P) -> Result<TokenLookup, Error> {
596 let name = name.as_ref();
597 let mut f = File::open(name)?;
598 let mut src = String::new();
599 f.read_to_string(&mut src)?;
600 let file = syn::parse_file(&src)?;
601 for item in file.items {
602 match item {
603 Item::Macro(item) => {
604 match item.ident {
605 Some(ref i) if i == "Token" => {}
606 _ => continue,
607 }
608 let tts = &item.mac.tts;
609 let tokens = syn::parse2::<parsing::TokenMacro>(quote!(#tts))?.0;
610 return Ok(tokens);
611 }
612 _ => {}
613 }
614 }
615
616 Err("failed to parse Token macro".into())
617}