All Projects → LukasKalbertodt → litrs

LukasKalbertodt / litrs

Licence: Apache-2.0, MIT licenses found Licenses found Apache-2.0 LICENSE-APACHE MIT LICENSE-MIT
Parsing and inspecting Rust literals (particularly useful for proc macros)

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to litrs

rust-fsm
Finite state machine framework for Rust with readable specifications
Stars: ✭ 67 (+168%)
Mutual labels:  proc-macro
velcro
Collection initialization macros for Rust
Stars: ✭ 45 (+80%)
Mutual labels:  literal
field names
proc-macro for accessing struct field names at runtime
Stars: ✭ 26 (+4%)
Mutual labels:  proc-macro
gnirts
Obfuscate string literals in JavaScript code.
Stars: ✭ 65 (+160%)
Mutual labels:  literal
lombok-rs
Lombok port for Rust
Stars: ✭ 31 (+24%)
Mutual labels:  proc-macro
reacty yew
Generate Yew components from React components via Typescript type definitions
Stars: ✭ 46 (+84%)
Mutual labels:  proc-macro
real-async-trait-rs
A proc macro for real async traits, using nightly-only existential types and generic associated types to work around the need for type erasure
Stars: ✭ 38 (+52%)
Mutual labels:  proc-macro
path-dsl-rs
A Rust utility DSL and macro to help construct and modify Paths.
Stars: ✭ 19 (-24%)
Mutual labels:  rust-macro
proc-quote
A procedural macro implementation of `quote!`.
Stars: ✭ 22 (-12%)
Mutual labels:  proc-macro
Javascript Obfuscator
A powerful obfuscator for JavaScript and Node.js
Stars: ✭ 8,204 (+32716%)
Mutual labels:  literal
maybe-async-rs
A procedure macro to unify SYNC and ASYNC implementation for downstream application/crates
Stars: ✭ 22 (-12%)
Mutual labels:  proc-macro
futures-async-stream
Async stream for Rust and the futures crate.
Stars: ✭ 141 (+464%)
Mutual labels:  proc-macro
superstruct
Rust library for versioned data types
Stars: ✭ 27 (+8%)
Mutual labels:  proc-macro
easy-ext
An attribute macro for easily writing extension trait pattern.
Stars: ✭ 17 (-32%)
Mutual labels:  proc-macro
zkp
Experimental zero-knowledge proof compiler in Rust macros
Stars: ✭ 121 (+384%)
Mutual labels:  rust-macro

litrs: parsing and inspecting Rust literals

CI status of master Crates.io Version docs.rs

litrs offers functionality to parse Rust literals, i.e. tokens in the Rust programming language that represent fixed values. 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.

While this library is fairly young, it is extensively tested and I think the number of parsing bugs should already be very low. 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].