plex 0.3.1

A syntax extension for writing lexers and parsers.
Documentation
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
use std::collections::btree_map::Entry;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::{self, Write};

use lalr::*;

use proc_macro2::{Span, TokenStream};
use quote::{quote, quote_spanned, ToTokens};
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::token::{Bracket, Paren};
use syn::{
    self, braced, bracketed, parenthesized, parse_macro_input, token, Attribute, Block, Error,
    Expr, Ident, Meta, Pat, Token, Type, Visibility,
};

/// Return the most frequent item in the given iterator, or None if it is empty.
/// Picks an arbitrary item in case of a tie.
fn most_frequent<T: Ord, I: Iterator<Item = T>>(it: I) -> Option<T> {
    let mut freq = BTreeMap::new();
    for x in it {
        *freq.entry(x).or_insert(0) += 1;
    }
    freq.into_iter().max_by_key(|&(_, f)| f).map(|(x, _)| x)
}

fn expected_one_of<S: fmt::Display>(xs: &[S]) -> String {
    let mut err_msg: String = "expected".to_string();
    for (i, x) in xs.iter().enumerate() {
        if i == 0 {
            let _ = write!(&mut err_msg, " ");
        } else if i == xs.len() - 1 {
            if i == 1 {
                let _ = write!(&mut err_msg, " or ");
            } else {
                let _ = write!(&mut err_msg, ", or ");
            }
        } else {
            let _ = write!(&mut err_msg, ", ");
        }
        let _ = write!(&mut err_msg, "{}", x);
    }
    err_msg
}

