about summary refs log tree commit diff
path: root/library/stdarch/crates/stdarch-gen-arm/src/wildstring.rs
blob: 4f8cc67f5e0197857d388f56a872eb24afcd9a22 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
use itertools::Itertools;
use proc_macro2::TokenStream;
use quote::{ToTokens, TokenStreamExt, quote};
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::str::pattern::Pattern;
use std::{fmt, str::FromStr};

use crate::context::LocalContext;
use crate::fn_suffix::make_neon_suffix;
use crate::typekinds::{ToRepr, TypeRepr};
use crate::wildcards::Wildcard;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WildStringPart {
    String(String),
    Wildcard(Wildcard),
}

/// Wildcard-able string
#[derive(Debug, Clone, PartialEq, Eq, Default, SerializeDisplay, DeserializeFromStr)]
pub struct WildString(pub Vec<WildStringPart>);

impl WildString {
    pub fn has_wildcards(&self) -> bool {
        for part in self.0.iter() {
            if let WildStringPart::Wildcard(..) = part {
                return true;
            }
        }

        false
    }

    pub fn wildcards(&self) -> impl Iterator<Item = &Wildcard> + '_ {
        self.0.iter().filter_map(|part| match part {
            WildStringPart::Wildcard(w) => Some(w),
            _ => None,
        })
    }

    pub fn iter(&self) -> impl Iterator<Item = &WildStringPart> + '_ {
        self.0.iter()
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut WildStringPart> + '_ {
        self.0.iter_mut()
    }

    pub fn starts_with(&self, s2: &str) -> bool {
        self.to_string().starts_with(s2)
    }

    pub fn prepend_str(&mut self, s: impl Into<String>) {
        self.0.insert(0, WildStringPart::String(s.into()))
    }

    pub fn push_str(&mut self, s: impl Into<String>) {
        self.0.push(WildStringPart::String(s.into()))
    }

    pub fn push_wildcard(&mut self, w: Wildcard) {
        self.0.push(WildStringPart::Wildcard(w))
    }

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

    pub fn replace<P>(&self, from: P, to: &str) -> WildString
    where
        P: Pattern + Copy,
    {
        WildString(
            self.0
                .iter()
                .map(|part| match part {
                    WildStringPart::String(s) => WildStringPart::String(s.replace(from, to)),
                    part => part.clone(),
                })
                .collect_vec(),
        )
    }

    pub fn build_acle(&mut self, ctx: &LocalContext) -> Result<(), String> {
        self.build(ctx, TypeRepr::ACLENotation)
    }

    pub fn build_neon_intrinsic_signature(&mut self, ctx: &LocalContext) -> Result<(), String> {
        let repr = TypeRepr::ACLENotation;
        self.iter_mut().try_for_each(|wp| -> Result<(), String> {
            if let WildStringPart::Wildcard(w) = wp {
                match w {
                    &mut Wildcard::NEONType(_, _, ref maybe_suffix_kind) => {
                        if let Some(suffix_kind) = maybe_suffix_kind {
                            let x = ctx.provide_type_wildcard(w).unwrap();
                            *wp = WildStringPart::String(make_neon_suffix(x, *suffix_kind))
                        } else {
                            *wp = WildString::make_default_build(ctx, repr, w)
                        }
                    }
                    _ => *wp = WildString::make_default_build(ctx, repr, w),
                }
            }
            Ok(())
        })
    }

    pub fn build(&mut self, ctx: &LocalContext, repr: TypeRepr) -> Result<(), String> {
        match repr {
            TypeRepr::ACLENotation | TypeRepr::LLVMMachine => {
                self.iter_mut().try_for_each(|wp| -> Result<(), String> {
                    if let WildStringPart::Wildcard(w) = wp {
                        match w {
                            &mut Wildcard::NEONType(_, _, ref maybe_suffix_kind) => {
                                if let Some(suffix_kind) = maybe_suffix_kind {
                                    let x = ctx.provide_type_wildcard(w).unwrap();
                                    *wp = WildStringPart::String(make_neon_suffix(x, *suffix_kind))
                                } else {
                                    *wp = WildString::make_default_build(ctx, repr, w)
                                }
                            }
                            _ => *wp = WildString::make_default_build(ctx, repr, w),
                        }
                    }
                    Ok(())
                })
            }
            _ => self.iter_mut().try_for_each(|wp| -> Result<(), String> {
                if let WildStringPart::Wildcard(w) = wp {
                    *wp = WildString::make_default_build(ctx, repr, w);
                }
                Ok(())
            }),
        }
    }

    fn make_default_build(ctx: &LocalContext, repr: TypeRepr, w: &mut Wildcard) -> WildStringPart {
        WildStringPart::String(
            ctx.provide_substitution_wildcard(w)
                .or_else(|_| ctx.provide_type_wildcard(w).map(|ty| ty.repr(repr)))
                .unwrap(),
        )
    }
}

