#literal #parsing #proc-macro #type #procedural

litrs

Parse and inspect Rust literals (i.e. tokens in the Rust programming language representing fixed values). Particularly useful for proc macros, but can also be used outside of a proc-macro context.

8 unstable releases (3 breaking)

0.4.0 Mar 5, 2023
0.3.0 Dec 19, 2022
0.2.3 Jun 9, 2021
0.2.0 May 28, 2021
0.1.1 May 25, 2021

#7 in Procedural macros

Download history 4400/week @ 2022-11-28 4069/week @ 2022-12-05 3905/week @ 2022-12-12 3872/week @ 2022-12-19 2737/week @ 2022-12-26 5842/week @ 2023-01-02 4985/week @ 2023-01-09 4690/week @ 2023-01-16 5248/week @ 2023-01-23 6486/week @ 2023-01-30 6251/week @ 2023-02-06 6504/week @ 2023-02-13 6403/week @ 2023-02-20 6280/week @ 2023-02-27 5910/week @ 2023-03-06 6713/week @ 2023-03-13

26,005 downloads per month
Used in 202 crates (28 directly)

MIT/Apache

170KB
3.5K SLoC

litrs: parsing and inspecting Rust literals

CI status of main Crates.io Version docs.rs

litrs offers functionality to parse Rust literals, i.e. tokens in the Rust programming language that represent fixed values. For example: 27, "crab", bool. This is particularly useful for proc macros, but can also be used outside of a proc-macro context.

Why this library? Unfortunately, the proc_macro API shipped with the compiler offers no easy way to inspect literals. There are mainly two libraries for this purpose: syn and literalext. The latter is deprecated. And syn is oftentimes overkill for the task at hand, especially when developing function-like proc-macros (e.g. foo!(..)). This crate is a lightweight alternative. Also, when it comes to literals, litrs offers a bit more flexibility and a few more features compared to syn.

I'm interested in community feedback! If you consider using this, please speak your mind in this issue.

Example

In proc macro

use std::convert::TryFrom;
use proc_macro::TokenStream;
use litrs::Literal;

#[proc_macro]
pub fn foo(input: TokenStream) -> TokenStream {
    // Please do proper error handling in your real code!
    let first_token = input.into_iter().next().expect("no input");

    // `try_from` will return an error if the token is not a literal.
    match Literal::try_from(first_token) {
        // Convenient methods to produce decent errors via `compile_error!`.
        Err(e) => return e.to_compile_error(),

        // You can now inspect your literal!
        Ok(Literal::Integer(i)) => {
            println!("Got an integer specified in base {:?}", i.base());

            let value = i.value::<u64>().expect("integer literal too large");
            println!("Is your integer even? {}", value % 2 == 0);
        }
        Ok(other) => {
            println!("Got a non-integer literal");
        }
    }

    TokenStream::new() // dummy output
}

If you are expecting a specific kind of literal, you can also use this, which will return an error if the token is not a float literal.

FloatLit::try_from(first_token)

Parsing from a &str

Outside of a proc macro context you might want to parse a string directly.

use litrs::{FloatLit, Literal};

let lit = Literal::parse("'🦀'").expect("failed to parse literal");
let float_lit = FloatLit::parse("2.7e3").expect("failed to parse as float literal");

See the documentation or the examples/ directory for more examples and information.



License

Licensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~86KB