Skip to content

Commit eee10a6

Browse files
author
Hana Dusíková
committed
change trampolining to avoid instantiation and add fixed_string alias
1 parent e2879f5 commit eee10a6

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

include/ctll/parser.hpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ enum class decision {
1515
undecided
1616
};
1717

18+
template <typename T> void id(T);
19+
20+
struct placeholder { };
21+
1822
#if !__cpp_nontype_template_parameter_class
1923
template <typename Grammar, const auto & input, typename ActionSelector = empty_actions, bool IgnoreUnknownActions = false> struct parser {
2024
#else
@@ -25,8 +29,6 @@ template <typename Grammar, basic_fixed_string input, typename ActionSelector =
2529

2630
template <size_t Pos, typename Stack = void, typename Subject = void, decision Decision = decision::undecided> struct seed;
2731

28-
template <size_t Pos> struct placeholder { };
29-
3032
template <size_t Pos> static constexpr auto get_current_term() noexcept {
3133
if constexpr (Pos < input.size()) {
3234
return term<input[Pos]>{};
@@ -107,6 +109,7 @@ template <typename Grammar, basic_fixed_string input, typename ActionSelector =
107109
}
108110
}
109111
// helper type for trampoline
112+
110113
template <size_t Pos, typename Stack, typename Subject, decision Decision> struct seed {
111114
constexpr inline CTLL_FORCE_INLINE operator bool() const noexcept {
112115
return Decision == decision::accept;
@@ -119,22 +122,26 @@ template <typename Grammar, basic_fixed_string input, typename ActionSelector =
119122
return decide<Pos>(Stack{}, Subject{});
120123
}
121124

122-
template <size_t RPos> constexpr auto operator+(placeholder<RPos>) const noexcept {
125+
constexpr auto operator+(placeholder) const noexcept {
123126
if constexpr (Decision == decision::undecided) {
124127
// parse for current char (RPos) with previous stack and subject :)
125-
return decltype(seed<RPos, Stack, Subject, Decision>::parse()){};
128+
return decltype(seed<Pos, Stack, Subject, Decision>::parse()){};
126129
} else {
127130
// if there is decision already => just push it to the end of fold expression
128131
return *this;
129132
}
130133
}
131134
};
132135

136+
template <size_t> using index_placeholder = placeholder;
137+
133138
// trampolines with folded expression
134139
template <typename Subject, size_t... Pos> static constexpr auto trampoline_decide(Subject, std::index_sequence<Pos...>) noexcept {
135140
// parse everything for first char and than for next and next ...
136141
// Pos+1 is needed as we want to finish calculation with epsilons on stack
137-
return (seed<0, decltype(grammar.start_stack), Subject, decision::undecided>::parse() + ... + placeholder<Pos+1>());
142+
auto v = (seed<0, decltype(grammar.start_stack), Subject, decision::undecided>::parse() + ... + index_placeholder<Pos+1>());
143+
//id(v);
144+
return v;
138145
}
139146

140147
template <typename Subject = empty_subject> static constexpr auto trampoline_decide(Subject subject = {}) noexcept {

include/ctre/functions.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ template <typename CharT, size_t N> class pattern: public ctll::basic_fixed_stri
1919

2020
template <typename CharT, size_t N> pattern(const CharT (&)[N]) -> pattern<CharT, N>;
2121

22+
// for better examples
23+
template <typename CharT, size_t N> class fixed_string: public ctll::basic_fixed_string<CharT, N> {
24+
using parent = ctll::basic_fixed_string<CharT, N>;
25+
public:
26+
constexpr fixed_string(const CharT (&input)[N]) noexcept: parent(input) { }
27+
};
28+
29+
template <typename CharT, size_t N> fixed_string(const CharT (&)[N]) -> fixed_string<CharT, N>;
30+
2231
#if __cpp_nontype_template_parameter_class
2332
template <basic_fixed_string input> CTRE_FLATTEN constexpr CTRE_FORCE_INLINE auto re() noexcept {
2433
#else

test.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,12 @@
44
#include <iostream>
55

66

7-
void match(std::string_view subject) {
8-
using namespace ctre::literals;
9-
10-
if (auto [matched, inner] = "^([0-9]+?[a-z]++)+"_ctre.match(subject); matched) {
11-
std::cout << "matched = " << matched.to_view() << "\n";
12-
if (inner) {
13-
std::cout << " inner = " << inner.to_view() << "\n";
14-
}
15-
}
7+
static constexpr ctre::pattern pattern = "(?:hello|cau|guten tag|ola)|(?:hello|cau|guten tag|ola)|(?:hello|cau|guten tag|ola)|(?:hello|cau|guten tag|ola)|(?:hello|cau|guten tag|ola)|(?:hello|cau|guten tag|ola)|(?:hello|cau|guten tag|ola)";
8+
9+
bool fnc(std::string_view view) {
10+
return ctre::match<pattern>(view);
1611
}
1712

1813
int main() {
19-
using namespace std::string_view_literals;
20-
match("123abc456def"sv);
21-
22-
auto input = "123,456,768"sv;
23-
24-
using namespace ctre::literals;
25-
for (auto match: ctre::range(input,"[0-9]++"_ctre)) {
26-
std::cout << std::string_view{match} << "\n";
27-
}
28-
14+
std::cout << fnc("hello") << "\n";
2915
}

0 commit comments

Comments
 (0)