David Tolnay | ac51b36 | 2018-08-30 23:59:52 -0700 | [diff] [blame] | 1 | //! Extension traits to provide parsing methods on foreign types. |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 2 | //! |
| 3 | //! *This module is available if Syn is built with the `"parsing"` feature.* |
| 4 | |
| 5 | use proc_macro2::Ident; |
| 6 | |
David Tolnay | b8a68e4 | 2019-04-22 14:01:56 -0700 | [diff] [blame^] | 7 | use buffer::Cursor; |
| 8 | use parse::{ParseStream, Peek, Result}; |
| 9 | use sealed::lookahead; |
| 10 | use token::CustomToken; |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 11 | |
David Tolnay | c15a000 | 2019-04-22 12:33:39 -0700 | [diff] [blame] | 12 | /// Additional methods for `Ident` not provided by proc-macro2 or libproc_macro. |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 13 | /// |
David Tolnay | c15a000 | 2019-04-22 12:33:39 -0700 | [diff] [blame] | 14 | /// This trait is sealed and cannot be implemented for types outside of Syn. It |
| 15 | /// is implemented only for `proc_macro2::Ident`. |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 16 | /// |
| 17 | /// *This trait is available if Syn is built with the `"parsing"` feature.* |
| 18 | pub trait IdentExt: Sized + private::Sealed { |
| 19 | /// Parses any identifier including keywords. |
| 20 | /// |
David Tolnay | b8a68e4 | 2019-04-22 14:01:56 -0700 | [diff] [blame^] | 21 | /// This is useful when parsing macro input which allows Rust keywords as |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 22 | /// identifiers. |
| 23 | /// |
David Tolnay | c15a000 | 2019-04-22 12:33:39 -0700 | [diff] [blame] | 24 | /// # Example |
| 25 | /// |
David Tolnay | 95989db | 2019-01-01 15:05:57 -0500 | [diff] [blame] | 26 | /// ```edition2018 |
David Tolnay | fd5b117 | 2018-12-31 17:54:36 -0500 | [diff] [blame] | 27 | /// use syn::{Error, Ident, Result, Token}; |
David Tolnay | ac51b36 | 2018-08-30 23:59:52 -0700 | [diff] [blame] | 28 | /// use syn::ext::IdentExt; |
David Tolnay | 67fea04 | 2018-11-24 14:50:20 -0800 | [diff] [blame] | 29 | /// use syn::parse::ParseStream; |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 30 | /// |
David Tolnay | b8a68e4 | 2019-04-22 14:01:56 -0700 | [diff] [blame^] | 31 | /// mod kw { |
| 32 | /// syn::custom_keyword!(name); |
| 33 | /// } |
| 34 | /// |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 35 | /// // Parses input that looks like `name = NAME` where `NAME` can be |
| 36 | /// // any identifier. |
| 37 | /// // |
| 38 | /// // Examples: |
| 39 | /// // |
| 40 | /// // name = anything |
| 41 | /// // name = impl |
David Tolnay | ac51b36 | 2018-08-30 23:59:52 -0700 | [diff] [blame] | 42 | /// fn parse_dsl(input: ParseStream) -> Result<Ident> { |
David Tolnay | b8a68e4 | 2019-04-22 14:01:56 -0700 | [diff] [blame^] | 43 | /// input.parse::<kw::name>()?; |
David Tolnay | ac51b36 | 2018-08-30 23:59:52 -0700 | [diff] [blame] | 44 | /// input.parse::<Token![=]>()?; |
| 45 | /// let name = input.call(Ident::parse_any)?; |
| 46 | /// Ok(name) |
| 47 | /// } |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 48 | /// ``` |
| 49 | fn parse_any(input: ParseStream) -> Result<Self>; |
David Tolnay | c15a000 | 2019-04-22 12:33:39 -0700 | [diff] [blame] | 50 | |
David Tolnay | b8a68e4 | 2019-04-22 14:01:56 -0700 | [diff] [blame^] | 51 | /// Peeks any identifier including keywords. Usage: |
| 52 | /// `input.peek(Ident::peek_any)` |
| 53 | /// |
| 54 | /// This is different from `input.peek(Ident)` which only returns true in |
| 55 | /// the case of an ident which is not a Rust keyword. |
| 56 | #[allow(non_upper_case_globals)] |
| 57 | const peek_any: private::PeekFn = private::PeekFn; |
| 58 | |
David Tolnay | c15a000 | 2019-04-22 12:33:39 -0700 | [diff] [blame] | 59 | /// Strips the raw marker `r#`, if any, from the beginning of an ident. |
| 60 | /// |
| 61 | /// - unraw(`x`) = `x` |
| 62 | /// - unraw(`move`) = `move` |
| 63 | /// - unraw(`r#move`) = `move` |
| 64 | /// |
| 65 | /// # Example |
| 66 | /// |
| 67 | /// In the case of interop with other languages like Python that have a |
| 68 | /// different set of keywords than Rust, we might come across macro input |
| 69 | /// that involves raw identifiers to refer to ordinary variables in the |
| 70 | /// other language with a name that happens to be a Rust keyword. |
| 71 | /// |
| 72 | /// The function below appends an identifier from the caller's input onto a |
| 73 | /// fixed prefix. Without using `unraw()`, this would tend to produce |
| 74 | /// invalid identifiers like `__pyo3_get_r#move`. |
| 75 | /// |
| 76 | /// ```edition2018 |
| 77 | /// use proc_macro2::Span; |
| 78 | /// use syn::Ident; |
| 79 | /// use syn::ext::IdentExt; |
| 80 | /// |
| 81 | /// fn ident_for_getter(variable: &Ident) -> Ident { |
| 82 | /// let getter = format!("__pyo3_get_{}", variable.unraw()); |
| 83 | /// Ident::new(&getter, Span::call_site()) |
| 84 | /// } |
| 85 | /// ``` |
| 86 | fn unraw(&self) -> Ident; |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | impl IdentExt for Ident { |
| 90 | fn parse_any(input: ParseStream) -> Result<Self> { |
| 91 | input.step(|cursor| match cursor.ident() { |
| 92 | Some((ident, rest)) => Ok((ident, rest)), |
| 93 | None => Err(cursor.error("expected ident")), |
| 94 | }) |
| 95 | } |
David Tolnay | c15a000 | 2019-04-22 12:33:39 -0700 | [diff] [blame] | 96 | |
| 97 | fn unraw(&self) -> Ident { |
| 98 | let string = self.to_string(); |
| 99 | if string.starts_with("r#") { |
| 100 | Ident::new(&string[2..], self.span()) |
| 101 | } else { |
| 102 | self.clone() |
| 103 | } |
| 104 | } |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 105 | } |
| 106 | |
David Tolnay | b8a68e4 | 2019-04-22 14:01:56 -0700 | [diff] [blame^] | 107 | impl Peek for private::PeekFn { |
| 108 | type Token = private::IdentAny; |
| 109 | } |
| 110 | |
| 111 | impl CustomToken for private::IdentAny { |
| 112 | fn peek(cursor: Cursor) -> bool { |
| 113 | cursor.ident().is_some() |
| 114 | } |
| 115 | |
| 116 | fn display() -> &'static str { |
| 117 | "identifier" |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | impl lookahead::Sealed for private::PeekFn {} |
| 122 | |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 123 | mod private { |
| 124 | use proc_macro2::Ident; |
| 125 | |
| 126 | pub trait Sealed {} |
| 127 | |
| 128 | impl Sealed for Ident {} |
David Tolnay | b8a68e4 | 2019-04-22 14:01:56 -0700 | [diff] [blame^] | 129 | |
| 130 | #[derive(Copy, Clone)] |
| 131 | pub struct PeekFn; |
| 132 | pub struct IdentAny; |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 133 | } |