#[non_exhaustive]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>),
CString(CStringLit<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 (Non-exhaustive)§
This enum is marked as non-exhaustive
Bool(BoolLit)
Integer(IntegerLit<B>)
Float(FloatLit<B>)
Char(CharLit<B>)
String(StringLit<B>)
Byte(ByteLit<B>)
ByteString(ByteStringLit<B>)
CString(CStringLit<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
eorEas that conflicts with the exponent grammar for floats.0e1is a float;0eelis also parsed as a float and results in an error. - Hexadecimal integers eagerly parse digits, so
0x5abcdefghhas a suffix vongh. - Suffixes can contain and start with
_, but for integer and number literals,_is eagerly parsed as part of the number, so1_xhas the suffixx. - The input
55f32is 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");