blob: 40c1e2019ff8fd87bed971ef9bb84ed3eee36b13 [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 pub span: Span,
34}
35
36impl Lifetime {
David Tolnay73c98de2017-12-31 15:56:56 -050037 pub fn new(term: Term, span: Span) -> Self {
38 let s = term.as_str();
David Tolnay63e3dee2017-06-03 20:13:17 -070039
40 if !s.starts_with('\'') {
David Tolnay51382052017-12-27 13:46:21 -050041 panic!(
42 "lifetime name must start with apostrophe as in \"'a\", \
43 got {:?}",
44 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 Tolnay73c98de2017-12-31 15:56:56 -050071 term: term,
David Tolnay63e3dee2017-06-03 20:13:17 -070072 span: span,
73 }
74 }
75}
76
77impl Display for Lifetime {
78 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
David Tolnay73c98de2017-12-31 15:56:56 -050079 self.term.as_str().fmt(formatter)
David Tolnay63e3dee2017-06-03 20:13:17 -070080 }
81}
82
83impl PartialEq for Lifetime {
84 fn eq(&self, other: &Lifetime) -> bool {
David Tolnay73c98de2017-12-31 15:56:56 -050085 self.term.as_str() == other.term.as_str()
David Tolnay63e3dee2017-06-03 20:13:17 -070086 }
87}
88
89impl Eq for Lifetime {}
90
91impl PartialOrd for Lifetime {
92 fn partial_cmp(&self, other: &Lifetime) -> Option<Ordering> {
93 Some(self.cmp(other))
94 }
95}
96
97impl Ord for Lifetime {
98 fn cmp(&self, other: &Lifetime) -> Ordering {
David Tolnay73c98de2017-12-31 15:56:56 -050099 self.term.as_str().cmp(other.term.as_str())
David Tolnay63e3dee2017-06-03 20:13:17 -0700100 }
101}
102
103impl Hash for Lifetime {
104 fn hash<H: Hasher>(&self, h: &mut H) {
David Tolnay73c98de2017-12-31 15:56:56 -0500105 self.term.as_str().hash(h)
David Tolnay63e3dee2017-06-03 20:13:17 -0700106 }
107}
108
109#[cfg(feature = "parsing")]
110pub mod parsing {
111 use super::*;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500112 use synom::Synom;
David Tolnaydfc886b2018-01-06 08:03:09 -0800113 use buffer::Cursor;
David Tolnay203557a2017-12-27 23:59:33 -0500114 use parse_error;
115 use synom::PResult;
David Tolnay63e3dee2017-06-03 20:13:17 -0700116
117 impl Synom for Lifetime {
118 fn parse(input: Cursor) -> PResult<Self> {
David Tolnay65729482017-12-31 16:14:50 -0500119 let (span, term, rest) = match input.term() {
David Tolnay73c98de2017-12-31 15:56:56 -0500120 Some(term) => term,
David Tolnay63e3dee2017-06-03 20:13:17 -0700121 _ => return parse_error(),
122 };
David Tolnay73c98de2017-12-31 15:56:56 -0500123 if !term.as_str().starts_with('\'') {
David Tolnay63e3dee2017-06-03 20:13:17 -0700124 return parse_error();
125 }
126
David Tolnay51382052017-12-27 13:46:21 -0500127 Ok((
David Tolnay51382052017-12-27 13:46:21 -0500128 Lifetime {
David Tolnay73c98de2017-12-31 15:56:56 -0500129 term: term,
David Tolnay51382052017-12-27 13:46:21 -0500130 span: span,
131 },
David Tolnayf4aa6b42017-12-31 16:40:33 -0500132 rest,
David Tolnay51382052017-12-27 13:46:21 -0500133 ))
David Tolnay63e3dee2017-06-03 20:13:17 -0700134 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800135
136 fn description() -> Option<&'static str> {
137 Some("lifetime")
138 }
David Tolnay63e3dee2017-06-03 20:13:17 -0700139 }
140}
141
142#[cfg(feature = "printing")]
143mod printing {
144 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500145 use quote::{ToTokens, Tokens};
146 use proc_macro2::{TokenNode, TokenTree};
David Tolnay63e3dee2017-06-03 20:13:17 -0700147
148 impl ToTokens for Lifetime {
149 fn to_tokens(&self, tokens: &mut Tokens) {
150 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500151 span: self.span,
David Tolnay73c98de2017-12-31 15:56:56 -0500152 kind: TokenNode::Term(self.term),
David Tolnay63e3dee2017-06-03 20:13:17 -0700153 })
154 }
155 }
156}