Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/rust/macros/helpers.rs
29266 views
1
// SPDX-License-Identifier: GPL-2.0
2
3
use proc_macro::{token_stream, Group, Ident, TokenStream, TokenTree};
4
5
pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
6
if let Some(TokenTree::Ident(ident)) = it.next() {
7
Some(ident.to_string())
8
} else {
9
None
10
}
11
}
12
13
pub(crate) fn try_literal(it: &mut token_stream::IntoIter) -> Option<String> {
14
if let Some(TokenTree::Literal(literal)) = it.next() {
15
Some(literal.to_string())
16
} else {
17
None
18
}
19
}
20
21
pub(crate) fn try_string(it: &mut token_stream::IntoIter) -> Option<String> {
22
try_literal(it).and_then(|string| {
23
if string.starts_with('\"') && string.ends_with('\"') {
24
let content = &string[1..string.len() - 1];
25
if content.contains('\\') {
26
panic!("Escape sequences in string literals not yet handled");
27
}
28
Some(content.to_string())
29
} else if string.starts_with("r\"") {
30
panic!("Raw string literals are not yet handled");
31
} else {
32
None
33
}
34
})
35
}
36
37
pub(crate) fn expect_ident(it: &mut token_stream::IntoIter) -> String {
38
try_ident(it).expect("Expected Ident")
39
}
40
41
pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> char {
42
if let TokenTree::Punct(punct) = it.next().expect("Reached end of token stream for Punct") {
43
punct.as_char()
44
} else {
45
panic!("Expected Punct");
46
}
47
}
48
49
pub(crate) fn expect_string(it: &mut token_stream::IntoIter) -> String {
50
try_string(it).expect("Expected string")
51
}
52
53
pub(crate) fn expect_string_ascii(it: &mut token_stream::IntoIter) -> String {
54
let string = try_string(it).expect("Expected string");
55
assert!(string.is_ascii(), "Expected ASCII string");
56
string
57
}
58
59
pub(crate) fn expect_group(it: &mut token_stream::IntoIter) -> Group {
60
if let TokenTree::Group(group) = it.next().expect("Reached end of token stream for Group") {
61
group
62
} else {
63
panic!("Expected Group");
64
}
65
}
66
67
pub(crate) fn expect_end(it: &mut token_stream::IntoIter) {
68
if it.next().is_some() {
69
panic!("Expected end");
70
}
71
}
72
73
/// Given a function declaration, finds the name of the function.
74
pub(crate) fn function_name(input: TokenStream) -> Option<Ident> {
75
let mut input = input.into_iter();
76
while let Some(token) = input.next() {
77
match token {
78
TokenTree::Ident(i) if i.to_string() == "fn" => {
79
if let Some(TokenTree::Ident(i)) = input.next() {
80
return Some(i);
81
}
82
return None;
83
}
84
_ => continue,
85
}
86
}
87
None
88
}
89
90
pub(crate) fn file() -> String {
91
#[cfg(not(CONFIG_RUSTC_HAS_SPAN_FILE))]
92
{
93
proc_macro::Span::call_site()
94
.source_file()
95
.path()
96
.to_string_lossy()
97
.into_owned()
98
}
99
100
#[cfg(CONFIG_RUSTC_HAS_SPAN_FILE)]
101
#[allow(clippy::incompatible_msrv)]
102
{
103
proc_macro::Span::call_site().file()
104
}
105
}
106
107