-
Notifications
You must be signed in to change notification settings - Fork 421
/
translate.rs
3724 lines (3466 loc) · 125 KB
/
translate.rs
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
Defines a translator that converts an `Ast` to an `Hir`.
*/
use core::cell::{Cell, RefCell};
use alloc::{boxed::Box, string::ToString, vec, vec::Vec};
use crate::{
ast::{self, Ast, Span, Visitor},
either::Either,
hir::{self, Error, ErrorKind, Hir, HirKind},
unicode::{self, ClassQuery},
};
type Result<T> = core::result::Result<T, Error>;
/// A builder for constructing an AST->HIR translator.
#[derive(Clone, Debug)]
pub struct TranslatorBuilder {
utf8: bool,
line_terminator: u8,
flags: Flags,
}
impl Default for TranslatorBuilder {
fn default() -> TranslatorBuilder {
TranslatorBuilder::new()
}
}
impl TranslatorBuilder {
/// Create a new translator builder with a default c onfiguration.
pub fn new() -> TranslatorBuilder {
TranslatorBuilder {
utf8: true,
line_terminator: b'\n',
flags: Flags::default(),
}
}
/// Build a translator using the current configuration.
pub fn build(&self) -> Translator {
Translator {
stack: RefCell::new(vec![]),
flags: Cell::new(self.flags),
utf8: self.utf8,
line_terminator: self.line_terminator,
}
}
/// When disabled, translation will permit the construction of a regular
/// expression that may match invalid UTF-8.
///
/// When enabled (the default), the translator is guaranteed to produce an
/// expression that, for non-empty matches, will only ever produce spans
/// that are entirely valid UTF-8 (otherwise, the translator will return an
/// error).
///
/// Perhaps surprisingly, when UTF-8 is enabled, an empty regex or even
/// a negated ASCII word boundary (uttered as `(?-u:\B)` in the concrete
/// syntax) will be allowed even though they can produce matches that split
/// a UTF-8 encoded codepoint. This only applies to zero-width or "empty"
/// matches, and it is expected that the regex engine itself must handle
/// these cases if necessary (perhaps by suppressing any zero-width matches
/// that split a codepoint).
pub fn utf8(&mut self, yes: bool) -> &mut TranslatorBuilder {
self.utf8 = yes;
self
}
/// Sets the line terminator for use with `(?u-s:.)` and `(?-us:.)`.
///
/// Namely, instead of `.` (by default) matching everything except for `\n`,
/// this will cause `.` to match everything except for the byte given.
///
/// If `.` is used in a context where Unicode mode is enabled and this byte
/// isn't ASCII, then an error will be returned. When Unicode mode is
/// disabled, then any byte is permitted, but will return an error if UTF-8
/// mode is enabled and it is a non-ASCII byte.
///
/// In short, any ASCII value for a line terminator is always okay. But a
/// non-ASCII byte might result in an error depending on whether Unicode
/// mode or UTF-8 mode are enabled.
///
/// Note that if `R` mode is enabled then it always takes precedence and
/// the line terminator will be treated as `\r` and `\n` simultaneously.
///
/// Note also that this *doesn't* impact the look-around assertions
/// `(?m:^)` and `(?m:$)`. That's usually controlled by additional
/// configuration in the regex engine itself.
pub fn line_terminator(&mut self, byte: u8) -> &mut TranslatorBuilder {
self.line_terminator = byte;
self
}
/// Enable or disable the case insensitive flag (`i`) by default.
pub fn case_insensitive(&mut self, yes: bool) -> &mut TranslatorBuilder {
self.flags.case_insensitive = if yes { Some(true) } else { None };
self
}
/// Enable or disable the multi-line matching flag (`m`) by default.
pub fn multi_line(&mut self, yes: bool) -> &mut TranslatorBuilder {
self.flags.multi_line = if yes { Some(true) } else { None };
self
}
/// Enable or disable the "dot matches any character" flag (`s`) by
/// default.
pub fn dot_matches_new_line(
&mut self,
yes: bool,
) -> &mut TranslatorBuilder {
self.flags.dot_matches_new_line = if yes { Some(true) } else { None };
self
}
/// Enable or disable the CRLF mode flag (`R`) by default.
pub fn crlf(&mut self, yes: bool) -> &mut TranslatorBuilder {
self.flags.crlf = if yes { Some(true) } else { None };
self
}
/// Enable or disable the "swap greed" flag (`U`) by default.
pub fn swap_greed(&mut self, yes: bool) -> &mut TranslatorBuilder {
self.flags.swap_greed = if yes { Some(true) } else { None };
self
}
/// Enable or disable the Unicode flag (`u`) by default.
pub fn unicode(&mut self, yes: bool) -> &mut TranslatorBuilder {
self.flags.unicode = if yes { None } else { Some(false) };
self
}
}
/// A translator maps abstract syntax to a high level intermediate
/// representation.
///
/// A translator may be benefit from reuse. That is, a translator can translate
/// many abstract syntax trees.
///
/// A `Translator` can be configured in more detail via a
/// [`TranslatorBuilder`].
#[derive(Clone, Debug)]
pub struct Translator {
/// Our call stack, but on the heap.
stack: RefCell<Vec<HirFrame>>,
/// The current flag settings.
flags: Cell<Flags>,
/// Whether we're allowed to produce HIR that can match arbitrary bytes.
utf8: bool,
/// The line terminator to use for `.`.
line_terminator: u8,
}
impl Translator {
/// Create a new translator using the default configuration.
pub fn new() -> Translator {
TranslatorBuilder::new().build()
}
/// Translate the given abstract syntax tree (AST) into a high level
/// intermediate representation (HIR).
///
/// If there was a problem doing the translation, then an HIR-specific
/// error is returned.
///
/// The original pattern string used to produce the `Ast` *must* also be
/// provided. The translator does not use the pattern string during any
/// correct translation, but is used for error reporting.
pub fn translate(&mut self, pattern: &str, ast: &Ast) -> Result<Hir> {
ast::visit(ast, TranslatorI::new(self, pattern))
}
}
/// An HirFrame is a single stack frame, represented explicitly, which is
/// created for each item in the Ast that we traverse.
///
/// Note that technically, this type doesn't represent our entire stack
/// frame. In particular, the Ast visitor represents any state associated with
/// traversing the Ast itself.
#[derive(Clone, Debug)]
enum HirFrame {
/// An arbitrary HIR expression. These get pushed whenever we hit a base
/// case in the Ast. They get popped after an inductive (i.e., recursive)
/// step is complete.
Expr(Hir),
/// A literal that is being constructed, character by character, from the
/// AST. We need this because the AST gives each individual character its
/// own node. So as we see characters, we peek at the top-most HirFrame.
/// If it's a literal, then we add to it. Otherwise, we push a new literal.
/// When it comes time to pop it, we convert it to an Hir via Hir::literal.
Literal(Vec<u8>),
/// A Unicode character class. This frame is mutated as we descend into
/// the Ast of a character class (which is itself its own mini recursive
/// structure).
ClassUnicode(hir::ClassUnicode),
/// A byte-oriented character class. This frame is mutated as we descend
/// into the Ast of a character class (which is itself its own mini
/// recursive structure).
///
/// Byte character classes are created when Unicode mode (`u`) is disabled.
/// If `utf8` is enabled (the default), then a byte character is only
/// permitted to match ASCII text.
ClassBytes(hir::ClassBytes),
/// This is pushed whenever a repetition is observed. After visiting every
/// sub-expression in the repetition, the translator's stack is expected to
/// have this sentinel at the top.
///
/// This sentinel only exists to stop other things (like flattening
/// literals) from reaching across repetition operators.
Repetition,
/// This is pushed on to the stack upon first seeing any kind of capture,
/// indicated by parentheses (including non-capturing groups). It is popped
/// upon leaving a group.
Group {
/// The old active flags when this group was opened.
///
/// If this group sets flags, then the new active flags are set to the
/// result of merging the old flags with the flags introduced by this
/// group. If the group doesn't set any flags, then this is simply
/// equivalent to whatever flags were set when the group was opened.
///
/// When this group is popped, the active flags should be restored to
/// the flags set here.
///
/// The "active" flags correspond to whatever flags are set in the
/// Translator.
old_flags: Flags,
},
/// This is pushed whenever a concatenation is observed. After visiting
/// every sub-expression in the concatenation, the translator's stack is
/// popped until it sees a Concat frame.
Concat,
/// This is pushed whenever an alternation is observed. After visiting
/// every sub-expression in the alternation, the translator's stack is
/// popped until it sees an Alternation frame.
Alternation,
/// This is pushed immediately before each sub-expression in an
/// alternation. This separates the branches of an alternation on the
/// stack and prevents literal flattening from reaching across alternation
/// branches.
///
/// It is popped after each expression in a branch until an 'Alternation'
/// frame is observed when doing a post visit on an alternation.
AlternationBranch,
}
impl HirFrame {
/// Assert that the current stack frame is an Hir expression and return it.
fn unwrap_expr(self) -> Hir {
match self {
HirFrame::Expr(expr) => expr,
HirFrame::Literal(lit) => Hir::literal(lit),
_ => panic!("tried to unwrap expr from HirFrame, got: {:?}", self),
}
}
/// Assert that the current stack frame is a Unicode class expression and
/// return it.
fn unwrap_class_unicode(self) -> hir::ClassUnicode {
match self {
HirFrame::ClassUnicode(cls) => cls,
_ => panic!(
"tried to unwrap Unicode class \
from HirFrame, got: {:?}",
self
),
}
}
/// Assert that the current stack frame is a byte class expression and
/// return it.
fn unwrap_class_bytes(self) -> hir::ClassBytes {
match self {
HirFrame::ClassBytes(cls) => cls,
_ => panic!(
"tried to unwrap byte class \
from HirFrame, got: {:?}",
self
),
}
}
/// Assert that the current stack frame is a repetition sentinel. If it
/// isn't, then panic.
fn unwrap_repetition(self) {
match self {
HirFrame::Repetition => {}
_ => {
panic!(
"tried to unwrap repetition from HirFrame, got: {:?}",
self
)
}
}
}
/// Assert that the current stack frame is a group indicator and return
/// its corresponding flags (the flags that were active at the time the
/// group was entered).
fn unwrap_group(self) -> Flags {
match self {
HirFrame::Group { old_flags } => old_flags,
_ => {
panic!("tried to unwrap group from HirFrame, got: {:?}", self)
}
}
}
/// Assert that the current stack frame is an alternation pipe sentinel. If
/// it isn't, then panic.
fn unwrap_alternation_pipe(self) {
match self {
HirFrame::AlternationBranch => {}
_ => {
panic!(
"tried to unwrap alt pipe from HirFrame, got: {:?}",
self
)
}
}
}
}
impl<'t, 'p> Visitor for TranslatorI<'t, 'p> {
type Output = Hir;
type Err = Error;
fn finish(self) -> Result<Hir> {
// ... otherwise, we should have exactly one HIR on the stack.
assert_eq!(self.trans().stack.borrow().len(), 1);
Ok(self.pop().unwrap().unwrap_expr())
}
fn visit_pre(&mut self, ast: &Ast) -> Result<()> {
match *ast {
Ast::ClassBracketed(_) => {
if self.flags().unicode() {
let cls = hir::ClassUnicode::empty();
self.push(HirFrame::ClassUnicode(cls));
} else {
let cls = hir::ClassBytes::empty();
self.push(HirFrame::ClassBytes(cls));
}
}
Ast::Repetition(_) => self.push(HirFrame::Repetition),
Ast::Group(ref x) => {
let old_flags = x
.flags()
.map(|ast| self.set_flags(ast))
.unwrap_or_else(|| self.flags());
self.push(HirFrame::Group { old_flags });
}
Ast::Concat(_) => {
self.push(HirFrame::Concat);
}
Ast::Alternation(ref x) => {
self.push(HirFrame::Alternation);
if !x.asts.is_empty() {
self.push(HirFrame::AlternationBranch);
}
}
_ => {}
}
Ok(())
}
fn visit_post(&mut self, ast: &Ast) -> Result<()> {
match *ast {
Ast::Empty(_) => {
self.push(HirFrame::Expr(Hir::empty()));
}
Ast::Flags(ref x) => {
self.set_flags(&x.flags);
// Flags in the AST are generally considered directives and
// not actual sub-expressions. However, they can be used in
// the concrete syntax like `((?i))`, and we need some kind of
// indication of an expression there, and Empty is the correct
// choice.
//
// There can also be things like `(?i)+`, but we rule those out
// in the parser. In the future, we might allow them for
// consistency sake.
self.push(HirFrame::Expr(Hir::empty()));
}
Ast::Literal(ref x) => match self.ast_literal_to_scalar(x)? {
Either::Right(byte) => self.push_byte(byte),
Either::Left(ch) => match self.case_fold_char(x.span, ch)? {
None => self.push_char(ch),
Some(expr) => self.push(HirFrame::Expr(expr)),
},
},
Ast::Dot(ref span) => {
self.push(HirFrame::Expr(self.hir_dot(**span)?));
}
Ast::Assertion(ref x) => {
self.push(HirFrame::Expr(self.hir_assertion(x)?));
}
Ast::ClassPerl(ref x) => {
if self.flags().unicode() {
let cls = self.hir_perl_unicode_class(x)?;
let hcls = hir::Class::Unicode(cls);
self.push(HirFrame::Expr(Hir::class(hcls)));
} else {
let cls = self.hir_perl_byte_class(x)?;
let hcls = hir::Class::Bytes(cls);
self.push(HirFrame::Expr(Hir::class(hcls)));
}
}
Ast::ClassUnicode(ref x) => {
let cls = hir::Class::Unicode(self.hir_unicode_class(x)?);
self.push(HirFrame::Expr(Hir::class(cls)));
}
Ast::ClassBracketed(ref ast) => {
if self.flags().unicode() {
let mut cls = self.pop().unwrap().unwrap_class_unicode();
self.unicode_fold_and_negate(
&ast.span,
ast.negated,
&mut cls,
)?;
let expr = Hir::class(hir::Class::Unicode(cls));
self.push(HirFrame::Expr(expr));
} else {
let mut cls = self.pop().unwrap().unwrap_class_bytes();
self.bytes_fold_and_negate(
&ast.span,
ast.negated,
&mut cls,
)?;
let expr = Hir::class(hir::Class::Bytes(cls));
self.push(HirFrame::Expr(expr));
}
}
Ast::Repetition(ref x) => {
let expr = self.pop().unwrap().unwrap_expr();
self.pop().unwrap().unwrap_repetition();
self.push(HirFrame::Expr(self.hir_repetition(x, expr)));
}
Ast::Group(ref x) => {
let expr = self.pop().unwrap().unwrap_expr();
let old_flags = self.pop().unwrap().unwrap_group();
self.trans().flags.set(old_flags);
self.push(HirFrame::Expr(self.hir_capture(x, expr)));
}
Ast::Concat(_) => {
let mut exprs = vec![];
while let Some(expr) = self.pop_concat_expr() {
if !matches!(*expr.kind(), HirKind::Empty) {
exprs.push(expr);
}
}
exprs.reverse();
self.push(HirFrame::Expr(Hir::concat(exprs)));
}
Ast::Alternation(_) => {
let mut exprs = vec![];
while let Some(expr) = self.pop_alt_expr() {
self.pop().unwrap().unwrap_alternation_pipe();
exprs.push(expr);
}
exprs.reverse();
self.push(HirFrame::Expr(Hir::alternation(exprs)));
}
}
Ok(())
}
fn visit_alternation_in(&mut self) -> Result<()> {
self.push(HirFrame::AlternationBranch);
Ok(())
}
fn visit_class_set_item_pre(
&mut self,
ast: &ast::ClassSetItem,
) -> Result<()> {
match *ast {
ast::ClassSetItem::Bracketed(_) => {
if self.flags().unicode() {
let cls = hir::ClassUnicode::empty();
self.push(HirFrame::ClassUnicode(cls));
} else {
let cls = hir::ClassBytes::empty();
self.push(HirFrame::ClassBytes(cls));
}
}
// We needn't handle the Union case here since the visitor will
// do it for us.
_ => {}
}
Ok(())
}
fn visit_class_set_item_post(
&mut self,
ast: &ast::ClassSetItem,
) -> Result<()> {
match *ast {
ast::ClassSetItem::Empty(_) => {}
ast::ClassSetItem::Literal(ref x) => {
if self.flags().unicode() {
let mut cls = self.pop().unwrap().unwrap_class_unicode();
cls.push(hir::ClassUnicodeRange::new(x.c, x.c));
self.push(HirFrame::ClassUnicode(cls));
} else {
let mut cls = self.pop().unwrap().unwrap_class_bytes();
let byte = self.class_literal_byte(x)?;
cls.push(hir::ClassBytesRange::new(byte, byte));
self.push(HirFrame::ClassBytes(cls));
}
}
ast::ClassSetItem::Range(ref x) => {
if self.flags().unicode() {
let mut cls = self.pop().unwrap().unwrap_class_unicode();
cls.push(hir::ClassUnicodeRange::new(x.start.c, x.end.c));
self.push(HirFrame::ClassUnicode(cls));
} else {
let mut cls = self.pop().unwrap().unwrap_class_bytes();
let start = self.class_literal_byte(&x.start)?;
let end = self.class_literal_byte(&x.end)?;
cls.push(hir::ClassBytesRange::new(start, end));
self.push(HirFrame::ClassBytes(cls));
}
}
ast::ClassSetItem::Ascii(ref x) => {
if self.flags().unicode() {
let xcls = self.hir_ascii_unicode_class(x)?;
let mut cls = self.pop().unwrap().unwrap_class_unicode();
cls.union(&xcls);
self.push(HirFrame::ClassUnicode(cls));
} else {
let xcls = self.hir_ascii_byte_class(x)?;
let mut cls = self.pop().unwrap().unwrap_class_bytes();
cls.union(&xcls);
self.push(HirFrame::ClassBytes(cls));
}
}
ast::ClassSetItem::Unicode(ref x) => {
let xcls = self.hir_unicode_class(x)?;
let mut cls = self.pop().unwrap().unwrap_class_unicode();
cls.union(&xcls);
self.push(HirFrame::ClassUnicode(cls));
}
ast::ClassSetItem::Perl(ref x) => {
if self.flags().unicode() {
let xcls = self.hir_perl_unicode_class(x)?;
let mut cls = self.pop().unwrap().unwrap_class_unicode();
cls.union(&xcls);
self.push(HirFrame::ClassUnicode(cls));
} else {
let xcls = self.hir_perl_byte_class(x)?;
let mut cls = self.pop().unwrap().unwrap_class_bytes();
cls.union(&xcls);
self.push(HirFrame::ClassBytes(cls));
}
}
ast::ClassSetItem::Bracketed(ref ast) => {
if self.flags().unicode() {
let mut cls1 = self.pop().unwrap().unwrap_class_unicode();
self.unicode_fold_and_negate(
&ast.span,
ast.negated,
&mut cls1,
)?;
let mut cls2 = self.pop().unwrap().unwrap_class_unicode();
cls2.union(&cls1);
self.push(HirFrame::ClassUnicode(cls2));
} else {
let mut cls1 = self.pop().unwrap().unwrap_class_bytes();
self.bytes_fold_and_negate(
&ast.span,
ast.negated,
&mut cls1,
)?;
let mut cls2 = self.pop().unwrap().unwrap_class_bytes();
cls2.union(&cls1);
self.push(HirFrame::ClassBytes(cls2));
}
}
// This is handled automatically by the visitor.
ast::ClassSetItem::Union(_) => {}
}
Ok(())
}
fn visit_class_set_binary_op_pre(
&mut self,
_op: &ast::ClassSetBinaryOp,
) -> Result<()> {
if self.flags().unicode() {
let cls = hir::ClassUnicode::empty();
self.push(HirFrame::ClassUnicode(cls));
} else {
let cls = hir::ClassBytes::empty();
self.push(HirFrame::ClassBytes(cls));
}
Ok(())
}
fn visit_class_set_binary_op_in(
&mut self,
_op: &ast::ClassSetBinaryOp,
) -> Result<()> {
if self.flags().unicode() {
let cls = hir::ClassUnicode::empty();
self.push(HirFrame::ClassUnicode(cls));
} else {
let cls = hir::ClassBytes::empty();
self.push(HirFrame::ClassBytes(cls));
}
Ok(())
}
fn visit_class_set_binary_op_post(
&mut self,
op: &ast::ClassSetBinaryOp,
) -> Result<()> {
use crate::ast::ClassSetBinaryOpKind::*;
if self.flags().unicode() {
let mut rhs = self.pop().unwrap().unwrap_class_unicode();
let mut lhs = self.pop().unwrap().unwrap_class_unicode();
let mut cls = self.pop().unwrap().unwrap_class_unicode();
if self.flags().case_insensitive() {
rhs.try_case_fold_simple().map_err(|_| {
self.error(
op.rhs.span().clone(),
ErrorKind::UnicodeCaseUnavailable,
)
})?;
lhs.try_case_fold_simple().map_err(|_| {
self.error(
op.lhs.span().clone(),
ErrorKind::UnicodeCaseUnavailable,
)
})?;
}
match op.kind {
Intersection => lhs.intersect(&rhs),
Difference => lhs.difference(&rhs),
SymmetricDifference => lhs.symmetric_difference(&rhs),
}
cls.union(&lhs);
self.push(HirFrame::ClassUnicode(cls));
} else {
let mut rhs = self.pop().unwrap().unwrap_class_bytes();
let mut lhs = self.pop().unwrap().unwrap_class_bytes();
let mut cls = self.pop().unwrap().unwrap_class_bytes();
if self.flags().case_insensitive() {
rhs.case_fold_simple();
lhs.case_fold_simple();
}
match op.kind {
Intersection => lhs.intersect(&rhs),
Difference => lhs.difference(&rhs),
SymmetricDifference => lhs.symmetric_difference(&rhs),
}
cls.union(&lhs);
self.push(HirFrame::ClassBytes(cls));
}
Ok(())
}
}
/// The internal implementation of a translator.
///
/// This type is responsible for carrying around the original pattern string,
/// which is not tied to the internal state of a translator.
///
/// A TranslatorI exists for the time it takes to translate a single Ast.
#[derive(Clone, Debug)]
struct TranslatorI<'t, 'p> {
trans: &'t Translator,
pattern: &'p str,
}
impl<'t, 'p> TranslatorI<'t, 'p> {
/// Build a new internal translator.
fn new(trans: &'t Translator, pattern: &'p str) -> TranslatorI<'t, 'p> {
TranslatorI { trans, pattern }
}
/// Return a reference to the underlying translator.
fn trans(&self) -> &Translator {
&self.trans
}
/// Push the given frame on to the call stack.
fn push(&self, frame: HirFrame) {
self.trans().stack.borrow_mut().push(frame);
}
/// Push the given literal char on to the call stack.
///
/// If the top-most element of the stack is a literal, then the char
/// is appended to the end of that literal. Otherwise, a new literal
/// containing just the given char is pushed to the top of the stack.
fn push_char(&self, ch: char) {
let mut buf = [0; 4];
let bytes = ch.encode_utf8(&mut buf).as_bytes();
let mut stack = self.trans().stack.borrow_mut();
if let Some(HirFrame::Literal(ref mut literal)) = stack.last_mut() {
literal.extend_from_slice(bytes);
} else {
stack.push(HirFrame::Literal(bytes.to_vec()));
}
}
/// Push the given literal byte on to the call stack.
///
/// If the top-most element of the stack is a literal, then the byte
/// is appended to the end of that literal. Otherwise, a new literal
/// containing just the given byte is pushed to the top of the stack.
fn push_byte(&self, byte: u8) {
let mut stack = self.trans().stack.borrow_mut();
if let Some(HirFrame::Literal(ref mut literal)) = stack.last_mut() {
literal.push(byte);
} else {
stack.push(HirFrame::Literal(vec![byte]));
}
}
/// Pop the top of the call stack. If the call stack is empty, return None.
fn pop(&self) -> Option<HirFrame> {
self.trans().stack.borrow_mut().pop()
}
/// Pop an HIR expression from the top of the stack for a concatenation.
///
/// This returns None if the stack is empty or when a concat frame is seen.
/// Otherwise, it panics if it could not find an HIR expression.
fn pop_concat_expr(&self) -> Option<Hir> {
let frame = self.pop()?;
match frame {
HirFrame::Concat => None,
HirFrame::Expr(expr) => Some(expr),
HirFrame::Literal(lit) => Some(Hir::literal(lit)),
HirFrame::ClassUnicode(_) => {
unreachable!("expected expr or concat, got Unicode class")
}
HirFrame::ClassBytes(_) => {
unreachable!("expected expr or concat, got byte class")
}
HirFrame::Repetition => {
unreachable!("expected expr or concat, got repetition")
}
HirFrame::Group { .. } => {
unreachable!("expected expr or concat, got group")
}
HirFrame::Alternation => {
unreachable!("expected expr or concat, got alt marker")
}
HirFrame::AlternationBranch => {
unreachable!("expected expr or concat, got alt branch marker")
}
}
}
/// Pop an HIR expression from the top of the stack for an alternation.
///
/// This returns None if the stack is empty or when an alternation frame is
/// seen. Otherwise, it panics if it could not find an HIR expression.
fn pop_alt_expr(&self) -> Option<Hir> {
let frame = self.pop()?;
match frame {
HirFrame::Alternation => None,
HirFrame::Expr(expr) => Some(expr),
HirFrame::Literal(lit) => Some(Hir::literal(lit)),
HirFrame::ClassUnicode(_) => {
unreachable!("expected expr or alt, got Unicode class")
}
HirFrame::ClassBytes(_) => {
unreachable!("expected expr or alt, got byte class")
}
HirFrame::Repetition => {
unreachable!("expected expr or alt, got repetition")
}
HirFrame::Group { .. } => {
unreachable!("expected expr or alt, got group")
}
HirFrame::Concat => {
unreachable!("expected expr or alt, got concat marker")
}
HirFrame::AlternationBranch => {
unreachable!("expected expr or alt, got alt branch marker")
}
}
}
/// Create a new error with the given span and error type.
fn error(&self, span: Span, kind: ErrorKind) -> Error {
Error { kind, pattern: self.pattern.to_string(), span }
}
/// Return a copy of the active flags.
fn flags(&self) -> Flags {
self.trans().flags.get()
}
/// Set the flags of this translator from the flags set in the given AST.
/// Then, return the old flags.
fn set_flags(&self, ast_flags: &ast::Flags) -> Flags {
let old_flags = self.flags();
let mut new_flags = Flags::from_ast(ast_flags);
new_flags.merge(&old_flags);
self.trans().flags.set(new_flags);
old_flags
}
/// Convert an Ast literal to its scalar representation.
///
/// When Unicode mode is enabled, then this always succeeds and returns a
/// `char` (Unicode scalar value).
///
/// When Unicode mode is disabled, then a `char` will still be returned
/// whenever possible. A byte is returned only when invalid UTF-8 is
/// allowed and when the byte is not ASCII. Otherwise, a non-ASCII byte
/// will result in an error when invalid UTF-8 is not allowed.
fn ast_literal_to_scalar(
&self,
lit: &ast::Literal,
) -> Result<Either<char, u8>> {
if self.flags().unicode() {
return Ok(Either::Left(lit.c));
}
let byte = match lit.byte() {
None => return Ok(Either::Left(lit.c)),
Some(byte) => byte,
};
if byte <= 0x7F {
return Ok(Either::Left(char::try_from(byte).unwrap()));
}
if self.trans().utf8 {
return Err(self.error(lit.span, ErrorKind::InvalidUtf8));
}
Ok(Either::Right(byte))
}
fn case_fold_char(&self, span: Span, c: char) -> Result<Option<Hir>> {
if !self.flags().case_insensitive() {
return Ok(None);
}
if self.flags().unicode() {
// If case folding won't do anything, then don't bother trying.
let map = unicode::SimpleCaseFolder::new()
.map(|f| f.overlaps(c, c))
.map_err(|_| {
self.error(span, ErrorKind::UnicodeCaseUnavailable)
})?;
if !map {
return Ok(None);
}
let mut cls =
hir::ClassUnicode::new(vec![hir::ClassUnicodeRange::new(
c, c,
)]);
cls.try_case_fold_simple().map_err(|_| {
self.error(span, ErrorKind::UnicodeCaseUnavailable)
})?;
Ok(Some(Hir::class(hir::Class::Unicode(cls))))
} else {
if !c.is_ascii() {
return Ok(None);
}
// If case folding won't do anything, then don't bother trying.
match c {
'A'..='Z' | 'a'..='z' => {}
_ => return Ok(None),
}
let mut cls =
hir::ClassBytes::new(vec![hir::ClassBytesRange::new(
// OK because 'c.len_utf8() == 1' which in turn implies
// that 'c' is ASCII.
u8::try_from(c).unwrap(),
u8::try_from(c).unwrap(),
)]);
cls.case_fold_simple();
Ok(Some(Hir::class(hir::Class::Bytes(cls))))
}
}
fn hir_dot(&self, span: Span) -> Result<Hir> {
let (utf8, lineterm, flags) =
(self.trans().utf8, self.trans().line_terminator, self.flags());
if utf8 && (!flags.unicode() || !lineterm.is_ascii()) {
return Err(self.error(span, ErrorKind::InvalidUtf8));
}
let dot = if flags.dot_matches_new_line() {
if flags.unicode() {
hir::Dot::AnyChar
} else {
hir::Dot::AnyByte
}
} else {
if flags.unicode() {
if flags.crlf() {
hir::Dot::AnyCharExceptCRLF
} else {
if !lineterm.is_ascii() {
return Err(
self.error(span, ErrorKind::InvalidLineTerminator)
);
}
hir::Dot::AnyCharExcept(char::from(lineterm))
}
} else {
if flags.crlf() {
hir::Dot::AnyByteExceptCRLF
} else {
hir::Dot::AnyByteExcept(lineterm)
}
}
};
Ok(Hir::dot(dot))
}
fn hir_assertion(&self, asst: &ast::Assertion) -> Result<Hir> {
let unicode = self.flags().unicode();
let multi_line = self.flags().multi_line();
let crlf = self.flags().crlf();
Ok(match asst.kind {
ast::AssertionKind::StartLine => Hir::look(if multi_line {
if crlf {
hir::Look::StartCRLF
} else {
hir::Look::StartLF
}
} else {
hir::Look::Start
}),
ast::AssertionKind::EndLine => Hir::look(if multi_line {
if crlf {
hir::Look::EndCRLF
} else {
hir::Look::EndLF
}
} else {
hir::Look::End
}),
ast::AssertionKind::StartText => Hir::look(hir::Look::Start),
ast::AssertionKind::EndText => Hir::look(hir::Look::End),
ast::AssertionKind::WordBoundary => Hir::look(if unicode {
hir::Look::WordUnicode
} else {
hir::Look::WordAscii
}),
ast::AssertionKind::NotWordBoundary => Hir::look(if unicode {
hir::Look::WordUnicodeNegate
} else {
hir::Look::WordAsciiNegate
}),
ast::AssertionKind::WordBoundaryStart
| ast::AssertionKind::WordBoundaryStartAngle => {
Hir::look(if unicode {
hir::Look::WordStartUnicode
} else {
hir::Look::WordStartAscii
})
}
ast::AssertionKind::WordBoundaryEnd
| ast::AssertionKind::WordBoundaryEndAngle => {
Hir::look(if unicode {
hir::Look::WordEndUnicode
} else {
hir::Look::WordEndAscii
})
}
ast::AssertionKind::WordBoundaryStartHalf => {
Hir::look(if unicode {
hir::Look::WordStartHalfUnicode
} else {
hir::Look::WordStartHalfAscii
})
}
ast::AssertionKind::WordBoundaryEndHalf => Hir::look(if unicode {
hir::Look::WordEndHalfUnicode
} else {
hir::Look::WordEndHalfAscii
}),
})
}
fn hir_capture(&self, group: &ast::Group, expr: Hir) -> Hir {
let (index, name) = match group.kind {
ast::GroupKind::CaptureIndex(index) => (index, None),
ast::GroupKind::CaptureName { ref name, .. } => {
(name.index, Some(name.name.clone().into_boxed_str()))
}
// The HIR doesn't need to use non-capturing groups, since the way
// in which the data type is defined handles this automatically.
ast::GroupKind::NonCapturing(_) => return expr,
};
Hir::capture(hir::Capture { index, name, sub: Box::new(expr) })
}