Skip to content

support for look behind #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/ctll/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ template <typename T = _nothing> constexpr auto pop_and_get_front(empty_list, T
template <typename Head, typename... As, typename T = _nothing> constexpr auto front(list<Head, As...>, T = T()) noexcept -> Head { return {}; }
template <typename T = _nothing> constexpr auto front(empty_list, T = T()) noexcept -> T { return {}; }

// rotate list
template <typename T> struct rotate_item {
template <typename... Ts> friend constexpr auto operator+(list<Ts...>, rotate_item<T>) noexcept -> list<T, Ts...> { return {}; }
};

template <typename... Ts> constexpr auto rotate(list<Ts...>) -> decltype((list<>{} + ... + rotate_item<Ts>{})) {
return {};
}

// set operations
template <typename T> struct item_matcher {
Expand Down
34 changes: 34 additions & 0 deletions include/ctre/actions/look.inc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,38 @@ template <auto V, typename... Look, typename... Ts, size_t Counter> static const
return pcre_context{ctll::list<lookahead_negative<Look...>, Ts...>(), pcre_parameters<Counter>()};
}

// LOOKBEHIND

// lookbehind positive start
template <auto V, typename... Ts, size_t Counter> static constexpr auto apply(pcre::start_lookbehind_positive, ctll::term<V>, pcre_context<ctll::list<Ts...>, pcre_parameters<Counter>>) {
return pcre_context{ctll::list<look_start<lookbehind_positive<>>, Ts...>(), pcre_parameters<Counter>()};
}

// lookbehind positive end
template <auto V, typename Look, typename... Ts, size_t Counter> static constexpr auto apply(pcre::look_finish, ctll::term<V>, pcre_context<ctll::list<Look, look_start<lookbehind_positive<>>, Ts...>, pcre_parameters<Counter>>) {
return pcre_context{ctll::list<lookbehind_positive<decltype(ctre::rotate_for_lookbehind::rotate(Look{}))>, Ts...>(), pcre_parameters<Counter>()};
}

// lookbehind positive end (sequence)
template <auto V, typename... Look, typename... Ts, size_t Counter> static constexpr auto apply(pcre::look_finish, ctll::term<V>, pcre_context<ctll::list<ctre::sequence<Look...>, look_start<lookbehind_positive<>>, Ts...>, pcre_parameters<Counter>>) {
using my_lookbehind = decltype(ctre::convert_to_basic_list<lookbehind_positive>(ctll::rotate(ctll::list<decltype(ctre::rotate_for_lookbehind::rotate(Look{}))...>{})));
return pcre_context{ctll::list<my_lookbehind, Ts...>(), pcre_parameters<Counter>()};
}

// lookbehind negative start
template <auto V, typename... Ts, size_t Counter> static constexpr auto apply(pcre::start_lookbehind_negative, ctll::term<V>, pcre_context<ctll::list<Ts...>, pcre_parameters<Counter>>) {
return pcre_context{ctll::list<look_start<lookbehind_negative<>>, Ts...>(), pcre_parameters<Counter>()};
}

// lookbehind negative end
template <auto V, typename Look, typename... Ts, size_t Counter> static constexpr auto apply(pcre::look_finish, ctll::term<V>, pcre_context<ctll::list<Look, look_start<lookbehind_negative<>>, Ts...>, pcre_parameters<Counter>>) {
return pcre_context{ctll::list<lookbehind_negative<decltype(ctre::rotate_for_lookbehind::rotate(Look{}))>, Ts...>(), pcre_parameters<Counter>()};
}

// lookbehind negative end (sequence)
template <auto V, typename... Look, typename... Ts, size_t Counter> static constexpr auto apply(pcre::look_finish, ctll::term<V>, pcre_context<ctll::list<ctre::sequence<Look...>, look_start<lookbehind_negative<>>, Ts...>, pcre_parameters<Counter>>) {
using my_lookbehind = decltype(ctre::convert_to_basic_list<lookbehind_negative>(ctll::rotate(ctll::list<decltype(ctre::rotate_for_lookbehind::rotate(Look{}))...>{})));
return pcre_context{ctll::list<my_lookbehind, Ts...>(), pcre_parameters<Counter>()};
}

#endif
4 changes: 4 additions & 0 deletions include/ctre/atoms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct start_mark { };
struct end_mark { };
struct end_cycle_mark { };
struct end_lookahead_mark { };
struct end_lookbehind_mark { };
template <size_t Id> struct numeric_mark { };

struct any { };
Expand Down Expand Up @@ -51,6 +52,9 @@ template <typename Type> struct look_start { };
template <typename... Content> struct lookahead_positive { };
template <typename... Content> struct lookahead_negative { };

template <typename... Content> struct lookbehind_positive { };
template <typename... Content> struct lookbehind_negative { };

struct atomic_start { };

template <typename... Content> struct atomic_group { };
Expand Down
169 changes: 104 additions & 65 deletions include/ctre/evaluation.hpp

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions include/ctre/first.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ constexpr auto first(ctll::list<Content...>, ctll::list<lookahead_positive<Seq..
return ctll::list<can_be_anything>{};
}

// lookbehind_negative TODO fixme
template <typename... Content, typename... Seq, typename... Tail>
constexpr auto first(ctll::list<Content...>, ctll::list<lookbehind_negative<Seq...>, Tail...>) noexcept {
return ctll::list<can_be_anything>{};
}

// lookbehind_positive
template <typename... Content, typename... Seq, typename... Tail>
constexpr auto first(ctll::list<Content...>, ctll::list<lookbehind_positive<Seq...>, Tail...>) noexcept {
return ctll::list<can_be_anything>{};
}

// lookahead_negative TODO fixme
template <typename... Content, typename... Seq, typename... Tail>
constexpr auto first(ctll::list<Content...>, ctll::list<lookahead_negative<Seq...>, Tail...>) noexcept {
Expand Down
10 changes: 6 additions & 4 deletions include/ctre/pcre.gram
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ number2->epsilon | num,[push_number],<number2>
preblock->open,[prepare_capture],<block>

block-><content_in_capture>,[make_capture],close
block->questionmark,[reset_capture],angle_close,[start_atomic],<content_in_capture>,[make_atomic],close
block->questionmark,[reset_capture],equal_sign,[start_lookahead_positive],<content_in_capture>,[look_finish],close
block->questionmark,[reset_capture],exclamation_mark,[start_lookahead_negative],<content_in_capture>,[look_finish],close
block->questionmark,[reset_capture],colon,<content_in_capture>,close
block->questionmark,angle_close,[reset_capture],[start_atomic],<content_in_capture>,[make_atomic],close
block->questionmark,equal_sign,[reset_capture],[start_lookahead_positive],<content_in_capture>,[look_finish],close
block->questionmark,angle_open,equal_sign,[reset_capture],[start_lookbehind_positive],<content_in_capture>,[look_finish],close
block->questionmark,exclamation_mark,[reset_capture],[start_lookahead_negative],<content_in_capture>,[look_finish],close
block->questionmark,angle_open,exclamation_mark,[reset_capture],[start_lookbehind_negative],<content_in_capture>,[look_finish],close
block->questionmark,colon,[reset_capture],<content_in_capture>,close
block->questionmark,angle_open,<block_name>,angle_close,<content_in_capture>,[make_capture_with_name],close

block_name->alpha_characters,[push_name],<block_name2>
Expand Down
Loading