David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 9 | use std::cmp::Ordering; |
| 10 | use std::fmt::{self, Display}; |
| 11 | use std::hash::{Hash, Hasher}; |
| 12 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 13 | use proc_macro2::{Span, Ident}; |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 14 | use unicode_xid::UnicodeXID; |
| 15 | |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 16 | /// A Rust lifetime: `'a`. |
| 17 | /// |
| 18 | /// Lifetime names must conform to the following rules: |
| 19 | /// |
| 20 | /// - Must start with an apostrophe. |
| 21 | /// - Must not consist of just an apostrophe: `'`. |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 22 | /// - Character after the apostrophe must be `_` or a Unicode code point with |
| 23 | /// the XID_Start property. |
| 24 | /// - All following characters must be Unicode code points with the XID_Continue |
| 25 | /// property. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 26 | /// |
| 27 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 28 | /// feature.* |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 29 | #[cfg_attr(feature = "extra-traits", derive(Debug))] |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 30 | #[derive(Clone)] |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 31 | pub struct Lifetime { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 32 | ident: Ident, |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | impl Lifetime { |
David Tolnay | e2099f3 | 2018-03-31 18:42:43 +0200 | [diff] [blame] | 36 | pub fn new(s: &str, span: Span) -> Self { |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 37 | if !s.starts_with('\'') { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 38 | panic!( |
| 39 | "lifetime name must start with apostrophe as in \"'a\", \ |
| 40 | got {:?}", |
| 41 | s |
| 42 | ); |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | if s == "'" { |
| 46 | panic!("lifetime name must not be empty"); |
| 47 | } |
| 48 | |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 49 | fn xid_ok(s: &str) -> bool { |
| 50 | let mut chars = s.chars(); |
| 51 | let first = chars.next().unwrap(); |
| 52 | if !(UnicodeXID::is_xid_start(first) || first == '_') { |
| 53 | return false; |
| 54 | } |
| 55 | for ch in chars { |
| 56 | if !UnicodeXID::is_xid_continue(ch) { |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | true |
| 61 | } |
| 62 | |
| 63 | if !xid_ok(&s[1..]) { |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 64 | panic!("{:?} is not a valid lifetime name", s); |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | Lifetime { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 68 | ident: Ident::new(&s[1..], span), |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 71 | |
| 72 | pub fn span(&self) -> Span { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 73 | self.ident.span() |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 77 | self.ident.set_span(span); |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 78 | } |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | impl Display for Lifetime { |
| 82 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 83 | "'".fmt(formatter)?; |
| 84 | self.ident.fmt(formatter) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | impl PartialEq for Lifetime { |
| 89 | fn eq(&self, other: &Lifetime) -> bool { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 90 | self.ident.eq(&other.ident) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | impl Eq for Lifetime {} |
| 95 | |
| 96 | impl PartialOrd for Lifetime { |
| 97 | fn partial_cmp(&self, other: &Lifetime) -> Option<Ordering> { |
| 98 | Some(self.cmp(other)) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl Ord for Lifetime { |
| 103 | fn cmp(&self, other: &Lifetime) -> Ordering { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 104 | self.ident.cmp(&other.ident) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | impl Hash for Lifetime { |
| 109 | fn hash<H: Hasher>(&self, h: &mut H) { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 110 | self.ident.hash(h) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | #[cfg(feature = "parsing")] |
| 115 | pub mod parsing { |
| 116 | use super::*; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 117 | use proc_macro2::Spacing; |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 118 | use buffer::Cursor; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 119 | use parse_error; |
| 120 | use synom::PResult; |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 121 | use synom::Synom; |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 122 | |
| 123 | impl Synom for Lifetime { |
| 124 | fn parse(input: Cursor) -> PResult<Self> { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 125 | let rest = match input.op() { |
| 126 | Some((op, rest)) => { |
| 127 | if op.as_char() == '\'' && op.spacing() == Spacing::Joint { |
| 128 | rest |
| 129 | } else { |
| 130 | return parse_error() |
| 131 | } |
| 132 | } |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 133 | _ => return parse_error(), |
| 134 | }; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 135 | let (ident, rest) = match rest.term() { |
| 136 | Some(pair) => pair, |
| 137 | None => return parse_error(), |
| 138 | }; |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 139 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 140 | Ok((Lifetime { ident: ident }, rest)) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 141 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 142 | |
| 143 | fn description() -> Option<&'static str> { |
| 144 | Some("lifetime") |
| 145 | } |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | #[cfg(feature = "printing")] |
| 150 | mod printing { |
| 151 | use super::*; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 152 | use quote::{ToTokens, TokenStreamExt}; |
| 153 | use proc_macro2::{TokenStream, Punct, Spacing}; |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 154 | |
| 155 | impl ToTokens for Lifetime { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 156 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 157 | tokens.append(Punct::new('\'', Spacing::Joint)); |
| 158 | self.ident.to_tokens(tokens); |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |