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
//! Comments, and the rule that decides where one starts.
//!
//! A comment starts at a `#` written where a line or a token starts and runs to
//! the end of the line, unless the `#` opens a value:
//!
//! - at the start of a line, after any indentation, a `#` always starts one;
//! - later on a line, after a space or a tab, a `#` starts one only when no
//! character that could continue a bare reference follows it, that is when
//! whitespace, the end of the document, `(`, `)` or `:` comes next. So
//! `a: b # why` and `a: b #` hold a comment, while `tag #release` is the link
//! of `tag` and `#release` (issue #444): a value is never silently dropped.
//!
//! A `#` written inside a token (`issue#1047`) or inside a delimited reference
//! (`"# not a comment"`) is an ordinary character. The parser never sees it: [`strip_comments`] replaces
//! every byte of every comment with a space before the document is parsed, so
//! the notation itself stays exactly as it was and a position reported by the
//! parser still points at the same character of the document the caller wrote.
use crate;
use cratereference_literal_end;
/// The character that opens a comment.
pub const COMMENT: char = '#';
/// What ends a bare reference. A token starts after one of these, at the start
/// of the document, and right after a delimited reference or a versioned
/// literal: the places the parser reads a reference from.
const SEPARATORS: = *b" \t\n\r(:)";
/// What can stand before a comment: the comment is the first thing on a line,
/// or it follows whitespace. A `#` inside a word is part of that word, so
/// `issue#1047` is a reference and not the start of a comment.
const BEFORE_COMMENT: = *b" \t\n\r";
/// Indentation: what may stand between the start of a line and a comment.
const INDENTATION: = *b" \t";
/// Whether the `#` at byte `position` of `document` starts a comment: it opens
/// a token, and either only indentation stands before it on its line or no
/// character that could continue a bare reference follows it.
///
/// The comment stripper and the stream parser both judge a `#` with this one
/// rule, so they always agree on where a comment starts.
pub
/// Blanks out every comment in `document`, keeping every other byte where it
/// was.
///
/// Comments are replaced rather than removed so that a byte offset in the
/// result is the same byte offset in `document`: the line and column a parse
/// error reports are the line and column the writer sees in their file.
///
/// A `#` inside a delimited reference is content, so `"# not a comment"` is
/// still one reference. The document is read token by token, the way the
/// parser reads it, so this holds as well for a reference that starts right
/// after a group, another reference or a literal, and references are read in
/// the document as written, as the parser reads them (issue #377).
///
/// # Examples
/// ```
/// use links_notation::comments::strip_comments;
///
/// assert_eq!(strip_comments("a: b # why\n"), "a: b \n");
/// assert_eq!(strip_comments("\"# kept\"\n"), "\"# kept\"\n");
/// assert_eq!(strip_comments("tag #release\n"), "tag #release\n");
/// assert_eq!(strip_comments("issue#1047\n"), "issue#1047\n");
/// assert_eq!(strip_comments("(a)\" #kept\"\n"), "(a)\" #kept\"\n");
/// ```
/// The offset just past the versioned literal or delimited reference that
/// starts the token at `start`, or `None` when the token there is a bare
/// reference: a delimiter run that never closes is read as one, as the parser
/// reads it.
/// Reports whether the byte before `position` is one of `allowed`, treating the
/// start of the document as one of them.