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