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
use crate::kw;
use proc_macro2::Span;
use std::fmt;
use syn::{
    parse::{Parse, ParseStream},
    Result,
};

/// A string literal.
#[derive(Clone)]
pub struct LitStr {
    pub unicode_token: Option<kw::unicode>,
    pub values: Vec<syn::LitStr>,
}

impl fmt::Debug for LitStr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LitStr")
            .field("unicode", &self.unicode_token.is_some())
            .field("values", &self.values)
            .finish()
    }
}

impl fmt::Display for LitStr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for value in &self.values {
            f.write_str(&value.value())?;
        }
        Ok(())
    }
}

impl Parse for LitStr {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        Ok(Self {
            unicode_token: input.parse()?,
            values: {
                let mut values = Vec::new();
                while !input.peek(syn::LitStr) {
                    values.push(input.parse()?);
                }
                if values.is_empty() {
                    return Err(input.parse::<syn::LitStr>().unwrap_err())
                }
                values
            },
        })
    }
}

impl LitStr {
    pub fn span(&self) -> Span {
        let mut span = if let Some(kw) = &self.unicode_token {
            kw.span
        } else {
            self.values.first().unwrap().span()
        };
        for value in &self.values {
            span = span.join(value.span()).unwrap_or(span);
        }
        span
    }

    pub fn set_span(&mut self, span: Span) {
        if let Some(kw) = &mut self.unicode_token {
            kw.span = span;
        }
        for value in &mut self.values {
            value.set_span(span);
        }
    }
}