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
//! Format-agnostic tokenizer for `nb` source bytes.
//!
//! The tokenizer owns the **BOM-only
//! preamble** and emits one [`Line`] per physical line. It does
//! not look for `##`/`#`/`=` or any marker — it has no notion of
//! Markdown or any other format.
//!
//! Per the frozen private-intermediate requirement, the
//! tokenizer types and entry point are crate-private (`pub(crate)`).
//! They are visible only to the parser module within this crate;
//! library consumers see the byte-faithful `NoteDocument` surface
//! and nothing of the layered pipeline.
//!
//! Two crate-private types:
//!
//! - [`Preamble`] — a half-open byte range covering 0 or 3
//! bytes (UTF-8 BOM) at the start of source.
//! - [`Line`] — a borrowed slice of source plus its terminator
//! (`b""`, `b"\n"`, `b"\r\n"`, or `b"\r"`).
//!
//! The [`tokenize`] function returns `(Preamble, Vec<Line>)` and
//! is the only entry point.
use Range;
/// The UTF-8 byte-order mark: `EF BB BF`.
const UTF8_BOM: & = b"\xef\xbb\xbf";
/// BOM-only preamble range.
///
/// Zero bytes (`0..0`) when the source begins with no BOM;
/// three bytes (`0..3`) when the source begins with the UTF-8
/// BOM. Leading whitespace
/// and leading blank lines do NOT belong to the preamble — they
/// are ordinary `Line` tokens emitted by [`tokenize`] and owned
/// later by `separator_ranges` / a body fragment.
pub
/// A physical line in the source.
///
/// `terminator` is one of:
///
/// - `b""` — the source ends without a line terminator (the
/// final line is unterminated).
/// - `b"\n"` — Unix LF terminator.
/// - `b"\r\n"` — Windows CRLF terminator.
/// - `b"\r"` — classic Mac CR terminator.
///
/// `content` is the bytes preceding the terminator on the same
/// line. For an empty line the content is `b""` and the
/// `range` covers only the terminator bytes.
///
/// `range` spans `[line_start, terminator_end)` — i.e.,
/// everything the line owns in the source. The assembler
/// reuses these ranges directly without re-deriving them from
/// byte cursors (tokens carry per-line
/// ranges; the assembler reads them as-is).
pub
/// Tokenize `source` into a (BOM preamble, physical lines)
/// pair. Format-agnostic: never inspects `##`/`#`/`=` or any
/// marker.
///
/// An EOF-unterminated final line is emitted with
/// `terminator = b""` and `range = content_range`. The
/// `terminator_end_at` helper returns `pos` unchanged when
/// `pos >= source.len()`, so a final line with content but no
/// terminator is distinguishable from a bare-terminator blank
/// line by `line.content.is_empty()` (zero vs non-zero).
pub
/// Return the byte offset just past the line content at `pos`
/// (i.e., before any line terminator). If `pos` is already at or
/// past end, returns `pos` unchanged.
/// Return the byte offset just past the line terminator
/// starting at `pos` (which must be a `\n`, `\r`, or `\r\n`).
/// If `pos` is past end, returns `pos`.
///
/// A bare `\n` advances one byte. A bare `\r` advances one
/// byte. A `\r\n` pair advances two bytes. No other byte is
/// recognized as a terminator here.