impl From<String> for WildString {
    fn from(s: String) -> Self {
        WildString(vec![WildStringPart::String(s)])
    }
}

impl FromStr for WildString {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        enum State {
            Normal { start: usize },
            Wildcard { start: usize, count: usize },
            EscapeTokenOpen { start: usize, at: usize },
            EscapeTokenClose { start: usize, at: usize },
        }

        let mut ws = WildString::default();
        match s
            .char_indices()
            .try_fold(State::Normal { start: 0 }, |state, (idx, ch)| {
                match (state, ch) {
                    (State::Normal { start }, '{') => Ok(State::EscapeTokenOpen { start, at: idx }),
                    (State::Normal { start }, '}') => {
                        Ok(State::EscapeTokenClose { start, at: idx })
                    }
                    (State::EscapeTokenOpen { start, at }, '{')
                    | (State::EscapeTokenClose { start, at }, '}') => {
                        if start < at {
                            ws.push_str(&s[start..at])
                        }

                        Ok(State::Normal { start: idx })
                    }
                    (State::EscapeTokenOpen { at, .. }, '}') => Err(format!(
                        "empty wildcard given in string {s:?} at position {at}"
                    )),
                    (State::EscapeTokenOpen { start, at }, _) => {
                        if start < at {
                            ws.push_str(&s[start..at])
                        }

                        Ok(State::Wildcard {
                            start: idx,
                            count: 0,
                        })
                    }
                    (State::EscapeTokenClose { at, .. }, _) => Err(format!(
                        "closing a non-wildcard/bad escape in string {s:?} at position {at}"
                    )),
                    // Nesting wildcards is only supported for `{foo as {bar}}`, wildcards cannot be
                    // nested at the start of a WildString.
                    (State::Wildcard { start, count }, '{') => Ok(State::Wildcard {
                        start,
                        count: count + 1,
                    }),
                    (State::Wildcard { start, count: 0 }, '}') => {
                        ws.push_wildcard(s[start..idx].parse()?);
                        Ok(State::Normal { start: idx + 1 })
                    }
                    (State::Wildcard { start, count }, '}') => Ok(State::Wildcard {
                        start,
                        count: count - 1,
                    }),
                    (state @ State::Normal { .. }, _) | (state @ State::Wildcard { .. }, _) => {
                        Ok(state)
                    }
                }
            })? {
            State::Normal { start } => {
                if start < s.len() {
                    ws.push_str(&s[start..]);
                }

                Ok(ws)
            }
            State::EscapeTokenOpen { at, .. } | State::Wildcard { start: at, .. } => Err(format!(
                "unclosed wildcard in string {s:?} at position {at}"
            )),
            State::EscapeTokenClose { at, .. } => Err(format!(
                "closing a non-wildcard/bad escape in string {s:?} at position {at}"
            )),
        }
    }
}

impl fmt::Display for WildString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            self.0
                .iter()
                .map(|part| match part {
                    WildStringPart::String(s) => s.to_owned(),
                    WildStringPart::Wildcard(w) => format!("{{{w}}}"),
                })
                .join("")
        )
    }
}

impl ToTokens for WildString {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        assert!(
            !self.has_wildcards(),
            "cannot convert string with wildcards {self:?} to TokenStream"
        );
        let str = self.to_string();
        tokens.append_all(quote! { #str })
    }
}

#[cfg(test)]
mod tests {
    use crate::typekinds::*;
    use crate::wildstring::*;

