blob: 276e2d58fbe361afd15bfed7dddccc106d0694de [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 Tolnay98942562017-12-26 21:24:35 -050013use proc_macro2::{Span, Term};
David Tolnay63e3dee2017-06-03 20:13:17 -070014use unicode_xid::UnicodeXID;
15
David Tolnay05658502018-01-07 09:56:37 -080016/// 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 Tolnay05658502018-01-07 09:56:37 -080022/// - 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 Tolnay461d98e2018-01-07 11:07:19 -080026///
27/// *This type is available if Syn is built with the `"derive"` or `"full"`
28/// feature.*
David Tolnay63e3dee2017-06-03 20:13:17 -070029#[cfg_attr(feature = "extra-traits", derive(Debug))]
David Tolnay42d0bc52018-01-09 14:36:04 -080030#[derive(Copy, Clone)]
David Tolnay63e3dee2017-06-03 20:13:17 -070031pub struct Lifetime {
David Tolnay73c98de2017-12-31 15:56:56 -050032 term: Term,
David Tolnay63e3dee2017-06-03 20:13:17 -070033}
34
35impl Lifetime {
David Tolnaye2099f32018-03-31 18:42:43 +020036 pub fn new(s: &str, span: Span) -> Self {
David Tolnay63e3dee2017-06-03 20:13:17 -070037 if !s.starts_with('\'') {
David Tolnay51382052017-12-27 13:46:21 -050038 panic!(
39 "lifetime name must start with apostrophe as in \"'a\", \
40 got {:?}",
41 s
42 );
David Tolnay63e3dee2017-06-03 20:13:17 -070043 }
44
45 if s == "'" {
46 panic!("lifetime name must not be empty");
47 }
48
David Tolnay63e3dee2017-06-03 20:13:17 -070049 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 Tolnaybb4ca9f2017-12-26 12:28:58 -050064 panic!("{:?} is not a valid lifetime name", s);
David Tolnay63e3dee2017-06-03 20:13:17 -070065 }
66
67 Lifetime {
David Tolnaye2099f32018-03-31 18:42:43 +020068 term: Term::new(s, span),
David Tolnay63e3dee2017-06-03 20:13:17 -070069 }
70 }
Alex Crichton9a4dca22018-03-28 06:32:19 -070071
72 pub fn span(&self) -> Span {
73 self.term.span()
74 }
75
76 pub fn set_span(&mut self, span: Span) {
77 self.term.set_span(span);
78 }
David Tolnay63e3dee2017-06-03 20:13:17 -070079}
80
81impl Display for Lifetime {
82 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
David Tolnay73c98de2017-12-31 15:56:56 -050083 self.term.as_str().fmt(formatter)
David Tolnay63e3dee2017-06-03 20:13:17 -070084 }
85}
86
87impl PartialEq for Lifetime {
88 fn eq(&self, other: &Lifetime) -> bool {
David Tolnay73c98de2017-12-31 15:56:56 -050089 self.term.as_str() == other.term.as_str()
David Tolnay63e3dee2017-06-03 20:13:17 -070090 }
91}
92
93impl Eq for Lifetime {}
94
95impl PartialOrd for Lifetime {
96 fn partial_cmp(&self, other: &Lifetime) -> Option<Ordering> {
97 Some(self.cmp(other))
98 }
99}
100
101impl Ord for Lifetime {
102 fn cmp(&self, other: &Lifetime) -> Ordering {
David Tolnay73c98de2017-12-31 15:56:56 -0500103 self.term.as_str().cmp(other.term.as_str())
David Tolnay63e3dee2017-06-03 20:13:17 -0700104 }
105}
106
107impl Hash for Lifetime {
108 fn hash<H: Hasher>(&self, h: &mut H) {
David Tolnay73c98de2017-12-31 15:56:56 -0500109 self.term.as_str().hash(h)
David Tolnay63e3dee2017-06-03 20:13:17 -0700110 }
111}
112
113#[cfg(feature = "parsing")]
114pub mod parsing {
115 use super::*;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500116 use synom::Synom;
David Tolnaydfc886b2018-01-06 08:03:09 -0800117 use buffer::Cursor;
David Tolnay203557a2017-12-27 23:59:33 -0500118 use parse_error;
119 use synom::PResult;
David Tolnay63e3dee2017-06-03 20:13:17 -0700120
121 impl Synom for Lifetime {
122 fn parse(input: Cursor) -> PResult<Self> {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700123 let (term, rest) = match input.term() {
David Tolnay73c98de2017-12-31 15:56:56 -0500124 Some(term) => term,
David Tolnay63e3dee2017-06-03 20:13:17 -0700125 _ => return parse_error(),
126 };
David Tolnay73c98de2017-12-31 15:56:56 -0500127 if !term.as_str().starts_with('\'') {
David Tolnay63e3dee2017-06-03 20:13:17 -0700128 return parse_error();
129 }
130
David Tolnay51382052017-12-27 13:46:21 -0500131 Ok((
David Tolnay51382052017-12-27 13:46:21 -0500132 Lifetime {
David Tolnay73c98de2017-12-31 15:56:56 -0500133 term: term,
David Tolnay51382052017-12-27 13:46:21 -0500134 },
David Tolnayf4aa6b42017-12-31 16:40:33 -0500135 rest,
David Tolnay51382052017-12-27 13:46:21 -0500136 ))
David Tolnay63e3dee2017-06-03 20:13:17 -0700137 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800138
139 fn description() -> Option<&'static str> {
140 Some("lifetime")
141 }
David Tolnay63e3dee2017-06-03 20:13:17 -0700142 }
143}
144
145#[cfg(feature = "printing")]
146mod printing {
147 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500148 use quote::{ToTokens, Tokens};
David Tolnay63e3dee2017-06-03 20:13:17 -0700149
150 impl ToTokens for Lifetime {
151 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700152 self.term.to_tokens(tokens);
David Tolnay63e3dee2017-06-03 20:13:17 -0700153 }
154 }
155}