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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
use *;
/// Apply roxygen2's prose-text pipeline to a raw source run *without* normalizing
/// whitespace — the caller normalizes once over the fully coalesced atom (see
/// [`flush_run`]), so a `Final` block-quote segment can glue seamlessly onto the
/// processed prose on either side. With markdown off the run is literal Rd, where
/// an unescaped `%` begins a comment to end of line (parse_Rd's rule), so the
/// comment is stripped per physical line; with markdown on roxygen2 escapes `%`
/// (`\%`) so it survives and the markdown escapes (backslash runs, `[`/`]`, HTML
/// entities) resolve instead. (Source line breaks stay as `\n`, which the caller's
/// `norm_ws` later collapses, so a comment's end-of-line is honored either way.)
pub
/// Resolve parse_Rd's literal-text escapes in non-`@md` prose. Backslashes pair
/// left-to-right (`\\` → one literal `\`); an unpaired trailing backslash
/// before one of the Rd escape characters `%`, `{`, `}` is consumed with the
/// escape resolved (`\%` → `%`, `\{` → `{`); an unpaired backslash before a
/// brace-required known macro name not followed by `{` re-forms the macro whose
/// missing argument triggers parse_Rd's drop-recovery — the `\name` vanishes
/// and the text continues (`\emph z` → ` z`; see
/// [`is_rd_braceless_drop_macro`], which excludes the sticky code/verbatim-mode
/// names left literal as backlog); an unpaired backslash before anything else
/// stays literal (`a \ b` keeps its backslash). Runs before `%` interact with
/// the line comment: [`strip_rd_comments`] runs first with the same pairing
/// (its `escaped` flip-flop), so a `%` that survives it is always
/// escape-consumed here.
pub
/// Resolve parse_Rd's Rd-string escapes inside a **literal Rd macro's braced
/// argument** (`\code{…}`, `\verb{…}`, `\emph{…}`, `\link{…}`, `\url{…}`, …).
/// parse_Rd lexes every braced argument — verbatim `RCODE`/`VERB` or prose `TEXT`
/// alike — with the same escape rules: backslashes pair left-to-right (`\\` → one
/// literal `\`), and an unpaired trailing backslash before one of the Rd
/// metacharacters `{`, `}`, `%` is consumed with the character rendered bare
/// (`\{` → `{`, `\}` → `}`, `\%` → `%`); any other unpaired backslash stays literal.
///
/// Unlike [`resolve_rd_text_escapes`] this does **no** brace-less-macro drop
/// recovery (an in-argument `\word` is a real nested macro, already carved into a
/// child node, so the run between children is pure argument text) and **no**
/// `%`-comment stripping (`%` inside a braced argument is literal, never a comment).
///
/// Mode-independent: parse_Rd resolves these escapes in a fragile macro's argument
/// under `@md` exactly as it does with markdown off (engine-probed). A markdown
/// code span or fence keeps its `\{` because it projects through a *different* path
/// ([`md_code_atom`]/[`serialize_md_code_block`]/[`verb_atoms`]), never this one.
pub
/// The end index of a brace-required known macro name at `run[start..]` whose
/// brace-less misuse parse_Rd drop-recovers, or `None` when the bytes there are
/// not such a name (or a `{` follows, making it a real macro call — those are
/// carved by the lexer and never reach prose text, but the guard keeps the
/// transform faithful on any input). Shared by the non-`@md` escape resolution
/// and the `@md` backslash-run collapse: the preceding unpaired `\` plus this
/// name vanish from the rendered text.
/// Split each unescaped, brace-less `\item` in a prose inline run into its own
/// [`Inline::BracelessItem`] node, mirroring parse_Rd's out-of-list recovery
/// (`a \item b` → `(TEXT "a") (UNKNOWN "\item") (TEXT "b")`). Recurses into bare
/// brace groups (`{a \item b}` → `(LIST (TEXT "a") (UNKNOWN "\item") (TEXT "b"))`)
/// and emphasis spans so a nested `\item` splits too. Returns `None` when the run
/// contains no brace-less `\item` (the caller keeps the original body). Runs after
/// [`group_brace_lists`], so a following `{…}` has already become an
/// `Inline::BraceGroup` (`\item{x}` → `(UNKNOWN "\item") (LIST (TEXT "x"))`,
/// matching parse_Rd, which never binds the group to the unknown macro).
pub
/// Partition one prose text string at each unescaped, brace-less `\item` (see
/// [`split_braceless_items`]). The interleaved `Inline::Text` (raw, escapes
/// unresolved for the downstream [`process_prose`]) and [`Inline::BracelessItem`]
/// pieces, or `None` when there is no split. Parity-gated like every `\`-carve: a
/// backslash run of even length keeps the final `\` escaped (`\\item` stays literal
/// `\item` text), so only a run of odd length that abuts the exact name `item`
/// begins the macro. The name must be exactly `item` (a longer `\itemize`/`\itemx`
/// is a different macro — the lexer or the drop-recovery handles those).
/// Rewrite an explicit prose tag's body when a brace-less sticky code/verbatim Rd
/// macro (`\code`/`\verb`/…, see [`sticky_braceless_code_mode`]) triggers
/// parse_Rd's argument-mode swallow, or `None` when no such trigger applies (the
/// caller keeps the body). The dropped `\name` and everything after it, to section
/// end, becomes an [`Inline::StickyVerbatim`] — one `RCODE`/`VERB` atom per
/// physical source line — while the prose *before* the trigger stays ordinary text
/// (`a \code z here` → `(TEXT "a") (RCODE " z here\n")`).
///
/// **Scope (this slice): an explicit prose tag with a single-paragraph plain-text
/// tail.** The swallow crosses paragraph breaks in roxygen2, but arity's paragraph
/// model collapses blank-line *counts* (many blanks → one part boundary), so a
/// cross-paragraph tail cannot be reconstructed faithfully from the inline run and
/// is withheld. The tail must also be free of macros/lists/emphasis (they still
/// parse inside the swallow, splitting the RCODE — `\code z \emph{x}` →
/// `(RCODE " z ") (\emph …) …`) and of the raw chars `\ { } %` (a bare `{`/`}`
/// breaks the section's field braces, a `%` acts as an Rd line comment, a stray
/// `\` risks a nested carve) — all withheld as backlog. Withholding leaves the
/// `\code` literal (its current projection), never a wrong shape.
///
/// The intro paragraph is deliberately excluded: its swallow crosses roxygen2's
/// generated field braces (`{`/`}` scaffolding leaks into the atoms), which is
/// unmodelable at section granularity. Only [`project_tag_section`] calls this, so
/// the intro (emitted directly in [`project_block`]) never reaches it.
pub
/// Find the first brace-less sticky code/verbatim macro trigger in one prose text
/// piece: `(backslash index, name end, code)` where `backslash` is the position of
/// the opening `\`, `name_end` is one past the macro name, and `code` is
/// [`sticky_braceless_code_mode`]'s R-code/verbatim flag. Parity-gated like every
/// `\`-carve — only an odd-length backslash run opens a macro (`\\code` is an
/// escaped literal) — and brace-less only (a `{` follows a real macro call, carved
/// by the lexer and never reaching prose text; guarded regardless).
/// Split a sticky swallow's tail into its per-physical-line contents. Physical
/// lines are the [`SOFT_BREAK`] boundaries of the folded run; the first line (the
/// trigger line's remainder) keeps its leading whitespace verbatim
/// (`(RCODE " z here\n")`) in both modes.
///
/// Continuation-line leading whitespace differs by mode. **Non-`@md`:** roxygen2
/// strips only the `#'` comment prefix and one following space, so a `#' cont`
/// line surfaces as `" cont"` (two spaces surviving). **`@md`:** cmark
/// additionally strips a paragraph continuation line's remaining leading
/// whitespace before the swallow captures the rendered text, so `#' cont`
/// surfaces as `"cont"` (fully flush). Trailing `\n` is added per line by the
/// serializer.
/// In `@md` prose, roxygen2 honors a CommonMark backslash escape for the square
/// brackets `[`/`]` only: an escaped `\[`/`\]` is literal (never a link delimiter)
/// *and the backslash is consumed* (`\[`→`[`, `\]`→`]`). This is unique to
/// brackets — roxygen2's `double_escape_md` doubles every backslash but then
/// reverts `\\[`→`\[` and `\\]`→`\]`, so only the bracket escape survives cmark;
/// every other punctuation escape (`\*`, `` \` ``, `\%`, …) keeps its backslash
/// because the doubling neutralizes it. The lexer already suppresses the link at an
/// escaped `[` ([`bracket_is_escaped`](crate::parser::roxygen)); this drops the
/// now-redundant backslash so the projected literal text matches roxygen2.
///
/// Only a *single* adjacent backslash is consumed (`\\[`→`\[`); deeper backslash
/// runs follow `double_escape_md`'s non-overlapping `gsub` semantics and are left
/// as backlog (a `\\\[` run is rare in real docs).
pub
/// Resolve `parse_Rd`'s escaped-brace rendering in the **projected `@md` TEXT**
/// leaves. The leaf arrives carrying the cmark-stage backslash runs
/// ([`collapse_md_backslash_runs`] leaves a run abutting `{`/`}` verbatim), so a run
/// of `k` backslashes before a brace is exactly what parse_Rd receives from
/// `markdown(text)`: it pairs the backslashes left-to-right into `floor(k/2)` literal
/// backslashes, and for **odd** `k` the trailing unpaired `\` escapes the brace to a
/// **bare** literal (`\{` → `{`, `\\\{` → `\{`, `\\\\\{` → `\\{`). A run before any
/// non-brace character — or a bare brace with no preceding backslash — is untouched.
///
/// This runs **after** the section's `rdComplete` drop decision (see
/// [`resolve_md_text_braces`]), which is why it lives here and not in
/// [`process_prose`]: the drop scan must weigh the *escaped* (pre-resolution) brace
/// (roxygen2 runs `rdComplete(markdown(text))` where `\{` is still escaped, so an
/// unbalanced *escaped* brace does not drop the section — `a \{ b` is kept), whereas
/// the rendered TEXT wants the bare brace. Resolving before the scan would count the
/// bare brace and false-drop.
///
/// An **even** `k` pairs off entirely and leaves the brace *unescaped* — a real Rd
/// brace group parse_Rd models as `(LIST …)`. arity keeps flat `TEXT` there (the
/// bare-brace-group model is separate backlog); this transform still halves the
/// backslashes to `k/2` and copies the brace bare, so the projection stays divergent
/// for that shape without compounding the backslash count.
pub
/// Apply [`resolve_md_brace_runs`] to every `(TEXT "…")` leaf in a projected
/// `@md` section string, leaving every other leaf verbatim. The escaped-brace
/// resolution is a **`@md`-mode, prose-TEXT-only** encoding difference: a code
/// span's verbatim `VERB` keeps its `\{` (roxygen2 renders `\verb` content
/// literally), a data-name `RCODE`/`\code` body resolves the brace but is left as
/// backlog here, and non-`@md` TEXT already had its escapes resolved upstream
/// ([`resolve_rd_text_escapes`]). So the transform is gated to `@md` sections by
/// its single caller ([`project_block`]) and scoped to `TEXT` heads here.
///
/// The scan tracks quote state so a literal `(TEXT "` *inside* another leaf's
/// string (e.g. a code span) is copied as data, never mistaken for a leaf opener.
pub
/// In `@md` prose, model roxygen2's `%`-swallow. `%` is the Rd comment character,
/// so roxygen2's markdown→Rd pass escapes a rendered `%` to `\%`; but when the
/// markdown already places a literal backslash immediately before the `%`, that
/// escaping backslash collides with the literal one and the `%` is left **bare** in
/// the Rd, starting a comment that eats to end of line. Whether the collision
/// happens is keyed on the **parity of the source backslash run** before the `%`
/// (`double_escape_md` doubles the run to `2k`, cmark resolves the `\\` pairs, and
/// the emitted Rd carries the `k` literal backslashes plus the one escaping the
/// `%` — a run of `k + 1`, which parse_Rd leaves a trailing bare `%` iff `k` is
/// odd):
///
/// - `k` **odd** (`\%`, `\\\%`, …): the `%` comments to end of line. The `k`
/// backslashes are kept (later halved to `ceil(k/2)` by
/// [`collapse_md_backslash_runs`]) and everything from the `%` to the physical
/// line's end is dropped.
/// - `k` **even** (bare `%`, `\\%`, `\\\\%`, …): the `%` survives as a literal
/// percent; the run keeps its `ceil(k/2)` backslashes and the `%`.
///
/// The swallow is line-scoped (roxygen2's comment ends at the newline, and a
/// soft-wrapped continuation on the next `#'` line survives), mirroring the
/// non-`@md` [`strip_rd_comments`]. It runs **before** [`collapse_md_backslash_runs`]
/// so the odd/even decision reads the original run length, not its halved form.
pub
/// The prefix of `line` up to (not including) the first `%` whose preceding
/// maximal backslash run has **odd** length (the whole line if none); the kept
/// backslashes are retained for [`collapse_md_backslash_runs`] to halve.
/// Strip each odd-run `\%` comment region from a prose `TEXT` leaf **for the
/// `@md` `rdComplete` drop scan only**. Per physical line, the backslash run, the
/// `%`, and everything to the line's end are dropped.
///
/// roxygen2 decides the drop on `rdComplete(markdown(text))`. In `markdown(text)`
/// an odd source backslash run before a `%` renders an *even* run plus a bare
/// comment `%` (`\%` → `\\%`, `\\\%` → `\\\%`): the even run pairs cleanly and the
/// bare `%` comments to end of line, so the region contributes nothing to the
/// brace balance and never leaves a dangling escape. The **output** serializer
/// models the same swallow via [`md_percent_swallow`], but it keeps `ceil(k/2)`
/// backslashes — parse_Rd's rendered text (`y \% …` → `y \`) — which can leave an
/// *odd* trailing backslash at the section's end. Reconstructing the scan from
/// those output atoms then reads a dangling escape and false-drops a section
/// roxygen2 keeps (`@details y \% {z} end.`). Dropping the whole region here (not
/// just the tail) removes that backslash so the scan matches `markdown(text)`. An
/// **even**-run `%` — a genuine literal percent roxygen2 escapes to `\%` — is left
/// untouched for render-time re-escaping ([`append_leaf_text`]).
pub
/// The prefix of `line` up to (not including) the backslash run of the first `%`
/// whose preceding maximal backslash run has **odd** length (the whole line if
/// none). Unlike [`md_percent_swallow_line`], the backslashes are dropped too, so
/// no trailing escape survives into the [`rd_complete`] scan.
/// Rewrite a prose body's top-level `TEXT` leaves for the `@md` `rdComplete` drop
/// scan, applying [`strip_scan_percent_comment`]. Returns `None` when no leaf
/// changed (the common case — no odd-run `\%`), so the scan reuses the original
/// body. Only top-level prose is stripped: a `%`-comment inside a balanced macro
/// argument contributes a balanced pair regardless (backlog).
pub
/// In `@md` prose, a run of literal backslashes collapses per CommonMark's
/// backslash escaping. roxygen2's `double_escape_md` doubles every backslash
/// (`k` → `2k`), cmark then resolves each `\\` pair to one literal backslash
/// (`2k` → `k`), and finally `parse_Rd` collapses the rendered `\\` pairs again
/// (`k` → `ceil(k/2)`, the trailing odd backslash escaping the next character).
/// The net effect on the parsed text is that a run of `k` source backslashes
/// renders as `ceil(k/2)` backslashes: a lone `\` (`\*`, `` \` ``, `\_`, …) keeps
/// its single backslash (`ceil(1/2) == 1`, a no-op), while `\\` → `\`,
/// `\\\\` → `\\`, and so on.
///
/// A run immediately before `[`/`]` is left untouched — those bracket escapes
/// follow `double_escape_md`'s revert (`\\[` → `\[`) and are resolved separately
/// by [`unescape_md_brackets`], which runs after this. A run before `{`/`}` is
/// **also** left untouched, at cmark's `k`-backslash stage: parse_Rd's brace
/// resolution is parity-dependent and is deferred to the post-pass
/// ([`resolve_md_brace_runs`]) so the `rdComplete` scan can weigh the still-escaped
/// brace first (halving here would destroy the parity it needs). Runs before `%`
/// (the Rd comment character) are also left to the separate `%`-swallow modeling (a
/// lone `\%` keeps its backslash but the bare `%` still comments to end of line);
/// `ceil(k/2)` is a no-op for the common `k == 1` case there anyway.
///
/// An **odd** run before a brace-required known macro name not followed by `{`
/// is the brace-less misuse: the `k` source backslashes reach parse_Rd intact
/// (double → cmark halves), which pairs them into `k/2` literal backslashes and
/// re-forms the trailing `\name` — whose missing argument drop-recovers, so the
/// name vanishes (`\emph z` → ` z`, `\\\link q` → `\ q`; see
/// [`braceless_drop_name_end`]). Mirrors the non-`@md`
/// [`resolve_rd_text_escapes`].
pub