| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | use crate::buffer::Cursor;
|
| 6 | use crate::error::Result;
|
| 7 | use crate::parse::ParseStream;
|
| 8 | use crate::parse::Peek;
|
| 9 | use crate::sealed::lookahead;
|
| 10 | use crate::token::CustomToken;
|
| 11 | use proc_macro2::Ident;
|
| 12 |
|
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | pub trait IdentExt: Sized + private::Sealed {
|
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | fn parse_any(input: ParseStream) -> Result<Self>;
|
| 49 |
|
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | #[allow(non_upper_case_globals)]
|
| 56 | const peek_any: private::PeekFn = private::PeekFn;
|
| 57 |
|
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | fn unraw(&self) -> Ident;
|
| 86 | }
|
| 87 |
|
| 88 | impl IdentExt for Ident {
|
| 89 | fn parse_any(input: ParseStream) -> Result<Self> {
|
| 90 | input.step(|cursor| match cursor.ident() {
|
| 91 | Some((ident, rest)) => Ok((ident, rest)),
|
| 92 | None => Err(cursor.error("expected ident")),
|
| 93 | })
|
| 94 | }
|
| 95 |
|
| 96 | fn unraw(&self) -> Ident {
|
| 97 | let string = self.to_string();
|
| 98 | if let Some(string) = string.strip_prefix("r#") {
|
| 99 | Ident::new(string, self.span())
|
| 100 | } else {
|
| 101 | self.clone()
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | impl Peek for private::PeekFn {
|
| 107 | type Token = private::IdentAny;
|
| 108 | }
|
| 109 |
|
| 110 | impl CustomToken for private::IdentAny {
|
| 111 | fn peek(cursor: Cursor) -> bool {
|
| 112 | cursor.ident().is_some()
|
| 113 | }
|
| 114 |
|
| 115 | fn display() -> &'static str {
|
| 116 | "identifier"
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | impl lookahead::Sealed for private::PeekFn {}
|
| 121 |
|
| 122 | mod private {
|
| 123 | use proc_macro2::Ident;
|
| 124 |
|
| 125 | pub trait Sealed {}
|
| 126 |
|
| 127 | impl Sealed for Ident {}
|
| 128 |
|
| 129 | pub struct PeekFn;
|
| 130 | pub struct IdentAny;
|
| 131 |
|
| 132 | impl Copy for PeekFn {}
|
| 133 | impl Clone for PeekFn {
|
| 134 | fn clone(&self) -> Self {
|
| 135 | *self
|
| 136 | }
|
| 137 | }
|
| 138 | }
|