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
// What: `const TROUBLESHOOT_REF: &str = "..."`. `&str` is a borrowed,
// read-only view of UTF-8 bytes baked into the binary at compile
// time (sibling: `String`, a heap-allocated owned buffer). This
// file keeps its own copy rather than importing `engine.rs`'s
// private const of the same name.
// Why: `&str` (not `String`) because the text is a fixed compile-time
// literal that never changes and never needs to be owned or
// grown; appending it to error messages only borrows it.
// TS map: `const TROUBLESHOOT_REF = "See docs/troubleshooting/resharp.md for workarounds.";`
// -- TS strings are always GC'd and owned, so the
// borrowed-vs-owned distinction is invisible.
//
// In TS you'd write (pseudocode):
// ```ts
// const TROUBLESHOOT_REF = "See docs/troubleshooting/resharp.md for workarounds.";
// ```
const TROUBLESHOOT_REF: &str = "See docs/troubleshooting/resharp.md for workarounds.";
// What: `const MAX_NESTING_DEPTH: usize = 1_000`. `usize` is the
// unsigned integer wide enough to count any byte offset on this
// platform (siblings the reader might expect: `u32`, `u64`,
// `i32`, `i64`). The `1_000` underscore is digit grouping, the
// same value as `1000`.
// Why: `usize` (not `u32`/`u64`) because it is compared against a
// depth counter that itself indexes source bytes, and the std
// length/index APIs all speak `usize`, so matching the width
// avoids casts. The value 1,000 sits below every observed
// resharp stack-overflow floor: about 1,500 in the debug /
// fuzz profile and about 20,000 for complement and lookahead
// nesting in release, and below resharp's `expanded_ast_limit`
// of 50,000. A cap this low keeps even resharp's recursive
// `Drop` of the parsed tree shallow enough not to overflow.
// TS map: `const MAX_NESTING_DEPTH = 1000;` -- TS has one `number` type,
// so there is no width choice to make.
//
// In TS you'd write (pseudocode):
// ```ts
// const MAX_NESTING_DEPTH = 1000;
// ```
const MAX_NESTING_DEPTH: usize = 1_000;
// What: `pub fn nesting_depth(src: &str) -> Option<String>`. Takes a
// borrowed rule source string (`&str`, not an owned `String`,
// because it only reads it) and returns `Option<String>`: the
// present variant `Some(reason)` when the rule nests groups
// deeper than `MAX_NESTING_DEPTH`, or the absent variant `None`
// when the rule is within the cap. `Option` is Rust's stand-in
// for "a value or nothing"; it has no `null`.
// Why: resharp's parser is iterative, but the passes that walk the
// parsed tree afterwards (`expanded_ast_size`, the AST-to-node
// translation, the algebra walks `get_bounded_length` /
// `reverse` / `der` / `contains_look`, and the recursive `Drop`)
// are not depth-bounded. Through 0.6.8, deeply nested complement
// (`~(...)`) or lookaround (`(?=...)`) patterns overflowed the
// stack and aborted the process with a stack-overflow SIGABRT
// during `Regex::new`, below resharp's own size guard, which
// `catch_unwind` cannot intercept. resharp 0.6.9 fixed this
// upstream: the parser now caps recursion at
// `DEFAULT_MAX_DEPTH = 1_000`, rejecting over-deep rules with a
// clean `Parse` error (Bug G in docs/troubleshooting/resharp.md).
// This cheap source-text scan keeps the same 1_000 cap as
// belt-and-suspenders, rejecting the rule before resharp ever
// sees it; over-rejection is fail-closed-safe because the
// production corpus has no deeply nested rules.
// TS map: `function nestingDepth(src: string): string | null` -- the
// `Option<String>` return maps to `string | null`, with
// `Some(x)` being `x` and `None` being `null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function nestingDepth(src: string): string | null {
// // walk bytes; skip char classes and escaped chars; on `(` increment
// // depth and track the max; on `)` decrement. Return a reason string
// // when the max exceeds MAX_NESTING_DEPTH, else null.
// }
// ```