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::*; |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 60 | use synom::IResult; |
| 61 | use synom::space::skip_whitespace; |
David Tolnay | e26baba | 2016-10-24 22:05:57 -0700 | [diff] [blame] | 62 | use unicode_xid::UnicodeXID; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 63 | |
David Tolnay | 05f462f | 2016-10-24 22:19:42 -0700 | [diff] [blame] | 64 | pub fn ident(input: &str) -> IResult<&str, Ident> { |
| 65 | let (rest, id) = match word(input) { |
| 66 | IResult::Done(rest, id) => (rest, id), |
| 67 | IResult::Error => return IResult::Error, |
| 68 | }; |
| 69 | |
| 70 | match id.as_ref() { |
| 71 | // From https://doc.rust-lang.org/grammar.html#keywords |
| 72 | "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue" | |
| 73 | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | |
| 74 | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | |
| 75 | "mut" | "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | |
| 76 | "return" | "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | |
| 77 | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | |
| 78 | "while" | "yield" => IResult::Error, |
| 79 | _ => IResult::Done(rest, id), |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | pub fn word(mut input: &str) -> IResult<&str, Ident> { |
David Tolnay | e26baba | 2016-10-24 22:05:57 -0700 | [diff] [blame] | 84 | input = skip_whitespace(input); |
| 85 | |
| 86 | let mut chars = input.char_indices(); |
| 87 | match chars.next() { |
| 88 | Some((_, ch)) if UnicodeXID::is_xid_start(ch) || ch == '_' => {} |
| 89 | _ => return IResult::Error, |
| 90 | } |
| 91 | |
David Tolnay | 02a8d47 | 2017-02-19 12:59:44 -0800 | [diff] [blame] | 92 | for (i, ch) in chars { |
David Tolnay | e26baba | 2016-10-24 22:05:57 -0700 | [diff] [blame] | 93 | if !UnicodeXID::is_xid_continue(ch) { |
| 94 | return IResult::Done(&input[i..], input[..i].into()); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | IResult::Done("", input.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 99 | } |
David Tolnay | 3903944 | 2016-10-30 11:25:21 -0700 | [diff] [blame] | 100 | |
David Tolnay | b19657b | 2016-10-30 12:48:32 -0700 | [diff] [blame] | 101 | #[cfg(feature = "full")] |
David Tolnay | 3903944 | 2016-10-30 11:25:21 -0700 | [diff] [blame] | 102 | pub fn wordlike(mut input: &str) -> IResult<&str, Ident> { |
| 103 | input = skip_whitespace(input); |
| 104 | |
| 105 | for (i, ch) in input.char_indices() { |
| 106 | if !UnicodeXID::is_xid_start(ch) && !UnicodeXID::is_xid_continue(ch) { |
| 107 | return if i == 0 { |
David Tolnay | 05120ef | 2017-03-12 18:29:26 -0700 | [diff] [blame] | 108 | IResult::Error |
| 109 | } else { |
| 110 | IResult::Done(&input[i..], input[..i].into()) |
| 111 | }; |
David Tolnay | 3903944 | 2016-10-30 11:25:21 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | IResult::Done("", input.into()) |
| 116 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 117 | } |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 118 | |
| 119 | #[cfg(feature = "printing")] |
| 120 | mod printing { |
| 121 | use super::*; |
| 122 | use quote::{Tokens, ToTokens}; |
| 123 | |
| 124 | impl ToTokens for Ident { |
| 125 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 126 | tokens.append(self.as_ref()) |
| 127 | } |
| 128 | } |
| 129 | } |