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

  • proc-macro (default): adds From<proc_macro::Literal> and From<&proc_macro::Literal> for Literal.
  • proc-macro2 (default): adds the dependency proc_macro2 and the impls From<proc_macro2::Literal> and From<&proc_macro2::Literal> for Literal.

Structs

ByteLit

A (single) byte literal, e.g. b'k' or b'!'.

ByteStringLit

A byte string or raw byte string literal, e.g. b"hello" or br#"abc"def"#.

CharLit

A character literal, e.g. 'g' or '🦊'.

Error

Errors during parsing.

FloatLit

A floating point literal, e.g. 3.14, 8., 135e12, 27f32 or 1.956e2f64.

IntegerLit

An integer literal, e.g. 27, 0x7F, 0b101010u8 or 5_000_000i64.

StringLit

A string or raw string literal, e.g. "foo", "Grüße" or r#"a🦊c"d🦀f"#.

Enums

BoolLit

A bool literal: true or false. Also see the reference.

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 String and &str. Implementation detail.

FromIntegerLiteral

Integer literal types. Implementation detail.

Type Definitions

OwnedLiteral

A literal which owns the underlying buffer.

SharedLiteral

A literal whose underlying buffer is borrowed.