]> git.proxmox.com Git - rustc.git/blob - src/vendor/syn-0.11.11/src/ident.rs
New upstream version 1.27.1+dfsg1
[rustc.git] / src / vendor / syn-0.11.11 / src / ident.rs
1 use std::borrow::Cow;
2 use std::fmt::{self, Display};
3
4 #[derive(Debug, Clone, Eq, Hash, Ord, PartialOrd)]
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
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
25 impl From<String> for Ident {
26 fn from(s: String) -> Self {
27 Ident(s)
28 }
29 }
30
31 impl From<usize> for Ident {
32 fn from(u: usize) -> Self {
33 Ident(u.to_string())
34 }
35 }
36
37 impl AsRef<str> for Ident {
38 fn as_ref(&self) -> &str {
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 }
48
49 impl<T: ?Sized> PartialEq<T> for Ident
50 where T: AsRef<str>
51 {
52 fn eq(&self, other: &T) -> bool {
53 self.0 == other.as_ref()
54 }
55 }
56
57 #[cfg(feature = "parsing")]
58 pub mod parsing {
59 use super::*;
60 use synom::IResult;
61 use synom::space::skip_whitespace;
62 use unicode_xid::UnicodeXID;
63
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> {
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
92 for (i, ch) in chars {
93 if !UnicodeXID::is_xid_continue(ch) {
94 return IResult::Done(&input[i..], input[..i].into());
95 }
96 }
97
98 IResult::Done("", input.into())
99 }
100
101 #[cfg(feature = "full")]
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 {
108 IResult::Error
109 } else {
110 IResult::Done(&input[i..], input[..i].into())
111 };
112 }
113 }
114
115 IResult::Done("", input.into())
116 }
117 }
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 }