literalext 0.2.0

A helper crate for interpreting proc-macro `Literal` values
Documentation
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use {RawInt, IntLit, FloatLit};

use std::char;
use std::ops::{Index, RangeFrom};
use std::ascii::AsciiExt;

/// Filter the input string, removing all bytes which match the given input
/// byte in place, without allocation.
///
/// # Panics
///
/// Panics if the filter byte is not a valid ASCII character.
fn string_filter(string: String, remove: u8) -> String {
    assert!(remove.is_ascii());
    let mut bytes = string.into_bytes();
    let mut write = 0;
    for read in 0..bytes.len() {
        if bytes[read] == remove {
            continue; // Don't increase write
        } else if write != read {
            let x = bytes[read];
            bytes[write] = x;
        }
        write += 1;
    }
    bytes.truncate(write);
    String::from_utf8(bytes)
        .expect("Transformation should not have impacted utf-8 validity")
}

#[cfg(test)]
#[test]
fn test_string_filter() {
    assert_eq!(string_filter("1921381902_1231___23___333".to_string(), b'_'),
               "1921381902123123333");

    assert_eq!(string_filter("_________".to_string(), b'_'), "");
    assert_eq!(string_filter("12837912837192837129387129_".to_string(), b'_'),
               "12837912837192837129387129");
    assert_eq!(string_filter("_12837912837192837129387129".to_string(), b'_'),
               "12837912837192837129387129");
}

/// Get the byte at offset idx, or a default of `b'\0'` if we're looking past
/// the end of the input buffer.
fn byte<S: AsRef<[u8]> + ?Sized>(s: &S, idx: usize) -> u8 {
    let s = s.as_ref();
    if idx < s.len() {
        s[idx]
    } else {
        0
    }
}

fn next_chr(s: &str) -> char {
    s.chars().next().unwrap_or('\0')
}

fn raw_str(s: &str) -> &str {
    // NOTE: This makes very very strong assumptions about the formatting of raw
    // string literals. If you pass a malformed literal into this function it is
    // very likely to panic or do the wrong thing.
    let begin = s.find('"').expect("Raw string must begin with \" char");
    let end = s.rfind('"').expect("Raw string must end with \" char");
    &s[begin + 1..end]
}

fn backslash_x<S>(s: &S) -> (&S, u8)
    where S: Index<RangeFrom<usize>, Output=S> + AsRef<[u8]> + ?Sized
{
    let mut ch = 0;
    let b0 = byte(s, 0);
    let b1 = byte(s, 1);
    ch += 0x10 * match b0 {
        b'0'...b'9' => b0 - b'0',
        b'a'...b'f' => 10 + (b0 - b'a'),
        b'A'...b'F' => 10 + (b0 - b'A'),
        _ => panic!("unexpected non-hex character after \\x"),
    };
    ch += 0x1 * match b1 {
        b'0'...b'9' => b1 - b'0',
        b'a'...b'f' => 10 + (b1 - b'a'),
        b'A'...b'F' => 10 + (b1 - b'A'),
        _ => panic!("unexpected non-hex character after \\x"),
    };
    (&s[2..], ch)
}

fn backslash_u(mut s: &str) -> (&str, char) {
    if byte(s, 0) != b'{' {
        panic!("expected {{ after \\u");
    }
    s = &s[1..];

    let mut ch = 0;
    for _ in 0..6 {
        let b = byte(s, 0);
        match b {
            b'0'...b'9' => {
                ch *= 0x10;
                ch += (b - b'0') as u32;
                s = &s[1..];
            }
            b'a'...b'f' => {
                ch *= 0x10;
                ch += (10 + b - b'a') as u32;
                s = &s[1..];
            }
            b'A'...b'F' => {
                ch *= 0x10;
                ch += (10 + b - b'A') as u32;
                s = &s[1..];
            }
            b'}' => break,
            _ => panic!("unexpected non-hex character after \\u"),
        }
    }
    assert!(byte(s, 0) == b'}');
    s = &s[1..];

    if let Some(ch) = char::from_u32(ch) {
        (s, ch)
    } else {
        panic!("character code {:x} is not a valid unicode character", ch);
    }
}

