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 |
David Tolnay | b7622c9 | 2019-05-08 13:17:59 -0700 | [diff] [blame] | 27 | //! |
| 28 | //! ## Example |
| 29 | //! |
| 30 | //! ``` |
| 31 | //! use syn_codegen::Definitions; |
| 32 | //! |
| 33 | //! # const IGNORE: &str = stringify! { |
| 34 | //! const SYN: &str = include_str!("syn.json"); |
| 35 | //! # }; |
| 36 | //! # const SYN: &str = include_str!("../../syn.json"); |
| 37 | //! |
| 38 | //! fn main() { |
| 39 | //! let defs: Definitions = serde_json::from_str(SYN).unwrap(); |
| 40 | //! |
| 41 | //! for node in &defs.types { |
| 42 | //! println!("syn::{}", node.ident); |
| 43 | //! } |
| 44 | //! } |
| 45 | //! ``` |
David Tolnay | 35f071f | 2019-05-08 12:40:13 -0700 | [diff] [blame] | 46 | |
David Tolnay | 14d463e | 2019-02-15 14:23:51 -0800 | [diff] [blame] | 47 | use indexmap::IndexMap; |
David Tolnay | 822790e | 2019-02-15 21:12:30 -0800 | [diff] [blame] | 48 | use semver::Version; |
David Tolnay | 1022712 | 2019-02-15 20:53:45 -0800 | [diff] [blame] | 49 | use serde::{Deserialize, Deserializer, Serialize}; |
David Tolnay | 14d463e | 2019-02-15 14:23:51 -0800 | [diff] [blame] | 50 | |
David Tolnay | 440fe58 | 2019-02-15 20:23:14 -0800 | [diff] [blame] | 51 | use std::collections::{BTreeMap, BTreeSet}; |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 52 | |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 53 | /// Top-level content of the syntax tree description. |
David Tolnay | 4bc5523 | 2019-02-15 21:09:00 -0800 | [diff] [blame] | 54 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame] | 55 | pub struct Definitions { |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 56 | /// The Syn version whose syntax tree is described by this data. |
David Tolnay | 822790e | 2019-02-15 21:12:30 -0800 | [diff] [blame] | 57 | pub version: Version, |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 58 | |
| 59 | /// Syntax tree types defined by Syn. |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame] | 60 | pub types: Vec<Node>, |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 61 | |
| 62 | /// Token types defined by Syn (keywords as well as punctuation). |
| 63 | /// |
| 64 | /// The keys in the map are the Rust type name for the token. The values in |
| 65 | /// the map are the printed token representation. |
| 66 | /// |
| 67 | /// These tokens are accessible in the Syn public API as `syn::token::#name` |
| 68 | /// or alternatively `syn::Token![#repr]`. |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame] | 69 | pub tokens: BTreeMap<String, String>, |
| 70 | } |
| 71 | |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 72 | /// Syntax tree type defined by Syn. |
David Tolnay | 4bc5523 | 2019-02-15 21:09:00 -0800 | [diff] [blame] | 73 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 74 | pub struct Node { |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 75 | /// Name of the type. |
| 76 | /// |
| 77 | /// This type is accessible in the Syn public API as `syn::#name`. |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 78 | pub ident: String, |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 79 | |
| 80 | /// Features behind which this type is cfg gated. |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 81 | pub features: Features, |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 82 | |
| 83 | /// Content of the data structure. |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 84 | #[serde( |
| 85 | flatten, |
| 86 | skip_serializing_if = "is_private", |
David Tolnay | 6f1b7f2 | 2019-02-15 21:38:54 -0800 | [diff] [blame] | 87 | deserialize_with = "private_if_absent" |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 88 | )] |
| 89 | pub data: Data, |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 90 | } |
| 91 | |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 92 | /// Content of a syntax tree data structure. |
David Tolnay | 4bc5523 | 2019-02-15 21:09:00 -0800 | [diff] [blame] | 93 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 94 | pub enum Data { |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 95 | /// This is an opaque type with no publicy accessible structure. |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 96 | Private, |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 97 | |
| 98 | /// This type is a braced struct with named fields. |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 99 | #[serde(rename = "fields")] |
David Tolnay | 75c5a17 | 2019-02-15 20:35:41 -0800 | [diff] [blame] | 100 | Struct(Fields), |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 101 | |
| 102 | /// This type is an enum. |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 103 | #[serde(rename = "variants")] |
David Tolnay | 75c5a17 | 2019-02-15 20:35:41 -0800 | [diff] [blame] | 104 | Enum(Variants), |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 105 | } |
| 106 | |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 107 | /// Fields of a braced struct syntax tree node with named fields. |
| 108 | /// |
| 109 | /// The keys in the map are the field names. |
David Tolnay | 75c5a17 | 2019-02-15 20:35:41 -0800 | [diff] [blame] | 110 | pub type Fields = IndexMap<String, Type>; |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 111 | |
| 112 | /// Variants of an enum syntax tree node. |
| 113 | /// |
| 114 | /// The keys in the map are the variant names. |
| 115 | /// |
| 116 | /// Variants are unit variants if they hold no data and tuple variants |
| 117 | /// otherwise. The Syn syntax tree does not make use of braced variants. |
David Tolnay | 75c5a17 | 2019-02-15 20:35:41 -0800 | [diff] [blame] | 118 | pub type Variants = IndexMap<String, Vec<Type>>; |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 119 | |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 120 | /// Type of a struct field or tuple variant field in the syntax tree. |
David Tolnay | 4bc5523 | 2019-02-15 21:09:00 -0800 | [diff] [blame] | 121 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
David Tolnay | 295141b | 2019-02-15 12:45:33 -0800 | [diff] [blame] | 122 | #[serde(rename_all = "lowercase")] |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 123 | pub enum Type { |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 124 | /// Syntax tree type defined by Syn. |
| 125 | /// |
| 126 | /// This name will match the ident of some `Node`. |
David Tolnay | d307657 | 2019-02-15 13:32:44 -0800 | [diff] [blame] | 127 | Syn(String), |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 128 | |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 129 | /// Type defined by the Rust language or standard library. |
| 130 | /// |
| 131 | /// All such types used by Syn are accessible in the Rust prelude and can be |
| 132 | /// used without a qualifying path in most Rust code. |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 133 | Std(String), |
| 134 | |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 135 | /// Type defined by proc-macro2. |
| 136 | /// |
| 137 | /// The type is accessible in the proc-macro2 public API as |
| 138 | /// `proc_macro2::#name`. |
David Tolnay | d307657 | 2019-02-15 13:32:44 -0800 | [diff] [blame] | 139 | #[serde(rename = "proc_macro2")] |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 140 | Ext(String), |
| 141 | |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 142 | /// Keyword or punctuation token type defined by Syn. |
| 143 | /// |
| 144 | /// This name will match one of the keys in the `tokens` map. |
David Tolnay | 157c7eb | 2019-02-15 13:21:48 -0800 | [diff] [blame] | 145 | Token(String), |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 146 | |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 147 | /// Grouping token defined by Syn. |
| 148 | /// |
| 149 | /// The type is accessible in the Syn public API as `syn::token::#name`. |
David Tolnay | 295141b | 2019-02-15 12:45:33 -0800 | [diff] [blame] | 150 | Group(String), |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 151 | |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 152 | /// Punctuated list. |
| 153 | /// |
| 154 | /// This refers to `syn::punctuated::Punctuated<T, P>` with the specified |
| 155 | /// element type and punctuation. |
David Tolnay | 295141b | 2019-02-15 12:45:33 -0800 | [diff] [blame] | 156 | Punctuated(Punctuated), |
| 157 | |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 158 | /// `std::option::Option` |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 159 | Option(Box<Type>), |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 160 | |
| 161 | /// `std::boxed::Box` |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 162 | Box(Box<Type>), |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 163 | |
| 164 | /// `std::vec::Vec` |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 165 | Vec(Box<Type>), |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 166 | |
| 167 | /// Rust tuple with two or more fields. |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 168 | Tuple(Vec<Type>), |
| 169 | } |
| 170 | |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 171 | /// Type of a punctuated list. |
| 172 | /// |
| 173 | /// This refers to `syn::punctuated::Punctuated<#element, #punct>`. |
| 174 | /// |
| 175 | /// The punct string will match one of the keys in the `tokens` map. |
David Tolnay | 4bc5523 | 2019-02-15 21:09:00 -0800 | [diff] [blame] | 176 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
David Tolnay | 295141b | 2019-02-15 12:45:33 -0800 | [diff] [blame] | 177 | pub struct Punctuated { |
David Tolnay | 485973a | 2019-02-15 14:42:48 -0800 | [diff] [blame] | 178 | pub element: Box<Type>, |
| 179 | pub punct: String, |
David Tolnay | 295141b | 2019-02-15 12:45:33 -0800 | [diff] [blame] | 180 | } |
| 181 | |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 182 | /// Features behind which a syntax tree type is cfg gated. |
David Tolnay | 4bc5523 | 2019-02-15 21:09:00 -0800 | [diff] [blame] | 183 | #[derive(Debug, Default, PartialEq, Serialize, Deserialize)] |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 184 | pub struct Features { |
David Tolnay | 5b2cd85 | 2019-05-08 13:09:33 -0700 | [diff] [blame] | 185 | /// Type is accessible if at least one of these features is enabled against |
| 186 | /// the Syn dependency. |
David Tolnay | 440fe58 | 2019-02-15 20:23:14 -0800 | [diff] [blame] | 187 | pub any: BTreeSet<String>, |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 188 | } |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 189 | |
| 190 | fn is_private(data: &Data) -> bool { |
| 191 | match data { |
| 192 | Data::Private => true, |
| 193 | Data::Struct(_) | Data::Enum(_) => false, |
| 194 | } |
| 195 | } |
| 196 | |
David Tolnay | 6f1b7f2 | 2019-02-15 21:38:54 -0800 | [diff] [blame] | 197 | fn private_if_absent<'de, D>(deserializer: D) -> Result<Data, D::Error> |
David Tolnay | c2be7b2 | 2019-02-15 18:48:31 -0800 | [diff] [blame] | 198 | where |
| 199 | D: Deserializer<'de>, |
| 200 | { |
| 201 | let option = Option::deserialize(deserializer)?; |
| 202 | Ok(option.unwrap_or(Data::Private)) |
| 203 | } |