move backtracing out into a step
Matt S Trout [Wed, 22 Jun 2016 02:30:23 +0000 (02:30 +0000)]
lib/DX/Step/Backtrack.pm [new file with mode: 0644]

diff --git a/lib/DX/Step/Backtrack.pm b/lib/DX/Step/Backtrack.pm
new file mode 100644 (file)
index 0000000..8e3b5d8
--- /dev/null
@@ -0,0 +1,19 @@
+package DX::Step::Backtrack;
+
+use DX::Class;
+
+with 'DX::Role::Step';
+
+sub apply_to {
+  my ($self, $ss) = @_;
+  my ($first_alt, @rest_alt) = @{$ss->alternatives};
+  return $ss->but(next_step => undef) unless $first_alt;
+  trace 'search.backtrack.rewind_to' => $first_alt->[1];
+  return $ss->but(
+    current_hypothesis => $first_alt->[0],
+    alternatives => \@rest_alt,
+    next_step => $first_alt->[1],
+  );
+}
+
+1;