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
use proc_macro2::{Span, TokenStream, TokenTree};
use quote::quote;
use std::borrow::Cow;
use syn::spanned::Spanned;
use syn::{Attribute, GenericParam, Ident, Lit, LitBool, Meta, Type};
use crate::error::Errors;
use crate::leaf::{Callback, InlineCallback};
use crate::util::{expect_punct, MaybeVoid};
use crate::LOGOS_ATTR;
mod definition;
mod error_type;
mod ignore_flags;
mod nested;
mod subpattern;
mod type_params;
pub use self::definition::{Definition, Literal};
pub use self::error_type::ErrorType;
pub use self::ignore_flags::IgnoreFlags;
use self::nested::{AttributeParser, Nested, NestedValue};
pub use self::subpattern::Subpatterns;
use self::type_params::{replace_lifetime, traverse_type, TypeParams};
#[derive(Default)]
pub struct Parser {
pub errors: Errors,
pub utf8_mode: Option<LitBool>,
pub skips: Vec<Definition>,
pub extras: MaybeVoid,
pub subpatterns: Vec<(Ident, Literal)>,
pub error_type: Option<ErrorType>,
pub logos_path: Option<TokenStream>,
pub export_path: Option<String>,
types: TypeParams,
}
impl Parser {
pub fn parse_generic(&mut self, param: GenericParam) {
match param {
GenericParam::Lifetime(lt) => {
self.types.explicit_lifetime(lt, &mut self.errors);
}
GenericParam::Type(ty) => {
self.types.add(ty.ident);
}
GenericParam::Const(c) => {
self.err("Logos doesn't support const generics.", c.span());
}
}
}
pub fn generics(&mut self) -> Option<TokenStream> {
self.types.generics(&mut self.errors)
}
fn parse_attr(&mut self, attr: &mut Attribute) -> Option<AttributeParser> {
match &mut attr.meta {
Meta::List(list) => {
let tokens = std::mem::replace(&mut list.tokens, TokenStream::new());
Some(AttributeParser::new(tokens))
}
_ => None,
}
}
/// Try to parse the main `#[logos(...)]`, does nothing if
/// the attribute's name isn't `logos`.
pub fn try_parse_logos(&mut self, attr: &mut Attribute) {
if !attr.path().is_ident(LOGOS_ATTR) {
return;
}
let nested = match self.parse_attr(attr) {
Some(tokens) => tokens,
None => {
self.err("Expected #[logos(...)]", attr.span());
return;
}
};
for nested in nested {
let (name, value) = match nested {
Nested::Named(name, value) => (name, value),
Nested::Unexpected(tokens) | Nested::Unnamed(tokens) => {
self.err("Invalid nested attribute", tokens.span());
continue;
}
};
let span = name.span();
match name.to_string().as_str() {
"crate" => match value {
NestedValue::Assign(logos_path) => self.logos_path = Some(logos_path),
_ => {
self.err("Expected: #[logos(crate = path::to::logos)]", span);
}
},
"error" => match value {
NestedValue::Assign(value) => {
let span = value.span();
let error_ty = ErrorType::new(value);
if let Some(previous) = self.error_type.replace(error_ty) {
self.err("Error type can be defined only once", span)
.err("Previous definition here", previous.span());
}
}
NestedValue::Group(value) => {
let span = value.span();
let mut nested = AttributeParser::new(value);
let ty = match nested.parsed::<Type>() {
Some(Ok(ty)) => ty,
Some(Err(e)) => {
self.err(e.to_string(), e.span());
return;
}
None => {
self.err("Expected #[logos(error(SomeType))]", span);
return;
}
};
let mut error_type = {
use quote::ToTokens;
ErrorType::new(ty.into_token_stream())
};
for (position, next) in nested.enumerate() {
match next {
Nested::Unexpected(tokens) => {
self.err("Unexpected token in attribute", tokens.span());
}
Nested::Unnamed(tokens) => match position {
0 => error_type.callback = self.parse_callback(tokens),
_ => {
self.err(
"\
Expected a named argument at this position\n\
\n\
hint: If you are trying to define a callback here use: callback = ...\
",
tokens.span(),
);
}
},
Nested::Named(name, value) => {
error_type.named_attr(name, value, self);
}
}
}
if let Some(previous) = self.error_type.replace(error_type) {
self.err("Error type can be defined only once", span)
.err("Previous definition here", previous.span());
}
}
_ => {
self.err(
concat!(
"Expected: #[logos(error = SomeType)] or ",
"#[logos(error(SomeType[, callback))]"
),
span,
);
}
},
"export_dir" => match value {
NestedValue::Assign(value) => {
let span = value.span();
match syn::parse2::<Literal>(value) {
Ok(Literal::Utf8(str)) => {
if let Some(previous) = self.export_path.replace(str.value()) {
self.err("Export path can be defined only once", span)
.err("Previous definition here", previous.span());
}
}
Ok(_) => {
self.err("Expected a &str", span);
}
Err(e) => {
self.err(e.to_string(), span);
}
}
}
_ => {
self.err(
"Expected #[logos(export_dir = \"path/to/export/dir\")]",
span,
);
}
},
"extras" => match value {
NestedValue::Assign(value) => {
let span = value.span();
if let MaybeVoid::Some(previous) = self.extras.replace(value) {
self.err("Extras can be defined only once", span)
.err("Previous definition here", previous.span());
}
}
_ => {
self.err("Expected: #[logos(extras = SomeType)]", span);
}
},
"skip" => match value {
NestedValue::Literal(lit) => {
if let Some(literal) = self.parse_literal(Lit::new(lit)) {
self.skips.push(Definition::new(literal));
}
}
NestedValue::Group(tokens) => {
let token_span = tokens.span();
if let Some(skip) = self.parse_definition(AttributeParser::new(tokens)) {
self.skips.push(skip);
} else {
self.err(
"Expected #[logos(skip(\"regex literal\"[, [callback = ] callback, priority = priority]))]",
token_span,
);
}
}
_ => {
self.err(
"Expected: #[logos(skip \"regex literal\")] or #[logos(skip(...))]",
span,
);
}
},
"source" => {
self.err(
"The `source` attribute is deprecated. Use the `utf8` attribute instead",
span,
);
}
"subpattern" => match value {
NestedValue::KeywordAssign(name, value) => {
match syn::parse2::<Literal>(value) {
Ok(lit) => {
self.subpatterns.push((name, lit));
}
Err(e) => {
self.errors.err(e.to_string(), e.span());
}
};
}
_ => {
self.err(r#"Expected: #[logos(subpattern name = r"regex")]"#, span);
}
},
"type" => match value {
NestedValue::KeywordAssign(generic, ty) => {
self.types.set(generic, ty, &mut self.errors);
}
_ => {
self.err("Expected: #[logos(type T = SomeType)]", span);
}
},
"utf8" => match value {
NestedValue::Assign(value) => {
let span = value.span();
match syn::parse2::<LitBool>(value) {
Ok(lit) => {
if let Some(previous) = self.utf8_mode.replace(lit) {
self.err("Utf8 mode can be defined only once", span)
.err("Previous definition here", previous.span());
}
}
Err(e) => {
self.err(format!("Expected a boolean literal: {e}"), span);
}
}
}
_ => {
self.err("Expected: #[logos(utf8 = true)]", span);
}
},
name => {
self.err(format!("Unknown nested attribute #[logos({name})]",), span);
}
}
}
}
pub fn parse_literal(&mut self, lit: Lit) -> Option<Literal> {
match lit {
Lit::Str(string) => Some(Literal::Utf8(string)),
Lit::ByteStr(bytes) => Some(Literal::Bytes(bytes)),
_ => {
self.err("Expected a &str or &[u8] slice", lit.span());
None
}
}
}
/// Parse attribute definition of a token:
///
/// + `#[token(literal[, callback])]`
/// + `#[regex(literal[, callback])]`
pub fn parse_definition_attr(&mut self, attr: &mut Attribute) -> Option<Definition> {
let nested = self.parse_attr(attr)?;
self.parse_definition(nested)
}
fn parse_definition(&mut self, mut nested: AttributeParser) -> Option<Definition> {
let literal = match nested.parsed::<Lit>()? {
Ok(lit) => self.parse_literal(lit)?,
Err(err) => {
self.err(err.to_string(), err.span());
return None;
}
};
let mut def = Definition::new(literal);
for (position, next) in nested.enumerate() {
match next {
Nested::Unexpected(tokens) => {
self.err("Unexpected token in attribute", tokens.span());
}
Nested::Unnamed(tokens) => match position {
0 => def.callback = self.parse_callback(tokens),
_ => {
self.err(
"\
Expected a named argument at this position\n\
\n\
hint: If you are trying to define a callback here use: callback = ...\
",
tokens.span(),
);
}
},
Nested::Named(name, value) => {
def.named_attr(name, value, self);
}
}
}
Some(def)
}
fn parse_callback(&mut self, tokens: TokenStream) -> Option<Callback> {
let span = tokens.span();
let mut tokens = tokens.into_iter();
if let Some(tt) = expect_punct(tokens.next(), '|') {
let mut label = TokenStream::from(tt);
label.extend(tokens);
return Some(Callback::Label(label));
}
let first = tokens.next();
let error = expect_punct(tokens.next(), '|');
let arg = match (error, first) {
(None, Some(TokenTree::Ident(arg))) => arg,
_ => {
self.err(
"Inline callbacks must use closure syntax with exactly one parameter",
span,
);
return None;
}
};
let body = match tokens.next() {
Some(TokenTree::Group(group)) => group.stream(),
Some(first) => {
let mut body = TokenStream::from(first);
body.extend(tokens);
body
}
None => {
self.err("Callback missing a body", span);
return None;
}
};
let inline = InlineCallback { arg, body, span };
Some(inline.into())
}
/// Checks if `ty` is a declared generic param, if so replaces it
/// with a concrete type defined using #[logos(type T = Type)]
///
/// If no matching generic param is found, all lifetimes are fixed
/// to the source lifetime
pub fn get_type(&self, ty: &mut Type) -> TokenStream {
traverse_type(ty, &mut |ty| {
if let Type::Path(tp) = ty {
// Skip types that begin with `self::`
if tp.qself.is_none() {
// If `ty` is a generic type parameter, try to find
// its concrete type defined with #[logos(type T = Type)]
if let Some(substitute) = self.types.find(&tp.path) {
*ty = substitute;
}
}
}
// If `ty` is a concrete type, fix its lifetimes to 'source
replace_lifetime(ty);
});
quote!(#ty)
}
pub fn err<M>(&mut self, message: M, span: Span) -> &mut Errors
where
M: Into<Cow<'static, str>>,
{
self.errors.err(message, span)
}
}