pub(crate) fn str_lit(mut s: &str) -> Option<String> {
    match byte(s, 0) {
        b'"' => {
            s = &s[1..]
        }
        b'r' => {
            return Some(raw_str(s).to_string());
        }
        _ => return None,
    }

    let mut out = String::new();
    'outer: loop {
        let ch = match byte(s, 0) {
            b'"' => break,
            b'\\' => {
                let b = byte(s, 1);
                s = &s[2..];
                match b {
                    b'x' => {
                        let (rest, byte) = backslash_x(s);
                        s = rest;
                        assert!(byte <= 0x80, "Invalid \\x byte in string literal");
                        char::from_u32(byte as u32).unwrap()
                    }
                    b'u' => {
                        let (rest, chr) = backslash_u(&s);
                        s = rest;
                        chr
                    }
                    b'n' => '\n',
                    b'r' => '\r',
                    b't' => '\t',
                    b'\\' => '\\',
                    b'0' => '\0',
                    b'\'' => '\'',
                    b'"' => '"',
                    b'\r' | b'\n' => {
                        loop {
                            let ch = next_chr(s);
                            if ch.is_whitespace() {
                                s = &s[ch.len_utf8()..];
                            } else {
                                continue 'outer;
                            }
                        }
                    }
                    b => {
                        panic!("unexpected byte {:?} after \\ character in byte literal", b)
                    }
                }
            }
            b'\r' => {
                assert_eq!(byte(s, 1), b'\n', "Bare CR not allowed in string");
                s = &s[2..];
                '\n'
            }
            _ => {
                let ch = next_chr(s);
                s = &s[ch.len_utf8()..];
                ch
            }
        };
        out.push(ch);
    }

    assert_eq!(s, "\"");
    return Some(out);
}

pub(crate) fn byte_str_lit(mut s: &str) -> Option<Vec<u8>> {
    match (byte(s, 0), byte(s, 1)) {
        (b'b', b'"') => {
            s = &s[2..];
        }
        (b'b', b'r') => {
            return Some(raw_str(s).as_bytes().to_vec());
        }
        _ => return None,
    }
    // We're going to want to have slices which don't respect codepoint boundaries.
    let mut s = s.as_bytes();

    let mut out = Vec::new();
    'outer: loop {
        let byte = match byte(s, 0) {
            b'"' => break,
            b'\\' => {
                let b = byte(s, 1);
                s = &s[2..];
                match b {
                    b'x' => {
                        let (rest, b) = backslash_x(s);
                        s = rest;
                        b
                    }
                    b'n' => b'\n',
                    b'r' => b'\r',
                    b't' => b'\t',
                    b'\\' => b'\\',
                    b'0' => b'\0',
                    b'\'' => b'\'',
                    b'"' => b'"',
                    b'\r' | b'\n' => {
                        loop {
                            let byte = byte(s, 0);
                            let ch = char::from_u32(byte as u32).unwrap();
                            if ch.is_whitespace() {
                                s = &s[1..];
                            } else {
                                continue 'outer;
                            }
                        }
                    }
                    b => {
                        panic!("unexpected byte {:?} after \\ character in byte literal", b)
                    }
                }
            }
            b'\r' => {
                assert_eq!(byte(s, 1), b'\n', "Bare CR not allowed in string");
                s = &s[2..];
                b'\n'
            }
            b => {
                s = &s[1..];
                b
            }
        };
        out.push(byte);
    }

    assert_eq!(s, b"\"");
    return Some(out);
}

pub(crate) fn char_lit(mut s: &str) -> Option<char> {
    if byte(s, 0) != b'\'' {
        return None;
    }
    s = &s[1..];

    let ch = match byte(s, 0) {
        b'\\' => {
            let b = byte(s, 1);
            s = &s[2..];
            match b {
                b'x' => {
                    let (rest, byte) = backslash_x(s);
                    s = rest;
                    assert!(byte <= 0x80, "Invalid \\x byte in string literal");
                    char::from_u32(byte as u32).unwrap()
                }
                b'u' => {
                    let (rest, chr) = backslash_u(s);
                    s = rest;
                    chr
                }
                b'n' => '\n',
                b'r' => '\r',
                b't' => '\t',
                b'\\' => '\\',
                b'0' => '\0',
                b'\'' => '\'',
                b'"' => '"',
                b => {
                    panic!("unexpected byte {:?} after \\ character in byte literal", b)
                }
            }
        }
        _ => {
            let ch = next_chr(s);
            s = &s[ch.len_utf8()..];
            ch
        }
    };
    assert_eq!(s, "\'", "Expected end of char literal");
    Some(ch)
}

