File: nonterminal-matching.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (56 lines) | stat: -rw-r--r-- 1,641 bytes parent folder | download | duplicates (3)
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
// Check that we are refusing to match on complex nonterminals for which tokens are
// unavailable and we'd have to go through AST comparisons.

#![feature(decl_macro)]

macro simple_nonterminal($nt_ident: ident, $nt_lifetime: lifetime, $nt_tt: tt) {
    macro n(a $nt_ident b $nt_lifetime c $nt_tt d) {
        struct S;
    }

    n!(a $nt_ident b $nt_lifetime c $nt_tt d);
}

macro complex_nonterminal($nt_item: item) {
    macro n(a $nt_item b) {
        struct S;
    }

    n!(a $nt_item b); //~ ERROR no rules expected item `enum E {}`
}

simple_nonterminal!(a, 'a, (x, y, z)); // OK

complex_nonterminal!(enum E {});

// `ident`, `lifetime`, and `tt` all work. Other fragments do not. See
// https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment
macro_rules! foo {
    (ident $x:ident) => { bar!(ident $x); };
    (lifetime $x:lifetime) => { bar!(lifetime $x); };
    (tt $x:tt) => { bar!(tt $x); };
    (expr $x:expr) => { bar!(expr $x); }; //~ ERROR: no rules expected expression `3`
    (literal $x:literal) => { bar!(literal $x); }; //~ ERROR: no rules expected literal `4`
    (path $x:path) => { bar!(path $x); }; //~ ERROR: no rules expected path `a::b::c`
    (stmt $x:stmt) => { bar!(stmt $x); }; //~ ERROR: no rules expected statement `let abc = 0`
}

macro_rules! bar {
    (ident abc) => {};
    (lifetime 'abc) => {};
    (tt 2) => {};
    (expr 3) => {};
    (literal 4) => {};
    (path a::b::c) => {};
    (stmt let abc = 0) => {};
}

foo!(ident abc);
foo!(lifetime 'abc);
foo!(tt 2);
foo!(expr 3);
foo!(literal 4);
foo!(path a::b::c);
foo!(stmt let abc = 0);

fn main() {}