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
//! The match side of a rule: what an invocation must look like.
use Vec;
use Symbol;
/// One element of a rule's pattern — the left-hand side of a `macro_rules!` arm.
///
/// A pattern is a sequence of elements matched against an invocation's token
/// trees from left to right. Literal tokens and groups must appear exactly;
/// a [`Bind`](Pattern::Bind) captures a tree under a name the template can
/// substitute; a [`Repeat`](Pattern::Repeat) matches its body any number of
/// times. The whole pattern must consume the whole invocation.
///
/// Patterns are plain data: a language's front end parses its own macro
/// syntax (say `$x:ident` or `$($e:expr),*`) and lowers it into these
/// elements. [`Macro::new`](crate::Macro::new) validates and compiles them.
///
/// Literal tokens compare by kind alone. Spans never matter to a match, and
/// neither do hygiene contexts: a literal `else` in a pattern matches an `else`
/// token whichever expansion introduced it.
///
/// The enum is `#[non_exhaustive]` so that new kinds of pattern element can be
/// added in a minor release. Constructing any variant is unaffected; code that
/// matches on a `Pattern` needs a wildcard arm.
///
/// # Examples
///
/// The pattern `$name:tt = ( $($arg:tt),* )` with `char` tokens:
///
/// ```
/// use macro_lang::{Fragment, Kleene, Pattern};
/// use intern_lang::Interner;
///
/// let mut names = Interner::new();
/// let pattern: Vec<Pattern<char>> = vec![
/// Pattern::Bind { name: names.intern("name"), fragment: Fragment::Tree },
/// Pattern::Token('='),
/// Pattern::Group {
/// open: '(',
/// close: ')',
/// body: vec![Pattern::Repeat {
/// body: vec![Pattern::Bind { name: names.intern("arg"), fragment: Fragment::Tree }],
/// separator: Some(','),
/// kleene: Kleene::ZeroOrMore,
/// }],
/// },
/// ];
/// assert_eq!(pattern.len(), 3);
/// ```
/// What a [`Pattern::Bind`] may capture: the fragment specifier.
///
/// macro-lang knows no grammar, so it cannot offer `expr` or `ty` fragments;
/// those need a parser. What it can offer is the structural fragment every
/// macro system has — any single token tree — and a token-level test the
/// language supplies, which covers specifiers like `ident` and `literal`.
///
/// The enum is `#[non_exhaustive]` so that new fragment kinds can be added in a
/// minor release; code that matches on a `Fragment` needs a wildcard arm.
///
/// # Examples
///
/// ```
/// use macro_lang::Fragment;
///
/// // Any single tree: a token or a whole delimited group (`$x:tt`).
/// let any: Fragment<char> = Fragment::Tree;
///
/// // A single token whose kind passes a test (`$x:ident`).
/// let ident: Fragment<char> = Fragment::Kind(|c: &char| c.is_alphabetic());
///
/// assert!(matches!(any, Fragment::Tree));
/// assert!(matches!(ident, Fragment::Kind(test) if test(&'x') && !test(&'1')));
/// ```
/// How many times a [`Pattern::Repeat`] may match: the Kleene operator.
///
/// # Examples
///
/// ```
/// use macro_lang::Kleene;
///
/// assert!(Kleene::ZeroOrMore.allows(0));
/// assert!(!Kleene::OneOrMore.allows(0));
/// assert!(!Kleene::ZeroOrOne.allows(2));
/// ```