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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
use proc_macro::*;
use std::iter;
use std::str;
use crate::errors::Errors;
use ArgConstraints::*;
pub struct Parser<'a> {
iterator: token_stream::IntoIter,
most_recent_span: Span,
errors: &'a mut Errors,
}
impl<'a> Parser<'a> {
pub fn new(errors: &'a mut Errors, context: Span, tokens: TokenStream) -> Self {
return Self {
iterator: tokens.into_iter(),
most_recent_span: context,
errors,
};
}
fn from_group(errors: &'a mut Errors, group: Group) -> Self {
let group_span = group.span();
return Self {
iterator: group.stream().into_iter(),
most_recent_span: group_span,
errors,
};
}
pub fn errors(&mut self) -> &mut Errors {
return self.errors;
}
pub fn move_next(&mut self) -> Option<TokenTree> {
let current = self.iterator.next();
if let Some(token) = ¤t {
self.most_recent_span = token.span();
}
return current;
}
/// Moves to the next comma or the end-of-stream.
/// Emits "expected ..." error for other tokens encountered before comma or end-of-stream.
pub fn next_comma(&mut self, constraints: ArgConstraints) {
match self.move_next() {
Some(TokenTree::Punct(punct)) if punct.as_char() == ',' => {
self.comma_after_item(constraints)
}
Some(token) => {
self.unexpected_token_before_end(constraints, token.span());
self.skip_to_comma(token);
}
None => (),
};
}
/// Reads an identifier then moves to the next comma or the end-of-stream.
/// Emits "expected ..." error for other tokens encountered before comma or end-of-stream.
pub fn next_ident(
&mut self,
constraints: ArgConstraints,
error_message: &str,
) -> Option<Ident> {
let result;
match self.move_next() {
Some(TokenTree::Ident(ident)) => {
result = Some(ident);
self.next_comma(constraints);
}
Some(token) => {
self.errors.add(token.span(), error_message);
if self.skip_to_comma(token) {
self.comma_after_item(constraints);
}
result = None;
}
None => {
self.eos_before_item(constraints, error_message);
result = None;
}
}
return result;
}
/// Reads a string literal then moves to the next comma or the end-of-stream.
/// Emits "expected ..." error for other tokens encountered before comma or end-of-stream.
pub fn next_string_literal(
&mut self,
constraints: ArgConstraints,
error_message: &str,
) -> Option<(String, Span)> {
let tree = self.move_next();
self.next_string_literal_token(tree, constraints, error_message)
}
/// Processes the next TokenTree in a context where a literal string is expected.
pub fn next_string_literal_token(
&mut self,
tokens: Option<TokenTree>,
constraints: ArgConstraints,
error_message: &str,
) -> Option<(String, Span)> {
let result;
match tokens {
Some(TokenTree::Literal(literal)) => {
let lit_str = literal.to_string();
if lit_str.len() < 2 || !lit_str.starts_with('"') || !lit_str.ends_with('"') {
self.errors.add(literal.span(), error_message);
if self.skip_to_comma(TokenTree::Literal(literal)) {
self.comma_after_item(constraints);
}
result = None;
} else {
if let Some(unescaped) = unescape(&lit_str[1..lit_str.len() - 1]) {
result = Some((unescaped, literal.span()));
} else {
self.errors
.add(literal.span(), "unsupported escape sequence");
result = None;
}
self.next_comma(constraints);
}
}
Some(TokenTree::Group(group)) if Delimiter::None == group.delimiter() => {
let mut contents: Vec<_> = group.stream().into_iter().collect();
if contents.len() == 1 {
result =
self.next_string_literal_token(contents.pop(), constraints, error_message);
} else {
self.errors.add(group.span(), error_message);
if let Some(token) = contents.pop() {
if self.skip_to_comma(token) {
self.comma_after_item(constraints);
}
}
result = None;
}
}
Some(token) => {
self.errors.add(token.span(), error_message);
if self.skip_to_comma(token) {
self.comma_after_item(constraints);
}
result = None;
}
None => {
self.eos_before_item(constraints, error_message);
result = None;
}
}
return result;
}
/// Reads tokens to the next comma or the end-of-stream.
/// Emits an error if no tokens or if ';'.
pub fn next_tokens(&mut self, constraints: ArgConstraints, error_message: &str) -> TokenStream {
enum State {
GotNone,
GotSome,
Done,
}
let mut state = State::GotNone;
return TokenStream::from_iter(iter::from_fn(|| {
if let State::Done = state {
return None;
} else {
match self.move_next() {
Some(TokenTree::Punct(punct)) if punct.as_char() == ',' => {
// Treat ',' as end-of-arg.
if let State::GotNone = state {
self.errors.add(punct.span(), error_message);
}
self.comma_after_item(constraints);
state = State::Done;
return None;
}
Some(TokenTree::Punct(punct)) if punct.as_char() == ';' => {
// Treat ';' as invalid at top level of arguments.
self.unexpected_token_before_end(constraints, punct.span());
self.skip_to_comma(punct.into());
state = State::Done;
return None;
}
Some(token) => {
state = State::GotSome;
return Some(token);
}
None => {
if let State::GotNone = state {
self.eos_before_item(constraints, error_message);
}
state = State::Done;
return None;
}
}
}
}));
}
/// Reads OptionIdent(ArgsGroup) or {...} then moves to the next comma or the end-of-stream.
/// Emits "expected option" errors for non-option syntax.
/// Emits "expected ..." error for other tokens encountered before comma or end-of-stream.
pub fn next_arg(&mut self, want_struct: bool) -> ArgResult<'_> {
const EXPECTED_OPTION: &str = "expected identifier for option name, e.g. Option(args...)";
const EXPECTED_OPTION_OR_STRUCT: &str =
"expected '{' for struct or identifier for option name, e.g. Option(args...)";
const EXPECTED_OPTION_ARGS: &str = "expected '(' after option name, e.g. Option(args...)";
let result;
loop {
// Expect: OptionName or {}
let first_token = self.move_next();
match first_token {
Some(TokenTree::Group(struct_group))
if want_struct && struct_group.delimiter() == Delimiter::Brace =>
{
result = ArgResult::Struct(Parser::from_group(self.errors, struct_group));
break;
}
Some(TokenTree::Ident(name_ident)) => {
// Expect: (option_args)
let args_token = self.move_next();
let args_group = match args_token {
Some(TokenTree::Group(group))
if group.delimiter() == Delimiter::Parenthesis =>
{
group
}
Some(token) => {
self.errors.add(token.span(), EXPECTED_OPTION_ARGS);
self.skip_to_comma(token);
continue;
}
None => {
self.errors.add(name_ident.span(), EXPECTED_OPTION_ARGS);
result = ArgResult::None;
break;
}
};
self.next_comma(if want_struct {
OptionalNotLast
} else {
Optional
}); // Assume options are optional.
result =
ArgResult::Option(name_ident, Parser::from_group(self.errors, args_group));
break;
}
Some(token) => {
self.errors.add(
token.span(),
if want_struct {
EXPECTED_OPTION_OR_STRUCT
} else {
EXPECTED_OPTION
},
);
self.skip_to_comma(token);
continue;
}
None => {
if !want_struct {
// Assume options are optional.
} else {
self.errors
.add(self.most_recent_span, EXPECTED_OPTION_OR_STRUCT);
}
result = ArgResult::None;
break;
}
};
}
return result;
}
/// Typically called to recover from a syntax error.
/// Returns true for comma, false for end-of-stream.
/// If skip_to_comma returns true, you may want to call comma_after_item.
///
/// `while current() != ',' && current() != None { move_next(); }`
fn skip_to_comma(&mut self, next_token: TokenTree) -> bool {
let result;
let mut current = Some(next_token);
loop {
match current {
None => {
result = false;
break;
}
Some(TokenTree::Punct(punct)) if punct.as_char() == ',' => {
result = true;
break;
}
_ => current = self.move_next(),
};
}
return result;
}
/// If the item was required then emit an error message.
fn eos_before_item(&mut self, constraints: ArgConstraints, error_message: &str) {
match constraints {
Optional /*| OptionalLast*/ => (),
_ => self.errors.add(self.most_recent_span, error_message),
}
}
/// If item must be last and isn't then emit `expected ')'`.
fn comma_after_item(&mut self, constraints: ArgConstraints) {
if let RequiredLast /*| OptionalLast*/ = constraints {
if self.move_next().is_some() {
self.unexpected_token_before_end(constraints, self.most_recent_span);
}
}
}
/// Adds one of the following errors, selected based on constraints:
/// - `expected ','`
/// - `expected ')'`
/// - `expected ',' or ')'`
fn unexpected_token_before_end(&mut self, constraints: ArgConstraints, span: Span) {
let error_message = match constraints {
Optional | Required => "expected ',' or ')'",
RequiredLast /*| OptionalLast*/ => "expected ')'",
OptionalNotLast | RequiredNotLast => "expected ','",
};
self.errors.add(span, error_message);
}
}
pub enum ArgResult<'a> {
None,
Option(Ident, Parser<'a>),
Struct(Parser<'a>),
}
#[derive(Clone, Copy)]
pub enum ArgConstraints {
/// Optional:
/// - No error for end-of-stream while looking for item.
///
/// Maybe last:
/// - `expected ',' or ')'` if unexpected tokens after item.
Optional,
/*
/// Optional:
/// - No error for end-of-stream while looking for item.
///
/// Must be last:
/// - `expected ')'` if unexpected tokens after item.
/// - `expected ')'` if any tokens after trailing comma.
OptionalLast,
*/
/// Optional:
/// - No error for end-of-stream while looking for item.
///
/// Never last:
/// - `expected ','` if unexpected tokens after item.
OptionalNotLast,
/// Required:
/// - Error for end-of-stream while looking for item.
///
/// Maybe last:
/// - `expected ',' or ')'` if unexpected tokens after item.
Required,
/// Required:
/// - Error for end-of-stream while looking for item.
///
/// Must be last:
/// - `expected ')'` if unexpected tokens after item.
/// - `expected ')'` if any tokens after trailing comma.
RequiredLast,
/// Required:
/// - Error for end-of-stream while looking for item.
///
/// Never last:
/// - `expected ','` if unexpected tokens after item.
RequiredNotLast,
}
enum NextHexResult {
Digit(u32),
Char(char),
End,
}
fn next_hex(it: &mut str::Chars) -> NextHexResult {
if let Some(ch) = it.next() {
if let Some(digit) = ch.to_digit(16) {
return NextHexResult::Digit(digit);
} else {
return NextHexResult::Char(ch);
}
} else {
return NextHexResult::End;
}
}
fn unescape_x(dest: &mut String, it: &mut str::Chars) -> bool {
let mut val = 0;
for _ in 0..2 {
match next_hex(it) {
NextHexResult::Digit(digit) => {
val = (val << 4) | digit;
}
NextHexResult::Char(_) => {
return false;
}
NextHexResult::End => {
return false;
}
}
}
dest.push(char::from_u32(val).unwrap());
return true;
}
fn unescape_u(dest: &mut String, it: &mut str::Chars) -> bool {
if let Some(ch) = it.next() {
if ch != '{' {
return false;
}
} else {
return false;
}
let mut val = 0;
for n in 0..6 {
match next_hex(it) {
NextHexResult::Digit(digit) => {
val = (val << 4) | digit;
}
NextHexResult::Char(ch) => {
if n == 0 || ch != '}' {
return false;
} else if let Some(val_ch) = char::from_u32(val) {
dest.push(val_ch);
return true; // SUCCESS
} else {
return false;
}
}
NextHexResult::End => {
return false;
}
}
}
return false; // Too many digits
}
fn unescape(src: &str) -> Option<String> {
let mut dest = String::with_capacity(src.len());
let mut it = src.chars();
while let Some(ch) = it.next() {
if ch != '\\' {
dest.push(ch);
} else {
match it.next() {
Some('0') => dest.push('\0'),
Some('n') => dest.push('\n'),
Some('r') => dest.push('\r'),
Some('t') => dest.push('\t'),
Some('\\') => dest.push('\\'),
Some('x') => {
if !unescape_x(&mut dest, &mut it) {
return None;
}
}
Some('u') => {
if !unescape_u(&mut dest, &mut it) {
return None;
}
}
_ => return None,
}
}
}
return Some(dest);
}