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