    #[test]
    fn test_empty_string() {
        let ws: WildString = "".parse().unwrap();
        assert_eq!(ws.0.len(), 0);
    }

    #[test]
    fn test_plain_string() {
        let ws: WildString = "plain string".parse().unwrap();
        assert_eq!(ws.0.len(), 1);
        assert_eq!(
            ws,
            WildString(vec![WildStringPart::String("plain string".to_string())])
        )
    }

    #[test]
    fn test_escaped_curly_brackets() {
        let ws: WildString = "VALUE = {{value}}".parse().unwrap();
        assert_eq!(ws.to_string(), "VALUE = {value}");
        assert!(!ws.has_wildcards());
    }

    #[test]
    fn test_escaped_curly_brackets_wildcard() {
        let ws: WildString = "TYPE = {{{type}}}".parse().unwrap();
        assert_eq!(ws.to_string(), "TYPE = {{type}}");
        assert_eq!(ws.0.len(), 4);
        assert!(ws.has_wildcards());
    }

    #[test]
    fn test_wildcard_right_boundary() {
        let s = "string test {type}";
        let ws: WildString = s.parse().unwrap();
        assert_eq!(&ws.to_string(), s);
        assert!(ws.has_wildcards());
    }

    #[test]
    fn test_wildcard_left_boundary() {
        let s = "{type} string test";
        let ws: WildString = s.parse().unwrap();
        assert_eq!(&ws.to_string(), s);
        assert!(ws.has_wildcards());
    }

    #[test]
    fn test_recursive_wildcard() {
        let s = "string test {type[0] as {type[1]}}";
        let ws: WildString = s.parse().unwrap();

        assert_eq!(ws.0.len(), 2);
        assert_eq!(
            ws,
            WildString(vec![
                WildStringPart::String("string test ".to_string()),
                WildStringPart::Wildcard(Wildcard::Scale(
                    Box::new(Wildcard::Type(Some(0))),
                    Box::new(TypeKind::Wildcard(Wildcard::Type(Some(1)))),
                ))
            ])
        );
    }

    #[test]
    fn test_scale_wildcard() {
        let s = "string {type[0] as i8} test";
        let ws: WildString = s.parse().unwrap();

        assert_eq!(ws.0.len(), 3);
        assert_eq!(
            ws,
            WildString(vec![
                WildStringPart::String("string ".to_string()),
                WildStringPart::Wildcard(Wildcard::Scale(
                    Box::new(Wildcard::Type(Some(0))),
                    Box::new(TypeKind::Base(BaseType::Sized(BaseTypeKind::Int, 8))),
                )),
                WildStringPart::String(" test".to_string())
            ])
        );
    }

    #[test]
    fn test_solitaire_wildcard() {
        let ws: WildString = "{type}".parse().unwrap();
        assert_eq!(ws.0.len(), 1);
        assert_eq!(
            ws,
            WildString(vec![WildStringPart::Wildcard(Wildcard::Type(None))])
        )
    }

    #[test]
    fn test_empty_wildcard() {
        "string {}"
            .parse::<WildString>()
            .expect_err("expected parse error");
    }

    #[test]
    fn test_invalid_open_wildcard_right() {
        "string {"
            .parse::<WildString>()
            .expect_err("expected parse error");
    }

    #[test]
    fn test_invalid_close_wildcard_right() {
        "string }"
            .parse::<WildString>()
            .expect_err("expected parse error");
    }

    #[test]
    fn test_invalid_open_wildcard_left() {
        "{string"
            .parse::<WildString>()
            .expect_err("expected parse error");
    }

    #[test]
    fn test_invalid_close_wildcard_left() {
        "}string"
            .parse::<WildString>()
            .expect_err("expected parse error");
    }

    #[test]
    fn test_consecutive_wildcards() {
        let s = "svprf{size_literal[1]}_gather_{type[0]}{index_or_offset}";
        let ws: WildString = s.parse().unwrap();
        assert_eq!(ws.to_string(), s)
    }
}