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