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
//! Structs and macros for searching source for combinations of byte values.
//!
//! * `SafeByteMatchTable` is a lookup table type for byte values.
//! * The `safe_byte_match_table!` macro creates those tables at compile time.
//! * The `byte_search!` macro searches source text for the first byte matching a
//! `SafeByteMatchTable`.
/// Batch size for searching
pub const SEARCH_BATCH_SIZE: usize = 32;
/// Safe byte matcher lookup table.
///
/// Create table at compile time as a `static` or `const` with `safe_byte_match_table!` macro.
/// Test bytes against table with `SafeByteMatchTable::matches`.
/// Or use `byte_search!` macro to search for first matching byte in source.
///
/// `byte_search!` using this table is guaranteed to leave `lexer.source` positioned on a UTF-8
/// character boundary, provided that its starting position is already on a UTF-8 character
/// boundary.
///
/// To make this guarantee, one of the following must be true:
///
/// 1. Table contains `true` for all byte values 192 - 247
/// i.e. first byte of any multi-byte Unicode character matches.
/// (NB: 248 - 255 cannot occur in UTF-8 strings)
/// e.g.
/// * `safe_byte_match_table!(|b| b >= 192)`
/// * `safe_byte_match_table!(|b| !b.is_ascii())`
///
/// 2. Table contains `false` for all byte values 128 - 191
/// i.e. the continuation bytes of any multi-byte Unicode chars will be consumed in full.
/// e.g.
/// * `safe_byte_match_table!(|b| b < 128 || b >= 192)`
/// * `safe_byte_match_table!(|b| b.is_ascii())`
/// * `safe_byte_match_table!(|b| b == ' ' || b == '\t')`
///
/// This is statically checked by `SafeByteMatchTable::new`, and will fail to compile if match
/// pattern does not satisfy one of the above.
///
/// # Examples
/// ```rust,ignore
/// use crate::lexer::search::{SafeByteMatchTable, safe_byte_match_table};
///
/// static NOT_ASCII: SafeByteMatchTable = safe_byte_match_table!(|b| !b.is_ascii());
/// assert_eq!(NOT_ASCII.matches(b'X'), false);
/// assert_eq!(NOT_ASCII.matches(192), true);
///
/// impl<'a> Lexer<'a> {
/// fn eat_ascii(&mut self) {
/// // NB: Using `byte_search!` macro with a `SafeByteMatchTable` is safe
/// byte_search! {
/// lexer: self,
/// table: NOT_ASCII,
/// handle_match: |matched_byte, start| {},
/// handle_eof: |start| {},
/// };
/// }
/// }
/// ```
;
/// Macro to create a `SafeByteMatchTable` at compile time.
///
/// `safe_byte_match_table!(|b| !b.is_ascii())` expands to:
///
/// ```rust,ignore
/// {
/// use crate::lexer::search::SafeByteMatchTable;
/// #[allow(clippy::eq_op, clippy::allow_attributes)]
/// const TABLE: SafeByteMatchTable = SafeByteMatchTable::new([
/// (!0u8.is_ascii()),
/// (!1u8.is_ascii()),
/// /* ... */
/// (!255u8.is_ascii()),
/// ]);
/// TABLE
/// }
/// ```
pub use safe_byte_match_table;
/// Macro to search for first byte matching a `SafeByteMatchTable`.
///
/// Search processes source in batches of `SEARCH_BATCH_SIZE` bytes for speed.
/// When not enough bytes remaining in source for a batch, search source byte by byte.
///
/// This is a macro rather than a function because searching is a bit faster when all the code
/// is in a single function, and some parts (e.g. `continue_if`) can be statically removed by
/// the compiler if they're not used.
///
/// Used as follows:
///
/// ```rust,ignore
/// static NOT_STUFF_TABLE: SafeByteMatchTable = safe_byte_match_table!(|b| !is_stuff(b));
///
/// impl<'a> Lexer<'a> {
/// fn eat_stuff(&mut self) -> bool {
/// let matched_byte = byte_search! {
/// lexer: self,
/// table: NOT_STUFF_TABLE,
/// handle_eof: {
/// // No bytes from start position to end of source matched the table.
/// // `lexer.source` is now positioned at EOF.
/// // Evaluate to a `u8` which macro call will evaluate to.
/// 0xFF
/// // Or can `return` from enclosing function e.g. `return false;`
/// },
/// };
///
/// // Matching byte has been found.
/// // `matched_byte` is `u8` value of first byte which matched the table
/// // (or `0xFF` if EOF, because `handle_eof` evaluates to `0xFF`).
/// // `lexer.source` is now positioned on first matching byte.
/// // Handle the next matching byte (deal with any special cases).
/// matched_byte == b'X'
/// }
/// }
/// ```
///
/// or provide the `SourcePosition` to start searching from:
///
/// ```rust,ignore
/// impl<'a> Lexer<'a> {
/// fn eat_stuff(&mut self) -> bool {
/// let start = unsafe { self.source.position().add(1) };
/// let matched_byte = byte_search! {
/// lexer: self,
/// table: NOT_STUFF_TABLE,
/// start: start,
/// handle_eof: {
/// // No bytes from start position to end of source matched the table.
/// // `lexer.source` is now positioned at EOF.
/// return false;
/// },
/// };
///
/// // Matching byte has been found.
/// // `matched_byte` is `u8` value of first byte which matched the table.
/// // `lexer.source` is now positioned on first matching byte.
/// // Handle the next matching byte (deal with any special cases).
/// matched_byte == b'X'
/// }
/// }
/// ```
///
/// Can also add a block to decide whether to continue searching for some matches:
///
/// ```rust,ignore
/// impl<'a> Lexer<'a> {
/// fn eat_stuff(&mut self) -> bool {
/// let matched_byte = byte_search! {
/// lexer: self,
/// table: NOT_STUFF_TABLE,
/// continue_if: (matched_byte, pos) {
/// // Matching byte found. Decide whether it's really a match.
/// // Return `true` to continue searching, or `false` to end search.
/// // NB: `lexer.source` has NOT been updated at this point.
/// if matched_byte == 0xE2 {
/// // Only match a specific Unicode char (in this case 0xE2, 0x80, 0xA8)
/// // NB: We don't need to check if `pos` is at EOF here, as 0xE2 is always 1st byte
/// // of a 3-byte Unicode char, but if matching an ASCII char, would need to make sure
/// // don't read out of bounds.
/// unsafe { pos.add(1).read2() != [0x80, 0xA8] }
/// } else {
/// // End search for all other possibilities
/// false
/// }
/// },
/// handle_eof: {
/// // No bytes from start position to end of source matched the table.
/// // `lexer.source` is now positioned at EOF.
/// return false;
/// },
/// };
///
/// // Matching byte has been found.
/// // `matched_byte` is `u8` value of first byte which matched the table.
/// // `lexer.source` is now positioned on first matching byte.
/// // Handle the next matching byte (deal with any special cases).
/// matched_byte == b'X'
/// }
/// }
/// ```
///
/// # SAFETY
///
/// This macro consumes bytes from `lexer.source` according to the provided `SafeByteMatchTable`.
///
/// The `start` position must be on a UTF-8 character boundary. The overloads without an explicit
/// `start` use the current position of `lexer.source`.
///
/// Using `byte_search!` with the provided `SafeByteMatchTable` is guaranteed to leave
/// `lexer.source` positioned on a UTF-8 character boundary when entering `handle_match`, which
/// makes the internal calls to `Source`'s unsafe methods sound.
}};
// With provided `start` position
=> ;
// Actual implementation - with both `start` and `continue_if`
=> ;
}
pub use byte_search;