Skip to content

Commit 90828a2

Browse files
committed
Add AST -> HIR translation for lookarounds
1 parent 8352a9e commit 90828a2

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

regex-syntax/src/hir/literal.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2456,7 +2456,6 @@ mod tests {
24562456
}
24572457

24582458
#[test]
2459-
#[ignore = "Missing parser support for lookaround"]
24602459
fn lookaround() {
24612460
assert_eq!(exact(["ab"]), e(r"a(?<=qwa)b"));
24622461
assert_eq!(exact(["ab"]), e(r"a(?<!qwe)b"));

regex-syntax/src/hir/print.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -485,15 +485,29 @@ mod tests {
485485
}
486486

487487
#[test]
488-
#[ignore = "Missing parser support for lookaround"]
489488
fn print_look_around() {
490-
roundtrip("(?<=)", "(?<=(?:))");
491-
roundtrip("(?<!)", "(?<!(?:))");
489+
// we do not want to do a roundtrip: printed lookarounds are not
490+
// can contain capture groups which are unsupported by the parser.
491+
// TODO(shilangyu): is this a problem that some regexes are not
492+
// roundtrippable?
493+
fn test(given: &str, expected: &str) {
494+
let builder = ParserBuilder::new();
495+
let hir = builder.build().parse(given).unwrap();
496+
497+
let mut printer = Printer::new();
498+
let mut dst = String::new();
499+
printer.print(&hir, &mut dst).unwrap();
500+
501+
assert_eq!(expected, dst);
502+
}
503+
504+
test("(?<=)", "(?<=(?:))");
505+
test("(?<!)", "(?<!(?:))");
492506

493-
roundtrip("(?<=a)", "(?<=a)");
494-
roundtrip("(?<!a)", "(?<!a)");
507+
test("(?<=a)", "(?<=a)");
508+
test("(?<!a)", "(?<!a)");
495509

496-
roundtrip("(?<=(?<!(?<!(?<=a))))", "(?<=(?<!(?<!(?<=a))))");
510+
test("(?<=(?<!(?<!(?<=a))))", "(?<=(?<!(?<!(?<=a))))");
497511
}
498512

499513
#[test]

regex-syntax/src/hir/translate.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,6 @@ impl<'t, 'p> Visitor for TranslatorI<'t, 'p> {
354354
.unwrap_or_else(|| self.flags());
355355
self.push(HirFrame::Group { old_flags });
356356
}
357-
Ast::LookAround(ref x) => {
358-
todo!("translation from AST to HIR");
359-
}
360357
Ast::Concat(_) => {
361358
self.push(HirFrame::Concat);
362359
}
@@ -449,8 +446,16 @@ impl<'t, 'p> Visitor for TranslatorI<'t, 'p> {
449446
self.trans().flags.set(old_flags);
450447
self.push(HirFrame::Expr(self.hir_capture(x, expr)));
451448
}
452-
Ast::LookAround(_) => {
453-
todo!("translation from AST to HIR");
449+
Ast::LookAround(ref x) => {
450+
let expr = Box::new(self.pop().unwrap().unwrap_expr());
451+
self.push(HirFrame::Expr(Hir::lookaround(match x.kind {
452+
ast::LookAroundKind::PositiveLookBehind => {
453+
hir::LookAround::PositiveLookBehind(expr)
454+
}
455+
ast::LookAroundKind::NegativeLookBehind => {
456+
hir::LookAround::NegativeLookBehind(expr)
457+
}
458+
})));
454459
}
455460
Ast::Concat(_) => {
456461
let mut exprs = vec![];

0 commit comments

Comments
 (0)