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
#[cfg(doc)]
use crate::*;

/// This macro supports the definition of enums, tuple structs and normal structs and
/// generates [`Parser`] and [`ToTokens`] implementations for them. It will implement `Debug`
/// and `Display` if the `impl_debug` and `impl_display` features are
/// enabled. Generics/Lifetimes are not supported (yet). Note: eventually a derive macro for
/// `Parser` and `ToTokens` will become supported by a 'unsynn-derive' crate to give finer
/// control over the expansion. `#[derive(Copy, Clone)]` have to be manually defined. `Debug`
/// and `Display` are automatically implemented when the respective features are enabled.
///
/// Common for all three variants is that entries are tried in order. Disjunctive for enums
/// and conjunctive in structures. This makes the order important, e.g. for enums, in case
/// some entries are subsets of others.
///
/// # Example
///
/// ```
/// # use unsynn::*;
/// // Define some types
/// unsynn!{
///     enum MyEnum {
///         Ident(Ident),
///         Braced(BraceGroup),
///         Text(LiteralString),
///         Number(LiteralInteger),
///     }
///
///     struct MyStruct {
///         text: LiteralString,
///         number: LiteralInteger,
///     }
///
///     struct MyTupleStruct(Ident, LiteralString);
/// }
///
/// // Create an iterator over the things we want to parse
/// let mut token_iter = r#"
///     ident { within brace } "literal string" 1234
///     "literal string" 1234
///     ident "literal string"
/// "#.to_token_iter();
///
/// // Use the defined types
/// let MyEnum::Ident(_) = MyEnum::parse(&mut token_iter).unwrap() else { panic!()};
/// let MyEnum::Braced(_) = MyEnum::parse(&mut token_iter).unwrap() else { panic!()};
/// let MyEnum::Text(_) = MyEnum::parse(&mut token_iter).unwrap() else { panic!()};
/// let MyEnum::Number(_) = MyEnum::parse(&mut token_iter).unwrap() else { panic!()};
///
/// let my_struct =  MyStruct::parser(&mut token_iter).unwrap();
///
/// let my_tuple_struct =  MyTupleStruct::parser(&mut token_iter).unwrap();
/// ```
#[cfg(doc)]
#[macro_export]
macro_rules! unsynn {
    (enum $name:ident { $( $variant:ident($parser:ty) ),* }) => {};
    (struct $name:ident { $( $member:ident: $parser:ty ),* }) => {};
    (struct $name:ident ( $( $parser:ty ),*);) => {};
}

