blob: e192c5c599215b0797ac3f0543f3c4f2c85a85e1 [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: `'`.
22/// - Must not consist of apostrophe + underscore: `'_`.
23/// - Character after the apostrophe must be `_` or a Unicode code point with
24/// the XID_Start property.
25/// - All following characters must be Unicode code points with the XID_Continue
26/// property.
David Tolnay461d98e2018-01-07 11:07:19 -080027///
28/// *This type is available if Syn is built with the `"derive"` or `"full"`
29/// feature.*
David Tolnay63e3dee2017-06-03 20:13:17 -070030#[cfg_attr(feature = "extra-traits", derive(Debug))]
31#[cfg_attr(feature = "clone-impls", derive(Clone))]
32pub struct Lifetime {
David Tolnay73c98de2017-12-31 15:56:56 -050033 term: Term,
David Tolnay63e3dee2017-06-03 20:13:17 -070034 pub span: Span,
35}
36
37impl Lifetime {
David Tolnay73c98de2017-12-31 15:56:56 -050038 pub fn new(term: Term, span: Span) -> Self {
39 let s = term.as_str();
David Tolnay63e3dee2017-06-03 20:13:17 -070040
41 if !s.starts_with('\'') {
David Tolnay51382052017-12-27 13:46:21 -050042 panic!(
43 "lifetime name must start with apostrophe as in \"'a\", \
44 got {:?}",
45 s
46 );
David Tolnay63e3dee2017-06-03 20:13:17 -070047 }
48
49 if s == "'" {
50 panic!("lifetime name must not be empty");
51 }
52
53 if s == "'_" {
54 panic!("\"'_\" is not a valid lifetime name");
55 }
56
57 fn xid_ok(s: &str) -> bool {
58 let mut chars = s.chars();
59 let first = chars.next().unwrap();
60 if !(UnicodeXID::is_xid_start(first) || first == '_') {
61 return false;
62 }
63 for ch in chars {
64 if !UnicodeXID::is_xid_continue(ch) {
65 return false;
66 }
67 }
68 true
69 }
70
71 if !xid_ok(&s[1..]) {
David Tolnaybb4ca9f2017-12-26 12:28:58 -050072 panic!("{:?} is not a valid lifetime name", s);
David Tolnay63e3dee2017-06-03 20:13:17 -070073 }
74
75 Lifetime {
David Tolnay73c98de2017-12-31 15:56:56 -050076 term: term,
David Tolnay63e3dee2017-06-03 20:13:17 -070077 span: span,
78 }
79 }
80}
81
82impl Display for Lifetime {
83 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
David Tolnay73c98de2017-12-31 15:56:56 -050084 self.term.as_str().fmt(formatter)
David Tolnay63e3dee2017-06-03 20:13:17 -070085 }
86}
87
88impl PartialEq for Lifetime {
89 fn eq(&self, other: &Lifetime) -> bool {
David Tolnay73c98de2017-12-31 15:56:56 -050090 self.term.as_str() == other.term.as_str()
David Tolnay63e3dee2017-06-03 20:13:17 -070091 }
92}
93
94impl Eq for Lifetime {}
95
96impl PartialOrd for Lifetime {
97 fn partial_cmp(&self, other: &Lifetime) -> Option<Ordering> {
98 Some(self.cmp(other))
99 }
100}
101
102impl Ord for Lifetime {
103 fn cmp(&self, other: &Lifetime) -> Ordering {
David Tolnay73c98de2017-12-31 15:56:56 -0500104 self.term.as_str().cmp(other.term.as_str())
David Tolnay63e3dee2017-06-03 20:13:17 -0700105 }
106}
107
108impl Hash for Lifetime {
109 fn hash<H: Hasher>(&self, h: &mut H) {
David Tolnay73c98de2017-12-31 15:56:56 -0500110 self.term.as_str().hash(h)
David Tolnay63e3dee2017-06-03 20:13:17 -0700111 }
112}
113
114#[cfg(feature = "parsing")]
115pub mod parsing {
116 use super::*;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500117 use synom::Synom;
David Tolnaydfc886b2018-01-06 08:03:09 -0800118 use buffer::Cursor;
David Tolnay203557a2017-12-27 23:59:33 -0500119 use parse_error;
120 use synom::PResult;
David Tolnay63e3dee2017-06-03 20:13:17 -0700121
122 impl Synom for Lifetime {
123 fn parse(input: Cursor) -> PResult<Self> {
David Tolnay65729482017-12-31 16:14:50 -0500124 let (span, term, rest) = match input.term() {
David Tolnay73c98de2017-12-31 15:56:56 -0500125 Some(term) => term,
David Tolnay63e3dee2017-06-03 20:13:17 -0700126 _ => return parse_error(),
127 };
David Tolnay73c98de2017-12-31 15:56:56 -0500128 if !term.as_str().starts_with('\'') {
David Tolnay63e3dee2017-06-03 20:13:17 -0700129 return parse_error();
130 }
131
David Tolnay51382052017-12-27 13:46:21 -0500132 Ok((
David Tolnay51382052017-12-27 13:46:21 -0500133 Lifetime {
David Tolnay73c98de2017-12-31 15:56:56 -0500134 term: term,
David Tolnay51382052017-12-27 13:46:21 -0500135 span: span,
136 },
David Tolnayf4aa6b42017-12-31 16:40:33 -0500137 rest,
David Tolnay51382052017-12-27 13:46:21 -0500138 ))
David Tolnay63e3dee2017-06-03 20:13:17 -0700139 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800140
141 fn description() -> Option<&'static str> {
142 Some("lifetime")
143 }
David Tolnay63e3dee2017-06-03 20:13:17 -0700144 }
145}
146
147#[cfg(feature = "printing")]
148mod printing {
149 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500150 use quote::{ToTokens, Tokens};
151 use proc_macro2::{TokenNode, TokenTree};
David Tolnay63e3dee2017-06-03 20:13:17 -0700152
153 impl ToTokens for Lifetime {
154 fn to_tokens(&self, tokens: &mut Tokens) {
155 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500156 span: self.span,
David Tolnay73c98de2017-12-31 15:56:56 -0500157 kind: TokenNode::Term(self.term),
David Tolnay63e3dee2017-06-03 20:13:17 -0700158 })
159 }
160 }
161}