David Tolnay | 35f071f | 2019-05-08 12:40:13 -0700 | [diff] [blame^] | 1 | //! # Data structures that describe Syn's syntax tree. |
| 2 | //! |
| 3 | //! The Syn syntax tree is made up of more than 200 types. Occasionally it can |
| 4 | //! come up that you need to implement some behavior across them all. |
| 5 | //! |
| 6 | //! - For example [the Rust integration for AST Explorer][astexplorer] wants to |
| 7 | //! turn a syntax tree from Syn into a JavaScript value understood by the |
| 8 | //! platform's existing cross-language syntax tree visualization code. |
| 9 | //! |
| 10 | //! [astexplorer]: https://astexplorer.net/#/gist/388150a52f74d45a355d2b5e865ded96/0c6d563f28d900472f699c21a1845ad20ae9927f |
| 11 | //! |
| 12 | //! - As another example from within Syn itself, the traits and implementations |
| 13 | //! of the [`visit`], [`visit_mut`], and [`fold`] modules can be generated |
| 14 | //! programmatically from a description of the syntax tree. |
| 15 | //! |
| 16 | //! [`visit`]: https://docs.rs/syn/0.15/syn/visit/index.html |
| 17 | //! [`visit_mut`]: https://docs.rs/syn/0.15/syn/visit_mut/index.html |
| 18 | //! [`fold`]: https://docs.rs/syn/0.15/syn/fold/index.html |
| 19 | //! |
| 20 | //! To make this type of code as easy as possible to implement in any language, |
| 21 | //! every Syn release comes with a machine-readable description of that version |
| 22 | //! of the syntax tree as a JSON file [syn.json]. This `syn-codegen` crate |
| 23 | //! provides the canonical data structures for parsing and making use of the |
| 24 | //! representation in syn.json from Rust code. |
| 25 | //! |
| 26 | //! [syn.json]: https://raw.githubusercontent.com/dtolnay/syn/master/syn.json |
| 27 | |
David Tolnay | 14d463e | 2019-02-15 14:23:51 -0800 | [diff] [blame] | 28 | use indexmap::IndexMap; |
David Tolnay | 822790e | 2019-02-15 21:12:30 -0800 | [diff] [blame] | 29 | use semver::Version; |
David Tolnay | 1022712 | 2019-02-15 20:53:45 -0800 | [diff] [blame] | 30 | use serde::{Deserialize, Deserializer, Serialize}; |
David Tolnay | 14d463e | 2019-02-15 14:23:51 -0800 | [diff] [blame] | 31 | |
David Tolnay | 440fe58 | 2019-02-15 20:23:14 -0800 | [diff] [blame] | 32 | use std::collections::{BTreeMap, BTreeSet}; |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 33 | |
David Tolnay | 4bc5523 | 2019-02-15 21:09:00 -0800 | [diff] [blame] | 34 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame] | 35 | pub struct Definitions { |
David Tolnay | 1022712 | 2019-02-15 20:53:45 -0800 | [diff] [blame] | 36 | /// The Syn version used to generate the introspection file. |
David Tolnay | 822790e | 2019-02-15 21:12:30 -0800 | [diff] [blame] | 37 | pub version: Version, |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame] | 38 | pub types: Vec<Node>, |
| 39 | pub tokens: BTreeMap<String, String>, |
| 40 | } |
| 41 | |
David Tolnay | 4bc5523 | 2019-02-15 21:09:00 -0800 | [diff] [blame] | 42 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 43 | pub struct Node { |
| 44 | pub ident: String, |
| 45 | pub features: Features, |
| 46 | #[serde( |
| 47 | flatten, |
| 48 | skip_serializing_if = "is_private", |
David Tolnay | 6f1b7f2 | 2019-02-15 21:38:54 -0800 | [diff] [blame] | 49 | deserialize_with = "private_if_absent" |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 50 | )] |
| 51 | pub data: Data, |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 52 | } |
| 53 | |
David Tolnay | 4bc5523 | 2019-02-15 21:09:00 -0800 | [diff] [blame] | 54 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 55 | pub enum Data { |
| 56 | Private, |
| 57 | #[serde(rename = "fields")] |
David Tolnay | 75c5a17 | 2019-02-15 20:35:41 -0800 | [diff] [blame] | 58 | Struct(Fields), |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 59 | #[serde(rename = "variants")] |
David Tolnay | 75c5a17 | 2019-02-15 20:35:41 -0800 | [diff] [blame] | 60 | Enum(Variants), |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 61 | } |
| 62 | |
David Tolnay | 75c5a17 | 2019-02-15 20:35:41 -0800 | [diff] [blame] | 63 | pub type Fields = IndexMap<String, Type>; |
| 64 | pub type Variants = IndexMap<String, Vec<Type>>; |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 65 | |
David Tolnay | 4bc5523 | 2019-02-15 21:09:00 -0800 | [diff] [blame] | 66 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
David Tolnay | 295141b | 2019-02-15 12:45:33 -0800 | [diff] [blame] | 67 | #[serde(rename_all = "lowercase")] |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 68 | pub enum Type { |
| 69 | /// Type defined by `syn` |
David Tolnay | d307657 | 2019-02-15 13:32:44 -0800 | [diff] [blame] | 70 | Syn(String), |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 71 | |
| 72 | /// Type defined in `std`. |
| 73 | Std(String), |
| 74 | |
| 75 | /// Type external to `syn` |
David Tolnay | d307657 | 2019-02-15 13:32:44 -0800 | [diff] [blame] | 76 | #[serde(rename = "proc_macro2")] |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 77 | Ext(String), |
| 78 | |
| 79 | /// Token type |
David Tolnay | 157c7eb | 2019-02-15 13:21:48 -0800 | [diff] [blame] | 80 | Token(String), |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 81 | |
| 82 | /// Token group |
David Tolnay | 295141b | 2019-02-15 12:45:33 -0800 | [diff] [blame] | 83 | Group(String), |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 84 | |
| 85 | /// Punctuated list |
David Tolnay | 295141b | 2019-02-15 12:45:33 -0800 | [diff] [blame] | 86 | Punctuated(Punctuated), |
| 87 | |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 88 | Option(Box<Type>), |
| 89 | Box(Box<Type>), |
| 90 | Vec(Box<Type>), |
| 91 | Tuple(Vec<Type>), |
| 92 | } |
| 93 | |
David Tolnay | 4bc5523 | 2019-02-15 21:09:00 -0800 | [diff] [blame] | 94 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
David Tolnay | 295141b | 2019-02-15 12:45:33 -0800 | [diff] [blame] | 95 | pub struct Punctuated { |
David Tolnay | 485973a | 2019-02-15 14:42:48 -0800 | [diff] [blame] | 96 | pub element: Box<Type>, |
| 97 | pub punct: String, |
David Tolnay | 295141b | 2019-02-15 12:45:33 -0800 | [diff] [blame] | 98 | } |
| 99 | |
David Tolnay | 4bc5523 | 2019-02-15 21:09:00 -0800 | [diff] [blame] | 100 | #[derive(Debug, Default, PartialEq, Serialize, Deserialize)] |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 101 | pub struct Features { |
David Tolnay | 440fe58 | 2019-02-15 20:23:14 -0800 | [diff] [blame] | 102 | pub any: BTreeSet<String>, |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 103 | } |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 104 | |
| 105 | fn is_private(data: &Data) -> bool { |
| 106 | match data { |
| 107 | Data::Private => true, |
| 108 | Data::Struct(_) | Data::Enum(_) => false, |
| 109 | } |
| 110 | } |
| 111 | |
David Tolnay | 6f1b7f2 | 2019-02-15 21:38:54 -0800 | [diff] [blame] | 112 | fn private_if_absent<'de, D>(deserializer: D) -> Result<Data, D::Error> |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 113 | where |
| 114 | D: Deserializer<'de>, |
| 115 | { |
| 116 | let option = Option::deserialize(deserializer)?; |
| 117 | Ok(option.unwrap_or(Data::Private)) |
| 118 | } |