#[doc(hidden)]
#[cfg(not(doc))]
#[macro_export]
macro_rules! unsynn{
    ($(#[$attribute:meta])* $pub:vis enum $name:ident {
        $($(#[$vattr:meta])* $variant:ident($parser:ty)),* $(,)?
    } $($cont:tt)*) => {
        #[cfg_attr(feature = "impl_debug", derive(Debug))]
        $(#[$attribute])* $pub enum $name {
            $($(#[$vattr])* $variant($parser)),*
        }

        impl Parser for $name {
            fn parser(tokens: &mut TokenIter) -> Result<Self> {
                $(
                    if let Ok(parsed) = <$parser>::parse(tokens) {
                        return Ok($name::$variant(parsed));
                    }
                )*
                    match tokens.next() {
                        Some(token) => $crate::Error::unexpected_token(token),
                        None => $crate::Error::unexpected_end()
                    }
            }
        }

        impl ToTokens for $name {
            fn to_tokens(&self, tokens: &mut TokenStream) {
                match self {
                    $(
                        $name::$variant(matched) => matched.to_tokens(tokens),
                    )*
                }
            }
        }

        #[cfg(feature = "impl_display")]
        impl std::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                match self {
                    $(
                        $name::$variant(matched) => write!(f, "{matched} "),
                    )*
                }
            }
        }
        $crate::unsynn!{$($cont)*}
    };

    ($(#[$attribute:meta])* $pub:vis struct $name:ident {
        $($(#[$mattr:meta])* $mpub:vis $member:ident: $parser:ty),* $(,)?
    } $($cont:tt)*) => {
        #[cfg_attr(feature = "impl_debug", derive(Debug))]
        $(#[$attribute])* $pub struct $name {
            $($(#[$mattr])* $mpub $member : $parser),*
        }

        impl Parser for $name {
            fn parser(tokens: &mut TokenIter) -> Result<Self> {
                Ok(Self{$($member: <$parser>::parser(tokens)?),*})
            }
        }

        impl ToTokens for $name {
            fn to_tokens(&self, tokens: &mut TokenStream) {
                $(self.$member.to_tokens(tokens);)*
            }
        }

        #[cfg(feature = "impl_display")]
        impl std::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                $(write!(f, "{} ", &self.$member);)*
                    Ok(())
            }
        }
        $crate::unsynn!{$($cont)*}
    };

    ($(#[$attribute:meta])* $pub:vis struct $name:ident (
        $($(#[$mattr:meta])* $mpub:vis $parser:ty),* $(,)?
    ); $($cont:tt)*) => {
        #[cfg_attr(feature = "impl_debug", derive(Debug))]
        $(#[$attribute])* $pub struct $name (
            $($(#[$mattr])* $mpub $parser),*
        );

        impl Parser for $name {
            fn parser(tokens: &mut TokenIter) -> Result<Self> {
                Ok(Self($(<$parser>::parser(tokens)?),*))
            }
        }

        impl ToTokens for $name {
            fn to_tokens(&self, tokens: &mut TokenStream) {
                $crate::unsynn!{@tuple_to_tokens $name(self, tokens) $($parser),*}
            }
        }

        #[cfg(feature = "impl_display")]
        impl std::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                $crate::unsynn!{@tuple_write $name(self, f) $($parser),*}
                Ok(())
            }
        }
        $crate::unsynn!{$($cont)*}
    };

    // terminate recursion
    () => {};

    // For the tuple struct ToTokens impl we need to match each tuple member and call to_tokens on it
    (@tuple_to_tokens $name:ident($this:ident,$param:ident) $element:ty $(,$rest:ty)* $(,)?) => {
        $crate::unsynn!{@tuple_to_tokens $name($this,$param) $($rest),*}
        let $name($($crate::unsynn!{@_ $rest},)*  that, .. ) = $this;
        that.to_tokens($param);
    };
    (@tuple_to_tokens $name:ident($this:ident,$param:ident)) => {};

    // same for write
    (@tuple_write $name:ident($this:ident,$f:ident) $element:ty $(,$rest:ty)* $(,)?) => {
        $crate::unsynn!{@tuple_write $name($this,$f) $($rest),*}
        let $name($($crate::unsynn!{@_ $rest},)*  that, .. ) = $this;
        write!($f, "{} ", &that)?;
    };
    (@tuple_write $name:ident($this:ident,$f:ident)) => {};

    // replaces a single token with a underscore
    (@_ $unused:tt) => {_};
}

/// Define types matching keywords.
///
/// `keyword!{ Name = "identifier", ...}`
///
/// * `Name` is the name for the (`struct Name(Cached<Ident>)`) to be generated
/// * `"identifier"` is the case sensitive keyword
/// * keywords are always defined as `pub`
///
/// `Name::parse()` will then only match the defined identifier.  It will implement `Debug`
/// and `Display` if the `impl_debug` and `impl_display` features are enabled. `Clone` is
/// always implemented for keywords. Additionally `AsRef<str>` is implemented for each Keyword
/// to access the identifier string from rust code.
///
/// # Example
///
/// ```
/// # use unsynn::*;
/// keyword!{
///     /// Optional documentation for `If`
///     If = "if",
///     Else = "else",
/// }
///
/// let mut tokens = "if else".to_token_iter();
/// let if_kw = If::parse(&mut tokens).unwrap();
/// assert_eq!(if_kw.as_ref(), "if");
/// let else_kw = Else::parse(&mut tokens).unwrap();
/// assert_eq!(else_kw.as_ref(), "else");
/// ```
#[macro_export]
macro_rules! keyword{
    ($($(#[$attribute:meta])* $name:ident = $str:literal),*$(,)?) => {
        $(
            $(#[$attribute])*
            #[cfg_attr(feature = "impl_debug", derive(Debug))]
            #[derive(Clone)]
            pub struct $name;

            impl Parser for $name {
                fn parser(tokens: &mut TokenIter) -> Result<Self> {
                    use $crate::Parse;
                    $crate::CachedIdent::parse_with(tokens, |ident| {
                        if ident == $str {
                            Ok($name)
                        } else {
                            $crate::Error::other::<$name>(
                                format!(
                                    "keyword {:?} expected, got {:?} at {:?}",
                                    $str,
                                    ident.string(),
                                    ident.span().start()
                                )
                            )
                        }
                    })
                }
            }

            impl ToTokens for $name {
                fn to_tokens(&self, tokens: &mut TokenStream) {
                    $crate::Ident::new($str, $crate::Span::call_site()).to_tokens(tokens);
                }
            }

            impl AsRef<str> for $name {
                fn as_ref(&self) -> &str {
                    &$str
                }
            }

            #[cfg(feature = "impl_display")]
            impl std::fmt::Display for $name {
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    write!(f, "{} ", $str)
                }
            }
        )*
    }
}