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
//! Token-level errors and the recovery-token mechanism.
//!
//! A [`TokenError`] may carry a [`TokenRecovery`]: a placeholder token to emit *as if*
//! tokenization had succeeded, plus the stream position at which to resume reading. The
//! token reader itself is policy-free — it always reports the error; the session-level
//! [`Recovery`](crate::error::Recovery) policy decides whether to abort (strict)
//! or to record a [`Diagnostic`](crate::error::Diagnostic) and continue with the recovery
//! token (tolerant). Conversion to diagnostics happens there too; within the token layer,
//! errors are transient values carrying plain byte [`Span`](crate::source::Span)s, like
//! tokens themselves.
//!
//! Like the tokens themselves, these types are generic over the language type `L`: a
//! recovery holds one of the language's own tokens, and implementations of
//! `Lang::scan_specials` report their failures as these conditions.
use Box;
use fmt;
use crate;
use crateSourceSpan;
use crateLang;
use ;
/// Result of a tokenization operation: `T`, or a [`TokenError`] for the language `L`.
pub type TokenResult<L, T> = Result;
/// The error that is reported when the input ends directly after an escape character
/// (id `core.token.end-of-stream-after-escape`).
///
/// An escape character starts a command and must be followed by the command name, as in
/// `\alpha` or `\&`. The escape characters of a language are those of its
/// [`CommandRule`](super::CommandRule)s.
///
/// Reported by the standard token reader ([`StdTokenReader`](super::StdTokenReader)) when
/// the escape character is the last character of its input. In a tolerant parse, the
/// escape character is parsed as an ordinary character of the text.
/// The error that is reported when a character that the language forbids appears in the
/// text (id `core.token.forbidden-char`).
///
/// A language lists the characters that may not appear as ordinary characters of the
/// text in [`ForbiddenCharsRules::chars`](super::ForbiddenCharsRules::chars). The list is
/// part of the parsing state, so a character can be forbidden in one part of a document
/// and allowed in another, e.g. in math mode and in text mode.
///
/// Reported by the standard token reader ([`StdTokenReader`](super::StdTokenReader)) when
/// it reads a forbidden character as an ordinary character. Whitespace and the characters
/// that start a group delimiter, a command, a comment or a specials trigger are read as
/// such and are not checked. In a tolerant parse, the forbidden character is parsed as
/// an ordinary character of the text.
// Hand-written wording: the code-point rendering needs a cast (`as u32`), which the
// message format string cannot express.
/// A `TokenErrorKind` describes what went wrong while reading a token. It is the condition
/// of a [`TokenError`].
///
/// Each variant contains a condition that implements [`DiagnosticInfo`], so that a token
/// error is reported as a [`Diagnostic`](crate::error::Diagnostic) like any other
/// condition. The [`Custom`](Self::Custom) variant contains a condition that a language or
/// a custom token reader defines. To inspect a `Custom` condition, downcast it to its
/// type.
///
/// Variants may be added in a minor release; a `match` needs a catch-all arm.
// `Custom` and `#[non_exhaustive]` serve different extension axes: `Custom` lets third
// parties define new condition *payloads*, while `non_exhaustive` reserves *our* right to
// promote a recurring condition to a first-class matchable variant in a minor release —
// without it, adding any variant breaks every exhaustive downstream `match` (semver).
/// An error that a [`TokenReader`](super::TokenReader) returns when it fails to read a
/// token.
///
/// A token error contains the condition that describes the error
/// ([`kind`](TokenError::kind)), the span of the source where the error occurred
/// ([`span`](TokenError::span)), and optionally a [`TokenRecovery`]
/// ([`recovery`](TokenError::recovery)). The recovery describes how a parse can continue
/// past the error: it contains a placeholder token to use in place of the token that
/// could not be read, and the position at which to resume reading.
///
/// The token reader always returns the error. The [`Recovery`](crate::error::Recovery)
/// policy of the parse decides what happens next. A strict parse stops with the error. A
/// tolerant parse reports the condition as a [`Diagnostic`](crate::error::Diagnostic) and
/// continues with the placeholder token. A token error without a recovery stops the
/// parse in both cases.
// The recovery payload is boxed: every `peek`/`next` returns a `Result` sized by its
// error variant, and the error path is cold by construction — boxing keeps the hot
// `Result` at payload-plus-tag size.
/// How to continue past a [`TokenError`] in tolerant mode: pretend `token` was read, then
/// resume reading at `resume`.
///
/// `resume` is explicit rather than derived from the token: a custom token source's
/// placeholder need not end where reading should resume (its span may stand for
/// normalized or synthesized content), and the explicit position is what the content
/// loop's advancement check (below) is enforced against.
///
/// # Contract: `resume` must move the stream
///
/// `resume` must name a place the reader is not already at — in particular, one past
/// the error. The content loop's recovery arm consumes no token (the placeholder was
/// never in the stream), so its termination rests entirely on this move: after
/// [`move_to_position(&resume)`](super::TokenReader::move_to_position) the loop
/// compares [`position_here()`](super::TokenReader::position_here) with the position it
/// held before, and treats an unchanged position as a contract violation by the token
/// source — it aborts the parse with the token error, even in tolerant mode. Stream
/// positions compare only for equality, so the check is "different", not "greater".
// Manual impls to avoid spurious `L:` bounds (see token.rs).
// No PartialEq for TokenError: its kind may carry a dyn condition payload ([§dd-dr:errors]) —
// consumers match the kind's variants or downcast the payload.