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