David Tolnay | f733f7e | 2016-11-25 10:36:25 -0800 | [diff] [blame] | 1 | use std::borrow::Cow; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2 | use std::cmp::Ordering; |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 3 | use std::fmt::{self, Display}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 4 | use std::hash::{Hash, Hasher}; |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 5 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 6 | use proc_macro2::Symbol; |
| 7 | |
| 8 | use Span; |
| 9 | |
| 10 | #[derive(Clone)] |
| 11 | pub struct Ident { |
| 12 | pub sym: Symbol, |
| 13 | pub span: Span, |
| 14 | } |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 15 | |
| 16 | impl Ident { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 17 | pub fn new(sym: Symbol, span: Span) -> Self { |
| 18 | Ident { |
| 19 | sym: sym, |
| 20 | span: span, |
| 21 | } |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 22 | } |
| 23 | } |
| 24 | |
| 25 | impl<'a> From<&'a str> for Ident { |
| 26 | fn from(s: &str) -> Self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 27 | Ident::new(s.into(), Span::default()) |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
David Tolnay | f733f7e | 2016-11-25 10:36:25 -0800 | [diff] [blame] | 31 | impl<'a> From<Cow<'a, str>> for Ident { |
| 32 | fn from(s: Cow<'a, str>) -> Self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 33 | Ident::new(s[..].into(), Span::default()) |
David Tolnay | f733f7e | 2016-11-25 10:36:25 -0800 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 37 | impl From<String> for Ident { |
| 38 | fn from(s: String) -> Self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 39 | Ident::new(s[..].into(), Span::default()) |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
David Tolnay | c168310 | 2016-09-27 08:17:58 -0700 | [diff] [blame] | 43 | impl From<usize> for Ident { |
| 44 | fn from(u: usize) -> Self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 45 | Ident::new(u.to_string()[..].into(), Span::default()) |
David Tolnay | c168310 | 2016-09-27 08:17:58 -0700 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 49 | impl AsRef<str> for Ident { |
| 50 | fn as_ref(&self) -> &str { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 51 | self.sym.as_str() |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | impl Display for Ident { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 56 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 57 | self.sym.as_str().fmt(formatter) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl fmt::Debug for Ident { |
| 62 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 63 | fmt::Debug::fmt(self.sym.as_str(), formatter) |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 66 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 67 | impl<T: ?Sized> PartialEq<T> for Ident |
| 68 | where T: AsRef<str> |
| 69 | { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 70 | fn eq(&self, other: &T) -> bool { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 71 | self.as_ref() == other.as_ref() |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl Eq for Ident {} |
| 76 | |
| 77 | impl PartialOrd for Ident { |
| 78 | fn partial_cmp(&self, other: &Ident) -> Option<Ordering> { |
| 79 | Some(self.cmp(other)) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl Ord for Ident { |
| 84 | fn cmp(&self, other: &Ident) -> Ordering { |
| 85 | self.as_ref().cmp(other.as_ref()) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl Hash for Ident { |
| 90 | fn hash<H: Hasher>(&self, h: &mut H) { |
| 91 | self.as_ref().hash(h) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 92 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 93 | } |
| 94 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 95 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 96 | pub mod parsing { |
| 97 | use super::*; |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 98 | use synom::{TokenTree, TokenKind, IResult}; |
| 99 | #[cfg(feature = "full")] |
| 100 | use lit::parsing::int; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 101 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 102 | pub fn ident(input: &[TokenTree]) -> IResult<&[TokenTree], Ident> { |
| 103 | if let IResult::Done(rest, id) = word(input) { |
| 104 | match id.as_ref() { |
| 105 | // From https://doc.rust-lang.org/grammar.html#keywords |
| 106 | "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue" | |
| 107 | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | |
| 108 | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | |
| 109 | "mut" | "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | |
| 110 | "return" | "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | |
| 111 | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | |
| 112 | "while" | "yield" => IResult::Error, |
| 113 | _ => IResult::Done(rest, id), |
| 114 | } |
| 115 | } else { |
| 116 | IResult::Error |
David Tolnay | 05f462f | 2016-10-24 22:19:42 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 120 | pub fn word(input: &[TokenTree]) -> IResult<&[TokenTree], Ident> { |
| 121 | if let Some(&TokenTree { kind: TokenKind::Word(ref id), .. }) = input.first() { |
| 122 | // Check if this word is _actually_ a lifetime, and treat that differently |
| 123 | if id.chars().next().unwrap() == '\'' { |
| 124 | IResult::Error |
| 125 | } else { |
| 126 | IResult::Done(&input[1..], Ident(id.to_string())) |
David Tolnay | e26baba | 2016-10-24 22:05:57 -0700 | [diff] [blame] | 127 | } |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 128 | } else { |
| 129 | IResult::Error |
David Tolnay | e26baba | 2016-10-24 22:05:57 -0700 | [diff] [blame] | 130 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 131 | } |
David Tolnay | 3903944 | 2016-10-30 11:25:21 -0700 | [diff] [blame] | 132 | |
David Tolnay | b19657b | 2016-10-30 12:48:32 -0700 | [diff] [blame] | 133 | #[cfg(feature = "full")] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 134 | named!(pub wordlike -> Ident, alt!( |
| 135 | word |
| 136 | | |
| 137 | int => { |d| format!("{}", d).into() } |
| 138 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 139 | } |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 140 | |
| 141 | #[cfg(feature = "printing")] |
| 142 | mod printing { |
| 143 | use super::*; |
| 144 | use quote::{Tokens, ToTokens}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 145 | use proc_macro2::{TokenTree, TokenKind}; |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 146 | |
| 147 | impl ToTokens for Ident { |
| 148 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 149 | tokens.append(TokenTree { |
| 150 | span: self.span.0, |
| 151 | kind: TokenKind::Word(self.sym), |
| 152 | }) |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |