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 | |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 13 | use proc_macro2::{Ident, Span}; |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 14 | use unicode_xid::UnicodeXID; |
| 15 | |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 16 | #[cfg(feature = "parsing")] |
| 17 | use lookahead; |
Alex Crichton | 131308c | 2018-05-18 14:00:24 -0700 | [diff] [blame] | 18 | |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 19 | /// A Rust lifetime: `'a`. |
| 20 | /// |
| 21 | /// Lifetime names must conform to the following rules: |
| 22 | /// |
| 23 | /// - Must start with an apostrophe. |
| 24 | /// - Must not consist of just an apostrophe: `'`. |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 25 | /// - Character after the apostrophe must be `_` or a Unicode code point with |
| 26 | /// the XID_Start property. |
| 27 | /// - All following characters must be Unicode code points with the XID_Continue |
| 28 | /// property. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 29 | /// |
| 30 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 31 | /// feature.* |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 32 | #[cfg_attr(feature = "extra-traits", derive(Debug))] |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 33 | #[derive(Clone)] |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 34 | pub struct Lifetime { |
David Tolnay | 7861267 | 2018-08-31 08:47:41 -0700 | [diff] [blame] | 35 | pub apostrophe: Span, |
Alex Crichton | 131308c | 2018-05-18 14:00:24 -0700 | [diff] [blame] | 36 | pub ident: Ident, |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | impl Lifetime { |
David Tolnay | e2099f3 | 2018-03-31 18:42:43 +0200 | [diff] [blame] | 40 | pub fn new(s: &str, span: Span) -> Self { |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 41 | if !s.starts_with('\'') { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 42 | panic!( |
David Tolnay | 76178be | 2018-07-31 23:06:15 -0700 | [diff] [blame] | 43 | "lifetime name must start with apostrophe as in \"'a\", got {:?}", |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 44 | s |
| 45 | ); |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | if s == "'" { |
| 49 | panic!("lifetime name must not be empty"); |
| 50 | } |
| 51 | |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 52 | fn xid_ok(s: &str) -> bool { |
| 53 | let mut chars = s.chars(); |
| 54 | let first = chars.next().unwrap(); |
| 55 | if !(UnicodeXID::is_xid_start(first) || first == '_') { |
| 56 | return false; |
| 57 | } |
| 58 | for ch in chars { |
| 59 | if !UnicodeXID::is_xid_continue(ch) { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | true |
| 64 | } |
| 65 | |
| 66 | if !xid_ok(&s[1..]) { |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 67 | panic!("{:?} is not a valid lifetime name", s); |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | Lifetime { |
David Tolnay | 7861267 | 2018-08-31 08:47:41 -0700 | [diff] [blame] | 71 | apostrophe: Span::call_site(), |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 72 | ident: Ident::new(&s[1..], span), |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl Display for Lifetime { |
| 78 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 79 | "'".fmt(formatter)?; |
| 80 | self.ident.fmt(formatter) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | impl PartialEq for Lifetime { |
| 85 | fn eq(&self, other: &Lifetime) -> bool { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 86 | self.ident.eq(&other.ident) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | impl Eq for Lifetime {} |
| 91 | |
| 92 | impl PartialOrd for Lifetime { |
| 93 | fn partial_cmp(&self, other: &Lifetime) -> Option<Ordering> { |
| 94 | Some(self.cmp(other)) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | impl Ord for Lifetime { |
| 99 | fn cmp(&self, other: &Lifetime) -> Ordering { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 100 | self.ident.cmp(&other.ident) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | impl Hash for Lifetime { |
| 105 | fn hash<H: Hasher>(&self, h: &mut H) { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 106 | self.ident.hash(h) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | #[cfg(feature = "parsing")] |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 111 | #[doc(hidden)] |
| 112 | #[allow(non_snake_case)] |
| 113 | pub fn Lifetime(marker: lookahead::TokenMarker) -> Lifetime { |
| 114 | match marker {} |
| 115 | } |
| 116 | |
| 117 | #[cfg(feature = "parsing")] |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 118 | pub mod parsing { |
| 119 | use super::*; |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 120 | |
David Tolnay | 7861267 | 2018-08-31 08:47:41 -0700 | [diff] [blame] | 121 | use parse::{Parse, ParseStream, Result}; |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 122 | |
David Tolnay | 7744d9d | 2018-08-25 22:15:52 -0400 | [diff] [blame] | 123 | impl Parse for Lifetime { |
| 124 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 66cb0c4 | 2018-08-31 09:01:30 -0700 | [diff] [blame] | 125 | input.step(|cursor| { |
| 126 | cursor |
| 127 | .lifetime() |
| 128 | .ok_or_else(|| cursor.error("expected lifetime")) |
David Tolnay | 7744d9d | 2018-08-25 22:15:52 -0400 | [diff] [blame] | 129 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 130 | } |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | #[cfg(feature = "printing")] |
| 135 | mod printing { |
| 136 | use super::*; |
David Tolnay | 7861267 | 2018-08-31 08:47:41 -0700 | [diff] [blame] | 137 | |
| 138 | use proc_macro2::{Punct, Spacing, TokenStream}; |
| 139 | use quote::{ToTokens, TokenStreamExt}; |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 140 | |
| 141 | impl ToTokens for Lifetime { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 142 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 7861267 | 2018-08-31 08:47:41 -0700 | [diff] [blame] | 143 | let mut apostrophe = Punct::new('\'', Spacing::Joint); |
| 144 | apostrophe.set_span(self.apostrophe); |
| 145 | tokens.append(apostrophe); |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 146 | self.ident.to_tokens(tokens); |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | } |