4 #ifndef LEXY_DSL_LITERAL_HPP_INCLUDED
5 #define LEXY_DSL_LITERAL_HPP_INCLUDED
18 template <std::size_t CurCharIndex,
typename CharT, CharT... Cs,
typename Reader>
21 static_assert(lexy::is_char_encoding<typename Reader::encoding>);
23 if constexpr (CurCharIndex >=
sizeof...(Cs))
26 return std::true_type{};
29 else if constexpr (is_swar_reader<Reader> &&
sizeof...(Cs) >= swar_length<char_type>)
33 constexpr
auto pack = swar_pack<CurCharIndex>(transcode_char<char_type>(Cs)...);
36 if ((reader.peek_swar() & pack.mask) == pack.value)
38 reader.bump_swar(pack.count);
41 return bool(match_literal<CurCharIndex + pack.count, CharT, Cs...>(reader));
45 auto partial = swar_find_difference<CharT>(reader.peek_swar() & pack.mask, pack.value);
46 reader.bump_swar(partial);
52 static_assert(CurCharIndex == 0);
55 return ((reader.peek() == transcode_int<typename Reader::encoding>(Cs)
56 ? (reader.bump(),
true)
66 template <
typename Encoding,
template <
typename>
typename CaseFolding, std::size_t MaxCharCount,
67 typename... CharClasses>
73 template <
typename Reader>
99 template <
typename CharT>
102 auto c = transcode_char<char_type>(_c);
127 template <
typename CharT, CharT... C>
130 return ((pos =
insert(pos, C)), ...);
145 result.index[result.length] = i;
153 template <
typename... CharClasses>
156 template <
typename Encoding,
template <
typename>
typename CaseFolding, std::size_t N>
159 static constexpr
auto size =
sizeof...(CharClasses);
161 template <
typename... T>
168 template <
typename CurrentCaseFolding,
typename... Literals>
173 template <
typename Reader>
176 template <
typename CurrentCaseFolding>
179 template <
typename Reader>
180 using case_folding =
typename CurrentCaseFolding::template reader<Reader>;
182 template <
typename CurrentCaseFolding,
typename H,
typename... T>
185 typename H::lit_case_folding, CurrentCaseFolding>,
188 static_assert(std::is_same_v<CurrentCaseFolding,
189 typename H::lit_case_folding>
190 || std::is_void_v<CurrentCaseFolding>
191 || std::is_void_v<typename H::lit_case_folding>,
192 "cannot mix literals with different case foldings in a literal_set");
195 template <
typename Reader>
198 template <
typename Encoding,
typename... Literals>
201 constexpr
auto max_char_count = (0 + ... + Literals::lit_max_char_count);
204 constexpr
auto char_classes
208 return typename decltype(char_classes)::
template trie_type<
211 template <
typename Encoding,
typename... Literals>
212 using lit_trie_for = decltype(make_empty_trie<Encoding, Literals...>());
214 template <std::size_t CharClassIdx, bool,
typename...>
217 template <
typename Reader>
223 template <
typename H,
typename... T>
226 template <
typename Reader>
232 template <std::size_t Idx,
typename H,
typename... T>
235 template <std::size_t CharClassIdx,
typename... CharClasses>
239 template <const auto& Trie, std::
size_t CurNode>
241 template <
typename Encoding,
template <
typename>
typename CaseFolding, std::size_t N,
242 typename... CharClasses,
const lit_trie<Encoding, CaseFolding, N, CharClasses...>& Trie,
246 template <std::
size_t TransIdx,
typename Reader,
typename IntT>
250 static_assert(Trie.transition_from[TransIdx] == CurNode);
252 using encoding =
typename Reader::encoding;
253 constexpr
auto trans_char = Trie.transition_char[TransIdx];
254 if (cur != encoding::to_int_type(trans_char))
262 static constexpr
auto transitions = Trie.node_transitions(CurNode);
266 template <std::size_t... Idx>
269 template <
typename Reader>
272 constexpr
auto cur_value = Trie.node_value[CurNode];
274 if constexpr (
sizeof...(Idx) > 0)
276 auto cur = reader.current();
277 auto cur_char = reader.peek();
279 auto next_value = Trie.node_no_match;
280 (
void)(_try_transition<transitions.index[Idx]>(next_value, reader, cur_char)
282 if (next_value != Trie.node_no_match)
291 constexpr
auto char_class = Trie.node_char_class[CurNode];
292 if constexpr (cur_value == Trie.node_no_match || char_class >=
sizeof...(CharClasses))
299 return Trie.node_no_match;
306 template <
typename Reader>
309 static_assert(lexy::is_char_encoding<typename Reader::encoding>);
310 if constexpr (std::is_same_v<CaseFolding<Reader>, Reader>)
312 return _impl<>::try_match(_reader);
316 CaseFolding<Reader> reader{_reader};
317 auto result = _impl<>::try_match(reader);
318 _reader.reset(reader.current());
328 template <
typename CharT, CharT... C>
330 : token_base<_lit<CharT, C...>,
331 std::conditional_t<sizeof...(C) == 0, unconditional_branch_base, branch_base>>,
338 template <
typename Encoding>
346 template <
typename Trie>
349 return ((pos = trie.insert(pos, C)), ...);
352 template <
typename Reader>
355 typename Reader::marker
end;
357 constexpr
explicit tp(
const Reader& reader) :
end(reader.current()) {}
362 end = reader.current();
366 template <
typename Context>
372 auto begin = reader.position();
383 template <
unsigned char... C>
387 template <lexy::_detail::
string_literal Str>
389 constexpr
auto lit = lexy::_detail::to_type_string<_lit, Str>{};
392 #define LEXY_LIT(Str) \
393 LEXY_NTTP_STRING(::lexyd::_lit, Str) {}
398 template <
typename CharT, CharT... C>
405 template <char32_t... Cp>
406 struct _lcp : token_base<_lcp<Cp...>>, _lit_base
408 template <
typename Encoding>
416 ((
length += lexy::_detail::encode_code_point<Encoding>(Cp,
data +
length, 4)), ...);
419 template <
typename Encoding>
426 template <
typename Encoding>
429 return _string<Encoding>.data[0];
432 template <
typename Trie>
435 using encoding =
typename Trie::encoding;
437 for (
auto i = 0u; i != _string<encoding>.length; ++i)
438 pos = trie.insert(pos, _string<encoding>.data[i]);
443 template <
typename Reader,
447 template <
typename Reader, std::size_t... Idx>
450 typename Reader::marker
end;
452 constexpr
explicit tp(
const Reader& reader) :
end(reader.current()) {}
456 using encoding =
typename Reader::encoding;
459 _string<encoding>.data[Idx]...>(reader);
460 end = reader.current();
464 template <
typename Context>
467 using encoding =
typename Reader::encoding;
469 auto begin = reader.position();
472 index, _string<encoding>.length);
478 template <char32_t... CodePoint>
484 template <char32_t... Cp>
491 template <
typename T,
template <
typename>
typename CaseFolding,
typename... Strings>
498 return "expected literal set";
505 template <
typename Literal,
template <
typename>
typename CaseFolding>
508 template <
template <
typename>
typename CaseFolding,
typename CharT, CharT... C>
511 if constexpr (std::is_same_v<CaseFolding<lexy::_pr8>, lexy::_pr8>)
512 return _lit<CharT, C...>{};
514 return _cfl<
_lit<CharT, C...>, CaseFolding>{};
517 template <
typename... Literals>
522 template <
typename Encoding>
527 [[maybe_unused]]
auto char_class = std::size_t(0);
528 ((result.node_value[Literals::lit_insert(result, 0, char_class)] = 0,
530 char_class += Literals::lit_char_classes.size),
535 template <
typename Encoding>
537 = _build_trie<Encoding>();
539 template <
typename Reader>
542 typename Reader::marker
end;
544 constexpr
explicit tp(
const Reader& reader) :
end(reader.current()) {}
548 using encoding =
typename Reader::encoding;
551 auto result = matcher::try_match(reader);
552 end = reader.current();
553 return result != _t<encoding>.node_no_match;
556 template <
typename Context>
565 template <
typename Lit>
568 if constexpr (lexy::is_literal_rule<Lit>)
570 return _lset<Literals..., Lit>{};
572 else if constexpr (
sizeof...(Literals) == 0)
575 static_assert(lexy::is_literal_set_rule<Lit>);
581 static_assert(lexy::is_literal_set_rule<Lit>);
582 return *
this /
typename Lit::as_lset{};
585 template <
typename... Lit>
588 return _lset<Literals..., Lit...>{};
593 template <
typename... Literals>
596 static_assert((lexy::is_literal_rule<Literals> && ...));
597 return _lset<Literals...>{};
601 template <
typename T,
template <
typename>
typename CaseFolding,
typename... Strings>
608 #define LEXY_LITERAL_SET(...) \
610 using impl = decltype(::lexyd::literal_set(__VA_ARGS__)); \
613 using impl::operator/; \
620 template <
typename... Literals>
624 #endif // LEXY_DSL_LITERAL_HPP_INCLUDED