Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 8cbbbb3

Browse files
committed
feat(old): proc-macro-lib trial
1 parent 0af8cc8 commit 8cbbbb3

File tree

5 files changed

+209
-0
lines changed

5 files changed

+209
-0
lines changed

proc/Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "merge_proc"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
proc-macro2.workspace = true
8+
quote.workspace = true
9+
syn.workspace = true
10+
11+
[lib]
12+
proc-macro = true

proc/README.org

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#+title: Merge Proc Macro Library
2+
3+
* Proc-Macro Library
4+
5+
** Desired Syntax
6+
#+begin_src rust
7+
#[python]
8+
use {
9+
matplotlib::pyplot as plt,
10+
numpy as np,
11+
}
12+
13+
#[cpp]
14+
use {
15+
std::vector,
16+
std::string,
17+
}
18+
19+
#[rust]
20+
use {
21+
std::collections::HashMap,
22+
std::collections::HashSet,
23+
}
24+
25+
#[go]
26+
use {
27+
fmt,
28+
strings,
29+
}
30+
31+
#[javascript]
32+
use {
33+
std::collections::HashMap,
34+
std::collections::HashSet,
35+
}
36+
#+END_SRC

proc/src/lib.rs

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
use proc_macro::{self, TokenStream};
2+
use syn::{self, parse_macro_input, DeriveInput};
3+
// use proc_macro2::TokenStream;
4+
5+
#[proc_macro_attribute]
6+
/// Generates Rust imports
7+
///
8+
/// * `_attr`: _
9+
/// * `input`: use statement
10+
pub fn rust(_attr: TokenStream, input: TokenStream) -> TokenStream {
11+
input
12+
}
13+
14+
#[proc_macro_attribute]
15+
pub fn cpp(attr: TokenStream, input: TokenStream) -> TokenStream {
16+
let mut output = TokenStream::new();
17+
18+
// Return transformed tokens
19+
output
20+
}
21+
22+
#[proc_macro_attribute]
23+
pub fn python(attr: TokenStream, input: TokenStream) -> TokenStream {
24+
let input = parse_macro_input!(input as DeriveInput);
25+
26+
let expanded = quote::quote! {
27+
#input
28+
println!("Expanded Python use statement");
29+
};
30+
31+
TokenStream::from(expanded)
32+
}
33+
34+
#[proc_macro_attribute]
35+
pub fn java(attr: TokenStream, input: TokenStream) -> TokenStream {
36+
let mut output = TokenStream::new();
37+
38+
// Return transformed tokens
39+
output
40+
}
41+
42+
#[proc_macro_attribute]
43+
pub fn csharp(attr: TokenStream, input: TokenStream) -> TokenStream {
44+
let mut output = TokenStream::new();
45+
46+
// Return transformed tokens
47+
output
48+
}
49+
50+
#[proc_macro_attribute]
51+
pub fn javascript(attr: TokenStream, input: TokenStream) -> TokenStream {
52+
let mut output = TokenStream::new();
53+
54+
// Return transformed tokens
55+
output
56+
}
57+
58+
#[proc_macro_attribute]
59+
pub fn typescript(attr: TokenStream, input: TokenStream) -> TokenStream {
60+
let mut output = TokenStream::new();
61+
62+
// Return transformed tokens
63+
output
64+
}
65+
66+
#[proc_macro_attribute]
67+
pub fn go(attr: TokenStream, input: TokenStream) -> TokenStream {
68+
let mut output = TokenStream::new();
69+
70+
// Return transformed tokens
71+
output
72+
}
73+
74+
#[proc_macro_attribute]
75+
pub fn php(attr: TokenStream, input: TokenStream) -> TokenStream {
76+
let mut output = TokenStream::new();
77+
78+
// Return transformed tokens
79+
output
80+
}
81+
82+
#[proc_macro_attribute]
83+
pub fn ruby(attr: TokenStream, input: TokenStream) -> TokenStream {
84+
let mut output = TokenStream::new();
85+
86+
// Return transformed tokens
87+
output
88+
}
89+
90+
#[proc_macro_attribute]
91+
pub fn swift(attr: TokenStream, input: TokenStream) -> TokenStream {
92+
let mut output = TokenStream::new();
93+
94+
output
95+
}
96+
97+
#[proc_macro_attribute]
98+
pub fn kotlin(attr: TokenStream, input: TokenStream) -> TokenStream {
99+
let mut output = TokenStream::new();
100+
output
101+
}
102+
103+
#[proc_macro_attribute]
104+
pub fn c(attr: TokenStream, input: TokenStream) -> TokenStream {
105+
let mut output = TokenStream::new();
106+
output
107+
}

proc/tests/python.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use merge_proc::python;
2+
3+
// Check out for more metavariable types:
4+
// https://doc.rust-lang.org/reference/macros-by-example.html#metavariables
5+
macro_rules! using { // single use statement
6+
($e:path) => {
7+
use $e;
8+
};
9+
($e:path, $($es:path),+) => { // no trailing comma
10+
using!($e);
11+
using!($($es),+);
12+
};
13+
($e:path, $($es:path),+ ,) => { // trailing comma
14+
using!($e);
15+
using!($($es),+);
16+
};
17+
($e:path, ) => { // trailing comma
18+
using!($e);
19+
using!($($es),+);
20+
};
21+
22+
/************************************************/
23+
/* ERRORS */
24+
/************************************************/
25+
// Two trailing non capturing commas
26+
() => {
27+
::std::compile_error!("Redundant using statement")
28+
};
29+
}
30+
31+
#[test]
32+
fn use_python() {
33+
using! {
34+
std::io::copy,
35+
std::io,
36+
std::iostream,
37+
std::io
38+
};
39+
}

proc/tests/rust.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use merge_proc::rust;
2+
3+
#[rust]
4+
use std::{
5+
collections::HashMap,
6+
io::{copy, sink},
7+
mem::zeroed,
8+
};
9+
10+
#[test]
11+
fn use_rust() {
12+
let mut map = HashMap::new();
13+
map.insert("hello", "world");
14+
println!("{:?}", map);
15+
}

0 commit comments

Comments
 (0)