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 | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 27 | /// |
| 28 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 29 | /// feature.* |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 30 | #[cfg_attr(feature = "extra-traits", derive(Debug))] |
| 31 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
| 32 | pub struct Lifetime { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 33 | term: Term, |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 34 | pub span: Span, |
| 35 | } |
| 36 | |
| 37 | impl Lifetime { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 38 | pub fn new(term: Term, span: Span) -> Self { |
| 39 | let s = term.as_str(); |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 40 | |
| 41 | if !s.starts_with('\'') { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 42 | panic!( |
| 43 | "lifetime name must start with apostrophe as in \"'a\", \ |
| 44 | got {:?}", |
| 45 | s |
| 46 | ); |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 47 | } |
| 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 Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 72 | panic!("{:?} is not a valid lifetime name", s); |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | Lifetime { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 76 | term: term, |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 77 | span: span, |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl Display for Lifetime { |
| 83 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 84 | self.term.as_str().fmt(formatter) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | impl PartialEq for Lifetime { |
| 89 | fn eq(&self, other: &Lifetime) -> bool { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 90 | self.term.as_str() == other.term.as_str() |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | impl Eq for Lifetime {} |
| 95 | |
| 96 | impl PartialOrd for Lifetime { |
| 97 | fn partial_cmp(&self, other: &Lifetime) -> Option<Ordering> { |
| 98 | Some(self.cmp(other)) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl Ord for Lifetime { |
| 103 | fn cmp(&self, other: &Lifetime) -> Ordering { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 104 | self.term.as_str().cmp(other.term.as_str()) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | impl Hash for Lifetime { |
| 109 | fn hash<H: Hasher>(&self, h: &mut H) { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 110 | self.term.as_str().hash(h) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | #[cfg(feature = "parsing")] |
| 115 | pub mod parsing { |
| 116 | use super::*; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 117 | use synom::Synom; |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 118 | use buffer::Cursor; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 119 | use parse_error; |
| 120 | use synom::PResult; |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 121 | |
| 122 | impl Synom for Lifetime { |
| 123 | fn parse(input: Cursor) -> PResult<Self> { |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 124 | let (span, term, rest) = match input.term() { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 125 | Some(term) => term, |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 126 | _ => return parse_error(), |
| 127 | }; |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 128 | if !term.as_str().starts_with('\'') { |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 129 | return parse_error(); |
| 130 | } |
| 131 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 132 | Ok(( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 133 | Lifetime { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 134 | term: term, |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 135 | span: span, |
| 136 | }, |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 137 | rest, |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 138 | )) |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 139 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 140 | |
| 141 | fn description() -> Option<&'static str> { |
| 142 | Some("lifetime") |
| 143 | } |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | #[cfg(feature = "printing")] |
| 148 | mod printing { |
| 149 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 150 | use quote::{ToTokens, Tokens}; |
| 151 | use proc_macro2::{TokenNode, TokenTree}; |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 152 | |
| 153 | impl ToTokens for Lifetime { |
| 154 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 155 | tokens.append(TokenTree { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 156 | span: self.span, |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 157 | kind: TokenNode::Term(self.term), |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 158 | }) |
| 159 | } |
| 160 | } |
| 161 | } |