blob: 6f00b563894d89090683de56dbeebce8e13f95fc [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// 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 Tolnay63e3dee2017-06-03 20:13:17 -07009use std::cmp::Ordering;
10use std::fmt::{self, Display};
11use std::hash::{Hash, Hasher};
12
David Tolnay65fb5662018-05-20 20:02:28 -070013use proc_macro2::{Ident, Span};
David Tolnay63e3dee2017-06-03 20:13:17 -070014use unicode_xid::UnicodeXID;
15
David Tolnay4fb71232018-08-25 23:14:50 -040016#[cfg(feature = "parsing")]
17use lookahead;
Alex Crichton131308c2018-05-18 14:00:24 -070018
David Tolnay05658502018-01-07 09:56:37 -080019/// 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 Tolnay05658502018-01-07 09:56:37 -080025/// - 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 Tolnay461d98e2018-01-07 11:07:19 -080029///
30/// *This type is available if Syn is built with the `"derive"` or `"full"`
31/// feature.*
David Tolnay63e3dee2017-06-03 20:13:17 -070032#[cfg_attr(feature = "extra-traits", derive(Debug))]
Alex Crichtona74a1c82018-05-16 10:20:44 -070033#[derive(Clone)]
David Tolnay63e3dee2017-06-03 20:13:17 -070034pub struct Lifetime {
David Tolnay78612672018-08-31 08:47:41 -070035 pub apostrophe: Span,
Alex Crichton131308c2018-05-18 14:00:24 -070036 pub ident: Ident,
David Tolnay63e3dee2017-06-03 20:13:17 -070037}
38
39impl Lifetime {
David Tolnaye2099f32018-03-31 18:42:43 +020040 pub fn new(s: &str, span: Span) -> Self {
David Tolnay63e3dee2017-06-03 20:13:17 -070041 if !s.starts_with('\'') {
David Tolnay51382052017-12-27 13:46:21 -050042 panic!(
David Tolnay76178be2018-07-31 23:06:15 -070043 "lifetime name must start with apostrophe as in \"'a\", got {:?}",
David Tolnay51382052017-12-27 13:46:21 -050044 s
45 );
David Tolnay63e3dee2017-06-03 20:13:17 -070046 }
47
48 if s == "'" {
49 panic!("lifetime name must not be empty");
50 }
51
David Tolnay63e3dee2017-06-03 20:13:17 -070052 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 Tolnaybb4ca9f2017-12-26 12:28:58 -050067 panic!("{:?} is not a valid lifetime name", s);
David Tolnay63e3dee2017-06-03 20:13:17 -070068 }
69
70 Lifetime {
David Tolnaya19a8192018-09-01 02:36:16 -070071 apostrophe: span,
Alex Crichtona74a1c82018-05-16 10:20:44 -070072 ident: Ident::new(&s[1..], span),
David Tolnay63e3dee2017-06-03 20:13:17 -070073 }
74 }
75}
76
77impl Display for Lifetime {
78 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtona74a1c82018-05-16 10:20:44 -070079 "'".fmt(formatter)?;
80 self.ident.fmt(formatter)
David Tolnay63e3dee2017-06-03 20:13:17 -070081 }
82}
83
84impl PartialEq for Lifetime {
85 fn eq(&self, other: &Lifetime) -> bool {
Alex Crichtona74a1c82018-05-16 10:20:44 -070086 self.ident.eq(&other.ident)
David Tolnay63e3dee2017-06-03 20:13:17 -070087 }
88}
89
90impl Eq for Lifetime {}
91
92impl PartialOrd for Lifetime {
93 fn partial_cmp(&self, other: &Lifetime) -> Option<Ordering> {
94 Some(self.cmp(other))
95 }
96}
97
98impl Ord for Lifetime {
99 fn cmp(&self, other: &Lifetime) -> Ordering {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700100 self.ident.cmp(&other.ident)
David Tolnay63e3dee2017-06-03 20:13:17 -0700101 }
102}
103
104impl Hash for Lifetime {
105 fn hash<H: Hasher>(&self, h: &mut H) {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700106 self.ident.hash(h)
David Tolnay63e3dee2017-06-03 20:13:17 -0700107 }
108}
109
110#[cfg(feature = "parsing")]
David Tolnay4fb71232018-08-25 23:14:50 -0400111#[doc(hidden)]
112#[allow(non_snake_case)]
113pub fn Lifetime(marker: lookahead::TokenMarker) -> Lifetime {
114 match marker {}
115}
116
117#[cfg(feature = "parsing")]
David Tolnay63e3dee2017-06-03 20:13:17 -0700118pub mod parsing {
119 use super::*;
David Tolnay94d304f2018-08-30 23:43:53 -0700120
David Tolnay78612672018-08-31 08:47:41 -0700121 use parse::{Parse, ParseStream, Result};
David Tolnay63e3dee2017-06-03 20:13:17 -0700122
David Tolnay7744d9d2018-08-25 22:15:52 -0400123 impl Parse for Lifetime {
124 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay66cb0c42018-08-31 09:01:30 -0700125 input.step(|cursor| {
126 cursor
127 .lifetime()
128 .ok_or_else(|| cursor.error("expected lifetime"))
David Tolnay7744d9d2018-08-25 22:15:52 -0400129 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800130 }
David Tolnay63e3dee2017-06-03 20:13:17 -0700131 }
132}
133
134#[cfg(feature = "printing")]
135mod printing {
136 use super::*;
David Tolnay78612672018-08-31 08:47:41 -0700137
138 use proc_macro2::{Punct, Spacing, TokenStream};
139 use quote::{ToTokens, TokenStreamExt};
David Tolnay63e3dee2017-06-03 20:13:17 -0700140
141 impl ToTokens for Lifetime {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700142 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay78612672018-08-31 08:47:41 -0700143 let mut apostrophe = Punct::new('\'', Spacing::Joint);
144 apostrophe.set_span(self.apostrophe);
145 tokens.append(apostrophe);
Alex Crichtona74a1c82018-05-16 10:20:44 -0700146 self.ident.to_tokens(tokens);
David Tolnay63e3dee2017-06-03 20:13:17 -0700147 }
148 }
149}