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

macro_rules! str_lit {
    ($(#[$attr:meta])* $vis:vis struct $name:ident($t:ty) $(: $kw:ident)?) => {
        #[derive(Clone, Debug)]
        $vis struct $name {
            $vis values: Vec<$t>,
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                for (i, value) in self.values.iter().enumerate() {
                    if i > 0 {
                        f.write_str(" ")?;
                    }

                    $(
                        f.write_str(stringify!($kw))?;
                    )?
                    f.write_str(&value.value())?;
                }
                Ok(())
            }
        }

        impl Parse for $name {
            fn parse(input: ParseStream<'_>) -> Result<Self> {
                let mut values = Vec::new();
                let mut first = true;
                while first || Self::peek(&input.lookahead1()) {
                    first = false;
                    values.push(input.parse()?);
                }
                Ok(Self { values })
            }
        }

        impl Spanned for $name {
            fn span(&self) -> Span {
                self.values.span()
            }

            fn set_span(&mut self, span: Span) {
                self.values.set_span(span);
            }
        }

        impl $name {
            pub fn peek(lookahead: &Lookahead1<'_>) -> bool {
                $(lookahead.peek(kw::$kw) || )? lookahead.peek(syn::LitStr)
            }

            pub fn parse_opt(input: ParseStream<'_>) -> Result<Option<Self>> {
                if Self::peek(&input.lookahead1()) {
                    input.parse().map(Some)
                } else {
                    Ok(None)
                }
            }

            pub fn value(&self) -> String {
                self.values.iter().map(|v| v.value()).collect()
            }
        }
    };
}

macro_rules! wrap_str {
    ($(#[$attr:meta])* $vis:vis struct $name:ident { $token:ident : kw::$kw:ident $(,)? }) => {
        $(#[$attr])*
        #[derive(Clone)]
        $vis struct $name {
            /// The prefix of the string.
            $vis $token: kw::$kw,
            /// The string literal.
            $vis value: syn::LitStr,
        }

        impl fmt::Debug for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.debug_struct(stringify!($name))
                    .field("value", &self.value)
                    .finish()
            }
        }

        impl Deref for $name {
            type Target = syn::LitStr;

            #[inline]
            fn deref(&self) -> &Self::Target {
                &self.value
            }
        }

        impl DerefMut for $name {
            #[inline]
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.value
            }
        }

        impl Parse for $name {
            fn parse(input: ParseStream<'_>) -> Result<Self> {
                Ok(Self {
                    $token: input.parse()?,
                    value: input.parse()?,
                })
            }
        }

        impl Spanned for $name {
            fn span(&self) -> Span {
                let span = self.$token.span;
                span.join(self.value.span()).unwrap_or(span)
            }

            fn set_span(&mut self, span: Span) {
                self.$token.span = span;
                self.value.set_span(span);
            }
        }
    };
}

str_lit! {
    /// A string literal.
    pub struct LitStr(syn::LitStr)
}

str_lit! {
    /// A unicode string literal.
    pub struct LitUnicodeStr(UnicodeStr): unicode
}

wrap_str! {
    /// A unicode string.
    pub struct UnicodeStr {
        unicode_token: kw::unicode,
    }
}

str_lit! {
    /// A hex string literal.
    pub struct LitHexStr(HexStr): hex
}

wrap_str! {
    /// A hex string.
    pub struct HexStr {
        hex_token: kw::hex,
    }
}