Skip to main content

ab_riscv_macros_common/
code_utils.rs

1//! This module contains hacks for Rust nightly syntax to make it compatible with `syn`
2// TODO: Remove this module once `syn` supports used Rust nightly syntax
3
4const FROM_TRAIT_CONST_1: &str = "const trait ";
5const TO_TRAIT_CONST_1: &str = "trait _const";
6const FROM_TRAIT_CONST_2: &str = "const unsafe trait ";
7const TO_TRAIT_CONST_2: &str = "unsafe trait _const";
8const FROM_IMPL_CONST_1: &str = "const impl";
9const TO_IMPL_CONST_1: &str = "#[cst]impl";
10const FROM_IMPL_CONST_2: &str = "const unsafe impl";
11const TO_IMPL_CONST_2: &str = "#[cst]unsafe impl";
12const FROM_IMPL_CONST_3: &str = "const  impl";
13const TO_IMPL_CONST_3: &str = "#[cst]\nimpl";
14const FROM_IMPL_CONST_4: &str = "const  unsafe impl";
15const TO_IMPL_CONST_4: &str = "#[cst]\nunsafe impl";
16const FROM_IMPL_CONST_REVERT_1: &str = "pub cnst::";
17const TO_IMPL_CONST_REVERT_1: &str = "pub const ";
18const FROM_IMPL_CONST_REVERT_2: &str = "pub cnst ::";
19const TO_IMPL_CONST_REVERT_2: &str = "pub const  ";
20const FROM_IMPL_CONST_REVERT_3: &str = ") cnst::";
21const TO_IMPL_CONST_REVERT_3: &str = ") const ";
22const FROM_IMPL_CONST_REVERT_4: &str = ") cnst ::";
23const TO_IMPL_CONST_REVERT_4: &str = ") const  ";
24const FROM_IMPL_CONST_REVERT_5: &str = "cnst::fn";
25const TO_IMPL_CONST_REVERT_5: &str = "const fn";
26
27const FROM_BRACKETS_CONST_1: &str = "[const] ";
28const TO_BRACKETS_CONST_1: &str = "BRCONST+";
29const FROM_BRACKETS_CONST_2: &str = " [const] ";
30const TO_BRACKETS_CONST_2: &str = "BRCONST +";
31
32/// Replace bits of Rust nightly syntax with something that is technically valid in stable Rust, so
33/// `syn` can parse it
34pub fn pre_process_rust_code(s: &mut str) {
35    replace_inplace(s, FROM_TRAIT_CONST_1, TO_TRAIT_CONST_1);
36    replace_inplace(s, FROM_TRAIT_CONST_2, TO_TRAIT_CONST_2);
37    replace_inplace(s, FROM_IMPL_CONST_1, TO_IMPL_CONST_1);
38    replace_inplace(s, FROM_IMPL_CONST_2, TO_IMPL_CONST_2);
39    replace_inplace(s, FROM_IMPL_CONST_3, TO_IMPL_CONST_3);
40    replace_inplace(s, FROM_IMPL_CONST_4, TO_IMPL_CONST_4);
41    replace_inplace(s, FROM_IMPL_CONST_REVERT_1, TO_IMPL_CONST_REVERT_1);
42    replace_inplace(s, FROM_IMPL_CONST_REVERT_2, TO_IMPL_CONST_REVERT_2);
43    replace_inplace(s, FROM_IMPL_CONST_REVERT_3, TO_IMPL_CONST_REVERT_3);
44    replace_inplace(s, FROM_IMPL_CONST_REVERT_4, TO_IMPL_CONST_REVERT_4);
45    replace_inplace(s, FROM_IMPL_CONST_REVERT_5, TO_IMPL_CONST_REVERT_5);
46    replace_inplace(s, FROM_BRACKETS_CONST_1, TO_BRACKETS_CONST_1);
47    replace_inplace(s, FROM_BRACKETS_CONST_2, TO_BRACKETS_CONST_2);
48}
49
50/// The inverse of [`pre_process_rust_code()`]
51pub fn post_process_rust_code(s: &mut str) {
52    replace_inplace(s, TO_TRAIT_CONST_2, FROM_TRAIT_CONST_2);
53    replace_inplace(s, TO_TRAIT_CONST_1, FROM_TRAIT_CONST_1);
54    replace_inplace(s, TO_IMPL_CONST_1, FROM_IMPL_CONST_1);
55    replace_inplace(s, TO_IMPL_CONST_2, FROM_IMPL_CONST_2);
56    replace_inplace(s, TO_IMPL_CONST_3, FROM_IMPL_CONST_3);
57    replace_inplace(s, TO_IMPL_CONST_4, FROM_IMPL_CONST_4);
58    replace_inplace(s, TO_BRACKETS_CONST_1, FROM_BRACKETS_CONST_1);
59    replace_inplace(s, TO_BRACKETS_CONST_2, FROM_BRACKETS_CONST_2);
60}
61
62fn replace_inplace(mut s: &mut str, from: &str, to: &str) {
63    assert_eq!(from.len(), to.len(), "`{from}` != `{to}`");
64
65    if from.is_empty() {
66        return;
67    }
68
69    while let Some(found) = s.find(from) {
70        let start = found;
71        let end = found + from.len();
72
73        // SAFETY: Replacing a valid string with a valid string of the same length
74        unsafe { s.as_bytes_mut() }
75            .get_mut(start..end)
76            .expect("Just found a string of the desired length; qed")
77            .copy_from_slice(to.as_bytes());
78
79        s = &mut s[end..];
80    }
81}