Crate litrs[−][src]
Expand description
Parsing and inspecting Rust literal tokens.
This library offers functionality to parse Rust literals, i.e. tokens in the Rust programming language that represent fixed values. The grammar for those is defined here.
This kind of functionality already exists in the crate syn
. However, as
you oftentimes don’t need (nor want) the full power of syn
, litrs
was
built. This crate also offers a bit more flexibility compared to syn
(only regarding literals, of course).
The main type of this library is Literal
. You can obtain it via
Literal::parse
or by using the From<proc_macro[2]::Literal>
impls.
use litrs::Literal; let lit = Literal::parse("3.14f32").expect("failed to parse literal"); match lit { Literal::Float(lit) => { println!("{:?}", lit.type_suffix()); } Literal::Bool(lit) => { /* ... */ } Literal::Integer(lit) => { /* ... */ } Literal::Char(lit) => { /* ... */ } Literal::String(lit) => { /* ... */ } Literal::Byte(lit) => { /* ... */ } Literal::ByteString(lit) => { /* ... */ } }
If you know what kind of literal your input represents, or if you want to
allow only one specific literal kind, you can also parse into specific
literal types (e.g. IntegerLit
) directly. All literal types have a
parse
method for that purpose.
Crate features
Structs
ByteLit | A (single) byte literal, e.g. |
ByteStringLit | A byte string or raw byte string literal, e.g. |
CharLit | A character literal, e.g. |
Error | Errors during parsing. |
FloatLit | A floating point literal, e.g. |
IntegerLit | An integer literal, e.g. |
StringLit | A string or raw string literal, e.g. |
Enums
BoolLit | A bool literal: |
FloatType | All possible float type suffixes. |
IntegerBase | The bases in which an integer can be specified. |
IntegerType | All possible integer type suffixes. |
Literal | A literal. This is the main type of this library. |
Traits
Buffer | A shared or owned string buffer. Implemented for |
FromIntegerLiteral | Integer literal types. Implementation detail. |
Type Definitions
OwnedLiteral | A literal which owns the underlying buffer. |
SharedLiteral | A literal whose underlying buffer is borrowed. |