-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
128 lines (120 loc) · 3.58 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use proc_macro::{Group, Ident, TokenStream, TokenTree};
fn replace_ident(ident: Ident) -> Option<TokenTree> {
let ident_str = ident.to_string();
let new_str = match ident_str.as_str() {
"Xato" => "Err",
"Yaxshi" => "Ok",
"Matn" => "String",
"Lug`at" => "HashMap",
"Odatiy" => "Default",
"Xatolik" => "Error",
"Tanlov" => "Option",
"Bir" => "Some",
"Hech" => "None",
"Natija" => "Result",
"Shu" => "Self",
"yoziq" => "println",
"to`xta" => "break",
"asinxron" => "async",
"kut" => "await",
"takror" => "loop",
"ko`chir" => "move",
"quti" => "crate",
"yetib_bormaydi" => "unreachable_code",
"day" => "as",
"konstanta" => "const",
"hislat" => "trait",
"xavfli" => "unsafe",
"da" => "in",
"dan" => "from",
"dinamik" => "dyn",
"och" => "unwrap",
"odatiy" => "default",
"ko`chirma" => "as_ref",
"kch" => "io",
"kengaytma" => "extern",
"yo`q" => "false",
"funksiya" => "fn",
"ushbu" => "super",
"kirit" => "insert",
"ol" => "get",
"mumkin" => "allow",
"panika" | "chiq" | "vahima" => "panic",
"modul" => "mod",
"o`zgaruvchan" => "mut",
"yangi" => "new",
"qayerda" => "where",
"qachonki" => "for",
"ol_yoki_kirit" => "get_or_insert_with",
"asosiy" => "main",
"ochiq" => "pub",
"qaytar" => "return",
"to`ldir" => "impl",
"manzil" => "ref",
"solishtir" => "match",
"agar" => "if",
"unda" => "else",
"shu" => "self",
"joy" => "let",
"statik" => "static",
"shakl" => "struct",
"kutma" => "expect",
"toki" => "while",
"yukal" => "use",
"ga" => "into",
"ha" => "true",
"tur" => "enum",
"Guruh" => "Group",
"Identatsiya" => "Ident",
"TokenOqim" => "TokenStream",
"TokenDaraxt" => "TokenTree",
"matnga" => "to_string",
"statik_matnga" => "as_str",
"radius" => "span",
"Vektor" => "Vec",
"oqim" => "stream",
"qo`sh" => "push",
"kengay" => "extend",
"delimeter" => "delimiter",
"Punktuatsiya" => "Punct",
"Literal" => "Literal",
"protsedural_makro" => "proc_macro",
// Extra keywords for lexing
"cha" => None?,
_ => &ident_str,
};
let new_ident = Ident::new(new_str, ident.span());
Some(TokenTree::Ident(new_ident))
}
fn replace_tree(tok: TokenTree, out: &mut Vec<TokenTree>) {
match tok {
TokenTree::Group(group) => {
let mut group_elem = Vec::new();
replace_stream(group.stream(), &mut group_elem);
let mut new_stream = TokenStream::new();
new_stream.extend(group_elem);
out.push(TokenTree::Group(Group::new(group.delimiter(), new_stream)));
}
TokenTree::Ident(ident) => {
if let Some(ident) = replace_ident(ident) {
out.push(ident);
}
}
TokenTree::Punct(..) | TokenTree::Literal(..) => {
out.push(tok);
}
}
}
fn replace_stream(ts: TokenStream, out: &mut Vec<TokenTree>) {
for tok in ts {
replace_tree(tok, out)
}
}
#[proc_macro]
pub fn zang(item: TokenStream) -> TokenStream {
let mut returned = Vec::new();
replace_stream(item, &mut returned);
let mut out = TokenStream::new();
out.extend(returned);
out
}