• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

"Freedom from syn": Proc macro tools for operating on token trees

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 easy to use for any attribute-style proc macro. You can still use this crate with a manually-executed proc macro hack.

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 a more involved example.

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.

More Repositories

1

rust-gc

Simple tracing (mark and sweep) garbage collector for Rust
Rust
862
star
2

compiletest-rs

An extraction of the compiletest utility from the Rust compiler
Rust
216
star
3

elsa

Append-only collections for Rust where borrows to entries can outlive insertions
Rust
171
star
4

triomphe

Fork of std::sync::Arc with lots of utilities useful for FFI
Rust
154
star
5

rust-adorn

Python-style function decorators for Rust
Rust
153
star
6

mitosis

Spawn processes with arbitrary closures in rust
Rust
105
star
7

oreutils

Installer for various Rust reimaginations of coreutils. These are not drop-in replacements.
Rust
93
star
8

array-init

Safe wrapper for initializing arrays
Rust
67
star
9

ChatExchange

A Python API for talking to Stack Exchange chat
Python
65
star
10

namespacing-rfc

RFC for Packages as Optional Namespaces
46
star
11

rust-tenacious

Lint to disallow the moving of marked types in Rust
Rust
34
star
12

pathdiff

Rust
28
star
13

cs733

CS 733 assignments
Go
25
star
14

rust-internals-docs

Deprecated, please use the official docs at https://doc.rust-lang.org/nightly/nightly-rustc
HTML
24
star
15

IIT-Timetable

Timetable generator for IIT Bombay
HTML
14
star
16

humpty_dumpty

Implicit Drop/move protection for Rust (linear types)
Rust
12
star
17

AnnoTabe

Annotations for tabs in Chrome
JavaScript
11
star
18

dash

Distributed Shell: A way to replicate system administration commands across machines using Raft
Go
10
star
19

handlebars-fluent

Rust
10
star
20

webgl-to-webvr

Example of turning a WebGL application into WebVR
JavaScript
8
star
21

Manish-Codes

JavaScript
8
star
22

manishearth.github.io

HTML
5
star
23

webgl-to-webxr

Webxr version of https://github.com/Manishearth/webgl-to-webvr
JavaScript
5
star
24

error-index-localization

Rust error index localization
Rust
4
star
25

trashmap

A HashMap and HashSet that operate directly on hashes instead of keys, avoiding rehashing
Rust
4
star
26

rust-extensible

Extensible enums for Rust
Rust
4
star
27

hashglobe

A stable fork of std::HashMap with fallible APIs. Untested.
Rust
3
star
28

reiterate

Iterator adaptor with caching that allows reiterating over the same iterator through the cache
Rust
3
star
29

sudoku-zkp

JavaScript
2
star
30

media-capture

playing around with gstreamer media capture (temporary repo)
Rust
2
star
31

typing-cheatsheets

Cheatsheets for various keyboards
HTML
2
star
32

HostelNoticeboard

Electronic noticeboard for IIT Bombay hostels. Meant to run on an Raspberry Pi.
PHP
2
star
33

sudoku

sudoku annotation/solving tool
JavaScript
2
star
34

webxr-three

three.js fork for webxr testing stuff.
JavaScript
2
star
35

Kapi

Math notebook for Windows 8 Metro
JavaScript
2
star
36

freestring

CString, but with malloc and free
Rust
2
star
37

MathToTeX

JavaScript
2
star
38

css-properties-list

HTML
2
star
39

testcrate

Rust
1
star
40

gstreamer-presentation

JavaScript
1
star
41

rustconf-2017

Website for RustConf 2017
CSS
1
star
42

ChatExchange-Scripts

Scripts that use ChatExchange
Python
1
star
43

ElectionPortal

The worst PHP code I have ever written. Hey, I was in a hurry.
CSS
1
star
44

stylo-flat

Copy of http://hg.mozilla.org/incubator/stylo with history removed for easy cloning and contribution
1
star
45

servo-gecko-try

Service for forwarding servo PRs to gecko's try infra
Python
1
star
46

iterm-git-add-hunk

lol
Shell
1
star
47

StackExchange-ChatBot

Generic Stack Exchange chatbot, using ChatExchange
Python
1
star
48

Presentations

Slides and presentations
JavaScript
1
star
49

rustfest-slides

Slides for my RustFest 2017 (Kiev) keynote
JavaScript
1
star
50

starcon-presentation

JavaScript
1
star
51

bell-inequality-paper

Term paper on Bell's inequality
TeX
1
star