summaryrefslogtreecommitdiffstats
path: root/vendor/windows-tokens/src/token_stream.rs
blob: 923e5faa0e250422d169f7501145047ad6d389fd (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/// A stream of tokens
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct TokenStream(pub String);

impl From<String> for TokenStream {
    fn from(tokens: String) -> Self {
        Self(tokens)
    }
}

impl From<&String> for TokenStream {
    fn from(tokens: &String) -> Self {
        Self(tokens.to_string())
    }
}

impl From<&str> for TokenStream {
    fn from(tokens: &str) -> Self {
        Self(tokens.to_string())
    }
}

impl TokenStream {
    pub fn new() -> Self {
        Self(String::new())
    }

    /// Appends another stream to the stream
    ///
    /// note: a space will be inserted before the other stream
    pub fn combine(&mut self, other: &TokenStream) {
        self.push_space();
        self.0.push_str(&other.0)
    }

    #[must_use]
    pub fn join(&self, value: &str) -> Self {
        Self(format!("{}{value}", self.0))
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// View the stream as a string
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Convert the stream into a `String`
    pub fn into_string(self) -> String {
        self.0
    }

    /// Parse the token stream as something
    ///
    /// Mostly used with `proc_macro2::TokenStream` or `proc_macro::TokenStream`
    pub fn parse<T: core::str::FromStr>(self) -> Result<T, T::Err> {
        self.into_string().parse()
    }

    pub(crate) fn push_space(&mut self) {
        match self.last_char() {
            None | Some(' ') => {}
            _ => self.0.push(' '),
        }
    }

    pub fn push(&mut self, c: char) {
        self.0.push(c)
    }

    pub fn push_str(&mut self, str: &str) {
        self.0.push_str(str)
    }

    fn last_char(&self) -> Option<char> {
        self.0.chars().last()
    }
}

impl Default for TokenStream {
    fn default() -> Self {
        Self::new()
    }
}

impl core::iter::FromIterator<TokenStream> for TokenStream {
    fn from_iter<I: IntoIterator<Item = TokenStream>>(iter: I) -> Self {
        iter.into_iter()
            .fold(None, |accum: Option<TokenStream>, n| {
                let mut ts = accum.unwrap_or_else(TokenStream::new);
                ts.combine(&n);
                Some(ts)
            })
            .unwrap_or_else(TokenStream::new)
    }
}

/// A delimiter around a block of code
#[derive(Copy, Clone)]
pub enum Delimiter {
    /// `[]`
    Bracket,
    /// `{}`
    Brace,
    /// `()`
    Parenthesis,
}

impl Delimiter {
    /// The opening delimiter
    pub fn open(self) -> char {
        match self {
            Delimiter::Bracket => '[',
            Delimiter::Brace => '{',
            Delimiter::Parenthesis => '(',
        }
    }

    /// The closing delimiter
    pub fn close(self) -> char {
        match self {
            Delimiter::Bracket => ']',
            Delimiter::Brace => '}',
            Delimiter::Parenthesis => ')',
        }
    }
}

/// A literal of some sort
pub struct Literal {
    inner: String,
}

macro_rules! unsuffixed {
    ($ty:ty => $name:ident) => {
        pub fn $name(n: $ty) -> Self {
            Self { inner: n.to_string() }
        }
    };
}

impl Literal {
    unsuffixed!(i64 => i64_unsuffixed);
    unsuffixed!(usize => usize_unsuffixed);
    unsuffixed!(u32 => u32_unsuffixed);
    unsuffixed!(u16 => u16_unsuffixed);
    unsuffixed!(u8 => u8_unsuffixed);

    pub fn byte_string(s: &[u8]) -> Self {
        Self { inner: format!("b\"{}\"", core::str::from_utf8(s).expect("Could not turn bytes into byte literal")) }
    }

    pub fn as_str(&self) -> &str {
        &self.inner
    }
}

impl core::fmt::Display for TokenStream {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}