| 1 |
// Copyright (C) 2023-2024 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU Proc Macro Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// Under Section 7 of GPL version 3, you are granted additional |
| 15 |
// permissions described in the GCC Runtime Library Exception, version |
| 16 |
// 3.1, as published by the Free Software Foundation. |
| 17 |
|
| 18 |
// You should have received a copy of the GNU General Public License and |
| 19 |
// a copy of the GCC Runtime Library Exception along with this program; |
| 20 |
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 21 |
// <http://www.gnu.org/licenses/>. |
| 22 |
|
| 23 |
use bridge; |
| 24 |
use std::fmt; |
| 25 |
use std::str::FromStr; |
| 26 |
use LexError; |
| 27 |
use Span; |
| 28 |
|
| 29 |
/// A type representing a literal value except `true` and `false`. |
| 30 |
/// |
| 31 |
/// This could be one of the following: |
| 32 |
/// * literal string (`"hello"`) |
| 33 |
/// * byte string (`b"hello"`) |
| 34 |
/// * character (`'a'`) |
| 35 |
/// * byte character (`b'a'`) |
| 36 |
/// * unsuffixed integer (`42`) |
| 37 |
/// * suffixed integer (`42u8`) |
| 38 |
/// * unsuffixed floating point number (`1.618`) |
| 39 |
/// * suffixed floating point number (`1.618f32`) |
| 40 |
/// |
| 41 |
/// # Note |
| 42 |
/// |
| 43 |
/// Boolean literals like `true` and `false` are `Ident`s and do not belong |
| 44 |
/// here. |
| 45 |
#[derive(Clone)] |
| 46 |
pub struct Literal(pub(crate) bridge::literal::Literal); |
| 47 |
|
| 48 |
impl Literal { |
| 49 |
// TODO: Add experimental API functions for this type |
| 50 |
// TODO: Generate those constructor with 1/2 macros instead |
| 51 |
|
| 52 |
pub fn u8_suffixed(n: u8) -> Self { |
| 53 |
Literal(bridge::literal::Literal::u8_suffixed(n)) |
| 54 |
} |
| 55 |
|
| 56 |
pub fn u16_suffixed(n: u16) -> Self { |
| 57 |
Literal(bridge::literal::Literal::u16_suffixed(n)) |
| 58 |
} |
| 59 |
|
| 60 |
pub fn u32_suffixed(n: u32) -> Self { |
| 61 |
Literal(bridge::literal::Literal::u32_suffixed(n)) |
| 62 |
} |
| 63 |
|
| 64 |
pub fn u64_suffixed(n: u64) -> Self { |
| 65 |
Literal(bridge::literal::Literal::u64_suffixed(n)) |
| 66 |
} |
| 67 |
|
| 68 |
pub fn u128_suffixed(n: u128) -> Self { |
| 69 |
Literal(bridge::literal::Literal::u128_suffixed(n)) |
| 70 |
} |
| 71 |
|
| 72 |
pub fn usize_suffixed(n: usize) -> Self { |
| 73 |
Literal(bridge::literal::Literal::usize_suffixed(n)) |
| 74 |
} |
| 75 |
|
| 76 |
pub fn i8_suffixed(n: i8) -> Self { |
| 77 |
Literal(bridge::literal::Literal::i8_suffixed(n)) |
| 78 |
} |
| 79 |
|
| 80 |
pub fn i16_suffixed(n: i16) -> Self { |
| 81 |
Literal(bridge::literal::Literal::i16_suffixed(n)) |
| 82 |
} |
| 83 |
|
| 84 |
pub fn i32_suffixed(n: i32) -> Self { |
| 85 |
Literal(bridge::literal::Literal::i32_suffixed(n)) |
| 86 |
} |
| 87 |
|
| 88 |
pub fn i64_suffixed(n: i64) -> Self { |
| 89 |
Literal(bridge::literal::Literal::i64_suffixed(n)) |
| 90 |
} |
| 91 |
|
| 92 |
pub fn i128_suffixed(n: i128) -> Self { |
| 93 |
Literal(bridge::literal::Literal::i128_suffixed(n)) |
| 94 |
} |
| 95 |
|
| 96 |
pub fn isize_suffixed(n: isize) -> Self { |
| 97 |
Literal(bridge::literal::Literal::isize_suffixed(n)) |
| 98 |
} |
| 99 |
|
| 100 |
// Unsuffixed |
| 101 |
|
| 102 |
pub fn u8_unsuffixed(n: u8) -> Self { |
| 103 |
Literal(bridge::literal::Literal::u8_unsuffixed(n)) |
| 104 |
} |
| 105 |
|
| 106 |
pub fn u16_unsuffixed(n: u16) -> Self { |
| 107 |
Literal(bridge::literal::Literal::u16_unsuffixed(n)) |
| 108 |
} |
| 109 |
|
| 110 |
pub fn u32_unsuffixed(n: u32) -> Self { |
| 111 |
Literal(bridge::literal::Literal::u32_unsuffixed(n)) |
| 112 |
} |
| 113 |
|
| 114 |
pub fn u64_unsuffixed(n: u64) -> Self { |
| 115 |
Literal(bridge::literal::Literal::u64_unsuffixed(n)) |
| 116 |
} |
| 117 |
|
| 118 |
pub fn u128_unsuffixed(n: u128) -> Self { |
| 119 |
Literal(bridge::literal::Literal::u128_unsuffixed(n)) |
| 120 |
} |
| 121 |
|
| 122 |
pub fn usize_unsuffixed(n: usize) -> Self { |
| 123 |
Literal(bridge::literal::Literal::usize_unsuffixed(n)) |
| 124 |
} |
| 125 |
|
| 126 |
pub fn i8_unsuffixed(n: i8) -> Self { |
| 127 |
Literal(bridge::literal::Literal::i8_unsuffixed(n)) |
| 128 |
} |
| 129 |
|
| 130 |
pub fn i16_unsuffixed(n: i16) -> Self { |
| 131 |
Literal(bridge::literal::Literal::i16_unsuffixed(n)) |
| 132 |
} |
| 133 |
|
| 134 |
pub fn i32_unsuffixed(n: i32) -> Self { |
| 135 |
Literal(bridge::literal::Literal::i32_unsuffixed(n)) |
| 136 |
} |
| 137 |
|
| 138 |
pub fn i64_unsuffixed(n: i64) -> Self { |
| 139 |
Literal(bridge::literal::Literal::i64_unsuffixed(n)) |
| 140 |
} |
| 141 |
|
| 142 |
pub fn i128_unsuffixed(n: i128) -> Self { |
| 143 |
Literal(bridge::literal::Literal::i128_unsuffixed(n)) |
| 144 |
} |
| 145 |
|
| 146 |
pub fn isize_unsuffixed(n: isize) -> Self { |
| 147 |
Literal(bridge::literal::Literal::isize_unsuffixed(n)) |
| 148 |
} |
| 149 |
|
| 150 |
pub fn f32_unsuffixed(n: f32) -> Self { |
| 151 |
Literal(bridge::literal::Literal::f32_unsuffixed(n)) |
| 152 |
} |
| 153 |
|
| 154 |
pub fn f32_suffixed(n: f32) -> Self { |
| 155 |
Literal(bridge::literal::Literal::f32_suffixed(n)) |
| 156 |
} |
| 157 |
|
| 158 |
pub fn f64_unsuffixed(n: f64) -> Self { |
| 159 |
Literal(bridge::literal::Literal::f64_unsuffixed(n)) |
| 160 |
} |
| 161 |
|
| 162 |
pub fn f64_suffixed(n: f64) -> Self { |
| 163 |
Literal(bridge::literal::Literal::f64_suffixed(n)) |
| 164 |
} |
| 165 |
|
| 166 |
pub fn string(string: &str) -> Self { |
| 167 |
Literal(bridge::literal::Literal::string(string)) |
| 168 |
} |
| 169 |
|
| 170 |
pub fn character(c: char) -> Self { |
| 171 |
Literal(bridge::literal::Literal::character(c)) |
| 172 |
} |
| 173 |
|
| 174 |
pub fn byte_string(bytes: &[u8]) -> Self { |
| 175 |
Literal(bridge::literal::Literal::byte_string(bytes)) |
| 176 |
} |
| 177 |
|
| 178 |
/// Get the [`Span`] for this literal. |
| 179 |
pub fn span(&self) -> Span { |
| 180 |
Span(self.0.span()) |
| 181 |
} |
| 182 |
|
| 183 |
/// Set the span for this literal. |
| 184 |
/// |
| 185 |
/// # Arguments |
| 186 |
/// |
| 187 |
/// * `span` - The new span value. |
| 188 |
pub fn set_span(&mut self, span: Span) { |
| 189 |
self.0.set_span(span.0); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
impl fmt::Debug for Literal { |
| 194 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 195 |
self.0.fmt(f) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
impl fmt::Display for Literal { |
| 200 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 201 |
self.0.fmt(f) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
impl FromStr for Literal { |
| 206 |
type Err = LexError; |
| 207 |
|
| 208 |
fn from_str(src: &str) -> Result<Self, LexError> { |
| 209 |
bridge::literal::Literal::from_str(src).map(Literal) |
| 210 |
} |
| 211 |
} |