pub enum Literal<B: Buffer> {
Bool(BoolLit),
Integer(IntegerLit<B>),
Float(FloatLit<B>),
Char(CharLit<B>),
String(StringLit<B>),
Byte(ByteLit<B>),
ByteString(ByteStringLit<B>),
}
Expand description
A literal. This is the main type of this library.
This type is generic over the underlying buffer B
, which can be &str
or
String
.
To create this type, you have to either call Literal::parse
with an
input string or use the From<_>
impls of this type. The impls are only
available of the corresponding crate features are enabled (they are enabled
by default).
Variants§
Bool(BoolLit)
Integer(IntegerLit<B>)
Float(FloatLit<B>)
Char(CharLit<B>)
String(StringLit<B>)
Byte(ByteLit<B>)
ByteString(ByteStringLit<B>)
Implementations§
source§impl<B: Buffer> Literal<B>
impl<B: Buffer> Literal<B>
sourcepub fn parse(input: B) -> Result<Self, ParseError>
pub fn parse(input: B) -> Result<Self, ParseError>
Parses the given input as a Rust literal.
sourcepub fn suffix(&self) -> &str
pub fn suffix(&self) -> &str
Returns the suffix of this literal or ""
if it doesn’t have one.
Rust token grammar actually allows suffixes for all kinds of tokens.
Most Rust programmer only know the type suffixes for integer and
floats, e.g. 0u32
. And in normal Rust code, everything else causes an
error. But it is possible to pass literals with arbitrary suffixes to
proc macros, for example:
some_macro!(3.14f33 16px '🦊'good_boy "toph"beifong);
Boolean literals, not actually being literals, but idents, cannot have
suffixes and this method always returns ""
for those.
There are some edge cases to be aware of:
- Integer suffixes must not start with
e
orE
as that conflicts with the exponent grammar for floats.0e1
is a float;0eel
is also parsed as a float and results in an error. - Hexadecimal integers eagerly parse digits, so
0x5abcdefgh
has a suffix vongh
. - Suffixes can contain and start with
_
, but for integer and number literals,_
is eagerly parsed as part of the number, so1_x
has the suffixx
. - The input
55f32
is regarded as integer literal with suffixf32
.
§Example
use litrs::Literal;
assert_eq!(Literal::parse(r##"3.14f33"##).unwrap().suffix(), "f33");
assert_eq!(Literal::parse(r##"123hackerman"##).unwrap().suffix(), "hackerman");
assert_eq!(Literal::parse(r##"0x0fuck"##).unwrap().suffix(), "uck");
assert_eq!(Literal::parse(r##"'🦊'good_boy"##).unwrap().suffix(), "good_boy");
assert_eq!(Literal::parse(r##""toph"beifong"##).unwrap().suffix(), "beifong");
Trait Implementations§
source§impl<B: Buffer> From<ByteStringLit<B>> for Literal<B>
impl<B: Buffer> From<ByteStringLit<B>> for Literal<B>
source§fn from(src: ByteStringLit<B>) -> Self
fn from(src: ByteStringLit<B>) -> Self
source§impl<B: Buffer> From<IntegerLit<B>> for Literal<B>
impl<B: Buffer> From<IntegerLit<B>> for Literal<B>
source§fn from(src: IntegerLit<B>) -> Self
fn from(src: IntegerLit<B>) -> Self
impl<B: Eq + Buffer> Eq for Literal<B>
impl<B: Buffer> StructuralPartialEq for Literal<B>
Auto Trait Implementations§
impl<B> Freeze for Literal<B>where
B: Freeze,
impl<B> RefUnwindSafe for Literal<B>where
B: RefUnwindSafe,
impl<B> Send for Literal<B>where
B: Send,
impl<B> Sync for Literal<B>where
B: Sync,
impl<B> Unpin for Literal<B>where
B: Unpin,
impl<B> UnwindSafe for Literal<B>where
B: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)