Skip to content

Commit

Permalink
Fix ordering for possessive repeat.
Browse files Browse the repository at this point in the history
  • Loading branch information
leni536 committed Oct 30, 2018
1 parent 866ae7a commit a2cc417
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions include/ctre/evaluation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ constexpr CTRE_FORCE_INLINE R evaluate(const Iterator begin, Iterator current, c

template <typename R, typename Iterator, typename EndIterator, typename... Tail>
constexpr CTRE_FORCE_INLINE R ordered_evaluate(const Iterator begin, Iterator current, const EndIterator end, R captures, ctll::list<assert_begin, Tail...>) noexcept {
if (begin != current) { //TODO I'm really not sure how to handle this.
// Problematic pattern: "x*+^[a-z]*" (words that don't start with x)
return not_matched;
if (begin != current) {
captures.mask_elg({0,1,1});
return captures;
}
return ordered_evaluate(begin, current, end, captures, ctll::list<Tail...>());
}
Expand Down Expand Up @@ -406,7 +406,6 @@ constexpr CTRE_FORCE_INLINE R evaluate(const Iterator begin, Iterator current, c

template <typename R, typename Iterator, typename EndIterator, size_t A, size_t B, typename... Content, typename... Tail>
constexpr CTRE_FORCE_INLINE R ordered_evaluate(const Iterator begin, Iterator current, const EndIterator end, R captures, ctll::list<possessive_repeat<A,B,Content...>, Tail...>) noexcept {
//TODO this doesn't work yet!
// A..B
size_t i{0};
for (; i < A && (A != 0); ++i) {
Expand All @@ -419,12 +418,18 @@ constexpr CTRE_FORCE_INLINE R ordered_evaluate(const Iterator begin, Iterator cu

for (; (i < B) || (B == 0); ++i) {
// try as many of inner as possible and then try outer once

// I have to mask this ordering beforehand, as we can't backtrack later.
// This basically throws away the usual runtime benefits of a possessive repeat.
auto outer_result = ordered_evaluate(begin, current, end, captures, ctll::list<Tail...>());
captures.mask_lg(outer_result);

auto inner_result = ordered_evaluate(begin, current, end, captures, ctll::list<sequence<Content...>, end_cycle_mark>());
captures.mask_lg(inner_result);
if (inner_result) {
current = inner_result.get_end_position();
} else {
return ordered_evaluate(begin, current, end, captures, ctll::list<Tail...>());
return outer_result;
}
}

Expand Down
2 changes: 1 addition & 1 deletion test_ordering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ std::ostream& operator<<(std::ostream& os, ctre::partial_ordering ord) {

int main(int argc, char ** argv) {
using namespace ctre::literals;
constexpr auto re = "(?<ab>[b-c])\\g{ab}"_ctre;
constexpr auto re = "x*+^[a-y]+"_ctre;

std::cout << ctre::partial_ordering(re.match(argv[1])) << std::endl;
std::cout << ctre::partial_ordering(re.ordered_match(argv[1])) << std::endl;
Expand Down

0 comments on commit a2cc417

Please sign in to comment.