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
//! Macros for crate [`laps`](https://crates.io/crates/laps),
//! including derive macros and other helper macros.
use TokenStream;
use result_to_tokens;
/// Generates the `Parse` trait implementation.
///
/// # Helper Attributes
///
/// * `#[token(type)]`: implements `Parse` trait for token streams that
/// produce tokens of the given type.
/// * `#[starts_with(token_ast0, token_ast1, ...)]`: specifies which tokens
/// the current AST may start with. This will affect the implementation of
/// method `maybe` of the `Parse` trait.
/// Generates the `Spanned` trait implementation.
///
/// # `#[try_span]`
///
/// Tells the macro that a field implements the `TrySpan` trait.
/// This may be helpful when:
///
/// ```
/// # use laps_macros::Spanned;
/// # mod laps {
/// # pub mod span {
/// # pub type Result<T> = std::result::Result<T, ()>;
/// # pub struct Span;
/// # impl Span {
/// # pub fn into_end_updated(self, span: Self) -> Self { todo!() }
/// # }
/// # pub trait Spanned {
/// # fn span(&self) -> Span;
/// # }
/// # pub trait TrySpan {
/// # fn try_span(&self) -> Option<Span>;
/// # }
/// # impl<T> TrySpan for T where T: Spanned {
/// # fn try_span(&self) -> Option<Span> { todo!() }
/// # }
/// # impl<T> TrySpan for Option<T> where T: TrySpan {
/// # fn try_span(&self) -> Option<Span> { todo!() }
/// # }
/// # }
/// # }
/// # struct Atom;
/// # impl laps::span::Spanned for Atom {
/// # fn span(&self) -> laps::span::Span { todo!() }
/// # }
/// # type ReturnKeyword = Atom;
/// # type Value = Atom;
/// #[derive(Spanned)]
/// struct Return {
/// ret: ReturnKeyword,
/// #[try_span]
/// value: Option<Value>,
/// }
/// ```
///
/// The following deriving fails to compile:
///
/// ```compile_fail
/// # use laps_macros::Spanned;
/// # mod laps {
/// # pub mod span {
/// # pub type Result<T> = std::result::Result<T, ()>;
/// # pub struct Span;
/// # impl Span {
/// # pub fn into_end_updated(self, span: Self) -> Self { todo!() }
/// # }
/// # pub trait Spanned {
/// # fn span(&self) -> Span;
/// # }
/// # pub trait TrySpan {
/// # fn try_span(&self) -> Option<Span>;
/// # }
/// # impl<T> TrySpan for T where T: Spanned {
/// # fn try_span(&self) -> Option<Span> { todo!() }
/// # }
/// # impl<T> TrySpan for Option<T> where T: TrySpan {
/// # fn try_span(&self) -> Option<Span> { todo!() }
/// # }
/// # }
/// # }
/// # struct Atom;
/// # impl laps::span::Spanned for Atom {
/// # fn span(&self) -> laps::span::Span { todo!() }
/// # }
/// # type ReturnKeyword = Atom;
/// # type Value = Atom;
/// #[derive(Spanned)]
/// struct Return {
/// ret: ReturnKeyword,
/// value: Option<Value>,
/// }
/// ```
/// Generates the `Tokenize` trait implementation for token kinds. This macro
/// can only be applied to `enum`s.
///
/// # Helper Attributes
///
/// * `#[char_type(type)]`: optional, specifies `CharType` of `Tokenize` trait.
/// Defaults to [`char`], and can only be [`char`] or [`u8`].
/// * `#[enable_par(true/false)]`: optional, set to `true` to generate lexer in
/// parallel, `false` to disable parallelization. Defaults to automatic
/// selection.
/// * `#[regex(regex [, parser])]`: marks a enum variant can be matched by the
/// given regular expression. The `parser` parameter is optional, which is a
/// function that converts a <code>&[str]</code> (`char_type` = [`char`]) or
/// a <code>&[[u8]]</code> (`char_type` = [`u8`]) to [`Option<T>`], which `T`
/// is type of the tuple field of this variant. If `parser` is omitted,
/// [`std::str::FromStr`] will be called.
/// * `#[skip(regex)]`: marks a enum variant can be matched by the
/// given regular expression, and it should be skipped.
/// * `#[eof]`: marks a enum variant should be returned when the tokenizer
/// encountered end-of-file.
///
/// # Notes
///
/// The variants that appear first will be matched first.
/// Implements [`From`], [`TryFrom`] and [`Display`](std::fmt::Display)
/// trait for token kind enums.
///
/// The [`From`] and [`TryFrom`] trait will only be implemented for variants
/// with single unnamed field.
///
/// # Examples
///
/// ```
/// # use laps_macros::token_kind;
/// #[token_kind]
/// enum TokenKind {
/// /// String literal.
/// Str(String),
/// /// Integer literal.
/// Int(i32),
/// /// End-of-file.
/// Eof,
/// }
/// ```
///
/// will be expanded to:
///
/// ```
/// #[derive(Clone, PartialEq)]
/// enum TokenKind {
/// // ...
/// # Str(String),
/// # Int(i32),
/// # Eof,
/// }
///
/// impl From<String> for TokenKind {
/// fn from(s: String) -> Self {
/// Self::Str(s)
/// }
/// }
///
/// impl TryFrom<TokenKind> for String {
/// type Error = ();
/// fn try_from(kind: TokenKind) -> Result<Self, Self::Error> {
/// match kind {
/// TokenKind::Str(s) => Ok(s),
/// _ => Err(()),
/// }
/// }
/// }
///
/// impl<'a> TryFrom<&'a TokenKind> for &'a String {
/// type Error = ();
/// fn try_from(kind: &'a TokenKind) -> Result<Self, Self::Error> {
/// match kind {
/// TokenKind::Str(s) => Ok(s),
/// _ => Err(()),
/// }
/// }
/// }
///
/// // Same for `TokenKind::Int`.
/// // ...
///
/// impl std::fmt::Display for TokenKind {
/// fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
/// match self {
/// Self::Str(s) => write!(f, "string literal `{s}`"),
/// Self::Int(i) => write!(f, "integer literal `{i}`"),
/// Self::Eof => write!(f, "end-of-file"),
/// }
/// }
/// }
/// ```
/// Generates ASTs for tokens, also generates a macro
/// for referencing AST types.
///
/// The generated ASTs can be parsed from token stream that produces
/// `laps::tokens::Token` with the given type as its kind.
///
/// # Examples
///
/// ```
/// # use laps_macros::token_ast;
/// # mod laps {
/// # pub mod span {
/// # pub type Result<T> = std::result::Result<T, ()>;
/// # pub struct Span;
/// # pub trait Spanned {
/// # fn span(&self) -> Span;
/// # }
/// # }
/// # pub mod token {
/// # #[derive(Clone, Debug, PartialEq, Eq, Hash)]
/// # pub struct Token<Kind> {
/// # pub kind: Kind,
/// # pub span: (),
/// # }
/// # impl<Kind> super::span::Spanned for Token<Kind> {
/// # fn span(&self) -> super::span::Span { super::span::Span }
/// # }
/// # impl<Kind> AsRef<Kind> for Token<Kind> {
/// # fn as_ref(&self) -> &Kind {
/// # &self.kind
/// # }
/// # }
/// # pub trait TokenStream {
/// # type Token;
/// # fn next_token(&mut self) -> super::span::Result<Self::Token>;
/// # fn peek(&mut self) -> super::span::Result<Self::Token>;
/// # fn expect<T>(&mut self, _: T) -> super::span::Result<Self::Token>;
/// # }
/// # }
/// # pub mod parse {
/// # pub trait Parse<TS>: Sized {
/// # fn parse(_: &mut TS) -> super::span::Result<Self>;
/// # fn maybe(_: &mut TS) -> super::span::Result<bool>;
/// # }
/// # }
/// # macro_rules! return_error {
/// # ($span:expr, $($arg:tt)+) => {
/// # return Err(())
/// # };
/// # }
/// # pub(crate) use return_error;
/// # }
/// # fn main() {}
/// #[derive(Clone, Debug, PartialEq, Eq, Hash)]
/// enum TokenKind {
/// /// String literal.
/// Str(String),
/// /// Integer literal.
/// Int(i32),
/// /// Other character.
/// Other(char),
/// /// End-of-file.
/// Eof,
/// }
///
/// // Declare ASTs and there name, define macro `Token` for referencing ASTs.
/// // You can use `Token![..]` to represent the generated ASTs,
/// // such as `Token![str]`, `Token![+]`, ...
/// // All of the generated ASTs are single-field structures, you can access
/// // the inner token by using `ast.0`.
/// token_ast! {
/// // optional, all derives will be applied to the generated AST structures.
/// #[derive(Debug, PartialEq)]
/// pub(crate) macro Token</* specify the token kind type here */ TokenKind> {
/// // pattern, and prompt for error messages
/// [str] => { kind: TokenKind::Str(_), prompt: "string literal" },
/// [int] => { kind: TokenKind::Int(_), prompt: "integer literal" },
/// [0] => { kind: TokenKind::Int(i) if *i == 0, prompt: "zero" },
/// // use default prompt of the token kind
/// [+] => { kind: TokenKind::Other('+') },
/// [-] => { kind: TokenKind::Other('-') },
/// [*] => { kind: TokenKind::Other('*') },
/// [/] => { kind: TokenKind::Other('/') },
/// [eof] => { kind: TokenKind::Eof },
/// }
/// }
/// ```