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 | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 13 | use proc_macro2::{Span, Term}; |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 14 | use unicode_xid::UnicodeXID; |
| 15 | |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 16 | /// 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 Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 27 | #[cfg_attr(feature = "extra-traits", derive(Debug))] |
| 28 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
| 29 | pub struct Lifetime { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 30 | term: Term, |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 31 | pub span: Span, |
| 32 | } |
| 33 | |
| 34 | impl Lifetime { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 35 | pub fn new(term: Term, span: Span) -> Self { |
| 36 | let s = term.as_str(); |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 37 | |
| 38 | if !s.starts_with('\'') { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 39 | panic!( |
| 40 | "lifetime name must start with apostrophe as in \"'a\", \ |
| 41 | got {:?}", |
| 42 | s |
| 43 | ); |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | if s == "'" { |
| 47 | panic!("lifetime name must not be empty"); |
| 48 | } |
| 49 | |
| 50 | if s == "'_" { |
| 51 | panic!("\"'_\" is not a valid lifetime name"); |
| 52 | } |
| 53 | |
| 54 | fn xid_ok(s: &str) -> bool { |
| 55 | let mut chars = s.chars(); |
| 56 | let first = chars.next().unwrap(); |
| 57 | if !(UnicodeXID::is_xid_start(first) || first == '_') { |
| 58 | return false; |
| 59 | } |
| 60 | for ch in chars { |
| 61 | if !UnicodeXID::is_xid_continue(ch) { |
| 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | true |
| 66 | } |
| 67 | |
| 68 | if !xid_ok(&s[1..]) { |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 69 | panic!("{:?} is not a valid lifetime name", s); |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | Lifetime { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 73 | term: term, |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 74 | span: span, |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl Display for Lifetime { |
| 80 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 81 | self.term.as_str().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 { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 87 | self.term.as_str() == other.term.as_str() |
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 { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 101 | self.term.as_str().cmp(other.term.as_str()) |
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) { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 107 | self.term.as_str().hash(h) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | #[cfg(feature = "parsing")] |
| 112 | pub mod parsing { |
| 113 | use super::*; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 114 | use synom::Synom; |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 115 | use buffer::Cursor; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 116 | use parse_error; |
| 117 | use synom::PResult; |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 118 | |
| 119 | impl Synom for Lifetime { |
| 120 | fn parse(input: Cursor) -> PResult<Self> { |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 121 | let (span, term, rest) = match input.term() { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 122 | Some(term) => term, |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 123 | _ => return parse_error(), |
| 124 | }; |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 125 | if !term.as_str().starts_with('\'') { |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 126 | return parse_error(); |
| 127 | } |
| 128 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 129 | Ok(( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 130 | Lifetime { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 131 | term: term, |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 132 | span: span, |
| 133 | }, |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 134 | rest, |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 135 | )) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 136 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 137 | |
| 138 | fn description() -> Option<&'static str> { |
| 139 | Some("lifetime") |
| 140 | } |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | #[cfg(feature = "printing")] |
| 145 | mod printing { |
| 146 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 147 | use quote::{ToTokens, Tokens}; |
| 148 | use proc_macro2::{TokenNode, TokenTree}; |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 149 | |
| 150 | impl ToTokens for Lifetime { |
| 151 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 152 | tokens.append(TokenTree { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 153 | span: self.span, |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 154 | kind: TokenNode::Term(self.term), |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 155 | }) |
| 156 | } |
| 157 | } |
| 158 | } |