Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1 | use std::fmt; |
| 2 | use std::hash::{Hash, Hasher}; |
| 3 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 4 | use proc_macro2::{self, Literal, Span, Term, TokenNode, TokenTree}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 5 | |
| 6 | #[derive(Clone)] |
| 7 | pub struct Lit { |
| 8 | pub value: LitKind, |
| 9 | pub span: Span, |
| 10 | } |
| 11 | |
| 12 | #[derive(Clone)] |
| 13 | pub enum LitKind { |
| 14 | Bool(bool), |
| 15 | Other(Literal), |
| 16 | } |
| 17 | |
| 18 | impl Lit { |
| 19 | pub fn into_token_tree(self) -> TokenTree { |
| 20 | let kind = match self.value { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 21 | LitKind::Bool(true) => TokenNode::Term(Term::intern("true")), |
| 22 | LitKind::Bool(false) => TokenNode::Term(Term::intern("false")), |
| 23 | LitKind::Other(l) => TokenNode::Literal(l), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 24 | }; |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 25 | proc_macro2::TokenTree { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 26 | span: self.span, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 27 | kind: kind, |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 28 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 29 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 32 | impl fmt::Display for Lit { |
| 33 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 34 | fmt::Display::fmt(&self.value, f) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 35 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 38 | impl fmt::Debug for Lit { |
| 39 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 40 | fmt::Display::fmt(&self.value, f) |
Pascal Hertleif | 36342c5 | 2016-10-19 10:31:42 +0200 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 44 | impl PartialEq for Lit { |
| 45 | fn eq(&self, other: &Lit) -> bool { |
| 46 | self.value == other.value |
Pascal Hertleif | 36342c5 | 2016-10-19 10:31:42 +0200 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 50 | impl Eq for Lit {} |
| 51 | |
| 52 | impl Hash for Lit { |
| 53 | fn hash<H: Hasher>(&self, hasher: &mut H) { |
| 54 | self.value.hash(hasher) |
Pascal Hertleif | 36342c5 | 2016-10-19 10:31:42 +0200 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 58 | impl fmt::Display for LitKind { |
| 59 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 60 | match *self { |
| 61 | LitKind::Bool(b) => b.fmt(f), |
| 62 | LitKind::Other(ref l) => l.fmt(f), |
| 63 | } |
Pascal Hertleif | 36342c5 | 2016-10-19 10:31:42 +0200 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 67 | impl fmt::Debug for LitKind { |
| 68 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 69 | match *self { |
| 70 | LitKind::Bool(b) => b.fmt(f), |
| 71 | LitKind::Other(ref l) => fmt::Display::fmt(l, f), |
| 72 | } |
Pascal Hertleif | 36342c5 | 2016-10-19 10:31:42 +0200 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 76 | impl PartialEq for LitKind { |
| 77 | fn eq(&self, other: &LitKind) -> bool { |
| 78 | match (self, other) { |
| 79 | (&LitKind::Bool(b1), &LitKind::Bool(b2)) => b1 == b2, |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 80 | (&LitKind::Other(ref l1), &LitKind::Other(ref l2)) => l1.to_string() == l2.to_string(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 81 | _ => false, |
| 82 | } |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 83 | } |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 86 | impl Eq for LitKind {} |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 87 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 88 | impl Hash for LitKind { |
| 89 | fn hash<H: Hasher>(&self, hasher: &mut H) { |
| 90 | match *self { |
| 91 | LitKind::Bool(b) => (0u8, b).hash(hasher), |
| 92 | LitKind::Other(ref l) => (1u8, l.to_string()).hash(hasher), |
| 93 | } |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 94 | } |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | #[cfg(feature = "parsing")] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 98 | pub mod parsing { |
| 99 | use super::*; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 100 | use synom::Synom; |
| 101 | use cursor::Cursor; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 102 | use parse_error; |
| 103 | use synom::PResult; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 104 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 105 | impl Synom for Lit { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 106 | fn parse(input: Cursor) -> PResult<Self> { |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 107 | match input.literal() { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 108 | Some((rest, span, lit)) => Ok(( |
| 109 | rest, |
| 110 | Lit { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 111 | span: span, |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 112 | value: LitKind::Other(lit), |
| 113 | }, |
| 114 | )), |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 115 | _ => match input.word() { |
| 116 | Some((rest, span, sym)) => { |
| 117 | let kind = if sym.as_str() == "true" { |
| 118 | LitKind::Bool(true) |
| 119 | } else if sym.as_str() == "false" { |
| 120 | LitKind::Bool(false) |
| 121 | } else { |
| 122 | return parse_error(); |
| 123 | }; |
| 124 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 125 | Ok(( |
| 126 | rest, |
| 127 | Lit { |
| 128 | span: span, |
| 129 | value: kind, |
| 130 | }, |
| 131 | )) |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 132 | } |
| 133 | _ => parse_error(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 134 | }, |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 135 | } |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -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("literal") |
| 140 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -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}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 148 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 149 | impl ToTokens for Lit { |
| 150 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 151 | self.clone().into_token_tree().to_tokens(tokens) |
David Tolnay | f17fd2f | 2016-10-07 23:38:08 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 154 | } |