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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#![allow(ellipsis_inclusive_range_patterns)]
use std::prelude::v1::*;
use std::{cmp, fmt, mem, str};
#[cfg(feature = "std")]
use std::error;
pub const STACK_SIZE: usize = 4;
pub(crate) const PTR_SKIP: u8 = 0;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct ParsePatError {
kind: PatError,
position: usize,
}
impl fmt::Display for ParsePatError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Syntax Error @{}: {}.", self.position, self.kind.to_str())
}
}
#[cfg(feature = "std")]
impl error::Error for ParsePatError {
fn description(&self) -> &str {
self.kind.to_str()
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum PatError {
UnpairedHexDigit,
UnknownChar,
ManyOverflow,
ManyRange,
ManyInvalid,
SaveOverflow,
StackError,
StackInvalid,
UnclosedQuote,
AlignedOperand,
ReadOperand,
SubPattern,
SubOverflow,
}
impl PatError {
fn to_str(self) -> &'static str {
match self {
PatError::UnpairedHexDigit => "unpaired hex digit",
PatError::UnknownChar => "unknown character",
PatError::ManyOverflow => "many range exceeded",
PatError::ManyRange => "many bounds nonsensical",
PatError::ManyInvalid => "many invalid syntax",
PatError::SaveOverflow => "save store overflow",
PatError::StackError => "stack unbalanced",
PatError::StackInvalid => "stack must follow jump",
PatError::UnclosedQuote => "string missing end quote",
PatError::AlignedOperand => "aligned operand error",
PatError::ReadOperand => "read operand error",
PatError::SubPattern => "sub pattern error",
PatError::SubOverflow => "sub pattern too large",
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Atom {
Byte(u8),
Save(u8),
Push(u8),
Pop,
Fuzzy(u8),
Skip(u8),
Back(u8),
Rangext(u8),
Many(u8),
Jump1,
Jump4,
Ptr,
Pir(u8),
VTypeName,
Check(u8),
Aligned(u8),
ReadI8(u8),
ReadU8(u8),
ReadI16(u8),
ReadU16(u8),
ReadI32(u8),
ReadU32(u8),
Zero(u8),
Case(u8),
Break(u8),
Nop,
}
pub type Pattern = Vec<Atom>;
pub fn save_len(pat: &[Atom]) -> usize {
pat.iter().filter_map(|&atom| {
match atom {
Atom::Save(slot) | Atom::Pir(slot) | Atom::Check(slot) | Atom::Zero(slot) |
Atom::ReadI8(slot) | Atom::ReadI16(slot) | Atom::ReadI32(slot) |
Atom::ReadU8(slot) | Atom::ReadU16(slot)| Atom::ReadU32(slot) => Some(slot as usize + 1),
_ => None,
}
}).max().unwrap_or(0)
}
pub fn parse(pat: &str) -> Result<Pattern, ParsePatError> {
let mut result = Vec::with_capacity(pat.len() / 2);
let mut pat_end = pat;
match parse_helper(&mut pat_end, &mut result) {
Ok(()) => Ok(result),
Err(kind) => {
let position = pat_end.as_ptr() as usize - pat.as_ptr() as usize;
Err(ParsePatError { kind, position })
},
}
}
fn parse_helper(pat: &mut &str, result: &mut Vec<Atom>) -> Result<(), PatError> {
result.push(Atom::Save(0));
let mut iter = pat.as_bytes().iter();
let mut save = 1;
let mut depth = 0;
#[derive(Default)]
struct SubPattern {
case: usize,
brks: Vec<usize>,
save: u8,
save_next: u8,
depth: u8,
}
let mut subs = Vec::<SubPattern>::new();
while let Some(mut chr) = iter.next().cloned() {
match chr {
b'%' => result.push(Atom::Jump1),
b'$' => result.push(Atom::Jump4),
b'*' => result.push(Atom::Ptr),
b'{' => {
depth += 1;
let atom = match result.last_mut() {
Some(atom @ Atom::Jump1) => mem::replace(atom, Atom::Push(1)),
Some(atom @ Atom::Jump4) => mem::replace(atom, Atom::Push(4)),
Some(atom @ Atom::Ptr) => mem::replace(atom, Atom::Push(PTR_SKIP)),
_ => return Err(PatError::StackInvalid),
};
result.push(atom);
},
b'}' => {
if depth <= 0 {
return Err(PatError::StackError);
}
depth -= 1;
result.push(Atom::Pop);
},
b'(' => {
subs.push(SubPattern::default());
let sub = subs.last_mut().unwrap();
sub.save = save;
sub.depth = depth;
sub.case = result.len();
result.push(Atom::Case(0));
},
b'|' => {
let sub = subs.last_mut().ok_or(PatError::SubPattern)?;
sub.save_next = cmp::max(sub.save_next, save);
save = sub.save;
depth = sub.depth;
sub.brks.push(result.len());
result.push(Atom::Break(0));
let case_offset = result.len() - sub.case - 1;
if case_offset >= 256 {
return Err(PatError::SubOverflow);
}
result[sub.case] = Atom::Case(case_offset as u8);
sub.case = result.len();
result.push(Atom::Case(0));
},
b')' => {
let sub = subs.pop().ok_or(PatError::SubPattern)?;
save = cmp::max(sub.save_next, save);
depth = sub.depth;
result[sub.case] = Atom::Nop;
for &brk in &sub.brks {
let brk_offset = result.len() - brk - 1;
if brk_offset >= 256 {
return Err(PatError::SubOverflow);
}
result[brk] = Atom::Break(brk_offset as u8);
}
},
b'[' => {
let mut lower_bound = 0u32;
let mut at_least_one_char = false;
loop {
chr = iter.next().cloned().ok_or(PatError::ManyInvalid)?;
match chr {
b'-' | b']' => break,
chr @ b'0'...b'9' => {
at_least_one_char = true;
lower_bound = lower_bound * 10 + (chr - b'0') as u32;
if lower_bound >= 16384 {
return Err(PatError::ManyOverflow);
}
},
_ => return Err(PatError::ManyInvalid),
}
}
if !at_least_one_char {
return Err(PatError::ManyInvalid);
}
if lower_bound > 0 {
if lower_bound >= 256 {
result.push(Atom::Rangext((lower_bound >> 8) as u8));
}
result.push(Atom::Skip((lower_bound & 0xff) as u8));
}
if chr == b']' {
continue;
}
let mut upper_bound = 0u32;
loop {
chr = iter.next().cloned().ok_or(PatError::ManyInvalid)?;
match chr {
b']' => break,
chr @ b'0'...b'9' => {
upper_bound = upper_bound * 10 + (chr - b'0') as u32;
if upper_bound >= 16384 {
return Err(PatError::ManyOverflow);
}
},
_ => return Err(PatError::ManyInvalid),
}
}
if lower_bound < upper_bound {
let many_skip = upper_bound - lower_bound;
if many_skip >= 256 {
result.push(Atom::Rangext((many_skip >> 8) as u8));
}
result.push(Atom::Many((many_skip & 0xff) as u8));
}
else {
return Err(PatError::ManyRange);
}
},
b'0'...b'9' | b'A'...b'F' | b'a'...b'f' => {
let hi = if chr >= b'a' { chr - b'a' + 10 }
else if chr >= b'A' { chr - b'A' + 10 }
else { chr - b'0' };
chr = iter.next().cloned().ok_or(PatError::UnpairedHexDigit)?;
let lo = if chr >= b'a' && chr <= b'f' { chr - b'a' + 10 }
else if chr >= b'A' && chr <= b'F' { chr - b'A' + 10 }
else if chr >= b'0' && chr <= b'9' { chr - b'0' }
else { return Err(PatError::UnpairedHexDigit); };
result.push(Atom::Byte((hi << 4) + lo));
},
b'"' => {
loop {
if let Some(chr) = iter.next().cloned() {
if chr != b'"' {
result.push(Atom::Byte(chr));
}
else {
break;
}
}
else {
return Err(PatError::UnclosedQuote);
}
}
},
b'\'' => {
if save >= u8::max_value() {
return Err(PatError::SaveOverflow);
}
result.push(Atom::Save(save));
save += 1;
},
b'?' => {
if let Some(Atom::Skip(skip)) = result.last_mut() {
if *skip != PTR_SKIP && *skip < 255u8 {
*skip += 1;
continue;
}
}
result.push(Atom::Skip(1));
},
b'@' => {
let op = iter.next().cloned().ok_or(PatError::AlignedOperand)?;
let atom = if op >= b'0' && op <= b'9' {
Atom::Aligned(op - b'0')
}
else if op >= b'A' && op <= b'Z' {
Atom::Aligned(10 + (op - b'A'))
}
else if op >= b'a' && op <= b'z' {
Atom::Aligned(10 + (op - b'a'))
}
else {
return Err(PatError::AlignedOperand);
};
result.push(atom);
},
b'i' => {
let atom = match iter.next().cloned() {
Some(b'1') => Atom::ReadI8(save),
Some(b'2') => Atom::ReadI16(save),
Some(b'4') => Atom::ReadI32(save),
_ => return Err(PatError::ReadOperand),
};
if save >= u8::max_value() {
return Err(PatError::SaveOverflow);
}
save += 1;
result.push(atom);
},
b'u' => {
let atom = match iter.next().cloned() {
Some(b'1') => Atom::ReadU8(save),
Some(b'2') => Atom::ReadU16(save),
Some(b'4') => Atom::ReadU32(save),
_ => return Err(PatError::ReadOperand),
};
if save >= u8::max_value() {
return Err(PatError::SaveOverflow);
}
save += 1;
result.push(atom);
},
b'z' => {
if save >= u8::max_value() {
return Err(PatError::SaveOverflow);
}
result.push(Atom::Zero(save));
save += 1;
},
b' ' | b'\n' | b'\r' | b'\t' => {},
_ => {
return Err(PatError::UnknownChar);
},
}
*pat = unsafe { str::from_utf8_unchecked(iter.as_slice()) };
}
if depth != 0 {
return Err(PatError::StackError);
}
if subs.len() != 0 {
return Err(PatError::SubPattern);
}
fn is_redundant(atom: &Atom) -> bool {
return match atom {
| Atom::Skip(_)
| Atom::Rangext(_)
| Atom::Pop
| Atom::Many(_) => true,
_ => false,
}
}
while result.last().map(is_redundant).unwrap_or(false) {
result.pop();
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
const _: [(); 2] = [(); std::mem::size_of::<Atom>()];
#[test]
fn patterns() {
use self::Atom::*;
assert_eq!(parse("12 34 56 ? ?"), Ok(vec![Save(0), Byte(0x12), Byte(0x34), Byte(0x56)]));
assert_eq!(parse("B9'?? 68???? E8${'} 8B"), Ok(vec![
Save(0), Byte(0xB9), Save(1), Skip(2), Byte(0x68), Skip(4), Byte(0xE8), Push(4), Jump4, Save(2), Pop, Byte(0x8B)
]));
assert_eq!(parse("${%{${%{}}}}"), Ok(vec![
Save(0), Push(4), Jump4, Push(1), Jump1, Push(4), Jump4, Push(1), Jump1
]));
assert_eq!(parse("24 5A9e D0 AFBea3 fCdd"), Ok(vec![
Save(0), Byte(0x24), Byte(0x5A), Byte(0x9E), Byte(0xD0), Byte(0xAF), Byte(0xBE), Byte(0xA3), Byte(0xFC), Byte(0xDD)
]));
assert_eq!(parse("\"string\""), Ok(vec![
Save(0), Byte(115), Byte(116), Byte(114), Byte(105), Byte(110), Byte(103)
]));
assert_eq!(parse("*{FF D8 42}"), Ok(vec![
Save(0), Push(PTR_SKIP), Ptr, Byte(0xFF), Byte(0xD8), Byte(0x42)
]));
assert_eq!(parse("*{\"hello\"00}"), Ok(vec![
Save(0), Push(PTR_SKIP), Ptr, Byte(104), Byte(101), Byte(108), Byte(108), Byte(111), Byte(0)
]));
assert_eq!(parse("b8 [16] 50 [13-42] ff"), Ok(vec![
Save(0), Byte(0xb8), Skip(16), Byte(0x50), Skip(13), Many(29), Byte(0xff)
]));
assert_eq!(parse("e9 $ @4"), Ok(vec![
Save(0), Byte(0xe9), Jump4, Aligned(4)
]));
assert_eq!(parse("83 c0 2a ( 6a ? | 68 ? ? ? ? ) e8"), Ok(vec![
Save(0), Byte(0x83), Byte(0xc0), Byte(0x2a),
Case(3), Byte(0x6a), Skip(1), Break(3),
Nop, Byte(0x68), Skip(4), Byte(0xe8),
]));
}
#[test]
fn errors() {
use self::PatError::*;
assert_eq!(Err(ParsePatError { kind: StackError, position: 2 }), parse("${"));
assert_eq!(Err(ParsePatError { kind: StackError, position: 0 }), parse("}}"));
assert_eq!(Err(ParsePatError { kind: StackInvalid, position: 3 }), parse("AB {}"));
assert_eq!(Err(ParsePatError { kind: UnpairedHexDigit, position: 2 }), parse("123"));
assert_eq!(Err(ParsePatError { kind: UnpairedHexDigit, position: 3 }), parse("EE BZ"));
assert_eq!(Err(ParsePatError { kind: UnpairedHexDigit, position: 0 }), parse("A?"));
assert_eq!(Err(ParsePatError { kind: UnknownChar, position: 0 }), parse("é"));
assert_eq!(Err(ParsePatError { kind: AlignedOperand, position: 0 }), parse("@"));
assert_eq!(Err(ParsePatError { kind: UnclosedQuote, position: 0 }), parse("\"unbalanced"));
assert_eq!(Err(ParsePatError { kind: ManyInvalid, position: 0 }), parse("[-2]"));
assert_eq!(Err(ParsePatError { kind: ManyInvalid, position: 0 }), parse("[-]"));
assert_eq!(Err(ParsePatError { kind: ManyInvalid, position: 0 }), parse("[]"));
assert_eq!(Err(ParsePatError { kind: ManyRange, position: 0 }), parse("[0-]"));
assert_eq!(Err(ParsePatError { kind: ManyRange, position: 0 }), parse("[0-0]"));
assert_eq!(Err(ParsePatError { kind: ManyRange, position: 0 }), parse("[20-1]"));
assert_eq!(Err(ParsePatError { kind: ManyOverflow, position: 0 }), parse("[20000-40000]"));
}
}