pub fn lr1_machine<'a, T, N, A, FM, FA, FR, FO>(
    grammar: &'a Grammar<T, N, A>,
    types: &BTreeMap<N, Type>,
    token_ty: Type,
    span_ty: Type,
    range_fn: Option<(Ident, Ident, Block)>,
    vis: Visibility,
    name: Ident,
    mut to_pat: FM,
    mut to_expr: FA,
    reduce_on: FR,
    priority_of: FO,
) -> Result<TokenStream, LR1Conflict<'a, T, N, A>>
where
    T: Ord + fmt::Debug + fmt::Display,
    N: Ord + fmt::Debug,
    A: fmt::Debug,
    FM: FnMut(&T) -> TokenStream,
    FA: FnMut(&N, &A, &[Symbol<T, N>]) -> (TokenStream, Vec<Option<TokenStream>>, Span),
    FR: FnMut(&Rhs<T, N, A>, Option<&T>) -> bool,
    FO: FnMut(&Rhs<T, N, A>, Option<&T>) -> i32,
{
    let actual_start = match grammar
        .rules
        .get(&grammar.start)
        .expect("Grammar didn't contain its start nonterminal")[0]
        .syms[0]
    {
        Terminal(_) => panic!("bad grammar"),
        Nonterminal(ref x) => x,
    };
    let any_ty = quote!(::std::any::Any);
    let start_ty = &types[actual_start];
    let table: LR1ParseTable<T, N, A> = grammar.lalr1(reduce_on, priority_of)?;
    let rule_fn_ids: BTreeMap<_, _> = grammar
        .rules
        .iter()
        .filter(|&(lhs, _)| *lhs != grammar.start)
        .flat_map(|(_, rhss)| {
            // Identify rules by their RHS, which should have unique addresses
            // FIXME: maybe not the best idea
            rhss.iter().map(|rhs| rhs as *const _)
        })
        .enumerate()
        .map(|(i, k)| (k, Ident::new(&format!("reduce_{}", i), Span::call_site())))
        .collect();
    let goto_fn_ids: BTreeMap<_, _> = grammar
        .rules
        .keys()
        .filter(|&lhs| *lhs != grammar.start)
        .enumerate()
        .map(|(i, lhs)| (lhs, Ident::new(&format!("goto_{}", i), Span::call_site())))
        .collect();

    let mut stmts = Vec::new();

    stmts.push(if let Some((a, b, body)) = range_fn {
        quote!(fn range(#a: #span_ty, #b: #span_ty) -> #span_ty {
            #body
        })
    } else {
        quote!(
            fn range(_a: (), _b: ()) {}
        )
    });
    stmts.push(
        quote!(fn range_array(x: &[Option<#span_ty>]) -> Option<#span_ty> {
            if let Some(lo) = x.iter().filter_map(|&x| x).next() {
                let hi = x.iter().rev().filter_map(|&x| x).next().unwrap();
                Some(range(lo, hi))
            } else {
                None
            }
        }),
    );
    // The `lhs`s which are actually unreachable; as such, their associated code should not be generated.
    let mut lhs_unreachable = BTreeSet::new();
    for (lhs, id) in &goto_fn_ids {
        let expr = if let Some(&most_freq) =
            most_frequent(table.states.iter().filter_map(|state| state.goto.get(lhs)))
        {
            let most_freq = most_freq as u32;
            let mut pats_by_dest = BTreeMap::new();
            for (ix, state) in table.states.iter().enumerate() {
                let ix = ix as u32;
                if let Some(&dest) = state.goto.get(lhs) {
                    let dest = dest as u32;
                    if dest != most_freq {
                        pats_by_dest.entry(dest).or_insert(vec![]).push(quote!(#ix));
                    }
                }
            }
            let mut arms: Vec<_> = pats_by_dest
                .into_iter()
                .map(|(dest, pats)| quote!(#(#pats)|* => #dest,))
                .collect();
            arms.push(quote!(_ => #most_freq,));
            quote!(match state { #(#arms)* })
        } else {
            // This shouldn't normally happen, but it can when `lhs` is unused in the
            // grammar.
            lhs_unreachable.insert(lhs);
            continue;
        };
        stmts.push(quote!(fn #id(state: u32) -> u32 {
            #expr
        }));
    }
    for (lhs, rhss) in &grammar.rules {
        if *lhs == grammar.start || lhs_unreachable.contains(&lhs) {
            continue;
        }
        let goto_fn = goto_fn_ids[lhs].clone();
        let lhs_ty = &types[lhs];
        for rhs in rhss.iter() {
            let (result, arg_pats, rhs_span) = to_expr(lhs, &rhs.act, &rhs.syms);
            let len = rhs.syms.len();
            let current_span_stmt = if rhs.syms.len() > 0 {
                // Make the current_span available to the user by exposing it through a macro whose name is unhygienic.
                let current_span_ident = quote_spanned!(rhs_span => current_span);
                let span_macro = quote!(
                    #[allow(unused_macros)]
                    macro_rules! span {
                        () => { #current_span_ident.unwrap() }
                    }
                );
                quote_spanned!(rhs_span =>
                    let current_span: Option<#span_ty> = {
                        let sp = range_array(&span_stack[(span_stack.len() - #len)..]);
                        let newlen = span_stack.len() - #len;
                        span_stack.truncate(newlen);
                        sp
                    };
                    #span_macro
                )
            } else {
                quote_spanned!(rhs_span =>
                    let current_span: Option<#span_ty> = None;
                )
            };
            let mut reduce_stmts = vec![current_span_stmt];
            reduce_stmts.extend(rhs.syms.iter().zip(arg_pats.iter().cloned()).rev().map(
                |(sym, maybe_pat)| match maybe_pat {
                    // TODO: maybe use an even more precise span
                    Some(pat) => {
                        let ty = match *sym {
                            Terminal(_) => token_ty.clone(),
                            Nonterminal(ref n) => types[n].clone(),
                        };
                        quote_spanned!(rhs_span =>
                            let #pat: #ty = *stack.pop().unwrap().downcast().unwrap();
                        )
                    }
                    None => quote_spanned!(rhs_span => stack.pop();),
                },
            ));
            if rhs.syms.len() > 1 {
                let len_minus_one = rhs.syms.len() - 1;
                // XXX: Annoying syntax :(
                reduce_stmts.push(
                    quote_spanned!(rhs_span => match state_stack.len() - #len_minus_one { x => state_stack.truncate(x) };),
                );
            } else if rhs.syms.len() == 0 {
                reduce_stmts.push(quote_spanned!(rhs_span => state_stack.push(*state);));
            }
            reduce_stmts.push(quote_spanned!(
                rhs_span =>
                *state = #goto_fn(*state_stack.last().unwrap());
                let result: #lhs_ty = ( || -> #lhs_ty { #result } )();
                stack.push(Box::new(result) as Box<#any_ty>);
                span_stack.push(current_span);
            ));

            let fn_id = rule_fn_ids.get(&(rhs as *const _)).unwrap().clone();
            stmts.push(quote_spanned!(
                rhs_span =>
                fn #fn_id(
                    stack: &mut Vec<Box<#any_ty>>,
                    span_stack: &mut Vec<Option<#span_ty>>,
                    state_stack: &mut Vec<u32>,
                    state: &mut u32,
                ) {
                    #(#reduce_stmts)*
                }
            ));
        }
    }
    stmts.push(quote!(
        let mut stack: Vec<Box<#any_ty>> = Vec::new();
        let mut span_stack = Vec::new();
        let mut state_stack = Vec::new();
        let mut state: u32 = 0;
        let mut token_span = it.next();
    ));
    stmts.push({
        let state_arms = table.states.iter().enumerate().map(|(ix, state)| {
            let mut arms = vec![];
            let mut reduce_arms = BTreeMap::new();
            let mut expected = vec![];
            for (&tok, action) in &state.lookahead {
                expected.push(format!("`{}`", tok));
                let tok_pat = to_pat(tok);
                let pat = quote!(Some((#tok_pat, _)));
                let arm_expr = match *action {
                    LRAction::Shift(dest) => dest as u32,
                    LRAction::Reduce(_, rhs) => {
                        reduce_arms
                            .entry(rhs as *const _)
                            .or_insert(vec![])
                            .push(pat);
                        continue;
                    }
                    LRAction::Accept => unreachable!(),
                };
                arms.push(quote!(#pat => #arm_expr,));
            }
            if let Some(ref action) = state.eof {
                expected.push("end of file".into());
                let pat = quote!(None);
                match *action {
                    LRAction::Shift(_) => unreachable!(),
                    LRAction::Reduce(_, rhs) => {
                        reduce_arms
                            .entry(rhs as *const _)
                            .or_insert(vec![])
                            .push(pat);
                    }
                    LRAction::Accept => {
                        arms.push(
                            quote!(#pat => return Ok(*stack.pop().unwrap().downcast::<#start_ty>().unwrap()),),
                        );
                    }
                };
            }
            for (rhs_ptr, pats) in reduce_arms.into_iter() {
                let reduce_fn = rule_fn_ids[&rhs_ptr].clone();
                arms.push(quote!(#(#pats)|* => {
                    #reduce_fn(&mut stack, &mut span_stack, &mut state_stack, &mut state);
                    continue
                }));
            }
            let err_msg = expected_one_of(&expected);
            arms.push(quote!(_ => return Err((token_span, #err_msg)),));
            let ix = ix as u32;
            quote!(#ix => match token_span { #(#arms)* })
        });
        quote!(
            loop {
                let next_state = match state {
                    #(#state_arms)*
                    _ => unreachable!(),
                };
                match token_span {
                    Some((token, span)) => {
                        stack.push(Box::new(token) as Box<#any_ty>);
                        span_stack.push(Some(span));
                    }
                    None => unreachable!(),
                };
                state_stack.push(state);
                token_span = it.next();
                state = next_state;
            }
        )
    });
    // `quote` bug: can't quote `'static`, so use `&'a str` for any `'a`. hopefully this is fine.
    Ok(quote!(
        #vis fn #name<'a, I: Iterator<Item=(#token_ty, #span_ty)>>(mut it: I) -> Result<#start_ty, (Option<(#token_ty, #span_ty)>, &'a str)> {
            #(#stmts)*
        }
    ))
}

#[derive(Debug)]
enum RuleRhsItem {
    Symbol(Ident),
    SymbolPat(Ident, Bracket, Pat),
    Destructure(Ident, Paren, Punctuated<Pat, Token![,]>),
}

impl ToTokens for RuleRhsItem {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            RuleRhsItem::Symbol(ident) => ident.to_tokens(tokens),
            RuleRhsItem::SymbolPat(ident, bracket, pat) => {
                ident.to_tokens(tokens);
                bracket.surround(tokens, |tokens| pat.to_tokens(tokens));
            }
            RuleRhsItem::Destructure(ident, paren, pats) => {
                ident.to_tokens(tokens);
                paren.surround(tokens, |tokens| pats.to_tokens(tokens));
            }
        }
    }
}

impl RuleRhsItem {
    fn ident(&self) -> &Ident {
        match *self {
            RuleRhsItem::Symbol(ref ident)
            | RuleRhsItem::SymbolPat(ref ident, _, _)
            | RuleRhsItem::Destructure(ref ident, _, _) => ident,
        }
    }
}

#[derive(Debug)]
struct Rule {
    rhs: Vec<RuleRhsItem>,
    arrow: Token![=>],
    action: TokenStream,
    exclusions: BTreeSet<Ident>,
    exclude_eof: bool,
    priority: i32,
}

impl Rule {
    fn head(&self) -> proc_macro2::TokenStream {
        let rhs = &self.rhs;
        let arrow = &self.arrow;
        quote!(#(#rhs)* #arrow)
    }
}

fn parse_rules(input: ParseStream) -> syn::Result<Vec<Rule>> {
    let mut rules = vec![];
    while !input.is_empty() {
        // FIXME: Make some nicer error messages.
        let mut exclusions = BTreeSet::new();
        let mut exclude_eof = false;
        let mut priority = 0;
        let attrs = Attribute::parse_outer(input)?;
        for attr in attrs {
            match attr.meta {
                Meta::List(_) if attr.path().is_ident("no_reduce") => {
                    attr.parse_nested_meta(|nested| {
                        if let Some(ident) = nested.path.get_ident() {
                            if ident == "EOF" {
                                exclude_eof = true;
                            } else {
                                exclusions.insert(ident.clone());
                            }
                            Ok(())
                        } else {
                            Err(nested.error("invalid syntax: no_reduce list includes a non-token"))
                        }
                    })?;
                }
                Meta::Path(_) if attr.path().is_ident("overriding") => {
                    priority = 1;
                }
                _ => {
                    return Err(Error::new_spanned(attr, "unknown attribute"));
                }
            }
        }
        let mut rhs = vec![];
        while !input.peek(Token![=>]) {
            let ident: Ident = input.parse()?;
            rhs.push(if input.peek(token::Bracket) {
                let inner;
                let bracket = bracketed!(inner in input);
                let pat = Pat::parse_single(&inner)?;
                if !inner.is_empty() {
                    return Err(inner.error("unexpected token after pattern"));
                }
                RuleRhsItem::SymbolPat(ident, bracket, pat)
            } else if input.peek(token::Paren) {
                let inner;
                let paren = parenthesized!(inner in input);
                let pats = inner.parse_terminated(Pat::parse_single, Token![,])?;
                RuleRhsItem::Destructure(
                    ident,
                    paren,
                    pats.into_pairs().map(|p| p.into_value()).collect(),
                )
            } else {
                RuleRhsItem::Symbol(ident)
            });
        }
        let arrow = input.parse::<Token![=>]>()?;
        // Like in a `match` expression, braced block doesn't require a comma before the next rule.
        let optional_comma = input.peek(token::Brace);
        let action: Expr = input.parse()?;
        rules.push(Rule {
            rhs,
            arrow,
            action: action.into_token_stream(),
            exclusions,
            exclude_eof,
            priority,
        });
        match <Token![,]>::parse(input) {
            Ok(_) => {}
            Err(e) => {
                if !input.is_empty() && !optional_comma {
                    return Err(e);
                }
            }
        }
    }
    Ok(rules)
}

struct RuleSet {
    lhs: Ident,
    return_ty: Type,
    rules: Vec<Rule>,
}

impl Parse for RuleSet {
    fn parse(input: ParseStream) -> syn::Result<RuleSet> {
        Ok(RuleSet {
            lhs: input.parse()?,
            return_ty: {
                input.parse::<Token![:]>()?;
                input.parse()?
            },
            rules: {
                let rule_content;
                braced!(rule_content in input);
                parse_rules(&rule_content)?
            },
        })
    }
}

struct Parser {
    vis: Visibility,
    name: Ident,
    token_ty: Type,
    span_ty: Type,
    range_fn: Option<(Ident, Ident, Block)>,
    rule_sets: Vec<RuleSet>,
}

impl Parse for Parser {
    fn parse(input: ParseStream) -> syn::Result<Parser> {
        let span_ty;
        Ok(Parser {
            vis: input.parse()?,
            name: {
                input.parse::<Token![fn]>()?;
                input.parse()?
            },
            token_ty: {
                let inner;
                parenthesized!(inner in input);
                let token = inner.parse()?;
                inner.parse::<Token![,]>()?;
                span_ty = inner.parse()?;
                if !inner.is_empty() {
                    return Err(inner.error("unexpected token after span type"));
                }
                input.parse::<Token![;]>()?;
                token
            },
            span_ty,
            range_fn: {
                if input.peek(token::Paren) {
                    let inner;
                    parenthesized!(inner in input);
                    let a = inner.parse()?;
                    inner.parse::<Token![,]>()?;
                    let b = inner.parse()?;
                    if !inner.is_empty() {
                        return Err(inner.error("unexpected token after second range"));
                    }
                    let body = input.parse::<Block>()?;
                    Some((a, b, body))
                } else {
                    None
                }
            },
            rule_sets: {
                let mut r = vec![];
                while !input.is_empty() {
                    r.push(input.parse()?);
                }
                r
            },
        })
    }
}

fn pretty_rule(lhs: Ident, syms: &[Symbol<Ident, Ident>]) -> String {
    let mut r = String::new();
    let _ = write!(&mut r, "{} ->", lhs);
    for sym in syms.iter() {
        let _ = write!(&mut r, " {}", sym);
    }
    r
}

// Pretty-print an item set, for error messages.
fn pretty(x: &ItemSet<Ident, Ident, &Rule>, pad: &str) -> String {
    let mut r = String::new();
    let mut first = true;
    for item in x.items.iter() {
        if first {
            first = false;
        } else {
            let _ = write!(&mut r, "\n{}", pad);
        }
        let _ = write!(&mut r, "{} ->", item.lhs);
        for j in 0..item.pos {
            let _ = write!(&mut r, " {}", item.rhs.syms[j]);
        }
        let _ = write!(&mut r, " •");
        for j in item.pos..item.rhs.syms.len() {
            let _ = write!(&mut r, " {}", item.rhs.syms[j]);
        }
    }
    r
}

pub fn parser(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let parser = parse_macro_input!(input as Parser);
    let fake_rule; // N.B. must go before `rules` to appease dropck
    let mut rules = BTreeMap::new();
    let mut types = BTreeMap::new();
    let mut start = None;
    let mut errors = vec![];
    for rule_set in &parser.rule_sets {
        // parse "LHS: Type {"
        let lhs = &rule_set.lhs;
        if start.is_none() {
            start = Some(lhs.clone());
        }
        match rules.entry(lhs.clone()) {
            Entry::Occupied(ent) => {
                errors.push(Error::new_spanned(lhs, "duplicate nonterminal"));
                errors.push(Error::new_spanned(
                    ent.key(),
                    "the first definition is here",
                ));
            }
            Entry::Vacant(ent) => {
                types.insert(lhs.clone(), rule_set.return_ty.clone());
                ent.insert(&rule_set.rules);
            }
        }
    }
    let start = if let Some(start) = start {
        start
    } else {
        return Error::new(Span::call_site(), "at least one nonterminal is required")
            .into_compile_error()
            .into();
    };
    let mut rules: BTreeMap<Ident, Vec<_>> = rules
        .into_iter()
        .map(|(lhs, rules)| {
            let rhss = rules
                .iter()
                .map(|rule| {
                    // figure out which symbols in `rule` are nonterminals vs terminals
                    let syms = rule
                        .rhs
                        .iter()
                        .map(|tok| {
                            let ident = tok.ident().clone();
                            if types.contains_key(&ident) {
                                Nonterminal(ident)
                            } else {
                                Terminal(ident)
                            }
                        })
                        .collect();
                    Rhs {
                        syms: syms,
                        act: rule,
                    }
                })
                .collect();
            (lhs, rhss)
        })
        .collect();
    let fake_start = Ident::new("__FIXME__start", Span::call_site());
    fake_rule = Rule {
        rhs: vec![],
        arrow: Default::default(),
        action: quote!(),
        exclusions: BTreeSet::new(),
        exclude_eof: false,
        priority: -1,
    };
    rules.insert(
        fake_start.clone(),
        vec![Rhs {
            syms: vec![Nonterminal(start)],
            act: &fake_rule,
        }],
    );
    let grammar = Grammar {
        rules: rules,
        start: fake_start,
    };
    let result = lr1_machine(
        &grammar,
        &types,
        parser.token_ty,
        parser.span_ty,
        parser.range_fn,
        parser.vis,
        parser.name,
        |ident| quote!(#ident { .. }),
        |lhs, act, syms| {
            let mut expr = act.action.clone().into_token_stream();
            let mut args = vec![];
            debug_assert_eq!(syms.len(), act.rhs.len());
            for (i, (sym, x)) in syms.iter().zip(&act.rhs).enumerate() {
                args.push(match *x {
                    RuleRhsItem::SymbolPat(_, _, ref pat) => Some(pat.clone().into_token_stream()),
                    RuleRhsItem::Destructure(ref ident, _, ref pats) => {
                        let id = Ident::new(&format!("s{}", i), Span::call_site());
                        let terminal = match *sym {
                            Nonterminal(_) => {
                                errors.push(Error::new_spanned(
                                    x,
                                    "can't bind enum case to a nonterminal",
                                ));
                                Ident::new("__error", Span::call_site())
                            }
                            Terminal(ref x) => {
                                debug_assert_eq!(*x, ident.to_string());
                                x.clone()
                            }
                        };
                        expr = quote_spanned!(act.head().span() =>
                        {
                            // force a by-move capture
                            match {#id} {
                                #terminal(#pats) => #expr,
                                _ => unreachable!(),
                            }
                        });
                        Some(id.into_token_stream())
                    }
                    RuleRhsItem::Symbol(_) => None,
                });
            }

            // XXX: should be a cargo feature (?)
            if false {
                let rule_str = pretty_rule(lhs.clone(), syms);
                expr = quote!({
                    println!("reduce by {}", #rule_str);
                    #expr
                });
            }

            (expr, args, act.head().span())
        },
        |rhs, token| match token {
            Some(id) => !rhs.act.exclusions.contains(&id),
            None => !rhs.act.exclude_eof,
        },
        |rhs, _| rhs.act.priority,
    )
    .unwrap_or_else(|conflict| {
        match conflict {
            LR1Conflict::ReduceReduce {
                state,
                token,
                r1,
                r2,
            } => {
                let mut error = Error::new_spanned(
                    r1.1.act.head(),
                    format!(
                        "reduce-reduce conflict:
state: {}
token: {}",
                        pretty(&state, "       "),
                        match token {
                            Some(id) => id.to_string(),
                            None => "EOF".to_string(),
                        }
                    ),
                );
                error.combine(Error::new_spanned(
                    r2.1.act.head(),
                    "this is the conflicting rule",
                ));
                error
            }
            LR1Conflict::ShiftReduce { state, token, rule } => Error::new_spanned(
                rule.1.act.head(),
                format!(
                    "shift-reduce conflict:
state: {}
token: {}",
                    pretty(&state, "       "),
                    match token {
                        Some(id) => id.to_string(),
                        None => "EOF".to_string(),
                    }
                ),
            ),
        }
        .into_compile_error()
    });
    
    let errors = errors.into_iter().map(|e| e.into_compile_error()).collect::<TokenStream>();
    quote!(#errors #result).into()
}