2017-09-17 10:50:53 +02:00
|
|
|
#include "regex_impl.hh"
|
2017-10-07 04:43:21 +02:00
|
|
|
|
|
|
|
#include "exception.hh"
|
2017-09-17 10:50:53 +02:00
|
|
|
#include "string.hh"
|
2017-09-18 04:56:14 +02:00
|
|
|
#include "unicode.hh"
|
2017-10-07 04:43:21 +02:00
|
|
|
#include "unit_tests.hh"
|
2017-09-25 15:24:24 +02:00
|
|
|
#include "utf8.hh"
|
|
|
|
#include "utf8_iterator.hh"
|
2017-10-09 08:37:43 +02:00
|
|
|
#include "string_utils.hh"
|
2017-10-07 04:43:21 +02:00
|
|
|
#include "vector.hh"
|
2018-10-10 13:35:38 +02:00
|
|
|
#include "utils.hh"
|
2017-09-26 09:44:30 +02:00
|
|
|
|
Fix build on FreeBSD
file.cc:390:21: error: use of undeclared identifier 'rename'; did you mean 'devname'?
if (replace and rename(temp_filename, zfilename) != 0)
^~~~~~
devname
/usr/include/stdlib.h:277:7: note: 'devname' declared here
char *devname(__dev_t, __mode_t);
^
file.cc:390:28: error: cannot initialize a parameter of type '__dev_t' (aka 'unsigned long') with an lvalue of type 'char [1024]'
if (replace and rename(temp_filename, zfilename) != 0)
^~~~~~~~~~~~~
/usr/include/stdlib.h:277:22: note: passing argument to parameter here
char *devname(__dev_t, __mode_t);
^
2 errors generated.
---
highlighters.cc:1110:13: error: use of undeclared identifier 'snprintf'; did you mean 'vswprintf'?
snprintf(buffer, 16, format, std::abs(line_to_format));
^~~~~~~~
vswprintf
/usr/include/wchar.h:139:5: note: 'vswprintf' declared here
int vswprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
highlighters.cc:1110:22: error: cannot initialize a parameter of type 'wchar_t *' with an lvalue of type 'char [16]'
snprintf(buffer, 16, format, std::abs(line_to_format));
^~~~~~
/usr/include/wchar.h:139:35: note: passing argument to parameter here
int vswprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
2 errors generated.
---
json_ui.cc:60:13: error: use of undeclared identifier 'sprintf'; did you mean 'swprintf'?
sprintf(buf, "\\u%04x", *next);
^~~~~~~
swprintf
/usr/include/wchar.h:133:5: note: 'swprintf' declared here
int swprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
json_ui.cc:60:21: error: cannot initialize a parameter of type 'wchar_t *' with an lvalue of type 'char [7]'
sprintf(buf, "\\u%04x", *next);
^~~
/usr/include/wchar.h:133:34: note: passing argument to parameter here
int swprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
json_ui.cc:74:9: error: use of undeclared identifier 'sprintf'
sprintf(buffer, R"("#%02x%02x%02x")", color.r, color.g, color.b);
^
3 errors generated.
---
regex_impl.cc:1039:9: error: use of undeclared identifier 'sprintf'; did you mean 'swprintf'?
sprintf(buf, " %03d ", count++);
^~~~~~~
swprintf
/usr/include/wchar.h:133:5: note: 'swprintf' declared here
int swprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
regex_impl.cc:1039:17: error: cannot initialize a parameter of type 'wchar_t *' with an lvalue of type 'char [20]'
sprintf(buf, " %03d ", count++);
^~~
/usr/include/wchar.h:133:34: note: passing argument to parameter here
int swprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
regex_impl.cc:1197:17: error: use of undeclared identifier 'puts'
{ if (dump) puts(dump_regex(*this).c_str()); }
^
regex_impl.cc:1208:18: note: in instantiation of member function 'Kakoune::(anonymous namespace)::TestVM<Kakoune::RegexMode::Forward>::TestVM' requested here
TestVM<> vm{R"(a*b)"};
^
regex_impl.cc:1197:17: error: use of undeclared identifier 'puts'
{ if (dump) puts(dump_regex(*this).c_str()); }
^
regex_impl.cc:1283:56: note: in instantiation of member function 'Kakoune::(anonymous namespace)::TestVM<5>::TestVM' requested here
TestVM<RegexMode::Forward | RegexMode::Search> vm{R"(f.*a(.*o))"};
^
regex_impl.cc:1197:17: error: use of undeclared identifier 'puts'
{ if (dump) puts(dump_regex(*this).c_str()); }
^
regex_impl.cc:1423:57: note: in instantiation of member function 'Kakoune::(anonymous namespace)::TestVM<6>::TestVM' requested here
TestVM<RegexMode::Backward | RegexMode::Search> vm{R"(fo{1,})"};
^
5 errors generated.
---
remote.cc:829:9: error: use of undeclared identifier 'rename'; did you mean 'devname'?
if (rename(old_socket_file.c_str(), new_socket_file.c_str()) != 0)
^~~~~~
devname
/usr/include/stdlib.h:277:7: note: 'devname' declared here
char *devname(__dev_t, __mode_t);
^
remote.cc:829:16: error: cannot initialize a parameter of type '__dev_t' (aka 'unsigned long') with an rvalue of type 'const char *'
if (rename(old_socket_file.c_str(), new_socket_file.c_str()) != 0)
^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/stdlib.h:277:22: note: passing argument to parameter here
char *devname(__dev_t, __mode_t);
^
2 errors generated.
---
string_utils.cc:126:20: error: use of undeclared identifier 'sprintf'; did you mean 'swprintf'?
res.m_length = sprintf(res.m_data, "%i", val);
^~~~~~~
swprintf
/usr/include/wchar.h:133:5: note: 'swprintf' declared here
int swprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
string_utils.cc:126:28: error: cannot initialize a parameter of type 'wchar_t *' with an lvalue of type 'char [15]'
res.m_length = sprintf(res.m_data, "%i", val);
^~~~~~~~~~
/usr/include/wchar.h:133:34: note: passing argument to parameter here
int swprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
string_utils.cc:133:20: error: use of undeclared identifier 'sprintf'; did you mean 'swprintf'?
res.m_length = sprintf(res.m_data, "%u", val);
^~~~~~~
swprintf
[...]
2019-07-01 16:32:14 +02:00
|
|
|
#include <cstdio>
|
2018-04-05 00:52:33 +02:00
|
|
|
#include <cstring>
|
2021-01-03 00:58:09 +01:00
|
|
|
#include <limits>
|
2017-10-14 10:50:49 +02:00
|
|
|
|
2017-09-17 10:50:53 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2017-12-01 07:46:18 +01:00
|
|
|
constexpr Codepoint CompiledRegex::StartDesc::other;
|
2018-04-29 08:42:46 +02:00
|
|
|
constexpr Codepoint CompiledRegex::StartDesc::count;
|
2017-10-21 04:04:08 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
struct ParsedRegex
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-10-09 15:04:28 +02:00
|
|
|
enum Op : char
|
2017-09-19 10:44:58 +02:00
|
|
|
{
|
|
|
|
Literal,
|
|
|
|
AnyChar,
|
2018-06-24 12:13:35 +02:00
|
|
|
AnyCharExceptNewLine,
|
2020-07-25 06:07:57 +02:00
|
|
|
CharClass,
|
|
|
|
CharType,
|
2017-09-26 16:04:47 +02:00
|
|
|
Sequence,
|
|
|
|
Alternation,
|
2017-09-19 10:44:58 +02:00
|
|
|
LineStart,
|
|
|
|
LineEnd,
|
|
|
|
WordBoundary,
|
|
|
|
NotWordBoundary,
|
|
|
|
SubjectBegin,
|
|
|
|
SubjectEnd,
|
2017-09-26 17:01:06 +02:00
|
|
|
ResetStart,
|
2017-09-28 14:43:45 +02:00
|
|
|
LookAhead,
|
|
|
|
NegativeLookAhead,
|
2017-10-04 17:00:19 +02:00
|
|
|
LookBehind,
|
2017-09-28 14:43:45 +02:00
|
|
|
NegativeLookBehind,
|
2017-09-19 10:44:58 +02:00
|
|
|
};
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
struct Quantifier
|
|
|
|
{
|
2017-10-09 15:04:28 +02:00
|
|
|
enum Type : char
|
2017-09-26 16:04:47 +02:00
|
|
|
{
|
|
|
|
One,
|
|
|
|
Optional,
|
|
|
|
RepeatZeroOrMore,
|
|
|
|
RepeatOneOrMore,
|
|
|
|
RepeatMinMax,
|
|
|
|
};
|
|
|
|
Type type = One;
|
2017-09-28 11:50:04 +02:00
|
|
|
bool greedy = true;
|
2017-10-23 09:51:24 +02:00
|
|
|
int16_t min = -1, max = -1;
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
bool allows_none() const
|
|
|
|
{
|
|
|
|
return type == Quantifier::Optional or
|
|
|
|
type == Quantifier::RepeatZeroOrMore or
|
|
|
|
(type == Quantifier::RepeatMinMax and min <= 0);
|
|
|
|
}
|
2017-09-19 11:26:24 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
bool allows_infinite_repeat() const
|
|
|
|
{
|
|
|
|
return type == Quantifier::RepeatZeroOrMore or
|
|
|
|
type == Quantifier::RepeatOneOrMore or
|
2017-10-23 09:51:24 +02:00
|
|
|
(type == Quantifier::RepeatMinMax and max < 0);
|
2017-09-26 16:04:47 +02:00
|
|
|
};
|
2017-09-18 11:22:11 +02:00
|
|
|
};
|
|
|
|
|
2020-07-25 06:07:57 +02:00
|
|
|
using NodeIndex = int16_t;
|
2021-08-21 16:06:14 +02:00
|
|
|
struct [[gnu::packed]] Node
|
2017-09-18 11:22:11 +02:00
|
|
|
{
|
2017-09-26 16:04:47 +02:00
|
|
|
Op op;
|
2017-10-09 15:04:28 +02:00
|
|
|
bool ignore_case;
|
2017-10-23 09:45:43 +02:00
|
|
|
NodeIndex children_end;
|
2017-09-26 16:04:47 +02:00
|
|
|
Codepoint value;
|
|
|
|
Quantifier quantifier;
|
2021-08-20 16:13:34 +02:00
|
|
|
uint16_t filler = 0;
|
2021-08-21 16:06:14 +02:00
|
|
|
};
|
2017-11-27 18:03:54 +01:00
|
|
|
static_assert(sizeof(Node) == 16, "");
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-10-23 09:45:43 +02:00
|
|
|
Vector<Node, MemoryDomain::Regex> nodes;
|
2017-11-25 11:14:15 +01:00
|
|
|
|
|
|
|
Vector<CharacterClass, MemoryDomain::Regex> character_classes;
|
2019-01-03 12:52:15 +01:00
|
|
|
Vector<CompiledRegex::NamedCapture, MemoryDomain::Regex> named_captures;
|
|
|
|
uint32_t capture_count;
|
2017-09-23 09:18:21 +02:00
|
|
|
};
|
|
|
|
|
2017-10-23 09:35:43 +02:00
|
|
|
namespace
|
|
|
|
{
|
2018-12-19 13:21:47 +01:00
|
|
|
|
2018-12-27 01:29:33 +01:00
|
|
|
template<RegexMode mode = RegexMode::Forward>
|
2018-12-19 13:21:47 +01:00
|
|
|
struct Children
|
2017-10-23 09:35:43 +02:00
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
static_assert(has_direction(mode));
|
2018-12-19 13:21:47 +01:00
|
|
|
using Index = ParsedRegex::NodeIndex;
|
|
|
|
struct Sentinel {};
|
|
|
|
struct Iterator
|
2017-10-23 09:35:43 +02:00
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
static constexpr bool forward = mode & RegexMode::Forward;
|
2018-12-19 13:21:47 +01:00
|
|
|
Iterator(ArrayView<const ParsedRegex::Node> nodes, Index index)
|
|
|
|
: m_nodes{nodes},
|
|
|
|
m_pos(forward ? index+1 : find_prev(index, nodes[index].children_end)),
|
|
|
|
m_end(forward ? nodes[index].children_end : index)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Iterator& operator++()
|
2018-02-09 12:35:12 +01:00
|
|
|
{
|
2018-12-19 13:21:47 +01:00
|
|
|
m_pos = forward ? m_nodes[m_pos].children_end : find_prev(m_end, m_pos);
|
|
|
|
return *this;
|
2018-02-09 12:35:12 +01:00
|
|
|
}
|
2017-10-23 09:35:43 +02:00
|
|
|
|
2018-12-19 13:21:47 +01:00
|
|
|
Index operator*() const { return m_pos; }
|
|
|
|
bool operator!=(Sentinel) const { return m_pos != m_end; }
|
|
|
|
|
2022-08-05 12:29:43 +02:00
|
|
|
private:
|
2018-12-19 13:21:47 +01:00
|
|
|
Index find_prev(Index parent, Index pos) const
|
2018-02-09 12:35:12 +01:00
|
|
|
{
|
2018-12-19 13:21:47 +01:00
|
|
|
Index child = parent+1;
|
|
|
|
if (child == pos)
|
|
|
|
return parent;
|
|
|
|
while (m_nodes[child].children_end != pos)
|
|
|
|
child = m_nodes[child].children_end;
|
|
|
|
return child;
|
2018-02-09 12:35:12 +01:00
|
|
|
}
|
2018-12-19 13:21:47 +01:00
|
|
|
|
|
|
|
ArrayView<const ParsedRegex::Node> m_nodes;
|
|
|
|
Index m_pos;
|
|
|
|
Index m_end;
|
|
|
|
};
|
|
|
|
|
|
|
|
Iterator begin() const { return {m_parsed_regex.nodes, m_index}; }
|
|
|
|
Sentinel end() const { return {}; }
|
|
|
|
|
|
|
|
const ParsedRegex& m_parsed_regex;
|
|
|
|
const Index m_index;
|
2018-02-09 12:35:12 +01:00
|
|
|
};
|
2018-12-19 13:21:47 +01:00
|
|
|
|
2017-10-23 09:35:43 +02:00
|
|
|
}
|
|
|
|
|
2017-09-23 09:18:21 +02:00
|
|
|
// Recursive descent parser based on naming used in the ECMAScript
|
2017-09-19 04:57:02 +02:00
|
|
|
// standard, although the syntax is not fully compatible.
|
2017-09-26 15:38:56 +02:00
|
|
|
struct RegexParser
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
RegexParser(StringView re)
|
|
|
|
: m_regex{re}, m_pos{re.begin(), re}
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
m_parsed_regex.capture_count = 1;
|
2017-11-27 17:59:57 +01:00
|
|
|
m_parsed_regex.nodes.reserve((size_t)re.length());
|
2017-10-23 09:45:43 +02:00
|
|
|
NodeIndex root = disjunction(0);
|
2017-10-23 09:35:43 +02:00
|
|
|
kak_assert(root == 0);
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
ParsedRegex get_parsed_regex() { return std::move(m_parsed_regex); }
|
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
static ParsedRegex parse(StringView re) { return RegexParser{re}.get_parsed_regex(); }
|
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
private:
|
2017-09-26 09:44:30 +02:00
|
|
|
struct InvalidPolicy
|
|
|
|
{
|
2019-11-02 21:49:26 +01:00
|
|
|
Codepoint operator()(Codepoint cp) const { throw regex_error{"Invalid utf8 in regex"}; }
|
2017-09-26 09:44:30 +02:00
|
|
|
};
|
|
|
|
|
2018-06-17 12:28:07 +02:00
|
|
|
enum class Flags
|
|
|
|
{
|
|
|
|
None = 0,
|
|
|
|
IgnoreCase = 1 << 0,
|
|
|
|
DotMatchesNewLine = 1 << 1,
|
|
|
|
};
|
|
|
|
friend constexpr bool with_bit_ops(Meta::Type<Flags>) { return true; }
|
|
|
|
|
2018-10-31 09:41:12 +01:00
|
|
|
using Iterator = utf8::iterator<const char*, const char*, Codepoint, int, InvalidPolicy>;
|
2017-10-23 09:45:43 +02:00
|
|
|
using NodeIndex = ParsedRegex::NodeIndex;
|
2017-09-26 09:44:30 +02:00
|
|
|
|
2019-01-03 12:52:15 +01:00
|
|
|
NodeIndex disjunction(uint32_t capture = -1)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2021-11-24 22:56:52 +01:00
|
|
|
NodeIndex index = add_node(ParsedRegex::Alternation, capture);
|
2017-10-23 09:35:43 +02:00
|
|
|
while (true)
|
2017-10-09 15:04:28 +02:00
|
|
|
{
|
2021-11-24 22:56:52 +01:00
|
|
|
alternative(ParsedRegex::Sequence);
|
2017-10-23 09:35:43 +02:00
|
|
|
if (at_end() or *m_pos != '|')
|
|
|
|
break;
|
2017-10-09 15:04:28 +02:00
|
|
|
++m_pos;
|
|
|
|
}
|
2017-10-23 09:35:43 +02:00
|
|
|
get_node(index).children_end = m_parsed_regex.nodes.size();
|
|
|
|
|
|
|
|
return index;
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2021-11-24 22:56:52 +01:00
|
|
|
NodeIndex alternative(ParsedRegex::Op op)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2021-11-24 22:56:52 +01:00
|
|
|
NodeIndex index = add_node(op);
|
|
|
|
while (term())
|
2017-10-23 09:35:43 +02:00
|
|
|
{}
|
|
|
|
get_node(index).children_end = m_parsed_regex.nodes.size();
|
|
|
|
|
|
|
|
return index;
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-10-23 09:45:43 +02:00
|
|
|
Optional<NodeIndex> term()
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-10-13 07:09:33 +02:00
|
|
|
while (modifiers()) // read all modifiers
|
2017-10-13 04:44:24 +02:00
|
|
|
{}
|
2017-09-26 15:38:56 +02:00
|
|
|
if (auto node = assertion())
|
2017-09-17 12:15:43 +02:00
|
|
|
return node;
|
2017-09-26 15:38:56 +02:00
|
|
|
if (auto node = atom())
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-10-23 09:35:43 +02:00
|
|
|
get_node(*node).quantifier = quantifier();
|
2017-09-17 12:15:43 +02:00
|
|
|
return node;
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-10-23 09:35:43 +02:00
|
|
|
return {};
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-11-30 07:32:29 +01:00
|
|
|
bool modifiers()
|
2017-10-13 04:44:24 +02:00
|
|
|
{
|
2017-11-25 11:13:27 +01:00
|
|
|
auto it = m_pos.base();
|
2017-11-30 07:32:29 +01:00
|
|
|
if (m_regex.end() - it >= 4 and *it++ == '(' and *it++ == '?')
|
2017-10-13 04:44:24 +02:00
|
|
|
{
|
2018-06-04 18:00:59 +02:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
auto m = *it++;
|
2018-06-17 12:28:07 +02:00
|
|
|
switch (m)
|
2018-06-04 18:00:59 +02:00
|
|
|
{
|
2018-06-17 12:28:07 +02:00
|
|
|
case 'i': m_flags |= Flags::IgnoreCase; break;
|
|
|
|
case 'I': m_flags &= ~Flags::IgnoreCase; break;
|
|
|
|
case 's': m_flags |= Flags::DotMatchesNewLine; break;
|
|
|
|
case 'S': m_flags &= ~Flags::DotMatchesNewLine; break;
|
|
|
|
case ')':
|
|
|
|
m_pos = Iterator{it, m_regex};
|
|
|
|
return true;
|
|
|
|
default: return false;
|
2018-06-04 18:00:59 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-13 04:44:24 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-23 09:45:43 +02:00
|
|
|
Optional<NodeIndex> assertion()
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
if (at_end())
|
2017-10-23 09:35:43 +02:00
|
|
|
return {};
|
2017-09-25 15:24:24 +02:00
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
switch (*m_pos)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2021-11-24 22:56:52 +01:00
|
|
|
case '^': ++m_pos; return add_node(ParsedRegex::LineStart);
|
|
|
|
case '$': ++m_pos; return add_node(ParsedRegex::LineEnd);
|
2017-09-18 04:56:14 +02:00
|
|
|
case '\\':
|
2017-09-26 15:38:56 +02:00
|
|
|
if (m_pos+1 == m_regex.end())
|
2017-10-23 09:35:43 +02:00
|
|
|
return {};
|
2017-09-26 15:38:56 +02:00
|
|
|
switch (*(m_pos+1))
|
2017-09-18 04:56:14 +02:00
|
|
|
{
|
2021-11-24 22:56:52 +01:00
|
|
|
case 'b': m_pos += 2; return add_node(ParsedRegex::WordBoundary);
|
|
|
|
case 'B': m_pos += 2; return add_node(ParsedRegex::NotWordBoundary);
|
|
|
|
case 'A': m_pos += 2; return add_node(ParsedRegex::SubjectBegin);
|
|
|
|
case 'z': m_pos += 2; return add_node(ParsedRegex::SubjectEnd);
|
|
|
|
case 'K': m_pos += 2; return add_node(ParsedRegex::ResetStart);
|
2017-09-18 04:56:14 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-10-13 04:44:24 +02:00
|
|
|
case '(':
|
|
|
|
{
|
2017-11-30 07:32:29 +01:00
|
|
|
auto it = m_pos.base()+1;
|
|
|
|
if (m_regex.end() - it <= 2 or *it++ != '?')
|
|
|
|
return {};
|
|
|
|
|
|
|
|
ParsedRegex::Op op;
|
|
|
|
switch (*it++)
|
2017-10-13 04:44:24 +02:00
|
|
|
{
|
2017-11-30 07:32:29 +01:00
|
|
|
case '=': op = ParsedRegex::LookAhead; break;
|
|
|
|
case '!': op = ParsedRegex::NegativeLookAhead; break;
|
|
|
|
case '<':
|
2017-10-13 04:44:24 +02:00
|
|
|
{
|
2017-11-30 07:32:29 +01:00
|
|
|
switch (*it++)
|
|
|
|
{
|
|
|
|
case '=': op = ParsedRegex::LookBehind; break;
|
|
|
|
case '!': op = ParsedRegex::NegativeLookBehind; break;
|
|
|
|
default: return {};
|
|
|
|
}
|
2017-10-13 04:44:24 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-11-30 07:32:29 +01:00
|
|
|
default: return {};
|
2017-10-13 04:44:24 +02:00
|
|
|
}
|
2017-11-30 07:32:29 +01:00
|
|
|
m_pos = Iterator{it, m_regex};
|
|
|
|
NodeIndex lookaround = alternative(op);
|
2017-10-13 04:44:24 +02:00
|
|
|
if (at_end() or *m_pos++ != ')')
|
|
|
|
parse_error("unclosed parenthesis");
|
|
|
|
|
|
|
|
validate_lookaround(lookaround);
|
|
|
|
return lookaround;
|
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-10-23 09:35:43 +02:00
|
|
|
return {};
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-10-23 09:45:43 +02:00
|
|
|
Optional<NodeIndex> atom()
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
if (at_end())
|
2017-10-23 09:35:43 +02:00
|
|
|
return {};
|
2017-09-25 15:24:24 +02:00
|
|
|
|
2017-11-30 07:32:29 +01:00
|
|
|
switch (const Codepoint cp = *m_pos)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2018-06-04 18:00:59 +02:00
|
|
|
case '.':
|
|
|
|
++m_pos;
|
2021-11-24 22:56:52 +01:00
|
|
|
return add_node((m_flags & Flags::DotMatchesNewLine) ? ParsedRegex::AnyChar : ParsedRegex::AnyCharExceptNewLine);
|
2017-09-17 12:15:43 +02:00
|
|
|
case '(':
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2019-01-03 12:52:15 +01:00
|
|
|
uint32_t capture_group = -1;
|
|
|
|
const char* it = (++m_pos).base();
|
|
|
|
if (m_regex.end() - it < 2 or *it++ != '?')
|
|
|
|
capture_group = m_parsed_regex.capture_count++;
|
|
|
|
else if (*it == ':')
|
|
|
|
m_pos = Iterator{++it, m_regex};
|
|
|
|
else if (*it == '<')
|
|
|
|
{
|
|
|
|
const auto name_start = ++it;
|
|
|
|
while (it != m_regex.end() and is_word(*it))
|
|
|
|
++it;
|
|
|
|
if (it == m_regex.end() or *it != '>')
|
|
|
|
parse_error("named captures should be only ascii word characters");
|
|
|
|
capture_group = m_parsed_regex.capture_count++;
|
|
|
|
m_parsed_regex.named_captures.push_back({{name_start, it}, capture_group});
|
|
|
|
m_pos = Iterator{++it, m_regex};
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeIndex content = disjunction(capture_group);
|
2017-10-13 04:44:24 +02:00
|
|
|
if (at_end() or *m_pos++ != ')')
|
2017-09-26 15:38:56 +02:00
|
|
|
parse_error("unclosed parenthesis");
|
2017-09-17 12:15:43 +02:00
|
|
|
return content;
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-23 09:18:21 +02:00
|
|
|
case '\\':
|
2017-09-26 15:38:56 +02:00
|
|
|
++m_pos;
|
|
|
|
return atom_escape();
|
2017-09-23 09:18:21 +02:00
|
|
|
case '[':
|
2017-09-26 15:38:56 +02:00
|
|
|
++m_pos;
|
|
|
|
return character_class();
|
2017-09-26 16:42:54 +02:00
|
|
|
case '|': case ')':
|
2017-10-23 09:35:43 +02:00
|
|
|
return {};
|
2017-09-17 12:15:43 +02:00
|
|
|
default:
|
2021-12-10 23:01:03 +01:00
|
|
|
if (contains(StringView{"^$.*+?[]{}"}, cp) or (cp >= 0xF0000 and cp <= 0xFFFFF))
|
2017-09-26 16:42:54 +02:00
|
|
|
parse_error(format("unexpected '{}'", cp));
|
2017-09-26 15:38:56 +02:00
|
|
|
++m_pos;
|
2021-11-24 22:56:52 +01:00
|
|
|
return add_node(ParsedRegex::Literal, cp);
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2019-11-06 10:48:48 +01:00
|
|
|
Codepoint read_hex(size_t count)
|
|
|
|
{
|
|
|
|
Codepoint res = 0;
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
if (at_end())
|
|
|
|
parse_error("unterminated hex sequence");
|
|
|
|
Codepoint digit = *m_pos++;
|
|
|
|
Codepoint digit_value;
|
|
|
|
if ('0' <= digit and digit <= '9')
|
|
|
|
digit_value = digit - '0';
|
|
|
|
else if ('a' <= digit and digit <= 'f')
|
|
|
|
digit_value = 0xa + digit - 'a';
|
|
|
|
else if ('A' <= digit and digit <= 'F')
|
|
|
|
digit_value = 0xa + digit - 'A';
|
|
|
|
else
|
|
|
|
parse_error(format("invalid hex digit '{}'", digit));
|
|
|
|
|
|
|
|
res = res * 16 + digit_value;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-10-23 09:45:43 +02:00
|
|
|
NodeIndex atom_escape()
|
2017-09-23 09:18:21 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
const Codepoint cp = *m_pos++;
|
2017-09-23 09:18:21 +02:00
|
|
|
|
2017-09-26 16:51:05 +02:00
|
|
|
if (cp == 'Q')
|
|
|
|
{
|
2021-11-24 22:56:52 +01:00
|
|
|
auto escaped_sequence = add_node(ParsedRegex::Sequence);
|
2017-09-26 16:51:05 +02:00
|
|
|
constexpr StringView end_mark{"\\E"};
|
2017-10-23 09:35:43 +02:00
|
|
|
|
2017-09-26 16:51:05 +02:00
|
|
|
auto quote_end = std::search(m_pos.base(), m_regex.end(), end_mark.begin(), end_mark.end());
|
|
|
|
while (m_pos != quote_end)
|
2021-11-24 22:56:52 +01:00
|
|
|
add_node(ParsedRegex::Literal, *m_pos++);
|
2017-10-23 09:35:43 +02:00
|
|
|
get_node(escaped_sequence).children_end = m_parsed_regex.nodes.size();
|
|
|
|
|
2017-09-26 16:51:05 +02:00
|
|
|
if (quote_end != m_regex.end())
|
|
|
|
m_pos += 2;
|
|
|
|
|
|
|
|
return escaped_sequence;
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:04:41 +02:00
|
|
|
// CharacterClassEscape
|
2017-11-25 11:14:15 +01:00
|
|
|
auto class_it = find_if(character_class_escapes, [cp](auto& c) { return c.cp == cp; });
|
2017-10-06 13:51:41 +02:00
|
|
|
if (class_it != std::end(character_class_escapes))
|
2021-11-24 22:56:52 +01:00
|
|
|
return add_node(ParsedRegex::CharType, (Codepoint)class_it->ctype);
|
2017-09-26 15:04:41 +02:00
|
|
|
|
|
|
|
// CharacterEscape
|
2017-09-23 09:18:21 +02:00
|
|
|
for (auto& control : control_escapes)
|
|
|
|
{
|
2017-09-25 15:24:24 +02:00
|
|
|
if (control.name == cp)
|
2021-11-24 22:56:52 +01:00
|
|
|
return add_node(ParsedRegex::Literal, control.value);
|
2017-09-23 09:18:21 +02:00
|
|
|
}
|
|
|
|
|
2017-10-20 06:08:24 +02:00
|
|
|
if (cp == '0')
|
2021-11-24 22:56:52 +01:00
|
|
|
return add_node(ParsedRegex::Literal, '\0');
|
2017-10-20 06:08:24 +02:00
|
|
|
else if (cp == 'c')
|
|
|
|
{
|
|
|
|
if (at_end())
|
|
|
|
parse_error("unterminated control escape");
|
|
|
|
Codepoint ctrl = *m_pos++;
|
|
|
|
if (('a' <= ctrl and ctrl <= 'z') or ('A' <= ctrl and ctrl <= 'Z'))
|
2021-11-24 22:56:52 +01:00
|
|
|
return add_node(ParsedRegex::Literal, ctrl % 32);
|
2017-10-20 06:08:24 +02:00
|
|
|
parse_error(format("Invalid control escape character '{}'", ctrl));
|
|
|
|
}
|
|
|
|
else if (cp == 'x')
|
2021-11-24 22:56:52 +01:00
|
|
|
return add_node(ParsedRegex::Literal, read_hex(2));
|
2017-10-20 06:08:24 +02:00
|
|
|
else if (cp == 'u')
|
2021-11-24 22:56:52 +01:00
|
|
|
return add_node(ParsedRegex::Literal, read_hex(6));
|
2017-09-23 09:18:21 +02:00
|
|
|
|
2017-09-25 15:24:24 +02:00
|
|
|
if (contains("^$\\.*+?()[]{}|", cp)) // SyntaxCharacter
|
2021-11-24 22:56:52 +01:00
|
|
|
return add_node(ParsedRegex::Literal, cp);
|
2017-09-28 14:43:45 +02:00
|
|
|
parse_error(format("unknown atom escape '{}'", cp));
|
2017-09-23 09:18:21 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 21:49:26 +01:00
|
|
|
static void normalize_ranges(Vector<CharacterClass::Range, MemoryDomain::Regex>& ranges)
|
2017-10-14 16:10:56 +02:00
|
|
|
{
|
|
|
|
if (ranges.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Sort ranges so that we can use binary search
|
|
|
|
std::sort(ranges.begin(), ranges.end(),
|
|
|
|
[](auto& lhs, auto& rhs) { return lhs.min < rhs.min; });
|
|
|
|
|
2018-04-05 00:52:33 +02:00
|
|
|
// merge overlapping ranges
|
2017-10-14 16:10:56 +02:00
|
|
|
auto pos = ranges.begin();
|
|
|
|
for (auto next = pos+1; next != ranges.end(); ++next)
|
|
|
|
{
|
|
|
|
if (pos->max + 1 >= next->min)
|
|
|
|
{
|
|
|
|
if (next->max > pos->max)
|
|
|
|
pos->max = next->max;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*++pos = *next;
|
|
|
|
}
|
|
|
|
ranges.erase(pos+1, ranges.end());
|
|
|
|
}
|
|
|
|
|
2017-10-23 09:45:43 +02:00
|
|
|
NodeIndex character_class()
|
2017-09-23 09:18:21 +02:00
|
|
|
{
|
2017-11-25 11:14:15 +01:00
|
|
|
CharacterClass character_class;
|
|
|
|
|
2018-06-17 12:28:07 +02:00
|
|
|
character_class.ignore_case = (m_flags & Flags::IgnoreCase);
|
2017-11-25 11:14:15 +01:00
|
|
|
character_class.negative = m_pos != m_regex.end() and *m_pos == '^';
|
|
|
|
if (character_class.negative)
|
2017-09-26 15:38:56 +02:00
|
|
|
++m_pos;
|
2017-09-23 09:18:21 +02:00
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
while (m_pos != m_regex.end() and *m_pos != ']')
|
2017-09-23 09:18:21 +02:00
|
|
|
{
|
2017-10-02 07:49:08 +02:00
|
|
|
auto cp = *m_pos++;
|
2017-09-25 15:24:24 +02:00
|
|
|
if (cp == '-')
|
2017-09-23 09:18:21 +02:00
|
|
|
{
|
2017-11-25 11:14:15 +01:00
|
|
|
character_class.ranges.push_back({ '-', '-' });
|
2017-09-23 09:18:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
if (at_end())
|
2017-09-23 09:18:21 +02:00
|
|
|
break;
|
|
|
|
|
2019-11-06 10:48:48 +01:00
|
|
|
auto read_escaped_char = [this]() {
|
|
|
|
Codepoint cp = *m_pos++;
|
|
|
|
auto it = find_if(control_escapes, [cp](auto&& t) { return t.name == cp; });
|
|
|
|
if (it != std::end(control_escapes))
|
|
|
|
return it->value;
|
|
|
|
if (cp == 'x')
|
|
|
|
return read_hex(2);
|
|
|
|
if (cp == 'u')
|
|
|
|
return read_hex(6);
|
|
|
|
if (not contains("^$\\.*+?()[]{}|-", cp)) // SyntaxCharacter and -
|
|
|
|
parse_error(format("unknown character class escape '{}'", cp));
|
|
|
|
return cp;
|
|
|
|
};
|
|
|
|
|
2017-09-26 15:04:41 +02:00
|
|
|
if (cp == '\\')
|
|
|
|
{
|
|
|
|
auto it = find_if(character_class_escapes,
|
2018-03-19 18:57:47 +01:00
|
|
|
[cp = *m_pos](auto&& t) { return t.cp == cp; });
|
2017-09-26 15:04:41 +02:00
|
|
|
if (it != std::end(character_class_escapes))
|
|
|
|
{
|
2017-11-25 11:14:15 +01:00
|
|
|
character_class.ctypes |= it->ctype;
|
2017-09-26 15:38:56 +02:00
|
|
|
++m_pos;
|
2017-09-26 15:04:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-03-19 18:57:47 +01:00
|
|
|
else // its an escaped character
|
2019-11-06 10:48:48 +01:00
|
|
|
cp = read_escaped_char();
|
2017-09-26 15:04:41 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 11:14:15 +01:00
|
|
|
CharacterClass::Range range = { cp, cp };
|
2017-09-26 15:38:56 +02:00
|
|
|
if (*m_pos == '-')
|
2017-09-23 09:18:21 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
if (++m_pos == m_regex.end())
|
2017-09-23 09:18:21 +02:00
|
|
|
break;
|
2017-09-26 16:37:21 +02:00
|
|
|
if (*m_pos != ']')
|
|
|
|
{
|
2019-11-06 10:48:48 +01:00
|
|
|
cp = *m_pos++;
|
|
|
|
if (cp == '\\')
|
|
|
|
cp = read_escaped_char();
|
|
|
|
range.max = cp;
|
2017-09-26 16:37:21 +02:00
|
|
|
if (range.min > range.max)
|
|
|
|
parse_error("invalid range specified");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-25 11:14:15 +01:00
|
|
|
character_class.ranges.push_back(range);
|
2017-09-26 16:37:21 +02:00
|
|
|
range = { '-', '-' };
|
|
|
|
}
|
2017-09-23 09:18:21 +02:00
|
|
|
}
|
2017-11-25 11:14:15 +01:00
|
|
|
character_class.ranges.push_back(range);
|
2017-09-23 09:18:21 +02:00
|
|
|
}
|
2017-09-26 15:38:56 +02:00
|
|
|
if (at_end())
|
|
|
|
parse_error("unclosed character class");
|
|
|
|
++m_pos;
|
2017-09-23 09:18:21 +02:00
|
|
|
|
2018-06-17 12:28:07 +02:00
|
|
|
if (character_class.ignore_case)
|
2017-09-29 05:22:09 +02:00
|
|
|
{
|
2017-11-25 11:14:15 +01:00
|
|
|
for (auto& range : character_class.ranges)
|
2017-09-29 05:22:09 +02:00
|
|
|
{
|
2017-11-12 17:27:41 +01:00
|
|
|
range.min = to_lower(range.min);
|
2017-09-29 05:22:09 +02:00
|
|
|
range.max = to_lower(range.max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 11:14:15 +01:00
|
|
|
normalize_ranges(character_class.ranges);
|
2017-10-14 16:10:56 +02:00
|
|
|
|
2017-10-05 03:36:40 +02:00
|
|
|
// Optimize the relatively common case of using a character class to
|
|
|
|
// escape a character, such as [*]
|
2017-11-25 11:14:15 +01:00
|
|
|
if (character_class.ctypes == CharacterType::None and not character_class.negative and
|
|
|
|
character_class.ranges.size() == 1 and
|
|
|
|
character_class.ranges.front().min == character_class.ranges.front().max)
|
2021-11-24 22:56:52 +01:00
|
|
|
return add_node(ParsedRegex::Literal, character_class.ranges.front().min);
|
2017-11-25 11:14:15 +01:00
|
|
|
|
|
|
|
if (character_class.ctypes != CharacterType::None and not character_class.negative and
|
|
|
|
character_class.ranges.empty())
|
2021-11-24 22:56:52 +01:00
|
|
|
return add_node(ParsedRegex::CharType, (Codepoint)character_class.ctypes);
|
2017-09-26 11:03:12 +02:00
|
|
|
|
2022-08-05 12:29:43 +02:00
|
|
|
auto it = std::find(m_parsed_regex.character_classes.begin(), m_parsed_regex.character_classes.end(), character_class);
|
|
|
|
auto class_id = it - m_parsed_regex.character_classes.begin();
|
|
|
|
if (it == m_parsed_regex.character_classes.end())
|
|
|
|
m_parsed_regex.character_classes.push_back(std::move(character_class));
|
2017-09-23 09:18:21 +02:00
|
|
|
|
2021-11-24 22:56:52 +01:00
|
|
|
return add_node(ParsedRegex::CharClass, class_id);
|
2017-09-23 09:18:21 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
ParsedRegex::Quantifier quantifier()
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-09-26 15:38:56 +02:00
|
|
|
if (at_end())
|
2017-09-26 16:04:47 +02:00
|
|
|
return {ParsedRegex::Quantifier::One};
|
2017-09-25 15:24:24 +02:00
|
|
|
|
2017-10-11 13:28:22 +02:00
|
|
|
constexpr int max_repeat = 1000;
|
2017-11-27 18:03:54 +01:00
|
|
|
auto read_bound = [&]() {
|
2017-10-23 09:51:24 +02:00
|
|
|
int16_t res = 0;
|
2017-11-27 18:03:54 +01:00
|
|
|
for (auto begin = m_pos; m_pos != m_regex.end(); ++m_pos)
|
2017-09-18 11:22:11 +02:00
|
|
|
{
|
2017-11-27 18:03:54 +01:00
|
|
|
const auto cp = *m_pos;
|
2017-09-25 15:24:24 +02:00
|
|
|
if (cp < '0' or cp > '9')
|
2017-11-27 18:03:54 +01:00
|
|
|
return m_pos == begin ? (int16_t)-1 : res;
|
2017-09-25 15:24:24 +02:00
|
|
|
res = res * 10 + cp - '0';
|
2017-10-11 13:28:22 +02:00
|
|
|
if (res > max_repeat)
|
|
|
|
parse_error(format("Explicit quantifier is too big, maximum is {}", max_repeat));
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2017-09-28 11:50:04 +02:00
|
|
|
auto check_greedy = [&]() {
|
|
|
|
if (at_end() or *m_pos != '?')
|
|
|
|
return true;
|
|
|
|
++m_pos;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
switch (*m_pos)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-28 11:50:04 +02:00
|
|
|
case '*': ++m_pos; return {ParsedRegex::Quantifier::RepeatZeroOrMore, check_greedy()};
|
|
|
|
case '+': ++m_pos; return {ParsedRegex::Quantifier::RepeatOneOrMore, check_greedy()};
|
|
|
|
case '?': ++m_pos; return {ParsedRegex::Quantifier::Optional, check_greedy()};
|
2017-09-18 11:22:11 +02:00
|
|
|
case '{':
|
|
|
|
{
|
2017-11-27 18:03:54 +01:00
|
|
|
++m_pos;
|
|
|
|
const int16_t min = read_bound();
|
2017-10-23 09:51:24 +02:00
|
|
|
int16_t max = min;
|
2017-11-27 18:03:54 +01:00
|
|
|
if (*m_pos == ',')
|
2017-09-18 11:22:11 +02:00
|
|
|
{
|
2017-11-27 18:03:54 +01:00
|
|
|
++m_pos;
|
|
|
|
max = read_bound();
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
2017-11-27 18:03:54 +01:00
|
|
|
if (*m_pos++ != '}')
|
2017-09-26 15:38:56 +02:00
|
|
|
parse_error("expected closing bracket");
|
2017-10-02 09:35:21 +02:00
|
|
|
return {ParsedRegex::Quantifier::RepeatMinMax, check_greedy(), min, max};
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
2017-09-26 16:04:47 +02:00
|
|
|
default: return {ParsedRegex::Quantifier::One};
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-26 15:04:41 +02:00
|
|
|
|
2021-11-24 22:56:52 +01:00
|
|
|
NodeIndex add_node(ParsedRegex::Op op, Codepoint value = -1, ParsedRegex::Quantifier quantifier = {ParsedRegex::Quantifier::One})
|
2017-09-26 16:04:47 +02:00
|
|
|
{
|
2018-04-27 00:18:04 +02:00
|
|
|
constexpr auto max_nodes = std::numeric_limits<int16_t>::max();
|
2017-10-23 09:45:43 +02:00
|
|
|
const NodeIndex res = m_parsed_regex.nodes.size();
|
2017-10-23 09:35:43 +02:00
|
|
|
if (res == max_nodes)
|
|
|
|
parse_error(format("regex parsed to more than {} ast nodes", max_nodes));
|
2017-10-23 09:45:43 +02:00
|
|
|
const NodeIndex next = res+1;
|
2018-06-17 12:28:07 +02:00
|
|
|
m_parsed_regex.nodes.push_back({op, m_flags & Flags::IgnoreCase, next, value, quantifier});
|
2017-10-23 09:35:43 +02:00
|
|
|
return res;
|
2017-09-26 16:04:47 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 09:45:43 +02:00
|
|
|
ParsedRegex::Node& get_node(NodeIndex index)
|
2017-10-23 09:35:43 +02:00
|
|
|
{
|
|
|
|
return m_parsed_regex.nodes[index];
|
|
|
|
}
|
|
|
|
|
2021-11-24 22:56:52 +01:00
|
|
|
bool at_end() const { return m_pos == m_regex.end(); }
|
2017-10-23 09:35:43 +02:00
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
[[gnu::noreturn]]
|
2017-10-13 04:44:24 +02:00
|
|
|
void parse_error(StringView error) const
|
2017-09-26 15:38:56 +02:00
|
|
|
{
|
2017-10-09 08:04:14 +02:00
|
|
|
throw regex_error(format("regex parse error: {} at '{}<<<HERE>>>{}'", error,
|
|
|
|
StringView{m_regex.begin(), m_pos.base()},
|
|
|
|
StringView{m_pos.base(), m_regex.end()}));
|
2017-09-26 15:38:56 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 09:45:43 +02:00
|
|
|
void validate_lookaround(NodeIndex index)
|
2017-09-28 14:43:45 +02:00
|
|
|
{
|
2018-10-10 13:35:38 +02:00
|
|
|
using Lookaround = CompiledRegex::Lookaround;
|
2018-12-19 13:21:47 +01:00
|
|
|
for (auto child_index : Children<>{m_parsed_regex, index})
|
|
|
|
{
|
2017-10-23 09:35:43 +02:00
|
|
|
auto& child = get_node(child_index);
|
2020-07-25 06:07:57 +02:00
|
|
|
if (child.op != ParsedRegex::Literal and child.op != ParsedRegex::CharClass and
|
|
|
|
child.op != ParsedRegex::CharType and child.op != ParsedRegex::AnyChar and
|
2018-06-24 12:13:35 +02:00
|
|
|
child.op != ParsedRegex::AnyCharExceptNewLine)
|
2017-10-09 05:12:42 +02:00
|
|
|
parse_error("Lookaround can only contain literals, any chars or character classes");
|
2018-10-10 13:35:38 +02:00
|
|
|
if (child.op == ParsedRegex::Literal and
|
|
|
|
to_underlying(Lookaround::OpBegin) <= child.value and
|
|
|
|
child.value < to_underlying(Lookaround::OpEnd))
|
|
|
|
parse_error("Lookaround does not support literals codepoint between 0xF0000 and 0xFFFFD");
|
2017-10-23 09:35:43 +02:00
|
|
|
if (child.quantifier.type != ParsedRegex::Quantifier::One)
|
2017-10-09 05:12:42 +02:00
|
|
|
parse_error("Quantifiers cannot be used in lookarounds");
|
2018-12-19 13:21:47 +01:00
|
|
|
}
|
2017-09-28 14:43:45 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 15:38:56 +02:00
|
|
|
ParsedRegex m_parsed_regex;
|
|
|
|
StringView m_regex;
|
|
|
|
Iterator m_pos;
|
2018-06-17 12:28:07 +02:00
|
|
|
|
|
|
|
Flags m_flags = Flags::DotMatchesNewLine;
|
2017-09-26 15:38:56 +02:00
|
|
|
|
2017-10-09 12:38:34 +02:00
|
|
|
static constexpr struct CharacterClassEscape {
|
2017-09-26 15:04:41 +02:00
|
|
|
Codepoint cp;
|
2017-11-25 11:14:15 +01:00
|
|
|
CharacterType ctype;
|
2017-10-09 12:38:34 +02:00
|
|
|
} character_class_escapes[] = {
|
2017-11-25 11:14:15 +01:00
|
|
|
{ 'd', CharacterType::Digit }, { 'D', CharacterType::NotDigit },
|
|
|
|
{ 'w', CharacterType::Word }, { 'W', CharacterType::NotWord },
|
|
|
|
{ 's', CharacterType::Whitespace }, { 'S', CharacterType::NotWhitespace },
|
|
|
|
{ 'h', CharacterType::HorizontalWhitespace }, { 'H', CharacterType::NotHorizontalWhitespace },
|
2017-09-26 15:04:41 +02:00
|
|
|
};
|
2017-10-07 15:24:05 +02:00
|
|
|
|
2017-10-09 12:38:34 +02:00
|
|
|
static constexpr struct ControlEscape {
|
|
|
|
Codepoint name;
|
|
|
|
Codepoint value;
|
|
|
|
} control_escapes[] = {
|
|
|
|
{ 'f', '\f' },
|
|
|
|
{ 'n', '\n' },
|
|
|
|
{ 'r', '\r' },
|
|
|
|
{ 't', '\t' },
|
|
|
|
{ 'v', '\v' }
|
|
|
|
};
|
2017-09-17 12:15:43 +02:00
|
|
|
};
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-10-09 12:38:34 +02:00
|
|
|
constexpr RegexParser::CharacterClassEscape RegexParser::character_class_escapes[];
|
|
|
|
constexpr RegexParser::ControlEscape RegexParser::control_escapes[];
|
2017-10-07 15:24:05 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
struct RegexCompiler
|
2017-09-25 15:24:24 +02:00
|
|
|
{
|
2020-07-25 06:07:57 +02:00
|
|
|
using OpIndex = int16_t;
|
|
|
|
|
2017-12-01 12:57:02 +01:00
|
|
|
RegexCompiler(ParsedRegex&& parsed_regex, RegexCompileFlags flags)
|
2019-02-09 05:41:09 +01:00
|
|
|
: m_flags(flags), m_parsed_regex{parsed_regex}
|
2017-09-26 16:04:47 +02:00
|
|
|
{
|
2017-12-01 12:57:02 +01:00
|
|
|
kak_assert(not (flags & RegexCompileFlags::NoForward) or flags & RegexCompileFlags::Backward);
|
2017-11-27 17:59:57 +01:00
|
|
|
// Approximation of the number of instructions generated
|
2019-01-05 08:28:56 +01:00
|
|
|
m_program.instructions.reserve((parsed_regex.nodes.size() + 1)
|
2017-12-01 12:57:02 +01:00
|
|
|
* (((flags & RegexCompileFlags::Backward) and
|
|
|
|
not (flags & RegexCompileFlags::NoForward)) ? 2 : 1));
|
|
|
|
|
|
|
|
if (not (flags & RegexCompileFlags::NoForward))
|
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
m_program.forward_start_desc = compute_start_desc<RegexMode::Forward>();
|
|
|
|
compile_node<RegexMode::Forward>(0);
|
2019-12-05 11:10:14 +01:00
|
|
|
optimize(0, m_program.instructions.size());
|
2017-12-01 12:57:02 +01:00
|
|
|
push_inst(CompiledRegex::Match);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & RegexCompileFlags::Backward)
|
|
|
|
{
|
|
|
|
m_program.first_backward_inst = m_program.instructions.size();
|
2018-12-27 01:29:33 +01:00
|
|
|
m_program.backward_start_desc = compute_start_desc<RegexMode::Backward>();
|
|
|
|
compile_node<RegexMode::Backward>(0);
|
2019-12-05 11:10:14 +01:00
|
|
|
optimize(m_program.first_backward_inst, m_program.instructions.size());
|
2017-12-01 12:57:02 +01:00
|
|
|
push_inst(CompiledRegex::Match);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_program.first_backward_inst = -1;
|
2017-11-27 17:59:57 +01:00
|
|
|
|
|
|
|
m_program.character_classes = std::move(m_parsed_regex.character_classes);
|
2019-01-03 12:52:15 +01:00
|
|
|
m_program.named_captures = std::move(m_parsed_regex.named_captures);
|
2017-09-26 16:04:47 +02:00
|
|
|
m_program.save_count = m_parsed_regex.capture_count * 2;
|
|
|
|
}
|
2017-09-25 15:24:24 +02:00
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
CompiledRegex get_compiled_regex() { return std::move(m_program); }
|
2017-09-23 09:18:21 +02:00
|
|
|
|
2017-10-07 04:57:24 +02:00
|
|
|
private:
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2018-12-27 01:29:33 +01:00
|
|
|
template<RegexMode direction>
|
2020-07-25 06:07:57 +02:00
|
|
|
OpIndex compile_node_inner(ParsedRegex::NodeIndex index)
|
2017-09-26 16:04:47 +02:00
|
|
|
{
|
2017-10-23 09:35:43 +02:00
|
|
|
auto& node = get_node(index);
|
|
|
|
|
|
|
|
const uint32_t start_pos = (uint32_t)m_program.instructions.size();
|
|
|
|
const bool ignore_case = node.ignore_case;
|
2017-09-26 16:04:47 +02:00
|
|
|
|
2017-10-23 09:35:43 +02:00
|
|
|
const bool save = (node.op == ParsedRegex::Alternation or node.op == ParsedRegex::Sequence) and
|
|
|
|
(node.value == 0 or (node.value != -1 and not (m_flags & RegexCompileFlags::NoSubs)));
|
2018-12-27 01:29:33 +01:00
|
|
|
constexpr bool forward = direction == RegexMode::Forward;
|
2017-10-21 04:30:35 +02:00
|
|
|
if (save)
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::Save, {.save_index = int16_t(node.value * 2 + (forward ? 0 : 1))});
|
2017-09-17 12:15:43 +02:00
|
|
|
|
2017-10-07 12:51:32 +02:00
|
|
|
Vector<uint32_t> goto_inner_end_offsets;
|
2017-10-23 09:35:43 +02:00
|
|
|
switch (node.op)
|
2017-09-26 16:04:47 +02:00
|
|
|
{
|
|
|
|
case ParsedRegex::Literal:
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::Literal, {.literal={.codepoint=ignore_case ? to_lower(node.value) : node.value, .ignore_case=ignore_case}});
|
2017-09-26 16:04:47 +02:00
|
|
|
break;
|
|
|
|
case ParsedRegex::AnyChar:
|
2017-10-07 12:51:32 +02:00
|
|
|
push_inst(CompiledRegex::AnyChar);
|
2017-09-26 16:04:47 +02:00
|
|
|
break;
|
2018-06-24 12:13:35 +02:00
|
|
|
case ParsedRegex::AnyCharExceptNewLine:
|
|
|
|
push_inst(CompiledRegex::AnyCharExceptNewLine);
|
|
|
|
break;
|
2020-07-25 06:07:57 +02:00
|
|
|
case ParsedRegex::CharClass:
|
|
|
|
push_inst(CompiledRegex::CharClass, {.character_class_index=int16_t(node.value)});
|
2017-11-25 11:14:15 +01:00
|
|
|
break;
|
2020-07-25 06:07:57 +02:00
|
|
|
case ParsedRegex::CharType:
|
|
|
|
push_inst(CompiledRegex::CharType, {.character_type=CharacterType{(unsigned char)node.value}});
|
2017-10-07 12:51:32 +02:00
|
|
|
break;
|
2017-09-26 16:04:47 +02:00
|
|
|
case ParsedRegex::Sequence:
|
2017-10-07 06:46:27 +02:00
|
|
|
{
|
2018-12-19 13:21:47 +01:00
|
|
|
for (auto child : Children<direction>{m_parsed_regex, index})
|
|
|
|
compile_node<direction>(child);
|
2017-09-26 16:04:47 +02:00
|
|
|
break;
|
2017-10-07 06:46:27 +02:00
|
|
|
}
|
2017-09-26 16:04:47 +02:00
|
|
|
case ParsedRegex::Alternation:
|
|
|
|
{
|
2018-12-19 13:21:47 +01:00
|
|
|
for (auto child : Children<>{m_parsed_regex, index})
|
|
|
|
{
|
2017-10-23 09:35:43 +02:00
|
|
|
if (child != index+1)
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::Split);
|
2018-12-19 13:21:47 +01:00
|
|
|
}
|
2022-02-02 04:51:04 +01:00
|
|
|
auto split_pos = m_program.instructions.size();
|
2017-09-17 13:08:10 +02:00
|
|
|
|
2018-12-19 13:21:47 +01:00
|
|
|
const auto end = node.children_end;
|
|
|
|
for (auto child : Children<>{m_parsed_regex, index})
|
|
|
|
{
|
2018-02-09 12:35:12 +01:00
|
|
|
auto node = compile_node<direction>(child);
|
2017-10-23 09:35:43 +02:00
|
|
|
if (child != index+1)
|
2022-02-02 04:51:04 +01:00
|
|
|
m_program.instructions[--split_pos].param.split = CompiledRegex::Param::Split{.target = node, .prioritize_parent = true};
|
2017-10-23 09:35:43 +02:00
|
|
|
if (get_node(child).children_end != end)
|
2017-10-09 15:04:28 +02:00
|
|
|
{
|
|
|
|
auto jump = push_inst(CompiledRegex::Jump);
|
|
|
|
goto_inner_end_offsets.push_back(jump);
|
|
|
|
}
|
2018-12-19 13:21:47 +01:00
|
|
|
}
|
2017-09-26 16:04:47 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-09-28 14:43:45 +02:00
|
|
|
case ParsedRegex::LookAhead:
|
|
|
|
case ParsedRegex::NegativeLookAhead:
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::LookAround, {.lookaround={
|
|
|
|
.index=push_lookaround<RegexMode::Forward>(index, ignore_case),
|
|
|
|
.ahead=true,
|
|
|
|
.positive=node.op == ParsedRegex::LookAhead,
|
|
|
|
.ignore_case=ignore_case}});
|
2017-09-28 14:43:45 +02:00
|
|
|
break;
|
2017-10-04 17:00:19 +02:00
|
|
|
case ParsedRegex::LookBehind:
|
2017-09-28 14:43:45 +02:00
|
|
|
case ParsedRegex::NegativeLookBehind:
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::LookAround, {.lookaround={
|
|
|
|
.index=push_lookaround<RegexMode::Backward>(index, ignore_case),
|
|
|
|
.ahead=false,
|
|
|
|
.positive=node.op == ParsedRegex::LookBehind,
|
|
|
|
.ignore_case=ignore_case}});
|
2017-09-28 14:43:45 +02:00
|
|
|
break;
|
2017-09-26 16:04:47 +02:00
|
|
|
case ParsedRegex::LineStart:
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::LineAssertion, {.line_start=true});
|
2017-09-26 16:04:47 +02:00
|
|
|
break;
|
|
|
|
case ParsedRegex::LineEnd:
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::LineAssertion, {.line_start=false});
|
2017-09-26 16:04:47 +02:00
|
|
|
break;
|
|
|
|
case ParsedRegex::WordBoundary:
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::WordBoundary, {.word_boundary_positive=true});
|
2017-09-26 16:04:47 +02:00
|
|
|
break;
|
|
|
|
case ParsedRegex::NotWordBoundary:
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::WordBoundary, {.word_boundary_positive=false});
|
2017-09-26 16:04:47 +02:00
|
|
|
break;
|
|
|
|
case ParsedRegex::SubjectBegin:
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::SubjectAssertion, {.subject_begin=true});
|
2017-09-26 16:04:47 +02:00
|
|
|
break;
|
|
|
|
case ParsedRegex::SubjectEnd:
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::SubjectAssertion, {.subject_begin=false});
|
2017-09-26 16:04:47 +02:00
|
|
|
break;
|
2017-09-26 17:01:06 +02:00
|
|
|
case ParsedRegex::ResetStart:
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::Save, {.save_index=0});
|
2017-09-26 17:01:06 +02:00
|
|
|
break;
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
for (auto& offset : goto_inner_end_offsets)
|
2020-07-25 06:07:57 +02:00
|
|
|
m_program.instructions[offset].param.jump_target = m_program.instructions.size();
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2017-10-21 04:30:35 +02:00
|
|
|
if (save)
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::Save, {.save_index=int16_t(node.value * 2 + (forward ? 1 : 0))});
|
2017-09-26 16:04:47 +02:00
|
|
|
|
|
|
|
return start_pos;
|
2017-09-19 09:56:21 +02:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:29:33 +01:00
|
|
|
template<RegexMode direction>
|
2020-07-25 06:07:57 +02:00
|
|
|
OpIndex compile_node(ParsedRegex::NodeIndex index)
|
2017-09-26 16:04:47 +02:00
|
|
|
{
|
2017-10-23 09:35:43 +02:00
|
|
|
auto& node = get_node(index);
|
|
|
|
|
2020-07-25 06:07:57 +02:00
|
|
|
const OpIndex start_pos = (OpIndex)m_program.instructions.size();
|
|
|
|
Vector<OpIndex> goto_ends;
|
2017-09-18 11:22:11 +02:00
|
|
|
|
2017-10-23 09:35:43 +02:00
|
|
|
auto& quantifier = node.quantifier;
|
2017-09-28 11:50:04 +02:00
|
|
|
|
|
|
|
if (quantifier.allows_none())
|
2017-09-26 16:04:47 +02:00
|
|
|
{
|
2020-07-25 06:07:57 +02:00
|
|
|
auto split_pos = push_inst(CompiledRegex::Split, {.split={.target=0, .prioritize_parent=quantifier.greedy}});
|
2017-10-07 12:51:32 +02:00
|
|
|
goto_ends.push_back(split_pos);
|
2017-09-26 16:04:47 +02:00
|
|
|
}
|
2017-09-18 11:22:11 +02:00
|
|
|
|
2018-02-09 12:35:12 +01:00
|
|
|
auto inner_pos = compile_node_inner<direction>(index);
|
2017-09-26 16:04:47 +02:00
|
|
|
// Write the node multiple times when we have a min count quantifier
|
2017-09-28 11:50:04 +02:00
|
|
|
for (int i = 1; i < quantifier.min; ++i)
|
2018-02-09 12:35:12 +01:00
|
|
|
inner_pos = compile_node_inner<direction>(index);
|
2017-09-18 11:22:11 +02:00
|
|
|
|
2017-09-28 11:50:04 +02:00
|
|
|
if (quantifier.allows_infinite_repeat())
|
2020-07-25 06:07:57 +02:00
|
|
|
push_inst(CompiledRegex::Split, {.split = {.target=inner_pos, .prioritize_parent=not quantifier.greedy}});
|
2017-09-26 16:04:47 +02:00
|
|
|
// Write the node as an optional match for the min -> max counts
|
2017-10-23 09:51:24 +02:00
|
|
|
else for (int i = std::max((int16_t)1, quantifier.min); // STILL UGLY !
|
2017-09-28 11:50:04 +02:00
|
|
|
i < quantifier.max; ++i)
|
2017-09-26 16:04:47 +02:00
|
|
|
{
|
2020-07-25 06:07:57 +02:00
|
|
|
auto split_pos = push_inst(CompiledRegex::Split, {.split={.target=0, .prioritize_parent=quantifier.greedy}});
|
2017-10-07 12:51:32 +02:00
|
|
|
goto_ends.push_back(split_pos);
|
2018-02-09 12:35:12 +01:00
|
|
|
compile_node_inner<direction>(index);
|
2017-09-26 16:04:47 +02:00
|
|
|
}
|
2017-09-18 11:22:11 +02:00
|
|
|
|
2017-10-07 12:51:32 +02:00
|
|
|
for (auto offset : goto_ends)
|
2020-07-25 06:07:57 +02:00
|
|
|
m_program.instructions[offset].param.split.target = m_program.instructions.size();
|
2017-09-26 16:04:47 +02:00
|
|
|
|
2017-10-23 09:35:43 +02:00
|
|
|
return start_pos;
|
2017-09-26 16:04:47 +02:00
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2020-07-25 06:07:57 +02:00
|
|
|
OpIndex push_inst(CompiledRegex::Op op, CompiledRegex::Param param = {})
|
2017-09-26 16:04:47 +02:00
|
|
|
{
|
2020-07-25 06:07:57 +02:00
|
|
|
constexpr auto max_instructions = std::numeric_limits<OpIndex>::max();
|
|
|
|
const auto res = m_program.instructions.size();
|
|
|
|
if (res >= max_instructions)
|
2017-10-16 13:36:06 +02:00
|
|
|
throw regex_error(format("regex compiled to more than {} instructions", max_instructions));
|
2023-02-13 11:15:55 +01:00
|
|
|
m_program.instructions.push_back({ op, 0, param });
|
2020-07-25 06:07:57 +02:00
|
|
|
return OpIndex(res);
|
2017-09-26 16:04:47 +02:00
|
|
|
}
|
2017-09-19 10:44:58 +02:00
|
|
|
|
2018-12-27 01:29:33 +01:00
|
|
|
template<RegexMode direction>
|
2020-07-25 06:07:57 +02:00
|
|
|
int16_t push_lookaround(ParsedRegex::NodeIndex index, bool ignore_case)
|
2017-09-28 14:43:45 +02:00
|
|
|
{
|
2018-10-10 13:35:38 +02:00
|
|
|
using Lookaround = CompiledRegex::Lookaround;
|
|
|
|
|
2020-07-25 06:07:57 +02:00
|
|
|
const int16_t res = m_program.lookarounds.size();
|
2018-12-19 13:21:47 +01:00
|
|
|
for (auto child : Children<direction>{m_parsed_regex, index})
|
|
|
|
{
|
|
|
|
auto& character = get_node(child);
|
|
|
|
if (character.op == ParsedRegex::Literal)
|
|
|
|
m_program.lookarounds.push_back(
|
|
|
|
static_cast<Lookaround>(ignore_case ? to_lower(character.value) : character.value));
|
|
|
|
else if (character.op == ParsedRegex::AnyChar)
|
|
|
|
m_program.lookarounds.push_back(Lookaround::AnyChar);
|
|
|
|
else if (character.op == ParsedRegex::AnyCharExceptNewLine)
|
|
|
|
m_program.lookarounds.push_back(Lookaround::AnyCharExceptNewLine);
|
2020-07-25 06:07:57 +02:00
|
|
|
else if (character.op == ParsedRegex::CharClass)
|
2018-12-19 13:21:47 +01:00
|
|
|
m_program.lookarounds.push_back(static_cast<Lookaround>(to_underlying(Lookaround::CharacterClass) + character.value));
|
2020-07-25 06:07:57 +02:00
|
|
|
else if (character.op == ParsedRegex::CharType)
|
2018-12-19 13:21:47 +01:00
|
|
|
m_program.lookarounds.push_back(static_cast<Lookaround>(to_underlying(Lookaround::CharacterType) | character.value));
|
|
|
|
else
|
|
|
|
kak_assert(false);
|
|
|
|
}
|
2018-10-10 13:35:38 +02:00
|
|
|
m_program.lookarounds.push_back(Lookaround::EndOfLookaround);
|
2017-10-07 12:51:32 +02:00
|
|
|
return res;
|
2017-09-28 14:43:45 +02:00
|
|
|
}
|
|
|
|
|
2018-02-24 07:21:15 +01:00
|
|
|
// Mutate start_desc with informations on which Codepoint could start a match.
|
2018-07-08 08:58:19 +02:00
|
|
|
// Returns true if the node possibly does not consume the char, in which case
|
|
|
|
// the next node would still be relevant for the parent node start chars computation.
|
2018-12-27 01:29:33 +01:00
|
|
|
template<RegexMode direction>
|
2017-12-01 07:46:18 +01:00
|
|
|
bool compute_start_desc(ParsedRegex::NodeIndex index,
|
2018-02-09 12:35:12 +01:00
|
|
|
CompiledRegex::StartDesc& start_desc) const
|
2017-10-06 07:40:27 +02:00
|
|
|
{
|
2017-10-23 09:35:43 +02:00
|
|
|
auto& node = get_node(index);
|
|
|
|
switch (node.op)
|
2017-10-06 07:40:27 +02:00
|
|
|
{
|
|
|
|
case ParsedRegex::Literal:
|
2017-12-01 07:46:18 +01:00
|
|
|
if (node.value < CompiledRegex::StartDesc::count)
|
2017-10-13 04:58:09 +02:00
|
|
|
{
|
2017-10-23 09:35:43 +02:00
|
|
|
if (node.ignore_case)
|
2017-10-13 04:58:09 +02:00
|
|
|
{
|
2017-12-01 07:46:18 +01:00
|
|
|
start_desc.map[to_lower(node.value)] = true;
|
|
|
|
start_desc.map[to_upper(node.value)] = true;
|
2017-10-13 04:58:09 +02:00
|
|
|
}
|
|
|
|
else
|
2017-12-01 07:46:18 +01:00
|
|
|
start_desc.map[node.value] = true;
|
2017-10-13 04:58:09 +02:00
|
|
|
}
|
2017-10-20 12:26:33 +02:00
|
|
|
else
|
2017-12-01 07:46:18 +01:00
|
|
|
start_desc.map[CompiledRegex::StartDesc::other] = true;
|
2017-10-23 09:35:43 +02:00
|
|
|
return node.quantifier.allows_none();
|
2017-10-06 07:40:27 +02:00
|
|
|
case ParsedRegex::AnyChar:
|
2017-12-01 07:46:18 +01:00
|
|
|
for (auto& b : start_desc.map)
|
2017-10-06 07:40:27 +02:00
|
|
|
b = true;
|
2017-11-27 18:03:54 +01:00
|
|
|
return node.quantifier.allows_none();
|
2018-06-24 12:13:35 +02:00
|
|
|
case ParsedRegex::AnyCharExceptNewLine:
|
|
|
|
for (Codepoint cp = 0; cp < CompiledRegex::StartDesc::count; ++cp)
|
|
|
|
{
|
|
|
|
if (cp != '\n')
|
|
|
|
start_desc.map[cp] = true;
|
|
|
|
}
|
|
|
|
return node.quantifier.allows_none();
|
2020-07-25 06:07:57 +02:00
|
|
|
case ParsedRegex::CharClass:
|
2017-11-25 11:14:15 +01:00
|
|
|
{
|
|
|
|
auto& character_class = m_parsed_regex.character_classes[node.value];
|
2020-02-06 21:37:29 +01:00
|
|
|
if (character_class.ctypes == CharacterType::None and
|
|
|
|
not character_class.negative and
|
|
|
|
not character_class.ignore_case)
|
2017-11-25 11:14:15 +01:00
|
|
|
{
|
2017-11-30 07:19:41 +01:00
|
|
|
for (auto& range : character_class.ranges)
|
|
|
|
{
|
2018-04-29 23:41:02 +02:00
|
|
|
const auto clamp = [](Codepoint cp) { return std::min(CompiledRegex::StartDesc::count, cp); };
|
2018-04-29 08:42:46 +02:00
|
|
|
for (auto cp = clamp(range.min), end = clamp(range.max + 1); cp < end; ++cp)
|
2017-12-01 07:46:18 +01:00
|
|
|
start_desc.map[cp] = true;
|
2018-04-29 08:42:46 +02:00
|
|
|
if (range.max >= CompiledRegex::StartDesc::count)
|
|
|
|
start_desc.map[CompiledRegex::StartDesc::other] = true;
|
2017-11-30 07:19:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-29 08:42:46 +02:00
|
|
|
for (Codepoint cp = 0; cp < CompiledRegex::StartDesc::count; ++cp)
|
2017-11-30 07:19:41 +01:00
|
|
|
{
|
2017-12-01 07:46:18 +01:00
|
|
|
if (start_desc.map[cp] or is_character_class(character_class, cp))
|
|
|
|
start_desc.map[cp] = true;
|
2017-11-30 07:19:41 +01:00
|
|
|
}
|
2017-11-25 11:14:15 +01:00
|
|
|
}
|
2017-12-01 07:46:18 +01:00
|
|
|
start_desc.map[CompiledRegex::StartDesc::other] = true;
|
2017-11-25 11:14:15 +01:00
|
|
|
return node.quantifier.allows_none();
|
|
|
|
}
|
2020-07-25 06:07:57 +02:00
|
|
|
case ParsedRegex::CharType:
|
2017-11-25 11:14:15 +01:00
|
|
|
{
|
|
|
|
const CharacterType ctype = (CharacterType)node.value;
|
2018-04-29 08:42:46 +02:00
|
|
|
for (Codepoint cp = 0; cp < CompiledRegex::StartDesc::count; ++cp)
|
2017-11-25 11:14:15 +01:00
|
|
|
{
|
|
|
|
if (is_ctype(ctype, cp))
|
2017-12-01 07:46:18 +01:00
|
|
|
start_desc.map[cp] = true;
|
2017-11-25 11:14:15 +01:00
|
|
|
}
|
2017-12-01 07:46:18 +01:00
|
|
|
start_desc.map[CompiledRegex::StartDesc::other] = true;
|
2017-10-23 09:35:43 +02:00
|
|
|
return node.quantifier.allows_none();
|
2017-11-25 11:14:15 +01:00
|
|
|
}
|
2017-10-06 07:40:27 +02:00
|
|
|
case ParsedRegex::Sequence:
|
|
|
|
{
|
2018-12-19 13:21:47 +01:00
|
|
|
for (auto child : Children<direction>{m_parsed_regex, index})
|
|
|
|
{
|
|
|
|
if (not compute_start_desc<direction>(child, start_desc))
|
|
|
|
return node.quantifier.allows_none();
|
|
|
|
}
|
|
|
|
return true;
|
2017-10-06 07:40:27 +02:00
|
|
|
}
|
|
|
|
case ParsedRegex::Alternation:
|
|
|
|
{
|
2017-10-23 09:35:43 +02:00
|
|
|
bool all_consumed = not node.quantifier.allows_none();
|
2018-12-19 13:21:47 +01:00
|
|
|
for (auto child : Children<>{m_parsed_regex, index})
|
|
|
|
{
|
2018-02-09 12:35:12 +01:00
|
|
|
if (compute_start_desc<direction>(child, start_desc))
|
2017-10-06 07:40:27 +02:00
|
|
|
all_consumed = false;
|
2018-12-19 13:21:47 +01:00
|
|
|
}
|
2017-10-06 07:40:27 +02:00
|
|
|
return not all_consumed;
|
|
|
|
}
|
|
|
|
case ParsedRegex::LineStart:
|
|
|
|
case ParsedRegex::LineEnd:
|
|
|
|
case ParsedRegex::WordBoundary:
|
|
|
|
case ParsedRegex::NotWordBoundary:
|
|
|
|
case ParsedRegex::SubjectBegin:
|
|
|
|
case ParsedRegex::SubjectEnd:
|
|
|
|
case ParsedRegex::ResetStart:
|
|
|
|
case ParsedRegex::LookAhead:
|
|
|
|
case ParsedRegex::LookBehind:
|
2017-10-09 12:19:36 +02:00
|
|
|
case ParsedRegex::NegativeLookAhead:
|
2017-10-06 07:40:27 +02:00
|
|
|
case ParsedRegex::NegativeLookBehind:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-27 01:29:33 +01:00
|
|
|
template<RegexMode direction>
|
2017-10-09 15:04:28 +02:00
|
|
|
[[gnu::noinline]]
|
2018-02-09 12:35:12 +01:00
|
|
|
std::unique_ptr<CompiledRegex::StartDesc> compute_start_desc() const
|
2017-10-06 07:40:27 +02:00
|
|
|
{
|
2017-12-01 07:46:18 +01:00
|
|
|
CompiledRegex::StartDesc start_desc{};
|
2018-02-09 12:35:12 +01:00
|
|
|
if (compute_start_desc<direction>(0, start_desc) or
|
2017-12-01 07:46:18 +01:00
|
|
|
not contains(start_desc.map, false))
|
2017-10-08 06:22:52 +02:00
|
|
|
return nullptr;
|
|
|
|
|
2017-12-01 07:46:18 +01:00
|
|
|
return std::make_unique<CompiledRegex::StartDesc>(start_desc);
|
2017-10-06 07:40:27 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 11:10:14 +01:00
|
|
|
void optimize(size_t begin, size_t end)
|
|
|
|
{
|
|
|
|
if (not (m_flags & RegexCompileFlags::Optimize))
|
|
|
|
return;
|
|
|
|
|
2020-07-25 06:07:57 +02:00
|
|
|
auto is_jump = [](CompiledRegex::Op op) { return op >= CompiledRegex::Op::Jump and op <= CompiledRegex::Op::Split; };
|
2019-12-05 11:10:14 +01:00
|
|
|
for (auto i = begin; i < end; ++i)
|
|
|
|
{
|
|
|
|
auto& inst = m_program.instructions[i];
|
|
|
|
if (is_jump(inst.op))
|
2020-07-25 06:07:57 +02:00
|
|
|
m_program.instructions[inst.param.jump_target].last_step = 0xffff; // tag as jump target
|
2019-12-05 11:10:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto block_begin = begin; block_begin < end; )
|
|
|
|
{
|
|
|
|
auto block_end = block_begin + 1;
|
|
|
|
while (block_end < end and
|
|
|
|
not is_jump(m_program.instructions[block_end].op) and
|
|
|
|
m_program.instructions[block_end].last_step != 0xffff)
|
|
|
|
++block_end;
|
|
|
|
peephole_optimize(block_begin, block_end);
|
|
|
|
block_begin = block_end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void peephole_optimize(size_t begin, size_t end)
|
|
|
|
{
|
|
|
|
// Move saves after all assertions on the same character
|
2020-07-25 06:07:57 +02:00
|
|
|
auto is_assertion = [](CompiledRegex::Op op) { return op >= CompiledRegex::LineAssertion; };
|
2019-12-05 11:10:14 +01:00
|
|
|
for (auto i = begin, j = begin + 1; j < end; ++i, ++j)
|
|
|
|
{
|
|
|
|
if (m_program.instructions[i].op == CompiledRegex::Save and
|
|
|
|
is_assertion(m_program.instructions[j].op))
|
|
|
|
std::swap(m_program.instructions[i], m_program.instructions[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 09:45:43 +02:00
|
|
|
const ParsedRegex::Node& get_node(ParsedRegex::NodeIndex index) const
|
2017-10-23 09:35:43 +02:00
|
|
|
{
|
|
|
|
return m_parsed_regex.nodes[index];
|
|
|
|
}
|
|
|
|
|
2017-09-26 16:04:47 +02:00
|
|
|
CompiledRegex m_program;
|
2017-10-09 08:04:14 +02:00
|
|
|
RegexCompileFlags m_flags;
|
2017-11-27 17:59:57 +01:00
|
|
|
ParsedRegex& m_parsed_regex;
|
2017-09-26 16:04:47 +02:00
|
|
|
};
|
2017-09-17 10:50:53 +02:00
|
|
|
|
2018-04-27 00:34:49 +02:00
|
|
|
String dump_regex(const CompiledRegex& program)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2018-04-27 00:34:49 +02:00
|
|
|
String res;
|
2017-10-09 13:29:45 +02:00
|
|
|
int count = 0;
|
2017-10-07 12:51:32 +02:00
|
|
|
for (auto& inst : program.instructions)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2018-04-28 01:26:14 +02:00
|
|
|
char buf[20];
|
2018-04-27 00:34:49 +02:00
|
|
|
sprintf(buf, " %03d ", count++);
|
|
|
|
res += buf;
|
2017-10-07 12:51:32 +02:00
|
|
|
switch (inst.op)
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::Literal:
|
2020-07-25 06:07:57 +02:00
|
|
|
res += format("literal {}{}\n", inst.param.literal.ignore_case ? "(ignore case) " : "", inst.param.literal.codepoint);
|
2017-09-29 05:22:09 +02:00
|
|
|
break;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::AnyChar:
|
2018-04-27 00:34:49 +02:00
|
|
|
res += "any char\n";
|
2017-09-17 10:50:53 +02:00
|
|
|
break;
|
2018-06-24 12:13:35 +02:00
|
|
|
case CompiledRegex::AnyCharExceptNewLine:
|
|
|
|
res += "anything but newline\n";
|
|
|
|
break;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::Jump:
|
2020-07-25 06:07:57 +02:00
|
|
|
res += format("jump {}\n", inst.param.jump_target);
|
2017-09-17 10:50:53 +02:00
|
|
|
break;
|
2020-07-25 06:07:57 +02:00
|
|
|
case CompiledRegex::Split:
|
2017-09-17 10:50:53 +02:00
|
|
|
{
|
2018-04-27 00:34:49 +02:00
|
|
|
res += format("split (prioritize {}) {}\n",
|
2020-07-25 06:07:57 +02:00
|
|
|
(inst.param.split.prioritize_parent) ? "parent" : "child",
|
|
|
|
inst.param.split.target);
|
2017-09-17 10:50:53 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::Save:
|
2020-07-25 06:07:57 +02:00
|
|
|
res += format("save {}\n", inst.param.save_index);
|
2017-09-19 09:56:21 +02:00
|
|
|
break;
|
2020-07-25 06:07:57 +02:00
|
|
|
case CompiledRegex::CharClass:
|
|
|
|
res += format("character class {}\n", inst.param.character_class_index);
|
2017-11-25 11:14:15 +01:00
|
|
|
break;
|
2020-07-25 06:07:57 +02:00
|
|
|
case CompiledRegex::CharType:
|
|
|
|
res += format("character type {}\n", to_underlying(inst.param.character_type));
|
2017-09-23 09:18:21 +02:00
|
|
|
break;
|
2020-07-25 06:07:57 +02:00
|
|
|
case CompiledRegex::LineAssertion:
|
|
|
|
res += format("line {}\n", inst.param.line_start ? "start" : "end");;
|
2017-09-17 10:50:53 +02:00
|
|
|
break;
|
2020-07-25 06:07:57 +02:00
|
|
|
case CompiledRegex::SubjectAssertion:
|
|
|
|
res += format("subject {}\n", inst.param.subject_begin ? "begin" : "end");
|
2017-09-17 10:50:53 +02:00
|
|
|
break;
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::WordBoundary:
|
2020-07-25 06:07:57 +02:00
|
|
|
res += format("{}word boundary\n", inst.param.word_boundary_positive ? "" : "not ");
|
2017-09-18 05:47:10 +02:00
|
|
|
break;
|
2020-07-25 06:07:57 +02:00
|
|
|
case CompiledRegex::LookAround:
|
2017-09-28 14:43:45 +02:00
|
|
|
{
|
2020-07-25 06:07:57 +02:00
|
|
|
String name;
|
|
|
|
name += inst.param.lookaround.positive ? "" : "negative ";
|
|
|
|
name += "look ";
|
|
|
|
name += inst.param.lookaround.ahead ? "ahead " : "behind ";
|
|
|
|
if (inst.param.lookaround.ignore_case)
|
|
|
|
name += " (ignore case)";
|
2017-10-10 05:21:21 +02:00
|
|
|
|
2017-10-07 12:51:32 +02:00
|
|
|
String str;
|
2020-07-25 06:07:57 +02:00
|
|
|
for (auto it = program.lookarounds.begin() + inst.param.lookaround.index;
|
2018-10-10 13:35:38 +02:00
|
|
|
*it != CompiledRegex::Lookaround::EndOfLookaround; ++it)
|
|
|
|
utf8::dump(std::back_inserter(str), to_underlying(*it));
|
2018-04-27 00:34:49 +02:00
|
|
|
res += format("{} ({})\n", name, str);
|
2017-09-28 14:43:45 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-09-19 10:44:58 +02:00
|
|
|
case CompiledRegex::Match:
|
2018-04-27 00:34:49 +02:00
|
|
|
res += "match\n";
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-02 21:49:26 +01:00
|
|
|
auto dump_start_desc = [&](const CompiledRegex::StartDesc& desc, StringView name) {
|
2018-11-04 02:01:29 +01:00
|
|
|
res += name + " start desc: [";
|
|
|
|
for (size_t c = 0; c < CompiledRegex::StartDesc::count; ++c)
|
|
|
|
{
|
|
|
|
if (desc.map[c])
|
|
|
|
{
|
|
|
|
if (c < 32)
|
|
|
|
res += format("<0x{}>", Hex{c});
|
|
|
|
else
|
|
|
|
res += (char)c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res += "]\n";
|
|
|
|
};
|
|
|
|
if (program.forward_start_desc)
|
|
|
|
dump_start_desc(*program.forward_start_desc, "forward");
|
|
|
|
if (program.backward_start_desc)
|
|
|
|
dump_start_desc(*program.backward_start_desc, "backward");
|
2018-04-27 00:34:49 +02:00
|
|
|
return res;
|
2017-09-17 10:50:53 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 12:57:02 +01:00
|
|
|
CompiledRegex compile_regex(StringView re, RegexCompileFlags flags)
|
2017-09-26 09:44:30 +02:00
|
|
|
{
|
2017-12-01 12:57:02 +01:00
|
|
|
return RegexCompiler{RegexParser::parse(re), flags}.get_compiled_regex();
|
2017-09-26 09:44:30 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 11:14:15 +01:00
|
|
|
bool is_character_class(const CharacterClass& character_class, Codepoint cp)
|
|
|
|
{
|
|
|
|
if (character_class.ignore_case)
|
|
|
|
cp = to_lower(cp);
|
|
|
|
|
2020-07-30 11:51:25 +02:00
|
|
|
auto it = std::find_if(character_class.ranges.begin(),
|
|
|
|
character_class.ranges.end(),
|
|
|
|
[cp](auto& range) { return range.min <= cp and cp <= range.max; });
|
2017-11-25 11:14:15 +01:00
|
|
|
|
2020-07-30 11:51:25 +02:00
|
|
|
bool found = it != character_class.ranges.end() or (character_class.ctypes != CharacterType::None and
|
|
|
|
is_ctype(character_class.ctypes, cp));
|
2017-11-25 11:14:15 +01:00
|
|
|
return found != character_class.negative;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_ctype(CharacterType ctype, Codepoint cp)
|
|
|
|
{
|
2020-07-30 11:51:25 +02:00
|
|
|
auto check = [&](CharacterType bit, CharacterType not_bit, auto&& func) {
|
|
|
|
return (ctype & (bit | not_bit)) and func(cp) == (bool)(ctype & bit);
|
|
|
|
};
|
|
|
|
return check(CharacterType::Word, CharacterType::NotWord, [](Codepoint cp) { return is_word(cp); }) or
|
|
|
|
check(CharacterType::Whitespace, CharacterType::NotWhitespace, is_blank) or
|
|
|
|
check(CharacterType::HorizontalWhitespace, CharacterType::NotHorizontalWhitespace, is_horizontal_blank) or
|
|
|
|
check(CharacterType::Digit, CharacterType::NotDigit, iswdigit);
|
2017-11-25 11:14:15 +01:00
|
|
|
}
|
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
namespace
|
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
template<RegexMode mode = RegexMode::Forward>
|
|
|
|
struct TestVM : CompiledRegex, ThreadedRegexVM<const char*, mode>
|
2017-10-07 06:46:27 +02:00
|
|
|
{
|
2021-07-31 00:55:52 +02:00
|
|
|
TestVM(StringView re)
|
|
|
|
: CompiledRegex{compile_regex(re, mode & RegexMode::Forward ? RegexCompileFlags::None : RegexCompileFlags::Backward)},
|
|
|
|
TestVM::ThreadedRegexVM{static_cast<const CompiledRegex&>(*this)}
|
|
|
|
{}
|
2017-10-07 06:46:27 +02:00
|
|
|
|
2018-12-27 01:29:33 +01:00
|
|
|
bool exec(StringView re, RegexExecFlags flags = RegexExecFlags::None)
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2021-07-31 00:55:52 +02:00
|
|
|
return TestVM::ThreadedRegexVM::exec(re.begin(), re.end(), re.begin(), re.end(), flags);
|
2017-10-07 06:46:27 +02:00
|
|
|
}
|
2023-01-23 07:38:02 +01:00
|
|
|
|
|
|
|
using TestVM::ThreadedRegexVM::exec;
|
2017-10-07 06:46:27 +02:00
|
|
|
};
|
|
|
|
}
|
2017-09-26 16:16:35 +02:00
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
auto test_regex = UnitTest{[]{
|
2017-09-26 16:16:35 +02:00
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"(a*b)"};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("b"));
|
|
|
|
kak_assert(vm.exec("ab"));
|
|
|
|
kak_assert(vm.exec("aaab"));
|
|
|
|
kak_assert(not vm.exec("acb"));
|
2017-09-24 16:23:02 +02:00
|
|
|
kak_assert(not vm.exec("abc"));
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(not vm.exec(""));
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-17 13:12:46 +02:00
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"(^a.*b$)"};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("afoob"));
|
|
|
|
kak_assert(vm.exec("ab"));
|
|
|
|
kak_assert(not vm.exec("bab"));
|
|
|
|
kak_assert(not vm.exec(""));
|
2017-09-17 13:12:46 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 12:15:43 +02:00
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"(^(foo|qux|baz)+(bar)?baz$)"};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("fooquxbarbaz"));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == "qux");
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(not vm.exec("fooquxbarbaze"));
|
|
|
|
kak_assert(not vm.exec("quxbar"));
|
|
|
|
kak_assert(not vm.exec("blahblah"));
|
|
|
|
kak_assert(vm.exec("bazbaz"));
|
|
|
|
kak_assert(vm.exec("quxbaz"));
|
2017-09-17 12:15:43 +02:00
|
|
|
}
|
2017-09-18 04:56:14 +02:00
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"(.*\b(foo|bar)\b.*)"};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("qux foo baz"));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == "foo");
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(not vm.exec("quxfoobaz"));
|
|
|
|
kak_assert(vm.exec("bar"));
|
|
|
|
kak_assert(not vm.exec("foobar"));
|
2017-09-18 04:56:14 +02:00
|
|
|
}
|
2017-09-26 16:16:35 +02:00
|
|
|
|
2017-09-18 05:47:10 +02:00
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"((foo|bar))"};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("foo"));
|
|
|
|
kak_assert(vm.exec("bar"));
|
|
|
|
kak_assert(not vm.exec("foobar"));
|
2017-09-18 05:47:10 +02:00
|
|
|
}
|
2017-09-18 11:22:11 +02:00
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"(a{3,5}b)"};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(not vm.exec("aab"));
|
|
|
|
kak_assert(vm.exec("aaab"));
|
|
|
|
kak_assert(not vm.exec("aaaaaab"));
|
|
|
|
kak_assert(vm.exec("aaaaab"));
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"(a{3}b)"};
|
2017-09-26 13:44:01 +02:00
|
|
|
kak_assert(not vm.exec("aab"));
|
|
|
|
kak_assert(vm.exec("aaab"));
|
|
|
|
kak_assert(not vm.exec("aaaab"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"(a{3,}b)"};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(not vm.exec("aab"));
|
|
|
|
kak_assert(vm.exec("aaab"));
|
|
|
|
kak_assert(vm.exec("aaaaab"));
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"(a{,3}b)"};
|
2017-09-19 11:26:24 +02:00
|
|
|
kak_assert(vm.exec("b"));
|
|
|
|
kak_assert(vm.exec("ab"));
|
|
|
|
kak_assert(vm.exec("aaab"));
|
|
|
|
kak_assert(not vm.exec("aaaab"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Forward | RegexMode::Search> vm{R"(f.*a(.*o))"};
|
|
|
|
kak_assert(vm.exec("blahfoobarfoobaz"));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "foobarfoo");
|
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == "rfoo");
|
2018-12-27 01:29:33 +01:00
|
|
|
kak_assert(vm.exec("mais que fais la police"));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "fais la po");
|
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == " po");
|
2017-09-18 11:22:11 +02:00
|
|
|
}
|
2017-09-23 09:18:21 +02:00
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"([àb-dX-Z-]{3,5})"};
|
2017-09-26 16:37:21 +02:00
|
|
|
kak_assert(vm.exec("cà-Y"));
|
2017-09-25 15:24:24 +02:00
|
|
|
kak_assert(not vm.exec("àeY"));
|
|
|
|
kak_assert(vm.exec("dcbàX"));
|
2017-09-23 09:18:21 +02:00
|
|
|
kak_assert(not vm.exec("efg"));
|
|
|
|
}
|
2017-09-26 15:04:41 +02:00
|
|
|
|
2017-10-02 09:35:21 +02:00
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"((a{3,5})a+)"};
|
2017-10-06 16:28:49 +02:00
|
|
|
kak_assert(vm.exec("aaaaaa", RegexExecFlags::None));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == "aaaaa");
|
2017-10-02 09:35:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"((a{3,5}?)a+)"};
|
2017-10-06 16:28:49 +02:00
|
|
|
kak_assert(vm.exec("aaaaaa", RegexExecFlags::None));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == "aaa");
|
2017-10-02 09:35:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"((a{3,5}?)a)"};
|
2017-10-02 09:35:21 +02:00
|
|
|
kak_assert(vm.exec("aaaa"));
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:04:41 +02:00
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"(\d{3})"};
|
2017-09-26 15:04:41 +02:00
|
|
|
kak_assert(vm.exec("123"));
|
|
|
|
kak_assert(not vm.exec("1x3"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"([-\d]+)"};
|
2017-09-26 15:04:41 +02:00
|
|
|
kak_assert(vm.exec("123-456"));
|
|
|
|
kak_assert(not vm.exec("123_456"));
|
|
|
|
}
|
2017-09-26 16:51:05 +02:00
|
|
|
|
2017-09-27 08:04:05 +02:00
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"([ \H]+)"};
|
2017-09-27 08:04:05 +02:00
|
|
|
kak_assert(vm.exec("abc "));
|
|
|
|
kak_assert(not vm.exec("a \t"));
|
|
|
|
}
|
|
|
|
|
2017-09-26 16:51:05 +02:00
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"(\Q{}[]*+?\Ea+)"};
|
2017-09-26 16:51:05 +02:00
|
|
|
kak_assert(vm.exec("{}[]*+?aa"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"(\Q...)"};
|
2017-09-26 16:51:05 +02:00
|
|
|
kak_assert(vm.exec("..."));
|
|
|
|
kak_assert(not vm.exec("bla"));
|
|
|
|
}
|
2017-09-26 17:01:06 +02:00
|
|
|
|
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Forward> vm{R"(foo\Kbar)"};
|
|
|
|
kak_assert(vm.exec("foobar"));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "bar");
|
2018-12-27 01:29:33 +01:00
|
|
|
kak_assert(not vm.exec("bar"));
|
2017-09-26 17:01:06 +02:00
|
|
|
}
|
2022-02-02 04:51:04 +01:00
|
|
|
{
|
|
|
|
TestVM<RegexMode::Forward | RegexMode::Search> vm{R"(foobaz|foo|foobar)"};
|
|
|
|
kak_assert(vm.exec("foobar"));
|
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "foo");
|
|
|
|
}
|
2017-09-28 11:50:04 +02:00
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Forward> vm{R"((fo+?).*)"};
|
|
|
|
kak_assert(vm.exec("foooo"));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == "fo");
|
2017-09-28 11:50:04 +02:00
|
|
|
}
|
2017-09-28 14:43:45 +02:00
|
|
|
|
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Forward | RegexMode::Search> vm{R"((?=fo[\w]).)"};
|
|
|
|
kak_assert(vm.exec("barfoo"));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "f");
|
2017-09-28 14:43:45 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 03:22:24 +02:00
|
|
|
{
|
|
|
|
TestVM<> vm{R"((?<!f).)"};
|
|
|
|
kak_assert(vm.exec("f"));
|
|
|
|
}
|
|
|
|
|
2017-09-28 14:43:45 +02:00
|
|
|
{
|
2017-10-09 05:20:05 +02:00
|
|
|
TestVM<> vm{R"((?!f[oa]o)...)"};
|
2017-09-28 14:43:45 +02:00
|
|
|
kak_assert(not vm.exec("foo"));
|
|
|
|
kak_assert(vm.exec("qux"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-11-25 11:14:15 +01:00
|
|
|
TestVM<> vm{R"(...(?<=f\w.))"};
|
2017-09-28 14:43:45 +02:00
|
|
|
kak_assert(vm.exec("foo"));
|
|
|
|
kak_assert(not vm.exec("qux"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"(...(?<!foo))"};
|
2017-09-28 14:43:45 +02:00
|
|
|
kak_assert(not vm.exec("foo"));
|
|
|
|
kak_assert(vm.exec("qux"));
|
|
|
|
}
|
2017-09-29 05:22:09 +02:00
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"(Foo(?i)f[oB]+)"};
|
2017-09-29 05:22:09 +02:00
|
|
|
kak_assert(vm.exec("FooFOoBb"));
|
|
|
|
}
|
2017-10-02 07:49:08 +02:00
|
|
|
|
2020-02-06 21:37:29 +01:00
|
|
|
{
|
|
|
|
TestVM<> vm{R"((?i)[a-z]+)"};
|
|
|
|
kak_assert(vm.exec("ABC"));
|
|
|
|
}
|
|
|
|
|
2017-10-02 07:49:08 +02:00
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"([^\]]+)"};
|
2017-10-02 07:49:08 +02:00
|
|
|
kak_assert(not vm.exec("a]c"));
|
|
|
|
kak_assert(vm.exec("abc"));
|
2017-10-07 15:24:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TestVM<> vm{R"([^:\n]+)"};
|
|
|
|
kak_assert(not vm.exec("\nbc"));
|
|
|
|
kak_assert(vm.exec("abc"));
|
2017-10-02 07:49:08 +02:00
|
|
|
}
|
2017-10-04 16:30:22 +02:00
|
|
|
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
TestVM<> vm{R"((?:foo)+)"};
|
2017-10-04 16:30:22 +02:00
|
|
|
kak_assert(vm.exec("foofoofoo"));
|
|
|
|
kak_assert(not vm.exec("barbarbar"));
|
|
|
|
}
|
2017-10-06 07:40:27 +02:00
|
|
|
|
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Forward | RegexMode::Search> vm{R"((?<!\\)(?:\\\\)*")"};
|
|
|
|
kak_assert(vm.exec("foo\""));
|
2017-10-06 07:40:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Forward | RegexMode::Search> vm{R"($)"};
|
|
|
|
kak_assert(vm.exec("foo\n"));
|
2017-10-06 13:30:46 +02:00
|
|
|
kak_assert(*vm.captures()[0] == '\n');
|
2017-10-06 07:40:27 +02:00
|
|
|
}
|
2017-10-07 06:46:27 +02:00
|
|
|
|
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Backward | RegexMode::Search> vm{R"(fo{1,})"};
|
|
|
|
kak_assert(vm.exec("foo1fooo2"));
|
2017-10-07 06:46:27 +02:00
|
|
|
kak_assert(*vm.captures()[1] == '2');
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Backward | RegexMode::Search> vm{R"((?<=f)oo(b[ae]r)?(?=baz))"};
|
|
|
|
kak_assert(vm.exec("foobarbazfoobazfooberbaz"));
|
2017-10-07 06:46:27 +02:00
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "oober");
|
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == "ber");
|
|
|
|
}
|
2017-10-07 08:25:14 +02:00
|
|
|
|
2017-10-09 12:19:36 +02:00
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Backward | RegexMode::Search> vm{R"((baz|boz|foo|qux)(?<!baz)(?<!o))"};
|
|
|
|
kak_assert(vm.exec("quxbozfoobaz"));
|
2017-10-09 12:19:36 +02:00
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "boz");
|
|
|
|
}
|
|
|
|
|
2017-10-11 15:05:02 +02:00
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Backward | RegexMode::Search> vm{R"(foo)"};
|
|
|
|
kak_assert(vm.exec("foofoo"));
|
2017-10-11 15:05:02 +02:00
|
|
|
kak_assert(*vm.captures()[1] == 0);
|
|
|
|
}
|
|
|
|
|
2017-10-11 13:24:01 +02:00
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Backward | RegexMode::Search> vm{R"($)"};
|
|
|
|
kak_assert(vm.exec("foo\nbar\nbaz\nqux", RegexExecFlags::NotEndOfLine));
|
2017-10-11 13:24:01 +02:00
|
|
|
kak_assert(StringView{vm.captures()[0]} == "\nqux");
|
2018-12-27 01:29:33 +01:00
|
|
|
kak_assert(vm.exec("foo\nbar\nbaz\nqux", RegexExecFlags::None));
|
2018-11-03 03:52:40 +01:00
|
|
|
kak_assert(StringView{vm.captures()[0]} == "");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Backward | RegexMode::Search> vm{R"(^)"};
|
|
|
|
kak_assert(not vm.exec("foo", RegexExecFlags::NotBeginOfLine));
|
|
|
|
kak_assert(vm.exec("foo", RegexExecFlags::None));
|
|
|
|
kak_assert(vm.exec("foo\nbar", RegexExecFlags::None));
|
2018-11-03 03:52:40 +01:00
|
|
|
kak_assert(StringView{vm.captures()[0]} == "bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Backward | RegexMode::Search> vm{R"(\A\w+)"};
|
|
|
|
kak_assert(vm.exec("foo\nbar\nbaz", RegexExecFlags::None));
|
2018-11-03 03:52:40 +01:00
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Backward | RegexMode::Search> vm{R"(\b\w+\z)"};
|
|
|
|
kak_assert(vm.exec("foo\nbar\nbaz", RegexExecFlags::None));
|
2018-11-03 03:52:40 +01:00
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "baz");
|
2017-10-11 13:24:01 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 07:33:29 +01:00
|
|
|
{
|
|
|
|
TestVM<RegexMode::Backward | RegexMode::Search> vm{R"(a[^\n]*\n|\n)"};
|
|
|
|
kak_assert(vm.exec("foo\nbar\nb", RegexExecFlags::None));
|
|
|
|
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "ar\n");
|
|
|
|
}
|
|
|
|
|
2017-10-07 08:25:14 +02:00
|
|
|
{
|
|
|
|
TestVM<> vm{R"(()*)"};
|
|
|
|
kak_assert(not vm.exec(" "));
|
|
|
|
}
|
2017-10-08 03:22:24 +02:00
|
|
|
|
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Forward | RegexMode::Search> vm{R"(\b(?<!-)(a|b|)(?!-)\b)"};
|
|
|
|
kak_assert(vm.exec("# foo bar", RegexExecFlags::None));
|
2017-10-08 03:22:24 +02:00
|
|
|
kak_assert(*vm.captures()[0] == '#');
|
|
|
|
}
|
2017-10-08 13:03:38 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
TestVM<> vm{R"((?=))"};
|
|
|
|
kak_assert(vm.exec(""));
|
|
|
|
}
|
2017-10-10 05:21:21 +02:00
|
|
|
|
2017-10-13 04:58:09 +02:00
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Forward | RegexMode::Search> vm{R"((?i)FOO)"};
|
|
|
|
kak_assert(vm.exec("foo", RegexExecFlags::None));
|
2017-10-13 04:58:09 +02:00
|
|
|
}
|
|
|
|
|
2017-10-15 04:25:46 +02:00
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Forward | RegexMode::Search> vm{R"(.?(?=foo))"};
|
|
|
|
kak_assert(vm.exec("afoo", RegexExecFlags::None));
|
2017-10-15 04:25:46 +02:00
|
|
|
kak_assert(*vm.captures()[0] == 'a');
|
|
|
|
}
|
|
|
|
|
2017-10-10 05:21:21 +02:00
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Forward | RegexMode::Search> vm{R"((?i)(?=Foo))"};
|
|
|
|
kak_assert(vm.exec("fOO", RegexExecFlags::None));
|
2017-10-10 05:21:21 +02:00
|
|
|
kak_assert(*vm.captures()[0] == 'f');
|
|
|
|
}
|
2017-10-14 16:10:56 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
TestVM<> vm{R"([d-ea-dcf-k]+)"};
|
|
|
|
kak_assert(vm.exec("abcde"));
|
|
|
|
}
|
2017-10-20 06:08:24 +02:00
|
|
|
|
2017-11-12 18:12:05 +01:00
|
|
|
{
|
|
|
|
TestVM<> vm{R"((?i)[a-c]+)"};
|
|
|
|
kak_assert(vm.exec("bCa"));
|
|
|
|
}
|
|
|
|
|
2019-11-06 10:48:48 +01:00
|
|
|
{
|
|
|
|
TestVM<> vm{R"([\t-\r]+)"};
|
|
|
|
kak_assert(vm.exec("\t\n\v\f\r"));
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:29:43 +02:00
|
|
|
{
|
|
|
|
TestVM<> vm{R"([\t-\r]\h+[\t-\r])"};
|
|
|
|
kak_assert(vm.character_classes.size() == 1);
|
|
|
|
kak_assert(vm.exec("\n \f"));
|
|
|
|
}
|
|
|
|
|
2019-11-06 10:48:48 +01:00
|
|
|
{
|
|
|
|
TestVM<> vm{R"([^\x00-\x7F]+)"};
|
|
|
|
kak_assert(not vm.exec("ascii"));
|
|
|
|
kak_assert(vm.exec("←↑→↓"));
|
|
|
|
kak_assert(vm.exec("😄😊😉"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TestVM<> vm{R"([^\u000000-\u00ffff]+)"};
|
|
|
|
kak_assert(not vm.exec("ascii"));
|
|
|
|
kak_assert(not vm.exec("←↑→↓"));
|
|
|
|
kak_assert(vm.exec("😄😊😉"));
|
|
|
|
}
|
|
|
|
|
2017-10-20 12:26:33 +02:00
|
|
|
{
|
2018-12-27 01:29:33 +01:00
|
|
|
TestVM<RegexMode::Forward | RegexMode::Search> vm{R"(д)"};
|
|
|
|
kak_assert(vm.exec("д", RegexExecFlags::None));
|
2017-10-20 12:26:33 +02:00
|
|
|
}
|
|
|
|
|
2023-01-23 07:38:02 +01:00
|
|
|
{
|
|
|
|
TestVM<RegexMode::Forward | RegexMode::Search> vm{"ab"};
|
|
|
|
const char str[] = "fa😄ab";
|
|
|
|
kak_assert(not vm.exec(str, str+4, str, str + sizeof(str)-1, RegexExecFlags::None));
|
|
|
|
}
|
|
|
|
|
2017-10-20 06:08:24 +02:00
|
|
|
{
|
2019-11-06 10:48:48 +01:00
|
|
|
TestVM<> vm{R"(\0\x0A\u00260e\u00260F)"};
|
2017-10-20 06:08:24 +02:00
|
|
|
const char str[] = "\0\n☎☏"; // work around the null byte in the literal
|
|
|
|
kak_assert(vm.exec({str, str + sizeof(str)-1}));
|
|
|
|
}
|
2019-01-03 12:52:15 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
auto eq = [](const CompiledRegex::NamedCapture& lhs,
|
|
|
|
const CompiledRegex::NamedCapture& rhs) {
|
|
|
|
return lhs.name == rhs.name and
|
|
|
|
lhs.index == rhs.index;
|
|
|
|
};
|
|
|
|
|
|
|
|
TestVM<> vm{R"((?<year>\d+)-(?<month>\d+)-(?<day>\d+))"};
|
|
|
|
kak_assert(vm.exec("2019-01-03", RegexExecFlags::None));
|
|
|
|
kak_assert(StringView{vm.captures()[2], vm.captures()[3]} == "2019");
|
|
|
|
kak_assert(StringView{vm.captures()[4], vm.captures()[5]} == "01");
|
|
|
|
kak_assert(StringView{vm.captures()[6], vm.captures()[7]} == "03");
|
|
|
|
kak_assert(vm.named_captures.size() == 3);
|
|
|
|
kak_assert(eq(vm.named_captures[0], {"year", 1}));
|
|
|
|
kak_assert(eq(vm.named_captures[1], {"month", 2}));
|
|
|
|
kak_assert(eq(vm.named_captures[2], {"day", 3}));
|
|
|
|
}
|
2017-09-17 10:50:53 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
}
|