MCPcopy Index your code
hub / github.com/Manishearth/absolution

github.com/Manishearth/absolution @v0.1.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.1.1 ↗ · + Follow
45 symbols 99 edges 6 files 8 documented · 18%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

absolution

Build Status Current Version License: MIT/Apache-2.0

"Freedom from syn"

This crate provides an introspectible token tree representation for writing Rust proc macros. It's still somewhat unstable: It's usable, however I haven't quite decided what I want the API to look like and may publish breaking changes. I welcome feedback of all kinds!

The proc-macro2 crate provides a token tree representation, however for backwards compatibility reasons, this representation isn't very introspectible and you have to further parse it to do almost anything with it. For example, the Literal type does not actually expose any further information about the contained literal, only that it is a literal.

Typically people pull in the awesome syn crate if they wish to introspect the code further than this; syn parses Rust code into a full, introspectible AST that is easy to work with. This is great when writing custom derives, where your proc macro is being applied to some Rust item.

However, bang-style procedural macros, like format!(), typically don't need to introspect the Rust AST, they just need to look at the tokens. For example, a very simple format!() implementation just needs to be able to read the initial format string, and then get the comma delimited token trees for the arguments. Pulling in an entire Rust parser into your dependency tree for this is somewhat overkill.

absolution provides an introspectible token tree representation, structured similarly to that of proc-macro2, for use in designing bang-style procedural macros.

Currently the proc-macro-hack crate still relies on syn to work, so this crate is not yet useful for any attribute-style proc macro.

To use, simply use Into to convert from proc_macro or proc_macro2 token streams to absolution ones, and quote! to convert back.

extern crate proc_macro;
use absolution::{Ident, LitKind, Literal, TokenStream, TokenTree};
use quote::quote;

#[proc_macro]
pub fn make_func(tt: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let stream: TokenStream = tt.into();
    let first_token = &stream.tokens[0];
    let s = if let TokenTree::Literal(Literal {
        kind: LitKind::Str(s),
        ..
    }) = &first_token
    {
        s
    } else {
        panic!("Must start with a string!")
    };

    let ident = Ident {
        ident: s.to_string(),
        span: first_token.span(),
    };

    quote!(
        fn #ident() -> &'static str {
            #first_token
        }
    )
    .into()
}

See examples/string-enum for more examples

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 crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Extension points exported contracts — how you extend this code

AsNative (Interface)
(no doc) [6 implementers]
src/lib.rs

Core symbols most depended-on inside this repo

byte
called by 22
src/literal.rs
span
called by 6
src/lib.rs
to_string
called by 5
src/bigint.rs
backslash_x
called by 4
src/literal.rs
next_chr
called by 3
src/literal.rs
as_char
called by 2
src/lib.rs
reserve_two_digits
called by 2
src/bigint.rs
parse_lit_str_raw
called by 2
src/literal.rs

Shape

Function 19
Method 12
Class 8
Enum 5
Interface 1

Languages

Rust100%

Modules by API surface

src/literal.rs24 symbols
src/lib.rs11 symbols
src/bigint.rs6 symbols
tests/roundtrip.rs2 symbols
examples/string-enum/src/lib.rs1 symbols
examples/string-enum/examples/simple.rs1 symbols

For agents

$ claude mcp add absolution \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact