]>
git.proxmox.com Git - rustc.git/blob - src/vendor/syn-0.11.11/src/ident.rs
2 use std
::fmt
::{self, Display}
;
4 #[derive(Debug, Clone, Eq, Hash, Ord, PartialOrd)]
5 pub struct Ident(String
);
8 pub fn new
<T
: Into
<Ident
>>(t
: T
) -> Self {
13 impl<'a
> From
<&'a
str> for Ident
{
14 fn from(s
: &str) -> Self {
19 impl<'a
> From
<Cow
<'a
, str>> for Ident
{
20 fn from(s
: Cow
<'a
, str>) -> Self {
25 impl From
<String
> for Ident
{
26 fn from(s
: String
) -> Self {
31 impl From
<usize> for Ident
{
32 fn from(u
: usize) -> Self {
37 impl AsRef
<str> for Ident
{
38 fn as_ref(&self) -> &str {
43 impl Display
for Ident
{
44 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> Result
<(), fmt
::Error
> {
49 impl<T
: ?Sized
> PartialEq
<T
> for Ident
52 fn eq(&self, other
: &T
) -> bool
{
53 self.0 == other
.as_ref()
57 #[cfg(feature = "parsing")]
61 use synom
::space
::skip_whitespace
;
62 use unicode_xid
::UnicodeXID
;
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
,
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
),
83 pub fn word(mut input
: &str) -> IResult
<&str, Ident
> {
84 input
= skip_whitespace(input
);
86 let mut chars
= input
.char_indices();
88 Some((_
, ch
)) if UnicodeXID
::is_xid_start(ch
) || ch
== '_'
=> {}
89 _
=> return IResult
::Error
,
92 for (i
, ch
) in chars
{
93 if !UnicodeXID
::is_xid_continue(ch
) {
94 return IResult
::Done(&input
[i
..], input
[..i
].into());
98 IResult
::Done("", input
.into())
101 #[cfg(feature = "full")]
102 pub fn wordlike(mut input
: &str) -> IResult
<&str, Ident
> {
103 input
= skip_whitespace(input
);
105 for (i
, ch
) in input
.char_indices() {
106 if !UnicodeXID
::is_xid_start(ch
) && !UnicodeXID
::is_xid_continue(ch
) {
110 IResult
::Done(&input
[i
..], input
[..i
].into())
115 IResult
::Done("", input
.into())
119 #[cfg(feature = "printing")]
122 use quote
::{Tokens, ToTokens}
;
124 impl ToTokens
for Ident
{
125 fn to_tokens(&self, tokens
: &mut Tokens
) {
126 tokens
.append(self.as_ref())