lexer_kit 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +265 -0
- data/Cargo.toml +37 -0
- data/README.md +7 -3
- data/crates/lexer_kit_core/Cargo.toml +17 -0
- data/crates/lexer_kit_core/src/dfa.rs +437 -0
- data/crates/lexer_kit_core/src/format.rs +599 -0
- data/crates/lexer_kit_core/src/lib.rs +18 -0
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/opcodes.rs +375 -1
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/safety_test.rs +21 -16
- data/crates/lexer_kit_core/src/token_buffer.rs +130 -0
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/trie.rs +37 -1
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/types.rs +1 -1
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/vm.rs +165 -2
- data/ext/lexer_kit_rust/Cargo.toml +7 -3
- data/ext/lexer_kit_rust/src/fast_stream.rs +101 -438
- data/ext/lexer_kit_rust/src/lib.rs +200 -215
- data/lib/lexer_kit/builder/compiler.rb +50 -15
- data/lib/lexer_kit/builder/conflict_detector.rb +18 -20
- data/lib/lexer_kit/builder/validator.rb +45 -0
- data/lib/lexer_kit/builder.rb +34 -7
- data/lib/lexer_kit/cli/commands.rb +17 -7
- data/lib/lexer_kit/core/source.rb +3 -2
- data/lib/lexer_kit/debug/disassembler.rb +0 -2
- data/lib/lexer_kit/dfa/dfa_builder.rb +1 -1
- data/lib/lexer_kit/dfa/dfa_minimizer.rb +2 -2
- data/lib/lexer_kit/dfa/regex_ast.rb +4 -0
- data/lib/lexer_kit/dfa/regex_parser.rb +41 -8
- data/lib/lexer_kit/errors.rb +2 -2
- data/lib/lexer_kit/format/lkb1.rb +3 -3
- data/lib/lexer_kit/format/lkt1.rb +3 -1
- data/lib/lexer_kit/format.rb +4 -4
- data/lib/lexer_kit/ir/compiled_program.rb +39 -26
- data/lib/lexer_kit/ir/constant_pool.rb +33 -11
- data/lib/lexer_kit/ir/dfa_table.rb +9 -19
- data/lib/lexer_kit/ir/instruction.rb +3 -0
- data/lib/lexer_kit/ir/jump_table.rb +3 -13
- data/lib/lexer_kit/ir/keyword_table.rb +0 -9
- data/lib/lexer_kit/ir/limits.rb +48 -0
- data/lib/lexer_kit/ir/serializer.rb +69 -30
- data/lib/lexer_kit/ir.rb +1 -0
- data/lib/lexer_kit/trie.rb +1 -1
- data/lib/lexer_kit/version.rb +1 -1
- data/lib/lexer_kit.rb +20 -2
- data/vendor/nupur/Cargo.toml +24 -0
- data/vendor/nupur/LICENSE +176 -0
- data/vendor/nupur/NOTICE +64 -0
- data/vendor/nupur/README.ja.md +150 -0
- data/vendor/nupur/README.md +257 -0
- data/vendor/nupur/src/abi.rs +143 -0
- data/vendor/nupur/src/args.rs +318 -0
- data/vendor/nupur/src/build.rs +101 -0
- data/vendor/nupur/src/caught.rs +171 -0
- data/vendor/nupur/src/cname.rs +37 -0
- data/vendor/nupur/src/describe.rs +175 -0
- data/vendor/nupur/src/encoding.rs +240 -0
- data/vendor/nupur/src/gvl.rs +181 -0
- data/vendor/nupur/src/hash.rs +52 -0
- data/vendor/nupur/src/inject.rs +20 -0
- data/vendor/nupur/src/lib.rs +280 -0
- data/vendor/nupur/src/lint_probe.rs +142 -0
- data/vendor/nupur/src/msg.rs +193 -0
- data/vendor/nupur/src/no_unwind.rs +39 -0
- data/vendor/nupur/src/raw/check.rs +71 -0
- data/vendor/nupur/src/raw/data.rs +28 -0
- data/vendor/nupur/src/raw/define.rs +215 -0
- data/vendor/nupur/src/raw/exception.rs +69 -0
- data/vendor/nupur/src/raw/iterate.rs +115 -0
- data/vendor/nupur/src/raw/mod.rs +105 -0
- data/vendor/nupur/src/raw/protect.rs +144 -0
- data/vendor/nupur/src/raw/string.rs +148 -0
- data/vendor/nupur/src/raw/symbol.rs +38 -0
- data/vendor/nupur/src/raw/values.rs +293 -0
- data/vendor/nupur/src/rb/call.rs +118 -0
- data/vendor/nupur/src/rb/class.rs +170 -0
- data/vendor/nupur/src/rb/convert.rs +134 -0
- data/vendor/nupur/src/rb/error.rs +355 -0
- data/vendor/nupur/src/rb/global.rs +141 -0
- data/vendor/nupur/src/rb/kept.rs +55 -0
- data/vendor/nupur/src/rb/method/frame.rs +81 -0
- data/vendor/nupur/src/rb/method.rs +468 -0
- data/vendor/nupur/src/rb/mod.rs +40 -0
- data/vendor/nupur/src/rb/object.rs +66 -0
- data/vendor/nupur/src/rb/ruby.rs +107 -0
- data/vendor/nupur/src/rb/value/header.rs +65 -0
- data/vendor/nupur/src/rb/value/id.rs +61 -0
- data/vendor/nupur/src/rb/value/integer.rs +132 -0
- data/vendor/nupur/src/rb/value.rs +454 -0
- data/vendor/nupur/src/stack.rs +32 -0
- data/vendor/nupur/src/string/make.rs +234 -0
- data/vendor/nupur/src/string/mod.rs +24 -0
- data/vendor/nupur/src/string/read.rs +411 -0
- data/vendor/nupur/src/table.rs +58 -0
- data/vendor/nupur/src/typed/callbacks.rs +125 -0
- data/vendor/nupur/src/typed/data_type.rs +132 -0
- data/vendor/nupur/src/typed/embedded.rs +103 -0
- data/vendor/nupur/src/typed/external.rs +76 -0
- data/vendor/nupur/src/typed/hidden.rs +95 -0
- data/vendor/nupur/src/typed/mod.rs +373 -0
- data/vendor/nupur/src/utf8_copy.rs +262 -0
- data/vendor/nupur/sys/Cargo.toml +29 -0
- data/vendor/nupur/sys/src/data_type.rs +213 -0
- data/vendor/nupur/sys/src/int.rs +248 -0
- data/vendor/nupur/sys/src/lib.rs +61 -0
- data/vendor/nupur/sys/src/lint_probe.rs +93 -0
- data/vendor/nupur/sys/src/no_unwind.rs +99 -0
- data/vendor/nupur/sys/src/non_raising.rs +585 -0
- data/vendor/nupur/sys/src/protect.rs +349 -0
- data/vendor/nupur/sys/src/raising.rs +257 -0
- data/vendor/nupur/sys/src/value.rs +279 -0
- data/vendor/nupur.sha256 +68 -0
- metadata +102 -8
- data/ext/lexer_kit_rust/src/deserializer.rs +0 -213
- data/ext/lexer_kit_rust/src/dfa.rs +0 -217
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
//! DFA (Deterministic Finite Automaton) execution
|
|
2
|
+
//!
|
|
3
|
+
//! This module implements DFA-based pattern matching for the lexer VM.
|
|
4
|
+
|
|
5
|
+
use crate::types::{DfaTable, DFA_DEAD_STATE, DFA_NO_ACCEPT};
|
|
6
|
+
|
|
7
|
+
/// DFA match result (length of the match)
|
|
8
|
+
#[derive(Debug, Clone, Copy)]
|
|
9
|
+
pub struct DfaMatch {
|
|
10
|
+
pub length: usize,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
impl DfaTable {
|
|
14
|
+
/// Run DFA on input bytes, returning the length of the longest match.
|
|
15
|
+
///
|
|
16
|
+
/// Returns None if no state accepts. The accept token value only marks a state as accepting.
|
|
17
|
+
/// Supports zero-length matches (e.g., "a*" matching empty string).
|
|
18
|
+
pub fn run(&self, bytes: &[u8]) -> Option<DfaMatch> {
|
|
19
|
+
// Safety check: verify DFA has valid dimensions
|
|
20
|
+
if self.state_count == 0 || self.class_count == 0 {
|
|
21
|
+
return None;
|
|
22
|
+
}
|
|
23
|
+
if self.byte_class.len() < 256 {
|
|
24
|
+
return None;
|
|
25
|
+
}
|
|
26
|
+
if self.transitions.is_empty() || self.accept_tokens.is_empty() {
|
|
27
|
+
return None;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let mut state: u16 = 1; // Start state (0 is dead state)
|
|
31
|
+
let mut last_accept: Option<DfaMatch> = None;
|
|
32
|
+
|
|
33
|
+
// Validate start state is within bounds
|
|
34
|
+
if state >= self.state_count {
|
|
35
|
+
return None;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Check if start state is accepting (handles zero-length matches like "a*")
|
|
39
|
+
if self.accept_tokens[state as usize] != DFA_NO_ACCEPT {
|
|
40
|
+
last_accept = Some(DfaMatch { length: 0 });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
for (i, &byte) in bytes.iter().enumerate() {
|
|
44
|
+
let cls = self.byte_class[byte as usize];
|
|
45
|
+
let idx = (state as usize) * (self.class_count as usize) + (cls as usize);
|
|
46
|
+
|
|
47
|
+
if idx >= self.transitions.len() {
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
state = self.transitions[idx];
|
|
52
|
+
|
|
53
|
+
if state == DFA_DEAD_STATE || state >= self.state_count {
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Check if accepting state
|
|
58
|
+
if self.accept_tokens[state as usize] != DFA_NO_ACCEPT {
|
|
59
|
+
last_accept = Some(DfaMatch { length: i + 1 });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
last_accept
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/// Scan until delimiter found (using memchr for optimization)
|
|
68
|
+
///
|
|
69
|
+
/// Returns the position where delimiter starts, or input length if not found.
|
|
70
|
+
pub fn scan_until(bytes: &[u8], delim: &[u8]) -> usize {
|
|
71
|
+
if delim.is_empty() {
|
|
72
|
+
return bytes.len();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if delim.len() == 1 {
|
|
76
|
+
// Single-byte delimiter: use memchr for SIMD optimization
|
|
77
|
+
return memchr_single(bytes, delim[0]).unwrap_or(bytes.len());
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Multi-byte delimiter: search for first byte, then check rest
|
|
81
|
+
let first = delim[0];
|
|
82
|
+
let mut pos = 0;
|
|
83
|
+
|
|
84
|
+
while pos < bytes.len() {
|
|
85
|
+
match memchr_single(&bytes[pos..], first) {
|
|
86
|
+
Some(idx) => {
|
|
87
|
+
let abs_idx = pos + idx;
|
|
88
|
+
if abs_idx + delim.len() > bytes.len() {
|
|
89
|
+
return bytes.len();
|
|
90
|
+
}
|
|
91
|
+
if &bytes[abs_idx..abs_idx + delim.len()] == delim {
|
|
92
|
+
return abs_idx;
|
|
93
|
+
}
|
|
94
|
+
pos = abs_idx + 1;
|
|
95
|
+
}
|
|
96
|
+
None => return bytes.len(),
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
bytes.len()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/// Simple memchr implementation
|
|
104
|
+
#[inline]
|
|
105
|
+
fn memchr_single(haystack: &[u8], needle: u8) -> Option<usize> {
|
|
106
|
+
haystack.iter().position(|&b| b == needle)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// Match literal string at current position
|
|
110
|
+
///
|
|
111
|
+
/// Returns true if the bytes at the start match the literal.
|
|
112
|
+
#[inline]
|
|
113
|
+
pub fn match_literal(bytes: &[u8], literal: &[u8]) -> bool {
|
|
114
|
+
if bytes.len() < literal.len() {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
&bytes[..literal.len()] == literal
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/// Scan until delimiter found, skipping escape sequences
|
|
121
|
+
///
|
|
122
|
+
/// Returns (found, position) where found is true if delimiter was found.
|
|
123
|
+
pub fn scan_until_escape(bytes: &[u8], close: &[u8], escape: &[u8]) -> (bool, usize) {
|
|
124
|
+
let mut pos = 0;
|
|
125
|
+
|
|
126
|
+
while pos < bytes.len() {
|
|
127
|
+
let remaining = &bytes[pos..];
|
|
128
|
+
let scanned = scan_until(remaining, close);
|
|
129
|
+
|
|
130
|
+
if scanned >= remaining.len() {
|
|
131
|
+
// Delimiter not found - reached EOF
|
|
132
|
+
return (false, bytes.len());
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let hit = pos + scanned;
|
|
136
|
+
|
|
137
|
+
// Check if this delimiter is part of an escape sequence
|
|
138
|
+
if !escape.is_empty() {
|
|
139
|
+
// Case 1: escape sequence starts with the close delimiter
|
|
140
|
+
// e.g., escape = "{[{]}" with close = "{["
|
|
141
|
+
if escape.starts_with(close)
|
|
142
|
+
&& hit + escape.len() <= bytes.len()
|
|
143
|
+
&& &bytes[hit..hit + escape.len()] == escape
|
|
144
|
+
{
|
|
145
|
+
// Escape sequence found - skip past it and continue
|
|
146
|
+
pos = hit + escape.len();
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Case 2: escape sequence ends with the close delimiter
|
|
151
|
+
// e.g., escape = "\\\"" with close = "\""
|
|
152
|
+
if escape.len() > close.len() && escape.ends_with(close) {
|
|
153
|
+
let prefix_len = escape.len() - close.len();
|
|
154
|
+
if hit >= prefix_len && &bytes[hit - prefix_len..hit + close.len()] == escape {
|
|
155
|
+
// Escape sequence found - skip past the close delimiter
|
|
156
|
+
pos = hit + close.len();
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Delimiter found without escape
|
|
163
|
+
return (true, hit);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
(false, bytes.len())
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Kani proof harnesses: verify scan_until against a naive reference over all
|
|
170
|
+
// bounded inputs (symbolic). Complements the bounded TLC model in
|
|
171
|
+
// formal/tla/ScanUntil.tla by checking the real Rust code, not an abstraction.
|
|
172
|
+
// Run with: cargo kani
|
|
173
|
+
#[cfg(kani)]
|
|
174
|
+
mod kani_proofs {
|
|
175
|
+
use super::*;
|
|
176
|
+
|
|
177
|
+
/// Obviously-correct reference: position of the first occurrence of `delim`
|
|
178
|
+
/// in `bytes`, or `bytes.len()` if not found (matching scan_until's contract).
|
|
179
|
+
fn first_match_ref(bytes: &[u8], delim: &[u8]) -> usize {
|
|
180
|
+
if delim.is_empty() {
|
|
181
|
+
return bytes.len();
|
|
182
|
+
}
|
|
183
|
+
let mut i = 0;
|
|
184
|
+
while i + delim.len() <= bytes.len() {
|
|
185
|
+
if &bytes[i..i + delim.len()] == delim {
|
|
186
|
+
return i;
|
|
187
|
+
}
|
|
188
|
+
i += 1;
|
|
189
|
+
}
|
|
190
|
+
bytes.len()
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Symbolic haystack of length 0..=N over a small alphabet {0,1}.
|
|
194
|
+
// The alphabet only needs 2 symbols to form matches and partial matches;
|
|
195
|
+
// keeping it small makes the proof tractable.
|
|
196
|
+
const N: usize = 6;
|
|
197
|
+
|
|
198
|
+
fn symbolic_haystack(buf: &mut [u8; N]) -> usize {
|
|
199
|
+
for b in buf.iter_mut() {
|
|
200
|
+
*b = kani::any();
|
|
201
|
+
kani::assume(*b <= 1);
|
|
202
|
+
}
|
|
203
|
+
let len: usize = kani::any();
|
|
204
|
+
kani::assume(len <= N);
|
|
205
|
+
len
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/// scan_until with a single-byte delimiter (memchr path) equals the reference.
|
|
209
|
+
#[kani::proof]
|
|
210
|
+
#[kani::unwind(8)]
|
|
211
|
+
fn scan_until_single_byte_matches_reference() {
|
|
212
|
+
let mut buf = [0u8; N];
|
|
213
|
+
let len = symbolic_haystack(&mut buf);
|
|
214
|
+
let hay = &buf[..len];
|
|
215
|
+
|
|
216
|
+
let d0: u8 = kani::any();
|
|
217
|
+
kani::assume(d0 <= 1);
|
|
218
|
+
let delim = [d0];
|
|
219
|
+
|
|
220
|
+
assert_eq!(scan_until(hay, &delim), first_match_ref(hay, &delim));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/// scan_until with a two-byte delimiter (partial-match / advance-by-1 path)
|
|
224
|
+
/// equals the reference. This exercises the bug-prone multi-byte logic.
|
|
225
|
+
#[kani::proof]
|
|
226
|
+
#[kani::unwind(8)]
|
|
227
|
+
fn scan_until_multi_byte_matches_reference() {
|
|
228
|
+
let mut buf = [0u8; N];
|
|
229
|
+
let len = symbolic_haystack(&mut buf);
|
|
230
|
+
let hay = &buf[..len];
|
|
231
|
+
|
|
232
|
+
let d0: u8 = kani::any();
|
|
233
|
+
let d1: u8 = kani::any();
|
|
234
|
+
kani::assume(d0 <= 1 && d1 <= 1);
|
|
235
|
+
let delim = [d0, d1];
|
|
236
|
+
|
|
237
|
+
assert_eq!(scan_until(hay, &delim), first_match_ref(hay, &delim));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/// Reference for scan_until_escape with a Case-2 escape (escape ends with
|
|
241
|
+
/// close, e.g. `\"`). Linearly returns the first close occurrence that is
|
|
242
|
+
/// NOT immediately preceded by the escape prefix. `prefix_len` is
|
|
243
|
+
/// `escape.len() - close.len()`. Mirrors formal/tla/ScanUntil.tla.
|
|
244
|
+
fn first_nonescaped_close_ref(
|
|
245
|
+
bytes: &[u8],
|
|
246
|
+
close: &[u8],
|
|
247
|
+
escape: &[u8],
|
|
248
|
+
prefix_len: usize,
|
|
249
|
+
) -> (bool, usize) {
|
|
250
|
+
let mut i = 0;
|
|
251
|
+
while i + close.len() <= bytes.len() {
|
|
252
|
+
if &bytes[i..i + close.len()] == close {
|
|
253
|
+
let escaped = i >= prefix_len && &bytes[i - prefix_len..i + close.len()] == escape;
|
|
254
|
+
if !escaped {
|
|
255
|
+
return (true, i);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
i += 1;
|
|
259
|
+
}
|
|
260
|
+
(false, bytes.len())
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/// scan_until_escape with a Case-2 escape equals the reference: it returns
|
|
264
|
+
/// the first non-escaped close, skipping closes preceded by the escape
|
|
265
|
+
/// prefix. Exercises the escape-skip / position-jump logic.
|
|
266
|
+
#[kani::proof]
|
|
267
|
+
#[kani::unwind(8)]
|
|
268
|
+
fn scan_until_escape_case2_matches_reference() {
|
|
269
|
+
let mut buf = [0u8; N];
|
|
270
|
+
// alphabet {0,1,2}: 0 = close byte, 1 = escape prefix byte, 2 = other
|
|
271
|
+
for b in buf.iter_mut() {
|
|
272
|
+
*b = kani::any();
|
|
273
|
+
kani::assume(*b <= 2);
|
|
274
|
+
}
|
|
275
|
+
let len: usize = kani::any();
|
|
276
|
+
kani::assume(len <= N);
|
|
277
|
+
let hay = &buf[..len];
|
|
278
|
+
|
|
279
|
+
// close = "0", escape = "10" (escape ends with close; prefix is "1")
|
|
280
|
+
let close = [0u8];
|
|
281
|
+
let escape = [1u8, 0u8];
|
|
282
|
+
|
|
283
|
+
assert_eq!(
|
|
284
|
+
scan_until_escape(hay, &close, &escape),
|
|
285
|
+
first_nonescaped_close_ref(hay, &close, &escape, 1)
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/// Reference for scan_until_escape with a Case-1 escape (escape STARTS with
|
|
290
|
+
/// close, e.g. close `{[` / escape `{[{]}`). Linearly returns the first
|
|
291
|
+
/// close occurrence that does not begin a full escape sequence.
|
|
292
|
+
/// Mirrors formal/tla/ScanEscapeCase1.tla.
|
|
293
|
+
fn first_nonescaped_close_case1_ref(
|
|
294
|
+
bytes: &[u8],
|
|
295
|
+
close: &[u8],
|
|
296
|
+
escape: &[u8],
|
|
297
|
+
) -> (bool, usize) {
|
|
298
|
+
let mut i = 0;
|
|
299
|
+
while i + close.len() <= bytes.len() {
|
|
300
|
+
if &bytes[i..i + close.len()] == close {
|
|
301
|
+
let escaped =
|
|
302
|
+
i + escape.len() <= bytes.len() && &bytes[i..i + escape.len()] == escape;
|
|
303
|
+
if !escaped {
|
|
304
|
+
return (true, i);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
i += 1;
|
|
308
|
+
}
|
|
309
|
+
(false, bytes.len())
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/// scan_until_escape with a Case-1 escape and a multi-byte close equals the
|
|
313
|
+
/// reference. Exercises the "escape starts with close" branch and the
|
|
314
|
+
/// jump-past-escape logic against a linear reference.
|
|
315
|
+
#[kani::proof]
|
|
316
|
+
#[kani::unwind(7)]
|
|
317
|
+
fn scan_until_escape_case1_matches_reference() {
|
|
318
|
+
// Case 1 has a multi-byte close, so scan_until's inner loop nests inside
|
|
319
|
+
// scan_until_escape's loop; use a smaller bound to stay tractable.
|
|
320
|
+
const M: usize = 5;
|
|
321
|
+
let mut buf = [0u8; M];
|
|
322
|
+
// alphabet {0,1,2}: close = "01", escape = "012" (escape starts with close)
|
|
323
|
+
for b in buf.iter_mut() {
|
|
324
|
+
*b = kani::any();
|
|
325
|
+
kani::assume(*b <= 2);
|
|
326
|
+
}
|
|
327
|
+
let len: usize = kani::any();
|
|
328
|
+
kani::assume(len <= M);
|
|
329
|
+
let hay = &buf[..len];
|
|
330
|
+
|
|
331
|
+
let close = [0u8, 1u8];
|
|
332
|
+
let escape = [0u8, 1u8, 2u8];
|
|
333
|
+
|
|
334
|
+
assert_eq!(
|
|
335
|
+
scan_until_escape(hay, &close, &escape),
|
|
336
|
+
first_nonescaped_close_case1_ref(hay, &close, &escape)
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/// DfaTable::run is memory-safe on an arbitrary (possibly malformed) DFA
|
|
341
|
+
/// table: with symbolic transitions, accept tokens and byte classes, and a
|
|
342
|
+
/// symbolic input, run() never panics or indexes out of bounds, and any
|
|
343
|
+
/// returned match length is within the input. This is the Kani-specific
|
|
344
|
+
/// value (safety on malformed serialized data); the functional longest-match
|
|
345
|
+
/// property is verified abstractly in formal/tla/DfaRun.tla.
|
|
346
|
+
#[kani::proof]
|
|
347
|
+
#[kani::unwind(7)]
|
|
348
|
+
fn dfa_run_is_memory_safe() {
|
|
349
|
+
const STATES: u16 = 3;
|
|
350
|
+
const CLASSES: u16 = 2;
|
|
351
|
+
const MAXIN: usize = 4;
|
|
352
|
+
|
|
353
|
+
let mut dfa = DfaTable::new(); // byte_class starts as vec![0; 256]
|
|
354
|
+
dfa.state_count = STATES;
|
|
355
|
+
dfa.class_count = CLASSES;
|
|
356
|
+
|
|
357
|
+
// Byte classes for the two byte values the input can take are symbolic
|
|
358
|
+
// (may even be >= class_count, exercising the idx bounds guard).
|
|
359
|
+
dfa.byte_class[0] = kani::any();
|
|
360
|
+
dfa.byte_class[1] = kani::any();
|
|
361
|
+
|
|
362
|
+
// Symbolic transition table (STATES*CLASSES) and accept tokens (STATES).
|
|
363
|
+
// Transition targets may point anywhere (>= state_count is guarded).
|
|
364
|
+
for _ in 0..(STATES as usize * CLASSES as usize) {
|
|
365
|
+
dfa.transitions.push(kani::any());
|
|
366
|
+
}
|
|
367
|
+
for _ in 0..(STATES as usize) {
|
|
368
|
+
dfa.accept_tokens.push(kani::any());
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Symbolic input over {0,1}, length 0..=MAXIN.
|
|
372
|
+
let mut buf = [0u8; MAXIN];
|
|
373
|
+
for b in buf.iter_mut() {
|
|
374
|
+
*b = kani::any();
|
|
375
|
+
kani::assume(*b <= 1);
|
|
376
|
+
}
|
|
377
|
+
let len: usize = kani::any();
|
|
378
|
+
kani::assume(len <= MAXIN);
|
|
379
|
+
let input = &buf[..len];
|
|
380
|
+
|
|
381
|
+
// Kani checks panic/overflow/OOB automatically; assert the returned
|
|
382
|
+
// match length never exceeds the input.
|
|
383
|
+
if let Some(m) = dfa.run(input) {
|
|
384
|
+
assert!(m.length <= input.len());
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
#[cfg(test)]
|
|
390
|
+
mod tests {
|
|
391
|
+
use super::*;
|
|
392
|
+
|
|
393
|
+
#[test]
|
|
394
|
+
fn test_scan_until_single_byte() {
|
|
395
|
+
assert_eq!(scan_until(b"hello world", b" "), 5);
|
|
396
|
+
assert_eq!(scan_until(b"hello", b" "), 5);
|
|
397
|
+
assert_eq!(scan_until(b" hello", b" "), 0);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
#[test]
|
|
401
|
+
fn test_scan_until_multi_byte() {
|
|
402
|
+
assert_eq!(scan_until(b"hello-->world", b"-->"), 5);
|
|
403
|
+
assert_eq!(scan_until(b"-->world", b"-->"), 0);
|
|
404
|
+
assert_eq!(scan_until(b"hello world", b"-->"), 11);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
#[test]
|
|
408
|
+
fn test_match_literal() {
|
|
409
|
+
assert!(match_literal(b"hello world", b"hello"));
|
|
410
|
+
assert!(!match_literal(b"hello world", b"world"));
|
|
411
|
+
assert!(!match_literal(b"hi", b"hello"));
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
#[test]
|
|
415
|
+
fn test_scan_until_escape() {
|
|
416
|
+
// Without escape - finds delimiter at position 5
|
|
417
|
+
assert_eq!(
|
|
418
|
+
scan_until_escape(b"hello\"world", b"\"", b"\\\""),
|
|
419
|
+
(true, 5)
|
|
420
|
+
);
|
|
421
|
+
// With escape - escape starts at position 5, skips past it, finds real delimiter at 12
|
|
422
|
+
assert_eq!(
|
|
423
|
+
scan_until_escape(b"hello\\\"world\"end", b"\"", b"\\\""),
|
|
424
|
+
(true, 12)
|
|
425
|
+
);
|
|
426
|
+
// No delimiter
|
|
427
|
+
assert_eq!(
|
|
428
|
+
scan_until_escape(b"hello world", b"\"", b"\\\""),
|
|
429
|
+
(false, 11)
|
|
430
|
+
);
|
|
431
|
+
// Multi-byte delimiter with escape
|
|
432
|
+
assert_eq!(
|
|
433
|
+
scan_until_escape(b"hello{[{]}world{[test", b"{[", b"{[{]}"),
|
|
434
|
+
(true, 15)
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
}
|