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
use crate::parser::Parser;
use crate::support::{find_byte, scan_label_chars};
use mos_core::codes;
use crate::{Inline, InlineKind};
#[derive(Clone, Copy, Debug, Default)]
enum InlineStyle {
#[default]
Plain,
Emphasis,
Strong,
BoldItalic,
}
impl InlineStyle {
fn with(self, delimiter: Delimiter) -> Self {
match delimiter {
Delimiter::Strong => self.with_strong(),
Delimiter::Emphasis => self.with_emphasis(),
}
}
fn with_strong(self) -> Self {
match self {
Self::Plain => Self::Strong,
Self::Emphasis | Self::Strong | Self::BoldItalic => Self::BoldItalic,
}
}
fn with_emphasis(self) -> Self {
match self {
Self::Plain => Self::Emphasis,
Self::Strong | Self::Emphasis | Self::BoldItalic => Self::BoldItalic,
}
}
fn kind(self) -> InlineKind {
match self {
Self::Plain => InlineKind::Text,
Self::Emphasis => InlineKind::Emphasis,
Self::Strong => InlineKind::Strong,
Self::BoldItalic => InlineKind::BoldItalic,
}
}
}
#[derive(Clone, Copy, Debug)]
enum Delimiter {
Emphasis,
Strong,
}
impl Delimiter {
fn width(self) -> usize {
match self {
Self::Emphasis => 1,
Self::Strong => 2,
}
}
}
struct ParsedSegment {
inlines: Vec<Inline>,
next: usize,
closed: Option<ClosedDelimiter>,
}
struct ClosedDelimiter {
end: usize,
}
impl Parser<'_> {
/// Tokenize `slice` (whose first byte sits at `base` in `self.src`)
/// into inline runs. Backtick code and `@label` references are
/// atomic; emphasis delimiters can nest into bold+italic text runs.
pub(crate) fn parse_inlines(&mut self, slice: &str, base: usize) -> Vec<Inline> {
self.parse_inline_segment(slice, base, 0, InlineStyle::default(), None)
.inlines
}
fn parse_inline_segment(
&mut self,
slice: &str,
base: usize,
from: usize,
style: InlineStyle,
close: Option<Delimiter>,
) -> ParsedSegment {
let bytes = slice.as_bytes();
let mut out: Vec<Inline> = Vec::new();
// `pending` accumulates characters that belong to the current
// styled text run but aren't a verbatim slice of `slice` — at
// the moment, only the soft-hyphen shorthand `\-` (which
// contributes a U+00AD codepoint without `\` or `-` ever
// appearing in the run). When `pending` is non-empty, the run
// can't be captured by a single `slice[start..end]` so we
// switch to a `String`-buffered flush path. `pending_source_start`
// remembers the first source byte that fed `pending` so the
// emitted Inline's span still covers the full source extent
// (including the consumed `\-` bytes).
let mut pending: String = String::new();
let mut pending_source_start: Option<usize> = None;
let mut i = from;
let mut text_start = from;
while i < bytes.len() {
let c = bytes[i];
if c == b'\\' {
if i + 1 < bytes.len() && bytes[i + 1] == b'\\' {
self.flush_styled_text_with_pending(
&mut out,
slice,
base,
text_start,
i,
style,
&mut pending,
&mut pending_source_start,
);
out.push(Inline {
kind: InlineKind::HardBreak,
text: String::new(),
span: self.span(base + i, base + i + 2),
});
i += 2;
text_start = i;
continue;
}
if i + 1 < bytes.len() && bytes[i + 1] == b'-' {
// `\-` -> literal U+00AD soft hyphen. Splice the
// preceding slice text into `pending`, append the
// SHY codepoint, skip both source bytes. Remember
// the earliest source byte covered by `pending` so
// the eventual flush spans the original `\-` bytes
// instead of collapsing to a zero-width range.
if pending_source_start.is_none() {
pending_source_start = Some(text_start);
}
pending.push_str(&slice[text_start..i]);
pending.push('\u{AD}');
i += 2;
text_start = i;
continue;
}
// Backslash followed by anything other than `\` or `-`
// is left to fall through as a literal `\` byte (the
// slice path picks it up at the next flush). A lone
// trailing `\` at end-of-input gets a warning so the
// author notices a likely-incomplete escape; a `\`
// followed by some other character is kept silent
// because the previous behaviour was "backslash is
// literal", and emitting a diagnostic for every
// `C:\Temp` / `\*foo*` / etc. would be noisy.
if i + 1 >= bytes.len() {
self.diagnostics.push(self.warn(
&codes::MOS0038,
"lone trailing `\\` is not a recognized escape; treated as literal text",
base + i,
base + i + 1,
));
}
i += 1;
continue;
}
if c == b'*' {
let run_len = star_run_len(bytes, i);
if let Some(delimiter) = close
&& delimiter_closes(delimiter, run_len)
{
self.flush_styled_text_with_pending(
&mut out,
slice,
base,
text_start,
i,
style,
&mut pending,
&mut pending_source_start,
);
let width = delimiter.width();
return ParsedSegment {
inlines: out,
next: i + width,
closed: Some(ClosedDelimiter { end: i + width }),
};
}
let delimiter = if run_len >= 2 {
Delimiter::Strong
} else {
Delimiter::Emphasis
};
let diagnostic_checkpoint = self.diagnostics.len();
let parsed = self.parse_inline_segment(
slice,
base,
i + delimiter.width(),
style.with(delimiter),
Some(delimiter),
);
if let Some(closed) = parsed.closed {
self.flush_styled_text_with_pending(
&mut out,
slice,
base,
text_start,
i,
style,
&mut pending,
&mut pending_source_start,
);
let mut children = parsed.inlines;
widen_span_to_delimiters(&mut children, base + i, base + closed.end);
out.extend(children);
i = parsed.next;
text_start = i;
continue;
}
self.diagnostics.truncate(diagnostic_checkpoint);
if close.is_none() {
self.warn_unterminated_delimiter(base, i, delimiter);
}
i += delimiter.width();
continue;
}
if c == b'`' {
if let Some(end) = find_byte(bytes, b'`', i + 1) {
self.flush_styled_text_with_pending(
&mut out,
slice,
base,
text_start,
i,
style,
&mut pending,
&mut pending_source_start,
);
out.push(Inline {
kind: InlineKind::Code,
text: slice[i + 1..end].to_owned(),
span: self.span(base + i, base + end + 1),
});
i = end + 1;
text_start = i;
continue;
}
self.diagnostics.push(self.warn(
&codes::MOS0034,
"unterminated `` `code` `` run; treated as text",
base + i,
base + i + 1,
));
i += 1;
continue;
}
if c == b'@' {
let id_end = scan_label_chars(bytes, i + 1);
if id_end > i + 1 {
self.flush_styled_text_with_pending(
&mut out,
slice,
base,
text_start,
i,
style,
&mut pending,
&mut pending_source_start,
);
out.push(Inline {
kind: InlineKind::Reference,
text: slice[i + 1..id_end].to_owned(),
span: self.span(base + i, base + id_end),
});
i = id_end;
text_start = i;
continue;
}
self.diagnostics.push(self.warn(
&codes::MOS0036,
"stray `@` is not followed by a label identifier; treated as text",
base + i,
base + i + 1,
));
i += 1;
continue;
}
if c == b'[' && i + 1 < bytes.len() && bytes[i + 1] == b'@' {
// `[@key]` — citation. Only enter the citation branch
// once we have seen `[@`, so a bare `[` keeps its
// current literal-text behaviour and never warns.
let key_start = i + 2;
let key_end = scan_label_chars(bytes, key_start);
if key_end > key_start && key_end < bytes.len() && bytes[key_end] == b']' {
self.flush_styled_text_with_pending(
&mut out,
slice,
base,
text_start,
i,
style,
&mut pending,
&mut pending_source_start,
);
let end = key_end + 1;
out.push(Inline {
kind: InlineKind::Citation,
text: slice[key_start..key_end].to_owned(),
span: self.span(base + i, base + end),
});
i = end;
text_start = i;
continue;
}
// Either the key was empty, the `]` was missing, or
// the body uses a not-yet-supported form (`[@a; @b]`,
// prefix/suffix). Warn once and *consume* the
// citation-candidate extent so the trailing `@key`
// bytes don't fall back through to the `@`-reference
// branch — that would surface a bogus MOS0033 in the
// resolver for what was syntactically a malformed
// citation, not an unknown label.
//
// Recovery extent:
// * if a `]` exists later in this inline slice,
// consume up to and including it (covers
// `[@a; @b]`, `[@see @key, p. 33]`, `[@]`);
// * otherwise skip past `[@` only (covers truly
// unterminated `[@key…` at end of paragraph) and
// let the bare key chars settle as literal text.
let recovery_end = if let Some(close) = find_byte(bytes, b']', key_start) {
close + 1
} else {
key_start
};
self.diagnostics.push(self.warn(
&codes::MOS0039,
"malformed citation `[@…]`; expected `[@key]`; treated as text",
base + i,
base + recovery_end,
));
i = recovery_end;
continue;
}
i += 1;
}
self.flush_styled_text_with_pending(
&mut out,
slice,
base,
text_start,
bytes.len(),
style,
&mut pending,
&mut pending_source_start,
);
ParsedSegment {
inlines: out,
next: bytes.len(),
closed: None,
}
}
/// Flush `slice[from..to]` (possibly prefixed by buffered `pending`
/// text from earlier escape expansions like `\-` → U+00AD) into a
/// single styled-text inline. The span covers the full source range
/// from the earliest byte that fed `pending` (or `from` when pending
/// is empty) through `to`, so emitted inlines whose text includes
/// expanded escapes still carry a span covering the original source
/// bytes — including the consumed `\-` markers.
#[allow(
clippy::too_many_arguments,
reason = "transitional: extends the existing `flush_styled_text` (7-arg) with a buffered-text channel and a pending-source-start tracker for escape expansion. Bundling the slice/base/style triple into a context struct would churn every call site in `parse_inline_segment` for no net clarity."
)]
fn flush_styled_text_with_pending(
&self,
out: &mut Vec<Inline>,
slice: &str,
base: usize,
from: usize,
to: usize,
style: InlineStyle,
pending: &mut String,
pending_source_start: &mut Option<usize>,
) {
if pending.is_empty() {
// Defensive: pending_source_start should always be paired
// with a non-empty pending. Clear it anyway so a future
// escape that splices into `pending` starts from a fresh
// state.
*pending_source_start = None;
self.flush_styled_text(out, slice, base, from, to, style);
return;
}
let mut text = std::mem::take(pending);
if from < to {
text.push_str(&slice[from..to]);
}
let span_from = pending_source_start.take().unwrap_or(from);
out.push(Inline {
kind: style.kind(),
text,
span: self.span(base + span_from, base + to),
});
}
fn flush_styled_text(
&self,
out: &mut Vec<Inline>,
slice: &str,
base: usize,
from: usize,
to: usize,
style: InlineStyle,
) {
if from < to {
out.push(Inline {
kind: style.kind(),
text: slice[from..to].to_owned(),
span: self.span(base + from, base + to),
});
}
}
fn warn_unterminated_delimiter(&mut self, base: usize, i: usize, delimiter: Delimiter) {
match delimiter {
Delimiter::Strong => self.diagnostics.push(self.warn(
&codes::MOS0028,
"unterminated `**strong**` run; treated as text",
base + i,
base + i + 2,
)),
Delimiter::Emphasis => self.diagnostics.push(self.warn(
&codes::MOS0031,
"unterminated `*emphasis*` run; treated as text",
base + i,
base + i + 1,
)),
}
}
}
fn star_run_len(bytes: &[u8], from: usize) -> usize {
let mut end = from;
while end < bytes.len() && bytes[end] == b'*' {
end += 1;
}
end - from
}
fn delimiter_closes(delimiter: Delimiter, run_len: usize) -> bool {
match delimiter {
Delimiter::Strong => run_len >= 2,
Delimiter::Emphasis => run_len % 2 == 1,
}
}
fn widen_span_to_delimiters(inlines: &mut [Inline], start: usize, end: usize) {
if let Some(first) = inlines.first_mut() {
first.span.start = start;
}
if let Some(last) = inlines.last_mut() {
last.span.end = end;
}
}