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
//! Token trees: the input and output of every expansion.
use Vec;
use ;
use crateContext;
/// A token tree: a single token, or a delimited group of trees.
///
/// Macro expansion works on token trees rather than on a flat token stream or a
/// finished syntax tree. Grouping by delimiters is the one piece of structure
/// every language agrees on before parsing, and it is exactly the structure
/// a pattern needs: a group is matched as a unit, and a fragment can capture a
/// whole bracketed expression without knowing the grammar inside it. Expansion
/// runs before parsing; the parser consumes the expanded trees.
///
/// `K` is the language's token kind — the same type a lexer produces for
/// `token_lang::Token<K>`. The crate only ever compares kinds for equality and
/// clones them, so a small `Copy` kind (an `enum` carrying interned
/// [`Symbol`](intern_lang::Symbol)s) is the fast path.
///
/// Every token carries its hygiene [`Context`]. Trees built from source use
/// [`Context::ROOT`], which is what [`Tree::token`] fills in.
///
/// # Examples
///
/// The token trees for `f(a, b)`, using `char` as the token kind:
///
/// ```
/// use macro_lang::Tree;
/// use token_lang::{Span, Token};
///
/// let call = vec![
/// Tree::token('f', Span::new(0, 1)),
/// Tree::Group {
/// open: Token::new('(', Span::new(1, 2)),
/// close: Token::new(')', Span::new(6, 7)),
/// trees: vec![
/// Tree::token('a', Span::new(2, 3)),
/// Tree::token(',', Span::new(3, 4)),
/// Tree::token('b', Span::new(5, 6)),
/// ],
/// },
/// ];
/// assert_eq!(call[1].span(), Span::new(1, 7));
/// ```