Skip to content

Commit 331aebc

Browse files
authored
Merge pull request #258 from hanickadot/lookbehind
support for look behind
2 parents 7d33a6c + be0dc3f commit 331aebc

15 files changed

+977
-280
lines changed

include/ctll/list.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ template <typename T = _nothing> constexpr auto pop_and_get_front(empty_list, T
4747
template <typename Head, typename... As, typename T = _nothing> constexpr auto front(list<Head, As...>, T = T()) noexcept -> Head { return {}; }
4848
template <typename T = _nothing> constexpr auto front(empty_list, T = T()) noexcept -> T { return {}; }
4949

50+
// rotate list
51+
template <typename T> struct rotate_item {
52+
template <typename... Ts> friend constexpr auto operator+(list<Ts...>, rotate_item<T>) noexcept -> list<T, Ts...> { return {}; }
53+
};
54+
55+
template <typename... Ts> constexpr auto rotate(list<Ts...>) -> decltype((list<>{} + ... + rotate_item<Ts>{})) {
56+
return {};
57+
}
5058

5159
// set operations
5260
template <typename T> struct item_matcher {

include/ctre/actions/look.inc.hpp

+34
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,38 @@ template <auto V, typename... Look, typename... Ts, size_t Counter> static const
3131
return pcre_context{ctll::list<lookahead_negative<Look...>, Ts...>(), pcre_parameters<Counter>()};
3232
}
3333

34+
// LOOKBEHIND
35+
36+
// lookbehind positive start
37+
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>>) {
38+
return pcre_context{ctll::list<look_start<lookbehind_positive<>>, Ts...>(), pcre_parameters<Counter>()};
39+
}
40+
41+
// lookbehind positive end
42+
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>>) {
43+
return pcre_context{ctll::list<lookbehind_positive<decltype(ctre::rotate_for_lookbehind::rotate(Look{}))>, Ts...>(), pcre_parameters<Counter>()};
44+
}
45+
46+
// lookbehind positive end (sequence)
47+
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>>) {
48+
using my_lookbehind = decltype(ctre::convert_to_basic_list<lookbehind_positive>(ctll::rotate(ctll::list<decltype(ctre::rotate_for_lookbehind::rotate(Look{}))...>{})));
49+
return pcre_context{ctll::list<my_lookbehind, Ts...>(), pcre_parameters<Counter>()};
50+
}
51+
52+
// lookbehind negative start
53+
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>>) {
54+
return pcre_context{ctll::list<look_start<lookbehind_negative<>>, Ts...>(), pcre_parameters<Counter>()};
55+
}
56+
57+
// lookbehind negative end
58+
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>>) {
59+
return pcre_context{ctll::list<lookbehind_negative<decltype(ctre::rotate_for_lookbehind::rotate(Look{}))>, Ts...>(), pcre_parameters<Counter>()};
60+
}
61+
62+
// lookbehind negative end (sequence)
63+
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>>) {
64+
using my_lookbehind = decltype(ctre::convert_to_basic_list<lookbehind_negative>(ctll::rotate(ctll::list<decltype(ctre::rotate_for_lookbehind::rotate(Look{}))...>{})));
65+
return pcre_context{ctll::list<my_lookbehind, Ts...>(), pcre_parameters<Counter>()};
66+
}
67+
3468
#endif

include/ctre/atoms.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct start_mark { };
1313
struct end_mark { };
1414
struct end_cycle_mark { };
1515
struct end_lookahead_mark { };
16+
struct end_lookbehind_mark { };
1617
template <size_t Id> struct numeric_mark { };
1718

1819
struct any { };
@@ -51,6 +52,9 @@ template <typename Type> struct look_start { };
5152
template <typename... Content> struct lookahead_positive { };
5253
template <typename... Content> struct lookahead_negative { };
5354

55+
template <typename... Content> struct lookbehind_positive { };
56+
template <typename... Content> struct lookbehind_negative { };
57+
5458
struct atomic_start { };
5559

5660
template <typename... Content> struct atomic_group { };

include/ctre/evaluation.hpp

+104-65
Large diffs are not rendered by default.

include/ctre/first.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,18 @@ constexpr auto first(ctll::list<Content...>, ctll::list<lookahead_positive<Seq..
161161
return ctll::list<can_be_anything>{};
162162
}
163163

164+
// lookbehind_negative TODO fixme
165+
template <typename... Content, typename... Seq, typename... Tail>
166+
constexpr auto first(ctll::list<Content...>, ctll::list<lookbehind_negative<Seq...>, Tail...>) noexcept {
167+
return ctll::list<can_be_anything>{};
168+
}
169+
170+
// lookbehind_positive
171+
template <typename... Content, typename... Seq, typename... Tail>
172+
constexpr auto first(ctll::list<Content...>, ctll::list<lookbehind_positive<Seq...>, Tail...>) noexcept {
173+
return ctll::list<can_be_anything>{};
174+
}
175+
164176
// lookahead_negative TODO fixme
165177
template <typename... Content, typename... Seq, typename... Tail>
166178
constexpr auto first(ctll::list<Content...>, ctll::list<lookahead_negative<Seq...>, Tail...>) noexcept {

include/ctre/pcre.gram

+6-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@ number2->epsilon | num,[push_number],<number2>
6767
preblock->open,[prepare_capture],<block>
6868

6969
block-><content_in_capture>,[make_capture],close
70-
block->questionmark,[reset_capture],angle_close,[start_atomic],<content_in_capture>,[make_atomic],close
71-
block->questionmark,[reset_capture],equal_sign,[start_lookahead_positive],<content_in_capture>,[look_finish],close
72-
block->questionmark,[reset_capture],exclamation_mark,[start_lookahead_negative],<content_in_capture>,[look_finish],close
73-
block->questionmark,[reset_capture],colon,<content_in_capture>,close
70+
block->questionmark,angle_close,[reset_capture],[start_atomic],<content_in_capture>,[make_atomic],close
71+
block->questionmark,equal_sign,[reset_capture],[start_lookahead_positive],<content_in_capture>,[look_finish],close
72+
block->questionmark,angle_open,equal_sign,[reset_capture],[start_lookbehind_positive],<content_in_capture>,[look_finish],close
73+
block->questionmark,exclamation_mark,[reset_capture],[start_lookahead_negative],<content_in_capture>,[look_finish],close
74+
block->questionmark,angle_open,exclamation_mark,[reset_capture],[start_lookbehind_negative],<content_in_capture>,[look_finish],close
75+
block->questionmark,colon,[reset_capture],<content_in_capture>,close
7476
block->questionmark,angle_open,<block_name>,angle_close,<content_in_capture>,[make_capture_with_name],close
7577

7678
block_name->alpha_characters,[push_name],<block_name2>

0 commit comments

Comments
 (0)