pub(crate) fn byte_lit(s: &str) -> Option<u8> {
    if byte(s, 0) != b'b' || byte(s, 1) != b'\'' {
        return None;
    }
    // We're going to want to have slices which don't respect codepoint boundaries.
    let mut s = s[2..].as_bytes();

    let b = match byte(s, 0) {
        b'\\' => {
            let b = byte(s, 1);
            s = &s[2..];
            match b {
                b'x' => {
                    let (rest, b) = backslash_x(s);
                    s = rest;
                    b
                }
                b'n' => b'\n',
                b'r' => b'\r',
                b't' => b'\t',
                b'\\' => b'\\',
                b'0' => b'\0',
                b'\'' => b'\'',
                b'"' => b'"',
                b => {
                    panic!("unexpected byte {:?} after \\ character in byte literal", b)
                }
            }
        }
        b => {
            s = &s[1..];
            b
        }
    };

    assert!(byte(s, 0) == b'\'');
    Some(b)
}

pub(crate) fn int_lit(mut s: &str) -> Option<IntLit> {
    let base = match (byte(s, 0), byte(s, 1)) {
        (b'0', b'x') => {
            s = &s[2..];
            16
        }
        (b'0', b'o') => {
            s = &s[2..];
            8
        }
        (b'0', b'b') => {
            s = &s[2..];
            2
        }
        (b'0'...b'9', _) => 10,
        _ => return None,
    };

    let mut value: Option<RawInt> = Some(0);
    loop {
        let b = byte(s, 0);
        let digit = match b {
            b'0'...b'9' => (b - b'0') as RawInt,
            b'a'...b'f' if base > 10 => 10 + (b - b'a') as RawInt,
            b'A'...b'F' if base > 10 => 10 + (b - b'A') as RawInt,
            b'_' => {
                s = &s[1..];
                continue;
            }
            // NOTE: Looking at a floating point literal, we don't want to
            // consider these integers.
            b'.' if base == 10 => return None,
            b'e' | b'E' if base == 10 => return None,
            _ => break,
        };

        if digit >= base {
            panic!("Unexpected digit {:x} out of base range", digit);
        }

        value = value
            .and_then(|v| v.checked_mul(base))
            .and_then(|v| v.checked_add(digit));
        s = &s[1..];
    }

    // Check if the suffix is one of our legal suffixes, if it is, return an
    // equal 'static string which we can store in the IntLit object.
    let suffix = match s {
        "u8" => "u8",
        "i8" => "i8",
        "u16" => "u16",
        "i16" => "i16",
        "u32" => "u32",
        "i32" => "i32",
        "u64" => "u64",
        "i64" => "i64",
        "usize" => "usize",
        "isize" => "isize",
        "" => "",
        _ => return None,
    };

    assert_eq!(suffix, s);

    Some(IntLit {
        val: value,
        suffix: suffix,
    })
}

pub(crate) fn float_lit(input: String) -> Option<FloatLit> {
    match (byte(&input, 0), byte(&input, 1)) {
        (b'0', b'x') | (b'0', b'o') | (b'0', b'b') => return None,
        (b'0'...b'9', _) => {}
        _ => return None,
    };

    // Rust's floating point literals are very similar to the ones parsed by the
    // standard library, except that rust's literals can contain ignorable
    // underscores. Let's remove those underscores in-place.
    let input = string_filter(input, b'_');
    let mut s = &input[..];

    let mut has_dot = false;
    let mut has_exp = false;
    loop {
        match byte(s, 0) {
            b'0'...b'9' => {
                s = &s[1..];
            }
            b'.' => {
                s = &s[1..];
                if has_dot {
                    panic!("Unexpected second dot while parsing float literal");
                }
                has_dot = true;
            }
            b'e' | b'E' => {
                s = &s[1..];
                loop {
                    match byte(s, 0) {
                        b'+' | b'-' if !has_exp => {
                            s = &s[1..];
                        }
                        b'0'...b'9' => {
                            s = &s[1..];
                            has_exp = true;
                        }
                        b'_' => {
                            s = &s[1..];
                        }
                        _ => break,
                    }
                }
                assert!(has_exp,
                        "Unexpected end of float literal after `E` char");
                break;
            }
            _ => break,
        };
    }

    let suffix = match s {
        "f32" => "f32",
        "f64" => "f64",
        "" => "",
        _ => return None,
    };
    assert_eq!(suffix, s);

    // If we don't have an exponent or a . and the suffix is empty, then we're
    // looking at an integer literal. Don't parse it as a float.
    if !has_exp && !has_dot && suffix == "" {
        return None;
    }

    Some(FloatLit {
        val: input[..input.len() - suffix.len()].parse::<f64>().unwrap(),
        suffix: suffix,
    })
}

pub(crate) fn outer_doc(s: String) -> Option<String> {
    if s.starts_with("///") || s.starts_with("/**") {
        Some(s)
    } else {
        None
    }
}

pub(crate) fn inner_doc(s: String) -> Option<String> {
    if s.starts_with("//!") || s.starts_with("/*!") {
        Some(s)
    } else {
        None
    }
}